[PATCH] D55118: [ELF] - Report an error if empty sections are referenced in LOADADDR/ADDR commands.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 30 04:08:22 PST 2018


grimar created this revision.
grimar added reviewers: ruiu, psmith.
Herald added subscribers: arichardson, emaste.
Herald added a reviewer: espindola.

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.

This is an alternative version of D54621 <https://reviews.llvm.org/D54621>, in this patch, I just report an error in this case,
so that we do not emit broken output silently.


https://reviews.llvm.org/D55118

Files:
  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,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 ileCheck %s ff
+
+# CHECK: error: {{.*}}.test:9: reference to removed section '.empty' not allowed
+
+SECTIONS {
+ . = 0x00080000;
+ .empty  : { *(.empty ) }
+ .text   : AT(LOADADDR (.empty)) { *(.text) }
+ .data   : AT(ADDR (.empty)) { *(.data) }
+}
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() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55118.176084.patch
Type: text/x-patch
Size: 1308 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181130/19125a5b/attachment.bin>


More information about the llvm-commits mailing list