Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -307,7 +307,19 @@ // you can call lld::elf::main more than once as a library. memset(&Out::First, 0, sizeof(Out)); - auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); }; + auto Add = [](InputSectionBase *Sec) { + // We assume all synthetic sections are placed to the end, + // after regular ones. Do not mix them. We allow merge sections + // because them are converted to synthetic mergeable sections later. + if (!InputSections.empty()) { + auto IsOk = [](InputSectionBase::Kind K) { + return K == InputSectionBase::Synthetic || K == InputSectionBase::Merge; + }; + if (IsOk(InputSections.back()->kind())) + assert(IsOk(Sec->kind())); + } + InputSections.push_back(Sec); + }; // Create singleton output sections. Out::Bss = make(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); @@ -324,6 +336,12 @@ Out::ProgramHeaders = make("", 0, SHF_ALLOC); Out::ProgramHeaders->updateAlignment(sizeof(uintX_t)); + InputSection *Common = createCommonSection(); + if (!Common->Data.empty()) { + In::Common = Common; + Add(Common); + } + if (needsInterpSection()) { In::Interp = createInterpSection(); Add(In::Interp); @@ -344,12 +362,6 @@ Add(In::BuildId); } - InputSection *Common = createCommonSection(); - if (!Common->Data.empty()) { - In::Common = Common; - Add(Common); - } - // Add MIPS-specific sections. bool HasDynSymTab = !Symtab::X->getSharedFiles().empty() || Config->pic() ||