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,...@@ -137,8 +137,9 @@ got_section_index: ?u8 = null,
137data_const_section_index: ?u8 = null,137data_const_section_index: ?u8 = null,
138la_symbol_ptr_section_index: ?u8 = null,138la_symbol_ptr_section_index: ?u8 = null,
139data_section_index: ?u8 = null,139data_section_index: ?u8 = null,
140tls_vars_section_index: ?u8 = null,140thread_ptr_section_index: ?u8 = null,
141tls_data_section_index: ?u8 = null,141thread_vars_section_index: ?u8 = null,
142thread_data_section_index: ?u8 = null,
142143
143locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},144locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
144globals: std.ArrayListUnmanaged(SymbolWithLoc) = .{},145globals: std.ArrayListUnmanaged(SymbolWithLoc) = .{},
...@@ -1365,6 +1366,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !Atom.Index {...@@ -1365,6 +1366,7 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !Atom.Index {
1365 } else {1366 } else {
1366 try Atom.addRebase(self, atom_index, 0);1367 try Atom.addRebase(self, atom_index, 0);
1367 }1368 }
1369 try self.writePtrWidthAtom(atom_index);
13681370
1369 return atom_index;1371 return atom_index;
1370}1372}
...@@ -2071,7 +2073,7 @@ fn addGotEntry(self: *MachO, target: SymbolWithLoc) !void {...@@ -2071,7 +2073,7 @@ fn addGotEntry(self: *MachO, target: SymbolWithLoc) !void {
2071 const got_atom_index = try self.createGotAtom(target);2073 const got_atom_index = try self.createGotAtom(target);
2072 const got_atom = self.getAtom(got_atom_index);2074 const got_atom = self.getAtom(got_atom_index);
2073 self.got_table.entries.items[got_index].sym_index = got_atom.getSymbolIndex().?;2075 self.got_table.entries.items[got_index].sym_index = got_atom.getSymbolIndex().?;
2074 try self.writePtrWidthAtom(got_atom_index);2076 self.markRelocsDirtyByTarget(target);
2075}2077}
20762078
2077fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {2079fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {
...@@ -2087,6 +2089,10 @@ fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {...@@ -2087,6 +2089,10 @@ fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {
2087 self.markRelocsDirtyByTarget(target);2089 self.markRelocsDirtyByTarget(target);
2088}2090}
20892091
2092fn addTlvEntry(self: *MachO, target: SymbolWithLoc) !void {
2093 if (self.tlvp_table.lookup.contains(target)) return;
2094}
2095
2090pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {2096pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
2091 if (build_options.skip_non_native and builtin.object_format != .macho) {2097 if (build_options.skip_non_native and builtin.object_format != .macho) {
2092 @panic("Attempted to compile for object format that was disabled by build configuration");2098 @panic("Attempted to compile for object format that was disabled by build configuration");
...@@ -2366,7 +2372,6 @@ fn updateLazySymbolAtom(...@@ -2366,7 +2372,6 @@ fn updateLazySymbolAtom(
2366 symbol.n_value = vaddr;2372 symbol.n_value = vaddr;
23672373
2368 try self.addGotEntry(.{ .sym_index = local_sym_index });2374 try self.addGotEntry(.{ .sym_index = local_sym_index });
2369 self.markRelocsDirtyByTarget(atom.getSymbolWithLoc());
2370 try self.writeAtom(atom_index, code);2375 try self.writeAtom(atom_index, code);
2371}2376}
23722377
...@@ -2413,7 +2418,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {...@@ -2413,7 +2418,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
24132418
2414 if (val.castTag(.variable)) |variable| {2419 if (val.castTag(.variable)) |variable| {
2415 if (variable.data.is_threadlocal and !single_threaded) {2420 if (variable.data.is_threadlocal and !single_threaded) {
2416 break :blk self.tls_data_section_index.?;2421 break :blk self.thread_data_section_index.?;
2417 }2422 }
2418 break :blk self.data_section_index.?;2423 break :blk self.data_section_index.?;
2419 }2424 }
...@@ -2501,7 +2506,6 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []u8) !u64...@@ -2501,7 +2506,6 @@ fn updateDeclCode(self: *MachO, decl_index: Module.Decl.Index, code: []u8) !u64
2501 try self.addGotEntry(.{ .sym_index = sym_index });2506 try self.addGotEntry(.{ .sym_index = sym_index });
2502 }2507 }
25032508
2504 self.markRelocsDirtyByTarget(atom.getSymbolWithLoc());
2505 try self.writeAtom(atom_index, code);2509 try self.writeAtom(atom_index, code);
25062510
2507 return atom.getSymbol(self).n_value;2511 return atom.getSymbol(self).n_value;
...@@ -2835,8 +2839,17 @@ fn populateMissingMetadata(self: *MachO) !void {...@@ -2835,8 +2839,17 @@ fn populateMissingMetadata(self: *MachO) !void {
2835 }2839 }
28362840
2837 if (!self.base.options.single_threaded) {2841 if (!self.base.options.single_threaded) {
2838 if (self.tls_vars_section_index == null) {2842 if (self.thread_ptr_section_index == null) {
2839 self.tls_vars_section_index = try self.allocateSection("__DATA2", "__thread_vars", .{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", .{
2840 .size = @sizeOf(u64) * 3,2853 .size = @sizeOf(u64) * 3,
2841 .alignment = @alignOf(u64),2854 .alignment = @alignOf(u64),
2842 .flags = macho.S_THREAD_LOCAL_VARIABLES,2855 .flags = macho.S_THREAD_LOCAL_VARIABLES,
...@@ -2845,8 +2858,8 @@ fn populateMissingMetadata(self: *MachO) !void {...@@ -2845,8 +2858,8 @@ fn populateMissingMetadata(self: *MachO) !void {
2845 self.segment_table_dirty = true;2858 self.segment_table_dirty = true;
2846 }2859 }
28472860
2848 if (self.tls_data_section_index == null) {2861 if (self.thread_data_section_index == null) {
2849 self.tls_data_section_index = try self.allocateSection("__DATA3", "__thread_data", .{2862 self.thread_data_section_index = try self.allocateSection("__DATA4", "__thread_data", .{
2850 .size = @sizeOf(u64),2863 .size = @sizeOf(u64),
2851 .alignment = @alignOf(u64),2864 .alignment = @alignOf(u64),
2852 .flags = macho.S_THREAD_LOCAL_REGULAR,2865 .flags = macho.S_THREAD_LOCAL_REGULAR,