Skip to content

Commit

Permalink
lld: improve the -arch handling for MachO
Browse files Browse the repository at this point in the history
Use the default target triple configured by the user to determine the
default architecture for `ld64.lld`.  Stash the architecture in the
configuration as when linking against TBDs, we will need to filter out
the symbols based upon the architecture.  Treat the Haswell slice as it
is equivalent to `x86_64` but with the extra Haswell extensions (e.g.
AVX2, FMA3, BMI1, etc).  This will make it easier to add new
architectures in the future.

This change also changes the failure mode where an invalid `-arch`
parameter will result in the linker exiting without further processing.
  • Loading branch information
compnerd committed Jun 8, 2020
1 parent 22c2dc5 commit fcdf757
Show file tree
Hide file tree
Showing 40 changed files with 109 additions and 100 deletions.
2 changes: 2 additions & 0 deletions lld/MachO/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/TextAPI/MachO/Architecture.h"

#include <vector>

Expand All @@ -26,6 +27,7 @@ struct Configuration {
bool hasReexports = false;
llvm::StringRef installName;
llvm::StringRef outputFile;
llvm::MachO::Architecture arch;
llvm::MachO::HeaderFileType outputType;
std::vector<llvm::StringRef> searchPaths;
llvm::DenseMap<llvm::StringRef, SymbolPriorityEntry> priorities;
Expand Down
15 changes: 11 additions & 4 deletions lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Config/config.h"
#include "llvm/Object/Archive.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
Expand Down Expand Up @@ -90,10 +91,16 @@ static Optional<std::string> findLibrary(StringRef name) {
}

static TargetInfo *createTargetInfo(opt::InputArgList &args) {
StringRef s = args.getLastArgValue(OPT_arch, "x86_64");
if (s != "x86_64")
error("missing or unsupported -arch " + s);
return createX86_64TargetInfo();
StringRef arch = llvm::Triple(LLVM_DEFAULT_TARGET_TRIPLE).getArchName();
config->arch = llvm::MachO::getArchitectureFromName(
args.getLastArgValue(OPT_arch, arch));
switch (config->arch) {
case llvm::MachO::AK_x86_64:
case llvm::MachO::AK_x86_64h:
return createX86_64TargetInfo();
default:
fatal("missing or unsupported -arch " + args.getLastArgValue(OPT_arch));
}
}

static std::vector<StringRef> getSearchPaths(opt::InputArgList &args) {
Expand Down
4 changes: 2 additions & 2 deletions lld/test/MachO/archive.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# RUN: rm -f %t/test.a
# RUN: llvm-ar rcs %t/test.a %t/2.o %t/3.o %t/4.o
# RUN: lld -flavor darwinnew %t/main.o %t/test.a -o %t/test.out
# RUN: lld -flavor darwinnew -arch x86_64 %t/main.o %t/test.a -o %t/test.out

## TODO: Run llvm-nm -p to validate symbol order
# RUN: llvm-nm %t/test.out | FileCheck %s
Expand All @@ -16,7 +16,7 @@
# CHECK: T _main

## Linking with the archive first in the command line shouldn't change anything
# RUN: lld -flavor darwinnew %t/test.a %t/main.o -o %t/test.out
# RUN: lld -flavor darwinnew -arch x86_64 %t/test.a %t/main.o -o %t/test.out
# RUN: llvm-nm %t/test.out | FileCheck %s --check-prefix ARCHIVE-FIRST
# ARCHIVE-FIRST: T _bar
# ARCHIVE-FIRST: T _boo
Expand Down
4 changes: 2 additions & 2 deletions lld/test/MachO/dylib.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o

# RUN: lld -flavor darwinnew -dylib -install_name @executable_path/libfoo.dylib \
# RUN: lld -flavor darwinnew -arch x86_64 -dylib -install_name @executable_path/libfoo.dylib \
# RUN: %t.o -o %t.dylib
# RUN: llvm-objdump --macho --dylib-id %t.dylib | FileCheck %s
# CHECK: @executable_path/libfoo.dylib
Expand All @@ -10,7 +10,7 @@
## a flag for a missing entry symbol (since dylibs don't have entry symbols).
## Also check that we come up with the right install name if one isn't
## specified.
# RUN: lld -flavor darwinnew -dylib %t.o -o %t.defaultInstallName.dylib -e missing_entry
# RUN: lld -flavor darwinnew -arch x86_64 -dylib %t.o -o %t.defaultInstallName.dylib -e missing_entry
# RUN: obj2yaml %t.defaultInstallName.dylib | FileCheck %s -DOUTPUT=%t.defaultInstallName.dylib --check-prefix=DEFAULT-INSTALL-NAME
# DEFAULT-INSTALL-NAME: [[OUTPUT]]

Expand Down
6 changes: 3 additions & 3 deletions lld/test/MachO/dylink-lazy.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
# RUN: -o %t/libhello.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %p/Inputs/libgoodbye.s \
# RUN: -o %t/libgoodbye.o
# RUN: lld -flavor darwinnew -dylib -install_name \
# RUN: lld -flavor darwinnew -arch x86_64 -dylib -install_name \
# RUN: @executable_path/libhello.dylib %t/libhello.o -o %t/libhello.dylib
# RUN: lld -flavor darwinnew -dylib -install_name \
# RUN: lld -flavor darwinnew -arch x86_64 -dylib -install_name \
# RUN: @executable_path/libgoodbye.dylib %t/libgoodbye.o -o %t/libgoodbye.dylib

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/dylink-lazy.o
# RUN: lld -flavor darwinnew -o %t/dylink-lazy -L%t -lhello -lgoodbye %t/dylink-lazy.o
# RUN: lld -flavor darwinnew -arch x86_64 -o %t/dylink-lazy -L%t -lhello -lgoodbye %t/dylink-lazy.o

## When looking at the __stubs section alone, we are unable to easily tell which
## symbol each entry points to. So we call objdump twice in order to get the
Expand Down
6 changes: 3 additions & 3 deletions lld/test/MachO/dylink.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# RUN: -o %t/libhello.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %p/Inputs/libgoodbye.s \
# RUN: -o %t/libgoodbye.o
# RUN: lld -flavor darwinnew -dylib -install_name \
# RUN: lld -flavor darwinnew -arch x86_64 -dylib -install_name \
# RUN: @executable_path/libhello.dylib %t/libhello.o -o %t/libhello.dylib
# RUN: lld -flavor darwinnew -dylib -install_name \
# RUN: lld -flavor darwinnew -arch x86_64 -dylib -install_name \
# RUN: @executable_path/libgoodbye.dylib %t/libgoodbye.o -o %t/libgoodbye.dylib

## Make sure we are using the export trie and not the symbol table when linking
Expand All @@ -18,7 +18,7 @@
# NOSYM: no symbols

# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/dylink.o
# RUN: lld -flavor darwinnew -o %t/dylink -Z -L%t -lhello -lgoodbye %t/dylink.o
# RUN: lld -flavor darwinnew -arch x86_64 -o %t/dylink -Z -L%t -lhello -lgoodbye %t/dylink.o
# RUN: llvm-objdump --bind -d %t/dylink | FileCheck %s

# CHECK: movq [[#%u, HELLO_OFF:]](%rip), %rsi
Expand Down
6 changes: 3 additions & 3 deletions lld/test/MachO/entry-symbol.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: lld -flavor darwinnew -o %t %t.o -e _not_main
# RUN: lld -flavor darwinnew -arch x86_64 -o %t %t.o -e _not_main
# RUN: llvm-objdump --macho --all-headers --syms %t | FileCheck %s
# CHECK-LABEL: SYMBOL TABLE
# CHECK-NEXT: {{0*}}[[#%x, ENTRY_ADDR:]] {{.*}} __TEXT,__text _not_main
Expand All @@ -16,9 +16,9 @@
# CHECK-NEXT: offset [[#ENTRYOFF]]


# RUN: not lld -flavor darwinnew -o /dev/null %t.o -e _missing 2>&1 | FileCheck %s --check-prefix=UNDEFINED
# RUN: not lld -flavor darwinnew -arch x86_64 -o /dev/null %t.o -e _missing 2>&1 | FileCheck %s --check-prefix=UNDEFINED
# UNDEFINED: error: undefined symbol: _missing
# RUN: not lld -flavor darwinnew -o /dev/null %t.o 2>&1 | FileCheck %s --check-prefix=DEFAULT-ENTRY
# RUN: not lld -flavor darwinnew -arch x86_64 -o /dev/null %t.o 2>&1 | FileCheck %s --check-prefix=DEFAULT-ENTRY
# DEFAULT-ENTRY: error: undefined symbol: _main

.text
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/export-trie.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: lld -flavor darwinnew -dylib %t.o -o %t.dylib
# RUN: lld -flavor darwinnew -arch x86_64 -dylib %t.o -o %t.dylib

# RUN: llvm-objdump --syms --exports-trie %t.dylib | \
# RUN: FileCheck %s --check-prefix=EXPORTS
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/alignment-too-large.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# RUN: yaml2obj %s -o %t.o
# RUN: not lld -flavor darwinnew -o %t %t.o 2>&1 | FileCheck %s
# RUN: not lld -flavor darwinnew -arch x86_64 -o %t %t.o 2>&1 | FileCheck %s
#
# CHECK: error: alignment 32 of section __text is too large
--- !mach-o
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/archive-no-index.s
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# RUN: rm -f %t/test.a
# RUN: llvm-ar rcS %t/test.a %t/2.o %t/3.o %t/4.o

# RUN: not lld -flavor darwinnew %t/test.o %t/test.a -o /dev/null 2>&1 | FileCheck %s
# RUN: not lld -flavor darwinnew -arch x86_64 %t/test.o %t/test.a -o /dev/null 2>&1 | FileCheck %s
# CHECK: error: {{.*}}.a: archive has no index; run ranlib to add one

.global _main
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/bad-archive.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# RUN: echo "foo" >> %t.a
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o

# RUN: not lld -flavor darwinnew %t.o %t.a -o /dev/null 2>&1 | FileCheck -DFILE=%t.a %s
# RUN: not lld -flavor darwinnew -arch x86_64 %t.o %t.a -o /dev/null 2>&1 | FileCheck -DFILE=%t.a %s
# CHECK: error: [[FILE]]: failed to parse archive: truncated or malformed archive (remaining size of archive too small for next archive member header at offset 8)

.global _main
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/duplicate-symbol.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t-dup.o
# RUN: not lld -flavor darwinnew -o /dev/null %t-dup.o %t.o 2>&1 | FileCheck %s
# RUN: not lld -flavor darwinnew -arch x86_64 -o /dev/null %t-dup.o %t.o 2>&1 | FileCheck %s

# CHECK: error: duplicate symbol: _main

Expand Down
4 changes: 2 additions & 2 deletions lld/test/MachO/invalid/invalid-executable.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-darwin %s -o %t.o
# RUN: lld -flavor darwinnew -o %t %t.o
# RUN: not lld -flavor darwinnew -o /dev/null %t 2>&1 | FileCheck %s -DFILE=%t
# RUN: lld -flavor darwinnew -arch x86_64 -o %t %t.o
# RUN: not lld -flavor darwinnew -arch x86_64 -o /dev/null %t 2>&1 | FileCheck %s -DFILE=%t
# CHECK: error: [[FILE]]: unhandled file type

.text
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/invalid-relocation.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: yaml2obj %s -o %t.o
# RUN: not lld -flavor darwinnew -o %t %t.o 2>&1 | FileCheck %s -DFILE=%t.o
# RUN: not lld -flavor darwinnew -arch x86_64 -o %t %t.o 2>&1 | FileCheck %s -DFILE=%t.o
#
# CHECK: error: invalid relocation at offset 1 of __TEXT,__text in [[FILE]]: relocations of type 0 must not be pcrel

Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/missing-dylib.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: not lld -flavor darwinnew -Z -o %t -lmissing %t.o 2>&1 | FileCheck %s
# RUN: not lld -flavor darwinnew -arch x86_64 -Z -o %t -lmissing %t.o 2>&1 | FileCheck %s

# CHECK: error: library not found for -lmissing
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/no-id-dylink.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# RUN: mkdir -p %t
# RUN: yaml2obj %s -o %t/libnoid.dylib
# RUN: echo ".globl _main; .text; _main: ret" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t/no-id-dylink.o
# RUN: not lld -flavor darwinnew -o %t/no-id-dylink -Z -L%t -lnoid %t/no-id-dylink.o 2>&1 | FileCheck %s
# RUN: not lld -flavor darwinnew -arch x86_64 -o %t/no-id-dylink -Z -L%t -lnoid %t/no-id-dylink.o 2>&1 | FileCheck %s
# CHECK: error: dylib {{.*}}libnoid.dylib missing LC_ID_DYLIB load command

## This YAML file was originally generated from linking the following source
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/no-such-file.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# REQUIRES: x86
# RUN: not lld -flavor darwinnew -o /dev/null %t-no-such-file.o 2>&1 | FileCheck %s
# RUN: not lld -flavor darwinnew -arch x86_64 -o /dev/null %t-no-such-file.o 2>&1 | FileCheck %s

# CHECK: error: cannot open {{.*}}no-such-file.o
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/order-file-bad-arch.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: echo ".globl _main; .text; _main: ret" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t.o
# RUN: not lld -flavor darwinnew -o %t %t.o -order_file %s 2>&1 | FileCheck %s
# RUN: not lld -flavor darwinnew -arch x86_64 -o %t %t.o -order_file %s 2>&1 | FileCheck %s
# CHECK: error: invalid arch "sparc" in order file: expected one of arm, arm64, i386, x86_64, ppc, ppc64
# CHECK-EMPTY:

Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/order-file-bad-objfile.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: echo ".globl _main; .text; _main: ret" | llvm-mc -filetype=obj -triple=x86_64-apple-darwin -o %t.o
# RUN: not lld -flavor darwinnew -o %t %t.o -order_file %s 2>&1 | FileCheck %s
# RUN: not lld -flavor darwinnew -arch x86_64 -o %t %t.o -order_file %s 2>&1 | FileCheck %s
# CHECK: invalid object file name "helloo" in order file: should end with .o
# CHECK: invalid object file name "z80" in order file: should end with .o
# CHECK-EMPTY:
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/invalid/undefined-symbol.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: not lld -flavor darwinnew -Z -o %t %t.o 2>&1 | FileCheck %s -DBASENAME=%basename_t
# RUN: not lld -flavor darwinnew -arch x86_64 -Z -o %t %t.o 2>&1 | FileCheck %s -DBASENAME=%basename_t
# CHECK: error: undefined symbol _foo, referenced from [[BASENAME]]

.globl _main
Expand Down
6 changes: 3 additions & 3 deletions lld/test/MachO/link-search-order.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
# RUN: mkdir -p %t
#
# RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %p/Inputs/libhello.s -o %t/hello.o
# RUN: lld -flavor darwinnew -dylib -install_name @executable_path/libhello.dylib %t/hello.o -o %t/libhello.dylib
# RUN: lld -flavor darwinnew -arch x86_64 -dylib -install_name @executable_path/libhello.dylib %t/hello.o -o %t/libhello.dylib
#
# RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %p/Inputs/libgoodbye.s -o %t/goodbye.o
# RUN: lld -flavor darwinnew -dylib -install_name @executable_path/libgoodbye.dylib %t/goodbye.o -o %t/libgoodbye.dylib
# RUN: lld -flavor darwinnew -arch x86_64 -dylib -install_name @executable_path/libgoodbye.dylib %t/goodbye.o -o %t/libgoodbye.dylib
# RUN: llvm-ar --format=darwin crs %t/libgoodbye.a %t/goodbye.o
#
# RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %s -o %t/test.o
# RUN: lld -flavor darwinnew -o %t/test -Z -L%t -lhello -lgoodbye %t/test.o
# RUN: lld -flavor darwinnew -arch x86_64 -o %t/test -Z -L%t -lhello -lgoodbye %t/test.o
#
# RUN: llvm-objdump --macho --dylibs-used %t/test | FileCheck %s

Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/load-commands.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: lld -flavor darwinnew -o %t %t.o
# RUN: lld -flavor darwinnew -arch x86_64 -o %t %t.o

## Check for the presence of load commands that are essential for a working
## executable.
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/no-exports-dylib.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: lld -flavor darwinnew -dylib %t.o -o %t.dylib
# RUN: lld -flavor darwinnew -arch x86_64 -dylib %t.o -o %t.dylib

# RUN: obj2yaml %t.dylib | FileCheck %s
# CHECK: export_size: 0
Loading

0 comments on commit fcdf757

Please sign in to comment.