Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/MachOLinkingContext.cpp ---------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Brian Gesiak | b9f7f4b | 2018-06-12 02:34:04 +0000 | [diff] [blame] | 9 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 10 | #include "lld/ReaderWriter/MachOLinkingContext.h" |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 11 | #include "ArchHandler.h" |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 12 | #include "File.h" |
Lang Hames | 5c69200 | 2015-09-28 20:25:14 +0000 | [diff] [blame] | 13 | #include "FlatNamespaceFile.h" |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 14 | #include "MachONormalizedFile.h" |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 15 | #include "MachOPasses.h" |
Lang Hames | b1b67f4 | 2015-10-24 08:20:51 +0000 | [diff] [blame] | 16 | #include "SectCreateFile.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 17 | #include "lld/Common/Driver.h" |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 18 | #include "lld/Core/ArchiveLibraryFile.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 19 | #include "lld/Core/PassManager.h" |
Greg Fitzgerald | 4b6a7e3 | 2015-01-21 22:54:56 +0000 | [diff] [blame] | 20 | #include "lld/Core/Reader.h" |
| 21 | #include "lld/Core/Writer.h" |
Benjamin Kramer | 06a42af | 2015-03-02 00:48:06 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 25 | #include "llvm/BinaryFormat/MachO.h" |
Rafael Espindola | d194213 | 2016-09-06 19:17:14 +0000 | [diff] [blame] | 26 | #include "llvm/Demangle/Demangle.h" |
Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 89642a7 | 2015-01-14 11:26:52 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Errc.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Host.h" |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Path.h" |
Rui Ueyama | 57a2953 | 2014-08-06 19:37:35 +0000 | [diff] [blame] | 31 | #include <algorithm> |
| 32 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 33 | using lld::mach_o::ArchHandler; |
Pete Cooper | 99f3b94 | 2016-01-14 23:25:06 +0000 | [diff] [blame] | 34 | using lld::mach_o::MachOFile; |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 35 | using lld::mach_o::MachODylibFile; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 36 | using namespace llvm::MachO; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 37 | |
| 38 | namespace lld { |
| 39 | |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 40 | bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) { |
| 41 | result = 0; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 42 | |
| 43 | if (str.empty()) |
| 44 | return false; |
| 45 | |
| 46 | SmallVector<StringRef, 3> parts; |
| 47 | llvm::SplitString(str, parts, "."); |
| 48 | |
| 49 | unsigned long long num; |
| 50 | if (llvm::getAsUnsignedInteger(parts[0], 10, num)) |
| 51 | return true; |
| 52 | if (num > 65535) |
| 53 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 54 | result = num << 16; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 55 | |
| 56 | if (parts.size() > 1) { |
| 57 | if (llvm::getAsUnsignedInteger(parts[1], 10, num)) |
| 58 | return true; |
| 59 | if (num > 255) |
| 60 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 61 | result |= (num << 8); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (parts.size() > 2) { |
| 65 | if (llvm::getAsUnsignedInteger(parts[2], 10, num)) |
| 66 | return true; |
| 67 | if (num > 255) |
| 68 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 69 | result |= num; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
Pete Cooper | 40576fa | 2016-02-04 02:45:23 +0000 | [diff] [blame] | 75 | bool MachOLinkingContext::parsePackedVersion(StringRef str, uint64_t &result) { |
| 76 | result = 0; |
| 77 | |
| 78 | if (str.empty()) |
| 79 | return false; |
| 80 | |
| 81 | SmallVector<StringRef, 5> parts; |
| 82 | llvm::SplitString(str, parts, "."); |
| 83 | |
Pete Cooper | d5c0e4d | 2016-02-04 02:50:47 +0000 | [diff] [blame] | 84 | unsigned long long num; |
Pete Cooper | 40576fa | 2016-02-04 02:45:23 +0000 | [diff] [blame] | 85 | if (llvm::getAsUnsignedInteger(parts[0], 10, num)) |
| 86 | return true; |
| 87 | if (num > 0xFFFFFF) |
| 88 | return true; |
| 89 | result = num << 40; |
| 90 | |
| 91 | unsigned Shift = 30; |
| 92 | for (StringRef str : llvm::makeArrayRef(parts).slice(1)) { |
| 93 | if (llvm::getAsUnsignedInteger(str, 10, num)) |
| 94 | return true; |
| 95 | if (num > 0x3FF) |
| 96 | return true; |
| 97 | result |= (num << Shift); |
| 98 | Shift -= 10; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 104 | MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = { |
| 105 | { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL }, |
| 106 | { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL }, |
| 107 | { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL }, |
| 108 | { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 }, |
| 109 | { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 }, |
| 110 | { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S }, |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 111 | { "arm64", arch_arm64, true, CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL }, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 112 | { "", arch_unknown,false, 0, 0 } |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | MachOLinkingContext::Arch |
| 116 | MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 117 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 118 | if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype)) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 119 | return info->arch; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 120 | } |
| 121 | return arch_unknown; |
| 122 | } |
| 123 | |
| 124 | MachOLinkingContext::Arch |
| 125 | MachOLinkingContext::archFromName(StringRef archName) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 126 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 127 | if (info->archName.equals(archName)) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 128 | return info->arch; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 129 | } |
| 130 | return arch_unknown; |
| 131 | } |
| 132 | |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 133 | StringRef MachOLinkingContext::nameFromArch(Arch arch) { |
| 134 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 135 | if (info->arch == arch) |
| 136 | return info->archName; |
| 137 | } |
| 138 | return "<unknown>"; |
| 139 | } |
| 140 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 141 | uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) { |
| 142 | assert(arch != arch_unknown); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 143 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 144 | if (info->arch == arch) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 145 | return info->cputype; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 146 | } |
| 147 | llvm_unreachable("Unknown arch type"); |
| 148 | } |
| 149 | |
| 150 | uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) { |
| 151 | assert(arch != arch_unknown); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 152 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 153 | if (info->arch == arch) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 154 | return info->cpusubtype; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 155 | } |
| 156 | llvm_unreachable("Unknown arch type"); |
| 157 | } |
| 158 | |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 159 | bool MachOLinkingContext::isThinObjectFile(StringRef path, Arch &arch) { |
| 160 | return mach_o::normalized::isThinObjectFile(path, arch); |
| 161 | } |
| 162 | |
Rafael Espindola | ed48e53 | 2015-04-27 22:48:51 +0000 | [diff] [blame] | 163 | bool MachOLinkingContext::sliceFromFatFile(MemoryBufferRef mb, uint32_t &offset, |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 164 | uint32_t &size) { |
| 165 | return mach_o::normalized::sliceFromFatFile(mb, _arch, offset, size); |
| 166 | } |
| 167 | |
Rui Ueyama | 5150255 | 2016-03-02 19:06:20 +0000 | [diff] [blame] | 168 | MachOLinkingContext::MachOLinkingContext() {} |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 169 | |
Pete Cooper | 8ad55fb | 2016-03-22 17:15:50 +0000 | [diff] [blame] | 170 | MachOLinkingContext::~MachOLinkingContext() { |
| 171 | // Atoms are allocated on BumpPtrAllocator's on File's. |
| 172 | // As we transfer atoms from one file to another, we need to clear all of the |
| 173 | // atoms before we remove any of the BumpPtrAllocator's. |
| 174 | auto &nodes = getNodes(); |
| 175 | for (unsigned i = 0, e = nodes.size(); i != e; ++i) { |
| 176 | FileNode *node = dyn_cast<FileNode>(nodes[i].get()); |
| 177 | if (!node) |
| 178 | continue; |
| 179 | File *file = node->getFile(); |
| 180 | file->clearAtoms(); |
| 181 | } |
| 182 | } |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 183 | |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 184 | void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os, |
Pete Cooper | 3511645 | 2016-01-22 21:13:24 +0000 | [diff] [blame] | 185 | uint32_t minOSVersion, |
| 186 | bool exportDynamicSymbols) { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 187 | _outputMachOType = type; |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 188 | _arch = arch; |
| 189 | _os = os; |
| 190 | _osMinVersion = minOSVersion; |
| 191 | |
Nick Kledzik | cb2018f | 2014-10-09 01:01:16 +0000 | [diff] [blame] | 192 | // If min OS not specified on command line, use reasonable defaults. |
Pete Cooper | 3dd478a | 2016-02-04 01:57:59 +0000 | [diff] [blame] | 193 | // Note that we only do sensible defaults when emitting something other than |
| 194 | // object and preload. |
| 195 | if (_outputMachOType != llvm::MachO::MH_OBJECT && |
| 196 | _outputMachOType != llvm::MachO::MH_PRELOAD) { |
| 197 | if (minOSVersion == 0) { |
| 198 | switch (_arch) { |
| 199 | case arch_x86_64: |
| 200 | case arch_x86: |
| 201 | parsePackedVersion("10.8", _osMinVersion); |
| 202 | _os = MachOLinkingContext::OS::macOSX; |
| 203 | break; |
| 204 | case arch_armv6: |
| 205 | case arch_armv7: |
| 206 | case arch_armv7s: |
| 207 | case arch_arm64: |
| 208 | parsePackedVersion("7.0", _osMinVersion); |
| 209 | _os = MachOLinkingContext::OS::iOS; |
| 210 | break; |
| 211 | default: |
| 212 | break; |
| 213 | } |
Nick Kledzik | cb2018f | 2014-10-09 01:01:16 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 217 | switch (_outputMachOType) { |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 218 | case llvm::MachO::MH_EXECUTE: |
| 219 | // If targeting newer OS, use _main |
| 220 | if (minOS("10.8", "6.0")) { |
| 221 | _entrySymbolName = "_main"; |
| 222 | } else { |
| 223 | // If targeting older OS, use start (in crt1.o) |
| 224 | _entrySymbolName = "start"; |
| 225 | } |
| 226 | |
| 227 | // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not |
| 228 | // support) and 4KB on 32-bit. |
| 229 | if (is64Bit(_arch)) { |
| 230 | _pageZeroSize = 0x100000000; |
| 231 | } else { |
| 232 | _pageZeroSize = 0x1000; |
| 233 | } |
| 234 | |
Lang Hames | c8034428 | 2015-09-21 22:06:02 +0000 | [diff] [blame] | 235 | // Initial base address is __PAGEZERO size. |
| 236 | _baseAddress = _pageZeroSize; |
| 237 | |
Nick Kledzik | b7035ae | 2014-09-09 00:17:52 +0000 | [diff] [blame] | 238 | // Make PIE by default when targetting newer OSs. |
| 239 | switch (os) { |
| 240 | case OS::macOSX: |
| 241 | if (minOSVersion >= 0x000A0700) // MacOSX 10.7 |
| 242 | _pie = true; |
| 243 | break; |
| 244 | case OS::iOS: |
| 245 | if (minOSVersion >= 0x00040300) // iOS 4.3 |
| 246 | _pie = true; |
| 247 | break; |
| 248 | case OS::iOS_simulator: |
| 249 | _pie = true; |
| 250 | break; |
| 251 | case OS::unknown: |
| 252 | break; |
| 253 | } |
Pete Cooper | 3511645 | 2016-01-22 21:13:24 +0000 | [diff] [blame] | 254 | setGlobalsAreDeadStripRoots(exportDynamicSymbols); |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 255 | break; |
| 256 | case llvm::MachO::MH_DYLIB: |
Pete Cooper | 3511645 | 2016-01-22 21:13:24 +0000 | [diff] [blame] | 257 | setGlobalsAreDeadStripRoots(exportDynamicSymbols); |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 258 | break; |
| 259 | case llvm::MachO::MH_BUNDLE: |
| 260 | break; |
| 261 | case llvm::MachO::MH_OBJECT: |
| 262 | _printRemainingUndefines = false; |
| 263 | _allowRemainingUndefines = true; |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 264 | break; |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 265 | default: |
| 266 | break; |
| 267 | } |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 268 | |
| 269 | // Set default segment page sizes based on arch. |
| 270 | if (arch == arch_arm64) |
| 271 | _pageSize = 4*4096; |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 274 | uint32_t MachOLinkingContext::getCPUType() const { |
| 275 | return cpuTypeFromArch(_arch); |
| 276 | } |
| 277 | |
| 278 | uint32_t MachOLinkingContext::getCPUSubType() const { |
| 279 | return cpuSubtypeFromArch(_arch); |
| 280 | } |
| 281 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 282 | bool MachOLinkingContext::is64Bit(Arch arch) { |
| 283 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 284 | if (info->arch == arch) { |
| 285 | return (info->cputype & CPU_ARCH_ABI64); |
| 286 | } |
| 287 | } |
| 288 | // unknown archs are not 64-bit. |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | bool MachOLinkingContext::isHostEndian(Arch arch) { |
| 293 | assert(arch != arch_unknown); |
| 294 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 295 | if (info->arch == arch) { |
| 296 | return (info->littleEndian == llvm::sys::IsLittleEndianHost); |
| 297 | } |
| 298 | } |
| 299 | llvm_unreachable("Unknown arch type"); |
| 300 | } |
| 301 | |
| 302 | bool MachOLinkingContext::isBigEndian(Arch arch) { |
| 303 | assert(arch != arch_unknown); |
| 304 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 305 | if (info->arch == arch) { |
| 306 | return ! info->littleEndian; |
| 307 | } |
| 308 | } |
| 309 | llvm_unreachable("Unknown arch type"); |
| 310 | } |
| 311 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 312 | bool MachOLinkingContext::is64Bit() const { |
| 313 | return is64Bit(_arch); |
| 314 | } |
| 315 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 316 | bool MachOLinkingContext::outputTypeHasEntry() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 317 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 318 | case MH_EXECUTE: |
| 319 | case MH_DYLINKER: |
| 320 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 321 | return true; |
| 322 | default: |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 327 | bool MachOLinkingContext::needsStubsPass() const { |
| 328 | switch (_outputMachOType) { |
| 329 | case MH_EXECUTE: |
| 330 | return !_outputMachOTypeStatic; |
| 331 | case MH_DYLIB: |
| 332 | case MH_BUNDLE: |
| 333 | return true; |
| 334 | default: |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | bool MachOLinkingContext::needsGOTPass() const { |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 340 | // GOT pass not used in -r mode. |
| 341 | if (_outputMachOType == MH_OBJECT) |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 342 | return false; |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 343 | // Only some arches use GOT pass. |
| 344 | switch (_arch) { |
| 345 | case arch_x86_64: |
| 346 | case arch_arm64: |
| 347 | return true; |
| 348 | default: |
| 349 | return false; |
| 350 | } |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Tim Northover | cf78d37 | 2014-09-30 21:29:54 +0000 | [diff] [blame] | 353 | bool MachOLinkingContext::needsCompactUnwindPass() const { |
| 354 | switch (_outputMachOType) { |
| 355 | case MH_EXECUTE: |
| 356 | case MH_DYLIB: |
| 357 | case MH_BUNDLE: |
| 358 | return archHandler().needsCompactUnwind(); |
| 359 | default: |
| 360 | return false; |
| 361 | } |
| 362 | } |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 363 | |
Pete Cooper | 90dbab0 | 2016-01-19 21:54:21 +0000 | [diff] [blame] | 364 | bool MachOLinkingContext::needsObjCPass() const { |
| 365 | // ObjC pass is only needed if any of the inputs were ObjC. |
| 366 | return _objcConstraint != objc_unknown; |
| 367 | } |
| 368 | |
Nick Kledzik | 4121bce | 2014-10-14 01:51:42 +0000 | [diff] [blame] | 369 | bool MachOLinkingContext::needsShimPass() const { |
| 370 | // Shim pass only used in final executables. |
| 371 | if (_outputMachOType == MH_OBJECT) |
| 372 | return false; |
| 373 | // Only 32-bit arm arches use Shim pass. |
| 374 | switch (_arch) { |
| 375 | case arch_armv6: |
| 376 | case arch_armv7: |
| 377 | case arch_armv7s: |
| 378 | return true; |
| 379 | default: |
| 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | |
Lang Hames | 4904703 | 2015-06-23 20:35:31 +0000 | [diff] [blame] | 384 | bool MachOLinkingContext::needsTLVPass() const { |
| 385 | switch (_outputMachOType) { |
| 386 | case MH_BUNDLE: |
| 387 | case MH_EXECUTE: |
| 388 | case MH_DYLIB: |
| 389 | return true; |
| 390 | default: |
| 391 | return false; |
| 392 | } |
| 393 | } |
| 394 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 395 | StringRef MachOLinkingContext::binderSymbolName() const { |
| 396 | return archHandler().stubInfo().binderSymbolName; |
| 397 | } |
| 398 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 399 | bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 400 | uint32_t parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 401 | switch (_os) { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 402 | case OS::macOSX: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 403 | if (parsePackedVersion(mac, parsedVersion)) |
| 404 | return false; |
| 405 | return _osMinVersion >= parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 406 | case OS::iOS: |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 407 | case OS::iOS_simulator: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 408 | if (parsePackedVersion(iOS, parsedVersion)) |
| 409 | return false; |
| 410 | return _osMinVersion >= parsedVersion; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 411 | case OS::unknown: |
Pete Cooper | 3dd478a | 2016-02-04 01:57:59 +0000 | [diff] [blame] | 412 | // If we don't know the target, then assume that we don't meet the min OS. |
| 413 | // This matches the ld64 behaviour |
| 414 | return false; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 415 | } |
Reid Kleckner | 257102e | 2016-02-10 19:28:13 +0000 | [diff] [blame] | 416 | llvm_unreachable("invalid OS enum"); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | bool MachOLinkingContext::addEntryPointLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 420 | if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) { |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 421 | return minOS("10.8", "6.0"); |
| 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | bool MachOLinkingContext::addUnixThreadLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 427 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 428 | case MH_EXECUTE: |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 429 | if (_outputMachOTypeStatic) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 430 | return true; |
| 431 | else |
| 432 | return !minOS("10.8", "6.0"); |
| 433 | break; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 434 | case MH_DYLINKER: |
| 435 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 436 | return true; |
| 437 | default: |
| 438 | return false; |
| 439 | } |
| 440 | } |
| 441 | |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 442 | bool MachOLinkingContext::pathExists(StringRef path) const { |
Nick Kledzik | 94174f7 | 2014-08-15 19:53:41 +0000 | [diff] [blame] | 443 | if (!_testingFileUsage) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 444 | return llvm::sys::fs::exists(path.str()); |
| 445 | |
| 446 | // Otherwise, we're in test mode: only files explicitly provided on the |
| 447 | // command-line exist. |
Rui Ueyama | 57a2953 | 2014-08-06 19:37:35 +0000 | [diff] [blame] | 448 | std::string key = path.str(); |
| 449 | std::replace(key.begin(), key.end(), '\\', '/'); |
| 450 | return _existingPaths.find(key) != _existingPaths.end(); |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 453 | bool MachOLinkingContext::fileExists(StringRef path) const { |
| 454 | bool found = pathExists(path); |
| 455 | // Log search misses. |
| 456 | if (!found) |
| 457 | addInputFileNotFound(path); |
| 458 | |
| 459 | // When testing, file is never opened, so logging is done here. |
| 460 | if (_testingFileUsage && found) |
| 461 | addInputFileDependency(path); |
| 462 | |
| 463 | return found; |
| 464 | } |
| 465 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 466 | void MachOLinkingContext::setSysLibRoots(const StringRefVector &paths) { |
| 467 | _syslibRoots = paths; |
| 468 | } |
| 469 | |
Jean-Daniel Dupas | 23dd15e | 2014-12-18 21:33:38 +0000 | [diff] [blame] | 470 | void MachOLinkingContext::addRpath(StringRef rpath) { |
| 471 | _rpaths.push_back(rpath); |
| 472 | } |
| 473 | |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 474 | void MachOLinkingContext::addModifiedSearchDir(StringRef libPath, |
| 475 | bool isSystemPath) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 476 | bool addedModifiedPath = false; |
| 477 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 478 | // -syslibroot only applies to absolute paths. |
| 479 | if (libPath.startswith("/")) { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 480 | for (auto syslibRoot : _syslibRoots) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 481 | SmallString<256> path(syslibRoot); |
| 482 | llvm::sys::path::append(path, libPath); |
| 483 | if (pathExists(path)) { |
| 484 | _searchDirs.push_back(path.str().copy(_allocator)); |
| 485 | addedModifiedPath = true; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (addedModifiedPath) |
| 491 | return; |
| 492 | |
| 493 | // Finally, if only one -syslibroot is given, system paths which aren't in it |
| 494 | // get suppressed. |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 495 | if (_syslibRoots.size() != 1 || !isSystemPath) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 496 | if (pathExists(libPath)) { |
| 497 | _searchDirs.push_back(libPath); |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 502 | void MachOLinkingContext::addFrameworkSearchDir(StringRef fwPath, |
| 503 | bool isSystemPath) { |
| 504 | bool pathAdded = false; |
| 505 | |
| 506 | // -syslibroot only used with to absolute framework search paths. |
| 507 | if (fwPath.startswith("/")) { |
| 508 | for (auto syslibRoot : _syslibRoots) { |
| 509 | SmallString<256> path(syslibRoot); |
| 510 | llvm::sys::path::append(path, fwPath); |
| 511 | if (pathExists(path)) { |
| 512 | _frameworkDirs.push_back(path.str().copy(_allocator)); |
| 513 | pathAdded = true; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | // If fwPath found in any -syslibroot, then done. |
| 518 | if (pathAdded) |
| 519 | return; |
| 520 | |
| 521 | // If only one -syslibroot, system paths not in that SDK are suppressed. |
| 522 | if (isSystemPath && (_syslibRoots.size() == 1)) |
| 523 | return; |
| 524 | |
| 525 | // Only use raw fwPath if that directory exists. |
| 526 | if (pathExists(fwPath)) |
| 527 | _frameworkDirs.push_back(fwPath); |
| 528 | } |
| 529 | |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 530 | llvm::Optional<StringRef> |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 531 | MachOLinkingContext::searchDirForLibrary(StringRef path, |
| 532 | StringRef libName) const { |
| 533 | SmallString<256> fullPath; |
| 534 | if (libName.endswith(".o")) { |
| 535 | // A request ending in .o is special: just search for the file directly. |
| 536 | fullPath.assign(path); |
| 537 | llvm::sys::path::append(fullPath, libName); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 538 | if (fileExists(fullPath)) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 539 | return fullPath.str().copy(_allocator); |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 540 | return llvm::None; |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Nico Weber | ba5087f | 2020-07-02 00:02:09 -0400 | [diff] [blame] | 543 | // Search for stub library |
| 544 | fullPath.assign(path); |
| 545 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".tbd"); |
| 546 | if (fileExists(fullPath)) |
| 547 | return fullPath.str().copy(_allocator); |
| 548 | |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 549 | // Search for dynamic library |
| 550 | fullPath.assign(path); |
| 551 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib"); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 552 | if (fileExists(fullPath)) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 553 | return fullPath.str().copy(_allocator); |
| 554 | |
| 555 | // If not, try for a static library |
| 556 | fullPath.assign(path); |
| 557 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a"); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 558 | if (fileExists(fullPath)) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 559 | return fullPath.str().copy(_allocator); |
| 560 | |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 561 | return llvm::None; |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 564 | llvm::Optional<StringRef> |
| 565 | MachOLinkingContext::searchLibrary(StringRef libName) const { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 566 | SmallString<256> path; |
| 567 | for (StringRef dir : searchDirs()) { |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 568 | llvm::Optional<StringRef> searchDir = searchDirForLibrary(dir, libName); |
| 569 | if (searchDir) |
| 570 | return searchDir; |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 573 | return llvm::None; |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 576 | llvm::Optional<StringRef> |
| 577 | MachOLinkingContext::findPathForFramework(StringRef fwName) const{ |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 578 | SmallString<256> fullPath; |
| 579 | for (StringRef dir : frameworkDirs()) { |
| 580 | fullPath.assign(dir); |
| 581 | llvm::sys::path::append(fullPath, Twine(fwName) + ".framework", fwName); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 582 | if (fileExists(fullPath)) |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 583 | return fullPath.str().copy(_allocator); |
| 584 | } |
| 585 | |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 586 | return llvm::None; |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Brian Gesiak | b9f7f4b | 2018-06-12 02:34:04 +0000 | [diff] [blame] | 589 | bool MachOLinkingContext::validateImpl() { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 590 | // TODO: if -arch not specified, look at arch of first .o file. |
| 591 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 592 | if (_currentVersion && _outputMachOType != MH_DYLIB) { |
Brian Gesiak | b9f7f4b | 2018-06-12 02:34:04 +0000 | [diff] [blame] | 593 | error("-current_version can only be used with dylibs"); |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 594 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 597 | if (_compatibilityVersion && _outputMachOType != MH_DYLIB) { |
Brian Gesiak | b9f7f4b | 2018-06-12 02:34:04 +0000 | [diff] [blame] | 598 | error("-compatibility_version can only be used with dylibs"); |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 599 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 602 | if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) { |
Brian Gesiak | b9f7f4b | 2018-06-12 02:34:04 +0000 | [diff] [blame] | 603 | error("-mark_dead_strippable_dylib can only be used with dylibs"); |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 604 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 607 | if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) { |
Brian Gesiak | b9f7f4b | 2018-06-12 02:34:04 +0000 | [diff] [blame] | 608 | error("-bundle_loader can only be used with Mach-O bundles"); |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 609 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 612 | // If -exported_symbols_list used, all exported symbols must be defined. |
Eric Christopher | 058ec20 | 2020-06-19 21:50:14 -0700 | [diff] [blame] | 613 | if (_exportMode == ExportMode::exported) { |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 614 | for (const auto &symbol : _exportedSymbols) |
| 615 | addInitialUndefinedSymbol(symbol.getKey()); |
| 616 | } |
| 617 | |
Nick Kledzik | 77afc71 | 2014-08-21 20:25:50 +0000 | [diff] [blame] | 618 | // If -dead_strip, set up initial live symbols. |
| 619 | if (deadStrip()) { |
| 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. |
Eric Christopher | 058ec20 | 2020-06-19 21:50:14 -0700 | [diff] [blame] | 627 | if (_exportMode == ExportMode::exported) { |
Davide Italiano | 7b68b90 | 2015-03-09 06:05:42 +0000 | [diff] [blame] | 628 | setGlobalsAreDeadStripRoots(false); |
Nick Kledzik | 77afc71 | 2014-08-21 20:25:50 +0000 | [diff] [blame] | 629 | for (const auto &symbol : _exportedSymbols) |
| 630 | addDeadStripRoot(symbol.getKey()); |
| 631 | } |
| 632 | } |
| 633 | |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 634 | addOutputFileDependency(outputPath()); |
| 635 | |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 636 | return true; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 639 | void MachOLinkingContext::addPasses(PassManager &pm) { |
Pete Cooper | 90dbab0 | 2016-01-19 21:54:21 +0000 | [diff] [blame] | 640 | // objc pass should be before layout pass. Otherwise test cases may contain |
| 641 | // no atoms which confuses the layout pass. |
| 642 | if (needsObjCPass()) |
| 643 | mach_o::addObjCPass(pm, *this); |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 644 | mach_o::addLayoutPass(pm, *this); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 645 | if (needsStubsPass()) |
| 646 | mach_o::addStubsPass(pm, *this); |
Tim Northover | cf78d37 | 2014-09-30 21:29:54 +0000 | [diff] [blame] | 647 | if (needsCompactUnwindPass()) |
| 648 | mach_o::addCompactUnwindPass(pm, *this); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 649 | if (needsGOTPass()) |
| 650 | mach_o::addGOTPass(pm, *this); |
Lang Hames | 4904703 | 2015-06-23 20:35:31 +0000 | [diff] [blame] | 651 | if (needsTLVPass()) |
| 652 | mach_o::addTLVPass(pm, *this); |
Nick Kledzik | 4121bce | 2014-10-14 01:51:42 +0000 | [diff] [blame] | 653 | if (needsShimPass()) |
| 654 | mach_o::addShimPass(pm, *this); // Shim pass must run after stubs pass. |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 657 | Writer &MachOLinkingContext::writer() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 658 | if (!_writer) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 659 | _writer = createWriterMachO(*this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 660 | return *_writer; |
| 661 | } |
| 662 | |
Greg Fitzgerald | b4eb64e | 2015-01-23 23:26:13 +0000 | [diff] [blame] | 663 | ErrorOr<std::unique_ptr<MemoryBuffer>> |
| 664 | MachOLinkingContext::getMemoryBuffer(StringRef path) { |
| 665 | addInputFileDependency(path); |
| 666 | |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 667 | ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = |
Greg Fitzgerald | b4eb64e | 2015-01-23 23:26:13 +0000 | [diff] [blame] | 668 | MemoryBuffer::getFileOrSTDIN(path); |
| 669 | if (std::error_code ec = mbOrErr.getError()) |
| 670 | return ec; |
| 671 | std::unique_ptr<MemoryBuffer> mb = std::move(mbOrErr.get()); |
| 672 | |
| 673 | // If buffer contains a fat file, find required arch in fat buffer |
| 674 | // and switch buffer to point to just that required slice. |
| 675 | uint32_t offset; |
| 676 | uint32_t size; |
Rafael Espindola | ed48e53 | 2015-04-27 22:48:51 +0000 | [diff] [blame] | 677 | if (sliceFromFatFile(mb->getMemBufferRef(), offset, size)) |
Greg Fitzgerald | b4eb64e | 2015-01-23 23:26:13 +0000 | [diff] [blame] | 678 | return MemoryBuffer::getFileSlice(path, size, offset); |
| 679 | return std::move(mb); |
| 680 | } |
| 681 | |
| 682 | MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) { |
| 683 | ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = getMemoryBuffer(path); |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 684 | if (mbOrErr.getError()) |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 685 | return nullptr; |
| 686 | |
Rafael Espindola | ab5696b | 2015-04-24 18:51:30 +0000 | [diff] [blame] | 687 | ErrorOr<std::unique_ptr<File>> fileOrErr = |
| 688 | registry().loadFile(std::move(mbOrErr.get())); |
| 689 | if (!fileOrErr) |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 690 | return nullptr; |
Rafael Espindola | 773a159 | 2015-04-24 19:01:30 +0000 | [diff] [blame] | 691 | std::unique_ptr<File> &file = fileOrErr.get(); |
| 692 | file->parse(); |
| 693 | MachODylibFile *result = reinterpret_cast<MachODylibFile *>(file.get()); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 694 | // Node object now owned by _indirectDylibs vector. |
Rafael Espindola | 773a159 | 2015-04-24 19:01:30 +0000 | [diff] [blame] | 695 | _indirectDylibs.push_back(std::move(file)); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 696 | return result; |
| 697 | } |
| 698 | |
Nick Kledzik | 22c9073 | 2014-10-01 20:24:30 +0000 | [diff] [blame] | 699 | MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 700 | // See if already loaded. |
| 701 | auto pos = _pathToDylibMap.find(path); |
| 702 | if (pos != _pathToDylibMap.end()) |
| 703 | return pos->second; |
| 704 | |
| 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); |
Pete Cooper | d0a643e | 2016-03-31 01:09:35 +0000 | [diff] [blame] | 711 | if (libPath) |
| 712 | return loadIndirectDylib(libPath.getValue()); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 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); |
| 722 | } |
| 723 | |
| 724 | // Try full path. |
| 725 | if (pathExists(path)) { |
| 726 | return loadIndirectDylib(path); |
| 727 | } |
| 728 | |
| 729 | return nullptr; |
| 730 | } |
| 731 | |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 732 | uint32_t MachOLinkingContext::dylibCurrentVersion(StringRef installName) const { |
| 733 | auto pos = _pathToDylibMap.find(installName); |
| 734 | if (pos != _pathToDylibMap.end()) |
| 735 | return pos->second->currentVersion(); |
| 736 | else |
Pete Cooper | 932bce6 | 2016-08-11 18:41:14 +0000 | [diff] [blame] | 737 | return 0x10000; // 1.0 |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | uint32_t MachOLinkingContext::dylibCompatVersion(StringRef installName) const { |
| 741 | auto pos = _pathToDylibMap.find(installName); |
| 742 | if (pos != _pathToDylibMap.end()) |
| 743 | return pos->second->compatVersion(); |
| 744 | else |
Pete Cooper | 932bce6 | 2016-08-11 18:41:14 +0000 | [diff] [blame] | 745 | return 0x10000; // 1.0 |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Simon Atanasyan | c437888 | 2015-04-06 20:43:35 +0000 | [diff] [blame] | 748 | void MachOLinkingContext::createImplicitFiles( |
Nick Kledzik | 22c9073 | 2014-10-01 20:24:30 +0000 | [diff] [blame] | 749 | std::vector<std::unique_ptr<File> > &result) { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 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); }); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // Let writer add output type specific extras. |
Simon Atanasyan | c437888 | 2015-04-06 20:43:35 +0000 | [diff] [blame] | 762 | writer().createImplicitFiles(result); |
Lang Hames | 5c69200 | 2015-09-28 20:25:14 +0000 | [diff] [blame] | 763 | |
Lang Hames | 9a4c94e | 2015-09-28 20:52:21 +0000 | [diff] [blame] | 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)); |
Lang Hames | 5c69200 | 2015-09-28 20:25:14 +0000 | [diff] [blame] | 768 | _flatNamespaceFile = result.back().get(); |
| 769 | } |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Nick Kledzik | 5172067 | 2014-10-16 19:31:28 +0000 | [diff] [blame] | 772 | void MachOLinkingContext::registerDylib(MachODylibFile *dylib, |
| 773 | bool upward) const { |
Lang Hames | 9bbc365 | 2015-05-13 00:17:08 +0000 | [diff] [blame] | 774 | std::lock_guard<std::mutex> lock(_dylibsMutex); |
Pete Cooper | 46cd113 | 2016-08-11 20:10:14 +0000 | [diff] [blame] | 775 | |
Fangrui Song | 8048fe2 | 2019-03-29 16:21:16 +0000 | [diff] [blame] | 776 | if (!llvm::count(_allDylibs, dylib)) |
Pete Cooper | 46cd113 | 2016-08-11 20:10:14 +0000 | [diff] [blame] | 777 | _allDylibs.push_back(dylib); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 778 | _pathToDylibMap[dylib->installName()] = dylib; |
| 779 | // If path is different than install name, register path too. |
| 780 | if (!dylib->path().equals(dylib->installName())) |
| 781 | _pathToDylibMap[dylib->path()] = dylib; |
Nick Kledzik | 5172067 | 2014-10-16 19:31:28 +0000 | [diff] [blame] | 782 | if (upward) |
| 783 | _upwardDylibs.insert(dylib); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Nick Kledzik | 5172067 | 2014-10-16 19:31:28 +0000 | [diff] [blame] | 786 | bool MachOLinkingContext::isUpwardDylib(StringRef installName) const { |
| 787 | for (MachODylibFile *dylib : _upwardDylibs) { |
| 788 | if (dylib->installName().equals(installName)) |
| 789 | return true; |
| 790 | } |
| 791 | return false; |
| 792 | } |
| 793 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 794 | ArchHandler &MachOLinkingContext::archHandler() const { |
| 795 | if (!_archHandler) |
| 796 | _archHandler = ArchHandler::create(_arch); |
| 797 | return *_archHandler; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 800 | void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect, |
Rui Ueyama | da74d57 | 2015-03-26 02:23:45 +0000 | [diff] [blame] | 801 | uint16_t align) { |
| 802 | SectionAlign entry = { seg, sect, align }; |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 803 | _sectAligns.push_back(entry); |
| 804 | } |
| 805 | |
Lang Hames | b1b67f4 | 2015-10-24 08:20:51 +0000 | [diff] [blame] | 806 | void MachOLinkingContext::addSectCreateSection( |
| 807 | StringRef seg, StringRef sect, |
| 808 | std::unique_ptr<MemoryBuffer> content) { |
| 809 | |
| 810 | if (!_sectCreateFile) { |
Jonas Devlieghere | 6ba7992 | 2019-08-14 22:28:17 +0000 | [diff] [blame] | 811 | auto sectCreateFile = std::make_unique<mach_o::SectCreateFile>(); |
Lang Hames | b1b67f4 | 2015-10-24 08:20:51 +0000 | [diff] [blame] | 812 | _sectCreateFile = sectCreateFile.get(); |
Jonas Devlieghere | 6ba7992 | 2019-08-14 22:28:17 +0000 | [diff] [blame] | 813 | getNodes().push_back(std::make_unique<FileNode>(std::move(sectCreateFile))); |
Lang Hames | b1b67f4 | 2015-10-24 08:20:51 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | assert(_sectCreateFile && "sectcreate file does not exist."); |
| 817 | _sectCreateFile->addSection(seg, sect, std::move(content)); |
| 818 | } |
| 819 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 820 | bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect, |
Rui Ueyama | da74d57 | 2015-03-26 02:23:45 +0000 | [diff] [blame] | 821 | uint16_t &align) const { |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 822 | for (const SectionAlign &entry : _sectAligns) { |
| 823 | if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) { |
Rui Ueyama | da74d57 | 2015-03-26 02:23:45 +0000 | [diff] [blame] | 824 | align = entry.align; |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 825 | return true; |
| 826 | } |
| 827 | } |
| 828 | return false; |
| 829 | } |
| 830 | |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 831 | void MachOLinkingContext::addExportSymbol(StringRef sym) { |
Nick Kledzik | 4183dbc | 2014-10-24 22:28:54 +0000 | [diff] [blame] | 832 | // Support old crufty export lists with bogus entries. |
| 833 | if (sym.endswith(".eh") || sym.startswith(".objc_category_name_")) { |
Rui Ueyama | d0371f4 | 2019-11-19 11:16:59 +0900 | [diff] [blame] | 834 | llvm::errs() << "warning: ignoring " << sym << " in export list\n"; |
Nick Kledzik | 4183dbc | 2014-10-24 22:28:54 +0000 | [diff] [blame] | 835 | return; |
| 836 | } |
| 837 | // Only i386 MacOSX uses old ABI, so don't change those. |
| 838 | if ((_os != OS::macOSX) || (_arch != arch_x86)) { |
Kazuaki Ishizaki | 7ae3d33 | 2020-01-06 10:21:05 -0800 | [diff] [blame] | 839 | // ObjC has two different ABIs. Be nice and allow one export list work for |
Nick Kledzik | 4183dbc | 2014-10-24 22:28:54 +0000 | [diff] [blame] | 840 | // both ABIs by renaming symbols. |
| 841 | if (sym.startswith(".objc_class_name_")) { |
| 842 | std::string abi2className("_OBJC_CLASS_$_"); |
| 843 | abi2className += sym.substr(17); |
| 844 | _exportedSymbols.insert(copy(abi2className)); |
| 845 | std::string abi2metaclassName("_OBJC_METACLASS_$_"); |
| 846 | abi2metaclassName += sym.substr(17); |
| 847 | _exportedSymbols.insert(copy(abi2metaclassName)); |
| 848 | return; |
| 849 | } |
| 850 | } |
| 851 | |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 852 | // FIXME: Support wildcards. |
| 853 | _exportedSymbols.insert(sym); |
| 854 | } |
| 855 | |
| 856 | bool MachOLinkingContext::exportSymbolNamed(StringRef sym) const { |
| 857 | switch (_exportMode) { |
| 858 | case ExportMode::globals: |
| 859 | llvm_unreachable("exportSymbolNamed() should not be called in this mode"); |
| 860 | break; |
Eric Christopher | 058ec20 | 2020-06-19 21:50:14 -0700 | [diff] [blame] | 861 | case ExportMode::exported: |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 862 | return _exportedSymbols.count(sym); |
Eric Christopher | 058ec20 | 2020-06-19 21:50:14 -0700 | [diff] [blame] | 863 | case ExportMode::unexported: |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 864 | return !_exportedSymbols.count(sym); |
| 865 | } |
Yaron Keren | 9682c85 | 2014-09-21 05:07:44 +0000 | [diff] [blame] | 866 | llvm_unreachable("_exportMode unknown enum value"); |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 869 | std::string MachOLinkingContext::demangle(StringRef symbolName) const { |
| 870 | // Only try to demangle symbols if -demangle on command line |
Davide Italiano | 6d86bb2 | 2015-02-18 03:54:21 +0000 | [diff] [blame] | 871 | if (!demangleSymbols()) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 872 | return std::string(symbolName); |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 873 | |
| 874 | // Only try to demangle symbols that look like C++ symbols |
| 875 | if (!symbolName.startswith("__Z")) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 876 | return std::string(symbolName); |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 877 | |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 878 | SmallString<256> symBuff; |
| 879 | StringRef nullTermSym = Twine(symbolName).toNullTerminatedStringRef(symBuff); |
| 880 | // Mach-O has extra leading underscore that needs to be removed. |
| 881 | const char *cstr = nullTermSym.data() + 1; |
| 882 | int status; |
Rafael Espindola | d194213 | 2016-09-06 19:17:14 +0000 | [diff] [blame] | 883 | char *demangled = llvm::itaniumDemangle(cstr, nullptr, nullptr, &status); |
Rui Ueyama | 43155d0 | 2015-10-02 00:36:00 +0000 | [diff] [blame] | 884 | if (demangled) { |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 885 | std::string result(demangled); |
| 886 | // __cxa_demangle() always uses a malloc'ed buffer to return the result. |
| 887 | free(demangled); |
| 888 | return result; |
| 889 | } |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 890 | |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 891 | return std::string(symbolName); |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Davide Italiano | f6c1d2c | 2016-09-12 21:07:26 +0000 | [diff] [blame] | 894 | static void addDependencyInfoHelper(llvm::raw_fd_ostream *DepInfo, |
| 895 | char Opcode, StringRef Path) { |
| 896 | if (!DepInfo) |
| 897 | return; |
| 898 | |
| 899 | *DepInfo << Opcode; |
| 900 | *DepInfo << Path; |
| 901 | *DepInfo << '\0'; |
| 902 | } |
| 903 | |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 904 | std::error_code MachOLinkingContext::createDependencyFile(StringRef path) { |
| 905 | std::error_code ec; |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 906 | _dependencyInfo = std::unique_ptr<llvm::raw_fd_ostream>( |
| 907 | new llvm::raw_fd_ostream(path, ec, llvm::sys::fs::OF_None)); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 908 | if (ec) { |
| 909 | _dependencyInfo.reset(); |
| 910 | return ec; |
| 911 | } |
| 912 | |
Davide Italiano | f6c1d2c | 2016-09-12 21:07:26 +0000 | [diff] [blame] | 913 | addDependencyInfoHelper(_dependencyInfo.get(), 0x00, "lld" /*FIXME*/); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 914 | return std::error_code(); |
| 915 | } |
| 916 | |
| 917 | void MachOLinkingContext::addInputFileDependency(StringRef path) const { |
Davide Italiano | f6c1d2c | 2016-09-12 21:07:26 +0000 | [diff] [blame] | 918 | addDependencyInfoHelper(_dependencyInfo.get(), 0x10, path); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | void MachOLinkingContext::addInputFileNotFound(StringRef path) const { |
Davide Italiano | f6c1d2c | 2016-09-12 21:07:26 +0000 | [diff] [blame] | 922 | addDependencyInfoHelper(_dependencyInfo.get(), 0x11, path); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | void MachOLinkingContext::addOutputFileDependency(StringRef path) const { |
Davide Italiano | f6c1d2c | 2016-09-12 21:07:26 +0000 | [diff] [blame] | 926 | addDependencyInfoHelper(_dependencyInfo.get(), 0x40, path); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 929 | void MachOLinkingContext::appendOrderedSymbol(StringRef symbol, |
| 930 | StringRef filename) { |
| 931 | // To support sorting static functions which may have the same name in |
| 932 | // multiple .o files, _orderFiles maps the symbol name to a vector |
| 933 | // of OrderFileNode each of which can specify a file prefix. |
| 934 | OrderFileNode info; |
| 935 | if (!filename.empty()) |
| 936 | info.fileFilter = copy(filename); |
| 937 | info.order = _orderFileEntries++; |
| 938 | _orderFiles[symbol].push_back(info); |
| 939 | } |
| 940 | |
| 941 | bool |
| 942 | MachOLinkingContext::findOrderOrdinal(const std::vector<OrderFileNode> &nodes, |
| 943 | const DefinedAtom *atom, |
| 944 | unsigned &ordinal) { |
| 945 | const File *objFile = &atom->file(); |
| 946 | assert(objFile); |
| 947 | StringRef objName = objFile->path(); |
| 948 | std::pair<StringRef, StringRef> dirAndLeaf = objName.rsplit('/'); |
| 949 | if (!dirAndLeaf.second.empty()) |
| 950 | objName = dirAndLeaf.second; |
| 951 | for (const OrderFileNode &info : nodes) { |
| 952 | if (info.fileFilter.empty()) { |
| 953 | // Have unprefixed symbol name in order file that matches this atom. |
| 954 | ordinal = info.order; |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 955 | return true; |
| 956 | } |
| 957 | if (info.fileFilter.equals(objName)) { |
| 958 | // Have prefixed symbol name in order file that matches atom's path. |
| 959 | ordinal = info.order; |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 960 | return true; |
| 961 | } |
| 962 | } |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | bool MachOLinkingContext::customAtomOrderer(const DefinedAtom *left, |
| 967 | const DefinedAtom *right, |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 968 | bool &leftBeforeRight) const { |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 969 | // No custom sorting if no order file entries. |
| 970 | if (!_orderFileEntries) |
| 971 | return false; |
| 972 | |
| 973 | // Order files can only order named atoms. |
| 974 | StringRef leftName = left->name(); |
| 975 | StringRef rightName = right->name(); |
| 976 | if (leftName.empty() || rightName.empty()) |
| 977 | return false; |
| 978 | |
| 979 | // If neither is in order file list, no custom sorter. |
| 980 | auto leftPos = _orderFiles.find(leftName); |
| 981 | auto rightPos = _orderFiles.find(rightName); |
| 982 | bool leftIsOrdered = (leftPos != _orderFiles.end()); |
| 983 | bool rightIsOrdered = (rightPos != _orderFiles.end()); |
| 984 | if (!leftIsOrdered && !rightIsOrdered) |
| 985 | return false; |
| 986 | |
| 987 | // There could be multiple symbols with same name but different file prefixes. |
| 988 | unsigned leftOrder; |
| 989 | unsigned rightOrder; |
| 990 | bool foundLeft = |
| 991 | leftIsOrdered && findOrderOrdinal(leftPos->getValue(), left, leftOrder); |
| 992 | bool foundRight = rightIsOrdered && |
| 993 | findOrderOrdinal(rightPos->getValue(), right, rightOrder); |
| 994 | if (!foundLeft && !foundRight) |
| 995 | return false; |
| 996 | |
| 997 | // If only one is in order file list, ordered one goes first. |
| 998 | if (foundLeft != foundRight) |
| 999 | leftBeforeRight = foundLeft; |
| 1000 | else |
| 1001 | leftBeforeRight = (leftOrder < rightOrder); |
| 1002 | |
| 1003 | return true; |
| 1004 | } |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 1005 | |
Rui Ueyama | 6163544 | 2015-01-15 08:31:46 +0000 | [diff] [blame] | 1006 | static bool isLibrary(const std::unique_ptr<Node> &elem) { |
Rui Ueyama | ae1daae | 2015-01-15 08:51:23 +0000 | [diff] [blame] | 1007 | if (FileNode *node = dyn_cast<FileNode>(const_cast<Node *>(elem.get()))) { |
| 1008 | File *file = node->getFile(); |
| 1009 | return isa<SharedLibraryFile>(file) || isa<ArchiveLibraryFile>(file); |
| 1010 | } |
| 1011 | return false; |
Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | // The darwin linker processes input files in two phases. The first phase |
| 1015 | // links in all object (.o) files in command line order. The second phase |
| 1016 | // links in libraries in command line order. |
| 1017 | // In this function we reorder the input files so that all the object files |
| 1018 | // comes before any library file. We also make a group for the library files |
| 1019 | // so that the Resolver will reiterate over the libraries as long as we find |
| 1020 | // new undefines from libraries. |
Denis Protivensky | cd61715 | 2015-03-14 10:34:43 +0000 | [diff] [blame] | 1021 | void MachOLinkingContext::finalizeInputFiles() { |
Rui Ueyama | 883afba | 2015-01-15 08:46:36 +0000 | [diff] [blame] | 1022 | std::vector<std::unique_ptr<Node>> &elements = getNodes(); |
Fangrui Song | 32c0ebe | 2019-04-23 02:42:06 +0000 | [diff] [blame] | 1023 | llvm::stable_sort(elements, [](const std::unique_ptr<Node> &a, |
| 1024 | const std::unique_ptr<Node> &b) { |
| 1025 | return !isLibrary(a) && isLibrary(b); |
| 1026 | }); |
Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 1027 | size_t numLibs = std::count_if(elements.begin(), elements.end(), isLibrary); |
Jonas Devlieghere | 6ba7992 | 2019-08-14 22:28:17 +0000 | [diff] [blame] | 1028 | elements.push_back(std::make_unique<GroupEnd>(numLibs)); |
Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Pete Cooper | 3c40b5b | 2016-03-30 20:56:54 +0000 | [diff] [blame] | 1031 | llvm::Error MachOLinkingContext::handleLoadedFile(File &file) { |
Pete Cooper | 99f3b94 | 2016-01-14 23:25:06 +0000 | [diff] [blame] | 1032 | auto *machoFile = dyn_cast<MachOFile>(&file); |
| 1033 | if (!machoFile) |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 1034 | return llvm::Error::success(); |
Pete Cooper | 99f3b94 | 2016-01-14 23:25:06 +0000 | [diff] [blame] | 1035 | |
| 1036 | // Check that the arch of the context matches that of the file. |
| 1037 | // Also set the arch of the context if it didn't have one. |
| 1038 | if (_arch == arch_unknown) { |
| 1039 | _arch = machoFile->arch(); |
| 1040 | } else if (machoFile->arch() != arch_unknown && machoFile->arch() != _arch) { |
| 1041 | // Archs are different. |
Pete Cooper | 3c40b5b | 2016-03-30 20:56:54 +0000 | [diff] [blame] | 1042 | return llvm::make_error<GenericError>(file.path() + |
Pete Cooper | 99f3b94 | 2016-01-14 23:25:06 +0000 | [diff] [blame] | 1043 | Twine(" cannot be linked due to incompatible architecture")); |
| 1044 | } |
| 1045 | |
| 1046 | // Check that the OS of the context matches that of the file. |
| 1047 | // Also set the OS of the context if it didn't have one. |
| 1048 | if (_os == OS::unknown) { |
| 1049 | _os = machoFile->OS(); |
| 1050 | } else if (machoFile->OS() != OS::unknown && machoFile->OS() != _os) { |
| 1051 | // OSes are different. |
Pete Cooper | 3c40b5b | 2016-03-30 20:56:54 +0000 | [diff] [blame] | 1052 | return llvm::make_error<GenericError>(file.path() + |
Pete Cooper | 99f3b94 | 2016-01-14 23:25:06 +0000 | [diff] [blame] | 1053 | Twine(" cannot be linked due to incompatible operating systems")); |
| 1054 | } |
Pete Cooper | a014ffe | 2016-01-16 00:07:22 +0000 | [diff] [blame] | 1055 | |
Pete Cooper | 0872e46 | 2016-01-19 19:46:41 +0000 | [diff] [blame] | 1056 | // Check that if the objc info exists, that it is compatible with the target |
| 1057 | // OS. |
| 1058 | switch (machoFile->objcConstraint()) { |
| 1059 | case objc_unknown: |
| 1060 | // The file is not compiled with objc, so skip the checks. |
| 1061 | break; |
| 1062 | case objc_gc_only: |
| 1063 | case objc_supports_gc: |
| 1064 | llvm_unreachable("GC support should already have thrown an error"); |
| 1065 | case objc_retainReleaseForSimulator: |
| 1066 | // The file is built with simulator objc, so make sure that the context |
| 1067 | // is also building with simulator support. |
| 1068 | if (_os != OS::iOS_simulator) |
Pete Cooper | 3c40b5b | 2016-03-30 20:56:54 +0000 | [diff] [blame] | 1069 | return llvm::make_error<GenericError>(file.path() + |
Pete Cooper | 0872e46 | 2016-01-19 19:46:41 +0000 | [diff] [blame] | 1070 | Twine(" cannot be linked. It contains ObjC built for the simulator" |
| 1071 | " while we are linking a non-simulator target")); |
| 1072 | assert((_objcConstraint == objc_unknown || |
| 1073 | _objcConstraint == objc_retainReleaseForSimulator) && |
| 1074 | "Must be linking with retain/release for the simulator"); |
| 1075 | _objcConstraint = objc_retainReleaseForSimulator; |
| 1076 | break; |
| 1077 | case objc_retainRelease: |
| 1078 | // The file is built without simulator objc, so make sure that the |
| 1079 | // context is also building without simulator support. |
| 1080 | if (_os == OS::iOS_simulator) |
Pete Cooper | 3c40b5b | 2016-03-30 20:56:54 +0000 | [diff] [blame] | 1081 | return llvm::make_error<GenericError>(file.path() + |
Pete Cooper | 0872e46 | 2016-01-19 19:46:41 +0000 | [diff] [blame] | 1082 | Twine(" cannot be linked. It contains ObjC built for a non-simulator" |
| 1083 | " target while we are linking a simulator target")); |
| 1084 | assert((_objcConstraint == objc_unknown || |
| 1085 | _objcConstraint == objc_retainRelease) && |
| 1086 | "Must be linking with retain/release for a non-simulator target"); |
| 1087 | _objcConstraint = objc_retainRelease; |
| 1088 | break; |
| 1089 | } |
| 1090 | |
Pete Cooper | a014ffe | 2016-01-16 00:07:22 +0000 | [diff] [blame] | 1091 | // Check that the swift version of the context matches that of the file. |
| 1092 | // Also set the swift version of the context if it didn't have one. |
| 1093 | if (!_swiftVersion) { |
| 1094 | _swiftVersion = machoFile->swiftVersion(); |
| 1095 | } else if (machoFile->swiftVersion() && |
| 1096 | machoFile->swiftVersion() != _swiftVersion) { |
| 1097 | // Swift versions are different. |
Pete Cooper | 3c40b5b | 2016-03-30 20:56:54 +0000 | [diff] [blame] | 1098 | return llvm::make_error<GenericError>("different swift versions"); |
Pete Cooper | a014ffe | 2016-01-16 00:07:22 +0000 | [diff] [blame] | 1099 | } |
Pete Cooper | 90dbab0 | 2016-01-19 21:54:21 +0000 | [diff] [blame] | 1100 | |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 1101 | return llvm::Error::success(); |
Pete Cooper | 80c0974 | 2016-01-14 21:53:13 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 1104 | } // end namespace lld |