@@ -75,6 +75,12 @@ GenerateFunctionOrderFile("generate-function-order",
75
75
" reordering" ),
76
76
cl::cat(BoltOptCategory));
77
77
78
+ static cl::opt<std::string>
79
+ LinkSectionsFile (" generate-link-sections" ,
80
+ cl::desc (" generate a list of function sections in a format suitable for "
81
+ " inclusion in a linker script" ),
82
+ cl::cat(BoltOptCategory));
83
+
78
84
static cl::opt<bool >
79
85
UseEdgeCounts (" use-edge-counts" ,
80
86
cl::desc (" use edge count data when doing clustering" ),
@@ -408,16 +414,32 @@ void ReorderFunctions::runOnFunctions(BinaryContext &BC,
408
414
409
415
reorder (std::move (Clusters), BFs);
410
416
417
+ std::unique_ptr<std::ofstream> FuncsFile;
411
418
if (!opts::GenerateFunctionOrderFile.empty ()) {
412
- std::ofstream FuncsFile (opts::GenerateFunctionOrderFile, std::ios::out);
419
+ FuncsFile =
420
+ llvm::make_unique<std::ofstream>(opts::GenerateFunctionOrderFile,
421
+ std::ios::out);
413
422
if (!FuncsFile) {
414
- errs () << " Ordered functions file \" " << opts::GenerateFunctionOrderFile
415
- << " \" can't be opened. \n " ;
423
+ errs () << " BOLT-ERROR: ordered functions file "
424
+ << opts::GenerateFunctionOrderFile << " cannot be opened\n " ;
416
425
exit (1 );
417
426
}
427
+ }
418
428
419
- std::vector<BinaryFunction *> SortedFunctions (BFs.size ());
429
+ std::unique_ptr<std::ofstream> LinkSectionsFile;
430
+ if (!opts::LinkSectionsFile.empty ()) {
431
+ LinkSectionsFile =
432
+ llvm::make_unique<std::ofstream>(opts::LinkSectionsFile,
433
+ std::ios::out);
434
+ if (!LinkSectionsFile) {
435
+ errs () << " BOLT-ERROR: link sections file "
436
+ << opts::LinkSectionsFile << " cannot be opened\n " ;
437
+ exit (1 );
438
+ }
439
+ }
420
440
441
+ if (FuncsFile || LinkSectionsFile) {
442
+ std::vector<BinaryFunction *> SortedFunctions (BFs.size ());
421
443
std::transform (BFs.begin (),
422
444
BFs.end (),
423
445
SortedFunctions.begin (),
@@ -446,25 +468,37 @@ void ReorderFunctions::runOnFunctions(BinaryContext &BC,
446
468
break ;
447
469
if (Func->isPLTFunction ())
448
470
continue ;
449
- const char *Indent = " " ;
450
- for (auto Name : Func->getNames ()) {
451
- const auto SlashPos = Name.find (' /' );
452
- if (SlashPos != std::string::npos) {
453
- // Avoid duplicates for local functions.
454
- if (Name.find (' /' , SlashPos + 1 ) != std::string::npos)
455
- continue ;
456
- Name = Name.substr (0 , SlashPos);
471
+
472
+ if (FuncsFile)
473
+ *FuncsFile << Func->getSymbol ()->getName ().data () << " \n " ;
474
+
475
+ if (LinkSectionsFile) {
476
+ const char *Indent = " " ;
477
+ for (auto Name : Func->getNames ()) {
478
+ const auto SlashPos = Name.find (' /' );
479
+ if (SlashPos != std::string::npos) {
480
+ // Avoid duplicates for local functions.
481
+ if (Name.find (' /' , SlashPos + 1 ) != std::string::npos)
482
+ continue ;
483
+ Name = Name.substr (0 , SlashPos);
484
+ }
485
+ *LinkSectionsFile << Indent << " .text." << Name << " \n " ;
486
+ Indent = " " ;
457
487
}
458
- FuncsFile << Indent << " .text." << Name << " \n " ;
459
- Indent = " " ;
460
488
}
461
489
}
462
- FuncsFile.close ();
463
490
464
- outs () << " BOLT-INFO: dumped function order to \" "
465
- << opts::GenerateFunctionOrderFile << " \"\n " ;
491
+ if (FuncsFile) {
492
+ FuncsFile->close ();
493
+ outs () << " BOLT-INFO: dumped function order to "
494
+ << opts::GenerateFunctionOrderFile << ' \n ' ;
495
+ }
466
496
467
- exit (0 );
497
+ if (LinkSectionsFile) {
498
+ LinkSectionsFile->close ();
499
+ outs () << " BOLT-INFO: dumped linker section order to "
500
+ << opts::LinkSectionsFile << ' \n ' ;
501
+ }
468
502
}
469
503
}
470
504
0 commit comments