Skip to content

Commit

Permalink
macho: report formatted error when CPU archs are mismatched in objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Aug 5, 2023
1 parent 2804038 commit f4cbe83
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ pub fn flush(self: *MachO) !void {
try self.parseLibs(libs.keys(), libs.values(), syslibroot, &dependent_libs);
try self.parseDependentLibs(syslibroot, &dependent_libs);

self.base.reportWarningsAndErrorsAndExit();

var resolver = SymbolResolver{
.arena = arena,
.table = std.StringHashMap(u32).init(arena),
Expand Down Expand Up @@ -652,7 +654,7 @@ fn parseObject(self: *MachO, path: []const u8) !bool {
.contents = contents,
};

object.parse(gpa, cpu_arch) catch |err| switch (err) {
object.parse(gpa, cpu_arch, self) catch |err| switch (err) {
error.EndOfStream, error.NotObject => {
object.deinit(gpa);
return false;
Expand Down Expand Up @@ -703,7 +705,7 @@ fn parseArchive(self: *MachO, path: []const u8, force_load: bool) !bool {
}
}
for (offsets.keys()) |off| {
const object = try archive.parseObject(gpa, cpu_arch, off);
const object = try archive.parseObject(gpa, cpu_arch, off, self);
try self.objects.append(gpa, object);
}
} else {
Expand Down Expand Up @@ -1650,7 +1652,7 @@ fn resolveSymbolsInArchives(self: *MachO, resolver: *SymbolResolver) !void {
assert(offsets.items.len > 0);

const object_id = @as(u16, @intCast(self.objects.items.len));
const object = try archive.parseObject(gpa, cpu_arch, offsets.items[0]);
const object = try archive.parseObject(gpa, cpu_arch, offsets.items[0], self);
try self.objects.append(gpa, object);
try self.resolveSymbolsInObject(object_id, resolver);

Expand Down
8 changes: 4 additions & 4 deletions src/MachO/Archive.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const macho = std.macho;
const mem = std.mem;

const Allocator = mem.Allocator;
const MachO = @import("../MachO.zig");
const Object = @import("Object.zig");

file: fs.File,
Expand Down Expand Up @@ -186,6 +187,7 @@ pub fn parseObject(
gpa: Allocator,
cpu_arch: std.Target.Cpu.Arch,
offset: u32,
macho_file: *MachO,
) !Object {
const reader = self.file.reader();
try reader.context.seekTo(self.fat_offset + offset);
Expand Down Expand Up @@ -216,17 +218,15 @@ pub fn parseObject(
const object_size = (try object_header.size()) - object_name_len;
const contents = try gpa.allocWithOptions(u8, object_size, @alignOf(u64), null);
const amt = try reader.readAll(contents);
if (amt != object_size) {
return error.Io;
}
if (amt != object_size) return error.Io;

var object = Object{
.name = name,
.mtime = object_header.date() catch 0,
.contents = contents,
};

try object.parse(gpa, cpu_arch);
try object.parse(gpa, cpu_arch, macho_file);

return object;
}
16 changes: 11 additions & 5 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ pub fn deinit(self: *Object, gpa: Allocator) void {
self.data_in_code.deinit(gpa);
}

pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) !void {
pub fn parse(
self: *Object,
allocator: Allocator,
cpu_arch: std.Target.Cpu.Arch,
macho_file: *MachO,
) !void {
const tracy = trace(@src());
defer tracy.end();

Expand All @@ -135,16 +140,17 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
macho.CPU_TYPE_ARM64 => .aarch64,
macho.CPU_TYPE_X86_64 => .x86_64,
else => |value| {
log.err("unsupported cpu architecture 0x{x}", .{value});
return error.UnsupportedCpuArchitecture;
macho_file.base.fatal("unsupported cpu architecture 0x{x}", .{value});
return;
},
};
if (this_arch != cpu_arch) {
log.err("mismatched cpu architecture: expected {s}, found {s}", .{
macho_file.base.fatal("{s}: mismatched cpu architecture: expected {s}, found {s}", .{
self.name,
@tagName(cpu_arch),
@tagName(this_arch),
});
return error.MismatchedCpuArchitecture;
return;
}

var it = LoadCommandIterator{
Expand Down

0 comments on commit f4cbe83

Please sign in to comment.