aboutsummaryrefslogtreecommitdiff
path: root/src/link/MachO/InternalObject.zig
blob: 9578bc84ec9f625e7d83ba223caa9e93e7c2cf7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
index: File.Index,

sections: std.MultiArrayList(Section) = .{},
atoms: std.ArrayListUnmanaged(Atom) = .{},
atoms_indexes: std.ArrayListUnmanaged(Atom.Index) = .{},
atoms_extra: std.ArrayListUnmanaged(u32) = .{},
symtab: std.ArrayListUnmanaged(macho.nlist_64) = .{},
strtab: std.ArrayListUnmanaged(u8) = .{},
symbols: std.ArrayListUnmanaged(Symbol) = .{},
symbols_extra: std.ArrayListUnmanaged(u32) = .{},
globals: std.ArrayListUnmanaged(MachO.SymbolResolver.Index) = .{},

objc_methnames: std.ArrayListUnmanaged(u8) = .{},
objc_selrefs: [@sizeOf(u64)]u8 = [_]u8{0} ** @sizeOf(u64),

force_undefined: std.ArrayListUnmanaged(Symbol.Index) = .{},
entry_index: ?Symbol.Index = null,
dyld_stub_binder_index: ?Symbol.Index = null,
dyld_private_index: ?Symbol.Index = null,
objc_msg_send_index: ?Symbol.Index = null,
mh_execute_header_index: ?Symbol.Index = null,
mh_dylib_header_index: ?Symbol.Index = null,
dso_handle_index: ?Symbol.Index = null,
boundary_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},

output_symtab_ctx: MachO.SymtabCtx = .{},

pub fn deinit(self: *InternalObject, allocator: Allocator) void {
    for (self.sections.items(.relocs)) |*relocs| {
        relocs.deinit(allocator);
    }
    self.sections.deinit(allocator);
    self.atoms.deinit(allocator);
    self.atoms_indexes.deinit(allocator);
    self.atoms_extra.deinit(allocator);
    self.symtab.deinit(allocator);
    self.strtab.deinit(allocator);
    self.symbols.deinit(allocator);
    self.symbols_extra.deinit(allocator);
    self.globals.deinit(allocator);
    self.objc_methnames.deinit(allocator);
    self.force_undefined.deinit(allocator);
    self.boundary_symbols.deinit(allocator);
}

pub fn init(self: *InternalObject, allocator: Allocator) !void {
    // Atom at index 0 is reserved as null atom.
    try self.atoms.append(allocator, .{});
    try self.atoms_extra.append(allocator, 0);
    // Null byte in strtab
    try self.strtab.append(allocator, 0);
}

