Skip to content

Commit 2d6fb62

Browse files
committed
[lld/mac] Handle symbols from -U in treatUndefinedSymbol()
In ld64, `-U section$start$FOO$bar` handles `section$start$FOO$bar` as a regular `section$start` symbol, that is section$start processing happens before -U processing. Likely, nobody uses that in practice so it doesn't seem very important to be compatible with this, but it also moves the -U handling code next to the `-undefined dynamic_lookup` handling code, which is nice because they do the same thing. And, in fact, this did identify a bug in a corner case in the intersection of `-undefined dynamic_lookup` and dead-stripping (fix for that in D106565). Vaguely related to PR50760. No interesting behavior change. Differential Revision: https://reviews.llvm.org/D106566
1 parent 681107e commit 2d6fb62

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

lld/MachO/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/ADT/DenseMap.h"
1414
#include "llvm/ADT/DenseSet.h"
1515
#include "llvm/ADT/StringRef.h"
16+
#include "llvm/ADT/StringSet.h"
1617
#include "llvm/BinaryFormat/MachO.h"
1718
#include "llvm/Support/CachePruning.h"
1819
#include "llvm/Support/GlobPattern.h"
@@ -158,6 +159,7 @@ struct Configuration {
158159
std::vector<llvm::StringRef> runtimePaths;
159160
std::vector<std::string> astPaths;
160161
std::vector<Symbol *> explicitUndefineds;
162+
llvm::StringSet<> explicitDynamicLookups;
161163
// There are typically few custom sectionAlignments or segmentProtections,
162164
// so use a vector instead of a map.
163165
std::vector<SectionAlign> sectionAlignments;

lld/MachO/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
11751175
}
11761176

11771177
for (const Arg *arg : args.filtered(OPT_U))
1178-
symtab->addDynamicLookup(arg->getValue());
1178+
config->explicitDynamicLookups.insert(arg->getValue());
11791179

11801180
config->mapFile = args.getLastArgValue(OPT_map);
11811181
config->optimize = args::getInteger(args, OPT_O, 1);

lld/MachO/SymbolTable.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ Defined *SymbolTable::addSynthetic(StringRef name, InputSection *isec,
197197
}
198198

199199
void lld::macho::treatUndefinedSymbol(const Undefined &sym, StringRef source) {
200+
// Handle -U.
201+
if (config->explicitDynamicLookups.count(sym.getName())) {
202+
symtab->addDynamicLookup(sym.getName());
203+
return;
204+
}
205+
206+
// Handle -undefined.
200207
auto message = [source, &sym]() {
201208
std::string message = "undefined symbol";
202209
if (config->archMultiple)

lld/test/MachO/dead-strip.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@
147147
# STRIPDYLIB-NEXT: l {{.*}} __dyld_private
148148
# STRIPDYLIB-NEXT: g {{.*}} _main
149149
# STRIPDYLIB-NEXT: g {{.*}} __mh_execute_header
150-
# STRIPDYLIB-NEXT: *UND* _ref_undef_fun
151150
# STRIPDYLIB-NEXT: *UND* dyld_stub_binder
152151
# STRIPDYLIB-NEXT: *UND* _ref_dylib_fun
152+
# STRIPDYLIB-NEXT: *UND* _ref_undef_fun
153153
# STRIPDYLIB: Bind table:
154154
# STRIPDYLIB: Lazy bind table:
155155
# STRIPDYLIB: __DATA __la_symbol_ptr {{.*}} flat-namespace _ref_undef_fun
@@ -202,9 +202,9 @@
202202
# LIVESUPP-NEXT: g {{.*}} _bar
203203
# LIVESUPP-NEXT: g {{.*}} _foo
204204
# LIVESUPP-NEXT: g {{.*}} __mh_execute_header
205-
# LIVESUPP-NEXT: *UND* _ref_undef_fun
206205
# LIVESUPP-NEXT: *UND* dyld_stub_binder
207206
# LIVESUPP-NEXT: *UND* _ref_dylib_fun
207+
# LIVESUPP-NEXT: *UND* _ref_undef_fun
208208

209209
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
210210
# RUN: %t/live-support-iterations.s -o %t/live-support-iterations.o

0 commit comments

Comments
 (0)