Skip to content

Commit b63919e

Browse files
committed
[lld-macho] Require -arch and -platform_version to always be specified
We previously defaulted to x86_64 and an unknown platform, which was fine when we only supported one arch and did no platform checks, but that will no longer be true going ahead. Therefore, we should require those flags to be specified whenever the linker is invoked. Note that LLD-ELF and ld64 both infer the arch from their input object files, but the usefulness of that is questionable since clang will always specify these flags, and most of the time `lld` will be invoked via clang. Reviewed By: #lld-macho, thakis Differential Revision: https://reviews.llvm.org/D97799
1 parent 1168736 commit b63919e

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

lld/MachO/Driver.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ static Optional<std::string> findFramework(StringRef name) {
126126
}
127127

128128
static TargetInfo *createTargetInfo(opt::InputArgList &args) {
129-
// TODO: should unspecified arch be an error rather than defaulting?
130-
// Jez: ld64 seems to make unspecified arch an error when LTO is
131-
// being used. I'm not sure why though. Feels like we should be able
132-
// to infer the arch from our input files regardless
133-
StringRef archName = args.getLastArgValue(OPT_arch, "x86_64");
129+
StringRef archName = args.getLastArgValue(OPT_arch);
130+
if (archName.empty())
131+
fatal("must specify -arch");
134132
config->arch = MachO::getArchitectureFromName(archName);
135133
switch (MachO::getCPUTypeFromArchitecture(config->arch).first) {
136134
case MachO::CPU_TYPE_X86_64:
@@ -563,8 +561,10 @@ static std::string lowerDash(StringRef s) {
563561
static PlatformInfo getPlatformVersion(const opt::ArgList &args) {
564562
const opt::Arg *arg = args.getLastArg(OPT_platform_version);
565563
PlatformInfo platform;
566-
if (!arg)
564+
if (!arg) {
565+
error("must specify -platform_version");
567566
return platform;
567+
}
568568

569569
StringRef platformStr = arg->getValue(0);
570570
StringRef minVersionStr = arg->getValue(1);

lld/MachO/InputFiles.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName)
486486
getArchitectureName(config->arch));
487487
return;
488488
}
489+
// TODO: check platform too
489490

490491
if (const load_command *cmd = findCommand(hdr, LC_LINKER_OPTION)) {
491492
auto *c = reinterpret_cast<const linker_option_command *>(cmd);

lld/test/MachO/lit.local.cfg

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import os
44

5-
lld = ('ld64.lld -syslibroot ' +
5+
# We specify the most commonly-used arch and platform version in our tests here
6+
# Tests which need different settings can just append to this, as only the last
7+
# value will be used.
8+
#
9+
# Note however that this does not apply to `-syslibroot`: each instance of that
10+
# flag will append to the set of library roots.
11+
lld = ('ld64.lld -arch x86_64 -platform_version macos 10.0 11.0 -syslibroot ' +
612
os.path.join(config.test_source_root, "MachO", "Inputs", "MacOSX.sdk"))
713
config.substitutions.append(('%lld', lld + ' -fatal_warnings'))
814
config.substitutions.append(('%no_fatal_warnings_lld', lld))

lld/test/MachO/syslibroot.test

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,68 @@
11
# Ensure that a nonexistent path is ignored with a syslibroot
22

3-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot /var/empty | FileCheck %s -check-prefix CHECK-NONEXISTENT-SYSLIBROOT
3+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
4+
RUN: -syslibroot /var/empty | FileCheck %s -check-prefix CHECK-NONEXISTENT-SYSLIBROOT
45

56
CHECK-NONEXISTENT-SYSLIBROOT: Library search paths:
67
CHECK-NONEXISTENT-SYSLIBROOT-NEXT: Framework search paths:
78

89
RUN: mkdir -p %t/usr/lib
9-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot %t 2>&1 | FileCheck %s -check-prefix CHECK-SYSLIBROOT -DROOT=%t
10+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
11+
RUN: -syslibroot %t 2>&1 | FileCheck %s -check-prefix CHECK-SYSLIBROOT -DROOT=%t
1012

1113
CHECK-SYSLIBROOT-NOT: directory not found{{.*}}usr/local/lib
1214
CHECK-SYSLIBROOT: Library search paths:
1315
CHECK-SYSLIBROOT-NEXT: [[ROOT]]/usr/lib
1416

1517
RUN: mkdir -p %t/Library/libxml2-development
16-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot %t -L /Library/libxml2-development | FileCheck %s -check-prefix CHECK-ABSOLUTE-PATH-REROOTED -DROOT=%t
18+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
19+
RUN: -syslibroot %t -L /Library/libxml2-development | FileCheck %s -check-prefix CHECK-ABSOLUTE-PATH-REROOTED -DROOT=%t
1720

1821
CHECK-ABSOLUTE-PATH-REROOTED: Library search paths:
1922
CHECK-ABSOLUTE-PATH-REROOTED: [[ROOT]]/Library/libxml2-development
2023
CHECK-ABSOLUTE-PATH-REROOTED: [[ROOT]]/usr/lib
2124

22-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot %t -L %t/Library/libxml2-development | FileCheck %s -check-prefix CHECK-PATH-WITHOUT-REROOT -DPATH=%t/Library/libxml2-development
25+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
26+
RUN: -syslibroot %t -L %t/Library/libxml2-development | FileCheck %s -check-prefix CHECK-PATH-WITHOUT-REROOT -DPATH=%t/Library/libxml2-development
2327
CHECK-PATH-WITHOUT-REROOT: Library search paths:
2428
CHECK-PATH-WITHOUT-REROOT-NEXT: [[PATH]]
2529

2630
RUN: mkdir -p %t.2/usr/lib
27-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot %t -syslibroot %t.2 | FileCheck %s -check-prefix CHECK-SYSLIBROOT-MATRIX -DROOT=%t
31+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
32+
RUN: -syslibroot %t -syslibroot %t.2 | FileCheck %s -check-prefix CHECK-SYSLIBROOT-MATRIX -DROOT=%t
2833

2934
CHECK-SYSLIBROOT-MATRIX: Library search paths:
3035
CHECK-SYSLIBROOT-MATRIX: [[ROOT]]/usr/lib
3136
CHECK-SYSLIBROOT-MATRIX: [[ROOT]].2/usr/lib
3237

33-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot %t -syslibroot %t.2 -syslibroot / | FileCheck %s -check-prefix CHECK-SYSLIBROOT-IGNORED -DROOT=%t
38+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
39+
RUN: -syslibroot %t -syslibroot %t.2 -syslibroot / | \
40+
RUN: FileCheck %s -check-prefix CHECK-SYSLIBROOT-IGNORED -DROOT=%t
3441

3542
CHECK-SYSLIBROOT-IGNORED: Library search paths:
3643
CHECK-SYSLIBROOT-IGNORED-NOT: [[ROOT]]/usr/lib
3744
CHECK-SYSLIBROOT-IGNORED-NOT: [[ROOT]].2/usr/lib
3845

3946
RUN: mkdir -p %t/System/Library/Frameworks
40-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot %t | FileCheck %s -check-prefix CHECK-SYSLIBROOT-FRAMEWORK -DROOT=%t
47+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
48+
RUN: -syslibroot %t | FileCheck %s -check-prefix CHECK-SYSLIBROOT-FRAMEWORK -DROOT=%t
4149

4250
CHECK-SYSLIBROOT-FRAMEWORK: Framework search paths:
4351
CHECK-SYSLIBROOT-FRAMEWORK: [[ROOT]]/System/Library/Frameworks
4452

4553
RUN: mkdir -p %t/Library/Frameworks
4654
RUN: mkdir -p %t.2/Library/Frameworks
47-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot %t -syslibroot %t.2 -F /Library/Frameworks | FileCheck %s -check-prefix CHECK-SYSLIBROOT-FRAMEWORK-MATRIX -DROOT=%t
55+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
56+
RUN: -syslibroot %t -syslibroot %t.2 -F /Library/Frameworks | \
57+
RUN: FileCheck %s -check-prefix CHECK-SYSLIBROOT-FRAMEWORK-MATRIX -DROOT=%t
4858

4959
CHECK-SYSLIBROOT-FRAMEWORK-MATRIX: Framework search paths:
5060
CHECK-SYSLIBROOT-FRAMEWORK-MATRIX: [[ROOT]]/Library/Frameworks
5161
CHECK-SYSLIBROOT-FRAMEWORK-MATRIX: [[ROOT]].2/Library/Frameworks
5262

53-
RUN: ld64.lld -v -dylib -o /dev/null -syslibroot %t -syslibroot %t.2 -syslibroot / -F /Library/Frameworks | FileCheck %s -check-prefix CHECK-SYSLIBROOT-FRAMEWORK-IGNORED -DROOT=%t
63+
RUN: ld64.lld -arch x86_64 -platform_version macos 10 11 -v -dylib -o /dev/null \
64+
RUN: -syslibroot %t -syslibroot %t.2 -syslibroot / -F /Library/Frameworks | \
65+
RUN: FileCheck %s -check-prefix CHECK-SYSLIBROOT-FRAMEWORK-IGNORED -DROOT=%t
5466

5567
CHECK-SYSLIBROOT-FRAMEWORK-IGNORED: Framework search paths:
5668
CHECK-SYSLIBROOT-FRAMEWORK-IGNORED-NOT: [[ROOT]]/Library/Frameworks

0 commit comments

Comments
 (0)