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
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}.
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
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
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
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
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
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
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
# 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