This is an archive of the discontinued LLVM Phabricator instance.

[lld/mac] Emit only one LC_LOAD_DYLIB per dylib
ClosedPublic

Authored by thakis on Jun 1 2021, 1:05 PM.

Details

Reviewers
int3
gkm
Group Reviewers
Restricted Project
Commits
rGaeae3e0ba906: [lld/mac] Emit only one LC_LOAD_DYLIB per dylib
Summary

In some cases, we end up with several distinct DylibFiles that
have the same install name. Only emit a single LC_LOAD_DYLIB in
those cases.

This happens in 3 cases I know of:

  1. Some tbd files are symlinks. libpthread.tbd is a symlink against libSystem.tbd for example, so -lSystem -lpthread loads libSystem.tbd twice. We could (and maybe should) cache loaded dylibs by realpath() to catch this.
  1. Some tbd files are copies of each other. For example, CFNetwork.framework/CFNetwork.tbd and CFNetwork.framework/Versions/A/CFNetwork.tbd are two distinct copies of the same file. The former is found by -framework CFNetwork and the latter by the reexport in CoreServices.tbd. We could conceivably catch this by making -framework search look in Versions/Current instead of in the root, and/or by using a content hash to cache tbd files, but that's starting to sound complicated.
  1. Magic $ld$ symbol processing can change the install name of a dylib based on the target platform_version. Here, two truly distinct dylibs can have the same install name.

So we need this code to deal with (3) anyways. Might as well use
it for 1 and 2, at least for now :)

With this (and D103430), clang-format links in the same dylibs
when linked with lld and ld64.

Diff Detail

Event Timeline

thakis created this revision.Jun 1 2021, 1:05 PM
Herald added a reviewer: gkm. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
thakis requested review of this revision.Jun 1 2021, 1:05 PM
int3 accepted this revision.Jun 1 2021, 2:52 PM
int3 added inline comments.
lld/MachO/Writer.cpp
681

nit: no need for llvm::

716
lld/test/MachO/dylink-ordinal.s
4

also, TIL about this flag... looks like I can clean up some of our tests :D

7–24

are these really necessary? All these pass in the current implementation (only the LOAD check below fails)... plus they seem pretty similar to what's already in dylink.s

This revision is now accepted and ready to land.Jun 1 2021, 2:52 PM
thakis marked 3 inline comments as done.Jun 1 2021, 3:13 PM

Thanks!

lld/test/MachO/dylink-ordinal.s
7–24

It's all different places that reference the ordinal – seems nice to have the coverage to me. But feel free to delete the bits you don't like :)

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJun 1 2021, 3:15 PM