blob: 042cca7125d7cdb79de5ddbf997409497622d8e2 [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"
33#include "llvm/ADT/Twine.h"
Nick Kledzik141330a2014-09-03 19:52:50 +000034#include "llvm/Object/MachO.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000035#include "llvm/Support/Casting.h"
Rafael Espindola372bc702014-06-13 17:20:48 +000036#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/FileOutputBuffer.h"
39#include "llvm/Support/Host.h"
40#include "llvm/Support/MachO.h"
41#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/Support/raw_ostream.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000043#include <functional>
Rafael Espindola54427cc2014-06-12 17:15:58 +000044#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000045
46using namespace llvm::MachO;
Nick Kledzik141330a2014-09-03 19:52:50 +000047using llvm::object::ExportEntry;
48using llvm::object::MachOObjectFile;
Nick Kledzike34182f2013-11-06 21:36:55 +000049
50namespace lld {
51namespace mach_o {
52namespace normalized {
53
54// Utility to call a lambda expression on each load command.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000055static std::error_code forEachLoadCommand(
Tim Northover40d3ad32014-10-27 22:48:35 +000056 StringRef lcRange, unsigned lcCount, bool isBig, bool is64,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000057 std::function<bool(uint32_t cmd, uint32_t size, const char *lc)> func) {
Nick Kledzike34182f2013-11-06 21:36:55 +000058 const char* p = lcRange.begin();
59 for (unsigned i=0; i < lcCount; ++i) {
60 const load_command *lc = reinterpret_cast<const load_command*>(p);
61 load_command lcCopy;
62 const load_command *slc = lc;
Tim Northover40d3ad32014-10-27 22:48:35 +000063 if (isBig != llvm::sys::IsBigEndianHost) {
Nick Kledzike34182f2013-11-06 21:36:55 +000064 memcpy(&lcCopy, lc, sizeof(load_command));
65 swapStruct(lcCopy);
66 slc = &lcCopy;
67 }
68 if ( (p + slc->cmdsize) > lcRange.end() )
Rafael Espindola372bc702014-06-13 17:20:48 +000069 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000070
Nick Kledzike34182f2013-11-06 21:36:55 +000071 if (func(slc->cmd, slc->cmdsize, p))
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000072 return std::error_code();
Shankar Easwaran3d8de472014-01-27 03:09:26 +000073
Nick Kledzike34182f2013-11-06 21:36:55 +000074 p += slc->cmdsize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +000075 }
76
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000077 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000078}
79
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000080static std::error_code appendRelocations(Relocations &relocs, StringRef buffer,
Tim Northover40d3ad32014-10-27 22:48:35 +000081 bool bigEndian,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000082 uint32_t reloff, uint32_t nreloc) {
Nick Kledzike34182f2013-11-06 21:36:55 +000083 if ((reloff + nreloc*8) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +000084 return make_error_code(llvm::errc::executable_format_error);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000085 const any_relocation_info* relocsArray =
Joey Gouly010b3762014-01-14 22:32:38 +000086 reinterpret_cast<const any_relocation_info*>(buffer.begin()+reloff);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000087
Nick Kledzike34182f2013-11-06 21:36:55 +000088 for(uint32_t i=0; i < nreloc; ++i) {
Tim Northover40d3ad32014-10-27 22:48:35 +000089 relocs.push_back(unpackRelocation(relocsArray[i], bigEndian));
Nick Kledzike34182f2013-11-06 21:36:55 +000090 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000091 return std::error_code();
Nick Kledzike34182f2013-11-06 21:36:55 +000092}
93
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +000094static std::error_code
Tim Northover40d3ad32014-10-27 22:48:35 +000095appendIndirectSymbols(IndirectSymbols &isyms, StringRef buffer, bool isBig,
96 uint32_t istOffset, uint32_t istCount,
Nick Kledzik388f3d02014-05-28 01:16:35 +000097 uint32_t startIndex, uint32_t count) {
98 if ((istOffset + istCount*4) > buffer.size())
Rafael Espindola372bc702014-06-13 17:20:48 +000099 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000100 if (startIndex+count > istCount)
Rafael Espindola372bc702014-06-13 17:20:48 +0000101 return make_error_code(llvm::errc::executable_format_error);
Tim Northover40d3ad32014-10-27 22:48:35 +0000102 const uint8_t *indirectSymbolArray = (const uint8_t *)buffer.data();
Nick Kledzik388f3d02014-05-28 01:16:35 +0000103
104 for(uint32_t i=0; i < count; ++i) {
Tim Northover40d3ad32014-10-27 22:48:35 +0000105 isyms.push_back(read32(
106 indirectSymbolArray + (startIndex + i) * sizeof(uint32_t), isBig));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000107 }
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000108 return std::error_code();
Nick Kledzik388f3d02014-05-28 01:16:35 +0000109}
110
111
Joey Gouly010b3762014-01-14 22:32:38 +0000112template <typename T> static T readBigEndian(T t) {
113 if (llvm::sys::IsLittleEndianHost)
Artyom Skrobovf8874b02014-06-14 13:26:14 +0000114 llvm::sys::swapByteOrder(t);
Joey Gouly010b3762014-01-14 22:32:38 +0000115 return t;
116}
Nick Kledzike34182f2013-11-06 21:36:55 +0000117
Nick Kledzik635f9c72014-09-04 20:08:30 +0000118
Tim Northover40d3ad32014-10-27 22:48:35 +0000119static bool isMachOHeader(const mach_header *mh, bool &is64, bool &isBig) {
120 switch (read32(&mh->magic, false)) {
Nick Kledzik635f9c72014-09-04 20:08:30 +0000121 case llvm::MachO::MH_MAGIC:
122 is64 = false;
Tim Northover40d3ad32014-10-27 22:48:35 +0000123 isBig = false;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000124 return true;
125 case llvm::MachO::MH_MAGIC_64:
126 is64 = true;
Tim Northover40d3ad32014-10-27 22:48:35 +0000127 isBig = false;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000128 return true;
129 case llvm::MachO::MH_CIGAM:
130 is64 = false;
Tim Northover40d3ad32014-10-27 22:48:35 +0000131 isBig = true;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000132 return true;
133 case llvm::MachO::MH_CIGAM_64:
134 is64 = true;
Tim Northover40d3ad32014-10-27 22:48:35 +0000135 isBig = true;
Nick Kledzik635f9c72014-09-04 20:08:30 +0000136 return true;
137 default:
138 return false;
139 }
140}
141
142
143bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch) {
144 // Try opening and mapping file at path.
145 ErrorOr<std::unique_ptr<MemoryBuffer>> b = MemoryBuffer::getFileOrSTDIN(path);
146 if (b.getError())
147 return false;
148
149 // If file length < 32 it is too small to be mach-o object file.
150 StringRef fileBuffer = b->get()->getBuffer();
151 if (fileBuffer.size() < 32)
152 return false;
153
154 // If file buffer does not start with MH_MAGIC (and variants), not obj file.
155 const mach_header *mh = reinterpret_cast<const mach_header *>(
156 fileBuffer.begin());
Tim Northover40d3ad32014-10-27 22:48:35 +0000157 bool is64, isBig;
158 if (!isMachOHeader(mh, is64, isBig))
Nick Kledzik635f9c72014-09-04 20:08:30 +0000159 return false;
160
161 // If not MH_OBJECT, not object file.
Tim Northover40d3ad32014-10-27 22:48:35 +0000162 if (read32(&mh->filetype, isBig) != MH_OBJECT)
Nick Kledzik635f9c72014-09-04 20:08:30 +0000163 return false;
164
165 // Lookup up arch from cpu/subtype pair.
Tim Northover40d3ad32014-10-27 22:48:35 +0000166 arch = MachOLinkingContext::archFromCpuType(
167 read32(&mh->cputype, isBig),
168 read32(&mh->cpusubtype, isBig));
Nick Kledzik635f9c72014-09-04 20:08:30 +0000169 return true;
170}
171
Nick Kledzik14b5d202014-10-08 01:48:10 +0000172
173bool sliceFromFatFile(const MemoryBuffer &mb, MachOLinkingContext::Arch arch,
174 uint32_t &offset, uint32_t &size) {
175 const char *start = mb.getBufferStart();
176 const llvm::MachO::fat_header *fh =
177 reinterpret_cast<const llvm::MachO::fat_header *>(start);
178 if (readBigEndian(fh->magic) != llvm::MachO::FAT_MAGIC)
179 return false;
180 uint32_t nfat_arch = readBigEndian(fh->nfat_arch);
181 const fat_arch *fstart =
182 reinterpret_cast<const fat_arch *>(start + sizeof(fat_header));
183 const fat_arch *fend =
184 reinterpret_cast<const fat_arch *>(start + sizeof(fat_header) +
185 sizeof(fat_arch) * nfat_arch);
186 const uint32_t reqCpuType = MachOLinkingContext::cpuTypeFromArch(arch);
187 const uint32_t reqCpuSubtype = MachOLinkingContext::cpuSubtypeFromArch(arch);
188 for (const fat_arch *fa = fstart; fa < fend; ++fa) {
189 if ((readBigEndian(fa->cputype) == reqCpuType) &&
190 (readBigEndian(fa->cpusubtype) == reqCpuSubtype)) {
191 offset = readBigEndian(fa->offset);
192 size = readBigEndian(fa->size);
193 if ((offset + size) > mb.getBufferSize())
194 return false;
195 return true;
196 }
197 }
198 return false;
199}
200
Nick Kledzike34182f2013-11-06 21:36:55 +0000201/// Reads a mach-o file and produces an in-memory normalized view.
Joey Gouly010b3762014-01-14 22:32:38 +0000202ErrorOr<std::unique_ptr<NormalizedFile>>
203readBinary(std::unique_ptr<MemoryBuffer> &mb,
204 const MachOLinkingContext::Arch arch) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000205 // Make empty NormalizedFile.
206 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
207
Joey Gouly010b3762014-01-14 22:32:38 +0000208 const char *start = mb->getBufferStart();
209 size_t objSize = mb->getBufferSize();
Joey Gouly010b3762014-01-14 22:32:38 +0000210 const mach_header *mh = reinterpret_cast<const mach_header *>(start);
Nick Kledzik14b5d202014-10-08 01:48:10 +0000211
212 uint32_t sliceOffset;
213 uint32_t sliceSize;
214 if (sliceFromFatFile(*mb, arch, sliceOffset, sliceSize)) {
215 start = &start[sliceOffset];
216 objSize = sliceSize;
Joey Gouly010b3762014-01-14 22:32:38 +0000217 mh = reinterpret_cast<const mach_header *>(start);
218 }
219
Nick Kledzik14b5d202014-10-08 01:48:10 +0000220 // Determine endianness and pointer size for mach-o file.
Tim Northover40d3ad32014-10-27 22:48:35 +0000221 bool is64, isBig;
222 if (!isMachOHeader(mh, is64, isBig))
Rafael Espindola372bc702014-06-13 17:20:48 +0000223 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000224
225 // Endian swap header, if needed.
226 mach_header headerCopy;
227 const mach_header *smh = mh;
Tim Northover40d3ad32014-10-27 22:48:35 +0000228 if (isBig != llvm::sys::IsBigEndianHost) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000229 memcpy(&headerCopy, mh, sizeof(mach_header));
230 swapStruct(headerCopy);
231 smh = &headerCopy;
232 }
233
234 // Validate head and load commands fit in buffer.
235 const uint32_t lcCount = smh->ncmds;
Joey Gouly010b3762014-01-14 22:32:38 +0000236 const char *lcStart =
237 start + (is64 ? sizeof(mach_header_64) : sizeof(mach_header));
Nick Kledzike34182f2013-11-06 21:36:55 +0000238 StringRef lcRange(lcStart, smh->sizeofcmds);
Joey Gouly010b3762014-01-14 22:32:38 +0000239 if (lcRange.end() > (start + objSize))
Rafael Espindola372bc702014-06-13 17:20:48 +0000240 return make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000241
Nick Kledzik378066c2014-06-30 22:57:33 +0000242 // Get architecture from mach_header.
Nick Kledzike34182f2013-11-06 21:36:55 +0000243 f->arch = MachOLinkingContext::archFromCpuType(smh->cputype, smh->cpusubtype);
Nick Kledzik378066c2014-06-30 22:57:33 +0000244 if (f->arch != arch) {
245 return make_dynamic_error_code(Twine("file is wrong architecture. Expected "
246 "(" + MachOLinkingContext::nameFromArch(arch)
247 + ") found ("
248 + MachOLinkingContext::nameFromArch(f->arch)
249 + ")" ));
250 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000251 // Copy file type and flags
252 f->fileType = HeaderFileType(smh->filetype);
253 f->flags = smh->flags;
254
255
Nick Kledzik388f3d02014-05-28 01:16:35 +0000256 // Pre-scan load commands looking for indirect symbol table.
257 uint32_t indirectSymbolTableOffset = 0;
258 uint32_t indirectSymbolTableCount = 0;
Tim Northover40d3ad32014-10-27 22:48:35 +0000259 std::error_code ec = forEachLoadCommand(lcRange, lcCount, isBig, is64,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000260 [&](uint32_t cmd, uint32_t size,
261 const char *lc) -> bool {
Nick Kledzik388f3d02014-05-28 01:16:35 +0000262 if (cmd == LC_DYSYMTAB) {
263 const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000264 indirectSymbolTableOffset = read32(&d->indirectsymoff, isBig);
265 indirectSymbolTableCount = read32(&d->nindirectsyms, isBig);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000266 return true;
267 }
268 return false;
269 });
270 if (ec)
271 return ec;
272
273 // Walk load commands looking for segments/sections and the symbol table.
Nick Kledzik21921372014-07-24 23:06:56 +0000274 const data_in_code_entry *dataInCode = nullptr;
Nick Kledzik141330a2014-09-03 19:52:50 +0000275 const dyld_info_command *dyldInfo = nullptr;
Nick Kledzik21921372014-07-24 23:06:56 +0000276 uint32_t dataInCodeSize = 0;
Tim Northover40d3ad32014-10-27 22:48:35 +0000277 ec = forEachLoadCommand(lcRange, lcCount, isBig, is64,
Nick Kledzik388f3d02014-05-28 01:16:35 +0000278 [&] (uint32_t cmd, uint32_t size, const char* lc) -> bool {
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000279 switch(cmd) {
280 case LC_SEGMENT_64:
281 if (is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000282 const segment_command_64 *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000283 reinterpret_cast<const segment_command_64*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000284 const unsigned sectionCount = read32(&seg->nsects, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000285 const section_64 *sects = reinterpret_cast<const section_64*>
286 (lc + sizeof(segment_command_64));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000287 const unsigned lcSize = sizeof(segment_command_64)
Nick Kledzike34182f2013-11-06 21:36:55 +0000288 + sectionCount*sizeof(section_64);
289 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000290 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000291 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000292 for (unsigned i=0; i < sectionCount; ++i) {
293 const section_64 *sect = &sects[i];
294 Section section;
295 section.segmentName = getString16(sect->segname);
296 section.sectionName = getString16(sect->sectname);
Tim Northover40d3ad32014-10-27 22:48:35 +0000297 section.type = (SectionType)(read32(&sect->flags, isBig) &
298 SECTION_TYPE);
299 section.attributes = read32(&sect->flags, isBig) & SECTION_ATTRIBUTES;
300 section.alignment = read32(&sect->align, isBig);
301 section.address = read64(&sect->addr, isBig);
Joey Gouly010b3762014-01-14 22:32:38 +0000302 const uint8_t *content =
Simon Atanasyan55c26992014-11-14 07:15:43 +0000303 (const uint8_t *)start + read32(&sect->offset, isBig);
Tim Northover40d3ad32014-10-27 22:48:35 +0000304 size_t contentSize = read64(&sect->size, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000305 // Note: this assign() is copying the content bytes. Ideally,
306 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000307 section.content = llvm::makeArrayRef(content, contentSize);
Tim Northover40d3ad32014-10-27 22:48:35 +0000308 appendRelocations(section.relocations, mb->getBuffer(), isBig,
309 read32(&sect->reloff, isBig),
310 read32(&sect->nreloc, isBig));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000311 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
312 appendIndirectSymbols(section.indirectSymbols, mb->getBuffer(),
Tim Northover40d3ad32014-10-27 22:48:35 +0000313 isBig,
Nick Kledzik388f3d02014-05-28 01:16:35 +0000314 indirectSymbolTableOffset,
315 indirectSymbolTableCount,
Tim Northover40d3ad32014-10-27 22:48:35 +0000316 read32(&sect->reserved1, isBig),
317 contentSize/4);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000318 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000319 f->sections.push_back(section);
320 }
321 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000322 break;
323 case LC_SEGMENT:
324 if (!is64) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000325 const segment_command *seg =
Nick Kledzike34182f2013-11-06 21:36:55 +0000326 reinterpret_cast<const segment_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000327 const unsigned sectionCount = read32(&seg->nsects, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000328 const section *sects = reinterpret_cast<const section*>
329 (lc + sizeof(segment_command));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000330 const unsigned lcSize = sizeof(segment_command)
Nick Kledzike34182f2013-11-06 21:36:55 +0000331 + sectionCount*sizeof(section);
332 // Verify sections don't extend beyond end of segment load command.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000333 if (lcSize > size)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000334 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000335 for (unsigned i=0; i < sectionCount; ++i) {
336 const section *sect = &sects[i];
337 Section section;
338 section.segmentName = getString16(sect->segname);
339 section.sectionName = getString16(sect->sectname);
Tim Northover40d3ad32014-10-27 22:48:35 +0000340 section.type = (SectionType)(read32(&sect->flags, isBig) &
341 SECTION_TYPE);
342 section.attributes =
Simon Atanasyan55c26992014-11-14 07:15:43 +0000343 read32((const uint8_t *)&sect->flags, isBig) & SECTION_ATTRIBUTES;
Tim Northover40d3ad32014-10-27 22:48:35 +0000344 section.alignment = read32(&sect->align, isBig);
345 section.address = read32(&sect->addr, isBig);
Joey Gouly010b3762014-01-14 22:32:38 +0000346 const uint8_t *content =
Simon Atanasyan55c26992014-11-14 07:15:43 +0000347 (const uint8_t *)start + read32(&sect->offset, isBig);
Tim Northover40d3ad32014-10-27 22:48:35 +0000348 size_t contentSize = read32(&sect->size, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000349 // Note: this assign() is copying the content bytes. Ideally,
350 // we can use a custom allocator for vector to avoid the copy.
Nick Kledzik6edd7222014-01-11 01:07:43 +0000351 section.content = llvm::makeArrayRef(content, contentSize);
Tim Northover40d3ad32014-10-27 22:48:35 +0000352 appendRelocations(section.relocations, mb->getBuffer(), isBig,
353 read32(&sect->reloff, isBig),
354 read32(&sect->nreloc, isBig));
Nick Kledzik388f3d02014-05-28 01:16:35 +0000355 if (section.type == S_NON_LAZY_SYMBOL_POINTERS) {
Tim Northover40d3ad32014-10-27 22:48:35 +0000356 appendIndirectSymbols(
357 section.indirectSymbols, mb->getBuffer(), isBig,
358 indirectSymbolTableOffset, indirectSymbolTableCount,
359 read32(&sect->reserved1, isBig), contentSize / 4);
Nick Kledzik388f3d02014-05-28 01:16:35 +0000360 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000361 f->sections.push_back(section);
362 }
363 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000364 break;
365 case LC_SYMTAB: {
Nick Kledzike34182f2013-11-06 21:36:55 +0000366 const symtab_command *st = reinterpret_cast<const symtab_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000367 const char *strings = start + read32(&st->stroff, isBig);
368 const uint32_t strSize = read32(&st->strsize, isBig);
Nick Kledzike34182f2013-11-06 21:36:55 +0000369 // Validate string pool and symbol table all in buffer.
Simon Atanasyan55c26992014-11-14 07:15:43 +0000370 if (read32((const uint8_t *)&st->stroff, isBig) +
371 read32((const uint8_t *)&st->strsize, isBig) >
Tim Northover40d3ad32014-10-27 22:48:35 +0000372 objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000373 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000374 if (is64) {
Tim Northover40d3ad32014-10-27 22:48:35 +0000375 const uint32_t symOffset = read32(&st->symoff, isBig);
376 const uint32_t symCount = read32(&st->nsyms, isBig);
Joey Gouly010b3762014-01-14 22:32:38 +0000377 if ( symOffset+(symCount*sizeof(nlist_64)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000378 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000379 const nlist_64 *symbols =
380 reinterpret_cast<const nlist_64 *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000381 // Convert each nlist_64 to a lld::mach_o::normalized::Symbol.
382 for(uint32_t i=0; i < symCount; ++i) {
383 const nlist_64 *sin = &symbols[i];
384 nlist_64 tempSym;
Tim Northover40d3ad32014-10-27 22:48:35 +0000385 if (isBig != llvm::sys::IsBigEndianHost) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000386 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
387 }
388 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000389 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000390 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000391 sout.name = &strings[sin->n_strx];
392 sout.type = (NListType)(sin->n_type & N_TYPE);
393 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
394 sout.sect = sin->n_sect;
395 sout.desc = sin->n_desc;
396 sout.value = sin->n_value;
397 if (sout.type == N_UNDF)
398 f->undefinedSymbols.push_back(sout);
Nick Kledzik3f690762014-06-27 18:25:01 +0000399 else if (sin->n_type & N_EXT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000400 f->globalSymbols.push_back(sout);
401 else
402 f->localSymbols.push_back(sout);
403 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000404 } else {
Tim Northover40d3ad32014-10-27 22:48:35 +0000405 const uint32_t symOffset = read32(&st->symoff, isBig);
406 const uint32_t symCount = read32(&st->nsyms, isBig);
Joey Gouly010b3762014-01-14 22:32:38 +0000407 if ( symOffset+(symCount*sizeof(nlist)) > objSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000408 return true;
Joey Gouly010b3762014-01-14 22:32:38 +0000409 const nlist *symbols =
410 reinterpret_cast<const nlist *>(start + symOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +0000411 // Convert each nlist to a lld::mach_o::normalized::Symbol.
412 for(uint32_t i=0; i < symCount; ++i) {
413 const nlist *sin = &symbols[i];
414 nlist tempSym;
Tim Northover40d3ad32014-10-27 22:48:35 +0000415 if (isBig != llvm::sys::IsBigEndianHost) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000416 tempSym = *sin; swapStruct(tempSym); sin = &tempSym;
417 }
418 Symbol sout;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000419 if (sin->n_strx > strSize)
Rafael Espindola1d364c12014-06-03 04:41:30 +0000420 return true;
Nick Kledzike34182f2013-11-06 21:36:55 +0000421 sout.name = &strings[sin->n_strx];
422 sout.type = (NListType)(sin->n_type & N_TYPE);
423 sout.scope = (sin->n_type & (N_PEXT|N_EXT));
424 sout.sect = sin->n_sect;
425 sout.desc = sin->n_desc;
426 sout.value = sin->n_value;
427 if (sout.type == N_UNDF)
428 f->undefinedSymbols.push_back(sout);
429 else if (sout.scope == (SymbolScope)N_EXT)
430 f->globalSymbols.push_back(sout);
431 else
432 f->localSymbols.push_back(sout);
433 }
434 }
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000435 }
436 break;
437 case LC_ID_DYLIB: {
Tim Northover301c4e62014-07-01 08:15:41 +0000438 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000439 f->installName = lc + read32(&dl->dylib.name, isBig);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000440 }
441 break;
442 case LC_DATA_IN_CODE: {
Nick Kledzik21921372014-07-24 23:06:56 +0000443 const linkedit_data_command *ldc =
444 reinterpret_cast<const linkedit_data_command*>(lc);
Tim Northover40d3ad32014-10-27 22:48:35 +0000445 dataInCode = reinterpret_cast<const data_in_code_entry *>(
446 start + read32(&ldc->dataoff, isBig));
447 dataInCodeSize = read32(&ldc->datasize, isBig);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000448 }
Nick Kledzik141330a2014-09-03 19:52:50 +0000449 break;
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000450 case LC_LOAD_DYLIB:
451 case LC_LOAD_WEAK_DYLIB:
452 case LC_REEXPORT_DYLIB:
453 case LC_LOAD_UPWARD_DYLIB: {
454 const dylib_command *dl = reinterpret_cast<const dylib_command*>(lc);
455 DependentDylib entry;
Tim Northover40d3ad32014-10-27 22:48:35 +0000456 entry.path = lc + read32(&dl->dylib.name, isBig);
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000457 entry.kind = LoadCommandType(cmd);
458 f->dependentDylibs.push_back(entry);
459 }
460 break;
Nick Kledzik141330a2014-09-03 19:52:50 +0000461 case LC_DYLD_INFO:
462 case LC_DYLD_INFO_ONLY:
463 dyldInfo = reinterpret_cast<const dyld_info_command*>(lc);
464 break;
Tim Northover301c4e62014-07-01 08:15:41 +0000465 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000466 return false;
467 });
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000468 if (ec)
Nick Kledzike34182f2013-11-06 21:36:55 +0000469 return ec;
470
Nick Kledzik21921372014-07-24 23:06:56 +0000471 if (dataInCode) {
472 // Convert on-disk data_in_code_entry array to DataInCode vector.
473 for (unsigned i=0; i < dataInCodeSize/sizeof(data_in_code_entry); ++i) {
474 DataInCode entry;
Tim Northover40d3ad32014-10-27 22:48:35 +0000475 entry.offset = read32(&dataInCode[i].offset, isBig);
476 entry.length = read16(&dataInCode[i].length, isBig);
477 entry.kind =
Simon Atanasyan55c26992014-11-14 07:15:43 +0000478 (DataRegionType)read16((const uint8_t *)&dataInCode[i].kind, isBig);
Nick Kledzik21921372014-07-24 23:06:56 +0000479 f->dataInCode.push_back(entry);
480 }
481 }
482
Nick Kledzik141330a2014-09-03 19:52:50 +0000483 if (dyldInfo) {
484 // If any exports, extract and add to normalized exportInfo vector.
485 if (dyldInfo->export_size) {
486 const uint8_t *trieStart = reinterpret_cast<const uint8_t*>(start +
487 dyldInfo->export_off);
488 ArrayRef<uint8_t> trie(trieStart, dyldInfo->export_size);
489 for (const ExportEntry &trieExport : MachOObjectFile::exports(trie)) {
490 Export normExport;
491 normExport.name = trieExport.name().copy(f->ownedAllocations);
492 normExport.offset = trieExport.address();
493 normExport.kind = ExportSymbolKind(trieExport.flags() & EXPORT_SYMBOL_FLAGS_KIND_MASK);
494 normExport.flags = trieExport.flags() & ~EXPORT_SYMBOL_FLAGS_KIND_MASK;
495 normExport.otherOffset = trieExport.other();
496 if (!trieExport.otherName().empty())
497 normExport.otherName = trieExport.otherName().copy(f->ownedAllocations);
498 f->exportInfo.push_back(normExport);
499 }
500 }
501 }
502
Nick Kledzike34182f2013-11-06 21:36:55 +0000503 return std::move(f);
504}
505
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000506class MachOReader : public Reader {
507public:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000508 MachOReader(MachOLinkingContext &ctx) : _ctx(ctx) {}
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000509
510 bool canParse(file_magic magic, StringRef ext,
511 const MemoryBuffer &mb) const override {
Nick Kledzik14b5d202014-10-08 01:48:10 +0000512 switch (magic) {
513 case llvm::sys::fs::file_magic::macho_object:
514 case llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib:
515 case llvm::sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
516 return (mb.getBufferSize() > 32);
517 default:
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000518 return false;
Nick Kledzik14b5d202014-10-08 01:48:10 +0000519 }
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000520 }
521
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000522 std::error_code
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000523 parseFile(std::unique_ptr<MemoryBuffer> &mb, const Registry &registry,
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000524 std::vector<std::unique_ptr<File>> &result) const override {
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000525 // Convert binary file to normalized mach-o.
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000526 auto normFile = readBinary(mb, _ctx.arch());
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000527 if (std::error_code ec = normFile.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000528 return ec;
529 // Convert normalized mach-o to atoms.
530 auto file = normalizedToAtoms(**normFile, mb->getBufferIdentifier(), false);
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000531 if (std::error_code ec = file.getError())
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000532 return ec;
533
534 result.push_back(std::move(*file));
535
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000536 return std::error_code();
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000537 }
538private:
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000539 MachOLinkingContext &_ctx;
Nick Kledzikcc28bc12014-05-30 01:13:49 +0000540};
541
542
Nick Kledzike34182f2013-11-06 21:36:55 +0000543} // namespace normalized
544} // namespace mach_o
Nick Kledzike5552772013-12-19 21:58:00 +0000545
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000546void Registry::addSupportMachOObjects(MachOLinkingContext &ctx) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000547 MachOLinkingContext::Arch arch = ctx.arch();
Nick Kledzik8fc67fb2014-08-13 23:55:41 +0000548 add(std::unique_ptr<Reader>(new mach_o::normalized::MachOReader(ctx)));
Nick Kledzik2458bec2014-07-16 19:49:02 +0000549 addKindTable(Reference::KindNamespace::mach_o, ctx.archHandler().kindArch(),
550 ctx.archHandler().kindStrings());
Nick Kledzik6edd7222014-01-11 01:07:43 +0000551 add(std::unique_ptr<YamlIOTaggedDocumentHandler>(
Nick Kledzik378066c2014-06-30 22:57:33 +0000552 new mach_o::MachOYamlIOTaggedDocumentHandler(arch)));
Nick Kledzike5552772013-12-19 21:58:00 +0000553}
554
Nick Kledzik14b5d202014-10-08 01:48:10 +0000555
Nick Kledzike34182f2013-11-06 21:36:55 +0000556} // namespace lld
557