Skip to content

Commit 090c92e

Browse files
authored
[BOLT] Emit synthetic FILE symbol for local cold fragments of global symbols (#89794)
1 parent 10661ba commit 090c92e

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ class RewriteInstance {
426426
static StringRef getEHFrameSectionName() { return ".eh_frame"; }
427427
static StringRef getRelaDynSectionName() { return ".rela.dyn"; }
428428

429+
/// FILE symbol name used for local fragments of global functions.
430+
static StringRef getBOLTFileSymbolName() { return "bolt-pseudo.o"; }
431+
429432
/// An instance of the input binary we are processing, externally owned.
430433
llvm::object::ELFObjectFileBase *InputFile;
431434

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4493,6 +4493,8 @@ void RewriteInstance::updateELFSymbolTable(
44934493
// Symbols for the new symbol table.
44944494
std::vector<ELFSymTy> Symbols;
44954495

4496+
bool EmittedColdFileSymbol = false;
4497+
44964498
auto getNewSectionIndex = [&](uint32_t OldIndex) {
44974499
// For dynamic symbol table, the section index could be wrong on the input,
44984500
// and its value is ignored by the runtime if it's different from
@@ -4551,6 +4553,20 @@ void RewriteInstance::updateELFSymbolTable(
45514553
Symbols.emplace_back(ICFSymbol);
45524554
}
45534555
if (Function.isSplit()) {
4556+
// Prepend synthetic FILE symbol to prevent local cold fragments from
4557+
// colliding with existing symbols with the same name.
4558+
if (!EmittedColdFileSymbol &&
4559+
FunctionSymbol.getBinding() == ELF::STB_GLOBAL) {
4560+
ELFSymTy FileSymbol;
4561+
FileSymbol.st_shndx = ELF::SHN_ABS;
4562+
FileSymbol.st_name = AddToStrTab(getBOLTFileSymbolName());
4563+
FileSymbol.st_value = 0;
4564+
FileSymbol.st_size = 0;
4565+
FileSymbol.st_other = 0;
4566+
FileSymbol.setBindingAndType(ELF::STB_LOCAL, ELF::STT_FILE);
4567+
Symbols.emplace_back(FileSymbol);
4568+
EmittedColdFileSymbol = true;
4569+
}
45544570
for (const FunctionFragment &FF :
45554571
Function.getLayout().getSplitFragments()) {
45564572
if (FF.getAddress()) {

bolt/test/X86/cdsplit-symbol-names.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# RUN: --call-scale=2 --data=%t.fdata --reorder-blocks=ext-tsp
1111
# RUN: llvm-objdump --syms %t.bolt | FileCheck %s --check-prefix=CHECK-SYMS-WARM
1212

13+
# CHECK-SYMS-WARM: 0000000000000000 l df *ABS* 0000000000000000 bolt-pseudo.o
1314
# CHECK-SYMS-WARM: .text.warm
1415
# CHECK-SYMS-WARM-SAME: chain.warm
1516
# CHECK-SYMS-WARM: .text.cold

0 commit comments

Comments
 (0)