[BOLT] Treat relr relocations like rela
The linker can emit certain relative .dyn.rela relocations in .dyn.relr. This includes relocations for jumptables emitted for indirect goto. The current processing recognizes these relocations as jump targets only for relative relocations from .dyn.rela, but not from .dyn.relr. Give .dyn.relr the same treatment, so CFG construction does not dismiss blocks that are only reachable indirectly.
@llvm/pr-subscribers-bolt
Author: Fabian Parzefall (pzfl)
Changes
The linker can emit certain relative .dyn.rela relocations in .dyn.relr. This includes relocations for jumptables emitted for indirect goto. The current processing recognizes these relocations as jump targets only for relative relocations from .dyn.rela, but not from .dyn.relr. Give .dyn.relr the same treatment, so CFG construction does not dismiss blocks that are only reachable indirectly.
Fixes #176901.
Full diff: https://github.com/llvm/llvm-project/pull/206830.diff
3 Files Affected:
- (modified) bolt/include/bolt/Rewrite/RewriteInstance.h (+4)
- (modified) bolt/lib/Rewrite/RewriteInstance.cpp (+27-20)
- (modified) bolt/test/X86/indirect-goto.test (+13)
diff --git a/bolt/include/bolt/Rewrite/RewriteInstance.h b/bolt/include/bolt/Rewrite/RewriteInstance.h
index 3873d944e01ba..efa5e20c16b7b 100644
--- a/bolt/include/bolt/Rewrite/RewriteInstance.h
+++ b/bolt/include/bolt/Rewrite/RewriteInstance.h
@@ -136,6 +136,10 @@ class RewriteInstance {
/// Read relocations from a given RELR section.
void readDynamicRelrRelocations(BinarySection &Section);
+ /// Process relative dynamic relocations targeting code, e.g. from jumptables.
+ void handleRelativeDynamicRelocation(uint64_t RelOffset,
+ uint64_t ReferencedAddress);
+
/// Print relocation information.
void printRelocationInfo(const RelocationRef &Rel, StringRef SymbolName,
uint64_t SymbolAddress, uint64_t Addend,
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 9e00d48e03853..2b5e8df2d3566 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -2806,27 +2806,13 @@ void RewriteInstance::readDynamicRelocations(const SectionRef &Section,
SymbolIndex[Symbol] = getRelocationSymbol(InputFile, Rel);
const uint64_t ReferencedAddress = SymbolAddress + Addend;
- BinaryFunction *Func =
- BC->getBinaryFunctionContainingAddress(ReferencedAddress);
-
- if (Relocation::isRelative(RType) && SymbolAddress == 0) {
- if (Func) {
- if (!Func->isInConstantIsland(ReferencedAddress)) {
- if (const uint64_t ReferenceOffset =
- ReferencedAddress - Func->getAddress()) {
- Func->addEntryPointAtOffset(ReferenceOffset);
- }
- } else {
- BC->errs() << "BOLT-ERROR: referenced address at 0x"
- << Twine::utohexstr(ReferencedAddress)
- << " is in constant island of function " << *Func << "\n";
- exit(1);
- }
+ if (Relocation::isRelative(RType)) {
+ if (SymbolAddress != 0){
+ BC->errs() << "BOLT-ERROR: symbol address non zero for RELATIVE "
+ "relocation type\n";
+ exit(1);
}
- } else if (Relocation::isRelative(RType) && SymbolAddress != 0) {
- BC->errs() << "BOLT-ERROR: symbol address non zero for RELATIVE "
- "relocation type\n";
- exit(1);
+ handleRelativeDynamicRelocation(Rel.getOffset(), ReferencedAddress);
}
BC->addDynamicRelocation(Rel.getOffset(), Symbol, RType, Addend);
@@ -2859,6 +2845,7 @@ void RewriteInstance::readDynamicRelrRelocations(BinarySection &Section) {
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: R_*_RELATIVE relocation at 0x"
<< Twine::utohexstr(Address) << " to 0x"
<< Twine::utohexstr(Addend) << '\n';);
+ handleRelativeDynamicRelocation(Address, Addend);
BC->addDynamicRelocation(Address, nullptr, RType, Addend, /*Value=*/0,
/*IsRELR=*/true);
};
@@ -2886,6 +2873,26 @@ void RewriteInstance::readDynamicRelrRelocations(BinarySection &Section) {
}
}
+void RewriteInstance::handleRelativeDynamicRelocation(
+ uint64_t RelOffset, uint64_t ReferencedAddress) {
+ BinaryFunction *Func =
+ BC->getBinaryFunctionContainingAddress(ReferencedAddress);
+ if (!Func)
+ return;
+
+ if (Func->isInConstantIsland(ReferencedAddress)) {
+ BC->errs() << "BOLT-ERROR: referenced address at 0x"
+ << Twine::utohexstr(ReferencedAddress)
+ << " is in constant island of function " << *Func << "\n";
+ exit(1);
+ }
+
+ if (const uint64_t ReferenceOffset = ReferencedAddress - Func->getAddress()) {
+ Func->addEntryPointAtOffset(ReferenceOffset);
+ Func->registerInternalRefDataRelocation(ReferenceOffset, RelOffset);
+ }
+}
+
void RewriteInstance::printRelocationInfo(const RelocationRef &Rel,
StringRef SymbolName,
uint64_t SymbolAddress,
diff --git a/bolt/test/X86/indirect-goto.test b/bolt/test/X86/indirect-goto.test
index aeb89de3f2fc2..38e8d9073a956 100644
--- a/bolt/test/X86/indirect-goto.test
+++ b/bolt/test/X86/indirect-goto.test
@@ -4,6 +4,19 @@ RUN: llvm-bolt %t -o %t.null --relocs=1 --print-cfg --print-only=main \
RUN: --strict \
RUN: 2>&1 | FileCheck %s
+## Check indirect goto works in pie
+RUN: %clang %cflags -fPIE -pie %S/../Inputs/indirect_goto.c -o %t.pie -Wl,-q
+RUN: llvm-bolt %t.pie -o %t.pie.null --relocs=1 --print-cfg --print-only=main \
+RUN: --strict \
+RUN: 2>&1 | FileCheck %s
+
+## Check indirect goto works in binaries linked with relr
+RUN: %clang %cflags -fPIE -pie %S/../Inputs/indirect_goto.c -o %t.relr \
+RUN: -Wl,-z,pack-relative-relocs -Wl,-q
+RUN: llvm-bolt %t.relr -o %t.relr.null --relocs=1 --print-cfg --print-only=main \
+RUN: --strict \
+RUN: 2>&1 | FileCheck %s
+
## Check that all possible destinations are included as successors.
CHECK: jmpq *%rax # UNKNOWN CONTROL FLOW
CHECK: Successors: .Ltmp0, .Ltmp1, .Ltmp2
Depends #206819.
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.