Skip to content

Commit c98edfe

Browse files
Vasily Leonenkovleonen
authored andcommitted
[BOLT] Print information about hooking methods for runtime library init/fini
Also extend AArch64 hook-init & hook-fini tests with corresponding bolt output checks.
1 parent fb200c5 commit c98edfe

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,9 @@ Error RewriteInstance::updateRtInitReloc() {
15951595
InitArraySection->addPendingRelocation(Relocation{
15961596
/*Offset*/ 0, /*Symbol*/ nullptr, /*Type*/ Relocation::getAbs64(),
15971597
/*Addend*/ RT->getRuntimeStartAddress(), /*Value*/ 0});
1598+
outs() << "BOLT-INFO: Runtime library initialization was hooked via 1st "
1599+
"entry of .init_array, set to "
1600+
<< Twine::utohexstr(RT->getRuntimeStartAddress()) << "\n";
15981601
return Error::success();
15991602
}
16001603

@@ -1640,6 +1643,9 @@ Error RewriteInstance::updateRtFiniReloc() {
16401643
FiniArraySection->addPendingRelocation(Relocation{
16411644
/*Offset*/ 0, /*Symbol*/ nullptr, /*Type*/ Relocation::getAbs64(),
16421645
/*Addend*/ RT->getRuntimeFiniAddress(), /*Value*/ 0});
1646+
outs() << "BOLT-INFO: Runtime library finalization was hooked via 1st entry "
1647+
"of .fini_array, set to "
1648+
<< Twine::utohexstr(RT->getRuntimeFiniAddress()) << "\n";
16431649
return Error::success();
16441650
}
16451651

