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