]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - unittests/DriverTests/DarwinLdDriverTest.cpp
Vendor import of lld trunk r338150:
[FreeBSD/FreeBSD.git] / unittests / DriverTests / DarwinLdDriverTest.cpp
1 //===- 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 /// Darwin's ld driver tests.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "lld/Common/Driver.h"
16 #include "lld/ReaderWriter/MachOLinkingContext.h"
17 #include "llvm/BinaryFormat/MachO.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "gtest/gtest.h"
20
21 using namespace llvm;
22 using namespace lld;
23
24 namespace lld {
25 namespace mach_o {
26 bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx);
27 }
28 }
29
30 namespace {
31 class DarwinLdParserTest : public testing::Test {
32 protected:
33   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");
44     return mach_o::parse(args, _ctx);
45   }
46
47   MachOLinkingContext _ctx;
48 };
49 }
50
51 TEST_F(DarwinLdParserTest, Basic) {
52   EXPECT_TRUE(parse({"foo.o", "bar.o", "-arch", "i386"}));
53   EXPECT_FALSE(_ctx.allowRemainingUndefines());
54   EXPECT_FALSE(_ctx.deadStrip());
55   EXPECT_EQ(2, inputFileCount());
56   EXPECT_EQ("foo.o", inputFile(0));
57   EXPECT_EQ("bar.o", inputFile(1));
58 }
59
60 TEST_F(DarwinLdParserTest, Output) {
61   EXPECT_TRUE(parse({"-o", "my.out", "foo.o", "-arch", "i386"}));
62   EXPECT_EQ("my.out", _ctx.outputPath());
63 }
64
65 TEST_F(DarwinLdParserTest, Dylib) {
66   EXPECT_TRUE(parse({"-dylib", "foo.o", "-arch", "i386"}));
67   EXPECT_EQ(llvm::MachO::MH_DYLIB, _ctx.outputMachOType());
68 }
69
70 TEST_F(DarwinLdParserTest, Relocatable) {
71   EXPECT_TRUE(parse({"-r", "foo.o", "-arch", "i386"}));
72   EXPECT_EQ(llvm::MachO::MH_OBJECT, _ctx.outputMachOType());
73 }
74
75 TEST_F(DarwinLdParserTest, Bundle) {
76   EXPECT_TRUE(parse({"-bundle", "foo.o", "-arch", "i386"}));
77   EXPECT_EQ(llvm::MachO::MH_BUNDLE, _ctx.outputMachOType());
78 }
79
80 TEST_F(DarwinLdParserTest, Preload) {
81   EXPECT_TRUE(parse({"-preload", "foo.o", "-arch", "i386"}));
82   EXPECT_EQ(llvm::MachO::MH_PRELOAD, _ctx.outputMachOType());
83 }
84
85 TEST_F(DarwinLdParserTest, Static) {
86   EXPECT_TRUE(parse({"-static", "foo.o", "-arch", "i386"}));
87   EXPECT_EQ(llvm::MachO::MH_EXECUTE, _ctx.outputMachOType());
88 }
89
90 TEST_F(DarwinLdParserTest, Entry) {
91   EXPECT_TRUE(parse({"-e", "entryFunc", "foo.o", "-arch", "i386"}));
92   EXPECT_EQ("entryFunc", _ctx.entrySymbolName());
93 }
94
95 TEST_F(DarwinLdParserTest, DeadStrip) {
96   EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
97   EXPECT_TRUE(_ctx.deadStrip());
98 }
99
100 TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
101   EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
102   EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
103 }
104
105 TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
106   EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip", "foo.o"}));
107   EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
108 }
109
110 TEST_F(DarwinLdParserTest, DeadStripRootsRelocatable) {
111   EXPECT_TRUE(parse({"-arch", "x86_64", "-r", "-dead_strip", "foo.o"}));
112   EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
113 }
114
115 TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicExe) {
116   EXPECT_TRUE(
117       parse({"-arch", "x86_64", "-dead_strip", "-export_dynamic", "foo.o"}));
118   EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
119 }
120
121 TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicDylib) {
122   EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip",
123                      "-export_dynamic", "foo.o"}));
124   EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
125 }
126
127 TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicRelocatable) {
128   EXPECT_TRUE(parse(
129       {"-arch", "x86_64", "-r", "-dead_strip", "-export_dynamic", "foo.o"}));
130   EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
131 }
132
133 TEST_F(DarwinLdParserTest, Arch) {
134   EXPECT_TRUE(parse({"-arch", "x86_64", "foo.o"}));
135   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());
138 }
139
140 TEST_F(DarwinLdParserTest, Arch_x86) {
141   EXPECT_TRUE(parse({"-arch", "i386", "foo.o"}));
142   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());
145 }
146
147 TEST_F(DarwinLdParserTest, Arch_armv6) {
148   EXPECT_TRUE(parse({"-arch", "armv6", "foo.o"}));
149   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());
152 }
153
154 TEST_F(DarwinLdParserTest, Arch_armv7) {
155   EXPECT_TRUE(parse({"-arch", "armv7", "foo.o"}));
156   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());
159 }
160
161 TEST_F(DarwinLdParserTest, Arch_armv7s) {
162   EXPECT_TRUE(parse({"-arch", "armv7s", "foo.o"}));
163   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());
166 }
167
168 TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
169   EXPECT_TRUE(
170       parse({"-macosx_version_min", "10.7", "foo.o", "-arch", "x86_64"}));
171   EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
172   EXPECT_TRUE(_ctx.minOS("10.7", ""));
173   EXPECT_FALSE(_ctx.minOS("10.8", ""));
174 }
175
176 TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
177   EXPECT_TRUE(
178       parse({"-macosx_version_min", "10.8.3", "foo.o", "-arch", "x86_64"}));
179   EXPECT_EQ(MachOLinkingContext::OS::macOSX, _ctx.os());
180   EXPECT_TRUE(_ctx.minOS("10.7", ""));
181   EXPECT_TRUE(_ctx.minOS("10.8", ""));
182 }
183
184 TEST_F(DarwinLdParserTest, iOS5) {
185   EXPECT_TRUE(parse({"-ios_version_min", "5.0", "foo.o", "-arch", "armv7"}));
186   EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
187   EXPECT_TRUE(_ctx.minOS("", "5.0"));
188   EXPECT_FALSE(_ctx.minOS("", "6.0"));
189 }
190
191 TEST_F(DarwinLdParserTest, iOS6) {
192   EXPECT_TRUE(parse({"-ios_version_min", "6.0", "foo.o", "-arch", "armv7"}));
193   EXPECT_EQ(MachOLinkingContext::OS::iOS, _ctx.os());
194   EXPECT_TRUE(_ctx.minOS("", "5.0"));
195   EXPECT_TRUE(_ctx.minOS("", "6.0"));
196 }
197
198 TEST_F(DarwinLdParserTest, iOS_Simulator5) {
199   EXPECT_TRUE(
200       parse({"-ios_simulator_version_min", "5.0", "a.o", "-arch", "i386"}));
201   EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
202   EXPECT_TRUE(_ctx.minOS("", "5.0"));
203   EXPECT_FALSE(_ctx.minOS("", "6.0"));
204 }
205
206 TEST_F(DarwinLdParserTest, iOS_Simulator6) {
207   EXPECT_TRUE(
208       parse({"-ios_simulator_version_min", "6.0", "a.o", "-arch", "i386"}));
209   EXPECT_EQ(MachOLinkingContext::OS::iOS_simulator, _ctx.os());
210   EXPECT_TRUE(_ctx.minOS("", "5.0"));
211   EXPECT_TRUE(_ctx.minOS("", "6.0"));
212 }
213
214 TEST_F(DarwinLdParserTest, compatibilityVersion) {
215   EXPECT_TRUE(parse(
216       {"-dylib", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
217   EXPECT_EQ(_ctx.compatibilityVersion(), 0x10203U);
218 }
219
220 TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
221   EXPECT_FALSE(parse(
222       {"-bundle", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
223 }
224
225 TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
226   EXPECT_FALSE(parse(
227       {"-bundle", "-compatibility_version", "1,2,3", "a.o", "-arch", "i386"}));
228 }
229
230 TEST_F(DarwinLdParserTest, currentVersion) {
231   EXPECT_TRUE(
232       parse({"-dylib", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
233   EXPECT_EQ(_ctx.currentVersion(), 0x10203U);
234 }
235
236 TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
237   EXPECT_FALSE(
238       parse({"-bundle", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
239 }
240
241 TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
242   EXPECT_FALSE(
243       parse({"-bundle", "-current_version", "1,2,3", "a.o", "-arch", "i386"}));
244 }
245
246 TEST_F(DarwinLdParserTest, bundleLoader) {
247   EXPECT_TRUE(
248       parse({"-bundle", "-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
249   EXPECT_EQ(_ctx.bundleLoader(), "/bin/ls");
250 }
251
252 TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
253   EXPECT_FALSE(parse({"-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
254 }
255
256 TEST_F(DarwinLdParserTest, deadStrippableDylib) {
257   EXPECT_TRUE(
258       parse({"-dylib", "-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
259   EXPECT_EQ(true, _ctx.deadStrippableDylib());
260 }
261
262 TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
263   EXPECT_FALSE(parse({"-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
264 }