authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-10 16:24:41+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-18 10:00:04+02:00
loge5da251635bb24d418dbec4385aa2319c7f75247
tree2200aa85546e4bdcae67a6f05e185796cf112503
parent05d0c42894b40c530819b1ac15f8133bfd34cf47

macho: clean up use of section ids


3 files changed, 88 insertions(+), 96 deletions(-)

src/link/MachO.zig+64-72
......@@ -1215,10 +1215,11 @@ pub fn createEmptyAtom(gpa: Allocator, sym_index: u32, size: u64, alignment: u32
12151215 return atom;
12161216}
12171217
1218pub fn writeAtom(self: *MachO, atom: *Atom, sect_id: u8) !void {
1219 const section = self.sections.get(sect_id);
1218pub fn writeAtom(self: *MachO, atom: *Atom) !void {
12201219 const sym = atom.getSymbol(self);
1220 const section = self.sections.get(sym.n_sect - 1);
12211221 const file_offset = section.header.offset + sym.n_value - section.header.addr;
1222 try atom.resolveRelocs(self);
12221223 log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ atom.getName(self), file_offset });
12231224 try self.base.file.?.pwriteAll(atom.code.items, file_offset);
12241225}
......@@ -1327,7 +1328,7 @@ fn writeAtoms(self: *MachO) !void {
13271328
13281329 while (true) {
13291330 if (atom.dirty) {
1330 try self.writeAtom(atom, sect_i);
1331 try self.writeAtom(atom);
13311332 atom.dirty = false;
13321333 }
13331334
......@@ -1344,6 +1345,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
13441345 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
13451346 const sym = atom.getSymbolPtr(self);
13461347 sym.n_type = macho.N_SECT;
1348 sym.n_sect = self.got_section_index.? + 1;
13471349
13481350 try atom.relocs.append(gpa, .{
13491351 .offset = 0,
......@@ -1373,7 +1375,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
13731375 try self.managed_atoms.append(gpa, atom);
13741376 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
13751377
1376 try self.allocateAtomCommon(atom, self.got_section_index.?);
1378 try self.allocateAtomCommon(atom);
13771379
13781380 return atom;
13791381}
......@@ -1382,8 +1384,6 @@ pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
13821384 const gpa = self.base.allocator;
13831385 const sym_index = try self.allocateSymbol();
13841386 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
1385 const sym = atom.getSymbolPtr(self);
1386 sym.n_type = macho.N_SECT;
13871387
13881388 const target_sym = self.getSymbol(target);
13891389 assert(target_sym.undf());
......@@ -1397,12 +1397,16 @@ pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
13971397 try self.managed_atoms.append(gpa, atom);
13981398 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
13991399
1400 const match = (try self.getOutputSection(.{
1400 const sym = atom.getSymbolPtr(self);
1401 sym.n_type = macho.N_SECT;
1402 const sect_id = (try self.getOutputSection(.{
14011403 .segname = makeStaticString("__DATA"),
14021404 .sectname = makeStaticString("__thread_ptrs"),
14031405 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
14041406 })).?;
1405 try self.allocateAtomCommon(atom, match);
1407 sym.n_sect = sect_id + 1;
1408
1409 try self.allocateAtomCommon(atom);
14061410
14071411 return atom;
14081412}
......@@ -1416,9 +1420,10 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {
14161420 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
14171421 const sym = atom.getSymbolPtr(self);
14181422 sym.n_type = macho.N_SECT;
1423 sym.n_sect = self.data_section_index.? + 1;
14191424 self.dyld_private_atom = atom;
14201425
1421 try self.allocateAtomCommon(atom, self.data_section_index.?);
1426 try self.allocateAtomCommon(atom);
14221427
14231428 try self.managed_atoms.append(gpa, atom);
14241429 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
......@@ -1444,6 +1449,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
14441449 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);
14451450 const sym = atom.getSymbolPtr(self);
14461451 sym.n_type = macho.N_SECT;
1452 sym.n_sect = self.stub_helper_section_index.? + 1;
14471453
14481454 const dyld_private_sym_index = self.dyld_private_atom.?.sym_index;
14491455 switch (arch) {
......@@ -1542,7 +1548,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
15421548 }
15431549 self.stub_helper_preamble_atom = atom;
15441550
1545 try self.allocateAtomCommon(atom, self.stub_helper_section_index.?);
1551 try self.allocateAtomCommon(atom);
15461552
15471553 try self.managed_atoms.append(gpa, atom);
15481554 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
......@@ -1565,6 +1571,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
15651571 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);
15661572 const sym = atom.getSymbolPtr(self);
15671573 sym.n_type = macho.N_SECT;
1574 sym.n_sect = self.stub_helper_section_index.? + 1;
15681575
15691576 try atom.relocs.ensureTotalCapacity(gpa, 1);
15701577
......@@ -1614,7 +1621,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
16141621 try self.managed_atoms.append(gpa, atom);
16151622 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
16161623
1617 try self.allocateAtomCommon(atom, self.stub_helper_section_index.?);
1624 try self.allocateAtomCommon(atom);
16181625
16191626 return atom;
16201627}
......@@ -1625,6 +1632,7 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi
16251632 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
16261633 const sym = atom.getSymbolPtr(self);
16271634 sym.n_type = macho.N_SECT;
1635 sym.n_sect = self.la_symbol_ptr_section_index.? + 1;
16281636
16291637 try atom.relocs.append(gpa, .{
16301638 .offset = 0,
......@@ -1650,7 +1658,7 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi
16501658 try self.managed_atoms.append(gpa, atom);
16511659 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
16521660
1653 try self.allocateAtomCommon(atom, self.la_symbol_ptr_section_index.?);
1661 try self.allocateAtomCommon(atom);
16541662
16551663 return atom;
16561664}
......@@ -1672,6 +1680,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
16721680 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);
16731681 const sym = atom.getSymbolPtr(self);
16741682 sym.n_type = macho.N_SECT;
1683 sym.n_sect = self.stubs_section_index.? + 1;
16751684
16761685 switch (arch) {
16771686 .x86_64 => {
......@@ -1725,7 +1734,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
17251734 try self.managed_atoms.append(gpa, atom);
17261735 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
17271736
1728 try self.allocateAtomCommon(atom, self.stubs_section_index.?);
1737 try self.allocateAtomCommon(atom);
17291738
17301739 return atom;
17311740}
......@@ -1762,7 +1771,7 @@ pub fn createTentativeDefAtoms(self: *MachO) !void {
17621771 const atom = try MachO.createEmptyAtom(gpa, global.sym_index, size, alignment);
17631772 atom.file = global.file;
17641773
1765 try self.allocateAtomCommon(atom, n_sect);
1774 try self.allocateAtomCommon(atom);
17661775
17671776 if (global.file) |file| {
17681777 const object = &self.objects.items[file];
......@@ -2376,12 +2385,13 @@ pub fn deinit(self: *MachO) void {
23762385 }
23772386}
23782387
2379fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void {
2388fn freeAtom(self: *MachO, atom: *Atom, owns_atom: bool) void {
23802389 log.debug("freeAtom {*}", .{atom});
23812390 if (!owns_atom) {
23822391 atom.deinit(self.base.allocator);
23832392 }
23842393
2394 const sect_id = atom.getSymbol(self).n_sect - 1;
23852395 const free_list = &self.sections.items(.free_list)[sect_id];
23862396 var already_have_free_list_node = false;
23872397 {
......@@ -2434,21 +2444,20 @@ fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void {
24342444 }
24352445}
24362446
2437fn shrinkAtom(self: *MachO, atom: *Atom, new_block_size: u64, sect_id: u8) void {
2447fn shrinkAtom(self: *MachO, atom: *Atom, new_block_size: u64) void {
24382448 _ = self;
24392449 _ = atom;
24402450 _ = new_block_size;
2441 _ = sect_id;
24422451 // TODO check the new capacity, and if it crosses the size threshold into a big enough
24432452 // capacity, insert a free list node for it.
24442453}
24452454
2446fn growAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, sect_id: u8) !u64 {
2455fn growAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64) !u64 {
24472456 const sym = atom.getSymbol(self);
24482457 const align_ok = mem.alignBackwardGeneric(u64, sym.n_value, alignment) == sym.n_value;
24492458 const need_realloc = !align_ok or new_atom_size > atom.capacity(self);
24502459 if (!need_realloc) return sym.n_value;
2451 return self.allocateAtom(atom, new_atom_size, alignment, sect_id);
2460 return self.allocateAtom(atom, new_atom_size, alignment);
24522461}
24532462
24542463fn allocateSymbol(self: *MachO) !u32 {
......@@ -2704,21 +2713,16 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
27042713 typed_value.val,
27052714 required_alignment,
27062715 );
2707 const addr = try self.allocateAtom(atom, code.len, required_alignment, sect_id);
2716 const symbol = atom.getSymbolPtr(self);
2717 symbol.n_strx = name_str_index;
2718 symbol.n_type = macho.N_SECT;
2719 symbol.n_sect = sect_id + 1;
2720 symbol.n_value = try self.allocateAtom(atom, code.len, required_alignment);
27082721
2709 log.debug("allocated atom for {?s} at 0x{x}", .{ name, addr });
2722 log.debug("allocated atom for {?s} at 0x{x}", .{ name, symbol.n_value });
27102723 log.debug(" (required alignment 0x{x})", .{required_alignment});
27112724
2712 errdefer self.freeAtom(atom, sect_id, true);
2713
2714 const symbol = atom.getSymbolPtr(self);
2715 symbol.* = .{
2716 .n_strx = name_str_index,
2717 .n_type = macho.N_SECT,
2718 .n_sect = sect_id + 1,
2719 .n_desc = 0,
2720 .n_value = addr,
2721 };
2725 errdefer self.freeAtom(atom, true);
27222726
27232727 try unnamed_consts.append(gpa, atom);
27242728
......@@ -2968,15 +2972,20 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !u64
29682972 required_alignment,
29692973 );
29702974 }
2971 const match = decl_ptr.*.?;
2975 const sect_id = decl_ptr.*.?;
29722976
29732977 if (decl.link.macho.size != 0) {
29742978 const symbol = decl.link.macho.getSymbolPtr(self);
2979 symbol.n_strx = try self.strtab.insert(self.base.allocator, sym_name);
2980 symbol.n_type = macho.N_SECT;
2981 symbol.n_sect = sect_id + 1;
2982 symbol.n_desc = 0;
2983
29752984 const capacity = decl.link.macho.capacity(self);
29762985 const need_realloc = code_len > capacity or !mem.isAlignedGeneric(u64, symbol.n_value, required_alignment);
29772986
29782987 if (need_realloc) {
2979 const vaddr = try self.growAtom(&decl.link.macho, code_len, required_alignment, match);
2988 const vaddr = try self.growAtom(&decl.link.macho, code_len, required_alignment);
29802989 log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ sym_name, symbol.n_value, vaddr });
29812990 log.debug(" (required alignment 0x{x})", .{required_alignment});
29822991 symbol.n_value = vaddr;
......@@ -2987,32 +2996,24 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !u64
29872996 }).?;
29882997 got_atom.dirty = true;
29892998 } else if (code_len < decl.link.macho.size) {
2990 self.shrinkAtom(&decl.link.macho, code_len, match);
2999 self.shrinkAtom(&decl.link.macho, code_len);
29913000 }
3001
29923002 decl.link.macho.size = code_len;
29933003 decl.link.macho.dirty = true;
2994
2995 symbol.n_strx = try self.strtab.insert(self.base.allocator, sym_name);
2996 symbol.n_type = macho.N_SECT;
2997 symbol.n_sect = @intCast(u8, self.text_section_index.?) + 1;
2998 symbol.n_desc = 0;
29993004 } else {
30003005 const name_str_index = try self.strtab.insert(self.base.allocator, sym_name);
3001 const addr = try self.allocateAtom(&decl.link.macho, code_len, required_alignment, match);
3006 const symbol = decl.link.macho.getSymbolPtr(self);
3007 symbol.n_strx = name_str_index;
3008 symbol.n_type = macho.N_SECT;
3009 symbol.n_sect = sect_id + 1;
3010 symbol.n_desc = 0;
3011 symbol.n_value = try self.allocateAtom(&decl.link.macho, code_len, required_alignment);
30023012
3003 log.debug("allocated atom for {s} at 0x{x}", .{ sym_name, addr });
3013 log.debug("allocated atom for {s} at 0x{x}", .{ sym_name, symbol.n_value });
30043014 log.debug(" (required alignment 0x{x})", .{required_alignment});
30053015
3006 errdefer self.freeAtom(&decl.link.macho, match, false);
3007
3008 const symbol = decl.link.macho.getSymbolPtr(self);
3009 symbol.* = .{
3010 .n_strx = name_str_index,
3011 .n_type = macho.N_SECT,
3012 .n_sect = match + 1,
3013 .n_desc = 0,
3014 .n_value = addr,
3015 };
3016 errdefer self.freeAtom(&decl.link.macho, false);
30163017
30173018 const got_target = SymbolWithLoc{ .sym_index = decl.link.macho.sym_index, .file = null };
30183019 const got_index = try self.allocateGotEntry(got_target);
......@@ -3171,10 +3172,7 @@ pub fn deleteExport(self: *MachO, exp: Export) void {
31713172fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void {
31723173 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
31733174 for (unnamed_consts.items) |atom| {
3174 // TODO
3175 // const sect_id = atom.getSymbol(self).n_sect;
3176 const sect_id = self.getSectionByName("__TEXT", "__const").?;
3177 self.freeAtom(atom, sect_id, true);
3175 self.freeAtom(atom, true);
31783176 self.locals_free_list.append(self.base.allocator, atom.sym_index) catch {};
31793177 self.locals.items[atom.sym_index].n_type = 0;
31803178 _ = self.atom_by_index_table.remove(atom.sym_index);
......@@ -3192,8 +3190,8 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void {
31923190 const decl = mod.declPtr(decl_index);
31933191 log.debug("freeDecl {*}", .{decl});
31943192 const kv = self.decls.fetchSwapRemove(decl_index);
3195 if (kv.?.value) |match| {
3196 self.freeAtom(&decl.link.macho, match, false);
3193 if (kv.?.value) |_| {
3194 self.freeAtom(&decl.link.macho, false);
31973195 self.freeUnnamedConsts(decl_index);
31983196 }
31993197 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
......@@ -3928,29 +3926,22 @@ fn getSectionMaxAlignment(self: *MachO, start: u8, end: u8) !u32 {
39283926 return max_alignment;
39293927}
39303928
3931fn allocateAtomCommon(self: *MachO, atom: *Atom, sect_id: u8) !void {
3932 const sym = atom.getSymbolPtr(self);
3929fn allocateAtomCommon(self: *MachO, atom: *Atom) !void {
39333930 if (self.mode == .incremental) {
3931 const sym_name = atom.getName(self);
39343932 const size = atom.size;
39353933 const alignment = try math.powi(u32, 2, atom.alignment);
3936 const vaddr = try self.allocateAtom(atom, size, alignment, sect_id);
3937 const sym_name = atom.getName(self);
3934 const vaddr = try self.allocateAtom(atom, size, alignment);
39383935 log.debug("allocated {s} atom at 0x{x}", .{ sym_name, vaddr });
3939 sym.n_value = vaddr;
3940 } else try self.addAtomToSection(atom, sect_id);
3941 sym.n_sect = sect_id + 1;
3936 atom.getSymbolPtr(self).n_value = vaddr;
3937 } else try self.addAtomToSection(atom);
39423938}
39433939
3944fn allocateAtom(
3945 self: *MachO,
3946 atom: *Atom,
3947 new_atom_size: u64,
3948 alignment: u64,
3949 sect_id: u8,
3950) !u64 {
3940fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64) !u64 {
39513941 const tracy = trace(@src());
39523942 defer tracy.end();
39533943
3944 const sect_id = atom.getSymbol(self).n_sect - 1;
39543945 const header = &self.sections.items(.header)[sect_id];
39553946 const free_list = &self.sections.items(.free_list)[sect_id];
39563947 const maybe_last_atom = &self.sections.items(.last_atom)[sect_id];
......@@ -4050,7 +4041,8 @@ fn allocateAtom(
40504041 return vaddr;
40514042}
40524043
4053pub fn addAtomToSection(self: *MachO, atom: *Atom, sect_id: u8) !void {
4044pub fn addAtomToSection(self: *MachO, atom: *Atom) !void {
4045 const sect_id = atom.getSymbol(self).n_sect - 1;
40544046 var section = self.sections.get(sect_id);
40554047 if (section.header.size > 0) {
40564048 section.last_atom.?.next = atom;
src/link/MachO/Atom.zig+2-2
......@@ -314,13 +314,13 @@ pub fn parseRelocs(self: *Atom, relocs: []align(1) const macho.relocation_info,
314314 const sect_id = @intCast(u16, rel.r_symbolnum - 1);
315315 const sym_index = object.sections_as_symbols.get(sect_id) orelse blk: {
316316 const sect = object.getSourceSection(sect_id);
317 const match = (try context.macho_file.getOutputSection(sect)) orelse
317 const out_sect_id = (try context.macho_file.getOutputSection(sect)) orelse
318318 unreachable;
319319 const sym_index = @intCast(u32, object.symtab.items.len);
320320 try object.symtab.append(gpa, .{
321321 .n_strx = 0,
322322 .n_type = macho.N_SECT,
323 .n_sect = match + 1,
323 .n_sect = out_sect_id + 1,
324324 .n_desc = 0,
325325 .n_value = sect.addr,
326326 });
src/link/MachO/Object.zig+22-22
......@@ -220,15 +220,15 @@ fn filterRelocs(
220220
221221pub fn scanInputSections(self: Object, macho_file: *MachO) !void {
222222 for (self.sections.items) |sect| {
223 const match = (try macho_file.getOutputSection(sect)) orelse {
223 const sect_id = (try macho_file.getOutputSection(sect)) orelse {
224224 log.debug(" unhandled section", .{});
225225 continue;
226226 };
227 const output = macho_file.sections.items(.header)[match];
227 const output = macho_file.sections.items(.header)[sect_id];
228228 log.debug("mapping '{s},{s}' into output sect({d}, '{s},{s}')", .{
229229 sect.segName(),
230230 sect.sectName(),
231 match + 1,
231 sect_id + 1,
232232 output.segName(),
233233 output.sectName(),
234234 });
......@@ -335,15 +335,15 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
335335 log.debug("splitting section '{s},{s}' into atoms", .{ sect.segName(), sect.sectName() });
336336
337337 // Get matching segment/section in the final artifact.
338 const match = (try macho_file.getOutputSection(sect)) orelse {
338 const out_sect_id = (try macho_file.getOutputSection(sect)) orelse {
339339 log.debug(" unhandled section", .{});
340340 continue;
341341 };
342342
343343 log.debug(" output sect({d}, '{s},{s}')", .{
344 match + 1,
345 macho_file.sections.items(.header)[match].segName(),
346 macho_file.sections.items(.header)[match].sectName(),
344 out_sect_id + 1,
345 macho_file.sections.items(.header)[out_sect_id].segName(),
346 macho_file.sections.items(.header)[out_sect_id].sectName(),
347347 });
348348
349349 const cpu_arch = macho_file.base.options.target.cpu.arch;
......@@ -376,7 +376,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
376376 try self.symtab.append(gpa, .{
377377 .n_strx = 0,
378378 .n_type = macho.N_SECT,
379 .n_sect = match + 1,
379 .n_sect = out_sect_id + 1,
380380 .n_desc = 0,
381381 .n_value = sect.addr,
382382 });
......@@ -397,10 +397,10 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
397397 atom_code,
398398 relocs,
399399 &.{},
400 match,
400 out_sect_id,
401401 sect,
402402 );
403 try macho_file.addAtomToSection(atom, match);
403 try macho_file.addAtomToSection(atom);
404404 }
405405
406406 var next_sym_count: usize = 0;
......@@ -452,7 +452,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
452452 atom_code,
453453 relocs,
454454 sorted_atom_syms.items[1..],
455 match,
455 out_sect_id,
456456 sect,
457457 );
458458
......@@ -465,7 +465,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
465465 try self.symtab.append(gpa, .{
466466 .n_strx = 0,
467467 .n_type = macho.N_SECT,
468 .n_sect = match + 1,
468 .n_sect = out_sect_id + 1,
469469 .n_desc = 0,
470470 .n_value = addr,
471471 });
......@@ -479,7 +479,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
479479 try self.atom_by_index_table.put(gpa, alias, atom);
480480 }
481481
482 try macho_file.addAtomToSection(atom, match);
482 try macho_file.addAtomToSection(atom);
483483 }
484484 } else {
485485 // If there is no symbol to refer to this atom, we create
......@@ -490,7 +490,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
490490 try self.symtab.append(gpa, .{
491491 .n_strx = 0,
492492 .n_type = macho.N_SECT,
493 .n_sect = match + 1,
493 .n_sect = out_sect_id + 1,
494494 .n_desc = 0,
495495 .n_value = sect.addr,
496496 });
......@@ -506,10 +506,10 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
506506 code,
507507 relocs,
508508 filtered_syms,
509 match,
509 out_sect_id,
510510 sect,
511511 );
512 try macho_file.addAtomToSection(atom, match);
512 try macho_file.addAtomToSection(atom);
513513 }
514514 }
515515}
......@@ -524,21 +524,21 @@ fn createAtomFromSubsection(
524524 code: ?[]const u8,
525525 relocs: []align(1) const macho.relocation_info,
526526 indexes: []const SymbolAtIndex,
527 match: u8,
527 out_sect_id: u8,
528528 sect: macho.section_64,
529529) !*Atom {
530530 const gpa = macho_file.base.allocator;
531531 const sym = self.symtab.items[sym_index];
532532 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);
533533 atom.file = object_id;
534 self.symtab.items[sym_index].n_sect = match + 1;
534 self.symtab.items[sym_index].n_sect = out_sect_id + 1;
535535
536536 log.debug("creating ATOM(%{d}, '{s}') in sect({d}, '{s},{s}') in object({d})", .{
537537 sym_index,
538538 self.getString(sym.n_strx),
539 match + 1,
540 macho_file.sections.items(.header)[match].segName(),
541 macho_file.sections.items(.header)[match].sectName(),
539 out_sect_id + 1,
540 macho_file.sections.items(.header)[out_sect_id].segName(),
541 macho_file.sections.items(.header)[out_sect_id].sectName(),
542542 object_id,
543543 });
544544
......@@ -566,7 +566,7 @@ fn createAtomFromSubsection(
566566 try atom.contained.ensureTotalCapacity(gpa, indexes.len);
567567 for (indexes) |inner_sym_index| {
568568 const inner_sym = &self.symtab.items[inner_sym_index.index];
569 inner_sym.n_sect = match + 1;
569 inner_sym.n_sect = out_sect_id + 1;
570570 atom.contained.appendAssumeCapacity(.{
571571 .sym_index = inner_sym_index.index,
572572 .offset = inner_sym.n_value - sym.n_value,