• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nickg / nvc / 14813902120

03 May 2025 07:22PM UTC coverage: 92.348% (+0.004%) from 92.344%
14813902120

push

github

nickg
Fix macOS debug symbol UUID check

69241 of 74978 relevant lines covered (92.35%)

415922.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.16
/src/jit/jit-code.c
1
//
2
//  Copyright (C) 2022-2024  Nick Gasson
3
//
4
//  This program is free software: you can redistribute it and/or modify
5
//  it under the terms of the GNU General Public License as published by
6
//  the Free Software Foundation, either version 3 of the License, or
7
//  (at your option) any later version.
8
//
9
//  This program is distributed in the hope that it will be useful,
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
//  GNU General Public License for more details.
13
//
14
//  You should have received a copy of the GNU General Public License
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

18
#include "util.h"
19
#include "cpustate.h"
20
#include "debug.h"
21
#include "hash.h"
22
#include "ident.h"
23
#include "jit/jit-priv.h"
24
#include "option.h"
25
#include "thread.h"
26

27
#include <assert.h>
28
#include <math.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <stdio.h>
32
#include <unistd.h>
33
#include <inttypes.h>
34

35
#if defined __MINGW32__
36
#include <winnt.h>
37
#elif defined __APPLE__
38
#include <mach-o/loader.h>
39
#include <mach-o/reloc.h>
40
#include <mach-o/nlist.h>
41
#include <mach-o/stab.h>
42
#include <mach-o/arm64/reloc.h>
43
#include <mach-o/x86_64/reloc.h>
44
#else
45
#include <elf.h>
46
#endif
47

48
#ifdef HAVE_CAPSTONE
49
#include <capstone.h>
50
#endif
51

52
#ifndef R_AARCH64_MOVW_UABS_G0_NC
53
#define R_AARCH64_MOVW_UABS_G0_NC 264
54
#endif
55

56
#ifndef R_AARCH64_MOVW_UABS_G1_NC
57
#define R_AARCH64_MOVW_UABS_G1_NC 266
58
#endif
59

60
#ifndef R_AARCH64_MOVW_UABS_G2_NC
61
#define R_AARCH64_MOVW_UABS_G2_NC 268
62
#endif
63

64
#ifndef R_AARCH64_MOVW_UABS_G3
65
#define R_AARCH64_MOVW_UABS_G3 269
66
#endif
67

68
#ifndef SHT_X86_64_UNWIND
69
#define SHT_X86_64_UNWIND 0x70000001
70
#endif
71

72
#ifndef IMAGE_REL_ARM64_BRANCH26
73
#define IMAGE_REL_ARM64_BRANCH26 0x03
74
#endif
75

76
#ifndef IMAGE_REL_ARM64_ADDR32NB
77
#define IMAGE_REL_ARM64_ADDR32NB 0x02
78
#endif
79

80
#ifndef IMAGE_REL_ARM64_PAGEBASE_REL21
81
#define IMAGE_REL_ARM64_PAGEBASE_REL21 0x04
82
#endif
83

84
#ifndef IMAGE_REL_ARM64_PAGEOFFSET_12A
85
#define IMAGE_REL_ARM64_PAGEOFFSET_12A 0x06
86
#endif
87

88
#ifndef IMAGE_REL_ARM64_PAGEOFFSET_12L
89
#define IMAGE_REL_ARM64_PAGEOFFSET_12L 0x07
90
#endif
91

92
#define CODE_PAGE_ALIGN   4096
93
#define CODE_PAGE_SIZE    0x400000
94
#define THREAD_CACHE_SIZE 0x10000
95
#define CODE_BLOB_ALIGN   256
96
#define MIN_BLOB_SIZE     0x4000
97

98
#define __IMM64(x) __IMM32(x), __IMM32((x) >> 32)
99
#define __IMM32(x) __IMM16(x), __IMM16((x) >> 16)
100
#define __IMM16(x) (x) & 0xff, ((x) >> 8) & 0xff
101

102
STATIC_ASSERT(MIN_BLOB_SIZE <= THREAD_CACHE_SIZE);
103
STATIC_ASSERT(MIN_BLOB_SIZE % CODE_BLOB_ALIGN == 0);
104
STATIC_ASSERT(CODE_PAGE_SIZE % THREAD_CACHE_SIZE == 0);
105

106
typedef struct _code_page code_page_t;
107

108
typedef struct {
109
   uintptr_t  addr;
110
   char      *text;
111
} code_comment_t;
112

113
typedef struct {
114
   unsigned        count;
115
   unsigned        max;
116
   code_comment_t *comments;
117
} code_debug_t;
118

119
typedef struct _code_span {
120
   code_cache_t *owner;
121
   code_span_t  *next;
122
   ident_t       name;
123
   uint8_t      *base;
124
   void         *entry;
125
   size_t        size;
126
#ifdef DEBUG
127
   code_debug_t  debug;
128
#endif
129
} code_span_t;
130

131
typedef struct _patch_list {
132
   patch_list_t    *next;
133
   uint8_t         *wptr;
134
   jit_label_t      label;
135
   code_patch_fn_t  fn;
136
} patch_list_t;
137

138
typedef struct _code_page {
139
   code_cache_t *owner;
140
   code_page_t  *next;
141
   uint8_t      *mem;
142
} code_page_t;
143

144
typedef struct _code_cache {
145
   nvc_lock_t   lock;
146
   code_page_t *pages;
147
   code_span_t *spans;
148
   code_span_t *freelist[MAX_THREADS];
149
   code_span_t *globalfree;
150
   shash_t     *symbols;
151
   FILE        *perfmap;
152
#ifdef HAVE_CAPSTONE
153
   csh          capstone;
154
#endif
155
#ifdef DEBUG
156
   size_t       used;
157
#endif
158
} code_cache_t;
159

160
static void code_disassemble(code_span_t *span, uintptr_t mark,
161
                             struct cpu_state *cpu);
162

163
static void code_cache_unwinder(uintptr_t addr, debug_frame_t *frame,
×
164
                                void *context)
165
{
166
   code_cache_t *code = context;
×
167

168
   const uint8_t *pc = (uint8_t *)addr;
×
169
   for (code_span_t *span = code->spans; span; span = span->next) {
×
170
      if (pc >= span->base && pc < span->base + span->size) {
×
171
         frame->kind = FRAME_VHDL;
×
172
         frame->disp = pc - span->base;
×
173
         frame->symbol = istr(span->name);
×
174
      }
175
   }
176
}
×
177

178
static void code_fault_handler(int sig, void *addr, struct cpu_state *cpu,
×
179
                               void *context)
180
{
181
   code_page_t *page = context;
×
182

183
   const uint8_t *pc = (uint8_t *)cpu->pc;
×
184
   if (pc < page->mem || pc > page->mem + CODE_PAGE_SIZE)
×
185
      return;
186

187
   uintptr_t mark = cpu->pc;
×
188
#ifndef __MINGW32__
189
   if (sig == SIGTRAP)
×
190
      mark--;   // Point to faulting instruction
×
191
#endif
192

193
   for (code_span_t *span = page->owner->spans; span; span = span->next) {
×
194
      if (pc >= span->base && pc < span->base + span->size && span->name)
×
195
         code_disassemble(span, mark, cpu);
×
196
   }
197
}
198

199
#ifdef DEBUG
200
static bool code_cache_contains(code_cache_t *code, uint8_t *base, size_t size)
9,472✔
201
{
202
   assert_lock_held(&code->lock);
9,472✔
203

204
   for (code_page_t *p = code->pages; p; p = p->next) {
9,472✔
205
      if (base >= p->mem && base + size <= p->mem + CODE_PAGE_SIZE)
9,472✔
206
         return true;
207
   }
208

209
   return false;
210
}
211
#endif
212

