Skip to content

Commit

Permalink
[BOLT] Ignore special symbols as function aliases in updateELFSymbolT…
Browse files Browse the repository at this point in the history
…able

Exempt special symbols (hot text/data and _end symbol) from normal
handling. We only need to set their value and make them absolute.

If these symbols are handled as normal symbols and if they alias
functions we may create non-sensical symbols, e.g. __hot_start.cold.

Test Plan: updated hot-end-symbol.s

Reviewers: maksfb, rafaelauler, ayermolo, dcci

Reviewed By: dcci, maksfb

Pull Request: #92713
  • Loading branch information
aaupov authored May 20, 2024
1 parent 888e087 commit bb627b0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
62 changes: 35 additions & 27 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4808,6 +4808,40 @@ void RewriteInstance::updateELFSymbolTable(
// Create a new symbol based on the existing symbol.
ELFSymTy NewSymbol = Symbol;

// Handle special symbols based on their name.
Expected<StringRef> SymbolName = Symbol.getName(StringSection);
assert(SymbolName && "cannot get symbol name");

auto updateSymbolValue = [&](const StringRef Name,
std::optional<uint64_t> Value = std::nullopt) {
NewSymbol.st_value = Value ? *Value : getNewValueForSymbol(Name);
NewSymbol.st_shndx = ELF::SHN_ABS;
BC->outs() << "BOLT-INFO: setting " << Name << " to 0x"
<< Twine::utohexstr(NewSymbol.st_value) << '\n';
};

if (*SymbolName == "__hot_start" || *SymbolName == "__hot_end") {
if (opts::HotText) {
updateSymbolValue(*SymbolName);
++NumHotTextSymsUpdated;
}
goto registerSymbol;
}

if (*SymbolName == "__hot_data_start" || *SymbolName == "__hot_data_end") {
if (opts::HotData) {
updateSymbolValue(*SymbolName);
++NumHotDataSymsUpdated;
}
goto registerSymbol;
}

if (*SymbolName == "_end") {
if (NextAvailableAddress > Symbol.st_value)
updateSymbolValue(*SymbolName, NextAvailableAddress);
goto registerSymbol;
}

if (Function) {
// If the symbol matched a function that was not emitted, update the
// corresponding section index but otherwise leave it unchanged.
Expand Down Expand Up @@ -4904,33 +4938,7 @@ void RewriteInstance::updateELFSymbolTable(
}
}

// Handle special symbols based on their name.
Expected<StringRef> SymbolName = Symbol.getName(StringSection);
assert(SymbolName && "cannot get symbol name");

auto updateSymbolValue = [&](const StringRef Name,
std::optional<uint64_t> Value = std::nullopt) {
NewSymbol.st_value = Value ? *Value : getNewValueForSymbol(Name);
NewSymbol.st_shndx = ELF::SHN_ABS;
BC->outs() << "BOLT-INFO: setting " << Name << " to 0x"
<< Twine::utohexstr(NewSymbol.st_value) << '\n';
};

if (opts::HotText &&
(*SymbolName == "__hot_start" || *SymbolName == "__hot_end")) {
updateSymbolValue(*SymbolName);
++NumHotTextSymsUpdated;
}

if (opts::HotData && (*SymbolName == "__hot_data_start" ||
*SymbolName == "__hot_data_end")) {
updateSymbolValue(*SymbolName);
++NumHotDataSymsUpdated;
}

if (*SymbolName == "_end" && NextAvailableAddress > Symbol.st_value)
updateSymbolValue(*SymbolName, NextAvailableAddress);

registerSymbol:
if (IsDynSym)
Write((&Symbol - cantFail(Obj.symbols(&SymTabSection)).begin()) *
sizeof(ELFSymTy),
Expand Down
3 changes: 2 additions & 1 deletion bolt/test/runtime/X86/hot-end-symbol.s
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# RUN: %clang %cflags -no-pie %t.o -o %t.exe -Wl,-q

# RUN: llvm-bolt %t.exe --relocs=1 --hot-text --reorder-functions=hfsort \
# RUN: --split-functions --split-strategy=all \
# RUN: --data %t.fdata -o %t.out | FileCheck %s

# RUN: %t.out 1
Expand All @@ -30,12 +31,12 @@
# CHECK-OUTPUT: __hot_start
# CHECK-OUTPUT-NEXT: main
# CHECK-OUTPUT-NEXT: __hot_end
# CHECK-OUTPUT-NOT: __hot_start.cold

.text
.globl main
.type main, %function
.globl __hot_start
.type __hot_start, %object
.p2align 4
main:
__hot_start:
Expand Down

0 comments on commit bb627b0

Please sign in to comment.