blob: 27162450d64006a713299180cd2fe441585ae4c6 [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"
Nick Kledzik2458bec2014-07-16 19:49:02 +000025
26#include "ArchHandler.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000027#include "MachONormalizedFileBinaryUtils.h"
Nick Kledzik2458bec2014-07-16 19:49:02 +000028
Nick Kledzike34182f2013-11-06 21:36:55 +000029#include "lld/Core/Error.h"
30#include "lld/Core/LLVM.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000031#include "llvm/ADT/SmallString.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/ADT/StringSwitch.h"
34#include "llvm/ADT/Twine.h"
35#include "llvm/Support/Casting.h"
Rafael Espindola372bc702014-06-13 17:20:48 +000036#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/FileOutputBuffer.h"
39#include "llvm/Support/Host.h"
40#include "llvm/Support/MachO.h"
41#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/Support/raw_ostream.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000043#include <functional>
Rafael Espindola54427cc2014-06-12 17:15:58 +000044#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000045
46using namespace llvm::MachO;
47
48namespace lld {
49namespace mach_o {
50namespace normalized {
51
52// Utility to call a lambda expression on each load command.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000053static std::error_code forEachLoadCommand(
54 StringRef lcRange, unsigned lcCount, bool swap, bool is64,
55 std::function<bool(uint32_t cmd, uint32_t size, const char *lc)> func) {
Nick Kledzike34182f2013-11-06 21:36:55 +000056 const char* p = lcRange.begin();
57 for (unsigned i=0; i < lcCount; ++i) {
58 const load_command *lc = reinterpret_cast<const load_command*>(p);
59 load_command lcCopy;
60 const load_command *slc = lc;
61 if (swap) {
62 memcpy(&lcCopy, lc, sizeof(load_command));
63 swapStruct(lcCopy);
64 slc = &lcCopy;
65 }
66 if ( (p + slc->cmdsize) > lcRange.end() )
Rafael Espindola372bc702014-06-13 17:20:48 +000067 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000068
Nick Kledzike34182f2013-11-06 21:36:55 +000069 if (func(slc->cmd, slc->cmdsize, p))
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000070 return std::error_code();
Shankar Easwaran3d8de472014-01-27 03:09:26 +000071
Nick Kledzike34182f2013-11-06 21:36:55 +000072 p += slc->cmdsize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +000073 }
74
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000075 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000076}
77
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000078static std::error_code appendRelocations(Relocations &relocs, StringRef buffer,
79 bool swap, bool bigEndian,
80 uint32_t reloff, uint32_t nreloc) {
Nick Kledzike34182f2013-11-06 21:36:55 +000081 if ((reloff + nreloc*8) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +000082 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000083 const any_relocation_info* relocsArray =
Joey Gouly010b3762014-01-14 22:32:38 +000084 reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000085
Nick Kledzike34182f2013-11-06 21:36:55 +000086 for(uint32_t i=0; i < nreloc; ++i) {
87 relocs.push_back(unpackRelocation(relocsArray[i], swap, bigEndian));
88 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000089 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000090}
91
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000092static std::error_code
Nick Kledzik388f3d02014-05-28 01:16:35 +000093appendIndirectSymbols(IndirectSymbols &isyms, StringRef buffer, bool swap,
94 bool bigEndian, uint32_t istOffset, uint32_t istCount,
95 uint32_t startIndex, uint32_t count) {
96 if ((istOffset + istCount*4) > buffer.size())
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 if (startIndex+count > istCount)
Rafael Espindola372bc702014-06-13 17:20:48 +000099 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000100 const uint32_t *indirectSymbolArray =
101 reinterpret_cast<const uint32_t*>(buffer.begin()+istOffset);
102
103 for(uint32_t i=0; i < count; ++i) {
104 isyms.push_back(read32(swap, indirectSymbolArray[startIndex+i]));
105 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000106 return std::error_code();
Nick Kledzik388f3d02014-05-28 01:16:35 +0000107}
108
109
Joey Gouly010b3762014-01-14 22:32:38 +0000110template <typename T> static T readBigEndian(T t) {
111 if (llvm::sys::IsLittleEndianHost)
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000112 llvm::sys::swapByteOrder(t);
Joey Gouly010b3762014-01-14 22:32:38 +0000113 return t;
114}
Nick Kledzike34182f2013-11-06 21:36:55 +0000115
116/// Reads a mach-o file and produces an in-memory normalized view.
Joey Gouly010b3762014-01-14 22:32:38 +0000117ErrorOr<std::unique_ptr<NormalizedFile>>
118readBinary(std::unique_ptr<MemoryBuffer> &mb,
119 const MachOLinkingContext::Arch arch) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000120 // Make empty NormalizedFile.
121 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
122
Joey Gouly010b3762014-01-14 22:32:38 +0000123 const char *start = mb->getBufferStart();
124 size_t objSize = mb->getBufferSize();
125
Nick Kledzike34182f2013-11-06 21:36:55 +0000126 // Determine endianness and pointer size for mach-o file.
Joey Gouly010b3762014-01-14 22:32:38 +0000127 const mach_header *mh = reinterpret_cast<const mach_header *>(start);
128 bool isFat = mh->magic == llvm::MachO::FAT_CIGAM ||
129 mh->magic == llvm::MachO::FAT_MAGIC;
130 if (isFat) {
131 uint32_t cputype = MachOLinkingContext::cpuTypeFromArch(arch);
132 uint32_t cpusubtype = MachOLinkingContext::cpuSubtypeFromArch(arch);
133 const fat_header *fh = reinterpret_cast<const fat_header *>(start);
134 uint32_t nfat_arch = readBigEndian(fh->nfat_arch);
135 const fat_arch *fa =
136 reinterpret_cast<const fat_arch *>(start + sizeof(fat_header));
137 bool foundArch = false;
138 while (nfat_arch-- > 0) {
139 if (readBigEndian(fa->cputype) == cputype &&
140 readBigEndian(fa->cpusubtype) == cpusubtype) {
141 foundArch = true;
142 break;
143 }
144 fa++;
145 }
146 if (!foundArch) {
Nick Kledzik378066c2014-06-30 22:57:33 +0000147 return make_dynamic_error_code(Twine("file does not contain required"
148 " architecture ("
149 + MachOLinkingContext::nameFromArch(arch)
150 + ")" ));
Joey Gouly010b3762014-01-14 22:32:38 +0000151 }
152 objSize = readBigEndian(fa->size);
153 uint32_t offset = readBigEndian(fa->offset);
154 if ((offset + objSize) > mb->getBufferSize())
Rafael Espindola372bc702014-06-13 17:20:48 +0000155 return make_error_code(llvm::errc::executable_format_error);
Joey Gouly010b3762014-01-14 22:32:38 +0000156 start += offset;
157 mh = reinterpret_cast<const mach_header *>(start);
158 }
159
Nick Kledzike34182f2013-11-06 21:36:55 +0000160 bool is64, swap;
161 switch (mh->magic) {
162 case llvm::MachO::MH_MAGIC:
163 is64 = false;
164 swap = false;
165 break;
166 case llvm::MachO::MH_MAGIC_64:
167 is64 = true;
168 swap = false;
169 break;
170 case llvm::MachO::MH_CIGAM:
171 is64 = false;
172 swap = true;
173 break;
174 case llvm::MachO::MH_CIGAM_64:
175 is64 = true;
176 swap = true;
177 break;
178 default:
Rafael Espindola372bc702014-06-13 17:20:48 +0000179 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000180 }
181
182 // Endian swap header, if needed.
183 mach_header headerCopy;
184 const mach_header *smh = mh;
185 if (swap) {
186 memcpy(&headerCopy, mh, sizeof(mach_header));
187 swapStruct(headerCopy);
188 smh = &headerCopy;
189 }
190
191 // Validate head and load commands fit in buffer.
192 const uint32_t lcCount = smh->ncmds;
Joey Gouly010b3762014-01-14 22:32:38 +0000193 const char *lcStart =
194 start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header));
Nick Kledzike34182f2013-11-06 21:36:55 +0000195 StringRef lcRange(lcStart, smh->sizeofcmds);
Joey Gouly010b3762014-01-14 22:32:38 +0000196 if (lcRange.end() > (start + objSize))
Rafael Espindola372bc702014-06-13 17:20:48 +0000197 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000198
Nick Kledzik378066c2014-06-30 22:57:33 +0000199 // Get architecture from mach_header.
Nick Kledzike34182f2013-11-06 21:36:55 +0000200 f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype);
Nick Kledzik378066c2014-06-30 22:57:33 +0000201 if (f->arch != arch) {
202 return make_dynamic_error_code(Twine("file is wrong architecture. Expected "
203 "(" + MachOLinkingContext::nameFromArch(arch)
204 + ") found ("
205 + MachOLinkingContext::nameFromArch(f->arch)
206 + ")" ));
207 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000208 bool isBigEndianArch = MachOLinkingContext::isBigEndian(f->arch);
209 // Copy file type and flags
210 f->fileType = HeaderFileType(smh->filetype);
211 f->flags = smh->flags;
212
213
Nick Kledzik388f3d02014-05-28 01:16:35 +0000214 // Pre-scan load commands looking for indirect symbol table.
215 uint32_t indirectSymbolTableOffset = 0;
216 uint32_t indirectSymbolTableCount = 0;
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000217 std::error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
218 [&](uint32_t cmd, uint32_t size,
219 const char *lc) -> bool {
Nick Kledzik388f3d02014-05-28 01:16:35 +0000220 if (cmd == LC_DYSYMTAB) {
221 const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
222 indirectSymbolTableOffset = read32(swap, d->indirectsymoff);
223 indirectSymbolTableCount = read32(swap, d->nindirectsyms);
224 return true;
225 }
226 return false;
227 });
228 if (ec)
229 return ec;
230
231 // Walk load commands looking for segments/sections and the symbol table.
Nick Kledzik21921372014-07-24 23:06:56 +0000232 const data_in_code_entry *dataInCode = nullptr;
233 uint32_t dataInCodeSize = 0;
Nick Kledzik388f3d02014-05-28 01:16:35 +0000234 ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
235 [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
Nick Kledzike34182f2013-11-06 21:36:55 +0000236 if (is64) {
237 if (cmd == LC_SEGMENT_64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000238 const segment_command_64 *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000239 reinterpret_cast<const segment_command_64*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000240 const unsigned sectionCount = (swap
241 ? llvm::sys::getSwappedBytes(seg->nsects)
242 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000243 const section_64 *sects = reinterpret_cast<const section_64*>
244 (lc + sizeof(segment_command_64));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000245 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000246 + sectionCount*sizeof(section_64);
247 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000248 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000249 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000250 for (unsigned i=0; i < sectionCount; ++i) {
251 const section_64 *sect = &sects[i];
252 Section section;
253 section.segmentName = getString16(sect->segname);
254 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000255 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000256 & SECTION_TYPE);
257 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
258 section.alignment = read32(swap, sect->align);
259 section.address = read64(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000260 const uint8_t *content =
261 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000262 size_t contentSize = read64(swap, sect->size);
263 // Note: this assign() is copying the content bytes. Ideally,
264 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000265 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000266 appendRelocations(section.relocations, mb->getBuffer(),
267 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000268 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000269 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
270 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
271 swap, isBigEndianArch,
272 indirectSymbolTableOffset,
273 indirectSymbolTableCount,
274 read32(swap, sect->reserved1), contentSize/4);
275 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000276 f->sections.push_back(section);
277 }
278 }
279 } else {
280 if (cmd == LC_SEGMENT) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000281 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000282 reinterpret_cast<const segment_command*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000283 const unsigned sectionCount = (swap
284 ? llvm::sys::getSwappedBytes(seg->nsects)
285 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000286 const section *sects = reinterpret_cast<const section*>
287 (lc + sizeof(segment_command));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000288 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000289 + sectionCount*sizeof(section);
290 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000291 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000292 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000293 for (unsigned i=0; i < sectionCount; ++i) {
294 const section *sect = &sects[i];
295 Section section;
296 section.segmentName = getString16(sect->segname);
297 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000298 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000299 & SECTION_TYPE);
300 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
301 section.alignment = read32(swap, sect->align);
302 section.address = read32(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000303 const uint8_t *content =
304 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000305 size_t contentSize = read32(swap, sect->size);
306 // Note: this assign() is copying the content bytes. Ideally,
307 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000308 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000309 appendRelocations(section.relocations, mb->getBuffer(),
310 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000311 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000312 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
313 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
314 swap, isBigEndianArch,
315 indirectSymbolTableOffset,
316 indirectSymbolTableCount,
317 read32(swap, sect->reserved1), contentSize/4);
318 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000319 f->sections.push_back(section);
320 }
321 }
322 }
323 if (cmd == LC_SYMTAB) {
324 const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
Joey Gouly010b3762014-01-14 22:32:38 +0000325 const char *strings = start + read32(swap, st->stroff);
Nick Kledzike34182f2013-11-06 21:36:55 +0000326 const uint32_t strSize = read32(swap, st->strsize);
327 // Validate string pool and symbol table all in buffer.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000328 if ( read32(swap, st->stroff)+read32(swap, st->strsize)
Joey Gouly010b3762014-01-14 22:32:38 +0000329 > objSize )
Rafael Espindola1d364c12014-06-03 04:41:30 +0000330 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000331 if (is64) {
332 const uint32_t symOffset = read32(swap, st->symoff);
333 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000334 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000335 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000336 const nlist_64 *symbols =
337 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000338 // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
339 for(uint32_t i=0; i < symCount; ++i) {
340 const nlist_64 *sin = &symbols[i];
341 nlist_64 tempSym;
342 if (swap) {
343 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
344 }
345 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000346 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000347 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000348 sout.name = &strings[sin->n_strx];
349 sout.type = (NListType)(sin->n_type & N_TYPE);
350 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
351 sout.sect = sin->n_sect;
352 sout.desc = sin->n_desc;
353 sout.value = sin->n_value;
354 if (sout.type == N_UNDF)
355 f->undefinedSymbols.push_back(sout);
Nick Kledzik3f690762014-06-27 18:25:01 +0000356 else if (sin->n_type & N_EXT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000357 f->globalSymbols.push_back(sout);
358 else
359 f->localSymbols.push_back(sout);
360 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000361 } else {
Nick Kledzike34182f2013-11-06 21:36:55 +0000362 const uint32_t symOffset = read32(swap, st->symoff);
363 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000364 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000365 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000366 const nlist *symbols =
367 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000368 // Convert each nlist to a lld::mach_o::normalized::Symbol.
369 for(uint32_t i=0; i < symCount; ++i) {
370 const nlist *sin = &symbols[i];
371 nlist tempSym;
372 if (swap) {
373 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
374 }
375 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000376 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000377 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000378 sout.name = &strings[sin->n_strx];
379 sout.type = (NListType)(sin->n_type & N_TYPE);
380 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
381 sout.sect = sin->n_sect;
382 sout.desc = sin->n_desc;
383 sout.value = sin->n_value;
384 if (sout.type == N_UNDF)
385 f->undefinedSymbols.push_back(sout);
386 else if (sout.scope == (SymbolScope)N_EXT)
387 f->globalSymbols.push_back(sout);
388 else
389 f->localSymbols.push_back(sout);
390 }
391 }
Nick Kledzik21921372014-07-24 23:06:56 +0000392 } else if (cmd == LC_ID_DYLIB) {
Tim Northover301c4e62014-07-01 08:15:41 +0000393 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
Nick Kledzik21921372014-07-24 23:06:56 +0000394 f->installName = lc + read32(swap, dl->dylib.name);
395 } else if (cmd == LC_DATA_IN_CODE) {
396 const linkedit_data_command *ldc =
397 reinterpret_cast<const linkedit_data_command*>(lc);
398 dataInCode = reinterpret_cast<const data_in_code_entry*>(
399 start + read32(swap, ldc->dataoff));
400 dataInCodeSize = read32(swap, ldc->datasize);
Tim Northover301c4e62014-07-01 08:15:41 +0000401 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000402 return false;
403 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000404 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000405 return ec;
406
Nick Kledzik21921372014-07-24 23:06:56 +0000407 if (dataInCode) {
408 // Convert on-disk data_in_code_entry array to DataInCode vector.
409 for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); ++i) {
410 DataInCode entry;
411 entry.offset = read32(swap, dataInCode[i].offset);
412 entry.length = read16(swap, dataInCode[i].length);
413 entry.kind = (DataRegionType)read16(swap, dataInCode[i].kind);
414 f->dataInCode.push_back(entry);
415 }
416 }
417
Nick Kledzike34182f2013-11-06 21:36:55 +0000418 return std::move(f);
419}
420
421
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000422
423class MachOReader : public Reader {
424public:
425 MachOReader(MachOLinkingContext::Arch arch) : _arch(arch) {}
426
427 bool canParse(file_magic magic, StringRef ext,
428 const MemoryBuffer &mb) const override {
Tim Northoverf9b13d62014-06-30 09:11:38 +0000429 if (magic != llvm::sys::fs::file_magic::macho_object &&
Nick Kledzik378066c2014-06-30 22:57:33 +0000430 magic != llvm::sys::fs::file_magic::macho_universal_binary &&
Tim Northoverf9b13d62014-06-30 09:11:38 +0000431 magic != llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib)
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000432 return false;
Nick Kledzik378066c2014-06-30 22:57:33 +0000433 return (mb.getBufferSize() > 32);
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000434 }
435
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000436 std::error_code
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000437 parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000438 std::vector<std::unique_ptr<File>> &result) const override {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000439 // Convert binary file to normalized mach-o.
440 auto normFile = readBinary(mb, _arch);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000441 if (std::error_code ec = normFile.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000442 return ec;
443 // Convert normalized mach-o to atoms.
444 auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000445 if (std::error_code ec = file.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000446 return ec;
447
448 result.push_back(std::move(*file));
449
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000450 return std::error_code();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000451 }
452private:
453 MachOLinkingContext::Arch _arch;
454};
455
456
Nick Kledzike34182f2013-11-06 21:36:55 +0000457} // namespace normalized
458} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000459
Nick Kledzik2458bec2014-07-16 19:49:02 +0000460void Registry::addSupportMachOObjects(const MachOLinkingContext &ctx) {
461 MachOLinkingContext::Arch arch = ctx.arch();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000462 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(arch)));
Nick Kledzik2458bec2014-07-16 19:49:02 +0000463 addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(),
464 ctx.archHandler().kindStrings());
Nick Kledzik6edd7222014-01-11 01:07:43 +0000465 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
Nick Kledzik378066c2014-06-30 22:57:33 +0000466 new mach_o::MachOYamlIOTaggedDocumentHandler(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000467}
468
Nick Kledzike34182f2013-11-06 21:36:55 +0000469} // namespace lld
470