Class: ELFTools::Sections::Section

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

Overview

Base class of sections.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, stream, offset_from_vma: nil, strtab: nil, **_kwargs) ⇒ Section

Instantiate a ELFTools::Sections::Section object.

Parameters:

  • header (ELFTools::Structs::ELF_Shdr)

    The section header object.

  • stream (#pos=, #read)

    The streaming object for further dump.

  • strtab (ELFTools::Sections::StrTabSection, Proc) (defaults to: nil)

    The string table object. For fetching section names. If Proc if given, it will call at the first time access #name.

  • offset_from_vma (Method) (defaults to: nil)

    The method to get offset of file, given virtual memory address.



22
23
24
25
26
27
# File 'lib/elftools/sections/section.rb', line 22

def initialize(header, stream, offset_from_vma: nil, strtab: nil, **_kwargs)
  @header = header
  @stream = stream
  @strtab = strtab
  @offset_from_vma = offset_from_vma
end

Instance Attribute Details

#headerELFTools::Structs::ELF_Shdr (readonly)

Returns Section header.

Returns:



8
9
10
# File 'lib/elftools/sections/section.rb', line 8

def header
  @header
end

#stream#pos=, #read (readonly)

Returns Streaming object.

Returns:

  • (#pos=, #read)

    Streaming object.



9
10
11
# File 'lib/elftools/sections/section.rb', line 9

def stream
  @stream
end

Class Method Details

.create(header, stream, *args, **kwargs) ⇒ ELFTools::Sections::Section

Use different class according to header.sh_type.

Parameters:

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elftools/sections/sections.rb', line 24

def create(header, stream, *args, **kwargs)
  klass = case header.sh_type
          when Constants::SHT_DYNAMIC then DynamicSection
          when Constants::SHT_NULL then NullSection
          when Constants::SHT_NOTE then NoteSection
          when Constants::SHT_RELA, Constants::SHT_REL then RelocationSection
          when Constants::SHT_STRTAB then StrTabSection
          when Constants::SHT_SYMTAB, Constants::SHT_DYNSYM then SymTabSection
          else Section
          end
  klass.new(header, stream, *args, **kwargs)
end

Instance Method Details

#dataString

Fetch data of this section.

Returns:

  • (String)

    Data.



44
45
46
47
# File 'lib/elftools/sections/section.rb', line 44

def data
  stream.pos = header.sh_offset
  stream.read(header.sh_size)
end

#nameString

Get name of this section.

Returns:

  • (String)

    The name.



38
39
40
# File 'lib/elftools/sections/section.rb', line 38

def name
  @name ||= @strtab.call.name_at(header.sh_name)
end

#null?Boolean

Is this a null section?

Returns:

  • (Boolean)

    No it’s not.



51
52
53
# File 'lib/elftools/sections/section.rb', line 51

def null?
  false
end

#typeInteger

Return header.sh_type in a simplier way.

Returns:

  • (Integer)

    The type, meaning of types are defined in Constants::SHT.



32
33
34
# File 'lib/elftools/sections/section.rb', line 32

def type
  header.sh_type.to_i
end