pub fn initSymbols(self: *InternalObject, macho_file: *MachO) !void {
    const createSymbol = struct {
        fn createSymbol(obj: *InternalObject, name: u32, args: struct {
            type: u8 = macho.N_UNDF | macho.N_EXT,
            desc: u16 = 0,
        }) Symbol.Index {
            const index = obj.addSymbolAssumeCapacity();
            const symbol = &obj.symbols.items[index];
            symbol.name = name;
            symbol.extra = obj.addSymbolExtraAssumeCapacity(.{});
            symbol.flags.dyn_ref = args.desc & macho.REFERENCED_DYNAMICALLY != 0;
            symbol.visibility = if (args.type & macho.N_EXT != 0) blk: {
                break :blk if (args.type & macho.N_PEXT != 0) .hidden else .global;
            } else .local;

            const nlist_idx: u32 = @intCast(obj.symtab.items.len);
            const nlist = obj.symtab.addOneAssumeCapacity();
            nlist.* = .{
                .n_strx = name,
                .n_type = args.type,
                .n_sect = 0,
                .n_desc = args.desc,
                .n_value = 0,
            };
            symbol.nlist_idx = nlist_idx;
            return index;
        }
    }.createSymbol;

    const gpa = macho_file.base.comp.gpa;
    var nsyms = macho_file.base.comp.force_undefined_symbols.keys().len;
    nsyms += 1; // dyld_stub_binder
    nsyms += 1; // _objc_msgSend
    if (!macho_file.base.isDynLib()) {
        nsyms += 1; // entry
        nsyms += 1; // __mh_execute_header
    } else {
        nsyms += 1; // __mh_dylib_header
    }
    nsyms += 1; // ___dso_handle
    nsyms += 1; // dyld_private

    try self.symbols.ensureTotalCapacityPrecise(gpa, nsyms);
    try self.symbols_extra.ensureTotalCapacityPrecise(gpa, nsyms * @sizeOf(Symbol.Extra));
    try self.symtab.ensureTotalCapacityPrecise(gpa, nsyms);
    try self.globals.ensureTotalCapacityPrecise(gpa, nsyms);
    self.globals.resize(gpa, nsyms) catch unreachable;
    @memset(self.globals.items, 0);

    try self.force_undefined.ensureTotalCapacityPrecise(gpa, macho_file.base.comp.force_undefined_symbols.keys().len);
    for (macho_file.base.comp.force_undefined_symbols.keys()) |name| {
        self.force_undefined.addOneAssumeCapacity().* = createSymbol(self, try self.addString(gpa, name), .{});
    }

    self.dyld_stub_binder_index = createSymbol(self, try self.addString(gpa, "dyld_stub_binder"), .{});
    self.objc_msg_send_index = createSymbol(self, try self.addString(gpa, "_objc_msgSend"), .{});

    if (!macho_file.base.isDynLib()) {
        self.entry_index = createSymbol(self, try self.addString(gpa, macho_file.entry_name orelse "_main"), .{});
        self.mh_execute_header_index = createSymbol(self, try self.addString(gpa, "__mh_execute_header"), .{
            .type = macho.N_SECT | macho.N_EXT,
            .desc = macho.REFERENCED_DYNAMICALLY,
        });
    } else {
        self.mh_dylib_header_index = createSymbol(self, try self.addString(gpa, "__mh_dylib_header"), .{
            .type = macho.N_SECT | macho.N_EXT,
        });
    }

    self.dso_handle_index = createSymbol(self, try self.addString(gpa, "___dso_handle"), .{
        .type = macho.N_SECT | macho.N_EXT,
    });
    self.dyld_private_index = createSymbol(self, try self.addString(gpa, "dyld_private"), .{
        .type = macho.N_SECT,
    });
}

pub fn resolveSymbols(self: *InternalObject, macho_file: *MachO) !void {
    const tracy = trace(@src());
    defer tracy.end();

    const gpa = macho_file.base.comp.gpa;

    for (self.symtab.items, self.globals.items, 0..) |nlist, *global, i| {
        const gop = try macho_file.resolver.getOrPut(gpa, .{
            .index = @intCast(i),
            .file = self.index,
        }, macho_file);
        if (!gop.found_existing) {
            gop.ref.* = .{ .index = 0, .file = 0 };
        }
        global.* = gop.index;

        if (nlist.undf()) continue;
        if (gop.ref.getFile(macho_file) == null) {
            gop.ref.* = .{ .index = @intCast(i), .file = self.index };
            continue;
        }

        if (self.asFile().getSymbolRank(.{
            .archive = false,
            .weak = false,
            .tentative = false,
        }) < gop.ref.getSymbol(macho_file).?.getSymbolRank(macho_file)) {
            gop.ref.* = .{ .index = @intCast(i), .file = self.index };
        }
    }
}

pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void {
    const tracy = trace(@src());
    defer tracy.end();

    const gpa = macho_file.base.comp.gpa;
    var boundary_symbols = std.StringArrayHashMap(MachO.Ref).init(gpa);
    defer boundary_symbols.deinit();

    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;
        for (object.symbols.items, 0..) |sym, i| {
            const nlist = object.symtab.items(.nlist)[i];
            if (!nlist.undf() or !nlist.ext()) continue;
            const ref = object.getSymbolRef(@intCast(i), macho_file);
            if (ref.getFile(macho_file) != null) continue;
            const name = sym.getName(macho_file);
            if (mem.startsWith(u8, name, "segment$start$") or
                mem.startsWith(u8, name, "segment$stop$") or
                mem.startsWith(u8, name, "section$start$") or
                mem.startsWith(u8, name, "section$stop$"))
            {
                const gop = try boundary_symbols.getOrPut(name);
                if (!gop.found_existing) {
                    gop.value_ptr.* = .{ .index = @intCast(i), .file = index };
                }
            }
        }
    }

    const nsyms = boundary_symbols.values().len;
    try self.boundary_symbols.ensureTotalCapacityPrecise(gpa, nsyms);
    try self.symbols.ensureUnusedCapacity(gpa, nsyms);
    try self.symtab.ensureUnusedCapacity(gpa, nsyms);
    try self.symbols_extra.ensureUnusedCapacity(gpa, nsyms * @sizeOf(Symbol.Extra));
    try self.globals.ensureUnusedCapacity(gpa, nsyms);

    for (boundary_symbols.keys(), boundary_symbols.values()) |name, ref| {
        const name_off = try self.addString(gpa, name);
        const sym_index = self.addSymbolAssumeCapacity();
        self.boundary_symbols.appendAssumeCapacity(sym_index);
        const sym = &self.symbols.items[sym_index];
        sym.name = name_off;
        sym.visibility = .local;
        const nlist_idx: u32 = @intCast(self.symtab.items.len);
        const nlist = self.symtab.addOneAssumeCapacity();
        nlist.* = .{
            .n_strx = name_off,
            .n_type = macho.N_SECT,
            .n_sect = 0,
            .n_desc = 0,
            .n_value = 0,
        };
        sym.nlist_idx = nlist_idx;
        sym.extra = self.addSymbolExtraAssumeCapacity(.{});

        const idx = ref.getFile(macho_file).?.object.globals.items[ref.index];
        self.globals.addOneAssumeCapacity().* = idx;
        macho_file.resolver.values.items[idx - 1] = .{ .index = sym_index, .file = self.index };
    }
}

