[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.
This commit is contained in:
Nuri Amari 2024-05-31 18:17:51 -07:00 committed by GitHub
parent 16832eb585
commit 1697030d9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -1162,6 +1162,8 @@ static void createFiles(const InputArgList &args) {
// This loop should be reserved for options whose exact ordering matters.
// Other options should be handled via filtered() and/or getLastArg().
bool isLazy = false;
// If we've processed an opening --start-lib, without a matching --end-lib
bool inLib = false;
for (const Arg *arg : args) {
const Option &opt = arg->getOption();
warnIfDeprecatedOption(opt);
@ -1219,13 +1221,16 @@ static void createFiles(const InputArgList &args) {
LoadType::CommandLine);
break;
case OPT_start_lib:
if (isLazy)
if (inLib)
error("nested --start-lib");
isLazy = true;
inLib = true;
if (!config->allLoad)
isLazy = true;
break;
case OPT_end_lib:
if (!isLazy)
if (!inLib)
error("stray --end-lib");
inLib = false;
isLazy = false;
break;
default:

View File

@ -86,6 +86,11 @@
# RUN: not %lld --end-lib 2>&1 | FileCheck %s --check-prefix=STRAY
# STRAY: error: stray --end-lib
# RUN: %lld -dylib --start-lib %t/1.bc %t/2.o --end-lib -all_load -o %t/out
# RUN: llvm-readobj -s %t/out | FileCheck --check-prefix=ALL-LOAD %s
# ALL-LOAD-DAG: _foo
# ALL-LOAD-DAG: _bar
#--- main.s
.globl _main
_main: