Skip to content

Commit 37a8cfb

Browse files
authored
[BOLT] Err when linking objects of different architectures (llvm#66770)
This could happen, for example, when instrumenting an AArch64 binary on an x86 host because the instrumentation library is always built for the host. Note that this check will probably need to be refined in the future as merely having the same architecture does not guarantee objects can be linked. For example, on RISC-V, the float ABI of all objects should match.
1 parent 17414ea commit 37a8cfb

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

bolt/lib/Rewrite/JITLinkLinker.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ void JITLinkLinker::loadObject(MemoryBufferRef Obj,
192192
exit(1);
193193
}
194194

195+
if ((*LG)->getTargetTriple().getArch() != BC.TheTriple->getArch()) {
196+
errs() << "BOLT-ERROR: linking object with arch "
197+
<< (*LG)->getTargetTriple().getArchName()
198+
<< " into context with arch " << BC.TheTriple->getArchName() << "\n";
199+
exit(1);
200+
}
201+
195202
auto Ctx = std::make_unique<Context>(*this, MapSections);
196203
jitlink::link(std::move(*LG), std::move(Ctx));
197204
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Test that BOLT errs when trying to instrument a binary with a different
2+
# architecture than the one BOLT is built for.
3+
4+
# REQUIRES: x86_64-linux,bolt-runtime,target=x86_64{{.*}}
5+
6+
# RUN: llvm-mc -triple aarch64 -filetype=obj %s -o %t.o
7+
# RUN: ld.lld -q -pie -o %t.exe %t.o
8+
# RUN: not llvm-bolt --instrument -o %t.out %t.exe 2>&1 | FileCheck %s
9+
10+
# CHECK: BOLT-ERROR: linking object with arch x86_64 into context with arch aarch64
11+
12+
.text
13+
.globl _start
14+
.type _start, %function
15+
_start:
16+
# BOLT errs when instrumenting without relocations; create a dummy one.
17+
.reloc 0, R_AARCH64_NONE
18+
ret
19+
.size _start, .-_start
20+
21+
.globl _fini
22+
.type _fini, %function
23+
# Force DT_FINI to be created (needed for instrumentation).
24+
_fini:
25+
ret
26+
.size _fini, .-_fini

0 commit comments

Comments
 (0)