Skip to content

[lld-macho] Ignore duplicate -rpath entries #99289

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
Jul 18, 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 @@ -199,6 +199,7 @@ struct Configuration {
std::vector<llvm::StringRef> systemLibraryRoots;
std::vector<llvm::StringRef> librarySearchPaths;
std::vector<llvm::StringRef> frameworkSearchPaths;
bool warnDuplicateRpath = true;
llvm::SmallVector<llvm::StringRef, 0> runtimePaths;
std::vector<std::string> astPaths;
std::vector<Symbol *> explicitUndefineds;
Expand Down
17 changes: 16 additions & 1 deletion lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,19 @@ static void eraseInitializerSymbols() {
sym->used = false;
}

static SmallVector<StringRef, 0> getRuntimePaths(opt::InputArgList &args) {
SmallVector<StringRef, 0> vals;
DenseSet<StringRef> seen;
for (const Arg *arg : args.filtered(OPT_rpath)) {
StringRef val = arg->getValue();
if (seen.insert(val).second)
vals.push_back(val);
else if (config->warnDuplicateRpath)
warn("duplicate -rpath '" + val + "' ignored [--warn-duplicate-rpath]");
}
return vals;
}

namespace lld {
namespace macho {
bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
Expand Down Expand Up @@ -1642,7 +1655,9 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
error("--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
"--thinlto-index-only=");
}
config->runtimePaths = args::getStrings(args, OPT_rpath);
config->warnDuplicateRpath =
args.hasFlag(OPT_warn_duplicate_rpath, OPT_no_warn_duplicate_rpath, true);
config->runtimePaths = getRuntimePaths(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
6 changes: 6 additions & 0 deletions lld/MachO/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ def no_warn_dylib_install_name: Flag<["--"], "no-warn-dylib-install-name">,
def warn_dylib_install_name: Flag<["--"], "warn-dylib-install-name">,
HelpText<"Warn on -install_name if -dylib is not passed">,
Group<grp_lld>;
def warn_duplicate_rpath: Flag<["--"], "warn-duplicate-rpath">,
HelpText<"Warn if the same -rpath is specified multiple times (default)">,
Group<grp_lld>;
def no_warn_duplicate_rpath: Flag<["--"], "no-warn-duplicate-rpath">,
HelpText<"Do not warn if the same -rpath is specified multiple times">,
Group<grp_lld>;
def call_graph_profile_sort: Flag<["--"], "call-graph-profile-sort">,
HelpText<"Reorder sections with call graph profile (default)">,
Group<grp_lld>;
Expand Down
1 change: 1 addition & 0 deletions lld/test/MachO/link-search-at-rpath.s
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# RUN: -rpath @loader_path/../foo \
# RUN: -rpath @loader_path/../subdir \
# RUN: -rpath @loader_path/../foo \
# RUN: --no-warn-duplicate-rpath \
# RUN: %t/bar.o -o %t/subdir2/libbar.dylib

# RUN: %lld -lSystem %t/main.o %t/subdir2/libbar.dylib -o %t/test
Expand Down
15 changes: 15 additions & 0 deletions lld/test/MachO/rpath.s
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
# CHECK-NEXT: cmdsize 32
# CHECK-NEXT: path /another/rpath

## Check that -rpath entries are deduplicated.
# RUN: not %lld %t.o -o /dev/null -rpath /some/rpath -rpath /other/rpath -rpath /some/rpath 2>&1 | \
# RUN: FileCheck --check-prefix=FATAL %s
# FATAL: error: duplicate -rpath '/some/rpath' ignored [--warn-duplicate-rpath]

# RUN: %lld -o %t-dup %t.o -rpath /some/rpath -rpath /other/rpath -rpath /some/rpath --no-warn-duplicate-rpath
# RUN: llvm-objdump --macho --all-headers %t-dup | FileCheck %s --check-prefix=DEDUP
# DEDUP: LC_RPATH
# DEDUP-NEXT: cmdsize 24
# DEDUP-NEXT: path /some/rpath
# DEDUP: LC_RPATH
# DEDUP-NEXT: cmdsize 32
# DEDUP-NEXT: path /other/rpath
# DEDUP-NOT: LC_RPATH

.text
.global _main
_main:
Expand Down
Loading