Skip to content

Commit fb7bc3a

Browse files
author
Vasily Leonenko
committed
[BOLT] Print information about hooking methods for runtime library init/fini
Also extend hook-init & hook-fini tests with corresponding bolt output checks.
1 parent 60babf6 commit fb7bc3a

File tree

4 files changed

+85
-21
lines changed

4 files changed

+85
-21
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,10 @@ Error RewriteInstance::updateRtInitReloc() {
15901590
InitArraySection->addPendingRelocation(Relocation{
15911591
/*Offset*/ 0, /*Symbol*/ nullptr, /*Type*/ Relocation::getAbs64(),
15921592
/*Addend*/ RT->getRuntimeStartAddress(), /*Value*/ 0});
1593+
BC->outs()
1594+
<< "BOLT-INFO: runtime library initialization was hooked via .init_array "
1595+
"entry, set to 0x"
1596+
<< Twine::utohexstr(RT->getRuntimeStartAddress()) << "\n";
15931597
return Error::success();
15941598
}
15951599

@@ -1628,6 +1632,9 @@ Error RewriteInstance::updateRtFiniReloc() {
16281632
FiniArraySection->addPendingRelocation(Relocation{
16291633
/*Offset*/ 0, /*Symbol*/ nullptr, /*Type*/ Relocation::getAbs64(),
16301634
/*Addend*/ RT->getRuntimeFiniAddress(), /*Value*/ 0});
1635+
BC->outs() << "BOLT-INFO: runtime library finalization was hooked via "
1636+
".fini_array entry, set to 0x"
1637+
<< Twine::utohexstr(RT->getRuntimeFiniAddress()) << "\n";
16311638
return Error::success();
16321639
}
16331640

@@ -2327,6 +2334,14 @@ void RewriteInstance::adjustCommandLineOptions() {
23272334
exit(1);
23282335
}
23292336

2337+
if (opts::Instrument && opts::RuntimeLibInitHook == opts::RLIH_ENTRY_POINT &&
2338+
!BC->HasInterpHeader) {
2339+
BC->errs()
2340+
<< "BOLT-WARNING: adjusted runtime-lib-init-hook to 'init' due to "
2341+
"absence of INTERP header\n";
2342+
opts::RuntimeLibInitHook = opts::RLIH_INIT;
2343+
}
2344+
23302345
if (opts::HotText && opts::HotTextMoveSections.getNumOccurrences() == 0) {
23312346
opts::HotTextMoveSections.addValue(".stub");
23322347
opts::HotTextMoveSections.addValue(".mover");
@@ -4999,9 +5014,13 @@ void RewriteInstance::patchELFSectionHeaderTable(ELFObjectFile<ELFT> *File) {
49995014

50005015
if (BC->HasRelocations) {
50015016
RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary();
5002-
if (RtLibrary && opts::RuntimeLibInitHook == opts::RLIH_ENTRY_POINT)
5017+
if (RtLibrary && opts::RuntimeLibInitHook == opts::RLIH_ENTRY_POINT) {
50035018
NewEhdr.e_entry = RtLibrary->getRuntimeStartAddress();
5004-
else
5019+
BC->outs()
5020+
<< "BOLT-INFO: runtime library initialization was hooked via ELF "
5021+
"Header Entry Point, set to 0x"
5022+
<< Twine::utohexstr(NewEhdr.e_entry) << "\n";
5023+
} else
50055024
NewEhdr.e_entry = getNewFunctionAddress(NewEhdr.e_entry);
50065025
assert((NewEhdr.e_entry || !Obj.getHeader().e_entry) &&
50075026
"cannot find new address for entry point");
@@ -5842,16 +5861,23 @@ void RewriteInstance::patchELFDynamic(ELFObjectFile<ELFT> *File) {
58425861
}
58435862
RuntimeLibrary *RtLibrary = BC->getRuntimeLibrary();
58445863
if (RtLibrary && Dyn.getTag() == ELF::DT_FINI) {
5845-
if (uint64_t Addr = RtLibrary->getRuntimeFiniAddress())
5864+
if (uint64_t Addr = RtLibrary->getRuntimeFiniAddress()) {
58465865
NewDE.d_un.d_ptr = Addr;
5866+
BC->outs()
5867+
<< "BOLT-INFO: runtime library finalization was hooked via "
5868+
"DT_FINI, set to 0x"
5869+
<< Twine::utohexstr(Addr) << "\n";
5870+
}
58475871
}
58485872
if (RtLibrary && Dyn.getTag() == ELF::DT_INIT &&
58495873
(!BC->HasInterpHeader ||
58505874
opts::RuntimeLibInitHook == opts::RLIH_INIT)) {
58515875
if (auto Addr = RtLibrary->getRuntimeStartAddress()) {
5852-
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: Set DT_INIT to 0x"
5853-
<< Twine::utohexstr(Addr) << '\n');
58545876
NewDE.d_un.d_ptr = Addr;
5877+
BC->outs()
5878+
<< "BOLT-INFO: runtime library initialization was hooked via "
5879+
"DT_INIT, set to 0x"
5880+
<< Twine::utohexstr(Addr) << "\n";
58555881
}
58565882
}
58575883
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 .fini_array entry
52+
53+
## Check BOLT output output finalization hook (.fini_array entry)
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 .fini_array entry
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 .init_array entry
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 .init_array entry
92+
93+
## Check BOLT output output initialization hook (.init_array entry)
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 .init_array entry
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:]]

bolt/test/X86/hook-init.s

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
# RUN: llvm-readelf -r %t.exe | FileCheck --check-prefix=RELOC-PIE %s
2121
# 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
@@ -38,21 +38,21 @@
3838
# RUN: llvm-readelf -r %t-no-init.exe | FileCheck --check-prefix=RELOC-PIE %s
3939
# 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_X86_64_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 .init_array entry
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 .init_array entry
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 .init_array entry
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)