Skip to content

Commit

Permalink
[lld/mac] Handle symbols from -U in treatUndefinedSymbol()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nico committed Jul 22, 2021
1 parent 681107e commit 2d6fb62
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lld/MachO/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/Support/CachePruning.h"
#include "llvm/Support/GlobPattern.h"
Expand Down Expand Up @@ -158,6 +159,7 @@ struct Configuration {
std::vector<llvm::StringRef> runtimePaths;
std::vector<std::string> astPaths;
std::vector<Symbol *> explicitUndefineds;
llvm::StringSet<> explicitDynamicLookups;
// There are typically few custom sectionAlignments or segmentProtections,
// so use a vector instead of a map.
std::vector<SectionAlign> sectionAlignments;
Expand Down
2 changes: 1 addition & 1 deletion lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
}

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

config->mapFile = args.getLastArgValue(OPT_map);
config->optimize = args::getInteger(args, OPT_O, 1);
Expand Down
7 changes: 7 additions & 0 deletions lld/MachO/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ Defined *SymbolTable::addSynthetic(StringRef name, InputSection *isec,
}

void lld::macho::treatUndefinedSymbol(const Undefined &sym, StringRef source) {
// Handle -U.
if (config->explicitDynamicLookups.count(sym.getName())) {
symtab->addDynamicLookup(sym.getName());
return;
}

// Handle -undefined.
auto message = [source, &sym]() {
std::string message = "undefined symbol";
if (config->archMultiple)
Expand Down
4 changes: 2 additions & 2 deletions lld/test/MachO/dead-strip.s
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@
# STRIPDYLIB-NEXT: l {{.*}} __dyld_private
# STRIPDYLIB-NEXT: g {{.*}} _main
# STRIPDYLIB-NEXT: g {{.*}} __mh_execute_header
# STRIPDYLIB-NEXT: *UND* _ref_undef_fun
# STRIPDYLIB-NEXT: *UND* dyld_stub_binder
# STRIPDYLIB-NEXT: *UND* _ref_dylib_fun
# STRIPDYLIB-NEXT: *UND* _ref_undef_fun
# STRIPDYLIB: Bind table:
# STRIPDYLIB: Lazy bind table:
# STRIPDYLIB: __DATA __la_symbol_ptr {{.*}} flat-namespace _ref_undef_fun
Expand Down Expand Up @@ -202,9 +202,9 @@
# LIVESUPP-NEXT: g {{.*}} _bar
# LIVESUPP-NEXT: g {{.*}} _foo
# LIVESUPP-NEXT: g {{.*}} __mh_execute_header
# LIVESUPP-NEXT: *UND* _ref_undef_fun
# LIVESUPP-NEXT: *UND* dyld_stub_binder
# LIVESUPP-NEXT: *UND* _ref_dylib_fun
# LIVESUPP-NEXT: *UND* _ref_undef_fun

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

0 comments on commit 2d6fb62

Please sign in to comment.