Module: ELFTools::Dynamic

Included in:
Sections::DynamicSection, Segments::DynamicSegment
Defined in:
lib/elftools/dynamic.rb

Overview

Note:

This module can only be included by Sections::DynamicSection and Segments::DynamicSegment because methods here assume some attributes exist.

Define common methods for dynamic sections and dynamic segments.

Defined Under Namespace

Classes: Tag

Instance Method Summary collapse

Instance Method Details

#each_tags {|tag| ... } ⇒ Enumerator<ELFTools::Dynamic::Tag>, Array<ELFTools::Dynamic::Tag>

Note:

This method assume the following methods already exist:

header
tag_start

Iterate all tags.

Yield Parameters:

Returns:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/elftools/dynamic.rb', line 21

def each_tags(&block)
  return enum_for(:each_tags) unless block_given?

  arr = []
  0.step do |i|
    tag = tag_at(i).tap(&block)
    arr << tag
    break if tag.header.d_tag == ELFTools::Constants::DT_NULL
  end
  arr
end

#tag_at(n) ⇒ ELFTools::Dynamic::Tag

Note:

This method assume the following methods already exist:

header
tag_start
Note:

We cannot do bound checking of n here since the only way to get size of tags is calling tags.size.

Get the n-th tag.

Tags are lazy loaded.

Parameters:

  • n (Integer)

    The index.

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/elftools/dynamic.rb', line 93

def tag_at(n)
  return if n.negative?

  @tag_at_map ||= {}
  return @tag_at_map[n] if @tag_at_map[n]

  dyn = Structs::ELF_Dyn.new(endian:)
  dyn.elf_class = header.elf_class
  stream.pos = tag_start + n * dyn.num_bytes
  dyn.offset = stream.pos
  @tag_at_map[n] = Tag.new(dyn.read(stream), stream, method(:str_offset))
end

#tag_by_type(type) ⇒ ELFTools::Dynamic::Tag

Get a tag of specific type.

Examples:

dynamic = elf.segment_by_type(:dynamic)
# type as integer
dynamic.tag_by_type(0) # the null tag
#=>  #<ELFTools::Dynamic::Tag:0x0055b5a5ecad28 @header={:d_tag=>0, :d_val=>0}>
dynamic.tag_by_type(ELFTools::Constants::DT_NULL)
#=>  #<ELFTools::Dynamic::Tag:0x0055b5a5ecad28 @header={:d_tag=>0, :d_val=>0}>

# symbol
dynamic.tag_by_type(:null)
#=>  #<ELFTools::Dynamic::Tag:0x0055b5a5ecad28 @header={:d_tag=>0, :d_val=>0}>
dynamic.tag_by_type(:pltgot)
#=> #<ELFTools::Dynamic::Tag:0x0055d3d2d91b28 @header={:d_tag=>3, :d_val=>6295552}>

# string
dynamic.tag_by_type('null')
#=>  #<ELFTools::Dynamic::Tag:0x0055b5a5ecad28 @header={:d_tag=>0, :d_val=>0}>
dynamic.tag_by_type('DT_PLTGOT')
#=> #<ELFTools::Dynamic::Tag:0x0055d3d2d91b28 @header={:d_tag=>3, :d_val=>6295552}>

Parameters:

  • type (Integer, Symbol, String)

    Constant value, symbol, or string of type is acceptable. See examples for more information.

Returns:



64
65
66
67
# File 'lib/elftools/dynamic.rb', line 64

def tag_by_type(type)
  type = Util.to_constant(Constants::DT, type)
  each_tags.find { |tag| tag.header.d_tag == type }
end

#tagsArray<ELFTools::Dynamic::Tag>

Use #tags to get all tags.

Returns:



36
37
38
# File 'lib/elftools/dynamic.rb', line 36

def tags
  @tags ||= each_tags.to_a
end

#tags_by_type(type) ⇒ Array<ELFTools::Dynamic::Tag>

Get tags of specific type.

Parameters:

  • type (Integer, Symbol, String)

    Constant value, symbol, or string of type is acceptable. See examples for more information.

Returns:

See Also:



76
77
78
79
# File 'lib/elftools/dynamic.rb', line 76

def tags_by_type(type)
  type = Util.to_constant(Constants::DT, type)
  each_tags.select { |tag| tag.header.d_tag == type }
end