mate.build.tob_chess_utils.elf module

A utility model containing functions and classes related to parsing and interpreting ELF information.

class mate.build.tob_chess_utils.elf.VTable(va: int, size: int, symbol: str, class_name: str, rtti_va: Optional[int], members: List[int])

Bases: object

Represents a C++ class’s virtual table.

The fields in this dataclass should be kept up-to-date with the JSON Schema for the “VTable” CPG node.

Parameters
  • va (int) –

  • size (int) –

  • symbol (str) –

  • class_name (str) –

  • rtti_va (Optional[int]) –

  • members (List[int]) –

Return type

None

class_name: str

The name of the C++ class that this vtable is for.

members: List[int]

All of the members of this vtable, as virtual addresses.

rtti_va: Optional[int]

The virtual address to any RTTI information, if present.

size: int

The vtable’s size, in bytes. Includes non-member fields, like the RTTI and “offset to base” field.

symbol: str

The virtual table’s symbol.

va: int

The vtable’s virtual address.

mate.build.tob_chess_utils.elf.all_vtables(elf: elftools.elf.elffile.ELFFile) List[mate.build.tob_chess_utils.elf.VTable]

Returns a list of all virtual tables in the binary.

Parameters

elf (elftools.elf.elffile.ELFFile) –

Return type

List[mate.build.tob_chess_utils.elf.VTable]

mate.build.tob_chess_utils.elf.elf_address_width(elf: elftools.elf.elffile.ELFFile) Tuple[int, str]

Returns an appropriate address/pointer width for this ELFFile.

Parameters

elf (elftools.elf.elffile.ELFFile) –

Return type

Tuple[int, str]

mate.build.tob_chess_utils.elf.elf_inner_stream(elf: elftools.elf.elffile.ELFFile) Iterator[BinaryIO]

Yield the given ELFFile’s inner stream, wrapped such that any reads or seeks against the stream are reset at the context manager’s close.

Parameters

elf (elftools.elf.elffile.ELFFile) –

Return type

Iterator[BinaryIO]

mate.build.tob_chess_utils.elf.get_global_va(elf: elftools.elf.elffile.ELFFile, symbol: elftools.elf.sections.Symbol) int

Returns the virtual address for the given (global variable) symbol, taking TLS into account.

Parameters
  • elf (elftools.elf.elffile.ELFFile) –

  • symbol (elftools.elf.sections.Symbol) –

Return type

int

mate.build.tob_chess_utils.elf.get_symbol_va_base(elf: elftools.elf.elffile.ELFFile, symbol: elftools.elf.sections.Symbol) int

Returns the base virtual address for a given symbol.

Parameters
  • elf (elftools.elf.elffile.ELFFile) –

  • symbol (elftools.elf.sections.Symbol) –

Return type

int

mate.build.tob_chess_utils.elf.get_va_text_base(elf: elftools.elf.elffile.ELFFile) int

Returns the base virtual address for the .text section.

Parameters

elf (elftools.elf.elffile.ELFFile) –

Return type

int

mate.build.tob_chess_utils.elf.parse_dyn_sym_table(elf: elftools.elf.elffile.ELFFile) Dict[int, str]

Parse the dynamic symbol table from the ELF File and return a mapping of VA to symbol.

Parameters

elf (elftools.elf.elffile.ELFFile) –

Return type

Dict[int, str]

mate.build.tob_chess_utils.elf.plt_va_map(elf: elftools.elf.elffile.ELFFile) Dict[str, int]

Unfurl the PLT relocation table in the given ELF file, returning a mapping of symbol names to VAs for each function that goes through the PLT.

Parameters

elf (elftools.elf.elffile.ELFFile) –

Return type

Dict[str, int]

mate.build.tob_chess_utils.elf.va_symbol_map(symtab: elftools.elf.sections.SymbolTableSection) Dict[int, List[elftools.elf.sections.Symbol]]

Returns a mapping of VAs to symbols for the given symbol table.

Parameters

symtab (elftools.elf.sections.SymbolTableSection) –

Return type

Dict[int, List[elftools.elf.sections.Symbol]]