[lld][MachO] Support for -interposable (#131813)

As discussed in https://github.com/llvm/llvm-project/issues/53680, add
support for ld64's -interposable flag on Apple platforms to lld.
This commit is contained in:
John Holdsworth 2025-03-28 20:05:50 +01:00 committed by GitHub
parent b0bb86e239
commit da84a7d403
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 7 deletions

View File

@ -183,6 +183,7 @@ struct Configuration {
bool deadStripDylibs = false;
bool demangle = false;
bool deadStrip = false;
bool interposable = false;
bool errorForArchMismatch = false;
bool ignoreAutoLink = false;
// ld64 allows invalid auto link options as long as the link succeeds. LLD

View File

@ -1676,6 +1676,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
// Must be set before any InputSections and Symbols are created.
config->deadStrip = args.hasArg(OPT_dead_strip);
config->interposable = args.hasArg(OPT_interposable);
config->systemLibraryRoots = getSystemLibraryRoots(args);
if (const char *path = getReproduceOption(args)) {

View File

@ -393,6 +393,9 @@ def grp_opts : OptionGroup<"opts">, HelpText<"OPTIMIZATIONS">;
def dead_strip : Flag<["-"], "dead_strip">,
HelpText<"Remove unreachable functions and data">,
Group<grp_opts>;
def interposable : Flag<["-"], "interposable">,
HelpText<"Indirects access to all exported symbols in an image">,
Group<grp_opts>;
def order_file : Separate<["-"], "order_file">,
MetaVarName<"<file>">,
HelpText<"Layout functions and data according to specification in <file>">,
@ -876,10 +879,6 @@ def setuid_safe : Flag<["-"], "setuid_safe">,
HelpText<"Set the MH_SETUID_SAFE bit in the mach-o header">,
Flags<[HelpHidden]>,
Group<grp_rare>;
def interposable : Flag<["-"], "interposable">,
HelpText<"Indirects access to all to exported symbols in a dylib">,
Flags<[HelpHidden]>,
Group<grp_rare>;
def multi_module : Flag<["-"], "multi_module">,
Alias<interposable>,
HelpText<"Alias for -interposable">,

View File

@ -203,9 +203,9 @@ Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
}
// With -flat_namespace, all extern symbols in dylibs are interposable.
// FIXME: Add support for `-interposable` (PR53680).
bool interposable = config->namespaceKind == NamespaceKind::flat &&
config->outputType != MachO::MH_EXECUTE &&
bool interposable = ((config->namespaceKind == NamespaceKind::flat &&
config->outputType != MachO::MH_EXECUTE) ||
config->interposable) &&
!isPrivateExtern;
Defined *defined = replaceSymbol<Defined>(
s, name, file, isec, value, size, isWeakDef, /*isExternal=*/true,

View File

@ -0,0 +1,34 @@
# REQUIRES: x86
# RUN: rm -rf %t; split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/2.s -o %t/2.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/3.s -o %t/3.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/main.s -o %t/main.o
# RUN: %lld -arch x86_64 -interposable -lSystem -o %t/main %t/main.o %t/2.o %t/3.o
# RUN: llvm-objdump --macho -d %t/main | FileCheck %s --check-prefix BUNDLE-OBJ
BUNDLE-OBJ-LABEL: _my_user:
BUNDLE-OBJ-NEXT: callq [[#%#x,]] ## symbol stub for: _my_friend
#--- 2.s
# my_lib: This contains the exported function
.globl _my_friend
_my_friend:
retq
#--- 3.s
# _my_user.s: This is the user/caller of the
# exported function
.text
_my_user:
callq _my_friend()
retq
#--- main.s
# main.s: dummy exec/main loads the exported function.
# This is basically a way to say `my_user` should get
# `my_func` from this executable.
.globl _main
.text
_main:
retq