ELF Backend#
- class cle.backends.ELF[source]#
Bases:
MetaELF
The main loader class for statically loading ELF executables. Uses the pyreadelf library where useful.
Useful backend options:
debug_symbols
: Provides the path to a separate file which contains the binary’s debug symbolsdiscard_section_headers
: Do not parse section headers. Use this if they are corrupted or malicious.discard_program_headers
: Do not parse program headers. Use this if the binary is for a platform whose ELFloader only looks at section headers, but whose toolchain generates program headers anyway.
- is_default = True#
- __init__(*args, addend=None, debug_symbols=None, discard_section_headers=False, discard_program_headers=False, **kwargs)[source]#
- Parameters:
binary – The path to the binary to load
binary_stream – The open stream to this binary. The reference to this will be held until you call close.
is_main_bin – Whether this binary should be loaded as the main executable
- imports: typing.Dict[str, 'Relocation']#
- relocs: List[Relocation]#
- classmethod check_compatibility(spec, obj)[source]#
Performs a minimal static load of
spec
and returns whether it’s compatible with other_obj
- classmethod check_magic_compatibility(stream)[source]#
Check if a stream of bytes contains the same magic number as the main object
- static is_compatible(stream)[source]#
Determine quickly whether this backend can load an object from this stream
- property initializers#
Stub function. Should be overridden by backends that can provide initializer functions that ought to be run before execution reaches the entry point. Addresses should be rebased.
- property finalizers#
Stub function. Like initializers, but with finalizers.
- property symbols_by_name#
- get_symbol(symid, symbol_table=None)[source]#
Gets a Symbol object for the specified symbol.
- Parameters:
symid – Either an index into .dynsym or the name of a symbol.
- rebase(new_base)[source]#
Rebase backend’s regions to the new base where they were mapped by the loader
- child_objects: List['Backend']#
- exception_handlings: List[ExceptionHandling]#
- function_hints: List[FunctionHint]#
- memory: Clemory#
- cached_content: Optional[bytes]#
- class cle.backends.elf.ELFCore[source]#
Bases:
ELF
Loader class for ELF core files.
One key pain point when analyzing a core dump generated on a remote machine is that the paths to binaries are absolute (and may not exist or be the same on your local machine).
Therefore, you can use the options
`remote_file_mapping
to specify adict
mapping (easy if there are a small number of mappings) orremote_file_mapper
to specify a function that accepts a remote file name and returns the local file name (useful if there are many mappings).If you specify both
remote_file_mapping
andremote_file_mapper
,remote_file_mapping
is applied first, then the result is passed toremote_file_mapper
.- Parameters:
executable – Optional path to the main binary of the core dump. If not supplied, ELFCore will attempt to figure it out automatically from the core dump.
remote_file_mapping – Optional dict that maps specific file names in the core dump to other file names.
remote_file_mapper – Optional function that is used to map every file name in the core dump to whatever is returned from this function.
- is_default = True#
- __init__(*args, executable=None, remote_file_mapping=None, remote_file_mapper=None, **kwargs)[source]#
- Parameters:
binary – The path to the binary to load
binary_stream – The open stream to this binary. The reference to this will be held until you call close.
is_main_bin – Whether this binary should be loaded as the main executable
- static is_compatible(stream)[source]#
Determine quickly whether this backend can load an object from this stream
- property threads#
If this backend represents a dump of a running program, it may contain one or more thread contexts, i.e. register files. This property should contain a list of names for these threads, which should be unique.
- thread_registers(thread=None)[source]#
If this backend represents a dump of a running program, it may contain one or more thread contexts, i.e. register files. This method should return the register file for a given thread (as named in
Backend.threads
) as a dict mapping register names (as seen in archinfo) to numbers. If the thread is not specified, it should return the context for a “default” thread. If there are no threads, it should return an empty dict.
- imports: typing.Dict[str, 'Relocation']#
- relocs: List[Relocation]#
- child_objects: List['Backend']#
- exception_handlings: List[ExceptionHandling]#
- function_hints: List[FunctionHint]#
- memory: Clemory#
- cached_content: Optional[bytes]#
- addr_to_line: SortedDict[int, Set[Tuple[int, int]]]#
- variables: Optional[List[Variable]]#
- compilation_units: Optional[List[CompilationUnit]]#
- class cle.backends.elf.MetaELF[source]#
Bases:
Backend
A base class that implements functions used by all backends that can load an ELF.
- __init__(*args, **kwargs)[source]#
- Parameters:
binary – The path to the binary to load
binary_stream – The open stream to this binary. The reference to this will be held until you call close.
is_main_bin – Whether this binary should be loaded as the main executable
- supported_filetypes = ['elf']#
- property plt#
Maps names to addresses.
- property reverse_plt#
Maps addresses to names.
- property is_ppc64_abiv1#
Returns whether the arch is PowerPC64 ABIv1.
- Returns:
True if PowerPC64 ABIv1, False otherwise.
- property is_ppc64_abiv2#
Returns whether the arch is PowerPC64 ABIv2.
- Returns:
True if PowerPC64 ABIv2, False otherwise.
- property ppc64_initial_rtoc#
Get initial rtoc value for PowerPC64 architecture.
- static extract_soname(path)[source]#
Extracts the shared object identifier from the path, or returns None if it cannot.
- imports: typing.Dict[str, 'Relocation']#
- relocs: List[Relocation]#
- child_objects: List['Backend']#
- exception_handlings: List[ExceptionHandling]#
- function_hints: List[FunctionHint]#
- memory: Clemory#
- cached_content: Optional[bytes]#
- class cle.backends.elf.metaelf.Relro[source]#
Bases:
Enum
An enumeration.
- NONE = 0#
- PARTIAL = 1#
- FULL = 2#
- class cle.backends.elf.symbol.ELFSymbol[source]#
Bases:
Symbol
Represents a symbol for the ELF format.
- Variables:
binding (str) – The binding of this symbol as an ELF enum string
section – The section associated with this symbol, or None
_subtype – The ELFSymbolType of this symbol
- owner: Backend#
- property subtype: ELFSymbolType#
A subclass’ ABI-specific types
- class cle.backends.elf.symbol_type.ELFSymbolType[source]#
Bases:
SymbolSubType
ELF-specific symbol types
- STT_NOTYPE = (0, None)#
- STT_OBJECT = (1, None)#
- STT_FUNC = (2, None)#
- STT_SECTION = (3, None)#
- STT_FILE = (4, None)#
- STT_COMMON = (5, None)#
- STT_TLS = (6, None)#
- STT_LOOS = (10, None)#
- STT_HIOS = (12, None)#
- STT_LOPROC = (13, None)#
- STT_HIPROC = (15, None)#
- STT_GNU_IFUNC = (10, 'gnu')#
- property elf_value#
- property os_proc#
- property is_custom_os_proc#
- class cle.backends.elf.regions.ELFSegment[source]#
Bases:
Segment
Represents a segment for the ELF format.
- property is_readable#
- property is_writable#
- property is_executable#
- property is_relro#
- class cle.backends.elf.regions.ELFSection[source]#
Bases:
Section
- SHF_WRITE = 1#
- SHF_ALLOC = 2#
- SHF_EXECINSTR = 4#
- SHF_STRINGS = 32#
- SHT_NULL = 'SHT_NULL'#
- property is_readable#
Whether this section has read permissions
- property is_active#
- property is_writable#
Whether this section has write permissions
- property occupies_memory#
- property is_executable#
Whether this section has execute permissions
- property is_strings#
- property only_contains_uninitialized_data#
Whether this section is initialized to zero after the executable is loaded.
- class cle.backends.elf.variable.Variable[source]#
Bases:
object
Variable for DWARF from a DW_TAG_variable or DW_TAG_formal_parameter
- Variables:
name (str) – The name of the variable
relative_addr – The relative addr (base addr depends on the type)
lexical_block – For a local variable, the lexical block where the variable is declared
- static from_die(die: DIE, expr_parser, elf_object: ELF, lexical_block: LexicalBlock | None = None)[source]#
- Parameters:
die (DIE) –
elf_object (ELF) –
lexical_block (LexicalBlock | None) –
- rebased_addr_from_cfa(cfa: int)[source]#
The address of this variable in the global memory.
- Parameters:
cfa (
int
) – The canonical frame address as described by the DWARF standard.
- property rebased_addr#
- property addr#
Please use ‘relative_addr’ or ‘rebased_addr’ instead.
- property type: VariableType#
- class cle.backends.elf.variable.MemoryVariable[source]#
Bases:
Variable
This includes all variables that are not on the stack and not in a register. So all global variables, and also local static variables in C!
- property rebased_addr#
- class cle.backends.elf.variable.StackVariable[source]#
Bases:
Variable
Stack Variable from DWARF.
- class cle.backends.elf.variable.RegisterVariable[source]#
Bases:
Variable
Register Variable from DWARF.
- class cle.backends.elf.variable_type.VariableType[source]#
Bases:
object
Entry class for DW_TAG_xxx_type
- Parameters:
- Variables:
name – name of the type
byte_size – amount of bytes the type take in memory
- class cle.backends.elf.variable_type.PointerType[source]#
Bases:
VariableType
Entry class for DW_TAG_pointer_type. It is inherited from VariableType
- Parameters:
- classmethod read_from_die(die: DIE, elf_object)[source]#
read an entry of DW_TAG_pointer_type. return None when there is no byte_size or type attribute.
- Parameters:
die (DIE) –
- property referenced_type#
attribute to get the referenced type. Return None if the type is not loaded
- class cle.backends.elf.variable_type.BaseType[source]#
Bases:
VariableType
Entry class for DW_TAG_base_type. It is inherited from VariableType
- class cle.backends.elf.variable_type.StructType[source]#
Bases:
VariableType
Entry class for DW_TAG_structure_type. It is inherited from VariableType
- Parameters:
- class cle.backends.elf.variable_type.UnionType[source]#
Bases:
StructType
Entry class for DW_TAG_union_type. Inherits from StructType to make it trivial.
- class cle.backends.elf.variable_type.StructMember[source]#
Bases:
object
Entry class for DW_TAG_member. This is not a type but a named member inside a struct. Use the property type to get its variable type.
- Parameters:
- Variables:
name – name of the member
- classmethod read_from_die(die: DIE, elf_object)[source]#
read an entry of DW_TAG_member_type. return None when there is no type attribute.
- Parameters:
die (DIE) –
- property type#
attribute to get the type of the member. Return None if the type is not loaded
- class cle.backends.elf.variable_type.ArrayType[source]#
Bases:
VariableType
Entry class for DW_TAG_array_type. It is inherited from VariableType
- Parameters:
byte_size – amount of bytes the type take in memory
elf_object – elf object to reference to (useful for pointer,…)
element_offset – type of the array elements as offset in the compilation_unit
- classmethod read_from_die(die: DIE, elf_object)[source]#
read an entry of DW_TAG_array_type. return None when there is no type attribute.
- Parameters:
die (DIE) –
- property element_type#
- class cle.backends.elf.variable_type.TypedefType[source]#
Bases:
VariableType
Entry class for DW_TAG_typedef. Inherits from VariableType.
- Parameters:
name (
str
) – name of the new typeelf_object – elf object to reference to (useful for pointer,…)
type_offset – type as offset in the compilation_unit
- classmethod read_from_die(die: DIE, elf_object)[source]#
read an entry of DW_TAG_member_type. return None when there is no type attribute.
- Parameters:
die (DIE) –
- property type#
attribute to get the type of the member. Return None if the type is not loaded
References
- class cle.backends.elf.lsda.ExceptionTableHeader[source]#
Bases:
object
- lp_start#
- ttype_encoding#
- ttype_offset#
- call_site_encoding#
- call_site_table_len#
- class cle.backends.elf.lsda.CallSiteEntry[source]#
Bases:
object
- cs_start#
- cs_len#
- cs_lp#
- cs_action#
- class cle.backends.elf.lsda.LSDAExceptionTable[source]#
Bases:
object
LSDA exception table parser.
TODO: Much of this class should be eventually moved to pyelftools.
- class cle.backends.elf.hashtable.ELFHashTable[source]#
Bases:
object
Functions to do lookup from a HASH section of an ELF file.
Information: http://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-48031.html
- __init__(symtab, stream, offset, arch)[source]#
- Parameters:
symtab – The symbol table to perform lookups from (as a pyelftools SymbolTableSection).
stream – A file-like object to read from the ELF’s memory.
offset – The offset in the object where the table starts.
arch – The ArchInfo object for the ELF file.
- class cle.backends.elf.hashtable.GNUHashTable[source]#
Bases:
object
Functions to do lookup from a GNU_HASH section of an ELF file.
Information: https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
- __init__(symtab, stream, offset, arch)[source]#
- Parameters:
symtab – The symbol table to perform lookups from (as a pyelftools SymbolTableSection).
stream – A file-like object to read from the ELF’s memory.
offset – The offset in the object where the table starts.
arch – The ArchInfo object for the ELF file.
- class cle.backends.elf.subprogram.LexicalBlock[source]#
Bases:
object
A lexical block is a sequence of source statements, e.g. a while/for loop or an if statement or some bracketed block.
Corresponds to a DW_TAG_LexicalBlock in DWARF.
- Parameters:
super_block – The lexical block which contains this block
low_pc – The relative start address of the block
high_pc – The relative end address of the block
- Variables:
low_pc – The relative start address of the subprogram
high_pc – The relative end address of the subprogram
child_blocks – Lexical blocks inside this block (only direct childs)
- class cle.backends.elf.subprogram.Subprogram[source]#
Bases:
LexicalBlock
DW_TAG_subprogram for DWARF. The behavior is mostly inherited from LexicalBlock to avoid redundancy.
- Parameters:
name (str) – The name of the function/program
low_pc – The relative start address of the subprogram
high_pc – The relative end address of the subprogram
- Variables:
name – The name of the function/program
local_variables – All local variables in a Subprogram (they may reside in serveral child blocks)