blob: 0993ea569c2758b86b61f83b8140260b1406046a [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
Nick Kledzik635f9c72014-09-04 20:08:30 +0000120
121static bool isMachOHeader(const mach_header *mh, bool &is64, bool &swap) {
122 switch (mh->magic) {
123 case llvm::MachO::MH_MAGIC:
124 is64 = false;
125 swap = false;
126 return true;
127 case llvm::MachO::MH_MAGIC_64:
128 is64 = true;
129 swap = false;
130 return true;
131 case llvm::MachO::MH_CIGAM:
132 is64 = false;
133 swap = true;
134 return true;
135 case llvm::MachO::MH_CIGAM_64:
136 is64 = true;
137 swap = true;
138 return true;
139 default:
140 return false;
141 }
142}
143
144
145bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch) {
146 // Try opening and mapping file at path.
147 ErrorOr<std::unique_ptr<MemoryBuffer>> b = MemoryBuffer::getFileOrSTDIN(path);
148 if (b.getError())
149 return false;
150
151 // If file length < 32 it is too small to be mach-o object file.
152 StringRef fileBuffer = b->get()->getBuffer();
153 if (fileBuffer.size() < 32)
154 return false;
155
156 // If file buffer does not start with MH_MAGIC (and variants), not obj file.
157 const mach_header *mh = reinterpret_cast<const mach_header *>(
158 fileBuffer.begin());
159 bool is64, swap;
160 if (!isMachOHeader(mh, is64, swap))
161 return false;
162
163 // If not MH_OBJECT, not object file.
164 if (read32(swap, mh->filetype) != MH_OBJECT)
165 return false;
166
167 // Lookup up arch from cpu/subtype pair.
168 arch = MachOLinkingContext::archFromCpuType(read32(swap, mh->cputype),
169 read32(swap, mh->cpusubtype));
170 return true;
171}
172
Nick Kledzik14b5d202014-10-08 01:48:10 +0000173
174bool sliceFromFatFile(const MemoryBuffer &mb, MachOLinkingContext::Arch arch,
175 uint32_t &offset, uint32_t &size) {
176 const char *start = mb.getBufferStart();
177 const llvm::MachO::fat_header *fh =
178 reinterpret_cast<const llvm::MachO::fat_header *>(start);
179 if (readBigEndian(fh->magic) != llvm::MachO::FAT_MAGIC)
180 return false;
181 uint32_t nfat_arch = readBigEndian(fh->nfat_arch);
182 const fat_arch *fstart =
183 reinterpret_cast<const fat_arch *>(start + sizeof(fat_header));
184 const fat_arch *fend =
185 reinterpret_cast<const fat_arch *>(start + sizeof(fat_header) +
186 sizeof(fat_arch) * nfat_arch);
187 const uint32_t reqCpuType = MachOLinkingContext::cpuTypeFromArch(arch);
188 const uint32_t reqCpuSubtype = MachOLinkingContext::cpuSubtypeFromArch(arch);
189 for (const fat_arch *fa = fstart; fa < fend; ++fa) {
190 if ((readBigEndian(fa->cputype) == reqCpuType) &&
191 (readBigEndian(fa->cpusubtype) == reqCpuSubtype)) {
192 offset = readBigEndian(fa->offset);
193 size = readBigEndian(fa->size);
194 if ((offset + size) > mb.getBufferSize())
195 return false;
196 return true;
197 }
198 }
199 return false;
200}
201
Nick Kledzike34182f2013-11-06 21:36:55 +0000202/// Reads a mach-o file and produces an in-memory normalized view.
Joey Gouly010b3762014-01-14 22:32:38 +0000203ErrorOr<std::unique_ptr<NormalizedFile>>
204readBinary(std::unique_ptr<MemoryBuffer> &mb,
205 const MachOLinkingContext::Arch arch) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000206 // Make empty NormalizedFile.
207 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
208
Joey Gouly010b3762014-01-14 22:32:38 +0000209 const char *start = mb->getBufferStart();
210 size_t objSize = mb->getBufferSize();
Joey Gouly010b3762014-01-14 22:32:38 +0000211 const mach_header *mh = reinterpret_cast<const mach_header *>(start);
Nick Kledzik14b5d202014-10-08 01:48:10 +0000212
213 uint32_t sliceOffset;
214 uint32_t sliceSize;
215 if (sliceFromFatFile(*mb, arch, sliceOffset, sliceSize)) {
216 start = &start[sliceOffset];
217 objSize = sliceSize;
Joey Gouly010b3762014-01-14 22:32:38 +0000218 mh = reinterpret_cast<const mach_header *>(start);
219 }
220
Nick Kledzik14b5d202014-10-08 01:48:10 +0000221 // Determine endianness and pointer size for mach-o file.
Nick Kledzike34182f2013-11-06 21:36:55 +0000222 bool is64, swap;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000223 if (!isMachOHeader(mh, is64, swap))
Rafael Espindola372bc702014-06-13 17:20:48 +0000224 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000225
226 // Endian swap header, if needed.
227 mach_header headerCopy;
228 const mach_header *smh = mh;
229 if (swap) {
230 memcpy(&headerCopy, mh, sizeof(mach_header));
231 swapStruct(headerCopy);
232 smh = &headerCopy;
233 }
234
235 // Validate head and load commands fit in buffer.
236 const uint32_t lcCount = smh->ncmds;
Joey Gouly010b3762014-01-14 22:32:38 +0000237 const char *lcStart =
238 start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header));
Nick Kledzike34182f2013-11-06 21:36:55 +0000239 StringRef lcRange(lcStart, smh->sizeofcmds);
Joey Gouly010b3762014-01-14 22:32:38 +0000240 if (lcRange.end() > (start + objSize))
Rafael Espindola372bc702014-06-13 17:20:48 +0000241 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000242
Nick Kledzik378066c2014-06-30 22:57:33 +0000243 // Get architecture from mach_header.
Nick Kledzike34182f2013-11-06 21:36:55 +0000244 f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype);
Nick Kledzik378066c2014-06-30 22:57:33 +0000245 if (f->arch != arch) {
246 return make_dynamic_error_code(Twine("file is wrong architecture. Expected "
247 "(" + MachOLinkingContext::nameFromArch(arch)
248 + ") found ("
249 + MachOLinkingContext::nameFromArch(f->arch)
250 + ")" ));
251 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000252 bool isBigEndianArch = MachOLinkingContext::isBigEndian(f->arch);
253 // Copy file type and flags
254 f->fileType = HeaderFileType(smh->filetype);
255 f->flags = smh->flags;
256
257
Nick Kledzik388f3d02014-05-28 01:16:35 +0000258 // Pre-scan load commands looking for indirect symbol table.
259 uint32_t indirectSymbolTableOffset = 0;
260 uint32_t indirectSymbolTableCount = 0;
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000261 std::error_code ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
262 [&](uint32_t cmd, uint32_t size,
263 const char *lc) -> bool {
Nick Kledzik388f3d02014-05-28 01:16:35 +0000264 if (cmd == LC_DYSYMTAB) {
265 const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
266 indirectSymbolTableOffset = read32(swap, d->indirectsymoff);
267 indirectSymbolTableCount = read32(swap, d->nindirectsyms);
268 return true;
269 }
270 return false;
271 });
272 if (ec)
273 return ec;
274
275 // Walk load commands looking for segments/sections and the symbol table.
Nick Kledzik21921372014-07-24 23:06:56 +0000276 const data_in_code_entry *dataInCode = nullptr;
Nick Kledzik141330a2014-09-03 19:52:50 +0000277 const dyld_info_command *dyldInfo = nullptr;
Nick Kledzik21921372014-07-24 23:06:56 +0000278 uint32_t dataInCodeSize = 0;
Nick Kledzik388f3d02014-05-28 01:16:35 +0000279 ec = forEachLoadCommand(lcRange, lcCount, swap, is64,
280 [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000281 switch(cmd) {
282 case LC_SEGMENT_64:
283 if (is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000284 const segment_command_64 *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000285 reinterpret_cast<const segment_command_64*>(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_64 *sects = reinterpret_cast<const section_64*>
290 (lc + sizeof(segment_command_64));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000291 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000292 + sectionCount*sizeof(section_64);
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_64 *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 = read64(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 = read64(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_SEGMENT:
327 if (!is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000328 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000329 reinterpret_cast<const segment_command*>(lc);
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000330 const unsigned sectionCount = (swap
331 ? llvm::sys::getSwappedBytes(seg->nsects)
332 : seg->nsects);
Nick Kledzike34182f2013-11-06 21:36:55 +0000333 const section *sects = reinterpret_cast<const section*>
334 (lc + sizeof(segment_command));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000335 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000336 + sectionCount*sizeof(section);
337 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000338 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000339 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000340 for (unsigned i=0; i < sectionCount; ++i) {
341 const section *sect = &sects[i];
342 Section section;
343 section.segmentName = getString16(sect->segname);
344 section.sectionName = getString16(sect->sectname);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000345 section.type = (SectionType)(read32(swap, sect->flags)
Nick Kledzike34182f2013-11-06 21:36:55 +0000346 & SECTION_TYPE);
347 section.attributes = read32(swap, sect->flags) & SECTION_ATTRIBUTES;
348 section.alignment = read32(swap, sect->align);
349 section.address = read32(swap, sect->addr);
Joey Gouly010b3762014-01-14 22:32:38 +0000350 const uint8_t *content =
351 (uint8_t *)start + read32(swap, sect->offset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000352 size_t contentSize = read32(swap, sect->size);
353 // Note: this assign() is copying the content bytes. Ideally,
354 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000355 section.content = llvm::makeArrayRef(content, contentSize);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000356 appendRelocations(section.relocations, mb->getBuffer(),
357 swap, isBigEndianArch, read32(swap, sect->reloff),
Nick Kledzike34182f2013-11-06 21:36:55 +0000358 read32(swap, sect->nreloc));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000359 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
360 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
361 swap, isBigEndianArch,
362 indirectSymbolTableOffset,
363 indirectSymbolTableCount,
364 read32(swap, sect->reserved1), contentSize/4);
365 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000366 f->sections.push_back(section);
367 }
368 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000369 break;
370 case LC_SYMTAB: {
Nick Kledzike34182f2013-11-06 21:36:55 +0000371 const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
Joey Gouly010b3762014-01-14 22:32:38 +0000372 const char *strings = start + read32(swap, st->stroff);
Nick Kledzike34182f2013-11-06 21:36:55 +0000373 const uint32_t strSize = read32(swap, st->strsize);
374 // Validate string pool and symbol table all in buffer.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000375 if ( read32(swap, st->stroff)+read32(swap, st->strsize)
Joey Gouly010b3762014-01-14 22:32:38 +0000376 > objSize )
Rafael Espindola1d364c12014-06-03 04:41:30 +0000377 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000378 if (is64) {
379 const uint32_t symOffset = read32(swap, st->symoff);
380 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000381 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000382 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000383 const nlist_64 *symbols =
384 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000385 // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
386 for(uint32_t i=0; i < symCount; ++i) {
387 const nlist_64 *sin = &symbols[i];
388 nlist_64 tempSym;
389 if (swap) {
390 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
391 }
392 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000393 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000394 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000395 sout.name = &strings[sin->n_strx];
396 sout.type = (NListType)(sin->n_type & N_TYPE);
397 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
398 sout.sect = sin->n_sect;
399 sout.desc = sin->n_desc;
400 sout.value = sin->n_value;
401 if (sout.type == N_UNDF)
402 f->undefinedSymbols.push_back(sout);
Nick Kledzik3f690762014-06-27 18:25:01 +0000403 else if (sin->n_type & N_EXT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000404 f->globalSymbols.push_back(sout);
405 else
406 f->localSymbols.push_back(sout);
407 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000408 } else {
Nick Kledzike34182f2013-11-06 21:36:55 +0000409 const uint32_t symOffset = read32(swap, st->symoff);
410 const uint32_t symCount = read32(swap, st->nsyms);
Joey Gouly010b3762014-01-14 22:32:38 +0000411 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000412 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000413 const nlist *symbols =
414 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000415 // Convert each nlist to a lld::mach_o::normalized::Symbol.
416 for(uint32_t i=0; i < symCount; ++i) {
417 const nlist *sin = &symbols[i];
418 nlist tempSym;
419 if (swap) {
420 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
421 }
422 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000423 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000424 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000425 sout.name = &strings[sin->n_strx];
426 sout.type = (NListType)(sin->n_type & N_TYPE);
427 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
428 sout.sect = sin->n_sect;
429 sout.desc = sin->n_desc;
430 sout.value = sin->n_value;
431 if (sout.type == N_UNDF)
432 f->undefinedSymbols.push_back(sout);
433 else if (sout.scope == (SymbolScope)N_EXT)
434 f->globalSymbols.push_back(sout);
435 else
436 f->localSymbols.push_back(sout);
437 }
438 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000439 }
440 break;
441 case LC_ID_DYLIB: {
Tim Northover301c4e62014-07-01 08:15:41 +0000442 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
Nick Kledzik21921372014-07-24 23:06:56 +0000443 f->installName = lc + read32(swap, dl->dylib.name);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000444 }
445 break;
446 case LC_DATA_IN_CODE: {
Nick Kledzik21921372014-07-24 23:06:56 +0000447 const linkedit_data_command *ldc =
448 reinterpret_cast<const linkedit_data_command*>(lc);
449 dataInCode = reinterpret_cast<const data_in_code_entry*>(
450 start + read32(swap, ldc->dataoff));
451 dataInCodeSize = read32(swap, ldc->datasize);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000452 }
Nick Kledzik141330a2014-09-03 19:52:50 +0000453 break;
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000454 case LC_LOAD_DYLIB:
455 case LC_LOAD_WEAK_DYLIB:
456 case LC_REEXPORT_DYLIB:
457 case LC_LOAD_UPWARD_DYLIB: {
458 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
459 DependentDylib entry;
460 entry.path = lc + read32(swap, dl->dylib.name);
461 entry.kind = LoadCommandType(cmd);
462 f->dependentDylibs.push_back(entry);
463 }
464 break;
Nick Kledzik141330a2014-09-03 19:52:50 +0000465 case LC_DYLD_INFO:
466 case LC_DYLD_INFO_ONLY:
467 dyldInfo = reinterpret_cast<const dyld_info_command*>(lc);
468 break;
Tim Northover301c4e62014-07-01 08:15:41 +0000469 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000470 return false;
471 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000472 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000473 return ec;
474
Nick Kledzik21921372014-07-24 23:06:56 +0000475 if (dataInCode) {
476 // Convert on-disk data_in_code_entry array to DataInCode vector.
477 for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); ++i) {
478 DataInCode entry;
479 entry.offset = read32(swap, dataInCode[i].offset);
480 entry.length = read16(swap, dataInCode[i].length);
481 entry.kind = (DataRegionType)read16(swap, dataInCode[i].kind);
482 f->dataInCode.push_back(entry);
483 }
484 }
485
Nick Kledzik141330a2014-09-03 19:52:50 +0000486 if (dyldInfo) {
487 // If any exports, extract and add to normalized exportInfo vector.
488 if (dyldInfo->export_size) {
489 const uint8_t *trieStart = reinterpret_cast<const uint8_t*>(start +
490 dyldInfo->export_off);
491 ArrayRef<uint8_t> trie(trieStart, dyldInfo->export_size);
492 for (const ExportEntry &trieExport : MachOObjectFile::exports(trie)) {
493 Export normExport;
494 normExport.name = trieExport.name().copy(f->ownedAllocations);
495 normExport.offset = trieExport.address();
496 normExport.kind = ExportSymbolKind(trieExport.flags() & EXPORT_SYMBOL_FLAGS_KIND_MASK);
497 normExport.flags = trieExport.flags() & ~EXPORT_SYMBOL_FLAGS_KIND_MASK;
498 normExport.otherOffset = trieExport.other();
499 if (!trieExport.otherName().empty())
500 normExport.otherName = trieExport.otherName().copy(f->ownedAllocations);
501 f->exportInfo.push_back(normExport);
502 }
503 }
504 }
505
Nick Kledzike34182f2013-11-06 21:36:55 +0000506 return std::move(f);
507}
508
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000509class MachOReader : public Reader {
510public:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000511 MachOReader(MachOLinkingContext &ctx) : _ctx(ctx) {}
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000512
513 bool canParse(file_magic magic, StringRef ext,
514 const MemoryBuffer &mb) const override {
Nick Kledzik14b5d202014-10-08 01:48:10 +0000515 switch (magic) {
516 case llvm::sys::fs::file_magic::macho_object:
517 case llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib:
518 case llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
519 return (mb.getBufferSize() > 32);
520 default:
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000521 return false;
Nick Kledzik14b5d202014-10-08 01:48:10 +0000522 }
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000523 }
524
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000525 std::error_code
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000526 parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000527 std::vector<std::unique_ptr<File>> &result) const override {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000528 // Convert binary file to normalized mach-o.
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000529 auto normFile = readBinary(mb, _ctx.arch());
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000530 if (std::error_code ec = normFile.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000531 return ec;
532 // Convert normalized mach-o to atoms.
533 auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000534 if (std::error_code ec = file.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000535 return ec;
536
537 result.push_back(std::move(*file));
538
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000539 return std::error_code();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000540 }
541private:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000542 MachOLinkingContext &_ctx;
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000543};
544
545
Nick Kledzike34182f2013-11-06 21:36:55 +0000546} // namespace normalized
547} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000548
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000549void Registry::addSupportMachOObjects(MachOLinkingContext &ctx) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000550 MachOLinkingContext::Arch arch = ctx.arch();
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000551 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(ctx)));
Nick Kledzik2458bec2014-07-16 19:49:02 +0000552 addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(),
553 ctx.archHandler().kindStrings());
Nick Kledzik6edd7222014-01-11 01:07:43 +0000554 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
Nick Kledzik378066c2014-06-30 22:57:33 +0000555 new mach_o::MachOYamlIOTaggedDocumentHandler(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000556}
557
Nick Kledzik14b5d202014-10-08 01:48:10 +0000558
Nick Kledzike34182f2013-11-06 21:36:55 +0000559} // namespace lld
560