1 //===- lld/unittest/DarwinLdDriverTest.cpp --------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Darwin's ld driver tests.
13 //===----------------------------------------------------------------------===//
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"
26 bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx);
31 class DarwinLdParserTest : public testing::Test {
33 int inputFileCount() { return _ctx.getNodes().size(); }
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");
42 bool parse(std::vector<const char *> args) {
43 args.insert(args.begin(), "ld");
44 return mach_o::parse(args, _ctx);
47 MachOLinkingContext _ctx;
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));
60 TEST_F(DarwinLdParserTest, Output) {
61 EXPECT_TRUE(parse({"-o", "my.out", "foo.o", "-arch", "i386"}));
62 EXPECT_EQ("my.out", _ctx.outputPath());
65 TEST_F(DarwinLdParserTest, Dylib) {
66 EXPECT_TRUE(parse({"-dylib", "foo.o", "-arch", "i386"}));
67 EXPECT_EQ(llvm::MachO::MH_DYLIB, _ctx.outputMachOType());
70 TEST_F(DarwinLdParserTest, Relocatable) {
71 EXPECT_TRUE(parse({"-r", "foo.o", "-arch", "i386"}));
72 EXPECT_EQ(llvm::MachO::MH_OBJECT, _ctx.outputMachOType());
75 TEST_F(DarwinLdParserTest, Bundle) {
76 EXPECT_TRUE(parse({"-bundle", "foo.o", "-arch", "i386"}));
77 EXPECT_EQ(llvm::MachO::MH_BUNDLE, _ctx.outputMachOType());
80 TEST_F(DarwinLdParserTest, Preload) {
81 EXPECT_TRUE(parse({"-preload", "foo.o", "-arch", "i386"}));
82 EXPECT_EQ(llvm::MachO::MH_PRELOAD, _ctx.outputMachOType());
85 TEST_F(DarwinLdParserTest, Static) {
86 EXPECT_TRUE(parse({"-static", "foo.o", "-arch", "i386"}));
87 EXPECT_EQ(llvm::MachO::MH_EXECUTE, _ctx.outputMachOType());
90 TEST_F(DarwinLdParserTest, Entry) {
91 EXPECT_TRUE(parse({"-e", "entryFunc", "foo.o", "-arch", "i386"}));
92 EXPECT_EQ("entryFunc", _ctx.entrySymbolName());
95 TEST_F(DarwinLdParserTest, DeadStrip) {
96 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
97 EXPECT_TRUE(_ctx.deadStrip());
100 TEST_F(DarwinLdParserTest, DeadStripRootsExe) {
101 EXPECT_TRUE(parse({"-arch", "x86_64", "-dead_strip", "foo.o"}));
102 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
105 TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
106 EXPECT_TRUE(parse({"-arch", "x86_64", "-dylib", "-dead_strip", "foo.o"}));
107 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
110 TEST_F(DarwinLdParserTest, DeadStripRootsRelocatable) {
111 EXPECT_TRUE(parse({"-arch", "x86_64", "-r", "-dead_strip", "foo.o"}));
112 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
115 TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicExe) {
117 parse({"-arch", "x86_64", "-dead_strip", "-export_dynamic", "foo.o"}));
118 EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
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());
127 TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicRelocatable) {
129 {"-arch", "x86_64", "-r", "-dead_strip", "-export_dynamic", "foo.o"}));
130 EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
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());
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());
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());
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());
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());
168 TEST_F(DarwinLdParserTest, MinMacOSX10_7) {
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", ""));
176 TEST_F(DarwinLdParserTest, MinMacOSX10_8) {
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", ""));
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"));
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"));
198 TEST_F(DarwinLdParserTest, iOS_Simulator5) {
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"));
206 TEST_F(DarwinLdParserTest, iOS_Simulator6) {
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"));
214 TEST_F(DarwinLdParserTest, compatibilityVersion) {
216 {"-dylib", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
217 EXPECT_EQ(_ctx.compatibilityVersion(), 0x10203U);
220 TEST_F(DarwinLdParserTest, compatibilityVersionInvalidType) {
222 {"-bundle", "-compatibility_version", "1.2.3", "a.o", "-arch", "i386"}));
225 TEST_F(DarwinLdParserTest, compatibilityVersionInvalidValue) {
227 {"-bundle", "-compatibility_version", "1,2,3", "a.o", "-arch", "i386"}));
230 TEST_F(DarwinLdParserTest, currentVersion) {
232 parse({"-dylib", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
233 EXPECT_EQ(_ctx.currentVersion(), 0x10203U);
236 TEST_F(DarwinLdParserTest, currentVersionInvalidType) {
238 parse({"-bundle", "-current_version", "1.2.3", "a.o", "-arch", "i386"}));
241 TEST_F(DarwinLdParserTest, currentVersionInvalidValue) {
243 parse({"-bundle", "-current_version", "1,2,3", "a.o", "-arch", "i386"}));
246 TEST_F(DarwinLdParserTest, bundleLoader) {
248 parse({"-bundle", "-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
249 EXPECT_EQ(_ctx.bundleLoader(), "/bin/ls");
252 TEST_F(DarwinLdParserTest, bundleLoaderInvalidType) {
253 EXPECT_FALSE(parse({"-bundle_loader", "/bin/ls", "a.o", "-arch", "i386"}));
256 TEST_F(DarwinLdParserTest, deadStrippableDylib) {
258 parse({"-dylib", "-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));
259 EXPECT_EQ(true, _ctx.deadStrippableDylib());
262 TEST_F(DarwinLdParserTest, deadStrippableDylibInvalidType) {
263 EXPECT_FALSE(parse({"-mark_dead_strippable_dylib", "a.o", "-arch", "i386"}));