blob: faefb3d2cb743a5596bab195710b16839be3198a [file] [log] [blame]
Rui Ueyama0ca149f2013-08-06 22:31:59 +00001//===- lib/ReaderWriter/MachO/MachOLinkingContext.cpp ---------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lld/ReaderWriter/MachOLinkingContext.h"
Nick Kledzik2458bec2014-07-16 19:49:02 +000011
12#include "ArchHandler.h"
Nick Kledzik8fc67fb2014-08-13 23:55:41 +000013#include "File.h"
Nick Kledzik635f9c72014-09-04 20:08:30 +000014#include "MachONormalizedFile.h"
Nick Kledzik2458bec2014-07-16 19:49:02 +000015#include "MachOPasses.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000016
17#include "lld/Core/PassManager.h"
Nick Kledzik8fc67fb2014-08-13 23:55:41 +000018#include "lld/Driver/DarwinInputGraph.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000019#include "lld/ReaderWriter/Reader.h"
20#include "lld/ReaderWriter/Writer.h"
21#include "lld/Passes/LayoutPass.h"
Shankar Easwaran2bc24922013-10-29 05:12:14 +000022#include "lld/Passes/RoundTripYAMLPass.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000023
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/Triple.h"
Tim Northover77d82202014-07-10 11:21:06 +000026#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000027#include "llvm/Support/Host.h"
Nick Kledzik473933b2013-09-27 22:50:00 +000028#include "llvm/Support/MachO.h"
Tim Northover77d82202014-07-10 11:21:06 +000029#include "llvm/Support/Path.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000030
Rui Ueyama57a29532014-08-06 19:37:35 +000031#include <algorithm>
32
Nick Kledzik2458bec2014-07-16 19:49:02 +000033using lld::mach_o::ArchHandler;
Nick Kledzik8fc67fb2014-08-13 23:55:41 +000034using lld::mach_o::MachODylibFile;
Nick Kledzike34182f2013-11-06 21:36:55 +000035using namespace llvm::MachO;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000036
37namespace lld {
38
Nick Kledzike850d9d2013-09-10 23:46:57 +000039bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) {
40 result = 0;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000041
42 if (str.empty())
43 return false;
44
45 SmallVector<StringRef, 3> parts;
46 llvm::SplitString(str, parts, ".");
47
48 unsigned long long num;
49 if (llvm::getAsUnsignedInteger(parts[0], 10, num))
50 return true;
51 if (num > 65535)
52 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000053 result = num << 16;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000054
55 if (parts.size() > 1) {
56 if (llvm::getAsUnsignedInteger(parts[1], 10, num))
57 return true;
58 if (num > 255)
59 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000060 result |= (num << 8);
Rui Ueyama0ca149f2013-08-06 22:31:59 +000061 }
62
63 if (parts.size() > 2) {
64 if (llvm::getAsUnsignedInteger(parts[2], 10, num))
65 return true;
66 if (num > 255)
67 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000068 result |= num;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000069 }
70
71 return false;
72}
73
Rui Ueyama0ca149f2013-08-06 22:31:59 +000074
Nick Kledzike34182f2013-11-06 21:36:55 +000075MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = {
76 { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL },
77 { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL },
78 { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL },
79 { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 },
80 { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 },
81 { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S },
Nick Kledzik1bebb282014-09-09 23:52:59 +000082 { "arm64", arch_arm64, true, CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL },
Nick Kledzike34182f2013-11-06 21:36:55 +000083 { "", arch_unknown,false, 0, 0 }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000084};
85
86MachOLinkingContext::Arch
87MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) {
Nick Kledzike34182f2013-11-06 21:36:55 +000088 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
89 if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype))
Rui Ueyama0ca149f2013-08-06 22:31:59 +000090 return info->arch;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000091 }
92 return arch_unknown;
93}
94
95MachOLinkingContext::Arch
96MachOLinkingContext::archFromName(StringRef archName) {
Nick Kledzike34182f2013-11-06 21:36:55 +000097 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
98 if (info->archName.equals(archName))
Rui Ueyama0ca149f2013-08-06 22:31:59 +000099 return info->arch;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000100 }
101 return arch_unknown;
102}
103
Nick Kledzike5552772013-12-19 21:58:00 +0000104StringRef MachOLinkingContext::nameFromArch(Arch arch) {
105 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
106 if (info->arch == arch)
107 return info->archName;
108 }
109 return "<unknown>";
110}
111
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000112uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) {
113 assert(arch != arch_unknown);
Nick Kledzike34182f2013-11-06 21:36:55 +0000114 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
115 if (info->arch == arch)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000116 return info->cputype;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000117 }
118 llvm_unreachable("Unknown arch type");
119}
120
121uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) {
122 assert(arch != arch_unknown);
Nick Kledzike34182f2013-11-06 21:36:55 +0000123 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
124 if (info->arch == arch)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000125 return info->cpusubtype;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000126 }
127 llvm_unreachable("Unknown arch type");
128}
129
Nick Kledzik635f9c72014-09-04 20:08:30 +0000130bool MachOLinkingContext::isThinObjectFile(StringRef path, Arch &arch) {
131 return mach_o::normalized::isThinObjectFile(path, arch);
132}
133
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000134MachOLinkingContext::MachOLinkingContext()
Tim Northoverd30a1f22014-06-20 15:59:00 +0000135 : _outputMachOType(MH_EXECUTE), _outputMachOTypeStatic(false),
Tim Northoveraf3075b2014-09-10 10:39:57 +0000136 _doNothing(false), _pie(false), _arch(arch_unknown), _os(OS::macOSX),
137 _osMinVersion(0), _pageZeroSize(0), _pageSize(4096), _baseAddress(0),
138 _compatibilityVersion(0), _currentVersion(0), _deadStrippableDylib(false),
139 _printAtoms(false), _testingFileUsage(false), _keepPrivateExterns(false),
Nick Kledzik8c0bf752014-08-21 01:59:11 +0000140 _archHandler(nullptr), _exportMode(ExportMode::globals) {}
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000141
142MachOLinkingContext::~MachOLinkingContext() {}
143
Nick Kledzik6960b072013-12-21 01:47:17 +0000144void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os,
145 uint32_t minOSVersion) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000146 _outputMachOType = type;
Nick Kledzik6960b072013-12-21 01:47:17 +0000147 _arch = arch;
148 _os = os;
149 _osMinVersion = minOSVersion;
150
Tim Northoverd30a1f22014-06-20 15:59:00 +0000151 switch (_outputMachOType) {
Nick Kledzik6960b072013-12-21 01:47:17 +0000152 case llvm::MachO::MH_EXECUTE:
153 // If targeting newer OS, use _main
154 if (minOS("10.8", "6.0")) {
155 _entrySymbolName = "_main";
156 } else {
157 // If targeting older OS, use start (in crt1.o)
158 _entrySymbolName = "start";
159 }
160
161 // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not
162 // support) and 4KB on 32-bit.
163 if (is64Bit(_arch)) {
164 _pageZeroSize = 0x100000000;
165 } else {
166 _pageZeroSize = 0x1000;
167 }
168
Nick Kledzikb7035ae2014-09-09 00:17:52 +0000169 // Make PIE by default when targetting newer OSs.
170 switch (os) {
171 case OS::macOSX:
172 if (minOSVersion >= 0x000A0700) // MacOSX 10.7
173 _pie = true;
174 break;
175 case OS::iOS:
176 if (minOSVersion >= 0x00040300) // iOS 4.3
177 _pie = true;
178 break;
179 case OS::iOS_simulator:
180 _pie = true;
181 break;
182 case OS::unknown:
183 break;
184 }
Nick Kledzik6960b072013-12-21 01:47:17 +0000185 break;
186 case llvm::MachO::MH_DYLIB:
187 _globalsAreDeadStripRoots = true;
188 break;
189 case llvm::MachO::MH_BUNDLE:
190 break;
191 case llvm::MachO::MH_OBJECT:
192 _printRemainingUndefines = false;
193 _allowRemainingUndefines = true;
194 default:
195 break;
196 }
Nick Kledzik1bebb282014-09-09 23:52:59 +0000197
198 // Set default segment page sizes based on arch.
199 if (arch == arch_arm64)
200 _pageSize = 4*4096;
Nick Kledzik6960b072013-12-21 01:47:17 +0000201}
202
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000203uint32_t MachOLinkingContext::getCPUType() const {
204 return cpuTypeFromArch(_arch);
205}
206
207uint32_t MachOLinkingContext::getCPUSubType() const {
208 return cpuSubtypeFromArch(_arch);
209}
210
Nick Kledzike34182f2013-11-06 21:36:55 +0000211bool MachOLinkingContext::is64Bit(Arch arch) {
212 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
213 if (info->arch == arch) {
214 return (info->cputype & CPU_ARCH_ABI64);
215 }
216 }
217 // unknown archs are not 64-bit.
218 return false;
219}
220
221bool MachOLinkingContext::isHostEndian(Arch arch) {
222 assert(arch != arch_unknown);
223 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
224 if (info->arch == arch) {
225 return (info->littleEndian == llvm::sys::IsLittleEndianHost);
226 }
227 }
228 llvm_unreachable("Unknown arch type");
229}
230
231bool MachOLinkingContext::isBigEndian(Arch arch) {
232 assert(arch != arch_unknown);
233 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
234 if (info->arch == arch) {
235 return ! info->littleEndian;
236 }
237 }
238 llvm_unreachable("Unknown arch type");
239}
240
241
242
243bool MachOLinkingContext::is64Bit() const {
244 return is64Bit(_arch);
245}
246
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000247bool MachOLinkingContext::outputTypeHasEntry() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000248 switch (_outputMachOType) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000249 case MH_EXECUTE:
250 case MH_DYLINKER:
251 case MH_PRELOAD:
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000252 return true;
253 default:
254 return false;
255 }
256}
257
Nick Kledzik2458bec2014-07-16 19:49:02 +0000258bool MachOLinkingContext::needsStubsPass() const {
259 switch (_outputMachOType) {
260 case MH_EXECUTE:
261 return !_outputMachOTypeStatic;
262 case MH_DYLIB:
263 case MH_BUNDLE:
264 return true;
265 default:
266 return false;
267 }
268}
269
270bool MachOLinkingContext::needsGOTPass() const {
Nick Kledzik1bebb282014-09-09 23:52:59 +0000271 // GOT pass not used in -r mode.
272 if (_outputMachOType == MH_OBJECT)
Nick Kledzik2458bec2014-07-16 19:49:02 +0000273 return false;
Nick Kledzik1bebb282014-09-09 23:52:59 +0000274 // Only some arches use GOT pass.
275 switch (_arch) {
276 case arch_x86_64:
277 case arch_arm64:
278 return true;
279 default:
280 return false;
281 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000282}
283
284
285StringRef MachOLinkingContext::binderSymbolName() const {
286 return archHandler().stubInfo().binderSymbolName;
287}
288
289
290
291
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000292bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const {
Nick Kledzik30332b12013-10-08 00:43:34 +0000293 uint32_t parsedVersion;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000294 switch (_os) {
Nick Kledzik30332b12013-10-08 00:43:34 +0000295 case OS::macOSX:
Nick Kledzike850d9d2013-09-10 23:46:57 +0000296 if (parsePackedVersion(mac, parsedVersion))
297 return false;
298 return _osMinVersion >= parsedVersion;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000299 case OS::iOS:
Nick Kledzik30332b12013-10-08 00:43:34 +0000300 case OS::iOS_simulator:
Nick Kledzike850d9d2013-09-10 23:46:57 +0000301 if (parsePackedVersion(iOS, parsedVersion))
302 return false;
303 return _osMinVersion >= parsedVersion;
Nick Kledzik30332b12013-10-08 00:43:34 +0000304 case OS::unknown:
305 break;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000306 }
307 llvm_unreachable("target not configured for iOS or MacOSX");
308}
309
310bool MachOLinkingContext::addEntryPointLoadCommand() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000311 if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000312 return minOS("10.8", "6.0");
313 }
314 return false;
315}
316
317bool MachOLinkingContext::addUnixThreadLoadCommand() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000318 switch (_outputMachOType) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000319 case MH_EXECUTE:
Tim Northoverd30a1f22014-06-20 15:59:00 +0000320 if (_outputMachOTypeStatic)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000321 return true;
322 else
323 return !minOS("10.8", "6.0");
324 break;
Nick Kledzike34182f2013-11-06 21:36:55 +0000325 case MH_DYLINKER:
326 case MH_PRELOAD:
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000327 return true;
328 default:
329 return false;
330 }
331}
332
Tim Northover77d82202014-07-10 11:21:06 +0000333bool MachOLinkingContext::pathExists(StringRef path) const {
Nick Kledzik94174f72014-08-15 19:53:41 +0000334 if (!_testingFileUsage)
Tim Northover77d82202014-07-10 11:21:06 +0000335 return llvm::sys::fs::exists(path.str());
336
337 // Otherwise, we're in test mode: only files explicitly provided on the
338 // command-line exist.
Rui Ueyama57a29532014-08-06 19:37:35 +0000339 std::string key = path.str();
340 std::replace(key.begin(), key.end(), '\\', '/');
341 return _existingPaths.find(key) != _existingPaths.end();
Tim Northover77d82202014-07-10 11:21:06 +0000342}
343
Nick Kledzik2d835da2014-08-14 22:20:41 +0000344void MachOLinkingContext::setSysLibRoots(const StringRefVector &paths) {
345 _syslibRoots = paths;
346}
347
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000348void MachOLinkingContext::addModifiedSearchDir(StringRef libPath,
349 bool isSystemPath) {
Tim Northover77d82202014-07-10 11:21:06 +0000350 bool addedModifiedPath = false;
351
Nick Kledzik2d835da2014-08-14 22:20:41 +0000352 // -syslibroot only applies to absolute paths.
353 if (libPath.startswith("/")) {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000354 for (auto syslibRoot : _syslibRoots) {
Tim Northover77d82202014-07-10 11:21:06 +0000355 SmallString<256> path(syslibRoot);
356 llvm::sys::path::append(path, libPath);
357 if (pathExists(path)) {
358 _searchDirs.push_back(path.str().copy(_allocator));
359 addedModifiedPath = true;
360 }
361 }
362 }
363
364 if (addedModifiedPath)
365 return;
366
367 // Finally, if only one -syslibroot is given, system paths which aren't in it
368 // get suppressed.
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000369 if (_syslibRoots.size() != 1 || !isSystemPath) {
Tim Northover77d82202014-07-10 11:21:06 +0000370 if (pathExists(libPath)) {
371 _searchDirs.push_back(libPath);
372 }
373 }
374}
375
Nick Kledzik2d835da2014-08-14 22:20:41 +0000376void MachOLinkingContext::addFrameworkSearchDir(StringRef fwPath,
377 bool isSystemPath) {
378 bool pathAdded = false;
379
380 // -syslibroot only used with to absolute framework search paths.
381 if (fwPath.startswith("/")) {
382 for (auto syslibRoot : _syslibRoots) {
383 SmallString<256> path(syslibRoot);
384 llvm::sys::path::append(path, fwPath);
385 if (pathExists(path)) {
386 _frameworkDirs.push_back(path.str().copy(_allocator));
387 pathAdded = true;
388 }
389 }
390 }
391 // If fwPath found in any -syslibroot, then done.
392 if (pathAdded)
393 return;
394
395 // If only one -syslibroot, system paths not in that SDK are suppressed.
396 if (isSystemPath && (_syslibRoots.size() == 1))
397 return;
398
399 // Only use raw fwPath if that directory exists.
400 if (pathExists(fwPath))
401 _frameworkDirs.push_back(fwPath);
402}
403
404
Tim Northover77d82202014-07-10 11:21:06 +0000405ErrorOr<StringRef>
406MachOLinkingContext::searchDirForLibrary(StringRef path,
407 StringRef libName) const {
408 SmallString<256> fullPath;
409 if (libName.endswith(".o")) {
410 // A request ending in .o is special: just search for the file directly.
411 fullPath.assign(path);
412 llvm::sys::path::append(fullPath, libName);
413 if (pathExists(fullPath))
414 return fullPath.str().copy(_allocator);
415 return make_error_code(llvm::errc::no_such_file_or_directory);
416 }
417
418 // Search for dynamic library
419 fullPath.assign(path);
420 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib");
421 if (pathExists(fullPath))
422 return fullPath.str().copy(_allocator);
423
424 // If not, try for a static library
425 fullPath.assign(path);
426 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a");
427 if (pathExists(fullPath))
428 return fullPath.str().copy(_allocator);
429
430 return make_error_code(llvm::errc::no_such_file_or_directory);
431}
432
433
434
435ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const {
436 SmallString<256> path;
437 for (StringRef dir : searchDirs()) {
438 ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName);
439 if (ec)
440 return ec;
441 }
442
443 return make_error_code(llvm::errc::no_such_file_or_directory);
444}
445
Nick Kledzik2d835da2014-08-14 22:20:41 +0000446
447ErrorOr<StringRef> MachOLinkingContext::findPathForFramework(StringRef fwName) const{
448 SmallString<256> fullPath;
449 for (StringRef dir : frameworkDirs()) {
450 fullPath.assign(dir);
451 llvm::sys::path::append(fullPath, Twine(fwName) + ".framework", fwName);
452 if (pathExists(fullPath))
453 return fullPath.str().copy(_allocator);
454 }
455
456 return make_error_code(llvm::errc::no_such_file_or_directory);
457}
458
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000459bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000460 // TODO: if -arch not specified, look at arch of first .o file.
461
Tim Northoverd30a1f22014-06-20 15:59:00 +0000462 if (_currentVersion && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000463 diagnostics << "error: -current_version can only be used with dylibs\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000464 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000465 }
466
Tim Northoverd30a1f22014-06-20 15:59:00 +0000467 if (_compatibilityVersion && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000468 diagnostics
469 << "error: -compatibility_version can only be used with dylibs\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000470 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000471 }
472
Tim Northoverd30a1f22014-06-20 15:59:00 +0000473 if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000474 diagnostics
475 << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000476 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000477 }
478
Tim Northoverd30a1f22014-06-20 15:59:00 +0000479 if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) {
Nick Kledzike773e322013-09-10 23:55:14 +0000480 diagnostics
481 << "error: -bundle_loader can only be used with Mach-O bundles\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000482 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000483 }
484
Nick Kledzik8c0bf752014-08-21 01:59:11 +0000485 // If -exported_symbols_list used, all exported symbols must be defined.
486 if (_exportMode == ExportMode::whiteList) {
487 for (const auto &symbol : _exportedSymbols)
488 addInitialUndefinedSymbol(symbol.getKey());
489 }
490
Nick Kledzik77afc712014-08-21 20:25:50 +0000491 // If -dead_strip, set up initial live symbols.
492 if (deadStrip()) {
493 // Entry point is live.
494 if (outputTypeHasEntry())
495 addDeadStripRoot(entrySymbolName());
496 // Lazy binding helper is live.
497 if (needsStubsPass())
498 addDeadStripRoot(binderSymbolName());
499 // If using -exported_symbols_list, make all exported symbols live.
500 if (_exportMode == ExportMode::whiteList) {
501 _globalsAreDeadStripRoots = false;
502 for (const auto &symbol : _exportedSymbols)
503 addDeadStripRoot(symbol.getKey());
504 }
505 }
506
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000507 return true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000508}
509
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000510void MachOLinkingContext::addPasses(PassManager &pm) {
Nico Rieckb9d84f42014-02-24 21:14:37 +0000511 pm.add(std::unique_ptr<Pass>(new LayoutPass(registry())));
Nick Kledzik2458bec2014-07-16 19:49:02 +0000512 if (needsStubsPass())
513 mach_o::addStubsPass(pm, *this);
514 if (needsGOTPass())
515 mach_o::addGOTPass(pm, *this);
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000516}
517
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000518Writer &MachOLinkingContext::writer() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000519 if (!_writer)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000520 _writer = createWriterMachO(*this);
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000521 return *_writer;
522}
523
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000524MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) const {
525 std::unique_ptr<MachOFileNode> node(new MachOFileNode(path, false));
526 std::error_code ec = node->parse(*this, llvm::errs());
527 if (ec)
528 return nullptr;
529
530 assert(node->files().size() == 1 && "expected one file in dylib");
531 // lld::File object is owned by MachOFileNode object. This method returns
532 // an unowned pointer to the lld::File object.
533 MachODylibFile* result = reinterpret_cast<MachODylibFile*>(
534 node->files().front().get());
535
536 // Node object now owned by _indirectDylibs vector.
537 _indirectDylibs.push_back(std::move(node));
538
539 return result;
540}
541
542
543MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) const {
544 // See if already loaded.
545 auto pos = _pathToDylibMap.find(path);
546 if (pos != _pathToDylibMap.end())
547 return pos->second;
548
549 // Search -L paths if of the form "libXXX.dylib"
550 std::pair<StringRef, StringRef> split = path.rsplit('/');
551 StringRef leafName = split.second;
552 if (leafName.startswith("lib") && leafName.endswith(".dylib")) {
553 // FIXME: Need to enhance searchLibrary() to only look for .dylib
554 auto libPath = searchLibrary(leafName);
555 if (!libPath.getError()) {
556 return loadIndirectDylib(libPath.get());
557 }
558 }
559
560 // Try full path with sysroot.
561 for (StringRef sysPath : _syslibRoots) {
562 SmallString<256> fullPath;
563 fullPath.assign(sysPath);
564 llvm::sys::path::append(fullPath, path);
565 if (pathExists(fullPath))
566 return loadIndirectDylib(fullPath);
567 }
568
569 // Try full path.
570 if (pathExists(path)) {
571 return loadIndirectDylib(path);
572 }
573
574 return nullptr;
575}
576
577bool MachOLinkingContext::createImplicitFiles(
578 std::vector<std::unique_ptr<File> > &result) const {
579 // Add indirect dylibs by asking each linked dylib to add its indirects.
580 // Iterate until no more dylibs get loaded.
581 size_t dylibCount = 0;
582 while (dylibCount != _allDylibs.size()) {
583 dylibCount = _allDylibs.size();
584 for (MachODylibFile *dylib : _allDylibs) {
585 dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* {
586 return findIndirectDylib(path); });
587 }
588 }
589
590 // Let writer add output type specific extras.
591 return writer().createImplicitFiles(result);
592}
593
594
595void MachOLinkingContext::registerDylib(MachODylibFile *dylib) {
596 _allDylibs.insert(dylib);
597 _pathToDylibMap[dylib->installName()] = dylib;
598 // If path is different than install name, register path too.
599 if (!dylib->path().equals(dylib->installName()))
600 _pathToDylibMap[dylib->path()] = dylib;
601}
602
603
Nick Kledzik2458bec2014-07-16 19:49:02 +0000604ArchHandler &MachOLinkingContext::archHandler() const {
605 if (!_archHandler)
606 _archHandler = ArchHandler::create(_arch);
607 return *_archHandler;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000608}
609
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000610
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000611void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect,
612 uint8_t align2) {
613 SectionAlign entry;
614 entry.segmentName = seg;
615 entry.sectionName = sect;
616 entry.align2 = align2;
617 _sectAligns.push_back(entry);
618}
619
620bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect,
621 uint8_t &align2) const {
622 for (const SectionAlign &entry : _sectAligns) {
623 if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) {
624 align2 = entry.align2;
625 return true;
626 }
627 }
628 return false;
629}
630
Nick Kledzik8c0bf752014-08-21 01:59:11 +0000631
632void MachOLinkingContext::addExportSymbol(StringRef sym) {
633 // FIXME: Support wildcards.
634 _exportedSymbols.insert(sym);
635}
636
637bool MachOLinkingContext::exportSymbolNamed(StringRef sym) const {
638 switch (_exportMode) {
639 case ExportMode::globals:
640 llvm_unreachable("exportSymbolNamed() should not be called in this mode");
641 break;
642 case ExportMode::whiteList:
643 return _exportedSymbols.count(sym);
644 case ExportMode::blackList:
645 return !_exportedSymbols.count(sym);
646 }
647}
648
649
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000650} // end namespace lld