Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -832,7 +832,10 @@ // We do not want to remove sections that reference symbols in address and // other expressions. We add script symbols as undefined, and want to ensure // all of them are defined in the output, hence have to keep them. - if (Sec.ExpressionsUseSymbols) + // Another case is sections referenced in ADDR and LOADADDR expressions. We do + // not want to drop the section and make these expressions to be calculated to + // null, because that would break the logic of commonly used generic scripts. + if (Sec.ForceKeepIfEmpty) return false; for (BaseCommand *Base : Sec.SectionCommands) { Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -102,7 +102,7 @@ std::string LMARegionName; bool NonAlloc = false; bool Noload = false; - bool ExpressionsUseSymbols = false; + bool ForceKeepIfEmpty = false; bool InOverlay = false; template void finalize(); Index: ELF/ScriptParser.cpp =================================================================== --- ELF/ScriptParser.cpp +++ ELF/ScriptParser.cpp @@ -868,7 +868,7 @@ consume(","); if (Script->ReferencedSymbols.size() > SymbolsReferenced) - Cmd->ExpressionsUseSymbols = true; + Cmd->ForceKeepIfEmpty = true; return Cmd; } @@ -1146,6 +1146,7 @@ if (Tok == "ADDR") { StringRef Name = readParenLiteral(); OutputSection *Sec = Script->getOrCreateOutputSection(Name); + Sec->ForceKeepIfEmpty = true; return [=]() -> ExprValue { checkIfExists(Sec, Location); return {Sec, false, 0, Location}; @@ -1222,6 +1223,7 @@ if (Tok == "LOADADDR") { StringRef Name = readParenLiteral(); OutputSection *Cmd = Script->getOrCreateOutputSection(Name); + Cmd->ForceKeepIfEmpty = true; return [=] { checkIfExists(Cmd, Location); return Cmd->getLMA(); 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,17 @@ +# REQUIRES: x86 +# RUN: echo ".text; nop; .data; .byte 0" \ +# RUN: | llvm-mc -filetype=obj -triple=x86_64-pc-linux - -o %t.o +# RUN: ld.lld -o %t --script %s %t.o +# RUN: llvm-readelf -program-headers %t | FileCheck %s + +SECTIONS { + . = 0x00080000; + .empty : { *(.empty ) } + .text : AT(LOADADDR (.empty) + SIZEOF (.empty)) { *(.text) } + .data : AT(ADDR (.empty) + 0x2000) { *(.data) } +} + +# CHECK: Program Headers: +# CHECK-NEXT: Type Offset VirtAddr PhysAddr +# CHECK-NEXT: LOAD 0x001000 0x0000000000080000 0x0000000000080000 +# CHECK-NEXT: LOAD 0x001001 0x0000000000080001 0x0000000000082000