Commit 8cdaaa7b4f

Jakub Konka <kubkon@jakubkonka.com>
2024-01-28 00:49:59
macho: move getAtomData switch into Atom
1 parent 6337ce1
Changed files (2)
src
src/link/MachO/Atom.zig
@@ -54,6 +54,16 @@ pub fn getFile(self: Atom, macho_file: *MachO) 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 {
     return switch (self.getFile(macho_file)) {
         .dylib => unreachable,
src/link/MachO.zig
@@ -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 code = try gpa.alloc(u8, atom_size);
             defer gpa.free(code);
-            zo.getAtomData(self, atom.*, code) catch |err| switch (err) {
+            atom.getData(self, code) catch |err| switch (err) {
                 error.InputOutput => {
                     try self.reportUnexpectedError("fetching code for '{s}' failed", .{
                         atom.getName(self),
@@ -2591,12 +2591,7 @@ fn writeAtoms(self: *MachO) !void {
             assert(atom.flags.alive);
             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;
-            switch (atom.getFile(self)) {
-                .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,
-            }
+            try atom.getData(self, buffer[off..][0..atom_size]);
             atom.resolveRelocs(self, buffer[off..][0..atom_size]) catch |err| switch (err) {
                 error.ResolveFailed => has_resolve_error = true,
                 else => |e| return e,