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" |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 11 | |
| 12 | #include "ArchHandler.h" |
| 13 | #include "MachOPasses.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 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 28 | using lld::mach_o::ArchHandler; |
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), |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 128 | _testingLibResolution(false), _archHandler(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 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 226 | bool MachOLinkingContext::needsStubsPass() const { |
| 227 | switch (_outputMachOType) { |
| 228 | case MH_EXECUTE: |
| 229 | return !_outputMachOTypeStatic; |
| 230 | case MH_DYLIB: |
| 231 | case MH_BUNDLE: |
| 232 | return true; |
| 233 | default: |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | bool MachOLinkingContext::needsGOTPass() const { |
| 239 | // Only x86_64 uses GOT pass but not in -r mode. |
| 240 | if (_arch != arch_x86_64) |
| 241 | return false; |
| 242 | return (_outputMachOType != MH_OBJECT); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | StringRef MachOLinkingContext::binderSymbolName() const { |
| 247 | return archHandler().stubInfo().binderSymbolName; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 253 | bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 254 | uint32_t parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 255 | switch (_os) { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 256 | case OS::macOSX: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 257 | if (parsePackedVersion(mac, parsedVersion)) |
| 258 | return false; |
| 259 | return _osMinVersion >= parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 260 | case OS::iOS: |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 261 | case OS::iOS_simulator: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 262 | if (parsePackedVersion(iOS, parsedVersion)) |
| 263 | return false; |
| 264 | return _osMinVersion >= parsedVersion; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 265 | case OS::unknown: |
| 266 | break; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 267 | } |
| 268 | llvm_unreachable("target not configured for iOS or MacOSX"); |
| 269 | } |
| 270 | |
| 271 | bool MachOLinkingContext::addEntryPointLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 272 | if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) { |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 273 | return minOS("10.8", "6.0"); |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | bool MachOLinkingContext::addUnixThreadLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 279 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 280 | case MH_EXECUTE: |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 281 | if (_outputMachOTypeStatic) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 282 | return true; |
| 283 | else |
| 284 | return !minOS("10.8", "6.0"); |
| 285 | break; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 286 | case MH_DYLINKER: |
| 287 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 288 | return true; |
| 289 | default: |
| 290 | return false; |
| 291 | } |
| 292 | } |
| 293 | |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 294 | bool MachOLinkingContext::pathExists(StringRef path) const { |
| 295 | if (!testingLibResolution()) |
| 296 | return llvm::sys::fs::exists(path.str()); |
| 297 | |
| 298 | // Otherwise, we're in test mode: only files explicitly provided on the |
| 299 | // command-line exist. |
| 300 | return _existingPaths.find(path) != _existingPaths.end(); |
| 301 | } |
| 302 | |
| 303 | void MachOLinkingContext::addModifiedSearchDir( |
| 304 | StringRef libPath, const StringRefVector &syslibRoots, bool isSystemPath) { |
| 305 | bool addedModifiedPath = false; |
| 306 | |
| 307 | // Two cases to consider here: |
| 308 | // + If the last -syslibroot is "/", all of them are ignored (don't ask). |
| 309 | // + -syslibroot only applies to absolute paths. |
| 310 | if (!syslibRoots.empty() && syslibRoots.back() != "/" && |
| 311 | llvm::sys::path::is_absolute(libPath)) { |
| 312 | for (auto syslibRoot : syslibRoots) { |
| 313 | SmallString<256> path(syslibRoot); |
| 314 | llvm::sys::path::append(path, libPath); |
| 315 | if (pathExists(path)) { |
| 316 | _searchDirs.push_back(path.str().copy(_allocator)); |
| 317 | addedModifiedPath = true; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (addedModifiedPath) |
| 323 | return; |
| 324 | |
| 325 | // Finally, if only one -syslibroot is given, system paths which aren't in it |
| 326 | // get suppressed. |
| 327 | if (syslibRoots.size() != 1 || !isSystemPath) { |
| 328 | if (pathExists(libPath)) { |
| 329 | _searchDirs.push_back(libPath); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | ErrorOr<StringRef> |
| 335 | MachOLinkingContext::searchDirForLibrary(StringRef path, |
| 336 | StringRef libName) const { |
| 337 | SmallString<256> fullPath; |
| 338 | if (libName.endswith(".o")) { |
| 339 | // A request ending in .o is special: just search for the file directly. |
| 340 | fullPath.assign(path); |
| 341 | llvm::sys::path::append(fullPath, libName); |
| 342 | if (pathExists(fullPath)) |
| 343 | return fullPath.str().copy(_allocator); |
| 344 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 345 | } |
| 346 | |
| 347 | // Search for dynamic library |
| 348 | fullPath.assign(path); |
| 349 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib"); |
| 350 | if (pathExists(fullPath)) |
| 351 | return fullPath.str().copy(_allocator); |
| 352 | |
| 353 | // If not, try for a static library |
| 354 | fullPath.assign(path); |
| 355 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a"); |
| 356 | if (pathExists(fullPath)) |
| 357 | return fullPath.str().copy(_allocator); |
| 358 | |
| 359 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const { |
| 365 | SmallString<256> path; |
| 366 | for (StringRef dir : searchDirs()) { |
| 367 | ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName); |
| 368 | if (ec) |
| 369 | return ec; |
| 370 | } |
| 371 | |
| 372 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 373 | } |
| 374 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 375 | bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 376 | // TODO: if -arch not specified, look at arch of first .o file. |
| 377 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 378 | if (_currentVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 379 | diagnostics << "error: -current_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 380 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 383 | if (_compatibilityVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 384 | diagnostics |
| 385 | << "error: -compatibility_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 386 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 389 | if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 390 | diagnostics |
| 391 | << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 392 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 395 | if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 396 | diagnostics |
| 397 | << "error: -bundle_loader can only be used with Mach-O bundles\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 398 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 401 | return true; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 404 | void MachOLinkingContext::addPasses(PassManager &pm) { |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 405 | pm.add(std::unique_ptr<Pass>(new LayoutPass(registry()))); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 406 | if (needsStubsPass()) |
| 407 | mach_o::addStubsPass(pm, *this); |
| 408 | if (needsGOTPass()) |
| 409 | mach_o::addGOTPass(pm, *this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 412 | Writer &MachOLinkingContext::writer() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 413 | if (!_writer) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 414 | _writer = createWriterMachO(*this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 415 | return *_writer; |
| 416 | } |
| 417 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 418 | ArchHandler &MachOLinkingContext::archHandler() const { |
| 419 | if (!_archHandler) |
| 420 | _archHandler = ArchHandler::create(_arch); |
| 421 | return *_archHandler; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 424 | |
| 425 | } // end namespace lld |