[LLD] Add support for --unique option
authorDavid Bozier <daveb@graphcore.ai>
Mon, 9 Mar 2020 15:43:20 +0000 (15:43 +0000)
committerDavid Bozier <daveb@graphcore.ai>
Tue, 10 Mar 2020 12:20:21 +0000 (12:20 +0000)
Summary:
Places orphan sections into a unique output section. This prevents the merging of orphan sections of the same name.
Matches behaviour of GNU ld --unique. --unique=pattern is not implemented.

Motivated user case shown in the test has 2 local symbols as they would appear if C++ source has been compiled with -ffunction-sections. The merging of these sections in the case of a partial link (-r) may limit the effectiveness of -gc-sections of a subsequent link.

Reviewers: espindola, jhenderson, bd1976llvm, edd, andrewng, JonChesterfield, MaskRay, grimar, ruiu, psmith

Reviewed By: MaskRay, grimar

Subscribers: emaste, arichardson, MaskRay, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D75536

lld/ELF/Config.h
lld/ELF/Driver.cpp
lld/ELF/LinkerScript.cpp
lld/ELF/Options.td
lld/docs/ld.lld.1
lld/test/ELF/unique-orphans.s [new file with mode: 0644]

index 81c42b6..05f44a5 100644 (file)
@@ -194,6 +194,7 @@ struct Configuration {
   bool timeTraceEnabled;
   bool tocOptimize;
   bool undefinedVersion;
+  bool unique;
   bool useAndroidRelrTags = false;
   bool warnBackrefs;
   bool warnCommon;
index 8d91930..f9a1340 100644 (file)
@@ -986,6 +986,7 @@ static void readConfigs(opt::InputArgList &args) {
   config->undefined = args::getStrings(args, OPT_undefined);
   config->undefinedVersion =
       args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, true);
+  config->unique = args.hasArg(OPT_unique);
   config->useAndroidRelrTags = args.hasFlag(
       OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false);
   config->unresolvedSymbols = getUnresolvedSymbolPolicy(args);
index 0aca7c1..2c71280 100644 (file)
@@ -685,7 +685,9 @@ void LinkerScript::addOrphanSections() {
       orphanSections.push_back(s);
 
       StringRef name = getOutputSectionName(s);
-      if (OutputSection *sec = findByName(sectionCommands, name)) {
+      if (config->unique) {
+        v.push_back(createSection(s, name));
+      } else if (OutputSection *sec = findByName(sectionCommands, name)) {
         sec->recordSection(s);
       } else {
         if (OutputSection *os = addInputSec(map, s, name))
index 307c781..e1c489c 100644 (file)
@@ -374,6 +374,8 @@ defm undefined: Eq<"undefined", "Force undefined symbol during linking">,
 defm undefined_glob: Eq<"undefined-glob", "Force undefined symbol during linking">,
   MetaVarName<"<pattern>">;
 
+def unique: F<"unique">, HelpText<"Creates a separate output section for every orphan input section">;
+
 defm unresolved_symbols:
   Eq<"unresolved-symbols", "Determine how to handle unresolved symbols">;
 
index 058c8dd..238ac28 100644 (file)
@@ -553,6 +553,8 @@ matches the characters within brackets.
 All symbols that match
 a given pattern are handled as if they were given as arguments of
 .Fl -undefined .
+.It Fl -unique
+Creates a separate output section for every orphan input section.
 .It Fl -unresolved-symbols Ns = Ns Ar value
 Determine how to handle unresolved symbols.
 .It Fl -use-android-relr-tags
diff --git a/lld/test/ELF/unique-orphans.s b/lld/test/ELF/unique-orphans.s
new file mode 100644 (file)
index 0000000..d4623a9
--- /dev/null
@@ -0,0 +1,26 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+.section .foo,"a",@progbits,unique,1
+.byte 1
+
+.section .foo,"a",@progbits,unique,2
+.byte 2
+
+.section .foo,"a",@progbits,unique,3
+.byte 3
+
+## We should have 3 instances of orphan section foo.
+## Test with -r
+# RUN: ld.lld %t.o -o %t.elf --unique 
+# RUN: llvm-readelf -S %t.elf | FileCheck %s
+
+# CHECK-COUNT-3: .foo
+# CHECK-NOT: .foo
+
+## Test that --unique does not affect sections specified in output section descriptions.
+# RUN: echo 'SECTIONS { .foo : { *(.foo) }}' > %t.script
+# RUN: ld.lld %t.o -o %t2.elf -T %t.script --unique 
+# RUN: llvm-readelf -S %t2.elf | FileCheck --check-prefix SCRIPT %s
+# SCRIPT: .foo
+# SCRIPT-NOT: .foo