| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | | pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u8) link.File.FlushError!void { |
| 1 | pub fn flushObject(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u8) link.File.FlushError!void { |
| 2 | 2 | const gpa = macho_file.base.comp.gpa; |
| 3 | 3 | |
| 4 | 4 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); |
| ... | ... | @@ -86,6 +86,72 @@ pub fn flush(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u |
| 86 | 86 | try writeHeader(macho_file, ncmds, sizeofcmds); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]const u8) link.File.FlushError!void { |
| 90 | const gpa = comp.gpa; |
| 91 | |
| 92 | var positionals = std.ArrayList(Compilation.LinkObject).init(gpa); |
| 93 | defer positionals.deinit(); |
| 94 | |
| 95 | try positionals.ensureUnusedCapacity(comp.objects.len); |
| 96 | positionals.appendSliceAssumeCapacity(comp.objects); |
| 97 | |
| 98 | for (comp.c_object_table.keys()) |key| { |
| 99 | try positionals.append(.{ .path = key.status.success.object_path }); |
| 100 | } |
| 101 | |
| 102 | if (module_obj_path) |path| try positionals.append(.{ .path = path }); |
| 103 | |
| 104 | for (positionals.items) |obj| { |
| 105 | // TODO: parse for archive meaning don't unpack objects |
| 106 | _ = obj; |
| 107 | } |
| 108 | |
| 109 | if (comp.link_errors.items.len > 0) return error.FlushFailure; |
| 110 | |
| 111 | // First, we flush relocatable object file generated with our backends. |
| 112 | if (macho_file.getZigObject()) |zo| { |
| 113 | zo.resolveSymbols(macho_file); |
| 114 | zo.asFile().claimUnresolvedRelocatable(macho_file); |
| 115 | try macho_file.sortSections(); |
| 116 | try macho_file.addAtomsToSections(); |
| 117 | try calcSectionSizes(macho_file); |
| 118 | try createSegment(macho_file); |
| 119 | try allocateSections(macho_file); |
| 120 | allocateSegment(macho_file); |
| 121 | |
| 122 | var off = off: { |
| 123 | const seg = macho_file.segments.items[0]; |
| 124 | const off = math.cast(u32, seg.fileoff + seg.filesize) orelse return error.Overflow; |
| 125 | break :off mem.alignForward(u32, off, @alignOf(macho.relocation_info)); |
| 126 | }; |
| 127 | off = allocateSectionsRelocs(macho_file, off); |
| 128 | |
| 129 | state_log.debug("{}", .{macho_file.dumpState()}); |
| 130 | |
| 131 | try macho_file.calcSymtabSize(); |
| 132 | try writeAtoms(macho_file); |
| 133 | |
| 134 | off = mem.alignForward(u32, off, @alignOf(u64)); |
| 135 | off = try macho_file.writeDataInCode(0, off); |
| 136 | off = mem.alignForward(u32, off, @alignOf(u64)); |
| 137 | off = try macho_file.writeSymtab(off); |
| 138 | off = mem.alignForward(u32, off, @alignOf(u64)); |
| 139 | off = try macho_file.writeStrtab(off); |
| 140 | |
| 141 | // In order to please Apple ld (and possibly other MachO linkers in the wild), |
| 142 | // we will now sanitize segment names of Zig-specific segments. |
| 143 | sanitizeZigSections(macho_file); |
| 144 | |
| 145 | const ncmds, const sizeofcmds = try writeLoadCommands(macho_file); |
| 146 | try writeHeader(macho_file, ncmds, sizeofcmds); |
| 147 | } |
| 148 | |
| 149 | var err = try macho_file.addErrorWithNotes(0); |
| 150 | try err.addMsg(macho_file, "TODO implement flushStaticLib", .{}); |
| 151 | |
| 152 | return error.FlushFailure; |
| 153 | } |
| 154 | |
| 89 | 155 | fn markExports(macho_file: *MachO) void { |
| 90 | 156 | if (macho_file.getZigObject()) |zo| { |
| 91 | 157 | zo.asFile().markExportsRelocatable(macho_file); |