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...@@ -1215,10 +1215,11 @@ pub fn createEmptyAtom(gpa: Allocator, sym_index: u32, size: u64, alignment: u32
1215 return atom;1215 return atom;
1216}1216}
12171217
1218pub fn writeAtom(self: *MachO, atom: *Atom, sect_id: u8) !void {1218pub fn writeAtom(self: *MachO, atom: *Atom) !void {
1219 const section = self.sections.get(sect_id);
1220 const sym = atom.getSymbol(self);1219 const sym = atom.getSymbol(self);
1220 const section = self.sections.get(sym.n_sect - 1);
1221 const file_offset = section.header.offset + sym.n_value - section.header.addr;1221 const file_offset = section.header.offset + sym.n_value - section.header.addr;
1222 try atom.resolveRelocs(self);
1222 log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ atom.getName(self), file_offset });1223 log.debug("writing atom for symbol {s} at file offset 0x{x}", .{ atom.getName(self), file_offset });
1223 try self.base.file.?.pwriteAll(atom.code.items, file_offset);1224 try self.base.file.?.pwriteAll(atom.code.items, file_offset);
1224}1225}
...@@ -1327,7 +1328,7 @@ fn writeAtoms(self: *MachO) !void {...@@ -1327,7 +1328,7 @@ fn writeAtoms(self: *MachO) !void {
13271328
1328 while (true) {1329 while (true) {
1329 if (atom.dirty) {1330 if (atom.dirty) {
1330 try self.writeAtom(atom, sect_i);1331 try self.writeAtom(atom);
1331 atom.dirty = false;1332 atom.dirty = false;
1332 }1333 }
13331334
...@@ -1344,6 +1345,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {...@@ -1344,6 +1345,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
1344 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);1345 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
1345 const sym = atom.getSymbolPtr(self);1346 const sym = atom.getSymbolPtr(self);
1346 sym.n_type = macho.N_SECT;1347 sym.n_type = macho.N_SECT;
1348 sym.n_sect = self.got_section_index.? + 1;
13471349
1348 try atom.relocs.append(gpa, .{1350 try atom.relocs.append(gpa, .{
1349 .offset = 0,1351 .offset = 0,
...@@ -1373,7 +1375,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {...@@ -1373,7 +1375,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
1373 try self.managed_atoms.append(gpa, atom);1375 try self.managed_atoms.append(gpa, atom);
1374 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);1376 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
1378 return atom;1380 return atom;
1379}1381}
...@@ -1382,8 +1384,6 @@ pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {...@@ -1382,8 +1384,6 @@ pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
1382 const gpa = self.base.allocator;1384 const gpa = self.base.allocator;
1383 const sym_index = try self.allocateSymbol();1385 const sym_index = try self.allocateSymbol();
1384 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);1386 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
1388 const target_sym = self.getSymbol(target);1388 const target_sym = self.getSymbol(target);
1389 assert(target_sym.undf());1389 assert(target_sym.undf());
...@@ -1397,12 +1397,16 @@ pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {...@@ -1397,12 +1397,16 @@ pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
1397 try self.managed_atoms.append(gpa, atom);1397 try self.managed_atoms.append(gpa, atom);
1398 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);1398 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(.{
1401 .segname = makeStaticString("__DATA"),1403 .segname = makeStaticString("__DATA"),
1402 .sectname = makeStaticString("__thread_ptrs"),1404 .sectname = makeStaticString("__thread_ptrs"),
1403 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,1405 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
1404 })).?;1406 })).?;
1405 try self.allocateAtomCommon(atom, match);1407 sym.n_sect = sect_id + 1;
1408
1409 try self.allocateAtomCommon(atom);
14061410
1407 return atom;1411 return atom;
1408}1412}
...@@ -1416,9 +1420,10 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {...@@ -1416,9 +1420,10 @@ pub fn createDyldPrivateAtom(self: *MachO) !void {
1416 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);1420 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
1417 const sym = atom.getSymbolPtr(self);1421 const sym = atom.getSymbolPtr(self);
1418 sym.n_type = macho.N_SECT;1422 sym.n_type = macho.N_SECT;
1423 sym.n_sect = self.data_section_index.? + 1;
1419 self.dyld_private_atom = atom;1424 self.dyld_private_atom = atom;
14201425
1421 try self.allocateAtomCommon(atom, self.data_section_index.?);1426 try self.allocateAtomCommon(atom);
14221427
1423 try self.managed_atoms.append(gpa, atom);1428 try self.managed_atoms.append(gpa, atom);
1424 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);1429 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
...@@ -1444,6 +1449,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {...@@ -1444,6 +1449,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
1444 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);1449 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);
1445 const sym = atom.getSymbolPtr(self);1450 const sym = atom.getSymbolPtr(self);
1446 sym.n_type = macho.N_SECT;1451 sym.n_type = macho.N_SECT;
1452 sym.n_sect = self.stub_helper_section_index.? + 1;
14471453
1448 const dyld_private_sym_index = self.dyld_private_atom.?.sym_index;1454 const dyld_private_sym_index = self.dyld_private_atom.?.sym_index;
1449 switch (arch) {1455 switch (arch) {
...@@ -1542,7 +1548,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {...@@ -1542,7 +1548,7 @@ pub fn createStubHelperPreambleAtom(self: *MachO) !void {
1542 }1548 }
1543 self.stub_helper_preamble_atom = atom;1549 self.stub_helper_preamble_atom = atom;
15441550
1545 try self.allocateAtomCommon(atom, self.stub_helper_section_index.?);1551 try self.allocateAtomCommon(atom);
15461552
1547 try self.managed_atoms.append(gpa, atom);1553 try self.managed_atoms.append(gpa, atom);
1548 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);1554 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);
...@@ -1565,6 +1571,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -1565,6 +1571,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
1565 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);1571 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);
1566 const sym = atom.getSymbolPtr(self);1572 const sym = atom.getSymbolPtr(self);
1567 sym.n_type = macho.N_SECT;1573 sym.n_type = macho.N_SECT;
1574 sym.n_sect = self.stub_helper_section_index.? + 1;
15681575
1569 try atom.relocs.ensureTotalCapacity(gpa, 1);1576 try atom.relocs.ensureTotalCapacity(gpa, 1);
15701577
...@@ -1614,7 +1621,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -1614,7 +1621,7 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
1614 try self.managed_atoms.append(gpa, atom);1621 try self.managed_atoms.append(gpa, atom);
1615 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);1622 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
1619 return atom;1626 return atom;
1620}1627}
...@@ -1625,6 +1632,7 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi...@@ -1625,6 +1632,7 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi
1625 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);1632 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
1626 const sym = atom.getSymbolPtr(self);1633 const sym = atom.getSymbolPtr(self);
1627 sym.n_type = macho.N_SECT;1634 sym.n_type = macho.N_SECT;
1635 sym.n_sect = self.la_symbol_ptr_section_index.? + 1;
16281636
1629 try atom.relocs.append(gpa, .{1637 try atom.relocs.append(gpa, .{
1630 .offset = 0,1638 .offset = 0,
...@@ -1650,7 +1658,7 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi...@@ -1650,7 +1658,7 @@ pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWi
1650 try self.managed_atoms.append(gpa, atom);1658 try self.managed_atoms.append(gpa, atom);
1651 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);1659 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
1655 return atom;1663 return atom;
1656}1664}
...@@ -1672,6 +1680,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {...@@ -1672,6 +1680,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
1672 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);1680 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);
1673 const sym = atom.getSymbolPtr(self);1681 const sym = atom.getSymbolPtr(self);
1674 sym.n_type = macho.N_SECT;1682 sym.n_type = macho.N_SECT;
1683 sym.n_sect = self.stubs_section_index.? + 1;
16751684
1676 switch (arch) {1685 switch (arch) {
1677 .x86_64 => {1686 .x86_64 => {
...@@ -1725,7 +1734,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {...@@ -1725,7 +1734,7 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
1725 try self.managed_atoms.append(gpa, atom);1734 try self.managed_atoms.append(gpa, atom);
1726 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom);1735 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
1730 return atom;1739 return atom;
1731}1740}
...@@ -1762,7 +1771,7 @@ pub fn createTentativeDefAtoms(self: *MachO) !void {...@@ -1762,7 +1771,7 @@ pub fn createTentativeDefAtoms(self: *MachO) !void {
1762 const atom = try MachO.createEmptyAtom(gpa, global.sym_index, size, alignment);1771 const atom = try MachO.createEmptyAtom(gpa, global.sym_index, size, alignment);
1763 atom.file = global.file;1772 atom.file = global.file;
17641773
1765 try self.allocateAtomCommon(atom, n_sect);1774 try self.allocateAtomCommon(atom);
17661775
1767 if (global.file) |file| {1776 if (global.file) |file| {
1768 const object = &self.objects.items[file];1777 const object = &self.objects.items[file];
...@@ -2376,12 +2385,13 @@ pub fn deinit(self: *MachO) void {...@@ -2376,12 +2385,13 @@ pub fn deinit(self: *MachO) void {
2376 }2385 }
2377}2386}
23782387
2379fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void {2388fn freeAtom(self: *MachO, atom: *Atom, owns_atom: bool) void {
2380 log.debug("freeAtom {*}", .{atom});2389 log.debug("freeAtom {*}", .{atom});
2381 if (!owns_atom) {2390 if (!owns_atom) {
2382 atom.deinit(self.base.allocator);2391 atom.deinit(self.base.allocator);
2383 }2392 }
23842393
2394 const sect_id = atom.getSymbol(self).n_sect - 1;
2385 const free_list = &self.sections.items(.free_list)[sect_id];2395 const free_list = &self.sections.items(.free_list)[sect_id];
2386 var already_have_free_list_node = false;2396 var already_have_free_list_node = false;
2387 {2397 {
...@@ -2434,21 +2444,20 @@ fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void {...@@ -2434,21 +2444,20 @@ fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void {
2434 }2444 }
2435}2445}
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 {
2438 _ = self;2448 _ = self;
2439 _ = atom;2449 _ = atom;
2440 _ = new_block_size;2450 _ = new_block_size;
2441 _ = sect_id;
2442 // TODO check the new capacity, and if it crosses the size threshold into a big enough2451 // TODO check the new capacity, and if it crosses the size threshold into a big enough
2443 // capacity, insert a free list node for it.2452 // capacity, insert a free list node for it.
2444}2453}
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 {
2447 const sym = atom.getSymbol(self);2456 const sym = atom.getSymbol(self);
2448 const align_ok = mem.alignBackwardGeneric(u64, sym.n_value, alignment) == sym.n_value;2457 const align_ok = mem.alignBackwardGeneric(u64, sym.n_value, alignment) == sym.n_value;
2449 const need_realloc = !align_ok or new_atom_size > atom.capacity(self);2458 const need_realloc = !align_ok or new_atom_size > atom.capacity(self);
2450 if (!need_realloc) return sym.n_value;2459 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);
2452}2461}
24532462
2454fn allocateSymbol(self: *MachO) !u32 {2463fn allocateSymbol(self: *MachO) !u32 {
...@@ -2704,21 +2713,16 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu...@@ -2704,21 +2713,16 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: Modu
2704 typed_value.val,2713 typed_value.val,
2705 required_alignment,2714 required_alignment,
2706 );2715 );
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 });
2710 log.debug(" (required alignment 0x{x})", .{required_alignment});2723 log.debug(" (required alignment 0x{x})", .{required_alignment});
27112724
2712 errdefer self.freeAtom(atom, sect_id, true);2725 errdefer self.freeAtom(atom, 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 };
27222726
2723 try unnamed_consts.append(gpa, atom);2727 try unnamed_consts.append(gpa, atom);
27242728
...@@ -2968,15 +2972,20 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !u64...@@ -2968,15 +2972,20 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !u64
2968 required_alignment,2972 required_alignment,
2969 );2973 );
2970 }2974 }
2971 const match = decl_ptr.*.?;2975 const sect_id = decl_ptr.*.?;
29722976
2973 if (decl.link.macho.size != 0) {2977 if (decl.link.macho.size != 0) {
2974 const symbol = decl.link.macho.getSymbolPtr(self);2978 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
2975 const capacity = decl.link.macho.capacity(self);2984 const capacity = decl.link.macho.capacity(self);
2976 const need_realloc = code_len > capacity or !mem.isAlignedGeneric(u64, symbol.n_value, required_alignment);2985 const need_realloc = code_len > capacity or !mem.isAlignedGeneric(u64, symbol.n_value, required_alignment);
29772986
2978 if (need_realloc) {2987 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);
2980 log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ sym_name, symbol.n_value, vaddr });2989 log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ sym_name, symbol.n_value, vaddr });
2981 log.debug(" (required alignment 0x{x})", .{required_alignment});2990 log.debug(" (required alignment 0x{x})", .{required_alignment});
2982 symbol.n_value = vaddr;2991 symbol.n_value = vaddr;
...@@ -2987,32 +2996,24 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !u64...@@ -2987,32 +2996,24 @@ fn placeDecl(self: *MachO, decl_index: Module.Decl.Index, code_len: usize) !u64
2987 }).?;2996 }).?;
2988 got_atom.dirty = true;2997 got_atom.dirty = true;
2989 } else if (code_len < decl.link.macho.size) {2998 } 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);
2991 }3000 }
3001
2992 decl.link.macho.size = code_len;3002 decl.link.macho.size = code_len;
2993 decl.link.macho.dirty = true;3003 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;
2999 } else {3004 } else {
3000 const name_str_index = try self.strtab.insert(self.base.allocator, sym_name);3005 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 });
3004 log.debug(" (required alignment 0x{x})", .{required_alignment});3014 log.debug(" (required alignment 0x{x})", .{required_alignment});
30053015
3006 errdefer self.freeAtom(&decl.link.macho, match, false);3016 errdefer self.freeAtom(&decl.link.macho, 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 };
30163017
3017 const got_target = SymbolWithLoc{ .sym_index = decl.link.macho.sym_index, .file = null };3018 const got_target = SymbolWithLoc{ .sym_index = decl.link.macho.sym_index, .file = null };
3018 const got_index = try self.allocateGotEntry(got_target);3019 const got_index = try self.allocateGotEntry(got_target);
...@@ -3171,10 +3172,7 @@ pub fn deleteExport(self: *MachO, exp: Export) void {...@@ -3171,10 +3172,7 @@ pub fn deleteExport(self: *MachO, exp: Export) void {
3171fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void {3172fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void {
3172 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;3173 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
3173 for (unnamed_consts.items) |atom| {3174 for (unnamed_consts.items) |atom| {
3174 // TODO3175 self.freeAtom(atom, true);
3175 // const sect_id = atom.getSymbol(self).n_sect;
3176 const sect_id = self.getSectionByName("__TEXT", "__const").?;
3177 self.freeAtom(atom, sect_id, true);
3178 self.locals_free_list.append(self.base.allocator, atom.sym_index) catch {};3176 self.locals_free_list.append(self.base.allocator, atom.sym_index) catch {};
3179 self.locals.items[atom.sym_index].n_type = 0;3177 self.locals.items[atom.sym_index].n_type = 0;
3180 _ = self.atom_by_index_table.remove(atom.sym_index);3178 _ = self.atom_by_index_table.remove(atom.sym_index);
...@@ -3192,8 +3190,8 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void {...@@ -3192,8 +3190,8 @@ pub fn freeDecl(self: *MachO, decl_index: Module.Decl.Index) void {
3192 const decl = mod.declPtr(decl_index);3190 const decl = mod.declPtr(decl_index);
3193 log.debug("freeDecl {*}", .{decl});3191 log.debug("freeDecl {*}", .{decl});
3194 const kv = self.decls.fetchSwapRemove(decl_index);3192 const kv = self.decls.fetchSwapRemove(decl_index);
3195 if (kv.?.value) |match| {3193 if (kv.?.value) |_| {
3196 self.freeAtom(&decl.link.macho, match, false);3194 self.freeAtom(&decl.link.macho, false);
3197 self.freeUnnamedConsts(decl_index);3195 self.freeUnnamedConsts(decl_index);
3198 }3196 }
3199 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.3197 // 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 {...@@ -3928,29 +3926,22 @@ fn getSectionMaxAlignment(self: *MachO, start: u8, end: u8) !u32 {
3928 return max_alignment;3926 return max_alignment;
3929}3927}
39303928
3931fn allocateAtomCommon(self: *MachO, atom: *Atom, sect_id: u8) !void {3929fn allocateAtomCommon(self: *MachO, atom: *Atom) !void {
3932 const sym = atom.getSymbolPtr(self);
3933 if (self.mode == .incremental) {3930 if (self.mode == .incremental) {
3931 const sym_name = atom.getName(self);
3934 const size = atom.size;3932 const size = atom.size;
3935 const alignment = try math.powi(u32, 2, atom.alignment);3933 const alignment = try math.powi(u32, 2, atom.alignment);
3936 const vaddr = try self.allocateAtom(atom, size, alignment, sect_id);3934 const vaddr = try self.allocateAtom(atom, size, alignment);
3937 const sym_name = atom.getName(self);
3938 log.debug("allocated {s} atom at 0x{x}", .{ sym_name, vaddr });3935 log.debug("allocated {s} atom at 0x{x}", .{ sym_name, vaddr });
3939 sym.n_value = vaddr;3936 atom.getSymbolPtr(self).n_value = vaddr;
3940 } else try self.addAtomToSection(atom, sect_id);3937 } else try self.addAtomToSection(atom);
3941 sym.n_sect = sect_id + 1;
3942}3938}
39433939
3944fn allocateAtom(3940fn allocateAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64) !u64 {
3945 self: *MachO,
3946 atom: *Atom,
3947 new_atom_size: u64,
3948 alignment: u64,
3949 sect_id: u8,
3950) !u64 {
3951 const tracy = trace(@src());3941 const tracy = trace(@src());
3952 defer tracy.end();3942 defer tracy.end();
39533943
3944 const sect_id = atom.getSymbol(self).n_sect - 1;
3954 const header = &self.sections.items(.header)[sect_id];3945 const header = &self.sections.items(.header)[sect_id];
3955 const free_list = &self.sections.items(.free_list)[sect_id];3946 const free_list = &self.sections.items(.free_list)[sect_id];
3956 const maybe_last_atom = &self.sections.items(.last_atom)[sect_id];3947 const maybe_last_atom = &self.sections.items(.last_atom)[sect_id];
...@@ -4050,7 +4041,8 @@ fn allocateAtom(...@@ -4050,7 +4041,8 @@ fn allocateAtom(
4050 return vaddr;4041 return vaddr;
4051}4042}
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;
4054 var section = self.sections.get(sect_id);4046 var section = self.sections.get(sect_id);
4055 if (section.header.size > 0) {4047 if (section.header.size > 0) {
4056 section.last_atom.?.next = atom;4048 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,...@@ -314,13 +314,13 @@ pub fn parseRelocs(self: *Atom, relocs: []align(1) const macho.relocation_info,
314 const sect_id = @intCast(u16, rel.r_symbolnum - 1);314 const sect_id = @intCast(u16, rel.r_symbolnum - 1);
315 const sym_index = object.sections_as_symbols.get(sect_id) orelse blk: {315 const sym_index = object.sections_as_symbols.get(sect_id) orelse blk: {
316 const sect = object.getSourceSection(sect_id);316 const sect = object.getSourceSection(sect_id);
317 const match = (try context.macho_file.getOutputSection(sect)) orelse317 const out_sect_id = (try context.macho_file.getOutputSection(sect)) orelse
318 unreachable;318 unreachable;
319 const sym_index = @intCast(u32, object.symtab.items.len);319 const sym_index = @intCast(u32, object.symtab.items.len);
320 try object.symtab.append(gpa, .{320 try object.symtab.append(gpa, .{
321 .n_strx = 0,321 .n_strx = 0,
322 .n_type = macho.N_SECT,322 .n_type = macho.N_SECT,
323 .n_sect = match + 1,323 .n_sect = out_sect_id + 1,
324 .n_desc = 0,324 .n_desc = 0,
325 .n_value = sect.addr,325 .n_value = sect.addr,
326 });326 });
src/link/MachO/Object.zig+22-22
...@@ -220,15 +220,15 @@ fn filterRelocs(...@@ -220,15 +220,15 @@ fn filterRelocs(
220220
221pub fn scanInputSections(self: Object, macho_file: *MachO) !void {221pub fn scanInputSections(self: Object, macho_file: *MachO) !void {
222 for (self.sections.items) |sect| {222 for (self.sections.items) |sect| {
223 const match = (try macho_file.getOutputSection(sect)) orelse {223 const sect_id = (try macho_file.getOutputSection(sect)) orelse {
224 log.debug(" unhandled section", .{});224 log.debug(" unhandled section", .{});
225 continue;225 continue;
226 };226 };
227 const output = macho_file.sections.items(.header)[match];227 const output = macho_file.sections.items(.header)[sect_id];
228 log.debug("mapping '{s},{s}' into output sect({d}, '{s},{s}')", .{228 log.debug("mapping '{s},{s}' into output sect({d}, '{s},{s}')", .{
229 sect.segName(),229 sect.segName(),
230 sect.sectName(),230 sect.sectName(),
231 match + 1,231 sect_id + 1,
232 output.segName(),232 output.segName(),
233 output.sectName(),233 output.sectName(),
234 });234 });
...@@ -335,15 +335,15 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {...@@ -335,15 +335,15 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
335 log.debug("splitting section '{s},{s}' into atoms", .{ sect.segName(), sect.sectName() });335 log.debug("splitting section '{s},{s}' into atoms", .{ sect.segName(), sect.sectName() });
336336
337 // Get matching segment/section in the final artifact.337 // 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 {
339 log.debug(" unhandled section", .{});339 log.debug(" unhandled section", .{});
340 continue;340 continue;
341 };341 };
342342
343 log.debug(" output sect({d}, '{s},{s}')", .{343 log.debug(" output sect({d}, '{s},{s}')", .{
344 match + 1,344 out_sect_id + 1,
345 macho_file.sections.items(.header)[match].segName(),345 macho_file.sections.items(.header)[out_sect_id].segName(),
346 macho_file.sections.items(.header)[match].sectName(),346 macho_file.sections.items(.header)[out_sect_id].sectName(),
347 });347 });
348348
349 const cpu_arch = macho_file.base.options.target.cpu.arch;349 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 {...@@ -376,7 +376,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
376 try self.symtab.append(gpa, .{376 try self.symtab.append(gpa, .{
377 .n_strx = 0,377 .n_strx = 0,
378 .n_type = macho.N_SECT,378 .n_type = macho.N_SECT,
379 .n_sect = match + 1,379 .n_sect = out_sect_id + 1,
380 .n_desc = 0,380 .n_desc = 0,
381 .n_value = sect.addr,381 .n_value = sect.addr,
382 });382 });
...@@ -397,10 +397,10 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {...@@ -397,10 +397,10 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
397 atom_code,397 atom_code,
398 relocs,398 relocs,
399 &.{},399 &.{},
400 match,400 out_sect_id,
401 sect,401 sect,
402 );402 );
403 try macho_file.addAtomToSection(atom, match);403 try macho_file.addAtomToSection(atom);
404 }404 }
405405
406 var next_sym_count: usize = 0;406 var next_sym_count: usize = 0;
...@@ -452,7 +452,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {...@@ -452,7 +452,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
452 atom_code,452 atom_code,
453 relocs,453 relocs,
454 sorted_atom_syms.items[1..],454 sorted_atom_syms.items[1..],
455 match,455 out_sect_id,
456 sect,456 sect,
457 );457 );
458458
...@@ -465,7 +465,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {...@@ -465,7 +465,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
465 try self.symtab.append(gpa, .{465 try self.symtab.append(gpa, .{
466 .n_strx = 0,466 .n_strx = 0,
467 .n_type = macho.N_SECT,467 .n_type = macho.N_SECT,
468 .n_sect = match + 1,468 .n_sect = out_sect_id + 1,
469 .n_desc = 0,469 .n_desc = 0,
470 .n_value = addr,470 .n_value = addr,
471 });471 });
...@@ -479,7 +479,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {...@@ -479,7 +479,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
479 try self.atom_by_index_table.put(gpa, alias, atom);479 try self.atom_by_index_table.put(gpa, alias, atom);
480 }480 }
481481
482 try macho_file.addAtomToSection(atom, match);482 try macho_file.addAtomToSection(atom);
483 }483 }
484 } else {484 } else {
485 // If there is no symbol to refer to this atom, we create485 // 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 {...@@ -490,7 +490,7 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
490 try self.symtab.append(gpa, .{490 try self.symtab.append(gpa, .{
491 .n_strx = 0,491 .n_strx = 0,
492 .n_type = macho.N_SECT,492 .n_type = macho.N_SECT,
493 .n_sect = match + 1,493 .n_sect = out_sect_id + 1,
494 .n_desc = 0,494 .n_desc = 0,
495 .n_value = sect.addr,495 .n_value = sect.addr,
496 });496 });
...@@ -506,10 +506,10 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {...@@ -506,10 +506,10 @@ pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) !void {
506 code,506 code,
507 relocs,507 relocs,
508 filtered_syms,508 filtered_syms,
509 match,509 out_sect_id,
510 sect,510 sect,
511 );511 );
512 try macho_file.addAtomToSection(atom, match);512 try macho_file.addAtomToSection(atom);
513 }513 }
514 }514 }
515}515}
...@@ -524,21 +524,21 @@ fn createAtomFromSubsection(...@@ -524,21 +524,21 @@ fn createAtomFromSubsection(
524 code: ?[]const u8,524 code: ?[]const u8,
525 relocs: []align(1) const macho.relocation_info,525 relocs: []align(1) const macho.relocation_info,
526 indexes: []const SymbolAtIndex,526 indexes: []const SymbolAtIndex,
527 match: u8,527 out_sect_id: u8,
528 sect: macho.section_64,528 sect: macho.section_64,
529) !*Atom {529) !*Atom {
530 const gpa = macho_file.base.allocator;530 const gpa = macho_file.base.allocator;
531 const sym = self.symtab.items[sym_index];531 const sym = self.symtab.items[sym_index];
532 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);532 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);
533 atom.file = object_id;533 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
536 log.debug("creating ATOM(%{d}, '{s}') in sect({d}, '{s},{s}') in object({d})", .{536 log.debug("creating ATOM(%{d}, '{s}') in sect({d}, '{s},{s}') in object({d})", .{
537 sym_index,537 sym_index,
538 self.getString(sym.n_strx),538 self.getString(sym.n_strx),
539 match + 1,539 out_sect_id + 1,
540 macho_file.sections.items(.header)[match].segName(),540 macho_file.sections.items(.header)[out_sect_id].segName(),
541 macho_file.sections.items(.header)[match].sectName(),541 macho_file.sections.items(.header)[out_sect_id].sectName(),
542 object_id,542 object_id,
543 });543 });
544544
...@@ -566,7 +566,7 @@ fn createAtomFromSubsection(...@@ -566,7 +566,7 @@ fn createAtomFromSubsection(
566 try atom.contained.ensureTotalCapacity(gpa, indexes.len);566 try atom.contained.ensureTotalCapacity(gpa, indexes.len);
567 for (indexes) |inner_sym_index| {567 for (indexes) |inner_sym_index| {
568 const inner_sym = &self.symtab.items[inner_sym_index.index];568 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;
570 atom.contained.appendAssumeCapacity(.{570 atom.contained.appendAssumeCapacity(.{
571 .sym_index = inner_sym_index.index,571 .sym_index = inner_sym_index.index,
572 .offset = inner_sym.n_value - sym.n_value,572 .offset = inner_sym.n_value - sym.n_value,