Skip to content

Commit 316ee58

Browse files
committed
[lld][MachO] Respect dylibs linked with -allowable_client
ld64.lld will currently allow you to link against dylibs linked with `-allowable_client`, even if the client's name does not match any allowed client. This change fixes that. See #114146 for related discussion. It doesn't quite fix that issue yet, but this change should enable fixing that issue too, once lld learns how to parse the `allowable_clients` field in `.tbd`s. The test binary `liballowable_client.dylib` was created on macOS with: echo | clang -xc - -dynamiclib -mmacosx-version-min=10.11 -arch x86_64 -Wl,-allowable_client,allowed -o lib/liballowable_client.dylib
1 parent e41df5c commit 316ee58

File tree

8 files changed

+55
-2
lines changed

8 files changed

+55
-2
lines changed

lld/MachO/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ struct Configuration {
164164
llvm::StringRef finalOutput;
165165

166166
llvm::StringRef installName;
167+
llvm::StringRef clientName;
167168
llvm::StringRef mapFile;
168169
llvm::StringRef ltoObjPath;
169170
llvm::StringRef thinLTOJobs;

lld/MachO/Driver.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,15 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
18631863
config->installName = config->finalOutput;
18641864
}
18651865

1866+
auto getClientName = [&]() {
1867+
StringRef cn = path::filename(config->finalOutput);
1868+
cn.consume_front("lib");
1869+
auto firstDotOrUnderscore = cn.find_first_of("._");
1870+
cn = cn.take_front(firstDotOrUnderscore);
1871+
return cn;
1872+
};
1873+
config->clientName = args.getLastArgValue(OPT_client_name, getClientName());
1874+
18661875
if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
18671876
if (config->outputType != MH_DYLIB)
18681877
warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");

lld/MachO/DriverUtils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,26 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
264264
if (newFile->exportingFile)
265265
newFile->parseLoadCommands(mbref);
266266
}
267+
268+
if (explicitlyLinked && !newFile->allowableClients.empty()) {
269+
bool allowed = std::any_of(
270+
newFile->allowableClients.begin(), newFile->allowableClients.end(),
271+
[&](StringRef allowableClient) {
272+
// We only do a prefix match to match LD64's behaviour.
273+
return allowableClient.starts_with(config->clientName);
274+
});
275+
276+
// TODO: This behaviour doesn't quite match the latest available source
277+
// release of LD64 (ld64-951.9), which allows "parents" and "siblings"
278+
// to link to libraries even when they're not explicitly named as
279+
// allowable clients. However, behaviour around this seems to have
280+
// changed in the latest release of Xcode (ld64-1115.7.3), so it's not
281+
// clear what the correct thing to do is yet.
282+
if (!allowed)
283+
error("cannot link directly with '" +
284+
sys::path::filename(newFile->installName) + "' because " +
285+
config->clientName + " is not an allowed client");
286+
}
267287
return newFile;
268288
}
269289

lld/MachO/InputFiles.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,14 @@ DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
17301730
? this
17311731
: this->umbrella;
17321732

1733+
if (!canBeImplicitlyLinked) {
1734+
for (auto *cmd : findCommands<sub_client_command>(hdr, LC_SUB_CLIENT)) {
1735+
StringRef allowableClient{reinterpret_cast<const char *>(cmd) +
1736+
cmd->client};
1737+
allowableClients.push_back(allowableClient);
1738+
}
1739+
}
1740+
17331741
const auto *dyldInfo = findCommand<dyld_info_command>(hdr, LC_DYLD_INFO_ONLY);
17341742
const auto *exportsTrie =
17351743
findCommand<linkedit_data_command>(hdr, LC_DYLD_EXPORTS_TRIE);

lld/MachO/InputFiles.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class DylibFile final : public InputFile {
241241
DylibFile *exportingFile = nullptr;
242242
DylibFile *umbrella;
243243
SmallVector<StringRef, 2> rpaths;
244+
SmallVector<StringRef> allowableClients;
244245
uint32_t compatibilityVersion = 0;
245246
uint32_t currentVersion = 0;
246247
int64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel

lld/MachO/Options.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,7 @@ def allowable_client : Separate<["-"], "allowable_client">,
875875
Group<grp_rare>;
876876
def client_name : Separate<["-"], "client_name">,
877877
MetaVarName<"<name>">,
878-
HelpText<"Specifies a <name> this client should match with the -allowable_client <name> in a dependent dylib">,
879-
Flags<[HelpHidden]>,
878+
HelpText<"Specifies a <name> this client should match with the -allowable_client <name> in an explicitly linked dylib">,
880879
Group<grp_rare>;
881880
def umbrella : Separate<["-"], "umbrella">,
882881
MetaVarName<"<name>">,
Binary file not shown.

lld/test/MachO/allowable-client.s

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
3+
# RUN: not %lld -o %t %t.o -L%S/Inputs -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED1
4+
# RUN: not %lld -o %t %t.o -L%S/Inputs -lallowable_client -client_name notallowed 2>&1 | FileCheck %s --check-prefix=NOTALLOWED2
5+
# RUN: %lld -o %t %t.o -L%S/Inputs -lallowable_client -client_name allowed
6+
# RUN: %lld -o %t %t.o -L%S/Inputs -lallowable_client -client_name all
7+
8+
# NOTALLOWED1: error: cannot link directly with 'liballowable_client.dylib' because {{.*}} is not an allowed client
9+
# NOTALLOWED2: error: cannot link directly with 'liballowable_client.dylib' because notallowed is not an allowed client
10+
11+
.text
12+
.global _main
13+
_main:
14+
mov $0, %rax
15+
ret

0 commit comments

Comments
 (0)