Commit 18778e2a0a

Jakub Konka <kubkon@jakubkonka.com>
2024-07-10 22:48:03
macho: bring back parts of ar
1 parent 174de37
src/link/MachO/file.zig
@@ -205,6 +205,8 @@ pub const File = union(enum) {
     pub fn updateArSymtab(file: File, ar_symtab: *Archive.ArSymtab, macho_file: *MachO) error{OutOfMemory}!void {
         return switch (file) {
             .dylib, .internal => unreachable,
+            // TODO
+            .zig_object => unreachable,
             inline else => |x| x.updateArSymtab(ar_symtab, macho_file),
         };
     }
@@ -212,7 +214,9 @@ pub const File = union(enum) {
     pub fn updateArSize(file: File, macho_file: *MachO) !void {
         return switch (file) {
             .dylib, .internal => unreachable,
-            .zig_object => |x| x.updateArSize(),
+            // TODO
+            .zig_object => unreachable,
+            // .zig_object => |x| x.updateArSize(),
             .object => |x| x.updateArSize(macho_file),
         };
     }
@@ -220,7 +224,9 @@ pub const File = union(enum) {
     pub fn writeAr(file: File, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
         return switch (file) {
             .dylib, .internal => unreachable,
-            .zig_object => |x| x.writeAr(ar_format, writer),
+            // TODO
+            .zig_object => unreachable,
+            // .zig_object => |x| x.writeAr(ar_format, writer),
             .object => |x| x.writeAr(ar_format, macho_file, writer),
         };
     }
src/link/MachO/Object.zig
@@ -77,6 +77,8 @@ pub fn parse(self: *Object, macho_file: *MachO) !void {
     const tracy = trace(@src());
     defer tracy.end();
 
+    log.debug("parsing {}", .{self.fmtPath()});
+
     const gpa = macho_file.base.comp.gpa;
     const handle = macho_file.getFileHandle(self.file_handle);
     const cpu_arch = macho_file.getTarget().cpu.arch;
src/link/MachO/relocatable.zig
@@ -127,55 +127,56 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
 
     if (comp.link_errors.items.len > 0) return error.FlushFailure;
 
-    // First, we flush relocatable object file generated with our backends.
-    if (macho_file.getZigObject()) |zo| {
-        zo.resolveSymbols(macho_file);
-        zo.asFile().markExportsRelocatable(macho_file);
-        zo.asFile().claimUnresolvedRelocatable(macho_file);
-        try macho_file.sortSections();
-        try macho_file.addAtomsToSections();
-        try calcSectionSizes(macho_file);
-        try createSegment(macho_file);
-        try allocateSections(macho_file);
-        allocateSegment(macho_file);
-
-        var off = off: {
-            const seg = macho_file.segments.items[0];
-            const off = math.cast(u32, seg.fileoff + seg.filesize) orelse return error.Overflow;
-            break :off mem.alignForward(u32, off, @alignOf(macho.relocation_info));
-        };
-        off = allocateSectionsRelocs(macho_file, off);
-
-        if (build_options.enable_logging) {
-            state_log.debug("{}", .{macho_file.dumpState()});
-        }
-
-        try macho_file.calcSymtabSize();
-        try writeAtoms(macho_file);
-
-        off = mem.alignForward(u32, off, @alignOf(u64));
-        off = try macho_file.writeDataInCode(0, off);
-        off = mem.alignForward(u32, off, @alignOf(u64));
-        off = try macho_file.writeSymtab(off);
-        off = mem.alignForward(u32, off, @alignOf(u64));
-        off = try macho_file.writeStrtab(off);
-
-        // In order to please Apple ld (and possibly other MachO linkers in the wild),
-        // we will now sanitize segment names of Zig-specific segments.
-        sanitizeZigSections(macho_file);
-
-        const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);
-        try writeHeader(macho_file, ncmds, sizeofcmds);
-
-        // TODO we can avoid reading in the file contents we just wrote if we give the linker
-        // ability to write directly to a buffer.
-        try zo.readFileContents(off, macho_file);
-    }
+    // TODO re-enable
+    // // First, we flush relocatable object file generated with our backends.
+    // if (macho_file.getZigObject()) |zo| {
+    //     zo.resolveSymbols(macho_file);
+    //     zo.asFile().markExportsRelocatable(macho_file);
+    //     zo.asFile().claimUnresolvedRelocatable(macho_file);
+    //     try macho_file.sortSections();
+    //     try macho_file.addAtomsToSections();
+    //     try calcSectionSizes(macho_file);
+    //     try createSegment(macho_file);
+    //     try allocateSections(macho_file);
+    //     allocateSegment(macho_file);
+
+    //     var off = off: {
+    //         const seg = macho_file.segments.items[0];
+    //         const off = math.cast(u32, seg.fileoff + seg.filesize) orelse return error.Overflow;
+    //         break :off mem.alignForward(u32, off, @alignOf(macho.relocation_info));
+    //     };
+    //     off = allocateSectionsRelocs(macho_file, off);
+
+    //     if (build_options.enable_logging) {
+    //         state_log.debug("{}", .{macho_file.dumpState()});
+    //     }
+
+    //     try macho_file.calcSymtabSize();
+    //     try writeAtoms(macho_file);
+
+    //     off = mem.alignForward(u32, off, @alignOf(u64));
+    //     off = try macho_file.writeDataInCode(0, off);
+    //     off = mem.alignForward(u32, off, @alignOf(u64));
+    //     off = try macho_file.writeSymtab(off);
+    //     off = mem.alignForward(u32, off, @alignOf(u64));
+    //     off = try macho_file.writeStrtab(off);
+
+    //     // In order to please Apple ld (and possibly other MachO linkers in the wild),
+    //     // we will now sanitize segment names of Zig-specific segments.
+    //     sanitizeZigSections(macho_file);
+
+    //     const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);
+    //     try writeHeader(macho_file, ncmds, sizeofcmds);
+
+    //     // TODO we can avoid reading in the file contents we just wrote if we give the linker
+    //     // ability to write directly to a buffer.
+    //     try zo.readFileContents(off, macho_file);
+    // }
 
     var files = std.ArrayList(File.Index).init(gpa);
     defer files.deinit();
     try files.ensureTotalCapacityPrecise(macho_file.objects.items.len + 1);
-    if (macho_file.getZigObject()) |zo| files.appendAssumeCapacity(zo.index);
+    // if (macho_file.getZigObject()) |zo| files.appendAssumeCapacity(zo.index);
     for (macho_file.objects.items) |index| files.appendAssumeCapacity(index);
 
     const format: Archive.Format = .p32;
src/link/MachO/Relocation.zig
@@ -41,7 +41,7 @@ pub fn getGotTargetAddress(rel: Relocation, atom: Atom, macho_file: *MachO) u64
 }
 
 pub fn getZigGotTargetAddress(rel: Relocation, macho_file: *MachO) u64 {
-    const zo = macho_file.getZigObject().?;
+    const zo = macho_file.getZigObject() orelse return 0;
     return switch (rel.tag) {
         .local => 0,
         .@"extern" => {
src/link/MachO.zig
@@ -356,7 +356,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n
     if (comp.verbose_link) try self.dumpArgv(comp);
 
     if (self.getZigObject()) |zo| try zo.flushModule(self, tid);
-    // if (self.base.isStaticLib()) return relocatable.flushStaticLib(self, comp, module_obj_path);
+    if (self.base.isStaticLib()) return relocatable.flushStaticLib(self, comp, module_obj_path);
     // if (self.base.isObject()) return relocatable.flushObject(self, comp, module_obj_path);
 
     var positionals = std.ArrayList(Compilation.LinkObject).init(gpa);