diff --git a/lld/MachO/Config.h b/lld/MachO/Config.h --- a/lld/MachO/Config.h +++ b/lld/MachO/Config.h @@ -73,7 +73,7 @@ }; struct Configuration { - Symbol *entry; + Symbol *entry = nullptr; bool hasReexports = false; bool allLoad = false; bool forceLoadObjC = false; diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -927,9 +927,6 @@ if (!get_threadpool_strategy(config->thinLTOJobs)) error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs); - config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"), - /*file=*/nullptr, - /*isWeakRef=*/false); for (const Arg *arg : args.filtered(OPT_u)) { config->explicitUndefineds.push_back(symtab->addUndefined( arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false)); @@ -1002,6 +999,11 @@ config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args); + if (config->outputType == MH_EXECUTE) + config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"), + /*file=*/nullptr, + /*isWeakRef=*/false); + config->librarySearchPaths = getLibrarySearchPaths(args, config->systemLibraryRoots); config->frameworkSearchPaths = diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -534,6 +534,8 @@ sym->stubsIndex * target->wordSize); } } + } else { + assert(false && "invalid symbol type for branch"); } } @@ -1047,7 +1049,8 @@ } template void Writer::run() { - prepareBranchTarget(config->entry); + if (config->entry) + prepareBranchTarget(config->entry); scanRelocations(); if (in.stubHelper->isNeeded()) in.stubHelper->setup(); diff --git a/lld/test/MachO/bundle-loader.s b/lld/test/MachO/bundle-loader.s --- a/lld/test/MachO/bundle-loader.s +++ b/lld/test/MachO/bundle-loader.s @@ -5,23 +5,19 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/3.s -o %t/3.o # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/main.s -o %t/main.o -# RUN: %lld -lSystem -dylib -install_name %t/my_lib.dylib -o %t/mylib.dylib %t/2.o -# RUN: %lld %t/2.o %t/main.o -o %t/main +# RUN: %lld -lSystem -dylib -install_name %t/my_lib.dylib -o %t/mylib.dylib %t/2.o +# RUN: %lld -lSystem %t/2.o %t/main.o -o %t/main # RUN: %lld -lSystem -bundle -bundle_loader %t/main -o %t/bundle.bundle %t/3.o %t/mylib.dylib -# Check bundle.bundle to ensure the `my_func` symbol is from executable +## Check bundle.bundle to ensure the `my_func` symbol is from executable # RUN: llvm-nm -m %t/bundle.bundle | FileCheck %s --check-prefix BUNDLE -# BUNDLE: (undefined) external _main (from executable) # BUNDLE: (undefined) external my_func (from executable) # RUN: llvm-objdump --macho --lazy-bind %t/bundle.bundle | FileCheck %s --check-prefix BUNDLE-OBJ # BUNDLE-OBJ: segment section address dylib symbol # BUNDLE-OBJ: __DATA __la_symbol_ptr 0x{{[0-9a-f]*}} main-executable my_fun - # RUN: %lld -lSystem -bundle -bundle_loader %t/main -o %t/bundle2.bundle %t/3.o %t/2.o -# Check bundle2.bundle to ensure that _main is still from executable -# but my_func is not. +## Check bundle.bundle to ensure the `my_func` symbol is not from executable # RUN: llvm-nm -m %t/bundle2.bundle | FileCheck %s --check-prefix BUNDLE2 -# BUNDLE2: (undefined) external _main (from executable) # BUNDLE2: (__TEXT,__text) external my_func # Test that bundle_loader can only be used with MachO bundle output. 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 @@ -4,10 +4,10 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libfoo.s -o %t/libfoo.o # RUN: %lld -lSystem -dylib %t/libfoo.o -o %t/libfoo.dylib -# RUN: %lld -o %t/not-main %t/not-main.o -e _not_main +# RUN: %lld -lSystem -o %t/not-main %t/not-main.o -e _not_main # RUN: llvm-objdump --macho --all-headers --syms %t/not-main | FileCheck %s # CHECK-LABEL: SYMBOL TABLE -# CHECK-NEXT: {{0*}}[[#%x, ENTRY_ADDR:]] {{.*}} __TEXT,__text _not_main +# CHECK: {{0*}}[[#%x, ENTRY_ADDR:]] {{.*}} __TEXT,__text _not_main # CHECK: sectname __text # CHECK-NEXT: segname __TEXT ## Note: the following checks assume that the entry symbol is right at the @@ -46,6 +46,10 @@ # WEAK-DYSYM-NEXT: segment section address type addend symbol # WEAK-DYSYM-NEXT: __DATA __la_symbol_ptr {{.*}} pointer 0 _weak_dysym_main +# RUN: %lld -dylib -lSystem -o %t/dysym-main.dylib %t/not-main.o %t/libfoo.dylib -e _dysym_main +# RUN: llvm-objdump --macho --indirect-symbols --lazy-bind %t/dysym-main.dylib | FileCheck %s --check-prefix=DYLIB-NO-MAIN +# DYLIB-NO-MAIN-NOT: _dysym_main + # RUN: not %lld -o /dev/null %t/not-main.o -e _missing 2>&1 | FileCheck %s --check-prefix=UNDEFINED # UNDEFINED: error: undefined symbol: _missing # RUN: not %lld -o /dev/null %t/not-main.o 2>&1 | FileCheck %s --check-prefix=DEFAULT-ENTRY