Relocations

CLE’s loader implements program relocations. If you would like to add support for more relocations, you can do so by subclassing the Relocation class and overriding any relevant methods or properties. Then, add or uncomment the appropriate line in the relocations_table dict at the bottom of the file. Look at the existing versions for details.

class cle.backends.relocation.Relocation[source]

Bases: object

A representation of a relocation in a binary file. Smart enough to relocate itself.

Variables:
  • owner – The binary this relocation was originaly found in, as a cle object

  • symbol – The Symbol object this relocation refers to

  • relative_addr – The address in owner this relocation would like to write to

  • resolvedby – If the symbol this relocation refers to is an import symbol and that import has been resolved, this attribute holds the symbol from a different binary that was used to resolve the import.

  • resolved – Whether the application of this relocation was successful

__init__(owner: Backend, symbol: Symbol | None, relative_addr: int)[source]
Parameters:
AUTO_HANDLE_NONE = False
resolve_symbol(solist: list[Any], thumb=False, extern_object=None, **kwargs)[source]
Parameters:

solist (list[Any])

resolve(obj, extern_object=None)[source]
property rebased_addr

The address in the global memory space this relocation would like to write to

property linked_addr
property dest_addr
property value
relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

property owner_obj
cle.backends.elf.relocation.get_relocation(arch, r_type)[source]
class cle.backends.elf.relocation.elfreloc.ELFReloc[source]

Bases: Relocation

__init__(owner, symbol, relative_addr, addend=None)[source]
property addend
property value
class cle.backends.elf.relocation.generic.GenericTLSDoffsetReloc[source]

Bases: ELFReloc

property value
resolve_symbol(solist, **kwargs)[source]
class cle.backends.elf.relocation.generic.GenericTLSOffsetReloc[source]

Bases: ELFReloc

AUTO_HANDLE_NONE = True
relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

class cle.backends.elf.relocation.generic.GenericTLSDescriptorReloc[source]

Bases: ELFReloc

RESOLVER_ADDR: int = NotImplemented
AUTO_HANDLE_NONE = True
relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

class cle.backends.elf.relocation.generic.GenericTLSModIdReloc[source]

Bases: ELFReloc

AUTO_HANDLE_NONE = True
relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

class cle.backends.elf.relocation.generic.GenericIRelativeReloc[source]

Bases: ELFReloc

AUTO_HANDLE_NONE = True
relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

class cle.backends.elf.relocation.generic.GenericAbsoluteAddendReloc[source]

Bases: ELFReloc

property value
class cle.backends.elf.relocation.generic.GenericPCRelativeAddendReloc[source]

Bases: ELFReloc

property value
class cle.backends.elf.relocation.generic.GenericJumpslotReloc[source]

Bases: ELFReloc

property value
class cle.backends.elf.relocation.generic.GenericRelativeReloc[source]

Bases: ELFReloc

AUTO_HANDLE_NONE = True
property value
class cle.backends.elf.relocation.generic.GenericAbsoluteReloc[source]

Bases: ELFReloc

property value
class cle.backends.elf.relocation.generic.GenericCopyReloc[source]

Bases: ELFReloc

resolve_symbol(solist, **kwargs)[source]
relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

class cle.backends.elf.relocation.generic.MipsGlobalReloc[source]

Bases: GenericAbsoluteReloc

class cle.backends.elf.relocation.generic.MipsLocalReloc[source]

Bases: ELFReloc

AUTO_HANDLE_NONE = True
resolve_symbol(solist, **kwargs)[source]
relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

class cle.backends.elf.relocation.generic.RelocTruncate32Mixin[source]

Bases: object

A mix-in class for relocations that cover a 32-bit field regardless of the architecture’s address word length.

check_zero_extend = False
check_sign_extend = False
relocate()[source]
class cle.backends.elf.relocation.generic.RelocGOTMixin[source]

Bases: object

A mix-in class which will cause the symbol to be resolved to a pointer to the symbol instead of the symbol

resolve(symbol, extern_object=None)[source]

Relocation types for PowerPC 32-bit architecture.

Reference: http://refspecs.linux-foundation.org/elf/elfspec_ppc.pdf page 4-18

Only relocations 1-37 are described in the document. The rest are from the GNU binutils source code. See include/elf/ppc.h in the binutils source code.

Relocation types for PPC64.

Reference: http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.pdf pages 57-59

Relocation types for i386.

Reference: https://github.com/hjl-tools/x86-psABI/wiki/intel386-psABI-1.1.pdf page 36

Relocations for amd64/x86_64

Reference: https://gitlab.com/x86-psABIs/x86-64-ABI/-/jobs/artifacts/master/raw/x86-64-ABI/abi.pdf?job=build page 73

Relocation types for MIPS 32-bit.

Reference: https://refspecs.linuxfoundation.org/elf/mipsabi.pdf page 4-19

The main document is old and does not contain all the relocation types. I could not find a more recent document, so I had to rely on the source code of GNU binutils for all relocations that are not in the main document. See include/elf/mips.h in the binutils source code.

Relocation types for ARM.

Reference: https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst#relocation-codes

Relocations for AARCH64

Reference: https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst#relocation

Relocation types for the S390X architecture.

Reference: https://github.com/IBM/s390x-abi/releases/download/v1.6.1/lzsabi_s390x.pdf pages 51-52

cle.backends.pe.relocation.get_relocation(arch, r_type)[source]
class cle.backends.pe.relocation.pereloc.PEReloc[source]

Bases: Relocation

AUTO_HANDLE_NONE = True
__init__(owner, symbol, addr, resolvewith=None)[source]
resolve_symbol(solist, bypass_compatibility=False, extern_object=None, **kwargs)[source]
relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

property value
property is_base_reloc

These relocations are ignored by the linker if the executable is loaded at its preferred base address. There is no associated symbol with base relocations.

property is_import
class cle.backends.pe.relocation.generic.DllImport[source]

Bases: PEReloc

There’s nothing special to be done for DLL imports but this class provides a unique name to the relocation type.

class cle.backends.pe.relocation.generic.IMAGE_REL_BASED_ABSOLUTE[source]

Bases: PEReloc

relocate()[source]

Applies this relocation. Will make changes to the memory object of the object it came from.

This implementation is a generic version that can be overridden in subclasses.

class cle.backends.pe.relocation.generic.IMAGE_REL_BASED_HIGHADJ[source]

Bases: PEReloc

__init__(owner, addr, next_rva)[source]
property value

In all the other cases, we can ignore the relocation difference part of the calculation because we simply use to_mva() to get our rebased address. In this case, however, we have to adjust the un-rebased address first.

class cle.backends.pe.relocation.generic.IMAGE_REL_BASED_HIGHLOW[source]

Bases: PEReloc

property value
class cle.backends.pe.relocation.generic.IMAGE_REL_BASED_DIR64[source]

Bases: PEReloc

property value
class cle.backends.pe.relocation.generic.IMAGE_REL_BASED_HIGH[source]

Bases: PEReloc

property value
class cle.backends.pe.relocation.generic.IMAGE_REL_BASED_LOW[source]

Bases: PEReloc

property value