Class: ELFTools::Sections::Symbol

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

Overview

Class of symbol.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, stream, symstr: nil, machine: nil, version: nil) ⇒ Symbol

Instantiate a ELFTools::Sections::Symbol object.

Parameters:

  • header (ELFTools::Structs::ELF32_sym, ELFTools::Structs::ELF64_sym)

    The symbol header.

  • stream (#pos=, #read)

    The streaming object.

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

    The symbol string section. If Proc is given, it will be called at the first time access #name.

  • machine (Integer) (defaults to: nil)

    The machine of the ELF file, which a name of a value depends on.

  • version (Proc) (defaults to: nil)

    Call this to get the version this symbol binds to, which only the symbols a file is loaded by have.



26
27
28
29
30
31
32
# File 'lib/elftools/sections/symbol.rb', line 26

def initialize(header, stream, symstr: nil, machine: nil, version: nil)
  @header = header
  @stream = stream
  @symstr = symstr
  @machine = machine
  @version = version
end

Instance Attribute Details

#headerELFTools::Structs::ELF32_sym, ELFTools::Structs::ELF64_sym (readonly)

Returns Section header.



10
11
12
# File 'lib/elftools/sections/symbol.rb', line 10

def header
  @header
end

#stream#pos=, #read (readonly)

Returns Streaming object.

Returns:

  • (#pos=, #read)

    Streaming object.



11
12
13
# File 'lib/elftools/sections/symbol.rb', line 11

def stream
  @stream
end

Instance Method Details

#bindInteger

How this symbol is linked against others with the same name.

The available bindings are listed in Constants::STB.

Examples:

symbol.bind == ELFTools::Constants::STB_GLOBAL
#=> true

Returns:

  • (Integer)

    The binding.



124
125
126
# File 'lib/elftools/sections/symbol.rb', line 124

def bind
  header.st_info >> 4
end

#bind=(bind) ⇒ Object

Sets how this symbol is linked against others with the same name.

Examples:

symbol.bind = ELFTools::Constants::STB_WEAK

Parameters:

  • bind (Integer)

    The binding.

Raises:

  • (ArgumentError)

    If the four bits recording it cannot hold it.



133
134
135
# File 'lib/elftools/sections/symbol.rb', line 133

def bind=(bind)
  header.st_info = (Util.fits!(bind, 4, 'Symbol binding') << 4) | type
end

#bind_nameString

The name of #bind.

Examples:

symbol.bind_name
#=> 'STB_GLOBAL'

Returns:

  • (String)

    The name.



142
143
144
# File 'lib/elftools/sections/symbol.rb', line 142

def bind_name
  Constants::STB.mapping(@machine, bind)
end

#nameString

Return the symbol name.

Returns:

  • (String)

    The name.



36
37
38
# File 'lib/elftools/sections/symbol.rb', line 36

def name
  @name ||= @symstr.call.name_at(header.st_name)
end

#section_indexInteger

The index of the section this symbol is defined in.

Values in Constants::SHN have special meanings instead of being an index.

Examples:

symbol.section_index == ELFTools::Constants::SHN_UNDEF
#=> true # the symbol is undefined and to be resolved at runtime

Returns:

  • (Integer)

    The section index.



197
198
199
# File 'lib/elftools/sections/symbol.rb', line 197

def section_index
  header.st_shndx.to_i
end

#sizeInteger

How many bytes what this symbol names takes.

Examples:

elf.section_by_name('.symtab').symbol_by_name('main').size
#=> 142

Returns:

  • (Integer)

    The number, zero where the file records none.



81
82
83
# File 'lib/elftools/sections/symbol.rb', line 81

def size
  header.st_size.to_i
end

#typeInteger

What kind of entity this symbol refers to.

The available types are listed in Constants::STT.

Examples:

symbol.type == ELFTools::Constants::STT_FUNC
#=> true

Returns:

  • (Integer)

    The type.



92
93
94
# File 'lib/elftools/sections/symbol.rb', line 92

def type
  header.st_info & 0xf
end

#type=(type) ⇒ Object

Sets what kind of entity this symbol refers to.

Examples:

symbol.type = ELFTools::Constants::STT_FUNC

Parameters:

  • type (Integer)

    The type.

Raises:

  • (ArgumentError)

    If the four bits recording it cannot hold it.



101
102
103
# File 'lib/elftools/sections/symbol.rb', line 101

def type=(type)
  header.st_info = (bind << 4) | Util.fits!(type, 4, 'Symbol type')
end

#type_nameString

The name of #type.

A machine names types of its own, so the name is only known when the machine of the file is.

Examples:

symbol.type_name
#=> 'STT_FUNC'

Returns:

  • (String)

    The name.



113
114
115
# File 'lib/elftools/sections/symbol.rb', line 113

def type_name
  Constants::STT.mapping(@machine, type)
end

#valueInteger

What this symbol is worth, which for most of them is the address of what they name.

A symbol of a file that is not loaded anywhere records an offset into the section holding it instead, and one the linker is still to place, which Constants::SHN::SHN_COMMON marks, records the alignment it needs. The ABI leaves the field to the kind of symbol for that reason, and this answers with what is recorded either way.

Examples:

elf.section_by_name('.symtab').symbol_by_name('main').value
#=> 4196061 # 0x4006dd

Returns:

  • (Integer)

    The value.



72
73
74
# File 'lib/elftools/sections/symbol.rb', line 72

def value
  header.st_value.to_i
end

#versionString?

The version this symbol binds to.

Only the symbols a file is loaded by have one, and only where the file records the versions at all. #name is left as the file records it, without the version appended.

Examples:

elf.dynamic.symbol_by_name('printf').version
#=> 'GLIBC_2.2.5'

Returns:

  • (String, nil)

    The name of the version.



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

def version
  binding_version&.name
end

#version_hidden?Boolean

Whether #version is one the symbol asks for by name rather than the default one of its name.

Returns:

  • (Boolean)

    The answer.



56
57
58
# File 'lib/elftools/sections/symbol.rb', line 56

def version_hidden?
  binding_version&.hidden? || false
end

#visibilityInteger

How this symbol is accessed once it becomes part of an executable or shared object.

The available visibilities are listed in Constants::STV.

Examples:

symbol.visibility == ELFTools::Constants::STV_HIDDEN
#=> true

Returns:

  • (Integer)

    The visibility.



154
155
156
# File 'lib/elftools/sections/symbol.rb', line 154

def visibility
  header.st_other & 0x3
end

#visibility=(visibility) ⇒ Object

Sets how this symbol is accessed once it becomes part of an executable or shared object.

The rest of st_other is left alone, which some machines record their own thing in.

Examples:

symbol.visibility = ELFTools::Constants::STV_HIDDEN

Parameters:

  • visibility (Integer)

    The visibility.

Raises:

  • (ArgumentError)

    If the two bits recording it cannot hold it.



167
168
169
# File 'lib/elftools/sections/symbol.rb', line 167

def visibility=(visibility)
  header.st_other = (header.st_other.to_i & 0xfc) | Util.fits!(visibility, 2, 'Symbol visibility')
end

#visibility_nameString

The name of #visibility.

Examples:

symbol.visibility_name
#=> 'STV_DEFAULT'

Returns:

  • (String)

    The name.



176
177
178
# File 'lib/elftools/sections/symbol.rb', line 176

def visibility_name
  Constants::STV.mapping(@machine, visibility)
end