diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2023-03-31 22:39:41 +0200 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2023-04-01 14:22:44 +0200 |
| commit | 6f83a741d8f3847c65aa8b791a865f339b77531d (patch) | |
| tree | 01c99cb5ef60f2d9db71abf6c51dc7a58377a4ca /src | |
| parent | f2587de4e915423d0224b8b6ef0cb3d9cb7e60e2 (diff) | |
| download | zig-6f83a741d8f3847c65aa8b791a865f339b77531d.tar.gz zig-6f83a741d8f3847c65aa8b791a865f339b77531d.zip | |
macho: handle weird case of entrypoint being a stub
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/MachO/ZldAtom.zig | 2 | ||||
| -rw-r--r-- | src/link/MachO/zld.zig | 22 |
2 files changed, 22 insertions, 2 deletions
diff --git a/src/link/MachO/ZldAtom.zig b/src/link/MachO/ZldAtom.zig index e701ee2a13..7e784ded05 100644 --- a/src/link/MachO/ZldAtom.zig +++ b/src/link/MachO/ZldAtom.zig @@ -389,7 +389,7 @@ pub fn addGotEntry(zld: *Zld, target: SymbolWithLoc) !void { try zld.got_table.putNoClobber(gpa, target, got_index); } -fn addStub(zld: *Zld, target: SymbolWithLoc) !void { +pub fn addStub(zld: *Zld, target: SymbolWithLoc) !void { const target_sym = zld.getSymbol(target); if (!target_sym.undf()) return; if (zld.stubs_table.contains(target)) return; diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig index 196a437884..2e2b215698 100644 --- a/src/link/MachO/zld.zig +++ b/src/link/MachO/zld.zig @@ -4042,6 +4042,16 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr try zld.createTentativeDefAtoms(); try zld.createStubHelperPreambleAtom(); + if (zld.options.output_mode == .Exe) { + const global = zld.getEntryPoint(); + if (zld.getSymbol(global).undf()) { + // We do one additional check here in case the entry point was found in one of the dylibs. + // (I actually have no idea what this would imply but it is a possible outcome and so we + // support it.) + try Atom.addStub(&zld, global); + } + } + for (zld.objects.items) |object| { for (object.atoms.items) |atom_index| { const atom = zld.getAtom(atom_index); @@ -4153,8 +4163,18 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr const seg = zld.segments.items[seg_id]; const global = zld.getEntryPoint(); const sym = zld.getSymbol(global); + + const addr: u64 = if (sym.undf()) blk: { + // In this case, the symbol has been resolved in one of dylibs and so we point + // to the stub as its vmaddr value. + const stub_atom_index = zld.getStubsAtomIndexForSymbol(global).?; + const stub_atom = zld.getAtom(stub_atom_index); + const stub_sym = zld.getSymbol(stub_atom.getSymbolWithLoc()); + break :blk stub_sym.n_value; + } else sym.n_value; + try lc_writer.writeStruct(macho.entry_point_command{ - .entryoff = @intCast(u32, sym.n_value - seg.vmaddr), + .entryoff = @intCast(u32, addr - seg.vmaddr), .stacksize = options.stack_size_override orelse 0, }); } else { |