pub fn markLive(self: *InternalObject, macho_file: *MachO) void {
    const tracy = trace(@src());
    defer tracy.end();

    for (0..self.symbols.items.len) |i| {
        const nlist = self.symtab.items[i];
        if (!nlist.ext()) continue;

        const ref = self.getSymbolRef(@intCast(i), macho_file);
        const file = ref.getFile(macho_file) orelse continue;
        if (file == .object and !file.object.alive) {
            file.object.alive = true;
            file.object.markLive(macho_file);
        }
    }
}

/// Creates a fake input sections __TEXT,__objc_methname and __DATA,__objc_selrefs.
pub fn addObjcMsgsendSections(self: *InternalObject, sym_name: []const u8, macho_file: *MachO) !Symbol.Index {
    const methname_sym_index = try self.addObjcMethnameSection(sym_name, macho_file);
    return try self.addObjcSelrefsSection(methname_sym_index, macho_file);
}

fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_file: *MachO) !Symbol.Index {
    const gpa = macho_file.base.comp.gpa;
    const atom_index = try self.addAtom(gpa);
    try self.atoms_indexes.append(gpa, atom_index);
    const atom = self.getAtom(atom_index).?;
    atom.size = methname.len + 1;
    atom.alignment = .@"1";

    const n_sect = try self.addSection(gpa, "__TEXT", "__objc_methname");
    const sect = &self.sections.items(.header)[n_sect];
    sect.flags = macho.S_CSTRING_LITERALS;
    sect.size = atom.size;
    sect.@"align" = 0;
    atom.n_sect = n_sect;
    self.sections.items(.extra)[n_sect].is_objc_methname = true;

    sect.offset = @intCast(self.objc_methnames.items.len);
    try self.objc_methnames.ensureUnusedCapacity(gpa, methname.len + 1);
    self.objc_methnames.writer(gpa).print("{s}\x00", .{methname}) catch unreachable;

    const name_str = try self.addString(gpa, "ltmp");
    const sym_index = try self.addSymbol(gpa);
    const sym = &self.symbols.items[sym_index];
    sym.name = name_str;
    sym.atom_ref = .{ .index = atom_index, .file = self.index };
    sym.extra = try self.addSymbolExtra(gpa, .{});
    const nlist_idx: u32 = @intCast(self.symtab.items.len);
    const nlist = try self.symtab.addOne(gpa);
    nlist.* = .{
        .n_strx = name_str,
        .n_type = macho.N_SECT,
        .n_sect = @intCast(n_sect + 1),
        .n_desc = 0,
        .n_value = 0,
    };
    sym.nlist_idx = nlist_idx;
    try self.globals.append(gpa, 0);

    return atom_index;
}

