[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
Fri Nov 16 02:28:54 PST 2018


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

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.

There is a trivial fix for that, so I would do that.


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,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
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: 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 <class ELFT> void finalize();
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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54621.174344.patch
Type: text/x-patch
Size: 3009 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181116/a615bd18/attachment.bin>


More information about the llvm-commits mailing list