macho: move getAtomData switch into Atom

This commit is contained in:
Jakub Konka 2024-01-28 00:49:59 +01:00
parent 6337ce16ae
commit 8cdaaa7b4f
2 changed files with 12 additions and 7 deletions

View File

@ -613,7 +613,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node
const atom_size = math.cast(usize, atom.size) orelse return error.Overflow; const atom_size = math.cast(usize, atom.size) orelse return error.Overflow;
const code = try gpa.alloc(u8, atom_size); const code = try gpa.alloc(u8, atom_size);
defer gpa.free(code); defer gpa.free(code);
zo.getAtomData(self, atom.*, code) catch |err| switch (err) { atom.getData(self, code) catch |err| switch (err) {
error.InputOutput => { error.InputOutput => {
try self.reportUnexpectedError("fetching code for '{s}' failed", .{ try self.reportUnexpectedError("fetching code for '{s}' failed", .{
atom.getName(self), atom.getName(self),
@ -2591,12 +2591,7 @@ fn writeAtoms(self: *MachO) !void {
assert(atom.flags.alive); assert(atom.flags.alive);
const off = math.cast(usize, atom.value - header.addr) orelse return error.Overflow; const off = math.cast(usize, atom.value - header.addr) orelse return error.Overflow;
const atom_size = math.cast(usize, atom.size) orelse return error.Overflow; const atom_size = math.cast(usize, atom.size) orelse return error.Overflow;
switch (atom.getFile(self)) { try atom.getData(self, buffer[off..][0..atom_size]);
.internal => |x| try x.getAtomData(atom.*, buffer[off..][0..atom_size]),
.object => |x| try x.getAtomData(atom.*, buffer[off..][0..atom_size]),
.zig_object => |x| try x.getAtomData(self, atom.*, buffer[off..][0..atom_size]),
else => unreachable,
}
atom.resolveRelocs(self, buffer[off..][0..atom_size]) catch |err| switch (err) { atom.resolveRelocs(self, buffer[off..][0..atom_size]) catch |err| switch (err) {
error.ResolveFailed => has_resolve_error = true, error.ResolveFailed => has_resolve_error = true,
else => |e| return e, else => |e| return e,

View File

@ -54,6 +54,16 @@ pub fn getFile(self: Atom, macho_file: *MachO) File {
return macho_file.getFile(self.file).?; return macho_file.getFile(self.file).?;
} }
pub fn getData(self: Atom, macho_file: *MachO, buffer: []u8) !void {
assert(buffer.len == self.size);
switch (self.getFile(macho_file)) {
.internal => |x| try x.getAtomData(self, buffer),
.object => |x| try x.getAtomData(self, buffer),
.zig_object => |x| try x.getAtomData(macho_file, self, buffer),
else => unreachable,
}
}
pub fn getRelocs(self: Atom, macho_file: *MachO) []const Relocation { pub fn getRelocs(self: Atom, macho_file: *MachO) []const Relocation {
return switch (self.getFile(macho_file)) { return switch (self.getFile(macho_file)) {
.dylib => unreachable, .dylib => unreachable,