Debian Codegraph @

src/host/buildvm_asm.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 ** LuaJIT VM builder: Assembler source code emitter.
3 ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h
4 */
5
6 #include "buildvm.h"
7 #include "lj_bc.h"
8
9 /* ------------------------------------------------------------------------ */
10
11 #if LJ_TARGET_X86ORX64
12 /* Emit bytes piecewise as assembler text. */
13 static void emit_asm_bytes(BuildCtx *ctx, uint8_t *p, int n)
14 {
15 int i;
16 for (i = 0; i < n; i++) {
17 if ((i & 15) == 0)
18 fprintf(ctx->fp, "\t.byte %d", p[i]);
19 else
20 fprintf(ctx->fp, ",%d", p[i]);
21 if ((i & 15) == 15) putc('\n', ctx->fp);
22 }
23 if ((n & 15) != 0) putc('\n', ctx->fp);
24 }
25
26 /* Emit relocation */
27 static void emit_asm_reloc(BuildCtx *ctx, int type, const char *sym)
28 {
29 switch (ctx->mode) {
30 case BUILD_elfasm:
31 if (type)
32 fprintf(ctx->fp, "\t.long %s-.-4\n", sym);
33 else
34 fprintf(ctx->fp, "\t.long %s\n", sym);
35 break;
36 case BUILD_coffasm:
37 fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", sym);
38 if (type)
39 fprintf(ctx->fp, "\t.long %s-.-4\n", sym);
40 else
41 fprintf(ctx->fp, "\t.long %s\n", sym);
42 break;
43 default: /* BUILD_machasm for relative relocations handled below. */
44 fprintf(ctx->fp, "\t.long %s\n", sym);
45 break;
46 }
47 }
48
49 static const char *const jccnames[] = {
50 "jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "ja",
51 "js", "jns", "jpe", "jpo", "jl", "jge", "jle", "jg"
52 };
53
54 /* Emit x86/x64 text relocations. */
55 static void emit_asm_reloc_text(BuildCtx *ctx, uint8_t *cp, int n,
56 const char *sym)
57 {
58 const char *opname = NULL;
59 if (--n < 0) goto err;
60 if (cp[n] == 0xe8) {
61 opname = "call";
62 } else if (cp[n] == 0xe9) {
63 opname = "jmp";
64 } else if (cp[n] >= 0x80 && cp[n] <= 0x8f && n > 0 && cp[n-1] == 0x0f) {
65 opname = jccnames[cp[n]-0x80];
66 n--;
67 } else {
68 err:
69 fprintf(stderr, "Error: unsupported opcode for %s symbol relocation.\n",
70 sym);
71 exit(1);
72 }
73 emit_asm_bytes(ctx, cp, n);
74 if (strncmp(sym+(*sym == '_'), LABEL_PREFIX, sizeof(LABEL_PREFIX)-1)) {
75 /* Various fixups for external symbols outside of our binary. */
76 if (ctx->mode == BUILD_elfasm) {
77 if (LJ_32)
78 fprintf(ctx->fp, "#if __PIC__\n\t%s lj_wrap_%s\n#else\n", opname, sym);
79 fprintf(ctx->fp, "\t%s %s@PLT\n", opname, sym);
80 if (LJ_32)
81 fprintf(ctx->fp, "#endif\n");
82 return;
83 } else if (LJ_32 && ctx->mode == BUILD_machasm) {
84 fprintf(ctx->fp, "\t%s L%s$stub\n", opname, sym);
85 return;
86 }
87 }
88 fprintf(ctx->fp, "\t%s %s\n", opname, sym);
89 }
90 #elif LJ_TARGET_S390X
91 /* Emit halfwords piecewise as assembler text. */
92 static void emit_asm_halfwords(BuildCtx *ctx, uint8_t *p, int n)
93 {
94 uint16_t *cp = (uint16_t*)p;
95 n /= 2;
96 int i;
97 for (i = 0; i < n; i++) {
98 if ((i & 7) == 0)
99 fprintf(ctx->fp, "\t.hword 0x%hx", cp[i]);
100 else
101 fprintf(ctx->fp, ",0x%hx", cp[i]);
102 if ((i & 7) == 7) putc('\n', ctx->fp);
103 }
104 if ((n & 7) != 0) putc('\n', ctx->fp);
105 }
106
107 /* Emit s390x text relocations. */
108 static void emit_asm_reloc_text(BuildCtx *ctx, uint8_t *cp, int n,
109 const char *sym)
110 {
111 if (n & 1 || n < 2) {
112 fprintf(stderr, "Error: instruction stream length invalid: %d.\n", n);
113 exit(1);
114 }
115 n -= 2;
116 const char *opname = NULL;
117 const char *argt = ""; /* Inserted before argument. */
118 int opcode = *(uint16_t*)(&cp[n]);
119 int arg = (opcode>>4) & 0xf;
120 switch (opcode & 0xff0f) {
121 case 0xa705: opname = "bras"; argt = "%r"; break;
122 case 0xc005: opname = "brasl"; argt = "%r"; break;
123 case 0xa704: opname = "brc"; break;
124 case 0xc004: opname = "brcl"; break;
125 default:
126 fprintf(stderr, "Error: unsupported opcode for %s symbol relocation.\n",
127 sym);
128 exit(1);
129 }
130 emit_asm_halfwords(ctx, cp, n);
131 if (strncmp(sym+(*sym == '_'), LABEL_PREFIX, sizeof(LABEL_PREFIX)-1)) {
132 /* Various fixups for external symbols outside of our binary. */
133 fprintf(ctx->fp, "\t%s %s%d, %s@PLT\n", opname, argt, arg, sym);
134 return;
135 }
136 fprintf(ctx->fp, "\t%s %s%d, %s\n", opname, argt, arg, sym);
137 }
138 #else
139 /* Emit words piecewise as assembler text. */
140 static void emit_asm_words(BuildCtx *ctx, uint8_t *p, int n)
141 {
142 int i;
143 for (i = 0; i < n; i += 4) {
144 uint32_t ins = *(uint32_t *)(p+i);
145 #if LJ_TARGET_ARM64 && LJ_BE
146 ins = lj_bswap(ins); /* ARM64 instructions are always little-endian. */
147 #endif
148 if ((i & 15) == 0)
149 fprintf(ctx->fp, "\t.long 0x%08x", ins);
150 else
151 fprintf(ctx->fp, ",0x%08x", ins);
152 if ((i & 15) == 12) putc('\n', ctx->fp);
153 }
154 if ((n & 15) != 0) putc('\n', ctx->fp);
155 }
156
157 /* Emit relocation as part of an instruction. */
158 static void emit_asm_wordreloc(BuildCtx *ctx, uint8_t *p, int n,
159 const char *sym)
160 {
161 uint32_t ins;
162 emit_asm_words(ctx, p, n-4);
163 ins = *(uint32_t *)(p+n-4);
164 #if LJ_TARGET_ARM
165 if ((ins & 0xff000000u) == 0xfa000000u) {
166 fprintf(ctx->fp, "\tblx %s\n", sym);
167 } else if ((ins & 0x0e000000u) == 0x0a000000u) {
168 fprintf(ctx->fp, "\t%s%.2s %s\n", (ins & 0x01000000u) ? "bl" : "b",
169 &"eqnecsccmiplvsvchilsgeltgtle"[2*(ins >> 28)], sym);
170 } else {
171 fprintf(stderr,
172 "Error: unsupported opcode %08x for %s symbol relocation.\n",
173 ins, sym);
174 exit(1);
175 }
176 #elif LJ_TARGET_ARM64
177 if ((ins >> 26) == 0x25u) {
178 fprintf(ctx->fp, "\tbl %s\n", sym);
179 } else {
180 fprintf(stderr,
181 "Error: unsupported opcode %08x for %s symbol relocation.\n",
182 ins, sym);
183 exit(1);
184 }
185 #elif LJ_TARGET_PPC
186 #if LJ_TARGET_PS3
187 #define TOCPREFIX "."
188 #else
189 #define TOCPREFIX ""
190 #endif
191 if ((ins >> 26) == 14) {
192 fprintf(ctx->fp, "\taddi %d,%d,%s\n", (ins >> 21) & 31, (ins >> 16) & 31, sym);
193 } else if ((ins >> 26) == 15) {
194 fprintf(ctx->fp, "\taddis %d,%d,%s\n", (ins >> 21) & 31, (ins >> 16) & 31, sym);
195 } else if ((ins >> 26) == 16) {
196 fprintf(ctx->fp, "\t%s %d, %d, " TOCPREFIX "%s\n",
197 (ins & 1) ? "bcl" : "bc", (ins >> 21) & 31, (ins >> 16) & 31, sym);
198 } else if ((ins >> 26) == 18) {
199 fprintf(ctx->fp, "\t%s " TOCPREFIX "%s\n", (ins & 1) ? "bl" : "b", sym);
200 } else {
201 fprintf(stderr,
202 "Error: unsupported opcode %08x for %s symbol relocation.\n",
203 ins, sym);
204 exit(1);
205 }
206 #elif LJ_TARGET_MIPS
207 fprintf(stderr,
208 "Error: unsupported opcode %08x for %s symbol relocation.\n",
209 ins, sym);
210 exit(1);
211 #else
212 #error "missing relocation support for this architecture"
213 #endif
214 }
215 #endif
216
217 #if LJ_TARGET_ARM
218 #define ELFASM_PX "%%"
219 #else
220 #define ELFASM_PX "@"
221 #endif
222
223 /* Emit an assembler label. */
224 static void emit_asm_label(BuildCtx *ctx, const char *name, int size, int isfunc)
225 {
226 switch (ctx->mode) {
227 case BUILD_elfasm:
228 #if LJ_TARGET_PS3
229 if (!strncmp(name, "lj_vm_", 6) &&
230 strcmp(name, ctx->beginsym) &&
231 !strstr(name, "hook")) {
232 fprintf(ctx->fp,
233 "\n\t.globl %s\n"
234 "\t.section \".opd\",\"aw\"\n"
235 "%s:\n"
236 "\t.long .%s,.TOC.@tocbase32\n"
237 "\t.size %s,8\n"
238 "\t.previous\n"
239 "\t.globl .%s\n"
240 "\t.hidden .%s\n"
241 "\t.type .%s, " ELFASM_PX "function\n"
242 "\t.size .%s, %d\n"
243 ".%s:\n",
244 name, name, name, name, name, name, name, name, size, name);
245 break;
246 }
247 #endif
248 fprintf(ctx->fp,
249 "\n\t.globl %s\n"
250 "\t.hidden %s\n"
251 "\t.type %s, " ELFASM_PX "%s\n"
252 "\t.size %s, %d\n"
253 "%s:\n",
254 name, name, name, isfunc ? "function" : "object", name, size, name);
255 break;
256 case BUILD_coffasm:
257 fprintf(ctx->fp, "\n\t.globl %s\n", name);
258 if (isfunc)
259 fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", name);
260 fprintf(ctx->fp, "%s:\n", name);
261 break;
262 case BUILD_machasm:
263 fprintf(ctx->fp,
264 "\n\t.private_extern %s\n"
265 "\t.no_dead_strip %s\n"
266 "%s:\n", name, name, name);
267 break;
268 default:
269 break;
270 }
271 }
272
273 /* Emit alignment. */
274 static void emit_asm_align(BuildCtx *ctx, int bits)
275 {
276 switch (ctx->mode) {
277 case BUILD_elfasm:
278 case BUILD_coffasm:
279 fprintf(ctx->fp, "\t.p2align %d\n", bits);
280 break;
281 case BUILD_machasm:
282 fprintf(ctx->fp, "\t.align %d\n", bits);
283 break;
284 default:
285 break;
286 }
287 }
288
289 /* ------------------------------------------------------------------------ */
290
291 /* Emit assembler source code. */
292 void emit_asm(BuildCtx *ctx)
293 {
294 int i, rel;
295
296 fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch);
297 #if LJ_ARCH_PPC_ELFV2
298 fprintf(ctx->fp, "\t.abiversion 2\n");
299 #endif
300 fprintf(ctx->fp, "\t.text\n");
301 #if LJ_TARGET_MIPS32 && !LJ_ABI_SOFTFP
302 fprintf(ctx->fp, "\t.module fp=32\n");
303 #endif
304 #if LJ_TARGET_MIPS
305 fprintf(ctx->fp, "\t.set nomips16\n\t.abicalls\n\t.set noreorder\n\t.set nomacro\n");
306 #endif
307 emit_asm_align(ctx, 4);
308
309 #if LJ_TARGET_PS3
310 emit_asm_label(ctx, ctx->beginsym, ctx->codesz, 0);
311 #else
312 emit_asm_label(ctx, ctx->beginsym, 0, 0);
313 #endif
314 if (ctx->mode != BUILD_machasm)
315 fprintf(ctx->fp, ".Lbegin:\n");
316
317 #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND
318 /* This should really be moved into buildvm_arm.dasc. */
319 #if LJ_ARCH_HASFPU
320 fprintf(ctx->fp,
321 ".fnstart\n"
322 ".save {r5, r6, r7, r8, r9, r10, r11, lr}\n"
323 ".vsave {d8-d15}\n"
324 ".save {r4}\n"
325 ".pad #28\n");
326 #else
327 fprintf(ctx->fp,
328 ".fnstart\n"
329 ".save {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
330 ".pad #28\n");
331 #endif
332 #endif
333
334 for (i = rel = 0; i < ctx->nsym; i++) {
335 int32_t ofs = ctx->sym[i].ofs;
336 int32_t next = ctx->sym[i+1].ofs;
337 #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND && LJ_HASFFI
338 if (!strcmp(ctx->sym[i].name, "lj_vm_ffi_call"))
339 fprintf(ctx->fp,
340 ".globl lj_err_unwind_arm\n"
341 ".personality lj_err_unwind_arm\n"
342 ".fnend\n"
343 ".fnstart\n"
344 ".save {r4, r5, r11, lr}\n"
345 ".setfp r11, sp\n");
346 #endif
347 emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1);
348 while (rel < ctx->nreloc && ctx->reloc[rel].ofs <= next) {
349 BuildReloc *r = &ctx->reloc[rel];
350 int n = r->ofs - ofs;
351 #if LJ_TARGET_X86ORX64
352 if (r->type != 0 &&
353 (ctx->mode == BUILD_elfasm || ctx->mode == BUILD_machasm)) {
354 emit_asm_reloc_text(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
355 } else {
356 emit_asm_bytes(ctx, ctx->code+ofs, n);
357 emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]);
358 }
359 ofs += n+4;
360 #elif LJ_TARGET_S390X
361 emit_asm_reloc_text(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
362 ofs += n+4;
363 #else
364 emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
365 ofs += n;
366 #endif
367 rel++;
368 }
369 #if LJ_TARGET_X86ORX64
370 emit_asm_bytes(ctx, ctx->code+ofs, next-ofs);
371 #elif LJ_TARGET_S390X
372 emit_asm_halfwords(ctx, ctx->code+ofs, next-ofs);
373 #else
374 emit_asm_words(ctx, ctx->code+ofs, next-ofs);
375 #endif
376 }
377
378 #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND
379 fprintf(ctx->fp,
380 #if !LJ_HASFFI
381 ".globl lj_err_unwind_arm\n"
382 ".personality lj_err_unwind_arm\n"
383 #endif
384 ".fnend\n");
385 #endif
386
387 fprintf(ctx->fp, "\n");
388 switch (ctx->mode) {
389 case BUILD_elfasm:
390 #if !(LJ_TARGET_PS3 || LJ_TARGET_PSVITA)
391 fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n");
392 #endif
393 #if LJ_TARGET_PPC && !LJ_TARGET_PS3 && !LJ_ABI_SOFTFP
394 /* Hard-float ABI. */
395 fprintf(ctx->fp, "\t.gnu_attribute 4, 1\n");
396 #endif
397 /* fallthrough */
398 case BUILD_coffasm:
399 fprintf(ctx->fp, "\t.ident \"%s\"\n", ctx->dasm_ident);
400 break;
401 case BUILD_machasm:
402 #if defined(__apple_build_version__) && __apple_build_version__ >= 15000000 && __apple_build_version__ < 15000300
403 /* Workaround for XCode 15.0 - 15.2. */
404 fprintf(ctx->fp, "\t.subsections_via_symbols\n");
405 #endif
406 fprintf(ctx->fp,
407 "\t.cstring\n"
408 "\t.ascii \"%s\\0\"\n", ctx->dasm_ident);
409 break;
410 default:
411 break;
412 }
413 fprintf(ctx->fp, "\n");
414 }
415