coff_weak_alias_test.c (5946B)
1 /* COFF WEAK_EXTERNAL alias capture in read_coff. 2 * 3 * mingw's x86_64 <setjmp.h> expands setjmp() to the intrinsic `_setjmp`, which 4 * libucrt.a provides as a COFF weak-external ALIAS to `__intrinsic_setjmp` (a 5 * DLL short-import). kit used to drop the aux TagIndex and lean on a single- 6 * underscore naming heuristic that can't derive `__intrinsic_setjmp` from 7 * `_setjmp`, so the cross-link failed with `undefined reference to '_setjmp'`. 8 * read_coff now records the alias target (when Characteristics is 9 * IMAGE_WEAK_EXTERN_SEARCH_ALIAS) so link_resolve can follow it by name. 10 * 11 * This pins the read-side decode: hand-build a minimal AMD64 COFF object whose 12 * symbol table carries the `_setjmp` -> `__intrinsic_setjmp` alias declarator, 13 * read it, and assert obj_get_weak_alias recovers the target. A negative case 14 * (a plain weak external with the SEARCH_LIBRARY policy kit's own emitter uses) 15 * confirms non-alias weak symbols record nothing. 16 * 17 * Exit 0 = pass; non-zero = fail. */ 18 19 #include <kit/core.h> 20 #include <stdint.h> 21 #include <string.h> 22 23 #include "core/core.h" 24 #include "core/pool.h" 25 #include "lib/kit_unit.h" 26 #include "obj/obj.h" 27 28 static KitUnit g_u; 29 #define EXPECT(cond, ...) CU_EXPECT(&g_u, cond, __VA_ARGS__) 30 31 #define IMAGE_FILE_MACHINE_AMD64 0x8664u 32 #define SC_EXTERNAL 2u 33 #define SC_WEAK_EXTERNAL 105u 34 #define SEARCH_LIBRARY 2u 35 #define SEARCH_ALIAS 3u 36 37 static void wr16(u8* p, u32 v) { 38 p[0] = (u8)v; 39 p[1] = (u8)(v >> 8); 40 } 41 static void wr32(u8* p, u32 v) { 42 p[0] = (u8)v; 43 p[1] = (u8)(v >> 8); 44 p[2] = (u8)(v >> 16); 45 p[3] = (u8)(v >> 24); 46 } 47 48 /* Build a 3-record symbol-table COFF object (no sections): 49 * idx 0: `__intrinsic_setjmp` (undef EXTERNAL, long name via strtab) 50 * idx 1: `weak_name` (WEAK_EXTERNAL, short name, 1 aux) 51 * idx 2: aux -> TagIndex 0 (`__intrinsic_setjmp`), `characteristics` 52 * `weak_name` must be <= 8 bytes (short name). Returns the byte length. */ 53 static size_t build_coff(u8* out, const char* weak_name, u32 characteristics) { 54 static const char kTarget[] = "__intrinsic_setjmp"; 55 const u32 nsyms = 3; 56 const u32 symtab_off = 20; 57 const u32 strtab_off = symtab_off + nsyms * 18u; 58 /* string table: 4-byte inclusive size + the target name + NUL */ 59 const u32 target_len = (u32)sizeof kTarget; /* includes NUL */ 60 const u32 strtab_size = 4u + target_len; 61 62 memset(out, 0, strtab_off + strtab_size); 63 64 /* ---- file header ---- */ 65 wr16(out + 0, IMAGE_FILE_MACHINE_AMD64); /* Machine */ 66 wr16(out + 2, 0); /* NumberOfSections */ 67 wr32(out + 4, 0); /* TimeDateStamp */ 68 wr32(out + 8, symtab_off); /* PointerToSymbolTable */ 69 wr32(out + 12, nsyms); /* NumberOfSymbols */ 70 wr16(out + 16, 0); /* SizeOfOptionalHeader */ 71 wr16(out + 18, 0); /* Characteristics */ 72 73 /* ---- symbol 0: __intrinsic_setjmp (long name) ---- */ 74 u8* s0 = out + symtab_off; 75 wr32(s0 + 0, 0); /* long-name marker */ 76 wr32(s0 + 4, 4); /* strtab offset (strings start at strtab+4) */ 77 wr32(s0 + 8, 0); 78 wr16(s0 + 12, 0); /* SectionNumber = UNDEFINED */ 79 wr16(s0 + 14, 0); /* Type */ 80 s0[16] = (u8)SC_EXTERNAL; 81 s0[17] = 0; /* naux */ 82 83 /* ---- symbol 1: weak_name (short name, weak external) ---- */ 84 u8* s1 = out + symtab_off + 18u; 85 size_t wn = strlen(weak_name); 86 memcpy(s1, weak_name, wn); /* zero-padded to 8 by the memset above */ 87 wr32(s1 + 8, 0); 88 wr16(s1 + 12, 0); /* SectionNumber = UNDEFINED */ 89 wr16(s1 + 14, 0); 90 s1[16] = (u8)SC_WEAK_EXTERNAL; 91 s1[17] = 1; /* one aux record */ 92 93 /* ---- symbol 2: weak-external aux ---- */ 94 u8* aux = out + symtab_off + 36u; 95 wr32(aux + 0, 0); /* TagIndex -> symbol 0 */ 96 wr32(aux + 4, characteristics); /* IMAGE_WEAK_EXTERN_SEARCH_* */ 97 98 /* ---- string table ---- */ 99 u8* st = out + strtab_off; 100 wr32(st + 0, strtab_size); 101 memcpy(st + 4, kTarget, target_len); 102 103 return strtab_off + strtab_size; 104 } 105 106 static Sym intern(Compiler* c, const char* s) { 107 return pool_intern_slice(c->global, (Slice){.s = s, .len = (u32)strlen(s)}); 108 } 109 110 int main(void) { 111 KitCompiler* kc = NULL; 112 u8 buf[160]; 113 kit_unit_init(&g_u); 114 115 KitTargetSpec t = 116 kit_unit_target(KIT_ARCH_X86_64, KIT_OS_WINDOWS, KIT_OBJ_COFF); 117 if (kit_unit_compiler_new(&g_u, t, &kc) != KIT_OK || !kc) { 118 fprintf(stderr, "compiler_new failed\n"); 119 return 2; 120 } 121 Compiler* c = kc; /* KitCompiler IS the internal Compiler */ 122 123 /* Positive: a genuine alias declaration records its target. */ 124 { 125 size_t len = build_coff(buf, "_setjmp", SEARCH_ALIAS); 126 ObjBuilder* ob = read_coff(c, "weak_alias.o", buf, len); 127 EXPECT(ob != NULL, "read_coff returned NULL"); 128 if (ob) { 129 ObjSymId id = obj_symbol_find(ob, intern(c, "_setjmp")); 130 EXPECT(id != OBJ_SYM_NONE, "_setjmp symbol not found after read"); 131 Sym target = obj_get_weak_alias(ob, id); 132 EXPECT(target != 0, "alias target not recorded for _setjmp"); 133 if (target != 0) { 134 Slice s = pool_slice(c->global, target); 135 EXPECT(s.len == strlen("__intrinsic_setjmp") && 136 memcmp(s.s, "__intrinsic_setjmp", s.len) == 0, 137 "alias target = '%.*s', want '__intrinsic_setjmp'", (int)s.len, 138 s.s); 139 } 140 obj_free(ob); 141 } 142 } 143 144 /* Negative: SEARCH_LIBRARY (kit's own STB_WEAK emit shape) is not an alias. 145 */ 146 { 147 size_t len = build_coff(buf, "wlib", SEARCH_LIBRARY); 148 ObjBuilder* ob = read_coff(c, "weak_lib.o", buf, len); 149 EXPECT(ob != NULL, "read_coff (lib) returned NULL"); 150 if (ob) { 151 ObjSymId id = obj_symbol_find(ob, intern(c, "wlib")); 152 EXPECT(id != OBJ_SYM_NONE, "wlib symbol not found after read"); 153 EXPECT(obj_get_weak_alias(ob, id) == 0, 154 "SEARCH_LIBRARY weak should not record an alias target"); 155 obj_free(ob); 156 } 157 } 158 159 kit_unit_summary(&g_u, "coff_weak_alias_test"); 160 return kit_unit_status(&g_u); 161 }