blob: 2ca3442a5fdcb4aae5591ea07703524a13a83e51 [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)
110 return SwapByteOrder(t);
111 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);
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 Easwaran3d8de472014-01-27 03:09:26 +0000230 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000231 + sectionCount*sizeof(section_64);
232 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000233 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000234 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000235 for (unsigned i=0; i < sectionCount; ++i) {
236 const section_64 *sect = &sects[i];
237 Section section;
238 section.segmentName = getString16(sect->segname);
239 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000240 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000241 & 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 Gouly010b3762014-01-14 22:32:38 +0000245 const uint8_t *content =
246 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000247 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 Kledzik6edd7222014-01-11 01:07:43 +0000250 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000251 appendRelocations(section.relocations, mb->getBuffer(),
252 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000253 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000254 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 Kledzike34182f2013-11-06 21:36:55 +0000261 f->sections.push_back(section);
262 }
263 }
264 } else {
265 if (cmd == LC_SEGMENT) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000266 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000267 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 Easwaran3d8de472014-01-27 03:09:26 +0000272 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000273 + sectionCount*sizeof(section);
274 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000275 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000276 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000277 for (unsigned i=0; i < sectionCount; ++i) {
278 const section *sect = &sects[i];
279 Section section;
280 section.segmentName = getString16(sect->segname);
281 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000282 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000283 & 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 Gouly010b3762014-01-14 22:32:38 +0000287 const uint8_t *content =
288 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000289 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 Kledzik6edd7222014-01-11 01:07:43 +0000292 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000293 appendRelocations(section.relocations, mb->getBuffer(),
294 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000295 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000296 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 Kledzike34182f2013-11-06 21:36:55 +0000303 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 Gouly010b3762014-01-14 22:32:38 +0000309 const char *strings = start + read32(swap, st->stroff);
Nick Kledzike34182f2013-11-06 21:36:55 +0000310 const uint32_t strSize = read32(swap, st->strsize);
311 // Validate string pool and symbol table all in buffer.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000312 if ( read32(swap, st->stroff)+read32(swap, st->strsize)
Joey Gouly010b3762014-01-14 22:32:38 +0000313 > objSize )
Rafael Espindola1d364c12014-06-03 04:41:30 +0000314 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000315 if (is64) {
316 const uint32_t symOffset = read32(swap, st->symoff);
317 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000318 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000319 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000320 const nlist_64 *symbols =
321 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000322 // 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 Easwaran3d8de472014-01-27 03:09:26 +0000330 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000331 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000332 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 Easwaran3d8de472014-01-27 03:09:26 +0000345 } else {
Nick Kledzike34182f2013-11-06 21:36:55 +0000346 const uint32_t symOffset = read32(swap, st->symoff);
347 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000348 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000349 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000350 const nlist *symbols =
351 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000352 // 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 Easwaran3d8de472014-01-27 03:09:26 +0000360 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000361 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000362 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 Kledzike34182f2013-11-06 21:36:55 +0000376 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000377 return false;
378 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000379 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000380 return ec;
381
382 return std::move(f);
383}
384
385
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000386
387class MachOReader : public Reader {
388public:
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 Espindolab1a4d3a2014-06-12 14:53:47 +0000412 std::error_code
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000413 parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000414 std::vector<std::unique_ptr<File>> &result) const override {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000415 // Convert binary file to normalized mach-o.
416 auto normFile = readBinary(mb, _arch);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000417 if (std::error_code ec = normFile.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000418 return ec;
419 // Convert normalized mach-o to atoms.
420 auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000421 if (std::error_code ec = file.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000422 return ec;
423
424 result.push_back(std::move(*file));
425
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000426 return std::error_code();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000427 }
428private:
429 MachOLinkingContext::Arch _arch;
430};
431
432
Nick Kledzike34182f2013-11-06 21:36:55 +0000433} // namespace normalized
434} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000435
436void Registry::addSupportMachOObjects(StringRef archName) {
437 MachOLinkingContext::Arch arch = MachOLinkingContext::archFromName(archName);
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000438 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000439 switch (arch) {
440 case MachOLinkingContext::arch_x86_64:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000441 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::x86_64,
Nick Kledzike5552772013-12-19 21:58:00 +0000442 mach_o::KindHandler_x86_64::kindStrings);
443 break;
444 case MachOLinkingContext::arch_x86:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000445 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::x86,
Nick Kledzike5552772013-12-19 21:58:00 +0000446 mach_o::KindHandler_x86::kindStrings);
447 break;
448 case MachOLinkingContext::arch_armv6:
449 case MachOLinkingContext::arch_armv7:
450 case MachOLinkingContext::arch_armv7s:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000451 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::ARM,
Nick Kledzike5552772013-12-19 21:58:00 +0000452 mach_o::KindHandler_arm::kindStrings);
453 break;
454 default:
455 llvm_unreachable("mach-o arch not supported");
456 }
Nick Kledzik6edd7222014-01-11 01:07:43 +0000457 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
458 new mach_o::MachOYamlIOTaggedDocumentHandler()));
Nick Kledzike5552772013-12-19 21:58:00 +0000459}
460
Nick Kledzike34182f2013-11-06 21:36:55 +0000461} // namespace lld
462