[PATCH] D48771: [ELF] Don't create empty output section for unreferenced PROVIDE symbol

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 29 06:49:12 PDT 2018


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

LLD removes empty output sections otherwise specified in the linker script. Prior to this change however, if section descriptions included ANY kind of symbol assignment, then the consequent output section would not be removed, even if the assignment was marked with PROVIDE and not actually triggered (i.e. the symbol was never referenced). This change modifies the isDiscarable function to ignore such directives when determining whether a section should be discarded, in keeping with bfd's behaviour. Symbol assignments that do result in a symbol definition will continue to result in a kept section (this is not actually the same as bfd's behaviour, but it is simpler, and probably makes more sense).


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D48771

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/Inputs/provide-empty-section.s
  test/ELF/linkerscript/provide-empty-section.s


Index: test/ELF/linkerscript/provide-empty-section.s
===================================================================
--- test/ELF/linkerscript/provide-empty-section.s
+++ test/ELF/linkerscript/provide-empty-section.s
@@ -0,0 +1,34 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %tundefined.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %tdefined.o -defsym="foo=42"
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/provide-empty-section.s -o %treference.o
+
+# RUN: echo "SECTIONS { " > %t.script
+# RUN: echo "  .bar : {" >> %t.script
+# RUN: echo "     PROVIDE(foo = .);" >> %t.script
+# RUN: echo "  }" >> %t.script
+# RUN: echo "}" >> %t.script
+
+# Case 1: Provided symbol is undefined and not referenced - empty section should be removed.
+# RUN: ld.lld %tundefined.o -T %t.script -o %t1.elf
+# RUN: llvm-readobj -sections %t1.elf | FileCheck %s --check-prefix=NOSECTION
+
+# Case 2: Provided symbol is undefined and referenced - empty section should not be removed.
+# RUN: ld.lld %tundefined.o %treference.o -T %t.script -o %t2.elf
+# RUN: llvm-readobj -sections %t2.elf | FileCheck %s --check-prefix=SECTION
+
+# Case 3: Provided symbol is defined and not referenced - empty section should be removed.
+# RUN: ld.lld %tdefined.o -T %t.script -o %t3.elf
+# RUN: llvm-readobj -sections %t3.elf | FileCheck %s --check-prefix=NOSECTION
+
+# Case 4: Provided symbol is defined and referenced - empty section should not be removed.
+# RUN: ld.lld %tdefined.o %treference.o -T %t.script -o %t4.elf
+# RUN: llvm-readobj -sections %t4.elf | FileCheck %s --check-prefix=SECTION
+
+.global _start
+_start:
+    ret
+
+# SECTION: .bar
+# NOSECTION-NOT: .bar
Index: test/ELF/linkerscript/Inputs/provide-empty-section.s
===================================================================
--- test/ELF/linkerscript/Inputs/provide-empty-section.s
+++ test/ELF/linkerscript/Inputs/provide-empty-section.s
@@ -0,0 +1,2 @@
+baz:
+    call foo
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -834,9 +834,16 @@
   if (Sec.ExpressionsUseSymbols)
     return false;
 
-  for (BaseCommand *Base : Sec.SectionCommands)
+  for (BaseCommand *Base : Sec.SectionCommands) {
+    if (auto Cmd = dyn_cast<SymbolAssignment>(Base))
+      // Don't create empty output sections just for unreferenced PROVIDE
+      // symbols.
+      if (Cmd->Provide)
+        continue;
+
     if (!isa<InputSectionDescription>(*Base))
       return false;
+  }
   return true;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48771.153486.patch
Type: text/x-patch
Size: 2627 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180629/fd932e23/attachment.bin>


More information about the llvm-commits mailing list