fn addObjcSelrefsSection(self: *InternalObject, methname_sym_index: Symbol.Index, macho_file: *MachO) !Symbol.Index {
    const gpa = macho_file.base.comp.gpa;
    const atom_index = try self.addAtom(gpa);
    try self.atoms_indexes.append(gpa, atom_index);
    const atom = self.getAtom(atom_index).?;
    atom.size = @sizeOf(u64);
    atom.alignment = .@"8";

    const n_sect = try self.addSection(gpa, "__DATA", "__objc_selrefs");
    const sect = &self.sections.items(.header)[n_sect];
    sect.flags = macho.S_LITERAL_POINTERS | macho.S_ATTR_NO_DEAD_STRIP;
    sect.offset = 0;
    sect.size = atom.size;
    sect.@"align" = 3;
    atom.n_sect = n_sect;
    self.sections.items(.extra)[n_sect].is_objc_selref = true;

    const relocs = &self.sections.items(.relocs)[n_sect];
    try relocs.ensureUnusedCapacity(gpa, 1);
    relocs.appendAssumeCapacity(.{
        .tag = .@"extern",
        .offset = 0,
        .target = methname_sym_index,
        .addend = 0,
        .type = .unsigned,
        .meta = .{
            .pcrel = false,
            .length = 3,
            .symbolnum = 0, // Only used when synthesising unwind records so can be anything
            .has_subtractor = false,
        },
    });
    atom.addExtra(.{ .rel_index = 0, .rel_count = 1 }, macho_file);

    const sym_index = try self.addSymbol(gpa);
    const sym = &self.symbols.items[sym_index];
    sym.atom_ref = .{ .index = atom_index, .file = self.index };
    sym.extra = try self.addSymbolExtra(gpa, .{});
    const nlist_idx: u32 = @intCast(self.symtab.items.len);
    const nlist = try self.symtab.addOne(gpa);
    nlist.* = .{
        .n_strx = 0,
        .n_type = macho.N_SECT,
        .n_sect = @intCast(n_sect + 1),
        .n_desc = 0,
        .n_value = 0,
    };
    sym.nlist_idx = nlist_idx;
    try self.globals.append(gpa, 0);
    atom.addExtra(.{ .literal_symbol_index = sym_index }, macho_file);

    return sym_index;
}

pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !void {
    const tracy = trace(@src());
    defer tracy.end();

    const gpa = macho_file.base.comp.gpa;

    var objc_msgsend_syms = std.StringArrayHashMap(MachO.Ref).init(gpa);
    defer objc_msgsend_syms.deinit();

    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;

        for (object.symbols.items, 0..) |sym, i| {
            const nlist = object.symtab.items(.nlist)[i];
            if (!nlist.ext()) continue;
            if (!nlist.undf()) continue;

            const ref = object.getSymbolRef(@intCast(i), macho_file);
            if (ref.getFile(macho_file) != null) continue;

            const name = sym.getName(macho_file);
            if (mem.startsWith(u8, name, "_objc_msgSend$")) {
                const gop = try objc_msgsend_syms.getOrPut(name);
                if (!gop.found_existing) {
                    gop.value_ptr.* = .{ .index = @intCast(i), .file = index };
                }
            }
        }
    }

    for (objc_msgsend_syms.keys(), objc_msgsend_syms.values()) |sym_name, ref| {
        const name = MachO.eatPrefix(sym_name, "_objc_msgSend$").?;
        const selrefs_index = try self.addObjcMsgsendSections(name, macho_file);

        const name_off = try self.addString(gpa, sym_name);
        const sym_index = try self.addSymbol(gpa);
        const sym = &self.symbols.items[sym_index];
        sym.name = name_off;
        sym.visibility = .hidden;
        const nlist_idx: u32 = @intCast(self.symtab.items.len);
        const nlist = try self.symtab.addOne(gpa);
        nlist.* = .{
            .n_strx = name_off,
            .n_type = macho.N_SECT | macho.N_EXT | macho.N_PEXT,
            .n_sect = 0,
            .n_desc = 0,
            .n_value = 0,
        };
        sym.nlist_idx = nlist_idx;
        sym.extra = try self.addSymbolExtra(gpa, .{ .objc_selrefs = selrefs_index });
        sym.flags.objc_stubs = true;

        const idx = ref.getFile(macho_file).?.object.globals.items[ref.index];
        try self.globals.append(gpa, idx);
        macho_file.resolver.values.items[idx - 1] = .{ .index = sym_index, .file = self.index };
    }
}

pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file: *MachO) !void {
    const tracy = trace(@src());
    defer tracy.end();

    const gpa = macho_file.base.comp.gpa;

    var buffer = std.ArrayList(u8).init(gpa);
    defer buffer.deinit();

    const slice = self.sections.slice();
    for (slice.items(.header), self.getAtoms()) |header, atom_index| {
        if (!Object.isPtrLiteral(header)) continue;
        const atom = self.getAtom(atom_index).?;
        const relocs = atom.getRelocs(macho_file);
        assert(relocs.len == 1);
        const rel = relocs[0];
        assert(rel.tag == .@"extern");
        const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?;
        try buffer.ensureUnusedCapacity(target.size);
        buffer.resize(target.size) catch unreachable;
        @memcpy(buffer.items, try self.getSectionData(target.n_sect));
        const res = try lp.insert(gpa, header.type(), buffer.items);
        buffer.clearRetainingCapacity();
        if (!res.found_existing) {
            res.ref.* = .{ .index = atom.getExtra(macho_file).literal_symbol_index, .file = self.index };
        } else {
            const lp_sym = lp.getSymbol(res.index, macho_file);
            const lp_atom = lp_sym.getAtom(macho_file).?;
            lp_atom.alignment = lp_atom.alignment.max(atom.alignment);
            atom.flags.alive = false;
        }
        atom.addExtra(.{ .literal_pool_index = res.index }, macho_file);
    }
}

pub fn dedupLiterals(self: *InternalObject, lp: MachO.LiteralPool, macho_file: *MachO) void {
    const tracy = trace(@src());
    defer tracy.end();

    for (self.getAtoms()) |atom_index| {
        const atom = self.getAtom(atom_index) orelse continue;
        if (!atom.flags.alive) continue;

        const relocs = blk: {
            const extra = atom.getExtra(macho_file);
            const relocs = self.sections.items(.relocs)[atom.n_sect].items;
            break :blk relocs[extra.rel_index..][0..extra.rel_count];
        };
        for (relocs) |*rel| {
            if (rel.tag != .@"extern") continue;
            const target_sym_ref = rel.getTargetSymbolRef(atom.*, macho_file);
            const file = target_sym_ref.getFile(macho_file) orelse continue;
            if (file.getIndex() != self.index) continue;
            const target_sym = target_sym_ref.getSymbol(macho_file).?;
            const target_atom = target_sym.getAtom(macho_file) orelse continue;
            if (!Object.isPtrLiteral(target_atom.getInputSection(macho_file))) continue;
            const lp_index = target_atom.getExtra(macho_file).literal_pool_index;
            const lp_sym = lp.getSymbol(lp_index, macho_file);
            const lp_atom_ref = lp_sym.atom_ref;
            if (target_atom.atom_index != lp_atom_ref.index or target_atom.file != lp_atom_ref.file) {
                target_sym.atom_ref = lp_atom_ref;
            }
        }
    }

    for (self.symbols.items) |*sym| {
        if (!sym.flags.objc_stubs) continue;
        const extra = sym.getExtra(macho_file);
        const file = sym.getFile(macho_file).?;
        if (file.getIndex() != self.index) continue;
        const tsym = switch (file) {
            .dylib => unreachable,
            inline else => |x| &x.symbols.items[extra.objc_selrefs],
        };
        const atom = tsym.getAtom(macho_file) orelse continue;
        if (!Object.isPtrLiteral(atom.getInputSection(macho_file))) continue;
        const lp_index = atom.getExtra(macho_file).literal_pool_index;
        const lp_sym = lp.getSymbol(lp_index, macho_file);
        const lp_atom_ref = lp_sym.atom_ref;
        if (atom.atom_index != lp_atom_ref.index or atom.file != lp_atom_ref.file) {
            tsym.atom_ref = lp_atom_ref;
        }
    }
}

pub fn scanRelocs(self: *InternalObject, macho_file: *MachO) void {
    const tracy = trace(@src());
    defer tracy.end();

    if (self.getEntryRef(macho_file)) |ref| {
        if (ref.getFile(macho_file) != null) {
            const sym = ref.getSymbol(macho_file).?;
            if (sym.flags.import) sym.flags.stubs = true;
        }
    }
    if (self.getDyldStubBinderRef(macho_file)) |ref| {
        if (ref.getFile(macho_file) != null) {
            const sym = ref.getSymbol(macho_file).?;
            sym.flags.needs_got = true;
        }
    }
    if (self.getObjcMsgSendRef(macho_file)) |ref| {
        if (ref.getFile(macho_file) != null) {
            const sym = ref.getSymbol(macho_file).?;
            // TODO is it always needed, or only if we are synthesising fast stubs
            sym.flags.needs_got = true;
        }
    }
}

