Skip to content

Commit 47f9722

Browse files
committed
[lld-macho] Add --ignore-auto-link-option
This provides a workaround for a small difference in ld64 behavior where ld64 ignores invalid LC_LINKER_OPTIONS unless the link fails. Instead of fully adopting that behavior, this provides an escape hatch by allowing users to specify `--ignore-auto-link-option` passing the invalid library or framework name Fixes #56939 Differential Revision: https://reviews.llvm.org/D135530
1 parent c1c7230 commit 47f9722

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

lld/MachO/Config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ struct Configuration {
169169
bool deadStrip = false;
170170
bool errorForArchMismatch = false;
171171
bool ignoreAutoLink = false;
172+
// ld64 allows invalid auto link options as long as the link succeeds. LLD
173+
// does not, but there are cases in the wild where the invalid linker options
174+
// exist. This allows users to ignore the specific invalid options in the case
175+
// they can't easily fix them.
176+
llvm::StringSet<> ignoreAutoLinkOptions;
172177
PlatformInfo platformInfo;
173178
llvm::Optional<PlatformInfo> secondaryPlatformInfo;
174179
NamespaceKind namespaceKind = NamespaceKind::twolevel;

lld/MachO/Driver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,15 @@ void macho::parseLCLinkerOption(InputFile *f, unsigned argc, StringRef data) {
478478
unsigned i = 0;
479479
StringRef arg = argv[i];
480480
if (arg.consume_front("-l")) {
481+
if (config->ignoreAutoLinkOptions.contains(arg))
482+
return;
481483
addLibrary(arg, /*isNeeded=*/false, /*isWeak=*/false,
482484
/*isReexport=*/false, /*isHidden=*/false, /*isExplicit=*/false,
483485
LoadType::LCLinkerOption);
484486
} else if (arg == "-framework") {
485487
StringRef name = argv[++i];
488+
if (config->ignoreAutoLinkOptions.contains(name))
489+
return;
486490
addFramework(name, /*isNeeded=*/false, /*isWeak=*/false,
487491
/*isReexport=*/false, /*isExplicit=*/false,
488492
LoadType::LCLinkerOption);
@@ -1543,6 +1547,8 @@ bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
15431547
getenv("LD_DYLIB_CPU_SUBTYPES_MUST_MATCH");
15441548
config->objcStubsMode = getObjCStubsMode(args);
15451549
config->ignoreAutoLink = args.hasArg(OPT_ignore_auto_link);
1550+
for (const Arg *arg : args.filtered(OPT_ignore_auto_link_option))
1551+
config->ignoreAutoLinkOptions.insert(arg->getValue());
15461552

15471553
for (const Arg *arg : args.filtered(OPT_alias)) {
15481554
config->aliasedSymbols.push_back(

lld/MachO/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def no_call_graph_profile_sort : Flag<["--"], "no-call-graph-profile-sort">,
9595
def print_symbol_order_eq: Joined<["--"], "print-symbol-order=">,
9696
HelpText<"Print a symbol order specified by --call-graph-profile-sort into the specified file">,
9797
Group<grp_lld>;
98+
def ignore_auto_link_option : Separate<["--"], "ignore-auto-link-option">,
99+
Group<grp_lld>;
100+
def ignore_auto_link_option_eq : Joined<["--"], "ignore-auto-link-option=">,
101+
Alias<!cast<Separate>(ignore_auto_link_option)>,
102+
HelpText<"Ignore a single auto-linked library or framework. Useful to ignore invalid options that ld64 ignores">,
103+
Group<grp_lld>;
98104

99105
// This is a complete Options.td compiled from Apple's ld(1) manpage
100106
// dated 2018-03-07 and cross checked with ld64 source code in repo

lld/test/MachO/lc-linker-option.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
; FRAME-NEXT: name /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
1414

1515
; RUN: not %lld %t/framework.o -o %t/frame_no_autolink -ignore_auto_link 2>&1 | FileCheck --check-prefix=NO_AUTOLINK %s
16+
; RUN: not %lld %t/framework.o -o %t/frame_no_autolink --ignore-auto-link-option CoreFoundation 2>&1 | FileCheck --check-prefix=NO_AUTOLINK %s
17+
; RUN: not %lld %t/framework.o -o %t/frame_no_autolink --ignore-auto-link-option=CoreFoundation 2>&1 | FileCheck --check-prefix=NO_AUTOLINK %s
1618
; NO_AUTOLINK: error: undefined symbol: __CFBigNumGetInt128
1719

1820
; RUN: llvm-as %t/l.ll -o %t/l.o

0 commit comments

Comments
 (0)