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