pub fn allocateSyntheticSymbols(self: *InternalObject, macho_file: *MachO) void {
    const text_seg = macho_file.getTextSegment();

    if (self.mh_execute_header_index) |index| {
        const ref = self.getSymbolRef(index, macho_file);
        if (ref.getFile(macho_file)) |file| {
            if (file.getIndex() == self.index) {
                const sym = &self.symbols.items[index];
                sym.value = text_seg.vmaddr;
            }
        }
    }

    if (macho_file.data_sect_index) |idx| {
        const sect = macho_file.sections.items(.header)[idx];
        for (&[_]?Symbol.Index{
            self.dso_handle_index,
            self.mh_dylib_header_index,
            self.dyld_private_index,
        }) |maybe_index| {
            if (maybe_index) |index| {
                const ref = self.getSymbolRef(index, macho_file);
                if (ref.getFile(macho_file)) |file| {
                    if (file.getIndex() == self.index) {
                        const sym = &self.symbols.items[index];
                        sym.value = sect.addr;
                        sym.out_n_sect = idx;
                    }
                }
            }
        }
    }
}

pub fn calcSymtabSize(self: *InternalObject, macho_file: *MachO) void {
    for (self.symbols.items, 0..) |*sym, i| {
        const ref = self.getSymbolRef(@intCast(i), macho_file);
        const file = ref.getFile(macho_file) orelse continue;
        if (file.getIndex() != self.index) continue;
        if (sym.getName(macho_file).len == 0) continue;
        sym.flags.output_symtab = true;
        if (sym.isLocal()) {
            sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file);
            self.output_symtab_ctx.nlocals += 1;
        } else if (sym.flags.@"export") {
            sym.addExtra(.{ .symtab = self.output_symtab_ctx.nexports }, macho_file);
            self.output_symtab_ctx.nexports += 1;
        } else {
            assert(sym.flags.import);
            sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
            self.output_symtab_ctx.nimports += 1;
        }
        self.output_symtab_ctx.strsize += @as(u32, @intCast(sym.getName(macho_file).len + 1));
    }
}

pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void {
    const tracy = trace(@src());
    defer tracy.end();

    for (self.getAtoms()) |atom_index| {
        const atom = self.getAtom(atom_index) orelse continue;
        if (!atom.flags.alive) continue;
        const sect = atom.getInputSection(macho_file);
        if (sect.isZerofill()) continue;
        const off = atom.value;
        const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..atom.size];
        @memcpy(buffer, try self.getSectionData(atom.n_sect));
        try atom.resolveRelocs(macho_file, buffer);
    }
}

pub fn writeSymtab(self: InternalObject, macho_file: *MachO, ctx: anytype) void {
    var n_strx = self.output_symtab_ctx.stroff;
    for (self.symbols.items, 0..) |sym, i| {
        const ref = self.getSymbolRef(@intCast(i), macho_file);
        const file = ref.getFile(macho_file) orelse continue;
        if (file.getIndex() != self.index) continue;
        const idx = sym.getOutputSymtabIndex(macho_file) orelse continue;
        const out_sym = &ctx.symtab.items[idx];
        out_sym.n_strx = n_strx;
        sym.setOutputSym(macho_file, out_sym);
        const name = sym.getName(macho_file);
        @memcpy(ctx.strtab.items[n_strx..][0..name.len], name);
        n_strx += @intCast(name.len);
        ctx.strtab.items[n_strx] = 0;
        n_strx += 1;
    }
}

fn addSection(self: *InternalObject, allocator: Allocator, segname: []const u8, sectname: []const u8) !u32 {
    const n_sect = @as(u32, @intCast(try self.sections.addOne(allocator)));
    self.sections.set(n_sect, .{
        .header = .{
            .sectname = MachO.makeStaticString(sectname),
            .segname = MachO.makeStaticString(segname),
        },
    });
    return n_sect;
}

fn getSectionData(self: *const InternalObject, index: u32) error{Overflow}![]const u8 {
    const slice = self.sections.slice();
    assert(index < slice.items(.header).len);
    const sect = slice.items(.header)[index];
    const extra = slice.items(.extra)[index];
    if (extra.is_objc_methname) {
        const size = std.math.cast(usize, sect.size) orelse return error.Overflow;
        return self.objc_methnames.items[sect.offset..][0..size];
    } else if (extra.is_objc_selref)
        return &self.objc_selrefs
    else
        @panic("ref to non-existent section");
}

