blob: 427b59a8b70a325be4b46b8adb2a55d3b12f3012 [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 Kledzik2458bec2014-07-16 19:49:02 +000014#include "MachOPasses.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000015
16#include "lld/Core/PassManager.h"
Nick Kledzik8fc67fb2014-08-13 23:55:41 +000017#include "lld/Driver/DarwinInputGraph.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000018#include "lld/ReaderWriter/Reader.h"
19#include "lld/ReaderWriter/Writer.h"
20#include "lld/Passes/LayoutPass.h"
Shankar Easwaran2bc24922013-10-29 05:12:14 +000021#include "lld/Passes/RoundTripYAMLPass.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000022
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Triple.h"
Tim Northover77d82202014-07-10 11:21:06 +000025#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000026#include "llvm/Support/Host.h"
Nick Kledzik473933b2013-09-27 22:50:00 +000027#include "llvm/Support/MachO.h"
Tim Northover77d82202014-07-10 11:21:06 +000028#include "llvm/Support/Path.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000029
Rui Ueyama57a29532014-08-06 19:37:35 +000030#include <algorithm>
31
Nick Kledzik2458bec2014-07-16 19:49:02 +000032using lld::mach_o::ArchHandler;
Nick Kledzik8fc67fb2014-08-13 23:55:41 +000033using lld::mach_o::MachODylibFile;
Nick Kledzike34182f2013-11-06 21:36:55 +000034using namespace llvm::MachO;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000035
36namespace lld {
37
Nick Kledzike850d9d2013-09-10 23:46:57 +000038bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) {
39 result = 0;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000040
41 if (str.empty())
42 return false;
43
44 SmallVector<StringRef, 3> parts;
45 llvm::SplitString(str, parts, ".");
46
47 unsigned long long num;
48 if (llvm::getAsUnsignedInteger(parts[0], 10, num))
49 return true;
50 if (num > 65535)
51 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000052 result = num << 16;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000053
54 if (parts.size() > 1) {
55 if (llvm::getAsUnsignedInteger(parts[1], 10, num))
56 return true;
57 if (num > 255)
58 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000059 result |= (num << 8);
Rui Ueyama0ca149f2013-08-06 22:31:59 +000060 }
61
62 if (parts.size() > 2) {
63 if (llvm::getAsUnsignedInteger(parts[2], 10, num))
64 return true;
65 if (num > 255)
66 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000067 result |= num;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000068 }
69
70 return false;
71}
72
Rui Ueyama0ca149f2013-08-06 22:31:59 +000073
Nick Kledzike34182f2013-11-06 21:36:55 +000074MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = {
75 { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL },
76 { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL },
77 { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL },
78 { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 },
79 { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 },
80 { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S },
81 { "", arch_unknown,false, 0, 0 }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000082};
83
84MachOLinkingContext::Arch
85MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) {
Nick Kledzike34182f2013-11-06 21:36:55 +000086 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
87 if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype))
Rui Ueyama0ca149f2013-08-06 22:31:59 +000088 return info->arch;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000089 }
90 return arch_unknown;
91}
92
93MachOLinkingContext::Arch
94MachOLinkingContext::archFromName(StringRef archName) {
Nick Kledzike34182f2013-11-06 21:36:55 +000095 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
96 if (info->archName.equals(archName))
Rui Ueyama0ca149f2013-08-06 22:31:59 +000097 return info->arch;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000098 }
99 return arch_unknown;
100}
101
Nick Kledzike5552772013-12-19 21:58:00 +0000102StringRef MachOLinkingContext::nameFromArch(Arch arch) {
103 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
104 if (info->arch == arch)
105 return info->archName;
106 }
107 return "<unknown>";
108}
109
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000110uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) {
111 assert(arch != arch_unknown);
Nick Kledzike34182f2013-11-06 21:36:55 +0000112 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
113 if (info->arch == arch)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000114 return info->cputype;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000115 }
116 llvm_unreachable("Unknown arch type");
117}
118
119uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) {
120 assert(arch != arch_unknown);
Nick Kledzike34182f2013-11-06 21:36:55 +0000121 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
122 if (info->arch == arch)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000123 return info->cpusubtype;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000124 }
125 llvm_unreachable("Unknown arch type");
126}
127
128MachOLinkingContext::MachOLinkingContext()
Tim Northoverd30a1f22014-06-20 15:59:00 +0000129 : _outputMachOType(MH_EXECUTE), _outputMachOTypeStatic(false),
Nick Kledzike850d9d2013-09-10 23:46:57 +0000130 _doNothing(false), _arch(arch_unknown), _os(OS::macOSX), _osMinVersion(0),
Nick Kledzik6960b072013-12-21 01:47:17 +0000131 _pageZeroSize(0), _pageSize(4096), _compatibilityVersion(0),
Nick Kledzik0224e342014-05-14 21:32:21 +0000132 _currentVersion(0), _deadStrippableDylib(false), _printAtoms(false),
Nick Kledzik2458bec2014-07-16 19:49:02 +0000133 _testingLibResolution(false), _archHandler(nullptr) {}
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000134
135MachOLinkingContext::~MachOLinkingContext() {}
136
Nick Kledzik6960b072013-12-21 01:47:17 +0000137void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os,
138 uint32_t minOSVersion) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000139 _outputMachOType = type;
Nick Kledzik6960b072013-12-21 01:47:17 +0000140 _arch = arch;
141 _os = os;
142 _osMinVersion = minOSVersion;
143
Tim Northoverd30a1f22014-06-20 15:59:00 +0000144 switch (_outputMachOType) {
Nick Kledzik6960b072013-12-21 01:47:17 +0000145 case llvm::MachO::MH_EXECUTE:
146 // If targeting newer OS, use _main
147 if (minOS("10.8", "6.0")) {
148 _entrySymbolName = "_main";
149 } else {
150 // If targeting older OS, use start (in crt1.o)
151 _entrySymbolName = "start";
152 }
153
154 // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not
155 // support) and 4KB on 32-bit.
156 if (is64Bit(_arch)) {
157 _pageZeroSize = 0x100000000;
158 } else {
159 _pageZeroSize = 0x1000;
160 }
161
162 break;
163 case llvm::MachO::MH_DYLIB:
164 _globalsAreDeadStripRoots = true;
165 break;
166 case llvm::MachO::MH_BUNDLE:
167 break;
168 case llvm::MachO::MH_OBJECT:
169 _printRemainingUndefines = false;
170 _allowRemainingUndefines = true;
171 default:
172 break;
173 }
174}
175
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000176uint32_t MachOLinkingContext::getCPUType() const {
177 return cpuTypeFromArch(_arch);
178}
179
180uint32_t MachOLinkingContext::getCPUSubType() const {
181 return cpuSubtypeFromArch(_arch);
182}
183
Nick Kledzike34182f2013-11-06 21:36:55 +0000184bool MachOLinkingContext::is64Bit(Arch arch) {
185 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
186 if (info->arch == arch) {
187 return (info->cputype & CPU_ARCH_ABI64);
188 }
189 }
190 // unknown archs are not 64-bit.
191 return false;
192}
193
194bool MachOLinkingContext::isHostEndian(Arch arch) {
195 assert(arch != arch_unknown);
196 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
197 if (info->arch == arch) {
198 return (info->littleEndian == llvm::sys::IsLittleEndianHost);
199 }
200 }
201 llvm_unreachable("Unknown arch type");
202}
203
204bool MachOLinkingContext::isBigEndian(Arch arch) {
205 assert(arch != arch_unknown);
206 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
207 if (info->arch == arch) {
208 return ! info->littleEndian;
209 }
210 }
211 llvm_unreachable("Unknown arch type");
212}
213
214
215
216bool MachOLinkingContext::is64Bit() const {
217 return is64Bit(_arch);
218}
219
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000220bool MachOLinkingContext::outputTypeHasEntry() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000221 switch (_outputMachOType) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000222 case MH_EXECUTE:
223 case MH_DYLINKER:
224 case MH_PRELOAD:
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000225 return true;
226 default:
227 return false;
228 }
229}
230
Nick Kledzik2458bec2014-07-16 19:49:02 +0000231bool MachOLinkingContext::needsStubsPass() const {
232 switch (_outputMachOType) {
233 case MH_EXECUTE:
234 return !_outputMachOTypeStatic;
235 case MH_DYLIB:
236 case MH_BUNDLE:
237 return true;
238 default:
239 return false;
240 }
241}
242
243bool MachOLinkingContext::needsGOTPass() const {
244 // Only x86_64 uses GOT pass but not in -r mode.
245 if (_arch != arch_x86_64)
246 return false;
247 return (_outputMachOType != MH_OBJECT);
248}
249
250
251StringRef MachOLinkingContext::binderSymbolName() const {
252 return archHandler().stubInfo().binderSymbolName;
253}
254
255
256
257
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000258bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const {
Nick Kledzik30332b12013-10-08 00:43:34 +0000259 uint32_t parsedVersion;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000260 switch (_os) {
Nick Kledzik30332b12013-10-08 00:43:34 +0000261 case OS::macOSX:
Nick Kledzike850d9d2013-09-10 23:46:57 +0000262 if (parsePackedVersion(mac, parsedVersion))
263 return false;
264 return _osMinVersion >= parsedVersion;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000265 case OS::iOS:
Nick Kledzik30332b12013-10-08 00:43:34 +0000266 case OS::iOS_simulator:
Nick Kledzike850d9d2013-09-10 23:46:57 +0000267 if (parsePackedVersion(iOS, parsedVersion))
268 return false;
269 return _osMinVersion >= parsedVersion;
Nick Kledzik30332b12013-10-08 00:43:34 +0000270 case OS::unknown:
271 break;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000272 }
273 llvm_unreachable("target not configured for iOS or MacOSX");
274}
275
276bool MachOLinkingContext::addEntryPointLoadCommand() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000277 if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000278 return minOS("10.8", "6.0");
279 }
280 return false;
281}
282
283bool MachOLinkingContext::addUnixThreadLoadCommand() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000284 switch (_outputMachOType) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000285 case MH_EXECUTE:
Tim Northoverd30a1f22014-06-20 15:59:00 +0000286 if (_outputMachOTypeStatic)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000287 return true;
288 else
289 return !minOS("10.8", "6.0");
290 break;
Nick Kledzike34182f2013-11-06 21:36:55 +0000291 case MH_DYLINKER:
292 case MH_PRELOAD:
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000293 return true;
294 default:
295 return false;
296 }
297}
298
Tim Northover77d82202014-07-10 11:21:06 +0000299bool MachOLinkingContext::pathExists(StringRef path) const {
300 if (!testingLibResolution())
301 return llvm::sys::fs::exists(path.str());
302
303 // Otherwise, we're in test mode: only files explicitly provided on the
304 // command-line exist.
Rui Ueyama57a29532014-08-06 19:37:35 +0000305 std::string key = path.str();
306 std::replace(key.begin(), key.end(), '\\', '/');
307 return _existingPaths.find(key) != _existingPaths.end();
Tim Northover77d82202014-07-10 11:21:06 +0000308}
309
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000310void MachOLinkingContext::addModifiedSearchDir(StringRef libPath,
311 bool isSystemPath) {
Tim Northover77d82202014-07-10 11:21:06 +0000312 bool addedModifiedPath = false;
313
314 // Two cases to consider here:
315 // + If the last -syslibroot is "/", all of them are ignored (don't ask).
316 // + -syslibroot only applies to absolute paths.
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000317 if (!_syslibRoots.empty() && _syslibRoots.back() != "/" &&
Rui Ueyama57a29532014-08-06 19:37:35 +0000318 libPath.startswith("/")) {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000319 for (auto syslibRoot : _syslibRoots) {
Tim Northover77d82202014-07-10 11:21:06 +0000320 SmallString<256> path(syslibRoot);
321 llvm::sys::path::append(path, libPath);
322 if (pathExists(path)) {
323 _searchDirs.push_back(path.str().copy(_allocator));
324 addedModifiedPath = true;
325 }
326 }
327 }
328
329 if (addedModifiedPath)
330 return;
331
332 // Finally, if only one -syslibroot is given, system paths which aren't in it
333 // get suppressed.
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000334 if (_syslibRoots.size() != 1 || !isSystemPath) {
Tim Northover77d82202014-07-10 11:21:06 +0000335 if (pathExists(libPath)) {
336 _searchDirs.push_back(libPath);
337 }
338 }
339}
340
341ErrorOr<StringRef>
342MachOLinkingContext::searchDirForLibrary(StringRef path,
343 StringRef libName) const {
344 SmallString<256> fullPath;
345 if (libName.endswith(".o")) {
346 // A request ending in .o is special: just search for the file directly.
347 fullPath.assign(path);
348 llvm::sys::path::append(fullPath, libName);
349 if (pathExists(fullPath))
350 return fullPath.str().copy(_allocator);
351 return make_error_code(llvm::errc::no_such_file_or_directory);
352 }
353
354 // Search for dynamic library
355 fullPath.assign(path);
356 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib");
357 if (pathExists(fullPath))
358 return fullPath.str().copy(_allocator);
359
360 // If not, try for a static library
361 fullPath.assign(path);
362 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a");
363 if (pathExists(fullPath))
364 return fullPath.str().copy(_allocator);
365
366 return make_error_code(llvm::errc::no_such_file_or_directory);
367}
368
369
370
371ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const {
372 SmallString<256> path;
373 for (StringRef dir : searchDirs()) {
374 ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName);
375 if (ec)
376 return ec;
377 }
378
379 return make_error_code(llvm::errc::no_such_file_or_directory);
380}
381
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000382bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000383 // TODO: if -arch not specified, look at arch of first .o file.
384
Tim Northoverd30a1f22014-06-20 15:59:00 +0000385 if (_currentVersion && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000386 diagnostics << "error: -current_version can only be used with dylibs\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000387 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000388 }
389
Tim Northoverd30a1f22014-06-20 15:59:00 +0000390 if (_compatibilityVersion && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000391 diagnostics
392 << "error: -compatibility_version can only be used with dylibs\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000393 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000394 }
395
Tim Northoverd30a1f22014-06-20 15:59:00 +0000396 if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000397 diagnostics
398 << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000399 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000400 }
401
Tim Northoverd30a1f22014-06-20 15:59:00 +0000402 if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) {
Nick Kledzike773e322013-09-10 23:55:14 +0000403 diagnostics
404 << "error: -bundle_loader can only be used with Mach-O bundles\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000405 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000406 }
407
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000408 return true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000409}
410
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000411void MachOLinkingContext::addPasses(PassManager &pm) {
Nico Rieckb9d84f42014-02-24 21:14:37 +0000412 pm.add(std::unique_ptr<Pass>(new LayoutPass(registry())));
Nick Kledzik2458bec2014-07-16 19:49:02 +0000413 if (needsStubsPass())
414 mach_o::addStubsPass(pm, *this);
415 if (needsGOTPass())
416 mach_o::addGOTPass(pm, *this);
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000417}
418
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000419Writer &MachOLinkingContext::writer() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000420 if (!_writer)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000421 _writer = createWriterMachO(*this);
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000422 return *_writer;
423}
424
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000425MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) const {
426 std::unique_ptr<MachOFileNode> node(new MachOFileNode(path, false));
427 std::error_code ec = node->parse(*this, llvm::errs());
428 if (ec)
429 return nullptr;
430
431 assert(node->files().size() == 1 && "expected one file in dylib");
432 // lld::File object is owned by MachOFileNode object. This method returns
433 // an unowned pointer to the lld::File object.
434 MachODylibFile* result = reinterpret_cast<MachODylibFile*>(
435 node->files().front().get());
436
437 // Node object now owned by _indirectDylibs vector.
438 _indirectDylibs.push_back(std::move(node));
439
440 return result;
441}
442
443
444MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) const {
445 // See if already loaded.
446 auto pos = _pathToDylibMap.find(path);
447 if (pos != _pathToDylibMap.end())
448 return pos->second;
449
450 // Search -L paths if of the form "libXXX.dylib"
451 std::pair<StringRef, StringRef> split = path.rsplit('/');
452 StringRef leafName = split.second;
453 if (leafName.startswith("lib") && leafName.endswith(".dylib")) {
454 // FIXME: Need to enhance searchLibrary() to only look for .dylib
455 auto libPath = searchLibrary(leafName);
456 if (!libPath.getError()) {
457 return loadIndirectDylib(libPath.get());
458 }
459 }
460
461 // Try full path with sysroot.
462 for (StringRef sysPath : _syslibRoots) {
463 SmallString<256> fullPath;
464 fullPath.assign(sysPath);
465 llvm::sys::path::append(fullPath, path);
466 if (pathExists(fullPath))
467 return loadIndirectDylib(fullPath);
468 }
469
470 // Try full path.
471 if (pathExists(path)) {
472 return loadIndirectDylib(path);
473 }
474
475 return nullptr;
476}
477
478bool MachOLinkingContext::createImplicitFiles(
479 std::vector<std::unique_ptr<File> > &result) const {
480 // Add indirect dylibs by asking each linked dylib to add its indirects.
481 // Iterate until no more dylibs get loaded.
482 size_t dylibCount = 0;
483 while (dylibCount != _allDylibs.size()) {
484 dylibCount = _allDylibs.size();
485 for (MachODylibFile *dylib : _allDylibs) {
486 dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* {
487 return findIndirectDylib(path); });
488 }
489 }
490
491 // Let writer add output type specific extras.
492 return writer().createImplicitFiles(result);
493}
494
495
496void MachOLinkingContext::registerDylib(MachODylibFile *dylib) {
497 _allDylibs.insert(dylib);
498 _pathToDylibMap[dylib->installName()] = dylib;
499 // If path is different than install name, register path too.
500 if (!dylib->path().equals(dylib->installName()))
501 _pathToDylibMap[dylib->path()] = dylib;
502}
503
504
Nick Kledzik2458bec2014-07-16 19:49:02 +0000505ArchHandler &MachOLinkingContext::archHandler() const {
506 if (!_archHandler)
507 _archHandler = ArchHandler::create(_arch);
508 return *_archHandler;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000509}
510
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000511
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000512void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect,
513 uint8_t align2) {
514 SectionAlign entry;
515 entry.segmentName = seg;
516 entry.sectionName = sect;
517 entry.align2 = align2;
518 _sectAligns.push_back(entry);
519}
520
521bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect,
522 uint8_t &align2) const {
523 for (const SectionAlign &entry : _sectAligns) {
524 if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) {
525 align2 = entry.align2;
526 return true;
527 }
528 }
529 return false;
530}
531
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000532} // end namespace lld