blob: 1af1a34fad5cd81b0692e0ea5e8dc7c1a52f8726 [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
Rui Ueyama0ca149f2013-08-06 22:31:59 +000017#include "lld/ReaderWriter/MachOLinkingContext.h"
Shankar Easwarane44104b2013-08-21 22:57:10 +000018#include "lld/ReaderWriter/MachOFormat.hpp"
Rui Ueyama9f56df82013-06-11 23:07:43 +000019
20using namespace llvm;
21using namespace lld;
22
23namespace {
24
Rui Ueyama0ca149f2013-08-06 22:31:59 +000025class DarwinLdParserTest
26 : public ParserTest<DarwinLdDriver, MachOLinkingContext> {
Rui Ueyama9f56df82013-06-11 23:07:43 +000027protected:
Rui Ueyama0ca149f2013-08-06 22:31:59 +000028 virtual const LinkingContext *linkingContext() { return &_context; }
Rui Ueyama9f56df82013-06-11 23:07:43 +000029};
30
31TEST_F(DarwinLdParserTest, Basic) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000032 EXPECT_TRUE(parse("ld", "foo.o", "bar.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000033 EXPECT_FALSE(_context.allowRemainingUndefines());
34 EXPECT_FALSE(_context.deadStrip());
Nick Kledzik2a709ea2013-07-16 18:45:57 +000035 EXPECT_EQ(2, inputFileCount());
36 EXPECT_EQ("foo.o", inputFile(0));
37 EXPECT_EQ("bar.o", inputFile(1));
Rui Ueyama9f56df82013-06-11 23:07:43 +000038}
39
Nick Kledzika1210532013-07-18 23:13:13 +000040TEST_F(DarwinLdParserTest, Output) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000041 EXPECT_TRUE(parse("ld", "-o", "my.out", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000042 EXPECT_EQ("my.out", _context.outputPath());
Nick Kledzika1210532013-07-18 23:13:13 +000043}
44
Rui Ueyama9f56df82013-06-11 23:07:43 +000045TEST_F(DarwinLdParserTest, Dylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000046 EXPECT_TRUE(parse("ld", "-dylib", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000047 EXPECT_EQ(mach_o::MH_DYLIB, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000048}
49
50TEST_F(DarwinLdParserTest, Relocatable) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000051 EXPECT_TRUE(parse("ld", "-r", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000052 EXPECT_EQ(mach_o::MH_OBJECT, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000053}
54
55TEST_F(DarwinLdParserTest, Bundle) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000056 EXPECT_TRUE(parse("ld", "-bundle", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000057 EXPECT_EQ(mach_o::MH_BUNDLE, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000058}
59
60TEST_F(DarwinLdParserTest, Preload) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000061 EXPECT_TRUE(parse("ld", "-preload", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000062 EXPECT_EQ(mach_o::MH_PRELOAD, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000063}
64
65TEST_F(DarwinLdParserTest, Static) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000066 EXPECT_TRUE(parse("ld", "-static", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000067 EXPECT_EQ(mach_o::MH_EXECUTE, _context.outputFileType());
Rui Ueyama9f56df82013-06-11 23:07:43 +000068}
69
70TEST_F(DarwinLdParserTest, Entry) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000071 EXPECT_TRUE(parse("ld", "-e", "entryFunc", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000072 EXPECT_EQ("entryFunc", _context.entrySymbolName());
Rui Ueyama9f56df82013-06-11 23:07:43 +000073}
74
75TEST_F(DarwinLdParserTest, OutputPath) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000076 EXPECT_TRUE(parse("ld", "-o", "foo", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000077 EXPECT_EQ("foo", _context.outputPath());
Rui Ueyama9f56df82013-06-11 23:07:43 +000078}
79
80TEST_F(DarwinLdParserTest, DeadStrip) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000081 EXPECT_TRUE(parse("ld", "-dead_strip", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000082 EXPECT_TRUE(_context.deadStrip());
Rui Ueyama9f56df82013-06-11 23:07:43 +000083}
84
Nick Kledzika1210532013-07-18 23:13:13 +000085TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000086 EXPECT_TRUE(parse("ld", "-dead_strip", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000087 EXPECT_FALSE(_context.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +000088}
89
90TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000091 EXPECT_TRUE(parse("ld", "-dylib", "-dead_strip", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000092 EXPECT_TRUE(_context.globalsAreDeadStripRoots());
Nick Kledzika1210532013-07-18 23:13:13 +000093}
94
Rui Ueyama9f56df82013-06-11 23:07:43 +000095TEST_F(DarwinLdParserTest, Arch) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +000096 EXPECT_TRUE(parse("ld", "-arch", "x86_64", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +000097 EXPECT_EQ(MachOLinkingContext::arch_x86_64, _context.arch());
98 EXPECT_EQ(mach_o::CPU_TYPE_X86_64, _context.getCPUType());
99 EXPECT_EQ(mach_o::CPU_SUBTYPE_X86_64_ALL, _context.getCPUSubType());
Rui Ueyama9f56df82013-06-11 23:07:43 +0000100}
101
Nick Kledzika1210532013-07-18 23:13:13 +0000102TEST_F(DarwinLdParserTest, Arch_x86) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000103 EXPECT_TRUE(parse("ld", "-arch", "i386", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000104 EXPECT_EQ(MachOLinkingContext::arch_x86, _context.arch());
105 EXPECT_EQ(mach_o::CPU_TYPE_I386, _context.getCPUType());
106 EXPECT_EQ(mach_o::CPU_SUBTYPE_X86_ALL, _context.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000107}
108
109TEST_F(DarwinLdParserTest, Arch_armv6) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000110 EXPECT_TRUE(parse("ld", "-arch", "armv6", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000111 EXPECT_EQ(MachOLinkingContext::arch_armv6, _context.arch());
112 EXPECT_EQ(mach_o::CPU_TYPE_ARM, _context.getCPUType());
113 EXPECT_EQ(mach_o::CPU_SUBTYPE_ARM_V6, _context.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000114}
115
116TEST_F(DarwinLdParserTest, Arch_armv7) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000117 EXPECT_TRUE(parse("ld", "-arch", "armv7", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000118 EXPECT_EQ(MachOLinkingContext::arch_armv7, _context.arch());
119 EXPECT_EQ(mach_o::CPU_TYPE_ARM, _context.getCPUType());
120 EXPECT_EQ(mach_o::CPU_SUBTYPE_ARM_V7, _context.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000121}
122
123TEST_F(DarwinLdParserTest, Arch_armv7s) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000124 EXPECT_TRUE(parse("ld", "-arch", "armv7s", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000125 EXPECT_EQ(MachOLinkingContext::arch_armv7s, _context.arch());
126 EXPECT_EQ(mach_o::CPU_TYPE_ARM, _context.getCPUType());
127 EXPECT_EQ(mach_o::CPU_SUBTYPE_ARM_V7S, _context.getCPUSubType());
Nick Kledzika1210532013-07-18 23:13:13 +0000128}
129
130TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000131 EXPECT_TRUE(parse("ld", "-macosx_version_min", "10.7", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000132 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _context.os());
133 EXPECT_TRUE(_context.minOS("10.7", ""));
134 EXPECT_FALSE(_context.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000135}
136
137TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000138 EXPECT_TRUE(parse("ld", "-macosx_version_min", "10.8.3", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000139 EXPECT_EQ(MachOLinkingContext::OS::macOSX, _context.os());
140 EXPECT_TRUE(_context.minOS("10.7", ""));
141 EXPECT_TRUE(_context.minOS("10.8", ""));
Nick Kledzika1210532013-07-18 23:13:13 +0000142}
143
144TEST_F(DarwinLdParserTest, iOS5) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000145 EXPECT_TRUE(parse("ld", "-ios_version_min", "5.0", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000146 EXPECT_EQ(MachOLinkingContext::OS::iOS, _context.os());
147 EXPECT_TRUE(_context.minOS("", "5.0"));
148 EXPECT_FALSE(_context.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000149}
150
151TEST_F(DarwinLdParserTest, iOS6) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000152 EXPECT_TRUE(parse("ld", "-ios_version_min", "6.0", "foo.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000153 EXPECT_EQ(MachOLinkingContext::OS::iOS, _context.os());
154 EXPECT_TRUE(_context.minOS("", "5.0"));
155 EXPECT_TRUE(_context.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000156}
157
158TEST_F(DarwinLdParserTest, iOS_Simulator5) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000159 EXPECT_TRUE(parse("ld", "-ios_simulator_version_min", "5.0", "a.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000160 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _context.os());
161 EXPECT_TRUE(_context.minOS("", "5.0"));
162 EXPECT_FALSE(_context.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000163}
164
165TEST_F(DarwinLdParserTest, iOS_Simulator6) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000166 EXPECT_TRUE(parse("ld", "-ios_simulator_version_min", "6.0", "a.o", nullptr));
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000167 EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _context.os());
168 EXPECT_TRUE(_context.minOS("", "5.0"));
169 EXPECT_TRUE(_context.minOS("", "6.0"));
Nick Kledzika1210532013-07-18 23:13:13 +0000170}
171
Nick Kledzike773e322013-09-10 23:55:14 +0000172TEST_F(DarwinLdParserTest, compatibilityVersion) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000173 EXPECT_TRUE(
Nick Kledzike773e322013-09-10 23:55:14 +0000174 parse("ld", "-dylib", "-compatibility_version", "1.2.3", "a.o", nullptr));
175 EXPECT_EQ(_context.compatibilityVersion(), 0x10203U);
176}
177
178TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000179 EXPECT_FALSE(parse("ld", "-bundle", "-compatibility_version", "1.2.3", "a.o",
180 nullptr));
Nick Kledzike773e322013-09-10 23:55:14 +0000181}
182
183TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000184 EXPECT_FALSE(parse("ld", "-bundle", "-compatibility_version", "1,2,3", "a.o",
185 nullptr));
Nick Kledzike773e322013-09-10 23:55:14 +0000186}
187
188TEST_F(DarwinLdParserTest, currentVersion) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000189 EXPECT_TRUE(
Nick Kledzike773e322013-09-10 23:55:14 +0000190 parse("ld", "-dylib", "-current_version", "1.2.3", "a.o", nullptr));
191 EXPECT_EQ(_context.currentVersion(), 0x10203U);
192}
193
194TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000195 EXPECT_FALSE(
Nick Kledzike773e322013-09-10 23:55:14 +0000196 parse("ld", "-bundle", "-current_version", "1.2.3", "a.o", nullptr));
197}
198
199TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000200 EXPECT_FALSE(
Nick Kledzike773e322013-09-10 23:55:14 +0000201 parse("ld", "-bundle", "-current_version", "1,2,3", "a.o", nullptr));
202}
203
204TEST_F(DarwinLdParserTest, bundleLoader) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000205 EXPECT_TRUE(
Nick Kledzike773e322013-09-10 23:55:14 +0000206 parse("ld", "-bundle", "-bundle_loader", "/bin/ls", "a.o", nullptr));
207 EXPECT_EQ(_context.bundleLoader(), "/bin/ls");
208}
209
210TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000211 EXPECT_FALSE(parse("ld", "-bundle_loader", "/bin/ls", "a.o", nullptr));
Nick Kledzike773e322013-09-10 23:55:14 +0000212}
213
214TEST_F(DarwinLdParserTest, deadStrippableDylib) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000215 EXPECT_TRUE(
Nick Kledzike773e322013-09-10 23:55:14 +0000216 parse("ld", "-dylib", "-mark_dead_strippable_dylib", "a.o", nullptr));
217 EXPECT_EQ(true, _context.deadStrippableDylib());
218}
219
220TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000221 EXPECT_FALSE(parse("ld", "-mark_dead_strippable_dylib", "a.o", nullptr));
Nick Kledzike773e322013-09-10 23:55:14 +0000222}
223
Rui Ueyama9f56df82013-06-11 23:07:43 +0000224} // end anonymous namespace