blob: 761face8a5d076f7b0bde24e58d94072a2c05a39 [file] [log] [blame]
Rui Ueyama9f56df82013-06-11 23:07:43 +00001//===- lld/unittest/DarwinLdDriverTest.cpp --------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief Darwin's ld driver tests.
12///
13//===----------------------------------------------------------------------===//
14
15#include "DriverTest.h"
16
Nick Kledzik473933b2013-09-27 22:50:00 +000017#include "llvm/Support/MachO.h"
18
Rui Ueyama0ca149f2013-08-06 22:31:59 +000019#include "lld/ReaderWriter/MachOLinkingContext.h"
Rui Ueyama9f56df82013-06-11 23:07:43 +000020
21using namespace llvm;
22using namespace lld;
23
24namespace {
Rui Ueyama0ca149f2013-08-06 22:31:59 +000025class DarwinLdParserTest
26 : public ParserTest<DarwinLdDriver, MachOLinkingContext> {
Rui Ueyama9f56df82013-06-11 23:07:43 +000027protected:
Rui Ueyama16e543b2014-03-06 21:14:04 +000028 const LinkingContext *linkingContext() override { return &_context; }
Rui Ueyama9f56df82013-06-11 23:07:43 +000029};
Rui Ueyama9024c362014-03-27 23:34:32 +000030}
Rui Ueyama9f56df82013-06-11 23:07:43 +000031
32TEST_F(DarwinLdParserTest, Basic) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000033 EXPECT_TRUE(parse("ld", "foo.o", "bar.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000034 EXPECT_FALSE(_context.allowRemainingUndefines());
35 EXPECT_FALSE(_context.deadStrip());
Nick Kledzik2a709ea2013-07-16 18:45:57 +000036 EXPECT_EQ(2, inputFileCount());
37 EXPECT_EQ("foo.o", inputFile(0));
38 EXPECT_EQ("bar.o", inputFile(1));
Rui Ueyama9f56df82013-06-11 23:07:43 +000039}
40
Nick Kledzika1210532013-07-18 23:13:13 +000041TEST_F(DarwinLdParserTest, Output) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000042 EXPECT_TRUE(parse("ld", "-o", "my.out", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000043 EXPECT_EQ("my.out", _context.outputPath());
Nick Kledzika1210532013-07-18 23:13:13 +000044}
45
Rui Ueyama9f56df82013-06-11 23:07:43 +000046TEST_F(DarwinLdParserTest, Dylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000047 EXPECT_TRUE(parse("ld", "-dylib", "foo.o", nullptr));
Nick Kledzik473933b2013-09-27 22:50:00 +000048 EXPECT_EQ(llvm::MachO::MH_DYLIB, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000049}
50
51TEST_F(DarwinLdParserTest, Relocatable) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000052 EXPECT_TRUE(parse("ld", "-r", "foo.o", nullptr));
Nick Kledzik473933b2013-09-27 22:50:00 +000053 EXPECT_EQ(llvm::MachO::MH_OBJECT, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000054}
55
56TEST_F(DarwinLdParserTest, Bundle) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000057 EXPECT_TRUE(parse("ld", "-bundle", "foo.o", nullptr));
Nick Kledzik473933b2013-09-27 22:50:00 +000058 EXPECT_EQ(llvm::MachO::MH_BUNDLE, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000059}
60
61TEST_F(DarwinLdParserTest, Preload) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000062 EXPECT_TRUE(parse("ld", "-preload", "foo.o", nullptr));
Nick Kledzik473933b2013-09-27 22:50:00 +000063 EXPECT_EQ(llvm::MachO::MH_PRELOAD, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000064}
65
66TEST_F(DarwinLdParserTest, Static) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000067 EXPECT_TRUE(parse("ld", "-static", "foo.o", nullptr));
Nick Kledzik473933b2013-09-27 22:50:00 +000068 EXPECT_EQ(llvm::MachO::MH_EXECUTE, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000069}
70
71TEST_F(DarwinLdParserTest, Entry) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000072 EXPECT_TRUE(parse("ld", "-e", "entryFunc", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000073 EXPECT_EQ("entryFunc", _context.entrySymbolName());
Rui Ueyama9f56df82013-06-11 23:07:43 +000074}
75
76TEST_F(DarwinLdParserTest, OutputPath) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000077 EXPECT_TRUE(parse("ld", "-o", "foo", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000078 EXPECT_EQ("foo", _context.outputPath());
Rui Ueyama9f56df82013-06-11 23:07:43 +000079}
80
81TEST_F(DarwinLdParserTest, DeadStrip) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000082 EXPECT_TRUE(parse("ld", "-dead_strip", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000083 EXPECT_TRUE(_context.deadStrip());
Rui Ueyama9f56df82013-06-11 23:07:43 +000084}
85
Nick Kledzika1210532013-07-18 23:13:13 +000086TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000087 EXPECT_TRUE(parse("ld", "-dead_strip", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000088 EXPECT_FALSE(_context.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +000089}
90
91TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000092 EXPECT_TRUE(parse("ld", "-dylib", "-dead_strip", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000093 EXPECT_TRUE(_context.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +000094}
95
Rui Ueyama9f56df82013-06-11 23:07:43 +000096TEST_F(DarwinLdParserTest, Arch) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000097 EXPECT_TRUE(parse("ld", "-arch", "x86_64", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000098 EXPECT_EQ(MachOLinkingContext::arch_x86_64, _context.arch());
Nick Kledzik473933b2013-09-27 22:50:00 +000099 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_X86_64, _context.getCPUType());
100 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_64_ALL, _context.getCPUSubType());
Rui Ueyama9f56df82013-06-11 23:07:43 +0000101}
102
Nick Kledzika1210532013-07-18 23:13:13 +0000103TEST_F(DarwinLdParserTest, Arch_x86) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000104 EXPECT_TRUE(parse("ld", "-arch", "i386", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000105 EXPECT_EQ(MachOLinkingContext::arch_x86, _context.arch());
Nick Kledzik473933b2013-09-27 22:50:00 +0000106 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_I386, _context.getCPUType());
107 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_ALL, _context.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000108}
109
110TEST_F(DarwinLdParserTest, Arch_armv6) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000111 EXPECT_TRUE(parse("ld", "-arch", "armv6", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000112 EXPECT_EQ(MachOLinkingContext::arch_armv6, _context.arch());
Nick Kledzik473933b2013-09-27 22:50:00 +0000113 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _context.getCPUType());
114 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V6, _context.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000115}
116
117TEST_F(DarwinLdParserTest, Arch_armv7) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000118 EXPECT_TRUE(parse("ld", "-arch", "armv7", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000119 EXPECT_EQ(MachOLinkingContext::arch_armv7, _context.arch());
Nick Kledzik473933b2013-09-27 22:50:00 +0000120 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _context.getCPUType());
121 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7, _context.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000122}
123
124TEST_F(DarwinLdParserTest, Arch_armv7s) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000125 EXPECT_TRUE(parse("ld", "-arch", "armv7s", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000126 EXPECT_EQ(MachOLinkingContext::arch_armv7s, _context.arch());
Nick Kledzik473933b2013-09-27 22:50:00 +0000127 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _context.getCPUType());
128 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7S, _context.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000129}
130
131TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000132 EXPECT_TRUE(parse("ld", "-macosx_version_min", "10.7", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000133 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _context.os());
134 EXPECT_TRUE(_context.minOS("10.7", ""));
135 EXPECT_FALSE(_context.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000136}
137
138TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000139 EXPECT_TRUE(parse("ld", "-macosx_version_min", "10.8.3", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000140 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _context.os());
141 EXPECT_TRUE(_context.minOS("10.7", ""));
142 EXPECT_TRUE(_context.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000143}
144
145TEST_F(DarwinLdParserTest, iOS5) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000146 EXPECT_TRUE(parse("ld", "-ios_version_min", "5.0", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000147 EXPECT_EQ(MachOLinkingContext::OS::iOS, _context.os());
148 EXPECT_TRUE(_context.minOS("", "5.0"));
149 EXPECT_FALSE(_context.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000150}
151
152TEST_F(DarwinLdParserTest, iOS6) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000153 EXPECT_TRUE(parse("ld", "-ios_version_min", "6.0", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000154 EXPECT_EQ(MachOLinkingContext::OS::iOS, _context.os());
155 EXPECT_TRUE(_context.minOS("", "5.0"));
156 EXPECT_TRUE(_context.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000157}
158
159TEST_F(DarwinLdParserTest, iOS_Simulator5) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000160 EXPECT_TRUE(parse("ld", "-ios_simulator_version_min", "5.0", "a.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000161 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _context.os());
162 EXPECT_TRUE(_context.minOS("", "5.0"));
163 EXPECT_FALSE(_context.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000164}
165
166TEST_F(DarwinLdParserTest, iOS_Simulator6) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000167 EXPECT_TRUE(parse("ld", "-ios_simulator_version_min", "6.0", "a.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000168 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _context.os());
169 EXPECT_TRUE(_context.minOS("", "5.0"));
170 EXPECT_TRUE(_context.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000171}
172
Nick Kledzike773e322013-09-10 23:55:14 +0000173TEST_F(DarwinLdParserTest, compatibilityVersion) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000174 EXPECT_TRUE(
Nick Kledzike773e322013-09-10 23:55:14 +0000175 parse("ld", "-dylib", "-compatibility_version", "1.2.3", "a.o", nullptr));
176 EXPECT_EQ(_context.compatibilityVersion(), 0x10203U);
177}
178
179TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000180 EXPECT_FALSE(parse("ld", "-bundle", "-compatibility_version", "1.2.3", "a.o",
181 nullptr));
Nick Kledzike773e322013-09-10 23:55:14 +0000182}
183
184TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000185 EXPECT_FALSE(parse("ld", "-bundle", "-compatibility_version", "1,2,3", "a.o",
186 nullptr));
Nick Kledzike773e322013-09-10 23:55:14 +0000187}
188
189TEST_F(DarwinLdParserTest, currentVersion) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000190 EXPECT_TRUE(
Nick Kledzike773e322013-09-10 23:55:14 +0000191 parse("ld", "-dylib", "-current_version", "1.2.3", "a.o", nullptr));
192 EXPECT_EQ(_context.currentVersion(), 0x10203U);
193}
194
195TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000196 EXPECT_FALSE(
Nick Kledzike773e322013-09-10 23:55:14 +0000197 parse("ld", "-bundle", "-current_version", "1.2.3", "a.o", nullptr));
198}
199
200TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000201 EXPECT_FALSE(
Nick Kledzike773e322013-09-10 23:55:14 +0000202 parse("ld", "-bundle", "-current_version", "1,2,3", "a.o", nullptr));
203}
204
205TEST_F(DarwinLdParserTest, bundleLoader) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000206 EXPECT_TRUE(
Nick Kledzike773e322013-09-10 23:55:14 +0000207 parse("ld", "-bundle", "-bundle_loader", "/bin/ls", "a.o", nullptr));
208 EXPECT_EQ(_context.bundleLoader(), "/bin/ls");
209}
210
211TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000212 EXPECT_FALSE(parse("ld", "-bundle_loader", "/bin/ls", "a.o", nullptr));
Nick Kledzike773e322013-09-10 23:55:14 +0000213}
214
215TEST_F(DarwinLdParserTest, deadStrippableDylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000216 EXPECT_TRUE(
Nick Kledzike773e322013-09-10 23:55:14 +0000217 parse("ld", "-dylib", "-mark_dead_strippable_dylib", "a.o", nullptr));
218 EXPECT_EQ(true, _context.deadStrippableDylib());
219}
220
221TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000222 EXPECT_FALSE(parse("ld", "-mark_dead_strippable_dylib", "a.o", nullptr));
Nick Kledzike773e322013-09-10 23:55:14 +0000223}
224
Nick Kledzik0932c002013-09-28 00:29:33 +0000225TEST_F(DarwinLdParserTest, llvmOptions) {
226 EXPECT_TRUE(parse("ld", "-mllvm", "-debug-only", "-mllvm", "foo", "a.o", nullptr));
227 const std::vector<const char *> &options = _context.llvmOptions();
228 EXPECT_EQ(options.size(), 2UL);
229 EXPECT_EQ(strcmp(options[0],"-debug-only"), 0);
230 EXPECT_EQ(strcmp(options[1],"foo"), 0);
231}