[lld] r254135 - [ELF] Reapply r254031 - LinkerScript: lookup absolute paths under sysroot

Simon Atanasyan via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 25 21:53:00 PST 2015


Author: atanasyan
Date: Wed Nov 25 23:53:00 2015
New Revision: 254135

URL: http://llvm.org/viewvc/llvm-project?rev=254135&view=rev
Log:
[ELF] Reapply r254031 - LinkerScript: lookup absolute paths under sysroot

In case a sysroot prefix is configured, and the filename starts with
the '/' character, and the script being processed was located inside
the sysroot prefix, the file's name will be looked for in the sysroot
prefix. Otherwise, the linker falls to the common lookup scheme.

It is slightly modified version of the commit r254031. The problem of
the initial commit was in the `is_absolute` call. On Windows 'C:\' is
absolute path but we do not need to find it under sysroot. In this patch
linker looks up a path under sysroot only if the paths starts with '/'
character.

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/test/ELF/linkerscript.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=254135&r1=254134&r2=254135&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Wed Nov 25 23:53:00 2015
@@ -28,8 +28,8 @@ using namespace lld::elf2;
 namespace {
 class LinkerScript {
 public:
-  LinkerScript(BumpPtrAllocator *A, StringRef S)
-      : Saver(*A), Tokens(tokenize(S)) {}
+  LinkerScript(BumpPtrAllocator *A, StringRef S, bool B)
+      : Saver(*A), Tokens(tokenize(S)), IsUnderSysroot(B) {}
   void run();
 
 private:
@@ -58,6 +58,7 @@ private:
   StringSaver Saver;
   std::vector<StringRef> Tokens;
   size_t Pos = 0;
+  bool IsUnderSysroot;
 };
 }
 
@@ -160,6 +161,15 @@ void LinkerScript::expect(StringRef Expe
 }
 
 void LinkerScript::addFile(StringRef S) {
+  if (IsUnderSysroot && S.startswith("/")) {
+    SmallString<128> Path;
+    (Config->Sysroot + S).toStringRef(Path);
+    if (sys::fs::exists(Path)) {
+      Driver->addFile(Saver.save(Path.str()));
+      return;
+    }
+  }
+
   if (sys::path::is_absolute(S)) {
     Driver->addFile(S);
   } else if (S.startswith("=")) {
@@ -290,7 +300,17 @@ void LinkerScript::readOutputSectionDesc
   }
 }
 
+static bool isUnderSysroot(StringRef Path) {
+  if (Config->Sysroot == "")
+    return false;
+  for (; !Path.empty(); Path = sys::path::parent_path(Path))
+    if (sys::fs::equivalent(Config->Sysroot, Path))
+      return true;
+  return false;
+}
+
 // Entry point. The other functions or classes are private to this file.
 void lld::elf2::readLinkerScript(BumpPtrAllocator *A, MemoryBufferRef MB) {
-  LinkerScript(A, MB.getBuffer()).run();
+  StringRef Path = MB.getBufferIdentifier();
+  LinkerScript(A, MB.getBuffer(), isUnderSysroot(Path)).run();
 }

Modified: lld/trunk/test/ELF/linkerscript.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript.s?rev=254135&r1=254134&r2=254135&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript.s (original)
+++ lld/trunk/test/ELF/linkerscript.s Wed Nov 25 23:53:00 2015
@@ -38,6 +38,13 @@
 # RUN: ld.lld -o %t2 %t.script -L%t.dir
 # RUN: llvm-readobj %t2 > /dev/null
 
+# RUN: echo "GROUP(" %t /libxyz.a ")" > %t.script
+# RUN: echo "GROUP(" %t /libxyz.a ")" > %t.dir/xyz.script
+# RUN: not ld.lld -o %t2 %t.script
+# RUN: not ld.lld -o %t2 %t.script --sysroot=%t.dir
+# RUN: ld.lld -o %t2 %t.dir/xyz.script --sysroot=%t.dir
+# RUN: llvm-readobj %t2 > /dev/null
+
 # RUN: echo "GROUP(" %t.script2 ")" > %t.script1
 # RUN: echo "GROUP(" %t ")" > %t.script2
 # RUN: ld.lld -o %t2 %t.script1




More information about the llvm-commits mailing list