authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-16 23:14:05+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:41+01:00
log8437ba6a795f015ad34c33b8ac768def24cfbc5e
tree3c822940dcac67b9bc46bd9a9efefb2ca17ba9d0
parent667af6511ff5f2973d48a962e507bf526259eb0f

macho: patch up more holes with ZigObject


2 files changed, 70 insertions(+), 24 deletions(-)

src/link/MachO.zig+42-24
......@@ -236,31 +236,32 @@ pub fn createEmpty(
236236 try self.getZigObject().?.init(self);
237237
238238 // TODO init metadata
239 // TODO init dwarf
239240
240 if (comp.config.debug_format != .strip) {
241 // Create dSYM bundle.
242 log.debug("creating {s}.dSYM bundle", .{emit.sub_path});
241 // if (comp.config.debug_format != .strip) {
242 // // Create dSYM bundle.
243 // log.debug("creating {s}.dSYM bundle", .{emit.sub_path});
243244
244 const d_sym_path = try std.fmt.allocPrint(
245 arena,
246 "{s}.dSYM" ++ fs.path.sep_str ++ "Contents" ++ fs.path.sep_str ++ "Resources" ++ fs.path.sep_str ++ "DWARF",
247 .{emit.sub_path},
248 );
245 // const d_sym_path = try std.fmt.allocPrint(
246 // arena,
247 // "{s}.dSYM" ++ fs.path.sep_str ++ "Contents" ++ fs.path.sep_str ++ "Resources" ++ fs.path.sep_str ++ "DWARF",
248 // .{emit.sub_path},
249 // );
249250
250 var d_sym_bundle = try emit.directory.handle.makeOpenPath(d_sym_path, .{});
251 defer d_sym_bundle.close();
251 // var d_sym_bundle = try emit.directory.handle.makeOpenPath(d_sym_path, .{});
252 // defer d_sym_bundle.close();
252253
253 const d_sym_file = try d_sym_bundle.createFile(emit.sub_path, .{
254 .truncate = false,
255 .read = true,
256 });
254 // const d_sym_file = try d_sym_bundle.createFile(emit.sub_path, .{
255 // .truncate = false,
256 // .read = true,
257 // });
257258
258 self.d_sym = .{
259 .allocator = gpa,
260 .dwarf = link.File.Dwarf.init(&self.base, .dwarf32),
261 .file = d_sym_file,
262 };
263 }
259 // self.d_sym = .{
260 // .allocator = gpa,
261 // .dwarf = link.File.Dwarf.init(&self.base, .dwarf32),
262 // .file = d_sym_file,
263 // };
264 // }
264265 }
265266 }
266267
......@@ -379,6 +380,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node
379380 // --verbose-link
380381 if (comp.verbose_link) try self.dumpArgv(comp);
381382
383 if (self.getZigObject()) |zo| try zo.flushModule(self);
382384 if (self.base.isStaticLib()) return self.flushStaticLib(comp, module_obj_path);
383385 if (self.base.isObject()) return relocatable.flush(self, comp, module_obj_path);
384386
......@@ -1311,6 +1313,8 @@ pub fn resolveSymbols(self: *MachO) !void {
13111313 const tracy = trace(@src());
13121314 defer tracy.end();
13131315
1316 // Resolve symbols in the ZigObject. For now, we assume that it's always live.
1317 if (self.getZigObject()) |zo| zo.asFile().resolveSymbols(self);
13141318 // Resolve symbols on the set of all objects and shared objects (even if some are unneeded).
13151319 for (self.objects.items) |index| self.getFile(index).?.resolveSymbols(self);
13161320 for (self.dylibs.items) |index| self.getFile(index).?.resolveSymbols(self);
......@@ -1319,6 +1323,7 @@ pub fn resolveSymbols(self: *MachO) !void {
13191323 self.markLive();
13201324
13211325 // Reset state of all globals after marking live objects.
1326 if (self.getZigObject()) |zo| zo.asFile().resetGlobals(self);
13221327 for (self.objects.items) |index| self.getFile(index).?.resetGlobals(self);
13231328 for (self.dylibs.items) |index| self.getFile(index).?.resetGlobals(self);
13241329
......@@ -1332,6 +1337,7 @@ pub fn resolveSymbols(self: *MachO) !void {
13321337 }
13331338
13341339 // Re-resolve the symbols.
1340 if (self.getZigObject()) |zo| zo.resolveSymbols(self);
13351341 for (self.objects.items) |index| self.getFile(index).?.resolveSymbols(self);
13361342 for (self.dylibs.items) |index| self.getFile(index).?.resolveSymbols(self);
13371343}
......@@ -1351,6 +1357,7 @@ fn markLive(self: *MachO) void {
13511357 if (file == .object) file.object.alive = true;
13521358 }
13531359 }
1360 if (self.getZigObject()) |zo| zo.markLive(self);
13541361 for (self.objects.items) |index| {
13551362 const object = self.getFile(index).?.object;
13561363 if (object.alive) object.markLive(self);
......@@ -1449,12 +1456,23 @@ fn createObjcSections(self: *MachO) !void {
14491456}
14501457
14511458fn claimUnresolved(self: *MachO) error{OutOfMemory}!void {
1452 for (self.objects.items) |index| {
1453 const object = self.getFile(index).?.object;
1459 const gpa = self.base.comp.gpa;
14541460
1455 for (object.symbols.items, 0..) |sym_index, i| {
1461 var objects = try std.ArrayList(File.Index).initCapacity(gpa, self.objects.items.len + 1);
1462 defer objects.deinit();
1463 if (self.getZigObject()) |zo| objects.appendAssumeCapacity(zo.index);
1464 objects.appendSliceAssumeCapacity(self.objects.items);
1465
1466 for (objects.items) |index| {
1467 const file = self.getFile(index).?;
1468
1469 for (file.getSymbols(), 0..) |sym_index, i| {
14561470 const nlist_idx = @as(Symbol.Index, @intCast(i));
1457 const nlist = object.symtab.items(.nlist)[nlist_idx];
1471 const nlist = switch (file) {
1472 .object => |x| x.symtab.items(.nlist)[nlist_idx],
1473 .zig_object => |x| x.symtab.items(.nlist)[nlist_idx],
1474 else => unreachable,
1475 };
14581476 if (!nlist.ext()) continue;
14591477 if (!nlist.undf()) continue;
14601478
src/link/MachO/ZigObject.zig+28
......@@ -49,6 +49,24 @@ pub fn resetGlobals(self: *ZigObject, macho_file: *MachO) void {
4949 }
5050}
5151
52pub fn markLive(self: *ZigObject, macho_file: *MachO) void {
53 const tracy = trace(@src());
54 defer tracy.end();
55
56 for (self.symbols.items, 0..) |index, nlist_idx| {
57 const nlist = self.symtab.items(.nlist)[nlist_idx];
58 if (!nlist.ext()) continue;
59
60 const sym = macho_file.getSymbol(index);
61 const file = sym.getFile(macho_file) orelse continue;
62 const should_keep = nlist.undf() or (nlist.tentative() and !sym.flags.tentative);
63 if (should_keep and file == .object and !file.object.alive) {
64 file.object.alive = true;
65 file.object.markLive(macho_file);
66 }
67 }
68}
69
5270pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) !void {
5371 const tracy = trace(@src());
5472 defer tracy.end();
......@@ -102,6 +120,12 @@ pub fn getInputSection(self: ZigObject, atom: Atom, macho_file: *MachO) macho.se
102120 return sect;
103121}
104122
123pub fn flushModule(self: *ZigObject, macho_file: *MachO) !void {
124 _ = self;
125 _ = macho_file;
126 @panic("TODO flushModule");
127}
128
105129pub fn getDeclVAddr(
106130 self: *ZigObject,
107131 macho_file: *MachO,
......@@ -241,6 +265,10 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l
241265 @panic("TODO getGlobalSymbol");
242266}
243267
268pub fn asFile(self: *ZigObject) File {
269 return .{ .zig_object = self };
270}
271
244272pub fn fmtSymtab(self: *ZigObject, macho_file: *MachO) std.fmt.Formatter(formatSymtab) {
245273 return .{ .data = .{
246274 .self = self,