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