Skip to content

Commit da84a7d

Browse files
authored
[lld][MachO] Support for -interposable (#131813)
As discussed in #53680, add support for ld64's -interposable flag on Apple platforms to lld.
1 parent b0bb86e commit da84a7d

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

lld/MachO/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ struct Configuration {
183183
bool deadStripDylibs = false;
184184
bool demangle = false;
185185
bool deadStrip = false;
186+
bool interposable = false;
186187
bool errorForArchMismatch = false;
187188
bool ignoreAutoLink = false;
188189
// ld64 allows invalid auto link options as long as the link succeeds. LLD

lld/MachO/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
16761676

16771677
// Must be set before any InputSections and Symbols are created.
16781678
config->deadStrip = args.hasArg(OPT_dead_strip);
1679+
config->interposable = args.hasArg(OPT_interposable);
16791680

16801681
config->systemLibraryRoots = getSystemLibraryRoots(args);
16811682
if (const char *path = getReproduceOption(args)) {

lld/MachO/Options.td

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ def grp_opts : OptionGroup<"opts">, HelpText<"OPTIMIZATIONS">;
393393
def dead_strip : Flag<["-"], "dead_strip">,
394394
HelpText<"Remove unreachable functions and data">,
395395
Group<grp_opts>;
396+
def interposable : Flag<["-"], "interposable">,
397+
HelpText<"Indirects access to all exported symbols in an image">,
398+
Group<grp_opts>;
396399
def order_file : Separate<["-"], "order_file">,
397400
MetaVarName<"<file>">,
398401
HelpText<"Layout functions and data according to specification in <file>">,
@@ -876,10 +879,6 @@ def setuid_safe : Flag<["-"], "setuid_safe">,
876879
HelpText<"Set the MH_SETUID_SAFE bit in the mach-o header">,
877880
Flags<[HelpHidden]>,
878881
Group<grp_rare>;
879-
def interposable : Flag<["-"], "interposable">,
880-
HelpText<"Indirects access to all to exported symbols in a dylib">,
881-
Flags<[HelpHidden]>,
882-
Group<grp_rare>;
883882
def multi_module : Flag<["-"], "multi_module">,
884883
Alias<interposable>,
885884
HelpText<"Alias for -interposable">,

lld/MachO/SymbolTable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
203203
}
204204

205205
// With -flat_namespace, all extern symbols in dylibs are interposable.
206-
// FIXME: Add support for `-interposable` (PR53680).
207-
bool interposable = config->namespaceKind == NamespaceKind::flat &&
208-
config->outputType != MachO::MH_EXECUTE &&
206+
bool interposable = ((config->namespaceKind == NamespaceKind::flat &&
207+
config->outputType != MachO::MH_EXECUTE) ||
208+
config->interposable) &&
209209
!isPrivateExtern;
210210
Defined *defined = replaceSymbol<Defined>(
211211
s, name, file, isec, value, size, isWeakDef, /*isExternal=*/true,

lld/test/MachO/interposable.s

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# REQUIRES: x86
2+
3+
# RUN: rm -rf %t; split-file %s %t
4+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/2.s -o %t/2.o
5+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/3.s -o %t/3.o
6+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/main.s -o %t/main.o
7+
8+
# RUN: %lld -arch x86_64 -interposable -lSystem -o %t/main %t/main.o %t/2.o %t/3.o
9+
# RUN: llvm-objdump --macho -d %t/main | FileCheck %s --check-prefix BUNDLE-OBJ
10+
BUNDLE-OBJ-LABEL: _my_user:
11+
BUNDLE-OBJ-NEXT: callq [[#%#x,]] ## symbol stub for: _my_friend
12+
13+
#--- 2.s
14+
# my_lib: This contains the exported function
15+
.globl _my_friend
16+
_my_friend:
17+
retq
18+
19+
#--- 3.s
20+
# _my_user.s: This is the user/caller of the
21+
# exported function
22+
.text
23+
_my_user:
24+
callq _my_friend()
25+
retq
26+
27+
#--- main.s
28+
# main.s: dummy exec/main loads the exported function.
29+
# This is basically a way to say `my_user` should get
30+
# `my_func` from this executable.
31+
.globl _main
32+
.text
33+
_main:
34+
retq

0 commit comments

Comments
 (0)