aruu

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

reloc_x64.c (9759B)


      1 /* x64-specific code relocation resolution
      2  * called from core resolve_relative_address for each IR_CODE node */
      3 
      4 #include "../../../config.h"
      5 #include "arch.h"
      6 
      7 #include <stdint.h>
      8 
      9 #include "inst.h"
     10 #include "parse_asm.h"
     11 #include "table.h"
     12 #include "tcutil.h"
     13 
     14 static void
     15 put_value(unsigned char *p, int64_t value, int size)
     16 {
     17   for (int i = 0; i < size; ++i) {
     18     *p++ = value;
     19     value >>= 8;
     20   }
     21 }
     22 
     23 static int
     24 make_jmp_long(struct IR *ir)
     25 {
     26   if (ir->u.code.flag & INST_LONG_OFFSET)
     27     return 0;
     28 
     29   struct Inst *inst = ir->u.code.inst;
     30   /* change to long offset and recalculate */
     31   ir->u.code.flag |= INST_LONG_OFFSET;
     32   ir->u.code.len = 0;
     33   if (inst->op == JMP_D)
     34     MAKE_CODE(inst, &ir->u.code, 0xe9, IM32(-1));
     35   else
     36     MAKE_CODE(inst, &ir->u.code, 0x0f, 0x80 + (inst->op - JO), IM32(-1));
     37   return 1;
     38 }
     39 
     40 static void
     41 resolve_direct_mov(
     42     struct IR          *ir,
     43     struct SectionInfo *section,
     44     uint64_t            address,
     45     uint64_t            start_address,
     46     struct Table       *label_table,
     47     struct Vector      *unresolved
     48 )
     49 {
     50   struct Inst           *inst  = ir->u.code.inst;
     51   struct Operand        *mem   = inst->op == MOV_DR ? &inst->opr[0] : &inst->opr[1];
     52   struct Reg            *reg   = inst->op == MOV_DR ? &inst->opr[1].u.reg : &inst->opr[0].u.reg;
     53   struct Value           value = calc_expr(label_table, mem->u.direct.expr);
     54   struct UnresolvedInfo *info;
     55 
     56   if (value.label == NULL) {
     57     put_value(ir->u.code.buf + ir->u.code.len - 4, value.offset, sizeof(int32_t));
     58     return;
     59   }
     60   info              = malloc_or_die(sizeof(*info));
     61   info->kind        = inst->op == MOV_DR && reg->size == REG32 ? UNRES_X64_ABS32S : UNRES_X64_ABS32;
     62   info->label       = value.label;
     63   info->src_section = section;
     64   info->offset      = address + ir->u.code.len - 4 - start_address;
     65   info->add         = value.offset;
     66   vec_push(unresolved, info);
     67 }
     68 
     69 void
     70 x64_resolve_code_reloc(
     71     struct IR          *ir,
     72     struct SectionInfo *section,
     73     uint64_t            address,
     74     uint64_t            start_address,
     75     struct Table       *label_table,
     76     struct Vector      *unresolved,
     77     int                *size_upgraded
     78 )
     79 {
     80   struct Inst *inst = ir->u.code.inst;
     81   switch (inst->op) {
     82     case MOV_DR:
     83     case MOV_RD:
     84       resolve_direct_mov(ir, section, address, start_address, label_table, unresolved);
     85       break;
     86     case MOV_IR:
     87       /* this shape loads through a got slot (mov sym@gotpcrel(%rip),
     88          %reg), not sym's address, so it needs a relocation even when
     89          sym is local. unlike lea_ir there is no direct-address
     90          fallback to patch in place */
     91       if (inst->opr[0].u.indirect.reg.no == RIP) {
     92         struct Value value = calc_expr(label_table, inst->opr[0].u.indirect.offset.expr);
     93         if (value.label != NULL) {
     94           struct UnresolvedInfo *info = malloc_or_die(sizeof(*info));
     95           info->kind                  = UNRES_X64_GOT_LOAD;
     96           info->label                 = value.label;
     97           info->src_section           = section;
     98           info->offset                = address + 3 - start_address;
     99           info->add                   = value.offset - 4;
    100           vec_push(unresolved, info);
    101         }
    102       }
    103       break;
    104     case ADD_IR:
    105       /* addq sym@gottpoff(%rip), %reg: the second half of qbe's
    106          initial-exec tls sequence (movq %fs:0, %reg first). it loads a
    107          got-resident tp-offset, otherwise the same shape as mov_ir */
    108       if (inst->opr[0].u.indirect.reg.no == RIP) {
    109         struct Value value = calc_expr(label_table, inst->opr[0].u.indirect.offset.expr);
    110         if (value.label != NULL) {
    111           struct UnresolvedInfo *info = malloc_or_die(sizeof(*info));
    112           info->kind  = (inst->opr[0].u.indirect.offset.flag & LF_GOTTPOFF) ? UNRES_X64_GOTTPOFF
    113                                                                             : UNRES_X64_GOT_LOAD;
    114           info->label = value.label;
    115           info->src_section = section;
    116           info->offset      = address + 3 - start_address;
    117           info->add         = value.offset - 4;
    118           vec_push(unresolved, info);
    119         }
    120       }
    121       break;
    122     case LEA_IR:
    123       if (inst->opr[0].u.indirect.reg.no == RIP
    124           && inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) {
    125         struct Value value = calc_expr(label_table, inst->opr[0].u.indirect.offset.expr);
    126         if (value.label != NULL) {
    127           struct UnresolvedInfo *info = malloc_or_die(sizeof(*info));
    128           info->kind                  = UNRES_EXTERN_PC32;
    129           info->label                 = value.label;
    130           info->src_section           = section;
    131           info->offset                = address + 3 - start_address;
    132           info->add                   = value.offset - 4;
    133           vec_push(unresolved, info);
    134 #if TC_TARGET_PLATFORM == TC_PLATFORM_APPLE
    135           put_value(ir->u.code.buf + 3, value.offset, sizeof(int32_t));
    136 #endif
    137         } else {
    138           int64_t offset = value.offset - (address + ir->u.code.len);
    139           put_value(ir->u.code.buf + 3, offset, sizeof(int32_t));
    140         }
    141       }
    142       break;
    143     case MOV_SR:
    144       /* r_x86_64_tpoff32: a plain tp-relative displacement, not
    145          pc-relative, so no address math here. the linker computes it
    146          once the tls layout is known, like ir_asm.c's unres_abs64
    147          registration for .quad label */
    148       if (inst->opr[0].u.segment.offset.flag & LF_TPOFF) {
    149         struct Value           value = calc_expr(label_table, inst->opr[0].u.segment.offset.expr);
    150         int                    reloc_offset = ir->u.code.len - 4;
    151         struct UnresolvedInfo *info         = malloc_or_die(sizeof(*info));
    152         info->kind                          = UNRES_X64_TPOFF32;
    153         info->label                         = value.label;
    154         info->src_section                   = section;
    155         info->offset                        = address + reloc_offset - start_address;
    156         info->add                           = value.offset;
    157         vec_push(unresolved, info);
    158       }
    159       break;
    160     case MOVSD_IX:
    161     case MOVSS_IX:
    162     case XORPD_IX:
    163     case XORPS_IX:
    164     case COMISD_IX:
    165     case UCOMISD_IX:
    166     case COMISS_IX:
    167     case UCOMISS_IX:
    168       /* rip-relative with a symbolic offset: disp32 is the last 4
    169          bytes, but unlike lea_ir these encodings may or may not have a
    170          rex byte, so derive the offset from the encoded length */
    171       if (inst->opr[0].u.indirect.reg.no == RIP
    172           && inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) {
    173         struct Value value        = calc_expr(label_table, inst->opr[0].u.indirect.offset.expr);
    174         int          reloc_offset = ir->u.code.len - 4;
    175         if (value.label != NULL) {
    176           struct UnresolvedInfo *info = malloc_or_die(sizeof(*info));
    177           info->kind                  = UNRES_EXTERN_PC32;
    178           info->label                 = value.label;
    179           info->src_section           = section;
    180           info->offset                = address + reloc_offset - start_address;
    181           info->add                   = value.offset - 4;
    182           vec_push(unresolved, info);
    183         } else {
    184           int64_t offset = value.offset - (address + ir->u.code.len);
    185           put_value(ir->u.code.buf + reloc_offset, offset, sizeof(int32_t));
    186         }
    187       }
    188       break;
    189     case JMP_D:
    190     case JO:
    191     case JNO:
    192     case JB:
    193     case JAE:
    194     case JE:
    195     case JNE:
    196     case JBE:
    197     case JA:
    198     case JS:
    199     case JNS:
    200     case JP:
    201     case JNP:
    202     case JL:
    203     case JGE:
    204     case JLE:
    205     case JG:
    206       if (inst->opr[0].type == DIRECT) {
    207         struct Value value = calc_expr(label_table, inst->opr[0].u.direct.expr);
    208         if (value.label != NULL) {
    209           struct LabelInfo *label_info = table_get(label_table, value.label);
    210           if (label_info == NULL) {
    211             *size_upgraded |= make_jmp_long(ir);
    212 
    213             struct UnresolvedInfo *info = malloc_or_die(sizeof(*info));
    214             info->kind                  = UNRES_CALL;
    215             info->label                 = value.label;
    216             info->src_section           = section;
    217             info->offset                = address + 1 - start_address;
    218             info->add                   = value.offset - 4;
    219             vec_push(unresolved, info);
    220             break;
    221           } else {
    222             value.offset += label_info->address;
    223           }
    224         }
    225 
    226         int64_t offset      = value.offset - (address + ir->u.code.len);
    227         int     long_offset = ir->u.code.flag & INST_LONG_OFFSET;
    228         if (!long_offset) {
    229           if (!is_im8(offset)) {
    230             *size_upgraded |= make_jmp_long(ir);
    231           } else {
    232             put_value(ir->u.code.buf + 1, offset, sizeof(int8_t));
    233           }
    234         } else {
    235           if (!is_im32(offset))
    236             error("jump offset too far (over 32bit)");
    237 
    238           int d = inst->op == JMP_D ? 1 : 2;
    239           put_value(ir->u.code.buf + d, offset, sizeof(int32_t));
    240         }
    241       }
    242       break;
    243     case CALL_D:
    244       if (inst->opr[0].type == DIRECT) {
    245         struct Value value = calc_expr(label_table, inst->opr[0].u.direct.expr);
    246         if (value.label != NULL) {
    247           struct UnresolvedInfo *info = malloc_or_die(sizeof(*info));
    248           info->kind                  = UNRES_CALL;
    249           info->label                 = value.label;
    250           info->src_section           = section;
    251           info->offset                = address + 1 - start_address;
    252           info->add                   = value.offset - 4;
    253           vec_push(unresolved, info);
    254           break;
    255         }
    256         int64_t offset = value.offset - (address + ir->u.code.len);
    257         put_value(ir->u.code.buf + 1, offset, sizeof(int32_t));
    258       }
    259       break;
    260     default:
    261       break;
    262   }
    263 }