[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 12:53:24 PDT 2025
================
@@ -1413,6 +1415,29 @@ void Writer::removeUnusedSections() {
llvm::erase_if(ctx.outputSections, isUnused);
}
+void Writer::layoutSections() {
+ llvm::TimeTraceScope timeScope("Layout sections");
+ if (ctx.config.sectionOrder.empty())
+ return;
+
+ llvm::stable_sort(ctx.outputSections,
+ [this](const OutputSection *a, const OutputSection *b) {
+ auto itA = ctx.config.sectionOrder.find(a->name.str());
+ auto itB = ctx.config.sectionOrder.find(b->name.str());
+ bool aInOrder = itA != ctx.config.sectionOrder.end();
+ bool bInOrder = itB != ctx.config.sectionOrder.end();
+
+ // Put unspecified sections after all specified sections
+ if (aInOrder && bInOrder) {
+ return itA->second < itB->second;
+ } else if (aInOrder && !bInOrder) {
+ return true;
+ } else {
+ return false;
----------------
mstorsjo wrote:
I wonder if it'd be good to clarify with a comment, that this both is for the `!aInOrder && bInOrder`, where we explicitly do want one specific order, as complement to the case above, but that we want to treat `!aInOrder && !bInOrder` as equal, i.e. keeping their original order.
https://github.com/llvm/llvm-project/pull/152779
More information about the llvm-commits
mailing list