LinkerScript: Add -T <scriptfile> option
authorMeador Inge <meadori@codesourcery.com>
Fri, 13 Mar 2015 18:15:01 +0000 (18:15 +0000)
committerMeador Inge <meadori@codesourcery.com>
Fri, 13 Mar 2015 18:15:01 +0000 (18:15 +0000)
GNU LD has an option named -T/--script which allows a user to specify
a linker script to be used [1].  LLD already accepts linker scripts
without this option, but the option is widely used.  Therefore it is
best to support it in LLD as well.

[1] https://sourceware.org/binutils/docs/ld/Options.html#Options

llvm-svn: 232183

lld/lib/Driver/GnuLdDriver.cpp
lld/lib/Driver/GnuLdOptions.td
lld/lib/Driver/TODO.rst
lld/lib/ReaderWriter/ELF/ELFLinkingContext.cpp
lld/test/elf/linkerscript/Inputs/valid.ls [new file with mode: 0644]
lld/test/elf/linkerscript/invalid-script-cli-1.test [new file with mode: 0644]
lld/test/elf/linkerscript/invalid-script-cli-2.test [new file with mode: 0644]
lld/test/elf/linkerscript/valid-script-cli.objtxt [new file with mode: 0644]

index c20e108..7a0e633 100644 (file)
@@ -659,7 +659,8 @@ bool GnuLdDriver::parse(int argc, const char *argv[],
     }
 
     case OPT_INPUT:
-    case OPT_l: {
+    case OPT_l:
+    case OPT_T: {
       bool dashL = (arg->getOption().getID() == OPT_l);
       StringRef path = arg->getValue();
 
index cf694f4..9d06f29 100644 (file)
@@ -253,6 +253,16 @@ defm wrap : smDash<"wrap", "wrap",
             " will be resolved to symbol.">,
             MetaVarName<"<symbol>">,
             Group<grp_symbolopts>;
+
+//===----------------------------------------------------------------------===//
+/// Script Options
+//===----------------------------------------------------------------------===//
+def grp_scriptopts : OptionGroup<"opts">,
+     HelpText<"SCRIPT OPTIONS">;
+defm T : smDash<"T", "script",
+      "Use the given linker script in place of the default script.">,
+      Group<grp_scriptopts>;
+
 //===----------------------------------------------------------------------===//
 /// Optimization Options
 //===----------------------------------------------------------------------===//
index b9752e6..e03d829 100644 (file)
@@ -31,7 +31,6 @@ Missing Options
 * -s,--strip-all
 * -S,--strip-debug
 * --trace
-* -T,--script
 * -dT,--default-script
 * -Ur
 * --unique
index ae995c9..721ffde 100644 (file)
@@ -145,10 +145,10 @@ ErrorOr<StringRef> ELFLinkingContext::searchLibrary(StringRef libName) const {
     if (llvm::sys::fs::exists(path.str()))
       return StringRef(*new (_allocator) std::string(path.str()));
   }
-  if (!llvm::sys::fs::exists(libName))
-    return make_error_code(llvm::errc::no_such_file_or_directory);
+  if (hasColonPrefix && llvm::sys::fs::exists(libName.drop_front()))
+      return libName.drop_front();
 
-  return libName;
+  return make_error_code(llvm::errc::no_such_file_or_directory);
 }
 
 ErrorOr<StringRef> ELFLinkingContext::searchFile(StringRef fileName,
diff --git a/lld/test/elf/linkerscript/Inputs/valid.ls b/lld/test/elf/linkerscript/Inputs/valid.ls
new file mode 100644 (file)
index 0000000..4359360
--- /dev/null
@@ -0,0 +1,6 @@
+/* A simple valid linker script used for testing the -T/--script options.
+ *
+ * An unresolved symbol named '_entry_point' can be scanned for by the tests
+ * to determine that the linker script was processed.
+ */
+ENTRY(_entry_point)
diff --git a/lld/test/elf/linkerscript/invalid-script-cli-1.test b/lld/test/elf/linkerscript/invalid-script-cli-1.test
new file mode 100644 (file)
index 0000000..904ba17
--- /dev/null
@@ -0,0 +1,10 @@
+# Check that the -T/--script options issue an error when passed
+# filenames for files that do not exist.
+
+RUN: not lld -flavor gnu -target x86_64 -T idonotexist.ls 2> %t.err
+RUN: FileCheck %s < %t.err
+
+RUN: not lld -flavor gnu -target x86_64 --script=idonotexist.ls 2> %t.err
+RUN: FileCheck %s < %t.err
+
+CHECK: {{.*}}lld: cannot find file {{.*}}idonotexist.ls
diff --git a/lld/test/elf/linkerscript/invalid-script-cli-2.test b/lld/test/elf/linkerscript/invalid-script-cli-2.test
new file mode 100644 (file)
index 0000000..6e0e42a
--- /dev/null
@@ -0,0 +1,6 @@
+# Check that linker script are *not* picked up with -lscript.ls.
+
+RUN: not lld -flavor gnu -target x86_64 -L%p/Inputs/ -lvalid.ls 2> %t.err
+RUN: FileCheck %s < %t.err
+
+CHECK: {{.*}}: Unable to find library -lvalid.ls
diff --git a/lld/test/elf/linkerscript/valid-script-cli.objtxt b/lld/test/elf/linkerscript/valid-script-cli.objtxt
new file mode 100644 (file)
index 0000000..b68d430
--- /dev/null
@@ -0,0 +1,23 @@
+# Check that the linker script inputs are accepted properly.
+
+# RUN: lld -flavor gnu -target x86_64 %p/Inputs/valid.ls -r %s \
+# RUN:     --output-filetype=yaml | FileCheck %s
+
+# RUN: lld -flavor gnu -target x86_64 -T %p/Inputs/valid.ls -r %s \
+# RUN:     --output-filetype=yaml | FileCheck %s
+
+# RUN: lld -flavor gnu -target x86_64 --script=%p/Inputs/valid.ls -r %s \
+# RUN:     --output-filetype=yaml | FileCheck %s
+
+# RUN: lld -flavor gnu -target x86_64 -L%p/Inputs/ -l:valid.ls -r %s \
+# RUN:     --output-filetype=yaml | FileCheck %s
+
+defined-atoms:
+  - name:            main
+    scope:           global
+    content:         [ B8, 00, 00, 00, 00, C7, 44, 24, FC, 00, 00, 00, 00, C3 ]
+    alignment:       2^4
+    section-choice:  custom-required
+    section-name:    .text
+
+# CHECK: _entry_point