kit

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

groupiter.c (7515B)


      1 /* Hand-built object with a COMDAT group, exercised through the public
      2  * object builder and kit_obj_groupiter APIs.
      3  *
      4  * Steps:
      5  *   1. Build an ELF with two sections wired into one COMDAT group.
      6  *   2. Emit ELF, reopen via kit_obj_open, walk the public iter; verify
      7  *      group survives the on-disk roundtrip and section IDs are
      8  *      remapped to the public 0-based space. */
      9 
     10 #include <kit/core.h>
     11 #include <kit/object.h>
     12 #include <stdarg.h>
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <string.h>
     16 
     17 #include "lib/kit_test_target.h"
     18 
     19 static void* heap_alloc(KitHeap* h, size_t n, size_t a) {
     20   (void)h;
     21   (void)a;
     22   return n ? malloc(n) : NULL;
     23 }
     24 static void* heap_realloc(KitHeap* h, void* p, size_t o, size_t n, size_t a) {
     25   (void)h;
     26   (void)o;
     27   (void)a;
     28   return realloc(p, n);
     29 }
     30 static void heap_free(KitHeap* h, void* p, size_t n) {
     31   (void)h;
     32   (void)n;
     33   free(p);
     34 }
     35 static KitHeap g_heap = {heap_alloc, heap_realloc, heap_free, NULL};
     36 
     37 static void diag_emit(KitDiagSink* s, KitDiagKind k, KitSrcLoc loc,
     38                       const char* fmt, va_list ap) {
     39   static const char* names[] = {"note", "warning", "error", "fatal"};
     40   (void)s;
     41   (void)loc;
     42   fprintf(stderr, "%s: ", names[k]);
     43   vfprintf(stderr, fmt, ap);
     44   fputc('\n', stderr);
     45 }
     46 static KitDiagSink g_diag = {diag_emit, NULL, 0, 0};
     47 
     48 static int g_failures;
     49 #define CHECK(cond, ...)                                   \
     50   do {                                                     \
     51     if (!(cond)) {                                         \
     52       fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
     53       fprintf(stderr, __VA_ARGS__);                        \
     54       fputc('\n', stderr);                                 \
     55       g_failures++;                                        \
     56     }                                                      \
     57   } while (0)
     58 
     59 /* mov w0, #1 ; ret */
     60 static const uint8_t TEXT_BYTES[8] = {
     61     0x20, 0x00, 0x80, 0x52, 0xc0, 0x03, 0x5f, 0xd6,
     62 };
     63 
     64 static int name_eq(KitSlice s, const char* want) {
     65   return s.s && kit_slice_eq_cstr(s, want);
     66 }
     67 
     68 int main(void) {
     69   KitTargetSpec target;
     70   if (kit_test_target_init(&target) != 0) {
     71     fprintf(stderr, "FAIL: kit_test_target_init\n");
     72     return 1;
     73   }
     74 
     75   KitContext ctx = {.heap = &g_heap,
     76                     .file_io = NULL,
     77                     .diag = &g_diag,
     78                     .now = -1};
     79   KitTargetOptions target_opts;
     80   memset(&target_opts, 0, sizeof target_opts);
     81   target_opts.spec = target;
     82   KitTarget* kt = NULL;
     83   if (kit_target_new(&ctx, &target_opts, &kt) != KIT_OK || !kt) {
     84     fprintf(stderr, "FAIL: kit_target_new\n");
     85     return 1;
     86   }
     87   KitCompiler* cc = NULL;
     88   if (kit_compiler_new(kt, &ctx, &cc) != KIT_OK || !cc) {
     89     fprintf(stderr, "FAIL: kit_compiler_new\n");
     90     kit_target_free(kt);
     91     return 1;
     92   }
     93   /* ---- build ---- */
     94   KitObjBuilder* in = NULL;
     95   CHECK(kit_obj_builder_new(cc, &in) == KIT_OK && in, "kit_obj_builder_new");
     96 
     97   KitSym sig_nm = kit_sym_intern(cc, KIT_SLICE_LIT("comdat_sig"));
     98   KitObjSection sec_text = KIT_SECTION_NONE;
     99   KitObjSection sec_data = KIT_SECTION_NONE;
    100   KitObjSectionDesc text_desc = {
    101       .name = kit_sym_intern(cc, KIT_SLICE_LIT(".text.comdat_fn")),
    102       .kind = KIT_SEC_TEXT,
    103       .flags = KIT_SF_ALLOC | KIT_SF_EXEC,
    104       .align = 4,
    105       .entsize = 0,
    106   };
    107   KitObjSectionDesc data_desc = {
    108       .name = kit_sym_intern(cc, KIT_SLICE_LIT(".data.comdat_fn")),
    109       .kind = KIT_SEC_DATA,
    110       .flags = KIT_SF_ALLOC | KIT_SF_WRITE,
    111       .align = 8,
    112       .entsize = 0,
    113   };
    114   CHECK(kit_obj_builder_section(in, &text_desc, &sec_text) == KIT_OK,
    115         "section .text.comdat_fn");
    116   CHECK(kit_obj_builder_section(in, &data_desc, &sec_data) == KIT_OK,
    117         "section .data.comdat_fn");
    118   CHECK(kit_obj_builder_write(in, sec_text, TEXT_BYTES, sizeof TEXT_BYTES) ==
    119             KIT_OK,
    120         "write .text.comdat_fn");
    121   static const uint8_t zero8[8] = {0};
    122   CHECK(kit_obj_builder_write(in, sec_data, zero8, sizeof zero8) == KIT_OK,
    123         "write .data.comdat_fn");
    124 
    125   KitObjSymbol sig_sym = KIT_OBJ_SYMBOL_NONE;
    126   KitObjSymbolDesc sig_desc = {
    127       .name = sig_nm,
    128       .bind = KIT_SB_WEAK,
    129       .kind = KIT_SK_FUNC,
    130       .section = sec_text,
    131       .value = 0,
    132       .size = sizeof TEXT_BYTES,
    133   };
    134   CHECK(kit_obj_builder_symbol(in, &sig_desc, &sig_sym) == KIT_OK,
    135         "symbol comdat_sig");
    136 
    137   KitObjGroup gid = KIT_OBJ_GROUP_NONE;
    138   CHECK(kit_obj_builder_group(in, sig_nm, sig_sym, KIT_OBJ_GROUP_COMDAT,
    139                               &gid) == KIT_OK,
    140         "group comdat_sig");
    141   CHECK(kit_obj_builder_group_add_section(in, gid, sec_text) == KIT_OK,
    142         "group add text");
    143   CHECK(kit_obj_builder_group_add_section(in, gid, sec_data) == KIT_OK,
    144         "group add data");
    145   CHECK(kit_obj_builder_finalize(in) == KIT_OK, "finalize");
    146 
    147   /* ---- emit + public-iter readback ---- */
    148   KitWriter* w = NULL;
    149   (void)kit_writer_mem(&g_heap, &w);
    150   CHECK(kit_obj_builder_emit(in, w) == KIT_OK, "emit");
    151   size_t out_len = 0;
    152   const uint8_t* out_data = kit_writer_mem_bytes(w, &out_len);
    153   uint8_t* roundtrip = (uint8_t*)malloc(out_len ? out_len : 1);
    154   memcpy(roundtrip, out_data, out_len);
    155   kit_writer_close(w);
    156 
    157   KitSlice input = {.data = roundtrip, .len = out_len};
    158   KitObjFile* f = NULL;
    159   CHECK(
    160       kit_obj_open(&ctx, KIT_SLICE_LIT("groupiter"), &input, &f) == KIT_OK && f,
    161       "kit_obj_open failed");
    162 
    163   if (f) {
    164     /* Resolve section IDs by name so the test isn't coupled to ordering. */
    165     KitObjSection text_pub = KIT_SECTION_NONE;
    166     KitObjSection data_pub = KIT_SECTION_NONE;
    167     CHECK(kit_obj_section_by_name(f, KIT_SLICE_LIT(".text.comdat_fn"),
    168                                   &text_pub) == KIT_OK,
    169           "section_by_name .text.comdat_fn");
    170     CHECK(kit_obj_section_by_name(f, KIT_SLICE_LIT(".data.comdat_fn"),
    171                                   &data_pub) == KIT_OK,
    172           "section_by_name .data.comdat_fn");
    173 
    174     KitObjGroupIter* git = NULL;
    175     CHECK(kit_obj_groupiter_new(f, &git) == KIT_OK, "kit_obj_groupiter_new");
    176     int seen = 0;
    177     KitObjGroupInfo gi;
    178     while (kit_obj_groupiter_next(git, &gi) == KIT_ITER_ITEM) {
    179       ++seen;
    180       CHECK(name_eq(gi.name, "comdat_sig"), "public iter: name=%.*s",
    181             KIT_SLICE_ARG(gi.name));
    182       CHECK((gi.flags & KIT_OBJ_GROUP_COMDAT) != 0,
    183             "public iter: missing COMDAT flag (flags=0x%x)", gi.flags);
    184       CHECK(gi.nsections == 2, "public iter: nsections=%u, want 2",
    185             gi.nsections);
    186       CHECK(gi.sections != NULL, "public iter: NULL sections");
    187       if (gi.sections && gi.nsections == 2) {
    188         /* Order is preserved by ELF SHT_GROUP, so members[0] should be
    189          * sec_text and members[1] sec_data. */
    190         CHECK(gi.sections[0] == text_pub,
    191               "public iter: sections[0]=%u, want %u (text)", gi.sections[0],
    192               text_pub);
    193         CHECK(gi.sections[1] == data_pub,
    194               "public iter: sections[1]=%u, want %u (data)", gi.sections[1],
    195               data_pub);
    196       }
    197       /* Signature should be a valid public symbol id, not NONE. */
    198       CHECK(gi.signature != KIT_OBJ_SYMBOL_NONE,
    199             "public iter: signature is NONE");
    200     }
    201     CHECK(seen == 1, "public iter: saw %d groups, want 1", seen);
    202     kit_obj_groupiter_free(git);
    203     kit_obj_free(f);
    204   }
    205 
    206   free(roundtrip);
    207   kit_obj_builder_free(in);
    208   kit_compiler_free(cc);
    209   kit_target_free(kt);
    210 
    211   if (g_failures) {
    212     fprintf(stderr, "%d failure(s)\n", g_failures);
    213     return 1;
    214   }
    215   fputs("groupiter: OK\n", stderr);
    216   return 0;
    217 }