Class: ELFTools::Sections::RelocationSection

Inherits:
Section
  • Object
show all
Defined in:
lib/elftools/sections/relocation_section.rb

Overview

Class of relocation section. Usually for sections .rel.* and .rela.*, which record relocations in ELF file.

Instance Attribute Summary

Attributes inherited from Section

#header, #stream

Instance Method Summary collapse

Methods inherited from Section

create, #data, #name, #null?, #type

Constructor Details

#initialize(header, stream, machine: nil, **_kwargs) ⇒ RelocationSection

Parameters:

  • header (ELFTools::Structs::ELF_Shdr)

    See Section#initialize for more information.

  • stream (#pos=, #read)

    See Section#initialize for more information.

  • machine (Integer) (defaults to: nil)

    The machine of the ELF file, which decides what a relocation type means. This should be e_machine of the ELF header.



22
23
24
25
# File 'lib/elftools/sections/relocation_section.rb', line 22

def initialize(header, stream, machine: nil, **_kwargs)
  @machine = machine
  super
end

Instance Method Details

#each_relocation {|rel| ... } ⇒ Enumerator<ELFTools::Relocation>, Array<ELFTools::Relocation> Also known as: each_relocations

Iterate all relocations.

All relocations are lazy loading, the relocation only be created whenever accessing it.

Yield Parameters:

Yield Returns:

  • (void)

Returns:



60
61
62
63
64
65
66
# File 'lib/elftools/sections/relocation_section.rb', line 60

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

  Array.new(num_relocations) do |i|
    relocation_at(i).tap(&block)
  end
end

#num_relocationsInteger

Number of relocations in this section.

Returns:

  • (Integer)

    The number.



35
36
37
# File 'lib/elftools/sections/relocation_section.rb', line 35

def num_relocations
  header.sh_size / header.sh_entsize
end

#rela?Boolean

Is this relocation a RELA or REL type.

Returns:

  • (Boolean)

    If is RELA.



29
30
31
# File 'lib/elftools/sections/relocation_section.rb', line 29

def rela?
  header.sh_type == Constants::SHT_RELA
end

#relocation_at(n) ⇒ ELFTools::Relocation?

Acquire the +n+-th relocation, 0-based.

relocations are lazy loaded.

Parameters:

  • n (Integer)

    The index.

Returns:



46
47
48
49
# File 'lib/elftools/sections/relocation_section.rb', line 46

def relocation_at(n)
  @relocations ||= LazyArray.new(num_relocations, &method(:create_relocation))
  @relocations[n]
end

#relocationsArray<ELFTools::Relocation>

Simply use #relocations to get all relocations.

Returns:



74
75
76
# File 'lib/elftools/sections/relocation_section.rb', line 74

def relocations
  each_relocation.to_a
end