Skip to content

Commit 3b5d626

Browse files
[BOLT] Fix --pad-funcs{,-before} state misinteraction
When --pad-funcs-before was introduced, it introduced a bug whereby the first one to get parsed could influence the other. Ensure that each has its own state and test that they don't interact in this manner.
1 parent ebe8796 commit 3b5d626

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

bolt/lib/Core/BinaryEmitter.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ BreakFunctionNames("break-funcs",
4646
cl::Hidden,
4747
cl::cat(BoltCategory));
4848

49-
cl::list<std::string>
49+
static cl::list<std::string>
5050
FunctionPadSpec("pad-funcs", cl::CommaSeparated,
5151
cl::desc("list of functions to pad with amount of bytes"),
5252
cl::value_desc("func1:pad1,func2:pad2,func3:pad3,..."),
5353
cl::Hidden, cl::cat(BoltCategory));
5454

55-
cl::list<std::string> FunctionPadBeforeSpec(
55+
static cl::list<std::string> FunctionPadBeforeSpec(
5656
"pad-funcs-before", cl::CommaSeparated,
5757
cl::desc("list of functions to pad with amount of bytes"),
5858
cl::value_desc("func1:pad1,func2:pad2,func3:pad3,..."), cl::Hidden,
@@ -74,10 +74,9 @@ X86AlignBranchBoundaryHotOnly("x86-align-branch-boundary-hot-only",
7474
cl::init(true),
7575
cl::cat(BoltOptCategory));
7676

77-
size_t padFunction(const cl::list<std::string> &Spec,
77+
size_t padFunction(std::map<std::string, size_t> &FunctionPadding,
78+
const cl::list<std::string> &Spec,
7879
const BinaryFunction &Function) {
79-
static std::map<std::string, size_t> FunctionPadding;
80-
8180
if (FunctionPadding.empty() && !Spec.empty()) {
8281
for (const std::string &Spec : Spec) {
8382
size_t N = Spec.find(':');
@@ -99,6 +98,15 @@ size_t padFunction(const cl::list<std::string> &Spec,
9998
return 0;
10099
}
101100

101+
size_t padFunctionBefore(const BinaryFunction &Function) {
102+
static std::map<std::string, size_t> CacheFunctionPadding;
103+
return padFunction(CacheFunctionPadding, FunctionPadBeforeSpec, Function);
104+
}
105+
size_t padFunctionAfter(const BinaryFunction &Function) {
106+
static std::map<std::string, size_t> CacheFunctionPadding;
107+
return padFunction(CacheFunctionPadding, FunctionPadSpec, Function);
108+
}
109+
102110
} // namespace opts
103111

104112
namespace {
@@ -324,8 +332,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
324332
Streamer.emitCodeAlignment(Function.getAlign(), &*BC.STI);
325333
}
326334

327-
if (size_t Padding =
328-
opts::padFunction(opts::FunctionPadBeforeSpec, Function)) {
335+
if (size_t Padding = opts::padFunctionBefore(Function)) {
329336
// Handle padFuncsBefore after the above alignment logic but before
330337
// symbol addresses are decided.
331338
if (!BC.HasRelocations) {
@@ -404,7 +411,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
404411
emitFunctionBody(Function, FF, /*EmitCodeOnly=*/false);
405412

406413
// Emit padding if requested.
407-
if (size_t Padding = opts::padFunction(opts::FunctionPadSpec, Function)) {
414+
if (size_t Padding = opts::padFunctionAfter(Function)) {
408415
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: padding function " << Function << " with "
409416
<< Padding << " bytes\n");
410417
Streamer.emitFill(Padding, MAI->getTextAlignFillValue());

bolt/lib/Passes/ReorderFunctions.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ extern cl::OptionCategory BoltOptCategory;
2828
extern cl::opt<unsigned> Verbosity;
2929
extern cl::opt<uint32_t> RandomSeed;
3030

31-
extern size_t padFunction(const cl::list<std::string> &Spec,
32-
const bolt::BinaryFunction &Function);
33-
extern cl::list<std::string> FunctionPadSpec, FunctionPadBeforeSpec;
31+
extern size_t padFunctionBefore(const bolt::BinaryFunction &Function);
32+
extern size_t padFunctionAfter(const bolt::BinaryFunction &Function);
3433

3534
extern cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions;
3635
cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions(
@@ -306,12 +305,10 @@ Error ReorderFunctions::runOnFunctions(BinaryContext &BC) {
306305
return false;
307306
if (B->isIgnored())
308307
return true;
309-
const size_t PadA =
310-
opts::padFunction(opts::FunctionPadSpec, *A) +
311-
opts::padFunction(opts::FunctionPadBeforeSpec, *A);
312-
const size_t PadB =
313-
opts::padFunction(opts::FunctionPadSpec, *B) +
314-
opts::padFunction(opts::FunctionPadBeforeSpec, *B);
308+
const size_t PadA = opts::padFunctionBefore(*A) +
309+
opts::padFunctionAfter(*A);
310+
const size_t PadB = opts::padFunctionBefore(*B) +
311+
opts::padFunctionAfter(*B);
315312
if (!PadA || !PadB) {
316313
if (PadA)
317314
return true;

bolt/test/AArch64/pad-before-funcs.s

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
# It should be able to introduce a configurable offset for the _start symbol.
33
# It should reject requests which don't obey the code alignment requirement.
44

5+
# Tests check inserting padding before _start; and additionally a test where
6+
# padding is inserted after start. In each case, check that the following
7+
# symbol ends up in the expected place as well.
8+
9+
510
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
611
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q -Wl,--section-start=.text=0x4000
712
# RUN: llvm-bolt %t.exe -o %t.bolt.0 --pad-funcs-before=_start:0
813
# RUN: llvm-bolt %t.exe -o %t.bolt.4 --pad-funcs-before=_start:4
914
# RUN: llvm-bolt %t.exe -o %t.bolt.8 --pad-funcs-before=_start:8
15+
# RUN: llvm-bolt %t.exe -o %t.bolt.4.4 --pad-funcs-before=_start:4 --pad-funcs=_start:4
16+
# RUN: llvm-bolt %t.exe -o %t.bolt.4.8 --pad-funcs-before=_start:4 --pad-funcs=_start:8
1017

1118
# RUN: not llvm-bolt %t.exe -o %t.bolt.8 --pad-funcs-before=_start:1 2>&1 | FileCheck --check-prefix=CHECK-BAD-ALIGN %s
1219

@@ -15,15 +22,27 @@
1522
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.0 | FileCheck --check-prefix=CHECK-0 %s
1623
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.4 | FileCheck --check-prefix=CHECK-4 %s
1724
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.8 | FileCheck --check-prefix=CHECK-8 %s
25+
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.4.4 | FileCheck --check-prefix=CHECK-4-4 %s
26+
# RUN: llvm-objdump --section=.text --disassemble %t.bolt.4.8 | FileCheck --check-prefix=CHECK-4-8 %s
1827

1928
# Trigger relocation mode in bolt.
2029
.reloc 0, R_AARCH64_NONE
2130

2231
.section .text
23-
.globl _start
2432

2533
# CHECK-0: 0000000000400000 <_start>
2634
# CHECK-4: 0000000000400004 <_start>
35+
# CHECK-4-4: 0000000000400004 <_start>
2736
# CHECK-8: 0000000000400008 <_start>
37+
.globl _start
2838
_start:
2939
ret
40+
41+
# CHECK-0: 0000000000400004 <_subsequent>
42+
# CHECK-4: 0000000000400008 <_subsequent>
43+
# CHECK-4-4: 000000000040000c <_subsequent>
44+
# CHECK-4-8: 0000000000400010 <_subsequent>
45+
# CHECK-8: 000000000040000c <_subsequent>
46+
.globl _subsequent
47+
_subsequent:
48+
ret

0 commit comments

Comments
 (0)