[lld] r198337 - Add default search path if -nostdlib is not provided.

Joerg Sonnenberger joerg at bec.de
Thu Jan 2 11:18:36 PST 2014


Author: joerg
Date: Thu Jan  2 13:18:36 2014
New Revision: 198337

URL: http://llvm.org/viewvc/llvm-project?rev=198337&view=rev
Log:
Add default search path if -nostdlib is not provided.
Add basic emulation mapping for NetBSD/amd64, so that clang -m32 works
as expected.

Added:
    lld/trunk/test/Driver/Inputs/
    lld/trunk/test/Driver/Inputs/usr/
    lld/trunk/test/Driver/Inputs/usr/lib/
    lld/trunk/test/Driver/Inputs/usr/lib/i386/
    lld/trunk/test/Driver/Inputs/usr/lib/i386/libtest.a
    lld/trunk/test/Driver/Inputs/usr/lib/libtest.a
Modified:
    lld/trunk/include/lld/Driver/Driver.h
    lld/trunk/lib/Driver/GnuLdDriver.cpp
    lld/trunk/lib/Driver/GnuLdOptions.td
    lld/trunk/test/Driver/lib-search.test

Modified: lld/trunk/include/lld/Driver/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Driver/Driver.h?rev=198337&r1=198336&r2=198337&view=diff
==============================================================================
--- lld/trunk/include/lld/Driver/Driver.h (original)
+++ lld/trunk/include/lld/Driver/Driver.h Thu Jan  2 13:18:36 2014
@@ -73,6 +73,12 @@ public:
 
 private:
   static llvm::Triple getDefaultTarget(const char *progName);
+  static bool applyEmulation(llvm::Triple &triple,
+                             llvm::opt::InputArgList &args,
+                             raw_ostream &diagnostics);
+  static void addPlatformSearchDirs(ELFLinkingContext &ctx,
+                                    llvm::Triple &triple,
+                                    llvm::Triple &baseTriple);
 
   GnuLdDriver() LLVM_DELETED_FUNCTION;
 };

Modified: lld/trunk/lib/Driver/GnuLdDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/GnuLdDriver.cpp?rev=198337&r1=198336&r2=198337&view=diff
==============================================================================
--- lld/trunk/lib/Driver/GnuLdDriver.cpp (original)
+++ lld/trunk/lib/Driver/GnuLdDriver.cpp Thu Jan  2 13:18:36 2014
@@ -123,6 +123,62 @@ bool GnuLdDriver::linkELF(int argc, cons
   return link(*options, diagnostics);
 }
 
