authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-17 07:17:58+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:41+01:00
log9509fadbe38e77bc0f8b079c4d9def2937d81322
treecd2501bd022c7bab315944c7839165db63ac6011
parentb66911370b3a5376c8f383dc0a187ffe9c3bbeb2

macho: split symbol.flags.got into needs_got and has_got


6 files changed, 94 insertions(+), 15 deletions(-)

src/link/MachO.zig+3-3
...@@ -1601,18 +1601,18 @@ fn scanRelocs(self: *MachO) !void {...@@ -1601,18 +1601,18 @@ fn scanRelocs(self: *MachO) !void {
16011601
1602 if (self.dyld_stub_binder_index) |index| {1602 if (self.dyld_stub_binder_index) |index| {
1603 const sym = self.getSymbol(index);1603 const sym = self.getSymbol(index);
1604 if (sym.getFile(self) != null) sym.flags.got = true;1604 if (sym.getFile(self) != null) sym.flags.needs_got = true;
1605 }1605 }
16061606
1607 if (self.objc_msg_send_index) |index| {1607 if (self.objc_msg_send_index) |index| {
1608 const sym = self.getSymbol(index);1608 const sym = self.getSymbol(index);
1609 if (sym.getFile(self) != null)1609 if (sym.getFile(self) != null)
1610 sym.flags.got = true; // TODO is it always needed, or only if we are synthesising fast stubs?1610 sym.flags.needs_got = true; // TODO is it always needed, or only if we are synthesising fast stubs?
1611 }1611 }
16121612
1613 for (self.symbols.items, 0..) |*symbol, i| {1613 for (self.symbols.items, 0..) |*symbol, i| {
1614 const index = @as(Symbol.Index, @intCast(i));1614 const index = @as(Symbol.Index, @intCast(i));
1615 if (symbol.flags.got) {1615 if (symbol.flags.needs_got) {
1616 log.debug("'{s}' needs GOT", .{symbol.getName(self)});1616 log.debug("'{s}' needs GOT", .{symbol.getName(self)});
1617 try self.got.addSymbol(index, self);1617 try self.got.addSymbol(index, self);
1618 }1618 }
src/link/MachO/Atom.zig+3-3
...@@ -204,7 +204,7 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void {...@@ -204,7 +204,7 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void {
204 (symbol.flags.@"export" and (symbol.flags.weak or symbol.flags.interposable)) or204 (symbol.flags.@"export" and (symbol.flags.weak or symbol.flags.interposable)) or
205 macho_file.getTarget().cpu.arch == .aarch64) // TODO relax on arm64205 macho_file.getTarget().cpu.arch == .aarch64) // TODO relax on arm64
206 {206 {
207 symbol.flags.got = true;207 symbol.flags.needs_got = true;
208 if (symbol.flags.weak) {208 if (symbol.flags.weak) {
209 macho_file.binds_to_weak = true;209 macho_file.binds_to_weak = true;
210 }210 }
...@@ -212,7 +212,7 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void {...@@ -212,7 +212,7 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void {
212 },212 },
213213
214 .got => {214 .got => {
215 rel.getTargetSymbol(macho_file).flags.got = true;215 rel.getTargetSymbol(macho_file).flags.needs_got = true;
216 },216 },
217217
218 .tlv,218 .tlv,
...@@ -452,7 +452,7 @@ fn resolveRelocInner(...@@ -452,7 +452,7 @@ fn resolveRelocInner(
452 assert(rel.tag == .@"extern");452 assert(rel.tag == .@"extern");
453 assert(rel.meta.length == 2);453 assert(rel.meta.length == 2);
454 assert(rel.meta.pcrel);454 assert(rel.meta.pcrel);
455 if (rel.getTargetSymbol(macho_file).flags.got) {455 if (rel.getTargetSymbol(macho_file).flags.has_got) {
456 try writer.writeInt(i32, @intCast(G + A - P), .little);456 try writer.writeInt(i32, @intCast(G + A - P), .little);
457 } else {457 } else {
458 try x86_64.relaxGotLoad(code[rel_offset - 3 ..]);458 try x86_64.relaxGotLoad(code[rel_offset - 3 ..]);
src/link/MachO/Object.zig+2-2
...@@ -1105,10 +1105,10 @@ pub fn scanRelocs(self: Object, macho_file: *MachO) !void {...@@ -1105,10 +1105,10 @@ pub fn scanRelocs(self: Object, macho_file: *MachO) !void {
1105 if (!rec.alive) continue;1105 if (!rec.alive) continue;
1106 if (rec.getFde(macho_file)) |fde| {1106 if (rec.getFde(macho_file)) |fde| {
1107 if (fde.getCie(macho_file).getPersonality(macho_file)) |sym| {1107 if (fde.getCie(macho_file).getPersonality(macho_file)) |sym| {
1108 sym.flags.got = true;1108 sym.flags.needs_got = true;
1109 }1109 }
1110 } else if (rec.getPersonality(macho_file)) |sym| {1110 } else if (rec.getPersonality(macho_file)) |sym| {
1111 sym.flags.got = true;1111 sym.flags.needs_got = true;
1112 }1112 }
1113 }1113 }
1114}1114}
src/link/MachO/Symbol.zig+3-2
...@@ -118,7 +118,7 @@ pub fn getAddress(symbol: Symbol, opts: struct {...@@ -118,7 +118,7 @@ pub fn getAddress(symbol: Symbol, opts: struct {
118}118}
119119
120pub fn getGotAddress(symbol: Symbol, macho_file: *MachO) u64 {120pub fn getGotAddress(symbol: Symbol, macho_file: *MachO) u64 {
121 if (!symbol.flags.got) return 0;121 if (!symbol.flags.has_got) return 0;
122 const extra = symbol.getExtra(macho_file).?;122 const extra = symbol.getExtra(macho_file).?;
123 return macho_file.got.getAddress(extra.got, macho_file);123 return macho_file.got.getAddress(extra.got, macho_file);
124}124}
...@@ -349,7 +349,8 @@ pub const Flags = packed struct {...@@ -349,7 +349,8 @@ pub const Flags = packed struct {
349 output_symtab: bool = false,349 output_symtab: bool = false,
350350
351 /// Whether the symbol contains __got indirection.351 /// Whether the symbol contains __got indirection.
352 got: bool = false,352 needs_got: bool = false,
353 has_got: bool = false,
353354
354 /// Whether the symbols contains __stubs indirection.355 /// Whether the symbols contains __stubs indirection.
355 stubs: bool = false,356 stubs: bool = false,
src/link/MachO/ZigObject.zig+82-5
...@@ -197,11 +197,88 @@ pub fn updateDecl(...@@ -197,11 +197,88 @@ pub fn updateDecl(
197 mod: *Module,197 mod: *Module,
198 decl_index: InternPool.DeclIndex,198 decl_index: InternPool.DeclIndex,
199) link.File.UpdateDeclError!void {199) link.File.UpdateDeclError!void {
200 _ = self;200 const tracy = trace(@src());
201 _ = macho_file;201 defer tracy.end();
202 _ = mod;202
203 _ = decl_index;203 const decl = mod.declPtr(decl_index);
204 @panic("TODO updateDecl");204
205 if (decl.val.getExternFunc(mod)) |_| {
206 return;
207 }
208
209 if (decl.isExtern(mod)) {
210 // Extern variable gets a __got entry only
211 const variable = decl.getOwnedVariable(mod).?;
212 const name = mod.intern_pool.stringToSlice(decl.name);
213 const lib_name = mod.intern_pool.stringToSliceUnwrap(variable.lib_name);
214 const index = try self.getGlobalSymbol(macho_file, name, lib_name);
215 macho_file.getSymbol(index).flags.needs_got = true;
216 return;
217 }
218
219 // const is_threadlocal = if (decl.val.getVariable(mod)) |variable|
220 // variable.is_threadlocal and comp.config.any_non_single_threaded
221 // else
222 // false;
223 // if (is_threadlocal) return self.updateThreadlocalVariable(mod, decl_index);
224
225 // const atom_index = try self.getOrCreateAtomForDecl(decl_index);
226 // const sym_index = self.getAtom(atom_index).getSymbolIndex().?;
227 // Atom.freeRelocations(self, atom_index);
228
229 // const comp = macho_file.base.comp;
230 // const gpa = comp.gpa;
231
232 // var code_buffer = std.ArrayList(u8).init(gpa);
233 // defer code_buffer.deinit();
234
235 // var decl_state: ?Dwarf.DeclState = if (self.d_sym) |*d_sym|
236 // try d_sym.dwarf.initDeclState(mod, decl_index)
237 // else
238 // null;
239 // defer if (decl_state) |*ds| ds.deinit();
240
241 // const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
242 // const res = if (decl_state) |*ds|
243 // try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
244 // .ty = decl.ty,
245 // .val = decl_val,
246 // }, &code_buffer, .{
247 // .dwarf = ds,
248 // }, .{
249 // .parent_atom_index = sym_index,
250 // })
251 // else
252 // try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
253 // .ty = decl.ty,
254 // .val = decl_val,
255 // }, &code_buffer, .none, .{
256 // .parent_atom_index = sym_index,
257 // });
258
259 // const code = switch (res) {
260 // .ok => code_buffer.items,
261 // .fail => |em| {
262 // decl.analysis = .codegen_failure;
263 // try mod.failed_decls.put(mod.gpa, decl_index, em);
264 // return;
265 // },
266 // };
267 // const addr = try self.updateDeclCode(decl_index, code);
268
269 // if (decl_state) |*ds| {
270 // try self.d_sym.?.dwarf.commitDeclState(
271 // mod,
272 // decl_index,
273 // addr,
274 // self.getAtom(atom_index).size,
275 // ds,
276 // );
277 // }
278
279 // // Since we updated the vaddr and the size, each corresponding export symbol also
280 // // needs to be updated.
281 // try self.updateExports(mod, .{ .decl_index = decl_index }, mod.getDeclExports(decl_index));
205}282}
206283
207pub fn lowerUnnamedConst(284pub fn lowerUnnamedConst(
src/link/MachO/synthetic.zig+1
...@@ -13,6 +13,7 @@ pub const GotSection = struct {...@@ -13,6 +13,7 @@ pub const GotSection = struct {
13 const entry = try got.symbols.addOne(gpa);13 const entry = try got.symbols.addOne(gpa);
14 entry.* = sym_index;14 entry.* = sym_index;
15 const symbol = macho_file.getSymbol(sym_index);15 const symbol = macho_file.getSymbol(sym_index);
16 symbol.flags.has_got = true;
16 try symbol.addExtra(.{ .got = index }, macho_file);17 try symbol.addExtra(.{ .got = index }, macho_file);
17 }18 }
1819