Index: ELF/ScriptParser.cpp =================================================================== --- ELF/ScriptParser.cpp +++ ELF/ScriptParser.cpp @@ -1121,8 +1121,14 @@ } static void checkIfExists(OutputSection *Cmd, StringRef Location) { - if (Cmd->Location.empty() && Script->ErrorOnMissingSection) + if (!Script->ErrorOnMissingSection) + return; + + if (Cmd->Location.empty()) error(Location + ": undefined section " + Cmd->Name); + else if (!Cmd->Live) + error(Cmd->Location + ": reference to removed section '" + Cmd->Name + + "' not allowed"); } Expr ScriptParser::readPrimary() { Index: test/ELF/linkerscript/empty-sections-expressions.test =================================================================== --- test/ELF/linkerscript/empty-sections-expressions.test +++ test/ELF/linkerscript/empty-sections-expressions.test @@ -0,0 +1,13 @@ +# REQUIRES: x86 +# RUN: echo ".text; nop; .data; .byte 0" \ +# RUN: | llvm-mc -filetype=obj -triple=x86_64-pc-linux - -o %t.o +# RUN: not ld.lld -o %t --script %s %t.o 2>&1 | FileCheck %s + +# CHECK: error: {{.*}}.test:10: reference to removed section '.empty' not allowed + +SECTIONS { + . = 0x00080000; + .empty : { *(.empty ) } + .text : AT(LOADADDR (.empty)) { *(.text) } + .data : AT(ADDR (.empty)) { *(.data) } +}