blob: a328ec46a2dfc8cebe649154eb365fe8cd181dca [file] [log] [blame]
Nick Kledzike34182f2013-11-06 21:36:55 +00001//===- 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 Kledzike5552772013-12-19 21:58:00 +000026#include "ReferenceKinds.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000027#include "lld/Core/Error.h"
28#include "lld/Core/LLVM.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000029#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 Espindola372bc702014-06-13 17:20:48 +000034#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000035#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 Kledzike34182f2013-11-06 21:36:55 +000041#include <functional>
Rafael Espindola54427cc2014-06-12 17:15:58 +000042#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000043
44using namespace llvm::MachO;
45
46namespace lld {
47namespace mach_o {
48namespace normalized {
49
50// Utility to call a lambda expression on each load command.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000051static 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 Kledzike34182f2013-11-06 21:36:55 +000054 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 Espindola372bc702014-06-13 17:20:48 +000065 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000066
Nick Kledzike34182f2013-11-06 21:36:55 +000067 if (func(slc->cmd, slc->cmdsize, p))
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000068 return std::error_code();
Shankar Easwaran3d8de472014-01-27 03:09:26 +000069
Nick Kledzike34182f2013-11-06 21:36:55 +000070 p += slc->cmdsize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +000071 }
72
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000073 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000074}
75
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000076static std::error_code appendRelocations(Relocations &relocs, StringRef buffer,
77 bool swap, bool bigEndian,
78 uint32_t reloff, uint32_t nreloc) {
Nick Kledzike34182f2013-11-06 21:36:55 +000079 if ((reloff + nreloc*8) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +000080 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000081 const any_relocation_info* relocsArray =
Joey Gouly010b3762014-01-14 22:32:38 +000082 reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000083
Nick Kledzike34182f2013-11-06 21:36:55 +000084 for(uint32_t i=0; i < nreloc; ++i) {
85 relocs.push_back(unpackRelocation(relocsArray[i], swap, bigEndian));
86 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000087 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000088}
89
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000090static std::error_code
Nick Kledzik388f3d02014-05-28 01:16:35 +000091appendIndirectSymbols(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 Espindola372bc702014-06-13 17:20:48 +000095 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzik388f3d02014-05-28 01:16:35 +000096 if (startIndex+count > istCount)
Rafael Espindola372bc702014-06-13 17:20:48 +000097 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzik388f3d02014-05-28 01:16:35 +000098 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 Espindolab1a4d3a2014-06-12 14:53:47 +0000104 return std::error_code();
Nick Kledzik388f3d02014-05-28 01:16:35 +0000105}
106
107
Joey Gouly010b3762014-01-14 22:32:38 +0000108template <typename T> static T readBigEndian(T t) {
109 if (llvm::sys::IsLittleEndianHost)
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000110 llvm::sys::swapByteOrder(t);
Joey Gouly010b3762014-01-14 22:32:38 +0000111 return t;
112}
Nick Kledzike34182f2013-11-06 21:36:55 +0000113
114/// Reads a mach-o file and produces an in-memory normalized view.
Joey Gouly010b3762014-01-14 22:32:38 +0000115ErrorOr<std::unique_ptr<NormalizedFile>>
116readBinary(std::unique_ptr<MemoryBuffer> &mb,
117 const MachOLinkingContext::Arch arch) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000118 // Make empty NormalizedFile.
119 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
120
Joey Gouly010b3762014-01-14 22:32:38 +0000121 const char *start = mb->getBufferStart();
122 size_t objSize = mb->getBufferSize();
123
Nick Kledzike34182f2013-11-06 21:36:55 +0000124 // Determine endianness and pointer size for mach-o file.
Joey Gouly010b3762014-01-14 22:32:38 +0000125 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 Espindola372bc702014-06-13 17:20:48 +0000145 return make_error_code(llvm::errc::executable_format_error);
Joey Gouly010b3762014-01-14 22:32:38 +0000146 }
147 objSize = readBigEndian(fa->size);
148 uint32_t offset = readBigEndian(fa->offset);
149 if ((offset + objSize) > mb->getBufferSize())
Rafael Espindola372bc702014-06-13 17:20:48 +0000150 return make_error_code(llvm::errc::executable_format_error);
Joey Gouly010b3762014-01-14 22:32:38 +0000151 start += offset;
152 mh = reinterpret_cast<const mach_header *>(start);
153 }
154
Nick Kledzike34182f2013-11-06 21:36:55 +0000155 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 Espindola372bc702014-06-13 17:20:48 +0000174 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000175 }
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 Gouly010b3762014-01-14 22:32:38 +0000188 const char *lcStart =
189 start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header));
Nick Kledzike34182f2013-11-06 21:36:55 +0000190 StringRef lcRange(lcStart, smh->sizeofcmds);
Joey Gouly010b3762014-01-14 22:32:38 +0000191 if (lcRange.end() > (start + objSize))
Rafael Espindola372bc702014-06-13 17:20:48 +0000192 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000193
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 Kledzik388f3d02014-05-28 01:16:35 +0000202 // Pre-scan load commands looking for indirect symbol table.
203 uint32_t indirectSymbolTableOffset = 0;
204 uint32_t indirectSymbolTableCount = 0;
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000205 std::error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
206 [&](uint32_t cmd, uint32_t size,
207 const char *lc) -> bool {
Nick Kledzik388f3d02014-05-28 01:16:35 +0000208 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 Kledzike34182f2013-11-06 21:36:55 +0000222 if (is64) {
223 if (cmd == LC_SEGMENT_64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000224 const segment_command_64 *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000225 reinterpret_cast<const segment_command_64*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000226 const unsigned sectionCount = (swap
227 ? llvm::sys::getSwappedBytes(seg->nsects)
228 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000229 const section_64 *sects = reinterpret_cast<const section_64*>
230 (lc + sizeof(segment_command_64));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000231 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000232 + sectionCount*sizeof(section_64);
233 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000234 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000235 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000236 for (unsigned i=0; i < sectionCount; ++i) {
237 const section_64 *sect = &sects[i];
238 Section section;
239 section.segmentName = getString16(sect->segname);
240 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000241 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000242 & 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 Gouly010b3762014-01-14 22:32:38 +0000246 const uint8_t *content =
247 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000248 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 Kledzik6edd7222014-01-11 01:07:43 +0000251 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000252 appendRelocations(section.relocations, mb->getBuffer(),
253 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000254 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000255 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 Kledzike34182f2013-11-06 21:36:55 +0000262 f->sections.push_back(section);
263 }
264 }
265 } else {
266 if (cmd == LC_SEGMENT) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000267 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000268 reinterpret_cast<const segment_command*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000269 const unsigned sectionCount = (swap
270 ? llvm::sys::getSwappedBytes(seg->nsects)
271 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000272 const section *sects = reinterpret_cast<const section*>
273 (lc + sizeof(segment_command));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000274 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000275 + sectionCount*sizeof(section);
276 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000277 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000278 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000279 for (unsigned i=0; i < sectionCount; ++i) {
280 const section *sect = &sects[i];
281 Section section;
282 section.segmentName = getString16(sect->segname);
283 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000284 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000285 & 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 Gouly010b3762014-01-14 22:32:38 +0000289 const uint8_t *content =
290 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000291 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 Kledzik6edd7222014-01-11 01:07:43 +0000294 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000295 appendRelocations(section.relocations, mb->getBuffer(),
296 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000297 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000298 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 Kledzike34182f2013-11-06 21:36:55 +0000305 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 Gouly010b3762014-01-14 22:32:38 +0000311 const char *strings = start + read32(swap, st->stroff);
Nick Kledzike34182f2013-11-06 21:36:55 +0000312 const uint32_t strSize = read32(swap, st->strsize);
313 // Validate string pool and symbol table all in buffer.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000314 if ( read32(swap, st->stroff)+read32(swap, st->strsize)
Joey Gouly010b3762014-01-14 22:32:38 +0000315 > objSize )
Rafael Espindola1d364c12014-06-03 04:41:30 +0000316 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000317 if (is64) {
318 const uint32_t symOffset = read32(swap, st->symoff);
319 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000320 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000321 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000322 const nlist_64 *symbols =
323 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000324 // 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 Easwaran3d8de472014-01-27 03:09:26 +0000332 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000333 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000334 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 Kledzik3f690762014-06-27 18:25:01 +0000342 else if (sin->n_type & N_EXT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000343 f->globalSymbols.push_back(sout);
344 else
345 f->localSymbols.push_back(sout);
346 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000347 } else {
Nick Kledzike34182f2013-11-06 21:36:55 +0000348 const uint32_t symOffset = read32(swap, st->symoff);
349 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000350 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000351 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000352 const nlist *symbols =
353 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000354 // 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 Easwaran3d8de472014-01-27 03:09:26 +0000362 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000363 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000364 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 Kledzike34182f2013-11-06 21:36:55 +0000378 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000379 return false;
380 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000381 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000382 return ec;
383
384 return std::move(f);
385}
386
387
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000388
389class MachOReader : public Reader {
390public:
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 Espindolab1a4d3a2014-06-12 14:53:47 +0000414 std::error_code
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000415 parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000416 std::vector<std::unique_ptr<File>> &result) const override {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000417 // Convert binary file to normalized mach-o.
418 auto normFile = readBinary(mb, _arch);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000419 if (std::error_code ec = normFile.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000420 return ec;
421 // Convert normalized mach-o to atoms.
422 auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000423 if (std::error_code ec = file.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000424 return ec;
425
426 result.push_back(std::move(*file));
427
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000428 return std::error_code();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000429 }
430private:
431 MachOLinkingContext::Arch _arch;
432};
433
434
Nick Kledzike34182f2013-11-06 21:36:55 +0000435} // namespace normalized
436} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000437
438void Registry::addSupportMachOObjects(StringRef archName) {
439 MachOLinkingContext::Arch arch = MachOLinkingContext::archFromName(archName);
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000440 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000441 switch (arch) {
442 case MachOLinkingContext::arch_x86_64:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000443 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::x86_64,
Nick Kledzike5552772013-12-19 21:58:00 +0000444 mach_o::KindHandler_x86_64::kindStrings);
445 break;
446 case MachOLinkingContext::arch_x86:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000447 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::x86,
Nick Kledzike5552772013-12-19 21:58:00 +0000448 mach_o::KindHandler_x86::kindStrings);
449 break;
450 case MachOLinkingContext::arch_armv6:
451 case MachOLinkingContext::arch_armv7:
452 case MachOLinkingContext::arch_armv7s:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000453 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::ARM,
Nick Kledzike5552772013-12-19 21:58:00 +0000454 mach_o::KindHandler_arm::kindStrings);
455 break;
456 default:
457 llvm_unreachable("mach-o arch not supported");
458 }
Nick Kledzik6edd7222014-01-11 01:07:43 +0000459 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
460 new mach_o::MachOYamlIOTaggedDocumentHandler()));
Nick Kledzike5552772013-12-19 21:58:00 +0000461}
462
Nick Kledzike34182f2013-11-06 21:36:55 +0000463} // namespace lld
464