pub fn addString(self: *InternalObject, allocator: Allocator, name: []const u8) !u32 {
    const off: u32 = @intCast(self.strtab.items.len);
    try self.strtab.ensureUnusedCapacity(allocator, name.len + 1);
    self.strtab.appendSliceAssumeCapacity(name);
    self.strtab.appendAssumeCapacity(0);
    return off;
}

pub fn getString(self: InternalObject, off: u32) [:0]const u8 {
    assert(off < self.strtab.items.len);
    return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0);
}

pub fn asFile(self: *InternalObject) File {
    return .{ .internal = self };
}

pub fn getAtomRelocs(self: *const InternalObject, atom: Atom, macho_file: *MachO) []const Relocation {
    const extra = atom.getExtra(macho_file);
    const relocs = self.sections.items(.relocs)[atom.n_sect];
    return relocs.items[extra.rel_index..][0..extra.rel_count];
}

fn addAtom(self: *InternalObject, allocator: Allocator) !Atom.Index {
    const atom_index: Atom.Index = @intCast(self.atoms.items.len);
    const atom = try self.atoms.addOne(allocator);
    atom.* = .{
        .file = self.index,
        .atom_index = atom_index,
        .extra = try self.addAtomExtra(allocator, .{}),
    };
    return atom_index;
}

pub fn getAtom(self: *InternalObject, atom_index: Atom.Index) ?*Atom {
    if (atom_index == 0) return null;
    assert(atom_index < self.atoms.items.len);
    return &self.atoms.items[atom_index];
}

pub fn getAtoms(self: InternalObject) []const Atom.Index {
    return self.atoms_indexes.items;
}

fn addAtomExtra(self: *InternalObject, allocator: Allocator, extra: Atom.Extra) !u32 {
    const fields = @typeInfo(Atom.Extra).Struct.fields;
    try self.atoms_extra.ensureUnusedCapacity(allocator, fields.len);
    return self.addAtomExtraAssumeCapacity(extra);
}

fn addAtomExtraAssumeCapacity(self: *InternalObject, extra: Atom.Extra) u32 {
    const index = @as(u32, @intCast(self.atoms_extra.items.len));
    const fields = @typeInfo(Atom.Extra).Struct.fields;
    inline for (fields) |field| {
        self.atoms_extra.appendAssumeCapacity(switch (field.type) {
            u32 => @field(extra, field.name),
            else => @compileError("bad field type"),
        });
    }
    return index;
}

pub fn getAtomExtra(self: InternalObject, index: u32) Atom.Extra {
    const fields = @typeInfo(Atom.Extra).Struct.fields;
    var i: usize = index;
    var result: Atom.Extra = undefined;
    inline for (fields) |field| {
        @field(result, field.name) = switch (field.type) {
            u32 => self.atoms_extra.items[i],
            else => @compileError("bad field type"),
        };
        i += 1;
    }
    return result;
}

pub fn setAtomExtra(self: *InternalObject, index: u32, extra: Atom.Extra) void {
    assert(index > 0);
    const fields = @typeInfo(Atom.Extra).Struct.fields;
    inline for (fields, 0..) |field, i| {
        self.atoms_extra.items[index + i] = switch (field.type) {
            u32 => @field(extra, field.name),
            else => @compileError("bad field type"),
        };
    }
}

pub fn getEntryRef(self: InternalObject, macho_file: *MachO) ?MachO.Ref {
    const index = self.entry_index orelse return null;
    return self.getSymbolRef(index, macho_file);
}

pub fn getDyldStubBinderRef(self: InternalObject, macho_file: *MachO) ?MachO.Ref {
    const index = self.dyld_stub_binder_index orelse return null;
    return self.getSymbolRef(index, macho_file);
}

pub fn getDyldPrivateRef(self: InternalObject, macho_file: *MachO) ?MachO.Ref {
    const index = self.dyld_private_index orelse return null;
    return self.getSymbolRef(index, macho_file);
}

pub fn getObjcMsgSendRef(self: InternalObject, macho_file: *MachO) ?MachO.Ref {
    const index = self.objc_msg_send_index orelse return null;
    return self.getSymbolRef(index, macho_file);
}

pub fn addSymbol(self: *InternalObject, allocator: Allocator) !Symbol.Index {
    try self.symbols.ensureUnusedCapacity(allocator, 1);
    return self.addSymbolAssumeCapacity();
}

pub fn addSymbolAssumeCapacity(self: *InternalObject) Symbol.Index {
    const index: Symbol.Index = @intCast(self.symbols.items.len);
    const symbol = self.symbols.addOneAssumeCapacity();
    symbol.* = .{ .file = self.index };
    return index;
}

