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