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" |
| 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; |
| 48 | |
| 49 | namespace lld { |
| 50 | namespace mach_o { |
| 51 | namespace normalized { |
| 52 | |
| 53 | // Utility to call a lambda expression on each load command. |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 54 | static std::error_code forEachLoadCommand( |
| 55 | StringRef lcRange, unsigned lcCount, bool swap, bool is64, |
| 56 | 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] | 57 | const char* p = lcRange.begin(); |
| 58 | for (unsigned i=0; i < lcCount; ++i) { |
| 59 | const load_command *lc = reinterpret_cast<const load_command*>(p); |
| 60 | load_command lcCopy; |
| 61 | const load_command *slc = lc; |
| 62 | if (swap) { |
| 63 | memcpy(&lcCopy, lc, sizeof(load_command)); |
| 64 | swapStruct(lcCopy); |
| 65 | slc = &lcCopy; |
| 66 | } |
| 67 | if ( (p + slc->cmdsize) > lcRange.end() ) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 68 | return make_error_code(llvm::errc::executable_format_error); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 69 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 70 | if (func(slc->cmd, slc->cmdsize, p)) |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 71 | return std::error_code(); |
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 | p += slc->cmdsize; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 76 | return std::error_code(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 79 | static std::error_code appendRelocations(Relocations &relocs, StringRef buffer, |
| 80 | bool swap, bool bigEndian, |
| 81 | uint32_t reloff, uint32_t nreloc) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 82 | if ((reloff + nreloc*8) > buffer.size()) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 83 | return make_error_code(llvm::errc::executable_format_error); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 84 | const any_relocation_info* relocsArray = |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 85 | reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 86 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 87 | for(uint32_t i=0; i < nreloc; ++i) { |
| 88 | relocs.push_back(unpackRelocation(relocsArray[i], swap, bigEndian)); |
| 89 | } |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 90 | return std::error_code(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 93 | static std::error_code |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 94 | appendIndirectSymbols(IndirectSymbols &isyms, StringRef buffer, bool swap, |
| 95 | bool bigEndian, uint32_t istOffset, uint32_t istCount, |
| 96 | uint32_t startIndex, uint32_t count) { |
| 97 | if ((istOffset + istCount*4) > buffer.size()) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 98 | return make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 99 | if (startIndex+count > istCount) |
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 | const uint32_t *indirectSymbolArray = |
| 102 | reinterpret_cast<const uint32_t*>(buffer.begin()+istOffset); |
| 103 | |
| 104 | for(uint32_t i=0; i < count; ++i) { |
| 105 | isyms.push_back(read32(swap, indirectSymbolArray[startIndex+i])); |
| 106 | } |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 107 | return std::error_code(); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 111 | template <typename T> static T readBigEndian(T t) { |
| 112 | if (llvm::sys::IsLittleEndianHost) |
Artyom Skrobov | f8874b0 | 2014-06-14 13:26:14 +0000 | [diff] [blame] | 113 | llvm::sys::swapByteOrder(t); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 114 | return t; |
| 115 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 116 | |
| 117 | /// Reads a mach-o file and produces an in-memory normalized view. |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 118 | ErrorOr<std::unique_ptr<NormalizedFile>> |
| 119 | readBinary(std::unique_ptr<MemoryBuffer> &mb, |
| 120 | const MachOLinkingContext::Arch arch) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 121 | // Make empty NormalizedFile. |
| 122 | std::unique_ptr<NormalizedFile> f(new NormalizedFile()); |
| 123 | |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 124 | const char *start = mb->getBufferStart(); |
| 125 | size_t objSize = mb->getBufferSize(); |
| 126 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 127 | // Determine endianness and pointer size for mach-o file. |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 128 | const mach_header *mh = reinterpret_cast<const mach_header *>(start); |
| 129 | bool isFat = mh->magic == llvm::MachO::FAT_CIGAM || |
| 130 | mh->magic == llvm::MachO::FAT_MAGIC; |
| 131 | if (isFat) { |
| 132 | uint32_t cputype = MachOLinkingContext::cpuTypeFromArch(arch); |
| 133 | uint32_t cpusubtype = MachOLinkingContext::cpuSubtypeFromArch(arch); |
| 134 | const fat_header *fh = reinterpret_cast<const fat_header *>(start); |
| 135 | uint32_t nfat_arch = readBigEndian(fh->nfat_arch); |
| 136 | const fat_arch *fa = |
| 137 | reinterpret_cast<const fat_arch *>(start + sizeof(fat_header)); |
| 138 | bool foundArch = false; |
| 139 | while (nfat_arch-- > 0) { |
| 140 | if (readBigEndian(fa->cputype) == cputype && |
| 141 | readBigEndian(fa->cpusubtype) == cpusubtype) { |
| 142 | foundArch = true; |
| 143 | break; |
| 144 | } |
| 145 | fa++; |
| 146 | } |
| 147 | if (!foundArch) { |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 148 | return make_dynamic_error_code(Twine("file does not contain required" |
| 149 | " architecture (" |
| 150 | + MachOLinkingContext::nameFromArch(arch) |
| 151 | + ")" )); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 152 | } |
| 153 | objSize = readBigEndian(fa->size); |
| 154 | uint32_t offset = readBigEndian(fa->offset); |
| 155 | if ((offset + objSize) > mb->getBufferSize()) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 156 | return make_error_code(llvm::errc::executable_format_error); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 157 | start += offset; |
| 158 | mh = reinterpret_cast<const mach_header *>(start); |
| 159 | } |
| 160 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 161 | bool is64, swap; |
| 162 | switch (mh->magic) { |
| 163 | case llvm::MachO::MH_MAGIC: |
| 164 | is64 = false; |
| 165 | swap = false; |
| 166 | break; |
| 167 | case llvm::MachO::MH_MAGIC_64: |
| 168 | is64 = true; |
| 169 | swap = false; |
| 170 | break; |
| 171 | case llvm::MachO::MH_CIGAM: |
| 172 | is64 = false; |
| 173 | swap = true; |
| 174 | break; |
| 175 | case llvm::MachO::MH_CIGAM_64: |
| 176 | is64 = true; |
| 177 | swap = true; |
| 178 | break; |
| 179 | default: |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 180 | return make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Endian swap header, if needed. |
| 184 | mach_header headerCopy; |
| 185 | const mach_header *smh = mh; |
| 186 | if (swap) { |
| 187 | memcpy(&headerCopy, mh, sizeof(mach_header)); |
| 188 | swapStruct(headerCopy); |
| 189 | smh = &headerCopy; |
| 190 | } |
| 191 | |
| 192 | // Validate head and load commands fit in buffer. |
| 193 | const uint32_t lcCount = smh->ncmds; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 194 | const char *lcStart = |
| 195 | start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 196 | StringRef lcRange(lcStart, smh->sizeofcmds); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 197 | if (lcRange.end() > (start + objSize)) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 198 | return make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 199 | |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 200 | // Get architecture from mach_header. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 201 | f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype); |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 202 | if (f->arch != arch) { |
| 203 | return make_dynamic_error_code(Twine("file is wrong architecture. Expected " |
| 204 | "(" + MachOLinkingContext::nameFromArch(arch) |
| 205 | + ") found (" |
| 206 | + MachOLinkingContext::nameFromArch(f->arch) |
| 207 | + ")" )); |
| 208 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 209 | bool isBigEndianArch = MachOLinkingContext::isBigEndian(f->arch); |
| 210 | // Copy file type and flags |
| 211 | f->fileType = HeaderFileType(smh->filetype); |
| 212 | f->flags = smh->flags; |
| 213 | |
| 214 | |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 215 | // Pre-scan load commands looking for indirect symbol table. |
| 216 | uint32_t indirectSymbolTableOffset = 0; |
| 217 | uint32_t indirectSymbolTableCount = 0; |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 218 | std::error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64, |
| 219 | [&](uint32_t cmd, uint32_t size, |
| 220 | const char *lc) -> bool { |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 221 | if (cmd == LC_DYSYMTAB) { |
| 222 | const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc); |
| 223 | indirectSymbolTableOffset = read32(swap, d->indirectsymoff); |
| 224 | indirectSymbolTableCount = read32(swap, d->nindirectsyms); |
| 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | }); |
| 229 | if (ec) |
| 230 | return ec; |
| 231 | |
| 232 | // Walk load commands looking for segments/sections and the symbol table. |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 233 | const data_in_code_entry *dataInCode = nullptr; |
| 234 | uint32_t dataInCodeSize = 0; |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 235 | ec = forEachLoadCommand(lcRange, lcCount, swap, is64, |
| 236 | [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool { |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 237 | switch(cmd) { |
| 238 | case LC_SEGMENT_64: |
| 239 | if (is64) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 240 | const segment_command_64 *seg = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 241 | reinterpret_cast<const segment_command_64*>(lc); |
Artyom Skrobov | f8874b0 | 2014-06-14 13:26:14 +0000 | [diff] [blame] | 242 | const unsigned sectionCount = (swap |
| 243 | ? llvm::sys::getSwappedBytes(seg->nsects) |
| 244 | : seg->nsects); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 245 | const section_64 *sects = reinterpret_cast<const section_64*> |
| 246 | (lc + sizeof(segment_command_64)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 247 | const unsigned lcSize = sizeof(segment_command_64) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 248 | + sectionCount*sizeof(section_64); |
| 249 | // Verify sections don't extend beyond end of segment load command. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 250 | if (lcSize > size) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 251 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 252 | for (unsigned i=0; i < sectionCount; ++i) { |
| 253 | const section_64 *sect = §s[i]; |
| 254 | Section section; |
| 255 | section.segmentName = getString16(sect->segname); |
| 256 | section.sectionName = getString16(sect->sectname); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 257 | section.type = (SectionType)(read32(swap, sect->flags) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 258 | & SECTION_TYPE); |
| 259 | section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES; |
| 260 | section.alignment = read32(swap, sect->align); |
| 261 | section.address = read64(swap, sect->addr); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 262 | const uint8_t *content = |
| 263 | (uint8_t *)start + read32(swap, sect->offset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 264 | size_t contentSize = read64(swap, sect->size); |
| 265 | // Note: this assign() is copying the content bytes. Ideally, |
| 266 | // we can use a custom allocator for vector to avoid the copy. |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 267 | section.content = llvm::makeArrayRef(content, contentSize); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 268 | appendRelocations(section.relocations, mb->getBuffer(), |
| 269 | swap, isBigEndianArch, read32(swap, sect->reloff), |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 270 | read32(swap, sect->nreloc)); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 271 | if (section.type == S_NON_LAZY_SYMBOL_POINTERS) { |
| 272 | appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(), |
| 273 | swap, isBigEndianArch, |
| 274 | indirectSymbolTableOffset, |
| 275 | indirectSymbolTableCount, |
| 276 | read32(swap, sect->reserved1), contentSize/4); |
| 277 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 278 | f->sections.push_back(section); |
| 279 | } |
| 280 | } |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 281 | break; |
| 282 | case LC_SEGMENT: |
| 283 | if (!is64) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 284 | const segment_command *seg = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 285 | reinterpret_cast<const segment_command*>(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 *sects = reinterpret_cast<const section*> |
| 290 | (lc + sizeof(segment_command)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 291 | const unsigned lcSize = sizeof(segment_command) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 292 | + sectionCount*sizeof(section); |
| 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 *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 = read32(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 = read32(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_SYMTAB: { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 327 | const symtab_command *st = reinterpret_cast<const symtab_command*>(lc); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 328 | const char *strings = start + read32(swap, st->stroff); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 329 | const uint32_t strSize = read32(swap, st->strsize); |
| 330 | // Validate string pool and symbol table all in buffer. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 331 | if ( read32(swap, st->stroff)+read32(swap, st->strsize) |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 332 | > objSize ) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 333 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 334 | if (is64) { |
| 335 | const uint32_t symOffset = read32(swap, st->symoff); |
| 336 | const uint32_t symCount = read32(swap, st->nsyms); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 337 | if ( symOffset+(symCount*sizeof(nlist_64)) > objSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 338 | return true; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 339 | const nlist_64 *symbols = |
| 340 | reinterpret_cast<const nlist_64 *>(start + symOffset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 341 | // Convert each nlist_64 to a lld::mach_o::normalized::Symbol. |
| 342 | for(uint32_t i=0; i < symCount; ++i) { |
| 343 | const nlist_64 *sin = &symbols[i]; |
| 344 | nlist_64 tempSym; |
| 345 | if (swap) { |
| 346 | tempSym = *sin; swapStruct(tempSym); sin = &tempSym; |
| 347 | } |
| 348 | Symbol sout; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 349 | if (sin->n_strx > strSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 350 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 351 | sout.name = &strings[sin->n_strx]; |
| 352 | sout.type = (NListType)(sin->n_type & N_TYPE); |
| 353 | sout.scope = (sin->n_type & (N_PEXT|N_EXT)); |
| 354 | sout.sect = sin->n_sect; |
| 355 | sout.desc = sin->n_desc; |
| 356 | sout.value = sin->n_value; |
| 357 | if (sout.type == N_UNDF) |
| 358 | f->undefinedSymbols.push_back(sout); |
Nick Kledzik | 3f69076 | 2014-06-27 18:25:01 +0000 | [diff] [blame] | 359 | else if (sin->n_type & N_EXT) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 360 | f->globalSymbols.push_back(sout); |
| 361 | else |
| 362 | f->localSymbols.push_back(sout); |
| 363 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 364 | } else { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 365 | const uint32_t symOffset = read32(swap, st->symoff); |
| 366 | const uint32_t symCount = read32(swap, st->nsyms); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 367 | if ( symOffset+(symCount*sizeof(nlist)) > objSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 368 | return true; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 369 | const nlist *symbols = |
| 370 | reinterpret_cast<const nlist *>(start + symOffset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 371 | // Convert each nlist to a lld::mach_o::normalized::Symbol. |
| 372 | for(uint32_t i=0; i < symCount; ++i) { |
| 373 | const nlist *sin = &symbols[i]; |
| 374 | nlist tempSym; |
| 375 | if (swap) { |
| 376 | tempSym = *sin; swapStruct(tempSym); sin = &tempSym; |
| 377 | } |
| 378 | Symbol sout; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 379 | if (sin->n_strx > strSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 380 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 381 | sout.name = &strings[sin->n_strx]; |
| 382 | sout.type = (NListType)(sin->n_type & N_TYPE); |
| 383 | sout.scope = (sin->n_type & (N_PEXT|N_EXT)); |
| 384 | sout.sect = sin->n_sect; |
| 385 | sout.desc = sin->n_desc; |
| 386 | sout.value = sin->n_value; |
| 387 | if (sout.type == N_UNDF) |
| 388 | f->undefinedSymbols.push_back(sout); |
| 389 | else if (sout.scope == (SymbolScope)N_EXT) |
| 390 | f->globalSymbols.push_back(sout); |
| 391 | else |
| 392 | f->localSymbols.push_back(sout); |
| 393 | } |
| 394 | } |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 395 | } |
| 396 | break; |
| 397 | case LC_ID_DYLIB: { |
Tim Northover | 301c4e6 | 2014-07-01 08:15:41 +0000 | [diff] [blame] | 398 | const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 399 | f->installName = lc + read32(swap, dl->dylib.name); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 400 | } |
| 401 | break; |
| 402 | case LC_DATA_IN_CODE: { |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 403 | const linkedit_data_command *ldc = |
| 404 | reinterpret_cast<const linkedit_data_command*>(lc); |
| 405 | dataInCode = reinterpret_cast<const data_in_code_entry*>( |
| 406 | start + read32(swap, ldc->dataoff)); |
| 407 | dataInCodeSize = read32(swap, ldc->datasize); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 408 | } |
| 409 | case LC_LOAD_DYLIB: |
| 410 | case LC_LOAD_WEAK_DYLIB: |
| 411 | case LC_REEXPORT_DYLIB: |
| 412 | case LC_LOAD_UPWARD_DYLIB: { |
| 413 | const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc); |
| 414 | DependentDylib entry; |
| 415 | entry.path = lc + read32(swap, dl->dylib.name); |
| 416 | entry.kind = LoadCommandType(cmd); |
| 417 | f->dependentDylibs.push_back(entry); |
| 418 | } |
| 419 | break; |
Tim Northover | 301c4e6 | 2014-07-01 08:15:41 +0000 | [diff] [blame] | 420 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 421 | return false; |
| 422 | }); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 423 | if (ec) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 424 | return ec; |
| 425 | |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 426 | if (dataInCode) { |
| 427 | // Convert on-disk data_in_code_entry array to DataInCode vector. |
| 428 | for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); ++i) { |
| 429 | DataInCode entry; |
| 430 | entry.offset = read32(swap, dataInCode[i].offset); |
| 431 | entry.length = read16(swap, dataInCode[i].length); |
| 432 | entry.kind = (DataRegionType)read16(swap, dataInCode[i].kind); |
| 433 | f->dataInCode.push_back(entry); |
| 434 | } |
| 435 | } |
| 436 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 437 | return std::move(f); |
| 438 | } |
| 439 | |
| 440 | |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 441 | |
| 442 | class MachOReader : public Reader { |
| 443 | public: |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 444 | MachOReader(MachOLinkingContext &ctx) : _ctx(ctx) {} |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 445 | |
| 446 | bool canParse(file_magic magic, StringRef ext, |
| 447 | const MemoryBuffer &mb) const override { |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 448 | if (magic != llvm::sys::fs::file_magic::macho_object && |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 449 | magic != llvm::sys::fs::file_magic::macho_universal_binary && |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 450 | magic != llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib) |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 451 | return false; |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 452 | return (mb.getBufferSize() > 32); |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 455 | std::error_code |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 456 | parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry ®istry, |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 457 | std::vector<std::unique_ptr<File>> &result) const override { |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 458 | // Convert binary file to normalized mach-o. |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 459 | auto normFile = readBinary(mb, _ctx.arch()); |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 460 | if (std::error_code ec = normFile.getError()) |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 461 | return ec; |
| 462 | // Convert normalized mach-o to atoms. |
| 463 | auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false); |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 464 | if (std::error_code ec = file.getError()) |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 465 | return ec; |
| 466 | |
| 467 | result.push_back(std::move(*file)); |
| 468 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 469 | return std::error_code(); |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 470 | } |
| 471 | private: |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 472 | MachOLinkingContext &_ctx; |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 476 | } // namespace normalized |
| 477 | } // namespace mach_o |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 478 | |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 479 | void Registry::addSupportMachOObjects(MachOLinkingContext &ctx) { |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 480 | MachOLinkingContext::Arch arch = ctx.arch(); |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame^] | 481 | add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(ctx))); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 482 | addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(), |
| 483 | ctx.archHandler().kindStrings()); |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 484 | add(std::unique_ptr<YamlIOTaggedDocumentHandler>( |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 485 | new mach_o::MachOYamlIOTaggedDocumentHandler(arch))); |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 488 | } // namespace lld |
| 489 | |