This is an archive of the discontinued LLVM Phabricator instance.

[MachO] Fix two bugs with reexported dylibs
AcceptedPublic

Authored by comex on May 8 2019, 7:32 PM.

Details

Reviewers
lhames
kledzik
Summary

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".
  1. 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.

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

comex created this revision.May 8 2019, 7:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 8 2019, 7:32 PM
comex added a comment.May 8 2019, 7:36 PM

Oops, forgot to mention:

This will reproduce the issue:

echo 'int x = 2;' > sub.c
clang -dynamiclib -o libsub.dylib sub.c -install_name /usr/local/lib/libsub.dylib
clang -dynamiclib -o libreexporter.dylib -x c /dev/null -Wl,-reexport-lsub -L.
echo 'extern int x; int main() { return x; }' > client.c
clang -c -o client.o client.c
# crashes:
/path/to/ld64.lld -o client -L. client.o -lreexporter -lSystem

Also, I don't have commit access, so someone will have to commit the patch for me.

lhames accepted this revision.May 14 2019, 12:46 PM

Is it possible to write a test case for this? Otherwise LGTM. Thanks for all the work on this! :)

This revision is now accepted and ready to land.May 14 2019, 12:46 PM