blob: 7c599152e8be50f52c1608a18b5629a8b8c92eea [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) {
Nick Kledzik378066c2014-06-30 22:57:33 +0000145 return make_dynamic_error_code(Twine("file does not contain required"
146 " architecture ("
147 + MachOLinkingContext::nameFromArch(arch)
148 + ")" ));
Joey Gouly010b3762014-01-14 22:32:38 +0000149 }
150 objSize = readBigEndian(fa->size);
151 uint32_t offset = readBigEndian(fa->offset);
152 if ((offset + objSize) > mb->getBufferSize())
Rafael Espindola372bc702014-06-13 17:20:48 +0000153 return make_error_code(llvm::errc::executable_format_error);
Joey Gouly010b3762014-01-14 22:32:38 +0000154 start += offset;
155 mh = reinterpret_cast<const mach_header *>(start);
156 }
157
Nick Kledzike34182f2013-11-06 21:36:55 +0000158 bool is64, swap;
159 switch (mh->magic) {
160 case llvm::MachO::MH_MAGIC:
161 is64 = false;
162 swap = false;
163 break;
164 case llvm::MachO::MH_MAGIC_64:
165 is64 = true;
166 swap = false;
167 break;
168 case llvm::MachO::MH_CIGAM:
169 is64 = false;
170 swap = true;
171 break;
172 case llvm::MachO::MH_CIGAM_64:
173 is64 = true;
174 swap = true;
175 break;
176 default:
Rafael Espindola372bc702014-06-13 17:20:48 +0000177 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000178 }
179
180 // Endian swap header, if needed.
181 mach_header headerCopy;
182 const mach_header *smh = mh;
183 if (swap) {
184 memcpy(&headerCopy, mh, sizeof(mach_header));
185 swapStruct(headerCopy);
186 smh = &headerCopy;
187 }
188
189 // Validate head and load commands fit in buffer.
190 const uint32_t lcCount = smh->ncmds;
Joey Gouly010b3762014-01-14 22:32:38 +0000191 const char *lcStart =
192 start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header));
Nick Kledzike34182f2013-11-06 21:36:55 +0000193 StringRef lcRange(lcStart, smh->sizeofcmds);
Joey Gouly010b3762014-01-14 22:32:38 +0000194 if (lcRange.end() > (start + objSize))
Rafael Espindola372bc702014-06-13 17:20:48 +0000195 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000196
Nick Kledzik378066c2014-06-30 22:57:33 +0000197 // Get architecture from mach_header.
Nick Kledzike34182f2013-11-06 21:36:55 +0000198 f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype);
Nick Kledzik378066c2014-06-30 22:57:33 +0000199 if (f->arch != arch) {
200 return make_dynamic_error_code(Twine("file is wrong architecture. Expected "
201 "(" + MachOLinkingContext::nameFromArch(arch)
202 + ") found ("
203 + MachOLinkingContext::nameFromArch(f->arch)
204 + ")" ));
205 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000206 bool isBigEndianArch = MachOLinkingContext::isBigEndian(f->arch);
207 // Copy file type and flags
208 f->fileType = HeaderFileType(smh->filetype);
209 f->flags = smh->flags;
210
211
Nick Kledzik388f3d02014-05-28 01:16:35 +0000212 // Pre-scan load commands looking for indirect symbol table.
213 uint32_t indirectSymbolTableOffset = 0;
214 uint32_t indirectSymbolTableCount = 0;
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000215 std::error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
216 [&](uint32_t cmd, uint32_t size,
217 const char *lc) -> bool {
Nick Kledzik388f3d02014-05-28 01:16:35 +0000218 if (cmd == LC_DYSYMTAB) {
219 const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
220 indirectSymbolTableOffset = read32(swap, d->indirectsymoff);
221 indirectSymbolTableCount = read32(swap, d->nindirectsyms);
222 return true;
223 }
224 return false;
225 });
226 if (ec)
227 return ec;
228
229 // Walk load commands looking for segments/sections and the symbol table.
230 ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
231 [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
Nick Kledzike34182f2013-11-06 21:36:55 +0000232 if (is64) {
233 if (cmd == LC_SEGMENT_64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000234 const segment_command_64 *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000235 reinterpret_cast<const segment_command_64*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000236 const unsigned sectionCount = (swap
237 ? llvm::sys::getSwappedBytes(seg->nsects)
238 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000239 const section_64 *sects = reinterpret_cast<const section_64*>
240 (lc + sizeof(segment_command_64));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000241 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000242 + sectionCount*sizeof(section_64);
243 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000244 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000245 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000246 for (unsigned i=0; i < sectionCount; ++i) {
247 const section_64 *sect = &sects[i];
248 Section section;
249 section.segmentName = getString16(sect->segname);
250 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000251 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000252 & SECTION_TYPE);
253 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
254 section.alignment = read32(swap, sect->align);
255 section.address = read64(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000256 const uint8_t *content =
257 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000258 size_t contentSize = read64(swap, sect->size);
259 // Note: this assign() is copying the content bytes. Ideally,
260 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000261 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000262 appendRelocations(section.relocations, mb->getBuffer(),
263 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000264 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000265 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
266 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
267 swap, isBigEndianArch,
268 indirectSymbolTableOffset,
269 indirectSymbolTableCount,
270 read32(swap, sect->reserved1), contentSize/4);
271 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000272 f->sections.push_back(section);
273 }
274 }
275 } else {
276 if (cmd == LC_SEGMENT) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000277 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000278 reinterpret_cast<const segment_command*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000279 const unsigned sectionCount = (swap
280 ? llvm::sys::getSwappedBytes(seg->nsects)
281 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000282 const section *sects = reinterpret_cast<const section*>
283 (lc + sizeof(segment_command));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000284 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000285 + sectionCount*sizeof(section);
286 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000287 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000288 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000289 for (unsigned i=0; i < sectionCount; ++i) {
290 const section *sect = &sects[i];
291 Section section;
292 section.segmentName = getString16(sect->segname);
293 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000294 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000295 & SECTION_TYPE);
296 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
297 section.alignment = read32(swap, sect->align);
298 section.address = read32(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000299 const uint8_t *content =
300 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000301 size_t contentSize = read32(swap, sect->size);
302 // Note: this assign() is copying the content bytes. Ideally,
303 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000304 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000305 appendRelocations(section.relocations, mb->getBuffer(),
306 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000307 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000308 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
309 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
310 swap, isBigEndianArch,
311 indirectSymbolTableOffset,
312 indirectSymbolTableCount,
313 read32(swap, sect->reserved1), contentSize/4);
314 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000315 f->sections.push_back(section);
316 }
317 }
318 }
319 if (cmd == LC_SYMTAB) {
320 const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
Joey Gouly010b3762014-01-14 22:32:38 +0000321 const char *strings = start + read32(swap, st->stroff);
Nick Kledzike34182f2013-11-06 21:36:55 +0000322 const uint32_t strSize = read32(swap, st->strsize);
323 // Validate string pool and symbol table all in buffer.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000324 if ( read32(swap, st->stroff)+read32(swap, st->strsize)
Joey Gouly010b3762014-01-14 22:32:38 +0000325 > objSize )
Rafael Espindola1d364c12014-06-03 04:41:30 +0000326 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000327 if (is64) {
328 const uint32_t symOffset = read32(swap, st->symoff);
329 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000330 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000331 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000332 const nlist_64 *symbols =
333 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000334 // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
335 for(uint32_t i=0; i < symCount; ++i) {
336 const nlist_64 *sin = &symbols[i];
337 nlist_64 tempSym;
338 if (swap) {
339 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
340 }
341 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000342 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000343 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000344 sout.name = &strings[sin->n_strx];
345 sout.type = (NListType)(sin->n_type & N_TYPE);
346 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
347 sout.sect = sin->n_sect;
348 sout.desc = sin->n_desc;
349 sout.value = sin->n_value;
350 if (sout.type == N_UNDF)
351 f->undefinedSymbols.push_back(sout);
Nick Kledzik3f690762014-06-27 18:25:01 +0000352 else if (sin->n_type & N_EXT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000353 f->globalSymbols.push_back(sout);
354 else
355 f->localSymbols.push_back(sout);
356 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000357 } else {
Nick Kledzike34182f2013-11-06 21:36:55 +0000358 const uint32_t symOffset = read32(swap, st->symoff);
359 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000360 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000361 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000362 const nlist *symbols =
363 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000364 // Convert each nlist to a lld::mach_o::normalized::Symbol.
365 for(uint32_t i=0; i < symCount; ++i) {
366 const nlist *sin = &symbols[i];
367 nlist tempSym;
368 if (swap) {
369 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
370 }
371 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000372 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000373 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000374 sout.name = &strings[sin->n_strx];
375 sout.type = (NListType)(sin->n_type & N_TYPE);
376 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
377 sout.sect = sin->n_sect;
378 sout.desc = sin->n_desc;
379 sout.value = sin->n_value;
380 if (sout.type == N_UNDF)
381 f->undefinedSymbols.push_back(sout);
382 else if (sout.scope == (SymbolScope)N_EXT)
383 f->globalSymbols.push_back(sout);
384 else
385 f->localSymbols.push_back(sout);
386 }
387 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000388 }
Tim Northover301c4e62014-07-01 08:15:41 +0000389 if (cmd == LC_ID_DYLIB) {
390 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
391 dylib_command tempDL;
392 if (swap) {
393 tempDL = *dl; swapStruct(tempDL); dl = &tempDL;
394 }
395
396 f->installName = lc + dl->dylib.name;
397 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000398 return false;
399 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000400 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000401 return ec;
402
403 return std::move(f);
404}
405
406
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000407
408class MachOReader : public Reader {
409public:
410 MachOReader(MachOLinkingContext::Arch arch) : _arch(arch) {}
411
412 bool canParse(file_magic magic, StringRef ext,
413 const MemoryBuffer &mb) const override {
Tim Northoverf9b13d62014-06-30 09:11:38 +0000414 if (magic != llvm::sys::fs::file_magic::macho_object &&
Nick Kledzik378066c2014-06-30 22:57:33 +0000415 magic != llvm::sys::fs::file_magic::macho_universal_binary &&
Tim Northoverf9b13d62014-06-30 09:11:38 +0000416 magic != llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib)
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000417 return false;
Nick Kledzik378066c2014-06-30 22:57:33 +0000418 return (mb.getBufferSize() > 32);
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000419 }
420
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000421 std::error_code
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000422 parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000423 std::vector<std::unique_ptr<File>> &result) const override {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000424 // Convert binary file to normalized mach-o.
425 auto normFile = readBinary(mb, _arch);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000426 if (std::error_code ec = normFile.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000427 return ec;
428 // Convert normalized mach-o to atoms.
429 auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000430 if (std::error_code ec = file.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000431 return ec;
432
433 result.push_back(std::move(*file));
434
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000435 return std::error_code();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000436 }
437private:
438 MachOLinkingContext::Arch _arch;
439};
440
441
Nick Kledzike34182f2013-11-06 21:36:55 +0000442} // namespace normalized
443} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000444
445void Registry::addSupportMachOObjects(StringRef archName) {
446 MachOLinkingContext::Arch arch = MachOLinkingContext::archFromName(archName);
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000447 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000448 switch (arch) {
449 case MachOLinkingContext::arch_x86_64:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000450 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::x86_64,
Nick Kledzike5552772013-12-19 21:58:00 +0000451 mach_o::KindHandler_x86_64::kindStrings);
452 break;
453 case MachOLinkingContext::arch_x86:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000454 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::x86,
Nick Kledzike5552772013-12-19 21:58:00 +0000455 mach_o::KindHandler_x86::kindStrings);
456 break;
457 case MachOLinkingContext::arch_armv6:
458 case MachOLinkingContext::arch_armv7:
459 case MachOLinkingContext::arch_armv7s:
Rui Ueyama170a1a82013-12-20 07:48:29 +0000460 addKindTable(Reference::KindNamespace::mach_o, Reference::KindArch::ARM,
Nick Kledzike5552772013-12-19 21:58:00 +0000461 mach_o::KindHandler_arm::kindStrings);
462 break;
463 default:
464 llvm_unreachable("mach-o arch not supported");
465 }
Nick Kledzik6edd7222014-01-11 01:07:43 +0000466 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
Nick Kledzik378066c2014-06-30 22:57:33 +0000467 new mach_o::MachOYamlIOTaggedDocumentHandler(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000468}
469
Nick Kledzike34182f2013-11-06 21:36:55 +0000470} // namespace lld
471