Skip to content

Commit d668304

Browse files
authored
[lld][MachO] Support -allowable_client (#117155)
Closes #117113. Follow-up to #114638.
1 parent 611f5b8 commit d668304

File tree

6 files changed

+62
-11
lines changed

6 files changed

+62
-11
lines changed

lld/MachO/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ struct Configuration {
204204
std::vector<llvm::StringRef> frameworkSearchPaths;
205205
bool warnDuplicateRpath = true;
206206
llvm::SmallVector<llvm::StringRef, 0> runtimePaths;
207+
llvm::SmallVector<llvm::StringRef, 0> allowableClients;
207208
std::vector<std::string> astPaths;
208209
std::vector<Symbol *> explicitUndefineds;
209210
llvm::StringSet<> explicitDynamicLookups;

lld/MachO/Driver.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,17 @@ static SmallVector<StringRef, 0> getRuntimePaths(opt::InputArgList &args) {
15281528
return vals;
15291529
}
15301530

1531+
static SmallVector<StringRef, 0> getAllowableClients(opt::InputArgList &args) {
1532+
SmallVector<StringRef, 0> vals;
1533+
DenseSet<StringRef> seen;
1534+
for (const Arg *arg : args.filtered(OPT_allowable_client)) {
1535+
StringRef val = arg->getValue();
1536+
if (seen.insert(val).second)
1537+
vals.push_back(val);
1538+
}
1539+
return vals;
1540+
}
1541+
15311542
namespace lld {
15321543
namespace macho {
15331544
bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
@@ -1772,6 +1783,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
17721783
config->warnDuplicateRpath =
17731784
args.hasFlag(OPT_warn_duplicate_rpath, OPT_no_warn_duplicate_rpath, true);
17741785
config->runtimePaths = getRuntimePaths(args);
1786+
config->allowableClients = getAllowableClients(args);
17751787
config->allLoad = args.hasFlag(OPT_all_load, OPT_noall_load, false);
17761788
config->archMultiple = args.hasArg(OPT_arch_multiple);
17771789
config->applicationExtension = args.hasFlag(

lld/MachO/Options.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,7 @@ def sub_umbrella : Separate<["-"], "sub_umbrella">,
876876
Group<grp_rare>;
877877
def allowable_client : Separate<["-"], "allowable_client">,
878878
MetaVarName<"<name>">,
879-
HelpText<"Specify <name> of a dylib or framework that is allowed to link to this dylib">,
880-
Flags<[HelpHidden]>,
879+
HelpText<"Specify <name> of a dylib, framework, or executable that is allowed to link to this dylib">,
881880
Group<grp_rare>;
882881
def client_name : Separate<["-"], "client_name">,
883882
MetaVarName<"<name>">,

lld/MachO/Writer.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,31 @@ class LCRPath final : public LoadCommand {
409409
StringRef path;
410410
};
411411

412+
class LCSubClient final : public LoadCommand {
413+
public:
414+
explicit LCSubClient(StringRef client) : client(client) {}
415+
416+
uint32_t getSize() const override {
417+
return alignToPowerOf2(sizeof(sub_client_command) + client.size() + 1,
418+
target->wordSize);
419+
}
420+
421+
void writeTo(uint8_t *buf) const override {
422+
auto *c = reinterpret_cast<sub_client_command *>(buf);
423+
buf += sizeof(sub_client_command);
424+
425+
c->cmd = LC_SUB_CLIENT;
426+
c->cmdsize = getSize();
427+
c->client = sizeof(sub_client_command);
428+
429+
memcpy(buf, client.data(), client.size());
430+
buf[client.size()] = '\0';
431+
}
432+
433+
private:
434+
StringRef client;
435+
};
436+
412437
class LCDyldEnv final : public LoadCommand {
413438
public:
414439
explicit LCDyldEnv(StringRef name) : name(name) {}
@@ -822,6 +847,8 @@ template <class LP> void Writer::createLoadCommands() {
822847
in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName,
823848
config->dylibCompatibilityVersion,
824849
config->dylibCurrentVersion));
850+
for (StringRef client : config->allowableClients)
851+
in.header->addLoadCommand(make<LCSubClient>(client));
825852
break;
826853
case MH_BUNDLE:
827854
break;
Binary file not shown.

lld/test/MachO/allowable-client.s

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
# REQUIRES: x86
22
# RUN: rm -rf %t; split-file %s %t
33
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
4+
# RUN: touch %t/empty.s; llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/empty.s -o %t/empty.o
45

5-
# Check linking against a .dylib
6-
# RUN: not %lld -o %t/test %t/test.o -L%S/Inputs -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT
7-
# RUN: not %lld -o %t/libtest_debug.exe %t/test.o -L%S/Inputs -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT
8-
# RUN: not %lld -o %t/test %t/test.o -L%S/Inputs -lallowable_client -client_name notallowed 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-EXPLICIT
9-
# RUN: %lld -o %t/test %t/test.o -L%S/Inputs -lallowable_client -client_name allowed
10-
# RUN: %lld -o %t/test %t/test.o -L%S/Inputs -lallowable_client -client_name all
11-
# RUN: %lld -o %t/all %t/test.o -L%S/Inputs -lallowable_client
12-
# RUN: %lld -o %t/allowed %t/test.o -L%S/Inputs -lallowable_client
13-
# RUN: %lld -o %t/liballowed_debug.exe %t/test.o -L%S/Inputs -lallowable_client
6+
# Check that `-allowable_client` generates LC_SUB_CLIENT.
7+
# We create our .dylib in a `lib` subdirectory to make sure we test linking against the `.dylib` instead of the `.tbd` below.
8+
# RUN: mkdir -p %t/lib; %lld -dylib -o %t/lib/liballowable_client.dylib %t/empty.o -allowable_client allowed -allowable_client also_allowed
9+
# RUN: llvm-objdump --macho --all-headers %t/lib/liballowable_client.dylib | FileCheck %s
10+
# CHECK: LC_SUB_CLIENT
11+
# CHECK-NEXT: cmdsize 24
12+
# CHECK-NEXT: client allowed
13+
# CHECK: LC_SUB_CLIENT
14+
# CHECK-NEXT: cmdsize 32
15+
# CHECK-NEXT: client also_allowed
16+
17+
# Check linking against the .dylib we created above
18+
# RUN: not %lld -o %t/test %t/test.o -L%t/lib -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT
19+
# RUN: not %lld -o %t/libtest_debug.exe %t/test.o -L%t/lib -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT
20+
# RUN: not %lld -o %t/test %t/test.o -L%t/lib -lallowable_client -client_name notallowed 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-EXPLICIT
21+
# RUN: %lld -o %t/test %t/test.o -L%t/lib -lallowable_client -client_name allowed
22+
# RUN: %lld -o %t/test %t/test.o -L%t/lib -lallowable_client -client_name all
23+
# RUN: %lld -o %t/all %t/test.o -L%t/lib -lallowable_client
24+
# RUN: %lld -o %t/allowed %t/test.o -L%t/lib -lallowable_client
25+
# RUN: %lld -o %t/liballowed_debug.exe %t/test.o -L%t/lib -lallowable_client
1426

1527
# Check linking against a .tbd
1628
# RUN: not %lld -o %t/test %t/test.o -L%t -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT

0 commit comments

Comments
 (0)