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