diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1138,10 +1138,6 @@ if (!orderFile.empty()) parseOrderFile(orderFile); - if (config->outputType == MH_EXECUTE && isa(config->entry)) { - error("undefined symbol: " + toString(*config->entry)); - return false; - } // FIXME: This prints symbols that are undefined both in input files and // via -u flag twice. for (const Symbol *undefined : config->explicitUndefineds) { diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -1049,8 +1049,14 @@ } template void Writer::run() { - if (config->entry) - prepareBranchTarget(config->entry); + if (config->entry) { + if (auto *undefined = dyn_cast(config->entry)) + treatUndefinedSymbol(*undefined); + // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check. + if (!isa(config->entry)) + prepareBranchTarget(config->entry); + } + scanRelocations(); if (in.stubHelper->isNeeded()) in.stubHelper->setup(); diff --git a/lld/test/MachO/entry-symbol.s b/lld/test/MachO/entry-symbol.s --- a/lld/test/MachO/entry-symbol.s +++ b/lld/test/MachO/entry-symbol.s @@ -22,7 +22,11 @@ # CHECK-NEXT: entryoff [[#ENTRYOFF]] # RUN: %lld -lSystem -o %t/dysym-main %t/not-main.o %t/libfoo.dylib -e _dysym_main -# RUN: llvm-objdump --macho --all-headers --indirect-symbols --lazy-bind %t/dysym-main | FileCheck %s --check-prefix=DYSYM +# RUN: llvm-objdump --macho --all-headers --indirect-symbols --lazy-bind %t/dysym-main | FileCheck %s --check-prefix=DYSYM -DDYLIB=libfoo + +# RUN: %lld -lSystem -o %t/dyn-lookup %t/not-main.o -e _dysym_main -undefined dynamic_lookup +# RUN: llvm-objdump --macho --all-headers --indirect-symbols --lazy-bind %t/dyn-lookup | FileCheck %s --check-prefix=DYSYM -DDYLIB=flat-namespace + # DYSYM-LABEL: Indirect symbols for (__TEXT,__stubs) 1 entries # DYSYM-NEXT: address index name # DYSYM-NEXT: 0x[[#%x,DYSYM_ENTRY_ADDR:]] [[#]] _dysym_main @@ -30,8 +34,8 @@ # DYSYM-NEXT: cmdsize 24 # DYSYM-NEXT: entryoff [[#%u, DYSYM_ENTRY_ADDR - 0x100000000]] # DYSYM-LABEL: Lazy bind table: -# DYSYM-NEXT: segment section address dylib symbol -# DYSYM-NEXT: __DATA __la_symbol_ptr {{.*}} libfoo _dysym_main +# DYSYM-NEXT: segment section address dylib symbol +# DYSYM-NEXT: __DATA __la_symbol_ptr {{.*}} [[DYLIB]] _dysym_main # RUN: %lld -lSystem -o %t/weak-dysym-main %t/not-main.o %t/libfoo.dylib -e _weak_dysym_main # RUN: llvm-objdump --macho --all-headers --indirect-symbols --bind --weak-bind %t/weak-dysym-main | FileCheck %s --check-prefix=WEAK-DYSYM @@ -42,8 +46,8 @@ # WEAK-DYSYM-NEXT: cmdsize 24 # WEAK-DYSYM-NEXT: entryoff [[#%u, DYSYM_ENTRY_ADDR - 0x100000000]] # WEAK-DYSYM-LABEL: Bind table: -# WEAK-DYSYM-NEXT: segment section address type addend dylib symbol -# WEAK-DYSYM: __DATA __la_symbol_ptr {{.*}} pointer 0 libfoo _weak_dysym_main +# WEAK-DYSYM-NEXT: segment section address type addend dylib symbol +# WEAK-DYSYM: __DATA __la_symbol_ptr {{.*}} pointer 0 libfoo _weak_dysym_main # WEAK-DYSYM-LABEL: Weak bind table: # WEAK-DYSYM-NEXT: segment section address type addend symbol # WEAK-DYSYM-NEXT: __DATA __la_symbol_ptr {{.*}} pointer 0 _weak_dysym_main