Skip to content

Commit 1697030

Browse files
authored
[MachO LLD] Respect -all_load with --start-lib --end-lib style archives (#93993)
The -all_load flag is intended to force the linker to load all lazy members, but doesn't do so if the archive is specified with --start-lib, --end-lib flags. The `-all_load` flag is global, that is it can be placed anywhere in the linker invocation, and it affects the load behavior of all conventional archives listed. Unlike ELF's --whole-archive, the user need not necessarily have access to the entire linker invocation to reasonably make use of the flag. The user can supply `-all_load` to a build system without inspecting the rest of the linker invocation. To make the behavior of `--start-lib` style archives consistent with regular archives, this patch makes it so that -all_load also applies in this case.
1 parent 16832eb commit 1697030

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lld/MachO/Driver.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,8 @@ static void createFiles(const InputArgList &args) {
11621162
// This loop should be reserved for options whose exact ordering matters.
11631163
// Other options should be handled via filtered() and/or getLastArg().
11641164
bool isLazy = false;
1165+
// If we've processed an opening --start-lib, without a matching --end-lib
1166+
bool inLib = false;
11651167
for (const Arg *arg : args) {
11661168
const Option &opt = arg->getOption();
11671169
warnIfDeprecatedOption(opt);
@@ -1219,13 +1221,16 @@ static void createFiles(const InputArgList &args) {
12191221
LoadType::CommandLine);
12201222
break;
12211223
case OPT_start_lib:
1222-
if (isLazy)
1224+
if (inLib)
12231225
error("nested --start-lib");
1224-
isLazy = true;
1226+
inLib = true;
1227+
if (!config->allLoad)
1228+
isLazy = true;
12251229
break;
12261230
case OPT_end_lib:
1227-
if (!isLazy)
1231+
if (!inLib)
12281232
error("stray --end-lib");
1233+
inLib = false;
12291234
isLazy = false;
12301235
break;
12311236
default:

lld/test/MachO/start-lib.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
# RUN: not %lld --end-lib 2>&1 | FileCheck %s --check-prefix=STRAY
8787
# STRAY: error: stray --end-lib
8888

89+
# RUN: %lld -dylib --start-lib %t/1.bc %t/2.o --end-lib -all_load -o %t/out
90+
# RUN: llvm-readobj -s %t/out | FileCheck --check-prefix=ALL-LOAD %s
91+
# ALL-LOAD-DAG: _foo
92+
# ALL-LOAD-DAG: _bar
93+
8994
#--- main.s
9095
.globl _main
9196
_main:

0 commit comments

Comments
 (0)