diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -4928,7 +4928,7 @@ if (!Section) return false; - // Remove the section symbol iif the corresponding section was stripped. + // Remove the section symbol if the corresponding section was stripped. if (Symbol.getType() == ELF::STT_SECTION) { if (!getNewSectionIndex(Symbol.st_shndx)) return true; @@ -5169,6 +5169,24 @@ addSymbol("__hot_data_end"); } + auto addSectionSymbol = [&](uint64_t Address, unsigned Index) { + ELFSymTy Symbol; + Symbol.st_value = Address; + Symbol.st_shndx = Index; + Symbol.st_name = AddToStrTab(""); + Symbol.st_size = 0; + Symbol.st_other = 0; + Symbol.setBindingAndType(ELF::STB_LOCAL, ELF::STT_SECTION); + Symbols.emplace_back(Symbol); + }; + + for (BinarySection &Section : BC->allocatableSections()) { + if (Section.hasSectionRef()) + continue; + + addSectionSymbol(Section.getOutputAddress(), Section.getIndex()); + } + // Put local symbols at the beginning. llvm::stable_sort(Symbols, [](const ELFSymTy &A, const ELFSymTy &B) { if (A.getBinding() == ELF::STB_LOCAL && B.getBinding() != ELF::STB_LOCAL) diff --git a/bolt/test/create_section_symbol.c b/bolt/test/create_section_symbol.c new file mode 100644 --- /dev/null +++ b/bolt/test/create_section_symbol.c @@ -0,0 +1,13 @@ +// This test check that newely created text section would have STT_SECTION type +// symbol in symtab. + +// RUN: %clang %cflags -nostdlib -Wl,-q %s -o %t.exe +// RUN: llvm-bolt %t.exe -o %t.bolt +// RUN: llvm-readelf -SsW %t.bolt | FileCheck %s + +// CHECK: [[#%x,NUM:]]] .text PROGBITS [[#%x,ADDR:]] +// CHECK: {{0*}}[[#ADDR]] 0 SECTION LOCAL DEFAULT [[#NUM]] .text + +__attribute__((noinline)) int foo() { return 0; } + +void _start() { foo(); }