Skip to content

Commit e385ec9

Browse files
speednoisemovementMark Rowe
and
Mark Rowe
authored
[lld-macho] Don't double emit reexported libraries (#132275)
When a library is specified with both `-l` and `-reexport_libraries`, lld will emit two load commands for it, in contrast with ld64. In an upcoming version of macOS, this fails dyld validation; see https://crbug.com/404905688 --------- Co-authored-by: Mark Rowe <[email protected]>>
1 parent 561dcb2 commit e385ec9

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lld/MachO/Writer.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -938,17 +938,20 @@ template <class LP> void Writer::createLoadCommands() {
938938
}
939939

940940
ordinal = dylibFile->ordinal = dylibOrdinal++;
941-
LoadCommandType lcType =
942-
dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak
943-
? LC_LOAD_WEAK_DYLIB
944-
: LC_LOAD_DYLIB;
941+
LoadCommandType lcType = LC_LOAD_DYLIB;
942+
if (dylibFile->reexport) {
943+
if (dylibFile->forceWeakImport)
944+
warn(path::filename(dylibFile->getName()) +
945+
" is re-exported so cannot be weak-linked");
946+
947+
lcType = LC_REEXPORT_DYLIB;
948+
} else if (dylibFile->forceWeakImport ||
949+
dylibFile->refState == RefState::Weak) {
950+
lcType = LC_LOAD_WEAK_DYLIB;
951+
}
945952
in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->installName,
946953
dylibFile->compatibilityVersion,
947954
dylibFile->currentVersion));
948-
949-
if (dylibFile->reexport)
950-
in.header->addLoadCommand(
951-
make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->installName));
952955
}
953956

954957
for (const auto &dyldEnv : config->dyldEnvs)

lld/test/MachO/sub-library.s

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242
# REEXPORT-HEADERS-NOT: Load command
4343
# REEXPORT-HEADERS: name [[PATH]]
4444

45+
## Check that specifying a library both with `l` and `reexport_library`
46+
## doesn't emit two load commands.
47+
# RUN: %lld -dylib -reexport_library %t/libgoodbye.dylib \
48+
# RUN: -L%t -lgoodbye %t/libsuper.o -o %t/libsuper.dylib
49+
# RUN: llvm-otool -L %t/libsuper.dylib | FileCheck %s \
50+
# RUN: --check-prefix=REEXPORT-DOUBLE -DPATH=%t/libgoodbye.dylib
51+
52+
# REEXPORT-DOUBLE: [[PATH]]
53+
# REEXPORT-DOUBLE-SAME: reexport
54+
# REEXPORT-DOUBLE-NOT: [[PATH]]
55+
4556
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/sub-library.o
4657
# RUN: %lld -o %t/sub-library -L%t -lsuper %t/sub-library.o
4758

0 commit comments

Comments
 (0)