aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

reloc_x64.c (4074B)


      1 /* x86_64 relocation handler for the static linker */
      2 
      3 #include "ld_arch.h"
      4 
      5 #include <limits.h>
      6 #include <stdint.h>
      7 
      8 static char *
      9 ld_x64_reloc_name(unsigned int type)
     10 {
     11   switch (type) {
     12     case R_X86_64_16:
     13       return "R_X86_64_16";
     14     case R_X86_64_32:
     15       return "R_X86_64_32";
     16     case R_X86_64_32S:
     17       return "R_X86_64_32S";
     18     case R_X86_64_PC32:
     19       return "R_X86_64_PC32";
     20     case R_X86_64_PLT32:
     21       return "R_X86_64_PLT32";
     22     case R_X86_64_GOTPCREL:
     23       return "R_X86_64_GOTPCREL";
     24     case R_X86_64_REX_GOTPCRELX:
     25       return "R_X86_64_REX_GOTPCRELX";
     26     case R_X86_64_GOTPCRELX:
     27       return "R_X86_64_GOTPCRELX";
     28     case R_X86_64_GOTTPOFF:
     29       return "R_X86_64_GOTTPOFF";
     30     case R_X86_64_TPOFF32:
     31       return "R_X86_64_TPOFF32";
     32     default:
     33       return "R_X86_64_UNKNOWN";
     34   }
     35 }
     36 
     37 static void
     38 ld_x64_check_s16_or_u16(struct ld_reloc *r, unsigned long value)
     39 {
     40   if (value > USHRT_MAX && value < ULONG_MAX - INT16_MAX)
     41     die_reloc(ld_x64_reloc_name(r->type), value);
     42 }
     43 
     44 static void
     45 ld_x64_check_s32(struct ld_reloc *r, unsigned long value)
     46 {
     47   if (value > INT32_MAX && value < ULONG_MAX - INT32_MAX)
     48     die_reloc(ld_x64_reloc_name(r->type), value);
     49 }
     50 
     51 static void
     52 ld_x64_check_u32(struct ld_reloc *r, unsigned long value)
     53 {
     54   if (value > UINT32_MAX)
     55     die_reloc(ld_x64_reloc_name(r->type), value);
     56 }
     57 
     58 static void
     59 ld_x64_write_field(unsigned long *dst, unsigned long value, int size)
     60 {
     61   memcpy(dst, &value, size);
     62 }
     63 
     64 void
     65 ld_x64_reloc_apply(struct ld_reloc *r)
     66 {
     67   unsigned long gotaddr, value;
     68   int           g;
     69 
     70   switch (r->type) {
     71     case R_X86_64_NONE:
     72       break;
     73     case R_X86_64_16:
     74       value = r->val;
     75       ld_x64_check_s16_or_u16(r, value);
     76       ld_x64_write_field(r->dst, value, sizeof(unsigned short));
     77       break;
     78     case R_X86_64_32:
     79       ld_x64_check_u32(r, r->val);
     80       ld_x64_write_field(r->dst, r->val, sizeof(unsigned int));
     81       break;
     82     case R_X86_64_32S:
     83       ld_x64_check_s32(r, r->val);
     84       ld_x64_write_field(r->dst, r->val, sizeof(unsigned int));
     85       break;
     86     case R_X86_64_64:
     87       *r->dst += r->val;
     88       break;
     89     case R_X86_64_PC64:
     90       *r->dst += r->val - r->addr;
     91       break;
     92     case R_X86_64_PC32:
     93     case R_X86_64_PLT32:
     94       /* plt32 is the same as pc32 for a static linker */
     95       value = r->val - r->addr;
     96       ld_x64_check_s32(r, value);
     97       ld_x64_write_field(r->dst, value, sizeof(unsigned int));
     98       break;
     99     case R_X86_64_GOTPCREL:
    100     case R_X86_64_REX_GOTPCRELX:
    101     case R_X86_64_GOTPCRELX:
    102     case R_X86_64_GOTTPOFF:
    103       /* gottpoff loads through a got slot like any gotpcrel form, but
    104          the slot holds a tp-relative offset, not an address. find the
    105          got entry for this symbol, since sym_idx alone is only unique
    106          within one object's symbol table */
    107       for (g = 0; g < r->oe->ngot; g++)
    108         if (r->oe->got[g].sym_idx == r->sym_idx && r->oe->got[g].obj_idx == r->obj_idx)
    109           break;
    110       if (g == r->oe->ngot)
    111         die("ld: missing got entry for gotpcrel relocation");
    112       gotaddr = r->oe->got_vaddr + g * sizeof(unsigned long);
    113       /* rip-relative disp32 = G + A - P, the addend corrects for
    114          trailing bytes (immediates) between the disp32 field and the
    115          next instruction that rip actually points at */
    116       value = gotaddr + r->addend - r->addr;
    117       ld_x64_check_s32(r, value);
    118       ld_x64_write_field(r->dst, value, sizeof(unsigned int));
    119       break;
    120     case R_X86_64_TPOFF32:
    121       /* tls offset, for static link use symbol value directly */
    122       ld_x64_check_s32(r, r->val);
    123       ld_x64_write_field(r->dst, r->val, sizeof(unsigned int));
    124       break;
    125     default:
    126       die("ld: unknown x86_64 relocation type");
    127   }
    128 }
    129 
    130 static int
    131 ld_x64_is_got_rel(unsigned int type)
    132 {
    133   return type == R_X86_64_GOTPCREL || type == R_X86_64_REX_GOTPCRELX || type == R_X86_64_GOTPCRELX
    134          || type == R_X86_64_GOTTPOFF;
    135 }
    136 
    137 const struct ld_arch_backend ld_arch_x64 = {
    138     .em_machine  = EM_X86_64,
    139     .reloc_apply = ld_x64_reloc_apply,
    140     .is_got_rel  = ld_x64_is_got_rel,
    141 };