Skip to content

Commit f219984

Browse files
vleonenVasily Leonenko
authored andcommitted
[BOLT] Allow missing DT_FINI{,_ARRAY} if instrumentation-sleep-time is used (llvm#170086)
This PR allows instrument binaries without the .fini and .fini_array entries if the user passes the `instrumentation-sleep-time` option. The `.fini` or `.fini_array` entries are used to hook into the process finalization process and write a profile during finalization. However, with the `instrumentation-sleep-time` option, the profile should be written periodically, without the need for it to be written at finalization. Co-authored-by: Vasily Leonenko <[email protected]>
1 parent 422d610 commit f219984

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ namespace opts {
8080
extern cl::list<std::string> HotTextMoveSections;
8181
extern cl::opt<bool> Hugify;
8282
extern cl::opt<bool> Instrument;
83+
extern cl::opt<uint32_t> InstrumentationSleepTime;
8384
extern cl::opt<bool> KeepNops;
8485
extern cl::opt<bool> Lite;
8586
extern cl::list<std::string> PrintOnly;
@@ -1507,6 +1508,9 @@ Error RewriteInstance::discoverRtFiniAddress() {
15071508
}
15081509

15091510
if (!BC->FiniArrayAddress || !BC->FiniArraySize) {
1511+
// Missing fini hooks are allowed when instrumentation-sleep-time is in use.
1512+
if (opts::InstrumentationSleepTime > 0)
1513+
return Error::success();
15101514
return createStringError(
15111515
std::errc::not_supported,
15121516
"Instrumentation needs either DT_FINI or DT_FINI_ARRAY");
@@ -1616,9 +1620,13 @@ Error RewriteInstance::updateRtFiniReloc() {
16161620
if (!RT || !RT->getRuntimeFiniAddress())
16171621
return Error::success();
16181622

1619-
if (!BC->FiniArrayAddress || !BC->FiniArraySize)
1623+
if (!BC->FiniArrayAddress || !BC->FiniArraySize) {
1624+
// Missing fini hooks are allowed when instrumentation-sleep-time is in use.
1625+
if (opts::InstrumentationSleepTime > 0)
1626+
return Error::success();
16201627
return createStringError(std::errc::not_supported,
16211628
"inconsistent .fini_array state");
1629+
}
16221630

16231631
ErrorOr<BinarySection &> FiniArraySection =
16241632
BC->getSectionForAddress(*BC->FiniArrayAddress);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Test that BOLT will produce error by default and pass with instrumentation-sleep-time option
2+
3+
# REQUIRES: system-linux,bolt-runtime,target=aarch64{{.*}}
4+
5+
# RUN: llvm-mc -triple aarch64 -filetype=obj %s -o %t.o
6+
# RUN: ld.lld -q -pie -o %t.exe %t.o
7+
# RUN: llvm-readelf -d %t.exe | FileCheck --check-prefix=CHECK-NO-FINI %s
8+
# RUN: not llvm-bolt --instrument -o %t.out %t.exe 2>&1 | FileCheck %s --check-prefix=CHECK-BOLT-FAIL
9+
# RUN: llvm-bolt --instrument --instrumentation-sleep-time=1 -o %t.out %t.exe 2>&1 | FileCheck %s --check-prefix=CHECK-BOLT-PASS
10+
11+
# CHECK-NO-FINI: INIT
12+
# CHECK-NO-FINI-NOT: FINI
13+
# CHECK-NO-FINI-NOT: FINI_ARRAY
14+
15+
# CHECK-BOLT-FAIL: Instrumentation needs either DT_FINI or DT_FINI_ARRAY
16+
17+
# CHECK-BOLT-PASS-NOT: Instrumentation needs either DT_FINI or DT_FINI_ARRAY
18+
# CHECK-BOLT-PASS: runtime library initialization was hooked via DT_INIT
19+
20+
.text
21+
.globl _start
22+
.type _start, %function
23+
_start:
24+
# BOLT errs when instrumenting without relocations; create a dummy one.
25+
.reloc 0, R_AARCH64_NONE
26+
ret
27+
.size _start, .-_start
28+
29+
.globl _init
30+
.type _init, %function
31+
# Force DT_INIT to be created (needed for instrumentation).
32+
_init:
33+
ret
34+
.size _init, .-_init

bolt/test/X86/instrument-no-fini.s

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Test that BOLT will produce error by default and pass with instrumentation-sleep-time option
2+
3+
# REQUIRES: system-linux,bolt-runtime,target=x86_64-{{.*}}
4+
5+
# RUN: llvm-mc -triple x86_64 -filetype=obj %s -o %t.o
6+
# RUN: ld.lld -q -pie -o %t.exe %t.o
7+
# RUN: llvm-readelf -d %t.exe | FileCheck --check-prefix=CHECK-NO-FINI %s
8+
# RUN: not llvm-bolt --instrument -o %t.out %t.exe 2>&1 | FileCheck %s --check-prefix=CHECK-BOLT-FAIL
9+
# RUN: llvm-bolt --instrument --instrumentation-sleep-time=1 -o %t.out %t.exe 2>&1 | FileCheck %s --check-prefix=CHECK-BOLT-PASS
10+
11+
# CHECK-NO-FINI: INIT
12+
# CHECK-NO-FINI-NOT: FINI
13+
# CHECK-NO-FINI-NOT: FINI_ARRAY
14+
15+
# CHECK-BOLT-FAIL: Instrumentation needs either DT_FINI or DT_FINI_ARRAY
16+
17+
# CHECK-BOLT-PASS-NOT: Instrumentation needs either DT_FINI or DT_FINI_ARRAY
18+
# CHECK-BOLT-PASS: runtime library initialization was hooked via DT_INIT
19+
20+
.text
21+
.globl _start
22+
.type _start, %function
23+
_start:
24+
# BOLT errs when instrumenting without relocations; create a dummy one.
25+
.reloc 0, R_X86_64_NONE
26+
retq
27+
.size _start, .-_start
28+
29+
.globl _init
30+
.type _init, %function
31+
# Force DT_INIT to be created (needed for instrumentation).
32+
_init:
33+
retq
34+
.size _init, .-_init

0 commit comments

Comments
 (0)