213
static code_span_t *code_span_new(code_cache_t *code, ident_t name,
9,472✔
214
                                  uint8_t *base, size_t size)
215
{
216
   SCOPED_LOCK(code->lock);
9,472✔
217

218
   assert(code_cache_contains(code, base, size));
9,472✔
219

220
   code_span_t *span = xcalloc(sizeof(code_span_t));
9,472✔
221
   span->name  = name;
9,472✔
222
   span->next  = code->spans;
9,472✔
223
   span->base  = base;
9,472✔
224
   span->entry = base;
9,472✔
225
   span->size  = size;
9,472✔
226
   span->owner = code;
9,472✔
227

228
   code->spans = span;
9,472✔
229
   return span;
9,472✔
230
}
231

232
static void code_page_new(code_cache_t *code)
3,597✔
233
{
234
   assert_lock_held(&code->lock);
3,597✔
235

236
   code_page_t *page = xcalloc(sizeof(code_page_t));
3,597✔
237
   page->owner = code;
3,597✔
238
   page->next  = code->pages;
3,597✔
239
   page->mem   = map_jit_pages(CODE_PAGE_ALIGN, CODE_PAGE_SIZE);
3,597✔
240

241
   add_fault_handler(code_fault_handler, page);
3,597✔
242
   debug_add_unwinder(page->mem, CODE_PAGE_SIZE, code_cache_unwinder, code);
3,597✔
243

244
   code->pages = page;
3,597✔
245

246
   code_span_t *span = xcalloc(sizeof(code_span_t));
3,597✔
247
   span->next  = code->spans;
3,597✔
248
   span->base  = page->mem;
3,597✔
249
   span->size  = CODE_PAGE_SIZE;
3,597✔
250
   span->owner = code;
3,597✔
251

252
   code->globalfree = code->spans = span;
3,597✔
253
}
3,597✔
254

255
code_cache_t *code_cache_new(void)
3,591✔
256
{
257
   code_cache_t *code = xcalloc(sizeof(code_cache_t));
3,591✔
258

259
   {
260
      SCOPED_LOCK(code->lock);
7,182✔
261
      code_page_new(code);
3,591✔
262
   }
263

264
#ifdef HAVE_CAPSTONE
265
#if defined ARCH_X86_64
266
   if (cs_open(CS_ARCH_X86, CS_MODE_64, &(code->capstone)) != CS_ERR_OK)
267
      fatal_trace("failed to init capstone for x86_64");
268
#elif defined ARCH_ARM64
269
   if (cs_open(CS_ARCH_ARM64, CS_MODE_ARM, &(code->capstone)) != CS_ERR_OK)
270
      fatal_trace("failed to init capstone for Arm64");
271
#else
272
#error Cannot configure capstone for this architecture
273
#endif
274

275
   if (cs_option(code->capstone, CS_OPT_DETAIL, 1) != CS_ERR_OK)
276
      fatal_trace("failed to set capstone detailed mode");
277
#endif
278

279
   shash_t *s = shash_new(32);
3,591✔
280

281
   extern void __nvc_putpriv(jit_handle_t, void *);
3,591✔
282
   extern void __nvc_sched_waveform(jit_anchor_t *, jit_scalar_t *, tlab_t *);
3,591✔
283
   extern void __nvc_sched_process(jit_anchor_t *, jit_scalar_t *, tlab_t *);
3,591✔
284
   extern void __nvc_test_event(jit_anchor_t *, jit_scalar_t *, tlab_t *);
3,591✔
285
   extern void __nvc_last_event(jit_anchor_t *, jit_scalar_t *, tlab_t *);
3,591✔
286

287
   shash_put(s, "__nvc_sched_waveform", &__nvc_sched_waveform);
3,591✔
288
   shash_put(s, "__nvc_sched_process", &__nvc_sched_process);
3,591✔
289
   shash_put(s, "__nvc_test_event", &__nvc_test_event);
3,591✔
290
   shash_put(s, "__nvc_last_event", &__nvc_last_event);
3,591✔
291
   shash_put(s, "__nvc_mspace_alloc", &__nvc_mspace_alloc);
3,591✔
292
   shash_put(s, "__nvc_putpriv", &__nvc_putpriv);
3,591✔
293
   shash_put(s, "__nvc_do_exit", &__nvc_do_exit);
3,591✔
294
   shash_put(s, "memmove", &memmove);
3,591✔
295
   shash_put(s, "memcpy", &memcpy);
3,591✔
296
   shash_put(s, "memset", &memset);
3,591✔
297
   shash_put(s, "pow", &pow);
3,591✔
298

299
#if defined __APPLE__ && defined ARCH_ARM64
300
   shash_put(s, "bzero", &bzero);
301
#elif defined __APPLE__ && defined ARCH_X86_64
302
   shash_put(s, "__bzero", &bzero);
303
#elif defined __MINGW32__ && defined ARCH_X86_64
304
   extern void ___chkstk_ms(void);
305
   shash_put(s, "___chkstk_ms", &___chkstk_ms);
306
#endif
307

308
   store_release(&code->symbols, s);
3,591✔
309

310
   return code;
3,591✔
311
}
312

313
void code_cache_free(code_cache_t *code)
3,589✔
314
{
315
   for (code_page_t *it = code->pages, *tmp; it; it = tmp) {
7,184✔
316
      debug_remove_unwinder(it->mem);
3,595✔
317
      remove_fault_handler(code_fault_handler, it);
3,595✔
318

319
      nvc_munmap(it->mem, CODE_PAGE_SIZE);
3,595✔
320

321
      tmp = it->next;
3,595✔
322
      free(it);
3,595✔
323
   }
324

325
   for (code_span_t *it = code->spans, *tmp; it; it = tmp) {
16,656✔
326
      tmp = it->next;
13,067✔
327
      DEBUG_ONLY(free(it->debug.comments));
13,067✔
328
      free(it);
13,067✔
329
   }
330

331
#ifdef HAVE_CAPSTONE
332
   cs_close(&(code->capstone));
333
#endif
334

335
#ifdef DEBUG
336
   if (code->used > 0)
3,589✔
337
      debugf("JIT code footprint: %zu bytes", code->used);
1,272✔
338
#endif
339

340
   shash_free(code->symbols);
3,589✔
341
   free(code);
3,589✔
342
}
3,589✔
343

344
#ifdef HAVE_CAPSTONE
345
static int code_print_spaces(int col, int tab)
346
{
347
   for (; col < tab; col++)
348
      fputc(' ', stdout);
349
   return col;
350
}
351
#endif
352

353
#ifdef DEBUG
354
static int code_comment_compare(const void *a, const void *b)
355
{
356
   const code_comment_t *ca = a;
357
   const code_comment_t *cb = b;
358

359
   if (ca->addr < cb->addr)
360
      return -1;
361
   else if (ca->addr > cb->addr)
362
      return 1;
363
   else
364
      return 0;
365
}
366
#endif
367

368
static void code_disassemble(code_span_t *span, uintptr_t mark,
×
369
                             struct cpu_state *cpu)
