Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryReader.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 | /// |
| 11 | /// \file For mach-o object files, this implementation converts from |
| 12 | /// mach-o on-disk binary format to in-memory normalized mach-o. |
| 13 | /// |
| 14 | /// +---------------+ |
| 15 | /// | binary mach-o | |
| 16 | /// +---------------+ |
| 17 | /// | |
| 18 | /// | |
| 19 | /// v |
| 20 | /// +------------+ |
| 21 | /// | normalized | |
| 22 | /// +------------+ |
| 23 | |
| 24 | #include "MachONormalizedFile.h" |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 25 | #include "ArchHandler.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 26 | #include "MachONormalizedFileBinaryUtils.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 27 | #include "lld/Core/Error.h" |
| 28 | #include "lld/Core/LLVM.h" |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 29 | #include "lld/Core/SharedLibraryFile.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
| 31 | #include "llvm/ADT/StringRef.h" |
| 32 | #include "llvm/ADT/StringSwitch.h" |
Rafael Espindola | c08ab8e | 2015-04-24 15:51:45 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Twine.h" |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 35 | #include "llvm/Object/MachO.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Casting.h" |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Errc.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
| 39 | #include "llvm/Support/FileOutputBuffer.h" |
| 40 | #include "llvm/Support/Host.h" |
| 41 | #include "llvm/Support/MachO.h" |
| 42 | #include "llvm/Support/MemoryBuffer.h" |
| 43 | #include "llvm/Support/raw_ostream.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 44 | #include <functional> |
Rafael Espindola | 54427cc | 2014-06-12 17:15:58 +0000 | [diff] [blame] | 45 | #include <system_error> |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 46 | |
| 47 | using namespace llvm::MachO; |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 48 | using llvm::object::ExportEntry; |
| 49 | using llvm::object::MachOObjectFile; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 50 | |
| 51 | namespace lld { |
| 52 | namespace mach_o { |
| 53 | namespace normalized { |
| 54 | |
| 55 | // Utility to call a lambda expression on each load command. |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 56 | static std::error_code forEachLoadCommand( |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 57 | StringRef lcRange, unsigned lcCount, bool isBig, bool is64, |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 58 | std::function<bool(uint32_t cmd, uint32_t size, const char *lc)> func) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 59 | const char* p = lcRange.begin(); |
| 60 | for (unsigned i=0; i < lcCount; ++i) { |
| 61 | const load_command *lc = reinterpret_cast<const load_command*>(p); |
| 62 | load_command lcCopy; |
| 63 | const load_command *slc = lc; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 64 | if (isBig != llvm::sys::IsBigEndianHost) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 65 | memcpy(&lcCopy, lc, sizeof(load_command)); |
| 66 | swapStruct(lcCopy); |
| 67 | slc = &lcCopy; |
| 68 | } |
| 69 | if ( (p + slc->cmdsize) > lcRange.end() ) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 70 | return make_error_code(llvm::errc::executable_format_error); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 71 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 72 | if (func(slc->cmd, slc->cmdsize, p)) |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 73 | return std::error_code(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 74 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 75 | p += slc->cmdsize; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 78 | return std::error_code(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 81 | static std::error_code appendRelocations(Relocations &relocs, StringRef buffer, |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 82 | bool bigEndian, |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 83 | uint32_t reloff, uint32_t nreloc) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 84 | if ((reloff + nreloc*8) > buffer.size()) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 85 | return make_error_code(llvm::errc::executable_format_error); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 86 | const any_relocation_info* relocsArray = |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 87 | reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 88 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 89 | for(uint32_t i=0; i < nreloc; ++i) { |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 90 | relocs.push_back(unpackRelocation(relocsArray[i], bigEndian)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 91 | } |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 92 | return std::error_code(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 95 | static std::error_code |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 96 | appendIndirectSymbols(IndirectSymbols &isyms, StringRef buffer, bool isBig, |
| 97 | uint32_t istOffset, uint32_t istCount, |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 98 | uint32_t startIndex, uint32_t count) { |
| 99 | if ((istOffset + istCount*4) > buffer.size()) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 100 | return make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 101 | if (startIndex+count > istCount) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 102 | return make_error_code(llvm::errc::executable_format_error); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 103 | const uint8_t *indirectSymbolArray = (const uint8_t *)buffer.data(); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 104 | |
| 105 | for(uint32_t i=0; i < count; ++i) { |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 106 | isyms.push_back(read32( |
| 107 | indirectSymbolArray + (startIndex + i) * sizeof(uint32_t), isBig)); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 108 | } |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 109 | return std::error_code(); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 113 | template <typename T> static T readBigEndian(T t) { |
| 114 | if (llvm::sys::IsLittleEndianHost) |
Artyom Skrobov | f8874b0 | 2014-06-14 13:26:14 +0000 | [diff] [blame] | 115 | llvm::sys::swapByteOrder(t); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 116 | return t; |
| 117 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 118 | |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 119 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 120 | static bool isMachOHeader(const mach_header *mh, bool &is64, bool &isBig) { |
| 121 | switch (read32(&mh->magic, false)) { |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 122 | case llvm::MachO::MH_MAGIC: |
| 123 | is64 = false; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 124 | isBig = false; |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 125 | return true; |
| 126 | case llvm::MachO::MH_MAGIC_64: |
| 127 | is64 = true; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 128 | isBig = false; |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 129 | return true; |
| 130 | case llvm::MachO::MH_CIGAM: |
| 131 | is64 = false; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 132 | isBig = true; |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 133 | return true; |
| 134 | case llvm::MachO::MH_CIGAM_64: |
| 135 | is64 = true; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 136 | isBig = true; |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 137 | return true; |
| 138 | default: |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch) { |
| 145 | // Try opening and mapping file at path. |
| 146 | ErrorOr<std::unique_ptr<MemoryBuffer>> b = MemoryBuffer::getFileOrSTDIN(path); |
| 147 | if (b.getError()) |
| 148 | return false; |
| 149 | |
| 150 | // If file length < 32 it is too small to be mach-o object file. |
| 151 | StringRef fileBuffer = b->get()->getBuffer(); |
| 152 | if (fileBuffer.size() < 32) |
| 153 | return false; |
| 154 | |
| 155 | // If file buffer does not start with MH_MAGIC (and variants), not obj file. |
| 156 | const mach_header *mh = reinterpret_cast<const mach_header *>( |
| 157 | fileBuffer.begin()); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 158 | bool is64, isBig; |
| 159 | if (!isMachOHeader(mh, is64, isBig)) |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 160 | return false; |
| 161 | |
| 162 | // If not MH_OBJECT, not object file. |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 163 | if (read32(&mh->filetype, isBig) != MH_OBJECT) |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 164 | return false; |
| 165 | |
| 166 | // Lookup up arch from cpu/subtype pair. |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 167 | arch = MachOLinkingContext::archFromCpuType( |
| 168 | read32(&mh->cputype, isBig), |
| 169 | read32(&mh->cpusubtype, isBig)); |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | |
Rafael Espindola | ed48e53 | 2015-04-27 22:48:51 +0000 | [diff] [blame] | 173 | bool sliceFromFatFile(MemoryBufferRef mb, MachOLinkingContext::Arch arch, |
| 174 | uint32_t &offset, uint32_t &size) { |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 175 | const char *start = mb.getBufferStart(); |
| 176 | const llvm::MachO::fat_header *fh = |
| 177 | reinterpret_cast<const llvm::MachO::fat_header *>(start); |
| 178 | if (readBigEndian(fh->magic) != llvm::MachO::FAT_MAGIC) |
| 179 | return false; |
| 180 | uint32_t nfat_arch = readBigEndian(fh->nfat_arch); |
| 181 | const fat_arch *fstart = |
| 182 | reinterpret_cast<const fat_arch *>(start + sizeof(fat_header)); |
| 183 | const fat_arch *fend = |
| 184 | reinterpret_cast<const fat_arch *>(start + sizeof(fat_header) + |
| 185 | sizeof(fat_arch) * nfat_arch); |
| 186 | const uint32_t reqCpuType = MachOLinkingContext::cpuTypeFromArch(arch); |
| 187 | const uint32_t reqCpuSubtype = MachOLinkingContext::cpuSubtypeFromArch(arch); |
| 188 | for (const fat_arch *fa = fstart; fa < fend; ++fa) { |
| 189 | if ((readBigEndian(fa->cputype) == reqCpuType) && |
| 190 | (readBigEndian(fa->cpusubtype) == reqCpuSubtype)) { |
| 191 | offset = readBigEndian(fa->offset); |
| 192 | size = readBigEndian(fa->size); |
| 193 | if ((offset + size) > mb.getBufferSize()) |
| 194 | return false; |
| 195 | return true; |
| 196 | } |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 201 | /// Reads a mach-o file and produces an in-memory normalized view. |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 202 | ErrorOr<std::unique_ptr<NormalizedFile>> |
| 203 | readBinary(std::unique_ptr<MemoryBuffer> &mb, |
| 204 | const MachOLinkingContext::Arch arch) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 205 | // Make empty NormalizedFile. |
| 206 | std::unique_ptr<NormalizedFile> f(new NormalizedFile()); |
| 207 | |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 208 | const char *start = mb->getBufferStart(); |
| 209 | size_t objSize = mb->getBufferSize(); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 210 | const mach_header *mh = reinterpret_cast<const mach_header *>(start); |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 211 | |
| 212 | uint32_t sliceOffset; |
| 213 | uint32_t sliceSize; |
Rafael Espindola | ed48e53 | 2015-04-27 22:48:51 +0000 | [diff] [blame] | 214 | if (sliceFromFatFile(mb->getMemBufferRef(), arch, sliceOffset, sliceSize)) { |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 215 | start = &start[sliceOffset]; |
| 216 | objSize = sliceSize; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 217 | mh = reinterpret_cast<const mach_header *>(start); |
| 218 | } |
| 219 | |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 220 | // Determine endianness and pointer size for mach-o file. |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 221 | bool is64, isBig; |
| 222 | if (!isMachOHeader(mh, is64, isBig)) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 223 | return make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 224 | |
| 225 | // Endian swap header, if needed. |
| 226 | mach_header headerCopy; |
| 227 | const mach_header *smh = mh; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 228 | if (isBig != llvm::sys::IsBigEndianHost) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 229 | memcpy(&headerCopy, mh, sizeof(mach_header)); |
| 230 | swapStruct(headerCopy); |
| 231 | smh = &headerCopy; |
| 232 | } |
| 233 | |
| 234 | // Validate head and load commands fit in buffer. |
| 235 | const uint32_t lcCount = smh->ncmds; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 236 | const char *lcStart = |
| 237 | start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 238 | StringRef lcRange(lcStart, smh->sizeofcmds); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 239 | if (lcRange.end() > (start + objSize)) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 240 | return make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 241 | |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 242 | // Get architecture from mach_header. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 243 | f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype); |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 244 | if (f->arch != arch) { |
| 245 | return make_dynamic_error_code(Twine("file is wrong architecture. Expected " |
| 246 | "(" + MachOLinkingContext::nameFromArch(arch) |
| 247 | + ") found (" |
| 248 | + MachOLinkingContext::nameFromArch(f->arch) |
| 249 | + ")" )); |
| 250 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 251 | // Copy file type and flags |
| 252 | f->fileType = HeaderFileType(smh->filetype); |
| 253 | f->flags = smh->flags; |
| 254 | |
| 255 | |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 256 | // Pre-scan load commands looking for indirect symbol table. |
| 257 | uint32_t indirectSymbolTableOffset = 0; |
| 258 | uint32_t indirectSymbolTableCount = 0; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 259 | std::error_code ec = forEachLoadCommand(lcRange, lcCount, isBig, is64, |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 260 | [&](uint32_t cmd, uint32_t size, |
| 261 | const char *lc) -> bool { |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 262 | if (cmd == LC_DYSYMTAB) { |
| 263 | const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 264 | indirectSymbolTableOffset = read32(&d->indirectsymoff, isBig); |
| 265 | indirectSymbolTableCount = read32(&d->nindirectsyms, isBig); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | return false; |
| 269 | }); |
| 270 | if (ec) |
| 271 | return ec; |
| 272 | |
| 273 | // Walk load commands looking for segments/sections and the symbol table. |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 274 | const data_in_code_entry *dataInCode = nullptr; |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 275 | const dyld_info_command *dyldInfo = nullptr; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 276 | uint32_t dataInCodeSize = 0; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 277 | ec = forEachLoadCommand(lcRange, lcCount, isBig, is64, |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 278 | [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 279 | switch(cmd) { |
| 280 | case LC_SEGMENT_64: |
| 281 | if (is64) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 282 | const segment_command_64 *seg = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 283 | reinterpret_cast<const segment_command_64*>(lc); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 284 | const unsigned sectionCount = read32(&seg->nsects, isBig); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 285 | const section_64 *sects = reinterpret_cast<const section_64*> |
| 286 | (lc + sizeof(segment_command_64)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 287 | const unsigned lcSize = sizeof(segment_command_64) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 288 | + sectionCount*sizeof(section_64); |
| 289 | // Verify sections don't extend beyond end of segment load command. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 290 | if (lcSize > size) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 291 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 292 | for (unsigned i=0; i < sectionCount; ++i) { |
| 293 | const section_64 *sect = §s[i]; |
| 294 | Section section; |
| 295 | section.segmentName = getString16(sect->segname); |
| 296 | section.sectionName = getString16(sect->sectname); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 297 | section.type = (SectionType)(read32(§->flags, isBig) & |
| 298 | SECTION_TYPE); |
| 299 | section.attributes = read32(§->flags, isBig) & SECTION_ATTRIBUTES; |
Rui Ueyama | f006f4d | 2015-03-26 01:44:01 +0000 | [diff] [blame] | 300 | section.alignment = 1 << read32(§->align, isBig); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 301 | section.address = read64(§->addr, isBig); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 302 | const uint8_t *content = |
Simon Atanasyan | 55c2699 | 2014-11-14 07:15:43 +0000 | [diff] [blame] | 303 | (const uint8_t *)start + read32(§->offset, isBig); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 304 | size_t contentSize = read64(§->size, isBig); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 305 | // Note: this assign() is copying the content bytes. Ideally, |
| 306 | // we can use a custom allocator for vector to avoid the copy. |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 307 | section.content = llvm::makeArrayRef(content, contentSize); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 308 | appendRelocations(section.relocations, mb->getBuffer(), isBig, |
| 309 | read32(§->reloff, isBig), |
| 310 | read32(§->nreloc, isBig)); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 311 | if (section.type == S_NON_LAZY_SYMBOL_POINTERS) { |
| 312 | appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(), |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 313 | isBig, |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 314 | indirectSymbolTableOffset, |
| 315 | indirectSymbolTableCount, |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 316 | read32(§->reserved1, isBig), |
| 317 | contentSize/4); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 318 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 319 | f->sections.push_back(section); |
| 320 | } |
| 321 | } |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 322 | break; |
| 323 | case LC_SEGMENT: |
| 324 | if (!is64) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 325 | const segment_command *seg = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 326 | reinterpret_cast<const segment_command*>(lc); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 327 | const unsigned sectionCount = read32(&seg->nsects, isBig); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 328 | const section *sects = reinterpret_cast<const section*> |
| 329 | (lc + sizeof(segment_command)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 330 | const unsigned lcSize = sizeof(segment_command) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 331 | + sectionCount*sizeof(section); |
| 332 | // Verify sections don't extend beyond end of segment load command. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 333 | if (lcSize > size) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 334 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 335 | for (unsigned i=0; i < sectionCount; ++i) { |
| 336 | const section *sect = §s[i]; |
| 337 | Section section; |
| 338 | section.segmentName = getString16(sect->segname); |
| 339 | section.sectionName = getString16(sect->sectname); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 340 | section.type = (SectionType)(read32(§->flags, isBig) & |
| 341 | SECTION_TYPE); |
| 342 | section.attributes = |
Simon Atanasyan | 55c2699 | 2014-11-14 07:15:43 +0000 | [diff] [blame] | 343 | read32((const uint8_t *)§->flags, isBig) & SECTION_ATTRIBUTES; |
Rui Ueyama | f006f4d | 2015-03-26 01:44:01 +0000 | [diff] [blame] | 344 | section.alignment = 1 << read32(§->align, isBig); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 345 | section.address = read32(§->addr, isBig); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 346 | const uint8_t *content = |
Simon Atanasyan | 55c2699 | 2014-11-14 07:15:43 +0000 | [diff] [blame] | 347 | (const uint8_t *)start + read32(§->offset, isBig); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 348 | size_t contentSize = read32(§->size, isBig); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 349 | // Note: this assign() is copying the content bytes. Ideally, |
| 350 | // we can use a custom allocator for vector to avoid the copy. |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 351 | section.content = llvm::makeArrayRef(content, contentSize); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 352 | appendRelocations(section.relocations, mb->getBuffer(), isBig, |
| 353 | read32(§->reloff, isBig), |
| 354 | read32(§->nreloc, isBig)); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 355 | if (section.type == S_NON_LAZY_SYMBOL_POINTERS) { |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 356 | appendIndirectSymbols( |
| 357 | section.indirectSymbols, mb->getBuffer(), isBig, |
| 358 | indirectSymbolTableOffset, indirectSymbolTableCount, |
| 359 | read32(§->reserved1, isBig), contentSize / 4); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 360 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 361 | f->sections.push_back(section); |
| 362 | } |
| 363 | } |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 364 | break; |
| 365 | case LC_SYMTAB: { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 366 | const symtab_command *st = reinterpret_cast<const symtab_command*>(lc); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 367 | const char *strings = start + read32(&st->stroff, isBig); |
| 368 | const uint32_t strSize = read32(&st->strsize, isBig); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 369 | // Validate string pool and symbol table all in buffer. |
Simon Atanasyan | 55c2699 | 2014-11-14 07:15:43 +0000 | [diff] [blame] | 370 | if (read32((const uint8_t *)&st->stroff, isBig) + |
| 371 | read32((const uint8_t *)&st->strsize, isBig) > |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 372 | objSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 373 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 374 | if (is64) { |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 375 | const uint32_t symOffset = read32(&st->symoff, isBig); |
| 376 | const uint32_t symCount = read32(&st->nsyms, isBig); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 377 | if ( symOffset+(symCount*sizeof(nlist_64)) > objSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 378 | return true; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 379 | const nlist_64 *symbols = |
| 380 | reinterpret_cast<const nlist_64 *>(start + symOffset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 381 | // Convert each nlist_64 to a lld::mach_o::normalized::Symbol. |
| 382 | for(uint32_t i=0; i < symCount; ++i) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 383 | nlist_64 tempSym; |
Pete Cooper | e82f3a0 | 2016-03-23 18:00:10 +0000 | [diff] [blame] | 384 | memcpy(&tempSym, &symbols[i], sizeof(nlist_64)); |
| 385 | const nlist_64 *sin = &tempSym; |
| 386 | if (isBig != llvm::sys::IsBigEndianHost) |
| 387 | swapStruct(tempSym); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 388 | Symbol sout; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 389 | if (sin->n_strx > strSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 390 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 391 | sout.name = &strings[sin->n_strx]; |
| 392 | sout.type = (NListType)(sin->n_type & N_TYPE); |
| 393 | sout.scope = (sin->n_type & (N_PEXT|N_EXT)); |
| 394 | sout.sect = sin->n_sect; |
| 395 | sout.desc = sin->n_desc; |
| 396 | sout.value = sin->n_value; |
| 397 | if (sout.type == N_UNDF) |
| 398 | f->undefinedSymbols.push_back(sout); |
Nick Kledzik | 3f69076 | 2014-06-27 18:25:01 +0000 | [diff] [blame] | 399 | else if (sin->n_type & N_EXT) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 400 | f->globalSymbols.push_back(sout); |
| 401 | else |
| 402 | f->localSymbols.push_back(sout); |
| 403 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 404 | } else { |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 405 | const uint32_t symOffset = read32(&st->symoff, isBig); |
| 406 | const uint32_t symCount = read32(&st->nsyms, isBig); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 407 | if ( symOffset+(symCount*sizeof(nlist)) > objSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 408 | return true; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 409 | const nlist *symbols = |
| 410 | reinterpret_cast<const nlist *>(start + symOffset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 411 | // Convert each nlist to a lld::mach_o::normalized::Symbol. |
| 412 | for(uint32_t i=0; i < symCount; ++i) { |
| 413 | const nlist *sin = &symbols[i]; |
| 414 | nlist tempSym; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 415 | if (isBig != llvm::sys::IsBigEndianHost) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 416 | tempSym = *sin; swapStruct(tempSym); sin = &tempSym; |
| 417 | } |
| 418 | Symbol sout; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 419 | if (sin->n_strx > strSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 420 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 421 | sout.name = &strings[sin->n_strx]; |
| 422 | sout.type = (NListType)(sin->n_type & N_TYPE); |
| 423 | sout.scope = (sin->n_type & (N_PEXT|N_EXT)); |
| 424 | sout.sect = sin->n_sect; |
| 425 | sout.desc = sin->n_desc; |
| 426 | sout.value = sin->n_value; |
| 427 | if (sout.type == N_UNDF) |
| 428 | f->undefinedSymbols.push_back(sout); |
| 429 | else if (sout.scope == (SymbolScope)N_EXT) |
| 430 | f->globalSymbols.push_back(sout); |
| 431 | else |
| 432 | f->localSymbols.push_back(sout); |
| 433 | } |
| 434 | } |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 435 | } |
| 436 | break; |
| 437 | case LC_ID_DYLIB: { |
Tim Northover | 301c4e6 | 2014-07-01 08:15:41 +0000 | [diff] [blame] | 438 | const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 439 | f->installName = lc + read32(&dl->dylib.name, isBig); |
Jean-Daniel Dupas | edefccc | 2014-12-20 09:22:56 +0000 | [diff] [blame] | 440 | f->currentVersion = read32(&dl->dylib.current_version, isBig); |
| 441 | f->compatVersion = read32(&dl->dylib.compatibility_version, isBig); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 442 | } |
| 443 | break; |
| 444 | case LC_DATA_IN_CODE: { |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 445 | const linkedit_data_command *ldc = |
| 446 | reinterpret_cast<const linkedit_data_command*>(lc); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 447 | dataInCode = reinterpret_cast<const data_in_code_entry *>( |
| 448 | start + read32(&ldc->dataoff, isBig)); |
| 449 | dataInCodeSize = read32(&ldc->datasize, isBig); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 450 | } |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 451 | break; |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 452 | case LC_LOAD_DYLIB: |
| 453 | case LC_LOAD_WEAK_DYLIB: |
| 454 | case LC_REEXPORT_DYLIB: |
| 455 | case LC_LOAD_UPWARD_DYLIB: { |
| 456 | const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc); |
| 457 | DependentDylib entry; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 458 | entry.path = lc + read32(&dl->dylib.name, isBig); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 459 | entry.kind = LoadCommandType(cmd); |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 460 | entry.compatVersion = read32(&dl->dylib.compatibility_version, isBig); |
| 461 | entry.currentVersion = read32(&dl->dylib.current_version, isBig); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 462 | f->dependentDylibs.push_back(entry); |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 463 | } |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 464 | break; |
Jean-Daniel Dupas | 23dd15e | 2014-12-18 21:33:38 +0000 | [diff] [blame] | 465 | case LC_RPATH: { |
| 466 | const rpath_command *rpc = reinterpret_cast<const rpath_command *>(lc); |
| 467 | f->rpaths.push_back(lc + read32(&rpc->path, isBig)); |
| 468 | } |
| 469 | break; |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 470 | case LC_DYLD_INFO: |
| 471 | case LC_DYLD_INFO_ONLY: |
| 472 | dyldInfo = reinterpret_cast<const dyld_info_command*>(lc); |
| 473 | break; |
Pete Cooper | ceee5de | 2016-02-04 02:16:08 +0000 | [diff] [blame] | 474 | case LC_VERSION_MIN_MACOSX: |
| 475 | case LC_VERSION_MIN_IPHONEOS: |
| 476 | case LC_VERSION_MIN_WATCHOS: |
| 477 | case LC_VERSION_MIN_TVOS: |
| 478 | // If we are emitting an object file, then we may take the load command |
| 479 | // kind from these commands and pass it on to the output |
| 480 | // file. |
| 481 | f->minOSVersionKind = (LoadCommandType)cmd; |
| 482 | break; |
Tim Northover | 301c4e6 | 2014-07-01 08:15:41 +0000 | [diff] [blame] | 483 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 484 | return false; |
| 485 | }); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 486 | if (ec) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 487 | return ec; |
| 488 | |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 489 | if (dataInCode) { |
| 490 | // Convert on-disk data_in_code_entry array to DataInCode vector. |
| 491 | for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); ++i) { |
| 492 | DataInCode entry; |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 493 | entry.offset = read32(&dataInCode[i].offset, isBig); |
| 494 | entry.length = read16(&dataInCode[i].length, isBig); |
| 495 | entry.kind = |
Simon Atanasyan | 55c2699 | 2014-11-14 07:15:43 +0000 | [diff] [blame] | 496 | (DataRegionType)read16((const uint8_t *)&dataInCode[i].kind, isBig); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 497 | f->dataInCode.push_back(entry); |
| 498 | } |
| 499 | } |
| 500 | |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 501 | if (dyldInfo) { |
| 502 | // If any exports, extract and add to normalized exportInfo vector. |
| 503 | if (dyldInfo->export_size) { |
| 504 | const uint8_t *trieStart = reinterpret_cast<const uint8_t*>(start + |
| 505 | dyldInfo->export_off); |
| 506 | ArrayRef<uint8_t> trie(trieStart, dyldInfo->export_size); |
| 507 | for (const ExportEntry &trieExport : MachOObjectFile::exports(trie)) { |
| 508 | Export normExport; |
| 509 | normExport.name = trieExport.name().copy(f->ownedAllocations); |
| 510 | normExport.offset = trieExport.address(); |
| 511 | normExport.kind = ExportSymbolKind(trieExport.flags() & EXPORT_SYMBOL_FLAGS_KIND_MASK); |
| 512 | normExport.flags = trieExport.flags() & ~EXPORT_SYMBOL_FLAGS_KIND_MASK; |
| 513 | normExport.otherOffset = trieExport.other(); |
| 514 | if (!trieExport.otherName().empty()) |
| 515 | normExport.otherName = trieExport.otherName().copy(f->ownedAllocations); |
| 516 | f->exportInfo.push_back(normExport); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 521 | return std::move(f); |
| 522 | } |
| 523 | |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 524 | class MachOObjectReader : public Reader { |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 525 | public: |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 526 | MachOObjectReader(MachOLinkingContext &ctx) : _ctx(ctx) {} |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 527 | |
Rafael Espindola | 6759319 | 2015-04-24 21:10:50 +0000 | [diff] [blame] | 528 | bool canParse(file_magic magic, MemoryBufferRef mb) const override { |
Rui Ueyama | 55f5b2b | 2015-04-04 02:44:36 +0000 | [diff] [blame] | 529 | return (magic == llvm::sys::fs::file_magic::macho_object && |
| 530 | mb.getBufferSize() > 32); |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Rafael Espindola | dedab91 | 2015-04-24 18:33:50 +0000 | [diff] [blame] | 533 | ErrorOr<std::unique_ptr<File>> |
| 534 | loadFile(std::unique_ptr<MemoryBuffer> mb, |
| 535 | const Registry ®istry) const override { |
| 536 | std::unique_ptr<File> ret = |
| 537 | llvm::make_unique<MachOFile>(std::move(mb), &_ctx); |
| 538 | return std::move(ret); |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | private: |
| 542 | MachOLinkingContext &_ctx; |
| 543 | }; |
| 544 | |
| 545 | class MachODylibReader : public Reader { |
| 546 | public: |
| 547 | MachODylibReader(MachOLinkingContext &ctx) : _ctx(ctx) {} |
| 548 | |
Rafael Espindola | 6759319 | 2015-04-24 21:10:50 +0000 | [diff] [blame] | 549 | bool canParse(file_magic magic, MemoryBufferRef mb) const override { |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 550 | switch (magic) { |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 551 | case llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib: |
| 552 | case llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: |
Rui Ueyama | 55f5b2b | 2015-04-04 02:44:36 +0000 | [diff] [blame] | 553 | return mb.getBufferSize() > 32; |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 554 | default: |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 555 | return false; |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 556 | } |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Rafael Espindola | dedab91 | 2015-04-24 18:33:50 +0000 | [diff] [blame] | 559 | ErrorOr<std::unique_ptr<File>> |
| 560 | loadFile(std::unique_ptr<MemoryBuffer> mb, |
| 561 | const Registry ®istry) const override { |
| 562 | std::unique_ptr<File> ret = |
| 563 | llvm::make_unique<MachODylibFile>(std::move(mb), &_ctx); |
| 564 | return std::move(ret); |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 565 | } |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 566 | |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 567 | private: |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 568 | MachOLinkingContext &_ctx; |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 569 | }; |
| 570 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 571 | } // namespace normalized |
| 572 | } // namespace mach_o |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 573 | |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 574 | void Registry::addSupportMachOObjects(MachOLinkingContext &ctx) { |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 575 | MachOLinkingContext::Arch arch = ctx.arch(); |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 576 | add(std::unique_ptr<Reader>(new mach_o::normalized::MachOObjectReader(ctx))); |
| 577 | add(std::unique_ptr<Reader>(new mach_o::normalized::MachODylibReader(ctx))); |
Shankar Easwaran | a1d3637 | 2015-02-22 23:54:38 +0000 | [diff] [blame] | 578 | addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(), |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 579 | ctx.archHandler().kindStrings()); |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 580 | add(std::unique_ptr<YamlIOTaggedDocumentHandler>( |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 581 | new mach_o::MachOYamlIOTaggedDocumentHandler(arch))); |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 584 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 585 | } // namespace lld |