blob: ea7fcc7eafcbc5317e2599f4aa2040d1718fb652 [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
Rui Ueyama3b9388f2016-02-28 20:56:34 +000015#include "lld/Driver/Driver.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000016#include "lld/ReaderWriter/MachOLinkingContext.h"
Shankar Easwaran2b67fca2014-10-18 05:33:55 +000017#include "llvm/Support/MachO.h"
Rui Ueyama3b9388f2016-02-28 20:56:34 +000018#include "llvm/Support/raw_ostream.h"
19#include "gtest/gtest.h"
Rui Ueyama9f56df82013-06-11 23:07:43 +000020
21using namespace llvm;
22using namespace lld;
23
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000024namespace lld {
25namespace mach_o {
26bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx,
27 raw_ostream &diagnostics);
28}
29}
30
Rui Ueyama9f56df82013-06-11 23:07:43 +000031namespace {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000032class DarwinLdParserTest : public testing::Test {
Rui Ueyama9f56df82013-06-11 23:07:43 +000033protected:
Rui Ueyama3b9388f2016-02-28 20:56:34 +000034 int inputFileCount() { return _ctx.getNodes().size(); }
35
36 std::string inputFile(int index) {
37 Node &node = *_ctx.getNodes()[index];
38 if (node.kind() == Node::Kind::File)
39 return cast<FileNode>(&node)->getFile()->path();
40 llvm_unreachable("not handling other types of input files");
41 }
42
43 bool parse(std::vector<const char *> args) {
44 args.insert(args.begin(), "ld");
45 std::string errorMessage;
46 raw_string_ostream os(errorMessage);
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000047 return mach_o::parse(args, _ctx, os);
Rui Ueyama3b9388f2016-02-28 20:56:34 +000048 }
49
50 MachOLinkingContext _ctx;
Rui Ueyama9f56df82013-06-11 23:07:43 +000051};
Rui Ueyama9024c362014-03-27 23:34:32 +000052}
Rui Ueyama9f56df82013-06-11 23:07:43 +000053
54TEST_F(DarwinLdParserTest, Basic) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000055 EXPECT_TRUE(parse({"foo.o", "bar.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000056 EXPECT_FALSE(_ctx.allowRemainingUndefines());
57 EXPECT_FALSE(_ctx.deadStrip());
Nick Kledzik2a709ea2013-07-16 18:45:57 +000058 EXPECT_EQ(2, inputFileCount());
59 EXPECT_EQ("foo.o", inputFile(0));
60 EXPECT_EQ("bar.o", inputFile(1));
Rui Ueyama9f56df82013-06-11 23:07:43 +000061}
62
Nick Kledzika1210532013-07-18 23:13:13 +000063TEST_F(DarwinLdParserTest, Output) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000064 EXPECT_TRUE(parse({"-o", "my.out", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000065 EXPECT_EQ("my.out", _ctx.outputPath());
Nick Kledzika1210532013-07-18 23:13:13 +000066}
67
Rui Ueyama9f56df82013-06-11 23:07:43 +000068TEST_F(DarwinLdParserTest, Dylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000069 EXPECT_TRUE(parse({"-dylib", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000070 EXPECT_EQ(llvm::MachO::MH_DYLIB, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000071}
72
73TEST_F(DarwinLdParserTest, Relocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000074 EXPECT_TRUE(parse({"-r", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000075 EXPECT_EQ(llvm::MachO::MH_OBJECT, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000076}
77
78TEST_F(DarwinLdParserTest, Bundle) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000079 EXPECT_TRUE(parse({"-bundle", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000080 EXPECT_EQ(llvm::MachO::MH_BUNDLE, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000081}
82
83TEST_F(DarwinLdParserTest, Preload) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000084 EXPECT_TRUE(parse({"-preload", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000085 EXPECT_EQ(llvm::MachO::MH_PRELOAD, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000086}
87
88TEST_F(DarwinLdParserTest, Static) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000089 EXPECT_TRUE(parse({"-static", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000090 EXPECT_EQ(llvm::MachO::MH_EXECUTE, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000091}
92
93TEST_F(DarwinLdParserTest, Entry) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000094 EXPECT_TRUE(parse({"-e", "entryFunc", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000095 EXPECT_EQ("entryFunc", _ctx.entrySymbolName());
Rui Ueyama9f56df82013-06-11 23:07:43 +000096}
97
Rui Ueyama9f56df82013-06-11 23:07:43 +000098TEST_F(DarwinLdParserTest, DeadStrip) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000099 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000100 EXPECT_TRUE(_ctx.deadStrip());
Rui Ueyama9f56df82013-06-11 23:07:43 +0000101}
102
Nick Kledzika1210532013-07-18 23:13:13 +0000103TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000104 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000105 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +0000106}
107
108TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000109 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000110 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
111}
112
113TEST_F(DarwinLdParserTest, DeadStripRootsRelocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000114 EXPECT_TRUE(parse({"-arch", "x86_64", "-r", "-dead_strip", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000115 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
116}
117
118TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicExe) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000119 EXPECT_TRUE(
120 parse({"-arch", "x86_64", "-dead_strip", "-export_dynamic", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000121 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +0000122}
123
Pete Cooper35116452016-01-22 21:13:24 +0000124TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicDylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000125 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip",
126 "-export_dynamic", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000127 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
128}
129
130TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicRelocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000131 EXPECT_TRUE(parse(
132 {"-arch", "x86_64", "-r", "-dead_strip", "-export_dynamic", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000133 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
134}
135
Rui Ueyama9f56df82013-06-11 23:07:43 +0000136TEST_F(DarwinLdParserTest, Arch) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000137 EXPECT_TRUE(parse({"-arch", "x86_64", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000138 EXPECT_EQ(MachOLinkingContext::arch_x86_64, _ctx.arch());
139 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_X86_64, _ctx.getCPUType());
140 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_64_ALL, _ctx.getCPUSubType());
Rui Ueyama9f56df82013-06-11 23:07:43 +0000141}
142
Nick Kledzika1210532013-07-18 23:13:13 +0000143TEST_F(DarwinLdParserTest, Arch_x86) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000144 EXPECT_TRUE(parse({"-arch", "i386", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000145 EXPECT_EQ(MachOLinkingContext::arch_x86, _ctx.arch());
146 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_I386, _ctx.getCPUType());
147 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_ALL, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000148}
149
150TEST_F(DarwinLdParserTest, Arch_armv6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000151 EXPECT_TRUE(parse({"-arch", "armv6", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000152 EXPECT_EQ(MachOLinkingContext::arch_armv6, _ctx.arch());
153 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
154 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V6, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000155}
156
157TEST_F(DarwinLdParserTest, Arch_armv7) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000158 EXPECT_TRUE(parse({"-arch", "armv7", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000159 EXPECT_EQ(MachOLinkingContext::arch_armv7, _ctx.arch());
160 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
161 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000162}
163
164TEST_F(DarwinLdParserTest, Arch_armv7s) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000165 EXPECT_TRUE(parse({"-arch", "armv7s", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000166 EXPECT_EQ(MachOLinkingContext::arch_armv7s, _ctx.arch());
167 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
168 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7S, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000169}
170
171TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000172 EXPECT_TRUE(
173 parse({"-macosx_version_min", "10.7", "foo.o", "-arch", "x86_64"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000174 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
175 EXPECT_TRUE(_ctx.minOS("10.7", ""));
176 EXPECT_FALSE(_ctx.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000177}
178
179TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000180 EXPECT_TRUE(
181 parse({"-macosx_version_min", "10.8.3", "foo.o", "-arch", "x86_64"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000182 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
183 EXPECT_TRUE(_ctx.minOS("10.7", ""));
184 EXPECT_TRUE(_ctx.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000185}
186
187TEST_F(DarwinLdParserTest, iOS5) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000188 EXPECT_TRUE(parse({"-ios_version_min", "5.0", "foo.o", "-arch", "armv7"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000189 EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
190 EXPECT_TRUE(_ctx.minOS("", "5.0"));
191 EXPECT_FALSE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000192}
193
194TEST_F(DarwinLdParserTest, iOS6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000195 EXPECT_TRUE(parse({"-ios_version_min", "6.0", "foo.o", "-arch", "armv7"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000196 EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
197 EXPECT_TRUE(_ctx.minOS("", "5.0"));
198 EXPECT_TRUE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000199}
200
201TEST_F(DarwinLdParserTest, iOS_Simulator5) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000202 EXPECT_TRUE(
203 parse({"-ios_simulator_version_min", "5.0", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000204 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
205 EXPECT_TRUE(_ctx.minOS("", "5.0"));
206 EXPECT_FALSE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000207}
208
209TEST_F(DarwinLdParserTest, iOS_Simulator6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000210 EXPECT_TRUE(
211 parse({"-ios_simulator_version_min", "6.0", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000212 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
213 EXPECT_TRUE(_ctx.minOS("", "5.0"));
214 EXPECT_TRUE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000215}
216
Nick Kledzike773e322013-09-10 23:55:14 +0000217TEST_F(DarwinLdParserTest, compatibilityVersion) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000218 EXPECT_TRUE(parse(
219 {"-dylib", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000220 EXPECT_EQ(_ctx.compatibilityVersion(), 0x10203U);
Nick Kledzike773e322013-09-10 23:55:14 +0000221}
222
223TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000224 EXPECT_FALSE(parse(
225 {"-bundle", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000226}
227
228TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000229 EXPECT_FALSE(parse(
230 {"-bundle", "-compatibility_version", "1,2,3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000231}
232
233TEST_F(DarwinLdParserTest, currentVersion) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000234 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000235 parse({"-dylib", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000236 EXPECT_EQ(_ctx.currentVersion(), 0x10203U);
Nick Kledzike773e322013-09-10 23:55:14 +0000237}
238
239TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000240 EXPECT_FALSE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000241 parse({"-bundle", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000242}
243
244TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000245 EXPECT_FALSE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000246 parse({"-bundle", "-current_version", "1,2,3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000247}
248
249TEST_F(DarwinLdParserTest, bundleLoader) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000250 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000251 parse({"-bundle", "-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000252 EXPECT_EQ(_ctx.bundleLoader(), "/bin/ls");
Nick Kledzike773e322013-09-10 23:55:14 +0000253}
254
255TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000256 EXPECT_FALSE(parse({"-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000257}
258
259TEST_F(DarwinLdParserTest, deadStrippableDylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000260 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000261 parse({"-dylib", "-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000262 EXPECT_EQ(true, _ctx.deadStrippableDylib());
Nick Kledzike773e322013-09-10 23:55:14 +0000263}
264
265TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000266 EXPECT_FALSE(parse({"-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000267}