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(...@@ -236,31 +236,32 @@ pub fn createEmpty(
236 try self.getZigObject().?.init(self);236 try self.getZigObject().?.init(self);
237237
238 // TODO init metadata238 // TODO init metadata
239 // TODO init dwarf
239240
240 if (comp.config.debug_format != .strip) {241 // if (comp.config.debug_format != .strip) {
241 // Create dSYM bundle.242 // // Create dSYM bundle.
242 log.debug("creating {s}.dSYM bundle", .{emit.sub_path});243 // log.debug("creating {s}.dSYM bundle", .{emit.sub_path});
243244
244 const d_sym_path = try std.fmt.allocPrint(245 // const d_sym_path = try std.fmt.allocPrint(
245 arena,246 // arena,
246 "{s}.dSYM" ++ fs.path.sep_str ++ "Contents" ++ fs.path.sep_str ++ "Resources" ++ fs.path.sep_str ++ "DWARF",247 // "{s}.dSYM" ++ fs.path.sep_str ++ "Contents" ++ fs.path.sep_str ++ "Resources" ++ fs.path.sep_str ++ "DWARF",
247 .{emit.sub_path},248 // .{emit.sub_path},
248 );249 // );
249250
250 var d_sym_bundle = try emit.directory.handle.makeOpenPath(d_sym_path, .{});251 // var d_sym_bundle = try emit.directory.handle.makeOpenPath(d_sym_path, .{});
251 defer d_sym_bundle.close();252 // defer d_sym_bundle.close();
252253
253 const d_sym_file = try d_sym_bundle.createFile(emit.sub_path, .{254 // const d_sym_file = try d_sym_bundle.createFile(emit.sub_path, .{
254 .truncate = false,255 // .truncate = false,
255 .read = true,256 // .read = true,
256 });257 // });
257258
258 self.d_sym = .{259 // self.d_sym = .{
259 .allocator = gpa,260 // .allocator = gpa,
260 .dwarf = link.File.Dwarf.init(&self.base, .dwarf32),261 // .dwarf = link.File.Dwarf.init(&self.base, .dwarf32),
261 .file = d_sym_file,262 // .file = d_sym_file,
262 };263 // };
263 }264 // }
264 }265 }
265 }266 }
266267
...@@ -379,6 +380,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node...@@ -379,6 +380,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node
379 // --verbose-link380 // --verbose-link
380 if (comp.verbose_link) try self.dumpArgv(comp);381 if (comp.verbose_link) try self.dumpArgv(comp);
381382
383 if (self.getZigObject()) |zo| try zo.flushModule(self);
382 if (self.base.isStaticLib()) return self.flushStaticLib(comp, module_obj_path);384 if (self.base.isStaticLib()) return self.flushStaticLib(comp, module_obj_path);
383 if (self.base.isObject()) return relocatable.flush(self, comp, module_obj_path);385 if (self.base.isObject()) return relocatable.flush(self, comp, module_obj_path);
384386
...@@ -1311,6 +1313,8 @@ pub fn resolveSymbols(self: *MachO) !void {...@@ -1311,6 +1313,8 @@ pub fn resolveSymbols(self: *MachO) !void {
1311 const tracy = trace(@src());1313 const tracy = trace(@src());
1312 defer tracy.end();1314 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);
1314 // Resolve symbols on the set of all objects and shared objects (even if some are unneeded).1318 // Resolve symbols on the set of all objects and shared objects (even if some are unneeded).
1315 for (self.objects.items) |index| self.getFile(index).?.resolveSymbols(self);1319 for (self.objects.items) |index| self.getFile(index).?.resolveSymbols(self);
1316 for (self.dylibs.items) |index| self.getFile(index).?.resolveSymbols(self);1320 for (self.dylibs.items) |index| self.getFile(index).?.resolveSymbols(self);
...@@ -1319,6 +1323,7 @@ pub fn resolveSymbols(self: *MachO) !void {...@@ -1319,6 +1323,7 @@ pub fn resolveSymbols(self: *MachO) !void {
1319 self.markLive();1323 self.markLive();
13201324
1321 // Reset state of all globals after marking live objects.1325 // Reset state of all globals after marking live objects.
1326 if (self.getZigObject()) |zo| zo.asFile().resetGlobals(self);
1322 for (self.objects.items) |index| self.getFile(index).?.resetGlobals(self);1327 for (self.objects.items) |index| self.getFile(index).?.resetGlobals(self);
1323 for (self.dylibs.items) |index| self.getFile(index).?.resetGlobals(self);1328 for (self.dylibs.items) |index| self.getFile(index).?.resetGlobals(self);
13241329
...@@ -1332,6 +1337,7 @@ pub fn resolveSymbols(self: *MachO) !void {...@@ -1332,6 +1337,7 @@ pub fn resolveSymbols(self: *MachO) !void {
1332 }1337 }
13331338
1334 // Re-resolve the symbols.1339 // Re-resolve the symbols.
1340 if (self.getZigObject()) |zo| zo.resolveSymbols(self);
1335 for (self.objects.items) |index| self.getFile(index).?.resolveSymbols(self);1341 for (self.objects.items) |index| self.getFile(index).?.resolveSymbols(self);
1336 for (self.dylibs.items) |index| self.getFile(index).?.resolveSymbols(self);1342 for (self.dylibs.items) |index| self.getFile(index).?.resolveSymbols(self);
1337}1343}
...@@ -1351,6 +1357,7 @@ fn markLive(self: *MachO) void {...@@ -1351,6 +1357,7 @@ fn markLive(self: *MachO) void {
1351 if (file == .object) file.object.alive = true;1357 if (file == .object) file.object.alive = true;
1352 }1358 }
1353 }1359 }
1360 if (self.getZigObject()) |zo| zo.markLive(self);
1354 for (self.objects.items) |index| {1361 for (self.objects.items) |index| {
1355 const object = self.getFile(index).?.object;1362 const object = self.getFile(index).?.object;
1356 if (object.alive) object.markLive(self);1363 if (object.alive) object.markLive(self);
...@@ -1449,12 +1456,23 @@ fn createObjcSections(self: *MachO) !void {...@@ -1449,12 +1456,23 @@ fn createObjcSections(self: *MachO) !void {
1449}1456}
14501457
1451fn claimUnresolved(self: *MachO) error{OutOfMemory}!void {1458fn claimUnresolved(self: *MachO) error{OutOfMemory}!void {
1452 for (self.objects.items) |index| {1459 const gpa = self.base.comp.gpa;
1453 const object = self.getFile(index).?.object;
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| {
1456 const nlist_idx = @as(Symbol.Index, @intCast(i));1470 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 };
1458 if (!nlist.ext()) continue;1476 if (!nlist.ext()) continue;
1459 if (!nlist.undf()) continue;1477 if (!nlist.undf()) continue;
14601478
src/link/MachO/ZigObject.zig+28
...@@ -49,6 +49,24 @@ pub fn resetGlobals(self: *ZigObject, macho_file: *MachO) void {...@@ -49,6 +49,24 @@ pub fn resetGlobals(self: *ZigObject, macho_file: *MachO) void {
49 }49 }
50}50}
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
52pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) !void {70pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) !void {
53 const tracy = trace(@src());71 const tracy = trace(@src());
54 defer tracy.end();72 defer tracy.end();
...@@ -102,6 +120,12 @@ pub fn getInputSection(self: ZigObject, atom: Atom, macho_file: *MachO) macho.se...@@ -102,6 +120,12 @@ pub fn getInputSection(self: ZigObject, atom: Atom, macho_file: *MachO) macho.se
102 return sect;120 return sect;
103}121}
104122
123pub fn flushModule(self: *ZigObject, macho_file: *MachO) !void {
124 _ = self;
125 _ = macho_file;
126 @panic("TODO flushModule");
127}
128
105pub fn getDeclVAddr(129pub fn getDeclVAddr(
106 self: *ZigObject,130 self: *ZigObject,
107 macho_file: *MachO,131 macho_file: *MachO,
...@@ -241,6 +265,10 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l...@@ -241,6 +265,10 @@ pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, l
241 @panic("TODO getGlobalSymbol");265 @panic("TODO getGlobalSymbol");
242}266}
243267
268pub fn asFile(self: *ZigObject) File {
269 return .{ .zig_object = self };
270}
271
244pub fn fmtSymtab(self: *ZigObject, macho_file: *MachO) std.fmt.Formatter(formatSymtab) {272pub fn fmtSymtab(self: *ZigObject, macho_file: *MachO) std.fmt.Formatter(formatSymtab) {
245 return .{ .data = .{273 return .{ .data = .{
246 .self = self,274 .self = self,