1 //===- lib/ReaderWriter/MachO/MachOLinkingContext.cpp ---------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lld/Common/ErrorHandler.h"
11 #include "lld/ReaderWriter/MachOLinkingContext.h"
12 #include "ArchHandler.h"
14 #include "FlatNamespaceFile.h"
15 #include "MachONormalizedFile.h"
16 #include "MachOPasses.h"
17 #include "SectCreateFile.h"
18 #include "lld/Common/Driver.h"
19 #include "lld/Core/ArchiveLibraryFile.h"
20 #include "lld/Core/PassManager.h"
21 #include "lld/Core/Reader.h"
22 #include "lld/Core/Writer.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/Triple.h"
26 #include "llvm/BinaryFormat/MachO.h"
27 #include "llvm/Demangle/Demangle.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Errc.h"
30 #include "llvm/Support/Host.h"
31 #include "llvm/Support/Path.h"
34 using lld::mach_o::ArchHandler;
35 using lld::mach_o::MachOFile;
36 using lld::mach_o::MachODylibFile;
37 using namespace llvm::MachO;
41 bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) {
47 SmallVector<StringRef, 3> parts;
48 llvm::SplitString(str, parts, ".");
50 unsigned long long num;
51 if (llvm::getAsUnsignedInteger(parts[0], 10, num))
57 if (parts.size() > 1) {
58 if (llvm::getAsUnsignedInteger(parts[1], 10, num))
65 if (parts.size() > 2) {
66 if (llvm::getAsUnsignedInteger(parts[2], 10, num))
76 bool MachOLinkingContext::parsePackedVersion(StringRef str, uint64_t &result) {
82 SmallVector<StringRef, 5> parts;
83 llvm::SplitString(str, parts, ".");
85 unsigned long long num;
86 if (llvm::getAsUnsignedInteger(parts[0], 10, num))
93 for (StringRef str : llvm::makeArrayRef(parts).slice(1)) {
94 if (llvm::getAsUnsignedInteger(str, 10, num))
98 result |= (num << Shift);
105 MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = {
106 { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL },
107 { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL },
108 { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL },
109 { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 },
110 { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 },
111 { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S },
112 { "arm64", arch_arm64, true, CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL },
113 { "", arch_unknown,false, 0, 0 }
116 MachOLinkingContext::Arch
117 MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) {
118 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
119 if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype))
125 MachOLinkingContext::Arch
126 MachOLinkingContext::archFromName(StringRef archName) {
127 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
128 if (info->archName.equals(archName))
134 StringRef MachOLinkingContext::nameFromArch(Arch arch) {
135 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
136 if (info->arch == arch)
137 return info->archName;
142 uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) {
143 assert(arch != arch_unknown);
144 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
145 if (info->arch == arch)
146 return info->cputype;
148 llvm_unreachable("Unknown arch type");
151 uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) {
152 assert(arch != arch_unknown);
153 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
154 if (info->arch == arch)
155 return info->cpusubtype;
157 llvm_unreachable("Unknown arch type");
160 bool MachOLinkingContext::isThinObjectFile(StringRef path, Arch &arch) {
161 return mach_o::normalized::isThinObjectFile(path, arch);
164 bool MachOLinkingContext::sliceFromFatFile(MemoryBufferRef mb, uint32_t &offset,
166 return mach_o::normalized::sliceFromFatFile(mb, _arch, offset, size);
169 MachOLinkingContext::MachOLinkingContext() {}
171 MachOLinkingContext::~MachOLinkingContext() {
172 // Atoms are allocated on BumpPtrAllocator's on File's.
173 // As we transfer atoms from one file to another, we need to clear all of the
174 // atoms before we remove any of the BumpPtrAllocator's.
175 auto &nodes = getNodes();
176 for (unsigned i = 0, e = nodes.size(); i != e; ++i) {
177 FileNode *node = dyn_cast<FileNode>(nodes[i].get());
180 File *file = node->getFile();
185 void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os,
186 uint32_t minOSVersion,
187 bool exportDynamicSymbols) {
188 _outputMachOType = type;
191 _osMinVersion = minOSVersion;
193 // If min OS not specified on command line, use reasonable defaults.
194 // Note that we only do sensible defaults when emitting something other than
195 // object and preload.
196 if (_outputMachOType != llvm::MachO::MH_OBJECT &&
197 _outputMachOType != llvm::MachO::MH_PRELOAD) {
198 if (minOSVersion == 0) {
202 parsePackedVersion("10.8", _osMinVersion);
203 _os = MachOLinkingContext::OS::macOSX;
209 parsePackedVersion("7.0", _osMinVersion);
210 _os = MachOLinkingContext::OS::iOS;
218 switch (_outputMachOType) {
219 case llvm::MachO::MH_EXECUTE:
220 // If targeting newer OS, use _main
221 if (minOS("10.8", "6.0")) {
222 _entrySymbolName = "_main";
224 // If targeting older OS, use start (in crt1.o)
225 _entrySymbolName = "start";
228 // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not
229 // support) and 4KB on 32-bit.
230 if (is64Bit(_arch)) {
231 _pageZeroSize = 0x100000000;
233 _pageZeroSize = 0x1000;
236 // Initial base address is __PAGEZERO size.
237 _baseAddress = _pageZeroSize;
239 // Make PIE by default when targetting newer OSs.
242 if (minOSVersion >= 0x000A0700) // MacOSX 10.7
246 if (minOSVersion >= 0x00040300) // iOS 4.3
249 case OS::iOS_simulator:
255 setGlobalsAreDeadStripRoots(exportDynamicSymbols);
257 case llvm::MachO::MH_DYLIB:
258 setGlobalsAreDeadStripRoots(exportDynamicSymbols);
260 case llvm::MachO::MH_BUNDLE:
262 case llvm::MachO::MH_OBJECT:
263 _printRemainingUndefines = false;
264 _allowRemainingUndefines = true;
270 // Set default segment page sizes based on arch.
271 if (arch == arch_arm64)
275 uint32_t MachOLinkingContext::getCPUType() const {
276 return cpuTypeFromArch(_arch);
279 uint32_t MachOLinkingContext::getCPUSubType() const {
280 return cpuSubtypeFromArch(_arch);
283 bool MachOLinkingContext::is64Bit(Arch arch) {
284 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
285 if (info->arch == arch) {
286 return (info->cputype & CPU_ARCH_ABI64);
289 // unknown archs are not 64-bit.
293 bool MachOLinkingContext::isHostEndian(Arch arch) {
294 assert(arch != arch_unknown);
295 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
296 if (info->arch == arch) {
297 return (info->littleEndian == llvm::sys::IsLittleEndianHost);
300 llvm_unreachable("Unknown arch type");
303 bool MachOLinkingContext::isBigEndian(Arch arch) {
304 assert(arch != arch_unknown);
305 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
306 if (info->arch == arch) {
307 return ! info->littleEndian;
310 llvm_unreachable("Unknown arch type");
313 bool MachOLinkingContext::is64Bit() const {
314 return is64Bit(_arch);
317 bool MachOLinkingContext::outputTypeHasEntry() const {
318 switch (_outputMachOType) {
328 bool MachOLinkingContext::needsStubsPass() const {
329 switch (_outputMachOType) {
331 return !_outputMachOTypeStatic;
340 bool MachOLinkingContext::needsGOTPass() const {
341 // GOT pass not used in -r mode.
342 if (_outputMachOType == MH_OBJECT)
344 // Only some arches use GOT pass.
354 bool MachOLinkingContext::needsCompactUnwindPass() const {
355 switch (_outputMachOType) {
359 return archHandler().needsCompactUnwind();
365 bool MachOLinkingContext::needsObjCPass() const {
366 // ObjC pass is only needed if any of the inputs were ObjC.
367 return _objcConstraint != objc_unknown;
370 bool MachOLinkingContext::needsShimPass() const {
371 // Shim pass only used in final executables.
372 if (_outputMachOType == MH_OBJECT)
374 // Only 32-bit arm arches use Shim pass.
385 bool MachOLinkingContext::needsTLVPass() const {
386 switch (_outputMachOType) {
396 StringRef MachOLinkingContext::binderSymbolName() const {
397 return archHandler().stubInfo().binderSymbolName;
400 bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const {
401 uint32_t parsedVersion;
404 if (parsePackedVersion(mac, parsedVersion))
406 return _osMinVersion >= parsedVersion;
408 case OS::iOS_simulator:
409 if (parsePackedVersion(iOS, parsedVersion))
411 return _osMinVersion >= parsedVersion;
413 // If we don't know the target, then assume that we don't meet the min OS.
414 // This matches the ld64 behaviour
417 llvm_unreachable("invalid OS enum");
420 bool MachOLinkingContext::addEntryPointLoadCommand() const {
421 if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) {
422 return minOS("10.8", "6.0");
427 bool MachOLinkingContext::addUnixThreadLoadCommand() const {
428 switch (_outputMachOType) {
430 if (_outputMachOTypeStatic)
433 return !minOS("10.8", "6.0");
443 bool MachOLinkingContext::pathExists(StringRef path) const {
444 if (!_testingFileUsage)
445 return llvm::sys::fs::exists(path.str());
447 // Otherwise, we're in test mode: only files explicitly provided on the
448 // command-line exist.
449 std::string key = path.str();
450 std::replace(key.begin(), key.end(), '\\', '/');
451 return _existingPaths.find(key) != _existingPaths.end();
454 bool MachOLinkingContext::fileExists(StringRef path) const {
455 bool found = pathExists(path);
456 // Log search misses.
458 addInputFileNotFound(path);
460 // When testing, file is never opened, so logging is done here.
461 if (_testingFileUsage && found)
462 addInputFileDependency(path);
467 void MachOLinkingContext::setSysLibRoots(const StringRefVector &paths) {
468 _syslibRoots = paths;
471 void MachOLinkingContext::addRpath(StringRef rpath) {
472 _rpaths.push_back(rpath);
475 void MachOLinkingContext::addModifiedSearchDir(StringRef libPath,
477 bool addedModifiedPath = false;
479 // -syslibroot only applies to absolute paths.
480 if (libPath.startswith("/")) {
481 for (auto syslibRoot : _syslibRoots) {
482 SmallString<256> path(syslibRoot);
483 llvm::sys::path::append(path, libPath);
484 if (pathExists(path)) {
485 _searchDirs.push_back(path.str().copy(_allocator));
486 addedModifiedPath = true;
491 if (addedModifiedPath)
494 // Finally, if only one -syslibroot is given, system paths which aren't in it
496 if (_syslibRoots.size() != 1 || !isSystemPath) {
497 if (pathExists(libPath)) {
498 _searchDirs.push_back(libPath);
503 void MachOLinkingContext::addFrameworkSearchDir(StringRef fwPath,
505 bool pathAdded = false;
507 // -syslibroot only used with to absolute framework search paths.
508 if (fwPath.startswith("/")) {
509 for (auto syslibRoot : _syslibRoots) {
510 SmallString<256> path(syslibRoot);
511 llvm::sys::path::append(path, fwPath);
512 if (pathExists(path)) {
513 _frameworkDirs.push_back(path.str().copy(_allocator));
518 // If fwPath found in any -syslibroot, then done.
522 // If only one -syslibroot, system paths not in that SDK are suppressed.
523 if (isSystemPath && (_syslibRoots.size() == 1))
526 // Only use raw fwPath if that directory exists.
527 if (pathExists(fwPath))
528 _frameworkDirs.push_back(fwPath);
531 llvm::Optional<StringRef>
532 MachOLinkingContext::searchDirForLibrary(StringRef path,
533 StringRef libName) const {
534 SmallString<256> fullPath;
535 if (libName.endswith(".o")) {
536 // A request ending in .o is special: just search for the file directly.
537 fullPath.assign(path);
538 llvm::sys::path::append(fullPath, libName);
539 if (fileExists(fullPath))
540 return fullPath.str().copy(_allocator);
544 // Search for dynamic library
545 fullPath.assign(path);
546 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib");
547 if (fileExists(fullPath))
548 return fullPath.str().copy(_allocator);
550 // If not, try for a static library
551 fullPath.assign(path);
552 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a");
553 if (fileExists(fullPath))
554 return fullPath.str().copy(_allocator);
559 llvm::Optional<StringRef>
560 MachOLinkingContext::searchLibrary(StringRef libName) const {
561 SmallString<256> path;
562 for (StringRef dir : searchDirs()) {
563 llvm::Optional<StringRef> searchDir = searchDirForLibrary(dir, libName);
571 llvm::Optional<StringRef>
572 MachOLinkingContext::findPathForFramework(StringRef fwName) const{
573 SmallString<256> fullPath;
574 for (StringRef dir : frameworkDirs()) {
575 fullPath.assign(dir);
576 llvm::sys::path::append(fullPath, Twine(fwName) + ".framework", fwName);
577 if (fileExists(fullPath))
578 return fullPath.str().copy(_allocator);
584 bool MachOLinkingContext::validateImpl() {
585 // TODO: if -arch not specified, look at arch of first .o file.
587 if (_currentVersion && _outputMachOType != MH_DYLIB) {
588 error("-current_version can only be used with dylibs");
592 if (_compatibilityVersion && _outputMachOType != MH_DYLIB) {
593 error("-compatibility_version can only be used with dylibs");
597 if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) {
598 error("-mark_dead_strippable_dylib can only be used with dylibs");
602 if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) {
603 error("-bundle_loader can only be used with Mach-O bundles");
607 // If -exported_symbols_list used, all exported symbols must be defined.
608 if (_exportMode == ExportMode::whiteList) {
609 for (const auto &symbol : _exportedSymbols)
610 addInitialUndefinedSymbol(symbol.getKey());
613 // If -dead_strip, set up initial live symbols.
615 // Entry point is live.
616 if (outputTypeHasEntry())
617 addDeadStripRoot(entrySymbolName());
618 // Lazy binding helper is live.
619 if (needsStubsPass())
620 addDeadStripRoot(binderSymbolName());
621 // If using -exported_symbols_list, make all exported symbols live.
622 if (_exportMode == ExportMode::whiteList) {
623 setGlobalsAreDeadStripRoots(false);
624 for (const auto &symbol : _exportedSymbols)
625 addDeadStripRoot(symbol.getKey());
629 addOutputFileDependency(outputPath());
634 void MachOLinkingContext::addPasses(PassManager &pm) {
635 // objc pass should be before layout pass. Otherwise test cases may contain
636 // no atoms which confuses the layout pass.
638 mach_o::addObjCPass(pm, *this);
639 mach_o::addLayoutPass(pm, *this);
640 if (needsStubsPass())
641 mach_o::addStubsPass(pm, *this);
642 if (needsCompactUnwindPass())
643 mach_o::addCompactUnwindPass(pm, *this);
645 mach_o::addGOTPass(pm, *this);
647 mach_o::addTLVPass(pm, *this);
649 mach_o::addShimPass(pm, *this); // Shim pass must run after stubs pass.
652 Writer &MachOLinkingContext::writer() const {
654 _writer = createWriterMachO(*this);
658 ErrorOr<std::unique_ptr<MemoryBuffer>>
659 MachOLinkingContext::getMemoryBuffer(StringRef path) {
660 addInputFileDependency(path);
662 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr =
663 MemoryBuffer::getFileOrSTDIN(path);
664 if (std::error_code ec = mbOrErr.getError())
666 std::unique_ptr<MemoryBuffer> mb = std::move(mbOrErr.get());
668 // If buffer contains a fat file, find required arch in fat buffer
669 // and switch buffer to point to just that required slice.
672 if (sliceFromFatFile(mb->getMemBufferRef(), offset, size))
673 return MemoryBuffer::getFileSlice(path, size, offset);
674 return std::move(mb);
677 MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) {
678 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = getMemoryBuffer(path);
679 if (mbOrErr.getError())
682 ErrorOr<std::unique_ptr<File>> fileOrErr =
683 registry().loadFile(std::move(mbOrErr.get()));
686 std::unique_ptr<File> &file = fileOrErr.get();
688 MachODylibFile *result = reinterpret_cast<MachODylibFile *>(file.get());
689 // Node object now owned by _indirectDylibs vector.
690 _indirectDylibs.push_back(std::move(file));
694 MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) {
695 // See if already loaded.
696 auto pos = _pathToDylibMap.find(path);
697 if (pos != _pathToDylibMap.end())
700 // Search -L paths if of the form "libXXX.dylib"
701 std::pair<StringRef, StringRef> split = path.rsplit('/');
702 StringRef leafName = split.second;
703 if (leafName.startswith("lib") && leafName.endswith(".dylib")) {
704 // FIXME: Need to enhance searchLibrary() to only look for .dylib
705 auto libPath = searchLibrary(leafName);
707 return loadIndirectDylib(libPath.getValue());
710 // Try full path with sysroot.
711 for (StringRef sysPath : _syslibRoots) {
712 SmallString<256> fullPath;
713 fullPath.assign(sysPath);
714 llvm::sys::path::append(fullPath, path);
715 if (pathExists(fullPath))
716 return loadIndirectDylib(fullPath);
720 if (pathExists(path)) {
721 return loadIndirectDylib(path);
727 uint32_t MachOLinkingContext::dylibCurrentVersion(StringRef installName) const {
728 auto pos = _pathToDylibMap.find(installName);
729 if (pos != _pathToDylibMap.end())
730 return pos->second->currentVersion();
732 return 0x10000; // 1.0
735 uint32_t MachOLinkingContext::dylibCompatVersion(StringRef installName) const {
736 auto pos = _pathToDylibMap.find(installName);
737 if (pos != _pathToDylibMap.end())
738 return pos->second->compatVersion();
740 return 0x10000; // 1.0
743 void MachOLinkingContext::createImplicitFiles(
744 std::vector<std::unique_ptr<File> > &result) {
745 // Add indirect dylibs by asking each linked dylib to add its indirects.
746 // Iterate until no more dylibs get loaded.
747 size_t dylibCount = 0;
748 while (dylibCount != _allDylibs.size()) {
749 dylibCount = _allDylibs.size();
750 for (MachODylibFile *dylib : _allDylibs) {
751 dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* {
752 return findIndirectDylib(path); });
756 // Let writer add output type specific extras.
757 writer().createImplicitFiles(result);
759 // If undefinedMode is != error, add a FlatNamespaceFile instance. This will
760 // provide a SharedLibraryAtom for symbols that aren't defined elsewhere.
761 if (undefinedMode() != UndefinedMode::error) {
762 result.emplace_back(new mach_o::FlatNamespaceFile(*this));
763 _flatNamespaceFile = result.back().get();
767 void MachOLinkingContext::registerDylib(MachODylibFile *dylib,
769 std::lock_guard<std::mutex> lock(_dylibsMutex);
771 if (std::find(_allDylibs.begin(),
772 _allDylibs.end(), dylib) == _allDylibs.end())
773 _allDylibs.push_back(dylib);
774 _pathToDylibMap[dylib->installName()] = dylib;
775 // If path is different than install name, register path too.
776 if (!dylib->path().equals(dylib->installName()))
777 _pathToDylibMap[dylib->path()] = dylib;
779 _upwardDylibs.insert(dylib);
782 bool MachOLinkingContext::isUpwardDylib(StringRef installName) const {
783 for (MachODylibFile *dylib : _upwardDylibs) {
784 if (dylib->installName().equals(installName))
790 ArchHandler &MachOLinkingContext::archHandler() const {
792 _archHandler = ArchHandler::create(_arch);
793 return *_archHandler;
796 void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect,
798 SectionAlign entry = { seg, sect, align };
799 _sectAligns.push_back(entry);
802 void MachOLinkingContext::addSectCreateSection(
803 StringRef seg, StringRef sect,
804 std::unique_ptr<MemoryBuffer> content) {
806 if (!_sectCreateFile) {
807 auto sectCreateFile = llvm::make_unique<mach_o::SectCreateFile>();
808 _sectCreateFile = sectCreateFile.get();
809 getNodes().push_back(llvm::make_unique<FileNode>(std::move(sectCreateFile)));
812 assert(_sectCreateFile && "sectcreate file does not exist.");
813 _sectCreateFile->addSection(seg, sect, std::move(content));
816 bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect,
817 uint16_t &align) const {
818 for (const SectionAlign &entry : _sectAligns) {
819 if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) {
827 void MachOLinkingContext::addExportSymbol(StringRef sym) {
828 // Support old crufty export lists with bogus entries.
829 if (sym.endswith(".eh") || sym.startswith(".objc_category_name_")) {
830 llvm::errs() << "warning: ignoring " << sym << " in export list\n";
833 // Only i386 MacOSX uses old ABI, so don't change those.
834 if ((_os != OS::macOSX) || (_arch != arch_x86)) {
835 // ObjC has two differnent ABIs. Be nice and allow one export list work for
836 // both ABIs by renaming symbols.
837 if (sym.startswith(".objc_class_name_")) {
838 std::string abi2className("_OBJC_CLASS_$_");
839 abi2className += sym.substr(17);
840 _exportedSymbols.insert(copy(abi2className));
841 std::string abi2metaclassName("_OBJC_METACLASS_$_");
842 abi2metaclassName += sym.substr(17);
843 _exportedSymbols.insert(copy(abi2metaclassName));
848 // FIXME: Support wildcards.
849 _exportedSymbols.insert(sym);
852 bool MachOLinkingContext::exportSymbolNamed(StringRef sym) const {
853 switch (_exportMode) {
854 case ExportMode::globals:
855 llvm_unreachable("exportSymbolNamed() should not be called in this mode");
857 case ExportMode::whiteList:
858 return _exportedSymbols.count(sym);
859 case ExportMode::blackList:
860 return !_exportedSymbols.count(sym);
862 llvm_unreachable("_exportMode unknown enum value");
865 std::string MachOLinkingContext::demangle(StringRef symbolName) const {
866 // Only try to demangle symbols if -demangle on command line
867 if (!demangleSymbols())
870 // Only try to demangle symbols that look like C++ symbols
871 if (!symbolName.startswith("__Z"))
874 SmallString<256> symBuff;
875 StringRef nullTermSym = Twine(symbolName).toNullTerminatedStringRef(symBuff);
876 // Mach-O has extra leading underscore that needs to be removed.
877 const char *cstr = nullTermSym.data() + 1;
879 char *demangled = llvm::itaniumDemangle(cstr, nullptr, nullptr, &status);
881 std::string result(demangled);
882 // __cxa_demangle() always uses a malloc'ed buffer to return the result.
890 static void addDependencyInfoHelper(llvm::raw_fd_ostream *DepInfo,
891 char Opcode, StringRef Path) {
900 std::error_code MachOLinkingContext::createDependencyFile(StringRef path) {
902 _dependencyInfo = std::unique_ptr<llvm::raw_fd_ostream>(new
903 llvm::raw_fd_ostream(path, ec, llvm::sys::fs::F_None));
905 _dependencyInfo.reset();
909 addDependencyInfoHelper(_dependencyInfo.get(), 0x00, "lld" /*FIXME*/);
910 return std::error_code();
913 void MachOLinkingContext::addInputFileDependency(StringRef path) const {
914 addDependencyInfoHelper(_dependencyInfo.get(), 0x10, path);
917 void MachOLinkingContext::addInputFileNotFound(StringRef path) const {
918 addDependencyInfoHelper(_dependencyInfo.get(), 0x11, path);
921 void MachOLinkingContext::addOutputFileDependency(StringRef path) const {
922 addDependencyInfoHelper(_dependencyInfo.get(), 0x40, path);
925 void MachOLinkingContext::appendOrderedSymbol(StringRef symbol,
926 StringRef filename) {
927 // To support sorting static functions which may have the same name in
928 // multiple .o files, _orderFiles maps the symbol name to a vector
929 // of OrderFileNode each of which can specify a file prefix.
931 if (!filename.empty())
932 info.fileFilter = copy(filename);
933 info.order = _orderFileEntries++;
934 _orderFiles[symbol].push_back(info);
938 MachOLinkingContext::findOrderOrdinal(const std::vector<OrderFileNode> &nodes,
939 const DefinedAtom *atom,
941 const File *objFile = &atom->file();
943 StringRef objName = objFile->path();
944 std::pair<StringRef, StringRef> dirAndLeaf = objName.rsplit('/');
945 if (!dirAndLeaf.second.empty())
946 objName = dirAndLeaf.second;
947 for (const OrderFileNode &info : nodes) {
948 if (info.fileFilter.empty()) {
949 // Have unprefixed symbol name in order file that matches this atom.
950 ordinal = info.order;
953 if (info.fileFilter.equals(objName)) {
954 // Have prefixed symbol name in order file that matches atom's path.
955 ordinal = info.order;
962 bool MachOLinkingContext::customAtomOrderer(const DefinedAtom *left,
963 const DefinedAtom *right,
964 bool &leftBeforeRight) const {
965 // No custom sorting if no order file entries.
966 if (!_orderFileEntries)
969 // Order files can only order named atoms.
970 StringRef leftName = left->name();
971 StringRef rightName = right->name();
972 if (leftName.empty() || rightName.empty())
975 // If neither is in order file list, no custom sorter.
976 auto leftPos = _orderFiles.find(leftName);
977 auto rightPos = _orderFiles.find(rightName);
978 bool leftIsOrdered = (leftPos != _orderFiles.end());
979 bool rightIsOrdered = (rightPos != _orderFiles.end());
980 if (!leftIsOrdered && !rightIsOrdered)
983 // There could be multiple symbols with same name but different file prefixes.
987 leftIsOrdered && findOrderOrdinal(leftPos->getValue(), left, leftOrder);
988 bool foundRight = rightIsOrdered &&
989 findOrderOrdinal(rightPos->getValue(), right, rightOrder);
990 if (!foundLeft && !foundRight)
993 // If only one is in order file list, ordered one goes first.
994 if (foundLeft != foundRight)
995 leftBeforeRight = foundLeft;
997 leftBeforeRight = (leftOrder < rightOrder);
1002 static bool isLibrary(const std::unique_ptr<Node> &elem) {
1003 if (FileNode *node = dyn_cast<FileNode>(const_cast<Node *>(elem.get()))) {
1004 File *file = node->getFile();
1005 return isa<SharedLibraryFile>(file) || isa<ArchiveLibraryFile>(file);
1010 // The darwin linker processes input files in two phases. The first phase
1011 // links in all object (.o) files in command line order. The second phase
1012 // links in libraries in command line order.
1013 // In this function we reorder the input files so that all the object files
1014 // comes before any library file. We also make a group for the library files
1015 // so that the Resolver will reiterate over the libraries as long as we find
1016 // new undefines from libraries.
1017 void MachOLinkingContext::finalizeInputFiles() {
1018 std::vector<std::unique_ptr<Node>> &elements = getNodes();
1019 std::stable_sort(elements.begin(), elements.end(),
1020 [](const std::unique_ptr<Node> &a,
1021 const std::unique_ptr<Node> &b) {
1022 return !isLibrary(a) && isLibrary(b);
1024 size_t numLibs = std::count_if(elements.begin(), elements.end(), isLibrary);
1025 elements.push_back(llvm::make_unique<GroupEnd>(numLibs));
1028 llvm::Error MachOLinkingContext::handleLoadedFile(File &file) {
1029 auto *machoFile = dyn_cast<MachOFile>(&file);
1031 return llvm::Error::success();
1033 // Check that the arch of the context matches that of the file.
1034 // Also set the arch of the context if it didn't have one.
1035 if (_arch == arch_unknown) {
1036 _arch = machoFile->arch();
1037 } else if (machoFile->arch() != arch_unknown && machoFile->arch() != _arch) {
1038 // Archs are different.
1039 return llvm::make_error<GenericError>(file.path() +
1040 Twine(" cannot be linked due to incompatible architecture"));
1043 // Check that the OS of the context matches that of the file.
1044 // Also set the OS of the context if it didn't have one.
1045 if (_os == OS::unknown) {
1046 _os = machoFile->OS();
1047 } else if (machoFile->OS() != OS::unknown && machoFile->OS() != _os) {
1048 // OSes are different.
1049 return llvm::make_error<GenericError>(file.path() +
1050 Twine(" cannot be linked due to incompatible operating systems"));
1053 // Check that if the objc info exists, that it is compatible with the target
1055 switch (machoFile->objcConstraint()) {
1057 // The file is not compiled with objc, so skip the checks.
1060 case objc_supports_gc:
1061 llvm_unreachable("GC support should already have thrown an error");
1062 case objc_retainReleaseForSimulator:
1063 // The file is built with simulator objc, so make sure that the context
1064 // is also building with simulator support.
1065 if (_os != OS::iOS_simulator)
1066 return llvm::make_error<GenericError>(file.path() +
1067 Twine(" cannot be linked. It contains ObjC built for the simulator"
1068 " while we are linking a non-simulator target"));
1069 assert((_objcConstraint == objc_unknown ||
1070 _objcConstraint == objc_retainReleaseForSimulator) &&
1071 "Must be linking with retain/release for the simulator");
1072 _objcConstraint = objc_retainReleaseForSimulator;
1074 case objc_retainRelease:
1075 // The file is built without simulator objc, so make sure that the
1076 // context is also building without simulator support.
1077 if (_os == OS::iOS_simulator)
1078 return llvm::make_error<GenericError>(file.path() +
1079 Twine(" cannot be linked. It contains ObjC built for a non-simulator"
1080 " target while we are linking a simulator target"));
1081 assert((_objcConstraint == objc_unknown ||
1082 _objcConstraint == objc_retainRelease) &&
1083 "Must be linking with retain/release for a non-simulator target");
1084 _objcConstraint = objc_retainRelease;
1088 // Check that the swift version of the context matches that of the file.
1089 // Also set the swift version of the context if it didn't have one.
1090 if (!_swiftVersion) {
1091 _swiftVersion = machoFile->swiftVersion();
1092 } else if (machoFile->swiftVersion() &&
1093 machoFile->swiftVersion() != _swiftVersion) {
1094 // Swift versions are different.
1095 return llvm::make_error<GenericError>("different swift versions");
1098 return llvm::Error::success();
1101 } // end namespace lld