370
{
371
   SCOPED_LOCK(span->owner->lock);
×
372

373
   printf("--");
×
374

375
   const int namelen = ident_len(span->name);
×
376
   for (int i = 0; i < 72 - namelen; i++)
×
377
      fputc('-', stdout);
×
378

379
   printf(" %s ----\n", istr(span->name));
×
380

381
#ifdef HAVE_CAPSTONE
382
   cs_insn *insn = cs_malloc(span->owner->capstone);
383

384
#ifdef DEBUG
385
   qsort(span->debug.comments, span->debug.count, sizeof(code_comment_t),
386
         code_comment_compare);
387
   code_comment_t *comment = span->debug.comments;
388
#endif
389

390
   const uint8_t *const eptr = span->base + span->size;
391
   for (const uint8_t *ptr = span->base; ptr < eptr; ) {
392
      uint64_t address = (uint64_t)ptr;
393

394
#ifdef DEBUG
395
      for (; comment < span->debug.comments + span->debug.count
396
              && comment->addr <= address; comment++)
397
         printf("%30s;; %s\n", "", comment->text);
398
#endif
399

400
      int zeros = 0;
401
      for (const uint8_t *zp = ptr; zp < eptr && *zp == 0; zp++, zeros++);
402

403
      if (zeros > 8 || zeros == eptr - ptr) {
404
         printf("%30s;; skipping %d zero bytes\n", "", zeros);
405
         ptr += zeros;
406
         continue;
407
      }
408

409
      size_t size = eptr - ptr;
410
      int col = 0;
411
      if (cs_disasm_iter(span->owner->capstone, &ptr, &size, &address, insn)) {
412
         char hex1[33], *p = hex1;
413
         for (size_t k = 0; k < insn->size; k++)
414
            p += checked_sprintf(p, hex1 + sizeof(hex1) - p, "%02x",
415
                                 insn->bytes[k]);
416

417
         col = printf("%-12" PRIx64 " %-16.16s %s %s", insn->address,
418
                          hex1, insn->mnemonic, insn->op_str);
419

420
#ifdef ARCH_X86_64
421
         if (strcmp(insn->mnemonic, "movabs") == 0) {
422
            const cs_x86_op *src = &(insn->detail->x86.operands[1]);
423
            if (src->type == X86_OP_IMM) {
424
               const char *sym = debug_symbol_name((void *)src->imm);
425
               if (sym != NULL) {
426
                  col = code_print_spaces(col, 60);
427
                  col += printf(" ; %s", sym);
428
               }
429
            }
430
         }
431
#endif
432

433
         if (strlen(hex1) > 16)
434
            col = printf("\n%15s -%-16s", "", hex1 + 16) - 1;
435
      }
436
      else {
437
#ifdef ARCH_ARM64
438
         col = printf("%-12" PRIx64 " %-16.08x %s 0x%08x", (uint64_t)ptr,
439
                      *(uint32_t *)ptr, ".word", *(uint32_t *)ptr);
440
         ptr += 4;
441
#else
442
         col = printf("%-12" PRIx64 " %-16.02x %s 0x%02x", (uint64_t)ptr,
443
                      *ptr, ".byte", *ptr);
444
         ptr++;
445
#endif
446
      }
447

448
      if (mark != 0 && (ptr >= eptr || address > mark)) {
449
         col = code_print_spaces(col, 66);
450
         printf("<=============\n");
451
         if (cpu != NULL) {
452
#ifdef ARCH_X86_64
453
            const char *names[] = {
454
               "RAX", "RCX", "RDX", "RBX", "RSP", "RBP", "RSI", "RDI",
455
               "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
456
            };
457
            for (int i = 0; i < ARRAY_LEN(names); i++)
458
               printf("\t%s\t%"PRIxPTR"\n", names[i], cpu->regs[i]);
459
#else
460
            for (int i = 0; i < 32; i++)
461
               printf("\tR%d\t%"PRIxPTR"\n", i, cpu->regs[i]);
462
#endif
463
         }
464
         mark = 0;
465
      }
466
      else
467
         printf("\n");
468
   }
469

470
   cs_free(insn, 1);
471
#else
472
   jit_hexdump(span->base, span->size, 16, (void *)mark, "");
×
473
#endif
474

475
   for (int i = 0; i < 80; i++)
×
476
      fputc('-', stdout);
×
477
   printf("\n");
×
478
   fflush(stdout);
×
479
}
×
480

481
static void code_write_perf_map(code_span_t *span)
×
482
{
483
   SCOPED_LOCK(span->owner->lock);
×
484

485
   if (span->owner->perfmap == NULL) {
×
486
      char *fname LOCAL = xasprintf("/tmp/perf-%d.map", getpid());
×
487
      if ((span->owner->perfmap = fopen(fname, "w")) == NULL) {
×
488
         warnf("cannot create %s: %s", fname, last_os_error());
×
489
         opt_set_int(OPT_PERF_MAP, 0);
×
490
         return;
×
491
      }
492
      else
493
         debugf("writing perf map to %s", fname);
×
494
   }
495

496
   fprintf(span->owner->perfmap, "%p 0x%zx %s\n", span->base, span->size,
×
497
           istr(span->name));
498
   fflush(span->owner->perfmap);
×
499
}
500

501
code_blob_t *code_blob_new(code_cache_t *code, ident_t name, size_t hint)
8,198✔
502
{
503
   code_span_t **freeptr = &(code->freelist[thread_id()]);
8,198✔
504

505
   code_span_t *free = relaxed_load(freeptr);
8,198✔
506
   if (free == NULL) {
8,198✔
507
      free = code_span_new(code, NULL, code->pages->mem, 0);
1,274✔
508
      relaxed_store(freeptr, free);
1,274✔
509
   }
510

511
   const size_t reqsz = hint ?: MIN_BLOB_SIZE;
8,198✔
512

513
   if (free->size < reqsz) {
8,198✔
514
      SCOPED_LOCK(code->lock);
1,342✔
515

516
#ifdef DEBUG
517
      if (free->size > 0)
1,342✔
518
         debugf("thread %d needs new code cache from global free list "
23✔
519
                "(requested %zu bytes, wasted %zu bytes)",
520
                thread_id(), reqsz, free->size);
521
#endif
522

523
      const size_t chunksz = MAX(reqsz, THREAD_CACHE_SIZE);
1,342✔
524
      const size_t alignedsz = ALIGN_UP(chunksz, CODE_BLOB_ALIGN);
1,342✔
525

526
      if (alignedsz > code->globalfree->size) {
1,342✔
527
         DEBUG_ONLY(debugf("requesting new %d byte code page", CODE_PAGE_SIZE));
6✔
528
         code_page_new(code);
6✔
529
         assert(code->globalfree->size == CODE_PAGE_SIZE);
6✔
530
      }
531

532
      const size_t take = MIN(code->globalfree->size, alignedsz);
1,342✔
533

534
      free->size = take;
1,342✔
535
      free->base = code->globalfree->base;
1,342✔
536

537
      code->globalfree->base += take;
1,342✔
538
      code->globalfree->size -= take;
1,342✔
539
   }
540

541
   assert(reqsz <= free->size);
8,198✔
542
   assert(((uintptr_t)free->base & (CODE_BLOB_ALIGN - 1)) == 0);
8,198✔
543

544
   code_span_t *span = code_span_new(code, name, free->base, free->size);
8,198✔
545

546
   free->base += span->size;
8,198✔
547
   free->size -= span->size;
8,198✔
548

549
   code_blob_t *blob = xcalloc(sizeof(code_blob_t));
8,198✔
550
   blob->span = span;
8,198✔
551
   blob->wptr = span->base;
8,198✔
552

553
   thread_wx_mode(WX_WRITE);
8,198✔
554

555
   return blob;
8,198✔
556
}
557

