class ELFTools::Sections::SymTabSection

Class of symbol table section. Usually for section .symtab and .dynsym, which will refer to symbols in ELF file.

Public Class Methods

new(header, stream, section_at: nil, **_kwargs) click to toggle source

Instantiate a {SymTabSection} object. There's a section_at lambda for {SymTabSection} to easily fetch other sections. @param [ELFTools::Structs::ELF_Shdr] header

See {Section#initialize} for more information.

@param [#pos=, read] stream

See {Section#initialize} for more information.

@param [Proc] section_at

The method for fetching other sections by index.
This lambda should be {ELFTools::ELFFile#section_at}.
Calls superclass method ELFTools::Sections::Section::new
# File lib/elftools/sections/sym_tab_section.rb, line 21
def initialize(header, stream, section_at: nil, **_kwargs)
  @section_at = section_at
  # For faster #symbol_by_name
  super
end

Public Instance Methods

each_symbols(&block) click to toggle source

Iterate all symbols.

All symbols are lazy loading, the symbol only be created whenever accessing it. This method is useful for {#symbol_by_name} since not all symbols need to be created. @yieldparam [ELFTools::Sections::Symbol] sym A symbol object. @yieldreturn [void] @return [Enumerator<ELFTools::Sections::Symbol>, Array<ELFTools::Sections::Symbol>]

If block is not given, an enumerator will be returned.
Otherwise return array of symbols.
# File lib/elftools/sections/sym_tab_section.rb, line 59
def each_symbols(&block)
  return enum_for(:each_symbols) unless block_given?

  Array.new(num_symbols) do |i|
    symbol_at(i).tap(&block)
  end
end
num_symbols() click to toggle source

Number of symbols. @return [Integer] The number. @example

symtab.num_symbols
#=> 75
# File lib/elftools/sections/sym_tab_section.rb, line 32
def num_symbols
  header.sh_size / header.sh_entsize
end
symbol_at(n) click to toggle source

Acquire the n-th symbol, 0-based.

Symbols are lazy loaded. @param [Integer] n The index. @return [ELFTools::Sections::Symbol, nil]

The target symbol.
If +n+ is out of bound, +nil+ is returned.
# File lib/elftools/sections/sym_tab_section.rb, line 43
def symbol_at(n)
  @symbols ||= LazyArray.new(num_symbols, &method(:create_symbol))
  @symbols[n]
end
symbol_by_name(name) click to toggle source

Get symbol by its name. @param [String] name

The name of symbol.

@return [ELFTools::Sections::Symbol] Desired symbol.

# File lib/elftools/sections/sym_tab_section.rb, line 78
def symbol_by_name(name)
  each_symbols.find { |symbol| symbol.name == name }
end
symbols() click to toggle source

Simply use {#symbols} to get all symbols. @return [Array<ELFTools::Sections::Symbol>]

The whole symbols.
# File lib/elftools/sections/sym_tab_section.rb, line 70
def symbols
  each_symbols.to_a
end
symstr() click to toggle source

Return the symbol string section. Lazy loaded. @return [ELFTools::Sections::StrTabSection] The string table section.

# File lib/elftools/sections/sym_tab_section.rb, line 85
def symstr
  @symstr ||= @section_at.call(header.sh_link)
end

Private Instance Methods

create_symbol(n) click to toggle source
# File lib/elftools/sections/sym_tab_section.rb, line 91
def create_symbol(n)
  stream.pos = header.sh_offset + n * header.sh_entsize
  sym = Structs::ELF_sym[header.elf_class].new(endian: header.class.self_endian, offset: stream.pos)
  sym.read(stream)
  Symbol.new(sym, stream, symstr: method(:symstr))
end