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