ELF Backend¶
- class cle.backends.ELF[source]¶
Bases:
MetaELFThe 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
- compilation_units: list[CompilationUnit] | None¶
- functions_debug_info: dict[int, Subprogram]¶
- classmethod check_compatibility(spec, obj)[source]¶
Performs a minimal static load of
specand 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¶
- class cle.backends.elf.ELFCore[source]¶
Bases:
ELFLoader 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_mappingto specify adictmapping (easy if there are a small number of mappings) orremote_file_mapperto 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_mappingandremote_file_mapper,remote_file_mappingis 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.
- class cle.backends.elf.MetaELF[source]¶
Bases:
BackendA 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.
- class cle.backends.elf.symbol.ELFSymbol[source]¶
Bases:
SymbolRepresents 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
- property subtype: ELFSymbolType¶
A subclass’ ABI-specific types
- class cle.backends.elf.symbol_type.ELFSymbolType[source]¶
Bases:
SymbolSubTypeELF-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¶
- __new__(value)¶
This is just a nice way to allow for just specifying the int for default types: ELFSymbolType(10) rather than ELFSymbolType((10,None)).
Idea courtesy: https://stackoverflow.com/q/24105268/1137728.
We don’t need to implement the str parsing like the SO link above since Enum already has built-in item access: ELFSymbolType[‘STT_FUNC’].
- class cle.backends.elf.regions.ELFSegment[source]¶
Bases:
SegmentRepresents 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:
objectVariable for DWARF from a DW_TAG_variable or DW_TAG_formal_parameter
- Variables:
name (str) – The name of the variable
relative_addr (
int|None) – The relative addr (base addr depends on the type)lexical_block (
LexicalBlock|None) – 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, namespace: list[str] | None = None)[source]¶
- Parameters:
die (DIE)
elf_object (ELF)
lexical_block (LexicalBlock | None)
- property rebased_addr¶
- property addr¶
Please use ‘relative_addr’ or ‘rebased_addr’ instead.
- property type: VariableType¶
- class cle.backends.elf.variable.MemoryVariable[source]¶
Bases:
VariableThis 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:
VariableStack Variable from DWARF.
- class cle.backends.elf.variable.RegisterVariable[source]¶
Bases:
VariableRegister Variable from DWARF.
- cle.backends.elf.variable_type.resolve_reference_addr(die: DIE, attr_name: str) int[source]¶
Resolves a reference attribute to the underlying DIE location :type die:
DIE:param die: The DIE containing the reference attribute :type attr_name:str:param attr_name: The name of the attribute as a string
- class cle.backends.elf.variable_type.VariableType[source]¶
Bases:
objectEntry 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:
VariableTypeEntry 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:
VariableTypeEntry class for DW_TAG_base_type. It is inherited from VariableType
- class cle.backends.elf.variable_type.StructType[source]¶
Bases:
VariableTypeEntry class for DW_TAG_structure_type. It is inherited from VariableType
- Parameters:
- class cle.backends.elf.variable_type.UnionType[source]¶
Bases:
StructTypeEntry class for DW_TAG_union_type. Inherits from StructType to make it trivial.
- class cle.backends.elf.variable_type.StructMember[source]¶
Bases:
objectEntry 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:
VariableTypeEntry 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:
VariableTypeEntry 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:
objectLSDA exception table parser.
TODO: Much of this class should be eventually moved to pyelftools.
- class cle.backends.elf.hashtable.ELFHashTable[source]¶
Bases:
objectFunctions 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:
objectFunctions 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:
objectA 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:
- Variables:
low_pc – The relative start address of the subprogram
high_pc – The relative end address of the subprogram
child_blocks (
list[LexicalBlock]) – Lexical blocks inside this block (only direct childs)
- class cle.backends.elf.subprogram.Subprogram[source]¶
Bases:
LexicalBlockDW_TAG_subprogram for DWARF. The behavior is mostly inherited from LexicalBlock to avoid redundancy.
- Parameters:
- Variables: