Skip to content

Commit e9935a9

Browse files
maksfbmemfrob
authored andcommitted
[BOLT] Create symbol table entries under -hot-text if they did not exist
Summary: If "-hot-text" options is specified and the input binary did not have __hot_start/__hot_end symbols, then add them to the symbol table. (cherry picked from FBD6027737)
1 parent 85e7ad2 commit e9935a9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

bolt/RewriteInstance.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,6 +3356,7 @@ void RewriteInstance::patchELFSymTabs(ELFObjectFile<ELFT> *File) {
33563356
Write,
33573357
std::function<size_t(StringRef)> AddToStrTab) {
33583358
auto StringSection = *Obj->getStringTableForSymtab(*Section);
3359+
unsigned IsHotTextUpdated = 0;
33593360

33603361
for (const Elf_Sym &Symbol : Obj->symbols(Section)) {
33613362
auto NewSymbol = Symbol;
@@ -3412,6 +3413,7 @@ void RewriteInstance::patchELFSymTabs(ELFObjectFile<ELFT> *File) {
34123413
NewSymbol.st_shndx = ELF::SHN_ABS;
34133414
outs() << "BOLT-INFO: setting " << Name << " to 0x"
34143415
<< Twine::utohexstr(NewSymbol.st_value) << '\n';
3416+
++IsHotTextUpdated;
34153417
return true;
34163418
};
34173419

@@ -3424,6 +3426,28 @@ void RewriteInstance::patchELFSymTabs(ELFObjectFile<ELFT> *File) {
34243426
Write((&Symbol - Obj->symbol_begin(Section)) * sizeof(Elf_Sym),
34253427
reinterpret_cast<const char *>(&NewSymbol), sizeof(NewSymbol));
34263428
}
3429+
3430+
assert((!IsHotTextUpdated || IsHotTextUpdated == 2) &&
3431+
"either none or both __hot_start/__hot_end symbols were expected");
3432+
if (opts::HotText && !IsHotTextUpdated && !PatchExisting) {
3433+
auto addSymbol = [&](const std::string &Name) {
3434+
Elf_Sym Symbol;
3435+
Symbol.st_value = getNewValueForSymbol(Name);
3436+
Symbol.st_shndx = ELF::SHN_ABS;
3437+
Symbol.st_name = AddToStrTab(Name);
3438+
Symbol.st_size = 0;
3439+
Symbol.st_shndx = 0;
3440+
Symbol.st_other = 0;
3441+
Symbol.setBindingAndType(ELF::STB_WEAK, ELF::STT_NOTYPE);
3442+
3443+
outs() << "BOLT-INFO: setting " << Name << " to 0x"
3444+
<< Twine::utohexstr(Symbol.st_value) << '\n';
3445+
3446+
Write(0, reinterpret_cast<const char *>(&Symbol), sizeof(Symbol));
3447+
};
3448+
addSymbol("__hot_start");
3449+
addSymbol("__hot_end");
3450+
}
34273451
};
34283452

34293453
// Update dynamic symbol table.

0 commit comments

Comments
 (0)