@@ -5011,9 +5017,12 @@ void RewriteInstance::patchELFSectionHeaderTable(ELFObjectFile<ELFT> *File) {
50115017

50125018
if (BC->HasRelocations) {
50135019
RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary();
5014-
if (RtLibrary && opts::RuntimeLibInitHook == opts::RLIH_ENTRY_POINT)
5020+
if (RtLibrary && opts::RuntimeLibInitHook == opts::RLIH_ENTRY_POINT) {
50155021
NewEhdr.e_entry = RtLibrary->getRuntimeStartAddress();
5016-
else
5022+
outs() << "BOLT-INFO: Runtime library initialization was hooked via ELF "
5023+
"Header Entry Point, set to "
5024+
<< Twine::utohexstr(NewEhdr.e_entry) << "\n";
5025+
} else
50175026
NewEhdr.e_entry = getNewFunctionAddress(NewEhdr.e_entry);
50185027
assert((NewEhdr.e_entry || !Obj.getHeader().e_entry) &&
50195028
"cannot find new address for entry point");
@@ -5854,16 +5863,21 @@ void RewriteInstance::patchELFDynamic(ELFObjectFile<ELFT> *File) {
58545863
}
58555864
RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary();
58565865
if (RtLibrary && Dyn.getTag() == ELF::DT_FINI) {
5857-
if (uint64_t Addr = RtLibrary->getRuntimeFiniAddress())
5866+
if (uint64_t Addr = RtLibrary->getRuntimeFiniAddress()) {
58585867
NewDE.d_un.d_ptr = Addr;
5868+
outs() << "BOLT-INFO: Runtime library finalization was hooked via "
5869+
"DT_FINI, set to "
5870+
<< Twine::utohexstr(Addr) << "\n";
5871+
}
58595872
}
58605873
if (RtLibrary && Dyn.getTag() == ELF::DT_INIT &&
58615874
(!BC->HasInterpHeader ||
58625875
opts::RuntimeLibInitHook == opts::RLIH_INIT)) {
58635876
if (auto Addr = RtLibrary->getRuntimeStartAddress()) {
5864-
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: Set DT_INIT to 0x"
5865-
<< Twine::utohexstr(Addr) << '\n');
58665877
NewDE.d_un.d_ptr = Addr;
5878+
outs() << "BOLT-INFO: Runtime library initialization was hooked via "
5879+
"DT_INIT, set to "
5880+
<< Twine::utohexstr(Addr) << "\n";
58675881
}
58685882
}
58695883
break;

bolt/test/AArch64/hook-fini.s

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
# RUN: %clang %cflags -pie %s -Wl,-q -o %t.exe
1616
# RUN: llvm-readelf -d %t.exe | FileCheck --check-prefix=DYN-FINI %s
1717
# RUN: llvm-readelf -r %t.exe | FileCheck --check-prefix=RELOC-PIE %s
18-
# RUN: llvm-bolt %t.exe -o %t --instrument
18+
# RUN: llvm-bolt %t.exe -o %t --instrument | FileCheck --check-prefix=CHECK-BOLT-RT-FINI %s
1919
# RUN: llvm-readelf -drs %t | FileCheck --check-prefix=CHECK-FINI %s
2020

2121
# RUN: %clang %cflags -pie %s -Wl,-q,-fini=0 -o %t-no-fini.exe
2222
# RUN: llvm-readelf -d %t-no-fini.exe | FileCheck --check-prefix=DYN-NO-FINI %s
2323
# RUN: llvm-readelf -r %t-no-fini.exe | FileCheck --check-prefix=RELOC-PIE %s
24-
# RUN: llvm-bolt %t-no-fini.exe -o %t-no-fini --instrument
24+
# RUN: llvm-bolt %t-no-fini.exe -o %t-no-fini --instrument | FileCheck --check-prefix=CHECK-BOLT-RT-FINI-ARRAY %s
2525
# RUN: llvm-readelf -drs %t-no-fini | FileCheck --check-prefix=CHECK-NO-FINI %s
2626
# RUN: llvm-readelf -ds -x .fini_array %t-no-fini | FileCheck --check-prefix=CHECK-NO-FINI-RELOC %s
2727

2828
## Create a dummy shared library to link against to force creation of the dynamic section.
2929
# RUN: %clang %cflags %p/../Inputs/stub.c -fPIC -shared -o %t-stub.so
3030
# RUN: %clang %cflags %s -no-pie -Wl,-q,-fini=0 %t-stub.so -o %t-no-pie-no-fini.exe
3131
# RUN: llvm-readelf -r %t-no-pie-no-fini.exe | FileCheck --check-prefix=RELOC-NO-PIE %s
32-
# RUN: llvm-bolt %t-no-pie-no-fini.exe -o %t-no-pie-no-fini --instrument
32+
# RUN: llvm-bolt %t-no-pie-no-fini.exe -o %t-no-pie-no-fini --instrument | FileCheck --check-prefix=CHECK-BOLT-RT-FINI-ARRAY %s
3333
# RUN: llvm-readelf -ds -x .fini_array %t-no-pie-no-fini | FileCheck --check-prefix=CHECK-NO-PIE-NO-FINI %s
3434

3535
## With fini: dynamic section should contain DT_FINI
@@ -46,6 +46,14 @@
4646
## Without PIE: binary should not have relative relocations
4747
# RELOC-NO-PIE-NOT: R_AARCH64_RELATIVE
4848

49+
## Check BOLT output output finalization hook (DT_FINI)
50+
# CHECK-BOLT-RT-FINI: Runtime library finalization was hooked via DT_FINI
51+
# CHECK-BOLT-RT-FINI-NOT: Runtime library finalization was hooked via 1st entry of .fini_array
52+
53+
## Check BOLT output output initialization hook (1st entry of .init_array)
54+
# CHECK-BOLT-RT-FINI-ARRAY-NOT: Runtime library finalization was hooked via DT_FINI
55+
# CHECK-BOLT-RT-FINI-ARRAY: Runtime library finalization was hooked via 1st entry of .fini_array
56+
4957
## Check that DT_FINI is set to __bolt_runtime_fini
5058
# CHECK-FINI: Dynamic section at offset {{.*}} contains {{.*}} entries:
5159
# CHECK-FINI-DAG: (FINI) 0x[[FINI:[[:xdigit:]]+]]

bolt/test/AArch64/hook-init.s

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,41 @@
1818
# RUN: llvm-readelf -d %t.exe | FileCheck --check-prefix=DYN-INIT %s
1919
# RUN: llvm-readelf -l %t.exe | FileCheck --check-prefix=PH-INTERP %s
2020
# RUN: llvm-readelf -r %t.exe | FileCheck --check-prefix=RELOC-PIE %s
21-
# RUN: llvm-bolt %t.exe -o %t --instrument
21+
# RUN: llvm-bolt %t.exe -o %t --instrument | FileCheck --check-prefix=CHECK-BOLT-RT-EP %s
2222
# RUN: llvm-readelf -hdrs %t | FileCheck --check-prefix=CHECK-INIT-EP %s
23-
# RUN: llvm-bolt %t.exe -o %t-no-ep --instrument --runtime-lib-init-hook=init
23+
# RUN: llvm-bolt %t.exe -o %t-no-ep --instrument --runtime-lib-init-hook=init | FileCheck --check-prefix=CHECK-BOLT-RT-INIT %s
2424
# RUN: llvm-readelf -hdrs %t-no-ep | FileCheck --check-prefix=CHECK-INIT-NO-EP %s
25-
# RUN: llvm-bolt %t.exe -o %t-no-ep --instrument --runtime-lib-init-hook=init_array
25+
# RUN: llvm-bolt %t.exe -o %t-no-ep --instrument --runtime-lib-init-hook=init_array | FileCheck --check-prefix=CHECK-BOLT-RT-INIT-ARRAY %s
2626
# RUN: llvm-readelf -hdrs %t-no-ep | FileCheck --check-prefix=CHECK-INIT-ARRAY-NO-EP %s
2727

2828
# RUN: %clang -shared %cflags -pie %s -Wl,-q -o %t-shared.exe
2929
# RUN: llvm-readelf -d %t-shared.exe | FileCheck --check-prefix=DYN-INIT %s
3030
# RUN: llvm-readelf -l %t-shared.exe | FileCheck --check-prefix=PH-INTERP-SHARED %s
3131
# RUN: llvm-readelf -r %t-shared.exe | FileCheck --check-prefix=RELOC-SHARED-PIE %s
32-
# RUN: llvm-bolt %t-shared.exe -o %t-shared --instrument
32+
# RUN: llvm-bolt %t-shared.exe -o %t-shared --instrument | FileCheck --check-prefix=CHECK-BOLT-RT-INIT %s
3333
# RUN: llvm-readelf -hdrs %t-shared | FileCheck --check-prefix=CHECK-SHARED-INIT %s
3434

3535
# RUN: %clang %cflags -pie %s -Wl,-q,-init=0 -o %t-no-init.exe
3636
# RUN: llvm-readelf -d %t-no-init.exe | FileCheck --check-prefix=DYN-NO-INIT %s
3737
# RUN: llvm-readelf -l %t-no-init.exe | FileCheck --check-prefix=PH-INTERP %s
3838
# RUN: llvm-readelf -r %t-no-init.exe | FileCheck --check-prefix=RELOC-PIE %s
39-
# RUN: llvm-bolt %t-no-init.exe -o %t-no-init --instrument
39+
# RUN: llvm-bolt %t-no-init.exe -o %t-no-init --instrument | FileCheck --check-prefix=CHECK-BOLT-RT-EP %s
4040
# RUN: llvm-readelf -hdrs %t-no-init | FileCheck --check-prefix=CHECK-NO-INIT-EP %s
41-
# RUN: llvm-bolt %t-no-init.exe -o %t-no-init-no-ep --instrument --runtime-lib-init-hook=init
41+
# RUN: llvm-bolt %t-no-init.exe -o %t-no-init-no-ep --instrument --runtime-lib-init-hook=init | FileCheck --check-prefix=CHECK-BOLT-RT-INIT-ARRAY %s
4242
# RUN: llvm-readelf -hdrs %t-no-init-no-ep | FileCheck --check-prefix=CHECK-NO-INIT-NO-EP %s
4343

4444
# RUN: %clang -shared %cflags -pie %s -Wl,-q,-init=0 -o %t-shared-no-init.exe
4545
# RUN: llvm-readelf -d %t-shared-no-init.exe | FileCheck --check-prefix=DYN-NO-INIT %s
4646
# RUN: llvm-readelf -l %t-shared-no-init.exe | FileCheck --check-prefix=PH-INTERP-SHARED %s
4747
# RUN: llvm-readelf -r %t-shared-no-init.exe | FileCheck --check-prefix=RELOC-SHARED-PIE %s
48-
# RUN: llvm-bolt %t-shared-no-init.exe -o %t-shared-no-init --instrument
48+
# RUN: llvm-bolt %t-shared-no-init.exe -o %t-shared-no-init --instrument | FileCheck --check-prefix=CHECK-BOLT-RT-INIT-ARRAY %s
4949
# RUN: llvm-readelf -drs %t-shared-no-init | FileCheck --check-prefix=CHECK-SHARED-NO-INIT %s
5050

5151
## Create a dummy shared library to link against to force creation of the dynamic section.
5252
# RUN: %clang %cflags %p/../Inputs/stub.c -fPIC -shared -o %t-stub.so
5353
# RUN: %clang %cflags %s -no-pie -Wl,-q,-init=0 %t-stub.so -o %t-no-pie-no-init.exe
5454
# RUN: llvm-readelf -r %t-no-pie-no-init.exe | FileCheck --check-prefix=RELOC-NO-PIE %s
55-
# RUN: llvm-bolt %t-no-pie-no-init.exe -o %t-no-pie-no-init --instrument
55+
# RUN: llvm-bolt %t-no-pie-no-init.exe -o %t-no-pie-no-init --instrument | FileCheck --check-prefix=CHECK-BOLT-RT-EP %s
5656
# RUN: llvm-readelf -hds %t-no-pie-no-init | FileCheck --check-prefix=CHECK-NO-PIE-NO-INIT-EP %s
5757

5858
## With init: dynamic section should contain DT_INIT
@@ -80,6 +80,21 @@
8080
## Without PIE: binary should not have relative relocations
8181
# RELOC-NO-PIE-NOT: R_AARCH64_RELATIVE
8282

83+
## Check BOLT output output initialization hook (ELF Header Entry Point)
84+
# CHECK-BOLT-RT-EP: Runtime library initialization was hooked via ELF Header Entry Point
85+
# CHECK-BOLT-RT-EP-NOT: Runtime library initialization was hooked via DT_INIT
86+
# CHECK-BOLT-RT-EP-NOT: Runtime library initialization was hooked via 1st entry of .init_array
87+
88+
## Check BOLT output output initialization hook (DT_INIT)
89+
# CHECK-BOLT-RT-INIT-NOT: Runtime library initialization was hooked via ELF Header Entry Point
90+
# CHECK-BOLT-RT-INIT: Runtime library initialization was hooked via DT_INIT
91+
# CHECK-BOLT-RT-INIT-NOT: Runtime library initialization was hooked via 1st entry of .init_array
92+
93+
## Check BOLT output output initialization hook (1st entry of .init_array)
94+
# CHECK-BOLT-RT-INIT-ARRAY-NOT: Runtime library initialization was hooked via ELF Header Entry Point
95+
# CHECK-BOLT-RT-INIT-ARRAY-NOT: Runtime library initialization was hooked via DT_INIT
96+
# CHECK-BOLT-RT-INIT-ARRAY: Runtime library initialization was hooked via 1st entry of .init_array
97+
8398
## Check that entry point address is set to __bolt_runtime_start for PIE executable with DT_INIT
8499
# CHECK-INIT-EP: ELF Header:
85100
# CHECK-INIT-EP: Entry point address: 0x[[#%x,EP_ADDR:]]

0 commit comments

Comments
 (0)