[PATCH] D135121: [BOLT] Properly set _end symbol

Maksim Panchenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 3 18:14:41 PDT 2022


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

To properly set the "_end" symbol, we need to track the last allocatable
address. Simply emitting "_end" at the end of some section is not
sufficient since the order of section allocation is unknown during the
emission step.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135121

Files:
  bolt/lib/Core/BinaryEmitter.cpp
  bolt/lib/Rewrite/RewriteInstance.cpp
  bolt/test/X86/end-symbol.test


Index: bolt/test/X86/end-symbol.test
===================================================================
--- /dev/null
+++ bolt/test/X86/end-symbol.test
@@ -0,0 +1,9 @@
+# RUN: yaml2obj %p/Inputs/plt-sec.yaml &> %t.exe
+# RUN: llvm-bolt %t.exe -o %t.out
+# RUN: (llvm-readelf --program-headers %t.out | tac ; llvm-nm %t.out) \
+# RUN:   | FileCheck %s
+
+## Check that llvm-bolt correctly updates _end symbol to match the end of the
+## last loadable segment.
+CHECK: LOAD 0x[[#%x,OFFSET:]] 0x[[#%x,VMA:]] 0x[[#%x,PA:]] 0x[[#%x,FILESIZE:]] 0x[[#%x,MEMSIZE:]]
+CHECK: [[#%.16x, VMA + MEMSIZE]] {{.}} _end
Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ bolt/lib/Rewrite/RewriteInstance.cpp
@@ -4730,27 +4730,29 @@
     Expected<StringRef> SymbolName = Symbol.getName(StringSection);
     assert(SymbolName && "cannot get symbol name");
 
-    auto updateSymbolValue = [&](const StringRef Name, unsigned &IsUpdated) {
-      NewSymbol.st_value = getNewValueForSymbol(Name);
+    auto updateSymbolValue = [&](const StringRef Name,
+                                 Optional<uint64_t> Value = NoneType()) {
+      NewSymbol.st_value = Value ? *Value : getNewValueForSymbol(Name);
       NewSymbol.st_shndx = ELF::SHN_ABS;
       outs() << "BOLT-INFO: setting " << Name << " to 0x"
              << Twine::utohexstr(NewSymbol.st_value) << '\n';
-      ++IsUpdated;
     };
 
     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);
+        (*SymbolName == "__hot_start" || *SymbolName == "__hot_end")) {
+      updateSymbolValue(*SymbolName);
+      ++NumHotTextSymsUpdated;
+    }
 
-    if (*SymbolName == "_end") {
-      unsigned Ignored;
-      updateSymbolValue(*SymbolName, Ignored);
+    if (opts::HotData && (*SymbolName == "__hot_data_start" ||
+                          *SymbolName == "__hot_data_end")) {
+      updateSymbolValue(*SymbolName);
+      ++NumHotDataSymsUpdated;
     }
 
+    if (*SymbolName == "_end")
+      updateSymbolValue(*SymbolName, NextAvailableAddress);
+
     if (IsDynSym)
       Write((&Symbol - cantFail(Obj.symbols(&SymTabSection)).begin()) *
                 sizeof(ELFSymTy),
Index: bolt/lib/Core/BinaryEmitter.cpp
===================================================================
--- bolt/lib/Core/BinaryEmitter.cpp
+++ bolt/lib/Core/BinaryEmitter.cpp
@@ -211,8 +211,6 @@
   }
 
   emitDataSections(OrgSecPrefix);
-
-  Streamer.emitLabel(BC.Ctx->getOrCreateSymbol("_end"));
 }
 
 void BinaryEmitter::emitFunctions() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135121.464867.patch
Type: text/x-patch
Size: 2843 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221004/b2893f8f/attachment-0001.bin>


More information about the llvm-commits mailing list