From 020cb4d30286da6488f1c5885a40955b4d8ba440 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 11 Jul 2024 21:11:11 +0200 Subject: [PATCH] macho: fix symbol visibility merging logic --- src/MachO/Atom.zig | 18 ------------------ src/MachO/Object.zig | 2 +- src/MachO/Symbol.zig | 25 ++++++++++++++----------- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/src/MachO/Atom.zig b/src/MachO/Atom.zig index 0b996035..e2659df4 100644 --- a/src/MachO/Atom.zig +++ b/src/MachO/Atom.zig @@ -66,24 +66,6 @@ pub fn getPriority(self: Atom, macho_file: *MachO) u64 { return (@as(u64, @intCast(file.getIndex())) << 32) | @as(u64, @intCast(self.n_sect)); } -pub fn getCode(self: Atom, macho_file: *MachO, buffer: []u8) !void { - assert(buffer.len == self.size); - switch (self.getFile(macho_file)) { - .dylib => unreachable, - .object => |x| { - const slice = x.sections.slice(); - const file = macho_file.getFileHandle(x.file_handle); - const sect = slice.items(.header)[self.n_sect]; - const amt = try file.preadAll(buffer, sect.offset + x.offset + self.off); - if (amt != buffer.len) return error.InputOutput; - }, - .internal => |x| { - const code = x.getSectionData(self.n_sect); - @memcpy(buffer, code); - }, - } -} - pub fn getRelocs(self: Atom, macho_file: *MachO) []const Relocation { const relocs = switch (self.getFile(macho_file)) { .dylib => unreachable, diff --git a/src/MachO/Object.zig b/src/MachO/Object.zig index fbef069a..263e247e 100644 --- a/src/MachO/Object.zig +++ b/src/MachO/Object.zig @@ -1532,7 +1532,7 @@ pub fn mergeSymbolVisibility(self: *Object, macho_file: *MachO) void { for (self.symbols.items, 0..) |sym, i| { const ref = self.getSymbolRef(@intCast(i), macho_file); const global = ref.getSymbol(macho_file) orelse continue; - if (global.visibility != .global) { + if (sym.visibility.rank() < global.visibility.rank()) { global.visibility = sym.visibility; } if (sym.flags.weak_ref) { diff --git a/src/MachO/Symbol.zig b/src/MachO/Symbol.zig index e33d0868..e06acd43 100644 --- a/src/MachO/Symbol.zig +++ b/src/MachO/Symbol.zig @@ -314,9 +314,14 @@ fn format2( if (symbol.getAtom(ctx.macho_file)) |atom| { try writer.print(" : atom({d})", .{atom.atom_index}); } - var buf: [2]u8 = .{'_'} ** 2; + var buf: [3]u8 = .{'_'} ** 3; if (symbol.flags.@"export") buf[0] = 'E'; if (symbol.flags.import) buf[1] = 'I'; + switch (symbol.visibility) { + .local => buf[2] = 'L', + .hidden => buf[2] = 'H', + .global => buf[2] = 'G', + } try writer.print(" : {s}", .{&buf}); if (symbol.flags.weak) try writer.writeAll(" : weak"); if (symbol.isSymbolStab(ctx.macho_file)) try writer.writeAll(" : stab"); @@ -383,6 +388,14 @@ pub const Visibility = enum { global, hidden, local, + + pub fn rank(vis: Visibility) u2 { + return switch (vis) { + .local => 2, + .hidden => 1, + .global => 0, + }; + } }; pub const Extra = struct { @@ -396,16 +409,6 @@ pub const Extra = struct { pub const Index = u32; -pub const Ref = struct { - index: Symbol.Index, - file: File.Index, - - pub fn getSymbol(ref: Ref, macho_file: *MachO) ?*Symbol { - const file = ref.getFile(macho_file) orelse return null; - return file.getSymbol(ref.index); - } -}; - const assert = std.debug.assert; const macho = std.macho; const std = @import("std");