[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:
@@ -183,6 +183,7 @@ struct Configuration {
|
|||||||
bool deadStripDylibs = false;
|
bool deadStripDylibs = false;
|
||||||
bool demangle = false;
|
bool demangle = false;
|
||||||
bool deadStrip = false;
|
bool deadStrip = false;
|
||||||
|
bool interposable = false;
|
||||||
bool errorForArchMismatch = false;
|
bool errorForArchMismatch = false;
|
||||||
bool ignoreAutoLink = false;
|
bool ignoreAutoLink = false;
|
||||||
// ld64 allows invalid auto link options as long as the link succeeds. LLD
|
// ld64 allows invalid auto link options as long as the link succeeds. LLD
|
||||||
|
|||||||
@@ -1676,6 +1676,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
|
|||||||
|
|
||||||
// Must be set before any InputSections and Symbols are created.
|
// Must be set before any InputSections and Symbols are created.
|
||||||
config->deadStrip = args.hasArg(OPT_dead_strip);
|
config->deadStrip = args.hasArg(OPT_dead_strip);
|
||||||
|
config->interposable = args.hasArg(OPT_interposable);
|
||||||
|
|
||||||
config->systemLibraryRoots = getSystemLibraryRoots(args);
|
config->systemLibraryRoots = getSystemLibraryRoots(args);
|
||||||
if (const char *path = getReproduceOption(args)) {
|
if (const char *path = getReproduceOption(args)) {
|
||||||
|
|||||||
@@ -393,6 +393,9 @@ def grp_opts : OptionGroup<"opts">, HelpText<"OPTIMIZATIONS">;
|
|||||||
def dead_strip : Flag<["-"], "dead_strip">,
|
def dead_strip : Flag<["-"], "dead_strip">,
|
||||||
HelpText<"Remove unreachable functions and data">,
|
HelpText<"Remove unreachable functions and data">,
|
||||||
Group<grp_opts>;
|
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">,
|
def order_file : Separate<["-"], "order_file">,
|
||||||
MetaVarName<"<file>">,
|
MetaVarName<"<file>">,
|
||||||
HelpText<"Layout functions and data according to specification in <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">,
|
HelpText<"Set the MH_SETUID_SAFE bit in the mach-o header">,
|
||||||
Flags<[HelpHidden]>,
|
Flags<[HelpHidden]>,
|
||||||
Group<grp_rare>;
|
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">,
|
def multi_module : Flag<["-"], "multi_module">,
|
||||||
Alias<interposable>,
|
Alias<interposable>,
|
||||||
HelpText<"Alias for -interposable">,
|
HelpText<"Alias for -interposable">,
|
||||||
|
|||||||
@@ -203,9 +203,9 @@ Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// With -flat_namespace, all extern symbols in dylibs are interposable.
|
// With -flat_namespace, all extern symbols in dylibs are interposable.
|
||||||
// FIXME: Add support for `-interposable` (PR53680).
|
bool interposable = ((config->namespaceKind == NamespaceKind::flat &&
|
||||||
bool interposable = config->namespaceKind == NamespaceKind::flat &&
|
config->outputType != MachO::MH_EXECUTE) ||
|
||||||
config->outputType != MachO::MH_EXECUTE &&
|
config->interposable) &&
|
||||||
!isPrivateExtern;
|
!isPrivateExtern;
|
||||||
Defined *defined = replaceSymbol<Defined>(
|
Defined *defined = replaceSymbol<Defined>(
|
||||||
s, name, file, isec, value, size, isWeakDef, /*isExternal=*/true,
|
s, name, file, isec, value, size, isWeakDef, /*isExternal=*/true,
|
||||||
|
|||||||
34
lld/test/MachO/interposable.s
Normal file
34
lld/test/MachO/interposable.s
Normal 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
|
||||||
Reference in New Issue
Block a user