[lld] r219990 - [mach-o] Fix crash when -all_load used with dylibs

Nick Kledzik kledzik at apple.com
Thu Oct 16 16:42:43 PDT 2014


Author: kledzik
Date: Thu Oct 16 18:42:42 2014
New Revision: 219990

URL: http://llvm.org/viewvc/llvm-project?rev=219990&view=rev
Log:
[mach-o] Fix crash when -all_load used with dylibs

-all_load tells the darwin linker to immediately load all members of all
archives.  The code do that used reinterpret_cast<> instead of dyn_cast<>.
If the file was a dylib, the reinterpret_cast<> turned a pointer to a dylib
into a pointer to an archive...boom.

Added test case to reproduce the crash, simplified the code and used dyn_cast<>.

Added:
    lld/trunk/test/mach-o/force_load-dylib.yaml
Modified:
    lld/trunk/lib/Driver/DarwinInputGraph.cpp

Modified: lld/trunk/lib/Driver/DarwinInputGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinInputGraph.cpp?rev=219990&r1=219989&r2=219990&view=diff
==============================================================================
--- lld/trunk/lib/Driver/DarwinInputGraph.cpp (original)
+++ lld/trunk/lib/Driver/DarwinInputGraph.cpp Thu Oct 16 18:42:42 2014
@@ -35,31 +35,21 @@ std::error_code MachOFileNode::parse(con
   narrowFatBuffer(*filePath);
 
   std::vector<std::unique_ptr<File>> parsedFiles;
-  if (_isWholeArchive) {
-    std::error_code ec = ctx.registry().parseFile(_buffer, parsedFiles);
-    if (ec)
-      return ec;
-    assert(parsedFiles.size() == 1);
-    std::unique_ptr<File> f(parsedFiles[0].release());
-    if (auto archive =
-            reinterpret_cast<const ArchiveLibraryFile *>(f.get())) {
-      // FIXME: something needs to own archive File
-      //_files.push_back(std::move(archive));
-      return archive->parseAllMembers(_files);
-    } else {
-      // if --whole-archive is around non-archive, just use it as normal.
-      _files.push_back(std::move(f));
-      return std::error_code();
-    }
-  }
   if (std::error_code ec = ctx.registry().parseFile(_buffer, parsedFiles))
     return ec;
   for (std::unique_ptr<File> &pf : parsedFiles) {
-    // If a dylib was parsed, inform LinkingContext about it.
+    // If file is a dylib, inform LinkingContext about it.
     if (SharedLibraryFile *shl = dyn_cast<SharedLibraryFile>(pf.get())) {
       _context.registerDylib(reinterpret_cast<mach_o::MachODylibFile*>(shl),
                              _upwardDylib);
     }
+    // If file is an archive and -all_load, then add all members.
+    if (ArchiveLibraryFile *archive = dyn_cast<ArchiveLibraryFile>(pf.get())) {
+      if (_isWholeArchive) {
+        // Note: the members are added to _files, but the archive is not.
+        return archive->parseAllMembers(_files);
+      }
+    }
     _files.push_back(std::move(pf));
   }
   return std::error_code();

Added: lld/trunk/test/mach-o/force_load-dylib.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/force_load-dylib.yaml?rev=219990&view=auto
==============================================================================
--- lld/trunk/test/mach-o/force_load-dylib.yaml (added)
+++ lld/trunk/test/mach-o/force_load-dylib.yaml Thu Oct 16 18:42:42 2014
@@ -0,0 +1,45 @@
+# RUN: lld -flavor darwin -arch x86_64 -dylib %p/Inputs/bar.yaml \
+# RUN:     -install_name /usr/lib/libbar.dylib %p/Inputs/libSystem.yaml -o %t1.dylib
+# RUN: lld -flavor darwin -arch x86_64 -dylib %s -all_load %t1.dylib \
+# RUN:      -install_name /usr/lib/libfoo.dylib %p/Inputs/libSystem.yaml -o %t 
+# RUN: llvm-nm -m %t | FileCheck %s
+#
+#
+# Test -all_load does not break linking with dylibs
+#
+
+--- !mach-o
+arch:            x86_64
+file-type:       MH_OBJECT
+flags:           [ MH_SUBSECTIONS_VIA_SYMBOLS ]
+sections:
+  - segment:         __TEXT
+    section:         __text
+    type:            S_REGULAR
+    attributes:      [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS ]
+    address:         0x0000000000000000
+    content:         [ 0x55, 0x48, 0x89, 0xE5, 0x31, 0xC0, 0x5D, 0xE9, 
+                       0x00, 0x00, 0x00, 0x00 ]
+    relocations:
+      - offset:          0x00000008
+        type:            X86_64_RELOC_BRANCH
+        length:          2
+        pc-rel:          true
+        extern:          true
+        symbol:          1
+global-symbols:
+  - name:            _foo
+    type:            N_SECT
+    scope:           [ N_EXT ]
+    sect:            1
+    value:           0x0000000000000000
+undefined-symbols:
+  - name:            _bar
+    type:            N_UNDF
+    scope:           [ N_EXT ]
+    value:           0x0000000000000000
+
+...
+
+
+# CHECK: (__TEXT,__text) external _foo





More information about the llvm-commits mailing list