558
void code_blob_finalise(code_blob_t *blob, jit_entry_fn_t *entry)
8,198✔
559
{
560
   code_span_t *span = blob->span;
8,198✔
561
   span->size = blob->wptr - span->base;
8,198✔
562

563
   code_span_t *freespan = relaxed_load(&(span->owner->freelist[thread_id()]));
8,198✔
564
   assert(freespan->size == 0);
8,198✔
565

566
   ihash_free(blob->labels);
8,198✔
567
   blob->labels = NULL;
8,198✔
568

569
   if (unlikely(blob->patches != NULL))
8,198✔
570
      fatal_trace("not all labels in %s were patched", istr(span->name));
571
   else if (unlikely(blob->overflow)) {
8,198✔
572
      // Return all the memory
573
      freespan->size = freespan->base - span->base;
1✔
574
      freespan->base = span->base;
1✔
575
      free(blob);
1✔
576
      return;
1✔
577
   }
578
   else if (span->size == 0)
8,197✔
579
      fatal_trace("code span %s is empty", istr(span->name));
580

581
   uint8_t *aligned = ALIGN_UP(blob->wptr, CODE_BLOB_ALIGN);
8,197✔
582
   freespan->size = freespan->base - aligned;
8,197✔
583
   freespan->base = aligned;
8,197✔
584

585
   if (opt_get_verbose(OPT_ASM_VERBOSE, istr(span->name))) {
8,197✔
586
      color_printf("\n$bold$$blue$");
×
587
      code_disassemble(span, 0, NULL);
×
588
      color_printf("$$\n");
×
589
   }
590

591
   __builtin___clear_cache((char *)span->base, (char *)blob->wptr);
8,197✔
592

593
   thread_wx_mode(WX_EXECUTE);
8,197✔
594

595
   store_release(entry, (jit_entry_fn_t)span->entry);
8,197✔
596

597
   DEBUG_ONLY(relaxed_add(&span->owner->used, span->size));
8,197✔
598
   free(blob);
8,197✔
599

600
   if (opt_get_int(OPT_PERF_MAP))
8,197✔
601
      code_write_perf_map(span);
×
602
}
603

604
__attribute__((cold, noinline))
605
static void code_blob_overflow(code_blob_t *blob)
1✔
606
{
607
   warnf("JIT code buffer for %s too small", istr(blob->span->name));
1✔
608
   for (patch_list_t *it = blob->patches, *tmp; it; it = tmp) {
1✔
609
      tmp = it->next;
×
610
      free(it);
×
611
   }
612
   blob->patches = NULL;
1✔
613
   blob->overflow = true;
1✔
614
}
1✔
615

616
void code_blob_emit(code_blob_t *blob, const uint8_t *bytes, size_t len)
33,849✔
617
{
618
   if (unlikely(blob->overflow))
33,849✔
619
      return;
620
   else if (unlikely(blob->wptr + len > blob->span->base + blob->span->size)) {
33,849✔
621
      code_blob_overflow(blob);
1✔
622
      return;
1✔
623
   }
624

625
   memcpy(blob->wptr, bytes, len);
33,848✔
626
   blob->wptr += len;
33,848✔
627
}
628

629
void code_blob_align(code_blob_t *blob, unsigned align)
8,488✔
630
{
631
#ifdef ARCH_X86_64
632
   const uint8_t pad[] = { 0x90 };
8,488✔
633
#else
634
   const uint8_t pad[] = { 0x00 };
635
#endif
636

637
   assert(is_power_of_2(align));
8,488✔
638
   assert(align % ARRAY_LEN(pad) == 0);
639

640
   while (((uintptr_t)blob->wptr & (align - 1)) && !blob->overflow)
11,440✔
641
      code_blob_emit(blob, pad, ARRAY_LEN(pad));
2,952✔
642
}
8,488✔
643

644
void code_blob_mark(code_blob_t *blob, jit_label_t label)
71✔
645
{
646
   if (unlikely(blob->overflow))
71✔
647
      return;
648
   else if (blob->labels == NULL)
71✔
649
      blob->labels = ihash_new(256);
66✔
650

651
   ihash_put(blob->labels, label, blob->wptr);
71✔
652

653
   for (patch_list_t **p = &(blob->patches); *p; ) {
88✔
654
      if ((*p)->label == label) {
17✔
655
         patch_list_t *next = (*p)->next;
7✔
656
         (*(*p)->fn)(blob, label, (*p)->wptr, blob->wptr);
7✔
657
         free(*p);
7✔
658
         *p = next;
7✔
659
      }
660
      else
661
         p = &((*p)->next);
10✔
662
   }
663
}
664

665
void code_blob_patch(code_blob_t *blob, jit_label_t label, code_patch_fn_t fn)
8✔
666
{
667
   void *ptr = NULL;
8✔
668
   if (unlikely(blob->overflow))
8✔
669
      return;
670
   else if (blob->labels != NULL && (ptr = ihash_get(blob->labels, label)))
8✔
671
      (*fn)(blob, label, blob->wptr, ptr);
1✔
672
   else {
673
      patch_list_t *new = xmalloc(sizeof(patch_list_t));
7✔
674
      new->next  = blob->patches;
7✔
675
      new->fn    = fn;
7✔
676
      new->label = label;
7✔
677
      new->wptr  = blob->wptr;
7✔
678

679
      blob->patches = new;
7✔
680
   }
681
}
682

683
#ifdef DEBUG
684
static void code_blob_print_value(text_buf_t *tb, jit_value_t value)
392✔
685
{
686
   switch (value.kind) {
392✔
687
   case JIT_VALUE_REG:
162✔
688
      tb_printf(tb, "R%d", value.reg);
162✔
689
      break;
162✔
690
   case JIT_VALUE_INT64:
203✔
691
      if (value.int64 < 4096)
203✔
692
         tb_printf(tb, "#%"PRIi64, value.int64);
199✔
693
      else
694
         tb_printf(tb, "#0x%"PRIx64, value.int64);
4✔
695
      break;
696
   case JIT_VALUE_DOUBLE:
1✔
697
      tb_printf(tb, "%%%g", value.dval);
1✔
698
      break;
1✔
699
   case JIT_ADDR_CPOOL:
×
700
      tb_printf(tb, "[CP+%"PRIi64"]", value.int64);
×
701
      break;
×
702
   case JIT_ADDR_REG:
19✔
703
      tb_printf(tb, "[R%d", value.reg);
19✔
704
      if (value.disp != 0)
19✔
705
         tb_printf(tb, "+%d", value.disp);
1✔
706
      tb_cat(tb, "]");
19✔
707
      break;
19✔
708
   case JIT_ADDR_ABS:
×
709
      tb_printf(tb, "[#%016"PRIx64"]", value.int64);
×
710
      break;
×
711
   case JIT_ADDR_COVER:
×
712
      tb_printf(tb, "@%"PRIi64, value.int64);
×
713
      break;
×
714
   case JIT_VALUE_LABEL:
5✔
715
      tb_printf(tb, "%d", value.label);
5✔
716
      break;
5✔
717
   case JIT_VALUE_HANDLE:
2✔
718
      tb_printf(tb, "<%d>", value.handle);
2✔
719
      break;
2✔
720
   case JIT_VALUE_EXIT:
×
721
      tb_printf(tb, "%s", jit_exit_name(value.exit));
×
722
      break;
×
723
   case JIT_VALUE_LOC:
×
724
      tb_printf(tb, "<%s:%d>", loc_file_str(&value.loc), value.loc.first_line);
×
725
      break;
×
726
   case JIT_VALUE_LOCUS:
×
727
      tb_printf(tb, "%p", value.locus);
×
728
      break;
×
729
   case JIT_VALUE_VPOS:
×
730
      tb_printf(tb, "%u:%u", value.vpos.block, value.vpos.op);
×
731
      break;
×
732
   default:
×
733
      tb_cat(tb, "???");
×
734
   }
735
}
392✔
736

737
static void code_blob_add_comment(code_blob_t *blob, uintptr_t addr, char *text)
70,976✔
738
{
739
   code_debug_t *dbg = &(blob->span->debug);
70,976✔
740

741
   if (dbg->count == dbg->max) {
70,976✔
742
      dbg->max = MAX(128, dbg->max * 2);
8,019✔
743
      dbg->comments = xrealloc_array(dbg->comments, dbg->max,
8,019✔
744
                                     sizeof(code_comment_t));
745
   }
746

747
   dbg->comments[dbg->count].addr = addr;
70,976✔
748
   dbg->comments[dbg->count].text = text;
70,976✔
749
   dbg->count++;
70,976✔
750
}
70,976✔
751

