blob: fc43141ca248ff18c59f14d41c4bf637a58aa66f [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 Kledzik8fc67fb2014-08-13 23:55:41 +000031#include "lld/Core/SharedLibraryFile.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000032#include "llvm/ADT/SmallString.h"
33#include "llvm/ADT/StringRef.h"
34#include "llvm/ADT/StringSwitch.h"
35#include "llvm/ADT/Twine.h"
36#include "llvm/Support/Casting.h"
Rafael Espindola372bc702014-06-13 17:20:48 +000037#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000038#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/FileOutputBuffer.h"
40#include "llvm/Support/Host.h"
41#include "llvm/Support/MachO.h"
42#include "llvm/Support/MemoryBuffer.h"
43#include "llvm/Support/raw_ostream.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000044#include <functional>
Rafael Espindola54427cc2014-06-12 17:15:58 +000045#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000046
47using namespace llvm::MachO;
48
49namespace lld {
50namespace mach_o {
51namespace normalized {
52
53// Utility to call a lambda expression on each load command.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000054static std::error_code forEachLoadCommand(
55 StringRef lcRange, unsigned lcCount, bool swap, bool is64,
56 std::function<bool(uint32_t cmd, uint32_t size, const char *lc)> func) {
Nick Kledzike34182f2013-11-06 21:36:55 +000057 const char* p = lcRange.begin();
58 for (unsigned i=0; i < lcCount; ++i) {
59 const load_command *lc = reinterpret_cast<const load_command*>(p);
60 load_command lcCopy;
61 const load_command *slc = lc;
62 if (swap) {
63 memcpy(&lcCopy, lc, sizeof(load_command));
64 swapStruct(lcCopy);
65 slc = &lcCopy;
66 }
67 if ( (p + slc->cmdsize) > lcRange.end() )
Rafael Espindola372bc702014-06-13 17:20:48 +000068 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000069
Nick Kledzike34182f2013-11-06 21:36:55 +000070 if (func(slc->cmd, slc->cmdsize, p))
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000071 return std::error_code();
Shankar Easwaran3d8de472014-01-27 03:09:26 +000072
Nick Kledzike34182f2013-11-06 21:36:55 +000073 p += slc->cmdsize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +000074 }
75
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000076 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000077}
78
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000079static std::error_code appendRelocations(Relocations &relocs, StringRef buffer,
80 bool swap, bool bigEndian,
81 uint32_t reloff, uint32_t nreloc) {
Nick Kledzike34182f2013-11-06 21:36:55 +000082 if ((reloff + nreloc*8) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +000083 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000084 const any_relocation_info* relocsArray =
Joey Gouly010b3762014-01-14 22:32:38 +000085 reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000086
Nick Kledzike34182f2013-11-06 21:36:55 +000087 for(uint32_t i=0; i < nreloc; ++i) {
88 relocs.push_back(unpackRelocation(relocsArray[i], swap, bigEndian));
89 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000090 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000091}
92
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000093static std::error_code
Nick Kledzik388f3d02014-05-28 01:16:35 +000094appendIndirectSymbols(IndirectSymbols &isyms, StringRef buffer, bool swap,
95 bool bigEndian, uint32_t istOffset, uint32_t istCount,
96 uint32_t startIndex, uint32_t count) {
97 if ((istOffset + istCount*4) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +000098 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzik388f3d02014-05-28 01:16:35 +000099 if (startIndex+count > istCount)
Rafael Espindola372bc702014-06-13 17:20:48 +0000100 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000101 const uint32_t *indirectSymbolArray =
102 reinterpret_cast<const uint32_t*>(buffer.begin()+istOffset);
103
104 for(uint32_t i=0; i < count; ++i) {
105 isyms.push_back(read32(swap, indirectSymbolArray[startIndex+i]));
106 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000107 return std::error_code();
Nick Kledzik388f3d02014-05-28 01:16:35 +0000108}
109
110
Joey Gouly010b3762014-01-14 22:32:38 +0000111template <typename T> static T readBigEndian(T t) {
112 if (llvm::sys::IsLittleEndianHost)
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000113 llvm::sys::swapByteOrder(t);
Joey Gouly010b3762014-01-14 22:32:38 +0000114 return t;
115}
Nick Kledzike34182f2013-11-06 21:36:55 +0000116
117/// Reads a mach-o file and produces an in-memory normalized view.
Joey Gouly010b3762014-01-14 22:32:38 +0000118ErrorOr<std::unique_ptr<NormalizedFile>>
119readBinary(std::unique_ptr<MemoryBuffer> &mb,
120 const MachOLinkingContext::Arch arch) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000121 // Make empty NormalizedFile.
122 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
123
Joey Gouly010b3762014-01-14 22:32:38 +0000124 const char *start = mb->getBufferStart();
125 size_t objSize = mb->getBufferSize();
126
Nick Kledzike34182f2013-11-06 21:36:55 +0000127 // Determine endianness and pointer size for mach-o file.
Joey Gouly010b3762014-01-14 22:32:38 +0000128 const mach_header *mh = reinterpret_cast<const mach_header *>(start);
129 bool isFat = mh->magic == llvm::MachO::FAT_CIGAM ||
130 mh->magic == llvm::MachO::FAT_MAGIC;
131 if (isFat) {
132 uint32_t cputype = MachOLinkingContext::cpuTypeFromArch(arch);
133 uint32_t cpusubtype = MachOLinkingContext::cpuSubtypeFromArch(arch);
134 const fat_header *fh = reinterpret_cast<const fat_header *>(start);
135 uint32_t nfat_arch = readBigEndian(fh->nfat_arch);
136 const fat_arch *fa =
137 reinterpret_cast<const fat_arch *>(start + sizeof(fat_header));
138 bool foundArch = false;
139 while (nfat_arch-- > 0) {
140 if (readBigEndian(fa->cputype) == cputype &&
141 readBigEndian(fa->cpusubtype) == cpusubtype) {
142 foundArch = true;
143 break;
144 }
145 fa++;
146 }
147 if (!foundArch) {
Nick Kledzik378066c2014-06-30 22:57:33 +0000148 return make_dynamic_error_code(Twine("file does not contain required"
149 " architecture ("
150 + MachOLinkingContext::nameFromArch(arch)
151 + ")" ));
Joey Gouly010b3762014-01-14 22:32:38 +0000152 }
153 objSize = readBigEndian(fa->size);
154 uint32_t offset = readBigEndian(fa->offset);
155 if ((offset + objSize) > mb->getBufferSize())
Rafael Espindola372bc702014-06-13 17:20:48 +0000156 return make_error_code(llvm::errc::executable_format_error);
Joey Gouly010b3762014-01-14 22:32:38 +0000157 start += offset;
158 mh = reinterpret_cast<const mach_header *>(start);
159 }
160
Nick Kledzike34182f2013-11-06 21:36:55 +0000161 bool is64, swap;
162 switch (mh->magic) {
163 case llvm::MachO::MH_MAGIC:
164 is64 = false;
165 swap = false;
166 break;
167 case llvm::MachO::MH_MAGIC_64:
168 is64 = true;
169 swap = false;
170 break;
171 case llvm::MachO::MH_CIGAM:
172 is64 = false;
173 swap = true;
174 break;
175 case llvm::MachO::MH_CIGAM_64:
176 is64 = true;
177 swap = true;
178 break;
179 default:
Rafael Espindola372bc702014-06-13 17:20:48 +0000180 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000181 }
182
183 // Endian swap header, if needed.
184 mach_header headerCopy;
185 const mach_header *smh = mh;
186 if (swap) {
187 memcpy(&headerCopy, mh, sizeof(mach_header));
188 swapStruct(headerCopy);
189 smh = &headerCopy;
190 }
191
192 // Validate head and load commands fit in buffer.
193 const uint32_t lcCount = smh->ncmds;
Joey Gouly010b3762014-01-14 22:32:38 +0000194 const char *lcStart =
195 start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header));
Nick Kledzike34182f2013-11-06 21:36:55 +0000196 StringRef lcRange(lcStart, smh->sizeofcmds);
Joey Gouly010b3762014-01-14 22:32:38 +0000197 if (lcRange.end() > (start + objSize))
Rafael Espindola372bc702014-06-13 17:20:48 +0000198 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000199
Nick Kledzik378066c2014-06-30 22:57:33 +0000200 // Get architecture from mach_header.
Nick Kledzike34182f2013-11-06 21:36:55 +0000201 f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype);
Nick Kledzik378066c2014-06-30 22:57:33 +0000202 if (f->arch != arch) {
203 return make_dynamic_error_code(Twine("file is wrong architecture. Expected "
204 "(" + MachOLinkingContext::nameFromArch(arch)
205 + ") found ("
206 + MachOLinkingContext::nameFromArch(f->arch)
207 + ")" ));
208 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000209 bool isBigEndianArch = MachOLinkingContext::isBigEndian(f->arch);
210 // Copy file type and flags
211 f->fileType = HeaderFileType(smh->filetype);
212 f->flags = smh->flags;
213
214
Nick Kledzik388f3d02014-05-28 01:16:35 +0000215 // Pre-scan load commands looking for indirect symbol table.
216 uint32_t indirectSymbolTableOffset = 0;
217 uint32_t indirectSymbolTableCount = 0;
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000218 std::error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
219 [&](uint32_t cmd, uint32_t size,
220 const char *lc) -> bool {
Nick Kledzik388f3d02014-05-28 01:16:35 +0000221 if (cmd == LC_DYSYMTAB) {
222 const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
223 indirectSymbolTableOffset = read32(swap, d->indirectsymoff);
224 indirectSymbolTableCount = read32(swap, d->nindirectsyms);
225 return true;
226 }
227 return false;
228 });
229 if (ec)
230 return ec;
231
232 // Walk load commands looking for segments/sections and the symbol table.
Nick Kledzik21921372014-07-24 23:06:56 +0000233 const data_in_code_entry *dataInCode = nullptr;
234 uint32_t dataInCodeSize = 0;
Nick Kledzik388f3d02014-05-28 01:16:35 +0000235 ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
236 [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000237 switch(cmd) {
238 case LC_SEGMENT_64:
239 if (is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000240 const segment_command_64 *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000241 reinterpret_cast<const segment_command_64*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000242 const unsigned sectionCount = (swap
243 ? llvm::sys::getSwappedBytes(seg->nsects)
244 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000245 const section_64 *sects = reinterpret_cast<const section_64*>
246 (lc + sizeof(segment_command_64));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000247 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000248 + sectionCount*sizeof(section_64);
249 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000250 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000251 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000252 for (unsigned i=0; i < sectionCount; ++i) {
253 const section_64 *sect = &sects[i];
254 Section section;
255 section.segmentName = getString16(sect->segname);
256 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000257 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000258 & SECTION_TYPE);
259 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
260 section.alignment = read32(swap, sect->align);
261 section.address = read64(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000262 const uint8_t *content =
263 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000264 size_t contentSize = read64(swap, sect->size);
265 // Note: this assign() is copying the content bytes. Ideally,
266 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000267 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000268 appendRelocations(section.relocations, mb->getBuffer(),
269 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000270 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000271 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
272 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
273 swap, isBigEndianArch,
274 indirectSymbolTableOffset,
275 indirectSymbolTableCount,
276 read32(swap, sect->reserved1), contentSize/4);
277 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000278 f->sections.push_back(section);
279 }
280 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000281 break;
282 case LC_SEGMENT:
283 if (!is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000284 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000285 reinterpret_cast<const segment_command*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000286 const unsigned sectionCount = (swap
287 ? llvm::sys::getSwappedBytes(seg->nsects)
288 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000289 const section *sects = reinterpret_cast<const section*>
290 (lc + sizeof(segment_command));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000291 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000292 + sectionCount*sizeof(section);
293 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000294 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000295 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000296 for (unsigned i=0; i < sectionCount; ++i) {
297 const section *sect = &sects[i];
298 Section section;
299 section.segmentName = getString16(sect->segname);
300 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000301 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000302 & SECTION_TYPE);
303 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
304 section.alignment = read32(swap, sect->align);
305 section.address = read32(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000306 const uint8_t *content =
307 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000308 size_t contentSize = read32(swap, sect->size);
309 // Note: this assign() is copying the content bytes. Ideally,
310 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000311 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000312 appendRelocations(section.relocations, mb->getBuffer(),
313 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000314 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000315 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
316 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
317 swap, isBigEndianArch,
318 indirectSymbolTableOffset,
319 indirectSymbolTableCount,
320 read32(swap, sect->reserved1), contentSize/4);
321 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000322 f->sections.push_back(section);
323 }
324 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000325 break;
326 case LC_SYMTAB: {
Nick Kledzike34182f2013-11-06 21:36:55 +0000327 const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
Joey Gouly010b3762014-01-14 22:32:38 +0000328 const char *strings = start + read32(swap, st->stroff);
Nick Kledzike34182f2013-11-06 21:36:55 +0000329 const uint32_t strSize = read32(swap, st->strsize);
330 // Validate string pool and symbol table all in buffer.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000331 if ( read32(swap, st->stroff)+read32(swap, st->strsize)
Joey Gouly010b3762014-01-14 22:32:38 +0000332 > objSize )
Rafael Espindola1d364c12014-06-03 04:41:30 +0000333 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000334 if (is64) {
335 const uint32_t symOffset = read32(swap, st->symoff);
336 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000337 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000338 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000339 const nlist_64 *symbols =
340 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000341 // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
342 for(uint32_t i=0; i < symCount; ++i) {
343 const nlist_64 *sin = &symbols[i];
344 nlist_64 tempSym;
345 if (swap) {
346 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
347 }
348 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000349 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000350 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000351 sout.name = &strings[sin->n_strx];
352 sout.type = (NListType)(sin->n_type & N_TYPE);
353 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
354 sout.sect = sin->n_sect;
355 sout.desc = sin->n_desc;
356 sout.value = sin->n_value;
357 if (sout.type == N_UNDF)
358 f->undefinedSymbols.push_back(sout);
Nick Kledzik3f690762014-06-27 18:25:01 +0000359 else if (sin->n_type & N_EXT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000360 f->globalSymbols.push_back(sout);
361 else
362 f->localSymbols.push_back(sout);
363 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000364 } else {
Nick Kledzike34182f2013-11-06 21:36:55 +0000365 const uint32_t symOffset = read32(swap, st->symoff);
366 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000367 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000368 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000369 const nlist *symbols =
370 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000371 // Convert each nlist to a lld::mach_o::normalized::Symbol.
372 for(uint32_t i=0; i < symCount; ++i) {
373 const nlist *sin = &symbols[i];
374 nlist tempSym;
375 if (swap) {
376 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
377 }
378 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000379 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000380 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000381 sout.name = &strings[sin->n_strx];
382 sout.type = (NListType)(sin->n_type & N_TYPE);
383 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
384 sout.sect = sin->n_sect;
385 sout.desc = sin->n_desc;
386 sout.value = sin->n_value;
387 if (sout.type == N_UNDF)
388 f->undefinedSymbols.push_back(sout);
389 else if (sout.scope == (SymbolScope)N_EXT)
390 f->globalSymbols.push_back(sout);
391 else
392 f->localSymbols.push_back(sout);
393 }
394 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000395 }
396 break;
397 case LC_ID_DYLIB: {
Tim Northover301c4e62014-07-01 08:15:41 +0000398 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
Nick Kledzik21921372014-07-24 23:06:56 +0000399 f->installName = lc + read32(swap, dl->dylib.name);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000400 }
401 break;
402 case LC_DATA_IN_CODE: {
Nick Kledzik21921372014-07-24 23:06:56 +0000403 const linkedit_data_command *ldc =
404 reinterpret_cast<const linkedit_data_command*>(lc);
405 dataInCode = reinterpret_cast<const data_in_code_entry*>(
406 start + read32(swap, ldc->dataoff));
407 dataInCodeSize = read32(swap, ldc->datasize);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000408 }
409 case LC_LOAD_DYLIB:
410 case LC_LOAD_WEAK_DYLIB:
411 case LC_REEXPORT_DYLIB:
412 case LC_LOAD_UPWARD_DYLIB: {
413 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
414 DependentDylib entry;
415 entry.path = lc + read32(swap, dl->dylib.name);
416 entry.kind = LoadCommandType(cmd);
417 f->dependentDylibs.push_back(entry);
418 }
419 break;
Tim Northover301c4e62014-07-01 08:15:41 +0000420 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000421 return false;
422 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000423 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000424 return ec;
425
Nick Kledzik21921372014-07-24 23:06:56 +0000426 if (dataInCode) {
427 // Convert on-disk data_in_code_entry array to DataInCode vector.
428 for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); ++i) {
429 DataInCode entry;
430 entry.offset = read32(swap, dataInCode[i].offset);
431 entry.length = read16(swap, dataInCode[i].length);
432 entry.kind = (DataRegionType)read16(swap, dataInCode[i].kind);
433 f->dataInCode.push_back(entry);
434 }
435 }
436
Nick Kledzike34182f2013-11-06 21:36:55 +0000437 return std::move(f);
438}
439
440
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000441
442class MachOReader : public Reader {
443public:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000444 MachOReader(MachOLinkingContext &ctx) : _ctx(ctx) {}
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000445
446 bool canParse(file_magic magic, StringRef ext,
447 const MemoryBuffer &mb) const override {
Tim Northoverf9b13d62014-06-30 09:11:38 +0000448 if (magic != llvm::sys::fs::file_magic::macho_object &&
Nick Kledzik378066c2014-06-30 22:57:33 +0000449 magic != llvm::sys::fs::file_magic::macho_universal_binary &&
Tim Northoverf9b13d62014-06-30 09:11:38 +0000450 magic != llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib)
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000451 return false;
Nick Kledzik378066c2014-06-30 22:57:33 +0000452 return (mb.getBufferSize() > 32);
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000453 }
454
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000455 std::error_code
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000456 parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000457 std::vector<std::unique_ptr<File>> &result) const override {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000458 // Convert binary file to normalized mach-o.
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000459 auto normFile = readBinary(mb, _ctx.arch());
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000460 if (std::error_code ec = normFile.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000461 return ec;
462 // Convert normalized mach-o to atoms.
463 auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000464 if (std::error_code ec = file.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000465 return ec;
466
467 result.push_back(std::move(*file));
468
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000469 return std::error_code();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000470 }
471private:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000472 MachOLinkingContext &_ctx;
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000473};
474
475
Nick Kledzike34182f2013-11-06 21:36:55 +0000476} // namespace normalized
477} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000478
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000479void Registry::addSupportMachOObjects(MachOLinkingContext &ctx) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000480 MachOLinkingContext::Arch arch = ctx.arch();
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000481 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(ctx)));
Nick Kledzik2458bec2014-07-16 19:49:02 +0000482 addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(),
483 ctx.archHandler().kindStrings());
Nick Kledzik6edd7222014-01-11 01:07:43 +0000484 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
Nick Kledzik378066c2014-06-30 22:57:33 +0000485 new mach_o::MachOYamlIOTaggedDocumentHandler(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000486}
487
Nick Kledzike34182f2013-11-06 21:36:55 +0000488} // namespace lld
489