[PATCH] D143085: [BOLT] Search section based on relocation symbol

Vladislav Khmelevsky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 6 15:18:18 PST 2023


yota9 updated this revision to Diff 495287.
yota9 added a comment.

@rafauler I've found out that we can make small linker scripts for lld, it uses it as a hint and adds missing sections by it self.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143085/new/

https://reviews.llvm.org/D143085

Files:
  bolt/lib/Rewrite/RewriteInstance.cpp
  bolt/test/AArch64/Inputs/array_end.ldd_script
  bolt/test/AArch64/array_end.c


Index: bolt/test/AArch64/array_end.c
===================================================================
--- /dev/null
+++ bolt/test/AArch64/array_end.c
@@ -0,0 +1,23 @@
+// Test checks that bolt properly finds end section label.
+// Linker script contains gap after destructor array, so
+// __init_array_end address would not be owned by any section.
+
+// REQUIRES: system-linux
+// RUN: %clang %cflags -no-pie %s -o %t.exe -Wl,-q \
+// RUN:   -Wl,-T %S/Inputs/array_end.ldd_script
+// RUN: llvm-bolt %t.exe -o %t.bolt --print-disasm \
+// RUN:  --print-only="callFini" | FileCheck %s
+
+// CHECK: adr [[REG:x[0-28]+]], "__fini_array_end/1"
+
+__attribute__((destructor)) void destr() {}
+
+__attribute__((noinline)) void callFini() {
+  extern void (*__fini_array_start[])();
+  extern void (*__fini_array_end[])();
+  unsigned long Count = __fini_array_end - __fini_array_start;
+  for (unsigned long I = 0; I < Count; ++I)
+    (*__fini_array_start[I])();
+}
+
+void _start() { callFini(); }
Index: bolt/test/AArch64/Inputs/array_end.ldd_script
===================================================================
--- /dev/null
+++ bolt/test/AArch64/Inputs/array_end.ldd_script
@@ -0,0 +1,13 @@
+SECTIONS {
+  .fini_array    :
+  {
+    PROVIDE_HIDDEN (__fini_array_start = .);
+    KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
+    KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
+    PROVIDE_HIDDEN (__fini_array_end = .);
+  }
+
+  . = . + 128;
+
+  .text : { *(.text) }
+}
Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ bolt/lib/Rewrite/RewriteInstance.cpp
@@ -2516,8 +2516,21 @@
     if (BinaryData *BD = BC->getBinaryDataByName(SymbolName))
       ReferencedSymbol = BD->getSymbol();
 
-  ErrorOr<BinarySection &> ReferencedSection =
-      BC->getSectionForAddress(SymbolAddress);
+  ErrorOr<BinarySection &> ReferencedSection{std::errc::bad_address};
+  symbol_iterator SymbolIter = Rel.getSymbol();
+  if (SymbolIter != InputFile->symbol_end()) {
+    SymbolRef Symbol = *SymbolIter;
+    section_iterator Section =
+        cantFail(Symbol.getSection(), "cannot get symbol section");
+    if (Section != InputFile->section_end()) {
+      Expected<StringRef> SectionName = Section->getName();
+      if (SectionName && !SectionName->empty())
+        ReferencedSection = BC->getUniqueSectionByName(*SectionName);
+    }
+  }
+
+  if (!ReferencedSection)
+    ReferencedSection = BC->getSectionForAddress(SymbolAddress);
 
   const bool IsToCode = ReferencedSection && ReferencedSection->isText();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143085.495287.patch
Type: text/x-patch
Size: 2700 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230206/a95f3fa3/attachment.bin>


More information about the llvm-commits mailing list