Module: ELFTools::Dynamic

Includes:
Symbols
Included in:
Sections::DynamicSection, Segments::DynamicSegment
Defined in:
lib/elftools/dynamic.rb,
lib/elftools/dynamic/tag.rb,
lib/elftools/dynamic/symbols.rb,
lib/elftools/dynamic/hash_table.rb,
lib/elftools/dynamic/string_table.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

Modules: Symbols Classes: HashTable, StringTable, Tag

Instance Method Summary collapse

Methods included from Symbols

#each_symbol, #num_symbols, #symbol_at, #symbol_by_name, #symbols

Instance Method Details

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

Note:

This method assume the following methods already exist:

header
tag_start

Iterate all tags.

Yield Parameters:

Returns:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elftools/dynamic.rb', line 31

def each_tag(&block)
  return enum_for(:each_tag) 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

#relocationsArray<ELFTools::Relocation>

The relocations the tags point at.

Two tables record them: the one DT_REL or DT_RELA names, and the one DT_JMPREL names, whose entries are of the kind DT_PLTREL names.

Examples:

elf.dynamic.relocations.map(&:type_name).uniq
#=> ['R_X86_64_GLOB_DAT', 'R_X86_64_JUMP_SLOT']

Returns:

Raises:



130
131
132
# File 'lib/elftools/dynamic.rb', line 130

def relocations
  @relocations ||= relocation_tables.flat_map { |start, size, rela| read_relocations(start, size, rela) }
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:



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/elftools/dynamic.rb', line 106

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, string_table)
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:



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

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

#tagsArray<ELFTools::Dynamic::Tag>

Use #tags to get all tags.

Returns:



49
50
51
# File 'lib/elftools/dynamic.rb', line 49

def tags
  @tags ||= each_tag.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:



89
90
91
92
# File 'lib/elftools/dynamic.rb', line 89

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