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 @@...@@ -1,3 +1,4 @@
1data: std.ArrayListUnmanaged(u8) = .{},
1/// Externally owned memory.2/// Externally owned memory.
2path: []const u8,3path: []const u8,
3index: File.Index,4index: File.Index,
...@@ -57,6 +58,7 @@ pub fn init(self: *ZigObject, macho_file: *MachO) !void {...@@ -57,6 +58,7 @@ pub fn init(self: *ZigObject, macho_file: *MachO) !void {
57}58}
5859
59pub fn deinit(self: *ZigObject, allocator: Allocator) void {60pub fn deinit(self: *ZigObject, allocator: Allocator) void {
61 self.data.deinit(allocator);
60 self.symtab.deinit(allocator);62 self.symtab.deinit(allocator);
61 self.strtab.deinit(allocator);63 self.strtab.deinit(allocator);
62 self.symbols.deinit(allocator);64 self.symbols.deinit(allocator);
...@@ -279,6 +281,22 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, macho_file: *MachO) !vo...@@ -279,6 +281,22 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, macho_file: *MachO) !vo
279 }281 }
280}282}
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
282pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void {300pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void {
283 for (self.atoms.items) |atom_index| {301 for (self.atoms.items) |atom_index| {
284 const atom = macho_file.getAtom(atom_index) orelse continue;302 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: ?...@@ -144,6 +144,10 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ?
144144
145 const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);145 const ncmds, const sizeofcmds = try writeLoadCommands(macho_file);
146 try writeHeader(macho_file, ncmds, sizeofcmds);146 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);
147 }151 }
148152
149 var err = try macho_file.addErrorWithNotes(0);153 var err = try macho_file.addErrorWithNotes(0);