summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorge Rimar <grimar@accesssoftek.com>2019-04-26 06:59:30 +0000
committerGeorge Rimar <grimar@accesssoftek.com>2019-04-26 06:59:30 +0000
commitdee900ae599bf88d89b2e30fb7c94f18e877c1b4 (patch)
tree6cd0e6d5303b18809910dfdeb3c9145eeb913b9a
parent2aa0bdeb257bff9f6e564d6400294f8441404e18 (diff)
downloadbcm5719-llvm-dee900ae599bf88d89b2e30fb7c94f18e877c1b4.tar.gz
bcm5719-llvm-dee900ae599bf88d89b2e30fb7c94f18e877c1b4.zip
[LLD][ELF] - Do not remove empty sections referenced in LOADADDR/ADDR commands.
This is https://bugs.llvm.org//show_bug.cgi?id=38750. If script references empty sections in LOADADDR/ADDR commands .empty : { *(.empty ) } .text : AT(LOADADDR (.empty) + SIZEOF (.empty)) { *(.text) } then an empty section will be removed and LOADADDR/ADDR will evaluate to null. It is not that user may expect from using of the generic script, what is a common case. Differential revision: https://reviews.llvm.org/D54621 llvm-svn: 359279
-rw-r--r--lld/ELF/LinkerScript.cpp12
-rw-r--r--lld/ELF/OutputSections.h1
-rw-r--r--lld/ELF/ScriptParser.cpp2
-rw-r--r--lld/test/ELF/linkerscript/empty-sections-expressions.test24
4 files changed, 36 insertions, 3 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index da6dc824a69..dd72233185e 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -825,12 +825,18 @@ static bool isDiscardable(OutputSection &Sec) {
if (!Sec.Phdrs.empty())
return false;
- // 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.
+ // We do not want to remove OutputSections with expressions that reference
+ // symbols even if the OutputSection is empty. We want to ensure that the
+ // expressions can be evaluated and report an error if they cannot.
if (Sec.ExpressionsUseSymbols)
return false;
+ // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
+ // as an empty Section can has a valid VMA and LMA we keep the OutputSection
+ // to maintain the integrity of the other Expression.
+ if (Sec.UsedInExpression)
+ return false;
+
for (BaseCommand *Base : Sec.SectionCommands) {
if (auto Cmd = dyn_cast<SymbolAssignment>(Base))
// Don't create empty output sections just for unreferenced PROVIDE
diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h
index d20d820cb99..c072f2c31b0 100644
--- a/lld/ELF/OutputSections.h
+++ b/lld/ELF/OutputSections.h
@@ -90,6 +90,7 @@ public:
bool NonAlloc = false;
bool Noload = false;
bool ExpressionsUseSymbols = false;
+ bool UsedInExpression = false;
bool InOverlay = false;
void finalize();
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 783d20c2025..741b32ff031 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1154,6 +1154,7 @@ Expr ScriptParser::readPrimary() {
if (Tok == "ADDR") {
StringRef Name = readParenLiteral();
OutputSection *Sec = Script->getOrCreateOutputSection(Name);
+ Sec->UsedInExpression = true;
return [=]() -> ExprValue {
checkIfExists(Sec, Location);
return {Sec, false, 0, Location};
@@ -1230,6 +1231,7 @@ Expr ScriptParser::readPrimary() {
if (Tok == "LOADADDR") {
StringRef Name = readParenLiteral();
OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
+ Cmd->UsedInExpression = true;
return [=] {
checkIfExists(Cmd, Location);
return Cmd->getLMA();
diff --git a/lld/test/ELF/linkerscript/empty-sections-expressions.test b/lld/test/ELF/linkerscript/empty-sections-expressions.test
new file mode 100644
index 00000000000..3f1da48f325
--- /dev/null
+++ b/lld/test/ELF/linkerscript/empty-sections-expressions.test
@@ -0,0 +1,24 @@
+# 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
+
+## Check we do not remove the empty output sections used in LOADADDR/ADDR
+## expressions and hence can evaluate the correct addresses.
+
+# CHECK: Program Headers:
+# CHECK-NEXT: Type Offset VirtAddr PhysAddr
+# CHECK-NEXT: LOAD 0x001000 0x0000000000080000 0x0000000000080000
+# CHECK-NEXT: LOAD 0x001001 0x0000000000080001 0x0000000000082000
+
+# CHECK: Section to Segment mapping:
+# CHECK: 00 .empty .text
+# CHECK-NEXT: 01 .data
+
+SECTIONS {
+ . = 0x00080000;
+ .empty : { *(.empty ) }
+ .text : AT(LOADADDR(.empty) + SIZEOF(.empty)) { *(.text) }
+ .data : AT(ADDR(.empty) + 0x2000) { *(.data) }
+}
OpenPOWER on IntegriCloud