pub fn getSymbolRef(self: InternalObject, index: Symbol.Index, macho_file: *MachO) MachO.Ref {
    const global_index = self.globals.items[index];
    if (macho_file.resolver.get(global_index)) |ref| return ref;
    return .{ .index = index, .file = self.index };
}

pub fn addSymbolExtra(self: *InternalObject, allocator: Allocator, extra: Symbol.Extra) !u32 {
    const fields = @typeInfo(Symbol.Extra).Struct.fields;
    try self.symbols_extra.ensureUnusedCapacity(allocator, fields.len);
    return self.addSymbolExtraAssumeCapacity(extra);
}

fn addSymbolExtraAssumeCapacity(self: *InternalObject, extra: Symbol.Extra) u32 {
    const index = @as(u32, @intCast(self.symbols_extra.items.len));
    const fields = @typeInfo(Symbol.Extra).Struct.fields;
    inline for (fields) |field| {
        self.symbols_extra.appendAssumeCapacity(switch (field.type) {
            u32 => @field(extra, field.name),
            else => @compileError("bad field type"),
        });
    }
    return index;
}

pub fn getSymbolExtra(self: InternalObject, index: u32) Symbol.Extra {
    const fields = @typeInfo(Symbol.Extra).Struct.fields;
    var i: usize = index;
    var result: Symbol.Extra = undefined;
    inline for (fields) |field| {
        @field(result, field.name) = switch (field.type) {
            u32 => self.symbols_extra.items[i],
            else => @compileError("bad field type"),
        };
        i += 1;
    }
    return result;
}

pub fn setSymbolExtra(self: *InternalObject, index: u32, extra: Symbol.Extra) void {
    const fields = @typeInfo(Symbol.Extra).Struct.fields;
    inline for (fields, 0..) |field, i| {
        self.symbols_extra.items[index + i] = switch (field.type) {
            u32 => @field(extra, field.name),
            else => @compileError("bad field type"),
        };
    }
}

const FormatContext = struct {
    self: *InternalObject,
    macho_file: *MachO,
};

pub fn fmtAtoms(self: *InternalObject, macho_file: *MachO) std.fmt.Formatter(formatAtoms) {
    return .{ .data = .{
        .self = self,
        .macho_file = macho_file,
    } };
}

fn formatAtoms(
    ctx: FormatContext,
    comptime unused_fmt_string: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,
) !void {
    _ = unused_fmt_string;
    _ = options;
    try writer.writeAll("  atoms\n");
    for (ctx.self.getAtoms()) |atom_index| {
        const atom = ctx.self.getAtom(atom_index) orelse continue;
        try writer.print("    {}\n", .{atom.fmt(ctx.macho_file)});
    }
}

pub fn fmtSymtab(self: *InternalObject, macho_file: *MachO) std.fmt.Formatter(formatSymtab) {
    return .{ .data = .{
        .self = self,
        .macho_file = macho_file,
    } };
}

fn formatSymtab(
    ctx: FormatContext,
    comptime unused_fmt_string: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,
) !void {
    _ = unused_fmt_string;
    _ = options;
    const macho_file = ctx.macho_file;
    const self = ctx.self;
    try writer.writeAll("  symbols\n");
    for (self.symbols.items, 0..) |sym, i| {
        const ref = self.getSymbolRef(@intCast(i), macho_file);
        if (ref.getFile(macho_file) == null) {
            // TODO any better way of handling this?
            try writer.print("    {s} : unclaimed\n", .{sym.getName(macho_file)});
        } else {
            try writer.print("    {}\n", .{ref.getSymbol(macho_file).?.fmt(macho_file)});
        }
    }
}

const Section = struct {
    header: macho.section_64,
    relocs: std.ArrayListUnmanaged(Relocation) = .{},
    extra: Extra = .{},

    const Extra = packed struct {
        is_objc_methname: bool = false,
        is_objc_selref: bool = false,
    };
};

const assert = std.debug.assert;
const macho = std.macho;
const mem = std.mem;
const std = @import("std");
const trace = @import("../../tracy.zig").trace;

const Allocator = std.mem.Allocator;
const Atom = @import("Atom.zig");
const File = @import("file.zig").File;
const InternalObject = @This();
const MachO = @import("../MachO.zig");
const Object = @import("Object.zig");
const Relocation = @import("Relocation.zig");
const Symbol = @import("Symbol.zig");