blob: 68bd73643fe63ddeac9086ec32959ef5c2efadf4 [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
24namespace {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000025class DarwinLdParserTest : public testing::Test {
Rui Ueyama9f56df82013-06-11 23:07:43 +000026protected:
Rui Ueyama3b9388f2016-02-28 20:56:34 +000027 int inputFileCount() { return _ctx.getNodes().size(); }
28
29 std::string inputFile(int index) {
30 Node &node = *_ctx.getNodes()[index];
31 if (node.kind() == Node::Kind::File)
32 return cast<FileNode>(&node)->getFile()->path();
33 llvm_unreachable("not handling other types of input files");
34 }
35
36 bool parse(std::vector<const char *> args) {
37 args.insert(args.begin(), "ld");
38 std::string errorMessage;
39 raw_string_ostream os(errorMessage);
40 return DarwinLdDriver::parse(args, _ctx, os);
41 }
42
43 MachOLinkingContext _ctx;
Rui Ueyama9f56df82013-06-11 23:07:43 +000044};
Rui Ueyama9024c362014-03-27 23:34:32 +000045}
Rui Ueyama9f56df82013-06-11 23:07:43 +000046
47TEST_F(DarwinLdParserTest, Basic) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000048 EXPECT_TRUE(parse({"foo.o", "bar.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000049 EXPECT_FALSE(_ctx.allowRemainingUndefines());
50 EXPECT_FALSE(_ctx.deadStrip());
Nick Kledzik2a709ea2013-07-16 18:45:57 +000051 EXPECT_EQ(2, inputFileCount());
52 EXPECT_EQ("foo.o", inputFile(0));
53 EXPECT_EQ("bar.o", inputFile(1));
Rui Ueyama9f56df82013-06-11 23:07:43 +000054}
55
Nick Kledzika1210532013-07-18 23:13:13 +000056TEST_F(DarwinLdParserTest, Output) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000057 EXPECT_TRUE(parse({"-o", "my.out", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000058 EXPECT_EQ("my.out", _ctx.outputPath());
Nick Kledzika1210532013-07-18 23:13:13 +000059}
60
Rui Ueyama9f56df82013-06-11 23:07:43 +000061TEST_F(DarwinLdParserTest, Dylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000062 EXPECT_TRUE(parse({"-dylib", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000063 EXPECT_EQ(llvm::MachO::MH_DYLIB, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000064}
65
66TEST_F(DarwinLdParserTest, Relocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000067 EXPECT_TRUE(parse({"-r", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000068 EXPECT_EQ(llvm::MachO::MH_OBJECT, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000069}
70
71TEST_F(DarwinLdParserTest, Bundle) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000072 EXPECT_TRUE(parse({"-bundle", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000073 EXPECT_EQ(llvm::MachO::MH_BUNDLE, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000074}
75
76TEST_F(DarwinLdParserTest, Preload) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000077 EXPECT_TRUE(parse({"-preload", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000078 EXPECT_EQ(llvm::MachO::MH_PRELOAD, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000079}
80
81TEST_F(DarwinLdParserTest, Static) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000082 EXPECT_TRUE(parse({"-static", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000083 EXPECT_EQ(llvm::MachO::MH_EXECUTE, _ctx.outputMachOType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000084}
85
86TEST_F(DarwinLdParserTest, Entry) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000087 EXPECT_TRUE(parse({"-e", "entryFunc", "foo.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000088 EXPECT_EQ("entryFunc", _ctx.entrySymbolName());
Rui Ueyama9f56df82013-06-11 23:07:43 +000089}
90
Rui Ueyama9f56df82013-06-11 23:07:43 +000091TEST_F(DarwinLdParserTest, DeadStrip) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000092 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000093 EXPECT_TRUE(_ctx.deadStrip());
Rui Ueyama9f56df82013-06-11 23:07:43 +000094}
95
Nick Kledzika1210532013-07-18 23:13:13 +000096TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +000097 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +000098 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +000099}
100
101TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000102 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000103 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
104}
105
106TEST_F(DarwinLdParserTest, DeadStripRootsRelocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000107 EXPECT_TRUE(parse({"-arch", "x86_64", "-r", "-dead_strip", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000108 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
109}
110
111TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicExe) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000112 EXPECT_TRUE(
113 parse({"-arch", "x86_64", "-dead_strip", "-export_dynamic", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000114 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +0000115}
116
Pete Cooper35116452016-01-22 21:13:24 +0000117TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicDylib) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000118 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip",
119 "-export_dynamic", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000120 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
121}
122
123TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicRelocatable) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000124 EXPECT_TRUE(parse(
125 {"-arch", "x86_64", "-r", "-dead_strip", "-export_dynamic", "foo.o"}));
Pete Cooper35116452016-01-22 21:13:24 +0000126 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
127}
128
Rui Ueyama9f56df82013-06-11 23:07:43 +0000129TEST_F(DarwinLdParserTest, Arch) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000130 EXPECT_TRUE(parse({"-arch", "x86_64", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000131 EXPECT_EQ(MachOLinkingContext::arch_x86_64, _ctx.arch());
132 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_X86_64, _ctx.getCPUType());
133 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_64_ALL, _ctx.getCPUSubType());
Rui Ueyama9f56df82013-06-11 23:07:43 +0000134}
135
Nick Kledzika1210532013-07-18 23:13:13 +0000136TEST_F(DarwinLdParserTest, Arch_x86) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000137 EXPECT_TRUE(parse({"-arch", "i386", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000138 EXPECT_EQ(MachOLinkingContext::arch_x86, _ctx.arch());
139 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_I386, _ctx.getCPUType());
140 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_X86_ALL, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000141}
142
143TEST_F(DarwinLdParserTest, Arch_armv6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000144 EXPECT_TRUE(parse({"-arch", "armv6", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000145 EXPECT_EQ(MachOLinkingContext::arch_armv6, _ctx.arch());
146 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
147 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V6, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000148}
149
150TEST_F(DarwinLdParserTest, Arch_armv7) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000151 EXPECT_TRUE(parse({"-arch", "armv7", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000152 EXPECT_EQ(MachOLinkingContext::arch_armv7, _ctx.arch());
153 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
154 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000155}
156
157TEST_F(DarwinLdParserTest, Arch_armv7s) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000158 EXPECT_TRUE(parse({"-arch", "armv7s", "foo.o"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000159 EXPECT_EQ(MachOLinkingContext::arch_armv7s, _ctx.arch());
160 EXPECT_EQ((uint32_t)llvm::MachO::CPU_TYPE_ARM, _ctx.getCPUType());
161 EXPECT_EQ(llvm::MachO::CPU_SUBTYPE_ARM_V7S, _ctx.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000162}
163
164TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000165 EXPECT_TRUE(
166 parse({"-macosx_version_min", "10.7", "foo.o", "-arch", "x86_64"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000167 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
168 EXPECT_TRUE(_ctx.minOS("10.7", ""));
169 EXPECT_FALSE(_ctx.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000170}
171
172TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000173 EXPECT_TRUE(
174 parse({"-macosx_version_min", "10.8.3", "foo.o", "-arch", "x86_64"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000175 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
176 EXPECT_TRUE(_ctx.minOS("10.7", ""));
177 EXPECT_TRUE(_ctx.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000178}
179
180TEST_F(DarwinLdParserTest, iOS5) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000181 EXPECT_TRUE(parse({"-ios_version_min", "5.0", "foo.o", "-arch", "armv7"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000182 EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
183 EXPECT_TRUE(_ctx.minOS("", "5.0"));
184 EXPECT_FALSE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000185}
186
187TEST_F(DarwinLdParserTest, iOS6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000188 EXPECT_TRUE(parse({"-ios_version_min", "6.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_TRUE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000192}
193
194TEST_F(DarwinLdParserTest, iOS_Simulator5) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000195 EXPECT_TRUE(
196 parse({"-ios_simulator_version_min", "5.0", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000197 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
198 EXPECT_TRUE(_ctx.minOS("", "5.0"));
199 EXPECT_FALSE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000200}
201
202TEST_F(DarwinLdParserTest, iOS_Simulator6) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000203 EXPECT_TRUE(
204 parse({"-ios_simulator_version_min", "6.0", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000205 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
206 EXPECT_TRUE(_ctx.minOS("", "5.0"));
207 EXPECT_TRUE(_ctx.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000208}
209
Nick Kledzike773e322013-09-10 23:55:14 +0000210TEST_F(DarwinLdParserTest, compatibilityVersion) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000211 EXPECT_TRUE(parse(
212 {"-dylib", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000213 EXPECT_EQ(_ctx.compatibilityVersion(), 0x10203U);
Nick Kledzike773e322013-09-10 23:55:14 +0000214}
215
216TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000217 EXPECT_FALSE(parse(
218 {"-bundle", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000219}
220
221TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000222 EXPECT_FALSE(parse(
223 {"-bundle", "-compatibility_version", "1,2,3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000224}
225
226TEST_F(DarwinLdParserTest, currentVersion) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000227 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000228 parse({"-dylib", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000229 EXPECT_EQ(_ctx.currentVersion(), 0x10203U);
Nick Kledzike773e322013-09-10 23:55:14 +0000230}
231
232TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000233 EXPECT_FALSE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000234 parse({"-bundle", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000235}
236
237TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000238 EXPECT_FALSE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000239 parse({"-bundle", "-current_version", "1,2,3", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000240}
241
242TEST_F(DarwinLdParserTest, bundleLoader) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000243 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000244 parse({"-bundle", "-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000245 EXPECT_EQ(_ctx.bundleLoader(), "/bin/ls");
Nick Kledzike773e322013-09-10 23:55:14 +0000246}
247
248TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000249 EXPECT_FALSE(parse({"-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000250}
251
252TEST_F(DarwinLdParserTest, deadStrippableDylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000253 EXPECT_TRUE(
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000254 parse({"-dylib", "-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
Rui Ueyamaea6c8e92015-02-10 21:28:52 +0000255 EXPECT_EQ(true, _ctx.deadStrippableDylib());
Nick Kledzike773e322013-09-10 23:55:14 +0000256}
257
258TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
Rui Ueyama3b9388f2016-02-28 20:56:34 +0000259 EXPECT_FALSE(parse({"-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
Nick Kledzike773e322013-09-10 23:55:14 +0000260}