Simulator should stop loading dynamic libraries from build directory

Originator:ruenzuo
Number:rdar://32197172 Date Originated:Open
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 8 (8.3.2)
Classification:Enhancement Reproducible:Always
 
Summary:
If an iOS project is missing the `Copy Files` build phase to copy dynamic frameworks to the proper `Frameworks` directory inside the application bundle, the app will crash on Device but it would work fine in Simulator.

A further inspection on the dyld API usage point that the Simulator is loading the libraries from the build directory, which should not do.

iOS Simulator build products directory contents:

$ ls
FrameworkA.framework  XING.app
$ ls XING.app/
Base.lproj  Info.plist  PkgInfo   XING    _CodeSignature

There is no Frameworks directory.

Executable inspection:

$ otool -L XING.app/XING 
XING.app/XING:
  @rpath/FrameworkA.framework/FrameworkA (compatibility version 1.0.0, current version 1.0.0)
  /System/Library/Frameworks/Foundation.framework/Foundation (compatibility version 300.0.0, current version 1349.54.0)
  /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
  /usr/lib/libSystem.dylib (compatibility version 1.0.0, current version 1238.50.2)
  /System/Library/Frameworks/UIKit.framework/UIKit (compatibility version 1.0.0, current version 3600.7.47)
$ otool -l XING.app/XING | grep RPATH -n7
397-          cmd LC_LOAD_DYLIB
398-      cmdsize 80
399-         name /System/Library/Frameworks/UIKit.framework/UIKit (offset 24)
400-   time stamp 2 Thu Jan  1 01:00:02 1970
401-      current version 3600.7.47
402-compatibility version 1.0.0
403-Load command 17
404:          cmd LC_RPATH
405-      cmdsize 40
406-         path @executable_path/Frameworks (offset 12)
407-Load command 18
408-      cmd LC_FUNCTION_STARTS
409-  cmdsize 16
410-  dataoff 17296
411- datasize 16

LC_RPATH is correctly set to Frameworks directory and used to load the framework.

Log from dyld API usage:

dyld: loaded: /Users/renzo.crisostomo/Library/Developer/Xcode/DerivedData/XING-bbsprjwhtcxfzzfivnkfqioxxlsh/Build/Products/Debug-iphonesimulator/FrameworkA.framework/FrameworkA

Framework is loaded from the build directory. This should not happen.

Running the project on Device, it will crash with the following:

dyld: Library not loaded: @rpath/FrameworkA.framework/FrameworkA
  Referenced from: /var/containers/Bundle/Application/EE4AAE10-2A0D-4C48-A8DC-A7933F999D15/XING.app/XING
  Reason: image not found

The .app contents are the same as in the Simulator build:

$ ls
FrameworkA.framework  XING.app
$ ls XING.app/
Base.lproj      PkgInfo       _CodeSignature
Info.plist      XING        embedded.mobileprovision
$ otool -L XING.app/XING 
XING.app/XING:
  @rpath/FrameworkA.framework/FrameworkA (compatibility version 1.0.0, current version 1.0.0)
  /System/Library/Frameworks/Foundation.framework/Foundation (compatibility version 300.0.0, current version 1349.54.0)
  /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
  /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.50.2)
  /System/Library/Frameworks/UIKit.framework/UIKit (compatibility version 1.0.0, current version 3600.7.47)

nb-rcrisostomo:Debug-iphoneos renzo.crisostomo$ otool -l XING.app/XING | grep RPATH -n7
392-          cmd LC_LOAD_DYLIB
393-      cmdsize 80
394-         name /System/Library/Frameworks/UIKit.framework/UIKit (offset 24)
395-   time stamp 2 Thu Jan  1 01:00:02 1970
396-      current version 3600.7.47
397-compatibility version 1.0.0
398-Load command 18
399:          cmd LC_RPATH
400-      cmdsize 40
401-         path @executable_path/Frameworks (offset 12)
402-Load command 19
403-      cmd LC_FUNCTION_STARTS
404-  cmdsize 16
405-  dataoff 5007

After property adding the missing `Copy Files` build phase to copy dynamic frameworks to the proper `Frameworks` directory, the application doesn't crash anymore, but nothing really changes in the dyld API usage, the only difference is that the Framework is in the proper directory:

$ ls XING.app/Frameworks/
FrameworkA.framework

Steps to Reproduce:
Using the latest version of Xcode (8.3.2 at the time of writing)

1. `git checkout HEAD^`
2. Open XING.xcworkspace
3. Select Device
4. Run
5. Crash: dyld: Library not loaded

The latest commit in the repository adds the missing build phase and fixes the problem.

Expected Results:
Simulator should stop loading dynamic libraries from build directory. This is not really helpful and it's a bit confusing. I would expect to get the same dyld crash in both cases, Simulator and Device.

Actual Results:
Simulator loads dynamic libraries from build directory, hiding build settings problems that are only reproducible when running the project using a device.

Version:
Xcode 8 (8.3.2)

Notes:


Configuration:
Xcode 8.3.2, macOS Sierra 10.12.4

Attachments:
'XING.zip' was successfully uploaded.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!