[PATCH] D54621: [ELF] - Do not remove empty sections referenced in LOADADDR/ADDR commands.
George Rimar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 25 23:57:34 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rLLD359279: [LLD][ELF] - Do not remove empty sections referenced in LOADADDR/ADDR commands. (authored by grimar, committed by ).
Herald added a project: LLVM.
Repository:
rLLD LLVM Linker
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D54621/new/
https://reviews.llvm.org/D54621
Files:
ELF/LinkerScript.cpp
ELF/OutputSections.h
ELF/ScriptParser.cpp
test/ELF/linkerscript/empty-sections-expressions.test
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,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) }
+}
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -825,12 +825,18 @@
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
Index: ELF/ScriptParser.cpp
===================================================================
--- ELF/ScriptParser.cpp
+++ ELF/ScriptParser.cpp
@@ -1154,6 +1154,7 @@
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 @@
if (Tok == "LOADADDR") {
StringRef Name = readParenLiteral();
OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
+ Cmd->UsedInExpression = true;
return [=] {
checkIfExists(Cmd, Location);
return Cmd->getLMA();
Index: ELF/OutputSections.h
===================================================================
--- ELF/OutputSections.h
+++ ELF/OutputSections.h
@@ -90,6 +90,7 @@
bool NonAlloc = false;
bool Noload = false;
bool ExpressionsUseSymbols = false;
+ bool UsedInExpression = false;
bool InOverlay = false;
void finalize();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54621.196801.patch
Type: text/x-patch
Size: 3362 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190426/38b3a32d/attachment.bin>
More information about the llvm-commits
mailing list