[lld] r248715 - [ELF2] Add --sysroot command line switch

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 28 08:02:01 PDT 2015


Author: ikudrin
Date: Mon Sep 28 10:01:59 2015
New Revision: 248715

URL: http://llvm.org/viewvc/llvm-project?rev=248715&view=rev
Log:
[ELF2] Add --sysroot command line switch

Reviewers: rafael, ruiu

Subscribers: llvm-commits

Projects: #lld

Differential Revision: http://reviews.llvm.org/D13209

Added:
    lld/trunk/test/elf2/sysroot.s
Modified:
    lld/trunk/ELF/Config.h
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/Options.td

Modified: lld/trunk/ELF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=248715&r1=248714&r2=248715&view=diff
==============================================================================
--- lld/trunk/ELF/Config.h (original)
+++ lld/trunk/ELF/Config.h Mon Sep 28 10:01:59 2015
@@ -22,6 +22,7 @@ struct Configuration {
   llvm::StringRef DynamicLinker;
   std::string RPath;
   std::vector<llvm::StringRef> InputSearchPaths;
+  llvm::StringRef Sysroot;
   bool Shared = false;
   bool DiscardAll = false;
   bool DiscardLocals = false;

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=248715&r1=248714&r2=248715&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Mon Sep 28 10:01:59 2015
@@ -60,6 +60,21 @@ static std::unique_ptr<InputFile> create
   return createELFFile<ObjectFile>(MB);
 }
 
+// Makes a path by concatenating Dir and File.
+// If Dir starts with "=" the result will be preceded by SysRoot,
+// which can be set with --sysroot command line switch.
+static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
+  SmallString<128> Path;
+  if (Dir.startswith("=")) {
+    Path.assign(Config->Sysroot);
+    sys::path::append(Path, Dir.substr(1), File);
+  } else {
+    Path.assign(Dir);
+    sys::path::append(Path, File);
+  }
+  return Path.str().str();
+}
+
 // Searches a given library from input search paths, which are filled
 // from -L command line switches. Returns a path to an existent library file.
 static std::string searchLibrary(StringRef Path) {
@@ -70,13 +85,11 @@ static std::string searchLibrary(StringR
     Names.push_back((Twine("lib") + Path + ".so").str());
     Names.push_back((Twine("lib") + Path + ".a").str());
   }
-  SmallString<128> FullPath;
   for (StringRef Dir : Config->InputSearchPaths) {
     for (const std::string &Name : Names) {
-      FullPath = Dir;
-      sys::path::append(FullPath, Name);
-      if (sys::fs::exists(FullPath.str()))
-        return FullPath.str().str();
+      std::string FullPath = buildSysrootedPath(Dir, Name);
+      if (sys::fs::exists(FullPath))
+        return FullPath;
     }
   }
   error(Twine("Unable to find library -l") + Path);
@@ -96,6 +109,9 @@ void LinkerDriver::link(ArrayRef<const c
   if (auto *Arg = Args.getLastArg(OPT_dynamic_linker))
     Config->DynamicLinker = Arg->getValue();
 
+  if (auto *Arg = Args.getLastArg(OPT_sysroot))
+    Config->Sysroot = Arg->getValue();
+
   std::vector<StringRef> RPaths;
   for (auto *Arg : Args.filtered(OPT_rpath))
     RPaths.push_back(Arg->getValue());

Modified: lld/trunk/ELF/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Options.td?rev=248715&r1=248714&r2=248715&view=diff
==============================================================================
--- lld/trunk/ELF/Options.td (original)
+++ lld/trunk/ELF/Options.td Mon Sep 28 10:01:59 2015
@@ -49,3 +49,6 @@ def l : Joined<["-"], "l">, MetaVarName<
 
 def alias_l : Joined<["--"], "library=">,
      Alias<l>;
+
+def sysroot : Joined<["--"], "sysroot=">,
+     HelpText<"Set the system root">;

Added: lld/trunk/test/elf2/sysroot.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/sysroot.s?rev=248715&view=auto
==============================================================================
--- lld/trunk/test/elf2/sysroot.s (added)
+++ lld/trunk/test/elf2/sysroot.s Mon Sep 28 10:01:59 2015
@@ -0,0 +1,36 @@
+// RUN: mkdir -p %t/lib
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t/m.o
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
+// RUN:     %p/Inputs/libsearch-st.s -o %t/st.o
+// RUN: rm -f %t/lib/libls.a
+// RUN: llvm-ar rcs %t/lib/libls.a %t/st.o
+// REQUIRES: x86
+
+// Should not link because of undefined symbol _bar
+// RUN: not lld -flavor gnu2 -o %t/r %t/m.o 2>&1 \
+// RUN:     | FileCheck --check-prefix=UNDEFINED %s
+// UNDEFINED: undefined symbol: _bar
+
+// We need to be sure that there is no suitable library in the /lib directory
+// RUN: not lld -flavor gnu2 -o %t/r %t/m.o -L/lib -l:libls.a 2>&1 \
+// RUN:     | FileCheck --check-prefix=NOLIB %s
+// NOLIB: Unable to find library -l:libls.a
+
+// Should just remove the '=' symbol if --sysroot is not specified.
+// Case 1: relative path
+// RUN: cd %t && lld -flavor gnu2 -o %t/r %t/m.o -L=lib -l:libls.a
+// Case 2: absolute path
+// RUN: cd %p && lld -flavor gnu2 -o %t/r %t/m.o -L=%t/lib -l:libls.a
+
+// RUN: cd %p
+
+// Should substitute SysRoot if specified
+// RUN: lld -flavor gnu2 -o %t/r %t/m.o --sysroot=%t -L=lib -l:libls.a
+// RUN: lld -flavor gnu2 -o %t/r %t/m.o --sysroot=%t -L=/lib -l:libls.a
+
+// Should not substitute SysRoot if the directory name does not start with '='
+// RUN: not lld -flavor gnu2 -o %t/r %r/m.o --sysroot=%t -Llib -l:libls.a
+// RUN: not lld -flavor gnu2 -o %t/r %r/m.o --sysroot=%t -L/lib -l:libls.a
+
+.globl _start,_bar;
+_start:




More information about the llvm-commits mailing list