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