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