| ... | ... | @@ -137,7 +137,6 @@ got_section_index: ?u8 = null, |
| 137 | 137 | data_const_section_index: ?u8 = null, |
| 138 | 138 | la_symbol_ptr_section_index: ?u8 = null, |
| 139 | 139 | data_section_index: ?u8 = null, |
| 140 | | thread_ptr_section_index: ?u8 = null, |
| 141 | 140 | thread_vars_section_index: ?u8 = null, |
| 142 | 141 | thread_data_section_index: ?u8 = null, |
| 143 | 142 | |
| ... | ... | @@ -157,7 +156,7 @@ strtab: StringTable(.strtab) = .{}, |
| 157 | 156 | |
| 158 | 157 | got_table: SectionTable = .{}, |
| 159 | 158 | stubs_table: SectionTable = .{}, |
| 160 | | tlvp_table: SectionTable = .{}, |
| 159 | tlv_table: SectionTable = .{}, |
| 161 | 160 | |
| 162 | 161 | error_flags: File.ErrorFlags = File.ErrorFlags{}, |
| 163 | 162 | |
| ... | ... | @@ -1741,31 +1740,6 @@ fn createThreadLocalDescriptorAtom(self: *MachO, target: SymbolWithLoc) !Atom.In |
| 1741 | 1740 | return atom_index; |
| 1742 | 1741 | } |
| 1743 | 1742 | |
| 1744 | | fn createThreadLocalPointerAtom(self: *MachO, tlv_desc_sym_index: u32) !Atom.Index { |
| 1745 | | const atom_index = try self.createAtom(); |
| 1746 | | self.getAtomPtr(atom_index).size = @sizeOf(u64); |
| 1747 | | |
| 1748 | | const sym = self.getAtom(atom_index).getSymbolPtr(self); |
| 1749 | | sym.n_type = macho.N_SECT; |
| 1750 | | sym.n_sect = self.thread_ptr_section_index.? + 1; |
| 1751 | | sym.n_value = try self.allocateAtom(atom_index, @sizeOf(u64), @alignOf(u64)); |
| 1752 | | |
| 1753 | | log.debug("allocated threadlocal pointer atom at 0x{x}", .{sym.n_value}); |
| 1754 | | |
| 1755 | | try Atom.addRelocation(self, atom_index, .{ |
| 1756 | | .type = .unsigned, |
| 1757 | | .target = .{ .sym_index = tlv_desc_sym_index }, |
| 1758 | | .offset = 0, |
| 1759 | | .addend = 0, |
| 1760 | | .pcrel = false, |
| 1761 | | .length = 3, |
| 1762 | | }); |
| 1763 | | try Atom.addRebase(self, atom_index, 0); |
| 1764 | | try self.writePtrWidthAtom(atom_index); |
| 1765 | | |
| 1766 | | return atom_index; |
| 1767 | | } |
| 1768 | | |
| 1769 | 1743 | fn createMhExecuteHeaderSymbol(self: *MachO) !void { |
| 1770 | 1744 | if (self.base.options.output_mode != .Exe) return; |
| 1771 | 1745 | if (self.getGlobal("__mh_execute_header")) |global| { |
| ... | ... | @@ -1916,7 +1890,7 @@ pub fn deinit(self: *MachO) void { |
| 1916 | 1890 | |
| 1917 | 1891 | self.got_table.deinit(gpa); |
| 1918 | 1892 | self.stubs_table.deinit(gpa); |
| 1919 | | self.tlvp_table.deinit(gpa); |
| 1893 | self.tlv_table.deinit(gpa); |
| 1920 | 1894 | self.strtab.deinit(gpa); |
| 1921 | 1895 | |
| 1922 | 1896 | self.locals.deinit(gpa); |
| ... | ... | @@ -2150,13 +2124,11 @@ fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void { |
| 2150 | 2124 | } |
| 2151 | 2125 | |
| 2152 | 2126 | fn addTlvEntry(self: *MachO, target: SymbolWithLoc) !void { |
| 2153 | | if (self.tlvp_table.lookup.contains(target)) return; |
| 2154 | | const tlvp_index = try self.tlvp_table.allocateEntry(self.base.allocator, target); |
| 2155 | | const tlv_desc_atom_index = try self.createThreadLocalDescriptorAtom(target); |
| 2156 | | const tlv_desc_atom = self.getAtom(tlv_desc_atom_index); |
| 2157 | | const tlv_ptr_atom_index = try self.createThreadLocalPointerAtom(tlv_desc_atom.getSymbolIndex().?); |
| 2158 | | const tlv_ptr_atom = self.getAtom(tlv_ptr_atom_index); |
| 2159 | | self.tlvp_table.entries.items[tlvp_index].sym_index = tlv_ptr_atom.getSymbolIndex().?; |
| 2127 | if (self.tlv_table.lookup.contains(target)) return; |
| 2128 | const tlv_index = try self.tlv_table.allocateEntry(self.base.allocator, target); |
| 2129 | const tlv_atom_index = try self.createThreadLocalDescriptorAtom(target); |
| 2130 | const tlv_atom = self.getAtom(tlv_atom_index); |
| 2131 | self.tlv_table.entries.items[tlv_index].sym_index = tlv_atom.getSymbolIndex().?; |
| 2160 | 2132 | self.markRelocsDirtyByTarget(target); |
| 2161 | 2133 | } |
| 2162 | 2134 | |
| ... | ... | @@ -2550,18 +2522,18 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []u8) !u64 |
| 2550 | 2522 | |
| 2551 | 2523 | if (vaddr != sym.n_value) { |
| 2552 | 2524 | sym.n_value = vaddr; |
| 2553 | | const target = SymbolWithLoc{ .sym_index = sym_index }; |
| 2525 | // TODO: I think we should update the offset to the initializer here too. |
| 2526 | const target: SymbolWithLoc = if (is_threadlocal) blk: { |
| 2527 | const tlv_atom_index = self.tlv_table.getAtomIndex(self, .{ |
| 2528 | .sym_index = sym_index, |
| 2529 | }).?; |
| 2530 | const tlv_atom = self.getAtom(tlv_atom_index); |
| 2531 | break :blk tlv_atom.getSymbolWithLoc(); |
| 2532 | } else .{ .sym_index = sym_index }; |
| 2554 | 2533 | self.markRelocsDirtyByTarget(target); |
| 2555 | | if (is_threadlocal) { |
| 2556 | | @panic("TODO update the threadlocal variable's name also"); |
| 2557 | | // log.debug(" (updating threadlocal pointer entry)", .{}); |
| 2558 | | // const tlvp_atom_index = self.tlvp_table.getAtomIndex(self, target).?; |
| 2559 | | // try self.writePtrWidthAtom(tlvp_atom_index); |
| 2560 | | } else { |
| 2561 | | log.debug(" (updating GOT entry)", .{}); |
| 2562 | | const got_atom_index = self.got_table.getAtomIndex(self, target).?; |
| 2563 | | try self.writePtrWidthAtom(got_atom_index); |
| 2564 | | } |
| 2534 | log.debug(" (updating GOT entry)", .{}); |
| 2535 | const got_atom_index = self.got_table.getAtomIndex(self, target).?; |
| 2536 | try self.writePtrWidthAtom(got_atom_index); |
| 2565 | 2537 | } |
| 2566 | 2538 | } else if (code_len < atom.size) { |
| 2567 | 2539 | self.shrinkAtom(atom_index, code_len); |
| ... | ... | @@ -2586,12 +2558,15 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []u8) !u64 |
| 2586 | 2558 | self.getAtomPtr(atom_index).size = code_len; |
| 2587 | 2559 | sym.n_value = vaddr; |
| 2588 | 2560 | |
| 2589 | | const target: SymbolWithLoc = .{ .sym_index = sym_index }; |
| 2590 | 2561 | if (is_threadlocal) { |
| 2591 | | try self.addTlvEntry(target); |
| 2592 | | } else { |
| 2593 | | try self.addGotEntry(target); |
| 2562 | try self.addTlvEntry(.{ .sym_index = sym_index }); |
| 2594 | 2563 | } |
| 2564 | const target: SymbolWithLoc = if (is_threadlocal) blk: { |
| 2565 | const tlv_atom_index = self.tlv_table.getAtomIndex(self, .{ .sym_index = sym_index }).?; |
| 2566 | const tlv_atom = self.getAtom(tlv_atom_index); |
| 2567 | break :blk tlv_atom.getSymbolWithLoc(); |
| 2568 | } else .{ .sym_index = sym_index }; |
| 2569 | try self.addGotEntry(target); |
| 2595 | 2570 | } |
| 2596 | 2571 | |
| 2597 | 2572 | try self.writeAtom(atom_index, code); |
| ... | ... | @@ -2927,17 +2902,8 @@ fn populateMissingMetadata(self: *MachO) !void { |
| 2927 | 2902 | } |
| 2928 | 2903 | |
| 2929 | 2904 | if (!self.base.options.single_threaded) { |
| 2930 | | if (self.thread_ptr_section_index == null) { |
| 2931 | | self.thread_ptr_section_index = try self.allocateSection("__DATA2", "__thread_ptrs", .{ |
| 2932 | | .size = @sizeOf(u64), |
| 2933 | | .alignment = @alignOf(u64), |
| 2934 | | .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS, |
| 2935 | | .prot = macho.PROT.READ | macho.PROT.WRITE, |
| 2936 | | }); |
| 2937 | | self.segment_table_dirty = true; |
| 2938 | | } |
| 2939 | 2905 | if (self.thread_vars_section_index == null) { |
| 2940 | | self.thread_vars_section_index = try self.allocateSection("__DATA3", "__thread_vars", .{ |
| 2906 | self.thread_vars_section_index = try self.allocateSection("__DATA2", "__thread_vars", .{ |
| 2941 | 2907 | .size = @sizeOf(u64) * 3, |
| 2942 | 2908 | .alignment = @sizeOf(u64), |
| 2943 | 2909 | .flags = macho.S_THREAD_LOCAL_VARIABLES, |
| ... | ... | @@ -2947,7 +2913,7 @@ fn populateMissingMetadata(self: *MachO) !void { |
| 2947 | 2913 | } |
| 2948 | 2914 | |
| 2949 | 2915 | if (self.thread_data_section_index == null) { |
| 2950 | | self.thread_data_section_index = try self.allocateSection("__DATA4", "__thread_data", .{ |
| 2916 | self.thread_data_section_index = try self.allocateSection("__DATA3", "__thread_data", .{ |
| 2951 | 2917 | .size = @sizeOf(u64), |
| 2952 | 2918 | .alignment = @alignOf(u64), |
| 2953 | 2919 | .flags = macho.S_THREAD_LOCAL_REGULAR, |
| ... | ... | @@ -4361,7 +4327,7 @@ pub fn logSymtab(self: *MachO) void { |
| 4361 | 4327 | log.debug("{}", .{self.stubs_table.fmtDebug(self)}); |
| 4362 | 4328 | |
| 4363 | 4329 | log.debug("threadlocal entries:", .{}); |
| 4364 | | log.debug("{}", .{self.tlvp_table.fmtDebug(self)}); |
| 4330 | log.debug("{}", .{self.tlv_table.fmtDebug(self)}); |
| 4365 | 4331 | } |
| 4366 | 4332 | |
| 4367 | 4333 | pub fn logAtoms(self: *MachO) void { |