Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/MachOLinkingContext.cpp ---------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lld/ReaderWriter/MachOLinkingContext.h" |
| 11 | #include "GOTPass.hpp" |
| 12 | #include "StubsPass.hpp" |
| 13 | #include "ReferenceKinds.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 14 | |
| 15 | #include "lld/Core/PassManager.h" |
| 16 | #include "lld/ReaderWriter/Reader.h" |
| 17 | #include "lld/ReaderWriter/Writer.h" |
| 18 | #include "lld/Passes/LayoutPass.h" |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 19 | #include "lld/Passes/RoundTripYAMLPass.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 20 | |
| 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/ADT/Triple.h" |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame^] | 23 | #include "llvm/Support/Errc.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Host.h" |
Nick Kledzik | 473933b | 2013-09-27 22:50:00 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MachO.h" |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame^] | 26 | #include "llvm/Support/Path.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 27 | |
| 28 | using lld::mach_o::KindHandler; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 29 | using namespace llvm::MachO; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 30 | |
| 31 | namespace lld { |
| 32 | |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 33 | bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) { |
| 34 | result = 0; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 35 | |
| 36 | if (str.empty()) |
| 37 | return false; |
| 38 | |
| 39 | SmallVector<StringRef, 3> parts; |
| 40 | llvm::SplitString(str, parts, "."); |
| 41 | |
| 42 | unsigned long long num; |
| 43 | if (llvm::getAsUnsignedInteger(parts[0], 10, num)) |
| 44 | return true; |
| 45 | if (num > 65535) |
| 46 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 47 | result = num << 16; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 48 | |
| 49 | if (parts.size() > 1) { |
| 50 | if (llvm::getAsUnsignedInteger(parts[1], 10, num)) |
| 51 | return true; |
| 52 | if (num > 255) |
| 53 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 54 | result |= (num << 8); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (parts.size() > 2) { |
| 58 | if (llvm::getAsUnsignedInteger(parts[2], 10, num)) |
| 59 | return true; |
| 60 | if (num > 255) |
| 61 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 62 | result |= num; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 68 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 69 | MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = { |
| 70 | { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL }, |
| 71 | { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL }, |
| 72 | { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL }, |
| 73 | { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 }, |
| 74 | { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 }, |
| 75 | { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S }, |
| 76 | { "", arch_unknown,false, 0, 0 } |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | MachOLinkingContext::Arch |
| 80 | MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 81 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 82 | if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype)) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 83 | return info->arch; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 84 | } |
| 85 | return arch_unknown; |
| 86 | } |
| 87 | |
| 88 | MachOLinkingContext::Arch |
| 89 | MachOLinkingContext::archFromName(StringRef archName) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 90 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 91 | if (info->archName.equals(archName)) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 92 | return info->arch; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 93 | } |
| 94 | return arch_unknown; |
| 95 | } |
| 96 | |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 97 | StringRef MachOLinkingContext::nameFromArch(Arch arch) { |
| 98 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 99 | if (info->arch == arch) |
| 100 | return info->archName; |
| 101 | } |
| 102 | return "<unknown>"; |
| 103 | } |
| 104 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 105 | uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) { |
| 106 | assert(arch != arch_unknown); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 107 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 108 | if (info->arch == arch) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 109 | return info->cputype; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 110 | } |
| 111 | llvm_unreachable("Unknown arch type"); |
| 112 | } |
| 113 | |
| 114 | uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) { |
| 115 | assert(arch != arch_unknown); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 116 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 117 | if (info->arch == arch) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 118 | return info->cpusubtype; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 119 | } |
| 120 | llvm_unreachable("Unknown arch type"); |
| 121 | } |
| 122 | |
| 123 | MachOLinkingContext::MachOLinkingContext() |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 124 | : _outputMachOType(MH_EXECUTE), _outputMachOTypeStatic(false), |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 125 | _doNothing(false), _arch(arch_unknown), _os(OS::macOSX), _osMinVersion(0), |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 126 | _pageZeroSize(0), _pageSize(4096), _compatibilityVersion(0), |
Nick Kledzik | 0224e34 | 2014-05-14 21:32:21 +0000 | [diff] [blame] | 127 | _currentVersion(0), _deadStrippableDylib(false), _printAtoms(false), |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame^] | 128 | _testingLibResolution(false), _kindHandler(nullptr) {} |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 129 | |
| 130 | MachOLinkingContext::~MachOLinkingContext() {} |
| 131 | |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 132 | void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os, |
| 133 | uint32_t minOSVersion) { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 134 | _outputMachOType = type; |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 135 | _arch = arch; |
| 136 | _os = os; |
| 137 | _osMinVersion = minOSVersion; |
| 138 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 139 | switch (_outputMachOType) { |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 140 | case llvm::MachO::MH_EXECUTE: |
| 141 | // If targeting newer OS, use _main |
| 142 | if (minOS("10.8", "6.0")) { |
| 143 | _entrySymbolName = "_main"; |
| 144 | } else { |
| 145 | // If targeting older OS, use start (in crt1.o) |
| 146 | _entrySymbolName = "start"; |
| 147 | } |
| 148 | |
| 149 | // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not |
| 150 | // support) and 4KB on 32-bit. |
| 151 | if (is64Bit(_arch)) { |
| 152 | _pageZeroSize = 0x100000000; |
| 153 | } else { |
| 154 | _pageZeroSize = 0x1000; |
| 155 | } |
| 156 | |
| 157 | break; |
| 158 | case llvm::MachO::MH_DYLIB: |
| 159 | _globalsAreDeadStripRoots = true; |
| 160 | break; |
| 161 | case llvm::MachO::MH_BUNDLE: |
| 162 | break; |
| 163 | case llvm::MachO::MH_OBJECT: |
| 164 | _printRemainingUndefines = false; |
| 165 | _allowRemainingUndefines = true; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 171 | uint32_t MachOLinkingContext::getCPUType() const { |
| 172 | return cpuTypeFromArch(_arch); |
| 173 | } |
| 174 | |
| 175 | uint32_t MachOLinkingContext::getCPUSubType() const { |
| 176 | return cpuSubtypeFromArch(_arch); |
| 177 | } |
| 178 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 179 | bool MachOLinkingContext::is64Bit(Arch arch) { |
| 180 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 181 | if (info->arch == arch) { |
| 182 | return (info->cputype & CPU_ARCH_ABI64); |
| 183 | } |
| 184 | } |
| 185 | // unknown archs are not 64-bit. |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | bool MachOLinkingContext::isHostEndian(Arch arch) { |
| 190 | assert(arch != arch_unknown); |
| 191 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 192 | if (info->arch == arch) { |
| 193 | return (info->littleEndian == llvm::sys::IsLittleEndianHost); |
| 194 | } |
| 195 | } |
| 196 | llvm_unreachable("Unknown arch type"); |
| 197 | } |
| 198 | |
| 199 | bool MachOLinkingContext::isBigEndian(Arch arch) { |
| 200 | assert(arch != arch_unknown); |
| 201 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 202 | if (info->arch == arch) { |
| 203 | return ! info->littleEndian; |
| 204 | } |
| 205 | } |
| 206 | llvm_unreachable("Unknown arch type"); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | bool MachOLinkingContext::is64Bit() const { |
| 212 | return is64Bit(_arch); |
| 213 | } |
| 214 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 215 | bool MachOLinkingContext::outputTypeHasEntry() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 216 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 217 | case MH_EXECUTE: |
| 218 | case MH_DYLINKER: |
| 219 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 220 | return true; |
| 221 | default: |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 227 | uint32_t parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 228 | switch (_os) { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 229 | case OS::macOSX: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 230 | if (parsePackedVersion(mac, parsedVersion)) |
| 231 | return false; |
| 232 | return _osMinVersion >= parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 233 | case OS::iOS: |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 234 | case OS::iOS_simulator: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 235 | if (parsePackedVersion(iOS, parsedVersion)) |
| 236 | return false; |
| 237 | return _osMinVersion >= parsedVersion; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 238 | case OS::unknown: |
| 239 | break; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 240 | } |
| 241 | llvm_unreachable("target not configured for iOS or MacOSX"); |
| 242 | } |
| 243 | |
| 244 | bool MachOLinkingContext::addEntryPointLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 245 | if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) { |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 246 | return minOS("10.8", "6.0"); |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | bool MachOLinkingContext::addUnixThreadLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 252 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 253 | case MH_EXECUTE: |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 254 | if (_outputMachOTypeStatic) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 255 | return true; |
| 256 | else |
| 257 | return !minOS("10.8", "6.0"); |
| 258 | break; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 259 | case MH_DYLINKER: |
| 260 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 261 | return true; |
| 262 | default: |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame^] | 267 | bool MachOLinkingContext::pathExists(StringRef path) const { |
| 268 | if (!testingLibResolution()) |
| 269 | return llvm::sys::fs::exists(path.str()); |
| 270 | |
| 271 | // Otherwise, we're in test mode: only files explicitly provided on the |
| 272 | // command-line exist. |
| 273 | return _existingPaths.find(path) != _existingPaths.end(); |
| 274 | } |
| 275 | |
| 276 | void MachOLinkingContext::addModifiedSearchDir( |
| 277 | StringRef libPath, const StringRefVector &syslibRoots, bool isSystemPath) { |
| 278 | bool addedModifiedPath = false; |
| 279 | |
| 280 | // Two cases to consider here: |
| 281 | // + If the last -syslibroot is "/", all of them are ignored (don't ask). |
| 282 | // + -syslibroot only applies to absolute paths. |
| 283 | if (!syslibRoots.empty() && syslibRoots.back() != "/" && |
| 284 | llvm::sys::path::is_absolute(libPath)) { |
| 285 | for (auto syslibRoot : syslibRoots) { |
| 286 | SmallString<256> path(syslibRoot); |
| 287 | llvm::sys::path::append(path, libPath); |
| 288 | if (pathExists(path)) { |
| 289 | _searchDirs.push_back(path.str().copy(_allocator)); |
| 290 | addedModifiedPath = true; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (addedModifiedPath) |
| 296 | return; |
| 297 | |
| 298 | // Finally, if only one -syslibroot is given, system paths which aren't in it |
| 299 | // get suppressed. |
| 300 | if (syslibRoots.size() != 1 || !isSystemPath) { |
| 301 | if (pathExists(libPath)) { |
| 302 | _searchDirs.push_back(libPath); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | ErrorOr<StringRef> |
| 308 | MachOLinkingContext::searchDirForLibrary(StringRef path, |
| 309 | StringRef libName) const { |
| 310 | SmallString<256> fullPath; |
| 311 | if (libName.endswith(".o")) { |
| 312 | // A request ending in .o is special: just search for the file directly. |
| 313 | fullPath.assign(path); |
| 314 | llvm::sys::path::append(fullPath, libName); |
| 315 | if (pathExists(fullPath)) |
| 316 | return fullPath.str().copy(_allocator); |
| 317 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 318 | } |
| 319 | |
| 320 | // Search for dynamic library |
| 321 | fullPath.assign(path); |
| 322 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib"); |
| 323 | if (pathExists(fullPath)) |
| 324 | return fullPath.str().copy(_allocator); |
| 325 | |
| 326 | // If not, try for a static library |
| 327 | fullPath.assign(path); |
| 328 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a"); |
| 329 | if (pathExists(fullPath)) |
| 330 | return fullPath.str().copy(_allocator); |
| 331 | |
| 332 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | |
| 337 | ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const { |
| 338 | SmallString<256> path; |
| 339 | for (StringRef dir : searchDirs()) { |
| 340 | ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName); |
| 341 | if (ec) |
| 342 | return ec; |
| 343 | } |
| 344 | |
| 345 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 346 | } |
| 347 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 348 | bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 349 | // TODO: if -arch not specified, look at arch of first .o file. |
| 350 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 351 | if (_currentVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 352 | diagnostics << "error: -current_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 353 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 356 | if (_compatibilityVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 357 | diagnostics |
| 358 | << "error: -compatibility_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 359 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 362 | if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 363 | diagnostics |
| 364 | << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 365 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 368 | if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 369 | diagnostics |
| 370 | << "error: -bundle_loader can only be used with Mach-O bundles\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 371 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 374 | return true; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 377 | void MachOLinkingContext::addPasses(PassManager &pm) { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 378 | if (outputMachOType() != MH_OBJECT) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 379 | pm.add(std::unique_ptr<Pass>(new mach_o::GOTPass)); |
| 380 | pm.add(std::unique_ptr<Pass>(new mach_o::StubsPass(*this))); |
| 381 | } |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 382 | pm.add(std::unique_ptr<Pass>(new LayoutPass(registry()))); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 385 | Writer &MachOLinkingContext::writer() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 386 | if (!_writer) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 387 | _writer = createWriterMachO(*this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 388 | return *_writer; |
| 389 | } |
| 390 | |
| 391 | KindHandler &MachOLinkingContext::kindHandler() const { |
| 392 | if (!_kindHandler) |
| 393 | _kindHandler = KindHandler::create(_arch); |
| 394 | return *_kindHandler; |
| 395 | } |
| 396 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 397 | |
| 398 | } // end namespace lld |