Skip to content

Commit 31ac3d0

Browse files
authored
[BOLT] Add .iplt support to x86 (#106513)
Add X86 support for parsing .iplt section and symbols.
1 parent 7e7009f commit 31ac3d0

File tree

6 files changed

+70
-25
lines changed

6 files changed

+70
-25
lines changed

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,11 @@ class RewriteInstance {
510510
};
511511

512512
/// Different types of X86-64 PLT sections.
513-
const PLTSectionInfo X86_64_PLTSections[4] = {
514-
{ ".plt", 16 },
515-
{ ".plt.got", 8 },
516-
{ ".plt.sec", 8 },
517-
{ nullptr, 0 }
518-
};
513+
const PLTSectionInfo X86_64_PLTSections[5] = {{".plt", 16},
514+
{".plt.got", 8},
515+
{".plt.sec", 8},
516+
{".iplt", 16},
517+
{nullptr, 0}};
519518

520519
/// AArch64 PLT sections.
521520
const PLTSectionInfo AArch64_PLTSections[4] = {

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ void RewriteInstance::createPLTBinaryFunction(uint64_t TargetAddress,
15331533

15341534
MCSymbol *Symbol = Rel->Symbol;
15351535
if (!Symbol) {
1536-
if (!BC->isAArch64() || !Rel->Addend || !Rel->isIRelative())
1536+
if (BC->isRISCV() || !Rel->Addend || !Rel->isIRelative())
15371537
return;
15381538

15391539
// IFUNC trampoline without symbol
Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// This test checks that IFUNC trampoline is properly recognised by BOLT
2-
31
// With -O0 indirect call is performed on IPLT trampoline. IPLT trampoline
42
// has IFUNC symbol.
5-
// RUN: %clang %cflags -nostdlib -O0 -no-pie %s -fuse-ld=lld \
3+
// RUN: %clang %cflags -nostdlib -O0 -no-pie %p/../Inputs/ifunc.c -fuse-ld=lld \
64
// RUN: -o %t.O0.exe -Wl,-q
75
// RUN: llvm-bolt %t.O0.exe -o %t.O0.bolt.exe \
86
// RUN: --print-disasm --print-only=_start | \
@@ -12,7 +10,7 @@
1210

1311
// Non-pie static executable doesn't generate PT_DYNAMIC, check relocation
1412
// is readed successfully and IPLT trampoline has been identified by bolt.
15-
// RUN: %clang %cflags -nostdlib -O3 %s -fuse-ld=lld -no-pie \
13+
// RUN: %clang %cflags -nostdlib -O3 %p/../Inputs/ifunc.c -fuse-ld=lld -no-pie \
1614
// RUN: -o %t.O3_nopie.exe -Wl,-q
1715
// RUN: llvm-readelf -l %t.O3_nopie.exe | \
1816
// RUN: FileCheck --check-prefix=NON_DYN_CHECK %s
@@ -25,7 +23,7 @@
2523
// With -O3 direct call is performed on IPLT trampoline. IPLT trampoline
2624
// doesn't have associated symbol. The ifunc symbol has the same address as
2725
// IFUNC resolver function.
28-
// RUN: %clang %cflags -nostdlib -O3 %s -fuse-ld=lld -fPIC -pie \
26+
// RUN: %clang %cflags -nostdlib -O3 %p/../Inputs/ifunc.c -fuse-ld=lld -fPIC -pie \
2927
// RUN: -o %t.O3_pie.exe -Wl,-q
3028
// RUN: llvm-bolt %t.O3_pie.exe -o %t.O3_pie.bolt.exe \
3129
// RUN: --print-disasm --print-only=_start | \
@@ -35,8 +33,8 @@
3533

3634
// Check that IPLT trampoline located in .plt section are normally handled by
3735
// BOLT. The gnu-ld linker doesn't use separate .iplt section.
38-
// RUN: %clang %cflags -nostdlib -O3 %s -fuse-ld=lld -fPIC -pie \
39-
// RUN: -T %p/Inputs/iplt.ld -o %t.iplt_O3_pie.exe -Wl,-q
36+
// RUN: %clang %cflags -nostdlib -O3 %p/../Inputs/ifunc.c -fuse-ld=lld -fPIC -pie \
37+
// RUN: -T %p/../Inputs/iplt.ld -o %t.iplt_O3_pie.exe -Wl,-q
4038
// RUN: llvm-bolt %t.iplt_O3_pie.exe -o %t.iplt_O3_pie.bolt.exe \
4139
// RUN: --print-disasm --print-only=_start | \
4240
// RUN: FileCheck --check-prefix=CHECK %s
@@ -49,14 +47,3 @@
4947

5048
// REL_CHECK: R_AARCH64_IRELATIVE [[#%x,REL_SYMB_ADDR:]]
5149
// REL_CHECK: [[#REL_SYMB_ADDR]] {{.*}} FUNC {{.*}} resolver_foo
52-
53-
static void foo() {}
54-
static void bar() {}
55-
56-
extern int use_foo;
57-
58-
static void *resolver_foo(void) { return use_foo ? foo : bar; }
59-
60-
__attribute__((ifunc("resolver_foo"))) void ifoo();
61-
62-
void _start() { ifoo(); }

bolt/test/Inputs/ifunc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This test checks that IFUNC trampoline is properly recognised by BOLT
2+
3+
static void foo() {}
4+
static void bar() {}
5+
6+
extern int use_foo;
7+
8+
static void *resolver_foo(void) { return use_foo ? foo : bar; }
9+
10+
__attribute__((ifunc("resolver_foo"))) void ifoo();
11+
12+
void _start() { ifoo(); }
File renamed without changes.

bolt/test/X86/ifunc.test

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Check if BOLT can process ifunc symbols from .plt section
2+
// RUN: %clang %cflags -nostdlib -no-pie %p/../Inputs/ifunc.c -fuse-ld=lld \
3+
// RUN: -o %t.exe -Wl,-q
4+
// RUN: llvm-bolt %t.exe -o %t.bolt.exe \
5+
// RUN: --print-disasm --print-only=_start | \
6+
// RUN: FileCheck --check-prefix=CHECK %s
7+
// RUN: llvm-readelf -aW %t.bolt.exe | \
8+
// RUN: FileCheck --check-prefix=REL_CHECK %s
9+
10+
// Check if BOLT can process ifunc symbols from .plt section in non-pie static
11+
// executable case.
12+
// RUN: %clang %cflags -nostdlib %p/../Inputs/ifunc.c -fuse-ld=lld -no-pie \
13+
// RUN: -o %t.nopie.exe -Wl,-q
14+
// RUN: llvm-readelf -l %t.nopie.exe | \
15+
// RUN: FileCheck --check-prefix=NON_DYN_CHECK %s
16+
// RUN: llvm-bolt %t.nopie.exe -o %t.nopie.bolt.exe \
17+
// RUN: --print-disasm --print-only=_start | \
18+
// RUN: FileCheck --check-prefix=CHECK %s
19+
// RUN: llvm-readelf -aW %t.nopie.bolt.exe | \
20+
// RUN: FileCheck --check-prefix=REL_CHECK %s
21+
22+
// Check if BOLT can process ifunc symbols from .plt section in pie executable
23+
// case.
24+
// RUN: %clang %cflags -nostdlib %p/../Inputs/ifunc.c -fuse-ld=lld -fPIC -pie \
25+
// RUN: -o %t.pie.exe -Wl,-q
26+
// RUN: llvm-bolt %t.pie.exe -o %t.pie.bolt.exe \
27+
// RUN: --print-disasm --print-only=_start | \
28+
// RUN: FileCheck --check-prefix=CHECK %s
29+
// RUN: llvm-readelf -aW %t.pie.bolt.exe | \
30+
// RUN: FileCheck --check-prefix=REL_CHECK %s
31+
32+
// Check that IPLT trampoline located in .plt section are normally handled by
33+
// BOLT. The gnu-ld linker doesn't use separate .iplt section.
34+
// RUN: %clang %cflags -nostdlib %p/../Inputs/ifunc.c -fuse-ld=lld -fPIC -pie \
35+
// RUN: -T %p/../Inputs/iplt.ld -o %t.iplt_pie.exe -Wl,-q
36+
// RUN: llvm-bolt %t.iplt_pie.exe -o %t.iplt_pie.bolt.exe \
37+
// RUN: --print-disasm --print-only=_start | \
38+
// RUN: FileCheck --check-prefix=CHECK %s
39+
// RUN: llvm-readelf -aW %t.iplt_pie.bolt.exe | \
40+
// RUN: FileCheck --check-prefix=REL_CHECK %s
41+
42+
// NON_DYN_CHECK-NOT: DYNAMIC
43+
44+
// CHECK: callq "resolver_foo/1@PLT"
45+
46+
// REL_CHECK: R_X86_64_IRELATIVE [[#%x,REL_SYMB_ADDR:]]
47+
// REL_CHECK: [[#REL_SYMB_ADDR]] {{.*}} FUNC {{.*}} resolver_foo

0 commit comments

Comments
 (0)