authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-07 12:20:38+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-07 19:27:26+01:00
logefa1c6124d167b3144c4d4b15ebf384130d35abd
treeeb0c4757fa06819b748249b2fee5d598212ddf06
parent897a554109baa3288d575cac0833e10edd1a316c

macho: emit an archive


6 files changed, 118 insertions(+), 41 deletions(-)

src/link/MachO.zig+2-2
......@@ -380,7 +380,7 @@ pub fn deinit(self: *MachO) void {
380380
381381pub fn flush(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node) link.File.FlushError!void {
382382 // TODO: I think this is just a temp and can be removed once we can emit static archives
383 if (self.base.isStaticLib() and build_options.have_llvm) {
383 if (self.base.isStaticLib() and build_options.have_llvm and self.base.comp.config.use_llvm) {
384384 return self.base.linkAsArchive(arena, prog_node);
385385 }
386386 try self.flushModule(arena, prog_node);
......@@ -396,7 +396,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node
396396 if (self.llvm_object) |llvm_object| {
397397 try self.base.emitLlvmObject(arena, llvm_object, prog_node);
398398 // TODO: I think this is just a temp and can be removed once we can emit static archives
399 if (self.base.isStaticLib() and build_options.have_llvm) return;
399 if (self.base.isStaticLib() and build_options.have_llvm and self.base.comp.config.use_llvm) return;
400400 }
401401
402402 var sub_prog_node = prog_node.start("MachO Flush", 0);
src/link/MachO/Archive.zig+30-31
......@@ -90,7 +90,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index:
9090
9191pub fn writeHeader(
9292 object_name: []const u8,
93 object_size: u32,
93 object_size: usize,
9494 format: Format,
9595 writer: anytype,
9696) !void {
......@@ -110,7 +110,7 @@ pub fn writeHeader(
110110 }
111111 @memcpy(&hdr.ar_fmag, ARFMAG);
112112
113 const object_name_len = mem.alignForward(u32, object_name.len + 1, format.ptrWidth());
113 const object_name_len = mem.alignForward(usize, object_name.len + 1, ptrWidth(format));
114114 const total_object_size = object_size + object_name_len;
115115
116116 {
......@@ -142,12 +142,12 @@ pub const SARMAG: u4 = 8;
142142/// String in ar_fmag at the end of each header.
143143const ARFMAG: *const [2:0]u8 = "`\n";
144144
145const SYMDEF = "__.SYMDEF";
146const SYMDEF64 = "__.SYMDEF_64";
147const SYMDEF_SORTED = "__.SYMDEF SORTED";
148const SYMDEF64_SORTED = "__.SYMDEF_64 SORTED";
145pub const SYMDEF = "__.SYMDEF";
146pub const SYMDEF64 = "__.SYMDEF_64";
147pub const SYMDEF_SORTED = "__.SYMDEF SORTED";
148pub const SYMDEF64_SORTED = "__.SYMDEF_64 SORTED";
149149
150const ar_hdr = extern struct {
150pub const ar_hdr = extern struct {
151151 /// Member file name, sometimes / terminated.
152152 ar_name: [16]u8,
153153
......@@ -197,7 +197,6 @@ const ar_hdr = extern struct {
197197pub const ArSymtab = struct {
198198 entries: std.ArrayListUnmanaged(Entry) = .{},
199199 strtab: StringTable = .{},
200 format: Format = .p32,
201200
202201 pub fn deinit(ar: *ArSymtab, allocator: Allocator) void {
203202 ar.entries.deinit(allocator);
......@@ -208,16 +207,16 @@ pub const ArSymtab = struct {
208207 mem.sort(Entry, ar.entries.items, {}, Entry.lessThan);
209208 }
210209
211 pub fn size(ar: ArSymtab) usize {
212 const ptr_width = ar.format.ptrWidth();
210 pub fn size(ar: ArSymtab, format: Format) usize {
211 const ptr_width = ptrWidth(format);
213212 return ptr_width + ar.entries.items.len * 2 * ptr_width + ptr_width + mem.alignForward(usize, ar.strtab.buffer.items.len, ptr_width);
214213 }
215214
216 pub fn write(ar: ArSymtab, macho_file: *MachO, writer: anytype) !void {
215 pub fn write(ar: ArSymtab, format: Format, macho_file: *MachO, writer: anytype) !void {
217216 // Header
218 try writeHeader(SYMDEF, ar.size());
217 try writeHeader(SYMDEF, ar.size(format), format, writer);
219218 // Symtab size
220 try ar.writeInt(ar.entries.items.len * 2);
219 try writeInt(format, ar.entries.items.len * 2, writer);
221220 // Symtab entries
222221 for (ar.entries.items) |entry| {
223222 const file_off = switch (macho_file.getFile(entry.file).?) {
......@@ -226,14 +225,14 @@ pub const ArSymtab = struct {
226225 else => unreachable,
227226 };
228227 // Name offset
229 try ar.writeInt(entry.off);
228 try writeInt(format, entry.off, writer);
230229 // File offset
231 try ar.writeInt(file_off);
230 try writeInt(format, file_off, writer);
232231 }
233232 // Strtab size
234 const strtab_size = mem.alignForward(u64, ar.strtab.buffer.items.len, ar.format.ptrWidth());
233 const strtab_size = mem.alignForward(u64, ar.strtab.buffer.items.len, ptrWidth(format));
235234 const padding = strtab_size - ar.strtab.buffer.items.len;
236 try ar.writeInt(strtab_size);
235 try writeInt(format, strtab_size, writer);
237236 // Strtab
238237 try writer.writeAll(ar.strtab.buffer.items);
239238 if (padding > 0) {
......@@ -241,13 +240,6 @@ pub const ArSymtab = struct {
241240 }
242241 }
243242
244 fn writeInt(ar: ArSymtab, value: u64, writer: anytype) !void {
245 switch (ar.format) {
246 .p32 => try writer.writeInt(u32, std.math.cast(u32, value) orelse return error.Overflow, .little),
247 .p64 => try writer.writeInt(u64, value, .little),
248 }
249 }
250
251243 const FormatContext = struct {
252244 ar: ArSymtab,
253245 macho_file: *MachO,
......@@ -288,17 +280,24 @@ pub const ArSymtab = struct {
288280 };
289281};
290282
291const Format = enum {
283pub const Format = enum {
292284 p32,
293285 p64,
286};
294287
295 fn ptrWidth(self: Format) usize {
296 return switch (self) {
297 .p32 => @as(usize, 4),
298 .p64 => 8,
299 };
288pub fn ptrWidth(format: Format) usize {
289 return switch (format) {
290 .p32 => @as(usize, 4),
291 .p64 => 8,
292 };
293}
294
295pub fn writeInt(format: Format, value: u64, writer: anytype) !void {
296 switch (format) {
297 .p32 => try writer.writeInt(u32, std.math.cast(u32, value) orelse return error.Overflow, .little),
298 .p64 => try writer.writeInt(u64, value, .little),
300299 }
301};
300}
302301
303302pub const ArState = struct {
304303 /// File offset of the ar_hdr describing the contributing
src/link/MachO/Object.zig+4-3
......@@ -1248,14 +1248,15 @@ pub fn updateArSize(self: *Object, macho_file: *MachO) !void {
12481248 self.output_ar_state.size = size;
12491249}
12501250
1251pub fn writeAr(self: Object, macho_file: *MachO, writer: anytype) !void {
1251pub fn writeAr(self: Object, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
12521252 // Header
1253 try Archive.writeHeader(self.path, self.output_ar_state.size, writer);
1253 const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;
1254 try Archive.writeHeader(self.path, size, ar_format, writer);
12541255 // Data
12551256 const file = macho_file.getFileHandle(self.file_handle);
12561257 // TODO try using copyRangeAll
12571258 const gpa = macho_file.base.comp.gpa;
1258 const data = try file.readToEndAlloc(gpa, self.output_ar_state.size);
1259 const data = try file.readToEndAlloc(gpa, size);
12591260 defer gpa.free(data);
12601261 try writer.writeAll(data);
12611262}
src/link/MachO/ZigObject.zig+3-2
......@@ -314,9 +314,10 @@ pub fn updateArSize(self: *ZigObject) void {
314314 self.output_ar_state.size = self.data.items.len;
315315}
316316
317pub fn writeAr(self: ZigObject, writer: anytype) !void {
317pub fn writeAr(self: ZigObject, ar_format: Archive.Format, writer: anytype) !void {
318318 // Header
319 try Archive.writeHeader(self.path, self.output_ar_state.size, writer);
319 const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;
320 try Archive.writeHeader(self.path, size, ar_format, writer);
320321 // Data
321322 try writer.writeAll(self.data.items);
322323}
src/link/MachO/file.zig+16
......@@ -182,6 +182,22 @@ pub const File = union(enum) {
182182 };
183183 }
184184
185 pub fn updateArSize(file: File, macho_file: *MachO) !void {
186 return switch (file) {
187 .dylib, .internal => unreachable,
188 .zig_object => |x| x.updateArSize(),
189 .object => |x| x.updateArSize(macho_file),
190 };
191 }
192
193 pub fn writeAr(file: File, ar_format: Archive.Format, macho_file: *MachO, writer: anytype) !void {
194 return switch (file) {
195 .dylib, .internal => unreachable,
196 .zig_object => |x| x.writeAr(ar_format, writer),
197 .object => |x| x.writeAr(ar_format, macho_file, writer),
198 };
199 }
200
185201 pub fn calcSymtabSize(file: File, macho_file: *MachO) !void {
186202 return switch (file) {
187203 inline else => |x| x.calcSymtabSize(macho_file),
src/link/MachO/relocatable.zig+63-3
......@@ -161,6 +161,9 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
161161 if (macho_file.getZigObject()) |zo| files.appendAssumeCapacity(zo.index);
162162 for (macho_file.objects.items) |index| files.appendAssumeCapacity(index);
163163
164 const format: Archive.Format = .p32;
165 const ptr_width = Archive.ptrWidth(format);
166
164167 // Update ar symtab from parsed objects
165168 var ar_symtab: Archive.ArSymtab = .{};
166169 defer ar_symtab.deinit(gpa);
......@@ -171,14 +174,71 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
171174
172175 ar_symtab.sort();
173176
177 // Update sizes of contributing objects
178 for (files.items) |index| {
179 try macho_file.getFile(index).?.updateArSize(macho_file);
180 }
181
182 // Update file offsets of contributing objects
183 const total_size: usize = blk: {
184 var pos: usize = Archive.SARMAG;
185 pos += @sizeOf(Archive.ar_hdr) + Archive.SYMDEF.len + 1;
186 pos = mem.alignForward(usize, pos, ptr_width);
187 pos += ar_symtab.size(format);
188
189 for (files.items) |index| {
190 const file = macho_file.getFile(index).?;
191 const state = switch (file) {
192 .zig_object => |x| &x.output_ar_state,
193 .object => |x| &x.output_ar_state,
194 else => unreachable,
195 };
196 const path = switch (file) {
197 .zig_object => |x| x.path,
198 .object => |x| x.path,
199 else => unreachable,
200 };
201 pos = mem.alignForward(usize, pos, ptr_width);
202 state.file_off = pos;
203 pos += @sizeOf(Archive.ar_hdr) + path.len + 1;
204 pos = mem.alignForward(usize, pos, ptr_width);
205 pos += math.cast(usize, state.size) orelse return error.Overflow;
206 }
207
208 break :blk pos;
209 };
210
174211 if (build_options.enable_logging) {
175212 state_log.debug("ar_symtab\n{}\n", .{ar_symtab.fmt(macho_file)});
176213 }
177214
178 var err = try macho_file.addErrorWithNotes(0);
179 try err.addMsg(macho_file, "TODO implement flushStaticLib", .{});
215 var buffer = std.ArrayList(u8).init(gpa);
216 defer buffer.deinit();
217 try buffer.ensureTotalCapacityPrecise(total_size);
218 const writer = buffer.writer();
219
220 // Write magic
221 try writer.writeAll(Archive.ARMAG);
180222
181 return error.FlushFailure;
223 // Write symtab
224 try ar_symtab.write(format, macho_file, writer);
225
226 // Write object files
227 for (files.items) |index| {
228 const aligned = mem.alignForward(usize, buffer.items.len, ptr_width);
229 const padding = aligned - buffer.items.len;
230 if (padding > 0) {
231 try writer.writeByteNTimes(0, padding);
232 }
233 try macho_file.getFile(index).?.writeAr(format, macho_file, writer);
234 }
235
236 assert(buffer.items.len == total_size);
237
238 try macho_file.base.file.?.setEndPos(total_size);
239 try macho_file.base.file.?.pwriteAll(buffer.items, 0);
240
241 if (comp.link_errors.items.len > 0) return error.FlushFailure;
182242}
183243
184244fn markExports(macho_file: *MachO) void {