Skip to content

Commit

Permalink
[lld-macho] Don't reference entry symbol for non-executables
Browse files Browse the repository at this point in the history
This would cause us to pull in symbols (and code) that should
be unused.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D102137
  • Loading branch information
int3 committed May 10, 2021
1 parent 78e9491 commit 0f8854f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lld/MachO/Config.h
Expand Up @@ -73,7 +73,7 @@ class SymbolPatterns {
};

struct Configuration {
Symbol *entry;
Symbol *entry = nullptr;
bool hasReexports = false;
bool allLoad = false;
bool forceLoadObjC = false;
Expand Down
8 changes: 5 additions & 3 deletions lld/MachO/Driver.cpp
Expand Up @@ -927,9 +927,6 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
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));
Expand Down Expand Up @@ -1002,6 +999,11 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,

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 =
Expand Down
5 changes: 4 additions & 1 deletion lld/MachO/Writer.cpp
Expand Up @@ -534,6 +534,8 @@ static void prepareBranchTarget(Symbol *sym) {
sym->stubsIndex * target->wordSize);
}
}
} else {
assert(false && "invalid symbol type for branch");
}
}

Expand Down Expand Up @@ -1053,7 +1055,8 @@ void Writer::writeOutputFile() {
}

template <class LP> void Writer::run() {
prepareBranchTarget(config->entry);
if (config->entry)
prepareBranchTarget(config->entry);
scanRelocations();
if (in.stubHelper->isNeeded())
in.stubHelper->setup();
Expand Down
14 changes: 5 additions & 9 deletions lld/test/MachO/bundle-loader.s
Expand Up @@ -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
# RUN: llvm-nm -m %t/bundle.bundle | FileCheck %s --check-prefix BUNDLE
# BUNDLE: (undefined) external _main (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 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.
Expand Down
10 changes: 8 additions & 2 deletions lld/test/MachO/entry-symbol.s
Expand Up @@ -4,10 +4,12 @@
# 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
Expand Down Expand Up @@ -46,6 +48,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
Expand Down

0 comments on commit 0f8854f

Please sign in to comment.