Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -210,7 +210,8 @@ InputSectionBase *L = File->getSections()[Link]; if (auto *IS = dyn_cast(L)) return IS; - error("a section with SHF_LINK_ORDER should not refer a non-regular " + error("a section " + toString(this) + + " with SHF_LINK_ORDER should not refer a non-regular " "section: " + toString(L)); } Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1307,6 +1307,15 @@ } } } + + // Check that all sections have link order dependency set. Ignore sentinels. + for (InputSection *IS : Sections) + if (!isa(IS) && !IS->getLinkOrderDep()) + error("unable to place section " + toString(IS) + " into section '" + + Sec->Name + "': SHF_LINK_ORDER flags mismatch"); + if (errorCount()) + return; + std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition); if (!Config->Relocatable && Config->EMachine == EM_ARM && Index: test/ELF/section-metadata-err.s =================================================================== --- test/ELF/section-metadata-err.s +++ test/ELF/section-metadata-err.s @@ -3,7 +3,7 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o # RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s -# CHECK: error: a section with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err.s.tmp.o:(.foo) +# CHECK: error: a section {{.*}}section-metadata-err.s.tmp.o:(.bar) with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err.s.tmp.o:(.foo) .global _start _start: @@ -12,4 +12,4 @@ .section .foo,"aM",@progbits,8 .quad 0 -.section bar,"ao",@progbits,.foo +.section .bar,"ao",@progbits,.foo Index: test/ELF/section-metadata-err2.s =================================================================== --- test/ELF/section-metadata-err2.s +++ test/ELF/section-metadata-err2.s @@ -0,0 +1,17 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s + +## Check we do not crash and report proper errors. +# CHECK: error: a section {{.*}}section-metadata-err2.s.tmp.o:(.bar) with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err2.s.tmp.o:(.foo) +# CHECK: error: a section {{.*}}section-metadata-err2.s.tmp.o:(.bar) with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err2.s.tmp.o:(.foo) + +.section .foo,"aM",@progbits,8 +.quad 0 + +.section .bar,"ao",@progbits,.foo,unique,1 +.quad 0 + +.section .bar,"ao",@progbits,.foo,unique,2 +.quad 1 Index: test/ELF/section-metadata-err3.s =================================================================== --- test/ELF/section-metadata-err3.s +++ test/ELF/section-metadata-err3.s @@ -0,0 +1,15 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s + +# CHECK: error: unable to place section {{.*}}section-metadata-err3.s.tmp.o:(.bar) into section '.bar': SHF_LINK_ORDER flags mismatch + +.section .foo,"a",@progbits +.quad 0 + +.section .bar,"ao",@progbits,.foo,unique,1 +.quad 0 + +.section .bar,"a",@progbits,unique,2 +.quad 1