Skip to content

[BOLT] Read .rela.dyn in static non-pie binary #71635

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

Merged
merged 2 commits into from
Nov 10, 2023
Merged
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
1 change: 1 addition & 0 deletions bolt/include/bolt/Rewrite/RewriteInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class RewriteInstance {

/// Common section names.
static StringRef getEHFrameSectionName() { return ".eh_frame"; }
static StringRef getRelaDynSectionName() { return ".rela.dyn"; }

/// An instance of the input binary we are processing, externally owned.
llvm::object::ELFObjectFileBase *InputFile;
Expand Down
13 changes: 13 additions & 0 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,19 @@ void RewriteInstance::processDynamicRelocations() {
}

// The rest of dynamic relocations - DT_RELA.
// The static executable might have .rela.dyn secion and not have PT_DYNAMIC
if (!DynamicRelocationsSize && BC->IsStaticExecutable) {
ErrorOr<BinarySection &> DynamicRelSectionOrErr =
BC->getUniqueSectionByName(getRelaDynSectionName());
if (DynamicRelSectionOrErr) {
DynamicRelocationsAddress = DynamicRelSectionOrErr->getAddress();
DynamicRelocationsSize = DynamicRelSectionOrErr->getSize();
const SectionRef &SectionRef = DynamicRelSectionOrErr->getSectionRef();
DynamicRelativeRelocationsCount = std::distance(
SectionRef.relocation_begin(), SectionRef.relocation_end());
}
}

if (DynamicRelocationsSize > 0) {
ErrorOr<BinarySection &> DynamicRelSectionOrErr =
BC->getSectionForAddress(*DynamicRelocationsAddress);
Expand Down
23 changes: 23 additions & 0 deletions bolt/test/AArch64/ifunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
// RUN: llvm-bolt %t.O0.exe -o %t.O0.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
// RUN: FileCheck --check-prefix=O0_CHECK %s
// RUN: llvm-readelf -aW %t.O0.bolt.exe | \
// RUN: FileCheck --check-prefix=REL_CHECK %s

// Non-pie static executable doesn't generate PT_DYNAMIC, check relocation
// is readed successfully and IPLT trampoline has been identified by bolt.
// RUN: %clang %cflags -nostdlib -O3 %s -fuse-ld=lld -no-pie \
// RUN: -o %t.O3_nopie.exe -Wl,-q
// RUN: llvm-readelf -l %t.O3_nopie.exe | \
// RUN: FileCheck --check-prefix=NON_DYN_CHECK %s
// RUN: llvm-bolt %t.O3_nopie.exe -o %t.O3_nopie.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
// RUN: FileCheck --check-prefix=O3_CHECK %s
// RUN: llvm-readelf -aW %t.O3_nopie.bolt.exe | \
// RUN: FileCheck --check-prefix=REL_CHECK %s

// With -O3 direct call is performed on IPLT trampoline. IPLT trampoline
// doesn't have associated symbol. The ifunc symbol has the same address as
Expand All @@ -16,6 +30,8 @@
// RUN: llvm-bolt %t.O3_pie.exe -o %t.O3_pie.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
// RUN: FileCheck --check-prefix=O3_CHECK %s
// RUN: llvm-readelf -aW %t.O3_pie.bolt.exe | \
// RUN: FileCheck --check-prefix=REL_CHECK %s

// Check that IPLT trampoline located in .plt section are normally handled by
// BOLT. The gnu-ld linker doesn't use separate .iplt section.
Expand All @@ -24,10 +40,17 @@
// RUN: llvm-bolt %t.iplt_O3_pie.exe -o %t.iplt_O3_pie.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
// RUN: FileCheck --check-prefix=O3_CHECK %s
// RUN: llvm-readelf -aW %t.iplt_O3_pie.bolt.exe | \
// RUN: FileCheck --check-prefix=REL_CHECK %s

// NON_DYN_CHECK-NOT: DYNAMIC

// O0_CHECK: adr x{{[0-9]+}}, ifoo
// O3_CHECK: b "{{resolver_foo|ifoo}}{{.*}}@PLT"

// REL_CHECK: R_AARCH64_IRELATIVE [[#%x,REL_SYMB_ADDR:]]
// REL_CHECK: [[#REL_SYMB_ADDR]] {{.*}} FUNC {{.*}} resolver_foo

static void foo() {}

static void *resolver_foo(void) { return foo; }
Expand Down