[PATCH] D61714: [MachO] Fix two bugs with reexported dylibs

Nicholas Allegra via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 8 19:32:44 PDT 2019


comex created this revision.
comex added reviewers: lhames, kledzik.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When linking against a Mach-O dylib that reexports other dylibs, lld searches for the reexported dylibs in multiple places: it tries the full path referenced by LC_REEXPORT_DYLIB (with and without sysroot prepended), and also searches in -L directories for a library matching the basename of that path.  This patch fixes two bugs related to that search:

1. When looking in -L paths, MachOLinkingContext::findIndirectDylib() passed the full basename to searchLibrary(), e.g. "libfoo.dylib".  But searchLibrary() prepends "lib" and appends ".dylib", so it ended up looking for "liblibfoo.dylib.dylib".  Fix findIndirectDylib() to strip "lib" and ".dylib".

2. If the library is not found for whatever reason, there was no error handling.  entry.file would just be set to null, which would later trigger an assert "assert(dylib.file)" in a different function.  To fix this, print an error if find() returns; since that (intentionally) does not abort the process and still leaves entry.file as null, change the function with the assert to tolerate that case.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D61714

Files:
  lib/ReaderWriter/MachO/File.h
  lib/ReaderWriter/MachO/MachOLinkingContext.cpp


Index: lib/ReaderWriter/MachO/MachOLinkingContext.cpp
===================================================================
--- lib/ReaderWriter/MachO/MachOLinkingContext.cpp
+++ lib/ReaderWriter/MachO/MachOLinkingContext.cpp
@@ -701,7 +701,7 @@
   StringRef leafName = split.second;
   if (leafName.startswith("lib") && leafName.endswith(".dylib")) {
     // FIXME: Need to enhance searchLibrary() to only look for .dylib
-    auto libPath = searchLibrary(leafName);
+    auto libPath = searchLibrary(leafName.slice(3, leafName.size() - 6));
     if (libPath)
       return loadIndirectDylib(libPath.getValue());
   }
Index: lib/ReaderWriter/MachO/File.h
===================================================================
--- lib/ReaderWriter/MachO/File.h
+++ lib/ReaderWriter/MachO/File.h
@@ -12,6 +12,7 @@
 #include "Atoms.h"
 #include "DebugInfo.h"
 #include "MachONormalizedFile.h"
+#include "lld/Common/ErrorHandler.h"
 #include "lld/Core/SharedLibraryFile.h"
 #include "lld/Core/Simple.h"
 #include "llvm/ADT/DenseMap.h"
@@ -323,6 +324,8 @@
   void loadReExportedDylibs(FindDylib find) {
     for (ReExportedDylib &entry : _reExportedDylibs) {
       entry.file = find(entry.path);
+      if (!entry.file)
+        error(Twine("could not load reexported dylib ") + entry.path);
     }
   }
 
@@ -361,7 +364,8 @@
 
     // Next, check if symbol is implemented in some re-exported dylib.
     for (const ReExportedDylib &dylib : _reExportedDylibs) {
-      assert(dylib.file);
+      if (!dylib.file)
+        continue;
       auto atom = dylib.file->exports(name, installName);
       if (atom.get())
         return atom;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61714.198757.patch
Type: text/x-patch
Size: 1628 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190509/c483d252/attachment.bin>


More information about the llvm-commits mailing list