752
void code_blob_print_ir(code_blob_t *blob, jit_ir_t *ir)
348✔
753
{
754
   LOCAL_TEXT_BUF tb = tb_new();
696✔
755
   tb_printf(tb, "%s%s", jit_op_name(ir->op), jit_cc_name(ir->cc));
348✔
756

757
   if (ir->size != JIT_SZ_UNSPEC)
348✔
758
      tb_printf(tb, ".%d", 1 << (3 + ir->size));
36✔
759

760
   tb_printf(tb, "%*.s", (int)MAX(0, 10 - tb_len(tb)), "");
348✔
761

762
   if (ir->result != JIT_REG_INVALID)
348✔
763
      tb_printf(tb, "R%d", ir->result);
203✔
764

765
   if (ir->arg1.kind != JIT_VALUE_INVALID) {
348✔
766
      if (ir->result != JIT_REG_INVALID)
263✔
767
         tb_cat(tb, ", ");
187✔
768
      code_blob_print_value(tb, ir->arg1);
263✔
769
   }
770

771
   if (ir->arg2.kind != JIT_VALUE_INVALID) {
348✔
772
      tb_cat(tb, ", ");
129✔
773
      code_blob_print_value(tb, ir->arg2);
129✔
774
   }
775

776
   code_blob_add_comment(blob, (uintptr_t)blob->wptr, tb_claim(tb));
348✔
777
}
348✔
778

779
void code_blob_printf(code_blob_t *blob, const char *fmt, ...)
25,681✔
780
{
781
   va_list ap;
25,681✔
782
   va_start(ap, fmt);
25,681✔
783

784
   char *text = xvasprintf(fmt, ap);
25,681✔
785
   code_blob_add_comment(blob, (uintptr_t)blob->wptr, text);
25,681✔
786

787
   va_end(ap);
25,681✔
788
}
25,681✔
789

790
__attribute__((format(printf, 3, 4)))
791
static void debug_reloc(code_blob_t *blob, void *patch, const char *fmt, ...)
44,947✔
792
{
793
   va_list ap;
44,947✔
794
   va_start(ap, fmt);
44,947✔
795

796
   char *text = xvasprintf(fmt, ap);
44,947✔
797
   code_blob_add_comment(blob, (uintptr_t)patch, text);
44,947✔
798

799
   va_end(ap);
44,947✔
800
}
44,947✔
801
#else
802
#define debug_reloc(...)
803
#endif   // DEBUG
804

805
#ifdef ARCH_ARM64
806
static void arm64_patch_page_offset21(code_blob_t *blob, uint32_t *patch,
807
                                      void *ptr)
808
{
809
   switch ((*patch >> 23) & 0x7f) {
810
   case 0b1111010:   // LDR (immediate, SIMD&FP)
811
   case 0b1110010:   // LDR (immediate)
812
      assert(*patch & (1 << 30));  // Quadword
813
      assert(((uintptr_t)ptr & 7) == 0);
814
      *patch |= (((uintptr_t)ptr & 0xfff) >> 3) << 10;
815
      break;
816
   case 0b0100010:   // ADD (immediate)
817
      *patch |= ((uintptr_t)ptr & 0xfff) << 10;
818
      break;
819
   default:
820
      blob->span->size = blob->wptr - blob->span->base;
821
      code_disassemble(blob->span, (uintptr_t)patch, NULL);
822
      fatal_trace("cannot patch instruction");
823
   }
824
}
825

826
static void arm64_patch_page_base_rel21(uint32_t *patch, void *ptr)
827
{
828
   const intptr_t dst_page = (intptr_t)ptr & ~UINT64_C(0xfff);
829
   const intptr_t src_page = (intptr_t)patch & ~UINT64_C(0xfff);
830
   const intptr_t upper21 = (dst_page - src_page) >> 12;
831
   assert((upper21 & ~UINT64_C(0x1fffff)) == 0);
832
   *(uint32_t *)patch |= (upper21 & 3) << 29;
833
   *(uint32_t *)patch |= ((upper21 >> 2) & 0x7ffff) << 5;
834
}
835
#endif
836

837
static void *code_emit_trampoline(code_blob_t *blob, void *dest)
40,807✔
838
{
839
#if defined ARCH_X86_64
840
   const uint8_t veneer[] = {
40,807✔
841
      0x48, 0xb8, __IMM64((uintptr_t)dest),  // MOVABS RAX, dest
40,807✔
842
      0xff, 0xe0                             // CALL RAX
843
   };
844
#elif defined ARCH_ARM64
845
   const uint8_t veneer[] = {
846
      0x50, 0x00, 0x00, 0x58,   // LDR X16, [PC+8]
847
      0x00, 0x02, 0x1f, 0xd6,   // BR X16
848
      __IMM64((uintptr_t)dest)
849
   };
850
#else
851
   should_not_reach_here();
852
#endif
853

854
   void *prev = memmem(blob->veneers, blob->wptr - blob->veneers,
40,807✔
855
                       veneer, ARRAY_LEN(veneer));
856
   if (prev != NULL)
40,807✔
857
      return prev;
858
   else {
859
      DEBUG_ONLY(code_blob_printf(blob, "Trampoline for %p", dest));
17,149✔
860

861
      void *addr = blob->wptr;
17,149✔
862
      code_blob_emit(blob, veneer, ARRAY_LEN(veneer));
17,149✔
863
      return addr;
17,149✔
864
   }
865
}
866

867
static void *code_emit_got(code_blob_t *blob, void *dest)
44✔
868
{
869
   const uint8_t data[] = { __IMM64((uintptr_t)dest) };
44✔
870

871
   void *prev = memmem(blob->veneers, blob->veneers - blob->wptr,
44✔
872
                       data, ARRAY_LEN(data));
873
   if (prev != NULL)
44✔
874
      return prev;
875
   else {
876
      DEBUG_ONLY(code_blob_printf(blob, "GOT entry for %p", dest));
44✔
877

878
      void *addr = blob->wptr;
44✔
879
      code_blob_emit(blob, data, ARRAY_LEN(data));
44✔
880
      return addr;
44✔
881
   }
882
}
883

