[Driver] Improve the `isPathUnderSysroot()` function. Now it returns a
authorSimon Atanasyan <simon@atanasyan.com>
Thu, 26 Jun 2014 10:48:52 +0000 (10:48 +0000)
committerSimon Atanasyan <simon@atanasyan.com>
Thu, 26 Jun 2014 10:48:52 +0000 (10:48 +0000)
correct result even if checking paths: a) symlinks and/or b) contains relative
parts like /dir1/dir2/../dir2.

llvm-svn: 211772

lld/lib/Driver/GnuLdInputGraph.cpp
lld/test/elf/group-cmd-search.test

index d463e5f..f634c2e 100644 (file)
@@ -70,15 +70,13 @@ std::error_code GNULdScript::parse(const LinkingContext &ctx,
 }
 
 static bool isPathUnderSysroot(StringRef sysroot, StringRef path) {
-  // TODO: Handle the case when 'sysroot' and/or 'path' are symlinks.
-  if (sysroot.empty() || sysroot.size() >= path.size())
-    return false;
-  if (llvm::sys::path::is_separator(sysroot.back()))
-    sysroot = sysroot.substr(0, sysroot.size() - 1);
-  if (!llvm::sys::path::is_separator(path[sysroot.size()]))
+  if (sysroot.empty())
     return false;
 
-  return llvm::sys::fs::equivalent(sysroot, path.substr(0, sysroot.size()));
+  while (!path.empty() && !llvm::sys::fs::equivalent(sysroot, path))
+    path = llvm::sys::path::parent_path(path);
+
+  return !path.empty();
 }
 
 /// \brief Handle GnuLD script for ELF.
index 8b95977..01af279 100644 (file)
@@ -90,6 +90,17 @@ RUN:     %p/Inputs/group-cmd-search-2.ls -o %t6
 */
 
 /*
+  This link should finish successfully. The group-cmd-search-2.ls
+  script contains GROUP command with an absolute path and the sysroot
+  directory is provided. The linker has to search the absolute path
+  under the sysroot directory.
+
+RUN: lld -flavor gnu -target x86_64 -shared --sysroot=%p/Inputs/../Inputs \
+RUN:     -L%p/Inputs %p/Inputs/use-shared.x86-64 \
+RUN:     %p/Inputs/group-cmd-search-2.ls -o %t6
+*/
+
+/*
   This link should fail with unknown input file format error.
   The linker script from this file contains GROUP with an absolute
   path which can be found under provided sysroot directory.