blob: c0d850039ae5d9b2bb2272610b935f1423e14ebe [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#include "ArchHandler.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000026#include "MachONormalizedFileBinaryUtils.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000027#include "lld/Core/Error.h"
28#include "lld/Core/LLVM.h"
Nick Kledzik8fc67fb2014-08-13 23:55:41 +000029#include "lld/Core/SharedLibraryFile.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000030#include "llvm/ADT/SmallString.h"
31#include "llvm/ADT/StringRef.h"
32#include "llvm/ADT/StringSwitch.h"
Rafael Espindolac08ab8e2015-04-24 15:51:45 +000033#include "llvm/ADT/STLExtras.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000034#include "llvm/ADT/Twine.h"
Nick Kledzik141330a2014-09-03 19:52:50 +000035#include "llvm/Object/MachO.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000036#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;
Nick Kledzik141330a2014-09-03 19:52:50 +000048using llvm::object::ExportEntry;
49using llvm::object::MachOObjectFile;
Nick Kledzike34182f2013-11-06 21:36:55 +000050
51namespace lld {
52namespace mach_o {
53namespace normalized {
54
55// Utility to call a lambda expression on each load command.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000056static std::error_code forEachLoadCommand(
Tim Northover40d3ad32014-10-27 22:48:35 +000057 StringRef lcRange, unsigned lcCount, bool isBig, bool is64,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000058 std::function<bool(uint32_t cmd, uint32_t size, const char *lc)> func) {
Nick Kledzike34182f2013-11-06 21:36:55 +000059 const char* p = lcRange.begin();
60 for (unsigned i=0; i < lcCount; ++i) {
61 const load_command *lc = reinterpret_cast<const load_command*>(p);
62 load_command lcCopy;
63 const load_command *slc = lc;
Tim Northover40d3ad32014-10-27 22:48:35 +000064 if (isBig != llvm::sys::IsBigEndianHost) {
Nick Kledzike34182f2013-11-06 21:36:55 +000065 memcpy(&lcCopy, lc, sizeof(load_command));
66 swapStruct(lcCopy);
67 slc = &lcCopy;
68 }
69 if ( (p + slc->cmdsize) > lcRange.end() )
Rafael Espindola372bc702014-06-13 17:20:48 +000070 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000071
Nick Kledzike34182f2013-11-06 21:36:55 +000072 if (func(slc->cmd, slc->cmdsize, p))
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000073 return std::error_code();
Shankar Easwaran3d8de472014-01-27 03:09:26 +000074
Nick Kledzike34182f2013-11-06 21:36:55 +000075 p += slc->cmdsize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +000076 }
77
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000078 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000079}
80
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000081static std::error_code appendRelocations(Relocations &relocs, StringRef buffer,
Tim Northover40d3ad32014-10-27 22:48:35 +000082 bool bigEndian,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000083 uint32_t reloff, uint32_t nreloc) {
Nick Kledzike34182f2013-11-06 21:36:55 +000084 if ((reloff + nreloc*8) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +000085 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000086 const any_relocation_info* relocsArray =
Joey Gouly010b3762014-01-14 22:32:38 +000087 reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000088
Nick Kledzike34182f2013-11-06 21:36:55 +000089 for(uint32_t i=0; i < nreloc; ++i) {
Tim Northover40d3ad32014-10-27 22:48:35 +000090 relocs.push_back(unpackRelocation(relocsArray[i], bigEndian));
Nick Kledzike34182f2013-11-06 21:36:55 +000091 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000092 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000093}
94
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000095static std::error_code
Tim Northover40d3ad32014-10-27 22:48:35 +000096appendIndirectSymbols(IndirectSymbols &isyms, StringRef buffer, bool isBig,
97 uint32_t istOffset, uint32_t istCount,
Nick Kledzik388f3d02014-05-28 01:16:35 +000098 uint32_t startIndex, uint32_t count) {
99 if ((istOffset + istCount*4) > buffer.size())
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 if (startIndex+count > istCount)
Rafael Espindola372bc702014-06-13 17:20:48 +0000102 return make_error_code(llvm::errc::executable_format_error);
Tim Northover40d3ad32014-10-27 22:48:35 +0000103 const uint8_t *indirectSymbolArray = (const uint8_t *)buffer.data();
Nick Kledzik388f3d02014-05-28 01:16:35 +0000104
105 for(uint32_t i=0; i < count; ++i) {
Tim Northover40d3ad32014-10-27 22:48:35 +0000106 isyms.push_back(read32(
107 indirectSymbolArray + (startIndex + i) * sizeof(uint32_t), isBig));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000108 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000109 return std::error_code();
Nick Kledzik388f3d02014-05-28 01:16:35 +0000110}
111
112
Joey Gouly010b3762014-01-14 22:32:38 +0000113template <typename T> static T readBigEndian(T t) {
114 if (llvm::sys::IsLittleEndianHost)
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000115 llvm::sys::swapByteOrder(t);
Joey Gouly010b3762014-01-14 22:32:38 +0000116 return t;
117}
Nick Kledzike34182f2013-11-06 21:36:55 +0000118
Nick Kledzik635f9c72014-09-04 20:08:30 +0000119
Tim Northover40d3ad32014-10-27 22:48:35 +0000120static bool isMachOHeader(const mach_header *mh, bool &is64, bool &isBig) {
121 switch (read32(&mh->magic, false)) {
Nick Kledzik635f9c72014-09-04 20:08:30 +0000122 case llvm::MachO::MH_MAGIC:
123 is64 = false;
Tim Northover40d3ad32014-10-27 22:48:35 +0000124 isBig = false;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000125 return true;
126 case llvm::MachO::MH_MAGIC_64:
127 is64 = true;
Tim Northover40d3ad32014-10-27 22:48:35 +0000128 isBig = false;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000129 return true;
130 case llvm::MachO::MH_CIGAM:
131 is64 = false;
Tim Northover40d3ad32014-10-27 22:48:35 +0000132 isBig = true;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000133 return true;
134 case llvm::MachO::MH_CIGAM_64:
135 is64 = true;
Tim Northover40d3ad32014-10-27 22:48:35 +0000136 isBig = true;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000137 return true;
138 default:
139 return false;
140 }
141}
142
143
144bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch) {
145 // Try opening and mapping file at path.
146 ErrorOr<std::unique_ptr<MemoryBuffer>> b = MemoryBuffer::getFileOrSTDIN(path);
147 if (b.getError())
148 return false;
149
150 // If file length < 32 it is too small to be mach-o object file.
151 StringRef fileBuffer = b->get()->getBuffer();
152 if (fileBuffer.size() < 32)
153 return false;
154
155 // If file buffer does not start with MH_MAGIC (and variants), not obj file.
156 const mach_header *mh = reinterpret_cast<const mach_header *>(
157 fileBuffer.begin());
Tim Northover40d3ad32014-10-27 22:48:35 +0000158 bool is64, isBig;
159 if (!isMachOHeader(mh, is64, isBig))
Nick Kledzik635f9c72014-09-04 20:08:30 +0000160 return false;
161
162 // If not MH_OBJECT, not object file.
Tim Northover40d3ad32014-10-27 22:48:35 +0000163 if (read32(&mh->filetype, isBig) != MH_OBJECT)
Nick Kledzik635f9c72014-09-04 20:08:30 +0000164 return false;
165
166 // Lookup up arch from cpu/subtype pair.
Tim Northover40d3ad32014-10-27 22:48:35 +0000167 arch = MachOLinkingContext::archFromCpuType(
168 read32(&mh->cputype, isBig),
169 read32(&mh->cpusubtype, isBig));
Nick Kledzik635f9c72014-09-04 20:08:30 +0000170 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.
Tim Northover40d3ad32014-10-27 22:48:35 +0000222 bool is64, isBig;
223 if (!isMachOHeader(mh, is64, isBig))
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;
Tim Northover40d3ad32014-10-27 22:48:35 +0000229 if (isBig != llvm::sys::IsBigEndianHost) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000230 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 // Copy file type and flags
253 f->fileType = HeaderFileType(smh->filetype);
254 f->flags = smh->flags;
255
256
Nick Kledzik388f3d02014-05-28 01:16:35 +0000257 // Pre-scan load commands looking for indirect symbol table.
258 uint32_t indirectSymbolTableOffset = 0;
259 uint32_t indirectSymbolTableCount = 0;
Tim Northover40d3ad32014-10-27 22:48:35 +0000260 std::error_code ec = forEachLoadCommand(lcRange, lcCount, isBig, is64,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000261 [&](uint32_t cmd, uint32_t size,
262 const char *lc) -> bool {
Nick Kledzik388f3d02014-05-28 01:16:35 +0000263 if (cmd == LC_DYSYMTAB) {
264 const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000265 indirectSymbolTableOffset = read32(&d->indirectsymoff, isBig);
266 indirectSymbolTableCount = read32(&d->nindirectsyms, isBig);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000267 return true;
268 }
269 return false;
270 });
271 if (ec)
272 return ec;
273
274 // Walk load commands looking for segments/sections and the symbol table.
Nick Kledzik21921372014-07-24 23:06:56 +0000275 const data_in_code_entry *dataInCode = nullptr;
Nick Kledzik141330a2014-09-03 19:52:50 +0000276 const dyld_info_command *dyldInfo = nullptr;
Nick Kledzik21921372014-07-24 23:06:56 +0000277 uint32_t dataInCodeSize = 0;
Tim Northover40d3ad32014-10-27 22:48:35 +0000278 ec = forEachLoadCommand(lcRange, lcCount, isBig, is64,
Nick Kledzik388f3d02014-05-28 01:16:35 +0000279 [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000280 switch(cmd) {
281 case LC_SEGMENT_64:
282 if (is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000283 const segment_command_64 *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000284 reinterpret_cast<const segment_command_64*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000285 const unsigned sectionCount = read32(&seg->nsects, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000286 const section_64 *sects = reinterpret_cast<const section_64*>
287 (lc + sizeof(segment_command_64));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000288 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000289 + sectionCount*sizeof(section_64);
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_64 *sect = &sects[i];
295 Section section;
296 section.segmentName = getString16(sect->segname);
297 section.sectionName = getString16(sect->sectname);
Tim Northover40d3ad32014-10-27 22:48:35 +0000298 section.type = (SectionType)(read32(&sect->flags, isBig) &
299 SECTION_TYPE);
300 section.attributes = read32(&sect->flags, isBig) & SECTION_ATTRIBUTES;
Rui Ueyamaf006f4d2015-03-26 01:44:01 +0000301 section.alignment = 1 << read32(&sect->align, isBig);
Tim Northover40d3ad32014-10-27 22:48:35 +0000302 section.address = read64(&sect->addr, isBig);
Joey Gouly010b3762014-01-14 22:32:38 +0000303 const uint8_t *content =
Simon Atanasyan55c26992014-11-14 07:15:43 +0000304 (const uint8_t *)start + read32(&sect->offset, isBig);
Tim Northover40d3ad32014-10-27 22:48:35 +0000305 size_t contentSize = read64(&sect->size, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000306 // 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);
Tim Northover40d3ad32014-10-27 22:48:35 +0000309 appendRelocations(section.relocations, mb->getBuffer(), isBig,
310 read32(&sect->reloff, isBig),
311 read32(&sect->nreloc, isBig));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000312 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
313 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
Tim Northover40d3ad32014-10-27 22:48:35 +0000314 isBig,
Nick Kledzik388f3d02014-05-28 01:16:35 +0000315 indirectSymbolTableOffset,
316 indirectSymbolTableCount,
Tim Northover40d3ad32014-10-27 22:48:35 +0000317 read32(&sect->reserved1, isBig),
318 contentSize/4);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000319 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000320 f->sections.push_back(section);
321 }
322 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000323 break;
324 case LC_SEGMENT:
325 if (!is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000326 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000327 reinterpret_cast<const segment_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000328 const unsigned sectionCount = read32(&seg->nsects, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000329 const section *sects = reinterpret_cast<const section*>
330 (lc + sizeof(segment_command));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000331 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000332 + sectionCount*sizeof(section);
333 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000334 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000335 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000336 for (unsigned i=0; i < sectionCount; ++i) {
337 const section *sect = &sects[i];
338 Section section;
339 section.segmentName = getString16(sect->segname);
340 section.sectionName = getString16(sect->sectname);
Tim Northover40d3ad32014-10-27 22:48:35 +0000341 section.type = (SectionType)(read32(&sect->flags, isBig) &
342 SECTION_TYPE);
343 section.attributes =
Simon Atanasyan55c26992014-11-14 07:15:43 +0000344 read32((const uint8_t *)&sect->flags, isBig) & SECTION_ATTRIBUTES;
Rui Ueyamaf006f4d2015-03-26 01:44:01 +0000345 section.alignment = 1 << read32(&sect->align, isBig);
Tim Northover40d3ad32014-10-27 22:48:35 +0000346 section.address = read32(&sect->addr, isBig);
Joey Gouly010b3762014-01-14 22:32:38 +0000347 const uint8_t *content =
Simon Atanasyan55c26992014-11-14 07:15:43 +0000348 (const uint8_t *)start + read32(&sect->offset, isBig);
Tim Northover40d3ad32014-10-27 22:48:35 +0000349 size_t contentSize = read32(&sect->size, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000350 // Note: this assign() is copying the content bytes. Ideally,
351 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000352 section.content = llvm::makeArrayRef(content, contentSize);
Tim Northover40d3ad32014-10-27 22:48:35 +0000353 appendRelocations(section.relocations, mb->getBuffer(), isBig,
354 read32(&sect->reloff, isBig),
355 read32(&sect->nreloc, isBig));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000356 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
Tim Northover40d3ad32014-10-27 22:48:35 +0000357 appendIndirectSymbols(
358 section.indirectSymbols, mb->getBuffer(), isBig,
359 indirectSymbolTableOffset, indirectSymbolTableCount,
360 read32(&sect->reserved1, isBig), contentSize / 4);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000361 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000362 f->sections.push_back(section);
363 }
364 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000365 break;
366 case LC_SYMTAB: {
Nick Kledzike34182f2013-11-06 21:36:55 +0000367 const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000368 const char *strings = start + read32(&st->stroff, isBig);
369 const uint32_t strSize = read32(&st->strsize, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000370 // Validate string pool and symbol table all in buffer.
Simon Atanasyan55c26992014-11-14 07:15:43 +0000371 if (read32((const uint8_t *)&st->stroff, isBig) +
372 read32((const uint8_t *)&st->strsize, isBig) >
Tim Northover40d3ad32014-10-27 22:48:35 +0000373 objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000374 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000375 if (is64) {
Tim Northover40d3ad32014-10-27 22:48:35 +0000376 const uint32_t symOffset = read32(&st->symoff, isBig);
377 const uint32_t symCount = read32(&st->nsyms, isBig);
Joey Gouly010b3762014-01-14 22:32:38 +0000378 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000379 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000380 const nlist_64 *symbols =
381 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000382 // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
383 for(uint32_t i=0; i < symCount; ++i) {
384 const nlist_64 *sin = &symbols[i];
385 nlist_64 tempSym;
Tim Northover40d3ad32014-10-27 22:48:35 +0000386 if (isBig != llvm::sys::IsBigEndianHost) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000387 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
388 }
389 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000390 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000391 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000392 sout.name = &strings[sin->n_strx];
393 sout.type = (NListType)(sin->n_type & N_TYPE);
394 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
395 sout.sect = sin->n_sect;
396 sout.desc = sin->n_desc;
397 sout.value = sin->n_value;
398 if (sout.type == N_UNDF)
399 f->undefinedSymbols.push_back(sout);
Nick Kledzik3f690762014-06-27 18:25:01 +0000400 else if (sin->n_type & N_EXT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000401 f->globalSymbols.push_back(sout);
402 else
403 f->localSymbols.push_back(sout);
404 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000405 } else {
Tim Northover40d3ad32014-10-27 22:48:35 +0000406 const uint32_t symOffset = read32(&st->symoff, isBig);
407 const uint32_t symCount = read32(&st->nsyms, isBig);
Joey Gouly010b3762014-01-14 22:32:38 +0000408 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000409 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000410 const nlist *symbols =
411 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000412 // Convert each nlist to a lld::mach_o::normalized::Symbol.
413 for(uint32_t i=0; i < symCount; ++i) {
414 const nlist *sin = &symbols[i];
415 nlist tempSym;
Tim Northover40d3ad32014-10-27 22:48:35 +0000416 if (isBig != llvm::sys::IsBigEndianHost) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000417 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
418 }
419 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000420 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000421 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000422 sout.name = &strings[sin->n_strx];
423 sout.type = (NListType)(sin->n_type & N_TYPE);
424 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
425 sout.sect = sin->n_sect;
426 sout.desc = sin->n_desc;
427 sout.value = sin->n_value;
428 if (sout.type == N_UNDF)
429 f->undefinedSymbols.push_back(sout);
430 else if (sout.scope == (SymbolScope)N_EXT)
431 f->globalSymbols.push_back(sout);
432 else
433 f->localSymbols.push_back(sout);
434 }
435 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000436 }
437 break;
438 case LC_ID_DYLIB: {
Tim Northover301c4e62014-07-01 08:15:41 +0000439 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000440 f->installName = lc + read32(&dl->dylib.name, isBig);
Jean-Daniel Dupasedefccc2014-12-20 09:22:56 +0000441 f->currentVersion = read32(&dl->dylib.current_version, isBig);
442 f->compatVersion = read32(&dl->dylib.compatibility_version, isBig);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000443 }
444 break;
445 case LC_DATA_IN_CODE: {
Nick Kledzik21921372014-07-24 23:06:56 +0000446 const linkedit_data_command *ldc =
447 reinterpret_cast<const linkedit_data_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000448 dataInCode = reinterpret_cast<const data_in_code_entry *>(
449 start + read32(&ldc->dataoff, isBig));
450 dataInCodeSize = read32(&ldc->datasize, isBig);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000451 }
Nick Kledzik141330a2014-09-03 19:52:50 +0000452 break;
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000453 case LC_LOAD_DYLIB:
454 case LC_LOAD_WEAK_DYLIB:
455 case LC_REEXPORT_DYLIB:
456 case LC_LOAD_UPWARD_DYLIB: {
457 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
458 DependentDylib entry;
Tim Northover40d3ad32014-10-27 22:48:35 +0000459 entry.path = lc + read32(&dl->dylib.name, isBig);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000460 entry.kind = LoadCommandType(cmd);
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000461 entry.compatVersion = read32(&dl->dylib.compatibility_version, isBig);
462 entry.currentVersion = read32(&dl->dylib.current_version, isBig);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000463 f->dependentDylibs.push_back(entry);
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000464 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000465 break;
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000466 case LC_RPATH: {
467 const rpath_command *rpc = reinterpret_cast<const rpath_command *>(lc);
468 f->rpaths.push_back(lc + read32(&rpc->path, isBig));
469 }
470 break;
Nick Kledzik141330a2014-09-03 19:52:50 +0000471 case LC_DYLD_INFO:
472 case LC_DYLD_INFO_ONLY:
473 dyldInfo = reinterpret_cast<const dyld_info_command*>(lc);
474 break;
Tim Northover301c4e62014-07-01 08:15:41 +0000475 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000476 return false;
477 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000478 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000479 return ec;
480
Nick Kledzik21921372014-07-24 23:06:56 +0000481 if (dataInCode) {
482 // Convert on-disk data_in_code_entry array to DataInCode vector.
483 for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); ++i) {
484 DataInCode entry;
Tim Northover40d3ad32014-10-27 22:48:35 +0000485 entry.offset = read32(&dataInCode[i].offset, isBig);
486 entry.length = read16(&dataInCode[i].length, isBig);
487 entry.kind =
Simon Atanasyan55c26992014-11-14 07:15:43 +0000488 (DataRegionType)read16((const uint8_t *)&dataInCode[i].kind, isBig);
Nick Kledzik21921372014-07-24 23:06:56 +0000489 f->dataInCode.push_back(entry);
490 }
491 }
492
Nick Kledzik141330a2014-09-03 19:52:50 +0000493 if (dyldInfo) {
494 // If any exports, extract and add to normalized exportInfo vector.
495 if (dyldInfo->export_size) {
496 const uint8_t *trieStart = reinterpret_cast<const uint8_t*>(start +
497 dyldInfo->export_off);
498 ArrayRef<uint8_t> trie(trieStart, dyldInfo->export_size);
499 for (const ExportEntry &trieExport : MachOObjectFile::exports(trie)) {
500 Export normExport;
501 normExport.name = trieExport.name().copy(f->ownedAllocations);
502 normExport.offset = trieExport.address();
503 normExport.kind = ExportSymbolKind(trieExport.flags() & EXPORT_SYMBOL_FLAGS_KIND_MASK);
504 normExport.flags = trieExport.flags() & ~EXPORT_SYMBOL_FLAGS_KIND_MASK;
505 normExport.otherOffset = trieExport.other();
506 if (!trieExport.otherName().empty())
507 normExport.otherName = trieExport.otherName().copy(f->ownedAllocations);
508 f->exportInfo.push_back(normExport);
509 }
510 }
511 }
512
Nick Kledzike34182f2013-11-06 21:36:55 +0000513 return std::move(f);
514}
515
Rui Ueyama1d510422014-12-12 07:31:09 +0000516class MachOObjectReader : public Reader {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000517public:
Rui Ueyama1d510422014-12-12 07:31:09 +0000518 MachOObjectReader(MachOLinkingContext &ctx) : _ctx(ctx) {}
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000519
Rui Ueyama55f5b2b2015-04-04 02:44:36 +0000520 bool canParse(file_magic magic, const MemoryBuffer &mb) const override {
521 return (magic == llvm::sys::fs::file_magic::macho_object &&
522 mb.getBufferSize() > 32);
Rui Ueyama1d510422014-12-12 07:31:09 +0000523 }
524
Rafael Espindoladedab912015-04-24 18:33:50 +0000525 ErrorOr<std::unique_ptr<File>>
526 loadFile(std::unique_ptr<MemoryBuffer> mb,
527 const Registry &registry) const override {
528 std::unique_ptr<File> ret =
529 llvm::make_unique<MachOFile>(std::move(mb), &_ctx);
530 return std::move(ret);
Rui Ueyama1d510422014-12-12 07:31:09 +0000531 }
532
533private:
534 MachOLinkingContext &_ctx;
535};
536
537class MachODylibReader : public Reader {
538public:
539 MachODylibReader(MachOLinkingContext &ctx) : _ctx(ctx) {}
540
Rui Ueyama55f5b2b2015-04-04 02:44:36 +0000541 bool canParse(file_magic magic, const MemoryBuffer &mb) const override {
Rui Ueyama1d510422014-12-12 07:31:09 +0000542 switch (magic) {
Nick Kledzik14b5d202014-10-08 01:48:10 +0000543 case llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib:
544 case llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
Rui Ueyama55f5b2b2015-04-04 02:44:36 +0000545 return mb.getBufferSize() > 32;
Nick Kledzik14b5d202014-10-08 01:48:10 +0000546 default:
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000547 return false;
Nick Kledzik14b5d202014-10-08 01:48:10 +0000548 }
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000549 }
550
Rafael Espindoladedab912015-04-24 18:33:50 +0000551 ErrorOr<std::unique_ptr<File>>
552 loadFile(std::unique_ptr<MemoryBuffer> mb,
553 const Registry &registry) const override {
554 std::unique_ptr<File> ret =
555 llvm::make_unique<MachODylibFile>(std::move(mb), &_ctx);
556 return std::move(ret);
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000557 }
Rui Ueyama1d510422014-12-12 07:31:09 +0000558
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000559private:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000560 MachOLinkingContext &_ctx;
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000561};
562
Nick Kledzike34182f2013-11-06 21:36:55 +0000563} // namespace normalized
564} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000565
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000566void Registry::addSupportMachOObjects(MachOLinkingContext &ctx) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000567 MachOLinkingContext::Arch arch = ctx.arch();
Rui Ueyama1d510422014-12-12 07:31:09 +0000568 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOObjectReader(ctx)));
569 add(std::unique_ptr<Reader>(new mach_o::normalized::MachODylibReader(ctx)));
Shankar Easwarana1d36372015-02-22 23:54:38 +0000570 addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(),
Nick Kledzik2458bec2014-07-16 19:49:02 +0000571 ctx.archHandler().kindStrings());
Nick Kledzik6edd7222014-01-11 01:07:43 +0000572 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
Nick Kledzik378066c2014-06-30 22:57:33 +0000573 new mach_o::MachOYamlIOTaggedDocumentHandler(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000574}
575
Nick Kledzik14b5d202014-10-08 01:48:10 +0000576
Nick Kledzike34182f2013-11-06 21:36:55 +0000577} // namespace lld