[lld] [LLD][COFF] Add support for custom section layout (PR #152779)
Martin Storsjö via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 3 04:34:23 PDT 2025
================
@@ -1413,6 +1415,30 @@ 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;
+
+ // Not found in layout file; respect the original order
+ return originalOrder[a] < originalOrder[b];
----------------
mstorsjo wrote:
That sounds like quite overkill - wouldn't this be doable just with the earlier `llvm::stable_sort`, just by adjusting the comparison function to take those cases into account? I see you've done exactly this in the updated form of the PR, so that's probably good as such, no need for a custom insertion sort implementation.
https://github.com/llvm/llvm-project/pull/152779
More information about the llvm-commits
mailing list