aruu

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

ir_asm.c (7588B)


      1 #include "../config.h"
      2 
      3 #include "ir_asm.h"
      4 
      5 #include <assert.h>
      6 #include <stdint.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 
     11 #include "arch.h"
     12 #include "parse_asm.h"
     13 #include "table.h"
     14 #include "tcutil.h"
     15 
     16 /* ir constructors */
     17 
     18 struct IR *
     19 new_ir_label(const struct Name *label)
     20 {
     21   struct IR *ir = calloc_or_die(sizeof(*ir));
     22   ir->kind      = IR_LABEL;
     23   ir->u.label   = label;
     24   return ir;
     25 }
     26 
     27 struct IR *
     28 new_ir_code(const struct Code *code)
     29 {
     30   struct IR *ir = calloc_or_die(sizeof(*ir));
     31   ir->kind      = IR_CODE;
     32   ir->u.code    = *code;
     33   return ir;
     34 }
     35 
     36 struct IR *
     37 new_ir_data(const void *data, size_t size)
     38 {
     39   struct IR *ir  = calloc_or_die(sizeof(*ir));
     40   ir->kind       = IR_DATA;
     41   ir->u.data.len = size;
     42   ir->u.data.buf = (unsigned char *)data;
     43   return ir;
     44 }
     45 
     46 struct IR *
     47 new_ir_bss(size_t size)
     48 {
     49   struct IR *ir = calloc_or_die(sizeof(*ir));
     50   ir->kind      = IR_BSS;
     51   ir->u.bss     = size;
     52   return ir;
     53 }
     54 
     55 struct IR *
     56 new_ir_zero(size_t size)
     57 {
     58   struct IR *ir = calloc_or_die(sizeof(*ir));
     59   ir->kind      = IR_ZERO;
     60   ir->u.zero    = size;
     61   return ir;
     62 }
     63 
     64 struct IR *
     65 new_ir_align(int align)
     66 {
     67   struct IR *ir = calloc_or_die(sizeof(*ir));
     68   ir->kind      = IR_ALIGN;
     69   ir->u.align   = align;
     70   return ir;
     71 }
     72 
     73 struct IR *
     74 new_ir_expr(enum IrKind kind, const struct Expr *expr)
     75 {
     76   struct IR *ir     = calloc_or_die(sizeof(*ir));
     77   ir->kind          = kind;
     78   ir->u.expr.expr   = expr;
     79   ir->u.expr.addend = 0;
     80   return ir;
     81 }
     82 
     83 /* section data helpers */
     84 
     85 static void
     86 sec_add_data(struct SectionInfo *section, const void *data, size_t bytes)
     87 {
     88   assert(!(section->flag & SF_BSS));
     89   data_append(section->ds, data, bytes);
     90 }
     91 
     92 static void
     93 sec_add_code(struct SectionInfo *section, const void *buf, size_t bytes)
     94 {
     95   sec_add_data(section, buf, bytes);
     96 }
     97 
     98 static void
     99 sec_add_bss(struct SectionInfo *section, size_t size)
    100 {
    101   assert(section->flag & SF_BSS);
    102   section->bss_size += size;
    103 }
    104 
    105 static void
    106 sec_align_size(struct SectionInfo *section, size_t align)
    107 {
    108   if (align > section->align)
    109     section->align = align;
    110 
    111   if (section->flag & SF_BSS) {
    112     section->bss_size = ALIGN(section->bss_size, align);
    113   } else if (section->flag & SF_EXECUTABLE) {
    114     /* data_align pads with zero bytes, which decode as real (and
    115        wrong) instructions if control flow falls through them; pad
    116        with single-byte nops instead */
    117     size_t len         = section->ds->len;
    118     size_t aligned_len = ALIGN(len, align);
    119     if (aligned_len > len) {
    120       unsigned char *nops = malloc_or_die(aligned_len - len);
    121       memset(nops, 0x90, aligned_len - len);
    122       data_append(section->ds, nops, aligned_len - len);
    123       free(nops);
    124     }
    125   } else {
    126     data_align(section->ds, align);
    127   }
    128 }
    129 
    130 /* label address assignment */
    131 
    132 int
    133 calc_label_address(uint64_t start_address, struct Vector *sections, struct Table *label_table)
    134 {
    135   int      settle  = 1;
    136   uint64_t address = start_address;
    137   for (int sec = 0; sec < sections->len; ++sec) {
    138     struct SectionInfo *section = sections->data[sec];
    139     address                     = ALIGN(address, section->align);
    140     section->start_address      = address;
    141 
    142     struct Vector *irs = section->irs;
    143     for (int i = 0, len = irs->len; i < len; ++i) {
    144       struct IR *ir = irs->data[i];
    145       ir->address   = address;
    146       switch (ir->kind) {
    147         case IR_LABEL: {
    148           struct LabelInfo *info;
    149           if (!table_try_get(label_table, ir->u.label, (void **)&info)) {
    150             fprintf(stderr, "[%.*s] not found\n", NAMES(ir->u.label));
    151             assert(!"unexpected");
    152           } else {
    153             info->address = address;
    154           }
    155         } break;
    156         case IR_CODE:
    157           address += ir->u.code.len;
    158           break;
    159         case IR_DATA:
    160           address += ir->u.data.len;
    161           break;
    162         case IR_BSS:
    163           address += ir->u.bss;
    164           break;
    165         case IR_ZERO:
    166           address += ir->u.zero;
    167           break;
    168         case IR_ALIGN:
    169           ir->address = address = ALIGN(address, ir->u.align);
    170           if ((size_t)ir->u.align > section->align) {
    171             section->align = ir->u.align;
    172             settle         = 0;
    173           }
    174           break;
    175         case IR_EXPR_BYTE:
    176           address += BYTE_SIZE;
    177           break;
    178         case IR_EXPR_SHORT:
    179           address += SHORT_SIZE;
    180           break;
    181         case IR_EXPR_LONG:
    182           address += LONG_SIZE;
    183           break;
    184         case IR_EXPR_QUAD:
    185           address += QUAD_SIZE;
    186           break;
    187       }
    188     }
    189   }
    190   return settle;
    191 }
    192 
    193 /* relative address resolution */
    194 
    195 int
    196 resolve_relative_address(
    197     struct Vector *sections, struct Table *label_table, struct Vector *unresolved
    198 )
    199 {
    200   assert(unresolved != NULL);
    201   vec_clear(unresolved);
    202   int size_upgraded = 0;
    203   for (int sec = 0; sec < sections->len; ++sec) {
    204     struct SectionInfo *section       = sections->data[sec];
    205     struct Vector      *irs           = section->irs;
    206     uint64_t            start_address = irs->len > 0 ? ((struct IR *)irs->data[0])->address : 0;
    207     for (int i = 0, len = irs->len; i < len; ++i) {
    208       struct IR *ir      = irs->data[i];
    209       uint64_t   address = ir->address;
    210       switch (ir->kind) {
    211         case IR_CODE:
    212           arch.resolve_code_reloc(
    213               ir, section, address, start_address, label_table, unresolved, &size_upgraded
    214           );
    215           break;
    216         case IR_EXPR_BYTE:
    217         case IR_EXPR_SHORT:
    218         case IR_EXPR_LONG:
    219         case IR_EXPR_QUAD: {
    220           struct Value value = calc_expr(label_table, ir->u.expr.expr);
    221           assert(value.label != NULL);
    222           struct UnresolvedInfo *info = malloc_or_die(sizeof(*info));
    223           info->kind                  = UNRES_ABS64;
    224           info->label                 = value.label;
    225           info->src_section           = section;
    226           info->offset                = address - start_address;
    227           info->add                   = value.offset;
    228           ir->u.expr.addend           = value.offset;
    229           vec_push(unresolved, info);
    230         } break;
    231         case IR_LABEL:
    232         case IR_DATA:
    233         case IR_BSS:
    234         case IR_ZERO:
    235         case IR_ALIGN:
    236           break;
    237       }
    238     }
    239   }
    240 
    241   return !size_upgraded;
    242 }
    243 
    244 /* emit section data */
    245 
    246 void
    247 emit_irs(struct Vector *sections)
    248 {
    249   for (int sec = 0; sec < sections->len; ++sec) {
    250     struct SectionInfo *section = sections->data[sec];
    251     struct Vector      *irs     = section->irs;
    252     for (int i = 0, len = irs->len; i < len; ++i) {
    253       struct IR *ir = irs->data[i];
    254       switch (ir->kind) {
    255         case IR_LABEL:
    256           break;
    257         case IR_CODE:
    258           sec_add_code(section, ir->u.code.buf, ir->u.code.len);
    259           break;
    260         case IR_DATA:
    261           sec_add_data(section, ir->u.data.buf, ir->u.data.len);
    262           break;
    263         case IR_BSS:
    264           sec_add_bss(section, ir->u.bss);
    265           break;
    266         case IR_ZERO:
    267           if (section->flag & SF_BSS) {
    268             sec_add_bss(section, ir->u.zero);
    269           } else {
    270             void *buf = calloc_or_die(ir->u.zero);
    271             sec_add_data(section, buf, ir->u.zero);
    272           }
    273           break;
    274         case IR_ALIGN:
    275           sec_align_size(section, ir->u.align);
    276           break;
    277         case IR_EXPR_BYTE:
    278         case IR_EXPR_SHORT:
    279         case IR_EXPR_LONG:
    280         case IR_EXPR_QUAD: {
    281 #if TC_TARGET_PLATFORM == TC_PLATFORM_APPLE
    282           int64_t value = ir->u.expr.addend;
    283 #else
    284           int64_t value = 0;
    285 #endif
    286           int size = 1 << (ir->kind - IR_EXPR_BYTE);
    287           sec_add_data(section, &value, size);
    288         } break;
    289       }
    290     }
    291   }
    292 }