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 | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 133 | _testingLibResolution(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 { |
| 300 | if (!testingLibResolution()) |
| 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 | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 310 | void MachOLinkingContext::addModifiedSearchDir(StringRef libPath, |
| 311 | bool isSystemPath) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 312 | bool addedModifiedPath = false; |
| 313 | |
| 314 | // Two cases to consider here: |
| 315 | // + If the last -syslibroot is "/", all of them are ignored (don't ask). |
| 316 | // + -syslibroot only applies to absolute paths. |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 317 | if (!_syslibRoots.empty() && _syslibRoots.back() != "/" && |
Rui Ueyama | 57a2953 | 2014-08-06 19:37:35 +0000 | [diff] [blame] | 318 | libPath.startswith("/")) { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 319 | for (auto syslibRoot : _syslibRoots) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 320 | SmallString<256> path(syslibRoot); |
| 321 | llvm::sys::path::append(path, libPath); |
| 322 | if (pathExists(path)) { |
| 323 | _searchDirs.push_back(path.str().copy(_allocator)); |
| 324 | addedModifiedPath = true; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (addedModifiedPath) |
| 330 | return; |
| 331 | |
| 332 | // Finally, if only one -syslibroot is given, system paths which aren't in it |
| 333 | // get suppressed. |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 334 | if (_syslibRoots.size() != 1 || !isSystemPath) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 335 | if (pathExists(libPath)) { |
| 336 | _searchDirs.push_back(libPath); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | ErrorOr<StringRef> |
| 342 | MachOLinkingContext::searchDirForLibrary(StringRef path, |
| 343 | StringRef libName) const { |
| 344 | SmallString<256> fullPath; |
| 345 | if (libName.endswith(".o")) { |
| 346 | // A request ending in .o is special: just search for the file directly. |
| 347 | fullPath.assign(path); |
| 348 | llvm::sys::path::append(fullPath, libName); |
| 349 | if (pathExists(fullPath)) |
| 350 | return fullPath.str().copy(_allocator); |
| 351 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 352 | } |
| 353 | |
| 354 | // Search for dynamic library |
| 355 | fullPath.assign(path); |
| 356 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib"); |
| 357 | if (pathExists(fullPath)) |
| 358 | return fullPath.str().copy(_allocator); |
| 359 | |
| 360 | // If not, try for a static library |
| 361 | fullPath.assign(path); |
| 362 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a"); |
| 363 | if (pathExists(fullPath)) |
| 364 | return fullPath.str().copy(_allocator); |
| 365 | |
| 366 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | |
| 371 | ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const { |
| 372 | SmallString<256> path; |
| 373 | for (StringRef dir : searchDirs()) { |
| 374 | ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName); |
| 375 | if (ec) |
| 376 | return ec; |
| 377 | } |
| 378 | |
| 379 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 380 | } |
| 381 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 382 | bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 383 | // TODO: if -arch not specified, look at arch of first .o file. |
| 384 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 385 | if (_currentVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 386 | diagnostics << "error: -current_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 387 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 390 | if (_compatibilityVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 391 | diagnostics |
| 392 | << "error: -compatibility_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 393 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 396 | if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 397 | diagnostics |
| 398 | << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 399 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 402 | if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 403 | diagnostics |
| 404 | << "error: -bundle_loader can only be used with Mach-O bundles\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 405 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 408 | return true; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 411 | void MachOLinkingContext::addPasses(PassManager &pm) { |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 412 | pm.add(std::unique_ptr<Pass>(new LayoutPass(registry()))); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 413 | if (needsStubsPass()) |
| 414 | mach_o::addStubsPass(pm, *this); |
| 415 | if (needsGOTPass()) |
| 416 | mach_o::addGOTPass(pm, *this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 419 | Writer &MachOLinkingContext::writer() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 420 | if (!_writer) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 421 | _writer = createWriterMachO(*this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 422 | return *_writer; |
| 423 | } |
| 424 | |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 425 | MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) const { |
| 426 | std::unique_ptr<MachOFileNode> node(new MachOFileNode(path, false)); |
| 427 | std::error_code ec = node->parse(*this, llvm::errs()); |
| 428 | if (ec) |
| 429 | return nullptr; |
| 430 | |
| 431 | assert(node->files().size() == 1 && "expected one file in dylib"); |
| 432 | // lld::File object is owned by MachOFileNode object. This method returns |
| 433 | // an unowned pointer to the lld::File object. |
| 434 | MachODylibFile* result = reinterpret_cast<MachODylibFile*>( |
| 435 | node->files().front().get()); |
| 436 | |
| 437 | // Node object now owned by _indirectDylibs vector. |
| 438 | _indirectDylibs.push_back(std::move(node)); |
| 439 | |
| 440 | return result; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) const { |
| 445 | // See if already loaded. |
| 446 | auto pos = _pathToDylibMap.find(path); |
| 447 | if (pos != _pathToDylibMap.end()) |
| 448 | return pos->second; |
| 449 | |
| 450 | // Search -L paths if of the form "libXXX.dylib" |
| 451 | std::pair<StringRef, StringRef> split = path.rsplit('/'); |
| 452 | StringRef leafName = split.second; |
| 453 | if (leafName.startswith("lib") && leafName.endswith(".dylib")) { |
| 454 | // FIXME: Need to enhance searchLibrary() to only look for .dylib |
| 455 | auto libPath = searchLibrary(leafName); |
| 456 | if (!libPath.getError()) { |
| 457 | return loadIndirectDylib(libPath.get()); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // Try full path with sysroot. |
| 462 | for (StringRef sysPath : _syslibRoots) { |
| 463 | SmallString<256> fullPath; |
| 464 | fullPath.assign(sysPath); |
| 465 | llvm::sys::path::append(fullPath, path); |
| 466 | if (pathExists(fullPath)) |
| 467 | return loadIndirectDylib(fullPath); |
| 468 | } |
| 469 | |
| 470 | // Try full path. |
| 471 | if (pathExists(path)) { |
| 472 | return loadIndirectDylib(path); |
| 473 | } |
| 474 | |
| 475 | return nullptr; |
| 476 | } |
| 477 | |
| 478 | bool MachOLinkingContext::createImplicitFiles( |
| 479 | std::vector<std::unique_ptr<File> > &result) const { |
| 480 | // Add indirect dylibs by asking each linked dylib to add its indirects. |
| 481 | // Iterate until no more dylibs get loaded. |
| 482 | size_t dylibCount = 0; |
| 483 | while (dylibCount != _allDylibs.size()) { |
| 484 | dylibCount = _allDylibs.size(); |
| 485 | for (MachODylibFile *dylib : _allDylibs) { |
| 486 | dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* { |
| 487 | return findIndirectDylib(path); }); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Let writer add output type specific extras. |
| 492 | return writer().createImplicitFiles(result); |
| 493 | } |
| 494 | |
| 495 | |
| 496 | void MachOLinkingContext::registerDylib(MachODylibFile *dylib) { |
| 497 | _allDylibs.insert(dylib); |
| 498 | _pathToDylibMap[dylib->installName()] = dylib; |
| 499 | // If path is different than install name, register path too. |
| 500 | if (!dylib->path().equals(dylib->installName())) |
| 501 | _pathToDylibMap[dylib->path()] = dylib; |
| 502 | } |
| 503 | |
| 504 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 505 | ArchHandler &MachOLinkingContext::archHandler() const { |
| 506 | if (!_archHandler) |
| 507 | _archHandler = ArchHandler::create(_arch); |
| 508 | return *_archHandler; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 511 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 512 | void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect, |
| 513 | uint8_t align2) { |
| 514 | SectionAlign entry; |
| 515 | entry.segmentName = seg; |
| 516 | entry.sectionName = sect; |
| 517 | entry.align2 = align2; |
| 518 | _sectAligns.push_back(entry); |
| 519 | } |
| 520 | |
| 521 | bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect, |
| 522 | uint8_t &align2) const { |
| 523 | for (const SectionAlign &entry : _sectAligns) { |
| 524 | if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) { |
| 525 | align2 = entry.align2; |
| 526 | return true; |
| 527 | } |
| 528 | } |
| 529 | return false; |
| 530 | } |
| 531 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 532 | } // end namespace lld |