kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

reloc_x86_64.c (2691B)


      1 /* RelocKind <-> x86_64 PE/COFF reloc-type mapping.  Mirror of
      2  * elf_reloc_x86_64.c for PE/COFF.
      3  *
      4  * PE/COFF's AMD64 reloc set is much narrower than ELF's: only ABSOLUTE,
      5  * ADDR64, ADDR32, ADDR32NB, REL32 (with REL32_1..5 implicit-addend
      6  * variants), plus a few section-relative forms kit does not model in
      7  * v1.  We emit plain REL32 (4) for every PC-relative kind and let the
      8  * explicit Reloc.addend ride in the patched bytes; on the read side
      9  * REL32_1..5 collapse to R_PC32 (the reader applies the implicit
     10  * addend itself).  IMAGE_REL_AMD64_ABSOLUTE (== 0) doubles as the
     11  * "unsupported" sentinel on the _to side, matching the ELF contract. */
     12 
     13 #include "obj/coff/coff.h"
     14 
     15 u32 coff_x86_64_reloc_to(u32 kind /* RelocKind */) {
     16   switch (kind) {
     17     case R_NONE:
     18       return IMAGE_REL_AMD64_ABSOLUTE;
     19     case R_ABS64:
     20       return IMAGE_REL_AMD64_ADDR64;
     21     case R_ABS32:
     22       return IMAGE_REL_AMD64_ADDR32;
     23     case R_COFF_ADDR32NB:
     24       return IMAGE_REL_AMD64_ADDR32NB;
     25     case R_X64_32S:
     26       return IMAGE_REL_AMD64_ADDR32NB;
     27     case R_PC32:
     28     case R_REL32:
     29     case R_PLT32:
     30     case R_X64_PLT32:
     31     case R_X64_GOTPCREL:
     32     case R_X64_GOTPCRELX:
     33     case R_X64_REX_GOTPCRELX:
     34       return IMAGE_REL_AMD64_REL32;
     35     case R_COFF_SECREL:
     36       return IMAGE_REL_AMD64_SECREL;
     37     case R_COFF_SECTION:
     38       return IMAGE_REL_AMD64_SECTION;
     39     default:
     40       return IMAGE_REL_AMD64_ABSOLUTE;
     41   }
     42 }
     43 
     44 u32 coff_x86_64_reloc_from(u32 wire_type) {
     45   switch (wire_type) {
     46     case IMAGE_REL_AMD64_ABSOLUTE:
     47       return R_NONE;
     48     case IMAGE_REL_AMD64_ADDR64:
     49       return R_ABS64;
     50     case IMAGE_REL_AMD64_ADDR32:
     51       return R_ABS32;
     52     case IMAGE_REL_AMD64_ADDR32NB:
     53       return R_COFF_ADDR32NB;
     54     case IMAGE_REL_AMD64_REL32:
     55     case IMAGE_REL_AMD64_REL32_1:
     56     case IMAGE_REL_AMD64_REL32_2:
     57     case IMAGE_REL_AMD64_REL32_3:
     58     case IMAGE_REL_AMD64_REL32_4:
     59     case IMAGE_REL_AMD64_REL32_5:
     60       return R_PC32;
     61     case IMAGE_REL_AMD64_SECREL:
     62       return R_COFF_SECREL;
     63     case IMAGE_REL_AMD64_SECTION:
     64       return R_COFF_SECTION;
     65     /* SECREL7 (7-bit section-relative) appears in mingw-emitted archive
     66      * members (intrinsic helpers, exception tables, DWARF). kit
     67      * doesn't currently apply or emit these, but panicking at read
     68      * time would block ingesting any mingw archive whose non-import
     69      * members carry .debug_info / .pdata. Map to R_NONE so the
     70      * relocation slot is preserved structurally but treated as a
     71      * no-op by the relocator; the member can still be dead-stripped
     72      * when nothing references it. */
     73     case IMAGE_REL_AMD64_SECREL7:
     74       return R_NONE;
     75     default:
     76       return (u32)-1; /* sentinel */
     77   }
     78 }