Skip to content

Commit

Permalink
[BOLT] Emit synthetic FILE symbol for local cold fragments of global …
Browse files Browse the repository at this point in the history
…symbols (#89794)
  • Loading branch information
aaupov authored Apr 25, 2024
1 parent 10661ba commit 090c92e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bolt/include/bolt/Rewrite/RewriteInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ class RewriteInstance {
static StringRef getEHFrameSectionName() { return ".eh_frame"; }
static StringRef getRelaDynSectionName() { return ".rela.dyn"; }

/// FILE symbol name used for local fragments of global functions.
static StringRef getBOLTFileSymbolName() { return "bolt-pseudo.o"; }

/// An instance of the input binary we are processing, externally owned.
llvm::object::ELFObjectFileBase *InputFile;

Expand Down
16 changes: 16 additions & 0 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4493,6 +4493,8 @@ void RewriteInstance::updateELFSymbolTable(
// Symbols for the new symbol table.
std::vector<ELFSymTy> Symbols;

bool EmittedColdFileSymbol = false;

auto getNewSectionIndex = [&](uint32_t OldIndex) {
// For dynamic symbol table, the section index could be wrong on the input,
// and its value is ignored by the runtime if it's different from
Expand Down Expand Up @@ -4551,6 +4553,20 @@ void RewriteInstance::updateELFSymbolTable(
Symbols.emplace_back(ICFSymbol);
}
if (Function.isSplit()) {
// Prepend synthetic FILE symbol to prevent local cold fragments from
// colliding with existing symbols with the same name.
if (!EmittedColdFileSymbol &&
FunctionSymbol.getBinding() == ELF::STB_GLOBAL) {
ELFSymTy FileSymbol;
FileSymbol.st_shndx = ELF::SHN_ABS;
FileSymbol.st_name = AddToStrTab(getBOLTFileSymbolName());
FileSymbol.st_value = 0;
FileSymbol.st_size = 0;
FileSymbol.st_other = 0;
FileSymbol.setBindingAndType(ELF::STB_LOCAL, ELF::STT_FILE);
Symbols.emplace_back(FileSymbol);
EmittedColdFileSymbol = true;
}
for (const FunctionFragment &FF :
Function.getLayout().getSplitFragments()) {
if (FF.getAddress()) {
Expand Down
1 change: 1 addition & 0 deletions bolt/test/X86/cdsplit-symbol-names.s
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# RUN: --call-scale=2 --data=%t.fdata --reorder-blocks=ext-tsp
# RUN: llvm-objdump --syms %t.bolt | FileCheck %s --check-prefix=CHECK-SYMS-WARM

# CHECK-SYMS-WARM: 0000000000000000 l df *ABS* 0000000000000000 bolt-pseudo.o
# CHECK-SYMS-WARM: .text.warm
# CHECK-SYMS-WARM-SAME: chain.warm
# CHECK-SYMS-WARM: .text.cold
Expand Down

0 comments on commit 090c92e

Please sign in to comment.