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) { |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 145 | return make_error_code(llvm::errc::executable_format_error); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 146 | } |
| 147 | objSize = readBigEndian(fa->size); |
| 148 | uint32_t offset = readBigEndian(fa->offset); |
| 149 | if ((offset + objSize) > mb->getBufferSize()) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 150 | return make_error_code(llvm::errc::executable_format_error); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 151 | start += offset; |
| 152 | mh = reinterpret_cast<const mach_header *>(start); |
| 153 | } |
| 154 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 155 | bool is64, swap; |
| 156 | switch (mh->magic) { |
| 157 | case llvm::MachO::MH_MAGIC: |
| 158 | is64 = false; |
| 159 | swap = false; |
| 160 | break; |
| 161 | case llvm::MachO::MH_MAGIC_64: |
| 162 | is64 = true; |
| 163 | swap = false; |
| 164 | break; |
| 165 | case llvm::MachO::MH_CIGAM: |
| 166 | is64 = false; |
| 167 | swap = true; |
| 168 | break; |
| 169 | case llvm::MachO::MH_CIGAM_64: |
| 170 | is64 = true; |
| 171 | swap = true; |
| 172 | break; |
| 173 | default: |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 174 | return make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Endian swap header, if needed. |
| 178 | mach_header headerCopy; |
| 179 | const mach_header *smh = mh; |
| 180 | if (swap) { |
| 181 | memcpy(&headerCopy, mh, sizeof(mach_header)); |
| 182 | swapStruct(headerCopy); |
| 183 | smh = &headerCopy; |
| 184 | } |
| 185 | |
| 186 | // Validate head and load commands fit in buffer. |
| 187 | const uint32_t lcCount = smh->ncmds; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 188 | const char *lcStart = |
| 189 | start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 190 | StringRef lcRange(lcStart, smh->sizeofcmds); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 191 | if (lcRange.end() > (start + objSize)) |
Rafael Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 192 | return make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 193 | |
| 194 | // Normalize architecture |
| 195 | f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype); |
| 196 | bool isBigEndianArch = MachOLinkingContext::isBigEndian(f->arch); |
| 197 | // Copy file type and flags |
| 198 | f->fileType = HeaderFileType(smh->filetype); |
| 199 | f->flags = smh->flags; |
| 200 | |
| 201 | |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 202 | // Pre-scan load commands looking for indirect symbol table. |
| 203 | uint32_t indirectSymbolTableOffset = 0; |
| 204 | uint32_t indirectSymbolTableCount = 0; |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 205 | std::error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64, |
| 206 | [&](uint32_t cmd, uint32_t size, |
| 207 | const char *lc) -> bool { |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 208 | if (cmd == LC_DYSYMTAB) { |
| 209 | const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc); |
| 210 | indirectSymbolTableOffset = read32(swap, d->indirectsymoff); |
| 211 | indirectSymbolTableCount = read32(swap, d->nindirectsyms); |
| 212 | return true; |
| 213 | } |
| 214 | return false; |
| 215 | }); |
| 216 | if (ec) |
| 217 | return ec; |
| 218 | |
| 219 | // Walk load commands looking for segments/sections and the symbol table. |
| 220 | ec = forEachLoadCommand(lcRange, lcCount, swap, is64, |
| 221 | [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 222 | if (is64) { |
| 223 | if (cmd == LC_SEGMENT_64) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 224 | const segment_command_64 *seg = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 225 | reinterpret_cast<const segment_command_64*>(lc); |
Artyom Skrobov | f8874b0 | 2014-06-14 13:26:14 +0000 | [diff] [blame] | 226 | const unsigned sectionCount = (swap |
| 227 | ? llvm::sys::getSwappedBytes(seg->nsects) |
| 228 | : seg->nsects); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 229 | const section_64 *sects = reinterpret_cast<const section_64*> |
| 230 | (lc + sizeof(segment_command_64)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 231 | const unsigned lcSize = sizeof(segment_command_64) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 232 | + sectionCount*sizeof(section_64); |
| 233 | // Verify sections don't extend beyond end of segment load command. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 234 | if (lcSize > size) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 235 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 236 | for (unsigned i=0; i < sectionCount; ++i) { |
| 237 | const section_64 *sect = §s[i]; |
| 238 | Section section; |
| 239 | section.segmentName = getString16(sect->segname); |
| 240 | section.sectionName = getString16(sect->sectname); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 241 | section.type = (SectionType)(read32(swap, sect->flags) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 242 | & SECTION_TYPE); |
| 243 | section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES; |
| 244 | section.alignment = read32(swap, sect->align); |
| 245 | section.address = read64(swap, sect->addr); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 246 | const uint8_t *content = |
| 247 | (uint8_t *)start + read32(swap, sect->offset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 248 | size_t contentSize = read64(swap, sect->size); |
| 249 | // Note: this assign() is copying the content bytes. Ideally, |
| 250 | // we can use a custom allocator for vector to avoid the copy. |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 251 | section.content = llvm::makeArrayRef(content, contentSize); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 252 | appendRelocations(section.relocations, mb->getBuffer(), |
| 253 | swap, isBigEndianArch, read32(swap, sect->reloff), |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 254 | read32(swap, sect->nreloc)); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 255 | if (section.type == S_NON_LAZY_SYMBOL_POINTERS) { |
| 256 | appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(), |
| 257 | swap, isBigEndianArch, |
| 258 | indirectSymbolTableOffset, |
| 259 | indirectSymbolTableCount, |
| 260 | read32(swap, sect->reserved1), contentSize/4); |
| 261 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 262 | f->sections.push_back(section); |
| 263 | } |
| 264 | } |
| 265 | } else { |
| 266 | if (cmd == LC_SEGMENT) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 267 | const segment_command *seg = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 268 | reinterpret_cast<const segment_command*>(lc); |
Artyom Skrobov | f8874b0 | 2014-06-14 13:26:14 +0000 | [diff] [blame] | 269 | const unsigned sectionCount = (swap |
| 270 | ? llvm::sys::getSwappedBytes(seg->nsects) |
| 271 | : seg->nsects); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 272 | const section *sects = reinterpret_cast<const section*> |
| 273 | (lc + sizeof(segment_command)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 274 | const unsigned lcSize = sizeof(segment_command) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 275 | + sectionCount*sizeof(section); |
| 276 | // Verify sections don't extend beyond end of segment load command. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 277 | if (lcSize > size) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 278 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 279 | for (unsigned i=0; i < sectionCount; ++i) { |
| 280 | const section *sect = §s[i]; |
| 281 | Section section; |
| 282 | section.segmentName = getString16(sect->segname); |
| 283 | section.sectionName = getString16(sect->sectname); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 284 | section.type = (SectionType)(read32(swap, sect->flags) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 285 | & SECTION_TYPE); |
| 286 | section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES; |
| 287 | section.alignment = read32(swap, sect->align); |
| 288 | section.address = read32(swap, sect->addr); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 289 | const uint8_t *content = |
| 290 | (uint8_t *)start + read32(swap, sect->offset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 291 | size_t contentSize = read32(swap, sect->size); |
| 292 | // Note: this assign() is copying the content bytes. Ideally, |
| 293 | // we can use a custom allocator for vector to avoid the copy. |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 294 | section.content = llvm::makeArrayRef(content, contentSize); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 295 | appendRelocations(section.relocations, mb->getBuffer(), |
| 296 | swap, isBigEndianArch, read32(swap, sect->reloff), |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 297 | read32(swap, sect->nreloc)); |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 298 | if (section.type == S_NON_LAZY_SYMBOL_POINTERS) { |
| 299 | appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(), |
| 300 | swap, isBigEndianArch, |
| 301 | indirectSymbolTableOffset, |
| 302 | indirectSymbolTableCount, |
| 303 | read32(swap, sect->reserved1), contentSize/4); |
| 304 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 305 | f->sections.push_back(section); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | if (cmd == LC_SYMTAB) { |
| 310 | const symtab_command *st = reinterpret_cast<const symtab_command*>(lc); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 311 | const char *strings = start + read32(swap, st->stroff); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 312 | const uint32_t strSize = read32(swap, st->strsize); |
| 313 | // Validate string pool and symbol table all in buffer. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 314 | if ( read32(swap, st->stroff)+read32(swap, st->strsize) |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 315 | > objSize ) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 316 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 317 | if (is64) { |
| 318 | const uint32_t symOffset = read32(swap, st->symoff); |
| 319 | const uint32_t symCount = read32(swap, st->nsyms); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 320 | if ( symOffset+(symCount*sizeof(nlist_64)) > objSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 321 | return true; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 322 | const nlist_64 *symbols = |
| 323 | reinterpret_cast<const nlist_64 *>(start + symOffset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 324 | // Convert each nlist_64 to a lld::mach_o::normalized::Symbol. |
| 325 | for(uint32_t i=0; i < symCount; ++i) { |
| 326 | const nlist_64 *sin = &symbols[i]; |
| 327 | nlist_64 tempSym; |
| 328 | if (swap) { |
| 329 | tempSym = *sin; swapStruct(tempSym); sin = &tempSym; |
| 330 | } |
| 331 | Symbol sout; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 332 | if (sin->n_strx > strSize) |
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 | sout.name = &strings[sin->n_strx]; |
| 335 | sout.type = (NListType)(sin->n_type & N_TYPE); |
| 336 | sout.scope = (sin->n_type & (N_PEXT|N_EXT)); |
| 337 | sout.sect = sin->n_sect; |
| 338 | sout.desc = sin->n_desc; |
| 339 | sout.value = sin->n_value; |
| 340 | if (sout.type == N_UNDF) |
| 341 | f->undefinedSymbols.push_back(sout); |
Nick Kledzik | 3f69076 | 2014-06-27 18:25:01 +0000 | [diff] [blame^] | 342 | else if (sin->n_type & N_EXT) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 343 | f->globalSymbols.push_back(sout); |
| 344 | else |
| 345 | f->localSymbols.push_back(sout); |
| 346 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 347 | } else { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 348 | const uint32_t symOffset = read32(swap, st->symoff); |
| 349 | const uint32_t symCount = read32(swap, st->nsyms); |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 350 | if ( symOffset+(symCount*sizeof(nlist)) > objSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 351 | return true; |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 352 | const nlist *symbols = |
| 353 | reinterpret_cast<const nlist *>(start + symOffset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 354 | // Convert each nlist to a lld::mach_o::normalized::Symbol. |
| 355 | for(uint32_t i=0; i < symCount; ++i) { |
| 356 | const nlist *sin = &symbols[i]; |
| 357 | nlist tempSym; |
| 358 | if (swap) { |
| 359 | tempSym = *sin; swapStruct(tempSym); sin = &tempSym; |
| 360 | } |
| 361 | Symbol sout; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 362 | if (sin->n_strx > strSize) |
Rafael Espindola | 1d364c1 | 2014-06-03 04:41:30 +0000 | [diff] [blame] | 363 | return true; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 364 | sout.name = &strings[sin->n_strx]; |
| 365 | sout.type = (NListType)(sin->n_type & N_TYPE); |
| 366 | sout.scope = (sin->n_type & (N_PEXT|N_EXT)); |
| 367 | sout.sect = sin->n_sect; |
| 368 | sout.desc = sin->n_desc; |
| 369 | sout.value = sin->n_value; |
| 370 | if (sout.type == N_UNDF) |
| 371 | f->undefinedSymbols.push_back(sout); |
| 372 | else if (sout.scope == (SymbolScope)N_EXT) |
| 373 | f->globalSymbols.push_back(sout); |
| 374 | else |
| 375 | f->localSymbols.push_back(sout); |
| 376 | } |
| 377 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 378 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 379 | return false; |
| 380 | }); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 381 | if (ec) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 382 | return ec; |
| 383 | |
| 384 | return std::move(f); |
| 385 | } |
| 386 | |
| 387 | |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 388 | |
| 389 | class MachOReader : public Reader { |
| 390 | public: |
| 391 | MachOReader(MachOLinkingContext::Arch arch) : _arch(arch) {} |
| 392 | |
| 393 | bool canParse(file_magic magic, StringRef ext, |
| 394 | const MemoryBuffer &mb) const override { |
| 395 | if (magic != llvm::sys::fs::file_magic::macho_object) |
| 396 | return false; |
| 397 | if (mb.getBufferSize() < 32) |
| 398 | return false; |
| 399 | const char *start = mb.getBufferStart(); |
| 400 | const mach_header *mh = reinterpret_cast<const mach_header *>(start); |
| 401 | const bool swap = (mh->magic == llvm::MachO::MH_CIGAM) || |
| 402 | (mh->magic == llvm::MachO::MH_CIGAM_64); |
| 403 | const uint32_t filesCpuType = read32(swap, mh->cputype); |
| 404 | const uint32_t filesCpuSubtype = read32(swap, mh->cpusubtype); |
| 405 | if (filesCpuType != MachOLinkingContext::cpuTypeFromArch(_arch)) |
| 406 | return false; |
| 407 | if (filesCpuSubtype != MachOLinkingContext::cpuSubtypeFromArch(_arch)) |
| 408 | return false; |
| 409 | |
| 410 | // Is mach-o file with correct cpu type/subtype. |
| 411 | return true; |
| 412 | } |
| 413 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 414 | std::error_code |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 415 | parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry ®istry, |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 416 | std::vector<std::unique_ptr<File>> &result) const override { |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 417 | // Convert binary file to normalized mach-o. |
| 418 | auto normFile = readBinary(mb, _arch); |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 419 | if (std::error_code ec = normFile.getError()) |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 420 | return ec; |
| 421 | // Convert normalized mach-o to atoms. |
| 422 | auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false); |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 423 | if (std::error_code ec = file.getError()) |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 424 | return ec; |
| 425 | |
| 426 | result.push_back(std::move(*file)); |
| 427 | |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 428 | return std::error_code(); |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 429 | } |
| 430 | private: |
| 431 | MachOLinkingContext::Arch _arch; |
| 432 | }; |
| 433 | |
| 434 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 435 | } // namespace normalized |
| 436 | } // namespace mach_o |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 437 | |
| 438 | void Registry::addSupportMachOObjects(StringRef archName) { |
| 439 | MachOLinkingContext::Arch arch = MachOLinkingContext::archFromName(archName); |
Nick Kledzik | cc28bc1 | 2014-05-30 01:13:49 +0000 | [diff] [blame] | 440 | add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(arch))); |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 441 | switch (arch) { |
| 442 | case MachOLinkingContext::arch_x86_64: |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 443 | addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::x86_64, |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 444 | mach_o::KindHandler_x86_64::kindStrings); |
| 445 | break; |
| 446 | case MachOLinkingContext::arch_x86: |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 447 | addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::x86, |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 448 | mach_o::KindHandler_x86::kindStrings); |
| 449 | break; |
| 450 | case MachOLinkingContext::arch_armv6: |
| 451 | case MachOLinkingContext::arch_armv7: |
| 452 | case MachOLinkingContext::arch_armv7s: |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 453 | addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::ARM, |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 454 | mach_o::KindHandler_arm::kindStrings); |
| 455 | break; |
| 456 | default: |
| 457 | llvm_unreachable("mach-o arch not supported"); |
| 458 | } |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 459 | add(std::unique_ptr<YamlIOTaggedDocumentHandler>( |
| 460 | new mach_o::MachOYamlIOTaggedDocumentHandler())); |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 463 | } // namespace lld |
| 464 | |