+bool GnuLdDriver::applyEmulation(llvm::Triple &triple,
+                                 llvm::opt::InputArgList &args,
+                                 raw_ostream &diagnostics) {
+  llvm::opt::Arg *arg = args.getLastArg(OPT_m);
+  if (!arg)
+    return true;
+  std::string value(arg->getValue());
+
+  switch (triple.getOS()) {
+  case llvm::Triple::NetBSD:
+    switch (triple.getArch()) {
+    case llvm::Triple::x86:
+    case llvm::Triple::x86_64:
+      if (value == "elf_i386") {
+        triple.setArch(llvm::Triple::x86);
+        return true;
+      }
+      if (value == "elf_x86_64") {
+        triple.setArch(llvm::Triple::x86_64);
+        return true;
+      }
+      break;
+    default:
+      break;
+    }
+    break;
+  default:
+    break;
+  }
+
+  diagnostics << "error: unsupported emulation '" << value << "'.\n";
+  return false;
+}
+
+void GnuLdDriver::addPlatformSearchDirs(ELFLinkingContext &ctx,
+                                       llvm::Triple &triple,
+                                       llvm::Triple &baseTriple) {
+  switch (triple.getOS()) {
+  case llvm::Triple::NetBSD:
+    switch (triple.getArch()) {
+    case llvm::Triple::x86:
+      if (baseTriple.getArch() == llvm::Triple::x86_64) {
+        ctx.addSearchPath("=/usr/lib/i386");
+        return;
+      }
+      break;
+    default:
+      break;
+    }
+    break;
+  default:
+    break;
+  }
+  ctx.addSearchPath("=/usr/lib");
+}
+
 bool GnuLdDriver::parse(int argc, const char *argv[],
                         std::unique_ptr<ELFLinkingContext> &context,
                         raw_ostream &diagnostics) {
@@ -148,11 +204,16 @@ bool GnuLdDriver::parse(int argc, const
   }
 
   // Use -target or use default target triple to instantiate LinkingContext
-  llvm::Triple triple;
+  llvm::Triple baseTriple;
   if (llvm::opt::Arg *trip = parsedArgs->getLastArg(OPT_target))
-    triple = llvm::Triple(trip->getValue());
+    baseTriple = llvm::Triple(trip->getValue());
   else
-    triple = getDefaultTarget(argv[0]);
+    baseTriple = getDefaultTarget(argv[0]);
+  llvm::Triple triple(baseTriple);
+
+  if (!applyEmulation(triple, *parsedArgs, diagnostics))
+    return false;
+
   std::unique_ptr<ELFLinkingContext> ctx(ELFLinkingContext::create(triple));
 
   if (!ctx) {
@@ -187,6 +248,9 @@ bool GnuLdDriver::parse(int argc, const
        it != ie; ++it)
     ctx->addSearchPath((*it)->getValue());
 
+  if (!parsedArgs->hasArg(OPT_nostdlib))
+    addPlatformSearchDirs(*ctx, triple, baseTriple);
+
   // Figure out output kind ( -r, -static, -shared)
   if ( llvm::opt::Arg *kind = parsedArgs->getLastArg(OPT_relocatable, OPT_static,
                                       OPT_shared, OPT_nmagic,

Modified: lld/trunk/lib/Driver/GnuLdOptions.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/GnuLdOptions.td?rev=198337&r1=198336&r2=198337&view=diff
==============================================================================
--- lld/trunk/lib/Driver/GnuLdOptions.td (original)
+++ lld/trunk/lib/Driver/GnuLdOptions.td Thu Jan  2 13:18:36 2014
@@ -54,8 +54,8 @@ def target : Separate<["-"], "target">,
 def output : Separate<["-"], "o">, MetaVarName<"<path>">,
      HelpText<"Path to file to write output">,
      Group<grp_general>;
-def m : Separate<["-"], "m">,
-     HelpText<"Emulate the emulation linker">,
+def m : Separate<["-"], "m">, MetaVarName<"<emulation>">,
+     HelpText<"Select target emulation">,
      Group<grp_general>;
 def build_id : Flag<["--"], "build-id">,
      HelpText<"Request creation of \".note.gnu.build-id\" ELF note section">,
@@ -95,6 +95,9 @@ def whole_archive: Flag<["--"], "whole-a
 def no_whole_archive: Flag<["--"], "no-whole-archive">,
      HelpText<"Restores the default behavior of loading archive members">,
      Group<grp_main>;
+def nostdlib : Flag<["-"], "nostdlib">,
+     HelpText<"Disable default search path for libraries">,
+     Group<grp_main>;
 
 //===----------------------------------------------------------------------===//
 /// Static Executable Options

Added: lld/trunk/test/Driver/Inputs/usr/lib/i386/libtest.a
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/Driver/Inputs/usr/lib/i386/libtest.a?rev=198337&view=auto
==============================================================================
--- lld/trunk/test/Driver/Inputs/usr/lib/i386/libtest.a (added)
+++ lld/trunk/test/Driver/Inputs/usr/lib/i386/libtest.a Thu Jan  2 13:18:36 2014
@@ -0,0 +1 @@
+!<arch>

Added: lld/trunk/test/Driver/Inputs/usr/lib/libtest.a
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/Driver/Inputs/usr/lib/libtest.a?rev=198337&view=auto
==============================================================================
--- lld/trunk/test/Driver/Inputs/usr/lib/libtest.a (added)
+++ lld/trunk/test/Driver/Inputs/usr/lib/libtest.a Thu Jan  2 13:18:36 2014
@@ -0,0 +1 @@
+!<arch>

Modified: lld/trunk/test/Driver/lib-search.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/Driver/lib-search.test?rev=198337&r1=198336&r2=198337&view=diff
==============================================================================
--- lld/trunk/test/Driver/lib-search.test (original)
+++ lld/trunk/test/Driver/lib-search.test Thu Jan  2 13:18:36 2014
@@ -1,6 +1,24 @@
 RUN: not lld -flavor gnu -t -lfnarchive -L%p/../elf/Inputs 2> %t.err
 RUN: FileCheck %s < %t.err
 
+RUN: not lld -flavor gnu -target x86_64--netbsd -t -ltest \
+RUN:    --sysroot=%p/Inputs 2> %t2
+RUN: FileCheck -check-prefix=NETBSD-AMD64 %s < %t2
+RUN: not lld -flavor gnu -target x86_64--netbsd -nostdlib -t -ltest \
+RUN:    --sysroot=%p/Inputs 2> %t3
+RUN: FileCheck -check-prefix=NETBSD-AMD64-NS %s < %t3
+RUN: not lld -flavor gnu -target i386--netbsd -t -ltest \
+RUN:    --sysroot=%p/Inputs 2> %t4
+RUN: FileCheck -check-prefix=NETBSD-I386 %s < %t4
+RUN: not lld -flavor gnu -target x86_64--netbsd -m elf_i386 -t -ltest \
+RUN:    --sysroot=%p/Inputs 2> %t5
+RUN: FileCheck -check-prefix=NETBSD-AMD64_32 %s < %t5
+
 # run linker with -t mode to dump full paths to input files
 
-CHECK: {{[^ ]+}}elf/Inputs{{[\\/]}}libfnarchive.a
+CHECK: {{[^ ]+}}elf{{[\\/]}}Inputs{{[\\/]}}libfnarchive.a
+
+NETBSD-AMD64: {{[^ ]+}}{{[\\/]}}Inputs{{[\\/]}}usr{{[\\/]}}lib{{[\\/]}}libtest.a
+NETBSD-AMD64-NS-NOT: {{[^ ]+}}{{[\\/]}}Inputs{{[\\/]}}usr{{[\\/]}}lib{{[\\/]}}libtest.a
+NETBSD-I386: {{[^ ]+}}{{[\\/]}}Inputs{{[\\/]}}usr{{[\\/]}}lib{{[\\/]}}libtest.a
+NETBSD-AMD64_32: {{[^ ]+}}{{[\\/]}}Inputs{{[\\/]}}usr{{[\\/]}}lib{{[\\/]}}i386{{[\\/]}}libtest.a





More information about the llvm-commits mailing list