diff --git a/lld/MachO/Symbols.h b/lld/MachO/Symbols.h --- a/lld/MachO/Symbols.h +++ b/lld/MachO/Symbols.h @@ -306,8 +306,10 @@ "Not a Symbol"); bool isUsedInRegularObj = s->isUsedInRegularObj; + bool used = s->used; T *sym = new (s) T(std::forward(arg)...); sym->isUsedInRegularObj |= isUsedInRegularObj; + sym->used |= used; return sym; } diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -611,6 +611,7 @@ static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec, const Reloc &r) { + assert(sym->isLive()); const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type); if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) { diff --git a/lld/test/MachO/dead-strip.s b/lld/test/MachO/dead-strip.s --- a/lld/test/MachO/dead-strip.s +++ b/lld/test/MachO/dead-strip.s @@ -173,6 +173,18 @@ # RUN: %lld -lSystem -dead_strip %t/strip-dylib-ref.o %t/dylib.dylib \ # RUN: -o %t/strip-dylib-ref -U _ref_undef_fun +## Check that referenced undefs are kept with -undefined dynamic_lookup. +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \ +# RUN: %t/ref-undef.s -o %t/ref-undef.o +# RUN: %lld -lSystem -dead_strip %t/ref-undef.o \ +# RUN: -o %t/ref-undef -undefined dynamic_lookup +# RUN: llvm-objdump --syms --lazy-bind %t/ref-undef | \ +# RUN: FileCheck --check-prefix=STRIPDYNLOOKUP %s +# STRIPDYNLOOKUP: SYMBOL TABLE: +# STRIPDYNLOOKUP: *UND* _ref_undef_fun +# STRIPDYNLOOKUP: Lazy bind table: +# STRIPDYNLOOKUP: __DATA __la_symbol_ptr {{.*}} flat-namespace _ref_undef_fun + ## S_ATTR_LIVE_SUPPORT tests. # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \ # RUN: %t/live-support.s -o %t/live-support.o @@ -812,3 +824,9 @@ .quad L._bar4 .subsections_via_symbols + +#--- ref-undef.s +.globl _main +_main: + callq _ref_undef_fun +.subsections_via_symbols