[ELF] - Do not crash when LLD synthesizes output sections with BYTE commands and -r
This is PR33596. Previously LLD would crash
because BYTE command synthesized output section,
but it was not assigned to Sec member of OutputSectionCommand.
Behaviour of -script and -r combination is not well defined,
but it seems after this change LLD naturally inherits behavior of
GNU linkers - creates output section requested in script and does not
crash anymore.
Differential revision: https://reviews.llvm.org/D34676
llvm-svn: 306527
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index b167ddd..112eee5 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1014,13 +1014,13 @@
}
template <class ELFT> void Writer<ELFT>::sortSections() {
+ if (Script->Opt.HasSections)
+ Script->adjustSectionsBeforeSorting();
+
// Don't sort if using -r. It is not necessary and we want to preserve the
// relative order for SHF_LINK_ORDER sections.
if (Config->Relocatable)
- return;
-
- if (Script->Opt.HasSections)
- Script->adjustSectionsBeforeSorting();
+ return;
for (BaseCommand *Base : Script->Opt.Commands)
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))