Skip to content

[BOLT] Emit synthetic FILE symbol for local cold fragments of global symbols #89794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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