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