Skip to content

Commit c28d9fb

Browse files
committed
[JITLink][MachO] Fix handling of non-extern UNSIGNED pair of SUBTRACTOR relocs.
When processing a MachO SUBTRACTOR/UNSIGNED pair, if the UNSIGNED target is non-extern then check the r_symbolnum field of the relocation to find the targeted section and use the section's address to find 'ToSymbol'. Previously 'ToSymbol' was found by loading the initial value stored at the fixup location and treating this as an address to search for. This is incorrect, however: the initial value includes the addend and will point to the wrong block if the addend is less than zero or greater than the block size. rdar://65756694
1 parent 52dd5ed commit c28d9fb

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder {
157157
else
158158
return ToSymbolOrErr.takeError();
159159
} else {
160-
if (auto ToSymbolOrErr = findSymbolByAddress(FixupValue))
161-
ToSymbol = &*ToSymbolOrErr;
162-
else
163-
return ToSymbolOrErr.takeError();
160+
auto ToSymbolSec = findSectionByIndex(UnsignedRI.r_symbolnum - 1);
161+
if (!ToSymbolSec)
162+
return ToSymbolSec.takeError();
163+
ToSymbol = getSymbolByAddress(ToSymbolSec->Address);
164+
assert(ToSymbol && "No symbol for section");
164165
FixupValue -= ToSymbol->getAddress();
165166
}
166167

llvm/lib/ExecutionEngine/JITLink/MachO_x86_64.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ class MachOLinkGraphBuilder_x86_64 : public MachOLinkGraphBuilder {
159159
else
160160
return ToSymbolOrErr.takeError();
161161
} else {
162-
if (auto ToSymbolOrErr = findSymbolByAddress(FixupValue))
163-
ToSymbol = &*ToSymbolOrErr;
164-
else
165-
return ToSymbolOrErr.takeError();
162+
auto ToSymbolSec = findSectionByIndex(UnsignedRI.r_symbolnum - 1);
163+
if (!ToSymbolSec)
164+
return ToSymbolSec.takeError();
165+
ToSymbol = getSymbolByAddress(ToSymbolSec->Address);
166+
assert(ToSymbol && "No symbol for section");
166167
FixupValue -= ToSymbol->getAddress();
167168
}
168169

llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_relocations.s

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,19 @@ anon_func_addr_quad:
187187

188188
# X86_64_RELOC_SUBTRACTOR Quad/Long in named storage with anonymous minuend
189189
#
190-
# jitlink-check: *{8}anon_minuend_quad1 = section_addr(macho_reloc.o, __data) - anon_minuend_quad1 + 2
190+
# jitlink-check: *{8}anon_minuend_quad1 = section_addr(macho_reloc.o, __data) - anon_minuend_quad1 - 2
191191
# Only the form "B: .quad LA - B + C" is tested. The form "B: .quad B - LA + C" is
192192
# invalid because the subtrahend can not be local.
193193
.globl anon_minuend_quad1
194194
.p2align 3
195195
anon_minuend_quad1:
196-
.quad Lanon_data - anon_minuend_quad1 + 2
196+
.quad Lanon_data - anon_minuend_quad1 - 2
197197

198-
# jitlink-check: *{4}anon_minuend_long1 = (section_addr(macho_reloc.o, __data) - anon_minuend_long1 + 2)[31:0]
198+
# jitlink-check: *{4}anon_minuend_long1 = (section_addr(macho_reloc.o, __data) - anon_minuend_long1 - 2)[31:0]
199199
.globl anon_minuend_long1
200200
.p2align 2
201201
anon_minuend_long1:
202-
.long Lanon_data - anon_minuend_long1 + 2
202+
.long Lanon_data - anon_minuend_long1 - 2
203203

204204
# Check X86_64_RELOC_SUBTRACTOR Quad/Long in named storage with minuend and subtrahend.
205205
# Both forms "A: .quad A - B + C" and "A: .quad B - A + C" are tested.

0 commit comments

Comments
 (0)