Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -62,14 +62,14 @@ return I->second; } -// A compartor to sort output sections. Returns -1 or 1 if both -// A and B are mentioned in linker scripts. Otherwise, returns 0 +// A compartor to sort output sections. Returns -1 or 1 if +// A or B are mentioned in linker script. Otherwise, returns 0 // to use the default rule which is implemented in Writer.cpp. int LinkerScript::compareSections(StringRef A, StringRef B) { auto E = SectionOrder.end(); auto I = std::find(SectionOrder.begin(), E, A); auto J = std::find(SectionOrder.begin(), E, B); - if (I == E || J == E) + if (I == E && J == E) return 0; return I < J ? -1 : 1; } Index: test/ELF/linkerscript-orphans.s =================================================================== --- test/ELF/linkerscript-orphans.s +++ test/ELF/linkerscript-orphans.s @@ -0,0 +1,31 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "SECTIONS { .writable : { *(.writable) } }" > %t.script +# RUN: ld.lld -o %t.out --script %t.script %t +# RUN: llvm-objdump -section-headers %t.out | \ +# RUN: FileCheck -check-prefix=TEXTORPHAN %s + +# RUN: echo "SECTIONS { .text : { *(.text) } }" > %t.script +# RUN: ld.lld -o %t.out --script %t.script %t +# RUN: llvm-objdump -section-headers %t.out | \ +# RUN: FileCheck -check-prefix=WRITABLEORPHAN %s + +# TEXTORPHAN: Sections: +# TEXTORPHAN-NEXT: Idx Name Size Address Type +# TEXTORPHAN-NEXT: 0 00000000 0000000000000000 +# TEXTORPHAN-NEXT: 1 .writable 00000004 0000000000011000 DATA +# TEXTORPHAN-NEXT: 2 .text 00000001 0000000000012000 TEXT DATA + +# WRITABLEORPHAN: Sections: +# WRITABLEORPHAN-NEXT: Idx Name Size Address Type +# WRITABLEORPHAN-NEXT: 0 00000000 0000000000000000 +# WRITABLEORPHAN-NEXT: 1 .text 00000001 0000000000011000 TEXT DATA +# WRITABLEORPHAN-NEXT: 2 .writable 00000004 0000000000012000 DATA + +.global _start +_start: + nop + +.section .writable,"aw" + .zero 4