blob: af0fbbeef2a5e5c7ae9e11106d400c7f801bba42 [file] [log] [blame]
Rui Ueyama9f56df82013-06-11 23:07:43 +00001//===- lld/unittest/DarwinLdDriverTest.cpp --------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Rui Ueyama9f56df82013-06-11 23:07:43 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Fangrui Songc505e062018-05-15 16:40:54 +000010/// Darwin's ld driver tests.
Rui Ueyama9f56df82013-06-11 23:07:43 +000011///
12//===----------------------------------------------------------------------===//
13
Rui Ueyama3f851702017-10-02 21:00:41 +000014#include "lld/Common/Driver.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000015#include "lld/ReaderWriter/MachOLinkingContext.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/MachO.h"
Rui Ueyama3b9388f2016-02-28 20:56:34 +000017#include "llvm/Support/raw_ostream.h"
18#include "gtest/gtest.h"
Rui Ueyama9f56df82013-06-11 23:07:43 +000019
20using namespace llvm;
21using namespace lld;
22
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000023namespace lld {
24namespace mach_o {
Brian Gesiakb9f7f4b2018-06-12 02:34:04 +000025bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx);
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000026}
27}
28
Rui Ueyama9f56df82013-06-11 23:07:43 +000029namespace {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000030class DarwinLdParserTest : public testing::Test {
Rui Ueyama9f56df82013-06-11 23:07:43 +000031protected:
Rui Ueyama3b9388f2016-02-28 20:56:34 +000032 int inputFileCount() { return _ctx.getNodes().size(); }
33
Benjamin Krameradcd0262020-01-28 20:23:46 +010034 StringRef inputFile(int index) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000035 Node &node = *_ctx.getNodes()[index];
36 if (node.kind() == Node::Kind::File)
37 return cast<FileNode>(&node)->getFile()->path();
38 llvm_unreachable("not handling other types of input files");
39 }
40
41 bool parse(std::vector<const char *> args) {
42 args.insert(args.begin(), "ld");
Brian Gesiakb9f7f4b2018-06-12 02:34:04 +000043 return mach_o::parse(args, _ctx);
Rui Ueyama3b9388f2016-02-28 20:56:34 +000044 }
45
46 MachOLinkingContext _ctx;
Rui Ueyama9f56df82013-06-11 23:07:43 +000047};
Rui Ueyama9024c362014-03-27 23:34:32 +000048}
Rui Ueyama9f56df82013-06-11 23:07:43 +000049
50TEST_F(DarwinLdParserTest, Basic) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000051 EXPECT_TRUE(parse({"foo.o", "bar.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000052 EXPECT_FALSE(_ctx.allowRemainingUndefines());
53 EXPECT_FALSE(_ctx.deadStrip());
Nick Kledzik2a709ea2013-07-16 18:45:57 +000054 EXPECT_EQ(2, inputFileCount());
55 EXPECT_EQ("foo.o", inputFile(0));
56 EXPECT_EQ("bar.o", inputFile(1));
Rui Ueyama9f56df82013-06-11 23:07:43 +000057}
58
Nick Kledzika1210532013-07-18 23:13:13 +000059TEST_F(DarwinLdParserTest, Output) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000060 EXPECT_TRUE(parse({"-o", "my.out", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000061 EXPECT_EQ("my.out", _ctx.outputPath());
Nick Kledzika1210532013-07-18 23:13:13 +000062}
63
Rui Ueyama9f56df82013-06-11 23:07:43 +000064TEST_F(DarwinLdParserTest, Dylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000065 EXPECT_TRUE(parse({"-dylib", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000066 EXPECT_EQ(llvm::MachO::MH_DYLIB, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000067}
68
69TEST_F(DarwinLdParserTest, Relocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000070 EXPECT_TRUE(parse({"-r", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000071 EXPECT_EQ(llvm::MachO::MH_OBJECT, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000072}
73
74TEST_F(DarwinLdParserTest, Bundle) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000075 EXPECT_TRUE(parse({"-bundle", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000076 EXPECT_EQ(llvm::MachO::MH_BUNDLE, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000077}
78
79TEST_F(DarwinLdParserTest, Preload) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000080 EXPECT_TRUE(parse({"-preload", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000081 EXPECT_EQ(llvm::MachO::MH_PRELOAD, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000082}
83
84TEST_F(DarwinLdParserTest, Static) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000085 EXPECT_TRUE(parse({"-static", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000086 EXPECT_EQ(llvm::MachO::MH_EXECUTE, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000087}
88
89TEST_F(DarwinLdParserTest, Entry) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000090 EXPECT_TRUE(parse({"-e", "entryFunc", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000091 EXPECT_EQ("entryFunc", _ctx.entrySymbolName());
Rui Ueyama9f56df82013-06-11 23:07:43 +000092}
93
Rui Ueyama9f56df82013-06-11 23:07:43 +000094TEST_F(DarwinLdParserTest, DeadStrip) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000095 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000096 EXPECT_TRUE(_ctx.deadStrip());
Rui Ueyama9f56df82013-06-11 23:07:43 +000097}
98
Nick Kledzika1210532013-07-18 23:13:13 +000099TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000100 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000101 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +0000102}
103
104TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000105 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000106 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
107}
108
109TEST_F(DarwinLdParserTest, DeadStripRootsRelocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000110 EXPECT_TRUE(parse({"-arch", "x86_64", "-r", "-dead_strip", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000111 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
112}
113
114TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicExe) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000115 EXPECT_TRUE(
116 parse({"-arch", "x86_64", "-dead_strip", "-export_dynamic", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000117 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +0000118}
119
Pete Cooper35116452016-01-22 21:13:24 +0000120TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicDylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000121 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip",
122 "-export_dynamic", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000123 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
124}
125
126TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicRelocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000127 EXPECT_TRUE(parse(
128 {"-arch", "x86_64", "-r", "-dead_strip", "-export_dynamic", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000129 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
130}
131
Rui Ueyama9f56df82013-06-11 23:07:43 +0000132TEST_F(DarwinLdParserTest, Arch) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000133 EXPECT_TRUE(parse({"-arch", "x86_64", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000134 EXPECT_EQ(MachOLinkingContext::arch_x86_64, _ctx.arch());
135 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_X86_64, _ctx.getCPUType());
136 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_64_ALL, _ctx.getCPUSubType());
Rui Ueyama9f56df82013-06-11 23:07:43 +0000137}
138
Nick Kledzika1210532013-07-18 23:13:13 +0000139TEST_F(DarwinLdParserTest, Arch_x86) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000140 EXPECT_TRUE(parse({"-arch", "i386", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000141 EXPECT_EQ(MachOLinkingContext::arch_x86, _ctx.arch());
142 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_I386, _ctx.getCPUType());
143 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_ALL, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000144}
145
146TEST_F(DarwinLdParserTest, Arch_armv6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000147 EXPECT_TRUE(parse({"-arch", "armv6", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000148 EXPECT_EQ(MachOLinkingContext::arch_armv6, _ctx.arch());
149 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
150 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V6, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000151}
152
153TEST_F(DarwinLdParserTest, Arch_armv7) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000154 EXPECT_TRUE(parse({"-arch", "armv7", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000155 EXPECT_EQ(MachOLinkingContext::arch_armv7, _ctx.arch());
156 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
157 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000158}
159
160TEST_F(DarwinLdParserTest, Arch_armv7s) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000161 EXPECT_TRUE(parse({"-arch", "armv7s", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000162 EXPECT_EQ(MachOLinkingContext::arch_armv7s, _ctx.arch());
163 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
164 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7S, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000165}
166
167TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000168 EXPECT_TRUE(
169 parse({"-macosx_version_min", "10.7", "foo.o", "-arch", "x86_64"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000170 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
171 EXPECT_TRUE(_ctx.minOS("10.7", ""));
172 EXPECT_FALSE(_ctx.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000173}
174
175TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000176 EXPECT_TRUE(
177 parse({"-macosx_version_min", "10.8.3", "foo.o", "-arch", "x86_64"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000178 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
179 EXPECT_TRUE(_ctx.minOS("10.7", ""));
180 EXPECT_TRUE(_ctx.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000181}
182
183TEST_F(DarwinLdParserTest, iOS5) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000184 EXPECT_TRUE(parse({"-ios_version_min", "5.0", "foo.o", "-arch", "armv7"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000185 EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
186 EXPECT_TRUE(_ctx.minOS("", "5.0"));
187 EXPECT_FALSE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000188}
189
190TEST_F(DarwinLdParserTest, iOS6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000191 EXPECT_TRUE(parse({"-ios_version_min", "6.0", "foo.o", "-arch", "armv7"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000192 EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
193 EXPECT_TRUE(_ctx.minOS("", "5.0"));
194 EXPECT_TRUE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000195}
196
197TEST_F(DarwinLdParserTest, iOS_Simulator5) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000198 EXPECT_TRUE(
199 parse({"-ios_simulator_version_min", "5.0", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000200 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
201 EXPECT_TRUE(_ctx.minOS("", "5.0"));
202 EXPECT_FALSE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000203}
204
205TEST_F(DarwinLdParserTest, iOS_Simulator6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000206 EXPECT_TRUE(
207 parse({"-ios_simulator_version_min", "6.0", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000208 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
209 EXPECT_TRUE(_ctx.minOS("", "5.0"));
210 EXPECT_TRUE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000211}
212
Nick Kledzike773e322013-09-10 23:55:14 +0000213TEST_F(DarwinLdParserTest, compatibilityVersion) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000214 EXPECT_TRUE(parse(
215 {"-dylib", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000216 EXPECT_EQ(_ctx.compatibilityVersion(), 0x10203U);
Nick Kledzike773e322013-09-10 23:55:14 +0000217}
218
219TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000220 EXPECT_FALSE(parse(
221 {"-bundle", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000222}
223
224TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000225 EXPECT_FALSE(parse(
226 {"-bundle", "-compatibility_version", "1,2,3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000227}
228
229TEST_F(DarwinLdParserTest, currentVersion) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000230 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000231 parse({"-dylib", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000232 EXPECT_EQ(_ctx.currentVersion(), 0x10203U);
Nick Kledzike773e322013-09-10 23:55:14 +0000233}
234
235TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000236 EXPECT_FALSE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000237 parse({"-bundle", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000238}
239
240TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000241 EXPECT_FALSE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000242 parse({"-bundle", "-current_version", "1,2,3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000243}
244
245TEST_F(DarwinLdParserTest, bundleLoader) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000246 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000247 parse({"-bundle", "-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000248 EXPECT_EQ(_ctx.bundleLoader(), "/bin/ls");
Nick Kledzike773e322013-09-10 23:55:14 +0000249}
250
251TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000252 EXPECT_FALSE(parse({"-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000253}
254
255TEST_F(DarwinLdParserTest, deadStrippableDylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000256 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000257 parse({"-dylib", "-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000258 EXPECT_EQ(true, _ctx.deadStrippableDylib());
Nick Kledzike773e322013-09-10 23:55:14 +0000259}
260
261TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000262 EXPECT_FALSE(parse({"-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000263}