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 | #include "ArchHandler.h" |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 12 | #include "File.h" |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 13 | #include "MachONormalizedFile.h" |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 14 | #include "MachOPasses.h" |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 15 | #include "lld/Core/ArchiveLibraryFile.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 16 | #include "lld/Core/PassManager.h" |
Greg Fitzgerald | 4b6a7e3 | 2015-01-21 22:54:56 +0000 | [diff] [blame] | 17 | #include "lld/Core/Reader.h" |
| 18 | #include "lld/Core/Writer.h" |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 19 | #include "lld/Driver/Driver.h" |
Benjamin Kramer | 06a42af | 2015-03-02 00:48:06 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/ADT/Triple.h" |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 23 | #include "llvm/Config/config.h" |
Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 89642a7 | 2015-01-14 11:26:52 +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 | 57a2953 | 2014-08-06 19:37:35 +0000 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | |
Rui Ueyama | fccf7ef | 2014-10-27 07:44:40 +0000 | [diff] [blame] | 31 | #if defined(HAVE_CXXABI_H) |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 32 | #include <cxxabi.h> |
| 33 | #endif |
| 34 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 35 | using lld::mach_o::ArchHandler; |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 36 | using lld::mach_o::MachODylibFile; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 37 | using namespace llvm::MachO; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 38 | |
| 39 | namespace lld { |
| 40 | |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 41 | bool MachOLinkingContext::parsePackedVersion(StringRef str, uint32_t &result) { |
| 42 | result = 0; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 43 | |
| 44 | if (str.empty()) |
| 45 | return false; |
| 46 | |
| 47 | SmallVector<StringRef, 3> parts; |
| 48 | llvm::SplitString(str, parts, "."); |
| 49 | |
| 50 | unsigned long long num; |
| 51 | if (llvm::getAsUnsignedInteger(parts[0], 10, num)) |
| 52 | return true; |
| 53 | if (num > 65535) |
| 54 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 55 | result = num << 16; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 56 | |
| 57 | if (parts.size() > 1) { |
| 58 | if (llvm::getAsUnsignedInteger(parts[1], 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 << 8); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (parts.size() > 2) { |
| 66 | if (llvm::getAsUnsignedInteger(parts[2], 10, num)) |
| 67 | return true; |
| 68 | if (num > 255) |
| 69 | return true; |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 70 | result |= num; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 76 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 77 | MachOLinkingContext::ArchInfo MachOLinkingContext::_s_archInfos[] = { |
| 78 | { "x86_64", arch_x86_64, true, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL }, |
| 79 | { "i386", arch_x86, true, CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL }, |
| 80 | { "ppc", arch_ppc, false, CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL }, |
| 81 | { "armv6", arch_armv6, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 }, |
| 82 | { "armv7", arch_armv7, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 }, |
| 83 | { "armv7s", arch_armv7s, true, CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S }, |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 84 | { "arm64", arch_arm64, true, CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL }, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 85 | { "", arch_unknown,false, 0, 0 } |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | MachOLinkingContext::Arch |
| 89 | MachOLinkingContext::archFromCpuType(uint32_t cputype, uint32_t cpusubtype) { |
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->cputype == cputype) && (info->cpusubtype == cpusubtype)) |
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 | |
| 97 | MachOLinkingContext::Arch |
| 98 | MachOLinkingContext::archFromName(StringRef archName) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 99 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 100 | if (info->archName.equals(archName)) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 101 | return info->arch; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 102 | } |
| 103 | return arch_unknown; |
| 104 | } |
| 105 | |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 106 | StringRef MachOLinkingContext::nameFromArch(Arch arch) { |
| 107 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 108 | if (info->arch == arch) |
| 109 | return info->archName; |
| 110 | } |
| 111 | return "<unknown>"; |
| 112 | } |
| 113 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 114 | uint32_t MachOLinkingContext::cpuTypeFromArch(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->cputype; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 119 | } |
| 120 | llvm_unreachable("Unknown arch type"); |
| 121 | } |
| 122 | |
| 123 | uint32_t MachOLinkingContext::cpuSubtypeFromArch(Arch arch) { |
| 124 | assert(arch != arch_unknown); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 125 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 126 | if (info->arch == arch) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 127 | return info->cpusubtype; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 128 | } |
| 129 | llvm_unreachable("Unknown arch type"); |
| 130 | } |
| 131 | |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 132 | bool MachOLinkingContext::isThinObjectFile(StringRef path, Arch &arch) { |
| 133 | return mach_o::normalized::isThinObjectFile(path, arch); |
| 134 | } |
| 135 | |
Rafael Espindola | ed48e53 | 2015-04-27 22:48:51 +0000 | [diff] [blame] | 136 | bool MachOLinkingContext::sliceFromFatFile(MemoryBufferRef mb, uint32_t &offset, |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 137 | uint32_t &size) { |
| 138 | return mach_o::normalized::sliceFromFatFile(mb, _arch, offset, size); |
| 139 | } |
| 140 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 141 | MachOLinkingContext::MachOLinkingContext() |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 142 | : _outputMachOType(MH_EXECUTE), _outputMachOTypeStatic(false), |
Tim Northover | af3075b | 2014-09-10 10:39:57 +0000 | [diff] [blame] | 143 | _doNothing(false), _pie(false), _arch(arch_unknown), _os(OS::macOSX), |
| 144 | _osMinVersion(0), _pageZeroSize(0), _pageSize(4096), _baseAddress(0), |
Lang Hames | ff4b13c | 2015-05-22 00:25:34 +0000 | [diff] [blame] | 145 | _stackSize(0), _compatibilityVersion(0), _currentVersion(0), |
Lang Hames | 65a64c9 | 2015-05-20 22:10:50 +0000 | [diff] [blame] | 146 | _deadStrippableDylib(false), _printAtoms(false), _testingFileUsage(false), |
| 147 | _keepPrivateExterns(false), _demangle(false), _archHandler(nullptr), |
Nick Kledzik | 8f75da0 | 2014-11-06 03:03:42 +0000 | [diff] [blame] | 148 | _exportMode(ExportMode::globals), |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 149 | _debugInfoMode(DebugInfoMode::addDebugMap), _orderFileEntries(0) {} |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 150 | |
| 151 | MachOLinkingContext::~MachOLinkingContext() {} |
| 152 | |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 153 | void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os, |
| 154 | uint32_t minOSVersion) { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 155 | _outputMachOType = type; |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 156 | _arch = arch; |
| 157 | _os = os; |
| 158 | _osMinVersion = minOSVersion; |
| 159 | |
Nick Kledzik | cb2018f | 2014-10-09 01:01:16 +0000 | [diff] [blame] | 160 | // If min OS not specified on command line, use reasonable defaults. |
| 161 | if (minOSVersion == 0) { |
| 162 | switch (_arch) { |
| 163 | case arch_x86_64: |
| 164 | case arch_x86: |
| 165 | parsePackedVersion("10.8", _osMinVersion); |
| 166 | _os = MachOLinkingContext::OS::macOSX; |
| 167 | break; |
| 168 | case arch_armv6: |
| 169 | case arch_armv7: |
| 170 | case arch_armv7s: |
| 171 | case arch_arm64: |
| 172 | parsePackedVersion("7.0", _osMinVersion); |
| 173 | _os = MachOLinkingContext::OS::iOS; |
| 174 | break; |
| 175 | default: |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 180 | switch (_outputMachOType) { |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 181 | case llvm::MachO::MH_EXECUTE: |
| 182 | // If targeting newer OS, use _main |
| 183 | if (minOS("10.8", "6.0")) { |
| 184 | _entrySymbolName = "_main"; |
| 185 | } else { |
| 186 | // If targeting older OS, use start (in crt1.o) |
| 187 | _entrySymbolName = "start"; |
| 188 | } |
| 189 | |
| 190 | // __PAGEZERO defaults to 4GB on 64-bit (except for PP64 which lld does not |
| 191 | // support) and 4KB on 32-bit. |
| 192 | if (is64Bit(_arch)) { |
| 193 | _pageZeroSize = 0x100000000; |
| 194 | } else { |
| 195 | _pageZeroSize = 0x1000; |
| 196 | } |
| 197 | |
Nick Kledzik | b7035ae | 2014-09-09 00:17:52 +0000 | [diff] [blame] | 198 | // Make PIE by default when targetting newer OSs. |
| 199 | switch (os) { |
| 200 | case OS::macOSX: |
| 201 | if (minOSVersion >= 0x000A0700) // MacOSX 10.7 |
| 202 | _pie = true; |
| 203 | break; |
| 204 | case OS::iOS: |
| 205 | if (minOSVersion >= 0x00040300) // iOS 4.3 |
| 206 | _pie = true; |
| 207 | break; |
| 208 | case OS::iOS_simulator: |
| 209 | _pie = true; |
| 210 | break; |
| 211 | case OS::unknown: |
| 212 | break; |
| 213 | } |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 214 | break; |
| 215 | case llvm::MachO::MH_DYLIB: |
Davide Italiano | 7b68b90 | 2015-03-09 06:05:42 +0000 | [diff] [blame] | 216 | setGlobalsAreDeadStripRoots(true); |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 217 | break; |
| 218 | case llvm::MachO::MH_BUNDLE: |
| 219 | break; |
| 220 | case llvm::MachO::MH_OBJECT: |
| 221 | _printRemainingUndefines = false; |
| 222 | _allowRemainingUndefines = true; |
| 223 | default: |
| 224 | break; |
| 225 | } |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 226 | |
| 227 | // Set default segment page sizes based on arch. |
| 228 | if (arch == arch_arm64) |
| 229 | _pageSize = 4*4096; |
Nick Kledzik | 6960b07 | 2013-12-21 01:47:17 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 232 | uint32_t MachOLinkingContext::getCPUType() const { |
| 233 | return cpuTypeFromArch(_arch); |
| 234 | } |
| 235 | |
| 236 | uint32_t MachOLinkingContext::getCPUSubType() const { |
| 237 | return cpuSubtypeFromArch(_arch); |
| 238 | } |
| 239 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 240 | bool MachOLinkingContext::is64Bit(Arch arch) { |
| 241 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 242 | if (info->arch == arch) { |
| 243 | return (info->cputype & CPU_ARCH_ABI64); |
| 244 | } |
| 245 | } |
| 246 | // unknown archs are not 64-bit. |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | bool MachOLinkingContext::isHostEndian(Arch arch) { |
| 251 | assert(arch != arch_unknown); |
| 252 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 253 | if (info->arch == arch) { |
| 254 | return (info->littleEndian == llvm::sys::IsLittleEndianHost); |
| 255 | } |
| 256 | } |
| 257 | llvm_unreachable("Unknown arch type"); |
| 258 | } |
| 259 | |
| 260 | bool MachOLinkingContext::isBigEndian(Arch arch) { |
| 261 | assert(arch != arch_unknown); |
| 262 | for (ArchInfo *info = _s_archInfos; !info->archName.empty(); ++info) { |
| 263 | if (info->arch == arch) { |
| 264 | return ! info->littleEndian; |
| 265 | } |
| 266 | } |
| 267 | llvm_unreachable("Unknown arch type"); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | bool MachOLinkingContext::is64Bit() const { |
| 273 | return is64Bit(_arch); |
| 274 | } |
| 275 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 276 | bool MachOLinkingContext::outputTypeHasEntry() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 277 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 278 | case MH_EXECUTE: |
| 279 | case MH_DYLINKER: |
| 280 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 281 | return true; |
| 282 | default: |
| 283 | return false; |
| 284 | } |
| 285 | } |
| 286 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 287 | bool MachOLinkingContext::needsStubsPass() const { |
| 288 | switch (_outputMachOType) { |
| 289 | case MH_EXECUTE: |
| 290 | return !_outputMachOTypeStatic; |
| 291 | case MH_DYLIB: |
| 292 | case MH_BUNDLE: |
| 293 | return true; |
| 294 | default: |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | bool MachOLinkingContext::needsGOTPass() const { |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 300 | // GOT pass not used in -r mode. |
| 301 | if (_outputMachOType == MH_OBJECT) |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 302 | return false; |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 303 | // Only some arches use GOT pass. |
| 304 | switch (_arch) { |
| 305 | case arch_x86_64: |
| 306 | case arch_arm64: |
| 307 | return true; |
| 308 | default: |
| 309 | return false; |
| 310 | } |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Tim Northover | cf78d37 | 2014-09-30 21:29:54 +0000 | [diff] [blame] | 313 | bool MachOLinkingContext::needsCompactUnwindPass() const { |
| 314 | switch (_outputMachOType) { |
| 315 | case MH_EXECUTE: |
| 316 | case MH_DYLIB: |
| 317 | case MH_BUNDLE: |
| 318 | return archHandler().needsCompactUnwind(); |
| 319 | default: |
| 320 | return false; |
| 321 | } |
| 322 | } |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 323 | |
Nick Kledzik | 4121bce | 2014-10-14 01:51:42 +0000 | [diff] [blame] | 324 | bool MachOLinkingContext::needsShimPass() const { |
| 325 | // Shim pass only used in final executables. |
| 326 | if (_outputMachOType == MH_OBJECT) |
| 327 | return false; |
| 328 | // Only 32-bit arm arches use Shim pass. |
| 329 | switch (_arch) { |
| 330 | case arch_armv6: |
| 331 | case arch_armv7: |
| 332 | case arch_armv7s: |
| 333 | return true; |
| 334 | default: |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | |
Lang Hames | 4904703 | 2015-06-23 20:35:31 +0000 | [diff] [blame^] | 339 | bool MachOLinkingContext::needsTLVPass() const { |
| 340 | switch (_outputMachOType) { |
| 341 | case MH_BUNDLE: |
| 342 | case MH_EXECUTE: |
| 343 | case MH_DYLIB: |
| 344 | return true; |
| 345 | default: |
| 346 | return false; |
| 347 | } |
| 348 | } |
| 349 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 350 | StringRef MachOLinkingContext::binderSymbolName() const { |
| 351 | return archHandler().stubInfo().binderSymbolName; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 357 | bool MachOLinkingContext::minOS(StringRef mac, StringRef iOS) const { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 358 | uint32_t parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 359 | switch (_os) { |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 360 | case OS::macOSX: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 361 | if (parsePackedVersion(mac, parsedVersion)) |
| 362 | return false; |
| 363 | return _osMinVersion >= parsedVersion; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 364 | case OS::iOS: |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 365 | case OS::iOS_simulator: |
Nick Kledzik | e850d9d | 2013-09-10 23:46:57 +0000 | [diff] [blame] | 366 | if (parsePackedVersion(iOS, parsedVersion)) |
| 367 | return false; |
| 368 | return _osMinVersion >= parsedVersion; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 369 | case OS::unknown: |
| 370 | break; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 371 | } |
| 372 | llvm_unreachable("target not configured for iOS or MacOSX"); |
| 373 | } |
| 374 | |
| 375 | bool MachOLinkingContext::addEntryPointLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 376 | if ((_outputMachOType == MH_EXECUTE) && !_outputMachOTypeStatic) { |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 377 | return minOS("10.8", "6.0"); |
| 378 | } |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | bool MachOLinkingContext::addUnixThreadLoadCommand() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 383 | switch (_outputMachOType) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 384 | case MH_EXECUTE: |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 385 | if (_outputMachOTypeStatic) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 386 | return true; |
| 387 | else |
| 388 | return !minOS("10.8", "6.0"); |
| 389 | break; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 390 | case MH_DYLINKER: |
| 391 | case MH_PRELOAD: |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 392 | return true; |
| 393 | default: |
| 394 | return false; |
| 395 | } |
| 396 | } |
| 397 | |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 398 | bool MachOLinkingContext::pathExists(StringRef path) const { |
Nick Kledzik | 94174f7 | 2014-08-15 19:53:41 +0000 | [diff] [blame] | 399 | if (!_testingFileUsage) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 400 | return llvm::sys::fs::exists(path.str()); |
| 401 | |
| 402 | // Otherwise, we're in test mode: only files explicitly provided on the |
| 403 | // command-line exist. |
Rui Ueyama | 57a2953 | 2014-08-06 19:37:35 +0000 | [diff] [blame] | 404 | std::string key = path.str(); |
| 405 | std::replace(key.begin(), key.end(), '\\', '/'); |
| 406 | return _existingPaths.find(key) != _existingPaths.end(); |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 409 | bool MachOLinkingContext::fileExists(StringRef path) const { |
| 410 | bool found = pathExists(path); |
| 411 | // Log search misses. |
| 412 | if (!found) |
| 413 | addInputFileNotFound(path); |
| 414 | |
| 415 | // When testing, file is never opened, so logging is done here. |
| 416 | if (_testingFileUsage && found) |
| 417 | addInputFileDependency(path); |
| 418 | |
| 419 | return found; |
| 420 | } |
| 421 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 422 | void MachOLinkingContext::setSysLibRoots(const StringRefVector &paths) { |
| 423 | _syslibRoots = paths; |
| 424 | } |
| 425 | |
Jean-Daniel Dupas | 23dd15e | 2014-12-18 21:33:38 +0000 | [diff] [blame] | 426 | void MachOLinkingContext::addRpath(StringRef rpath) { |
| 427 | _rpaths.push_back(rpath); |
| 428 | } |
| 429 | |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 430 | void MachOLinkingContext::addModifiedSearchDir(StringRef libPath, |
| 431 | bool isSystemPath) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 432 | bool addedModifiedPath = false; |
| 433 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 434 | // -syslibroot only applies to absolute paths. |
| 435 | if (libPath.startswith("/")) { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 436 | for (auto syslibRoot : _syslibRoots) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 437 | SmallString<256> path(syslibRoot); |
| 438 | llvm::sys::path::append(path, libPath); |
| 439 | if (pathExists(path)) { |
| 440 | _searchDirs.push_back(path.str().copy(_allocator)); |
| 441 | addedModifiedPath = true; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (addedModifiedPath) |
| 447 | return; |
| 448 | |
| 449 | // Finally, if only one -syslibroot is given, system paths which aren't in it |
| 450 | // get suppressed. |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 451 | if (_syslibRoots.size() != 1 || !isSystemPath) { |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 452 | if (pathExists(libPath)) { |
| 453 | _searchDirs.push_back(libPath); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 458 | void MachOLinkingContext::addFrameworkSearchDir(StringRef fwPath, |
| 459 | bool isSystemPath) { |
| 460 | bool pathAdded = false; |
| 461 | |
| 462 | // -syslibroot only used with to absolute framework search paths. |
| 463 | if (fwPath.startswith("/")) { |
| 464 | for (auto syslibRoot : _syslibRoots) { |
| 465 | SmallString<256> path(syslibRoot); |
| 466 | llvm::sys::path::append(path, fwPath); |
| 467 | if (pathExists(path)) { |
| 468 | _frameworkDirs.push_back(path.str().copy(_allocator)); |
| 469 | pathAdded = true; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | // If fwPath found in any -syslibroot, then done. |
| 474 | if (pathAdded) |
| 475 | return; |
| 476 | |
| 477 | // If only one -syslibroot, system paths not in that SDK are suppressed. |
| 478 | if (isSystemPath && (_syslibRoots.size() == 1)) |
| 479 | return; |
| 480 | |
| 481 | // Only use raw fwPath if that directory exists. |
| 482 | if (pathExists(fwPath)) |
| 483 | _frameworkDirs.push_back(fwPath); |
| 484 | } |
| 485 | |
| 486 | |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 487 | ErrorOr<StringRef> |
| 488 | MachOLinkingContext::searchDirForLibrary(StringRef path, |
| 489 | StringRef libName) const { |
| 490 | SmallString<256> fullPath; |
| 491 | if (libName.endswith(".o")) { |
| 492 | // A request ending in .o is special: just search for the file directly. |
| 493 | fullPath.assign(path); |
| 494 | llvm::sys::path::append(fullPath, libName); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 495 | if (fileExists(fullPath)) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 496 | return fullPath.str().copy(_allocator); |
| 497 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 498 | } |
| 499 | |
| 500 | // Search for dynamic library |
| 501 | fullPath.assign(path); |
| 502 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".dylib"); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 503 | if (fileExists(fullPath)) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 504 | return fullPath.str().copy(_allocator); |
| 505 | |
| 506 | // If not, try for a static library |
| 507 | fullPath.assign(path); |
| 508 | llvm::sys::path::append(fullPath, Twine("lib") + libName + ".a"); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 509 | if (fileExists(fullPath)) |
Tim Northover | 77d8220 | 2014-07-10 11:21:06 +0000 | [diff] [blame] | 510 | return fullPath.str().copy(_allocator); |
| 511 | |
| 512 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | |
| 517 | ErrorOr<StringRef> MachOLinkingContext::searchLibrary(StringRef libName) const { |
| 518 | SmallString<256> path; |
| 519 | for (StringRef dir : searchDirs()) { |
| 520 | ErrorOr<StringRef> ec = searchDirForLibrary(dir, libName); |
| 521 | if (ec) |
| 522 | return ec; |
| 523 | } |
| 524 | |
| 525 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 526 | } |
| 527 | |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 528 | |
| 529 | ErrorOr<StringRef> MachOLinkingContext::findPathForFramework(StringRef fwName) const{ |
| 530 | SmallString<256> fullPath; |
| 531 | for (StringRef dir : frameworkDirs()) { |
| 532 | fullPath.assign(dir); |
| 533 | llvm::sys::path::append(fullPath, Twine(fwName) + ".framework", fwName); |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 534 | if (fileExists(fullPath)) |
Nick Kledzik | 2d835da | 2014-08-14 22:20:41 +0000 | [diff] [blame] | 535 | return fullPath.str().copy(_allocator); |
| 536 | } |
| 537 | |
| 538 | return make_error_code(llvm::errc::no_such_file_or_directory); |
| 539 | } |
| 540 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 541 | bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 542 | // TODO: if -arch not specified, look at arch of first .o file. |
| 543 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 544 | if (_currentVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 545 | diagnostics << "error: -current_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 546 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 549 | if (_compatibilityVersion && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 550 | diagnostics |
| 551 | << "error: -compatibility_version can only be used with dylibs\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 552 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 555 | if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 556 | diagnostics |
| 557 | << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 558 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 561 | if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) { |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 562 | diagnostics |
| 563 | << "error: -bundle_loader can only be used with Mach-O bundles\n"; |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 564 | return false; |
Nick Kledzik | e773e32 | 2013-09-10 23:55:14 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 567 | // If -exported_symbols_list used, all exported symbols must be defined. |
| 568 | if (_exportMode == ExportMode::whiteList) { |
| 569 | for (const auto &symbol : _exportedSymbols) |
| 570 | addInitialUndefinedSymbol(symbol.getKey()); |
| 571 | } |
| 572 | |
Nick Kledzik | 77afc71 | 2014-08-21 20:25:50 +0000 | [diff] [blame] | 573 | // If -dead_strip, set up initial live symbols. |
| 574 | if (deadStrip()) { |
| 575 | // Entry point is live. |
| 576 | if (outputTypeHasEntry()) |
| 577 | addDeadStripRoot(entrySymbolName()); |
| 578 | // Lazy binding helper is live. |
| 579 | if (needsStubsPass()) |
| 580 | addDeadStripRoot(binderSymbolName()); |
| 581 | // If using -exported_symbols_list, make all exported symbols live. |
| 582 | if (_exportMode == ExportMode::whiteList) { |
Davide Italiano | 7b68b90 | 2015-03-09 06:05:42 +0000 | [diff] [blame] | 583 | setGlobalsAreDeadStripRoots(false); |
Nick Kledzik | 77afc71 | 2014-08-21 20:25:50 +0000 | [diff] [blame] | 584 | for (const auto &symbol : _exportedSymbols) |
| 585 | addDeadStripRoot(symbol.getKey()); |
| 586 | } |
| 587 | } |
| 588 | |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 589 | addOutputFileDependency(outputPath()); |
| 590 | |
Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 591 | return true; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 594 | void MachOLinkingContext::addPasses(PassManager &pm) { |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 595 | mach_o::addLayoutPass(pm, *this); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 596 | if (needsStubsPass()) |
| 597 | mach_o::addStubsPass(pm, *this); |
Tim Northover | cf78d37 | 2014-09-30 21:29:54 +0000 | [diff] [blame] | 598 | if (needsCompactUnwindPass()) |
| 599 | mach_o::addCompactUnwindPass(pm, *this); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 600 | if (needsGOTPass()) |
| 601 | mach_o::addGOTPass(pm, *this); |
Lang Hames | 4904703 | 2015-06-23 20:35:31 +0000 | [diff] [blame^] | 602 | if (needsTLVPass()) |
| 603 | mach_o::addTLVPass(pm, *this); |
Nick Kledzik | 4121bce | 2014-10-14 01:51:42 +0000 | [diff] [blame] | 604 | if (needsShimPass()) |
| 605 | mach_o::addShimPass(pm, *this); // Shim pass must run after stubs pass. |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 608 | Writer &MachOLinkingContext::writer() const { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 609 | if (!_writer) |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 610 | _writer = createWriterMachO(*this); |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 611 | return *_writer; |
| 612 | } |
| 613 | |
Greg Fitzgerald | b4eb64e | 2015-01-23 23:26:13 +0000 | [diff] [blame] | 614 | ErrorOr<std::unique_ptr<MemoryBuffer>> |
| 615 | MachOLinkingContext::getMemoryBuffer(StringRef path) { |
| 616 | addInputFileDependency(path); |
| 617 | |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 618 | ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = |
Greg Fitzgerald | b4eb64e | 2015-01-23 23:26:13 +0000 | [diff] [blame] | 619 | MemoryBuffer::getFileOrSTDIN(path); |
| 620 | if (std::error_code ec = mbOrErr.getError()) |
| 621 | return ec; |
| 622 | std::unique_ptr<MemoryBuffer> mb = std::move(mbOrErr.get()); |
| 623 | |
| 624 | // If buffer contains a fat file, find required arch in fat buffer |
| 625 | // and switch buffer to point to just that required slice. |
| 626 | uint32_t offset; |
| 627 | uint32_t size; |
Rafael Espindola | ed48e53 | 2015-04-27 22:48:51 +0000 | [diff] [blame] | 628 | if (sliceFromFatFile(mb->getMemBufferRef(), offset, size)) |
Greg Fitzgerald | b4eb64e | 2015-01-23 23:26:13 +0000 | [diff] [blame] | 629 | return MemoryBuffer::getFileSlice(path, size, offset); |
| 630 | return std::move(mb); |
| 631 | } |
| 632 | |
| 633 | MachODylibFile* MachOLinkingContext::loadIndirectDylib(StringRef path) { |
| 634 | ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = getMemoryBuffer(path); |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 635 | if (mbOrErr.getError()) |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 636 | return nullptr; |
| 637 | |
Rafael Espindola | ab5696b | 2015-04-24 18:51:30 +0000 | [diff] [blame] | 638 | ErrorOr<std::unique_ptr<File>> fileOrErr = |
| 639 | registry().loadFile(std::move(mbOrErr.get())); |
| 640 | if (!fileOrErr) |
Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 641 | return nullptr; |
Rafael Espindola | 773a159 | 2015-04-24 19:01:30 +0000 | [diff] [blame] | 642 | std::unique_ptr<File> &file = fileOrErr.get(); |
| 643 | file->parse(); |
| 644 | MachODylibFile *result = reinterpret_cast<MachODylibFile *>(file.get()); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 645 | // Node object now owned by _indirectDylibs vector. |
Rafael Espindola | 773a159 | 2015-04-24 19:01:30 +0000 | [diff] [blame] | 646 | _indirectDylibs.push_back(std::move(file)); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 647 | return result; |
| 648 | } |
| 649 | |
| 650 | |
Nick Kledzik | 22c9073 | 2014-10-01 20:24:30 +0000 | [diff] [blame] | 651 | MachODylibFile* MachOLinkingContext::findIndirectDylib(StringRef path) { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 652 | // See if already loaded. |
| 653 | auto pos = _pathToDylibMap.find(path); |
| 654 | if (pos != _pathToDylibMap.end()) |
| 655 | return pos->second; |
| 656 | |
| 657 | // Search -L paths if of the form "libXXX.dylib" |
| 658 | std::pair<StringRef, StringRef> split = path.rsplit('/'); |
| 659 | StringRef leafName = split.second; |
| 660 | if (leafName.startswith("lib") && leafName.endswith(".dylib")) { |
| 661 | // FIXME: Need to enhance searchLibrary() to only look for .dylib |
| 662 | auto libPath = searchLibrary(leafName); |
| 663 | if (!libPath.getError()) { |
| 664 | return loadIndirectDylib(libPath.get()); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | // Try full path with sysroot. |
| 669 | for (StringRef sysPath : _syslibRoots) { |
| 670 | SmallString<256> fullPath; |
| 671 | fullPath.assign(sysPath); |
| 672 | llvm::sys::path::append(fullPath, path); |
| 673 | if (pathExists(fullPath)) |
| 674 | return loadIndirectDylib(fullPath); |
| 675 | } |
| 676 | |
| 677 | // Try full path. |
| 678 | if (pathExists(path)) { |
| 679 | return loadIndirectDylib(path); |
| 680 | } |
| 681 | |
| 682 | return nullptr; |
| 683 | } |
| 684 | |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 685 | uint32_t MachOLinkingContext::dylibCurrentVersion(StringRef installName) const { |
| 686 | auto pos = _pathToDylibMap.find(installName); |
| 687 | if (pos != _pathToDylibMap.end()) |
| 688 | return pos->second->currentVersion(); |
| 689 | else |
| 690 | return 0x1000; // 1.0 |
| 691 | } |
| 692 | |
| 693 | uint32_t MachOLinkingContext::dylibCompatVersion(StringRef installName) const { |
| 694 | auto pos = _pathToDylibMap.find(installName); |
| 695 | if (pos != _pathToDylibMap.end()) |
| 696 | return pos->second->compatVersion(); |
| 697 | else |
| 698 | return 0x1000; // 1.0 |
| 699 | } |
| 700 | |
Simon Atanasyan | c437888 | 2015-04-06 20:43:35 +0000 | [diff] [blame] | 701 | void MachOLinkingContext::createImplicitFiles( |
Nick Kledzik | 22c9073 | 2014-10-01 20:24:30 +0000 | [diff] [blame] | 702 | std::vector<std::unique_ptr<File> > &result) { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 703 | // Add indirect dylibs by asking each linked dylib to add its indirects. |
| 704 | // Iterate until no more dylibs get loaded. |
| 705 | size_t dylibCount = 0; |
| 706 | while (dylibCount != _allDylibs.size()) { |
| 707 | dylibCount = _allDylibs.size(); |
| 708 | for (MachODylibFile *dylib : _allDylibs) { |
| 709 | dylib->loadReExportedDylibs([this] (StringRef path) -> MachODylibFile* { |
| 710 | return findIndirectDylib(path); }); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // Let writer add output type specific extras. |
Simon Atanasyan | c437888 | 2015-04-06 20:43:35 +0000 | [diff] [blame] | 715 | writer().createImplicitFiles(result); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | |
Nick Kledzik | 5172067 | 2014-10-16 19:31:28 +0000 | [diff] [blame] | 719 | void MachOLinkingContext::registerDylib(MachODylibFile *dylib, |
| 720 | bool upward) const { |
Lang Hames | 9bbc365 | 2015-05-13 00:17:08 +0000 | [diff] [blame] | 721 | std::lock_guard<std::mutex> lock(_dylibsMutex); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 722 | _allDylibs.insert(dylib); |
| 723 | _pathToDylibMap[dylib->installName()] = dylib; |
| 724 | // If path is different than install name, register path too. |
| 725 | if (!dylib->path().equals(dylib->installName())) |
| 726 | _pathToDylibMap[dylib->path()] = dylib; |
Nick Kledzik | 5172067 | 2014-10-16 19:31:28 +0000 | [diff] [blame] | 727 | if (upward) |
| 728 | _upwardDylibs.insert(dylib); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
Nick Kledzik | 5172067 | 2014-10-16 19:31:28 +0000 | [diff] [blame] | 732 | bool MachOLinkingContext::isUpwardDylib(StringRef installName) const { |
| 733 | for (MachODylibFile *dylib : _upwardDylibs) { |
| 734 | if (dylib->installName().equals(installName)) |
| 735 | return true; |
| 736 | } |
| 737 | return false; |
| 738 | } |
| 739 | |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 740 | ArchHandler &MachOLinkingContext::archHandler() const { |
| 741 | if (!_archHandler) |
| 742 | _archHandler = ArchHandler::create(_arch); |
| 743 | return *_archHandler; |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 746 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 747 | void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect, |
Rui Ueyama | da74d57 | 2015-03-26 02:23:45 +0000 | [diff] [blame] | 748 | uint16_t align) { |
| 749 | SectionAlign entry = { seg, sect, align }; |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 750 | _sectAligns.push_back(entry); |
| 751 | } |
| 752 | |
| 753 | bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect, |
Rui Ueyama | da74d57 | 2015-03-26 02:23:45 +0000 | [diff] [blame] | 754 | uint16_t &align) const { |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 755 | for (const SectionAlign &entry : _sectAligns) { |
| 756 | if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) { |
Rui Ueyama | da74d57 | 2015-03-26 02:23:45 +0000 | [diff] [blame] | 757 | align = entry.align; |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 758 | return true; |
| 759 | } |
| 760 | } |
| 761 | return false; |
| 762 | } |
| 763 | |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 764 | |
| 765 | void MachOLinkingContext::addExportSymbol(StringRef sym) { |
Nick Kledzik | 4183dbc | 2014-10-24 22:28:54 +0000 | [diff] [blame] | 766 | // Support old crufty export lists with bogus entries. |
| 767 | if (sym.endswith(".eh") || sym.startswith(".objc_category_name_")) { |
| 768 | llvm::errs() << "warning: ignoring " << sym << " in export list\n"; |
| 769 | return; |
| 770 | } |
| 771 | // Only i386 MacOSX uses old ABI, so don't change those. |
| 772 | if ((_os != OS::macOSX) || (_arch != arch_x86)) { |
| 773 | // ObjC has two differnent ABIs. Be nice and allow one export list work for |
| 774 | // both ABIs by renaming symbols. |
| 775 | if (sym.startswith(".objc_class_name_")) { |
| 776 | std::string abi2className("_OBJC_CLASS_$_"); |
| 777 | abi2className += sym.substr(17); |
| 778 | _exportedSymbols.insert(copy(abi2className)); |
| 779 | std::string abi2metaclassName("_OBJC_METACLASS_$_"); |
| 780 | abi2metaclassName += sym.substr(17); |
| 781 | _exportedSymbols.insert(copy(abi2metaclassName)); |
| 782 | return; |
| 783 | } |
| 784 | } |
| 785 | |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 786 | // FIXME: Support wildcards. |
| 787 | _exportedSymbols.insert(sym); |
| 788 | } |
| 789 | |
| 790 | bool MachOLinkingContext::exportSymbolNamed(StringRef sym) const { |
| 791 | switch (_exportMode) { |
| 792 | case ExportMode::globals: |
| 793 | llvm_unreachable("exportSymbolNamed() should not be called in this mode"); |
| 794 | break; |
| 795 | case ExportMode::whiteList: |
| 796 | return _exportedSymbols.count(sym); |
| 797 | case ExportMode::blackList: |
| 798 | return !_exportedSymbols.count(sym); |
| 799 | } |
Yaron Keren | 9682c85 | 2014-09-21 05:07:44 +0000 | [diff] [blame] | 800 | llvm_unreachable("_exportMode unknown enum value"); |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 803 | std::string MachOLinkingContext::demangle(StringRef symbolName) const { |
| 804 | // Only try to demangle symbols if -demangle on command line |
Davide Italiano | 6d86bb2 | 2015-02-18 03:54:21 +0000 | [diff] [blame] | 805 | if (!demangleSymbols()) |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 806 | return symbolName; |
| 807 | |
| 808 | // Only try to demangle symbols that look like C++ symbols |
| 809 | if (!symbolName.startswith("__Z")) |
| 810 | return symbolName; |
| 811 | |
Rui Ueyama | fccf7ef | 2014-10-27 07:44:40 +0000 | [diff] [blame] | 812 | #if defined(HAVE_CXXABI_H) |
Nick Kledzik | be43d7e | 2014-09-30 23:15:39 +0000 | [diff] [blame] | 813 | SmallString<256> symBuff; |
| 814 | StringRef nullTermSym = Twine(symbolName).toNullTerminatedStringRef(symBuff); |
| 815 | // Mach-O has extra leading underscore that needs to be removed. |
| 816 | const char *cstr = nullTermSym.data() + 1; |
| 817 | int status; |
| 818 | char *demangled = abi::__cxa_demangle(cstr, nullptr, nullptr, &status); |
| 819 | if (demangled != NULL) { |
| 820 | std::string result(demangled); |
| 821 | // __cxa_demangle() always uses a malloc'ed buffer to return the result. |
| 822 | free(demangled); |
| 823 | return result; |
| 824 | } |
| 825 | #endif |
| 826 | |
| 827 | return symbolName; |
| 828 | } |
| 829 | |
Nick Kledzik | 09d00bb | 2014-10-04 00:16:13 +0000 | [diff] [blame] | 830 | std::error_code MachOLinkingContext::createDependencyFile(StringRef path) { |
| 831 | std::error_code ec; |
| 832 | _dependencyInfo = std::unique_ptr<llvm::raw_fd_ostream>(new |
| 833 | llvm::raw_fd_ostream(path, ec, llvm::sys::fs::F_None)); |
| 834 | if (ec) { |
| 835 | _dependencyInfo.reset(); |
| 836 | return ec; |
| 837 | } |
| 838 | |
| 839 | char linkerVersionOpcode = 0x00; |
| 840 | *_dependencyInfo << linkerVersionOpcode; |
| 841 | *_dependencyInfo << "lld"; // FIXME |
| 842 | *_dependencyInfo << '\0'; |
| 843 | |
| 844 | return std::error_code(); |
| 845 | } |
| 846 | |
| 847 | void MachOLinkingContext::addInputFileDependency(StringRef path) const { |
| 848 | if (!_dependencyInfo) |
| 849 | return; |
| 850 | |
| 851 | char inputFileOpcode = 0x10; |
| 852 | *_dependencyInfo << inputFileOpcode; |
| 853 | *_dependencyInfo << path; |
| 854 | *_dependencyInfo << '\0'; |
| 855 | } |
| 856 | |
| 857 | void MachOLinkingContext::addInputFileNotFound(StringRef path) const { |
| 858 | if (!_dependencyInfo) |
| 859 | return; |
| 860 | |
| 861 | char inputFileOpcode = 0x11; |
| 862 | *_dependencyInfo << inputFileOpcode; |
| 863 | *_dependencyInfo << path; |
| 864 | *_dependencyInfo << '\0'; |
| 865 | } |
| 866 | |
| 867 | void MachOLinkingContext::addOutputFileDependency(StringRef path) const { |
| 868 | if (!_dependencyInfo) |
| 869 | return; |
| 870 | |
| 871 | char outputFileOpcode = 0x40; |
| 872 | *_dependencyInfo << outputFileOpcode; |
| 873 | *_dependencyInfo << path; |
| 874 | *_dependencyInfo << '\0'; |
| 875 | } |
| 876 | |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 877 | void MachOLinkingContext::appendOrderedSymbol(StringRef symbol, |
| 878 | StringRef filename) { |
| 879 | // To support sorting static functions which may have the same name in |
| 880 | // multiple .o files, _orderFiles maps the symbol name to a vector |
| 881 | // of OrderFileNode each of which can specify a file prefix. |
| 882 | OrderFileNode info; |
| 883 | if (!filename.empty()) |
| 884 | info.fileFilter = copy(filename); |
| 885 | info.order = _orderFileEntries++; |
| 886 | _orderFiles[symbol].push_back(info); |
| 887 | } |
| 888 | |
| 889 | bool |
| 890 | MachOLinkingContext::findOrderOrdinal(const std::vector<OrderFileNode> &nodes, |
| 891 | const DefinedAtom *atom, |
| 892 | unsigned &ordinal) { |
| 893 | const File *objFile = &atom->file(); |
| 894 | assert(objFile); |
| 895 | StringRef objName = objFile->path(); |
| 896 | std::pair<StringRef, StringRef> dirAndLeaf = objName.rsplit('/'); |
| 897 | if (!dirAndLeaf.second.empty()) |
| 898 | objName = dirAndLeaf.second; |
| 899 | for (const OrderFileNode &info : nodes) { |
| 900 | if (info.fileFilter.empty()) { |
| 901 | // Have unprefixed symbol name in order file that matches this atom. |
| 902 | ordinal = info.order; |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 903 | return true; |
| 904 | } |
| 905 | if (info.fileFilter.equals(objName)) { |
| 906 | // Have prefixed symbol name in order file that matches atom's path. |
| 907 | ordinal = info.order; |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 908 | return true; |
| 909 | } |
| 910 | } |
| 911 | return false; |
| 912 | } |
| 913 | |
| 914 | bool MachOLinkingContext::customAtomOrderer(const DefinedAtom *left, |
| 915 | const DefinedAtom *right, |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 916 | bool &leftBeforeRight) const { |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 917 | // No custom sorting if no order file entries. |
| 918 | if (!_orderFileEntries) |
| 919 | return false; |
| 920 | |
| 921 | // Order files can only order named atoms. |
| 922 | StringRef leftName = left->name(); |
| 923 | StringRef rightName = right->name(); |
| 924 | if (leftName.empty() || rightName.empty()) |
| 925 | return false; |
| 926 | |
| 927 | // If neither is in order file list, no custom sorter. |
| 928 | auto leftPos = _orderFiles.find(leftName); |
| 929 | auto rightPos = _orderFiles.find(rightName); |
| 930 | bool leftIsOrdered = (leftPos != _orderFiles.end()); |
| 931 | bool rightIsOrdered = (rightPos != _orderFiles.end()); |
| 932 | if (!leftIsOrdered && !rightIsOrdered) |
| 933 | return false; |
| 934 | |
| 935 | // There could be multiple symbols with same name but different file prefixes. |
| 936 | unsigned leftOrder; |
| 937 | unsigned rightOrder; |
| 938 | bool foundLeft = |
| 939 | leftIsOrdered && findOrderOrdinal(leftPos->getValue(), left, leftOrder); |
| 940 | bool foundRight = rightIsOrdered && |
| 941 | findOrderOrdinal(rightPos->getValue(), right, rightOrder); |
| 942 | if (!foundLeft && !foundRight) |
| 943 | return false; |
| 944 | |
| 945 | // If only one is in order file list, ordered one goes first. |
| 946 | if (foundLeft != foundRight) |
| 947 | leftBeforeRight = foundLeft; |
| 948 | else |
| 949 | leftBeforeRight = (leftOrder < rightOrder); |
| 950 | |
| 951 | return true; |
| 952 | } |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 953 | |
Rui Ueyama | 6163544 | 2015-01-15 08:31:46 +0000 | [diff] [blame] | 954 | static bool isLibrary(const std::unique_ptr<Node> &elem) { |
Rui Ueyama | ae1daae | 2015-01-15 08:51:23 +0000 | [diff] [blame] | 955 | if (FileNode *node = dyn_cast<FileNode>(const_cast<Node *>(elem.get()))) { |
| 956 | File *file = node->getFile(); |
| 957 | return isa<SharedLibraryFile>(file) || isa<ArchiveLibraryFile>(file); |
| 958 | } |
| 959 | return false; |
Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | // The darwin linker processes input files in two phases. The first phase |
| 963 | // links in all object (.o) files in command line order. The second phase |
| 964 | // links in libraries in command line order. |
| 965 | // In this function we reorder the input files so that all the object files |
| 966 | // comes before any library file. We also make a group for the library files |
| 967 | // so that the Resolver will reiterate over the libraries as long as we find |
| 968 | // new undefines from libraries. |
Denis Protivensky | cd61715 | 2015-03-14 10:34:43 +0000 | [diff] [blame] | 969 | void MachOLinkingContext::finalizeInputFiles() { |
Rui Ueyama | 883afba | 2015-01-15 08:46:36 +0000 | [diff] [blame] | 970 | std::vector<std::unique_ptr<Node>> &elements = getNodes(); |
Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 971 | std::stable_sort(elements.begin(), elements.end(), |
Rui Ueyama | 6163544 | 2015-01-15 08:31:46 +0000 | [diff] [blame] | 972 | [](const std::unique_ptr<Node> &a, |
| 973 | const std::unique_ptr<Node> &b) { |
Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 974 | return !isLibrary(a) && isLibrary(b); |
| 975 | }); |
| 976 | size_t numLibs = std::count_if(elements.begin(), elements.end(), isLibrary); |
| 977 | elements.push_back(llvm::make_unique<GroupEnd>(numLibs)); |
| 978 | } |
| 979 | |
Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 980 | } // end namespace lld |