Skip to content

Commit 4c4963d

Browse files
committed
macho: use MappedFile to parse input Dylibs
1 parent 2798249 commit 4c4963d

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/link/MachO.zig

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,14 +1456,6 @@ fn parseArchive(self: *MachO, path: []const u8, force_load: bool) !bool {
14561456
return true;
14571457
}
14581458

1459-
const ParseDylibError = error{
1460-
OutOfMemory,
1461-
EmptyStubFile,
1462-
MismatchedCpuArchitecture,
1463-
UnsupportedCpuArchitecture,
1464-
EndOfStream,
1465-
} || fs.File.OpenError || std.os.PReadError || Dylib.Id.ParseError;
1466-
14671459
const DylibCreateOpts = struct {
14681460
syslibroot: ?[]const u8,
14691461
id: ?Dylib.Id = null,
@@ -1472,12 +1464,12 @@ const DylibCreateOpts = struct {
14721464
weak: bool = false,
14731465
};
14741466

1475-
pub fn parseDylib(
1467+
fn parseDylib(
14761468
self: *MachO,
14771469
path: []const u8,
14781470
dependent_libs: anytype,
14791471
opts: DylibCreateOpts,
1480-
) ParseDylibError!bool {
1472+
) !bool {
14811473
const gpa = self.base.allocator;
14821474
const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) {
14831475
error.FileNotFound => return false,
@@ -1491,11 +1483,10 @@ pub fn parseDylib(
14911483
const reader = file.reader();
14921484
const fat_offset = math.cast(usize, try fat.getLibraryOffset(reader, cpu_arch)) orelse
14931485
return error.Overflow;
1494-
try file.seekTo(fat_offset);
14951486
file_size -= fat_offset;
14961487

1497-
const data = try file.readToEndAlloc(gpa, file_size);
1498-
defer gpa.free(data);
1488+
const data = try MappedFile.mapWithOptions(gpa, file, file_size, fat_offset);
1489+
defer data.unmap(gpa);
14991490

15001491
const dylib_id = @intCast(u16, self.dylibs.items.len);
15011492
var dylib = Dylib{ .weak = opts.weak };

src/link/MachO/Dylib.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const CrossTarget = std.zig.CrossTarget;
1515
const LibStub = @import("../tapi.zig").LibStub;
1616
const LoadCommandIterator = macho.LoadCommandIterator;
1717
const MachO = @import("../MachO.zig");
18+
const MappedFile = @import("../MappedFile.zig");
1819

1920
id: ?Id = null,
2021
weak: bool = false,
@@ -124,9 +125,10 @@ pub fn parseFromBinary(
124125
dylib_id: u16,
125126
dependent_libs: anytype,
126127
name: []const u8,
127-
data: []const u8,
128+
data: MappedFile,
128129
) !void {
129-
var stream = std.io.fixedBufferStream(data);
130+
const slice = data.slice();
131+
var stream = std.io.fixedBufferStream(slice);
130132
const reader = stream.reader();
131133

132134
log.debug("parsing shared library '{s}'", .{name});
@@ -151,17 +153,18 @@ pub fn parseFromBinary(
151153
const should_lookup_reexports = header.flags & macho.MH_NO_REEXPORTED_DYLIBS == 0;
152154
var it = LoadCommandIterator{
153155
.ncmds = header.ncmds,
154-
.buffer = data[@sizeOf(macho.mach_header_64)..][0..header.sizeofcmds],
156+
.buffer = slice[@sizeOf(macho.mach_header_64)..][0..header.sizeofcmds],
155157
};
156158
while (it.next()) |cmd| {
157159
switch (cmd.cmd()) {
158160
.SYMTAB => {
159161
const symtab_cmd = cmd.cast(macho.symtab_command).?;
162+
// SYMTAB in a final binary file (exe or lib) has to be aligned in file
160163
const symtab = @ptrCast(
161164
[*]const macho.nlist_64,
162-
@alignCast(@alignOf(macho.nlist_64), &data[symtab_cmd.symoff]),
165+
@alignCast(@alignOf(macho.nlist_64), slice.ptr + symtab_cmd.symoff),
163166
)[0..symtab_cmd.nsyms];
164-
const strtab = data[symtab_cmd.stroff..][0..symtab_cmd.strsize];
167+
const strtab = slice[symtab_cmd.stroff..][0..symtab_cmd.strsize];
165168

166169
for (symtab) |sym| {
167170
const add_to_symtab = sym.ext() and (sym.sect() or sym.indr());

0 commit comments

Comments
 (0)