[PATCH] D43069: [ELF] - Report error if removed empty output section used undefined symbols.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 8 07:22:54 PST 2018


grimar updated this revision to Diff 133420.
grimar edited the summary of this revision.
grimar added a comment.

- Simplified implementation, added missing testcases.


https://reviews.llvm.org/D43069

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/address-expr-symbols.s


Index: test/ELF/linkerscript/address-expr-symbols.s
===================================================================
--- test/ELF/linkerscript/address-expr-symbols.s
+++ test/ELF/linkerscript/address-expr-symbols.s
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: echo "SECTIONS { .bar (a+b) : AT(d) ALIGN(c) { } };" > %t.script
+# RUN: not ld.lld -o %t --script %t.script %t.o 2>&1 | FileCheck %s
+
+# CHECK-DAG: symbol not found: a
+# CHECK-DAG: symbol not found: b
+# CHECK-DAG: symbol not found: c
+# CHECK-DAG: symbol not found: d
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: echo "SECTIONS { .bar (a+b) : SUBALIGN(c) { } };" > %t.script
+# RUN: not ld.lld -o %t --script %t.script %t.o 2>&1 \
+# RUN:   | FileCheck %s --check-prefix=SUBALIGN
+# SUBALIGN: symbol not found: c
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -757,11 +757,21 @@
   // clutter the output.
   // We instead remove trivially empty sections. The bfd linker seems even
   // more aggressive at removing them.
-  llvm::erase_if(SectionCommands, [&](BaseCommand *Base) {
-    if (auto *Sec = dyn_cast<OutputSection>(Base))
-      return !Sec->Live;
-    return false;
-  });
+  for (BaseCommand *& Cmd : SectionCommands) {
+    auto *Sec = dyn_cast<OutputSection>(Cmd);
+    if (!Sec || Sec->Live)
+      continue;
+
+    // If section had address or align expressions we want to check them were
+    // calculatable before we remove the section. Otherwise if any expression
+    // contains undefined symbol, we would not report the proper error.
+    for (const Expr &Expression : {Sec->AddrExpr, Sec->AlignExpr, Sec->LMAExpr})
+      if (Expression)
+        Expression();
+    Cmd = nullptr;
+  }
+
+  llvm::erase_if(SectionCommands, [&](BaseCommand *Base) { return !Base; });
 }
 
 static bool isAllSectionDescription(const OutputSection &Cmd) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43069.133420.patch
Type: text/x-patch
Size: 2032 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180208/07fcb4f0/attachment.bin>


More information about the llvm-commits mailing list