[PATCH] D132195: [BOLT] Add data to code relocations postprocessing

Amir Ayupov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 18 18:46:52 PDT 2022


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

BOLT's assumption regarding data to code relocations is that
they will be "consumed", or accounted for, as jump table entries.
For jump tables that we failed to recognize, the relocation would
still be created and later attempted to be emitted, but if the
symbol was removed, this leads to undefined symbol error.

Add a post-processing step that accounts for such unclaimed
relocations and creates secondary entry points.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132195

Files:
  bolt/include/bolt/Core/BinaryContext.h
  bolt/lib/Core/BinaryContext.cpp
  bolt/lib/Rewrite/RewriteInstance.cpp


Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ bolt/lib/Rewrite/RewriteInstance.cpp
@@ -2868,6 +2868,7 @@
 
   BC->clearJumpTableTempData();
   BC->adjustCodePadding();
+  BC->postProcessDataRelocations();
 
   for (auto &BFI : BC->getBinaryFunctions()) {
     BinaryFunction &Function = BFI.second;
Index: bolt/lib/Core/BinaryContext.cpp
===================================================================
--- bolt/lib/Core/BinaryContext.cpp
+++ bolt/lib/Core/BinaryContext.cpp
@@ -991,6 +991,52 @@
   }
 }
 
+void BinaryContext::postProcessDataRelocations() {
+  for (BinarySection &Section : sections()) {
+    const uint64_t SourceSectionStart = Section.getAddress();
+    for (const Relocation &Rel : Section.relocations()) {
+      LLVM_DEBUG({
+        dbgs() << "BOLT-DEBUG: post-processing relocation ";
+        Rel.print(dbgs());
+        dbgs() << '\n';
+      });
+      // PC-relative relocations have separate checking mechanism (strict mode)
+      if (Relocation::isPCRelative(Rel.Type))
+        continue;
+      // We expect relocations against function local labels, which have
+      // Addend == 0.
+      if (Rel.Addend)
+        continue;
+      const MCSymbol *ReferencedSymbol = Rel.Symbol;
+      // Check if the relocation value is a function entry point
+      if (getFunctionForSymbol(ReferencedSymbol))
+        continue;
+      // Check if the relocation value is a recognized jump table target
+      const uint64_t RelValue = Rel.Value;
+      const uint64_t RelSourceAddress = SourceSectionStart + Rel.Offset;
+      BinaryFunction *BF = getBinaryFunctionContainingAddress(
+          RelValue, /* CheckPastEnd */ false, /* UseMaxSize */ false);
+      // If there's no function at RelValue address, no need to register the
+      // target symbol as an entry point - i.e. checking past function end.
+      if (!BF)
+        continue;
+      // Skip non-simple functions
+      if (!BF->isSimple())
+        continue;
+      const JumpTable *JT = BF->getJumpTableContainingAddress(RelSourceAddress);
+      if (JT && JT->Labels.find(RelValue) != JT->Labels.end())
+        continue;
+      // Otherwise register the relocation target as a secondary entry point
+      const uint64_t Offset = RelValue - BF->getAddress();
+      const MCSymbol *Label = BF->addEntryPointAtOffset(Offset);
+      LLVM_DEBUG({
+        dbgs() << "BOLT-DEBUG: registered label " << Label->getName()
+               << " as a secondary entry point in function " << *BF << '\n';
+      });
+    }
+  }
+}
+
 MCSymbol *BinaryContext::registerNameAtAddress(StringRef Name, uint64_t Address,
                                                uint64_t Size,
                                                uint16_t Alignment,
Index: bolt/include/bolt/Core/BinaryContext.h
===================================================================
--- bolt/include/bolt/Core/BinaryContext.h
+++ bolt/include/bolt/Core/BinaryContext.h
@@ -526,6 +526,10 @@
   /// accordingly.
   void adjustCodePadding();
 
+  /// Verify unclaimed Data to Code relocations, creating secondary entry points
+  /// for targets.
+  void postProcessDataRelocations();
+
   /// Regular page size.
   unsigned RegularPageSize{0x1000};
   static constexpr unsigned RegularPageSizeX86 = 0x1000;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132195.453859.patch
Type: text/x-patch
Size: 3378 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220819/97ba5d34/attachment.bin>


More information about the llvm-commits mailing list