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