authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-06 14:16:00+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-07 19:27:26+01:00
log80cafad9d32fed9f6a786f4d87f40b8ee622015e
tree33323965e7ea9556608fce0972d927af0b0719e6
parent7f01b61679999bcd0ff644632a26e8c35e7541b6

macho: read-in committed ZigObject to memory from file


2 files changed, 22 insertions(+), 0 deletions(-)

src/link/MachO/ZigObject.zig+18
......@@ -1,3 +1,4 @@
1data: std.ArrayListUnmanaged(u8) = .{},
12/// Externally owned memory.
23path: []const u8,
34index: File.Index,
......@@ -57,6 +58,7 @@ pub fn init(self: *ZigObject, macho_file: *MachO) !void {
5758}
5859
5960pub fn deinit(self: *ZigObject, allocator: Allocator) void {
61 self.data.deinit(allocator);
6062 self.symtab.deinit(allocator);
6163 self.strtab.deinit(allocator);
6264 self.symbols.deinit(allocator);
......@@ -279,6 +281,22 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, macho_file: *MachO) !vo
279281 }
280282}
281283
284/// This is just a temporary helper function that allows us to re-read what we wrote to file into a buffer.
285/// We need this so that we can write to an archive.
286/// TODO implement writing ZigObject data directly to a buffer instead.
287pub fn readFileContents(self: *ZigObject, macho_file: *MachO) !void {
288 const gpa = macho_file.base.comp.gpa;
289 var end_pos: u64 = 0;
290 for (macho_file.segments.items) |seg| {
291 end_pos = @max(end_pos, seg.fileoff + seg.filesize);
292 }
293 const size = std.math.cast(usize, end_pos) orelse return error.Overflow;
294 try self.data.resize(gpa, size);
295
296 const amt = try macho_file.base.file.?.preadAll(self.data.items, 0);
297 if (amt != size) return error.InputOutput;
298}
299
282300pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void {
283301 for (self.atoms.items) |atom_index| {
284302 const atom = macho_file.getAtom(atom_index) orelse continue;
src/link/MachO/relocatable.zig+4
......@@ -144,6 +144,10 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
144144
145145 const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);
146146 try writeHeader(macho_file, ncmds, sizeofcmds);
147
148 // TODO we can avoid reading in the file contents we just wrote if we give the linker
149 // ability to write directly to a buffer.
150 try zo.readFileContents(macho_file);
147151 }
148152
149153 var err = try macho_file.addErrorWithNotes(0);