@@ -46,13 +46,17 @@ BreakFunctionNames("break-funcs",
46
46
cl::Hidden,
47
47
cl::cat(BoltCategory));
48
48
49
- static cl::list<std::string>
50
- FunctionPadSpec (" pad-funcs" ,
51
- cl::CommaSeparated,
52
- cl::desc (" list of functions to pad with amount of bytes" ),
53
- cl::value_desc(" func1:pad1,func2:pad2,func3:pad3,..." ),
54
- cl::Hidden,
55
- cl::cat(BoltCategory));
49
+ cl::list<std::string>
50
+ FunctionPadSpec (" pad-funcs" , cl::CommaSeparated,
51
+ cl::desc (" list of functions to pad with amount of bytes" ),
52
+ cl::value_desc(" func1:pad1,func2:pad2,func3:pad3,..." ),
53
+ cl::Hidden, cl::cat(BoltCategory));
54
+
55
+ cl::list<std::string> FunctionPadBeforeSpec (
56
+ " pad-funcs-before" , cl::CommaSeparated,
57
+ cl::desc (" list of functions to pad with amount of bytes" ),
58
+ cl::value_desc(" func1:pad1,func2:pad2,func3:pad3,..." ), cl::Hidden,
59
+ cl::cat(BoltCategory));
56
60
57
61
static cl::opt<bool > MarkFuncs (
58
62
" mark-funcs" ,
@@ -70,11 +74,12 @@ X86AlignBranchBoundaryHotOnly("x86-align-branch-boundary-hot-only",
70
74
cl::init(true ),
71
75
cl::cat(BoltOptCategory));
72
76
73
- size_t padFunction (const BinaryFunction &Function) {
77
+ size_t padFunction (const cl::list<std::string> &Spec,
78
+ const BinaryFunction &Function) {
74
79
static std::map<std::string, size_t > FunctionPadding;
75
80
76
- if (FunctionPadding.empty () && !FunctionPadSpec .empty ()) {
77
- for (std::string &Spec : FunctionPadSpec ) {
81
+ if (FunctionPadding.empty () && !Spec .empty ()) {
82
+ for (const std::string &Spec : Spec ) {
78
83
size_t N = Spec.find (' :' );
79
84
if (N == std::string::npos)
80
85
continue ;
@@ -319,6 +324,32 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
319
324
Streamer.emitCodeAlignment (Function.getAlign (), &*BC.STI );
320
325
}
321
326
327
+ if (size_t Padding =
328
+ opts::padFunction (opts::FunctionPadBeforeSpec, Function)) {
329
+ // Handle padFuncsBefore after the above alignment logic but before
330
+ // symbol addresses are decided.
331
+ if (!BC.HasRelocations ) {
332
+ BC.errs () << " BOLT-ERROR: -pad-before-funcs is not supported in "
333
+ << " non-relocation mode\n " ;
334
+ exit (1 );
335
+ }
336
+
337
+ // Preserve Function.getMinAlign().
338
+ if (!isAligned (Function.getMinAlign (), Padding)) {
339
+ BC.errs () << " BOLT-ERROR: user-requested " << Padding
340
+ << " padding bytes before function " << Function
341
+ << " is not a multiple of the minimum function alignment ("
342
+ << Function.getMinAlign ().value () << " ).\n " ;
343
+ exit (1 );
344
+ }
345
+
346
+ LLVM_DEBUG (dbgs () << " BOLT-DEBUG: padding before function " << Function
347
+ << " with " << Padding << " bytes\n " );
348
+
349
+ // Since the padding is not executed, it can be null bytes.
350
+ Streamer.emitFill (Padding, 0 );
351
+ }
352
+
322
353
MCContext &Context = Streamer.getContext ();
323
354
const MCAsmInfo *MAI = Context.getAsmInfo ();
324
355
@@ -373,7 +404,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
373
404
emitFunctionBody (Function, FF, /* EmitCodeOnly=*/ false );
374
405
375
406
// Emit padding if requested.
376
- if (size_t Padding = opts::padFunction (Function)) {
407
+ if (size_t Padding = opts::padFunction (opts::FunctionPadSpec, Function)) {
377
408
LLVM_DEBUG (dbgs () << " BOLT-DEBUG: padding function " << Function << " with "
378
409
<< Padding << " bytes\n " );
379
410
Streamer.emitFill (Padding, MAI->getTextAlignFillValue ());
0 commit comments