blob: 6fcee3bf059ce837504c9e5890c14d0c6586c718 [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"
Nick Kledzik141330a2014-09-03 19:52:50 +000036#include "llvm/Object/MachO.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000037#include "llvm/Support/Casting.h"
Rafael Espindola372bc702014-06-13 17:20:48 +000038#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000039#include "llvm/Support/ErrorHandling.h"
40#include "llvm/Support/FileOutputBuffer.h"
41#include "llvm/Support/Host.h"
42#include "llvm/Support/MachO.h"
43#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/raw_ostream.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000045#include <functional>
Rafael Espindola54427cc2014-06-12 17:15:58 +000046#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000047
48using namespace llvm::MachO;
Nick Kledzik141330a2014-09-03 19:52:50 +000049using llvm::object::ExportEntry;
50using llvm::object::MachOObjectFile;
Nick Kledzike34182f2013-11-06 21:36:55 +000051
52namespace lld {
53namespace mach_o {
54namespace normalized {
55
56// Utility to call a lambda expression on each load command.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000057static std::error_code forEachLoadCommand(
58 StringRef lcRange, unsigned lcCount, bool swap, bool is64,
59 std::function<bool(uint32_t cmd, uint32_t size, const char *lc)> func) {
Nick Kledzike34182f2013-11-06 21:36:55 +000060 const char* p = lcRange.begin();
61 for (unsigned i=0; i < lcCount; ++i) {
62 const load_command *lc = reinterpret_cast<const load_command*>(p);
63 load_command lcCopy;
64 const load_command *slc = lc;
65 if (swap) {
66 memcpy(&lcCopy, lc, sizeof(load_command));
67 swapStruct(lcCopy);
68 slc = &lcCopy;
69 }
70 if ( (p + slc->cmdsize) > lcRange.end() )
Rafael Espindola372bc702014-06-13 17:20:48 +000071 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000072
Nick Kledzike34182f2013-11-06 21:36:55 +000073 if (func(slc->cmd, slc->cmdsize, p))
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000074 return std::error_code();
Shankar Easwaran3d8de472014-01-27 03:09:26 +000075
Nick Kledzike34182f2013-11-06 21:36:55 +000076 p += slc->cmdsize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +000077 }
78
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000079 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000080}
81
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000082static std::error_code appendRelocations(Relocations &relocs, StringRef buffer,
83 bool swap, bool bigEndian,
84 uint32_t reloff, uint32_t nreloc) {
Nick Kledzike34182f2013-11-06 21:36:55 +000085 if ((reloff + nreloc*8) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +000086 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000087 const any_relocation_info* relocsArray =
Joey Gouly010b3762014-01-14 22:32:38 +000088 reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000089
Nick Kledzike34182f2013-11-06 21:36:55 +000090 for(uint32_t i=0; i < nreloc; ++i) {
91 relocs.push_back(unpackRelocation(relocsArray[i], swap, bigEndian));
92 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000093 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000094}
95
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000096static std::error_code
Nick Kledzik388f3d02014-05-28 01:16:35 +000097appendIndirectSymbols(IndirectSymbols &isyms, StringRef buffer, bool swap,
98 bool bigEndian, uint32_t istOffset, uint32_t istCount,
99 uint32_t startIndex, uint32_t count) {
100 if ((istOffset + istCount*4) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +0000101 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000102 if (startIndex+count > istCount)
Rafael Espindola372bc702014-06-13 17:20:48 +0000103 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000104 const uint32_t *indirectSymbolArray =
105 reinterpret_cast<const uint32_t*>(buffer.begin()+istOffset);
106
107 for(uint32_t i=0; i < count; ++i) {
108 isyms.push_back(read32(swap, indirectSymbolArray[startIndex+i]));
109 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000110 return std::error_code();
Nick Kledzik388f3d02014-05-28 01:16:35 +0000111}
112
113
Joey Gouly010b3762014-01-14 22:32:38 +0000114template <typename T> static T readBigEndian(T t) {
115 if (llvm::sys::IsLittleEndianHost)
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000116 llvm::sys::swapByteOrder(t);
Joey Gouly010b3762014-01-14 22:32:38 +0000117 return t;
118}
Nick Kledzike34182f2013-11-06 21:36:55 +0000119
120/// Reads a mach-o file and produces an in-memory normalized view.
Joey Gouly010b3762014-01-14 22:32:38 +0000121ErrorOr<std::unique_ptr<NormalizedFile>>
122readBinary(std::unique_ptr<MemoryBuffer> &mb,
123 const MachOLinkingContext::Arch arch) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000124 // Make empty NormalizedFile.
125 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
126
Joey Gouly010b3762014-01-14 22:32:38 +0000127 const char *start = mb->getBufferStart();
128 size_t objSize = mb->getBufferSize();
129
Nick Kledzike34182f2013-11-06 21:36:55 +0000130 // Determine endianness and pointer size for mach-o file.
Joey Gouly010b3762014-01-14 22:32:38 +0000131 const mach_header *mh = reinterpret_cast<const mach_header *>(start);
132 bool isFat = mh->magic == llvm::MachO::FAT_CIGAM ||
133 mh->magic == llvm::MachO::FAT_MAGIC;
134 if (isFat) {
135 uint32_t cputype = MachOLinkingContext::cpuTypeFromArch(arch);
136 uint32_t cpusubtype = MachOLinkingContext::cpuSubtypeFromArch(arch);
137 const fat_header *fh = reinterpret_cast<const fat_header *>(start);
138 uint32_t nfat_arch = readBigEndian(fh->nfat_arch);
139 const fat_arch *fa =
140 reinterpret_cast<const fat_arch *>(start + sizeof(fat_header));
141 bool foundArch = false;
142 while (nfat_arch-- > 0) {
143 if (readBigEndian(fa->cputype) == cputype &&
144 readBigEndian(fa->cpusubtype) == cpusubtype) {
145 foundArch = true;
146 break;
147 }
148 fa++;
149 }
150 if (!foundArch) {
Nick Kledzik378066c2014-06-30 22:57:33 +0000151 return make_dynamic_error_code(Twine("file does not contain required"
152 " architecture ("
153 + MachOLinkingContext::nameFromArch(arch)
154 + ")" ));
Joey Gouly010b3762014-01-14 22:32:38 +0000155 }
156 objSize = readBigEndian(fa->size);
157 uint32_t offset = readBigEndian(fa->offset);
158 if ((offset + objSize) > mb->getBufferSize())
Rafael Espindola372bc702014-06-13 17:20:48 +0000159 return make_error_code(llvm::errc::executable_format_error);
Joey Gouly010b3762014-01-14 22:32:38 +0000160 start += offset;
161 mh = reinterpret_cast<const mach_header *>(start);
162 }
163
Nick Kledzike34182f2013-11-06 21:36:55 +0000164 bool is64, swap;
165 switch (mh->magic) {
166 case llvm::MachO::MH_MAGIC:
167 is64 = false;
168 swap = false;
169 break;
170 case llvm::MachO::MH_MAGIC_64:
171 is64 = true;
172 swap = false;
173 break;
174 case llvm::MachO::MH_CIGAM:
175 is64 = false;
176 swap = true;
177 break;
178 case llvm::MachO::MH_CIGAM_64:
179 is64 = true;
180 swap = true;
181 break;
182 default:
Rafael Espindola372bc702014-06-13 17:20:48 +0000183 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000184 }
185
186 // Endian swap header, if needed.
187 mach_header headerCopy;
188 const mach_header *smh = mh;
189 if (swap) {
190 memcpy(&headerCopy, mh, sizeof(mach_header));
191 swapStruct(headerCopy);
192 smh = &headerCopy;
193 }
194
195 // Validate head and load commands fit in buffer.
196 const uint32_t lcCount = smh->ncmds;
Joey Gouly010b3762014-01-14 22:32:38 +0000197 const char *lcStart =
198 start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header));
Nick Kledzike34182f2013-11-06 21:36:55 +0000199 StringRef lcRange(lcStart, smh->sizeofcmds);
Joey Gouly010b3762014-01-14 22:32:38 +0000200 if (lcRange.end() > (start + objSize))
Rafael Espindola372bc702014-06-13 17:20:48 +0000201 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000202
Nick Kledzik378066c2014-06-30 22:57:33 +0000203 // Get architecture from mach_header.
Nick Kledzike34182f2013-11-06 21:36:55 +0000204 f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype);
Nick Kledzik378066c2014-06-30 22:57:33 +0000205 if (f->arch != arch) {
206 return make_dynamic_error_code(Twine("file is wrong architecture. Expected "
207 "(" + MachOLinkingContext::nameFromArch(arch)
208 + ") found ("
209 + MachOLinkingContext::nameFromArch(f->arch)
210 + ")" ));
211 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000212 bool isBigEndianArch = MachOLinkingContext::isBigEndian(f->arch);
213 // Copy file type and flags
214 f->fileType = HeaderFileType(smh->filetype);
215 f->flags = smh->flags;
216
217
Nick Kledzik388f3d02014-05-28 01:16:35 +0000218 // Pre-scan load commands looking for indirect symbol table.
219 uint32_t indirectSymbolTableOffset = 0;
220 uint32_t indirectSymbolTableCount = 0;
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000221 std::error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
222 [&](uint32_t cmd, uint32_t size,
223 const char *lc) -> bool {
Nick Kledzik388f3d02014-05-28 01:16:35 +0000224 if (cmd == LC_DYSYMTAB) {
225 const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
226 indirectSymbolTableOffset = read32(swap, d->indirectsymoff);
227 indirectSymbolTableCount = read32(swap, d->nindirectsyms);
228 return true;
229 }
230 return false;
231 });
232 if (ec)
233 return ec;
234
235 // Walk load commands looking for segments/sections and the symbol table.
Nick Kledzik21921372014-07-24 23:06:56 +0000236 const data_in_code_entry *dataInCode = nullptr;
Nick Kledzik141330a2014-09-03 19:52:50 +0000237 const dyld_info_command *dyldInfo = nullptr;
Nick Kledzik21921372014-07-24 23:06:56 +0000238 uint32_t dataInCodeSize = 0;
Nick Kledzik388f3d02014-05-28 01:16:35 +0000239 ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
240 [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000241 switch(cmd) {
242 case LC_SEGMENT_64:
243 if (is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000244 const segment_command_64 *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000245 reinterpret_cast<const segment_command_64*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000246 const unsigned sectionCount = (swap
247 ? llvm::sys::getSwappedBytes(seg->nsects)
248 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000249 const section_64 *sects = reinterpret_cast<const section_64*>
250 (lc + sizeof(segment_command_64));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000251 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000252 + sectionCount*sizeof(section_64);
253 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000254 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000255 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000256 for (unsigned i=0; i < sectionCount; ++i) {
257 const section_64 *sect = &sects[i];
258 Section section;
259 section.segmentName = getString16(sect->segname);
260 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000261 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000262 & SECTION_TYPE);
263 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
264 section.alignment = read32(swap, sect->align);
265 section.address = read64(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000266 const uint8_t *content =
267 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000268 size_t contentSize = read64(swap, sect->size);
269 // Note: this assign() is copying the content bytes. Ideally,
270 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000271 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000272 appendRelocations(section.relocations, mb->getBuffer(),
273 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000274 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000275 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
276 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
277 swap, isBigEndianArch,
278 indirectSymbolTableOffset,
279 indirectSymbolTableCount,
280 read32(swap, sect->reserved1), contentSize/4);
281 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000282 f->sections.push_back(section);
283 }
284 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000285 break;
286 case LC_SEGMENT:
287 if (!is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000288 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000289 reinterpret_cast<const segment_command*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000290 const unsigned sectionCount = (swap
291 ? llvm::sys::getSwappedBytes(seg->nsects)
292 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000293 const section *sects = reinterpret_cast<const section*>
294 (lc + sizeof(segment_command));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000295 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000296 + sectionCount*sizeof(section);
297 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000298 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000299 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000300 for (unsigned i=0; i < sectionCount; ++i) {
301 const section *sect = &sects[i];
302 Section section;
303 section.segmentName = getString16(sect->segname);
304 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000305 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000306 & SECTION_TYPE);
307 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
308 section.alignment = read32(swap, sect->align);
309 section.address = read32(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000310 const uint8_t *content =
311 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000312 size_t contentSize = read32(swap, sect->size);
313 // Note: this assign() is copying the content bytes. Ideally,
314 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000315 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000316 appendRelocations(section.relocations, mb->getBuffer(),
317 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000318 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000319 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
320 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
321 swap, isBigEndianArch,
322 indirectSymbolTableOffset,
323 indirectSymbolTableCount,
324 read32(swap, sect->reserved1), contentSize/4);
325 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000326 f->sections.push_back(section);
327 }
328 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000329 break;
330 case LC_SYMTAB: {
Nick Kledzike34182f2013-11-06 21:36:55 +0000331 const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
Joey Gouly010b3762014-01-14 22:32:38 +0000332 const char *strings = start + read32(swap, st->stroff);
Nick Kledzike34182f2013-11-06 21:36:55 +0000333 const uint32_t strSize = read32(swap, st->strsize);
334 // Validate string pool and symbol table all in buffer.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000335 if ( read32(swap, st->stroff)+read32(swap, st->strsize)
Joey Gouly010b3762014-01-14 22:32:38 +0000336 > objSize )
Rafael Espindola1d364c12014-06-03 04:41:30 +0000337 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000338 if (is64) {
339 const uint32_t symOffset = read32(swap, st->symoff);
340 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000341 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000342 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000343 const nlist_64 *symbols =
344 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000345 // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
346 for(uint32_t i=0; i < symCount; ++i) {
347 const nlist_64 *sin = &symbols[i];
348 nlist_64 tempSym;
349 if (swap) {
350 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
351 }
352 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000353 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000354 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000355 sout.name = &strings[sin->n_strx];
356 sout.type = (NListType)(sin->n_type & N_TYPE);
357 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
358 sout.sect = sin->n_sect;
359 sout.desc = sin->n_desc;
360 sout.value = sin->n_value;
361 if (sout.type == N_UNDF)
362 f->undefinedSymbols.push_back(sout);
Nick Kledzik3f690762014-06-27 18:25:01 +0000363 else if (sin->n_type & N_EXT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000364 f->globalSymbols.push_back(sout);
365 else
366 f->localSymbols.push_back(sout);
367 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000368 } else {
Nick Kledzike34182f2013-11-06 21:36:55 +0000369 const uint32_t symOffset = read32(swap, st->symoff);
370 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000371 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000372 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000373 const nlist *symbols =
374 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000375 // Convert each nlist to a lld::mach_o::normalized::Symbol.
376 for(uint32_t i=0; i < symCount; ++i) {
377 const nlist *sin = &symbols[i];
378 nlist tempSym;
379 if (swap) {
380 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
381 }
382 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000383 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000384 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000385 sout.name = &strings[sin->n_strx];
386 sout.type = (NListType)(sin->n_type & N_TYPE);
387 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
388 sout.sect = sin->n_sect;
389 sout.desc = sin->n_desc;
390 sout.value = sin->n_value;
391 if (sout.type == N_UNDF)
392 f->undefinedSymbols.push_back(sout);
393 else if (sout.scope == (SymbolScope)N_EXT)
394 f->globalSymbols.push_back(sout);
395 else
396 f->localSymbols.push_back(sout);
397 }
398 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000399 }
400 break;
401 case LC_ID_DYLIB: {
Tim Northover301c4e62014-07-01 08:15:41 +0000402 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
Nick Kledzik21921372014-07-24 23:06:56 +0000403 f->installName = lc + read32(swap, dl->dylib.name);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000404 }
405 break;
406 case LC_DATA_IN_CODE: {
Nick Kledzik21921372014-07-24 23:06:56 +0000407 const linkedit_data_command *ldc =
408 reinterpret_cast<const linkedit_data_command*>(lc);
409 dataInCode = reinterpret_cast<const data_in_code_entry*>(
410 start + read32(swap, ldc->dataoff));
411 dataInCodeSize = read32(swap, ldc->datasize);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000412 }
Nick Kledzik141330a2014-09-03 19:52:50 +0000413 break;
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000414 case LC_LOAD_DYLIB:
415 case LC_LOAD_WEAK_DYLIB:
416 case LC_REEXPORT_DYLIB:
417 case LC_LOAD_UPWARD_DYLIB: {
418 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
419 DependentDylib entry;
420 entry.path = lc + read32(swap, dl->dylib.name);
421 entry.kind = LoadCommandType(cmd);
422 f->dependentDylibs.push_back(entry);
423 }
424 break;
Nick Kledzik141330a2014-09-03 19:52:50 +0000425 case LC_DYLD_INFO:
426 case LC_DYLD_INFO_ONLY:
427 dyldInfo = reinterpret_cast<const dyld_info_command*>(lc);
428 break;
Tim Northover301c4e62014-07-01 08:15:41 +0000429 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000430 return false;
431 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000432 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000433 return ec;
434
Nick Kledzik21921372014-07-24 23:06:56 +0000435 if (dataInCode) {
436 // Convert on-disk data_in_code_entry array to DataInCode vector.
437 for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); ++i) {
438 DataInCode entry;
439 entry.offset = read32(swap, dataInCode[i].offset);
440 entry.length = read16(swap, dataInCode[i].length);
441 entry.kind = (DataRegionType)read16(swap, dataInCode[i].kind);
442 f->dataInCode.push_back(entry);
443 }
444 }
445
Nick Kledzik141330a2014-09-03 19:52:50 +0000446 if (dyldInfo) {
447 // If any exports, extract and add to normalized exportInfo vector.
448 if (dyldInfo->export_size) {
449 const uint8_t *trieStart = reinterpret_cast<const uint8_t*>(start +
450 dyldInfo->export_off);
451 ArrayRef<uint8_t> trie(trieStart, dyldInfo->export_size);
452 for (const ExportEntry &trieExport : MachOObjectFile::exports(trie)) {
453 Export normExport;
454 normExport.name = trieExport.name().copy(f->ownedAllocations);
455 normExport.offset = trieExport.address();
456 normExport.kind = ExportSymbolKind(trieExport.flags() & EXPORT_SYMBOL_FLAGS_KIND_MASK);
457 normExport.flags = trieExport.flags() & ~EXPORT_SYMBOL_FLAGS_KIND_MASK;
458 normExport.otherOffset = trieExport.other();
459 if (!trieExport.otherName().empty())
460 normExport.otherName = trieExport.otherName().copy(f->ownedAllocations);
461 f->exportInfo.push_back(normExport);
462 }
463 }
464 }
465
Nick Kledzike34182f2013-11-06 21:36:55 +0000466 return std::move(f);
467}
468
469
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000470class MachOReader : public Reader {
471public:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000472 MachOReader(MachOLinkingContext &ctx) : _ctx(ctx) {}
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000473
474 bool canParse(file_magic magic, StringRef ext,
475 const MemoryBuffer &mb) const override {
Tim Northoverf9b13d62014-06-30 09:11:38 +0000476 if (magic != llvm::sys::fs::file_magic::macho_object &&
Nick Kledzik378066c2014-06-30 22:57:33 +0000477 magic != llvm::sys::fs::file_magic::macho_universal_binary &&
Tim Northoverf9b13d62014-06-30 09:11:38 +0000478 magic != llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib)
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000479 return false;
Nick Kledzik378066c2014-06-30 22:57:33 +0000480 return (mb.getBufferSize() > 32);
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000481 }
482
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000483 std::error_code
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000484 parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000485 std::vector<std::unique_ptr<File>> &result) const override {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000486 // Convert binary file to normalized mach-o.
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000487 auto normFile = readBinary(mb, _ctx.arch());
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000488 if (std::error_code ec = normFile.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000489 return ec;
490 // Convert normalized mach-o to atoms.
491 auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000492 if (std::error_code ec = file.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000493 return ec;
494
495 result.push_back(std::move(*file));
496
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000497 return std::error_code();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000498 }
499private:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000500 MachOLinkingContext &_ctx;
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000501};
502
503
Nick Kledzike34182f2013-11-06 21:36:55 +0000504} // namespace normalized
505} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000506
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000507void Registry::addSupportMachOObjects(MachOLinkingContext &ctx) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000508 MachOLinkingContext::Arch arch = ctx.arch();
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000509 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(ctx)));
Nick Kledzik2458bec2014-07-16 19:49:02 +0000510 addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(),
511 ctx.archHandler().kindStrings());
Nick Kledzik6edd7222014-01-11 01:07:43 +0000512 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
Nick Kledzik378066c2014-06-30 22:57:33 +0000513 new mach_o::MachOYamlIOTaggedDocumentHandler(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000514}
515
Nick Kledzike34182f2013-11-06 21:36:55 +0000516} // namespace lld
517