[PATCH] D14916: ELF2: LinkerScript: lookup absolute paths under sysroot

Simon Atanasyan via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 23 03:00:08 PST 2015


atanasyan created this revision.
atanasyan added a reviewer: ruiu.
atanasyan added a subscriber: llvm-commits.
atanasyan set the repository for this revision to rL LLVM.
atanasyan added a project: lld.

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.

https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html


Repository:
  rL LLVM

http://reviews.llvm.org/D14916

Files:
  ELF/Driver.cpp
  ELF/Driver.h
  ELF/LinkerScript.cpp
  test/ELF/linkerscript.s

Index: test/ELF/linkerscript.s
===================================================================
--- test/ELF/linkerscript.s
+++ test/ELF/linkerscript.s
@@ -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
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -28,8 +28,8 @@
 namespace {
 class LinkerScript {
 public:
-  LinkerScript(BumpPtrAllocator *A, StringRef S)
-      : Saver(*A), Tokens(tokenize(S)) {}
+  LinkerScript(BumpPtrAllocator *A, StringRef S, bool IsUnderSysroot)
+      : Saver(*A), Tokens(tokenize(S)), IsUnderSysroot(IsUnderSysroot) {}
   void run();
 
 private:
@@ -58,6 +58,7 @@
   StringSaver Saver;
   std::vector<StringRef> Tokens;
   size_t Pos = 0;
+  bool IsUnderSysroot;
 };
 }
 
@@ -161,6 +162,11 @@
 
 void LinkerScript::addFile(StringRef S) {
   if (sys::path::is_absolute(S)) {
+    if (IsUnderSysroot) {
+      SmallString<128> SysrootedPath;
+      if (sys::fs::exists((Config->Sysroot + S).toStringRef(SysrootedPath)))
+        S = Saver.save(SysrootedPath.str());
+    }
     Driver->addFile(S);
   } else if (S.startswith("=")) {
     if (Config->Sysroot.empty())
@@ -291,6 +297,14 @@
 }
 
 // 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();
+void lld::elf2::readLinkerScript(BumpPtrAllocator *A, MemoryBufferRef MB,
+                                 StringRef Path) {
+  bool IsUnderSysroot = false;
+  if (!Config->Sysroot.empty())
+    for (; !Path.empty(); Path = sys::path::parent_path(Path))
+      if (sys::fs::equivalent(Config->Sysroot, Path)) {
+        IsUnderSysroot = true;
+        break;
+      }
+  LinkerScript(A, MB.getBuffer(), IsUnderSysroot).run();
 }
Index: ELF/Driver.h
===================================================================
--- ELF/Driver.h
+++ ELF/Driver.h
@@ -55,7 +55,8 @@
 };
 
 // Parses a linker script. Calling this function updates the Symtab and Config.
-void readLinkerScript(llvm::BumpPtrAllocator *A, MemoryBufferRef MB);
+void readLinkerScript(llvm::BumpPtrAllocator *A, MemoryBufferRef MB,
+                      StringRef Path);
 
 std::string findFromSearchPaths(StringRef Path);
 std::string searchLibrary(StringRef Path);
Index: ELF/Driver.cpp
===================================================================
--- ELF/Driver.cpp
+++ ELF/Driver.cpp
@@ -69,7 +69,7 @@
 
   switch (identify_magic(MBRef.getBuffer())) {
   case file_magic::unknown:
-    readLinkerScript(&Alloc, MBRef);
+    readLinkerScript(&Alloc, MBRef, Path);
     return;
   case file_magic::archive:
     if (WholeArchive) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14916.40909.patch
Type: text/x-patch
Size: 3195 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151123/dae4ad13/attachment.bin>


More information about the llvm-commits mailing list