[llvm] f04aaf9 - [ORC][ELF] Process .init_array sections in priority order.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 1 10:35:55 PDT 2024


Author: Lang Hames
Date: 2024-11-01T10:35:49-07:00
New Revision: f04aaf948204068276f57dbcba5734c53382c7e2

URL: https://github.com/llvm/llvm-project/commit/f04aaf948204068276f57dbcba5734c53382c7e2
DIFF: https://github.com/llvm/llvm-project/commit/f04aaf948204068276f57dbcba5734c53382c7e2.diff

LOG: [ORC][ELF] Process .init_array sections in priority order.

Fixes compiler-rt/test/orc/TestCases/Linux/x86-64/priority-static-initializer.S
testcase after 244ea406259. This testcase had been succeeding because the
definition order of the .init_array sections in the testcase matched their
priorities, but began failing once 244ea406259 removed that guarantee. The
proper fix is to visit the .init_array sections according to their priority
order, regardless of how they're defined in the file.

This fixes the single-file testcase, but I think that ELFNixPlatform will need
to be extended to make priorities work correctly across file boundaries.

Added: 
    

Modified: 
    llvm/lib/ExecutionEngine/Orc/ELFNixPlatform.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ExecutionEngine/Orc/ELFNixPlatform.cpp b/llvm/lib/ExecutionEngine/Orc/ELFNixPlatform.cpp
index 610ecbff5c5c4d..c8c341668ddd0e 100644
--- a/llvm/lib/ExecutionEngine/Orc/ELFNixPlatform.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/ELFNixPlatform.cpp
@@ -921,12 +921,42 @@ Error ELFNixPlatform::ELFNixPlatformPlugin::registerInitSections(
   SmallVector<ExecutorAddrRange> ELFNixPlatformSecs;
   LLVM_DEBUG(dbgs() << "ELFNixPlatform::registerInitSections\n");
 
-  for (auto &Sec : G.sections()) {
-    if (isELFInitializerSection(Sec.getName())) {
-      jitlink::SectionRange R(Sec);
-      ELFNixPlatformSecs.push_back(R.getRange());
+  SmallVector<jitlink::Section *> OrderedInitSections;
+  for (auto &Sec : G.sections())
+    if (isELFInitializerSection(Sec.getName()))
+      OrderedInitSections.push_back(&Sec);
+
+  // FIXME: This handles priority order within the current graph, but we'll need
+  //        to include priority information in the initializer allocation
+  //        actions in order to respect the ordering across multiple graphs.
+  llvm::sort(OrderedInitSections, [](const jitlink::Section *LHS,
+                                     const jitlink::Section *RHS) {
+    if (LHS->getName().starts_with(".init_array")) {
+      if (RHS->getName().starts_with(".init_array")) {
+        StringRef LHSPrioStr(LHS->getName());
+        StringRef RHSPrioStr(RHS->getName());
+        uint64_t LHSPriority;
+        bool LHSHasPriority = LHSPrioStr.consume_front(".init_array.") &&
+                              !LHSPrioStr.getAsInteger(10, LHSPriority);
+        uint64_t RHSPriority;
+        bool RHSHasPriority = RHSPrioStr.consume_front(".init_array.") &&
+                              !RHSPrioStr.getAsInteger(10, RHSPriority);
+        if (LHSHasPriority)
+          return RHSHasPriority ? LHSPriority < RHSPriority : true;
+        else if (RHSHasPriority)
+          return false;
+        // If we get here we'll fall through to the
+        // LHS->getName() < RHS->getName() test below.
+      } else {
+        // .init_array[.N] comes before any non-.init_array[.N] section.
+        return true;
+      }
     }
-  }
+    return LHS->getName() < RHS->getName();
+  });
+
+  for (auto &Sec : OrderedInitSections)
+    ELFNixPlatformSecs.push_back(jitlink::SectionRange(*Sec).getRange());
 
   // Dump the scraped inits.
   LLVM_DEBUG({


        


More information about the llvm-commits mailing list