Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -1014,13 +1014,13 @@ } template void Writer::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(Base)) Index: lld/trunk/test/ELF/relocatable-script.s =================================================================== --- lld/trunk/test/ELF/relocatable-script.s +++ lld/trunk/test/ELF/relocatable-script.s @@ -0,0 +1,7 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux -o %t1.o %s +# RUN: echo "SECTIONS { .foo : { BYTE(0x0) } }" > %t.script +# RUN: ld.lld -r %t1.o -script %t.script -o %t2.o +# RUN: llvm-readobj -sections %t2.o | FileCheck %s + +# CHECK: Name: .foo