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 {
16011601
16021602 if (self.dyld_stub_binder_index) |index| {
16031603 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;
16051605 }
16061606
16071607 if (self.objc_msg_send_index) |index| {
16081608 const sym = self.getSymbol(index);
16091609 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?
16111611 }
16121612
16131613 for (self.symbols.items, 0..) |*symbol, i| {
16141614 const index = @as(Symbol.Index, @intCast(i));
1615 if (symbol.flags.got) {
1615 if (symbol.flags.needs_got) {
16161616 log.debug("'{s}' needs GOT", .{symbol.getName(self)});
16171617 try self.got.addSymbol(index, self);
16181618 }
src/link/MachO/Atom.zig+3-3
......@@ -204,7 +204,7 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void {
204204 (symbol.flags.@"export" and (symbol.flags.weak or symbol.flags.interposable)) or
205205 macho_file.getTarget().cpu.arch == .aarch64) // TODO relax on arm64
206206 {
207 symbol.flags.got = true;
207 symbol.flags.needs_got = true;
208208 if (symbol.flags.weak) {
209209 macho_file.binds_to_weak = true;
210210 }
......@@ -212,7 +212,7 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void {
212212 },
213213
214214 .got => {
215 rel.getTargetSymbol(macho_file).flags.got = true;
215 rel.getTargetSymbol(macho_file).flags.needs_got = true;
216216 },
217217
218218 .tlv,
......@@ -452,7 +452,7 @@ fn resolveRelocInner(
452452 assert(rel.tag == .@"extern");
453453 assert(rel.meta.length == 2);
454454 assert(rel.meta.pcrel);
455 if (rel.getTargetSymbol(macho_file).flags.got) {
455 if (rel.getTargetSymbol(macho_file).flags.has_got) {
456456 try writer.writeInt(i32, @intCast(G + A - P), .little);
457457 } else {
458458 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 {
11051105 if (!rec.alive) continue;
11061106 if (rec.getFde(macho_file)) |fde| {
11071107 if (fde.getCie(macho_file).getPersonality(macho_file)) |sym| {
1108 sym.flags.got = true;
1108 sym.flags.needs_got = true;
11091109 }
11101110 } else if (rec.getPersonality(macho_file)) |sym| {
1111 sym.flags.got = true;
1111 sym.flags.needs_got = true;
11121112 }
11131113 }
11141114}
src/link/MachO/Symbol.zig+3-2
......@@ -118,7 +118,7 @@ pub fn getAddress(symbol: Symbol, opts: struct {
118118}
119119
120120pub fn getGotAddress(symbol: Symbol, macho_file: *MachO) u64 {
121 if (!symbol.flags.got) return 0;
121 if (!symbol.flags.has_got) return 0;
122122 const extra = symbol.getExtra(macho_file).?;
123123 return macho_file.got.getAddress(extra.got, macho_file);
124124}
......@@ -349,7 +349,8 @@ pub const Flags = packed struct {
349349 output_symtab: bool = false,
350350
351351 /// Whether the symbol contains __got indirection.
352 got: bool = false,
352 needs_got: bool = false,
353 has_got: bool = false,
353354
354355 /// Whether the symbols contains __stubs indirection.
355356 stubs: bool = false,
src/link/MachO/ZigObject.zig+82-5
......@@ -197,11 +197,88 @@ pub fn updateDecl(
197197 mod: *Module,
198198 decl_index: InternPool.DeclIndex,
199199) link.File.UpdateDeclError!void {
200 _ = self;
201 _ = macho_file;
202 _ = mod;
203 _ = decl_index;
204 @panic("TODO updateDecl");
200 const tracy = trace(@src());
201 defer tracy.end();
202
203 const decl = mod.declPtr(decl_index);
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));
205282}
206283
207284pub fn lowerUnnamedConst(
src/link/MachO/synthetic.zig+1
......@@ -13,6 +13,7 @@ pub const GotSection = struct {
1313 const entry = try got.symbols.addOne(gpa);
1414 entry.* = sym_index;
1515 const symbol = macho_file.getSymbol(sym_index);
16 symbol.flags.has_got = true;
1617 try symbol.addExtra(.{ .got = index }, macho_file);
1718 }
1819