Skip to content

[BOLT] Add .iplt support to x86 #106513

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 1 commit into from
Sep 23, 2024
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
11 changes: 5 additions & 6 deletions bolt/include/bolt/Rewrite/RewriteInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,11 @@ class RewriteInstance {
};

/// Different types of X86-64 PLT sections.
const PLTSectionInfo X86_64_PLTSections[4] = {
{ ".plt", 16 },
{ ".plt.got", 8 },
{ ".plt.sec", 8 },
{ nullptr, 0 }
};
const PLTSectionInfo X86_64_PLTSections[5] = {{".plt", 16},
{".plt.got", 8},
{".plt.sec", 8},
{".iplt", 16},
{nullptr, 0}};

/// AArch64 PLT sections.
const PLTSectionInfo AArch64_PLTSections[4] = {
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ void RewriteInstance::createPLTBinaryFunction(uint64_t TargetAddress,

MCSymbol *Symbol = Rel->Symbol;
if (!Symbol) {
if (!BC->isAArch64() || !Rel->Addend || !Rel->isIRelative())
if (BC->isRISCV() || !Rel->Addend || !Rel->isIRelative())
return;

// IFUNC trampoline without symbol
Expand Down
23 changes: 5 additions & 18 deletions bolt/test/AArch64/ifunc.c → bolt/test/AArch64/ifunc.test
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// This test checks that IFUNC trampoline is properly recognised by BOLT

// With -O0 indirect call is performed on IPLT trampoline. IPLT trampoline
// has IFUNC symbol.
// RUN: %clang %cflags -nostdlib -O0 -no-pie %s -fuse-ld=lld \
// RUN: %clang %cflags -nostdlib -O0 -no-pie %p/../Inputs/ifunc.c -fuse-ld=lld \
// RUN: -o %t.O0.exe -Wl,-q
// RUN: llvm-bolt %t.O0.exe -o %t.O0.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
Expand All @@ -12,7 +10,7 @@

// 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: %clang %cflags -nostdlib -O3 %p/../Inputs/ifunc.c -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
Expand All @@ -25,7 +23,7 @@
// With -O3 direct call is performed on IPLT trampoline. IPLT trampoline
// doesn't have associated symbol. The ifunc symbol has the same address as
// IFUNC resolver function.
// RUN: %clang %cflags -nostdlib -O3 %s -fuse-ld=lld -fPIC -pie \
// RUN: %clang %cflags -nostdlib -O3 %p/../Inputs/ifunc.c -fuse-ld=lld -fPIC -pie \
// RUN: -o %t.O3_pie.exe -Wl,-q
// RUN: llvm-bolt %t.O3_pie.exe -o %t.O3_pie.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
Expand All @@ -35,8 +33,8 @@

// Check that IPLT trampoline located in .plt section are normally handled by
// BOLT. The gnu-ld linker doesn't use separate .iplt section.
// RUN: %clang %cflags -nostdlib -O3 %s -fuse-ld=lld -fPIC -pie \
// RUN: -T %p/Inputs/iplt.ld -o %t.iplt_O3_pie.exe -Wl,-q
// RUN: %clang %cflags -nostdlib -O3 %p/../Inputs/ifunc.c -fuse-ld=lld -fPIC -pie \
// RUN: -T %p/../Inputs/iplt.ld -o %t.iplt_O3_pie.exe -Wl,-q
// 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=CHECK %s
Expand All @@ -49,14 +47,3 @@

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

static void foo() {}
static void bar() {}

extern int use_foo;

static void *resolver_foo(void) { return use_foo ? foo : bar; }

__attribute__((ifunc("resolver_foo"))) void ifoo();

void _start() { ifoo(); }
12 changes: 12 additions & 0 deletions bolt/test/Inputs/ifunc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This test checks that IFUNC trampoline is properly recognised by BOLT

static void foo() {}
static void bar() {}

extern int use_foo;

static void *resolver_foo(void) { return use_foo ? foo : bar; }

__attribute__((ifunc("resolver_foo"))) void ifoo();

void _start() { ifoo(); }
File renamed without changes.
47 changes: 47 additions & 0 deletions bolt/test/X86/ifunc.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Check if BOLT can process ifunc symbols from .plt section
// RUN: %clang %cflags -nostdlib -no-pie %p/../Inputs/ifunc.c -fuse-ld=lld \
// RUN: -o %t.exe -Wl,-q
// RUN: llvm-bolt %t.exe -o %t.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
// RUN: FileCheck --check-prefix=CHECK %s
// RUN: llvm-readelf -aW %t.bolt.exe | \
// RUN: FileCheck --check-prefix=REL_CHECK %s

// Check if BOLT can process ifunc symbols from .plt section in non-pie static
// executable case.
// RUN: %clang %cflags -nostdlib %p/../Inputs/ifunc.c -fuse-ld=lld -no-pie \
// RUN: -o %t.nopie.exe -Wl,-q
// RUN: llvm-readelf -l %t.nopie.exe | \
// RUN: FileCheck --check-prefix=NON_DYN_CHECK %s
// RUN: llvm-bolt %t.nopie.exe -o %t.nopie.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
// RUN: FileCheck --check-prefix=CHECK %s
// RUN: llvm-readelf -aW %t.nopie.bolt.exe | \
// RUN: FileCheck --check-prefix=REL_CHECK %s

// Check if BOLT can process ifunc symbols from .plt section in pie executable
// case.
// RUN: %clang %cflags -nostdlib %p/../Inputs/ifunc.c -fuse-ld=lld -fPIC -pie \
// RUN: -o %t.pie.exe -Wl,-q
// RUN: llvm-bolt %t.pie.exe -o %t.pie.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
// RUN: FileCheck --check-prefix=CHECK %s
// RUN: llvm-readelf -aW %t.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.
// RUN: %clang %cflags -nostdlib %p/../Inputs/ifunc.c -fuse-ld=lld -fPIC -pie \
// RUN: -T %p/../Inputs/iplt.ld -o %t.iplt_pie.exe -Wl,-q
// RUN: llvm-bolt %t.iplt_pie.exe -o %t.iplt_pie.bolt.exe \
// RUN: --print-disasm --print-only=_start | \
// RUN: FileCheck --check-prefix=CHECK %s
// RUN: llvm-readelf -aW %t.iplt_pie.bolt.exe | \
// RUN: FileCheck --check-prefix=REL_CHECK %s

// NON_DYN_CHECK-NOT: DYNAMIC

// CHECK: callq "resolver_foo/1@PLT"

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