diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2024-02-06 14:16:00 +0100 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2024-02-07 19:27:26 +0100 |
| commit | 80cafad9d32fed9f6a786f4d87f40b8ee622015e (patch) | |
| tree | 33323965e7ea9556608fce0972d927af0b0719e6 /src | |
| parent | 7f01b61679999bcd0ff644632a26e8c35e7541b6 (diff) | |
| download | zig-80cafad9d32fed9f6a786f4d87f40b8ee622015e.tar.gz zig-80cafad9d32fed9f6a786f4d87f40b8ee622015e.zip | |
macho: read-in committed ZigObject to memory from file
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/MachO/ZigObject.zig | 18 | ||||
| -rw-r--r-- | src/link/MachO/relocatable.zig | 4 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/link/MachO/ZigObject.zig b/src/link/MachO/ZigObject.zig index b39905f259..bdcc658e75 100644 --- a/src/link/MachO/ZigObject.zig +++ b/src/link/MachO/ZigObject.zig @@ -1,3 +1,4 @@ +data: std.ArrayListUnmanaged(u8) = .{}, /// Externally owned memory. path: []const u8, index: File.Index, @@ -57,6 +58,7 @@ pub fn init(self: *ZigObject, macho_file: *MachO) !void { } pub fn deinit(self: *ZigObject, allocator: Allocator) void { + self.data.deinit(allocator); self.symtab.deinit(allocator); self.strtab.deinit(allocator); self.symbols.deinit(allocator); @@ -279,6 +281,22 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, macho_file: *MachO) !vo } } +/// This is just a temporary helper function that allows us to re-read what we wrote to file into a buffer. +/// We need this so that we can write to an archive. +/// TODO implement writing ZigObject data directly to a buffer instead. +pub fn readFileContents(self: *ZigObject, macho_file: *MachO) !void { + const gpa = macho_file.base.comp.gpa; + var end_pos: u64 = 0; + for (macho_file.segments.items) |seg| { + end_pos = @max(end_pos, seg.fileoff + seg.filesize); + } + const size = std.math.cast(usize, end_pos) orelse return error.Overflow; + try self.data.resize(gpa, size); + + const amt = try macho_file.base.file.?.preadAll(self.data.items, 0); + if (amt != size) return error.InputOutput; +} + pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void { for (self.atoms.items) |atom_index| { const atom = macho_file.getAtom(atom_index) orelse continue; diff --git a/src/link/MachO/relocatable.zig b/src/link/MachO/relocatable.zig index 1b0aa74d23..37ea17d751 100644 --- a/src/link/MachO/relocatable.zig +++ b/src/link/MachO/relocatable.zig @@ -144,6 +144,10 @@ pub fn flushStaticLib(macho_file: *MachO, comp: *Compilation, module_obj_path: ? 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(macho_file); } var err = try macho_file.addErrorWithNotes(0); |
