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" |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 13 | #include "File.h" |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 14 | #include "MachOPasses.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 15 | |
| 16 | #include "lld/Core/PassManager.h" |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 17 | #include "lld/Driver/DarwinInputGraph.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 18 | #include "lld/ReaderWriter/Reader.h" |
| 19 | #include "lld/ReaderWriter/Writer.h" |
| 20 | #include "lld/Passes/LayoutPass.h" |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 21 | #include "lld/Passes/RoundTripYAMLPass.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 22 | |
| 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/Triple.h" |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Errc.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Host.h" |
Nick Kledzik | 473933b | 2013-09-27 22:50:00 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MachO.h" |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Path.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 29 | |
Rui Ueyama | 57a2953 | 2014-08-06 19:37:35 +0000 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 32 | using lld::mach_o::ArchHandler; |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 33 | using lld::mach_o::MachODylibFile; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 34 | using namespace llvm::MachO; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 35 | |
| 36 | namespace lld { |
| 37 | |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 38 | bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) { |
| 39 | result = 0; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 40 | |
| 41 | if (str.empty()) |
| 42 | return false; |
| 43 | |
| 44 | SmallVector<StringRef, 3> parts; |
| 45 | llvm::SplitString(str, parts, "."); |
| 46 | |
| 47 | unsigned long long num; |
| 48 | if (llvm::getAsUnsignedInteger(parts[0], 10, num)) |
| 49 | return true; |
| 50 | if (num > 65535) |
| 51 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 52 | result = num << 16; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 53 | |
| 54 | if (parts.size() > 1) { |
| 55 | if (llvm::getAsUnsignedInteger(parts[1], 10, num)) |
| 56 | return true; |
| 57 | if (num > 255) |
| 58 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 59 | result |= (num << 8); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if (parts.size() > 2) { |
| 63 | if (llvm::getAsUnsignedInteger(parts[2], 10, num)) |
| 64 | return true; |
| 65 | if (num > 255) |
| 66 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 67 | result |= num; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 73 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 74 | MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = { |
| 75 | { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL }, |
| 76 | { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL }, |
| 77 | { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL }, |
| 78 | { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 }, |
| 79 | { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 }, |
| 80 | { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S }, |
| 81 | { "", arch_unknown,false, 0, 0 } |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | MachOLinkingContext::Arch |
| 85 | MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 86 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 87 | if ((info->cputype == cputype) && (info->cpusubtype == cpusubtype)) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 88 | return info->arch; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 89 | } |
| 90 | return arch_unknown; |
| 91 | } |
| 92 | |
| 93 | MachOLinkingContext::Arch |
| 94 | MachOLinkingContext::archFromName(StringRef archName) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 95 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 96 | if (info->archName.equals(archName)) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 97 | return info->arch; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 98 | } |
| 99 | return arch_unknown; |
| 100 | } |
| 101 | |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 102 | StringRef MachOLinkingContext::nameFromArch(Arch arch) { |
| 103 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 104 | if (info->arch == arch) |
| 105 | return info->archName; |
| 106 | } |
| 107 | return "<unknown>"; |
| 108 | } |
| 109 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 110 | uint32_t MachOLinkingContext::cpuTypeFromArch(Arch arch) { |
| 111 | assert(arch != arch_unknown); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 112 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 113 | if (info->arch == arch) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 114 | return info->cputype; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 115 | } |
| 116 | llvm_unreachable("Unknown arch type"); |
| 117 | } |
| 118 | |
| 119 | uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) { |
| 120 | assert(arch != arch_unknown); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 121 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 122 | if (info->arch == arch) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 123 | return info->cpusubtype; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 124 | } |
| 125 | llvm_unreachable("Unknown arch type"); |
| 126 | } |
| 127 | |
| 128 | MachOLinkingContext::MachOLinkingContext() |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 129 | : _outputMachOType(MH_EXECUTE), _outputMachOTypeStatic(false), |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 130 | _doNothing(false), _arch(arch_unknown), _os(OS::macOSX), _osMinVersion(0), |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 131 | _pageZeroSize(0), _pageSize(4096), _compatibilityVersion(0), |
Nick Kledzik | 0224e34 | 2014-05-14 21:32:21 +0000 | [diff] [blame] | 132 | _currentVersion(0), _deadStrippableDylib(false), _printAtoms(false), |
Nick Kledzik | 94174f7 | 2014-08-15 19:53:41 +0000 | [diff] [blame^] | 133 | _testingFileUsage(false), _archHandler(nullptr) {} |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 134 | |
| 135 | MachOLinkingContext::~MachOLinkingContext() {} |
| 136 | |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 137 | void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os, |
| 138 | uint32_t minOSVersion) { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 139 | _outputMachOType = type; |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 140 | _arch = arch; |
| 141 | _os = os; |
| 142 | _osMinVersion = minOSVersion; |
| 143 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 144 | switch (_outputMachOType) { |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 145 | case llvm::MachO::MH_EXECUTE: |
| 146 | // If targeting newer OS, use _main |
| 147 | if (minOS("10.8", "6.0")) { |
| 148 | _entrySymbolName = "_main"; |
| 149 | } else { |
| 150 | // If targeting older OS, use start (in crt1.o) |
| 151 | _entrySymbolName = "start"; |
| 152 | } |
| 153 | |
| 154 | // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not |
| 155 | // support) and 4KB on 32-bit. |
| 156 | if (is64Bit(_arch)) { |
| 157 | _pageZeroSize = 0x100000000; |
| 158 | } else { |
| 159 | _pageZeroSize = 0x1000; |
| 160 | } |
| 161 | |
| 162 | break; |
| 163 | case llvm::MachO::MH_DYLIB: |
| 164 | _globalsAreDeadStripRoots = true; |
| 165 | break; |
| 166 | case llvm::MachO::MH_BUNDLE: |
| 167 | break; |
| 168 | case llvm::MachO::MH_OBJECT: |
| 169 | _printRemainingUndefines = false; |
| 170 | _allowRemainingUndefines = true; |
| 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 176 | uint32_t MachOLinkingContext::getCPUType() const { |
| 177 | return cpuTypeFromArch(_arch); |
| 178 | } |
| 179 | |
| 180 | uint32_t MachOLinkingContext::getCPUSubType() const { |
| 181 | return cpuSubtypeFromArch(_arch); |
| 182 | } |
| 183 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 184 | bool MachOLinkingContext::is64Bit(Arch arch) { |
| 185 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 186 | if (info->arch == arch) { |
| 187 | return (info->cputype & CPU_ARCH_ABI64); |
| 188 | } |
| 189 | } |
| 190 | // unknown archs are not 64-bit. |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | bool MachOLinkingContext::isHostEndian(Arch arch) { |
| 195 | assert(arch != arch_unknown); |
| 196 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 197 | if (info->arch == arch) { |
| 198 | return (info->littleEndian == llvm::sys::IsLittleEndianHost); |
| 199 | } |
| 200 | } |
| 201 | llvm_unreachable("Unknown arch type"); |
| 202 | } |
| 203 | |
| 204 | bool MachOLinkingContext::isBigEndian(Arch arch) { |
| 205 | assert(arch != arch_unknown); |
| 206 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 207 | if (info->arch == arch) { |
| 208 | return ! info->littleEndian; |
| 209 | } |
| 210 | } |
| 211 | llvm_unreachable("Unknown arch type"); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | bool MachOLinkingContext::is64Bit() const { |
| 217 | return is64Bit(_arch); |
| 218 | } |
| 219 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 220 | bool MachOLinkingContext::outputTypeHasEntry() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 221 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 222 | case MH_EXECUTE: |
| 223 | case MH_DYLINKER: |
| 224 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 225 | return true; |
| 226 | default: |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 231 | bool MachOLinkingContext::needsStubsPass() const { |
| 232 | switch (_outputMachOType) { |
| 233 | case MH_EXECUTE: |
| 234 | return !_outputMachOTypeStatic; |
| 235 | case MH_DYLIB: |
| 236 | case MH_BUNDLE: |
| 237 | return true; |
| 238 | default: |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | bool MachOLinkingContext::needsGOTPass() const { |
| 244 | // Only x86_64 uses GOT pass but not in -r mode. |
| 245 | if (_arch != arch_x86_64) |
| 246 | return false; |
| 247 | return (_outputMachOType != MH_OBJECT); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | StringRef MachOLinkingContext::binderSymbolName() const { |
| 252 | return archHandler().stubInfo().binderSymbolName; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 258 | bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 259 | uint32_t parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 260 | switch (_os) { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 261 | case OS::macOSX: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 262 | if (parsePackedVersion(mac, parsedVersion)) |
| 263 | return false; |
| 264 | return _osMinVersion >= parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 265 | case OS::iOS: |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 266 | case OS::iOS_simulator: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 267 | if (parsePackedVersion(iOS, parsedVersion)) |
| 268 | return false; |
| 269 | return _osMinVersion >= parsedVersion; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 270 | case OS::unknown: |
| 271 | break; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 272 | } |
| 273 | llvm_unreachable("target not configured for iOS or MacOSX"); |
| 274 | } |
| 275 | |
| 276 | bool MachOLinkingContext::addEntryPointLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 277 | if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) { |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 278 | return minOS("10.8", "6.0"); |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | bool MachOLinkingContext::addUnixThreadLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 284 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 285 | case MH_EXECUTE: |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 286 | if (_outputMachOTypeStatic) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 287 | return true; |
| 288 | else |
| 289 | return !minOS("10.8", "6.0"); |
| 290 | break; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 291 | case MH_DYLINKER: |
| 292 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 293 | return true; |
| 294 | default: |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 299 | bool MachOLinkingContext::pathExists(StringRef path) const { |
Nick Kledzik | 94174f7 | 2014-08-15 19:53:41 +0000 | [diff] [blame^] | 300 | if (!_testingFileUsage) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 301 | return llvm::sys::fs::exists(path.str()); |
| 302 | |
| 303 | // Otherwise, we're in test mode: only files explicitly provided on the |
| 304 | // command-line exist. |
Rui Ueyama | 57a2953 | 2014-08-06 19:37:35 +0000 | [diff] [blame] | 305 | std::string key = path.str(); |
| 306 | std::replace(key.begin(), key.end(), '\\', '/'); |
| 307 | return _existingPaths.find(key) != _existingPaths.end(); |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 310 | void MachOLinkingContext::setSysLibRoots(const StringRefVector &paths) { |
| 311 | _syslibRoots = paths; |
| 312 | } |
| 313 | |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 314 | void MachOLinkingContext::addModifiedSearchDir(StringRef libPath, |
| 315 | bool isSystemPath) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 316 | bool addedModifiedPath = false; |
| 317 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 318 | // -syslibroot only applies to absolute paths. |
| 319 | if (libPath.startswith("/")) { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 320 | for (auto syslibRoot : _syslibRoots) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 321 | SmallString<256> path(syslibRoot); |
| 322 | llvm::sys::path::append(path, libPath); |
| 323 | if (pathExists(path)) { |
| 324 | _searchDirs.push_back(path.str().copy(_allocator)); |
| 325 | addedModifiedPath = true; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if (addedModifiedPath) |
| 331 | return; |
| 332 | |
| 333 | // Finally, if only one -syslibroot is given, system paths which aren't in it |
| 334 | // get suppressed. |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 335 | if (_syslibRoots.size() != 1 || !isSystemPath) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 336 | if (pathExists(libPath)) { |
| 337 | _searchDirs.push_back(libPath); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 342 | void MachOLinkingContext::addFrameworkSearchDir(StringRef fwPath, |
| 343 | bool isSystemPath) { |
| 344 | bool pathAdded = false; |
| 345 | |
| 346 | // -syslibroot only used with to absolute framework search paths. |
| 347 | if (fwPath.startswith("/")) { |
| 348 | for (auto syslibRoot : _syslibRoots) { |
| 349 | SmallString<256> path(syslibRoot); |
| 350 | llvm::sys::path::append(path, fwPath); |
| 351 | if (pathExists(path)) { |
| 352 | _frameworkDirs.push_back(path.str().copy(_allocator)); |
| 353 | pathAdded = true; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | // If fwPath found in any -syslibroot, then done. |
| 358 | if (pathAdded) |
| 359 | return; |
| 360 | |
| 361 | // If only one -syslibroot, system paths not in that SDK are suppressed. |
| 362 | if (isSystemPath && (_syslibRoots.size() == 1)) |
| 363 | return; |
| 364 | |
| 365 | // Only use raw fwPath if that directory exists. |
| 366 | if (pathExists(fwPath)) |
| 367 | _frameworkDirs.push_back(fwPath); |
| 368 | } |
| 369 | |
| 370 | |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 371 | ErrorOr<StringRef> |
| 372 | MachOLinkingContext::searchDirForLibrary(StringRef path, |
| 373 | StringRef libName) const { |
| 374 | SmallString<256> fullPath; |
| 375 | if (libName.endswith(".o")) { |
| 376 | // A request ending in .o is special: just search for the file directly. |
| 377 | fullPath.assign(path); |
| 378 | llvm::sys::path::append(fullPath, libName); |
| 379 | if (pathExists(fullPath)) |
| 380 | return fullPath.str().copy(_allocator); |
| 381 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 382 | } |
| 383 | |
| 384 | // Search for dynamic library |
| 385 | fullPath.assign(path); |
| 386 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib"); |
| 387 | if (pathExists(fullPath)) |
| 388 | return fullPath.str().copy(_allocator); |
| 389 | |
| 390 | // If not, try for a static library |
| 391 | fullPath.assign(path); |
| 392 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a"); |
| 393 | if (pathExists(fullPath)) |
| 394 | return fullPath.str().copy(_allocator); |
| 395 | |
| 396 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | |
| 401 | ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const { |
| 402 | SmallString<256> path; |
| 403 | for (StringRef dir : searchDirs()) { |
| 404 | ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName); |
| 405 | if (ec) |
| 406 | return ec; |
| 407 | } |
| 408 | |
| 409 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 410 | } |
| 411 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 412 | |
| 413 | ErrorOr<StringRef> MachOLinkingContext::findPathForFramework(StringRef fwName) const{ |
| 414 | SmallString<256> fullPath; |
| 415 | for (StringRef dir : frameworkDirs()) { |
| 416 | fullPath.assign(dir); |
| 417 | llvm::sys::path::append(fullPath, Twine(fwName) + ".framework", fwName); |
| 418 | if (pathExists(fullPath)) |
| 419 | return fullPath.str().copy(_allocator); |
| 420 | } |
| 421 | |
| 422 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 423 | } |
| 424 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 425 | bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 426 | // TODO: if -arch not specified, look at arch of first .o file. |
| 427 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 428 | if (_currentVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 429 | diagnostics << "error: -current_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 430 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 433 | if (_compatibilityVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 434 | diagnostics |
| 435 | << "error: -compatibility_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 436 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 439 | if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 440 | diagnostics |
| 441 | << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 442 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 445 | if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 446 | diagnostics |
| 447 | << "error: -bundle_loader can only be used with Mach-O bundles\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 448 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 451 | return true; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 454 | void MachOLinkingContext::addPasses(PassManager &pm) { |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 455 | pm.add(std::unique_ptr<Pass>(new LayoutPass(registry()))); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 456 | if (needsStubsPass()) |
| 457 | mach_o::addStubsPass(pm, *this); |
| 458 | if (needsGOTPass()) |
| 459 | mach_o::addGOTPass(pm, *this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 462 | Writer &MachOLinkingContext::writer() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 463 | if (!_writer) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 464 | _writer = createWriterMachO(*this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 465 | return *_writer; |
| 466 | } |
| 467 | |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 468 | MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) const { |
| 469 | std::unique_ptr<MachOFileNode> node(new MachOFileNode(path, false)); |
| 470 | std::error_code ec = node->parse(*this, llvm::errs()); |
| 471 | if (ec) |
| 472 | return nullptr; |
| 473 | |
| 474 | assert(node->files().size() == 1 && "expected one file in dylib"); |
| 475 | // lld::File object is owned by MachOFileNode object. This method returns |
| 476 | // an unowned pointer to the lld::File object. |
| 477 | MachODylibFile* result = reinterpret_cast<MachODylibFile*>( |
| 478 | node->files().front().get()); |
| 479 | |
| 480 | // Node object now owned by _indirectDylibs vector. |
| 481 | _indirectDylibs.push_back(std::move(node)); |
| 482 | |
| 483 | return result; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) const { |
| 488 | // See if already loaded. |
| 489 | auto pos = _pathToDylibMap.find(path); |
| 490 | if (pos != _pathToDylibMap.end()) |
| 491 | return pos->second; |
| 492 | |
| 493 | // Search -L paths if of the form "libXXX.dylib" |
| 494 | std::pair<StringRef, StringRef> split = path.rsplit('/'); |
| 495 | StringRef leafName = split.second; |
| 496 | if (leafName.startswith("lib") && leafName.endswith(".dylib")) { |
| 497 | // FIXME: Need to enhance searchLibrary() to only look for .dylib |
| 498 | auto libPath = searchLibrary(leafName); |
| 499 | if (!libPath.getError()) { |
| 500 | return loadIndirectDylib(libPath.get()); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Try full path with sysroot. |
| 505 | for (StringRef sysPath : _syslibRoots) { |
| 506 | SmallString<256> fullPath; |
| 507 | fullPath.assign(sysPath); |
| 508 | llvm::sys::path::append(fullPath, path); |
| 509 | if (pathExists(fullPath)) |
| 510 | return loadIndirectDylib(fullPath); |
| 511 | } |
| 512 | |
| 513 | // Try full path. |
| 514 | if (pathExists(path)) { |
| 515 | return loadIndirectDylib(path); |
| 516 | } |
| 517 | |
| 518 | return nullptr; |
| 519 | } |
| 520 | |
| 521 | bool MachOLinkingContext::createImplicitFiles( |
| 522 | std::vector<std::unique_ptr<File> > &result) const { |
| 523 | // Add indirect dylibs by asking each linked dylib to add its indirects. |
| 524 | // Iterate until no more dylibs get loaded. |
| 525 | size_t dylibCount = 0; |
| 526 | while (dylibCount != _allDylibs.size()) { |
| 527 | dylibCount = _allDylibs.size(); |
| 528 | for (MachODylibFile *dylib : _allDylibs) { |
| 529 | dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* { |
| 530 | return findIndirectDylib(path); }); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // Let writer add output type specific extras. |
| 535 | return writer().createImplicitFiles(result); |
| 536 | } |
| 537 | |
| 538 | |
| 539 | void MachOLinkingContext::registerDylib(MachODylibFile *dylib) { |
| 540 | _allDylibs.insert(dylib); |
| 541 | _pathToDylibMap[dylib->installName()] = dylib; |
| 542 | // If path is different than install name, register path too. |
| 543 | if (!dylib->path().equals(dylib->installName())) |
| 544 | _pathToDylibMap[dylib->path()] = dylib; |
| 545 | } |
| 546 | |
| 547 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 548 | ArchHandler &MachOLinkingContext::archHandler() const { |
| 549 | if (!_archHandler) |
| 550 | _archHandler = ArchHandler::create(_arch); |
| 551 | return *_archHandler; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 554 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 555 | void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect, |
| 556 | uint8_t align2) { |
| 557 | SectionAlign entry; |
| 558 | entry.segmentName = seg; |
| 559 | entry.sectionName = sect; |
| 560 | entry.align2 = align2; |
| 561 | _sectAligns.push_back(entry); |
| 562 | } |
| 563 | |
| 564 | bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect, |
| 565 | uint8_t &align2) const { |
| 566 | for (const SectionAlign &entry : _sectAligns) { |
| 567 | if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) { |
| 568 | align2 = entry.align2; |
| 569 | return true; |
| 570 | } |
| 571 | } |
| 572 | return false; |
| 573 | } |
| 574 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 575 | } // end namespace lld |