| ... | @@ -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 | } |
| 1217 | | 1217 | |
| 1218 | pub fn writeAtom(self: *MachO, atom: *Atom, sect_id: u8) !void { | 1218 | pub 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 { |
| 1327 | | 1328 | |
| 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 | } |
| 1333 | | 1334 | |
| ... | @@ -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; |
| 1347 | | 1349 | |
| 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); |
| 1375 | | 1377 | |
| 1376 | try self.allocateAtomCommon(atom, self.got_section_index.?); | 1378 | try self.allocateAtomCommon(atom); |
| 1377 | | 1379 | |
| 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; | | |
| 1387 | | 1387 | |
| 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); |
| 1399 | | 1399 | |
| 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); |
| 1406 | | 1410 | |
| 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; |
| 1420 | | 1425 | |
| 1421 | try self.allocateAtomCommon(atom, self.data_section_index.?); | 1426 | try self.allocateAtomCommon(atom); |
| 1422 | | 1427 | |
| 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; |
| 1447 | | 1453 | |
| 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; |
| 1544 | | 1550 | |
| 1545 | try self.allocateAtomCommon(atom, self.stub_helper_section_index.?); | 1551 | try self.allocateAtomCommon(atom); |
| 1546 | | 1552 | |
| 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; |
| 1568 | | 1575 | |
| 1569 | try atom.relocs.ensureTotalCapacity(gpa, 1); | 1576 | try atom.relocs.ensureTotalCapacity(gpa, 1); |
| 1570 | | 1577 | |
| ... | @@ -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); |
| 1616 | | 1623 | |
| 1617 | try self.allocateAtomCommon(atom, self.stub_helper_section_index.?); | 1624 | try self.allocateAtomCommon(atom); |
| 1618 | | 1625 | |
| 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; |
| 1628 | | 1636 | |
| 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); |
| 1652 | | 1660 | |
| 1653 | try self.allocateAtomCommon(atom, self.la_symbol_ptr_section_index.?); | 1661 | try self.allocateAtomCommon(atom); |
| 1654 | | 1662 | |
| 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; |
| 1675 | | 1684 | |
| 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); |
| 1727 | | 1736 | |
| 1728 | try self.allocateAtomCommon(atom, self.stubs_section_index.?); | 1737 | try self.allocateAtomCommon(atom); |
| 1729 | | 1738 | |
| 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; |
| 1764 | | 1773 | |
| 1765 | try self.allocateAtomCommon(atom, n_sect); | 1774 | try self.allocateAtomCommon(atom); |
| 1766 | | 1775 | |
| 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 | } |
| 2378 | | 2387 | |
| 2379 | fn freeAtom(self: *MachO, atom: *Atom, sect_id: u8, owns_atom: bool) void { | 2388 | fn 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 | } |
| 2384 | | 2393 | |
| | 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 | } |
| 2436 | | 2446 | |
| 2437 | fn shrinkAtom(self: *MachO, atom: *Atom, new_block_size: u64, sect_id: u8) void { | 2447 | fn 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 enough | 2451 | // 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 | } |
| 2445 | | 2454 | |
| 2446 | fn growAtom(self: *MachO, atom: *Atom, new_atom_size: u64, alignment: u64, sect_id: u8) !u64 { | 2455 | fn 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 | } |
| 2453 | | 2462 | |
| 2454 | fn allocateSymbol(self: *MachO) !u32 { | 2463 | fn 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); |
| 2708 | | 2721 | |
| 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}); |
| 2711 | | 2724 | |
| 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 | }; | | |
| 2722 | | 2726 | |
| 2723 | try unnamed_consts.append(gpa, atom); | 2727 | try unnamed_consts.append(gpa, atom); |
| 2724 | | 2728 | |
| ... | @@ -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.*.?; |
| 2972 | | 2976 | |
| 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); |
| 2977 | | 2986 | |
| 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); |
| 3002 | | 3012 | |
| 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}); |
| 3005 | | 3015 | |
| 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 | }; | | |
| 3016 | | 3017 | |
| 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 { |
| 3171 | fn freeUnnamedConsts(self: *MachO, decl_index: Module.Decl.Index) void { | 3172 | fn 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 | // TODO | 3175 | 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 | } |
| 3930 | | 3928 | |
| 3931 | fn allocateAtomCommon(self: *MachO, atom: *Atom, sect_id: u8) !void { | 3929 | fn 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 | } |
| 3943 | | 3939 | |
| 3944 | fn allocateAtom( | 3940 | fn 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(); |
| 3953 | | 3943 | |
| | 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 | } |
| 4052 | | 4043 | |
| 4053 | pub fn addAtomToSection(self: *MachO, atom: *Atom, sect_id: u8) !void { | 4044 | pub 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; |