884
#if defined __MINGW32__
885
static void code_load_pe(code_blob_t *blob, const void *data, size_t size)
886
{
887
   const IMAGE_FILE_HEADER *imghdr = data;
888

889
   switch (imghdr->Machine) {
890
   case IMAGE_FILE_MACHINE_AMD64:
891
   case IMAGE_FILE_MACHINE_ARM64:
892
      break;
893
   default:
894
      fatal_trace("unknown target machine %x", imghdr->Machine);
895
   }
896

897
   const IMAGE_SYMBOL *symtab = data + imghdr->PointerToSymbolTable;
898
   const char *strtab = data + imghdr->PointerToSymbolTable
899
      + imghdr->NumberOfSymbols * sizeof(IMAGE_SYMBOL);
900

901
   const IMAGE_SECTION_HEADER *sections =
902
      data + IMAGE_SIZEOF_FILE_HEADER + imghdr->SizeOfOptionalHeader;
903

904
   void **load_addr LOCAL =
905
      xmalloc_array(imghdr->NumberOfSections, sizeof(void *));
906

907
   for (int i = 0; i < imghdr->NumberOfSections; i++) {
908
      if ((sections[i].Characteristics & IMAGE_SCN_CNT_CODE)
909
          || (sections[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)) {
910
         const int align = sections[i].Characteristics & IMAGE_SCN_ALIGN_MASK;
911
         code_blob_align(blob, 1 << ((align >> 20) - 1));
912
         load_addr[i] = blob->wptr;
913
         code_blob_emit(blob, data + sections[i].PointerToRawData,
914
                        sections[i].SizeOfRawData);
915
      }
916
      else if ((sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
917
               && sections[i].Misc.VirtualSize > 0)
918
         fatal_trace("non-empty BSS not supported");
919
   }
920

921
   if (blob->overflow)
922
      return;   // Relocations might point outside of code span
923

924
   blob->veneers = blob->wptr;
925

926
   shash_t *external = load_acquire(&blob->span->owner->symbols);
927

928
   for (int i = 0; i < imghdr->NumberOfSections; i++) {
929
      const IMAGE_RELOCATION *relocs = data + sections[i].PointerToRelocations;
930
      for (int j = 0; j < sections[i].NumberOfRelocations; j++) {
931
         const char *name = NULL;
932
         char tmp[9];
933

934
         assert(relocs[j].SymbolTableIndex < imghdr->NumberOfSymbols);
935
         const IMAGE_SYMBOL *sym = symtab + relocs[j].SymbolTableIndex;
936

937
         if (sym->N.Name.Short) {
938
            memcpy(tmp, sym->N.ShortName, 8);
939
            tmp[8] = '\0';
940
            name = tmp;
941
         }
942
         else
943
            name = strtab + sym->N.Name.Long;
944

945
         void *ptr = NULL;
946
         if (sym->SectionNumber > 0) {
947
            assert(sym->SectionNumber - 1 < imghdr->NumberOfSections);
948
            ptr = load_addr[sym->SectionNumber - 1] + sym->Value;
949
         }
950
         else
951
            ptr = shash_get(external, name);
952

953
         if (ptr == NULL && icmp(blob->span->name, name))
954
            ptr = blob->span->base;
955

956
         if (ptr == NULL)
957
            fatal_trace("failed to resolve symbol %s", name);
958

959
         void *patch = load_addr[i] + relocs[j].VirtualAddress;
960
         assert((uint8_t *)patch >= blob->span->base);
961
         assert((uint8_t *)patch < blob->span->base + blob->span->size);
962

963
         switch (relocs[j].Type) {
964
#if defined ARCH_X86_64
965
         case IMAGE_REL_AMD64_ADDR64:
966
            *(uint64_t *)patch += (uint64_t)ptr;
967
            break;
968
         case IMAGE_REL_AMD64_ADDR32NB:
969
            *(uint32_t *)patch += (uint32_t)(ptr - (void *)blob->span->base);
970
            break;
971
#elif defined ARCH_ARM64
972
         case IMAGE_REL_ARM64_BRANCH26:
973
            {
974
               void *veneer = code_emit_trampoline(blob, ptr);
975
               const ptrdiff_t pcrel = (veneer - patch) >> 2;
976
               *(uint32_t *)patch &= ~0x3ffffff;
977
               *(uint32_t *)patch |= pcrel & 0x3ffffff;
978
            }
979
            break;
980
         case IMAGE_REL_ARM64_ADDR32NB:
981
            *(uint32_t *)patch += (uint32_t)(ptr - (void *)blob->span->base);
982
            break;
983
         case IMAGE_REL_ARM64_PAGEBASE_REL21:
984
            arm64_patch_page_base_rel21(patch, ptr);
985
            break;
986
         case IMAGE_REL_ARM64_PAGEOFFSET_12A:
987
         case IMAGE_REL_ARM64_PAGEOFFSET_12L:
988
            arm64_patch_page_offset21(blob, patch, ptr);
989
            break;
990
#endif
991
         default:
992
            blob->span->size = blob->wptr - blob->span->base;
993
            code_disassemble(blob->span, (uintptr_t)patch, NULL);
994
            fatal_trace("cannot handle relocation type %d for symbol %s",
995
                        relocs[j].Type, name);
996
         }
997
      }
998

999
      if (strncmp((const char *)sections[i].Name, ".pdata",
1000
                  IMAGE_SIZEOF_SHORT_NAME) == 0) {
1001
         assert(sections[i].SizeOfRawData % sizeof(RUNTIME_FUNCTION) == 0);
1002
         const int count = sections[i].SizeOfRawData / sizeof(RUNTIME_FUNCTION);
1003
         const DWORD64 base = (DWORD64)blob->span->base;
1004

1005
         // TODO: we should also call RtlDeleteFunctionTable at some point
1006
         if (!RtlAddFunctionTable(load_addr[i], count, base))
1007
            fatal_trace("RtlAddFunctionTable failed: %s", last_os_error());
1008
      }
1009
   }
1010

1011
   for (int i = 0; i < imghdr->NumberOfSymbols; i++) {
1012
      const IMAGE_SYMBOL *sym = &(symtab[i]);
1013

1014
      if (sym->SectionNumber == 0 || sym->N.Name.Short)
1015
         continue;
1016
      else if ((sym->Type >> 4) != IMAGE_SYM_DTYPE_FUNCTION)
1017
         continue;
1018
      else if (icmp(blob->span->name, strtab + sym->N.Name.Long)) {
1019
         blob->span->entry = load_addr[sym->SectionNumber - 1] + sym->Value;
1020
         break;
1021
      }
1022
   }
1023
}
1024
#elif defined __APPLE__
1025
static void code_load_macho(code_blob_t *blob, const void *data, size_t size)
1026
{
1027
   const void *rptr = data;
1028

1029
   const struct mach_header_64 *fhdr = rptr;
1030
   rptr += sizeof(struct mach_header_64);
1031

1032
   if (fhdr->magic != MH_MAGIC_64)
1033
      fatal_trace("bad Mach-O magic %x", fhdr->magic);
1034

1035
   const struct segment_command_64 *seg = NULL;
1036
   const struct symtab_command *symtab = NULL;
1037

1038
   void **load_addr LOCAL = NULL;
1039

1040
   for (int i = 0; i < fhdr->ncmds; i++) {
1041
      const struct load_command *load = rptr;
1042
      switch (load->cmd) {
1043
      case LC_SEGMENT_64:
1044
         {
1045
            seg = rptr;
1046
            load_addr = xmalloc_array(seg->nsects, sizeof(void *));
1047

1048
            for (int j = 0; j < seg->nsects; j++) {
1049
               const struct section_64 *sec =
1050
                  (void *)seg + sizeof(struct segment_command_64)
1051
                  + j * sizeof(struct section_64);
1052
               code_blob_align(blob, 1 << sec->align);
1053
               load_addr[j] = blob->wptr;
1054
               DEBUG_ONLY(code_blob_printf(blob, "%s", sec->sectname));
1055
               code_blob_emit(blob, data + sec->offset, sec->size);
1056
            }
1057
         }
1058
         break;
1059
      case LC_SYMTAB:
1060
         symtab = rptr;
1061
         assert(symtab->cmdsize == sizeof(struct symtab_command));
1062
         break;
1063
      case LC_DATA_IN_CODE:
1064
      case LC_LINKER_OPTIMIZATION_HINT:
1065
      case LC_BUILD_VERSION:
1066
      case LC_DYSYMTAB:
1067
         break;
1068
      default:
1069
         warnf("unrecognised load command 0x%0x", load->cmd);
1070
      }
1071

1072
      rptr += load->cmdsize;
1073
   }
1074
   assert(rptr == data + sizeof(struct mach_header_64) + fhdr->sizeofcmds);
1075

1076
   if (blob->overflow)
1077
      return;   // Relocations might point outside of code span
1078

1079
   blob->veneers = blob->wptr;
1080

1081
   assert(seg != NULL);
1082
   assert(symtab != NULL);
1083

1084
   shash_t *external = load_acquire(&blob->span->owner->symbols);
1085

1086
   for (int i = 0; i < seg->nsects; i++) {
1087
      const struct section_64 *sec =
1088
         (void *)seg + sizeof(struct segment_command_64)
1089
         + i * sizeof(struct section_64);
1090

1091
      uint32_t addend = 0;
1092
      for (int j = 0; j < sec->nreloc; j++) {
1093
         const struct relocation_info *rel =
1094
            data + sec->reloff + j * sizeof(struct relocation_info);
1095
         const char *name = NULL;
1096
         void *ptr = NULL;
1097
         if (rel->r_extern) {
1098
            assert(rel->r_symbolnum < symtab->nsyms);
1099
            const struct nlist_64 *nl = data + symtab->symoff
1100
               + rel->r_symbolnum * sizeof(struct nlist_64);
1101
            name = data + symtab->stroff + nl->n_un.n_strx;
1102

1103
            if (nl->n_type & N_EXT) {
1104
               if (icmp(blob->span->name, name + 1))
1105
                  ptr = blob->span->base;
1106
               else if ((ptr = shash_get(external, name + 1)) == NULL)
1107
                  fatal_trace("failed to resolve symbol %s", name + 1);
1108
            }
1109
            else if (nl->n_sect != NO_SECT)
1110
               ptr = blob->span->base + nl->n_value;
1111
         }
1112
         else
1113
            ptr = blob->span->base;
1114

1115
         ptr += addend;
1116
         addend = 0;
1117

1118
         void *patch = load_addr[i] + rel->r_address;
1119
         assert((uint8_t *)patch >= blob->span->base);
1120
         assert((uint8_t *)patch < blob->span->base + blob->span->size);
1121

1122
         switch (rel->r_type) {
1123
#ifdef ARCH_ARM64
1124
         case ARM64_RELOC_UNSIGNED:
1125
            assert(rel->r_length == 3);
1126
            *(void **)patch = ptr;
1127
            break;
1128
         case ARM64_RELOC_SUBTRACTOR:
1129
            break;   // What is this?
1130
         case ARM64_RELOC_GOT_LOAD_PAGEOFF12:
1131
         case ARM64_RELOC_PAGEOFF12:
1132
            arm64_patch_page_offset21(blob, patch, ptr);
1133
            break;
1134
         case ARM64_RELOC_GOT_LOAD_PAGE21:
1135
         case ARM64_RELOC_PAGE21:
1136
            arm64_patch_page_base_rel21(patch, ptr);
1137
            break;
1138
         case ARM64_RELOC_BRANCH26:
1139
            {
1140
               void *veneer = code_emit_trampoline(blob, ptr);
1141
               const ptrdiff_t pcrel = (veneer - patch) >> 2;
1142
               debug_reloc(blob, patch, "ARM64_RELOC_BRANCH26 %s PC%+"PRIiPTR,
1143
                           name, pcrel);
1144
               *(uint32_t *)patch &= ~0x3ffffff;
1145
               *(uint32_t *)patch |= pcrel & 0x3ffffff;
1146
            }
1147
            break;
1148
         case ARM64_RELOC_ADDEND:
1149
            addend = rel->r_symbolnum;
1150
            break;
1151
#elif defined ARCH_X86_64
1152
         case X86_64_RELOC_UNSIGNED:
1153
            *(uint64_t *)patch += (uint64_t)ptr;
1154
            break;
1155
         case X86_64_RELOC_BRANCH:
1156
            *(uint32_t *)patch += (uint32_t)(ptr - patch - 4);
1157
            break;
1158
#endif
1159
         default:
1160
            blob->span->size = blob->wptr - blob->span->base;
1161
            code_disassemble(blob->span, (uintptr_t)patch, NULL);
1162
            fatal_trace("cannot handle relocation type %d for symbol %s",
1163
                        rel->r_type, name);
1164
         }
1165
      }
1166
   }
1167

1168
   for (int i = 0; i < symtab->nsyms; i++) {
1169
      const struct nlist_64 *sym =
1170
         data + symtab->symoff + i * sizeof(struct nlist_64);
1171

1172
      if (sym->n_sect == NO_SECT || (sym->n_type & N_TYPE) != N_SECT)
1173
         continue;
1174

1175
      const char *name = data + symtab->stroff + sym->n_un.n_strx;
1176
      if (name[0] == '_' && icmp(blob->span->name, name + 1)) {
1177
         blob->span->entry = load_addr[sym->n_sect - 1] + sym->n_value;
1178
         break;
1179
      }
1180
   }
1181
}
1182
#elif !defined __MINGW32__
1183
static void code_load_elf(code_blob_t *blob, const void *data, size_t size)
7,943✔
1184
{
1185
   const Elf64_Ehdr *ehdr = data;
7,943✔
1186

1187
   if (ehdr->e_ident[EI_MAG0] != ELFMAG0
7,943✔
1188
       || ehdr->e_ident[EI_MAG1] != ELFMAG1
1189
       || ehdr->e_ident[EI_MAG2] != ELFMAG2
1190
       || ehdr->e_ident[EI_MAG3] != ELFMAG3)
7,943✔
1191
      fatal_trace("bad ELF magic");
1192
   else if (ehdr->e_shentsize != sizeof(Elf64_Shdr))
7,943✔
1193
      fatal_trace("bad section header size %d != %zu", ehdr->e_shentsize,
1194
                  sizeof(Elf64_Shdr));
1195

1196
   const Elf64_Shdr *strtab_hdr =
7,943✔
1197
      data + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize;
7,943✔
1198
   const char *strtab = data + strtab_hdr->sh_offset;
7,943✔
1199

1200
   void **load_addr LOCAL = xcalloc_array(ehdr->e_shnum, sizeof(void *));
15,886✔
1201

1202
   for (int i = 0; i < ehdr->e_shnum; i++) {
71,944✔
1203
      const Elf64_Shdr *shdr = data + ehdr->e_shoff + i * ehdr->e_shentsize;
64,001✔
1204

1205
      switch (shdr->sh_type) {
64,001✔
1206
      case SHT_PROGBITS:
16,431✔
1207
         if (shdr->sh_flags & SHF_ALLOC) {
16,431✔
1208
            code_blob_align(blob, shdr->sh_addralign);
8,488✔
1209
            load_addr[i] = blob->wptr;
8,488✔
1210
            DEBUG_ONLY(code_blob_printf(blob, "%s", strtab + shdr->sh_name));
8,488✔
1211
            code_blob_emit(blob, data + shdr->sh_offset, shdr->sh_size);
8,488✔
1212
         }
1213
         break;
1214

1215
      case SHT_RELA:
1216
         // Handled in second pass
1217
         break;
1218

1219
      case SHT_NULL:
1220
      case SHT_STRTAB:
1221
      case SHT_X86_64_UNWIND:
1222
         break;
1223

1224
      case SHT_SYMTAB:
1225
         for (int i = 0; i < shdr->sh_size / shdr->sh_entsize; i++) {
32,317✔
1226
            const Elf64_Sym *sym =
32,317✔
1227
               data + shdr->sh_offset + i * shdr->sh_entsize;
32,317✔
1228

1229
            if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
32,317✔
1230
               continue;
24,374✔
1231
            else if (!icmp(blob->span->name, strtab + sym->st_name))
7,943✔
1232
               continue;
×
1233
            else if (load_addr[sym->st_shndx] == NULL)
7,943✔
1234
               fatal_trace("missing section %d for symbol %s", sym->st_shndx,
1235
                           strtab + sym->st_name);
×
1236
            else {
1237
               blob->span->entry = load_addr[sym->st_shndx] + sym->st_value;
7,943✔
1238
               break;
7,943✔
1239
            }
1240
         }
1241
         break;
1242

1243
      default:
×
1244
         warnf("ignoring ELF section %s with type %x", strtab + shdr->sh_name,
×
1245
               shdr->sh_type);
1246
      }
1247
   }
1248

1249
   if (blob->overflow)
7,943✔
1250
      return;   // Relocations might point outside of code span
×
1251

1252
   blob->veneers = blob->wptr;
7,943✔
1253

1254
   shash_t *external = load_acquire(&blob->span->owner->symbols);
7,943✔
1255

1256
   for (int i = 0; i < ehdr->e_shnum; i++) {
71,944✔
1257
      const Elf64_Shdr *shdr = data + ehdr->e_shoff + i * ehdr->e_shentsize;
64,001✔
1258
      if (shdr->sh_type != SHT_RELA)
64,001✔
1259
         continue;
48,203✔
1260

1261
      const Elf64_Shdr *mod =
15,798✔
1262
         data + ehdr->e_shoff + shdr->sh_info * ehdr->e_shentsize;
15,798✔
1263
      if (mod->sh_type != SHT_PROGBITS || !(mod->sh_flags & SHF_ALLOC))
15,798✔
1264
         continue;
7,943✔
1265
      else if (load_addr[shdr->sh_info] == NULL)
7,855✔
1266
         fatal_trace("section %s not loaded", strtab + mod->sh_name);
1267

1268
      const Elf64_Shdr *symtab =
7,855✔
1269
         data + ehdr->e_shoff + shdr->sh_link * ehdr->e_shentsize;
7,855✔
1270
      if (symtab->sh_type != SHT_SYMTAB)
7,855✔
1271
         fatal_trace("section %s is not a symbol table",
1272
                     strtab + symtab->sh_name);
×
1273

1274
      const Elf64_Rela *endp = data + shdr->sh_offset + shdr->sh_size;
7,855✔
1275
      for (const Elf64_Rela *r = data + shdr->sh_offset; r < endp; r++) {
52,802✔
1276
         const Elf64_Sym *sym = data + symtab->sh_offset
44,947✔
1277
            + ELF64_R_SYM(r->r_info) * symtab->sh_entsize;
44,947✔
1278

1279
         void *ptr = NULL;
44,947✔
1280
         switch (ELF64_ST_TYPE(sym->st_info)) {
44,947✔
1281
         case STT_NOTYPE:
40,851✔
1282
         case STT_FUNC:
1283
            if (sym->st_shndx == 0)
40,851✔
1284
               ptr = shash_get(external, strtab + sym->st_name);
40,807✔
1285
            else
1286
               ptr = load_addr[sym->st_shndx] + sym->st_value;
44✔
1287
            break;
1288
         case STT_SECTION:
4,096✔
1289
            ptr = load_addr[sym->st_shndx];
4,096✔
1290
            break;
4,096✔
1291
         default:
×
1292
            fatal_trace("cannot handle ELF symbol type %d",
1293
                        ELF64_ST_TYPE(sym->st_info));
1294
         }
1295

1296
         if (ptr == NULL)
44,947✔
1297
            fatal_trace("cannot resolve symbol %s type %d",
1298
                        strtab + sym->st_name, ELF64_ST_TYPE(sym->st_info));
×
1299

1300
         void *patch = load_addr[shdr->sh_info] + r->r_offset;
44,947✔
1301
         assert(r->r_offset < mod->sh_size);
44,947✔
1302

1303
         switch (ELF64_R_TYPE(r->r_info)) {
44,947✔
1304
         case R_X86_64_64:
4,096✔
1305
            debug_reloc(blob, patch, "R_X86_64_64 %s", strtab + sym->st_name);
4,096✔
1306
            *(uint64_t *)patch = (uint64_t)ptr + r->r_addend;
4,096✔
1307
            break;
4,096✔
1308
         case R_X86_64_PC32:
×
1309
            {
1310
               const ptrdiff_t pcrel = ptr + r->r_addend - patch;
×
1311
               debug_reloc(blob, patch, "R_X86_64_PC32 %s PC%+"PRIiPTR,
×
1312
                           strtab + sym->st_name, pcrel);
×
1313
               assert(pcrel >= INT32_MIN && pcrel <= INT32_MAX);
×
1314
               *(uint32_t *)patch = pcrel;
×
1315
            }
1316
            break;
×
1317
         case R_X86_64_GOTPCREL:
44✔
1318
            {
1319
               void *got = code_emit_got(blob, ptr);
44✔
1320
               const ptrdiff_t pcrel = got + r->r_addend - patch;
44✔
1321
               debug_reloc(blob, patch, "R_X86_64_GOTPCREL %s PC%+"PRIiPTR,
44✔
1322
                           strtab + sym->st_name, pcrel);
44✔
1323
               assert(pcrel >= INT32_MIN && pcrel <= INT32_MAX);
44✔
1324
               *(uint32_t *)patch = pcrel;
44✔
1325
            }
1326
            break;
44✔
1327
         case R_X86_64_PLT32:
40,807✔
1328
            {
1329
               void *veneer = code_emit_trampoline(blob, ptr);
40,807✔
1330
               const ptrdiff_t pcrel = veneer + r->r_addend - patch;
40,807✔
1331
               debug_reloc(blob, patch, "R_X86_64_PLT32 %s PC%+"PRIiPTR,
40,807✔
1332
                           strtab + sym->st_name, pcrel);
40,807✔
1333
               assert(pcrel >= INT32_MIN && pcrel <= INT32_MAX);
40,807✔
1334
               *(uint32_t *)patch = pcrel;
40,807✔
1335
            }
1336
            break;
40,807✔
1337
         case R_AARCH64_CALL26:
×
1338
            {
1339
               void *veneer = code_emit_trampoline(blob, ptr);
×
1340
               const ptrdiff_t pcrel = (veneer + r->r_addend - patch) >> 2;
×
1341
               *(uint32_t *)patch &= ~0x3ffffff;
×
1342
               *(uint32_t *)patch |= pcrel & 0x3ffffff;
×
1343
            }
1344
            break;
×
1345
         case R_AARCH64_PREL64:
×
1346
            *(uint64_t *)patch = ptr + r->r_addend - patch;
×
1347
            break;
×
1348
         case R_AARCH64_MOVW_UABS_G0_NC:
×
1349
            *(uint32_t *)patch |=
×
1350
               (((uintptr_t)ptr + r->r_addend) & 0xffff) << 5;
×
1351
            break;
×
1352
         case R_AARCH64_MOVW_UABS_G1_NC:
×
1353
            *(uint32_t *)patch |=
×
1354
               ((((uintptr_t)ptr + r->r_addend) >> 16) & 0xffff) << 5;
×
1355
            break;
×
1356
         case R_AARCH64_MOVW_UABS_G2_NC:
×
1357
            *(uint32_t *)patch |=
×
1358
               ((((uintptr_t)ptr + r->r_addend) >> 32) & 0xffff) << 5;
×
1359
            break;
×
1360
         case R_AARCH64_MOVW_UABS_G3:
×
1361
            *(uint32_t *)patch |=
×
1362
               ((((uintptr_t)ptr + r->r_addend) >> 48) & 0xffff) << 5;
×
1363
            break;
×
1364
         default:
×
1365
            blob->span->size = blob->wptr - blob->span->base;
×
1366
            code_disassemble(blob->span, (uintptr_t)patch, NULL);
×
1367
            fatal_trace("cannot handle relocation type %ld for symbol %s",
1368
                        ELF64_R_TYPE(r->r_info), strtab + sym->st_name);
×
1369
         }
1370
      }
1371
   }
1372
}
1373
#endif
1374

1375
void code_load_object(code_blob_t *blob, const void *data, size_t size)
7,943✔
1376
{
1377
#if defined __APPLE__
1378
   code_load_macho(blob, data, size);
1379
#elif defined __MINGW32__
1380
   code_load_pe(blob, data, size);
1381
#else
1382
   code_load_elf(blob, data, size);
7,943✔
1383
#endif
1384
}
7,943✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc