Skip to content

[lld][MachO] Support -allowable_client #117155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lld/MachO/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ struct Configuration {
std::vector<llvm::StringRef> frameworkSearchPaths;
bool warnDuplicateRpath = true;
llvm::SmallVector<llvm::StringRef, 0> runtimePaths;
llvm::SmallVector<llvm::StringRef, 0> allowableClients;
std::vector<std::string> astPaths;
std::vector<Symbol *> explicitUndefineds;
llvm::StringSet<> explicitDynamicLookups;
Expand Down
12 changes: 12 additions & 0 deletions lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,17 @@ static SmallVector<StringRef, 0> getRuntimePaths(opt::InputArgList &args) {
return vals;
}

static SmallVector<StringRef, 0> getAllowableClients(opt::InputArgList &args) {
SmallVector<StringRef, 0> vals;
DenseSet<StringRef> seen;
for (const Arg *arg : args.filtered(OPT_allowable_client)) {
StringRef val = arg->getValue();
if (seen.insert(val).second)
vals.push_back(val);
}
return vals;
}

namespace lld {
namespace macho {
bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
Expand Down Expand Up @@ -1771,6 +1782,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
config->warnDuplicateRpath =
args.hasFlag(OPT_warn_duplicate_rpath, OPT_no_warn_duplicate_rpath, true);
config->runtimePaths = getRuntimePaths(args);
config->allowableClients = getAllowableClients(args);
config->allLoad = args.hasFlag(OPT_all_load, OPT_noall_load, false);
config->archMultiple = args.hasArg(OPT_arch_multiple);
config->applicationExtension = args.hasFlag(
Expand Down
3 changes: 1 addition & 2 deletions lld/MachO/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,7 @@ def sub_umbrella : Separate<["-"], "sub_umbrella">,
Group<grp_rare>;
def allowable_client : Separate<["-"], "allowable_client">,
MetaVarName<"<name>">,
HelpText<"Specify <name> of a dylib or framework that is allowed to link to this dylib">,
Flags<[HelpHidden]>,
HelpText<"Specify <name> of a dylib, framework, or executable that is allowed to link to this dylib">,
Group<grp_rare>;
def client_name : Separate<["-"], "client_name">,
MetaVarName<"<name>">,
Expand Down
27 changes: 27 additions & 0 deletions lld/MachO/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,31 @@ class LCRPath final : public LoadCommand {
StringRef path;
};

class LCSubClient final : public LoadCommand {
public:
explicit LCSubClient(StringRef client) : client(client) {}

uint32_t getSize() const override {
return alignToPowerOf2(sizeof(sub_client_command) + client.size() + 1,
target->wordSize);
}

void writeTo(uint8_t *buf) const override {
auto *c = reinterpret_cast<sub_client_command *>(buf);
buf += sizeof(sub_client_command);

c->cmd = LC_SUB_CLIENT;
c->cmdsize = getSize();
c->client = sizeof(sub_client_command);

memcpy(buf, client.data(), client.size());
buf[client.size()] = '\0';
}

private:
StringRef client;
};

class LCDyldEnv final : public LoadCommand {
public:
explicit LCDyldEnv(StringRef name) : name(name) {}
Expand Down Expand Up @@ -822,6 +847,8 @@ template <class LP> void Writer::createLoadCommands() {
in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName,
config->dylibCompatibilityVersion,
config->dylibCurrentVersion));
for (StringRef client : config->allowableClients)
in.header->addLoadCommand(make<LCSubClient>(client));
break;
case MH_BUNDLE:
break;
Expand Down
Binary file removed lld/test/MachO/Inputs/liballowable_client.dylib
Binary file not shown.
30 changes: 21 additions & 9 deletions lld/test/MachO/allowable-client.s
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
# REQUIRES: x86
# RUN: rm -rf %t; split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: touch %t/empty.s; llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/empty.s -o %t/empty.o

# Check linking against a .dylib
# RUN: not %lld -o %t/test %t/test.o -L%S/Inputs -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT
# RUN: not %lld -o %t/libtest_debug.exe %t/test.o -L%S/Inputs -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT
# 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
# RUN: %lld -o %t/test %t/test.o -L%S/Inputs -lallowable_client -client_name allowed
# RUN: %lld -o %t/test %t/test.o -L%S/Inputs -lallowable_client -client_name all
# RUN: %lld -o %t/all %t/test.o -L%S/Inputs -lallowable_client
# RUN: %lld -o %t/allowed %t/test.o -L%S/Inputs -lallowable_client
# RUN: %lld -o %t/liballowed_debug.exe %t/test.o -L%S/Inputs -lallowable_client
# Check that `-allowable_client` generates LC_SUB_CLIENT.
# We create our .dylib in a `lib` subdirectory to make sure we test linking against the `.dylib` instead of the `.tbd` below.
# RUN: mkdir -p %t/lib; %lld -dylib -o %t/lib/liballowable_client.dylib %t/empty.o -allowable_client allowed -allowable_client also_allowed
# RUN: llvm-objdump --macho --all-headers %t/lib/liballowable_client.dylib | FileCheck %s
# CHECK: LC_SUB_CLIENT
# CHECK-NEXT: cmdsize 24
# CHECK-NEXT: client allowed
# CHECK: LC_SUB_CLIENT
# CHECK-NEXT: cmdsize 32
# CHECK-NEXT: client also_allowed

# Check linking against the .dylib we created above
# RUN: not %lld -o %t/test %t/test.o -L%t/lib -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT
# RUN: not %lld -o %t/libtest_debug.exe %t/test.o -L%t/lib -lallowable_client 2>&1 | FileCheck %s --check-prefix=NOTALLOWED-IMPLICIT
# 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
# RUN: %lld -o %t/test %t/test.o -L%t/lib -lallowable_client -client_name allowed
# RUN: %lld -o %t/test %t/test.o -L%t/lib -lallowable_client -client_name all
# RUN: %lld -o %t/all %t/test.o -L%t/lib -lallowable_client
# RUN: %lld -o %t/allowed %t/test.o -L%t/lib -lallowable_client
# RUN: %lld -o %t/liballowed_debug.exe %t/test.o -L%t/lib -lallowable_client

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