authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-06 07:59:48+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-13 11:47:51+02:00
log09b6bd86dea690aeb1fe1a832bb56b44b414120b
tree39395be5582b536883778c12582eb6d8df0e127f
parent57f9304275b7704fc2bd2879cdbb344c9f82e75c

macho: cleanup dirtying and writing GOT atoms


1 files changed, 23 insertions(+), 10 deletions(-)

src/link/MachO.zig+23-10
......@@ -137,8 +137,9 @@ got_section_index: ?u8 = null,
137137data_const_section_index: ?u8 = null,
138138la_symbol_ptr_section_index: ?u8 = null,
139139data_section_index: ?u8 = null,
140tls_vars_section_index: ?u8 = null,
141tls_data_section_index: ?u8 = null,
140thread_ptr_section_index: ?u8 = null,
141thread_vars_section_index: ?u8 = null,
142thread_data_section_index: ?u8 = null,
142143
143144locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
144145globals: std.ArrayListUnmanaged(SymbolWithLoc) = .{},
......@@ -1365,6 +1366,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !Atom.Index {
13651366 } else {
13661367 try Atom.addRebase(self, atom_index, 0);
13671368 }
1369 try self.writePtrWidthAtom(atom_index);
13681370
13691371 return atom_index;
13701372}
......@@ -2071,7 +2073,7 @@ fn addGotEntry(self: *MachO, target: SymbolWithLoc) !void {
20712073 const got_atom_index = try self.createGotAtom(target);
20722074 const got_atom = self.getAtom(got_atom_index);
20732075 self.got_table.entries.items[got_index].sym_index = got_atom.getSymbolIndex().?;
2074 try self.writePtrWidthAtom(got_atom_index);
2076 self.markRelocsDirtyByTarget(target);
20752077}
20762078
20772079fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {
......@@ -2087,6 +2089,10 @@ fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {
20872089 self.markRelocsDirtyByTarget(target);
20882090}
20892091
2092fn addTlvEntry(self: *MachO, target: SymbolWithLoc) !void {
2093 if (self.tlvp_table.lookup.contains(target)) return;
2094}
2095
20902096pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
20912097 if (build_options.skip_non_native and builtin.object_format != .macho) {
20922098 @panic("Attempted to compile for object format that was disabled by build configuration");
......@@ -2366,7 +2372,6 @@ fn updateLazySymbolAtom(
23662372 symbol.n_value = vaddr;
23672373
23682374 try self.addGotEntry(.{ .sym_index = local_sym_index });
2369 self.markRelocsDirtyByTarget(atom.getSymbolWithLoc());
23702375 try self.writeAtom(atom_index, code);
23712376}
23722377
......@@ -2413,7 +2418,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
24132418
24142419 if (val.castTag(.variable)) |variable| {
24152420 if (variable.data.is_threadlocal and !single_threaded) {
2416 break :blk self.tls_data_section_index.?;
2421 break :blk self.thread_data_section_index.?;
24172422 }
24182423 break :blk self.data_section_index.?;
24192424 }
......@@ -2501,7 +2506,6 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []u8) !u64
25012506 try self.addGotEntry(.{ .sym_index = sym_index });
25022507 }
25032508
2504 self.markRelocsDirtyByTarget(atom.getSymbolWithLoc());
25052509 try self.writeAtom(atom_index, code);
25062510
25072511 return atom.getSymbol(self).n_value;
......@@ -2835,8 +2839,17 @@ fn populateMissingMetadata(self: *MachO) !void {
28352839 }
28362840
28372841 if (!self.base.options.single_threaded) {
2838 if (self.tls_vars_section_index == null) {
2839 self.tls_vars_section_index = try self.allocateSection("__DATA2", "__thread_vars", .{
2842 if (self.thread_ptr_section_index == null) {
2843 self.thread_ptr_section_index = try self.allocateSection("__DATA2", "__thread_ptrs", .{
2844 .size = @sizeOf(u64),
2845 .alignment = @alignOf(u64),
2846 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
2847 .prot = macho.PROT.READ | macho.PROT.WRITE,
2848 });
2849 self.segment_table_dirty = true;
2850 }
2851 if (self.thread_vars_section_index == null) {
2852 self.thread_vars_section_index = try self.allocateSection("__DATA3", "__thread_vars", .{
28402853 .size = @sizeOf(u64) * 3,
28412854 .alignment = @alignOf(u64),
28422855 .flags = macho.S_THREAD_LOCAL_VARIABLES,
......@@ -2845,8 +2858,8 @@ fn populateMissingMetadata(self: *MachO) !void {
28452858 self.segment_table_dirty = true;
28462859 }
28472860
2848 if (self.tls_data_section_index == null) {
2849 self.tls_data_section_index = try self.allocateSection("__DATA3", "__thread_data", .{
2861 if (self.thread_data_section_index == null) {
2862 self.thread_data_section_index = try self.allocateSection("__DATA4", "__thread_data", .{
28502863 .size = @sizeOf(u64),
28512864 .alignment = @alignOf(u64),
28522865 .flags = macho.S_THREAD_LOCAL_REGULAR,