Skip to content

Commit

Permalink
macho: do not store file and path in Archive
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Jan 27, 2024
1 parent 45adee2 commit 79640c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ fn parseArchive(self: *MachO, obj: LinkObject) !bool {

const gpa = self.base.allocator;
const file = try std.fs.cwd().openFile(obj.path, .{});
defer file.close();

const fat_arch: ?fat.Arch = if (fat.isFatLibrary(file)) blk: {
break :blk self.parseFatLibrary(obj.path, file) catch |err| switch (err) {
Expand All @@ -715,9 +716,9 @@ fn parseArchive(self: *MachO, obj: LinkObject) !bool {
if (!mem.eql(u8, &magic, Archive.ARMAG)) return false;
try file.seekTo(0);

var archive = Archive{ .path = obj.path, .file = file, .fat_arch = fat_arch };
var archive = Archive{};
defer archive.deinit(gpa);
try archive.parse(self);
try archive.parse(self, obj.path, file, fat_arch);

var has_parse_error = false;
for (archive.objects.items) |extracted| {
Expand Down
27 changes: 11 additions & 16 deletions src/MachO/Archive.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
file: std.fs.File,
fat_arch: ?fat.Arch,
path: []const u8,

objects: std.ArrayListUnmanaged(Object) = .{},

// Archive files start with the ARMAG identifying string. Then follows a
Expand Down Expand Up @@ -63,25 +59,24 @@ const ar_hdr = extern struct {
};

pub fn deinit(self: *Archive, allocator: Allocator) void {
self.file.close();
self.objects.deinit(allocator);
}

pub fn parse(self: *Archive, macho_file: *MachO) !void {
pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, file: std.fs.File, fat_arch: ?fat.Arch) !void {
const gpa = macho_file.base.allocator;

const offset = if (self.fat_arch) |ar| ar.offset else 0;
const size = if (self.fat_arch) |ar| ar.size else (try self.file.stat()).size;
try self.file.seekTo(offset);
const offset = if (fat_arch) |ar| ar.offset else 0;
const size = if (fat_arch) |ar| ar.size else (try file.stat()).size;
try file.seekTo(offset);

const reader = self.file.reader();
const reader = file.reader();
_ = try reader.readBytesNoEof(Archive.SARMAG);

var pos: usize = Archive.SARMAG;
while (true) {
if (pos >= size) break;
if (!mem.isAligned(pos, 2)) {
try self.file.seekBy(1);
try file.seekBy(1);
pos += 1;
}

Expand All @@ -90,7 +85,7 @@ pub fn parse(self: *Archive, macho_file: *MachO) !void {

if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) {
macho_file.base.fatal("{s}: invalid header delimiter: expected '{s}', found '{s}'", .{
self.path, std.fmt.fmtSliceEscapeLower(ARFMAG), std.fmt.fmtSliceEscapeLower(&hdr.ar_fmag),
path, std.fmt.fmtSliceEscapeLower(ARFMAG), std.fmt.fmtSliceEscapeLower(&hdr.ar_fmag),
});
return error.ParseFailed;
}
Expand All @@ -109,25 +104,25 @@ pub fn parse(self: *Archive, macho_file: *MachO) !void {
unreachable;
};
defer {
_ = self.file.seekBy(hdr_size) catch {};
_ = file.seekBy(hdr_size) catch {};
pos += hdr_size;
}

if (mem.eql(u8, name, "__.SYMDEF") or mem.eql(u8, name, "__.SYMDEF SORTED")) continue;

const object = Object{
.archive = .{
.path = try gpa.dupe(u8, self.path),
.path = try gpa.dupe(u8, path),
.offset = offset + pos,
},
.path = name,
.file = try std.fs.cwd().openFile(self.path, .{}),
.file = try std.fs.cwd().openFile(path, .{}),
.index = undefined,
.alive = false,
.mtime = hdr.date() catch 0,
};

log.debug("extracting object '{s}' from archive '{s}'", .{ object.path, self.path });
log.debug("extracting object '{s}' from archive '{s}'", .{ object.path, path });

try self.objects.append(gpa, object);
}
Expand Down

0 comments on commit 79640c9

Please sign in to comment.