diff --git a/cle/backends/elf/relocation/__init__.py b/cle/backends/elf/relocation/__init__.py index d3ca5bb2..d25203b5 100644 --- a/cle/backends/elf/relocation/__init__.py +++ b/cle/backends/elf/relocation/__init__.py @@ -1,44 +1,36 @@ from __future__ import annotations -import importlib import logging -import os -from collections import defaultdict -import archinfo +from .amd64 import relocation_table_amd64 +from .arm import relocation_table_arm +from .arm64 import relocation_table_arm64 +from .i386 import relocation_table_i386 +from .mips import relocation_table_mips +from .ppc import relocation_table_ppc +from .ppc64 import relocation_table_ppc64 +from .s390x import relocation_table_s390x +from .sparc import relocation_table_sparc + +ALL_RELOCATIONS = { + "AMD64": relocation_table_amd64, + "ARMCortexM": relocation_table_arm, + "ARM": relocation_table_arm, + "AARCH64": relocation_table_arm64, + "ARMEL": relocation_table_arm, + "ARMHF": relocation_table_arm, + "X86": relocation_table_i386, + "MIPS32": relocation_table_mips, + "MIPS64": relocation_table_mips, + "PPC32": relocation_table_ppc, + "PPC64": relocation_table_ppc64, + "S390X": relocation_table_s390x, + "sparc:BE:32:default": relocation_table_sparc, +} -from cle.backends.relocation import Relocation -ALL_RELOCATIONS = defaultdict(dict) -complaint_log = set() - -path = os.path.dirname(os.path.abspath(__file__)) log = logging.getLogger(name=__name__) - - -def load_relocations(): - for filename in os.listdir(path): - if not filename.endswith(".py"): - continue - if filename == "__init__.py": - continue - - log.debug("Importing ELF relocation module: %s", filename[:-3]) - module = importlib.import_module(f".{filename[:-3]}", "cle.backends.elf.relocation") - - try: - arch_name = module.arch - except AttributeError: - continue - - for item_name in dir(module): - if item_name not in archinfo.defines: - continue - item = getattr(module, item_name) - if not isinstance(item, type) or not issubclass(item, Relocation): - continue - - ALL_RELOCATIONS[arch_name][archinfo.defines[item_name]] = item +complaint_log = set() def get_relocation(arch, r_type): @@ -51,6 +43,3 @@ def get_relocation(arch, r_type): complaint_log.add((arch, r_type)) log.warning("Unknown reloc %d on %s", r_type, arch) return None - - -load_relocations() diff --git a/cle/backends/elf/relocation/amd64.py b/cle/backends/elf/relocation/amd64.py index 26e34fce..bde6f6ce 100644 --- a/cle/backends/elf/relocation/amd64.py +++ b/cle/backends/elf/relocation/amd64.py @@ -1,3 +1,8 @@ +"""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 +""" + from __future__ import annotations from .generic import ( @@ -14,8 +19,6 @@ RelocTruncate32Mixin, ) -arch = "AMD64" - class R_X86_64_64(GenericAbsoluteAddendReloc): pass @@ -79,3 +82,54 @@ class R_X86_64_GOTPCRELX(RelocGOTMixin, RelocTruncate32Mixin, GenericPCRelativeA class R_X86_64_REX_GOTPCRELX(RelocGOTMixin, RelocTruncate32Mixin, GenericPCRelativeAddendReloc): check_sign_extend = True + + +relocation_table_amd64 = { + 1: R_X86_64_64, + 2: R_X86_64_PC32, + # 3: R_X86_64_GOT32, + 4: R_X86_64_PLT32, + 5: R_X86_64_COPY, + 6: R_X86_64_GLOB_DAT, + 7: R_X86_64_JUMP_SLOT, + 8: R_X86_64_RELATIVE, + 9: R_X86_64_GOTPCREL, + 10: R_X86_64_32, + 11: R_X86_64_32S, + # 12: R_X86_64_16, + # 13: R_X86_64_PC16, + # 14: R_X86_64_8, + # 15: R_X86_64_PC8, + 16: R_X86_64_DTPMOD64, + 17: R_X86_64_DTPOFF64, + 18: R_X86_64_TPOFF64, + # 19: R_X86_64_TLSGD, + # 20: R_X86_64_TLSLD, + # 21: R_X86_64_DTPOFF32, + # 22: R_X86_64_GOTTPOFF, + # 23: R_X86_64_TPOFF32, + # 24: R_X86_64_PC64, + # 25: R_X86_64_GOTOFF64, + # 26: R_X86_64_GOTPC32, + # 32: R_X86_64_SIZE32, + # 33: R_X86_64_SIZE64, + # 34: R_X86_64_GOTPC32_TLSDESC, + # 35: R_X86_64_TLSDESC_CALL, + # 36: R_X86_64_TLSDESC, + 37: R_X86_64_IRELATIVE, + # 38: R_X86_64_RELATIVE64, + # 39, 40: Deprecated + 41: R_X86_64_GOTPCRELX, + 42: R_X86_64_REX_GOTPCRELX, + # 43: R_X86_64_CODE_4_GOTPCRELX, + # 44: R_X86_64_CODE_4_GOTTPOFF, + # 45: R_X86_64_CODE_4_GOTPC32_TLSDESC, + # 46: R_X86_64_CODE_5_GOTPCRELX, + # 47: R_X86_64_CODE_5_GOTTPOFF, + # 48: R_X86_64_CODE_5_GOTPC32_TLSDESC, + # 49: R_X86_64_CODE_6_GOTPCRELX, + # 50: R_X86_64_CODE_6_GOTTPOFF, + # 51: R_X86_64_CODE_6_GOTPC32_TLSDESC, +} + +__all__ = ("relocation_table_amd64",) diff --git a/cle/backends/elf/relocation/arm.py b/cle/backends/elf/relocation/arm.py index 2bbfc4fd..f62c1a75 100644 --- a/cle/backends/elf/relocation/arm.py +++ b/cle/backends/elf/relocation/arm.py @@ -1,3 +1,8 @@ +"""Relocation types for ARM. + +Reference: https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst#relocation-codes +""" + from __future__ import annotations import logging @@ -19,10 +24,6 @@ ) log = logging.getLogger(name=__name__) -arch = "ARM" - -# Reference: "ELF for the ARM Architecture ABI r2.10" -# http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044e/IHI0044E_aaelf.pdf def _applyReloc(inst, result, mask=0xFFFFFFFF): @@ -518,30 +519,131 @@ class R_ARM_GOT_PREL(GenericPCRelativeAddendReloc, RelocTruncate32Mixin, RelocGO """ -__all__ = [ - "arch", - "R_ARM_CALL", - "R_ARM_PREL31", - "R_ARM_REL32", - "R_ARM_ABS32", - "R_ARM_MOVW_ABS_NC", - "R_ARM_MOVT_ABS", - "R_ARM_THM_CALL", - "R_ARM_COPY", - "R_ARM_GLOB_DAT", - "R_ARM_JUMP_SLOT", - "R_ARM_RELATIVE", - "R_ARM_ABS32_NOI", - "R_ARM_REL32_NOI", - "R_ARM_TLS_DTPMOD32", - "R_ARM_TLS_DTPOFF32", - "R_ARM_TLS_TPOFF32", - "R_ARM_JUMP24", - "R_ARM_PC24", - "R_ARM_THM_JUMP24", - "R_ARM_THM_JUMP19", - "R_ARM_THM_JUMP6", - "R_ARM_THM_MOVW_ABS_NC", - "R_ARM_THM_MOVT_ABS", - "R_ARM_GOT_PREL", -] +relocation_table_arm = { + 1: R_ARM_PC24, + 2: R_ARM_ABS32, + 3: R_ARM_REL32, + # 4: ARM_LDR_PC_G0, + # 5: R_ARM_ABS16, + # 6: R_ARM_ABS12, + # 7: R_ARM_THM_ABS5, + # 8: R_ARM_ABS8, + # 9: R_ARM_SBREL32, + 10: R_ARM_THM_CALL, + # 11: R_ARM_THM_PC8, + # 12: R_ARM_BREL_ADJ, + # 13: R_ARM_TLS_DESC, + # 14: R_ARM_THM_SWI8, + # 15: R_ARM_XPC25, + # 16: R_ARM_THM_XPC22, + 17: R_ARM_TLS_DTPMOD32, + 18: R_ARM_TLS_DTPOFF32, + 19: R_ARM_TLS_TPOFF32, + 20: R_ARM_COPY, + 21: R_ARM_GLOB_DAT, + 22: R_ARM_JUMP_SLOT, + 23: R_ARM_RELATIVE, + # 24: R_ARM_GOTOFF, + # 25: R_ARM_BASE_PREL, + # 26: R_ARM_GOT_BREL, + # 27: R_ARM_PLT32, + 28: R_ARM_CALL, + 29: R_ARM_JUMP24, + 30: R_ARM_THM_JUMP24, + # 31: R_ARM_BASE_ABS, + # 32: R_ARM_ALU_PCREL_7_0, + # 33: R_ARM_ALU_PCREL_15_8, + # 34: R_ARM_ALU_PCREL_23_15, + # 35: R_ARM_LDR_SBREL_11_0_NC, + # 36: R_ARM_ALU_SBREL_19_12_NC, + # 37: R_ARM_ALU_SBREL_27_20_CK, + # 38: R_ARM_TARGET1, + # 39: R_ARM_SBREL31, + # 40: R_ARM_V4BX, + # 41: R_ARM_TARGET2, + 42: R_ARM_PREL31, + 43: R_ARM_MOVW_ABS_NC, + 44: R_ARM_MOVT_ABS, + # 45: R_ARM_MOVW_PREL_NC, + # 46: R_ARM_MOVT_PREL, + 47: R_ARM_THM_MOVW_ABS_NC, + 48: R_ARM_THM_MOVT_ABS, + # 49: R_ARM_THM_MOVW_PREL_NC, + # 50: R_ARM_THM_MOVT_PREL, + 51: R_ARM_THM_JUMP19, + 52: R_ARM_THM_JUMP6, + # 53: R_ARM_THM_ALU_PREL_11_0, + # 54: R_ARM_THM_PC12, + 55: R_ARM_ABS32_NOI, + 56: R_ARM_REL32_NOI, + # 57: R_ARM_ALU_PC_G0_NC, + # 58: R_ARM_ALU_PC_G0, + # 59: R_ARM_ALU_PC_G1_NC, + # 60: R_ARM_ALU_PC_G1, + # 61: R_ARM_ALU_PC_G2, + # 62: R_ARM_LDR_PC_G1, + # 63: R_ARM_LDR_PC_G2, + # 64: R_ARM_LDRS_PC_G0, + # 65: R_ARM_LDRS_PC_G1, + # 66: R_ARM_LDRS_PC_G2, + # 67: R_ARM_LDC_PC_G0, + # 68: R_ARM_LDC_PC_G1, + # 69: R_ARM_LDC_PC_G2, + # 70: R_ARM_ALU_SB_G0_NC, + # 71: R_ARM_ALU_SB_G0, + # 72: R_ARM_ALU_SB_G1_NC, + # 73: R_ARM_ALU_SB_G1, + # 74: R_ARM_ALU_SB_G2, + # 75: R_ARM_LDR_SB_G0, + # 76: R_ARM_LDR_SB_G1, + # 77: R_ARM_LDR_SB_G2, + # 78: R_ARM_LDRS_SB_G0, + # 79: R_ARM_LDRS_SB_G1, + # 80: R_ARM_LDRS_SB_G2, + # 81: R_ARM_LDC_SB_G0, + # 82: R_ARM_LDC_SB_G1, + # 83: R_ARM_LDC_SB_G2, + # 84: R_ARM_MOVW_BREL_NC, + # 85: R_ARM_MOVT_BREL, + # 86: R_ARM_MOVW_BREL, + # 87: R_ARM_THM_MOVW_BREL_NC, + # 88: R_ARM_THM_MOVT_BREL, + # 89: R_ARM_THM_MOVW_BREL, + # 90: R_ARM_TLS_GOTDESC, + # 91: R_ARM_TLS_CALL, + # 92: R_ARM_TLS_DESCSEQ, + # 93: R_ARM_THM_TLS_CALL, + # 94: R_ARM_PLT32_ABS, + # 95: R_ARM_GOT_ABS, + 96: R_ARM_GOT_PREL, + # 97: R_ARM_GOT_BREL12, + # 98: R_ARM_GOTOFF12, + # 99: R_ARM_GOTRELAX, + # 100: R_ARM_GNU_VTENTRY, + # 101: R_ARM_GNU_VTINHERIT, + # 102: R_ARM_JUMP11, + # 103: R_ARM_THM_PC9, + # 104: R_ARM_TLS_GD32, + # 105: R_ARM_TLS_LDM32, + # 106: R_ARM_TLS_LDO32, + # 107: R_ARM_TLS_IE32, + # 108: R_ARM_TLS_LE32, + # 109: R_ARM_TLS_LDO12, + # 110: R_ARM_TLS_LE12, + # 111: R_ARM_TLS_IE12GP, + # 112-127: R_ARM_PRIVATE_ + # 128: R_ARM_ME_TOO, + # 129: R_ARM_THM_TLS_DESCSEQ16, + # 130: R_ARM_THM_TLS_DESCSEQ32, + # 131: R_ARM_THM_GOT_BREL12, + # 132: R_ARM_THM_ALU_ABS_G0_NC, + # 133: R_ARM_THM_ALU_ABS_G1_NC, + # 134: R_ARM_THM_ALU_ABS_G2_NC, + # 135: R_ARM_THM_ALU_ABS_G3, + # 136: R_ARM_THM_BF16, + # 137: R_ARM_THM_BF12, + # 138: R_ARM_THM_BF18, +} + + +__all__ = ("relocation_table_arm",) diff --git a/cle/backends/elf/relocation/arm64.py b/cle/backends/elf/relocation/arm64.py index ef15576c..c3558215 100644 --- a/cle/backends/elf/relocation/arm64.py +++ b/cle/backends/elf/relocation/arm64.py @@ -1,3 +1,8 @@ +"""Relocations for AARCH64 + +Reference: https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst#relocation +""" + from __future__ import annotations import logging @@ -18,9 +23,6 @@ log = logging.getLogger(name=__name__) -# https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst -arch = "AARCH64" - class R_AARCH64_ABS64(GenericAbsoluteAddendReloc): pass @@ -267,3 +269,136 @@ def relocate(self): imm = (self.value & 0b1111_1111_1111) >> 4 self.owner.memory.pack_word(self.relative_addr, instr | (imm << 10), size=4) return True + + +relocation_table_arm64 = { + 257: R_AARCH64_ABS64, + 258: R_AARCH64_COPY, + # 259: R_AARCH64_ABS16, + # 260: R_AARCH64_PREL64, + 261: R_AARCH64_PREL32, + # 262: R_AARCH64_PREL16, + # 263: R_AARCH64_MOVW_UABS_G0, + # 264: R_AARCH64_MOVW_UABS_G0_NC, + # 265: R_AARCH64_MOVW_UABS_G1, + # 266: R_AARCH64_MOVW_UABS_G1_NC, + # 267: R_AARCH64_MOVW_UABS_G2, + # 268: R_AARCH64_MOVW_UABS_G2_NC, + # 269: R_AARCH64_MOVW_UABS_G3, + # 270: R_AARCH64_MOVW_SABS_G0, + # 271: R_AARCH64_MOVW_SABS_G1, + # 272: R_AARCH64_MOVW_SABS_G2, + # 273: R_AARCH64_LD_PREL_LO19, + # 274: R_AARCH64_ADR_PREL_LO21, + 275: R_AARCH64_ADR_PREL_PG_HI21, + # 276: R_AARCH64_ADR_PREL_PG_HI21_NC, + 277: R_AARCH64_ADD_ABS_LO12_NC, + 278: R_AARCH64_LDST8_ABS_LO12_NC, + # 279: R_AARCH64_TSTBR14, + # 280: R_AARCH64_CONDBR19, + 282: R_AARCH64_JUMP26, + 283: R_AARCH64_CALL26, + 284: R_AARCH64_LDST16_ABS_LO12_NC, + 285: R_AARCH64_LDST32_ABS_LO12_NC, + 286: R_AARCH64_LDST64_ABS_LO12_NC, + # 287: R_AARCH64_MOVW_PREL_G0, + # 288: R_AARCH64_MOVW_PREL_G0_NC, + # 289: R_AARCH64_MOVW_PREL_G1, + # 290: R_AARCH64_MOVW_PREL_G1_NC, + # 291: R_AARCH64_MOVW_PREL_G2, + # 292: R_AARCH64_MOVW_PREL_G2_NC, + # 293: R_AARCH64_MOVW_PREL_G3, + 299: R_AARCH64_LDST128_ABS_LO12_NC, + # 300: R_AARCH64_MOVW_GOTOFF_G0, + # 301: R_AARCH64_MOVW_GOTOFF_G0_NC, + # 302: R_AARCH64_MOVW_GOTOFF_G1, + # 303: R_AARCH64_MOVW_GOTOFF_G1_NC, + # 304: R_AARCH64_MOVW_GOTOFF_G2, + # 305: R_AARCH64_MOVW_GOTOFF_G2_NC, + # 306: R_AARCH64_MOVW_GOTOFF_G3, + # 307: R_AARCH64_GOTREL64, + # 308: R_AARCH64_GOTREL32, + # 309: R_AARCH64_GOT_LD_PREL19, + # 310: R_AARCH64_LD64_GOTOFF_LO15, + # 311: R_AARCH64_ADR_GOT_PAGE, + # 312: R_AARCH64_LD64_GOT_LO12_NC, + # 313: R_AARCH64_LD64_GOTPAGE_LO15, + # 314: R_AARCH64_PLT32, + # 315: R_AARCH64_GOTPCREL32, + # 512: R_AARCH64_TLSGD_ADR_PREL21, + # 513: R_AARCH64_TLSGD_ADR_PAGE21, + # 514: R_AARCH64_TLSGD_ADD_LO12_NC, + # 515: R_AARCH64_TLSGD_MOVW_G1, + # 516: R_AARCH64_TLSGD_MOVW_G0_NC, + # 517: R_AARCH64_TLSLD_ADR_PREL21, + # 518: R_AARCH64_TLSLD_ADR_PAGE21, + # 519: R_AARCH64_TLSLD_ADD_LO12_NC, + # 520: R_AARCH64_TLSLD_MOVW_G1, + # 521: R_AARCH64_TLSLD_MOVW_G0_NC, + # 522: R_AARCH64_TLSLD_LD_PREL19, + # 523: R_AARCH64_TLSLD_MOVW_DTPREL_G2, + # 524: R_AARCH64_TLSLD_MOVW_DTPREL_G1, + # 525: R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC, + # 526: R_AARCH64_TLSLD_MOVW_DTPREL_G0, + # 527: R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC, + # 528: R_AARCH64_TLSLD_ADD_DTPREL_HI12, + # 529: R_AARCH64_TLSLD_ADD_DTPREL_LO12, + # 530: R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC, + # 531: R_AARCH64_TLSLD_LDST8_DTPREL_LO12, + # 532: R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC, + # 533: R_AARCH64_TLSLD_LDST16_DTPREL_LO12, + # 534: R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC, + # 535: R_AARCH64_TLSLD_LDST32_DTPREL_LO12, + # 536: R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC, + # 537: R_AARCH64_TLSLD_LDST64_DTPREL_LO12, + # 538: R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC, + # 539: R_AARCH64_TLSIE_MOVW_GOTTPREL_G1, + # 540: R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC, + # 541: R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, + # 542: R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, + # 543: R_AARCH64_TLSIE_LD_GOTTPREL_PREL19, + # 544: R_AARCH64_TLSLE_MOVW_TPREL_G2, + # 545: R_AARCH64_TLSLE_MOVW_TPREL_G1, + # 546: R_AARCH64_TLSLE_MOVW_TPREL_G1_NC, + # 547: R_AARCH64_TLSLE_MOVW_TPREL_G0, + # 548: R_AARCH64_TLSLE_MOVW_TPREL_G0_NC, + # 549: R_AARCH64_TLSLE_ADD_TPREL_HI12, + # 550: R_AARCH64_TLSLE_ADD_TPREL_LO12, + # 551: R_AARCH64_TLSLE_ADD_TPREL_LO12_NC, + # 552: R_AARCH64_TLSLE_LDST8_TPREL_LO12, + # 553: R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC, + # 554: R_AARCH64_TLSLE_LDST16_TPREL_LO12, + # 555: R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC, + # 556: R_AARCH64_TLSLE_LDST32_TPREL_LO12, + # 557: R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC, + # 558: R_AARCH64_TLSLE_LDST64_TPREL_LO12, + # 559: R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC, + # 560: R_AARCH64_TLSDESC_LD_PREL19, + # 561: R_AARCH64_TLSDESC_ADR_PREL21, + # 562: R_AARCH64_TLSDESC_ADR_PAGE21, + # 563: R_AARCH64_TLSDESC_LD64_LO12, + # 564: R_AARCH64_TLSDESC_ADD_LO12, + # 565: R_AARCH64_TLSDESC_OFF_G1, + # 566: R_AARCH64_TLSDESC_OFF_G0_NC, + # 567: R_AARCH64_TLSDESC_LDR, + # 568: R_AARCH64_TLSDESC_ADD, + # 569: R_AARCH64_TLSDESC_CALL, + # 570: R_AARCH64_TLSLE_LDST128_TPREL_LO12, + # 571: R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC, + # 572: R_AARCH64_TLSLD_LDST128_DTPREL_LO12, + # 573: R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC, + # 580: R_AARCH64_AUTH_ABS64, + 1024: R_AARCH64_COPY, + 1025: R_AARCH64_GLOB_DAT, + 1026: R_AARCH64_JUMP_SLOT, + 1027: R_AARCH64_RELATIVE, + 1028: R_AARCH64_TLS_DTPMOD, # R_AARCH64_TLS_IMPDEF1 + 1029: R_AARCH64_TLS_DTPREL, # R_AARCH64_TLS_IMPDEF2 + 1030: R_AARCH64_TLS_TPREL, + 1031: R_AARCH64_TLSDESC, + 1032: R_AARCH64_IRELATIVE, + # 1040: R_AARCH64_AUTH_ABS64, + # 1041: R_AARCH64_AUTHELATIVE, +} + +__all__ = ("relocation_table_arm64",) diff --git a/cle/backends/elf/relocation/arm_cortex_m.py b/cle/backends/elf/relocation/arm_cortex_m.py deleted file mode 100644 index 1d2ddfbc..00000000 --- a/cle/backends/elf/relocation/arm_cortex_m.py +++ /dev/null @@ -1,54 +0,0 @@ -from __future__ import annotations - -from .arm import ( - R_ARM_ABS32, - R_ARM_ABS32_NOI, - R_ARM_CALL, - R_ARM_COPY, - R_ARM_GLOB_DAT, - R_ARM_JUMP24, - R_ARM_JUMP_SLOT, - R_ARM_MOVT_ABS, - R_ARM_MOVW_ABS_NC, - R_ARM_PC24, - R_ARM_PREL31, - R_ARM_REL32, - R_ARM_REL32_NOI, - R_ARM_RELATIVE, - R_ARM_THM_CALL, - R_ARM_THM_JUMP6, - R_ARM_THM_JUMP19, - R_ARM_THM_JUMP24, - R_ARM_THM_MOVT_ABS, - R_ARM_THM_MOVW_ABS_NC, - R_ARM_TLS_DTPOFF32, - R_ARM_TLS_TPOFF32, -) - -arch = "ARMCortexM" - -__all__ = [ - "arch", - "R_ARM_CALL", - "R_ARM_PREL31", - "R_ARM_REL32", - "R_ARM_ABS32", - "R_ARM_MOVW_ABS_NC", - "R_ARM_MOVT_ABS", - "R_ARM_THM_CALL", - "R_ARM_COPY", - "R_ARM_GLOB_DAT", - "R_ARM_JUMP_SLOT", - "R_ARM_RELATIVE", - "R_ARM_ABS32_NOI", - "R_ARM_REL32_NOI", - "R_ARM_TLS_DTPOFF32", - "R_ARM_TLS_TPOFF32", - "R_ARM_JUMP24", - "R_ARM_PC24", - "R_ARM_THM_JUMP24", - "R_ARM_THM_JUMP19", - "R_ARM_THM_JUMP6", - "R_ARM_THM_MOVW_ABS_NC", - "R_ARM_THM_MOVT_ABS", -] diff --git a/cle/backends/elf/relocation/armel.py b/cle/backends/elf/relocation/armel.py deleted file mode 100644 index 0ec6c173..00000000 --- a/cle/backends/elf/relocation/armel.py +++ /dev/null @@ -1,58 +0,0 @@ -from __future__ import annotations - -from .arm import ( - R_ARM_ABS32, - R_ARM_ABS32_NOI, - R_ARM_CALL, - R_ARM_COPY, - R_ARM_GLOB_DAT, - R_ARM_GOT_PREL, - R_ARM_JUMP24, - R_ARM_JUMP_SLOT, - R_ARM_MOVT_ABS, - R_ARM_MOVW_ABS_NC, - R_ARM_PC24, - R_ARM_PREL31, - R_ARM_REL32, - R_ARM_REL32_NOI, - R_ARM_RELATIVE, - R_ARM_THM_CALL, - R_ARM_THM_JUMP6, - R_ARM_THM_JUMP19, - R_ARM_THM_JUMP24, - R_ARM_THM_MOVT_ABS, - R_ARM_THM_MOVW_ABS_NC, - R_ARM_TLS_DTPMOD32, - R_ARM_TLS_DTPOFF32, - R_ARM_TLS_TPOFF32, -) - -arch = "ARMEL" - -__all__ = [ - "arch", - "R_ARM_CALL", - "R_ARM_PREL31", - "R_ARM_REL32", - "R_ARM_ABS32", - "R_ARM_MOVW_ABS_NC", - "R_ARM_MOVT_ABS", - "R_ARM_THM_CALL", - "R_ARM_COPY", - "R_ARM_GLOB_DAT", - "R_ARM_GOT_PREL", - "R_ARM_JUMP_SLOT", - "R_ARM_RELATIVE", - "R_ARM_ABS32_NOI", - "R_ARM_REL32_NOI", - "R_ARM_TLS_DTPMOD32", - "R_ARM_TLS_DTPOFF32", - "R_ARM_TLS_TPOFF32", - "R_ARM_JUMP24", - "R_ARM_PC24", - "R_ARM_THM_JUMP24", - "R_ARM_THM_JUMP19", - "R_ARM_THM_JUMP6", - "R_ARM_THM_MOVW_ABS_NC", - "R_ARM_THM_MOVT_ABS", -] diff --git a/cle/backends/elf/relocation/armhf.py b/cle/backends/elf/relocation/armhf.py deleted file mode 100644 index 89100c8b..00000000 --- a/cle/backends/elf/relocation/armhf.py +++ /dev/null @@ -1,58 +0,0 @@ -from __future__ import annotations - -from .arm import ( - R_ARM_ABS32, - R_ARM_ABS32_NOI, - R_ARM_CALL, - R_ARM_COPY, - R_ARM_GLOB_DAT, - R_ARM_GOT_PREL, - R_ARM_JUMP24, - R_ARM_JUMP_SLOT, - R_ARM_MOVT_ABS, - R_ARM_MOVW_ABS_NC, - R_ARM_PC24, - R_ARM_PREL31, - R_ARM_REL32, - R_ARM_REL32_NOI, - R_ARM_RELATIVE, - R_ARM_THM_CALL, - R_ARM_THM_JUMP6, - R_ARM_THM_JUMP19, - R_ARM_THM_JUMP24, - R_ARM_THM_MOVT_ABS, - R_ARM_THM_MOVW_ABS_NC, - R_ARM_TLS_DTPMOD32, - R_ARM_TLS_DTPOFF32, - R_ARM_TLS_TPOFF32, -) - -arch = "ARMHF" - -__all__ = [ - "arch", - "R_ARM_CALL", - "R_ARM_PREL31", - "R_ARM_REL32", - "R_ARM_ABS32", - "R_ARM_MOVW_ABS_NC", - "R_ARM_MOVT_ABS", - "R_ARM_THM_CALL", - "R_ARM_COPY", - "R_ARM_GLOB_DAT", - "R_ARM_GOT_PREL", - "R_ARM_JUMP_SLOT", - "R_ARM_RELATIVE", - "R_ARM_ABS32_NOI", - "R_ARM_REL32_NOI", - "R_ARM_TLS_DTPMOD32", - "R_ARM_TLS_DTPOFF32", - "R_ARM_TLS_TPOFF32", - "R_ARM_JUMP24", - "R_ARM_PC24", - "R_ARM_THM_JUMP24", - "R_ARM_THM_JUMP19", - "R_ARM_THM_JUMP6", - "R_ARM_THM_MOVW_ABS_NC", - "R_ARM_THM_MOVT_ABS", -] diff --git a/cle/backends/elf/relocation/i386.py b/cle/backends/elf/relocation/i386.py index 2c385738..09918cab 100644 --- a/cle/backends/elf/relocation/i386.py +++ b/cle/backends/elf/relocation/i386.py @@ -1,3 +1,8 @@ +"""Relocation types for i386. + +Reference: https://github.com/hjl-tools/x86-psABI/wiki/intel386-psABI-1.1.pdf page 36 +""" + from __future__ import annotations from .generic import ( @@ -13,8 +18,6 @@ RelocGOTMixin, ) -arch = "X86" - class R_386_32(GenericAbsoluteAddendReloc): """ @@ -110,3 +113,49 @@ class R_386_GOTPC(GenericPCRelativeAddendReloc, RelocGOTMixin): Field: word32 Calculation: GOT + A - P """ + + +relocation_table_i386 = { + 1: R_386_32, + 2: R_386_PC32, + # 3: R_386_GOT32, + 4: R_386_PLT32, + 5: R_386_COPY, + 6: R_386_GLOB_DAT, + 7: R_386_JMP_SLOT, + 8: R_386_RELATIVE, + # 9: R_386_GOTOFF, + 10: R_386_GOTPC, + 14: R_386_TLS_TPOFF, + # 15: R_386_TLS_IE, + # 16: R_386_TLS_GOTIE, + # 17: R_386_TLS_LE, + # 18: R_386_TLS_GD, + # 19: R_386_TLS_LDM, + # 20: R_386_16, + # 21: R_386_PC16, + # 22: R_386_8, + # 23: R_386_PC8, + # 24: R_386_TLS_GD_32, + # 25: R_386_TLS_GD_PUSH, + # 26: R_386_TLS_GD_CALL, + # 27: R_386_TLS_GD_POP, + # 28: R_386_TLS_LDM_32, + # 29: R_386_TLS_LDM_PUSH, + # 30: R_386_TLS_LDM_CALL, + # 31: R_386_TLS_LDM_POP, + # 32: R_386_TLS_LDO_32, + # 33: R_386_TLS_IE_32, + # 34: R_386_TLS_LE_32, + 35: R_386_TLS_DTPMOD32, + 36: R_386_TLS_DTPOFF32, + # 37: R_386_TLS_TPOFF32, + # 38: R_386_SIZE32, + # 39: R_386_TLS_GOTDESC, + # 40: R_386_TLS_DESC_CALL, + # 41: R_386_TLS_DESC, + 42: R_386_IRELATIVE, + # 43: R_386_GOT32X, +} + +__all__ = ("relocation_table_i386",) diff --git a/cle/backends/elf/relocation/mips.py b/cle/backends/elf/relocation/mips.py index 9d85ad21..2f72fb3d 100644 --- a/cle/backends/elf/relocation/mips.py +++ b/cle/backends/elf/relocation/mips.py @@ -1,15 +1,26 @@ +"""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. +""" + from __future__ import annotations from .generic import ( GenericAbsoluteAddendReloc, GenericAbsoluteReloc, + GenericCopyReloc, GenericRelativeReloc, GenericTLSDoffsetReloc, GenericTLSModIdReloc, GenericTLSOffsetReloc, ) -arch = "MIPS32" +# pylint: disable=missing-class-docstring class R_MIPS_32(GenericAbsoluteAddendReloc): @@ -56,3 +67,93 @@ def relocate(self): self.owner.memory.pack_word(self.dest_addr, self.value & 0xFFFF, size=2) return True + + +class R_MIPS_64(GenericAbsoluteAddendReloc): + pass + + +class R_MIPS_COPY(GenericCopyReloc): + pass + + +class R_MIPS_TLS_DTPMOD64(GenericTLSModIdReloc): + pass + + +class R_MIPS_TLS_DTPREL64(GenericTLSDoffsetReloc): + pass + + +class R_MIPS_TLS_TPREL64(GenericTLSOffsetReloc): + pass + + +relocation_table_mips = { + # 1: R_MIPS_16, + 2: R_MIPS_32, + 3: R_MIPS_REL32, + # 4: R_MIPS_26, + 5: R_MIPS_HI16, + 6: R_MIPS_LO16, + # 7: R_MIPS_GPREL16, + # 8: R_MIPS_LITERAL, + # 9: R_MIPS_GOT16, + # 10: R_MIPS_PC16, + # 11: R_MIPS_CALL16, + # 12: R_MIPS_GPREL32, + # 13: R_MIPS_UNUSED1, + # 14: R_MIPS_UNUSED2, + # 15: R_MIPS_UNUSED3, + # 16: R_MIPS_SHIFT5, + # 17: R_MIPS_SHIFT6, + 18: R_MIPS_64, + # 19: R_MIPS_GOT_DISP, + # 20: R_MIPS_GOT_PAGE, + # 21: R_MIPS_GOT_OFST, + # 22: R_MIPS_GOT_HI16, + # 23: R_MIPS_GOT_LO16, + # 24: R_MIPS_SUB, + # 25: R_MIPS_INSERT_A, + # 26: R_MIPS_INSERT_B, + # 27: R_MIPS_DELETE, + # 28: R_MIPS_HIGHER, + # 29: R_MIPS_HIGHEST, + # 30: R_MIPS_CALL_HI16, + # 31: R_MIPS_CALL_LO16, + # 32: R_MIPS_SCN_DISP, + # 33: R_MIPS_REL16, + # 34: R_MIPS_ADD_IMMEDIATE, + # 35: R_MIPS_PJUMP, + # 36: R_MIPS_RELGOT, + # 37: R_MIPS_JALR, + 38: R_MIPS_TLS_DTPMOD32, + 39: R_MIPS_TLS_DTPREL32, + 40: R_MIPS_TLS_DTPMOD64, + 41: R_MIPS_TLS_DTPREL64, + # 42: R_MIPS_TLS_GD, + # 43: R_MIPS_TLS_LDM, + # 44: R_MIPS_TLS_DTPREL_HI16, + # 45: R_MIPS_TLS_DTPREL_LO16, + # 46: R_MIPS_TLS_GOTTPREL, + 47: R_MIPS_TLS_TPREL32, + 48: R_MIPS_TLS_TPREL64, + # 49: R_MIPS_TLS_TPREL_HI16, + # 50: R_MIPS_TLS_TPREL_LO16, + 51: R_MIPS_GLOB_DAT, + # 60: R_MIPS_PC21_S2, + # 61: R_MIPS_PC26_S2, + # 62: R_MIPS_PC18_S3, + # 63: R_MIPS_PC19_S2, + # 64: R_MIPS_PCHI16, + # 65: R_MIPS_PCLO16, + 126: R_MIPS_COPY, + 127: R_MIPS_JUMP_SLOT, + # 248: R_MIPS_PC32, + # 249: R_MIPS_EH, + # 250: R_MIPS_GNU_REL16_S2, + # 253: R_MIPS_GNU_VTINHERIT, + # 254: R_MIPS_GNU_VTENTRY, +} + +__all__ = ("relocation_table_mips",) diff --git a/cle/backends/elf/relocation/mips64.py b/cle/backends/elf/relocation/mips64.py deleted file mode 100644 index 1a80e96b..00000000 --- a/cle/backends/elf/relocation/mips64.py +++ /dev/null @@ -1,36 +0,0 @@ -from __future__ import annotations - -from .generic import ( - GenericAbsoluteAddendReloc, - GenericCopyReloc, - GenericRelativeReloc, - GenericTLSDoffsetReloc, - GenericTLSModIdReloc, - GenericTLSOffsetReloc, -) - -arch = "MIPS64" - - -class R_MIPS_64(GenericAbsoluteAddendReloc): - pass - - -class R_MIPS_REL32(GenericRelativeReloc): - pass - - -class R_MIPS_COPY(GenericCopyReloc): - pass - - -class R_MIPS_TLS_DTPMOD64(GenericTLSModIdReloc): - pass - - -class R_MIPS_TLS_DTPREL64(GenericTLSDoffsetReloc): - pass - - -class R_MIPS_TLS_TPREL64(GenericTLSOffsetReloc): - pass diff --git a/cle/backends/elf/relocation/ppc.py b/cle/backends/elf/relocation/ppc.py index 4c1a7dc1..75dcfe1f 100644 --- a/cle/backends/elf/relocation/ppc.py +++ b/cle/backends/elf/relocation/ppc.py @@ -1,3 +1,11 @@ +"""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. +""" + from __future__ import annotations import logging @@ -14,10 +22,6 @@ ) log = logging.getLogger(name=__name__) -arch = "PPC32" - -# Reference: System V Application Binary Interface, PowerPC Processor Supplement -# http://refspecs.linux-foundation.org/elf/elfspec_ppc.pdf # PPC constants/masks to be used in relocations @@ -65,7 +69,7 @@ def value(self): return result -class R_PPC_ADDR16_LO(ELFReloc): # pylint: disable=undefined-variable +class R_PPC_ADDR16_LO(ELFReloc): """ Relocation Type: 0x4 Calculation: #lo(S + A) @@ -182,7 +186,7 @@ def value(self): return result -class R_PPC_REL24(ELFReloc): # pylint: disable=undefined-variable +class R_PPC_REL24(ELFReloc): """ Relocation Type: 0xa Calculation: (S + A - P) >> 2 @@ -434,3 +438,76 @@ class R_PPC_DTPREL32(GenericTLSDoffsetReloc): class R_PPC_TPREL32(GenericTLSOffsetReloc): pass + + +relocation_table_ppc = { + 1: R_PPC_ADDR32, + 2: R_PPC_ADDR24, + 3: R_PPC_ADDR16, + 4: R_PPC_ADDR16_LO, + 5: R_PPC_ADDR16_HI, + 6: R_PPC_ADDR16_HA, + 7: R_PPC_ADDR14, + 8: R_PPC_ADDR14_BRTAKEN, + 9: R_PPC_ADDR14_BRNTAKEN, + 10: R_PPC_REL24, + 11: R_PPC_REL14, + 12: R_PPC_REL14_BRTAKEN, + 13: R_PPC_REL14_BRNTAKEN, + # 14: R_PPC_GOT16, + # 15: R_PPC_GOT16_LO, + # 16: R_PPC_GOT16_HI, + # 17: R_PPC_GOT16_HA, + # 18: R_PPC_PLTREL24, + 19: R_PPC_COPY, + 20: R_PPC_GLOB_DAT, + 21: R_PPC_JMP_SLOT, + 22: R_PPC_RELATIVE, + # 23: R_PPC_LOCAL24PC, + 24: R_PPC_UADDR32, + 25: R_PPC_UADDR16, + 26: R_PPC_REL32, + # 27: R_PPC_PLT32, + # 28: R_PPC_PLTREL32, + # 29: R_PPC_PLT16_LO, + # 30: R_PPC_PLT16_HI, + # 31: R_PPC_PLT16_HA, + # 32: R_PPC_SDAREL16, + 33: R_PPC_SECTOFF, + 34: R_PPC_SECTOFF_LO, + 35: R_PPC_SECTOFF_HI, + 36: R_PPC_SECTOFF_HA, + 37: R_PPC_ADDR30, + # 67: R_PPC_TLS, + 68: R_PPC_DTPMOD32, + # 69: R_PPC_TPREL16, + # 70: R_PPC_TPREL16_LO, + # 71: R_PPC_TPREL16_HI, + # 72: R_PPC_TPREL16_HA, + 73: R_PPC_TPREL32, + # 74: R_PPC_DTPREL16, + # 75: R_PPC_DTPREL16_LO, + # 76: R_PPC_DTPREL16_HI, + # 77: R_PPC_DTPREL16_HA, + 78: R_PPC_DTPREL32, + # 79: R_PPC_GOT_TLSGD16, + # 80: R_PPC_GOT_TLSGD16_LO, + # 81: R_PPC_GOT_TLSGD16_HI, + # 82: R_PPC_GOT_TLSGD16_HA, + # 83: R_PPC_GOT_TLSLD16, + # 84: R_PPC_GOT_TLSLD16_LO, + # 85: R_PPC_GOT_TLSLD16_HI, + # 86: R_PPC_GOT_TLSLD16_HA, + # 87: R_PPC_GOT_TPREL16, + # 88: R_PPC_GOT_TPREL16_LO, + # 89: R_PPC_GOT_TPREL16_HI, + # 90: R_PPC_GOT_TPREL16_HA, + # 91: R_PPC_GOT_DTPREL16, + # 92: R_PPC_GOT_DTPREL16_LO, + # 93: R_PPC_GOT_DTPREL16_HI, + # 94: R_PPC_GOT_DTPREL16_HA, + # 95: R_PPC_TLSGD, + # 96: R_PPC_TLSLD, +} + +__all__ = ("relocation_table_ppc",) diff --git a/cle/backends/elf/relocation/pcc64.py b/cle/backends/elf/relocation/ppc64.py similarity index 57% rename from cle/backends/elf/relocation/pcc64.py rename to cle/backends/elf/relocation/ppc64.py index 12b07d52..01a58fd0 100644 --- a/cle/backends/elf/relocation/pcc64.py +++ b/cle/backends/elf/relocation/ppc64.py @@ -1,3 +1,8 @@ +"""Relocation types for PPC64. + +Reference: http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.pdf pages 57-59 +""" + from __future__ import annotations import logging @@ -15,8 +20,7 @@ log = logging.getLogger(name=__name__) -# http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.pdf -arch = "PPC64" +# pylint: disable=missing-class-docstring class R_PPC64_JMP_SLOT(ELFReloc): @@ -172,3 +176,117 @@ def value(self): log.warning(".TOC. value not found") return 0 return self.owner.ppc64_initial_rtoc + + +relocation_table_ppc64 = { + # 1: R_PPC64_ADDR32, + # 2: R_PPC64_ADDR24, + # 3: R_PPC64_ADDR16, + # 4: R_PPC64_ADDR16_LO, + # 5: R_PPC64_ADDR16_HI, + # 6: R_PPC64_ADDR16_HA, + # 7: R_PPC64_ADDR14, + # 8: R_PPC64_ADDR14_BRTAKEN, + # 9: R_PPC64_ADDR14_BRNTAKEN, + 10: R_PPC64_REL24, + # 11: R_PPC64_REL14, + # 12: R_PPC64_REL14_BRTAKEN, + # 13: R_PPC64_REL14_BRNTAKEN, + # 14: R_PPC64_GOT16, + # 15: R_PPC64_GOT16_LO, + # 16: R_PPC64_GOT16_HI, + # 17: R_PPC64_GOT16_HA, + # No 18 in doc + # 19: R_PPC64_COPY, + 20: R_PPC64_GLOB_DAT, + 21: R_PPC64_JMP_SLOT, + 22: R_PPC64_RELATIVE, + # No 23 in doc + # 24: R_PPC64_UADDR32, + # 25: R_PPC64_UADDR16, + # 26: R_PPC64_REL32, + # 27: R_PPC64_PLT32, + # 28: R_PPC64_PLTREL32, + # 29: R_PPC64_PLT16_LO, + # 30: R_PPC64_PLT16_HI, + # 31: R_PPC64_PLT16_HA, + # No 32 in doc + # 33: R_PPC64_SECTOFF, + # 34: R_PPC64_SECTOFF_LO, + # 35: R_PPC64_SECTOFF_HI, + # 36: R_PPC64_SECTOFF_HA, + # 37: R_PPC64_ADDR30, + 38: R_PPC64_ADDR64, + # 39: R_PPC64_ADDR16_HIGHER, + # 40: R_PPC64_ADDR16_HIGHERA, + # 41: R_PPC64_ADDR16_HIGHEST, + # 42: R_PPC64_ADDR16_HIGHESTA, + # 43: R_PPC64_UADDR64, + # 44: R_PPC64_REL64, + # 45: R_PPC64_PLT64, + # 46: R_PPC64_PLTREL64, + # 47: R_PPC64_TOC16, + 48: R_PPC64_TOC16_LO, + 49: R_PPC64_TOC16_HI, + 50: R_PPC64_TOC16_HA, + 51: R_PPC64_TOC, + # 52: R_PPC64_PLTGOT16, + # 53: R_PPC64_PLTGOT16_LO, + # 54: R_PPC64_PLTGOT16_HI, + # 55: R_PPC64_PLTGOT16_HA, + # 56: R_PPC64_ADDR16_DS, + # 57: R_PPC64_ADDR16_LO_DS, + # 58: R_PPC64_GOT16_DS, + # 59: R_PPC64_GOT16_LO_DS, + # 60: R_PPC64_PLT16_LO_DS, + # 61: R_PPC64_SECTOFF_DS, + # 62: R_PPC64_SECTOFF_LO_DS, + # 63: R_PPC64_TOC16_DS, + # 64: R_PPC64_TOC16_LO_DS, + # 65: R_PPC64_PLTGOT16_DS, + # 66: R_PPC64_PLTGOT16_LO_DS, + # 67: R_PPC64_TLS, + 68: R_PPC64_DTPMOD64, + # 69: R_PPC64_TPREL16, + # 70: R_PPC64_TPREL16_LO, + # 71: R_PPC64_TPREL16_HI, + # 72: R_PPC64_TPREL16_HA, + 73: R_PPC64_TPREL64, + # 74: R_PPC64_DTPREL16, + # 75: R_PPC64_DTPREL16_LO, + # 76: R_PPC64_DTPREL16_HI, + # 77: R_PPC64_DTPREL16_HA, + 78: R_PPC64_DTPREL64, + # 79: R_PPC64_GOT_TLSGD16, + # 80: R_PPC64_GOT_TLSGD16_LO, + # 81: R_PPC64_GOT_TLSGD16_HI, + # 82: R_PPC64_GOT_TLSGD16_HA, + # 83: R_PPC64_GOT_TLSLD16, + # 84: R_PPC64_GOT_TLSLD16_LO, + # 85: R_PPC64_GOT_TLSLD16_HI, + # 86: R_PPC64_GOT_TLSLD16_HA, + # 87: R_PPC64_GOT_TPREL16_DS, + # 88: R_PPC64_GOT_TPREL16_LO_DS, + # 89: R_PPC64_GOT_TPREL16_HI, + # 90: R_PPC64_GOT_TPREL16_HA, + # 91: R_PPC64_GOT_DTPREL16_DS, + # 92: R_PPC64_GOT_DTPREL16_LO_DS, + # 93: R_PPC64_GOT_DTPREL16_HI, + # 94: R_PPC64_GOT_DTPREL16_HA, + # 95: R_PPC64_TPREL16_DS, + # 96: R_PPC64_TPREL16_LO_DS, + # 97: R_PPC64_TPREL16_HIGHER, + # 98: R_PPC64_TPREL16_HIGHERA, + # 99: R_PPC64_TPREL16_HIGHEST, + # 100: R_PPC64_TPREL16_HIGHESTA, + # 101: R_PPC64_DTPREL16_DS, + # 102: R_PPC64_DTPREL16_LO_DS, + # 103: R_PPC64_DTPREL16_HIGHER, + # 104: R_PPC64_DTPREL16_HIGHERA, + # 105: R_PPC64_DTPREL16_HIGHEST, + # 106: R_PPC64_DTPREL16_HIGHESTA, + # Not in spec + 248: R_PPC64_IRELATIVE, +} + +__all__ = ("relocation_table_ppc64",) diff --git a/cle/backends/elf/relocation/s390x.py b/cle/backends/elf/relocation/s390x.py index 4a86dea4..44f87532 100644 --- a/cle/backends/elf/relocation/s390x.py +++ b/cle/backends/elf/relocation/s390x.py @@ -1,3 +1,8 @@ +"""Relocation types for the S390X architecture. + +Reference: https://github.com/IBM/s390x-abi/releases/download/v1.6.1/lzsabi_s390x.pdf pages 51-52 +""" + from __future__ import annotations from .generic import ( @@ -9,8 +14,6 @@ GenericTLSOffsetReloc, ) -arch = "S390X" - class R_390_GLOB_DAT(GenericJumpslotReloc): pass @@ -38,3 +41,74 @@ class R_390_IRELATIVE(GenericIRelativeReloc): class R_390_COPY(GenericCopyReloc): pass + + +relocation_table_s390x = { + # 1: R_390_8, + # 2: R_390_12, + # 3: R_390_16, + # 4: R_390_32, + # 5: R_390_PC32, + # 6: R_390_GOT12, + # 7: R_390_GOT32, + # 8: R_390_PLT32, + 9: R_390_COPY, + 10: R_390_GLOB_DAT, + 11: R_390_JMP_SLOT, + 12: R_390_RELATIVE, + # 13: R_390_GOTOFF32, + # 14: R_390_GOTPC, + # 15: R_390_GOT16, + # 16: R_390_PC16, + # 17: R_390_PC16DBL, + # 18: R_390_PLT16DBL, + # 19: R_390_PC32DBL, + # 20: R_390_PLT32DBL, + # 21: R_390_GOTPCDBL, + 22: R_390_64, + # 23: R_390_PC64, + # 24: R_390_GOT64, + # 25: R_390_PLT64, + # 26: R_390_GOTENT, + # 27: R_390_GOTOFF16, + # 28: R_390_GOTOFF64, + # 29: R_390_GOTPLT12, + # 30: R_390_GOTPLT16, + # 31: R_390_GOTPLT32, + # 32: R_390_GOTPLT64, + # 33: R_390_GOTPLTENT, + # 34: R_390_PLTOFF16, + # 35: R_390_PLTOFF32, + # 36: R_390_PLTOFF64, + # 37: R_390_TLS_LOAD, + # 38: R_390_TLS_GDCALL, + # 39: R_390_TLS_LDCALL, + # No 40 in doc + # 41: R_390_TLS_GD64, + # 42: R_390_TLS_GOTIE12, + # No 43 in doc + # 44: R_390_TLS_GOTIE64, + # No 45 in doc + # 46: R_390_TLS_LDM64, + # No 47 in doc + # 48: R_390_TLS_IE64, + # 49: R_390_TLS_IEENT, + # No 50 in doc + # 51: R_390_TLS_LE64, + # No 52 in doc + # 53: R_390_TLS_LDO64, + # 54: R_390_TLS_DTPMOD, + # 55: R_390_TLS_DTPOFF, + 56: R_390_TLS_TPOFF, + # 57: R_390_20, + # 58: R_390_GOT20, + # 59: R_390_GOTPLT20, + # 60: R_390_TLS_GOTIE20, + 61: R_390_IRELATIVE, + # 62: R_390_PC12DBL, + # 63: R_390_PLT12DBL, + # 64: R_390_PC24DBL, + # 65: R_390_PLT24DBL, +} + +__all__ = ("relocation_table_s390x",) diff --git a/cle/backends/elf/relocation/sparc.py b/cle/backends/elf/relocation/sparc.py index 59069fa0..70447ba6 100644 --- a/cle/backends/elf/relocation/sparc.py +++ b/cle/backends/elf/relocation/sparc.py @@ -1,10 +1,11 @@ -from __future__ import annotations +"""Relocations for SPARC -from .elfreloc import ELFReloc +Reference: https://sparc.org/wp-content/uploads/2014/01/psABI3rd.pdf.gz page 4-4 +""" -arch = "sparc:BE:32:default" +from __future__ import annotations -# Check The SPARC Architecture Manual for field definitions. +from .elfreloc import ELFReloc class R_SPARC_HI22(ELFReloc): @@ -50,3 +51,32 @@ def value(self): instr_bytes = self.owner.memory.load(self.relative_addr, 4) instr = int.from_bytes(instr_bytes, byteorder="big") return instr & 0xFFFFE000 | result & 0x1FFF + + +relocation_table_sparc = { + # 1: R_SPARC_8, + # 2: R_SPARC_16, + # 3: R_SPARC_32, + # 4: R_SPARC_DISP8, + # 5: R_SPARC_DISP16, + # 6: R_SPARC_DISP32, + 7: R_SPARC_WDISP30, + # 8: R_SPARC_WDISP22, + 9: R_SPARC_HI22, + # 10: R_SPARC_22, + # 11: R_SPARC_13, + 12: R_SPARC_LO10, + # 13: R_SPARC_GOT10, + # 14: R_SPARC_GOT13, + # 15: R_SPARC_GOT22, + # 16: R_SPARC_PC10, + # 17: R_SPARC_PC22, + # 18: R_SPARC_WPLT30, + # 19: R_SPARC_COPY, + # 20: R_SPARC_GLOB_DAT, + # 21: R_SPARC_JMP_SLOT, + # 22: R_SPARC_RELATIVE, + # 23: R_SPARC_UA32, +} + +__all__ = ("relocation_table_sparc",) diff --git a/cle/backends/pe/relocation/__init__.py b/cle/backends/pe/relocation/__init__.py index d4138499..f80a361e 100644 --- a/cle/backends/pe/relocation/__init__.py +++ b/cle/backends/pe/relocation/__init__.py @@ -1,44 +1,22 @@ from __future__ import annotations -import importlib import logging -import os -from collections import defaultdict -import archinfo +from .arm import relocation_table_arm +from .generic import relocation_table_generic +from .mips import relocation_table_mips +from .riscv import relocation_table_riscv -from cle.backends.relocation import Relocation +ALL_RELOCATIONS = { + "AMD64": relocation_table_generic, + "arm": relocation_table_generic | relocation_table_arm, + "X86": relocation_table_generic, + "mips": relocation_table_generic | relocation_table_mips, + "RISCV": relocation_table_generic | relocation_table_riscv, +} -ALL_RELOCATIONS = defaultdict(dict) -complaint_log = set() - -path = os.path.dirname(os.path.abspath(__file__)) log = logging.getLogger(name=__name__) - - -def load_relocations(): - for filename in os.listdir(path): - if not filename.endswith(".py"): - continue - if filename == "__init__.py": - continue - - log.debug("Importing PE relocation module: %s", filename[:-3]) - module = importlib.import_module(f".{filename[:-3]}", "cle.backends.pe.relocation") - - try: - arch_name = module.arch - except AttributeError: - continue - - for item_name in dir(module): - if item_name not in archinfo.defines: - continue - item = getattr(module, item_name) - if not isinstance(item, type) or not issubclass(item, Relocation): - continue - - ALL_RELOCATIONS[arch_name][archinfo.defines[item_name]] = item +complaint_log = set() def get_relocation(arch, r_type): @@ -51,6 +29,3 @@ def get_relocation(arch, r_type): complaint_log.add((arch, r_type)) log.warning("Unknown reloc %d on %s", r_type, arch) return None - - -load_relocations() diff --git a/cle/backends/pe/relocation/amd64.py b/cle/backends/pe/relocation/amd64.py deleted file mode 100644 index 59f03986..00000000 --- a/cle/backends/pe/relocation/amd64.py +++ /dev/null @@ -1,31 +0,0 @@ -from __future__ import annotations - -from .generic import ( - IMAGE_REL_BASED_DIR64, - IMAGE_REL_BASED_HIGH, - IMAGE_REL_BASED_HIGHADJ, - IMAGE_REL_BASED_HIGHLOW, - IMAGE_REL_BASED_LOW, -) - -arch = "AMD64" - - -class IMAGE_REL_BASED_HIGHADJ(IMAGE_REL_BASED_HIGHADJ): - pass - - -class IMAGE_REL_BASED_DIR64(IMAGE_REL_BASED_DIR64): - pass - - -class IMAGE_REL_BASED_HIGHLOW(IMAGE_REL_BASED_HIGHLOW): - pass - - -class IMAGE_REL_BASED_HIGH(IMAGE_REL_BASED_HIGH): - pass - - -class IMAGE_REL_BASED_LOW(IMAGE_REL_BASED_LOW): - pass diff --git a/cle/backends/pe/relocation/arm.py b/cle/backends/pe/relocation/arm.py index 0419048c..edb04687 100644 --- a/cle/backends/pe/relocation/arm.py +++ b/cle/backends/pe/relocation/arm.py @@ -1,40 +1,19 @@ from __future__ import annotations -from .generic import ( - IMAGE_REL_BASED_DIR64, - IMAGE_REL_BASED_HIGH, - IMAGE_REL_BASED_HIGHADJ, - IMAGE_REL_BASED_HIGHLOW, - IMAGE_REL_BASED_LOW, -) from .pereloc import PEReloc -arch = "arm" - -class IMAGE_REL_BASED_HIGHADJ(IMAGE_REL_BASED_HIGHADJ): - pass - - -class IMAGE_REL_BASED_DIR64(IMAGE_REL_BASED_DIR64): - pass - - -class IMAGE_REL_BASED_HIGHLOW(IMAGE_REL_BASED_HIGHLOW): - pass - - -class IMAGE_REL_BASED_HIGH(IMAGE_REL_BASED_HIGH): +class IMAGE_REL_BASED_ARM_MOV32(PEReloc): pass -class IMAGE_REL_BASED_LOW(IMAGE_REL_BASED_LOW): +class IMAGE_REL_BASED_THUMB_MOV32(PEReloc): pass -class IMAGE_REL_BASED_ARM_MOV32(PEReloc): - pass - +relocation_table_arm = { + 5: IMAGE_REL_BASED_ARM_MOV32, + 7: IMAGE_REL_BASED_THUMB_MOV32, +} -class IMAGE_REL_BASED_THUMB_MOV32(PEReloc): - pass +__all__ = ("relocation_table_arm",) diff --git a/cle/backends/pe/relocation/generic.py b/cle/backends/pe/relocation/generic.py index 645df396..bde57a3f 100644 --- a/cle/backends/pe/relocation/generic.py +++ b/cle/backends/pe/relocation/generic.py @@ -101,3 +101,12 @@ def value(self): adjusted_value = rebased_value & 0x0000FFFF adjusted_bytes = struct.pack("