Debian Codegraph @

libs/luajit/LuaJIT-src/src/host/buildvm_peobj.c

raw
1 /*
2 ** LuaJIT VM builder: PE object emitter.
3 ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h
4 **
5 ** Only used for building on Windows, since we cannot assume the presence
6 ** of a suitable assembler. The host and target byte order must match.
7 */
8
9 #include "buildvm.h"
10 #include "lj_bc.h"
11
12 #if LJ_TARGET_WINDOWS || LJ_TARGET_CYGWIN
13
14 /* Context for PE object emitter. */
15 static char *strtab;
16 static size_t strtabofs;
17
18 /* -- PE object definitions ----------------------------------------------- */
19
20 /* PE header. */
21 typedef struct PEheader {
22 uint16_t arch;
23 uint16_t nsects;
24 uint32_t time;
25 uint32_t symtabofs;
26 uint32_t nsyms;
27 uint16_t opthdrsz;
28 uint16_t flags;
29 } PEheader;
30
31 /* PE section. */
32 typedef struct PEsection {
33 char name[8];
34 uint32_t vsize;
35 uint32_t vaddr;
36 uint32_t size;
37 uint32_t ofs;
38 uint32_t relocofs;
39 uint32_t lineofs;
40 uint16_t nreloc;
41 uint16_t nline;
42 uint32_t flags;
43 } PEsection;
44
45 /* PE relocation. */
46 typedef struct PEreloc {
47 uint32_t vaddr;
48 uint32_t symidx;
49 uint16_t type;
50 } PEreloc;
51
52 /* Cannot use sizeof, because it pads up to the max. alignment. */
53 #define PEOBJ_RELOC_SIZE (4+4+2)
54
55 /* PE symbol table entry. */
56 typedef struct PEsym {
57 union {
58 char name[8];
59 uint32_t nameref[2];
60 } n;
61 uint32_t value;
62 int16_t sect;
63 uint16_t type;
64 uint8_t scl;
65 uint8_t naux;
66 } PEsym;
67
68 /* PE symbol table auxiliary entry for a section. */
69 typedef struct PEsymaux {
70 uint32_t size;
71 uint16_t nreloc;
72 uint16_t nline;
73 uint32_t cksum;
74 uint16_t assoc;
75 uint8_t comdatsel;
76 uint8_t unused[3];
77 } PEsymaux;
78
79 /* Cannot use sizeof, because it pads up to the max. alignment. */
80 #define PEOBJ_SYM_SIZE (8+4+2+2+1+1)
81
82 /* PE object CPU specific defines. */
83 #if LJ_TARGET_X86
84 #define PEOBJ_ARCH_TARGET 0x014c
85 #define PEOBJ_RELOC_REL32 0x14 /* MS: REL32, GNU: DISP32. */
86 #define PEOBJ_RELOC_DIR32 0x06
87 #define PEOBJ_RELOC_OFS 0
88 #define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */
89 #elif LJ_TARGET_X64
90 #define PEOBJ_ARCH_TARGET 0x8664
91 #define PEOBJ_RELOC_REL32 0x04 /* MS: REL32, GNU: DISP32. */
92 #define PEOBJ_RELOC_DIR32 0x02
93 #define PEOBJ_RELOC_ADDR32NB 0x03
94 #define PEOBJ_RELOC_OFS 0
95 #define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */
96 #define PEOBJ_PDATA_NRELOC 6
97 #define PEOBJ_XDATA_SIZE (8*2+4+6*2)
98 #elif LJ_TARGET_ARM64
99 #define PEOBJ_ARCH_TARGET 0xaa64
100 #define PEOBJ_RELOC_REL32 0x03 /* MS: BRANCH26. */
101 #define PEOBJ_RELOC_DIR32 0x01
102 #define PEOBJ_RELOC_ADDR32NB 0x02
103 #define PEOBJ_RELOC_OFS (-4)
104 #define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */
105 #define PEOBJ_PDATA_NRELOC 4
106 #define PEOBJ_XDATA_SIZE (4+24+4 +4+8)
107 #endif
108
109 /* Section numbers (0-based). */
110 enum {
111 PEOBJ_SECT_ABS = -2,
112 PEOBJ_SECT_UNDEF = -1,
113 PEOBJ_SECT_TEXT,
114 #ifdef PEOBJ_PDATA_NRELOC
115 PEOBJ_SECT_PDATA,
116 PEOBJ_SECT_XDATA,
117 #elif LJ_TARGET_X86
118 PEOBJ_SECT_SXDATA,
119 #endif
120 PEOBJ_SECT_RDATA_Z,
121 PEOBJ_NSECTIONS
122 };
123
124 /* Symbol types. */
125 #define PEOBJ_TYPE_NULL 0
126 #define PEOBJ_TYPE_FUNC 0x20
127
128 /* Symbol storage class. */
129 #define PEOBJ_SCL_EXTERN 2
130 #define PEOBJ_SCL_STATIC 3
131
132 /* -- PE object emitter --------------------------------------------------- */
133
134 /* Emit PE object symbol. */
135 static void emit_peobj_sym(BuildCtx *ctx, const char *name, uint32_t value,
136 int sect, int type, int scl)
137 {
138 PEsym sym;
139 size_t len = strlen(name);
140 if (!strtab) { /* Pass 1: only calculate string table length. */
141 if (len > 8) strtabofs += len+1;
142 return;
143 }
144 if (len <= 8) {
145 memcpy(sym.n.name, name, len);
146 memset(sym.n.name+len, 0, 8-len);
147 } else {
148 sym.n.nameref[0] = 0;
149 sym.n.nameref[1] = (uint32_t)strtabofs;
150 memcpy(strtab + strtabofs, name, len);
151 strtab[strtabofs+len] = 0;
152 strtabofs += len+1;
153 }
154 sym.value = value;
155 sym.sect = (int16_t)(sect+1); /* 1-based section number. */
156 sym.type = (uint16_t)type;
157 sym.scl = (uint8_t)scl;
158 sym.naux = 0;
159 owrite(ctx, &sym, PEOBJ_SYM_SIZE);
160 }
161
162 /* Emit PE object section symbol. */
163 static void emit_peobj_sym_sect(BuildCtx *ctx, PEsection *pesect, int sect)
164 {
165 PEsym sym;
166 PEsymaux aux;
167 if (!strtab) return; /* Pass 1: no output. */
168 memcpy(sym.n.name, pesect[sect].name, 8);
169 sym.value = 0;
170 sym.sect = (int16_t)(sect+1); /* 1-based section number. */
171 sym.type = PEOBJ_TYPE_NULL;
172 sym.scl = PEOBJ_SCL_STATIC;
173 sym.naux = 1;
174 owrite(ctx, &sym, PEOBJ_SYM_SIZE);
175 memset(&aux, 0, sizeof(PEsymaux));
176 aux.size = pesect[sect].size;
177 aux.nreloc = pesect[sect].nreloc;
178 owrite(ctx, &aux, PEOBJ_SYM_SIZE);
179 }
180
181 /* Emit Windows PE object file. */
182 void emit_peobj(BuildCtx *ctx)
183 {
184 PEheader pehdr;
185 PEsection pesect[PEOBJ_NSECTIONS];
186 uint32_t sofs;
187 int i, nrsym;
188 union { uint8_t b; uint32_t u; } host_endian;
189 #ifdef PEOBJ_PDATA_NRELOC
190 uint32_t fcofs = (uint32_t)ctx->sym[ctx->nsym-1].ofs;
191 #endif
192
193 sofs = sizeof(PEheader) + PEOBJ_NSECTIONS*sizeof(PEsection);
194
195 /* Fill in PE sections. */
196 memset(&pesect, 0, PEOBJ_NSECTIONS*sizeof(PEsection));
197 memcpy(pesect[PEOBJ_SECT_TEXT].name, ".text", sizeof(".text")-1);
198 pesect[PEOBJ_SECT_TEXT].ofs = sofs;
199 sofs += (pesect[PEOBJ_SECT_TEXT].size = (uint32_t)ctx->codesz);
200 pesect[PEOBJ_SECT_TEXT].relocofs = sofs;
201 sofs += (pesect[PEOBJ_SECT_TEXT].nreloc = (uint16_t)ctx->nreloc) * PEOBJ_RELOC_SIZE;
202 /* Flags: 60 = read+execute, 50 = align16, 20 = code. */
203 pesect[PEOBJ_SECT_TEXT].flags = PEOBJ_TEXT_FLAGS;
204
205 #ifdef PEOBJ_PDATA_NRELOC
206 memcpy(pesect[PEOBJ_SECT_PDATA].name, ".pdata", sizeof(".pdata")-1);
207 pesect[PEOBJ_SECT_PDATA].ofs = sofs;
208 sofs += (pesect[PEOBJ_SECT_PDATA].size = PEOBJ_PDATA_NRELOC*4);
209 pesect[PEOBJ_SECT_PDATA].relocofs = sofs;
210 sofs += (pesect[PEOBJ_SECT_PDATA].nreloc = PEOBJ_PDATA_NRELOC) * PEOBJ_RELOC_SIZE;
211 /* Flags: 40 = read, 30 = align4, 40 = initialized data. */
212 pesect[PEOBJ_SECT_PDATA].flags = 0x40300040;
213
214 memcpy(pesect[PEOBJ_SECT_XDATA].name, ".xdata", sizeof(".xdata")-1);
215 pesect[PEOBJ_SECT_XDATA].ofs = sofs;
216 sofs += (pesect[PEOBJ_SECT_XDATA].size = PEOBJ_XDATA_SIZE); /* See below. */
217 pesect[PEOBJ_SECT_XDATA].relocofs = sofs;
218 sofs += (pesect[PEOBJ_SECT_XDATA].nreloc = 1) * PEOBJ_RELOC_SIZE;
219 /* Flags: 40 = read, 30 = align4, 40 = initialized data. */
220 pesect[PEOBJ_SECT_XDATA].flags = 0x40300040;
221 #elif LJ_TARGET_X86
222 memcpy(pesect[PEOBJ_SECT_SXDATA].name, ".sxdata", sizeof(".sxdata")-1);
223 pesect[PEOBJ_SECT_SXDATA].ofs = sofs;
224 sofs += (pesect[PEOBJ_SECT_SXDATA].size = 4);
225 pesect[PEOBJ_SECT_SXDATA].relocofs = sofs;
226 /* Flags: 40 = read, 30 = align4, 02 = lnk_info, 40 = initialized data. */
227 pesect[PEOBJ_SECT_SXDATA].flags = 0x40300240;
228 #endif
229
230 memcpy(pesect[PEOBJ_SECT_RDATA_Z].name, ".rdata$Z", sizeof(".rdata$Z")-1);
231 pesect[PEOBJ_SECT_RDATA_Z].ofs = sofs;
232 sofs += (pesect[PEOBJ_SECT_RDATA_Z].size = (uint32_t)strlen(ctx->dasm_ident)+1);
233 /* Flags: 40 = read, 30 = align4, 40 = initialized data. */
234 pesect[PEOBJ_SECT_RDATA_Z].flags = 0x40300040;
235
236 /* Fill in PE header. */
237 pehdr.arch = PEOBJ_ARCH_TARGET;
238 pehdr.nsects = PEOBJ_NSECTIONS;
239 pehdr.time = 0; /* Timestamp is optional. */
240 pehdr.symtabofs = sofs;
241 pehdr.opthdrsz = 0;
242 pehdr.flags = 0;
243
244 /* Compute the size of the symbol table:
245 ** @feat.00 + nsections*2
246 ** + asm_start + nsym
247 ** + nrsym
248 */
249 nrsym = ctx->nrelocsym;
250 pehdr.nsyms = 1+PEOBJ_NSECTIONS*2 + 1+ctx->nsym + nrsym;
251 #ifdef PEOBJ_PDATA_NRELOC
252 pehdr.nsyms += 1; /* Symbol for lj_err_unwind_win. */
253 #endif
254
255 /* Write PE object header and all sections. */
256 owrite(ctx, &pehdr, sizeof(PEheader));
257 owrite(ctx, &pesect, sizeof(PEsection)*PEOBJ_NSECTIONS);
258
259 /* Write .text section. */
260 host_endian.u = 1;
261 if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) {
262 fprintf(stderr, "Error: different byte order for host and target\n");
263 exit(1);
264 }
265 owrite(ctx, ctx->code, ctx->codesz);
266 for (i = 0; i < ctx->nreloc; i++) {
267 PEreloc reloc;
268 reloc.vaddr = (uint32_t)ctx->reloc[i].ofs + PEOBJ_RELOC_OFS;
269 reloc.symidx = 1+2+ctx->reloc[i].sym; /* Reloc syms are after .text sym. */
270 reloc.type = ctx->reloc[i].type ? PEOBJ_RELOC_REL32 : PEOBJ_RELOC_DIR32;
271 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
272 }
273
274 #if LJ_TARGET_X64
275 { /* Write .pdata section. */
276 uint32_t pdata[3]; /* Start of .text, end of .text and .xdata. */
277 PEreloc reloc;
278 pdata[0] = 0; pdata[1] = fcofs; pdata[2] = 0;
279 owrite(ctx, &pdata, sizeof(pdata));
280 pdata[0] = fcofs; pdata[1] = (uint32_t)ctx->codesz; pdata[2] = 20;
281 owrite(ctx, &pdata, sizeof(pdata));
282 reloc.vaddr = 0; reloc.symidx = 1+2+nrsym+2+2+1;
283 reloc.type = PEOBJ_RELOC_ADDR32NB;
284 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
285 reloc.vaddr = 4; reloc.symidx = 1+2+nrsym+2+2+1;
286 reloc.type = PEOBJ_RELOC_ADDR32NB;
287 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
288 reloc.vaddr = 8; reloc.symidx = 1+2+nrsym+2;
289 reloc.type = PEOBJ_RELOC_ADDR32NB;
290 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
291 reloc.vaddr = 12; reloc.symidx = 1+2+nrsym+2+2+1;
292 reloc.type = PEOBJ_RELOC_ADDR32NB;
293 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
294 reloc.vaddr = 16; reloc.symidx = 1+2+nrsym+2+2+1;
295 reloc.type = PEOBJ_RELOC_ADDR32NB;
296 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
297 reloc.vaddr = 20; reloc.symidx = 1+2+nrsym+2;
298 reloc.type = PEOBJ_RELOC_ADDR32NB;
299 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
300 }
301 { /* Write .xdata section. */
302 uint16_t xdata[8+2+6];
303 PEreloc reloc;
304 xdata[0] = 0x01|0x08|0x10; /* Ver. 1, uhandler/ehandler, prolog size 0. */
305 xdata[1] = 0x0005; /* Number of unwind codes, no frame pointer. */
306 xdata[2] = 0x4200; /* Stack offset 4*8+8 = aword*5. */
307 xdata[3] = 0x3000; /* Push rbx. */
308 xdata[4] = 0x6000; /* Push rsi. */
309 xdata[5] = 0x7000; /* Push rdi. */
310 xdata[6] = 0x5000; /* Push rbp. */
311 xdata[7] = 0; /* Alignment. */
312 xdata[8] = xdata[9] = 0; /* Relocated address of exception handler. */
313 xdata[10] = 0x01; /* Ver. 1, no handler, prolog size 0. */
314 xdata[11] = 0x1504; /* Number of unwind codes, fp = rbp, fpofs = 16. */
315 xdata[12] = 0x0300; /* set_fpreg. */
316 xdata[13] = 0x0200; /* stack offset 0*8+8 = aword*1. */
317 xdata[14] = 0x3000; /* Push rbx. */
318 xdata[15] = 0x5000; /* Push rbp. */
319 owrite(ctx, &xdata, sizeof(xdata));
320 reloc.vaddr = 2*8; reloc.symidx = 1+2+nrsym+2+2;
321 reloc.type = PEOBJ_RELOC_ADDR32NB;
322 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
323 }
324 #elif LJ_TARGET_ARM64
325 /* https://learn.microsoft.com/en-us/cpp/build/arm64-exception-handling */
326 { /* Write .pdata section. */
327 uint32_t pdata[4];
328 PEreloc reloc;
329 pdata[0] = 0;
330 pdata[1] = 0;
331 pdata[2] = fcofs;
332 pdata[3] = 4+24+4;
333 owrite(ctx, &pdata, sizeof(pdata));
334 /* Start of .text and start of .xdata. */
335 reloc.vaddr = 0; reloc.symidx = 1+2+nrsym+2+2+1;
336 reloc.type = PEOBJ_RELOC_ADDR32NB;
337 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
338 reloc.vaddr = 4; reloc.symidx = 1+2+nrsym+2;
339 reloc.type = PEOBJ_RELOC_ADDR32NB;
340 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
341 /* Start of vm_ffi_call and start of second part of .xdata. */
342 reloc.vaddr = 8; reloc.symidx = 1+2+nrsym+2+2+1;
343 reloc.type = PEOBJ_RELOC_ADDR32NB;
344 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
345 reloc.vaddr = 12; reloc.symidx = 1+2+nrsym+2;
346 reloc.type = PEOBJ_RELOC_ADDR32NB;
347 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
348 }
349 { /* Write .xdata section. */
350 uint32_t u32;
351 uint8_t *p, uwc[24];
352 PEreloc reloc;
353
354 #define CBE16(x) (*p = ((x) >> 8) & 0xff, p[1] = (x) & 0xff, p += 2)
355 #define CALLOC_S(s) (*p++ = ((s) >> 4)) /* s < 512 */
356 #define CSAVE_FPLR(o) (*p++ = 0x40 | ((o) >> 3)) /* o <= 504 */
357 #define CSAVE_REGP(r,o) CBE16(0xc800 | (((r) - 19) << 6) | ((o) >> 3))
358 #define CSAVE_REGS(r1,r2,o1) do { \
359 int r, o; for (r = r1, o = o1; r <= r2; r += 2, o -= 16) CSAVE_REGP(r, o); \
360 } while (0)
361 #define CSAVE_REGPX(r,o) CBE16(0xcc00 | (((r) - 19) << 6) | (~(o) >> 3))
362 #define CSAVE_FREGP(r,o) CBE16(0xd800 | (((r) - 8) << 6) | ((o) >> 3))
363 #define CSAVE_FREGS(r1,r2,o1) do { \
364 int r, o; for (r = r1, o = o1; r <= r2; r += 2, o -= 16) CSAVE_FREGP(r, o); \
365 } while (0)
366 #define CADD_FP(s) CBE16(0xe200 | ((s) >> 3)) /* s < 8*256 */
367 #define CODE_NOP 0xe3
368 #define CODE_END 0xe4
369 #define CEND_ALIGN do { \
370 *p++ = CODE_END; \
371 while ((p - uwc) & 3) *p++ = CODE_NOP; \
372 } while (0)
373
374 /* Unwind codes for .text section with handler. */
375 p = uwc;
376 CADD_FP(192); /* +2 */
377 CSAVE_REGS(19, 28, 176); /* +5*2 */
378 CSAVE_FREGS(8, 15, 96); /* +4*2 */
379 CSAVE_FPLR(192); /* +1 */
380 CALLOC_S(208); /* +1 */
381 CEND_ALIGN; /* +1 +1 -> 24 */
382
383 u32 = ((24u >> 2) << 27) | (1u << 20) | (fcofs >> 2);
384 owrite(ctx, &u32, 4);
385 owrite(ctx, &uwc, 24);
386
387 u32 = 0; /* Handler RVA to be relocated at 4 + 24. */
388 owrite(ctx, &u32, 4);
389
390 /* Unwind codes for vm_ffi_call without handler. */
391 p = uwc;
392 CADD_FP(16); /* +2 */
393 CSAVE_FPLR(16); /* +1 */
394 CSAVE_REGPX(19, -32); /* +2 */
395 CEND_ALIGN; /* +1 +2 -> 8 */
396
397 u32 = ((8u >> 2) << 27) | (((uint32_t)ctx->codesz - fcofs) >> 2);
398 owrite(ctx, &u32, 4);
399 owrite(ctx, &uwc, 8);
400
401 reloc.vaddr = 4 + 24; reloc.symidx = 1+2+nrsym+2+2;
402 reloc.type = PEOBJ_RELOC_ADDR32NB;
403 owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
404 }
405 #elif LJ_TARGET_X86
406 /* Write .sxdata section. */
407 for (i = 0; i < nrsym; i++) {
408 if (!strcmp(ctx->relocsym[i], "_lj_err_unwind_win")) {
409 uint32_t symidx = 1+2+i;
410 owrite(ctx, &symidx, 4);
411 break;
412 }
413 }
414 if (i == nrsym) {
415 fprintf(stderr, "Error: extern lj_err_unwind_win not used\n");
416 exit(1);
417 }
418 #endif
419
420 /* Write .rdata$Z section. */
421 owrite(ctx, ctx->dasm_ident, strlen(ctx->dasm_ident)+1);
422
423 /* Write symbol table. */
424 strtab = NULL; /* 1st pass: collect string sizes. */
425 for (;;) {
426 strtabofs = 4;
427 /* Mark as SafeSEH compliant. */
428 emit_peobj_sym(ctx, "@feat.00", 1,
429 PEOBJ_SECT_ABS, PEOBJ_TYPE_NULL, PEOBJ_SCL_STATIC);
430
431 emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_TEXT);
432 for (i = 0; i < nrsym; i++)
433 emit_peobj_sym(ctx, ctx->relocsym[i], 0,
434 PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);
435
436 #ifdef PEOBJ_PDATA_NRELOC
437 emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_PDATA);
438 emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_XDATA);
439 emit_peobj_sym(ctx, "lj_err_unwind_win", 0,
440 PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);
441 #elif LJ_TARGET_X86
442 emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_SXDATA);
443 #endif
444
445 emit_peobj_sym(ctx, ctx->beginsym, 0,
446 PEOBJ_SECT_TEXT, PEOBJ_TYPE_NULL, PEOBJ_SCL_EXTERN);
447 for (i = 0; i < ctx->nsym; i++)
448 emit_peobj_sym(ctx, ctx->sym[i].name, (uint32_t)ctx->sym[i].ofs,
449 PEOBJ_SECT_TEXT, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);
450
451 emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_RDATA_Z);
452
453 if (strtab)
454 break;
455 /* 2nd pass: alloc strtab, write syms and copy strings. */
456 strtab = (char *)malloc(strtabofs);
457 *(uint32_t *)strtab = (uint32_t)strtabofs;
458 }
459
460 /* Write string table. */
461 owrite(ctx, strtab, strtabofs);
462 }
463
464 #else
465
466 void emit_peobj(BuildCtx *ctx)
467 {
468 UNUSED(ctx);
469 fprintf(stderr, "Error: no PE object support for this target\n");
470 exit(1);
471 }
472
473 #endif