[PATCH] [ELF] Fix the file look up algorithm used in the linker script GROUP command.

Simon Atanasyan simon at atanasyan.com
Wed Apr 30 13:02:35 PDT 2014


================
Comment at: include/lld/ReaderWriter/ELFLinkingContext.h:176-180
@@ -175,1 +175,7 @@
 
+  /// \brief Searches directories for a match on the input file.
+  /// If \p fileName is an absolute path and \p isSysRooted is true, check
+  /// the file under sysroot directory. If \p fileName is a relative path
+  /// and is not in the current directory, search the file through library
+  /// search directories.
+  ErrorOr<StringRef> searchFile(StringRef fileName, bool isSysRooted) const;
----------------
Shankar Kalpathi Easwaran wrote:
> There is also a third thing, to support namespaces. You could refer to -l:mylib.a, the linker would search by not adding a prefix 'lib'.
I did not plan to support namespaces in this fix. But now I think it is a good idea to cover all cases by this patch. Will implement this soon.

================
Comment at: lib/Driver/GnuLdInputGraph.cpp:76-86
@@ -72,1 +75,13 @@
 
+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()]))
+    return false;
+
+  return llvm::sys::fs::equivalent(sysroot, path.substr(0, sysroot.size()));
+}
+
----------------
Shankar Kalpathi Easwaran wrote:
> can we just use realpath and compare if the file is a prefix of another ?
> 
I use the llvm::sys::fs::equivalent() to make this code a bit more platform independent. As far as I understand on Windows it is possible to get sysroot = 'C:\work_folder\SysRoot' and path = 'c:\work_folder\sysroot\file_name'. So on Windows we have to use case insensitive string comparison and case sensitive string comparison on Unix.

================
Comment at: lib/Driver/GnuLdInputGraph.cpp:95
@@ +94,3 @@
+  StringRef sysRoot = _elfLinkingContext.getSysroot();
+  if (!sysRoot.empty() && isPathUnderSysroot(sysRoot, *getPath(ctx)))
+    attributes.setSysRooted(true);
----------------
Shankar Kalpathi Easwaran wrote:
> we should probably reject sysroot being empty during parsing command line options, that way we can get rid of empty() comparison.
I check that the sysroot is not empty here to recognize a valid case when a user does not provide `--sysroot` option at all.

http://reviews.llvm.org/D3575






More information about the llvm-commits mailing list