Skip to content

[WORKAROUND][BOLT][AArch64] Static binary patching for ELF. #97710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5265,6 +5265,34 @@ void RewriteInstance::patchELFGOT(ELFObjectFile<ELFT> *File) {
GOTContents.size());
++GOTEntry) {
if (uint64_t NewAddress = getNewFunctionAddress(*GOTEntry)) {
auto *Function = BC->getBinaryFunctionAtAddress(*GOTEntry);

// WORKAROUND:
// Background:
// Static binaries generated with GNU linker have GOT entries, which is
// wrong. LLD does not suffer from this. See related discussion:
// https://github.com/llvm/llvm-project/issues/100096
//
// The current approach is for BOLT to abort when a static binary contains
// such entries. However, this patch may serve as a 'Workaround' in case
// someone has encountered such a binary. ATTOW it is unclear if/when GNU
// linker will have this fixed.
//
// The workaround:
// On static binaries, avoid patching the "_init" got entry. It also
// checks that it does not belong to the original text section and that it
// an alias. This function actually aliases '.init', belongs to the
// '.init' section, and points to the same address of '_init' in the
// original binary.
if (BC->IsStaticExecutable && !Function->Aliases.empty() &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way the function entry won't be patched either I think? It might also be a problem

Function->getOriginSectionName() != ".bolt.org.text" &&
(Function->getOneName() == "_init")) {
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: ignoring GOT entry 0x"
<< Twine::utohexstr(*GOTEntry) << " for '"
<< Function->getOneName() << "'" << '\n');
continue;
}

LLVM_DEBUG(dbgs() << "BOLT-DEBUG: patching GOT entry 0x"
<< Twine::utohexstr(*GOTEntry) << " with 0x"
<< Twine::utohexstr(NewAddress) << '\n');
Expand Down
16 changes: 16 additions & 0 deletions bolt/test/AArch64/patch-elfstatic-libc.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Tests that statically linked AArch64 binaries do not crash at binary loading
// time.
//
// Functions like `_init` do not appear in the `.text` section of the original
// binary. For those cases, the BOLT'ed binary creates an alias (ie, `.init`),
// which matches the address of the relevant function in the original binary.
// If such function appears in the GOT and is patched, it leads to a runtime
// crash.

REQUIRES: system-linux

RUN: %clang %p/../Inputs/main.c -o %t -Wl,-q -static
RUN: llvm-bolt %t -o %t.bolt > /dev/null 2>&1
RUN: not %t.bolt 2>&1 | FileCheck %s --allow-empty

CHECK-NOT: Unexpected reloc type in static binary.
3 changes: 3 additions & 0 deletions bolt/test/Inputs/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// dummy function just for emitting relocations to the linker.
int foo() { return 0; }
int main(int argc, char **argv) { return foo() + 1; }