Skip to content

Commit

Permalink
macho: fix symbol visibility merging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Jul 11, 2024
1 parent 82a276a commit 020cb4d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
18 changes: 0 additions & 18 deletions src/MachO/Atom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 14 additions & 11 deletions src/MachO/Symbol.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 {
Expand All @@ -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");
Expand Down

0 comments on commit 020cb4d

Please sign in to comment.