blob: e2e634a4cb2d5d675b976685ea6709e797ec3bed [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
Fangrui Songc505e062018-05-15 16:40:54 +000011/// Darwin's ld driver tests.
Rui Ueyama9f56df82013-06-11 23:07:43 +000012///
13//===----------------------------------------------------------------------===//
14
Rui Ueyama3f851702017-10-02 21:00:41 +000015#include "lld/Common/Driver.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000016#include "lld/ReaderWriter/MachOLinkingContext.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/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 {
Brian Gesiakb9f7f4b2018-06-12 02:34:04 +000026bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx);
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000027}
28}
29
Rui Ueyama9f56df82013-06-11 23:07:43 +000030namespace {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000031class DarwinLdParserTest : public testing::Test {
Rui Ueyama9f56df82013-06-11 23:07:43 +000032protected:
Rui Ueyama3b9388f2016-02-28 20:56:34 +000033 int inputFileCount() { return _ctx.getNodes().size(); }
34
35 std::string inputFile(int index) {
36 Node &node = *_ctx.getNodes()[index];
37 if (node.kind() == Node::Kind::File)
38 return cast<FileNode>(&node)->getFile()->path();
39 llvm_unreachable("not handling other types of input files");
40 }
41
42 bool parse(std::vector<const char *> args) {
43 args.insert(args.begin(), "ld");
Brian Gesiakb9f7f4b2018-06-12 02:34:04 +000044 return mach_o::parse(args, _ctx);
Rui Ueyama3b9388f2016-02-28 20:56:34 +000045 }
46
47 MachOLinkingContext _ctx;
Rui Ueyama9f56df82013-06-11 23:07:43 +000048};
Rui Ueyama9024c362014-03-27 23:34:32 +000049}
Rui Ueyama9f56df82013-06-11 23:07:43 +000050
51TEST_F(DarwinLdParserTest, Basic) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000052 EXPECT_TRUE(parse({"foo.o", "bar.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000053 EXPECT_FALSE(_ctx.allowRemainingUndefines());
54 EXPECT_FALSE(_ctx.deadStrip());
Nick Kledzik2a709ea2013-07-16 18:45:57 +000055 EXPECT_EQ(2, inputFileCount());
56 EXPECT_EQ("foo.o", inputFile(0));
57 EXPECT_EQ("bar.o", inputFile(1));
Rui Ueyama9f56df82013-06-11 23:07:43 +000058}
59
Nick Kledzika1210532013-07-18 23:13:13 +000060TEST_F(DarwinLdParserTest, Output) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000061 EXPECT_TRUE(parse({"-o", "my.out", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000062 EXPECT_EQ("my.out", _ctx.outputPath());
Nick Kledzika1210532013-07-18 23:13:13 +000063}
64
Rui Ueyama9f56df82013-06-11 23:07:43 +000065TEST_F(DarwinLdParserTest, Dylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000066 EXPECT_TRUE(parse({"-dylib", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000067 EXPECT_EQ(llvm::MachO::MH_DYLIB, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000068}
69
70TEST_F(DarwinLdParserTest, Relocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000071 EXPECT_TRUE(parse({"-r", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000072 EXPECT_EQ(llvm::MachO::MH_OBJECT, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000073}
74
75TEST_F(DarwinLdParserTest, Bundle) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000076 EXPECT_TRUE(parse({"-bundle", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000077 EXPECT_EQ(llvm::MachO::MH_BUNDLE, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000078}
79
80TEST_F(DarwinLdParserTest, Preload) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000081 EXPECT_TRUE(parse({"-preload", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000082 EXPECT_EQ(llvm::MachO::MH_PRELOAD, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000083}
84
85TEST_F(DarwinLdParserTest, Static) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000086 EXPECT_TRUE(parse({"-static", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000087 EXPECT_EQ(llvm::MachO::MH_EXECUTE, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000088}
89
90TEST_F(DarwinLdParserTest, Entry) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000091 EXPECT_TRUE(parse({"-e", "entryFunc", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000092 EXPECT_EQ("entryFunc", _ctx.entrySymbolName());
Rui Ueyama9f56df82013-06-11 23:07:43 +000093}
94
Rui Ueyama9f56df82013-06-11 23:07:43 +000095TEST_F(DarwinLdParserTest, DeadStrip) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000096 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000097 EXPECT_TRUE(_ctx.deadStrip());
Rui Ueyama9f56df82013-06-11 23:07:43 +000098}
99
Nick Kledzika1210532013-07-18 23:13:13 +0000100TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000101 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000102 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +0000103}
104
105TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000106 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000107 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
108}
109
110TEST_F(DarwinLdParserTest, DeadStripRootsRelocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000111 EXPECT_TRUE(parse({"-arch", "x86_64", "-r", "-dead_strip", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000112 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
113}
114
115TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicExe) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000116 EXPECT_TRUE(
117 parse({"-arch", "x86_64", "-dead_strip", "-export_dynamic", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000118 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +0000119}
120
Pete Cooper35116452016-01-22 21:13:24 +0000121TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicDylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000122 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip",
123 "-export_dynamic", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000124 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
125}
126
127TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicRelocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000128 EXPECT_TRUE(parse(
129 {"-arch", "x86_64", "-r", "-dead_strip", "-export_dynamic", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000130 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
131}
132
Rui Ueyama9f56df82013-06-11 23:07:43 +0000133TEST_F(DarwinLdParserTest, Arch) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000134 EXPECT_TRUE(parse({"-arch", "x86_64", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000135 EXPECT_EQ(MachOLinkingContext::arch_x86_64, _ctx.arch());
136 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_X86_64, _ctx.getCPUType());
137 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_64_ALL, _ctx.getCPUSubType());
Rui Ueyama9f56df82013-06-11 23:07:43 +0000138}
139
Nick Kledzika1210532013-07-18 23:13:13 +0000140TEST_F(DarwinLdParserTest, Arch_x86) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000141 EXPECT_TRUE(parse({"-arch", "i386", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000142 EXPECT_EQ(MachOLinkingContext::arch_x86, _ctx.arch());
143 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_I386, _ctx.getCPUType());
144 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_ALL, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000145}
146
147TEST_F(DarwinLdParserTest, Arch_armv6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000148 EXPECT_TRUE(parse({"-arch", "armv6", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000149 EXPECT_EQ(MachOLinkingContext::arch_armv6, _ctx.arch());
150 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
151 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V6, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000152}
153
154TEST_F(DarwinLdParserTest, Arch_armv7) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000155 EXPECT_TRUE(parse({"-arch", "armv7", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000156 EXPECT_EQ(MachOLinkingContext::arch_armv7, _ctx.arch());
157 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
158 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000159}
160
161TEST_F(DarwinLdParserTest, Arch_armv7s) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000162 EXPECT_TRUE(parse({"-arch", "armv7s", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000163 EXPECT_EQ(MachOLinkingContext::arch_armv7s, _ctx.arch());
164 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
165 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7S, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000166}
167
168TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000169 EXPECT_TRUE(
170 parse({"-macosx_version_min", "10.7", "foo.o", "-arch", "x86_64"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000171 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
172 EXPECT_TRUE(_ctx.minOS("10.7", ""));
173 EXPECT_FALSE(_ctx.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000174}
175
176TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000177 EXPECT_TRUE(
178 parse({"-macosx_version_min", "10.8.3", "foo.o", "-arch", "x86_64"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000179 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
180 EXPECT_TRUE(_ctx.minOS("10.7", ""));
181 EXPECT_TRUE(_ctx.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000182}
183
184TEST_F(DarwinLdParserTest, iOS5) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000185 EXPECT_TRUE(parse({"-ios_version_min", "5.0", "foo.o", "-arch", "armv7"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000186 EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
187 EXPECT_TRUE(_ctx.minOS("", "5.0"));
188 EXPECT_FALSE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000189}
190
191TEST_F(DarwinLdParserTest, iOS6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000192 EXPECT_TRUE(parse({"-ios_version_min", "6.0", "foo.o", "-arch", "armv7"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000193 EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
194 EXPECT_TRUE(_ctx.minOS("", "5.0"));
195 EXPECT_TRUE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000196}
197
198TEST_F(DarwinLdParserTest, iOS_Simulator5) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000199 EXPECT_TRUE(
200 parse({"-ios_simulator_version_min", "5.0", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000201 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
202 EXPECT_TRUE(_ctx.minOS("", "5.0"));
203 EXPECT_FALSE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000204}
205
206TEST_F(DarwinLdParserTest, iOS_Simulator6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000207 EXPECT_TRUE(
208 parse({"-ios_simulator_version_min", "6.0", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000209 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
210 EXPECT_TRUE(_ctx.minOS("", "5.0"));
211 EXPECT_TRUE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000212}
213
Nick Kledzike773e322013-09-10 23:55:14 +0000214TEST_F(DarwinLdParserTest, compatibilityVersion) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000215 EXPECT_TRUE(parse(
216 {"-dylib", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000217 EXPECT_EQ(_ctx.compatibilityVersion(), 0x10203U);
Nick Kledzike773e322013-09-10 23:55:14 +0000218}
219
220TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000221 EXPECT_FALSE(parse(
222 {"-bundle", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000223}
224
225TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000226 EXPECT_FALSE(parse(
227 {"-bundle", "-compatibility_version", "1,2,3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000228}
229
230TEST_F(DarwinLdParserTest, currentVersion) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000231 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000232 parse({"-dylib", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000233 EXPECT_EQ(_ctx.currentVersion(), 0x10203U);
Nick Kledzike773e322013-09-10 23:55:14 +0000234}
235
236TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000237 EXPECT_FALSE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000238 parse({"-bundle", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000239}
240
241TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000242 EXPECT_FALSE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000243 parse({"-bundle", "-current_version", "1,2,3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000244}
245
246TEST_F(DarwinLdParserTest, bundleLoader) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000247 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000248 parse({"-bundle", "-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000249 EXPECT_EQ(_ctx.bundleLoader(), "/bin/ls");
Nick Kledzike773e322013-09-10 23:55:14 +0000250}
251
252TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000253 EXPECT_FALSE(parse({"-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000254}
255
256TEST_F(DarwinLdParserTest, deadStrippableDylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000257 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000258 parse({"-dylib", "-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000259 EXPECT_EQ(true, _ctx.deadStrippableDylib());
Nick Kledzike773e322013-09-10 23:55:14 +0000260}
261
262TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000263 EXPECT_FALSE(parse({"-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000264}