[lld] [LLD][COFF] Add support for custom section layout (PR #152779)

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 25 08:58:44 PDT 2025


================
@@ -1413,6 +1415,34 @@ void Writer::removeUnusedSections() {
   llvm::erase_if(ctx.outputSections, isUnused);
 }
 
+void Writer::layoutSections() {
+  llvm::TimeTraceScope timeScope("Layout sections");
+  if (ctx.config.sectionOrder.empty())
+    return;
+
+  std::unordered_map<const OutputSection *, size_t> originalOrder;
+  for (size_t i = 0; i < ctx.outputSections.size(); ++i)
+    originalOrder[ctx.outputSections[i]] = i;
+
+  llvm::stable_sort(
+      ctx.outputSections,
+      [this, &originalOrder](const OutputSection *a, const OutputSection *b) {
+        auto itA = ctx.config.sectionOrder.find(a->name.str());
+        auto itB = ctx.config.sectionOrder.find(b->name.str());
+
+        if (itA != ctx.config.sectionOrder.end() &&
+            itB != ctx.config.sectionOrder.end())
+          return itA->second < itB->second;
+
+        if (itA == ctx.config.sectionOrder.end() &&
----------------
aganea wrote:

You can remove this whole `if`, the other `return` below does the same thing.

https://github.com/llvm/llvm-project/pull/152779


More information about the llvm-commits mailing list