class Section(object):
""" Base class for ELF sections. Also used for all sections types that have
no special functionality.
-
+
Allows dictionary-like access to the section header. For example:
> sec = Section(...)
> sec['sh_type'] # section type
self.header = header
self.name = name
self.stream = stream
-
+
def data(self):
""" The section data from the file.
"""
""" Is this a null section?
"""
return False
-
+
def __getitem__(self, name):
""" Implement dict-like access to header entries
"""
def is_null(self):
return True
-
+
class StringTableSection(Section):
""" ELF string table section.
"""
def __init__(self, header, name, stream):
super(StringTableSection, self).__init__(header, name, stream)
-
+
def get_string(self, offset):
""" Get the string stored at the given offset in this string table.
"""
""" Number of symbols in the table
"""
return self['sh_size'] // self['sh_entsize']
-
+
def get_symbol(self, n):
""" Get the symbol at index #n from the table (Symbol object)
"""
return self['sh_size'] // self['sh_entsize'] - 1
def get_symbol(self, n):
- """ Get the symbol at index #n from the table (Symbol object)
+ """ Get the symbol at index #n from the table (Symbol object).
It begins at 1 and not 0 since the first entry is used to
- store the current version of the syminfo table
+ store the current version of the syminfo table.
"""
# Grab the symbol's entry from the stream
entry_offset = self['sh_offset'] + n * self['sh_entsize']
entry = struct_parse(
- self.elfstructs.Elf_Syminfo,
+ self.elfstructs.Elf_Sunw_Syminfo,
self.stream,
stream_pos=entry_offset)
# Find the symbol name in the associated symbol table
class ELFStructs(object):
""" Accessible attributes:
-
+
Elf_{byte|half|word|word64|addr|offset|sword|xword|xsword}:
- Data chunks, as specified by the ELF standard, adjusted for
+ Data chunks, as specified by the ELF standard, adjusted for
correct endianness and word-size.
Elf_Ehdr:
ELF file header
-
+
Elf_Phdr:
Program header
-
+
Elf_Shdr:
Section header
-
+
Elf_Sym:
Symbol table entry
def __init__(self, little_endian=True, elfclass=32):
assert elfclass == 32 or elfclass == 64
self.little_endian = little_endian
- self.elfclass = elfclass
+ self.elfclass = elfclass
self._create_structs()
-
+
def _create_structs(self):
if self.little_endian:
self.Elf_byte = ULInt8
self.Elf_sword = SBInt32
self.Elf_xword = UBInt32 if self.elfclass == 32 else UBInt64
self.Elf_sxword = SBInt32 if self.elfclass == 32 else SBInt64
-
+
self._create_ehdr()
self._create_phdr()
self._create_shdr()
self._create_sym()
self._create_rel()
self._create_dyn()
- self._create_syminfo()
-
+ self._create_sunw_syminfo()
+
def _create_ehdr(self):
self.Elf_Ehdr = Struct('Elf_Ehdr',
Struct('e_ident',
self.Elf_half('e_shnum'),
self.Elf_half('e_shstrndx'),
)
-
+
def _create_phdr(self):
if self.elfclass == 32:
self.Elf_Phdr = Struct('Elf_Phdr',
self.Elf_xword('p_filesz'),
self.Elf_xword('p_memsz'),
self.Elf_xword('p_align'),
- )
-
+ )
+
def _create_shdr(self):
self.Elf_Shdr = Struct('Elf_Shdr',
self.Elf_word('sh_name'),
self.Elf_xword('sh_addralign'),
self.Elf_xword('sh_entsize'),
)
-
+
def _create_rel(self):
# r_info is also taken apart into r_info_sym and r_info_type.
# This is done in Value to avoid endianity issues while parsing.
self.Elf_xword('st_size'),
)
- def _create_syminfo(self):
- self.Elf_Syminfo = Struct('Elf_Syminfo',
- Enum(self.Elf_half('si_boundto'), **ENUM_SYMINFO_BOUNDTO),
+ def _create_sunw_syminfo(self):
+ self.Elf_Sunw_Syminfo = Struct('Elf_Sunw_Syminfo',
+ Enum(self.Elf_half('si_boundto'), **ENUM_SUNW_SYMINFO_BOUNDTO),
self.Elf_half('si_flags'),
)