[PATCH] D146645: [BOLT] Create section symbols for inserted sections

Vladislav Khmelevsky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 22 10:18:32 PDT 2023


yota9 created this revision.
yota9 added reviewers: maksfb, rafauler, Amir.
Herald added subscribers: treapster, ayermolo.
Herald added a project: All.
yota9 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Just for consistensy create section symbols for inserted
sections (sections without reference).

Vladislav Khmelevsky,
Advanced Software Technology Lab, Huawei


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146645

Files:
  bolt/lib/Rewrite/RewriteInstance.cpp
  bolt/test/create_section_symbol.c


Index: bolt/test/create_section_symbol.c
===================================================================
--- /dev/null
+++ 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(); }
Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ 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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146645.507420.patch
Type: text/x-patch
Size: 1961 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230322/9c317f3b/attachment.bin>


More information about the llvm-commits mailing list