blob: 30ce7739732a2996aaefb7e66b008ef3bbe566c1 [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#include "ArchHandler.h"
Nick Kledzik8fc67fb2014-08-13 23:55:41 +000012#include "File.h"
Nick Kledzik635f9c72014-09-04 20:08:30 +000013#include "MachONormalizedFile.h"
Nick Kledzik2458bec2014-07-16 19:49:02 +000014#include "MachOPasses.h"
Rui Ueyamadf230b22015-01-15 04:34:31 +000015#include "lld/Core/ArchiveLibraryFile.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000016#include "lld/Core/PassManager.h"
Greg Fitzgerald4b6a7e32015-01-21 22:54:56 +000017#include "lld/Core/Reader.h"
18#include "lld/Core/Writer.h"
Rui Ueyamadf230b22015-01-15 04:34:31 +000019#include "lld/Driver/Driver.h"
Benjamin Kramer06a42af2015-03-02 00:48:06 +000020#include "llvm/ADT/STLExtras.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000021#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/Triple.h"
Nick Kledzikbe43d7e2014-09-30 23:15:39 +000023#include "llvm/Config/config.h"
Rui Ueyama00eb2572014-12-10 00:33:00 +000024#include "llvm/Support/Debug.h"
Chandler Carruth89642a72015-01-14 11:26:52 +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 Ueyama57a29532014-08-06 19:37:35 +000029#include <algorithm>
30
Rui Ueyamafccf7ef2014-10-27 07:44:40 +000031#if defined(HAVE_CXXABI_H)
Nick Kledzikbe43d7e2014-09-30 23:15:39 +000032#include <cxxabi.h>
33#endif
34
Nick Kledzik2458bec2014-07-16 19:49:02 +000035using lld::mach_o::ArchHandler;
Nick Kledzik8fc67fb2014-08-13 23:55:41 +000036using lld::mach_o::MachODylibFile;
Nick Kledzike34182f2013-11-06 21:36:55 +000037using namespace llvm::MachO;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000038
39namespace lld {
40
Nick Kledzike850d9d2013-09-10 23:46:57 +000041bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) {
42 result = 0;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000043
44 if (str.empty())
45 return false;
46
47 SmallVector<StringRef, 3> parts;
48 llvm::SplitString(str, parts, ".");
49
50 unsigned long long num;
51 if (llvm::getAsUnsignedInteger(parts[0], 10, num))
52 return true;
53 if (num > 65535)
54 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000055 result = num << 16;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000056
57 if (parts.size() > 1) {
58 if (llvm::getAsUnsignedInteger(parts[1], 10, num))
59 return true;
60 if (num > 255)
61 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000062 result |= (num << 8);
Rui Ueyama0ca149f2013-08-06 22:31:59 +000063 }
64
65 if (parts.size() > 2) {
66 if (llvm::getAsUnsignedInteger(parts[2], 10, num))
67 return true;
68 if (num > 255)
69 return true;
Nick Kledzike850d9d2013-09-10 23:46:57 +000070 result |= num;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000071 }
72
73 return false;
74}
75
Rui Ueyama0ca149f2013-08-06 22:31:59 +000076
Nick Kledzike34182f2013-11-06 21:36:55 +000077MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = {
78 { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL },
79 { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL },
80 { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL },
81 { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 },
82 { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 },
83 { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S },
Nick Kledzik1bebb282014-09-09 23:52:59 +000084 { "arm64", arch_arm64, true, CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL },
Nick Kledzike34182f2013-11-06 21:36:55 +000085 { "", arch_unknown,false, 0, 0 }
Rui Ueyama0ca149f2013-08-06 22:31:59 +000086};
87
88MachOLinkingContext::Arch
89MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) {
Nick Kledzike34182f2013-11-06 21:36:55 +000090 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
91 if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype))
Rui Ueyama0ca149f2013-08-06 22:31:59 +000092 return info->arch;
Rui Ueyama0ca149f2013-08-06 22:31:59 +000093 }
94 return arch_unknown;
95}
96
97MachOLinkingContext::Arch
98MachOLinkingContext::archFromName(StringRef archName) {
Nick Kledzike34182f2013-11-06 21:36:55 +000099 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
100 if (info->archName.equals(archName))
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000101 return info->arch;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000102 }
103 return arch_unknown;
104}
105
Nick Kledzike5552772013-12-19 21:58:00 +0000106StringRef MachOLinkingContext::nameFromArch(Arch arch) {
107 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
108 if (info->arch == arch)
109 return info->archName;
110 }
111 return "<unknown>";
112}
113
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000114uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) {
115 assert(arch != arch_unknown);
Nick Kledzike34182f2013-11-06 21:36:55 +0000116 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
117 if (info->arch == arch)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000118 return info->cputype;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000119 }
120 llvm_unreachable("Unknown arch type");
121}
122
123uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) {
124 assert(arch != arch_unknown);
Nick Kledzike34182f2013-11-06 21:36:55 +0000125 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
126 if (info->arch == arch)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000127 return info->cpusubtype;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000128 }
129 llvm_unreachable("Unknown arch type");
130}
131
Nick Kledzik635f9c72014-09-04 20:08:30 +0000132bool MachOLinkingContext::isThinObjectFile(StringRef path, Arch &arch) {
133 return mach_o::normalized::isThinObjectFile(path, arch);
134}
135
Rafael Espindolaed48e532015-04-27 22:48:51 +0000136bool MachOLinkingContext::sliceFromFatFile(MemoryBufferRef mb, uint32_t &offset,
Nick Kledzik14b5d202014-10-08 01:48:10 +0000137 uint32_t &size) {
138 return mach_o::normalized::sliceFromFatFile(mb, _arch, offset, size);
139}
140
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000141MachOLinkingContext::MachOLinkingContext()
Tim Northoverd30a1f22014-06-20 15:59:00 +0000142 : _outputMachOType(MH_EXECUTE), _outputMachOTypeStatic(false),
Tim Northoveraf3075b2014-09-10 10:39:57 +0000143 _doNothing(false), _pie(false), _arch(arch_unknown), _os(OS::macOSX),
144 _osMinVersion(0), _pageZeroSize(0), _pageSize(4096), _baseAddress(0),
Lang Hamesff4b13c2015-05-22 00:25:34 +0000145 _stackSize(0), _compatibilityVersion(0), _currentVersion(0),
Lang Hames65a64c92015-05-20 22:10:50 +0000146 _deadStrippableDylib(false), _printAtoms(false), _testingFileUsage(false),
147 _keepPrivateExterns(false), _demangle(false), _archHandler(nullptr),
Nick Kledzik8f75da02014-11-06 03:03:42 +0000148 _exportMode(ExportMode::globals),
Nick Kledzik82d24bc2014-11-07 21:01:21 +0000149 _debugInfoMode(DebugInfoMode::addDebugMap), _orderFileEntries(0) {}
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000150
151MachOLinkingContext::~MachOLinkingContext() {}
152
Nick Kledzik6960b072013-12-21 01:47:17 +0000153void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os,
154 uint32_t minOSVersion) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000155 _outputMachOType = type;
Nick Kledzik6960b072013-12-21 01:47:17 +0000156 _arch = arch;
157 _os = os;
158 _osMinVersion = minOSVersion;
159
Nick Kledzikcb2018f2014-10-09 01:01:16 +0000160 // If min OS not specified on command line, use reasonable defaults.
161 if (minOSVersion == 0) {
162 switch (_arch) {
163 case arch_x86_64:
164 case arch_x86:
165 parsePackedVersion("10.8", _osMinVersion);
166 _os = MachOLinkingContext::OS::macOSX;
167 break;
168 case arch_armv6:
169 case arch_armv7:
170 case arch_armv7s:
171 case arch_arm64:
172 parsePackedVersion("7.0", _osMinVersion);
173 _os = MachOLinkingContext::OS::iOS;
174 break;
175 default:
176 break;
177 }
178 }
179
Tim Northoverd30a1f22014-06-20 15:59:00 +0000180 switch (_outputMachOType) {
Nick Kledzik6960b072013-12-21 01:47:17 +0000181 case llvm::MachO::MH_EXECUTE:
182 // If targeting newer OS, use _main
183 if (minOS("10.8", "6.0")) {
184 _entrySymbolName = "_main";
185 } else {
186 // If targeting older OS, use start (in crt1.o)
187 _entrySymbolName = "start";
188 }
189
190 // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not
191 // support) and 4KB on 32-bit.
192 if (is64Bit(_arch)) {
193 _pageZeroSize = 0x100000000;
194 } else {
195 _pageZeroSize = 0x1000;
196 }
197
Nick Kledzikb7035ae2014-09-09 00:17:52 +0000198 // Make PIE by default when targetting newer OSs.
199 switch (os) {
200 case OS::macOSX:
201 if (minOSVersion >= 0x000A0700) // MacOSX 10.7
202 _pie = true;
203 break;
204 case OS::iOS:
205 if (minOSVersion >= 0x00040300) // iOS 4.3
206 _pie = true;
207 break;
208 case OS::iOS_simulator:
209 _pie = true;
210 break;
211 case OS::unknown:
212 break;
213 }
Nick Kledzik6960b072013-12-21 01:47:17 +0000214 break;
215 case llvm::MachO::MH_DYLIB:
Davide Italiano7b68b902015-03-09 06:05:42 +0000216 setGlobalsAreDeadStripRoots(true);
Nick Kledzik6960b072013-12-21 01:47:17 +0000217 break;
218 case llvm::MachO::MH_BUNDLE:
219 break;
220 case llvm::MachO::MH_OBJECT:
221 _printRemainingUndefines = false;
222 _allowRemainingUndefines = true;
223 default:
224 break;
225 }
Nick Kledzik1bebb282014-09-09 23:52:59 +0000226
227 // Set default segment page sizes based on arch.
228 if (arch == arch_arm64)
229 _pageSize = 4*4096;
Nick Kledzik6960b072013-12-21 01:47:17 +0000230}
231
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000232uint32_t MachOLinkingContext::getCPUType() const {
233 return cpuTypeFromArch(_arch);
234}
235
236uint32_t MachOLinkingContext::getCPUSubType() const {
237 return cpuSubtypeFromArch(_arch);
238}
239
Nick Kledzike34182f2013-11-06 21:36:55 +0000240bool MachOLinkingContext::is64Bit(Arch arch) {
241 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
242 if (info->arch == arch) {
243 return (info->cputype & CPU_ARCH_ABI64);
244 }
245 }
246 // unknown archs are not 64-bit.
247 return false;
248}
249
250bool MachOLinkingContext::isHostEndian(Arch arch) {
251 assert(arch != arch_unknown);
252 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
253 if (info->arch == arch) {
254 return (info->littleEndian == llvm::sys::IsLittleEndianHost);
255 }
256 }
257 llvm_unreachable("Unknown arch type");
258}
259
260bool MachOLinkingContext::isBigEndian(Arch arch) {
261 assert(arch != arch_unknown);
262 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
263 if (info->arch == arch) {
264 return ! info->littleEndian;
265 }
266 }
267 llvm_unreachable("Unknown arch type");
268}
269
270
271
272bool MachOLinkingContext::is64Bit() const {
273 return is64Bit(_arch);
274}
275
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000276bool MachOLinkingContext::outputTypeHasEntry() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000277 switch (_outputMachOType) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000278 case MH_EXECUTE:
279 case MH_DYLINKER:
280 case MH_PRELOAD:
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000281 return true;
282 default:
283 return false;
284 }
285}
286
Nick Kledzik2458bec2014-07-16 19:49:02 +0000287bool MachOLinkingContext::needsStubsPass() const {
288 switch (_outputMachOType) {
289 case MH_EXECUTE:
290 return !_outputMachOTypeStatic;
291 case MH_DYLIB:
292 case MH_BUNDLE:
293 return true;
294 default:
295 return false;
296 }
297}
298
299bool MachOLinkingContext::needsGOTPass() const {
Nick Kledzik1bebb282014-09-09 23:52:59 +0000300 // GOT pass not used in -r mode.
301 if (_outputMachOType == MH_OBJECT)
Nick Kledzik2458bec2014-07-16 19:49:02 +0000302 return false;
Nick Kledzik1bebb282014-09-09 23:52:59 +0000303 // Only some arches use GOT pass.
304 switch (_arch) {
305 case arch_x86_64:
306 case arch_arm64:
307 return true;
308 default:
309 return false;
310 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000311}
312
Tim Northovercf78d372014-09-30 21:29:54 +0000313bool MachOLinkingContext::needsCompactUnwindPass() const {
314 switch (_outputMachOType) {
315 case MH_EXECUTE:
316 case MH_DYLIB:
317 case MH_BUNDLE:
318 return archHandler().needsCompactUnwind();
319 default:
320 return false;
321 }
322}
Nick Kledzik2458bec2014-07-16 19:49:02 +0000323
Nick Kledzik4121bce2014-10-14 01:51:42 +0000324bool MachOLinkingContext::needsShimPass() const {
325 // Shim pass only used in final executables.
326 if (_outputMachOType == MH_OBJECT)
327 return false;
328 // Only 32-bit arm arches use Shim pass.
329 switch (_arch) {
330 case arch_armv6:
331 case arch_armv7:
332 case arch_armv7s:
333 return true;
334 default:
335 return false;
336 }
337}
338
Lang Hames49047032015-06-23 20:35:31 +0000339bool MachOLinkingContext::needsTLVPass() const {
340 switch (_outputMachOType) {
341 case MH_BUNDLE:
342 case MH_EXECUTE:
343 case MH_DYLIB:
344 return true;
345 default:
346 return false;
347 }
348}
349
Nick Kledzik2458bec2014-07-16 19:49:02 +0000350StringRef MachOLinkingContext::binderSymbolName() const {
351 return archHandler().stubInfo().binderSymbolName;
352}
353
354
355
356
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000357bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const {
Nick Kledzik30332b12013-10-08 00:43:34 +0000358 uint32_t parsedVersion;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000359 switch (_os) {
Nick Kledzik30332b12013-10-08 00:43:34 +0000360 case OS::macOSX:
Nick Kledzike850d9d2013-09-10 23:46:57 +0000361 if (parsePackedVersion(mac, parsedVersion))
362 return false;
363 return _osMinVersion >= parsedVersion;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000364 case OS::iOS:
Nick Kledzik30332b12013-10-08 00:43:34 +0000365 case OS::iOS_simulator:
Nick Kledzike850d9d2013-09-10 23:46:57 +0000366 if (parsePackedVersion(iOS, parsedVersion))
367 return false;
368 return _osMinVersion >= parsedVersion;
Nick Kledzik30332b12013-10-08 00:43:34 +0000369 case OS::unknown:
370 break;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000371 }
372 llvm_unreachable("target not configured for iOS or MacOSX");
373}
374
375bool MachOLinkingContext::addEntryPointLoadCommand() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000376 if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000377 return minOS("10.8", "6.0");
378 }
379 return false;
380}
381
382bool MachOLinkingContext::addUnixThreadLoadCommand() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000383 switch (_outputMachOType) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000384 case MH_EXECUTE:
Tim Northoverd30a1f22014-06-20 15:59:00 +0000385 if (_outputMachOTypeStatic)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000386 return true;
387 else
388 return !minOS("10.8", "6.0");
389 break;
Nick Kledzike34182f2013-11-06 21:36:55 +0000390 case MH_DYLINKER:
391 case MH_PRELOAD:
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000392 return true;
393 default:
394 return false;
395 }
396}
397
Tim Northover77d82202014-07-10 11:21:06 +0000398bool MachOLinkingContext::pathExists(StringRef path) const {
Nick Kledzik94174f72014-08-15 19:53:41 +0000399 if (!_testingFileUsage)
Tim Northover77d82202014-07-10 11:21:06 +0000400 return llvm::sys::fs::exists(path.str());
401
402 // Otherwise, we're in test mode: only files explicitly provided on the
403 // command-line exist.
Rui Ueyama57a29532014-08-06 19:37:35 +0000404 std::string key = path.str();
405 std::replace(key.begin(), key.end(), '\\', '/');
406 return _existingPaths.find(key) != _existingPaths.end();
Tim Northover77d82202014-07-10 11:21:06 +0000407}
408
Nick Kledzik09d00bb2014-10-04 00:16:13 +0000409bool MachOLinkingContext::fileExists(StringRef path) const {
410 bool found = pathExists(path);
411 // Log search misses.
412 if (!found)
413 addInputFileNotFound(path);
414
415 // When testing, file is never opened, so logging is done here.
416 if (_testingFileUsage && found)
417 addInputFileDependency(path);
418
419 return found;
420}
421
Nick Kledzik2d835da2014-08-14 22:20:41 +0000422void MachOLinkingContext::setSysLibRoots(const StringRefVector &paths) {
423 _syslibRoots = paths;
424}
425
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000426void MachOLinkingContext::addRpath(StringRef rpath) {
427 _rpaths.push_back(rpath);
428}
429
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000430void MachOLinkingContext::addModifiedSearchDir(StringRef libPath,
431 bool isSystemPath) {
Tim Northover77d82202014-07-10 11:21:06 +0000432 bool addedModifiedPath = false;
433
Nick Kledzik2d835da2014-08-14 22:20:41 +0000434 // -syslibroot only applies to absolute paths.
435 if (libPath.startswith("/")) {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000436 for (auto syslibRoot : _syslibRoots) {
Tim Northover77d82202014-07-10 11:21:06 +0000437 SmallString<256> path(syslibRoot);
438 llvm::sys::path::append(path, libPath);
439 if (pathExists(path)) {
440 _searchDirs.push_back(path.str().copy(_allocator));
441 addedModifiedPath = true;
442 }
443 }
444 }
445
446 if (addedModifiedPath)
447 return;
448
449 // Finally, if only one -syslibroot is given, system paths which aren't in it
450 // get suppressed.
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000451 if (_syslibRoots.size() != 1 || !isSystemPath) {
Tim Northover77d82202014-07-10 11:21:06 +0000452 if (pathExists(libPath)) {
453 _searchDirs.push_back(libPath);
454 }
455 }
456}
457
Nick Kledzik2d835da2014-08-14 22:20:41 +0000458void MachOLinkingContext::addFrameworkSearchDir(StringRef fwPath,
459 bool isSystemPath) {
460 bool pathAdded = false;
461
462 // -syslibroot only used with to absolute framework search paths.
463 if (fwPath.startswith("/")) {
464 for (auto syslibRoot : _syslibRoots) {
465 SmallString<256> path(syslibRoot);
466 llvm::sys::path::append(path, fwPath);
467 if (pathExists(path)) {
468 _frameworkDirs.push_back(path.str().copy(_allocator));
469 pathAdded = true;
470 }
471 }
472 }
473 // If fwPath found in any -syslibroot, then done.
474 if (pathAdded)
475 return;
476
477 // If only one -syslibroot, system paths not in that SDK are suppressed.
478 if (isSystemPath && (_syslibRoots.size() == 1))
479 return;
480
481 // Only use raw fwPath if that directory exists.
482 if (pathExists(fwPath))
483 _frameworkDirs.push_back(fwPath);
484}
485
486
Tim Northover77d82202014-07-10 11:21:06 +0000487ErrorOr<StringRef>
488MachOLinkingContext::searchDirForLibrary(StringRef path,
489 StringRef libName) const {
490 SmallString<256> fullPath;
491 if (libName.endswith(".o")) {
492 // A request ending in .o is special: just search for the file directly.
493 fullPath.assign(path);
494 llvm::sys::path::append(fullPath, libName);
Nick Kledzik09d00bb2014-10-04 00:16:13 +0000495 if (fileExists(fullPath))
Tim Northover77d82202014-07-10 11:21:06 +0000496 return fullPath.str().copy(_allocator);
497 return make_error_code(llvm::errc::no_such_file_or_directory);
498 }
499
500 // Search for dynamic library
501 fullPath.assign(path);
502 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib");
Nick Kledzik09d00bb2014-10-04 00:16:13 +0000503 if (fileExists(fullPath))
Tim Northover77d82202014-07-10 11:21:06 +0000504 return fullPath.str().copy(_allocator);
505
506 // If not, try for a static library
507 fullPath.assign(path);
508 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a");
Nick Kledzik09d00bb2014-10-04 00:16:13 +0000509 if (fileExists(fullPath))
Tim Northover77d82202014-07-10 11:21:06 +0000510 return fullPath.str().copy(_allocator);
511
512 return make_error_code(llvm::errc::no_such_file_or_directory);
513}
514
515
516
517ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const {
518 SmallString<256> path;
519 for (StringRef dir : searchDirs()) {
520 ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName);
521 if (ec)
522 return ec;
523 }
524
525 return make_error_code(llvm::errc::no_such_file_or_directory);
526}
527
Nick Kledzik2d835da2014-08-14 22:20:41 +0000528
529ErrorOr<StringRef> MachOLinkingContext::findPathForFramework(StringRef fwName) const{
530 SmallString<256> fullPath;
531 for (StringRef dir : frameworkDirs()) {
532 fullPath.assign(dir);
533 llvm::sys::path::append(fullPath, Twine(fwName) + ".framework", fwName);
Nick Kledzik09d00bb2014-10-04 00:16:13 +0000534 if (fileExists(fullPath))
Nick Kledzik2d835da2014-08-14 22:20:41 +0000535 return fullPath.str().copy(_allocator);
536 }
537
538 return make_error_code(llvm::errc::no_such_file_or_directory);
539}
540
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000541bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000542 // TODO: if -arch not specified, look at arch of first .o file.
543
Tim Northoverd30a1f22014-06-20 15:59:00 +0000544 if (_currentVersion && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000545 diagnostics << "error: -current_version can only be used with dylibs\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000546 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000547 }
548
Tim Northoverd30a1f22014-06-20 15:59:00 +0000549 if (_compatibilityVersion && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000550 diagnostics
551 << "error: -compatibility_version can only be used with dylibs\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000552 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000553 }
554
Tim Northoverd30a1f22014-06-20 15:59:00 +0000555 if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) {
Nick Kledzike773e322013-09-10 23:55:14 +0000556 diagnostics
557 << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000558 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000559 }
560
Tim Northoverd30a1f22014-06-20 15:59:00 +0000561 if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) {
Nick Kledzike773e322013-09-10 23:55:14 +0000562 diagnostics
563 << "error: -bundle_loader can only be used with Mach-O bundles\n";
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000564 return false;
Nick Kledzike773e322013-09-10 23:55:14 +0000565 }
566
Nick Kledzik8c0bf752014-08-21 01:59:11 +0000567 // If -exported_symbols_list used, all exported symbols must be defined.
568 if (_exportMode == ExportMode::whiteList) {
569 for (const auto &symbol : _exportedSymbols)
570 addInitialUndefinedSymbol(symbol.getKey());
571 }
572
Nick Kledzik77afc712014-08-21 20:25:50 +0000573 // If -dead_strip, set up initial live symbols.
574 if (deadStrip()) {
575 // Entry point is live.
576 if (outputTypeHasEntry())
577 addDeadStripRoot(entrySymbolName());
578 // Lazy binding helper is live.
579 if (needsStubsPass())
580 addDeadStripRoot(binderSymbolName());
581 // If using -exported_symbols_list, make all exported symbols live.
582 if (_exportMode == ExportMode::whiteList) {
Davide Italiano7b68b902015-03-09 06:05:42 +0000583 setGlobalsAreDeadStripRoots(false);
Nick Kledzik77afc712014-08-21 20:25:50 +0000584 for (const auto &symbol : _exportedSymbols)
585 addDeadStripRoot(symbol.getKey());
586 }
587 }
588
Nick Kledzik09d00bb2014-10-04 00:16:13 +0000589 addOutputFileDependency(outputPath());
590
Rui Ueyama8db1edd2013-09-24 23:26:34 +0000591 return true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000592}
593
Shankar Easwaran2bc24922013-10-29 05:12:14 +0000594void MachOLinkingContext::addPasses(PassManager &pm) {
Rui Ueyama00762152015-02-05 20:05:33 +0000595 mach_o::addLayoutPass(pm, *this);
Nick Kledzik2458bec2014-07-16 19:49:02 +0000596 if (needsStubsPass())
597 mach_o::addStubsPass(pm, *this);
Tim Northovercf78d372014-09-30 21:29:54 +0000598 if (needsCompactUnwindPass())
599 mach_o::addCompactUnwindPass(pm, *this);
Nick Kledzik2458bec2014-07-16 19:49:02 +0000600 if (needsGOTPass())
601 mach_o::addGOTPass(pm, *this);
Lang Hames49047032015-06-23 20:35:31 +0000602 if (needsTLVPass())
603 mach_o::addTLVPass(pm, *this);
Nick Kledzik4121bce2014-10-14 01:51:42 +0000604 if (needsShimPass())
605 mach_o::addShimPass(pm, *this); // Shim pass must run after stubs pass.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000606}
607
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000608Writer &MachOLinkingContext::writer() const {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000609 if (!_writer)
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000610 _writer = createWriterMachO(*this);
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000611 return *_writer;
612}
613
Greg Fitzgeraldb4eb64e2015-01-23 23:26:13 +0000614ErrorOr<std::unique_ptr<MemoryBuffer>>
615MachOLinkingContext::getMemoryBuffer(StringRef path) {
616 addInputFileDependency(path);
617
Rui Ueyamadf230b22015-01-15 04:34:31 +0000618 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr =
Greg Fitzgeraldb4eb64e2015-01-23 23:26:13 +0000619 MemoryBuffer::getFileOrSTDIN(path);
620 if (std::error_code ec = mbOrErr.getError())
621 return ec;
622 std::unique_ptr<MemoryBuffer> mb = std::move(mbOrErr.get());
623
624 // If buffer contains a fat file, find required arch in fat buffer
625 // and switch buffer to point to just that required slice.
626 uint32_t offset;
627 uint32_t size;
Rafael Espindolaed48e532015-04-27 22:48:51 +0000628 if (sliceFromFatFile(mb->getMemBufferRef(), offset, size))
Greg Fitzgeraldb4eb64e2015-01-23 23:26:13 +0000629 return MemoryBuffer::getFileSlice(path, size, offset);
630 return std::move(mb);
631}
632
633MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) {
634 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = getMemoryBuffer(path);
Rui Ueyamadf230b22015-01-15 04:34:31 +0000635 if (mbOrErr.getError())
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000636 return nullptr;
637
Rafael Espindolaab5696b2015-04-24 18:51:30 +0000638 ErrorOr<std::unique_ptr<File>> fileOrErr =
639 registry().loadFile(std::move(mbOrErr.get()));
640 if (!fileOrErr)
Rui Ueyamadf230b22015-01-15 04:34:31 +0000641 return nullptr;
Rafael Espindola773a1592015-04-24 19:01:30 +0000642 std::unique_ptr<File> &file = fileOrErr.get();
643 file->parse();
644 MachODylibFile *result = reinterpret_cast<MachODylibFile *>(file.get());
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000645 // Node object now owned by _indirectDylibs vector.
Rafael Espindola773a1592015-04-24 19:01:30 +0000646 _indirectDylibs.push_back(std::move(file));
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000647 return result;
648}
649
650
Nick Kledzik22c90732014-10-01 20:24:30 +0000651MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000652 // See if already loaded.
653 auto pos = _pathToDylibMap.find(path);
654 if (pos != _pathToDylibMap.end())
655 return pos->second;
656
657 // Search -L paths if of the form "libXXX.dylib"
658 std::pair<StringRef, StringRef> split = path.rsplit('/');
659 StringRef leafName = split.second;
660 if (leafName.startswith("lib") && leafName.endswith(".dylib")) {
661 // FIXME: Need to enhance searchLibrary() to only look for .dylib
662 auto libPath = searchLibrary(leafName);
663 if (!libPath.getError()) {
664 return loadIndirectDylib(libPath.get());
665 }
666 }
667
668 // Try full path with sysroot.
669 for (StringRef sysPath : _syslibRoots) {
670 SmallString<256> fullPath;
671 fullPath.assign(sysPath);
672 llvm::sys::path::append(fullPath, path);
673 if (pathExists(fullPath))
674 return loadIndirectDylib(fullPath);
675 }
676
677 // Try full path.
678 if (pathExists(path)) {
679 return loadIndirectDylib(path);
680 }
681
682 return nullptr;
683}
684
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000685uint32_t MachOLinkingContext::dylibCurrentVersion(StringRef installName) const {
686 auto pos = _pathToDylibMap.find(installName);
687 if (pos != _pathToDylibMap.end())
688 return pos->second->currentVersion();
689 else
690 return 0x1000; // 1.0
691}
692
693uint32_t MachOLinkingContext::dylibCompatVersion(StringRef installName) const {
694 auto pos = _pathToDylibMap.find(installName);
695 if (pos != _pathToDylibMap.end())
696 return pos->second->compatVersion();
697 else
698 return 0x1000; // 1.0
699}
700
Simon Atanasyanc4378882015-04-06 20:43:35 +0000701void MachOLinkingContext::createImplicitFiles(
Nick Kledzik22c90732014-10-01 20:24:30 +0000702 std::vector<std::unique_ptr<File> > &result) {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000703 // Add indirect dylibs by asking each linked dylib to add its indirects.
704 // Iterate until no more dylibs get loaded.
705 size_t dylibCount = 0;
706 while (dylibCount != _allDylibs.size()) {
707 dylibCount = _allDylibs.size();
708 for (MachODylibFile *dylib : _allDylibs) {
709 dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* {
710 return findIndirectDylib(path); });
711 }
712 }
713
714 // Let writer add output type specific extras.
Simon Atanasyanc4378882015-04-06 20:43:35 +0000715 writer().createImplicitFiles(result);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000716}
717
718
Nick Kledzik51720672014-10-16 19:31:28 +0000719void MachOLinkingContext::registerDylib(MachODylibFile *dylib,
720 bool upward) const {
Lang Hames9bbc3652015-05-13 00:17:08 +0000721 std::lock_guard<std::mutex> lock(_dylibsMutex);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000722 _allDylibs.insert(dylib);
723 _pathToDylibMap[dylib->installName()] = dylib;
724 // If path is different than install name, register path too.
725 if (!dylib->path().equals(dylib->installName()))
726 _pathToDylibMap[dylib->path()] = dylib;
Nick Kledzik51720672014-10-16 19:31:28 +0000727 if (upward)
728 _upwardDylibs.insert(dylib);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000729}
730
731
Nick Kledzik51720672014-10-16 19:31:28 +0000732bool MachOLinkingContext::isUpwardDylib(StringRef installName) const {
733 for (MachODylibFile *dylib : _upwardDylibs) {
734 if (dylib->installName().equals(installName))
735 return true;
736 }
737 return false;
738}
739
Nick Kledzik2458bec2014-07-16 19:49:02 +0000740ArchHandler &MachOLinkingContext::archHandler() const {
741 if (!_archHandler)
742 _archHandler = ArchHandler::create(_arch);
743 return *_archHandler;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000744}
745
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000746
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000747void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect,
Rui Ueyamada74d572015-03-26 02:23:45 +0000748 uint16_t align) {
749 SectionAlign entry = { seg, sect, align };
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000750 _sectAligns.push_back(entry);
751}
752
753bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect,
Rui Ueyamada74d572015-03-26 02:23:45 +0000754 uint16_t &align) const {
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000755 for (const SectionAlign &entry : _sectAligns) {
756 if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) {
Rui Ueyamada74d572015-03-26 02:23:45 +0000757 align = entry.align;
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000758 return true;
759 }
760 }
761 return false;
762}
763
Nick Kledzik8c0bf752014-08-21 01:59:11 +0000764
765void MachOLinkingContext::addExportSymbol(StringRef sym) {
Nick Kledzik4183dbc2014-10-24 22:28:54 +0000766 // Support old crufty export lists with bogus entries.
767 if (sym.endswith(".eh") || sym.startswith(".objc_category_name_")) {
768 llvm::errs() << "warning: ignoring " << sym << " in export list\n";
769 return;
770 }
771 // Only i386 MacOSX uses old ABI, so don't change those.
772 if ((_os != OS::macOSX) || (_arch != arch_x86)) {
773 // ObjC has two differnent ABIs. Be nice and allow one export list work for
774 // both ABIs by renaming symbols.
775 if (sym.startswith(".objc_class_name_")) {
776 std::string abi2className("_OBJC_CLASS_$_");
777 abi2className += sym.substr(17);
778 _exportedSymbols.insert(copy(abi2className));
779 std::string abi2metaclassName("_OBJC_METACLASS_$_");
780 abi2metaclassName += sym.substr(17);
781 _exportedSymbols.insert(copy(abi2metaclassName));
782 return;
783 }
784 }
785
Nick Kledzik8c0bf752014-08-21 01:59:11 +0000786 // FIXME: Support wildcards.
787 _exportedSymbols.insert(sym);
788}
789
790bool MachOLinkingContext::exportSymbolNamed(StringRef sym) const {
791 switch (_exportMode) {
792 case ExportMode::globals:
793 llvm_unreachable("exportSymbolNamed() should not be called in this mode");
794 break;
795 case ExportMode::whiteList:
796 return _exportedSymbols.count(sym);
797 case ExportMode::blackList:
798 return !_exportedSymbols.count(sym);
799 }
Yaron Keren9682c852014-09-21 05:07:44 +0000800 llvm_unreachable("_exportMode unknown enum value");
Nick Kledzik8c0bf752014-08-21 01:59:11 +0000801}
802
Nick Kledzikbe43d7e2014-09-30 23:15:39 +0000803std::string MachOLinkingContext::demangle(StringRef symbolName) const {
804 // Only try to demangle symbols if -demangle on command line
Davide Italiano6d86bb22015-02-18 03:54:21 +0000805 if (!demangleSymbols())
Nick Kledzikbe43d7e2014-09-30 23:15:39 +0000806 return symbolName;
807
808 // Only try to demangle symbols that look like C++ symbols
809 if (!symbolName.startswith("__Z"))
810 return symbolName;
811
Rui Ueyamafccf7ef2014-10-27 07:44:40 +0000812#if defined(HAVE_CXXABI_H)
Nick Kledzikbe43d7e2014-09-30 23:15:39 +0000813 SmallString<256> symBuff;
814 StringRef nullTermSym = Twine(symbolName).toNullTerminatedStringRef(symBuff);
815 // Mach-O has extra leading underscore that needs to be removed.
816 const char *cstr = nullTermSym.data() + 1;
817 int status;
818 char *demangled = abi::__cxa_demangle(cstr, nullptr, nullptr, &status);
819 if (demangled != NULL) {
820 std::string result(demangled);
821 // __cxa_demangle() always uses a malloc'ed buffer to return the result.
822 free(demangled);
823 return result;
824 }
825#endif
826
827 return symbolName;
828}
829
Nick Kledzik09d00bb2014-10-04 00:16:13 +0000830std::error_code MachOLinkingContext::createDependencyFile(StringRef path) {
831 std::error_code ec;
832 _dependencyInfo = std::unique_ptr<llvm::raw_fd_ostream>(new
833 llvm::raw_fd_ostream(path, ec, llvm::sys::fs::F_None));
834 if (ec) {
835 _dependencyInfo.reset();
836 return ec;
837 }
838
839 char linkerVersionOpcode = 0x00;
840 *_dependencyInfo << linkerVersionOpcode;
841 *_dependencyInfo << "lld"; // FIXME
842 *_dependencyInfo << '\0';
843
844 return std::error_code();
845}
846
847void MachOLinkingContext::addInputFileDependency(StringRef path) const {
848 if (!_dependencyInfo)
849 return;
850
851 char inputFileOpcode = 0x10;
852 *_dependencyInfo << inputFileOpcode;
853 *_dependencyInfo << path;
854 *_dependencyInfo << '\0';
855}
856
857void MachOLinkingContext::addInputFileNotFound(StringRef path) const {
858 if (!_dependencyInfo)
859 return;
860
861 char inputFileOpcode = 0x11;
862 *_dependencyInfo << inputFileOpcode;
863 *_dependencyInfo << path;
864 *_dependencyInfo << '\0';
865}
866
867void MachOLinkingContext::addOutputFileDependency(StringRef path) const {
868 if (!_dependencyInfo)
869 return;
870
871 char outputFileOpcode = 0x40;
872 *_dependencyInfo << outputFileOpcode;
873 *_dependencyInfo << path;
874 *_dependencyInfo << '\0';
875}
876
Nick Kledzik82d24bc2014-11-07 21:01:21 +0000877void MachOLinkingContext::appendOrderedSymbol(StringRef symbol,
878 StringRef filename) {
879 // To support sorting static functions which may have the same name in
880 // multiple .o files, _orderFiles maps the symbol name to a vector
881 // of OrderFileNode each of which can specify a file prefix.
882 OrderFileNode info;
883 if (!filename.empty())
884 info.fileFilter = copy(filename);
885 info.order = _orderFileEntries++;
886 _orderFiles[symbol].push_back(info);
887}
888
889bool
890MachOLinkingContext::findOrderOrdinal(const std::vector<OrderFileNode> &nodes,
891 const DefinedAtom *atom,
892 unsigned &ordinal) {
893 const File *objFile = &atom->file();
894 assert(objFile);
895 StringRef objName = objFile->path();
896 std::pair<StringRef, StringRef> dirAndLeaf = objName.rsplit('/');
897 if (!dirAndLeaf.second.empty())
898 objName = dirAndLeaf.second;
899 for (const OrderFileNode &info : nodes) {
900 if (info.fileFilter.empty()) {
901 // Have unprefixed symbol name in order file that matches this atom.
902 ordinal = info.order;
Nick Kledzik82d24bc2014-11-07 21:01:21 +0000903 return true;
904 }
905 if (info.fileFilter.equals(objName)) {
906 // Have prefixed symbol name in order file that matches atom's path.
907 ordinal = info.order;
Nick Kledzik82d24bc2014-11-07 21:01:21 +0000908 return true;
909 }
910 }
911 return false;
912}
913
914bool MachOLinkingContext::customAtomOrderer(const DefinedAtom *left,
915 const DefinedAtom *right,
Rui Ueyama00762152015-02-05 20:05:33 +0000916 bool &leftBeforeRight) const {
Nick Kledzik82d24bc2014-11-07 21:01:21 +0000917 // No custom sorting if no order file entries.
918 if (!_orderFileEntries)
919 return false;
920
921 // Order files can only order named atoms.
922 StringRef leftName = left->name();
923 StringRef rightName = right->name();
924 if (leftName.empty() || rightName.empty())
925 return false;
926
927 // If neither is in order file list, no custom sorter.
928 auto leftPos = _orderFiles.find(leftName);
929 auto rightPos = _orderFiles.find(rightName);
930 bool leftIsOrdered = (leftPos != _orderFiles.end());
931 bool rightIsOrdered = (rightPos != _orderFiles.end());
932 if (!leftIsOrdered && !rightIsOrdered)
933 return false;
934
935 // There could be multiple symbols with same name but different file prefixes.
936 unsigned leftOrder;
937 unsigned rightOrder;
938 bool foundLeft =
939 leftIsOrdered && findOrderOrdinal(leftPos->getValue(), left, leftOrder);
940 bool foundRight = rightIsOrdered &&
941 findOrderOrdinal(rightPos->getValue(), right, rightOrder);
942 if (!foundLeft && !foundRight)
943 return false;
944
945 // If only one is in order file list, ordered one goes first.
946 if (foundLeft != foundRight)
947 leftBeforeRight = foundLeft;
948 else
949 leftBeforeRight = (leftOrder < rightOrder);
950
951 return true;
952}
Nick Kledzik8c0bf752014-08-21 01:59:11 +0000953
Rui Ueyama61635442015-01-15 08:31:46 +0000954static bool isLibrary(const std::unique_ptr<Node> &elem) {
Rui Ueyamaae1daae2015-01-15 08:51:23 +0000955 if (FileNode *node = dyn_cast<FileNode>(const_cast<Node *>(elem.get()))) {
956 File *file = node->getFile();
957 return isa<SharedLibraryFile>(file) || isa<ArchiveLibraryFile>(file);
958 }
959 return false;
Rui Ueyama00eb2572014-12-10 00:33:00 +0000960}
961
962// The darwin linker processes input files in two phases. The first phase
963// links in all object (.o) files in command line order. The second phase
964// links in libraries in command line order.
965// In this function we reorder the input files so that all the object files
966// comes before any library file. We also make a group for the library files
967// so that the Resolver will reiterate over the libraries as long as we find
968// new undefines from libraries.
Denis Protivenskycd617152015-03-14 10:34:43 +0000969void MachOLinkingContext::finalizeInputFiles() {
Rui Ueyama883afba2015-01-15 08:46:36 +0000970 std::vector<std::unique_ptr<Node>> &elements = getNodes();
Rui Ueyama00eb2572014-12-10 00:33:00 +0000971 std::stable_sort(elements.begin(), elements.end(),
Rui Ueyama61635442015-01-15 08:31:46 +0000972 [](const std::unique_ptr<Node> &a,
973 const std::unique_ptr<Node> &b) {
Rui Ueyama00eb2572014-12-10 00:33:00 +0000974 return !isLibrary(a) && isLibrary(b);
975 });
976 size_t numLibs = std::count_if(elements.begin(), elements.end(), isLibrary);
977 elements.push_back(llvm::make_unique<GroupEnd>(numLibs));
978}
979
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000980} // end namespace lld