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/ReaderWriter/MachOLinkingContext.h"
11 #include "ArchHandler.h"
13 #include "FlatNamespaceFile.h"
14 #include "MachONormalizedFile.h"
15 #include "MachOPasses.h"
16 #include "SectCreateFile.h"
17 #include "lld/Core/ArchiveLibraryFile.h"
18 #include "lld/Core/PassManager.h"
19 #include "lld/Core/Reader.h"
20 #include "lld/Core/Writer.h"
21 #include "lld/Driver/Driver.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/Config/config.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Errc.h"
28 #include "llvm/Support/Host.h"
29 #include "llvm/Support/MachO.h"
30 #include "llvm/Support/Path.h"
33 #if defined(HAVE_CXXABI_H)
37 using lld::mach_o::ArchHandler;
38 using lld::mach_o::MachOFile;
39 using lld::mach_o::MachODylibFile;
40 using namespace llvm::MachO;
44 bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) {
50 SmallVector<StringRef, 3> parts;
51 llvm::SplitString(str, parts, ".");
53 unsigned long long num;
54 if (llvm::getAsUnsignedInteger(parts[0], 10, num))
60 if (parts.size() > 1) {
61 if (llvm::getAsUnsignedInteger(parts[1], 10, num))
68 if (parts.size() > 2) {
69 if (llvm::getAsUnsignedInteger(parts[2], 10, num))
79 bool MachOLinkingContext::parsePackedVersion(StringRef str, uint64_t &result) {
85 SmallVector<StringRef, 5> parts;
86 llvm::SplitString(str, parts, ".");
88 unsigned long long num;
89 if (llvm::getAsUnsignedInteger(parts[0], 10, num))
96 for (StringRef str : llvm::makeArrayRef(parts).slice(1)) {
97 if (llvm::getAsUnsignedInteger(str, 10, num))
101 result |= (num << Shift);
108 MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = {
109 { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL },
110 { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL },
111 { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL },
112 { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 },
113 { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 },
114 { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S },
115 { "arm64", arch_arm64, true, CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL },
116 { "", arch_unknown,false, 0, 0 }
119 MachOLinkingContext::Arch
120 MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) {
121 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
122 if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype))
128 MachOLinkingContext::Arch
129 MachOLinkingContext::archFromName(StringRef archName) {
130 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
131 if (info->archName.equals(archName))
137 StringRef MachOLinkingContext::nameFromArch(Arch arch) {
138 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
139 if (info->arch == arch)
140 return info->archName;
145 uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) {
146 assert(arch != arch_unknown);
147 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
148 if (info->arch == arch)
149 return info->cputype;
151 llvm_unreachable("Unknown arch type");
154 uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) {
155 assert(arch != arch_unknown);
156 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
157 if (info->arch == arch)
158 return info->cpusubtype;
160 llvm_unreachable("Unknown arch type");
163 bool MachOLinkingContext::isThinObjectFile(StringRef path, Arch &arch) {
164 return mach_o::normalized::isThinObjectFile(path, arch);
167 bool MachOLinkingContext::sliceFromFatFile(MemoryBufferRef mb, uint32_t &offset,
169 return mach_o::normalized::sliceFromFatFile(mb, _arch, offset, size);
172 MachOLinkingContext::MachOLinkingContext() {}
174 MachOLinkingContext::~MachOLinkingContext() {
175 // Atoms are allocated on BumpPtrAllocator's on File's.
176 // As we transfer atoms from one file to another, we need to clear all of the
177 // atoms before we remove any of the BumpPtrAllocator's.
178 auto &nodes = getNodes();
179 for (unsigned i = 0, e = nodes.size(); i != e; ++i) {
180 FileNode *node = dyn_cast<FileNode>(nodes[i].get());
183 File *file = node->getFile();
188 void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os,
189 uint32_t minOSVersion,
190 bool exportDynamicSymbols) {
191 _outputMachOType = type;
194 _osMinVersion = minOSVersion;
196 // If min OS not specified on command line, use reasonable defaults.
197 // Note that we only do sensible defaults when emitting something other than
198 // object and preload.
199 if (_outputMachOType != llvm::MachO::MH_OBJECT &&
200 _outputMachOType != llvm::MachO::MH_PRELOAD) {
201 if (minOSVersion == 0) {
205 parsePackedVersion("10.8", _osMinVersion);
206 _os = MachOLinkingContext::OS::macOSX;
212 parsePackedVersion("7.0", _osMinVersion);
213 _os = MachOLinkingContext::OS::iOS;
221 switch (_outputMachOType) {
222 case llvm::MachO::MH_EXECUTE:
223 // If targeting newer OS, use _main
224 if (minOS("10.8", "6.0")) {
225 _entrySymbolName = "_main";
227 // If targeting older OS, use start (in crt1.o)
228 _entrySymbolName = "start";
231 // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not
232 // support) and 4KB on 32-bit.
233 if (is64Bit(_arch)) {
234 _pageZeroSize = 0x100000000;
236 _pageZeroSize = 0x1000;
239 // Initial base address is __PAGEZERO size.
240 _baseAddress = _pageZeroSize;
242 // Make PIE by default when targetting newer OSs.
245 if (minOSVersion >= 0x000A0700) // MacOSX 10.7
249 if (minOSVersion >= 0x00040300) // iOS 4.3
252 case OS::iOS_simulator:
258 setGlobalsAreDeadStripRoots(exportDynamicSymbols);
260 case llvm::MachO::MH_DYLIB:
261 setGlobalsAreDeadStripRoots(exportDynamicSymbols);
263 case llvm::MachO::MH_BUNDLE:
265 case llvm::MachO::MH_OBJECT:
266 _printRemainingUndefines = false;
267 _allowRemainingUndefines = true;
272 // Set default segment page sizes based on arch.
273 if (arch == arch_arm64)
277 uint32_t MachOLinkingContext::getCPUType() const {
278 return cpuTypeFromArch(_arch);
281 uint32_t MachOLinkingContext::getCPUSubType() const {
282 return cpuSubtypeFromArch(_arch);
285 bool MachOLinkingContext::is64Bit(Arch arch) {
286 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
287 if (info->arch == arch) {
288 return (info->cputype & CPU_ARCH_ABI64);
291 // unknown archs are not 64-bit.
295 bool MachOLinkingContext::isHostEndian(Arch arch) {
296 assert(arch != arch_unknown);
297 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
298 if (info->arch == arch) {
299 return (info->littleEndian == llvm::sys::IsLittleEndianHost);
302 llvm_unreachable("Unknown arch type");
305 bool MachOLinkingContext::isBigEndian(Arch arch) {
306 assert(arch != arch_unknown);
307 for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) {
308 if (info->arch == arch) {
309 return ! info->littleEndian;
312 llvm_unreachable("Unknown arch type");
315 bool MachOLinkingContext::is64Bit() const {
316 return is64Bit(_arch);
319 bool MachOLinkingContext::outputTypeHasEntry() const {
320 switch (_outputMachOType) {
330 bool MachOLinkingContext::needsStubsPass() const {
331 switch (_outputMachOType) {
333 return !_outputMachOTypeStatic;
342 bool MachOLinkingContext::needsGOTPass() const {
343 // GOT pass not used in -r mode.
344 if (_outputMachOType == MH_OBJECT)
346 // Only some arches use GOT pass.
356 bool MachOLinkingContext::needsCompactUnwindPass() const {
357 switch (_outputMachOType) {
361 return archHandler().needsCompactUnwind();
367 bool MachOLinkingContext::needsObjCPass() const {
368 // ObjC pass is only needed if any of the inputs were ObjC.
369 return _objcConstraint != objc_unknown;
372 bool MachOLinkingContext::needsShimPass() const {
373 // Shim pass only used in final executables.
374 if (_outputMachOType == MH_OBJECT)
376 // Only 32-bit arm arches use Shim pass.
387 bool MachOLinkingContext::needsTLVPass() const {
388 switch (_outputMachOType) {
398 StringRef MachOLinkingContext::binderSymbolName() const {
399 return archHandler().stubInfo().binderSymbolName;
402 bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const {
403 uint32_t parsedVersion;
406 if (parsePackedVersion(mac, parsedVersion))
408 return _osMinVersion >= parsedVersion;
410 case OS::iOS_simulator:
411 if (parsePackedVersion(iOS, parsedVersion))
413 return _osMinVersion >= parsedVersion;
415 // If we don't know the target, then assume that we don't meet the min OS.
416 // This matches the ld64 behaviour
419 llvm_unreachable("invalid OS enum");
422 bool MachOLinkingContext::addEntryPointLoadCommand() const {
423 if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) {
424 return minOS("10.8", "6.0");
429 bool MachOLinkingContext::addUnixThreadLoadCommand() const {
430 switch (_outputMachOType) {
432 if (_outputMachOTypeStatic)
435 return !minOS("10.8", "6.0");
445 bool MachOLinkingContext::pathExists(StringRef path) const {
446 if (!_testingFileUsage)
447 return llvm::sys::fs::exists(path.str());
449 // Otherwise, we're in test mode: only files explicitly provided on the
450 // command-line exist.
451 std::string key = path.str();
452 std::replace(key.begin(), key.end(), '\\', '/');
453 return _existingPaths.find(key) != _existingPaths.end();
456 bool MachOLinkingContext::fileExists(StringRef path) const {
457 bool found = pathExists(path);
458 // Log search misses.
460 addInputFileNotFound(path);
462 // When testing, file is never opened, so logging is done here.
463 if (_testingFileUsage && found)
464 addInputFileDependency(path);
469 void MachOLinkingContext::setSysLibRoots(const StringRefVector &paths) {
470 _syslibRoots = paths;
473 void MachOLinkingContext::addRpath(StringRef rpath) {
474 _rpaths.push_back(rpath);
477 void MachOLinkingContext::addModifiedSearchDir(StringRef libPath,
479 bool addedModifiedPath = false;
481 // -syslibroot only applies to absolute paths.
482 if (libPath.startswith("/")) {
483 for (auto syslibRoot : _syslibRoots) {
484 SmallString<256> path(syslibRoot);
485 llvm::sys::path::append(path, libPath);
486 if (pathExists(path)) {
487 _searchDirs.push_back(path.str().copy(_allocator));
488 addedModifiedPath = true;
493 if (addedModifiedPath)
496 // Finally, if only one -syslibroot is given, system paths which aren't in it
498 if (_syslibRoots.size() != 1 || !isSystemPath) {
499 if (pathExists(libPath)) {
500 _searchDirs.push_back(libPath);
505 void MachOLinkingContext::addFrameworkSearchDir(StringRef fwPath,
507 bool pathAdded = false;
509 // -syslibroot only used with to absolute framework search paths.
510 if (fwPath.startswith("/")) {
511 for (auto syslibRoot : _syslibRoots) {
512 SmallString<256> path(syslibRoot);
513 llvm::sys::path::append(path, fwPath);
514 if (pathExists(path)) {
515 _frameworkDirs.push_back(path.str().copy(_allocator));
520 // If fwPath found in any -syslibroot, then done.
524 // If only one -syslibroot, system paths not in that SDK are suppressed.
525 if (isSystemPath && (_syslibRoots.size() == 1))
528 // Only use raw fwPath if that directory exists.
529 if (pathExists(fwPath))
530 _frameworkDirs.push_back(fwPath);
533 llvm::Optional<StringRef>
534 MachOLinkingContext::searchDirForLibrary(StringRef path,
535 StringRef libName) const {
536 SmallString<256> fullPath;
537 if (libName.endswith(".o")) {
538 // A request ending in .o is special: just search for the file directly.
539 fullPath.assign(path);
540 llvm::sys::path::append(fullPath, libName);
541 if (fileExists(fullPath))
542 return fullPath.str().copy(_allocator);
546 // Search for dynamic library
547 fullPath.assign(path);
548 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib");
549 if (fileExists(fullPath))
550 return fullPath.str().copy(_allocator);
552 // If not, try for a static library
553 fullPath.assign(path);
554 llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a");
555 if (fileExists(fullPath))
556 return fullPath.str().copy(_allocator);
561 llvm::Optional<StringRef>
562 MachOLinkingContext::searchLibrary(StringRef libName) const {
563 SmallString<256> path;
564 for (StringRef dir : searchDirs()) {
565 llvm::Optional<StringRef> searchDir = searchDirForLibrary(dir, libName);
573 llvm::Optional<StringRef>
574 MachOLinkingContext::findPathForFramework(StringRef fwName) const{
575 SmallString<256> fullPath;
576 for (StringRef dir : frameworkDirs()) {
577 fullPath.assign(dir);
578 llvm::sys::path::append(fullPath, Twine(fwName) + ".framework", fwName);
579 if (fileExists(fullPath))
580 return fullPath.str().copy(_allocator);
586 bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) {
587 // TODO: if -arch not specified, look at arch of first .o file.
589 if (_currentVersion && _outputMachOType != MH_DYLIB) {
590 diagnostics << "error: -current_version can only be used with dylibs\n";
594 if (_compatibilityVersion && _outputMachOType != MH_DYLIB) {
596 << "error: -compatibility_version can only be used with dylibs\n";
600 if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) {
602 << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n";
606 if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) {
608 << "error: -bundle_loader can only be used with Mach-O bundles\n";
612 // If -exported_symbols_list used, all exported symbols must be defined.
613 if (_exportMode == ExportMode::whiteList) {
614 for (const auto &symbol : _exportedSymbols)
615 addInitialUndefinedSymbol(symbol.getKey());
618 // If -dead_strip, set up initial live symbols.
620 // Entry point is live.
621 if (outputTypeHasEntry())
622 addDeadStripRoot(entrySymbolName());
623 // Lazy binding helper is live.
624 if (needsStubsPass())
625 addDeadStripRoot(binderSymbolName());
626 // If using -exported_symbols_list, make all exported symbols live.
627 if (_exportMode == ExportMode::whiteList) {
628 setGlobalsAreDeadStripRoots(false);
629 for (const auto &symbol : _exportedSymbols)
630 addDeadStripRoot(symbol.getKey());
634 addOutputFileDependency(outputPath());
639 void MachOLinkingContext::addPasses(PassManager &pm) {
640 // objc pass should be before layout pass. Otherwise test cases may contain
641 // no atoms which confuses the layout pass.
643 mach_o::addObjCPass(pm, *this);
644 mach_o::addLayoutPass(pm, *this);
645 if (needsStubsPass())
646 mach_o::addStubsPass(pm, *this);
647 if (needsCompactUnwindPass())
648 mach_o::addCompactUnwindPass(pm, *this);
650 mach_o::addGOTPass(pm, *this);
652 mach_o::addTLVPass(pm, *this);
654 mach_o::addShimPass(pm, *this); // Shim pass must run after stubs pass.
657 Writer &MachOLinkingContext::writer() const {
659 _writer = createWriterMachO(*this);
663 ErrorOr<std::unique_ptr<MemoryBuffer>>
664 MachOLinkingContext::getMemoryBuffer(StringRef path) {
665 addInputFileDependency(path);
667 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr =
668 MemoryBuffer::getFileOrSTDIN(path);
669 if (std::error_code ec = mbOrErr.getError())
671 std::unique_ptr<MemoryBuffer> mb = std::move(mbOrErr.get());
673 // If buffer contains a fat file, find required arch in fat buffer
674 // and switch buffer to point to just that required slice.
677 if (sliceFromFatFile(mb->getMemBufferRef(), offset, size))
678 return MemoryBuffer::getFileSlice(path, size, offset);
679 return std::move(mb);
682 MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) {
683 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = getMemoryBuffer(path);
684 if (mbOrErr.getError())
687 ErrorOr<std::unique_ptr<File>> fileOrErr =
688 registry().loadFile(std::move(mbOrErr.get()));
691 std::unique_ptr<File> &file = fileOrErr.get();
693 MachODylibFile *result = reinterpret_cast<MachODylibFile *>(file.get());
694 // Node object now owned by _indirectDylibs vector.
695 _indirectDylibs.push_back(std::move(file));
699 MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) {
700 // See if already loaded.
701 auto pos = _pathToDylibMap.find(path);
702 if (pos != _pathToDylibMap.end())
705 // Search -L paths if of the form "libXXX.dylib"
706 std::pair<StringRef, StringRef> split = path.rsplit('/');
707 StringRef leafName = split.second;
708 if (leafName.startswith("lib") && leafName.endswith(".dylib")) {
709 // FIXME: Need to enhance searchLibrary() to only look for .dylib
710 auto libPath = searchLibrary(leafName);
712 return loadIndirectDylib(libPath.getValue());
715 // Try full path with sysroot.
716 for (StringRef sysPath : _syslibRoots) {
717 SmallString<256> fullPath;
718 fullPath.assign(sysPath);
719 llvm::sys::path::append(fullPath, path);
720 if (pathExists(fullPath))
721 return loadIndirectDylib(fullPath);
725 if (pathExists(path)) {
726 return loadIndirectDylib(path);
732 uint32_t MachOLinkingContext::dylibCurrentVersion(StringRef installName) const {
733 auto pos = _pathToDylibMap.find(installName);
734 if (pos != _pathToDylibMap.end())
735 return pos->second->currentVersion();
737 return 0x1000; // 1.0
740 uint32_t MachOLinkingContext::dylibCompatVersion(StringRef installName) const {
741 auto pos = _pathToDylibMap.find(installName);
742 if (pos != _pathToDylibMap.end())
743 return pos->second->compatVersion();
745 return 0x1000; // 1.0
748 void MachOLinkingContext::createImplicitFiles(
749 std::vector<std::unique_ptr<File> > &result) {
750 // Add indirect dylibs by asking each linked dylib to add its indirects.
751 // Iterate until no more dylibs get loaded.
752 size_t dylibCount = 0;
753 while (dylibCount != _allDylibs.size()) {
754 dylibCount = _allDylibs.size();
755 for (MachODylibFile *dylib : _allDylibs) {
756 dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* {
757 return findIndirectDylib(path); });
761 // Let writer add output type specific extras.
762 writer().createImplicitFiles(result);
764 // If undefinedMode is != error, add a FlatNamespaceFile instance. This will
765 // provide a SharedLibraryAtom for symbols that aren't defined elsewhere.
766 if (undefinedMode() != UndefinedMode::error) {
767 result.emplace_back(new mach_o::FlatNamespaceFile(*this));
768 _flatNamespaceFile = result.back().get();
772 void MachOLinkingContext::registerDylib(MachODylibFile *dylib,
774 std::lock_guard<std::mutex> lock(_dylibsMutex);
775 _allDylibs.insert(dylib);
776 _pathToDylibMap[dylib->installName()] = dylib;
777 // If path is different than install name, register path too.
778 if (!dylib->path().equals(dylib->installName()))
779 _pathToDylibMap[dylib->path()] = dylib;
781 _upwardDylibs.insert(dylib);
784 bool MachOLinkingContext::isUpwardDylib(StringRef installName) const {
785 for (MachODylibFile *dylib : _upwardDylibs) {
786 if (dylib->installName().equals(installName))
792 ArchHandler &MachOLinkingContext::archHandler() const {
794 _archHandler = ArchHandler::create(_arch);
795 return *_archHandler;
798 void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect,
800 SectionAlign entry = { seg, sect, align };
801 _sectAligns.push_back(entry);
804 void MachOLinkingContext::addSectCreateSection(
805 StringRef seg, StringRef sect,
806 std::unique_ptr<MemoryBuffer> content) {
808 if (!_sectCreateFile) {
809 auto sectCreateFile = llvm::make_unique<mach_o::SectCreateFile>();
810 _sectCreateFile = sectCreateFile.get();
811 getNodes().push_back(llvm::make_unique<FileNode>(std::move(sectCreateFile)));
814 assert(_sectCreateFile && "sectcreate file does not exist.");
815 _sectCreateFile->addSection(seg, sect, std::move(content));
818 bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect,
819 uint16_t &align) const {
820 for (const SectionAlign &entry : _sectAligns) {
821 if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) {
829 void MachOLinkingContext::addExportSymbol(StringRef sym) {
830 // Support old crufty export lists with bogus entries.
831 if (sym.endswith(".eh") || sym.startswith(".objc_category_name_")) {
832 llvm::errs() << "warning: ignoring " << sym << " in export list\n";
835 // Only i386 MacOSX uses old ABI, so don't change those.
836 if ((_os != OS::macOSX) || (_arch != arch_x86)) {
837 // ObjC has two differnent ABIs. Be nice and allow one export list work for
838 // both ABIs by renaming symbols.
839 if (sym.startswith(".objc_class_name_")) {
840 std::string abi2className("_OBJC_CLASS_$_");
841 abi2className += sym.substr(17);
842 _exportedSymbols.insert(copy(abi2className));
843 std::string abi2metaclassName("_OBJC_METACLASS_$_");
844 abi2metaclassName += sym.substr(17);
845 _exportedSymbols.insert(copy(abi2metaclassName));
850 // FIXME: Support wildcards.
851 _exportedSymbols.insert(sym);
854 bool MachOLinkingContext::exportSymbolNamed(StringRef sym) const {
855 switch (_exportMode) {
856 case ExportMode::globals:
857 llvm_unreachable("exportSymbolNamed() should not be called in this mode");
859 case ExportMode::whiteList:
860 return _exportedSymbols.count(sym);
861 case ExportMode::blackList:
862 return !_exportedSymbols.count(sym);
864 llvm_unreachable("_exportMode unknown enum value");
867 std::string MachOLinkingContext::demangle(StringRef symbolName) const {
868 // Only try to demangle symbols if -demangle on command line
869 if (!demangleSymbols())
872 // Only try to demangle symbols that look like C++ symbols
873 if (!symbolName.startswith("__Z"))
876 #if defined(HAVE_CXXABI_H)
877 SmallString<256> symBuff;
878 StringRef nullTermSym = Twine(symbolName).toNullTerminatedStringRef(symBuff);
879 // Mach-O has extra leading underscore that needs to be removed.
880 const char *cstr = nullTermSym.data() + 1;
882 char *demangled = abi::__cxa_demangle(cstr, nullptr, nullptr, &status);
884 std::string result(demangled);
885 // __cxa_demangle() always uses a malloc'ed buffer to return the result.
894 std::error_code MachOLinkingContext::createDependencyFile(StringRef path) {
896 _dependencyInfo = std::unique_ptr<llvm::raw_fd_ostream>(new
897 llvm::raw_fd_ostream(path, ec, llvm::sys::fs::F_None));
899 _dependencyInfo.reset();
903 char linkerVersionOpcode = 0x00;
904 *_dependencyInfo << linkerVersionOpcode;
905 *_dependencyInfo << "lld"; // FIXME
906 *_dependencyInfo << '\0';
908 return std::error_code();
911 void MachOLinkingContext::addInputFileDependency(StringRef path) const {
912 if (!_dependencyInfo)
915 char inputFileOpcode = 0x10;
916 *_dependencyInfo << inputFileOpcode;
917 *_dependencyInfo << path;
918 *_dependencyInfo << '\0';
921 void MachOLinkingContext::addInputFileNotFound(StringRef path) const {
922 if (!_dependencyInfo)
925 char inputFileOpcode = 0x11;
926 *_dependencyInfo << inputFileOpcode;
927 *_dependencyInfo << path;
928 *_dependencyInfo << '\0';
931 void MachOLinkingContext::addOutputFileDependency(StringRef path) const {
932 if (!_dependencyInfo)
935 char outputFileOpcode = 0x40;
936 *_dependencyInfo << outputFileOpcode;
937 *_dependencyInfo << path;
938 *_dependencyInfo << '\0';
941 void MachOLinkingContext::appendOrderedSymbol(StringRef symbol,
942 StringRef filename) {
943 // To support sorting static functions which may have the same name in
944 // multiple .o files, _orderFiles maps the symbol name to a vector
945 // of OrderFileNode each of which can specify a file prefix.
947 if (!filename.empty())
948 info.fileFilter = copy(filename);
949 info.order = _orderFileEntries++;
950 _orderFiles[symbol].push_back(info);
954 MachOLinkingContext::findOrderOrdinal(const std::vector<OrderFileNode> &nodes,
955 const DefinedAtom *atom,
957 const File *objFile = &atom->file();
959 StringRef objName = objFile->path();
960 std::pair<StringRef, StringRef> dirAndLeaf = objName.rsplit('/');
961 if (!dirAndLeaf.second.empty())
962 objName = dirAndLeaf.second;
963 for (const OrderFileNode &info : nodes) {
964 if (info.fileFilter.empty()) {
965 // Have unprefixed symbol name in order file that matches this atom.
966 ordinal = info.order;
969 if (info.fileFilter.equals(objName)) {
970 // Have prefixed symbol name in order file that matches atom's path.
971 ordinal = info.order;
978 bool MachOLinkingContext::customAtomOrderer(const DefinedAtom *left,
979 const DefinedAtom *right,
980 bool &leftBeforeRight) const {
981 // No custom sorting if no order file entries.
982 if (!_orderFileEntries)
985 // Order files can only order named atoms.
986 StringRef leftName = left->name();
987 StringRef rightName = right->name();
988 if (leftName.empty() || rightName.empty())
991 // If neither is in order file list, no custom sorter.
992 auto leftPos = _orderFiles.find(leftName);
993 auto rightPos = _orderFiles.find(rightName);
994 bool leftIsOrdered = (leftPos != _orderFiles.end());
995 bool rightIsOrdered = (rightPos != _orderFiles.end());
996 if (!leftIsOrdered && !rightIsOrdered)
999 // There could be multiple symbols with same name but different file prefixes.
1001 unsigned rightOrder;
1003 leftIsOrdered && findOrderOrdinal(leftPos->getValue(), left, leftOrder);
1004 bool foundRight = rightIsOrdered &&
1005 findOrderOrdinal(rightPos->getValue(), right, rightOrder);
1006 if (!foundLeft && !foundRight)
1009 // If only one is in order file list, ordered one goes first.
1010 if (foundLeft != foundRight)
1011 leftBeforeRight = foundLeft;
1013 leftBeforeRight = (leftOrder < rightOrder);
1018 static bool isLibrary(const std::unique_ptr<Node> &elem) {
1019 if (FileNode *node = dyn_cast<FileNode>(const_cast<Node *>(elem.get()))) {
1020 File *file = node->getFile();
1021 return isa<SharedLibraryFile>(file) || isa<ArchiveLibraryFile>(file);
1026 // The darwin linker processes input files in two phases. The first phase
1027 // links in all object (.o) files in command line order. The second phase
1028 // links in libraries in command line order.
1029 // In this function we reorder the input files so that all the object files
1030 // comes before any library file. We also make a group for the library files
1031 // so that the Resolver will reiterate over the libraries as long as we find
1032 // new undefines from libraries.
1033 void MachOLinkingContext::finalizeInputFiles() {
1034 std::vector<std::unique_ptr<Node>> &elements = getNodes();
1035 std::stable_sort(elements.begin(), elements.end(),
1036 [](const std::unique_ptr<Node> &a,
1037 const std::unique_ptr<Node> &b) {
1038 return !isLibrary(a) && isLibrary(b);
1040 size_t numLibs = std::count_if(elements.begin(), elements.end(), isLibrary);
1041 elements.push_back(llvm::make_unique<GroupEnd>(numLibs));
1044 llvm::Error MachOLinkingContext::handleLoadedFile(File &file) {
1045 auto *machoFile = dyn_cast<MachOFile>(&file);
1047 return llvm::Error();
1049 // Check that the arch of the context matches that of the file.
1050 // Also set the arch of the context if it didn't have one.
1051 if (_arch == arch_unknown) {
1052 _arch = machoFile->arch();
1053 } else if (machoFile->arch() != arch_unknown && machoFile->arch() != _arch) {
1054 // Archs are different.
1055 return llvm::make_error<GenericError>(file.path() +
1056 Twine(" cannot be linked due to incompatible architecture"));
1059 // Check that the OS of the context matches that of the file.
1060 // Also set the OS of the context if it didn't have one.
1061 if (_os == OS::unknown) {
1062 _os = machoFile->OS();
1063 } else if (machoFile->OS() != OS::unknown && machoFile->OS() != _os) {
1064 // OSes are different.
1065 return llvm::make_error<GenericError>(file.path() +
1066 Twine(" cannot be linked due to incompatible operating systems"));
1069 // Check that if the objc info exists, that it is compatible with the target
1071 switch (machoFile->objcConstraint()) {
1073 // The file is not compiled with objc, so skip the checks.
1076 case objc_supports_gc:
1077 llvm_unreachable("GC support should already have thrown an error");
1078 case objc_retainReleaseForSimulator:
1079 // The file is built with simulator objc, so make sure that the context
1080 // is also building with simulator support.
1081 if (_os != OS::iOS_simulator)
1082 return llvm::make_error<GenericError>(file.path() +
1083 Twine(" cannot be linked. It contains ObjC built for the simulator"
1084 " while we are linking a non-simulator target"));
1085 assert((_objcConstraint == objc_unknown ||
1086 _objcConstraint == objc_retainReleaseForSimulator) &&
1087 "Must be linking with retain/release for the simulator");
1088 _objcConstraint = objc_retainReleaseForSimulator;
1090 case objc_retainRelease:
1091 // The file is built without simulator objc, so make sure that the
1092 // context is also building without simulator support.
1093 if (_os == OS::iOS_simulator)
1094 return llvm::make_error<GenericError>(file.path() +
1095 Twine(" cannot be linked. It contains ObjC built for a non-simulator"
1096 " target while we are linking a simulator target"));
1097 assert((_objcConstraint == objc_unknown ||
1098 _objcConstraint == objc_retainRelease) &&
1099 "Must be linking with retain/release for a non-simulator target");
1100 _objcConstraint = objc_retainRelease;
1104 // Check that the swift version of the context matches that of the file.
1105 // Also set the swift version of the context if it didn't have one.
1106 if (!_swiftVersion) {
1107 _swiftVersion = machoFile->swiftVersion();
1108 } else if (machoFile->swiftVersion() &&
1109 machoFile->swiftVersion() != _swiftVersion) {
1110 // Swift versions are different.
1111 return llvm::make_error<GenericError>("different swift versions");
1114 return llvm::Error();
1117 } // end namespace lld