From 02db9933a4a0a99836f33757932f07263945166a Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sat, 21 Jan 2023 14:11:56 +0100 Subject: [PATCH] macho: properly relocate target symbol address in __eh_frame --- src/link/MachO/eh_frame.zig | 10 +++++++++- test/link/macho/uuid/build.zig | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/link/MachO/eh_frame.zig b/src/link/MachO/eh_frame.zig index bc11766953..8345a8e4f3 100644 --- a/src/link/MachO/eh_frame.zig +++ b/src/link/MachO/eh_frame.zig @@ -159,6 +159,14 @@ pub fn write(zld: *Zld, unwind_info: *UnwindInfo) !void { switch (cpu_arch) { .aarch64 => {}, // relocs take care of LSDA pointers .x86_64 => { + // We need to relocate target symbol address ourselves. + const atom = zld.getAtom(atom_index); + const atom_sym = zld.getSymbol(atom.getSymbolWithLoc()); + try fde_record.setTargetSymbolAddress(atom_sym.n_value, .{ + .base_addr = sect.addr, + .base_offset = eh_frame_offset, + }); + // We need to parse LSDA pointer and relocate ourselves. const cie_record = eh_records.get( eh_frame_offset + 4 - fde_record.getCiePointer(), @@ -267,7 +275,7 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { }) !void { assert(rec.tag == .fde); const addend = @intCast(i64, value) - @intCast(i64, ctx.base_addr + ctx.base_offset + 8); - mem.writeIntLittle(i64, addend, rec.data[4..][0..8]); + mem.writeIntLittle(i64, rec.data[4..][0..8], addend); } pub fn getPersonalityPointerReloc( diff --git a/test/link/macho/uuid/build.zig b/test/link/macho/uuid/build.zig index b17c14b973..314febdb20 100644 --- a/test/link/macho/uuid/build.zig +++ b/test/link/macho/uuid/build.zig @@ -21,9 +21,9 @@ pub fn build(b: *Builder) void { .os_tag = .macos, }; - testUuid(b, test_step, .ReleaseSafe, x86_64_macos, "63f47191c7153f5fba48bd63cb2f5f57"); - testUuid(b, test_step, .ReleaseFast, x86_64_macos, "63f47191c7153f5fba48bd63cb2f5f57"); - testUuid(b, test_step, .ReleaseSmall, x86_64_macos, "e7bba66220e33eda9e73ab293ccf93d2"); + testUuid(b, test_step, .ReleaseSafe, x86_64_macos, "5b7071b4587c3071b0d2352fadce0e48"); + testUuid(b, test_step, .ReleaseFast, x86_64_macos, "5b7071b4587c3071b0d2352fadce0e48"); + testUuid(b, test_step, .ReleaseSmall, x86_64_macos, "4b58f2583c383169bbe3a716bd240048"); } fn testUuid(