Skip to content

Commit

Permalink
macho: save if a symbol is ref'd dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 7, 2023
1 parent 4f26452 commit 1fc19ff
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/MachO.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1005,9 +1005,8 @@ fn resolveSyntheticSymbols(self: *MachO) !void {
self.mh_execute_header_index = try internal.addSymbol("__mh_execute_header", self);
const sym = self.getSymbol(self.mh_execute_header_index.?);
sym.flags.@"export" = true;
sym.flags.dyn_ref = true;
sym.visibility = .global;
const nlist = &internal.symtab.items[sym.nlist_idx];
nlist.n_desc = macho.REFERENCED_DYNAMICALLY;
} else if (self.options.dylib) {
self.mh_dylib_header_index = try internal.addSymbol("__mh_dylib_header", self);
}
Expand Down
1 change: 1 addition & 0 deletions src/MachO/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ pub fn resolveSymbols(self: *Object, macho_file: *MachO) void {
symbol.file = self.index;
symbol.flags.weak = nlist.weakDef();
symbol.flags.weak_ref = false;
symbol.flags.dyn_ref = nlist.n_desc & macho.REFERENCED_DYNAMICALLY != 0;

if (nlist.pext() or nlist.weakDef() and nlist.weakRef()) {
if (symbol.visibility != .global) {
Expand Down
7 changes: 6 additions & 1 deletion src/MachO/Symbol.zig
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ pub fn setOutputSym(symbol: Symbol, macho_file: *MachO, out: *macho.nlist_64) vo
out.n_desc |= macho.N_WEAK_DEF;
macho_file.weak_defines = true;
}
if (nlist.n_desc & macho.REFERENCED_DYNAMICALLY != 0) out.n_desc |= macho.REFERENCED_DYNAMICALLY;
if (symbol.flags.dyn_ref) {
out.n_desc |= macho.REFERENCED_DYNAMICALLY;
}
} else {
assert(symbol.visibility == .global);
out.n_type = macho.N_EXT;
Expand Down Expand Up @@ -288,6 +290,9 @@ pub const Flags = packed struct {
/// Whether this symbol is weakly referenced.
weak_ref: bool = false,

/// Whether this symbol is dynamically referenced.
dyn_ref: bool = false,

/// Whether this symbol is a thread-local variable.
tlv: bool = false,

Expand Down
17 changes: 17 additions & 0 deletions test/macho.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn addMachOTests(b: *Build, options: common.Options) *Step {
macho_step.dependOn(testLayout(b, opts));
macho_step.dependOn(testLargeBss(b, opts));
macho_step.dependOn(testLinkOrder(b, opts));
macho_step.dependOn(testMhExecuteHeader(b, opts));
macho_step.dependOn(testNeededFramework(b, opts));
macho_step.dependOn(testNeededLibrary(b, opts));
macho_step.dependOn(testNoExportsDylib(b, opts));
Expand Down Expand Up @@ -884,6 +885,22 @@ fn testLinkOrder(b: *Build, opts: Options) *Step {
return test_step;
}

fn testMhExecuteHeader(b: *Build, opts: Options) *Step {
const test_step = b.step("test-macho-mh-execute-header", "");

const exe = cc(b, opts);
exe.addEmptyMain();

const check = exe.check();
check.checkInSymtab();
// TODO enhance Mach-O parser
// check.checkContains("[referenced dynamically] external __mh_execute_header");
check.checkContains("external __mh_execute_header");
test_step.dependOn(&check.step);

return test_step;
}

fn testNeededFramework(b: *Build, opts: Options) *Step {
const test_step = b.step("test-macho-needed-framework", "");

Expand Down

0 comments on commit 1fc19ff

Please sign in to comment.