Search code examples
xcodestatic-linkingdynamic-linkingxcode-mergeable-libraries

Can the Xcode linker deduplicate symbols in a merged binary?


Intro

I'm trying to determine whether Xcode's new Mergeable Libraries feature can remove duplicated symbols in a scenario where an app links to two dynamic frameworks which both link to the same static library, as follows:

|--App
  |--DynamicFramework1      
    |--StaticLibrary   
  |--DynamicFramework2      
    |--StaticLibrary <- the same library that DynamicFramework1 links to

Project setup

Here are some details about the app's structure:

  1. The static library is named StaticLibrary. It:
    • Contains a single Objective-C class named SLClass1
  2. The first dynamic framework target is named DynamicFramework1. It:
    • Contains a single Objective-C class named DF1Class1 that calls into SLClass1; and
    • Has a MERGEABLE_LIBRARY build setting value of YES.
  3. The second dynamic framework target is named DynamicFramework2. It:
    • Contains a single Objective-C class named DF2Class1 that calls into SLClass1; and
    • Has a MERGEABLE_LIBRARY build setting value of YES.
  4. The app target is named App. It:
    • Contains a single Swift class named ViewController that calls into the DF1Class1 and DF2Class1 classes; and
    • Has a MERGED_BINARY_TYPE build setting value of Automatic.1

I've created a minimal Xcode project here that has the above structure.

Expected result

The merged app binary, when built it in Release mode, contains the symbols for DF1Class1, DF2Class1 and SLClass1 directly within it and it contains only one instance of each of these symbols (i.e. no duplicates).

I am basing this expectation on the Benefits of mergeable libraries section of the Meet mergeable libraries talk from WWDC 2023 where the speaker said:

When merging, the linker can de-duplicate content, such as strings, across all libraries. For instance, it removes redundant symbol references, Objective-C selectors, and objc_msgsend stubs.

Actual result

The merged app binary, when built it in Release mode, contains the symbols for DF1Class1, DF2Class1 and SLClass1 directly within it but it contains two instances of SLClass1's symbols (i.e. it contains duplicates).2

My Question

Have I missed something in the app's build settings or have I misunderstood the capability of Mergeable Libraries? Can I get the Xcode linker to deduplicate symbols in the app's merged binary?

Footnotes

1I have tried with a MERGED_BINARY_TYPE build setting value of Manual also but the result is the same.
2I have validated this by running the nm command on the app's executable file and inspecting the output.

Solution

  • Apple's Quinn "The Eskimo" (renowned for his tireless support in the Apple Developer forums) has confirmed that Mergeable Libraries will not deduplicate symbols in the above scenario.

    For the details, see his answer in the Apple Developer Forums here.