Skip to content

Commit f7a5715

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 ef66e3d commit f7a5715

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
@@ -148,10 +148,11 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder {
148148
else
149149
return ToSymbolOrErr.takeError();
150150
} else {
151-
if (auto ToSymbolOrErr = findSymbolByAddress(FixupValue))
152-
ToSymbol = &*ToSymbolOrErr;
153-
else
154-
return ToSymbolOrErr.takeError();
151+
auto ToSymbolSec = findSectionByIndex(UnsignedRI.r_symbolnum - 1);
152+
if (!ToSymbolSec)
153+
return ToSymbolSec.takeError();
154+
ToSymbol = getSymbolByAddress(ToSymbolSec->Address);
155+
assert(ToSymbol && "No symbol for section");
155156
FixupValue -= ToSymbol->getAddress();
156157
}
157158

llvm/lib/ExecutionEngine/JITLink/MachO_x86_64.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ class MachOLinkGraphBuilder_x86_64 : public MachOLinkGraphBuilder {
150150
else
151151
return ToSymbolOrErr.takeError();
152152
} else {
153-
if (auto ToSymbolOrErr = findSymbolByAddress(FixupValue))
154-
ToSymbol = &*ToSymbolOrErr;
155-
else
156-
return ToSymbolOrErr.takeError();
153+
auto ToSymbolSec = findSectionByIndex(UnsignedRI.r_symbolnum - 1);
154+
if (!ToSymbolSec)
155+
return ToSymbolSec.takeError();
156+
ToSymbol = getSymbolByAddress(ToSymbolSec->Address);
157+
assert(ToSymbol && "No symbol for section");
157158
FixupValue -= ToSymbol->getAddress();
158159
}
159160

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

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

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

204-
# jitlink-check: *{4}anon_minuend_long1 = (section_addr(macho_reloc.o, __data) - anon_minuend_long1 + 2)[31:0]
204+
# jitlink-check: *{4}anon_minuend_long1 = (section_addr(macho_reloc.o, __data) - anon_minuend_long1 - 2)[31:0]
205205
.globl anon_minuend_long1
206206
.p2align 2
207207
anon_minuend_long1:
208-
.long Lanon_data - anon_minuend_long1 + 2
208+
.long Lanon_data - anon_minuend_long1 - 2
209209

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

0 commit comments

Comments
 (0)