authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-10 16:36:32+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-18 09:13:08+02:00
log174de37cefde3b22b02fee5982e7bacead4d200f
treede8e99729e11de1b9116ebd3da5077ad722d9993
parent5b4c0cc1f9c4cb047064cffb70bc649b83681814

macho: fix compile errors


17 files changed, 352 insertions(+), 281 deletions(-)

src/arch/x86_64/CodeGen.zig+6-4
......@@ -12362,8 +12362,9 @@ fn genCall(self: *Self, info: union(enum) {
1236212362 try self.genSetReg(.rax, Type.usize, .{ .lea_got = sym_index }, .{});
1236312363 try self.asmRegister(.{ ._, .call }, .rax);
1236412364 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
12365 const sym_index = try macho_file.getZigObject().?.getOrCreateMetadataForDecl(macho_file, func.owner_decl);
12366 const sym = macho_file.getSymbol(sym_index);
12365 const zo = macho_file.getZigObject().?;
12366 const sym_index = try zo.getOrCreateMetadataForDecl(macho_file, func.owner_decl);
12367 const sym = zo.symbols.items[sym_index];
1236712368 try self.genSetReg(
1236812369 .rax,
1236912370 Type.usize,
......@@ -15396,9 +15397,10 @@ fn genLazySymbolRef(
1539615397 else => unreachable,
1539715398 }
1539815399 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
15399 const sym_index = macho_file.getZigObject().?.getOrCreateMetadataForLazySymbol(macho_file, pt, lazy_sym) catch |err|
15400 const zo = macho_file.getZigObject().?;
15401 const sym_index = zo.getOrCreateMetadataForLazySymbol(macho_file, pt, lazy_sym) catch |err|
1540015402 return self.fail("{s} creating lazy symbol", .{@errorName(err)});
15401 const sym = macho_file.getSymbol(sym_index);
15403 const sym = zo.symbols.items[sym_index];
1540215404 switch (tag) {
1540315405 .lea, .call => try self.genSetReg(
1540415406 reg,
src/arch/x86_64/Emit.zig+1-1
......@@ -162,7 +162,7 @@ pub fn emitMir(emit: *Emit) Error!void {
162162 };
163163 const zo = macho_file.getZigObject().?;
164164 const atom = zo.symbols.items[data.atom_index].getAtom(macho_file).?;
165 const sym = zo.symbols.items[data.sym_index];
165 const sym = &zo.symbols.items[data.sym_index];
166166 if (sym.flags.needs_zig_got and !is_obj_or_static_lib) {
167167 _ = try sym.getOrCreateZigGotEntry(data.sym_index, macho_file);
168168 }
src/link/MachO.zig+36-29
......@@ -529,7 +529,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n
529529 dylib.ordinal = @intCast(ord);
530530 }
531531
532 try self.claimUnresolved();
532 self.claimUnresolved();
533533
534534 self.scanRelocs() catch |err| switch (err) {
535535 error.HasUndefinedSymbols => return error.FlushFailure,
......@@ -603,7 +603,12 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n
603603
604604 if (has_resolve_error) return error.FlushFailure;
605605 }
606 try self.writeSectionsAndUpdateLinkeditSizes();
606 self.writeSectionsAndUpdateLinkeditSizes() catch |err| {
607 switch (err) {
608 error.ResolveFailed => return error.FlushFailure,
609 else => |e| return e,
610 }
611 };
607612
608613 try self.writeSectionsToFile();
609614 try self.allocateLinkeditSegment();
......@@ -1450,12 +1455,12 @@ pub fn dedupLiterals(self: *MachO) !void {
14501455 }
14511456}
14521457
1453fn claimUnresolved(self: *MachO) error{OutOfMemory}!void {
1458fn claimUnresolved(self: *MachO) void {
14541459 if (self.getZigObject()) |zo| {
1455 try zo.asFile().claimUnresolved(self);
1460 zo.claimUnresolved(self);
14561461 }
14571462 for (self.objects.items) |index| {
1458 try self.getFile(index).?.claimUnresolved(self);
1463 self.getFile(index).?.object.claimUnresolved(self);
14591464 }
14601465}
14611466
......@@ -2402,7 +2407,7 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void {
24022407 }
24032408
24042409 if (self.la_symbol_ptr_sect_index) |_| {
2405 try self.updatelazyBindSize();
2410 try self.updateLazyBindSize();
24062411 }
24072412
24082413 try self.rebase.updateSize(self);
......@@ -2412,16 +2417,16 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void {
24122417 try self.data_in_code.updateSize(self);
24132418
24142419 if (self.getZigObject()) |zo| {
2415 zo.asFile().writeSymtab(self);
2420 zo.asFile().writeSymtab(self, self);
24162421 }
24172422 for (self.objects.items) |index| {
2418 self.getFile(index).?.writeSymtab(self);
2423 self.getFile(index).?.writeSymtab(self, self);
24192424 }
24202425 for (self.dylibs.items) |index| {
2421 self.getFile(index).?.writeSymtab(self);
2426 self.getFile(index).?.writeSymtab(self, self);
24222427 }
24232428 if (self.getInternalObject()) |obj| {
2424 obj.asFile().writeSymtab(self);
2429 obj.asFile().writeSymtab(self, self);
24252430 }
24262431}
24272432
......@@ -2484,7 +2489,7 @@ fn writeSectionsToFile(self: *MachO) !void {
24842489
24852490 const slice = self.sections.slice();
24862491 for (slice.items(.header), slice.items(.out)) |header, out| {
2487 try self.base.file.pwriteAll(out.items, header.offset);
2492 try self.base.file.?.pwriteAll(out.items, header.offset);
24882493 }
24892494}
24902495
......@@ -2501,7 +2506,7 @@ fn writeDyldInfo(self: *MachO) !void {
25012506 const tracy = trace(@src());
25022507 defer tracy.end();
25032508
2504 const gpa = self.base.allocator;
2509 const gpa = self.base.comp.gpa;
25052510 const base_off = self.getLinkeditSegment().fileoff;
25062511 const cmd = self.dyld_info_cmd;
25072512 var needed_size: u32 = 0;
......@@ -2527,14 +2532,14 @@ fn writeDyldInfo(self: *MachO) !void {
25272532 try self.lazy_bind.write(writer);
25282533 try stream.seekTo(cmd.export_off - base_off);
25292534 try self.export_trie.write(writer);
2530 try self.base.file.pwriteAll(buffer, cmd.rebase_off);
2535 try self.base.file.?.pwriteAll(buffer, cmd.rebase_off);
25312536}
25322537
25332538pub fn writeDataInCode(self: *MachO) !void {
25342539 const tracy = trace(@src());
25352540 defer tracy.end();
25362541 const cmd = self.data_in_code_cmd;
2537 try self.base.file.pwriteAll(mem.sliceAsBytes(self.data_in_code.entries.items), cmd.dataoff);
2542 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.data_in_code.entries.items), cmd.dataoff);
25382543}
25392544
25402545fn writeIndsymtab(self: *MachO) !void {
......@@ -2546,15 +2551,15 @@ fn writeIndsymtab(self: *MachO) !void {
25462551 var buffer = try std.ArrayList(u8).initCapacity(gpa, needed_size);
25472552 defer buffer.deinit();
25482553 try self.indsymtab.write(self, buffer.writer());
2549 try self.base.file.pwriteAll(buffer.items, cmd.indirectsymoff);
2554 try self.base.file.?.pwriteAll(buffer.items, cmd.indirectsymoff);
25502555}
25512556
25522557pub fn writeSymtabToFile(self: *MachO) !void {
25532558 const tracy = trace(@src());
25542559 defer tracy.end();
25552560 const cmd = self.symtab_cmd;
2556 try self.base.file.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff);
2557 try self.base.file.pwriteAll(self.strtab.items, cmd.stroff);
2561 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff);
2562 try self.base.file.?.pwriteAll(self.strtab.items, cmd.stroff);
25582563}
25592564
25602565fn writeUnwindInfo(self: *MachO) !void {
......@@ -2687,18 +2692,20 @@ fn writeLoadCommands(self: *MachO) !struct { usize, usize, u64 } {
26872692 try load_commands.writeDylinkerLC(writer);
26882693 ncmds += 1;
26892694
2690 if (self.entry_index) |global_index| {
2691 const sym = self.getSymbol(global_index);
2692 const seg = self.getTextSegment();
2693 const entryoff: u32 = if (sym.getFile(self) == null)
2694 0
2695 else
2696 @as(u32, @intCast(sym.getAddress(.{ .stubs = true }, self) - seg.vmaddr));
2697 try writer.writeStruct(macho.entry_point_command{
2698 .entryoff = entryoff,
2699 .stacksize = self.base.stack_size,
2700 });
2701 ncmds += 1;
2695 if (self.getInternalObject()) |obj| {
2696 if (obj.getEntryRef(self)) |ref| {
2697 const sym = ref.getSymbol(self).?;
2698 const seg = self.getTextSegment();
2699 const entryoff: u32 = if (sym.getFile(self) == null)
2700 0
2701 else
2702 @as(u32, @intCast(sym.getAddress(.{ .stubs = true }, self) - seg.vmaddr));
2703 try writer.writeStruct(macho.entry_point_command{
2704 .entryoff = entryoff,
2705 .stacksize = self.base.stack_size,
2706 });
2707 ncmds += 1;
2708 }
27022709 }
27032710
27042711 if (self.base.isDynLib()) {
src/link/MachO/Atom.zig+19-15
......@@ -213,7 +213,8 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
213213/// File offset relocation happens transparently, so it is not included in
214214/// this calculation.
215215pub fn capacity(self: Atom, macho_file: *MachO) u64 {
216 const next_addr = if (macho_file.getAtom(self.next_index)) |next|
216 const zo = macho_file.getZigObject().?;
217 const next_addr = if (zo.getAtom(self.next_index)) |next|
217218 next.getAddress(macho_file)
218219 else
219220 std.math.maxInt(u32);
......@@ -222,7 +223,8 @@ pub fn capacity(self: Atom, macho_file: *MachO) u64 {
222223
223224pub fn freeListEligible(self: Atom, macho_file: *MachO) bool {
224225 // No need to keep a free list node for the last block.
225 const next = macho_file.getAtom(self.next_index) orelse return false;
226 const zo = macho_file.getZigObject().?;
227 const next = zo.getAtom(self.next_index) orelse return false;
226228 const cap = next.getAddress(macho_file) - self.getAddress(macho_file);
227229 const ideal_cap = MachO.padToIdeal(self.size);
228230 if (cap <= ideal_cap) return false;
......@@ -231,6 +233,7 @@ pub fn freeListEligible(self: Atom, macho_file: *MachO) bool {
231233}
232234
233235pub fn allocate(self: *Atom, macho_file: *MachO) !void {
236 const zo = macho_file.getZigObject().?;
234237 const sect = &macho_file.sections.items(.header)[self.out_n_sect];
235238 const free_list = &macho_file.sections.items(.free_list)[self.out_n_sect];
236239 const last_atom_index = &macho_file.sections.items(.last_atom_index)[self.out_n_sect];
......@@ -250,7 +253,7 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void {
250253 var i: usize = free_list.items.len;
251254 while (i < free_list.items.len) {
252255 const big_atom_index = free_list.items[i];
253 const big_atom = macho_file.getAtom(big_atom_index).?;
256 const big_atom = zo.getAtom(big_atom_index).?;
254257 // We now have a pointer to a live atom that has too much capacity.
255258 // Is it enough that we could fit this new atom?
256259 const cap = big_atom.capacity(macho_file);
......@@ -282,7 +285,7 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void {
282285 free_list_removal = i;
283286 }
284287 break :blk new_start_vaddr;
285 } else if (macho_file.getAtom(last_atom_index.*)) |last| {
288 } else if (zo.getAtom(last_atom_index.*)) |last| {
286289 const ideal_capacity = MachO.padToIdeal(last.size);
287290 const ideal_capacity_end_vaddr = last.value + ideal_capacity;
288291 const new_start_vaddr = self.alignment.forward(ideal_capacity_end_vaddr);
......@@ -302,7 +305,7 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void {
302305 });
303306
304307 const expand_section = if (atom_placement) |placement_index|
305 macho_file.getAtom(placement_index).?.next_index == 0
308 zo.getAtom(placement_index).?.next_index == 0
306309 else
307310 true;
308311 if (expand_section) {
......@@ -327,15 +330,15 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void {
327330 // This function can also reallocate an atom.
328331 // In this case we need to "unplug" it from its previous location before
329332 // plugging it in to its new location.
330 if (macho_file.getAtom(self.prev_index)) |prev| {
333 if (zo.getAtom(self.prev_index)) |prev| {
331334 prev.next_index = self.next_index;
332335 }
333 if (macho_file.getAtom(self.next_index)) |next| {
336 if (zo.getAtom(self.next_index)) |next| {
334337 next.prev_index = self.prev_index;
335338 }
336339
337340 if (atom_placement) |big_atom_index| {
338 const big_atom = macho_file.getAtom(big_atom_index).?;
341 const big_atom = zo.getAtom(big_atom_index).?;
339342 self.prev_index = big_atom_index;
340343 self.next_index = big_atom.next_index;
341344 big_atom.next_index = self.atom_index;
......@@ -365,6 +368,7 @@ pub fn free(self: *Atom, macho_file: *MachO) void {
365368
366369 const comp = macho_file.base.comp;
367370 const gpa = comp.gpa;
371 const zo = macho_file.getZigObject().?;
368372 const free_list = &macho_file.sections.items(.free_list)[self.out_n_sect];
369373 const last_atom_index = &macho_file.sections.items(.last_atom_index)[self.out_n_sect];
370374 var already_have_free_list_node = false;
......@@ -383,9 +387,9 @@ pub fn free(self: *Atom, macho_file: *MachO) void {
383387 }
384388 }
385389
386 if (macho_file.getAtom(last_atom_index.*)) |last_atom| {
390 if (zo.getAtom(last_atom_index.*)) |last_atom| {
387391 if (last_atom.atom_index == self.atom_index) {
388 if (macho_file.getAtom(self.prev_index)) |_| {
392 if (zo.getAtom(self.prev_index)) |_| {
389393 // TODO shrink the section size here
390394 last_atom_index.* = self.prev_index;
391395 } else {
......@@ -394,7 +398,7 @@ pub fn free(self: *Atom, macho_file: *MachO) void {
394398 }
395399 }
396400
397 if (macho_file.getAtom(self.prev_index)) |prev| {
401 if (zo.getAtom(self.prev_index)) |prev| {
398402 prev.next_index = self.next_index;
399403 if (!already_have_free_list_node and prev.*.freeListEligible(macho_file)) {
400404 // The free list is heuristics, it doesn't have to be perfect, so we can
......@@ -405,7 +409,7 @@ pub fn free(self: *Atom, macho_file: *MachO) void {
405409 self.prev_index = 0;
406410 }
407411
408 if (macho_file.getAtom(self.next_index)) |next| {
412 if (zo.getAtom(self.next_index)) |next| {
409413 next.prev_index = self.prev_index;
410414 } else {
411415 self.next_index = 0;
......@@ -423,7 +427,7 @@ pub fn addReloc(self: *Atom, macho_file: *MachO, reloc: Relocation) !void {
423427 const gpa = macho_file.base.comp.gpa;
424428 const file = self.getFile(macho_file);
425429 assert(file == .zig_object);
426 var extra = self.getExtra(macho_file).?;
430 var extra = self.getExtra(macho_file);
427431 const rels = &file.zig_object.relocs.items[extra.rel_index];
428432 try rels.append(gpa, reloc);
429433 extra.rel_count += 1;
......@@ -432,7 +436,7 @@ pub fn addReloc(self: *Atom, macho_file: *MachO, reloc: Relocation) !void {
432436
433437pub fn freeRelocs(self: *Atom, macho_file: *MachO) void {
434438 self.getFile(macho_file).zig_object.freeAtomRelocs(self.*, macho_file);
435 var extra = self.getExtra(macho_file).?;
439 var extra = self.getExtra(macho_file);
436440 extra.rel_count = 0;
437441 self.setExtra(extra, macho_file);
438442}
......@@ -630,7 +634,7 @@ fn resolveRelocInner(
630634 const TLS = @as(i64, @intCast(macho_file.getTlsAddress()));
631635 const SUB = if (subtractor) |sub| @as(i64, @intCast(sub.getTargetAddress(self, macho_file))) else 0;
632636 // Address of the __got_zig table entry if any.
633 const ZIG_GOT = @as(i64, @intCast(rel.getZigGotTargetAddress(self, macho_file)));
637 const ZIG_GOT = @as(i64, @intCast(rel.getZigGotTargetAddress(macho_file)));
634638
635639 const divExact = struct {
636640 fn divExact(atom: Atom, r: Relocation, num: u12, den: u12, ctx: *MachO) !u12 {
src/link/MachO/DebugSymbols.zig+6-6
......@@ -175,8 +175,9 @@ fn findFreeSpace(self: *DebugSymbols, object_size: u64, min_alignment: u64) u64
175175}
176176
177177pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {
178 const zo = macho_file.getZigObject().?;
178179 for (self.relocs.items) |*reloc| {
179 const sym = macho_file.getSymbol(reloc.target);
180 const sym = zo.symbols.items[reloc.target];
180181 const sym_name = sym.getName(macho_file);
181182 const addr = switch (reloc.type) {
182183 .direct_load => sym.getAddress(.{}, macho_file),
......@@ -382,23 +383,22 @@ pub fn writeSymtab(self: *DebugSymbols, off: u32, macho_file: *MachO) !u32 {
382383 cmd.symoff = off;
383384
384385 try self.symtab.resize(gpa, cmd.nsyms);
385 try self.strtab.ensureUnusedCapacity(gpa, cmd.strsize - 1);
386 try self.strtab.resize(gpa, cmd.strsize);
387 self.strtab.items[0] = 0;
386388
387389 if (macho_file.getZigObject()) |zo| {
388390 zo.writeSymtab(macho_file, self);
389391 }
390392 for (macho_file.objects.items) |index| {
391 try macho_file.getFile(index).?.writeSymtab(macho_file, self);
393 macho_file.getFile(index).?.writeSymtab(macho_file, self);
392394 }
393395 for (macho_file.dylibs.items) |index| {
394 try macho_file.getFile(index).?.writeSymtab(macho_file, self);
396 macho_file.getFile(index).?.writeSymtab(macho_file, self);
395397 }
396398 if (macho_file.getInternalObject()) |internal| {
397399 internal.writeSymtab(macho_file, self);
398400 }
399401
400 assert(self.strtab.items.len == cmd.strsize);
401
402402 try self.file.pwriteAll(mem.sliceAsBytes(self.symtab.items), cmd.symoff);
403403
404404 return off + cmd.nsyms * @sizeOf(macho.nlist_64);
src/link/MachO/Dylib.zig+15-9
......@@ -6,7 +6,7 @@ strtab: std.ArrayListUnmanaged(u8) = .{},
66id: ?Id = null,
77ordinal: u16 = 0,
88
9symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
9symbols: std.ArrayListUnmanaged(Symbol) = .{},
1010symbols_extra: std.ArrayListUnmanaged(u32) = .{},
1111globals: std.ArrayListUnmanaged(MachO.SymbolResolver.Index) = .{},
1212dependents: std.ArrayListUnmanaged(Id) = .{},
......@@ -516,7 +516,7 @@ pub fn initSymbols(self: *Dylib, macho_file: *MachO) !void {
516516 }
517517}
518518
519pub fn resolveSymbols(self: *Dylib, macho_file: *MachO) void {
519pub fn resolveSymbols(self: *Dylib, macho_file: *MachO) !void {
520520 const tracy = trace(@src());
521521 defer tracy.end();
522522
......@@ -584,7 +584,7 @@ pub fn calcSymtabSize(self: *Dylib, macho_file: *MachO) void {
584584 }
585585}
586586
587pub fn writeSymtab(self: Dylib, macho_file: *MachO) void {
587pub fn writeSymtab(self: Dylib, macho_file: *MachO, ctx: anytype) void {
588588 const tracy = trace(@src());
589589 defer tracy.end();
590590
......@@ -594,13 +594,13 @@ pub fn writeSymtab(self: Dylib, macho_file: *MachO) void {
594594 const file = ref.getFile(macho_file) orelse continue;
595595 if (file.getIndex() != self.index) continue;
596596 const idx = sym.getOutputSymtabIndex(macho_file) orelse continue;
597 const out_sym = &macho_file.symtab.items[idx];
597 const out_sym = &ctx.symtab.items[idx];
598598 out_sym.n_strx = n_strx;
599599 sym.setOutputSym(macho_file, out_sym);
600600 const name = sym.getName(macho_file);
601 @memcpy(macho_file.strtab.items[n_strx..][0..name.len], name);
601 @memcpy(ctx.strtab.items[n_strx..][0..name.len], name);
602602 n_strx += @intCast(name.len);
603 macho_file.strtab.items[n_strx] = 0;
603 ctx.strtab.items[n_strx] = 0;
604604 n_strx += 1;
605605 }
606606}
......@@ -718,10 +718,16 @@ fn formatSymtab(
718718 _ = unused_fmt_string;
719719 _ = options;
720720 const dylib = ctx.dylib;
721 const macho_file = ctx.macho_file;
721722 try writer.writeAll(" globals\n");
722 for (dylib.symbols.items) |index| {
723 const global = ctx.macho_file.getSymbol(index);
724 try writer.print(" {}\n", .{global.fmt(ctx.macho_file)});
723 for (dylib.symbols.items, 0..) |sym, i| {
724 const ref = dylib.getSymbolRef(@intCast(i), macho_file);
725 if (ref.getFile(macho_file) == null) {
726 // TODO any better way of handling this?
727 try writer.print(" {s} : unclaimed\n", .{sym.getName(macho_file)});
728 } else {
729 try writer.print(" {}\n", .{ref.getSymbol(macho_file).?.fmt(macho_file)});
730 }
725731 }
726732}
727733
src/link/MachO/InternalObject.zig+19-19
......@@ -16,7 +16,7 @@ objc_selrefs: [@sizeOf(u64)]u8 = [_]u8{0} ** @sizeOf(u64),
1616force_undefined: std.ArrayListUnmanaged(Symbol.Index) = .{},
1717entry_index: ?Symbol.Index = null,
1818dyld_stub_binder_index: ?Symbol.Index = null,
19dyld_private: ?Symbol.Index = null,
19dyld_private_index: ?Symbol.Index = null,
2020objc_msg_send_index: ?Symbol.Index = null,
2121mh_execute_header_index: ?Symbol.Index = null,
2222mh_dylib_header_index: ?Symbol.Index = null,
......@@ -206,7 +206,7 @@ pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void {
206206 const nlist_idx: u32 = @intCast(self.symtab.items.len);
207207 const nlist = self.symtab.addOneAssumeCapacity();
208208 nlist.* = .{
209 .n_strx = name_off.pos,
209 .n_strx = name_off,
210210 .n_type = macho.N_SECT,
211211 .n_sect = 0,
212212 .n_desc = 0,
......@@ -273,7 +273,7 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil
273273 const nlist_idx: u32 = @intCast(self.symtab.items.len);
274274 const nlist = try self.symtab.addOne(gpa);
275275 nlist.* = .{
276 .n_strx = name_str.pos,
276 .n_strx = name_str,
277277 .n_type = macho.N_SECT,
278278 .n_sect = @intCast(n_sect + 1),
279279 .n_desc = 0,
......@@ -286,12 +286,12 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil
286286}
287287
288288fn addObjcSelrefsSection(self: *InternalObject, methname_sym_index: Symbol.Index, macho_file: *MachO) !Symbol.Index {
289 const gpa = macho_file.base.allocator;
289 const gpa = macho_file.base.comp.gpa;
290290 const atom_index = try self.addAtom(gpa);
291291 try self.atoms_indexes.append(gpa, atom_index);
292292 const atom = self.getAtom(atom_index).?;
293293 atom.size = @sizeOf(u64);
294 atom.alignment = 3;
294 atom.alignment = .@"8";
295295
296296 const n_sect = try self.addSection(gpa, "__DATA", "__objc_selrefs");
297297 const sect = &self.sections.items(.header)[n_sect];
......@@ -389,7 +389,7 @@ pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !voi
389389 };
390390 sym.nlist_idx = nlist_idx;
391391 sym.extra = try self.addSymbolExtra(gpa, .{ .objc_selrefs = selrefs_index });
392 sym.setSectionFlags(.{ .objc_stubs = true });
392 sym.flags.objc_stubs = true;
393393
394394 const idx = ref.getFile(macho_file).?.object.globals.items[ref.index];
395395 try self.globals.append(gpa, idx);
......@@ -417,7 +417,7 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file
417417 const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?;
418418 try buffer.ensureUnusedCapacity(target.size);
419419 buffer.resize(target.size) catch unreachable;
420 @memcpy(buffer.items, self.getSectionData(target.n_sect));
420 @memcpy(buffer.items, try self.getSectionData(target.n_sect));
421421 const res = try lp.insert(gpa, header.type(), buffer.items);
422422 buffer.clearRetainingCapacity();
423423 if (!res.found_existing) {
......@@ -425,7 +425,7 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file
425425 } else {
426426 const lp_sym = lp.getSymbol(res.index, macho_file);
427427 const lp_atom = lp_sym.getAtom(macho_file).?;
428 lp_atom.alignment = @max(lp_atom.alignment, atom.alignment);
428 lp_atom.alignment = lp_atom.alignment.max(atom.alignment);
429429 atom.flags.alive = false;
430430 }
431431 atom.addExtra(.{ .literal_pool_index = res.index }, macho_file);
......@@ -438,7 +438,7 @@ pub fn dedupLiterals(self: *InternalObject, lp: MachO.LiteralPool, macho_file: *
438438
439439 for (self.getAtoms()) |atom_index| {
440440 const atom = self.getAtom(atom_index) orelse continue;
441 if (!atom.alive.load(.seq_cst)) continue;
441 if (!atom.flags.alive) continue;
442442
443443 const relocs = blk: {
444444 const extra = atom.getExtra(macho_file);
......@@ -463,7 +463,7 @@ pub fn dedupLiterals(self: *InternalObject, lp: MachO.LiteralPool, macho_file: *
463463 }
464464
465465 for (self.symbols.items) |*sym| {
466 if (!sym.getSectionFlags().objc_stubs) continue;
466 if (!sym.flags.objc_stubs) continue;
467467 const extra = sym.getExtra(macho_file);
468468 const file = sym.getFile(macho_file).?;
469469 if (file.getIndex() != self.index) continue;
......@@ -495,14 +495,14 @@ pub fn scanRelocs(self: *InternalObject, macho_file: *MachO) void {
495495 if (self.getDyldStubBinderRef(macho_file)) |ref| {
496496 if (ref.getFile(macho_file) != null) {
497497 const sym = ref.getSymbol(macho_file).?;
498 sym.flags.got = true;
498 sym.flags.needs_got = true;
499499 }
500500 }
501501 if (self.getObjcMsgSendRef(macho_file)) |ref| {
502502 if (ref.getFile(macho_file) != null) {
503503 const sym = ref.getSymbol(macho_file).?;
504504 // TODO is it always needed, or only if we are synthesising fast stubs
505 sym.flags.got = true;
505 sym.flags.needs_got = true;
506506 }
507507 }
508508}
......@@ -569,30 +569,30 @@ pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void {
569569
570570 for (self.getAtoms()) |atom_index| {
571571 const atom = self.getAtom(atom_index) orelse continue;
572 if (!atom.alive.load(.seq_cst)) continue;
572 if (!atom.flags.alive) continue;
573573 const sect = atom.getInputSection(macho_file);
574574 if (sect.isZerofill()) continue;
575575 const off = atom.value;
576576 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..atom.size];
577 @memcpy(buffer, self.getSectionData(atom.n_sect));
577 @memcpy(buffer, try self.getSectionData(atom.n_sect));
578578 try atom.resolveRelocs(macho_file, buffer);
579579 }
580580}
581581
582pub fn writeSymtab(self: InternalObject, macho_file: *MachO) void {
582pub fn writeSymtab(self: InternalObject, macho_file: *MachO, ctx: anytype) void {
583583 var n_strx = self.output_symtab_ctx.stroff;
584584 for (self.symbols.items, 0..) |sym, i| {
585585 const ref = self.getSymbolRef(@intCast(i), macho_file);
586586 const file = ref.getFile(macho_file) orelse continue;
587587 if (file.getIndex() != self.index) continue;
588588 const idx = sym.getOutputSymtabIndex(macho_file) orelse continue;
589 const out_sym = &macho_file.symtab.items[idx];
589 const out_sym = &ctx.symtab.items[idx];
590590 out_sym.n_strx = n_strx;
591591 sym.setOutputSym(macho_file, out_sym);
592592 const name = sym.getName(macho_file);
593 @memcpy(macho_file.strtab.items[n_strx..][0..name.len], name);
593 @memcpy(ctx.strtab.items[n_strx..][0..name.len], name);
594594 n_strx += @intCast(name.len);
595 macho_file.strtab.items[n_strx] = 0;
595 ctx.strtab.items[n_strx] = 0;
596596 n_strx += 1;
597597 }
598598}
......@@ -640,7 +640,7 @@ pub fn asFile(self: *InternalObject) File {
640640}
641641
642642pub fn getAtomRelocs(self: *const InternalObject, atom: Atom, macho_file: *MachO) []const Relocation {
643 const extra = atom.getExtra(macho_file).?;
643 const extra = atom.getExtra(macho_file);
644644 const relocs = self.sections.items(.relocs)[atom.n_sect];
645645 return relocs.items[extra.rel_index..][0..extra.rel_count];
646646}
src/link/MachO/Object.zig+138-116
......@@ -11,10 +11,10 @@ sections: std.MultiArrayList(Section) = .{},
1111symtab: std.MultiArrayList(Nlist) = .{},
1212strtab: std.ArrayListUnmanaged(u8) = .{},
1313
14symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
14symbols: std.ArrayListUnmanaged(Symbol) = .{},
1515symbols_extra: std.ArrayListUnmanaged(u32) = .{},
1616globals: std.ArrayListUnmanaged(MachO.SymbolResolver.Index) = .{},
17atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
17atoms: std.ArrayListUnmanaged(Atom) = .{},
1818atoms_indexes: std.ArrayListUnmanaged(Atom.Index) = .{},
1919atoms_extra: std.ArrayListUnmanaged(u32) = .{},
2020
......@@ -228,7 +228,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
228228
229229 // Parse DWARF __TEXT,__eh_frame section
230230 if (self.eh_frame_sect_index) |index| {
231 try self.initEhFrameRecords(index, macho_file);
231 try self.initEhFrameRecords(gpa, index, handle, macho_file);
232232 }
233233
234234 // Parse Apple's __LD,__compact_unwind section
......@@ -261,7 +261,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
261261
262262 try self.parseDebugInfo(macho_file);
263263
264 for (self.atoms.items) |atom_index| {
264 for (self.getAtoms()) |atom_index| {
265265 const atom = self.getAtom(atom_index) orelse continue;
266266 const isec = atom.getInputSection(macho_file);
267267 if (mem.eql(u8, isec.sectName(), "__eh_frame") or
......@@ -606,7 +606,7 @@ fn initPointerLiterals(self: *Object, allocator: Allocator, macho_file: *MachO)
606606 }
607607}
608608
609pub fn resolveLiterals(self: Object, lp: *MachO.LiteralPool, macho_file: *MachO) !void {
609pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO) !void {
610610 const tracy = trace(@src());
611611 defer tracy.end();
612612
......@@ -646,7 +646,7 @@ pub fn resolveLiterals(self: Object, lp: *MachO.LiteralPool, macho_file: *MachO)
646646 } else {
647647 const lp_sym = lp.getSymbol(res.index, macho_file);
648648 const lp_atom = lp_sym.getAtom(macho_file).?;
649 lp_atom.alignment = @max(lp_atom.alignment, atom.alignment);
649 lp_atom.alignment = lp_atom.alignment.max(atom.alignment);
650650 atom.flags.alive = false;
651651 }
652652 atom.addExtra(.{ .literal_pool_index = res.index }, macho_file);
......@@ -684,7 +684,7 @@ pub fn resolveLiterals(self: Object, lp: *MachO.LiteralPool, macho_file: *MachO)
684684 } else {
685685 const lp_sym = lp.getSymbol(res.index, macho_file);
686686 const lp_atom = lp_sym.getAtom(macho_file).?;
687 lp_atom.alignment = @max(lp_atom.alignment, atom.alignment);
687 lp_atom.alignment = lp_atom.alignment.max(atom.alignment);
688688 atom.flags.alive = false;
689689 }
690690 atom.addExtra(.{ .literal_pool_index = res.index }, macho_file);
......@@ -823,7 +823,7 @@ fn initSymbols(self: *Object, allocator: Allocator, macho_file: *MachO) !void {
823823 const index = self.addSymbolAssumeCapacity();
824824 const symbol = &self.symbols.items[index];
825825 symbol.value = nlist.n_value;
826 symbol.name = .{ .pos = nlist.n_strx, .len = @intCast(self.getNStrx(nlist.n_strx).len + 1) };
826 symbol.name = nlist.n_strx;
827827 symbol.nlist_idx = @intCast(i);
828828 symbol.extra = self.addSymbolExtraAssumeCapacity(.{});
829829
......@@ -838,7 +838,9 @@ fn initSymbols(self: *Object, allocator: Allocator, macho_file: *MachO) !void {
838838 symbol.flags.tentative = nlist.tentative();
839839 symbol.flags.no_dead_strip = symbol.flags.no_dead_strip or nlist.noDeadStrip();
840840 symbol.flags.dyn_ref = nlist.n_desc & macho.REFERENCED_DYNAMICALLY != 0;
841 symbol.flags.interposable = nlist.ext() and (nlist.sect() or nlist.abs()) and macho_file.options.dylib and macho_file.options.namespace == .flat and !nlist.pext();
841 symbol.flags.interposable = nlist.ext() and (nlist.sect() or nlist.abs()) and macho_file.base.isDynLib() and !nlist.pext();
842 // TODO
843 // symbol.flags.interposable = nlist.ext() and (nlist.sect() or nlist.abs()) and macho_file.base.isDynLib() and macho_file.options.namespace == .flat and !nlist.pext();
842844
843845 if (nlist.sect() and
844846 self.sections.items(.header)[nlist.n_sect - 1].type() == macho.S_THREAD_LOCAL_VARIABLES)
......@@ -869,7 +871,7 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f
869871 fn find(fs: @This(), addr: u64) ?Symbol.Index {
870872 // TODO binary search since we have the list sorted
871873 for (fs.entries) |nlist| {
872 if (nlist.nlist.n_value == addr) return fs.ctx.symbols.items[nlist.idx];
874 if (nlist.nlist.n_value == addr) return @intCast(nlist.idx);
873875 }
874876 return null;
875877 }
......@@ -920,17 +922,17 @@ fn initSymbolStabs(self: *Object, allocator: Allocator, nlists: anytype, macho_f
920922 switch (nlist.n_type) {
921923 macho.N_BNSYM => {
922924 stab.is_func = true;
923 stab.symbol = sym_lookup.find(nlist.n_value);
925 stab.index = sym_lookup.find(nlist.n_value);
924926 // TODO validate
925927 i += 3;
926928 },
927929 macho.N_GSYM => {
928930 stab.is_func = false;
929 stab.symbol = sym_lookup.find(addr_lookup.get(self.getString(nlist.n_strx)).?);
931 stab.index = sym_lookup.find(addr_lookup.get(self.getString(nlist.n_strx)).?);
930932 },
931933 macho.N_STSYM => {
932934 stab.is_func = false;
933 stab.symbol = sym_lookup.find(nlist.n_value);
935 stab.index = sym_lookup.find(nlist.n_value);
934936 },
935937 else => {
936938 try macho_file.reportParseError2(self.index, "unhandled symbol stab type 0x{x}", .{
......@@ -1103,9 +1105,9 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil
11031105 ctx: *const Object,
11041106
11051107 fn find(fs: @This(), addr: u64) ?Symbol.Index {
1106 for (fs.ctx.symbols.items, 0..) |sym_index, i| {
1108 for (0..fs.ctx.symbols.items.len) |i| {
11071109 const nlist = fs.ctx.symtab.items(.nlist)[i];
1108 if (nlist.ext() and nlist.n_value == addr) return sym_index;
1110 if (nlist.ext() and nlist.n_value == addr) return @intCast(i);
11091111 }
11101112 return null;
11111113 }
......@@ -1274,7 +1276,7 @@ fn parseUnwindRecords(self: *Object, allocator: Allocator, cpu_arch: std.Target.
12741276 // Create a null record
12751277 const rec_index = try self.addUnwindRecord(allocator);
12761278 const rec = self.getUnwindRecord(rec_index);
1277 const atom = macho_file.getAtom(meta.atom).?;
1279 const atom = self.getAtom(meta.atom).?;
12781280 try self.unwind_records_indexes.append(allocator, rec_index);
12791281 rec.length = @intCast(meta.size);
12801282 rec.atom = meta.atom;
......@@ -1468,17 +1470,17 @@ fn findCompileUnit(self: *Object, args: struct {
14681470 };
14691471}
14701472
1471pub fn resolveSymbols(self: *Object, macho_file: *MachO) void {
1473pub fn resolveSymbols(self: *Object, macho_file: *MachO) !void {
14721474 const tracy = trace(@src());
14731475 defer tracy.end();
14741476
1475 const gpa = macho_file.base.allocator;
1477 const gpa = macho_file.base.comp.gpa;
14761478
14771479 for (self.symtab.items(.nlist), self.symtab.items(.atom), self.globals.items, 0..) |nlist, atom_index, *global, i| {
14781480 if (!nlist.ext()) continue;
14791481 if (nlist.sect()) {
14801482 const atom = self.getAtom(atom_index).?;
1481 if (!atom.alive.load(.seq_cst)) continue;
1483 if (!atom.flags.alive) continue;
14821484 }
14831485
14841486 const gop = try macho_file.resolver.getOrPut(gpa, .{
......@@ -1643,7 +1645,7 @@ pub fn claimUnresolved(self: *Object, macho_file: *MachO) void {
16431645
16441646 if (self.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue;
16451647
1646 const is_import = switch (macho_file.options.undefined_treatment) {
1648 const is_import = switch (macho_file.undefined_treatment) {
16471649 .@"error" => false,
16481650 .warn, .suppress => nlist.weakRef(),
16491651 .dynamic_lookup => true,
......@@ -1682,8 +1684,8 @@ pub fn claimUnresolvedRelocatable(self: *Object, macho_file: *MachO) void {
16821684 }
16831685}
16841686
1685fn addSection(self: *Object, allocator: Allocator, segname: []const u8, sectname: []const u8) !u32 {
1686 const n_sect = @as(u32, @intCast(try self.sections.addOne(allocator)));
1687fn addSection(self: *Object, allocator: Allocator, segname: []const u8, sectname: []const u8) !u8 {
1688 const n_sect = @as(u8, @intCast(try self.sections.addOne(allocator)));
16871689 self.sections.set(n_sect, .{
16881690 .header = .{
16891691 .sectname = MachO.makeStaticString(sectname),
......@@ -1799,7 +1801,7 @@ pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writ
17991801 try writer.writeAll(data);
18001802}
18011803
1802pub fn calcSymtabSize(self: *Object, macho_file: *MachO) !void {
1804pub fn calcSymtabSize(self: *Object, macho_file: *MachO) void {
18031805 const tracy = trace(@src());
18041806 defer tracy.end();
18051807
......@@ -1821,27 +1823,27 @@ pub fn calcSymtabSize(self: *Object, macho_file: *MachO) !void {
18211823 continue;
18221824 sym.flags.output_symtab = true;
18231825 if (sym.isLocal()) {
1824 try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file);
1826 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file);
18251827 self.output_symtab_ctx.nlocals += 1;
18261828 } else if (sym.flags.@"export") {
1827 try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nexports }, macho_file);
1829 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nexports }, macho_file);
18281830 self.output_symtab_ctx.nexports += 1;
18291831 } else {
18301832 assert(sym.flags.import);
1831 try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
1833 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
18321834 self.output_symtab_ctx.nimports += 1;
18331835 }
18341836 self.output_symtab_ctx.strsize += @as(u32, @intCast(sym.getName(macho_file).len + 1));
18351837 }
18361838
18371839 if (macho_file.base.comp.config.debug_format != .strip and self.hasDebugInfo())
1838 try self.calcStabsSize(macho_file);
1840 self.calcStabsSize(macho_file);
18391841}
18401842
1841pub fn calcStabsSize(self: *Object, macho_file: *MachO) error{Overflow}!void {
1843pub fn calcStabsSize(self: *Object, macho_file: *MachO) void {
18421844 if (self.compile_unit) |cu| {
1843 const comp_dir = cu.getCompDir(self);
1844 const tu_name = cu.getTuName(self);
1845 const comp_dir = cu.getCompDir(self.*);
1846 const tu_name = cu.getTuName(self.*);
18451847
18461848 self.output_symtab_ctx.nstabs += 4; // N_SO, N_SO, N_OSO, N_SO
18471849 self.output_symtab_ctx.strsize += @as(u32, @intCast(comp_dir.len + 1)); // comp_dir
......@@ -1876,12 +1878,12 @@ pub fn calcStabsSize(self: *Object, macho_file: *MachO) error{Overflow}!void {
18761878
18771879 for (self.stab_files.items) |sf| {
18781880 self.output_symtab_ctx.nstabs += 4; // N_SO, N_SO, N_OSO, N_SO
1879 self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getCompDir(self).len + 1)); // comp_dir
1880 self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getTuName(self).len + 1)); // tu_name
1881 self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getOsoPath(self).len + 1)); // path
1881 self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getCompDir(self.*).len + 1)); // comp_dir
1882 self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getTuName(self.*).len + 1)); // tu_name
1883 self.output_symtab_ctx.strsize += @as(u32, @intCast(sf.getOsoPath(self.*).len + 1)); // path
18821884
18831885 for (sf.stabs.items) |stab| {
1884 const sym = stab.getSymbol(macho_file) orelse continue;
1886 const sym = stab.getSymbol(self.*) orelse continue;
18851887 const file = sym.getFile(macho_file).?;
18861888 if (file.getIndex() != self.index) continue;
18871889 if (!sym.flags.output_symtab) continue;
......@@ -2065,7 +2067,7 @@ pub fn writeCompactUnwindRelocatable(self: *Object, macho_file: *MachO) !void {
20652067 }
20662068}
20672069
2068pub fn writeSymtab(self: Object, macho_file: *MachO) void {
2070pub fn writeSymtab(self: Object, macho_file: *MachO, ctx: anytype) void {
20692071 const tracy = trace(@src());
20702072 defer tracy.end();
20712073
......@@ -2075,21 +2077,21 @@ pub fn writeSymtab(self: Object, macho_file: *MachO) void {
20752077 const file = ref.getFile(macho_file) orelse continue;
20762078 if (file.getIndex() != self.index) continue;
20772079 const idx = sym.getOutputSymtabIndex(macho_file) orelse continue;
2078 const out_sym = &macho_file.symtab.items[idx];
2080 const out_sym = &ctx.symtab.items[idx];
20792081 out_sym.n_strx = n_strx;
20802082 sym.setOutputSym(macho_file, out_sym);
20812083 const name = sym.getName(macho_file);
2082 @memcpy(macho_file.strtab.items[n_strx..][0..name.len], name);
2084 @memcpy(ctx.strtab.items[n_strx..][0..name.len], name);
20832085 n_strx += @intCast(name.len);
2084 macho_file.strtab.items[n_strx] = 0;
2086 ctx.strtab.items[n_strx] = 0;
20852087 n_strx += 1;
20862088 }
20872089
20882090 if (macho_file.base.comp.config.debug_format != .strip and self.hasDebugInfo())
2089 try self.writeStabs(n_strx, macho_file);
2091 self.writeStabs(n_strx, macho_file, ctx);
20902092}
20912093
2092pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
2094pub fn writeStabs(self: Object, stroff: u32, macho_file: *MachO, ctx: anytype) void {
20932095 const writeFuncStab = struct {
20942096 inline fn writeFuncStab(
20952097 n_strx: u32,
......@@ -2097,7 +2099,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
20972099 n_value: u64,
20982100 size: u64,
20992101 index: u32,
2100 context: *MachO,
2102 context: anytype,
21012103 ) void {
21022104 context.symtab.items[index] = .{
21032105 .n_strx = 0,
......@@ -2139,7 +2141,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
21392141
21402142 // Open scope
21412143 // N_SO comp_dir
2142 macho_file.symtab.items[index] = .{
2144 ctx.symtab.items[index] = .{
21432145 .n_strx = n_strx,
21442146 .n_type = macho.N_SO,
21452147 .n_sect = 0,
......@@ -2147,9 +2149,9 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
21472149 .n_value = 0,
21482150 };
21492151 index += 1;
2150 @memcpy(macho_file.strtab.items[n_strx..][0..comp_dir.len], comp_dir);
2152 @memcpy(ctx.strtab.items[n_strx..][0..comp_dir.len], comp_dir);
21512153 n_strx += @intCast(comp_dir.len);
2152 macho_file.strtab.items[n_strx] = 0;
2154 ctx.strtab.items[n_strx] = 0;
21532155 n_strx += 1;
21542156 // N_SO tu_name
21552157 macho_file.symtab.items[index] = .{
......@@ -2160,12 +2162,12 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
21602162 .n_value = 0,
21612163 };
21622164 index += 1;
2163 @memcpy(macho_file.strtab.items[n_strx..][0..tu_name.len], tu_name);
2165 @memcpy(ctx.strtab.items[n_strx..][0..tu_name.len], tu_name);
21642166 n_strx += @intCast(tu_name.len);
2165 macho_file.strtab.items[n_strx] = 0;
2167 ctx.strtab.items[n_strx] = 0;
21662168 n_strx += 1;
21672169 // N_OSO path
2168 macho_file.symtab.items[index] = .{
2170 ctx.symtab.items[index] = .{
21692171 .n_strx = n_strx,
21702172 .n_type = macho.N_OSO,
21712173 .n_sect = 0,
......@@ -2174,20 +2176,20 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
21742176 };
21752177 index += 1;
21762178 if (self.in_archive) |ar| {
2177 @memcpy(macho_file.strtab.items[n_strx..][0..ar.path.len], ar.path);
2179 @memcpy(ctx.strtab.items[n_strx..][0..ar.path.len], ar.path);
21782180 n_strx += @intCast(ar.path.len);
2179 macho_file.strtab.items[n_strx] = '(';
2181 ctx.strtab.items[n_strx] = '(';
21802182 n_strx += 1;
2181 @memcpy(macho_file.strtab.items[n_strx..][0..self.path.len], self.path);
2183 @memcpy(ctx.strtab.items[n_strx..][0..self.path.len], self.path);
21822184 n_strx += @intCast(self.path.len);
2183 macho_file.strtab.items[n_strx] = ')';
2185 ctx.strtab.items[n_strx] = ')';
21842186 n_strx += 1;
2185 macho_file.strtab.items[n_strx] = 0;
2187 ctx.strtab.items[n_strx] = 0;
21862188 n_strx += 1;
21872189 } else {
2188 @memcpy(macho_file.strtab.items[n_strx..][0..self.path.len], self.path);
2190 @memcpy(ctx.strtab.items[n_strx..][0..self.path.len], self.path);
21892191 n_strx += @intCast(self.path.len);
2190 macho_file.strtab.items[n_strx] = 0;
2192 ctx.strtab.items[n_strx] = 0;
21912193 n_strx += 1;
21922194 }
21932195
......@@ -2203,17 +2205,17 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
22032205 const sect = macho_file.sections.items(.header)[sym.getOutputSectionIndex(macho_file)];
22042206 const sym_n_strx = n_strx: {
22052207 const symtab_index = sym.getOutputSymtabIndex(macho_file).?;
2206 const osym = macho_file.symtab.items[symtab_index];
2208 const osym = ctx.symtab.items[symtab_index];
22072209 break :n_strx osym.n_strx;
22082210 };
22092211 const sym_n_sect: u8 = if (!sym.flags.abs) @intCast(sym.getOutputSectionIndex(macho_file) + 1) else 0;
22102212 const sym_n_value = sym.getAddress(.{}, macho_file);
22112213 const sym_size = sym.getSize(macho_file);
22122214 if (sect.isCode()) {
2213 writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, macho_file);
2215 writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, ctx);
22142216 index += 4;
22152217 } else if (sym.visibility == .global) {
2216 macho_file.symtab.items[index] = .{
2218 ctx.symtab.items[index] = .{
22172219 .n_strx = sym_n_strx,
22182220 .n_type = macho.N_GSYM,
22192221 .n_sect = sym_n_sect,
......@@ -2222,7 +2224,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
22222224 };
22232225 index += 1;
22242226 } else {
2225 macho_file.symtab.items[index] = .{
2227 ctx.symtab.items[index] = .{
22262228 .n_strx = sym_n_strx,
22272229 .n_type = macho.N_STSYM,
22282230 .n_sect = sym_n_sect,
......@@ -2235,7 +2237,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
22352237
22362238 // Close scope
22372239 // N_SO
2238 macho_file.symtab.items[index] = .{
2240 ctx.symtab.items[index] = .{
22392241 .n_strx = 0,
22402242 .n_type = macho.N_SO,
22412243 .n_sect = 0,
......@@ -2246,13 +2248,13 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
22462248 assert(self.hasSymbolStabs());
22472249
22482250 for (self.stab_files.items) |sf| {
2249 const comp_dir = sf.getCombDir(self);
2251 const comp_dir = sf.getCompDir(self);
22502252 const tu_name = sf.getTuName(self);
22512253 const oso_path = sf.getOsoPath(self);
22522254
22532255 // Open scope
22542256 // N_SO comp_dir
2255 macho_file.symtab.items[index] = .{
2257 ctx.symtab.items[index] = .{
22562258 .n_strx = n_strx,
22572259 .n_type = macho.N_SO,
22582260 .n_sect = 0,
......@@ -2260,12 +2262,12 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
22602262 .n_value = 0,
22612263 };
22622264 index += 1;
2263 @memcpy(macho_file.strtab.items[n_strx..][0..comp_dir.len], comp_dir);
2265 @memcpy(ctx.strtab.items[n_strx..][0..comp_dir.len], comp_dir);
22642266 n_strx += @intCast(comp_dir.len);
2265 macho_file.strtab.items[n_strx] = 0;
2267 ctx.strtab.items[n_strx] = 0;
22662268 n_strx += 1;
22672269 // N_SO tu_name
2268 macho_file.symtab.items[index] = .{
2270 ctx.symtab.items[index] = .{
22692271 .n_strx = n_strx,
22702272 .n_type = macho.N_SO,
22712273 .n_sect = 0,
......@@ -2273,12 +2275,12 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
22732275 .n_value = 0,
22742276 };
22752277 index += 1;
2276 @memcpy(macho_file.strtab.items[n_strx..][0..tu_name.len], tu_name);
2278 @memcpy(ctx.strtab.items[n_strx..][0..tu_name.len], tu_name);
22772279 n_strx += @intCast(tu_name.len);
2278 macho_file.strtab.items[n_strx] = 0;
2280 ctx.strtab.items[n_strx] = 0;
22792281 n_strx += 1;
22802282 // N_OSO path
2281 macho_file.symtab.items[index] = .{
2283 ctx.symtab.items[index] = .{
22822284 .n_strx = n_strx,
22832285 .n_type = macho.N_OSO,
22842286 .n_sect = 0,
......@@ -2286,29 +2288,29 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
22862288 .n_value = sf.getOsoModTime(self),
22872289 };
22882290 index += 1;
2289 @memcpy(macho_file.strtab.items[n_strx..][0..oso_path.len], oso_path);
2291 @memcpy(ctx.strtab.items[n_strx..][0..oso_path.len], oso_path);
22902292 n_strx += @intCast(oso_path.len);
2291 macho_file.strtab.items[n_strx] = 0;
2293 ctx.strtab.items[n_strx] = 0;
22922294 n_strx += 1;
22932295
22942296 for (sf.stabs.items) |stab| {
2295 const sym = stab.getSymbol(macho_file) orelse continue;
2297 const sym = stab.getSymbol(self) orelse continue;
22962298 const file = sym.getFile(macho_file).?;
22972299 if (file.getIndex() != self.index) continue;
22982300 if (!sym.flags.output_symtab) continue;
22992301 const sym_n_strx = n_strx: {
23002302 const symtab_index = sym.getOutputSymtabIndex(macho_file).?;
2301 const osym = macho_file.symtab.items[symtab_index];
2303 const osym = ctx.symtab.items[symtab_index];
23022304 break :n_strx osym.n_strx;
23032305 };
23042306 const sym_n_sect: u8 = if (!sym.flags.abs) @intCast(sym.getOutputSectionIndex(macho_file) + 1) else 0;
23052307 const sym_n_value = sym.getAddress(.{}, macho_file);
23062308 const sym_size = sym.getSize(macho_file);
23072309 if (stab.is_func) {
2308 writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, macho_file);
2310 writeFuncStab(sym_n_strx, sym_n_sect, sym_n_value, sym_size, index, ctx);
23092311 index += 4;
23102312 } else if (sym.visibility == .global) {
2311 macho_file.symtab.items[index] = .{
2313 ctx.symtab.items[index] = .{
23122314 .n_strx = sym_n_strx,
23132315 .n_type = macho.N_GSYM,
23142316 .n_sect = sym_n_sect,
......@@ -2317,7 +2319,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
23172319 };
23182320 index += 1;
23192321 } else {
2320 macho_file.symtab.items[index] = .{
2322 ctx.symtab.items[index] = .{
23212323 .n_strx = sym_n_strx,
23222324 .n_type = macho.N_STSYM,
23232325 .n_sect = sym_n_sect,
......@@ -2330,7 +2332,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
23302332
23312333 // Close scope
23322334 // N_SO
2333 macho_file.symtab.items[index] = .{
2335 ctx.symtab.items[index] = .{
23342336 .n_strx = 0,
23352337 .n_type = macho.N_SO,
23362338 .n_sect = 0,
......@@ -2343,7 +2345,7 @@ pub fn writeStabs(self: *const Object, stroff: u32, macho_file: *MachO) void {
23432345}
23442346
23452347pub fn getAtomRelocs(self: *const Object, atom: Atom, macho_file: *MachO) []const Relocation {
2346 const extra = atom.getExtra(macho_file).?;
2348 const extra = atom.getExtra(macho_file);
23472349 const relocs = self.sections.items(.relocs)[atom.n_sect];
23482350 return relocs.items[extra.rel_index..][0..extra.rel_count];
23492351}
......@@ -2402,7 +2404,7 @@ pub fn asFile(self: *Object) File {
24022404}
24032405
24042406const AddAtomArgs = struct {
2405 name: MachO.String,
2407 name: u32,
24062408 n_sect: u8,
24072409 off: u64,
24082410 size: u64,
......@@ -2420,7 +2422,7 @@ fn addAtom(self: *Object, allocator: Allocator, args: AddAtomArgs) !Atom.Index {
24202422 .size = args.size,
24212423 .off = args.off,
24222424 .extra = try self.addAtomExtra(allocator, .{}),
2423 .alignment = args.alignment,
2425 .alignment = Atom.Alignment.fromLog2Units(args.alignment),
24242426 };
24252427 return atom_index;
24262428}
......@@ -2693,12 +2695,12 @@ fn formatSymtab(
26932695 }
26942696 for (object.stab_files.items) |sf| {
26952697 try writer.print(" stabs({s},{s},{s})\n", .{
2696 sf.getCompDir(object),
2697 sf.getTuName(object),
2698 sf.getOsoPath(object),
2698 sf.getCompDir(object.*),
2699 sf.getTuName(object.*),
2700 sf.getOsoPath(object.*),
26992701 });
27002702 for (sf.stabs.items) |stab| {
2701 try writer.print(" {}", .{stab.fmt(object)});
2703 try writer.print(" {}", .{stab.fmt(object.*)});
27022704 }
27032705 }
27042706}
......@@ -2744,33 +2746,33 @@ const StabFile = struct {
27442746 comp_dir: u32,
27452747 stabs: std.ArrayListUnmanaged(Stab) = .{},
27462748
2747 fn getCompDir(sf: StabFile, object: *const Object) [:0]const u8 {
2749 fn getCompDir(sf: StabFile, object: Object) [:0]const u8 {
27482750 const nlist = object.symtab.items(.nlist)[sf.comp_dir];
27492751 return object.getString(nlist.n_strx);
27502752 }
27512753
2752 fn getTuName(sf: StabFile, object: *const Object) [:0]const u8 {
2754 fn getTuName(sf: StabFile, object: Object) [:0]const u8 {
27532755 const nlist = object.symtab.items(.nlist)[sf.comp_dir + 1];
27542756 return object.getString(nlist.n_strx);
27552757 }
27562758
2757 fn getOsoPath(sf: StabFile, object: *const Object) [:0]const u8 {
2759 fn getOsoPath(sf: StabFile, object: Object) [:0]const u8 {
27582760 const nlist = object.symtab.items(.nlist)[sf.comp_dir + 2];
27592761 return object.getString(nlist.n_strx);
27602762 }
27612763
2762 fn getOsoModTime(sf: StabFile, object: *const Object) u64 {
2764 fn getOsoModTime(sf: StabFile, object: Object) u64 {
27632765 const nlist = object.symtab.items(.nlist)[sf.comp_dir + 2];
27642766 return nlist.n_value;
27652767 }
27662768
27672769 const Stab = struct {
27682770 is_func: bool = true,
2769 symbol: ?Symbol.Index = null,
2771 index: ?Symbol.Index = null,
27702772
2771 fn getSymbol(stab: Stab, object: *const Object) ?*Symbol {
2773 fn getSymbol(stab: Stab, object: Object) ?Symbol {
27722774 const index = stab.index orelse return null;
2773 return &object.symbols.items[index];
2775 return object.symbols.items[index];
27742776 }
27752777
27762778 pub fn format(
......@@ -2786,9 +2788,9 @@ const StabFile = struct {
27862788 @compileError("do not format stabs directly");
27872789 }
27882790
2789 const StabFormatContext = struct { Stab, *const Object };
2791 const StabFormatContext = struct { Stab, Object };
27902792
2791 pub fn fmt(stab: Stab, object: *const Object) std.fmt.Formatter(format2) {
2793 pub fn fmt(stab: Stab, object: Object) std.fmt.Formatter(format2) {
27922794 return .{ .data = .{ stab, object } };
27932795 }
27942796
......@@ -2817,11 +2819,11 @@ const CompileUnit = struct {
28172819 comp_dir: u32,
28182820 tu_name: u32,
28192821
2820 fn getCompDir(cu: CompileUnit, object: *const Object) [:0]const u8 {
2822 fn getCompDir(cu: CompileUnit, object: Object) [:0]const u8 {
28212823 return object.getString(cu.comp_dir);
28222824 }
28232825
2824 fn getTuName(cu: CompileUnit, object: *const Object) [:0]const u8 {
2826 fn getTuName(cu: CompileUnit, object: Object) [:0]const u8 {
28252827 return object.getString(cu.tu_name);
28262828 }
28272829};
......@@ -2840,15 +2842,14 @@ const CompactUnwindCtx = struct {
28402842
28412843const x86_64 = struct {
28422844 fn parseRelocs(
2843 self: *const Object,
2844 n_sect: u8,
2845 self: *Object,
28452846 sect: macho.section_64,
28462847 out: *std.ArrayListUnmanaged(Relocation),
2848 handle: File.Handle,
28472849 macho_file: *MachO,
28482850 ) !void {
28492851 const gpa = macho_file.base.comp.gpa;
28502852
2851 const handle = macho_file.getFileHandle(self.file_handle);
28522853 const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info));
28532854 defer gpa.free(relocs_buffer);
28542855 {
......@@ -2857,8 +2858,12 @@ const x86_64 = struct {
28572858 }
28582859 const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc];
28592860
2860 const code = try self.getSectionData(@intCast(n_sect), macho_file);
2861 const code = try gpa.alloc(u8, sect.size);
28612862 defer gpa.free(code);
2863 {
2864 const amt = try handle.preadAll(code, sect.offset + self.offset);
2865 if (amt != code.len) return error.InputOutput;
2866 }
28622867
28632868 try out.ensureTotalCapacityPrecise(gpa, relocs.len);
28642869
......@@ -2880,8 +2885,9 @@ const x86_64 = struct {
28802885 .X86_64_RELOC_SIGNED_4 => 4,
28812886 else => 0,
28822887 };
2888 var is_extern = rel.r_extern == 1;
28832889
2884 const target = if (rel.r_extern == 0) blk: {
2890 const target = if (!is_extern) blk: {
28852891 const nsect = rel.r_symbolnum - 1;
28862892 const taddr: i64 = if (rel.r_pcrel == 1)
28872893 @as(i64, @intCast(sect.addr)) + rel.r_address + addend + 4
......@@ -2893,9 +2899,15 @@ const x86_64 = struct {
28932899 });
28942900 return error.MalformedObject;
28952901 };
2896 addend = taddr - @as(i64, @intCast(macho_file.getAtom(target).?.getInputAddress(macho_file)));
2902 const target_atom = self.getAtom(target).?;
2903 addend = taddr - @as(i64, @intCast(target_atom.getInputAddress(macho_file)));
2904 const isec = target_atom.getInputSection(macho_file);
2905 if (isCstringLiteral(isec) or isFixedSizeLiteral(isec) or isPtrLiteral(isec)) {
2906 is_extern = true;
2907 break :blk target_atom.getExtra(macho_file).literal_symbol_index;
2908 }
28972909 break :blk target;
2898 } else self.symbols.items[rel.r_symbolnum];
2910 } else rel.r_symbolnum;
28992911
29002912 const has_subtractor = if (i > 0 and
29012913 @as(macho.reloc_type_x86_64, @enumFromInt(relocs[i - 1].r_type)) == .X86_64_RELOC_SUBTRACTOR)
......@@ -2909,7 +2921,7 @@ const x86_64 = struct {
29092921 break :blk true;
29102922 } else false;
29112923
2912 const @"type": Relocation.Type = validateRelocType(rel, rel_type) catch |err| {
2924 const @"type": Relocation.Type = validateRelocType(rel, rel_type, is_extern) catch |err| {
29132925 switch (err) {
29142926 error.Pcrel => try macho_file.reportParseError2(
29152927 self.index,
......@@ -2936,7 +2948,7 @@ const x86_64 = struct {
29362948 };
29372949
29382950 out.appendAssumeCapacity(.{
2939 .tag = if (rel.r_extern == 1) .@"extern" else .local,
2951 .tag = if (is_extern) .@"extern" else .local,
29402952 .offset = @as(u32, @intCast(rel.r_address)),
29412953 .target = target,
29422954 .addend = addend,
......@@ -2951,7 +2963,7 @@ const x86_64 = struct {
29512963 }
29522964 }
29532965
2954 fn validateRelocType(rel: macho.relocation_info, rel_type: macho.reloc_type_x86_64) !Relocation.Type {
2966 fn validateRelocType(rel: macho.relocation_info, rel_type: macho.reloc_type_x86_64, is_extern: bool) !Relocation.Type {
29552967 switch (rel_type) {
29562968 .X86_64_RELOC_UNSIGNED => {
29572969 if (rel.r_pcrel == 1) return error.Pcrel;
......@@ -2971,7 +2983,7 @@ const x86_64 = struct {
29712983 => {
29722984 if (rel.r_pcrel == 0) return error.NonPcrel;
29732985 if (rel.r_length != 2) return error.InvalidLength;
2974 if (rel.r_extern == 0) return error.NonExtern;
2986 if (!is_extern) return error.NonExtern;
29752987 return switch (rel_type) {
29762988 .X86_64_RELOC_BRANCH => .branch,
29772989 .X86_64_RELOC_GOT_LOAD => .got_load,
......@@ -3002,15 +3014,14 @@ const x86_64 = struct {
30023014
30033015const aarch64 = struct {
30043016 fn parseRelocs(
3005 self: *const Object,
3006 n_sect: u8,
3017 self: *Object,
30073018 sect: macho.section_64,
30083019 out: *std.ArrayListUnmanaged(Relocation),
3020 handle: File.Handle,
30093021 macho_file: *MachO,
30103022 ) !void {
30113023 const gpa = macho_file.base.comp.gpa;
30123024
3013 const handle = macho_file.getFileHandle(self.file_handle);
30143025 const relocs_buffer = try gpa.alloc(u8, sect.nreloc * @sizeOf(macho.relocation_info));
30153026 defer gpa.free(relocs_buffer);
30163027 {
......@@ -3019,8 +3030,12 @@ const aarch64 = struct {
30193030 }
30203031 const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc];
30213032
3022 const code = try self.getSectionData(@intCast(n_sect), macho_file);
3033 const code = try gpa.alloc(u8, sect.size);
30233034 defer gpa.free(code);
3035 {
3036 const amt = try handle.preadAll(code, sect.offset + self.offset);
3037 if (amt != code.len) return error.InputOutput;
3038 }
30243039
30253040 try out.ensureTotalCapacityPrecise(gpa, relocs.len);
30263041
......@@ -3066,8 +3081,9 @@ const aarch64 = struct {
30663081 }
30673082
30683083 const rel_type: macho.reloc_type_arm64 = @enumFromInt(rel.r_type);
3084 var is_extern = rel.r_extern == 1;
30693085
3070 const target = if (rel.r_extern == 0) blk: {
3086 const target = if (!is_extern) blk: {
30713087 const nsect = rel.r_symbolnum - 1;
30723088 const taddr: i64 = if (rel.r_pcrel == 1)
30733089 @as(i64, @intCast(sect.addr)) + rel.r_address + addend
......@@ -3079,9 +3095,15 @@ const aarch64 = struct {
30793095 });
30803096 return error.MalformedObject;
30813097 };
3082 addend = taddr - @as(i64, @intCast(macho_file.getAtom(target).?.getInputAddress(macho_file)));
3098 const target_atom = self.getAtom(target).?;
3099 addend = taddr - @as(i64, @intCast(target_atom.getInputAddress(macho_file)));
3100 const isec = target_atom.getInputSection(macho_file);
3101 if (isCstringLiteral(isec) or isFixedSizeLiteral(isec) or isPtrLiteral(isec)) {
3102 is_extern = true;
3103 break :blk target_atom.getExtra(macho_file).literal_symbol_index;
3104 }
30833105 break :blk target;
3084 } else self.symbols.items[rel.r_symbolnum];
3106 } else rel.r_symbolnum;
30853107
30863108 const has_subtractor = if (i > 0 and
30873109 @as(macho.reloc_type_arm64, @enumFromInt(relocs[i - 1].r_type)) == .ARM64_RELOC_SUBTRACTOR)
......@@ -3095,7 +3117,7 @@ const aarch64 = struct {
30953117 break :blk true;
30963118 } else false;
30973119
3098 const @"type": Relocation.Type = validateRelocType(rel, rel_type) catch |err| {
3120 const @"type": Relocation.Type = validateRelocType(rel, rel_type, is_extern) catch |err| {
30993121 switch (err) {
31003122 error.Pcrel => try macho_file.reportParseError2(
31013123 self.index,
......@@ -3122,7 +3144,7 @@ const aarch64 = struct {
31223144 };
31233145
31243146 out.appendAssumeCapacity(.{
3125 .tag = if (rel.r_extern == 1) .@"extern" else .local,
3147 .tag = if (is_extern) .@"extern" else .local,
31263148 .offset = @as(u32, @intCast(rel.r_address)),
31273149 .target = target,
31283150 .addend = addend,
......@@ -3137,7 +3159,7 @@ const aarch64 = struct {
31373159 }
31383160 }
31393161
3140 fn validateRelocType(rel: macho.relocation_info, rel_type: macho.reloc_type_arm64) !Relocation.Type {
3162 fn validateRelocType(rel: macho.relocation_info, rel_type: macho.reloc_type_arm64, is_extern: bool) !Relocation.Type {
31413163 switch (rel_type) {
31423164 .ARM64_RELOC_UNSIGNED => {
31433165 if (rel.r_pcrel == 1) return error.Pcrel;
......@@ -3158,7 +3180,7 @@ const aarch64 = struct {
31583180 => {
31593181 if (rel.r_pcrel == 0) return error.NonPcrel;
31603182 if (rel.r_length != 2) return error.InvalidLength;
3161 if (rel.r_extern == 0) return error.NonExtern;
3183 if (!is_extern) return error.NonExtern;
31623184 return switch (rel_type) {
31633185 .ARM64_RELOC_BRANCH26 => .branch,
31643186 .ARM64_RELOC_PAGE21 => .page,
......@@ -3175,7 +3197,7 @@ const aarch64 = struct {
31753197 => {
31763198 if (rel.r_pcrel == 1) return error.Pcrel;
31773199 if (rel.r_length != 2) return error.InvalidLength;
3178 if (rel.r_extern == 0) return error.NonExtern;
3200 if (!is_extern) return error.NonExtern;
31793201 return switch (rel_type) {
31803202 .ARM64_RELOC_PAGEOFF12 => .pageoff,
31813203 .ARM64_RELOC_GOT_LOAD_PAGEOFF12 => .got_load_pageoff,
src/link/MachO/Symbol.zig+2-2
......@@ -150,7 +150,7 @@ pub fn getObjcSelrefsAddress(symbol: Symbol, macho_file: *MachO) u64 {
150150 const file = symbol.getFile(macho_file).?;
151151 return switch (file) {
152152 .dylib, .zig_object => unreachable,
153 .object, .internal => |x| x.symbols.items[extra.objc_selrefs].getAddress(.{}, macho_file),
153 inline else => |x| x.symbols.items[extra.objc_selrefs].getAddress(.{}, macho_file),
154154 };
155155}
156156
......@@ -186,7 +186,7 @@ pub fn getOutputSymtabIndex(symbol: Symbol, macho_file: *MachO) ?u32 {
186186 const symtab_ctx = switch (file) {
187187 inline else => |x| x.output_symtab_ctx,
188188 };
189 var idx = symbol.getExtra(macho_file).?.symtab;
189 var idx = symbol.getExtra(macho_file).symtab;
190190 if (symbol.isLocal()) {
191191 idx += symtab_ctx.ilocal;
192192 } else if (symbol.flags.@"export") {
src/link/MachO/UnwindInfo.zig+2-2
......@@ -473,11 +473,11 @@ pub const Record = struct {
473473 }
474474
475475 pub fn getAtom(rec: Record, macho_file: *MachO) *Atom {
476 return macho_file.getAtom(rec.atom).?;
476 return rec.getObject(macho_file).getAtom(rec.atom).?;
477477 }
478478
479479 pub fn getLsdaAtom(rec: Record, macho_file: *MachO) ?*Atom {
480 return macho_file.getAtom(rec.lsda);
480 return rec.getObject(macho_file).getAtom(rec.lsda);
481481 }
482482
483483 pub fn getPersonality(rec: Record, macho_file: *MachO) ?*Symbol {
src/link/MachO/ZigObject.zig+35-5
......@@ -343,6 +343,36 @@ pub fn writeAr(self: ZigObject, ar_format: Archive.Format, writer: anytype) !voi
343343 try writer.writeAll(self.data.items);
344344}
345345
346pub fn claimUnresolved(self: *ZigObject, macho_file: *MachO) void {
347 const tracy = trace(@src());
348 defer tracy.end();
349
350 for (self.symbols.items, 0..) |*sym, i| {
351 const nlist = self.symtab.items(.nlist)[i];
352 if (!nlist.ext()) continue;
353 if (!nlist.undf()) continue;
354
355 if (self.getSymbolRef(@intCast(i), macho_file).getFile(macho_file) != null) continue;
356
357 const is_import = switch (macho_file.undefined_treatment) {
358 .@"error" => false,
359 .warn, .suppress => nlist.weakRef(),
360 .dynamic_lookup => true,
361 };
362 if (is_import) {
363 sym.value = 0;
364 sym.atom_ref = .{ .index = 0, .file = 0 };
365 sym.flags.weak = false;
366 sym.flags.weak_ref = nlist.weakRef();
367 sym.flags.import = is_import;
368 sym.visibility = .global;
369
370 const idx = self.globals.items[i];
371 macho_file.resolver.values.items[idx - 1] = .{ .index = @intCast(i), .file = self.index };
372 }
373 }
374}
375
346376pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void {
347377 for (self.getAtoms()) |atom_index| {
348378 const atom = self.getAtom(atom_index) orelse continue;
......@@ -378,7 +408,7 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void {
378408 }
379409}
380410
381pub fn writeSymtab(self: ZigObject, macho_file: *MachO) void {
411pub fn writeSymtab(self: ZigObject, macho_file: *MachO, ctx: anytype) void {
382412 const tracy = trace(@src());
383413 defer tracy.end();
384414
......@@ -388,13 +418,13 @@ pub fn writeSymtab(self: ZigObject, macho_file: *MachO) void {
388418 const file = ref.getFile(macho_file) orelse continue;
389419 if (file.getIndex() != self.index) continue;
390420 const idx = sym.getOutputSymtabIndex(macho_file) orelse continue;
391 const out_sym = &macho_file.symtab.items[idx];
421 const out_sym = &ctx.symtab.items[idx];
392422 out_sym.n_strx = n_strx;
393423 sym.setOutputSym(macho_file, out_sym);
394424 const name = sym.getName(macho_file);
395 @memcpy(macho_file.strtab.items[n_strx..][0..name.len], name);
425 @memcpy(ctx.strtab.items[n_strx..][0..name.len], name);
396426 n_strx += @intCast(name.len);
397 macho_file.strtab.items[n_strx] = 0;
427 ctx.strtab.items[n_strx] = 0;
398428 n_strx += 1;
399429 }
400430}
......@@ -1098,7 +1128,7 @@ pub fn lowerUnnamedConst(
10981128 },
10991129 };
11001130 const sym = self.symbols.items[sym_index];
1101 try unnamed_consts.append(gpa, sym.atom);
1131 try unnamed_consts.append(gpa, sym.atom_ref.index);
11021132 return sym_index;
11031133}
11041134
src/link/MachO/dyld_info/Rebase.zig+5-5
......@@ -34,7 +34,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void {
3434 for (objects.items) |index| {
3535 const file = macho_file.getFile(index).?;
3636 for (file.getAtoms()) |atom_index| {
37 const atom = macho_file.getAtom(atom_index) orelse continue;
37 const atom = file.getAtom(atom_index) orelse continue;
3838 if (!atom.flags.alive) continue;
3939 if (atom.getInputSection(macho_file).isZerofill()) continue;
4040 const atom_addr = atom.getAddress(macho_file);
......@@ -43,7 +43,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void {
4343 for (atom.getRelocs(macho_file)) |rel| {
4444 if (rel.type != .unsigned or rel.meta.length != 3) continue;
4545 if (rel.tag == .@"extern") {
46 const sym = rel.getTargetSymbol(macho_file);
46 const sym = rel.getTargetSymbol(atom.*, macho_file);
4747 if (sym.isTlvInit(macho_file)) continue;
4848 if (sym.flags.import) continue;
4949 }
......@@ -72,7 +72,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void {
7272 const seg_id = macho_file.sections.items(.segment_id)[sid];
7373 const seg = macho_file.segments.items[seg_id];
7474 for (macho_file.got.symbols.items, 0..) |ref, idx| {
75 const sym = macho_file.getSymbol(ref);
75 const sym = ref.getSymbol(macho_file).?;
7676 const addr = macho_file.got.getAddress(@intCast(idx), macho_file);
7777 if (!sym.flags.import) {
7878 try rebase.entries.append(gpa, .{
......@@ -88,7 +88,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void {
8888 const seg_id = macho_file.sections.items(.segment_id)[sid];
8989 const seg = macho_file.segments.items[seg_id];
9090 for (macho_file.stubs.symbols.items, 0..) |ref, idx| {
91 const sym = macho_file.getSymbol(ref);
91 const sym = ref.getSymbol(macho_file).?;
9292 const addr = sect.addr + idx * @sizeOf(u64);
9393 const rebase_entry = Rebase.Entry{
9494 .offset = addr - seg.vmaddr,
......@@ -104,7 +104,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void {
104104 const seg_id = macho_file.sections.items(.segment_id)[sid];
105105 const seg = macho_file.segments.items[seg_id];
106106 for (macho_file.tlv_ptr.symbols.items, 0..) |ref, idx| {
107 const sym = macho_file.getSymbol(ref);
107 const sym = ref.getSymbol(macho_file).?;
108108 const addr = macho_file.tlv_ptr.getAddress(@intCast(idx), macho_file);
109109 if (!sym.flags.import) {
110110 try rebase.entries.append(gpa, .{
src/link/MachO/dyld_info/Trie.zig+22-24
......@@ -94,33 +94,31 @@ pub fn updateSize(self: *Trie, macho_file: *MachO) !void {
9494 const gpa = macho_file.base.comp.gpa;
9595
9696 try self.init(gpa);
97 // TODO
98 // try self.nodes.ensureUnusedCapacity(gpa, macho_file.resolver.values.items.len * 2);
99 // try self.edges.ensureUnusedCapacity(gpa, macho_file.resolver.values.items.len * 2);
97 try self.nodes.ensureUnusedCapacity(gpa, macho_file.resolver.values.items.len * 2);
98 try self.edges.ensureUnusedCapacity(gpa, macho_file.resolver.values.items.len * 2);
10099
101100 const seg = macho_file.getTextSegment();
102 for (macho_file.objects.items) |index| {
103 for (macho_file.getFile(index).?.getSymbols()) |ref| {
104 const sym = macho_file.getSymbol(ref);
105 if (!sym.flags.@"export") continue;
106 if (sym.getAtom(macho_file)) |atom| if (!atom.flags.alive) continue;
107 var flags: u64 = if (sym.flags.abs)
108 macho.EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
109 else if (sym.flags.tlv)
110 macho.EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL
111 else
112 macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR;
113 if (sym.flags.weak) {
114 flags |= macho.EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
115 macho_file.weak_defines = true;
116 macho_file.binds_to_weak = true;
117 }
118 try self.put(gpa, .{
119 .name = sym.getName(macho_file),
120 .vmaddr_offset = sym.getAddress(.{ .stubs = false }, macho_file) - seg.vmaddr,
121 .export_flags = flags,
122 });
101 for (macho_file.resolver.values.items) |ref| {
102 if (ref.getFile(macho_file) == null) continue;
103 const sym = ref.getSymbol(macho_file).?;
104 if (!sym.flags.@"export") continue;
105 if (sym.getAtom(macho_file)) |atom| if (!atom.flags.alive) continue;
106 var flags: u64 = if (sym.flags.abs)
107 macho.EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
108 else if (sym.flags.tlv)
109 macho.EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL
110 else
111 macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR;
112 if (sym.flags.weak) {
113 flags |= macho.EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
114 macho_file.weak_defines = true;
115 macho_file.binds_to_weak = true;
123116 }
117 try self.put(gpa, .{
118 .name = sym.getName(macho_file),
119 .vmaddr_offset = sym.getAddress(.{ .stubs = false }, macho_file) - seg.vmaddr,
120 .export_flags = flags,
121 });
124122 }
125123
126124 try self.finalize(gpa);
src/link/MachO/dyld_info/bind.zig+27-25
......@@ -1,17 +1,19 @@
11pub const Entry = struct {
2 target: Symbol.Index,
2 target: MachO.Ref,
33 offset: u64,
44 segment_id: u8,
55 addend: i64,
66
77 pub fn lessThan(ctx: *MachO, entry: Entry, other: Entry) bool {
8 _ = ctx;
89 if (entry.segment_id == other.segment_id) {
9 if (entry.target == other.target) {
10 if (entry.target.eql(other.target)) {
1011 return entry.offset < other.offset;
1112 }
12 const entry_name = ctx.getSymbol(entry.target).getName(ctx);
13 const other_name = ctx.getSymbol(other.target).getName(ctx);
14 return std.mem.lessThan(u8, entry_name, other_name);
13 if (entry.target.file == other.target.file) {
14 return entry.target.index < other.target.index;
15 }
16 return entry.target.file < other.target.file;
1517 }
1618 return entry.segment_id < other.segment_id;
1719 }
......@@ -44,7 +46,7 @@ pub const Bind = struct {
4446 for (objects.items) |index| {
4547 const file = macho_file.getFile(index).?;
4648 for (file.getAtoms()) |atom_index| {
47 const atom = macho_file.getAtom(atom_index) orelse continue;
49 const atom = file.getAtom(atom_index) orelse continue;
4850 if (!atom.flags.alive) continue;
4951 if (atom.getInputSection(macho_file).isZerofill()) continue;
5052 const atom_addr = atom.getAddress(macho_file);
......@@ -55,10 +57,10 @@ pub const Bind = struct {
5557 if (rel.type != .unsigned or rel.meta.length != 3 or rel.tag != .@"extern") continue;
5658 const rel_offset = rel.offset - atom.off;
5759 const addend = rel.addend + rel.getRelocAddend(cpu_arch);
58 const sym = rel.getTargetSymbol(macho_file);
60 const sym = rel.getTargetSymbol(atom.*, macho_file);
5961 if (sym.isTlvInit(macho_file)) continue;
6062 const entry = Entry{
61 .target = rel.target,
63 .target = rel.getTargetSymbolRef(atom.*, macho_file),
6264 .offset = atom_addr + rel_offset - seg.vmaddr,
6365 .segment_id = seg_id,
6466 .addend = addend,
......@@ -74,7 +76,7 @@ pub const Bind = struct {
7476 const seg_id = macho_file.sections.items(.segment_id)[sid];
7577 const seg = macho_file.segments.items[seg_id];
7678 for (macho_file.got.symbols.items, 0..) |ref, idx| {
77 const sym = macho_file.getSymbol(ref);
79 const sym = ref.getSymbol(macho_file).?;
7880 const addr = macho_file.got.getAddress(@intCast(idx), macho_file);
7981 const entry = Entry{
8082 .target = ref,
......@@ -93,7 +95,7 @@ pub const Bind = struct {
9395 const seg_id = macho_file.sections.items(.segment_id)[sid];
9496 const seg = macho_file.segments.items[seg_id];
9597 for (macho_file.stubs.symbols.items, 0..) |ref, idx| {
96 const sym = macho_file.getSymbol(ref);
98 const sym = ref.getSymbol(macho_file).?;
9799 const addr = sect.addr + idx * @sizeOf(u64);
98100 const bind_entry = Entry{
99101 .target = ref,
......@@ -112,7 +114,7 @@ pub const Bind = struct {
112114 const seg = macho_file.segments.items[seg_id];
113115
114116 for (macho_file.tlv_ptr.symbols.items, 0..) |ref, idx| {
115 const sym = macho_file.getSymbol(ref);
117 const sym = ref.getSymbol(macho_file).?;
116118 const addr = macho_file.tlv_ptr.getAddress(@intCast(idx), macho_file);
117119 const entry = Entry{
118120 .target = ref,
......@@ -162,7 +164,7 @@ pub const Bind = struct {
162164 var addend: i64 = 0;
163165 var count: usize = 0;
164166 var skip: u64 = 0;
165 var target: ?Symbol.Index = null;
167 var target: ?MachO.Ref = null;
166168
167169 var state: enum {
168170 start,
......@@ -173,7 +175,7 @@ pub const Bind = struct {
173175 var i: usize = 0;
174176 while (i < entries.len) : (i += 1) {
175177 const current = entries[i];
176 if (target == null or target.? != current.target) {
178 if (target == null or !target.?.eql(current.target)) {
177179 switch (state) {
178180 .start => {},
179181 .bind_single => try doBind(writer),
......@@ -182,7 +184,7 @@ pub const Bind = struct {
182184 state = .start;
183185 target = current.target;
184186
185 const sym = ctx.getSymbol(current.target);
187 const sym = current.target.getSymbol(ctx).?;
186188 const name = sym.getName(ctx);
187189 const flags: u8 = if (sym.weakRef(ctx)) macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT else 0;
188190 const ordinal: i16 = ord: {
......@@ -296,7 +298,7 @@ pub const WeakBind = struct {
296298 for (objects.items) |index| {
297299 const file = macho_file.getFile(index).?;
298300 for (file.getAtoms()) |atom_index| {
299 const atom = macho_file.getAtom(atom_index) orelse continue;
301 const atom = file.getAtom(atom_index) orelse continue;
300302 if (!atom.flags.alive) continue;
301303 if (atom.getInputSection(macho_file).isZerofill()) continue;
302304 const atom_addr = atom.getAddress(macho_file);
......@@ -307,10 +309,10 @@ pub const WeakBind = struct {
307309 if (rel.type != .unsigned or rel.meta.length != 3 or rel.tag != .@"extern") continue;
308310 const rel_offset = rel.offset - atom.off;
309311 const addend = rel.addend + rel.getRelocAddend(cpu_arch);
310 const sym = rel.getTargetSymbol(macho_file);
312 const sym = rel.getTargetSymbol(atom.*, macho_file);
311313 if (sym.isTlvInit(macho_file)) continue;
312314 const entry = Entry{
313 .target = rel.target,
315 .target = rel.getTargetSymbolRef(atom.*, macho_file),
314316 .offset = atom_addr + rel_offset - seg.vmaddr,
315317 .segment_id = seg_id,
316318 .addend = addend,
......@@ -326,7 +328,7 @@ pub const WeakBind = struct {
326328 const seg_id = macho_file.sections.items(.segment_id)[sid];
327329 const seg = macho_file.segments.items[seg_id];
328330 for (macho_file.got.symbols.items, 0..) |ref, idx| {
329 const sym = macho_file.getSymbol(ref);
331 const sym = ref.getSymbol(macho_file).?;
330332 const addr = macho_file.got.getAddress(@intCast(idx), macho_file);
331333 const entry = Entry{
332334 .target = ref,
......@@ -346,7 +348,7 @@ pub const WeakBind = struct {
346348 const seg = macho_file.segments.items[seg_id];
347349
348350 for (macho_file.stubs.symbols.items, 0..) |ref, idx| {
349 const sym = macho_file.getSymbol(ref);
351 const sym = ref.getSymbol(macho_file).?;
350352 const addr = sect.addr + idx * @sizeOf(u64);
351353 const bind_entry = Entry{
352354 .target = ref,
......@@ -365,7 +367,7 @@ pub const WeakBind = struct {
365367 const seg = macho_file.segments.items[seg_id];
366368
367369 for (macho_file.tlv_ptr.symbols.items, 0..) |ref, idx| {
368 const sym = macho_file.getSymbol(ref);
370 const sym = ref.getSymbol(macho_file).?;
369371 const addr = macho_file.tlv_ptr.getAddress(@intCast(idx), macho_file);
370372 const entry = Entry{
371373 .target = ref,
......@@ -415,7 +417,7 @@ pub const WeakBind = struct {
415417 var addend: i64 = 0;
416418 var count: usize = 0;
417419 var skip: u64 = 0;
418 var target: ?Symbol.Index = null;
420 var target: ?MachO.Ref = null;
419421
420422 var state: enum {
421423 start,
......@@ -426,7 +428,7 @@ pub const WeakBind = struct {
426428 var i: usize = 0;
427429 while (i < entries.len) : (i += 1) {
428430 const current = entries[i];
429 if (target == null or target.? != current.target) {
431 if (target == null or !target.?.eql(current.target)) {
430432 switch (state) {
431433 .start => {},
432434 .bind_single => try doBind(writer),
......@@ -435,7 +437,7 @@ pub const WeakBind = struct {
435437 state = .start;
436438 target = current.target;
437439
438 const sym = ctx.getSymbol(current.target);
440 const sym = current.target.getSymbol(ctx).?;
439441 const name = sym.getName(ctx);
440442 const flags: u8 = 0; // TODO NON_WEAK_DEFINITION
441443
......@@ -536,7 +538,7 @@ pub const LazyBind = struct {
536538 const seg = macho_file.segments.items[seg_id];
537539
538540 for (macho_file.stubs.symbols.items, 0..) |ref, idx| {
539 const sym = macho_file.getSymbol(ref);
541 const sym = ref.getSymbol(macho_file).?;
540542 const addr = sect.addr + idx * @sizeOf(u64);
541543 const bind_entry = Entry{
542544 .target = ref,
......@@ -565,7 +567,7 @@ pub const LazyBind = struct {
565567 for (self.entries.items) |entry| {
566568 self.offsets.appendAssumeCapacity(@intCast(self.buffer.items.len));
567569
568 const sym = ctx.getSymbol(entry.target);
570 const sym = entry.target.getSymbol(ctx).?;
569571 const name = sym.getName(ctx);
570572 const flags: u8 = if (sym.weakRef(ctx)) macho.BIND_SYMBOL_FLAGS_WEAK_IMPORT else 0;
571573 const ordinal: i16 = ord: {
src/link/MachO/file.zig+11-11
......@@ -30,10 +30,10 @@ pub const File = union(enum) {
3030 }
3131 }
3232
33 pub fn resolveSymbols(file: File, macho_file: *MachO) void {
34 switch (file) {
33 pub fn resolveSymbols(file: File, macho_file: *MachO) !void {
34 return switch (file) {
3535 inline else => |x| x.resolveSymbols(macho_file),
36 }
36 };
3737 }
3838
3939 pub fn scanRelocs(file: File, macho_file: *MachO) !void {
......@@ -147,19 +147,19 @@ pub const File = union(enum) {
147147 if (ref.getFile(macho_file) == null) continue;
148148 if (ref.file != file.getIndex()) continue;
149149 const sym = ref.getSymbol(macho_file).?;
150 if (sym.getSectionFlags().got) {
150 if (sym.flags.needs_got) {
151151 log.debug("'{s}' needs GOT", .{sym.getName(macho_file)});
152152 try macho_file.got.addSymbol(ref, macho_file);
153153 }
154 if (sym.getSectionFlags().stubs) {
154 if (sym.flags.stubs) {
155155 log.debug("'{s}' needs STUBS", .{sym.getName(macho_file)});
156156 try macho_file.stubs.addSymbol(ref, macho_file);
157157 }
158 if (sym.getSectionFlags().tlv_ptr) {
158 if (sym.flags.tlv_ptr) {
159159 log.debug("'{s}' needs TLV pointer", .{sym.getName(macho_file)});
160160 try macho_file.tlv_ptr.addSymbol(ref, macho_file);
161161 }
162 if (sym.getSectionFlags().objc_stubs) {
162 if (sym.flags.objc_stubs) {
163163 log.debug("'{s}' needs OBJC STUBS", .{sym.getName(macho_file)});
164164 try macho_file.objc_stubs.addSymbol(ref, macho_file);
165165 }
......@@ -171,7 +171,7 @@ pub const File = union(enum) {
171171 defer tracy.end();
172172 for (file.getAtoms()) |atom_index| {
173173 const atom = file.getAtom(atom_index) orelse continue;
174 if (!atom.alive.load(.seq_cst)) continue;
174 if (!atom.flags.alive) continue;
175175 atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(macho_file), macho_file);
176176 }
177177 }
......@@ -185,18 +185,18 @@ pub const File = union(enum) {
185185
186186 pub fn writeAtoms(file: File, macho_file: *MachO) !void {
187187 return switch (file) {
188 .dylib => unreachable,
188 .dylib, .zig_object => unreachable,
189189 inline else => |x| x.writeAtoms(macho_file),
190190 };
191191 }
192192
193 pub fn calcSymtabSize(file: File, macho_file: *MachO) !void {
193 pub fn calcSymtabSize(file: File, macho_file: *MachO) void {
194194 return switch (file) {
195195 inline else => |x| x.calcSymtabSize(macho_file),
196196 };
197197 }
198198
199 pub fn writeSymtab(file: File, macho_file: *MachO, ctx: anytype) !void {
199 pub fn writeSymtab(file: File, macho_file: *MachO, ctx: anytype) void {
200200 return switch (file) {
201201 inline else => |x| x.writeSymtab(macho_file, ctx),
202202 };
src/link/MachO/synthetic.zig+5-5
......@@ -23,7 +23,7 @@ pub const ZigGotSection = struct {
2323 const index = try zig_got.allocateEntry(gpa);
2424 const entry = &zig_got.entries.items[index];
2525 entry.* = sym_index;
26 const symbol = zo.getSymbol(sym_index);
26 const symbol = &zo.symbols.items[sym_index];
2727 assert(symbol.flags.needs_zig_got);
2828 symbol.flags.has_zig_got = true;
2929 symbol.addExtra(.{ .zig_got = index }, macho_file);
......@@ -56,7 +56,7 @@ pub const ZigGotSection = struct {
5656 const zo = macho_file.getZigObject().?;
5757 const off = zig_got.entryOffset(index, macho_file);
5858 const entry = zig_got.entries.items[index];
59 const value = zo.getSymbol(entry).getAddress(.{ .stubs = false }, macho_file);
59 const value = zo.symbols.items[entry].getAddress(.{ .stubs = false }, macho_file);
6060
6161 var buf: [8]u8 = undefined;
6262 std.mem.writeInt(u64, &buf, value, .little);
......@@ -66,7 +66,7 @@ pub const ZigGotSection = struct {
6666 pub fn writeAll(zig_got: ZigGotSection, macho_file: *MachO, writer: anytype) !void {
6767 const zo = macho_file.getZigObject().?;
6868 for (zig_got.entries.items) |entry| {
69 const symbol = zo.getSymbol(entry);
69 const symbol = zo.symbols.items[entry];
7070 const value = symbol.address(.{ .stubs = false }, macho_file);
7171 try writer.writeInt(u64, value, .little);
7272 }
......@@ -94,7 +94,7 @@ pub const ZigGotSection = struct {
9494 const zo = macho_file.getZigObject().?;
9595 try writer.writeAll("__zig_got\n");
9696 for (zig_got.entries.items, 0..) |entry, index| {
97 const symbol = zo.getSymbol(entry);
97 const symbol = zo.symbols.items[entry];
9898 try writer.print(" {d}@0x{x} => {d}@0x{x} ({s})\n", .{
9999 index,
100100 zig_got.entryAddress(@intCast(index), macho_file),
......@@ -694,7 +694,7 @@ pub const DataInCode = struct {
694694 dices[next_dice].offset < end_off) : (next_dice += 1)
695695 {}
696696
697 if (atom.alive.load(.seq_cst)) for (dices[start_dice..next_dice]) |d| {
697 if (atom.flags.alive) for (dices[start_dice..next_dice]) |d| {
698698 dice.entries.appendAssumeCapacity(.{
699699 .offset = @intCast(atom.getAddress(macho_file) + d.offset - start_off - base_address),
700700 .length = d.length,
src/link/MachO/thunks.zig+3-3
......@@ -37,7 +37,7 @@ pub fn createThunks(sect_id: u8, macho_file: *MachO) !void {
3737
3838 // Scan relocs in the group and create trampolines for any unreachable callsite
3939 try scanRelocs(thunk_index, gpa, atoms[start..i], macho_file);
40 thunk.value = try advance(header, thunk.size(), .@"4");
40 thunk.value = advance(header, thunk.size(), .@"4");
4141
4242 log.debug("thunk({d}) : {}", .{ thunk_index, thunk.fmt(macho_file) });
4343 }
......@@ -99,8 +99,8 @@ pub const Thunk = struct {
9999 return header.addr + thunk.value;
100100 }
101101
102 pub fn getTargetAddress(thunk: Thunk, sym_index: Symbol.Index, macho_file: *MachO) u64 {
103 return thunk.getAddress(macho_file) + thunk.symbols.getIndex(sym_index).? * trampoline_size;
102 pub fn getTargetAddress(thunk: Thunk, ref: MachO.Ref, macho_file: *MachO) u64 {
103 return thunk.getAddress(macho_file) + thunk.symbols.getIndex(ref).? * trampoline_size;
104104 }
105105
106106 pub fn write(thunk: Thunk, macho_file: *MachO, writer: anytype) !void {