blob: c0d7d83d08e026192e1cee47385544d517e7aa58 [file] [log] [blame]
Eric Christopher7b015c72011-04-22 03:19:48 +00001//===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the MachOObjectFile class, which binds the MachOObject
11// class to the generic ObjectFile wrapper.
12//
13//===----------------------------------------------------------------------===//
14
Owen Anderson27c579d2011-10-11 17:32:27 +000015#include "llvm/Object/MachO.h"
Tim Northover00ed9962014-03-29 10:18:08 +000016#include "llvm/ADT/STLExtras.h"
Rafael Espindola72318b42014-08-08 16:30:17 +000017#include "llvm/ADT/StringSwitch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/Triple.h"
Rafael Espindola421305a2013-04-07 20:01:29 +000019#include "llvm/Support/DataExtractor.h"
Nick Kledzikac431442014-09-12 21:34:15 +000020#include "llvm/Support/Debug.h"
Owen Andersonbc14bd32011-10-26 20:42:54 +000021#include "llvm/Support/Format.h"
Rafael Espindola56f976f2013-04-18 18:08:55 +000022#include "llvm/Support/Host.h"
Nick Kledzikd04bc352014-08-30 00:20:14 +000023#include "llvm/Support/LEB128.h"
24#include "llvm/Support/MachO.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000025#include "llvm/Support/MemoryBuffer.h"
Jakub Staszak84a0ae72013-08-21 01:20:11 +000026#include "llvm/Support/raw_ostream.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000027#include <cctype>
28#include <cstring>
29#include <limits>
30
31using namespace llvm;
32using namespace object;
33
Artyom Skrobov7d602f72014-07-20 12:08:28 +000034namespace {
35 struct section_base {
36 char sectname[16];
37 char segname[16];
38 };
39}
Rafael Espindola56f976f2013-04-18 18:08:55 +000040
Lang Hames9e964f32016-03-25 17:25:34 +000041static Error
Kevin Enderbyd4e075b2016-05-06 20:16:28 +000042malformedError(Twine Msg) {
Kevin Enderby89134962016-05-05 23:41:05 +000043 std::string StringMsg = "truncated or malformed object (" + Msg.str() + ")";
Kevin Enderbyd4e075b2016-05-06 20:16:28 +000044 return make_error<GenericBinaryError>(std::move(StringMsg),
Kevin Enderby89134962016-05-05 23:41:05 +000045 object_error::parse_failed);
Lang Hames9e964f32016-03-25 17:25:34 +000046}
47
Alexey Samsonov9f336632015-06-04 19:45:22 +000048// FIXME: Replace all uses of this function with getStructOrErr.
Filipe Cabecinhas40139502015-01-15 22:52:38 +000049template <typename T>
Artyom Skrobov7d602f72014-07-20 12:08:28 +000050static T getStruct(const MachOObjectFile *O, const char *P) {
Filipe Cabecinhas40139502015-01-15 22:52:38 +000051 // Don't read before the beginning or past the end of the file
52 if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
53 report_fatal_error("Malformed MachO file.");
54
Rafael Espindola3cdeb172013-04-19 13:45:05 +000055 T Cmd;
56 memcpy(&Cmd, P, sizeof(T));
57 if (O->isLittleEndian() != sys::IsLittleEndianHost)
Artyom Skrobov78d5daf2014-07-18 09:26:16 +000058 MachO::swapStruct(Cmd);
Rafael Espindola3cdeb172013-04-19 13:45:05 +000059 return Cmd;
Rafael Espindola56f976f2013-04-18 18:08:55 +000060}
61
Alexey Samsonov9f336632015-06-04 19:45:22 +000062template <typename T>
Lang Hames9e964f32016-03-25 17:25:34 +000063static Expected<T> getStructOrErr(const MachOObjectFile *O, const char *P) {
Alexey Samsonov9f336632015-06-04 19:45:22 +000064 // Don't read before the beginning or past the end of the file
65 if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
Kevin Enderbyd4e075b2016-05-06 20:16:28 +000066 return malformedError("Structure read out-of-range");
Alexey Samsonov9f336632015-06-04 19:45:22 +000067
68 T Cmd;
69 memcpy(&Cmd, P, sizeof(T));
70 if (O->isLittleEndian() != sys::IsLittleEndianHost)
71 MachO::swapStruct(Cmd);
72 return Cmd;
73}
74
Rafael Espindola6e040c02013-04-26 20:07:33 +000075static const char *
76getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
77 unsigned Sec) {
Rafael Espindola56f976f2013-04-18 18:08:55 +000078 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
79
80 bool Is64 = O->is64Bit();
Charles Davis8bdfafd2013-09-01 04:28:48 +000081 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
82 sizeof(MachO::segment_command);
83 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
84 sizeof(MachO::section);
Rafael Espindola56f976f2013-04-18 18:08:55 +000085
86 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
Charles Davis1827bd82013-08-27 05:38:30 +000087 return reinterpret_cast<const char*>(SectionAddr);
Rafael Espindola60689982013-04-07 19:05:30 +000088}
89
Rafael Espindola56f976f2013-04-18 18:08:55 +000090static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
91 return O->getData().substr(Offset, 1).data();
Rafael Espindola60689982013-04-07 19:05:30 +000092}
93
Artyom Skrobov78d5daf2014-07-18 09:26:16 +000094static MachO::nlist_base
Rafael Espindola56f976f2013-04-18 18:08:55 +000095getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola75c30362013-04-24 19:47:55 +000096 const char *P = reinterpret_cast<const char *>(DRI.p);
Artyom Skrobov78d5daf2014-07-18 09:26:16 +000097 return getStruct<MachO::nlist_base>(O, P);
Eric Christopher7b015c72011-04-22 03:19:48 +000098}
99
Rafael Espindola56f976f2013-04-18 18:08:55 +0000100static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000101 if (P[15] == 0)
102 // Null terminated.
103 return P;
104 // Not null terminated, so this is a 16 char string.
105 return StringRef(P, 16);
106}
107
Rafael Espindola56f976f2013-04-18 18:08:55 +0000108// Helper to advance a section or symbol iterator multiple increments at a time.
109template<class T>
Rafael Espindola5e812af2014-01-30 02:49:50 +0000110static void advance(T &it, size_t Val) {
111 while (Val--)
112 ++it;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000113}
114
115static unsigned getCPUType(const MachOObjectFile *O) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000116 return O->getHeader().cputype;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000117}
118
Charles Davis8bdfafd2013-09-01 04:28:48 +0000119static uint32_t
120getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
121 return RE.r_word0;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000122}
123
124static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000125getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
126 return RE.r_word0 & 0xffffff;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000127}
128
129static bool getPlainRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000130 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000131 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000132 return (RE.r_word1 >> 24) & 1;
133 return (RE.r_word1 >> 7) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000134}
135
136static bool
137getScatteredRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000138 const MachO::any_relocation_info &RE) {
139 return (RE.r_word0 >> 30) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000140}
141
142static unsigned getPlainRelocationLength(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000143 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000144 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000145 return (RE.r_word1 >> 25) & 3;
146 return (RE.r_word1 >> 5) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000147}
148
149static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000150getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
151 return (RE.r_word0 >> 28) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000152}
153
154static unsigned getPlainRelocationType(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000155 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000156 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000157 return RE.r_word1 >> 28;
158 return RE.r_word1 & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000159}
160
Rafael Espindola56f976f2013-04-18 18:08:55 +0000161static uint32_t getSectionFlags(const MachOObjectFile *O,
162 DataRefImpl Sec) {
163 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000164 MachO::section_64 Sect = O->getSection64(Sec);
165 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000166 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000167 MachO::section Sect = O->getSection(Sec);
168 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000169}
170
Lang Hames9e964f32016-03-25 17:25:34 +0000171static Expected<MachOObjectFile::LoadCommandInfo>
Kevin Enderbya8e3ab02016-05-03 23:13:50 +0000172getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr,
173 uint32_t LoadCommandIndex) {
Lang Hames9e964f32016-03-25 17:25:34 +0000174 if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) {
175 if (CmdOrErr->cmdsize < 8)
Kevin Enderbyd4e075b2016-05-06 20:16:28 +0000176 return malformedError("load command " + Twine(LoadCommandIndex) +
Kevin Enderby89134962016-05-05 23:41:05 +0000177 " with size less than 8 bytes");
Lang Hames9e964f32016-03-25 17:25:34 +0000178 return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr});
179 } else
180 return CmdOrErr.takeError();
Alexey Samsonov4fdbed32015-06-04 19:34:14 +0000181}
182
Lang Hames9e964f32016-03-25 17:25:34 +0000183static Expected<MachOObjectFile::LoadCommandInfo>
Alexey Samsonov4fdbed32015-06-04 19:34:14 +0000184getFirstLoadCommandInfo(const MachOObjectFile *Obj) {
185 unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64)
186 : sizeof(MachO::mach_header);
Kevin Enderby9d0c9452016-08-31 17:57:46 +0000187 if (sizeof(MachO::load_command) > Obj->getHeader().sizeofcmds)
Kevin Enderbyd4e075b2016-05-06 20:16:28 +0000188 return malformedError("load command 0 extends past the end all load "
Kevin Enderby89134962016-05-05 23:41:05 +0000189 "commands in the file");
Kevin Enderbya8e3ab02016-05-03 23:13:50 +0000190 return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0);
Alexey Samsonov4fdbed32015-06-04 19:34:14 +0000191}
192
Lang Hames9e964f32016-03-25 17:25:34 +0000193static Expected<MachOObjectFile::LoadCommandInfo>
Kevin Enderby368e7142016-05-03 17:16:08 +0000194getNextLoadCommandInfo(const MachOObjectFile *Obj, uint32_t LoadCommandIndex,
Alexey Samsonov4fdbed32015-06-04 19:34:14 +0000195 const MachOObjectFile::LoadCommandInfo &L) {
Kevin Enderby368e7142016-05-03 17:16:08 +0000196 unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64)
197 : sizeof(MachO::mach_header);
Kevin Enderby9d0c9452016-08-31 17:57:46 +0000198 if (L.Ptr + L.C.cmdsize + sizeof(MachO::load_command) >
Kevin Enderby368e7142016-05-03 17:16:08 +0000199 Obj->getData().data() + HeaderSize + Obj->getHeader().sizeofcmds)
Kevin Enderbyd4e075b2016-05-06 20:16:28 +0000200 return malformedError("load command " + Twine(LoadCommandIndex + 1) +
Kevin Enderby89134962016-05-05 23:41:05 +0000201 " extends past the end all load commands in the file");
Kevin Enderbya8e3ab02016-05-03 23:13:50 +0000202 return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1);
Alexey Samsonov4fdbed32015-06-04 19:34:14 +0000203}
204
Alexey Samsonov9f336632015-06-04 19:45:22 +0000205template <typename T>
206static void parseHeader(const MachOObjectFile *Obj, T &Header,
Lang Hames9e964f32016-03-25 17:25:34 +0000207 Error &Err) {
Kevin Enderby87025742016-04-13 21:17:58 +0000208 if (sizeof(T) > Obj->getData().size()) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +0000209 Err = malformedError("the mach header extends past the end of the "
Kevin Enderby89134962016-05-05 23:41:05 +0000210 "file");
Kevin Enderby87025742016-04-13 21:17:58 +0000211 return;
212 }
Lang Hames9e964f32016-03-25 17:25:34 +0000213 if (auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0)))
214 Header = *HeaderOrErr;
Alexey Samsonov9f336632015-06-04 19:45:22 +0000215 else
Lang Hames9e964f32016-03-25 17:25:34 +0000216 Err = HeaderOrErr.takeError();
Alexey Samsonov9f336632015-06-04 19:45:22 +0000217}
218
Alexey Samsonove1a76ab2015-06-04 22:08:37 +0000219// Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all
220// sections to \param Sections, and optionally sets
221// \param IsPageZeroSegment to true.
Kevin Enderbyc614d282016-08-12 20:10:25 +0000222template <typename Segment, typename Section>
Lang Hames9e964f32016-03-25 17:25:34 +0000223static Error parseSegmentLoadCommand(
Alexey Samsonove1a76ab2015-06-04 22:08:37 +0000224 const MachOObjectFile *Obj, const MachOObjectFile::LoadCommandInfo &Load,
Kevin Enderbyb34e3a12016-05-05 17:43:35 +0000225 SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment,
Kevin Enderbyc614d282016-08-12 20:10:25 +0000226 uint32_t LoadCommandIndex, const char *CmdName, uint64_t SizeOfHeaders) {
227 const unsigned SegmentLoadSize = sizeof(Segment);
Alexey Samsonove1a76ab2015-06-04 22:08:37 +0000228 if (Load.C.cmdsize < SegmentLoadSize)
Kevin Enderbyd4e075b2016-05-06 20:16:28 +0000229 return malformedError("load command " + Twine(LoadCommandIndex) +
Kevin Enderby89134962016-05-05 23:41:05 +0000230 " " + CmdName + " cmdsize too small");
Kevin Enderbyc614d282016-08-12 20:10:25 +0000231 if (auto SegOrErr = getStructOrErr<Segment>(Obj, Load.Ptr)) {
232 Segment S = SegOrErr.get();
233 const unsigned SectionSize = sizeof(Section);
234 uint64_t FileSize = Obj->getData().size();
Lang Hames9e964f32016-03-25 17:25:34 +0000235 if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize ||
236 S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize)
Kevin Enderbyd4e075b2016-05-06 20:16:28 +0000237 return malformedError("load command " + Twine(LoadCommandIndex) +
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000238 " inconsistent cmdsize in " + CmdName +
Kevin Enderby89134962016-05-05 23:41:05 +0000239 " for the number of sections");
Lang Hames9e964f32016-03-25 17:25:34 +0000240 for (unsigned J = 0; J < S.nsects; ++J) {
241 const char *Sec = getSectionPtr(Obj, Load, J);
242 Sections.push_back(Sec);
Kevin Enderbyc614d282016-08-12 20:10:25 +0000243 Section s = getStruct<Section>(Obj, Sec);
244 if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB &&
245 Obj->getHeader().filetype != MachO::MH_DSYM &&
246 s.flags != MachO::S_ZEROFILL &&
247 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
248 s.offset > FileSize)
249 return malformedError("offset field of section " + Twine(J) + " in " +
250 CmdName + " command " + Twine(LoadCommandIndex) +
251 " extends past the end of the file");
252 if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB &&
253 Obj->getHeader().filetype != MachO::MH_DSYM &&
254 s.flags != MachO::S_ZEROFILL &&
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000255 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && S.fileoff == 0 &&
256 s.offset < SizeOfHeaders && s.size != 0)
Kevin Enderbyc614d282016-08-12 20:10:25 +0000257 return malformedError("offset field of section " + Twine(J) + " in " +
258 CmdName + " command " + Twine(LoadCommandIndex) +
259 " not past the headers of the file");
260 uint64_t BigSize = s.offset;
261 BigSize += s.size;
262 if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB &&
263 Obj->getHeader().filetype != MachO::MH_DSYM &&
264 s.flags != MachO::S_ZEROFILL &&
265 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
266 BigSize > FileSize)
267 return malformedError("offset field plus size field of section " +
268 Twine(J) + " in " + CmdName + " command " +
269 Twine(LoadCommandIndex) +
270 " extends past the end of the file");
271 if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB &&
272 Obj->getHeader().filetype != MachO::MH_DSYM &&
273 s.flags != MachO::S_ZEROFILL &&
274 s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
275 s.size > S.filesize)
276 return malformedError("size field of section " +
277 Twine(J) + " in " + CmdName + " command " +
278 Twine(LoadCommandIndex) +
279 " greater than the segment");
280 if (Obj->getHeader().filetype != MachO::MH_DYLIB_STUB &&
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000281 Obj->getHeader().filetype != MachO::MH_DSYM && s.size != 0 &&
282 s.addr < S.vmaddr)
283 return malformedError("addr field of section " + Twine(J) + " in " +
284 CmdName + " command " + Twine(LoadCommandIndex) +
285 " less than the segment's vmaddr");
Kevin Enderbyc614d282016-08-12 20:10:25 +0000286 BigSize = s.addr;
287 BigSize += s.size;
288 uint64_t BigEnd = S.vmaddr;
289 BigEnd += S.vmsize;
290 if (S.vmsize != 0 && s.size != 0 && BigSize > BigEnd)
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000291 return malformedError("addr field plus size of section " + Twine(J) +
292 " in " + CmdName + " command " +
293 Twine(LoadCommandIndex) +
294 " greater than than "
Kevin Enderbyc614d282016-08-12 20:10:25 +0000295 "the segment's vmaddr plus vmsize");
296 if (s.reloff > FileSize)
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000297 return malformedError("reloff field of section " + Twine(J) + " in " +
298 CmdName + " command " + Twine(LoadCommandIndex) +
Kevin Enderbyc614d282016-08-12 20:10:25 +0000299 " extends past the end of the file");
300 BigSize = s.nreloc;
301 BigSize *= sizeof(struct MachO::relocation_info);
302 BigSize += s.reloff;
303 if (BigSize > FileSize)
304 return malformedError("reloff field plus nreloc field times sizeof("
305 "struct relocation_info) of section " +
306 Twine(J) + " in " + CmdName + " command " +
NAKAMURA Takumi59a20642016-08-22 00:58:04 +0000307 Twine(LoadCommandIndex) +
Kevin Enderbyc614d282016-08-12 20:10:25 +0000308 " extends past the end of the file");
Lang Hames9e964f32016-03-25 17:25:34 +0000309 }
Kevin Enderby600fb3f2016-08-05 18:19:40 +0000310 if (S.fileoff > FileSize)
311 return malformedError("load command " + Twine(LoadCommandIndex) +
NAKAMURA Takumi9d0b5312016-08-22 00:58:47 +0000312 " fileoff field in " + CmdName +
Kevin Enderby600fb3f2016-08-05 18:19:40 +0000313 " extends past the end of the file");
Kevin Enderbyc614d282016-08-12 20:10:25 +0000314 uint64_t BigSize = S.fileoff;
315 BigSize += S.filesize;
316 if (BigSize > FileSize)
317 return malformedError("load command " + Twine(LoadCommandIndex) +
318 " fileoff field plus filesize field in " +
319 CmdName + " extends past the end of the file");
320 if (S.vmsize != 0 && S.filesize > S.vmsize)
321 return malformedError("load command " + Twine(LoadCommandIndex) +
322 " fileoff field in " + CmdName +
323 " greater than vmsize field");
Lang Hames9e964f32016-03-25 17:25:34 +0000324 IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname);
325 } else
326 return SegOrErr.takeError();
327
328 return Error::success();
Alexey Samsonove1a76ab2015-06-04 22:08:37 +0000329}
330
Kevin Enderby0e52c922016-08-26 19:34:07 +0000331static Error checkSymtabCommand(const MachOObjectFile *Obj,
332 const MachOObjectFile::LoadCommandInfo &Load,
333 uint32_t LoadCommandIndex,
334 const char **SymtabLoadCmd) {
335 if (Load.C.cmdsize < sizeof(MachO::symtab_command))
336 return malformedError("load command " + Twine(LoadCommandIndex) +
337 " LC_SYMTAB cmdsize too small");
338 if (*SymtabLoadCmd != nullptr)
339 return malformedError("more than one LC_SYMTAB command");
340 MachO::symtab_command Symtab =
341 getStruct<MachO::symtab_command>(Obj, Load.Ptr);
342 if (Symtab.cmdsize != sizeof(MachO::symtab_command))
343 return malformedError("LC_SYMTAB command " + Twine(LoadCommandIndex) +
344 " has incorrect cmdsize");
345 uint64_t FileSize = Obj->getData().size();
346 if (Symtab.symoff > FileSize)
347 return malformedError("symoff field of LC_SYMTAB command " +
348 Twine(LoadCommandIndex) + " extends past the end "
349 "of the file");
350 uint64_t BigSize = Symtab.nsyms;
351 const char *struct_nlist_name;
352 if (Obj->is64Bit()) {
353 BigSize *= sizeof(MachO::nlist_64);
354 struct_nlist_name = "struct nlist_64";
355 } else {
356 BigSize *= sizeof(MachO::nlist);
357 struct_nlist_name = "struct nlist";
358 }
359 BigSize += Symtab.symoff;
360 if (BigSize > FileSize)
361 return malformedError("symoff field plus nsyms field times sizeof(" +
362 Twine(struct_nlist_name) + ") of LC_SYMTAB command " +
363 Twine(LoadCommandIndex) + " extends past the end "
364 "of the file");
365 if (Symtab.stroff > FileSize)
366 return malformedError("stroff field of LC_SYMTAB command " +
367 Twine(LoadCommandIndex) + " extends past the end "
368 "of the file");
369 BigSize = Symtab.stroff;
370 BigSize += Symtab.strsize;
371 if (BigSize > FileSize)
372 return malformedError("stroff field plus strsize field of LC_SYMTAB "
373 "command " + Twine(LoadCommandIndex) + " extends "
374 "past the end of the file");
Kevin Enderby0e52c922016-08-26 19:34:07 +0000375 *SymtabLoadCmd = Load.Ptr;
376 return Error::success();
377}
378
Kevin Enderbydcbc5042016-08-30 21:28:30 +0000379static Error checkDysymtabCommand(const MachOObjectFile *Obj,
380 const MachOObjectFile::LoadCommandInfo &Load,
381 uint32_t LoadCommandIndex,
382 const char **DysymtabLoadCmd) {
383 if (Load.C.cmdsize < sizeof(MachO::dysymtab_command))
384 return malformedError("load command " + Twine(LoadCommandIndex) +
385 " LC_DYSYMTAB cmdsize too small");
386 if (*DysymtabLoadCmd != nullptr)
387 return malformedError("more than one LC_DYSYMTAB command");
388 MachO::dysymtab_command Dysymtab =
389 getStruct<MachO::dysymtab_command>(Obj, Load.Ptr);
390 if (Dysymtab.cmdsize != sizeof(MachO::dysymtab_command))
391 return malformedError("LC_DYSYMTAB command " + Twine(LoadCommandIndex) +
392 " has incorrect cmdsize");
393 uint64_t FileSize = Obj->getData().size();
394 if (Dysymtab.tocoff > FileSize)
395 return malformedError("tocoff field of LC_DYSYMTAB command " +
396 Twine(LoadCommandIndex) + " extends past the end of "
397 "the file");
398 uint64_t BigSize = Dysymtab.ntoc;
399 BigSize *= sizeof(MachO::dylib_table_of_contents);
400 BigSize += Dysymtab.tocoff;
401 if (BigSize > FileSize)
402 return malformedError("tocoff field plus ntoc field times sizeof(struct "
403 "dylib_table_of_contents) of LC_DYSYMTAB command " +
404 Twine(LoadCommandIndex) + " extends past the end of "
405 "the file");
406 if (Dysymtab.modtaboff > FileSize)
407 return malformedError("modtaboff field of LC_DYSYMTAB command " +
408 Twine(LoadCommandIndex) + " extends past the end of "
409 "the file");
410 BigSize = Dysymtab.nmodtab;
411 const char *struct_dylib_module_name;
412 if (Obj->is64Bit()) {
413 BigSize *= sizeof(MachO::dylib_module_64);
414 struct_dylib_module_name = "struct dylib_module_64";
415 } else {
416 BigSize *= sizeof(MachO::dylib_module);
417 struct_dylib_module_name = "struct dylib_module";
418 }
419 BigSize += Dysymtab.modtaboff;
420 if (BigSize > FileSize)
421 return malformedError("modtaboff field plus nmodtab field times sizeof(" +
422 Twine(struct_dylib_module_name) + ") of LC_DYSYMTAB "
423 "command " + Twine(LoadCommandIndex) + " extends "
424 "past the end of the file");
425 if (Dysymtab.extrefsymoff > FileSize)
426 return malformedError("extrefsymoff field of LC_DYSYMTAB command " +
427 Twine(LoadCommandIndex) + " extends past the end of "
428 "the file");
429 BigSize = Dysymtab.nextrefsyms;
430 BigSize *= sizeof(MachO::dylib_reference);
431 BigSize += Dysymtab.extrefsymoff;
432 if (BigSize > FileSize)
433 return malformedError("extrefsymoff field plus nextrefsyms field times "
434 "sizeof(struct dylib_reference) of LC_DYSYMTAB "
435 "command " + Twine(LoadCommandIndex) + " extends "
436 "past the end of the file");
437 if (Dysymtab.indirectsymoff > FileSize)
438 return malformedError("indirectsymoff field of LC_DYSYMTAB command " +
439 Twine(LoadCommandIndex) + " extends past the end of "
440 "the file");
441 BigSize = Dysymtab.nindirectsyms;
442 BigSize *= sizeof(uint32_t);
443 BigSize += Dysymtab.indirectsymoff;
444 if (BigSize > FileSize)
445 return malformedError("indirectsymoff field plus nindirectsyms field times "
446 "sizeof(uint32_t) of LC_DYSYMTAB command " +
447 Twine(LoadCommandIndex) + " extends past the end of "
448 "the file");
449 if (Dysymtab.extreloff > FileSize)
450 return malformedError("extreloff field of LC_DYSYMTAB command " +
451 Twine(LoadCommandIndex) + " extends past the end of "
452 "the file");
453 BigSize = Dysymtab.nextrel;
454 BigSize *= sizeof(MachO::relocation_info);
455 BigSize += Dysymtab.extreloff;
456 if (BigSize > FileSize)
457 return malformedError("extreloff field plus nextrel field times sizeof"
458 "(struct relocation_info) of LC_DYSYMTAB command " +
459 Twine(LoadCommandIndex) + " extends past the end of "
460 "the file");
461 if (Dysymtab.locreloff > FileSize)
462 return malformedError("locreloff field of LC_DYSYMTAB command " +
463 Twine(LoadCommandIndex) + " extends past the end of "
464 "the file");
465 BigSize = Dysymtab.nlocrel;
466 BigSize *= sizeof(MachO::relocation_info);
467 BigSize += Dysymtab.locreloff;
468 if (BigSize > FileSize)
469 return malformedError("locreloff field plus nlocrel field times sizeof"
470 "(struct relocation_info) of LC_DYSYMTAB command " +
471 Twine(LoadCommandIndex) + " extends past the end of "
472 "the file");
473 *DysymtabLoadCmd = Load.Ptr;
474 return Error::success();
475}
476
Kevin Enderby9d0c9452016-08-31 17:57:46 +0000477static Error checkLinkeditDataCommand(const MachOObjectFile *Obj,
478 const MachOObjectFile::LoadCommandInfo &Load,
479 uint32_t LoadCommandIndex,
480 const char **LoadCmd, const char *CmdName) {
481 if (Load.C.cmdsize < sizeof(MachO::linkedit_data_command))
482 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
483 CmdName + " cmdsize too small");
484 if (*LoadCmd != nullptr)
485 return malformedError("more than one " + Twine(CmdName) + " command");
486 MachO::linkedit_data_command LinkData =
487 getStruct<MachO::linkedit_data_command>(Obj, Load.Ptr);
488 if (LinkData.cmdsize != sizeof(MachO::linkedit_data_command))
489 return malformedError(Twine(CmdName) + " command " +
490 Twine(LoadCommandIndex) + " has incorrect cmdsize");
491 uint64_t FileSize = Obj->getData().size();
492 if (LinkData.dataoff > FileSize)
493 return malformedError("dataoff field of " + Twine(CmdName) + " command " +
494 Twine(LoadCommandIndex) + " extends past the end of "
495 "the file");
496 uint64_t BigSize = LinkData.dataoff;
497 BigSize += LinkData.datasize;
498 if (BigSize > FileSize)
499 return malformedError("dataoff field plus datasize field of " +
500 Twine(CmdName) + " command " +
501 Twine(LoadCommandIndex) + " extends past the end of "
502 "the file");
503 *LoadCmd = Load.Ptr;
504 return Error::success();
505}
506
Kevin Enderbyf76b56c2016-09-13 21:42:28 +0000507static Error checkDyldInfoCommand(const MachOObjectFile *Obj,
508 const MachOObjectFile::LoadCommandInfo &Load,
509 uint32_t LoadCommandIndex,
510 const char **LoadCmd, const char *CmdName) {
511 if (Load.C.cmdsize < sizeof(MachO::dyld_info_command))
512 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
513 CmdName + " cmdsize too small");
514 if (*LoadCmd != nullptr)
515 return malformedError("more than one LC_DYLD_INFO and or LC_DYLD_INFO_ONLY "
516 "command");
517 MachO::dyld_info_command DyldInfo =
518 getStruct<MachO::dyld_info_command>(Obj, Load.Ptr);
519 if (DyldInfo.cmdsize != sizeof(MachO::dyld_info_command))
520 return malformedError(Twine(CmdName) + " command " +
521 Twine(LoadCommandIndex) + " has incorrect cmdsize");
522 uint64_t FileSize = Obj->getData().size();
523 if (DyldInfo.rebase_off > FileSize)
524 return malformedError("rebase_off field of " + Twine(CmdName) +
525 " command " + Twine(LoadCommandIndex) + " extends "
526 "past the end of the file");
527 uint64_t BigSize = DyldInfo.rebase_off;
528 BigSize += DyldInfo.rebase_size;
529 if (BigSize > FileSize)
530 return malformedError("rebase_off field plus rebase_size field of " +
531 Twine(CmdName) + " command " +
532 Twine(LoadCommandIndex) + " extends past the end of "
533 "the file");
534 if (DyldInfo.bind_off > FileSize)
535 return malformedError("bind_off field of " + Twine(CmdName) +
536 " command " + Twine(LoadCommandIndex) + " extends "
537 "past the end of the file");
538 BigSize = DyldInfo.bind_off;
539 BigSize += DyldInfo.bind_size;
540 if (BigSize > FileSize)
541 return malformedError("bind_off field plus bind_size field of " +
542 Twine(CmdName) + " command " +
543 Twine(LoadCommandIndex) + " extends past the end of "
544 "the file");
545 if (DyldInfo.weak_bind_off > FileSize)
546 return malformedError("weak_bind_off field of " + Twine(CmdName) +
547 " command " + Twine(LoadCommandIndex) + " extends "
548 "past the end of the file");
549 BigSize = DyldInfo.weak_bind_off;
550 BigSize += DyldInfo.weak_bind_size;
551 if (BigSize > FileSize)
552 return malformedError("weak_bind_off field plus weak_bind_size field of " +
553 Twine(CmdName) + " command " +
554 Twine(LoadCommandIndex) + " extends past the end of "
555 "the file");
556 if (DyldInfo.lazy_bind_off > FileSize)
557 return malformedError("lazy_bind_off field of " + Twine(CmdName) +
558 " command " + Twine(LoadCommandIndex) + " extends "
559 "past the end of the file");
560 BigSize = DyldInfo.lazy_bind_off;
561 BigSize += DyldInfo.lazy_bind_size;
562 if (BigSize > FileSize)
563 return malformedError("lazy_bind_off field plus lazy_bind_size field of " +
564 Twine(CmdName) + " command " +
565 Twine(LoadCommandIndex) + " extends past the end of "
566 "the file");
567 if (DyldInfo.export_off > FileSize)
568 return malformedError("export_off field of " + Twine(CmdName) +
569 " command " + Twine(LoadCommandIndex) + " extends "
570 "past the end of the file");
571 BigSize = DyldInfo.export_off;
572 BigSize += DyldInfo.export_size;
573 if (BigSize > FileSize)
574 return malformedError("export_off field plus export_size field of " +
575 Twine(CmdName) + " command " +
576 Twine(LoadCommandIndex) + " extends past the end of "
577 "the file");
578 *LoadCmd = Load.Ptr;
579 return Error::success();
580}
581
Kevin Enderbyfc0929a2016-09-20 20:14:14 +0000582static Error checkDylibCommand(const MachOObjectFile *Obj,
583 const MachOObjectFile::LoadCommandInfo &Load,
584 uint32_t LoadCommandIndex, const char *CmdName) {
585 if (Load.C.cmdsize < sizeof(MachO::dylib_command))
586 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
587 CmdName + " cmdsize too small");
588 MachO::dylib_command D = getStruct<MachO::dylib_command>(Obj, Load.Ptr);
589 if (D.dylib.name < sizeof(MachO::dylib_command))
590 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
591 CmdName + " name.offset field too small, not past "
592 "the end of the dylib_command struct");
593 if (D.dylib.name >= D.cmdsize)
594 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
595 CmdName + " name.offset field extends past the end "
596 "of the load command");
597 // Make sure there is a null between the starting offset of the name and
598 // the end of the load command.
599 uint32_t i;
600 const char *P = (const char *)Load.Ptr;
601 for (i = D.dylib.name; i < D.cmdsize; i++)
602 if (P[i] == '\0')
603 break;
604 if (i >= D.cmdsize)
605 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
606 CmdName + " library name extends past the end of the "
607 "load command");
608 return Error::success();
609}
610
611static Error checkDylibIdCommand(const MachOObjectFile *Obj,
612 const MachOObjectFile::LoadCommandInfo &Load,
613 uint32_t LoadCommandIndex,
614 const char **LoadCmd) {
615 if (Error Err = checkDylibCommand(Obj, Load, LoadCommandIndex,
616 "LC_ID_DYLIB"))
617 return Err;
618 if (*LoadCmd != nullptr)
619 return malformedError("more than one LC_ID_DYLIB command");
620 if (Obj->getHeader().filetype != MachO::MH_DYLIB &&
621 Obj->getHeader().filetype != MachO::MH_DYLIB_STUB)
622 return malformedError("LC_ID_DYLIB load command in non-dynamic library "
623 "file type");
624 *LoadCmd = Load.Ptr;
625 return Error::success();
626}
627
Kevin Enderby3e490ef2016-09-27 23:24:13 +0000628static Error checkDyldCommand(const MachOObjectFile *Obj,
629 const MachOObjectFile::LoadCommandInfo &Load,
630 uint32_t LoadCommandIndex, const char *CmdName) {
631 if (Load.C.cmdsize < sizeof(MachO::dylinker_command))
632 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
633 CmdName + " cmdsize too small");
634 MachO::dylinker_command D = getStruct<MachO::dylinker_command>(Obj, Load.Ptr);
635 if (D.name < sizeof(MachO::dylinker_command))
636 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
637 CmdName + " name.offset field too small, not past "
638 "the end of the dylinker_command struct");
639 if (D.name >= D.cmdsize)
640 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
641 CmdName + " name.offset field extends past the end "
642 "of the load command");
643 // Make sure there is a null between the starting offset of the name and
644 // the end of the load command.
645 uint32_t i;
646 const char *P = (const char *)Load.Ptr;
647 for (i = D.name; i < D.cmdsize; i++)
648 if (P[i] == '\0')
649 break;
650 if (i >= D.cmdsize)
651 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
652 CmdName + " dyld name extends past the end of the "
653 "load command");
654 return Error::success();
655}
656
Kevin Enderby32359db2016-09-28 21:20:45 +0000657static Error checkVersCommand(const MachOObjectFile *Obj,
658 const MachOObjectFile::LoadCommandInfo &Load,
659 uint32_t LoadCommandIndex,
660 const char **LoadCmd, const char *CmdName) {
661 if (Load.C.cmdsize != sizeof(MachO::version_min_command))
662 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
663 CmdName + " has incorrect cmdsize");
664 if (*LoadCmd != nullptr)
665 return malformedError("more than one LC_VERSION_MIN_MACOSX, "
666 "LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_TVOS or "
667 "LC_VERSION_MIN_WATCHOS command");
668 *LoadCmd = Load.Ptr;
669 return Error::success();
670}
671
Kevin Enderby76966bf2016-09-28 23:16:01 +0000672static Error checkRpathCommand(const MachOObjectFile *Obj,
673 const MachOObjectFile::LoadCommandInfo &Load,
674 uint32_t LoadCommandIndex) {
675 if (Load.C.cmdsize < sizeof(MachO::rpath_command))
676 return malformedError("load command " + Twine(LoadCommandIndex) +
677 " LC_RPATH cmdsize too small");
678 MachO::rpath_command R = getStruct<MachO::rpath_command>(Obj, Load.Ptr);
679 if (R.path < sizeof(MachO::rpath_command))
680 return malformedError("load command " + Twine(LoadCommandIndex) +
681 " LC_RPATH path.offset field too small, not past "
682 "the end of the rpath_command struct");
683 if (R.path >= R.cmdsize)
684 return malformedError("load command " + Twine(LoadCommandIndex) +
685 " LC_RPATH path.offset field extends past the end "
686 "of the load command");
687 // Make sure there is a null between the starting offset of the path and
688 // the end of the load command.
689 uint32_t i;
690 const char *P = (const char *)Load.Ptr;
691 for (i = R.path; i < R.cmdsize; i++)
692 if (P[i] == '\0')
693 break;
694 if (i >= R.cmdsize)
695 return malformedError("load command " + Twine(LoadCommandIndex) +
696 " LC_RPATH library name extends past the end of the "
697 "load command");
698 return Error::success();
699}
700
Kevin Enderbyf993d6e2016-10-04 20:37:43 +0000701static Error checkEncryptCommand(const MachOObjectFile *Obj,
702 const MachOObjectFile::LoadCommandInfo &Load,
703 uint32_t LoadCommandIndex,
704 uint64_t cryptoff, uint64_t cryptsize,
705 const char **LoadCmd, const char *CmdName) {
706 if (*LoadCmd != nullptr)
707 return malformedError("more than one LC_ENCRYPTION_INFO and or "
708 "LC_ENCRYPTION_INFO_64 command");
709 uint64_t FileSize = Obj->getData().size();
710 if (cryptoff > FileSize)
711 return malformedError("cryptoff field of " + Twine(CmdName) +
712 " command " + Twine(LoadCommandIndex) + " extends "
713 "past the end of the file");
714 uint64_t BigSize = cryptoff;
715 BigSize += cryptsize;
716 if (BigSize > FileSize)
717 return malformedError("cryptoff field plus cryptsize field of " +
718 Twine(CmdName) + " command " +
719 Twine(LoadCommandIndex) + " extends past the end of "
720 "the file");
721 *LoadCmd = Load.Ptr;
722 return Error::success();
723}
724
Kevin Enderby68fffa82016-10-11 21:04:39 +0000725static Error checkLinkerOptCommand(const MachOObjectFile *Obj,
726 const MachOObjectFile::LoadCommandInfo &Load,
727 uint32_t LoadCommandIndex) {
728 if (Load.C.cmdsize < sizeof(MachO::linker_option_command))
729 return malformedError("load command " + Twine(LoadCommandIndex) +
730 " LC_LINKER_OPTION cmdsize too small");
731 MachO::linker_option_command L =
732 getStruct<MachO::linker_option_command>(Obj, Load.Ptr);
733 // Make sure the count of strings is correct.
734 const char *string = (const char *)Load.Ptr +
735 sizeof(struct MachO::linker_option_command);
736 uint32_t left = L.cmdsize - sizeof(struct MachO::linker_option_command);
737 uint32_t i = 0;
738 while (left > 0) {
739 while (*string == '\0' && left > 0) {
740 string++;
741 left--;
742 }
743 if (left > 0) {
744 i++;
745 uint32_t NullPos = StringRef(string, left).find('\0');
746 uint32_t len = std::min(NullPos, left) + 1;
747 string += len;
748 left -= len;
749 }
750 }
751 if (L.count != i)
752 return malformedError("load command " + Twine(LoadCommandIndex) +
753 " LC_LINKER_OPTION string count " + Twine(L.count) +
754 " does not match number of strings");
755 return Error::success();
756}
757
Kevin Enderby2490de02016-10-17 22:09:25 +0000758static Error checkSubCommand(const MachOObjectFile *Obj,
759 const MachOObjectFile::LoadCommandInfo &Load,
760 uint32_t LoadCommandIndex, const char *CmdName,
761 size_t SizeOfCmd, const char *CmdStructName,
762 uint32_t PathOffset, const char *PathFieldName) {
763 if (PathOffset < SizeOfCmd)
764 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
765 CmdName + " " + PathFieldName + ".offset field too "
766 "small, not past the end of the " + CmdStructName);
767 if (PathOffset >= Load.C.cmdsize)
768 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
769 CmdName + " " + PathFieldName + ".offset field "
770 "extends past the end of the load command");
771 // Make sure there is a null between the starting offset of the path and
772 // the end of the load command.
773 uint32_t i;
774 const char *P = (const char *)Load.Ptr;
775 for (i = PathOffset; i < Load.C.cmdsize; i++)
776 if (P[i] == '\0')
777 break;
778 if (i >= Load.C.cmdsize)
779 return malformedError("load command " + Twine(LoadCommandIndex) + " " +
780 CmdName + " " + PathFieldName + " name extends past "
781 "the end of the load command");
782 return Error::success();
783}
784
Lang Hames82627642016-03-25 21:59:14 +0000785Expected<std::unique_ptr<MachOObjectFile>>
786MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian,
787 bool Is64Bits) {
Lang Hamesd1af8fc2016-03-25 23:54:32 +0000788 Error Err;
Lang Hames82627642016-03-25 21:59:14 +0000789 std::unique_ptr<MachOObjectFile> Obj(
790 new MachOObjectFile(std::move(Object), IsLittleEndian,
791 Is64Bits, Err));
792 if (Err)
793 return std::move(Err);
794 return std::move(Obj);
795}
796
Rafael Espindola48af1c22014-08-19 18:44:46 +0000797MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
Lang Hames9e964f32016-03-25 17:25:34 +0000798 bool Is64bits, Error &Err)
Rafael Espindola48af1c22014-08-19 18:44:46 +0000799 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
Craig Topper2617dcc2014-04-15 06:32:26 +0000800 SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
Kevin Enderby9a509442015-01-27 21:28:24 +0000801 DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr),
802 DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr),
803 HasPageZeroSegment(false) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000804 ErrorAsOutParameter ErrAsOutParam(&Err);
Kevin Enderbyc614d282016-08-12 20:10:25 +0000805 uint64_t SizeOfHeaders;
Kevin Enderby87025742016-04-13 21:17:58 +0000806 if (is64Bit()) {
Lang Hames9e964f32016-03-25 17:25:34 +0000807 parseHeader(this, Header64, Err);
Kevin Enderbyc614d282016-08-12 20:10:25 +0000808 SizeOfHeaders = sizeof(MachO::mach_header_64);
Kevin Enderby87025742016-04-13 21:17:58 +0000809 } else {
Lang Hames9e964f32016-03-25 17:25:34 +0000810 parseHeader(this, Header, Err);
Kevin Enderbyc614d282016-08-12 20:10:25 +0000811 SizeOfHeaders = sizeof(MachO::mach_header);
Kevin Enderby87025742016-04-13 21:17:58 +0000812 }
Lang Hames9e964f32016-03-25 17:25:34 +0000813 if (Err)
Alexey Samsonov9f336632015-06-04 19:45:22 +0000814 return;
Kevin Enderbyc614d282016-08-12 20:10:25 +0000815 SizeOfHeaders += getHeader().sizeofcmds;
816 if (getData().data() + SizeOfHeaders > getData().end()) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +0000817 Err = malformedError("load commands extend past the end of the file");
Kevin Enderby87025742016-04-13 21:17:58 +0000818 return;
819 }
Alexey Samsonov13415ed2015-06-04 19:22:03 +0000820
821 uint32_t LoadCommandCount = getHeader().ncmds;
Lang Hames9e964f32016-03-25 17:25:34 +0000822 LoadCommandInfo Load;
Kevin Enderbyfc0929a2016-09-20 20:14:14 +0000823 if (LoadCommandCount != 0) {
824 if (auto LoadOrErr = getFirstLoadCommandInfo(this))
825 Load = *LoadOrErr;
826 else {
827 Err = LoadOrErr.takeError();
828 return;
829 }
Alexey Samsonovde5a94a2015-06-04 19:57:46 +0000830 }
Lang Hames9e964f32016-03-25 17:25:34 +0000831
Kevin Enderbyfc0929a2016-09-20 20:14:14 +0000832 const char *DyldIdLoadCmd = nullptr;
Kevin Enderby90986e62016-09-26 21:11:03 +0000833 const char *FuncStartsLoadCmd = nullptr;
834 const char *SplitInfoLoadCmd = nullptr;
835 const char *CodeSignDrsLoadCmd = nullptr;
Kevin Enderby32359db2016-09-28 21:20:45 +0000836 const char *VersLoadCmd = nullptr;
Kevin Enderby245be3e2016-09-29 17:45:23 +0000837 const char *SourceLoadCmd = nullptr;
Kevin Enderby4f229d82016-09-29 21:07:29 +0000838 const char *EntryPointLoadCmd = nullptr;
Kevin Enderbyf993d6e2016-10-04 20:37:43 +0000839 const char *EncryptLoadCmd = nullptr;
Kevin Enderby6f695822016-10-18 17:54:17 +0000840 const char *RoutinesLoadCmd = nullptr;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000841 for (unsigned I = 0; I < LoadCommandCount; ++I) {
Kevin Enderby1851a822016-07-07 22:11:42 +0000842 if (is64Bit()) {
843 if (Load.C.cmdsize % 8 != 0) {
844 // We have a hack here to allow 64-bit Mach-O core files to have
845 // LC_THREAD commands that are only a multiple of 4 and not 8 to be
846 // allowed since the macOS kernel produces them.
847 if (getHeader().filetype != MachO::MH_CORE ||
848 Load.C.cmd != MachO::LC_THREAD || Load.C.cmdsize % 4) {
849 Err = malformedError("load command " + Twine(I) + " cmdsize not a "
850 "multiple of 8");
851 return;
852 }
853 }
854 } else {
855 if (Load.C.cmdsize % 4 != 0) {
856 Err = malformedError("load command " + Twine(I) + " cmdsize not a "
857 "multiple of 4");
858 return;
859 }
860 }
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000861 LoadCommands.push_back(Load);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000862 if (Load.C.cmd == MachO::LC_SYMTAB) {
Kevin Enderby0e52c922016-08-26 19:34:07 +0000863 if ((Err = checkSymtabCommand(this, Load, I, &SymtabLoadCmd)))
David Majnemer73cc6ff2014-11-13 19:48:56 +0000864 return;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000865 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Kevin Enderbydcbc5042016-08-30 21:28:30 +0000866 if ((Err = checkDysymtabCommand(this, Load, I, &DysymtabLoadCmd)))
David Majnemer73cc6ff2014-11-13 19:48:56 +0000867 return;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000868 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby9d0c9452016-08-31 17:57:46 +0000869 if ((Err = checkLinkeditDataCommand(this, Load, I, &DataInCodeLoadCmd,
870 "LC_DATA_IN_CODE")))
David Majnemer73cc6ff2014-11-13 19:48:56 +0000871 return;
Kevin Enderby9a509442015-01-27 21:28:24 +0000872 } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
Kevin Enderby9d0c9452016-08-31 17:57:46 +0000873 if ((Err = checkLinkeditDataCommand(this, Load, I, &LinkOptHintsLoadCmd,
874 "LC_LINKER_OPTIMIZATION_HINT")))
Kevin Enderby9a509442015-01-27 21:28:24 +0000875 return;
Kevin Enderby90986e62016-09-26 21:11:03 +0000876 } else if (Load.C.cmd == MachO::LC_FUNCTION_STARTS) {
877 if ((Err = checkLinkeditDataCommand(this, Load, I, &FuncStartsLoadCmd,
878 "LC_FUNCTION_STARTS")))
879 return;
880 } else if (Load.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO) {
881 if ((Err = checkLinkeditDataCommand(this, Load, I, &SplitInfoLoadCmd,
882 "LC_SEGMENT_SPLIT_INFO")))
883 return;
884 } else if (Load.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) {
885 if ((Err = checkLinkeditDataCommand(this, Load, I, &CodeSignDrsLoadCmd,
886 "LC_DYLIB_CODE_SIGN_DRS")))
887 return;
Kevin Enderbyf76b56c2016-09-13 21:42:28 +0000888 } else if (Load.C.cmd == MachO::LC_DYLD_INFO) {
889 if ((Err = checkDyldInfoCommand(this, Load, I, &DyldInfoLoadCmd,
890 "LC_DYLD_INFO")))
David Majnemer73cc6ff2014-11-13 19:48:56 +0000891 return;
Kevin Enderbyf76b56c2016-09-13 21:42:28 +0000892 } else if (Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
893 if ((Err = checkDyldInfoCommand(this, Load, I, &DyldInfoLoadCmd,
894 "LC_DYLD_INFO_ONLY")))
895 return;
Alexander Potapenko6909b5b2014-10-15 23:35:45 +0000896 } else if (Load.C.cmd == MachO::LC_UUID) {
Kevin Enderbye71e13c2016-09-21 20:03:09 +0000897 if (Load.C.cmdsize != sizeof(MachO::uuid_command)) {
898 Err = malformedError("LC_UUID command " + Twine(I) + " has incorrect "
899 "cmdsize");
900 return;
901 }
David Majnemer73cc6ff2014-11-13 19:48:56 +0000902 if (UuidLoadCmd) {
Kevin Enderbye71e13c2016-09-21 20:03:09 +0000903 Err = malformedError("more than one LC_UUID command");
David Majnemer73cc6ff2014-11-13 19:48:56 +0000904 return;
905 }
Alexander Potapenko6909b5b2014-10-15 23:35:45 +0000906 UuidLoadCmd = Load.Ptr;
Alexey Samsonove1a76ab2015-06-04 22:08:37 +0000907 } else if (Load.C.cmd == MachO::LC_SEGMENT_64) {
Kevin Enderbyc614d282016-08-12 20:10:25 +0000908 if ((Err = parseSegmentLoadCommand<MachO::segment_command_64,
909 MachO::section_64>(
Kevin Enderbyb34e3a12016-05-05 17:43:35 +0000910 this, Load, Sections, HasPageZeroSegment, I,
Kevin Enderbyc614d282016-08-12 20:10:25 +0000911 "LC_SEGMENT_64", SizeOfHeaders)))
Alexey Samsonov074da9b2015-06-04 20:08:52 +0000912 return;
Alexey Samsonove1a76ab2015-06-04 22:08:37 +0000913 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
Kevin Enderbyc614d282016-08-12 20:10:25 +0000914 if ((Err = parseSegmentLoadCommand<MachO::segment_command,
915 MachO::section>(
916 this, Load, Sections, HasPageZeroSegment, I,
917 "LC_SEGMENT", SizeOfHeaders)))
Alexey Samsonov074da9b2015-06-04 20:08:52 +0000918 return;
Kevin Enderbyfc0929a2016-09-20 20:14:14 +0000919 } else if (Load.C.cmd == MachO::LC_ID_DYLIB) {
920 if ((Err = checkDylibIdCommand(this, Load, I, &DyldIdLoadCmd)))
921 return;
922 } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) {
923 if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_DYLIB")))
924 return;
925 Libraries.push_back(Load.Ptr);
926 } else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) {
927 if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_WEAK_DYLIB")))
928 return;
929 Libraries.push_back(Load.Ptr);
930 } else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) {
931 if ((Err = checkDylibCommand(this, Load, I, "LC_LAZY_LOAD_DYLIB")))
932 return;
933 Libraries.push_back(Load.Ptr);
934 } else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) {
935 if ((Err = checkDylibCommand(this, Load, I, "LC_REEXPORT_DYLIB")))
936 return;
937 Libraries.push_back(Load.Ptr);
938 } else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
939 if ((Err = checkDylibCommand(this, Load, I, "LC_LOAD_UPWARD_DYLIB")))
940 return;
Kevin Enderby980b2582014-06-05 21:21:57 +0000941 Libraries.push_back(Load.Ptr);
Kevin Enderby3e490ef2016-09-27 23:24:13 +0000942 } else if (Load.C.cmd == MachO::LC_ID_DYLINKER) {
943 if ((Err = checkDyldCommand(this, Load, I, "LC_ID_DYLINKER")))
944 return;
945 } else if (Load.C.cmd == MachO::LC_LOAD_DYLINKER) {
946 if ((Err = checkDyldCommand(this, Load, I, "LC_LOAD_DYLINKER")))
947 return;
948 } else if (Load.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
949 if ((Err = checkDyldCommand(this, Load, I, "LC_DYLD_ENVIRONMENT")))
950 return;
Kevin Enderby32359db2016-09-28 21:20:45 +0000951 } else if (Load.C.cmd == MachO::LC_VERSION_MIN_MACOSX) {
952 if ((Err = checkVersCommand(this, Load, I, &VersLoadCmd,
953 "LC_VERSION_MIN_MACOSX")))
954 return;
955 } else if (Load.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
956 if ((Err = checkVersCommand(this, Load, I, &VersLoadCmd,
957 "LC_VERSION_MIN_IPHONEOS")))
958 return;
959 } else if (Load.C.cmd == MachO::LC_VERSION_MIN_TVOS) {
960 if ((Err = checkVersCommand(this, Load, I, &VersLoadCmd,
961 "LC_VERSION_MIN_TVOS")))
962 return;
963 } else if (Load.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
964 if ((Err = checkVersCommand(this, Load, I, &VersLoadCmd,
965 "LC_VERSION_MIN_WATCHOS")))
966 return;
Kevin Enderby76966bf2016-09-28 23:16:01 +0000967 } else if (Load.C.cmd == MachO::LC_RPATH) {
968 if ((Err = checkRpathCommand(this, Load, I)))
969 return;
Kevin Enderby245be3e2016-09-29 17:45:23 +0000970 } else if (Load.C.cmd == MachO::LC_SOURCE_VERSION) {
971 if (Load.C.cmdsize != sizeof(MachO::source_version_command)) {
972 Err = malformedError("LC_SOURCE_VERSION command " + Twine(I) +
973 " has incorrect cmdsize");
974 return;
975 }
976 if (SourceLoadCmd) {
977 Err = malformedError("more than one LC_SOURCE_VERSION command");
978 return;
979 }
980 SourceLoadCmd = Load.Ptr;
Kevin Enderby4f229d82016-09-29 21:07:29 +0000981 } else if (Load.C.cmd == MachO::LC_MAIN) {
982 if (Load.C.cmdsize != sizeof(MachO::entry_point_command)) {
983 Err = malformedError("LC_MAIN command " + Twine(I) +
984 " has incorrect cmdsize");
985 return;
986 }
987 if (EntryPointLoadCmd) {
988 Err = malformedError("more than one LC_MAIN command");
989 return;
990 }
991 EntryPointLoadCmd = Load.Ptr;
Kevin Enderbyf993d6e2016-10-04 20:37:43 +0000992 } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO) {
993 if (Load.C.cmdsize != sizeof(MachO::encryption_info_command)) {
994 Err = malformedError("LC_ENCRYPTION_INFO command " + Twine(I) +
995 " has incorrect cmdsize");
996 return;
997 }
998 MachO::encryption_info_command E =
999 getStruct<MachO::encryption_info_command>(this, Load.Ptr);
1000 if ((Err = checkEncryptCommand(this, Load, I, E.cryptoff, E.cryptsize,
1001 &EncryptLoadCmd, "LC_ENCRYPTION_INFO")))
1002 return;
1003 } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
1004 if (Load.C.cmdsize != sizeof(MachO::encryption_info_command_64)) {
1005 Err = malformedError("LC_ENCRYPTION_INFO_64 command " + Twine(I) +
1006 " has incorrect cmdsize");
1007 return;
1008 }
1009 MachO::encryption_info_command_64 E =
1010 getStruct<MachO::encryption_info_command_64>(this, Load.Ptr);
1011 if ((Err = checkEncryptCommand(this, Load, I, E.cryptoff, E.cryptsize,
1012 &EncryptLoadCmd, "LC_ENCRYPTION_INFO_64")))
1013 return;
Kevin Enderby68fffa82016-10-11 21:04:39 +00001014 } else if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
1015 if ((Err = checkLinkerOptCommand(this, Load, I)))
1016 return;
Kevin Enderby2490de02016-10-17 22:09:25 +00001017 } else if (Load.C.cmd == MachO::LC_SUB_FRAMEWORK) {
1018 if (Load.C.cmdsize < sizeof(MachO::sub_framework_command)) {
1019 Err = malformedError("load command " + Twine(I) +
1020 " LC_SUB_FRAMEWORK cmdsize too small");
1021 return;
1022 }
1023 MachO::sub_framework_command S =
1024 getStruct<MachO::sub_framework_command>(this, Load.Ptr);
1025 if ((Err = checkSubCommand(this, Load, I, "LC_SUB_FRAMEWORK",
1026 sizeof(MachO::sub_framework_command),
1027 "sub_framework_command", S.umbrella,
1028 "umbrella")))
1029 return;
1030 } else if (Load.C.cmd == MachO::LC_SUB_UMBRELLA) {
1031 if (Load.C.cmdsize < sizeof(MachO::sub_umbrella_command)) {
1032 Err = malformedError("load command " + Twine(I) +
1033 " LC_SUB_UMBRELLA cmdsize too small");
1034 return;
1035 }
1036 MachO::sub_umbrella_command S =
1037 getStruct<MachO::sub_umbrella_command>(this, Load.Ptr);
1038 if ((Err = checkSubCommand(this, Load, I, "LC_SUB_UMBRELLA",
1039 sizeof(MachO::sub_umbrella_command),
1040 "sub_umbrella_command", S.sub_umbrella,
1041 "sub_umbrella")))
1042 return;
1043 } else if (Load.C.cmd == MachO::LC_SUB_LIBRARY) {
1044 if (Load.C.cmdsize < sizeof(MachO::sub_library_command)) {
1045 Err = malformedError("load command " + Twine(I) +
1046 " LC_SUB_LIBRARY cmdsize too small");
1047 return;
1048 }
1049 MachO::sub_library_command S =
1050 getStruct<MachO::sub_library_command>(this, Load.Ptr);
1051 if ((Err = checkSubCommand(this, Load, I, "LC_SUB_LIBRARY",
1052 sizeof(MachO::sub_library_command),
1053 "sub_library_command", S.sub_library,
1054 "sub_library")))
1055 return;
1056 } else if (Load.C.cmd == MachO::LC_SUB_CLIENT) {
1057 if (Load.C.cmdsize < sizeof(MachO::sub_client_command)) {
1058 Err = malformedError("load command " + Twine(I) +
1059 " LC_SUB_CLIENT cmdsize too small");
1060 return;
1061 }
1062 MachO::sub_client_command S =
1063 getStruct<MachO::sub_client_command>(this, Load.Ptr);
1064 if ((Err = checkSubCommand(this, Load, I, "LC_SUB_CLIENT",
1065 sizeof(MachO::sub_client_command),
1066 "sub_client_command", S.client, "client")))
1067 return;
Kevin Enderby6f695822016-10-18 17:54:17 +00001068 } else if (Load.C.cmd == MachO::LC_ROUTINES) {
1069 if (Load.C.cmdsize != sizeof(MachO::routines_command)) {
1070 Err = malformedError("LC_ROUTINES command " + Twine(I) +
1071 " has incorrect cmdsize");
1072 return;
1073 }
1074 if (RoutinesLoadCmd) {
1075 Err = malformedError("more than one LC_ROUTINES and or LC_ROUTINES_64 "
1076 "command");
1077 return;
1078 }
1079 RoutinesLoadCmd = Load.Ptr;
1080 } else if (Load.C.cmd == MachO::LC_ROUTINES_64) {
1081 if (Load.C.cmdsize != sizeof(MachO::routines_command_64)) {
1082 Err = malformedError("LC_ROUTINES_64 command " + Twine(I) +
1083 " has incorrect cmdsize");
1084 return;
1085 }
1086 if (RoutinesLoadCmd) {
1087 Err = malformedError("more than one LC_ROUTINES_64 and or LC_ROUTINES "
1088 "command");
1089 return;
1090 }
1091 RoutinesLoadCmd = Load.Ptr;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001092 }
Alexey Samsonovde5a94a2015-06-04 19:57:46 +00001093 if (I < LoadCommandCount - 1) {
Kevin Enderby368e7142016-05-03 17:16:08 +00001094 if (auto LoadOrErr = getNextLoadCommandInfo(this, I, Load))
Lang Hames9e964f32016-03-25 17:25:34 +00001095 Load = *LoadOrErr;
1096 else {
1097 Err = LoadOrErr.takeError();
Alexey Samsonovde5a94a2015-06-04 19:57:46 +00001098 return;
1099 }
Alexey Samsonovde5a94a2015-06-04 19:57:46 +00001100 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001101 }
Kevin Enderby1829c682016-01-22 22:49:55 +00001102 if (!SymtabLoadCmd) {
1103 if (DysymtabLoadCmd) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001104 Err = malformedError("contains LC_DYSYMTAB load command without a "
Kevin Enderby89134962016-05-05 23:41:05 +00001105 "LC_SYMTAB load command");
Kevin Enderby1829c682016-01-22 22:49:55 +00001106 return;
1107 }
1108 } else if (DysymtabLoadCmd) {
1109 MachO::symtab_command Symtab =
1110 getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
1111 MachO::dysymtab_command Dysymtab =
1112 getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
1113 if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001114 Err = malformedError("ilocalsym in LC_DYSYMTAB load command "
Kevin Enderby89134962016-05-05 23:41:05 +00001115 "extends past the end of the symbol table");
Kevin Enderby1829c682016-01-22 22:49:55 +00001116 return;
1117 }
Kevin Enderby5e55d172016-04-21 20:29:49 +00001118 uint64_t BigSize = Dysymtab.ilocalsym;
1119 BigSize += Dysymtab.nlocalsym;
1120 if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001121 Err = malformedError("ilocalsym plus nlocalsym in LC_DYSYMTAB load "
Kevin Enderby89134962016-05-05 23:41:05 +00001122 "command extends past the end of the symbol table");
Kevin Enderby1829c682016-01-22 22:49:55 +00001123 return;
1124 }
1125 if (Dysymtab.nextdefsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001126 Err = malformedError("nextdefsym in LC_DYSYMTAB load command "
Kevin Enderby89134962016-05-05 23:41:05 +00001127 "extends past the end of the symbol table");
Kevin Enderby1829c682016-01-22 22:49:55 +00001128 return;
1129 }
Kevin Enderby5e55d172016-04-21 20:29:49 +00001130 BigSize = Dysymtab.iextdefsym;
1131 BigSize += Dysymtab.nextdefsym;
1132 if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001133 Err = malformedError("iextdefsym plus nextdefsym in LC_DYSYMTAB "
Kevin Enderby89134962016-05-05 23:41:05 +00001134 "load command extends past the end of the symbol "
1135 "table");
Kevin Enderby1829c682016-01-22 22:49:55 +00001136 return;
1137 }
1138 if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001139 Err = malformedError("nundefsym in LC_DYSYMTAB load command "
Kevin Enderby89134962016-05-05 23:41:05 +00001140 "extends past the end of the symbol table");
Kevin Enderby1829c682016-01-22 22:49:55 +00001141 return;
1142 }
Kevin Enderby5e55d172016-04-21 20:29:49 +00001143 BigSize = Dysymtab.iundefsym;
1144 BigSize += Dysymtab.nundefsym;
1145 if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001146 Err = malformedError("iundefsym plus nundefsym in LC_DYSYMTAB load "
Kevin Enderby89134962016-05-05 23:41:05 +00001147 " command extends past the end of the symbol table");
Kevin Enderby1829c682016-01-22 22:49:55 +00001148 return;
1149 }
1150 }
Kevin Enderbyfc0929a2016-09-20 20:14:14 +00001151 if ((getHeader().filetype == MachO::MH_DYLIB ||
1152 getHeader().filetype == MachO::MH_DYLIB_STUB) &&
1153 DyldIdLoadCmd == nullptr) {
1154 Err = malformedError("no LC_ID_DYLIB load command in dynamic library "
1155 "filetype");
1156 return;
1157 }
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00001158 assert(LoadCommands.size() == LoadCommandCount);
Lang Hames9e964f32016-03-25 17:25:34 +00001159
1160 Err = Error::success();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001161}
1162
Rafael Espindola5e812af2014-01-30 02:49:50 +00001163void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001164 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001165 sizeof(MachO::nlist_64) :
1166 sizeof(MachO::nlist);
Rafael Espindola75c30362013-04-24 19:47:55 +00001167 Symb.p += SymbolTableEntrySize;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001168}
1169
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001170Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001171 StringRef StringTable = getStringTableData();
Artyom Skrobov78d5daf2014-07-18 09:26:16 +00001172 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001173 const char *Start = &StringTable.data()[Entry.n_strx];
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001174 if (Start < getData().begin() || Start >= getData().end()) {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001175 return malformedError("bad string index: " + Twine(Entry.n_strx) +
Kevin Enderby89134962016-05-05 23:41:05 +00001176 " for symbol at index " + Twine(getSymbolIndex(Symb)));
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001177 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001178 return StringRef(Start);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001179}
1180
Rafael Espindola0e77a942014-12-10 20:46:55 +00001181unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
1182 DataRefImpl DRI = Sec.getRawDataRefImpl();
1183 uint32_t Flags = getSectionFlags(this, DRI);
1184 return Flags & MachO::SECTION_TYPE;
1185}
1186
Rafael Espindola59128922015-06-24 18:14:41 +00001187uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const {
1188 if (is64Bit()) {
1189 MachO::nlist_64 Entry = getSymbol64TableEntry(Sym);
1190 return Entry.n_value;
1191 }
1192 MachO::nlist Entry = getSymbolTableEntry(Sym);
1193 return Entry.n_value;
1194}
1195
Kevin Enderby980b2582014-06-05 21:21:57 +00001196// getIndirectName() returns the name of the alias'ed symbol who's string table
1197// index is in the n_value field.
Rafael Espindola3acea392014-06-12 21:46:39 +00001198std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
1199 StringRef &Res) const {
Kevin Enderby980b2582014-06-05 21:21:57 +00001200 StringRef StringTable = getStringTableData();
Rafael Espindola59128922015-06-24 18:14:41 +00001201 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
1202 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
1203 return object_error::parse_failed;
1204 uint64_t NValue = getNValue(Symb);
Kevin Enderby980b2582014-06-05 21:21:57 +00001205 if (NValue >= StringTable.size())
1206 return object_error::parse_failed;
1207 const char *Start = &StringTable.data()[NValue];
1208 Res = StringRef(Start);
Rui Ueyama7d099192015-06-09 15:20:42 +00001209 return std::error_code();
Kevin Enderby980b2582014-06-05 21:21:57 +00001210}
1211
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00001212uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
Rafael Espindola7e7be922015-07-07 15:05:09 +00001213 return getNValue(Sym);
Rafael Espindola991af662015-06-24 19:11:10 +00001214}
1215
Kevin Enderby931cb652016-06-24 18:24:42 +00001216Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
Rafael Espindolaed067c42015-07-03 18:19:00 +00001217 return getSymbolValue(Sym);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001218}
1219
Rafael Espindolaa4d224722015-05-31 23:52:50 +00001220uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const {
Rafael Espindola20122a42014-01-31 20:57:12 +00001221 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +00001222 if (flags & SymbolRef::SF_Common) {
Artyom Skrobov78d5daf2014-07-18 09:26:16 +00001223 MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindolaa4d224722015-05-31 23:52:50 +00001224 return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +00001225 }
Rafael Espindolaa4d224722015-05-31 23:52:50 +00001226 return 0;
Rafael Espindolae4dd2e02013-04-29 22:24:22 +00001227}
1228
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00001229uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const {
Rafael Espindola05cbccc2015-07-07 13:58:32 +00001230 return getNValue(DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001231}
1232
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001233Expected<SymbolRef::Type>
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00001234MachOObjectFile::getSymbolType(DataRefImpl Symb) const {
Artyom Skrobov78d5daf2014-07-18 09:26:16 +00001235 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001236 uint8_t n_type = Entry.n_type;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001237
Rafael Espindola56f976f2013-04-18 18:08:55 +00001238 // If this is a STAB debugging symbol, we can do nothing more.
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00001239 if (n_type & MachO::N_STAB)
1240 return SymbolRef::ST_Debug;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001241
Charles Davis74ec8b02013-08-27 05:00:13 +00001242 switch (n_type & MachO::N_TYPE) {
1243 case MachO::N_UNDF :
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00001244 return SymbolRef::ST_Unknown;
Charles Davis74ec8b02013-08-27 05:00:13 +00001245 case MachO::N_SECT :
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001246 Expected<section_iterator> SecOrError = getSymbolSection(Symb);
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00001247 if (!SecOrError)
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001248 return SecOrError.takeError();
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00001249 section_iterator Sec = *SecOrError;
Kuba Breckade833222015-11-12 09:40:29 +00001250 if (Sec->isData() || Sec->isBSS())
1251 return SymbolRef::ST_Data;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00001252 return SymbolRef::ST_Function;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001253 }
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00001254 return SymbolRef::ST_Other;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001255}
1256
Rafael Espindola20122a42014-01-31 20:57:12 +00001257uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
Artyom Skrobov78d5daf2014-07-18 09:26:16 +00001258 MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001259
Charles Davis8bdfafd2013-09-01 04:28:48 +00001260 uint8_t MachOType = Entry.n_type;
1261 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001262
Rafael Espindola20122a42014-01-31 20:57:12 +00001263 uint32_t Result = SymbolRef::SF_None;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001264
Tim Northovereaef0742014-05-30 13:22:59 +00001265 if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
1266 Result |= SymbolRef::SF_Indirect;
1267
Rafael Espindolaa1356322013-11-02 05:03:24 +00001268 if (MachOType & MachO::N_STAB)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001269 Result |= SymbolRef::SF_FormatSpecific;
1270
Charles Davis74ec8b02013-08-27 05:00:13 +00001271 if (MachOType & MachO::N_EXT) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001272 Result |= SymbolRef::SF_Global;
Charles Davis74ec8b02013-08-27 05:00:13 +00001273 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindola05cbccc2015-07-07 13:58:32 +00001274 if (getNValue(DRI))
Rafael Espindolae4dd2e02013-04-29 22:24:22 +00001275 Result |= SymbolRef::SF_Common;
Rafael Espindolad8247722015-07-07 14:26:39 +00001276 else
1277 Result |= SymbolRef::SF_Undefined;
Rafael Espindolae4dd2e02013-04-29 22:24:22 +00001278 }
Lang Hames7e0692b2015-01-15 22:33:30 +00001279
1280 if (!(MachOType & MachO::N_PEXT))
1281 Result |= SymbolRef::SF_Exported;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001282 }
1283
Charles Davis74ec8b02013-08-27 05:00:13 +00001284 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindola56f976f2013-04-18 18:08:55 +00001285 Result |= SymbolRef::SF_Weak;
1286
Kevin Enderbyec5ca032014-08-18 20:21:02 +00001287 if (MachOFlags & (MachO::N_ARM_THUMB_DEF))
1288 Result |= SymbolRef::SF_Thumb;
1289
Charles Davis74ec8b02013-08-27 05:00:13 +00001290 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001291 Result |= SymbolRef::SF_Absolute;
1292
Rafael Espindola20122a42014-01-31 20:57:12 +00001293 return Result;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001294}
1295
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001296Expected<section_iterator>
Rafael Espindola8bab8892015-08-07 23:27:14 +00001297MachOObjectFile::getSymbolSection(DataRefImpl Symb) const {
Artyom Skrobov78d5daf2014-07-18 09:26:16 +00001298 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001299 uint8_t index = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001300
Rafael Espindola8bab8892015-08-07 23:27:14 +00001301 if (index == 0)
1302 return section_end();
1303 DataRefImpl DRI;
1304 DRI.d.a = index - 1;
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00001305 if (DRI.d.a >= Sections.size()){
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00001306 return malformedError("bad section index: " + Twine((int)index) +
Kevin Enderby89134962016-05-05 23:41:05 +00001307 " for symbol at index " + Twine(getSymbolIndex(Symb)));
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00001308 }
Rafael Espindola8bab8892015-08-07 23:27:14 +00001309 return section_iterator(SectionRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001310}
1311
Rafael Espindola6bf32212015-06-24 19:57:32 +00001312unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const {
1313 MachO::nlist_base Entry =
1314 getSymbolTableEntryBase(this, Sym.getRawDataRefImpl());
1315 return Entry.n_sect - 1;
1316}
1317
Rafael Espindola5e812af2014-01-30 02:49:50 +00001318void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001319 Sec.d.a++;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001320}
1321
Rafael Espindola3acea392014-06-12 21:46:39 +00001322std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
1323 StringRef &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001324 ArrayRef<char> Raw = getSectionRawName(Sec);
1325 Result = parseSegmentOrSectionName(Raw.data());
Rui Ueyama7d099192015-06-09 15:20:42 +00001326 return std::error_code();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001327}
1328
Rafael Espindola80291272014-10-08 15:28:58 +00001329uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
1330 if (is64Bit())
1331 return getSection64(Sec).addr;
1332 return getSection(Sec).addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001333}
1334
Rafael Espindola80291272014-10-08 15:28:58 +00001335uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const {
Kevin Enderby46e642f2015-10-08 22:50:55 +00001336 // In the case if a malformed Mach-O file where the section offset is past
1337 // the end of the file or some part of the section size is past the end of
1338 // the file return a size of zero or a size that covers the rest of the file
1339 // but does not extend past the end of the file.
1340 uint32_t SectOffset, SectType;
1341 uint64_t SectSize;
1342
1343 if (is64Bit()) {
1344 MachO::section_64 Sect = getSection64(Sec);
1345 SectOffset = Sect.offset;
1346 SectSize = Sect.size;
1347 SectType = Sect.flags & MachO::SECTION_TYPE;
1348 } else {
1349 MachO::section Sect = getSection(Sec);
1350 SectOffset = Sect.offset;
1351 SectSize = Sect.size;
1352 SectType = Sect.flags & MachO::SECTION_TYPE;
1353 }
1354 if (SectType == MachO::S_ZEROFILL || SectType == MachO::S_GB_ZEROFILL)
1355 return SectSize;
1356 uint64_t FileSize = getData().size();
1357 if (SectOffset > FileSize)
1358 return 0;
1359 if (FileSize - SectOffset < SectSize)
1360 return FileSize - SectOffset;
1361 return SectSize;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001362}
1363
Rafael Espindola3acea392014-06-12 21:46:39 +00001364std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
1365 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001366 uint32_t Offset;
1367 uint64_t Size;
1368
1369 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001370 MachO::section_64 Sect = getSection64(Sec);
1371 Offset = Sect.offset;
1372 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001373 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001374 MachO::section Sect = getSection(Sec);
1375 Offset = Sect.offset;
1376 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001377 }
1378
1379 Res = this->getData().substr(Offset, Size);
Rui Ueyama7d099192015-06-09 15:20:42 +00001380 return std::error_code();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001381}
1382
Rafael Espindola80291272014-10-08 15:28:58 +00001383uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001384 uint32_t Align;
1385 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001386 MachO::section_64 Sect = getSection64(Sec);
1387 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001388 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001389 MachO::section Sect = getSection(Sec);
1390 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001391 }
1392
Rafael Espindola80291272014-10-08 15:28:58 +00001393 return uint64_t(1) << Align;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001394}
1395
George Rimar401e4e52016-05-24 12:48:46 +00001396bool MachOObjectFile::isSectionCompressed(DataRefImpl Sec) const {
1397 return false;
1398}
1399
Rafael Espindola80291272014-10-08 15:28:58 +00001400bool MachOObjectFile::isSectionText(DataRefImpl Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001401 uint32_t Flags = getSectionFlags(this, Sec);
Rafael Espindola80291272014-10-08 15:28:58 +00001402 return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001403}
1404
Rafael Espindola80291272014-10-08 15:28:58 +00001405bool MachOObjectFile::isSectionData(DataRefImpl Sec) const {
Kevin Enderby403258f2014-05-19 20:36:02 +00001406 uint32_t Flags = getSectionFlags(this, Sec);
1407 unsigned SectionType = Flags & MachO::SECTION_TYPE;
Rafael Espindola80291272014-10-08 15:28:58 +00001408 return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
1409 !(SectionType == MachO::S_ZEROFILL ||
1410 SectionType == MachO::S_GB_ZEROFILL);
Michael J. Spencer800619f2011-09-28 20:57:30 +00001411}
1412
Rafael Espindola80291272014-10-08 15:28:58 +00001413bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const {
Kevin Enderby403258f2014-05-19 20:36:02 +00001414 uint32_t Flags = getSectionFlags(this, Sec);
1415 unsigned SectionType = Flags & MachO::SECTION_TYPE;
Rafael Espindola80291272014-10-08 15:28:58 +00001416 return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
1417 (SectionType == MachO::S_ZEROFILL ||
1418 SectionType == MachO::S_GB_ZEROFILL);
Preston Gurd2138ef62012-04-12 20:13:57 +00001419}
1420
Rafael Espindola6bf32212015-06-24 19:57:32 +00001421unsigned MachOObjectFile::getSectionID(SectionRef Sec) const {
1422 return Sec.getRawDataRefImpl().d.a;
1423}
1424
Rafael Espindola80291272014-10-08 15:28:58 +00001425bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +00001426 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +00001427 return false;
Rafael Espindolac2413f52013-04-09 14:49:08 +00001428}
1429
Steven Wuf2fe0142016-02-29 19:40:10 +00001430bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const {
1431 StringRef SegmentName = getSectionFinalSegmentName(Sec);
1432 StringRef SectName;
1433 if (!getSectionName(Sec, SectName))
1434 return (SegmentName == "__LLVM" && SectName == "__bitcode");
1435 return false;
1436}
1437
Rui Ueyamabc654b12013-09-27 21:47:05 +00001438relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +00001439 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +00001440 Ret.d.a = Sec.d.a;
1441 Ret.d.b = 0;
Rafael Espindola04d3f492013-04-25 12:45:46 +00001442 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001443}
Rafael Espindolac0406e12013-04-08 20:45:01 +00001444
Rafael Espindola56f976f2013-04-18 18:08:55 +00001445relocation_iterator
Rui Ueyamabc654b12013-09-27 21:47:05 +00001446MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +00001447 uint32_t Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001448 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001449 MachO::section_64 Sect = getSection64(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001450 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001451 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001452 MachO::section Sect = getSection(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001453 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001454 }
Eric Christopher7b015c72011-04-22 03:19:48 +00001455
Rafael Espindola56f976f2013-04-18 18:08:55 +00001456 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +00001457 Ret.d.a = Sec.d.a;
1458 Ret.d.b = Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001459 return relocation_iterator(RelocationRef(Ret, this));
1460}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001461
Rafael Espindola5e812af2014-01-30 02:49:50 +00001462void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +00001463 ++Rel.d.b;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001464}
Owen Anderson171f4852011-10-24 23:20:07 +00001465
Rafael Espindola96d071c2015-06-29 23:29:12 +00001466uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const {
Rafael Espindola72475462014-04-04 00:31:12 +00001467 assert(getHeader().filetype == MachO::MH_OBJECT &&
1468 "Only implemented for MH_OBJECT");
Charles Davis8bdfafd2013-09-01 04:28:48 +00001469 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001470 return getAnyRelocationAddress(RE);
David Meyer2fc34c52012-03-01 01:36:50 +00001471}
1472
Rafael Espindola806f0062013-06-05 01:33:53 +00001473symbol_iterator
1474MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001475 MachO::any_relocation_info RE = getRelocation(Rel);
Tim Northover07f99fb2014-07-04 10:57:56 +00001476 if (isRelocationScattered(RE))
1477 return symbol_end();
1478
Rafael Espindola56f976f2013-04-18 18:08:55 +00001479 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
1480 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola806f0062013-06-05 01:33:53 +00001481 if (!isExtern)
Rafael Espindolab5155a52014-02-10 20:24:04 +00001482 return symbol_end();
Rafael Espindola75c30362013-04-24 19:47:55 +00001483
Charles Davis8bdfafd2013-09-01 04:28:48 +00001484 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +00001485 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001486 sizeof(MachO::nlist_64) :
1487 sizeof(MachO::nlist);
1488 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001489 DataRefImpl Sym;
1490 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola806f0062013-06-05 01:33:53 +00001491 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001492}
1493
Keno Fischerc780e8e2015-05-21 21:24:32 +00001494section_iterator
1495MachOObjectFile::getRelocationSection(DataRefImpl Rel) const {
1496 return section_iterator(getAnyRelocationSection(getRelocation(Rel)));
1497}
1498
Rafael Espindola99c041b2015-06-30 01:53:01 +00001499uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001500 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +00001501 return getAnyRelocationType(RE);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001502}
1503
Rafael Espindola41bb4322015-06-30 04:08:37 +00001504void MachOObjectFile::getRelocationTypeName(
1505 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001506 StringRef res;
Rafael Espindola99c041b2015-06-30 01:53:01 +00001507 uint64_t RType = getRelocationType(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001508
1509 unsigned Arch = this->getArch();
1510
1511 switch (Arch) {
1512 case Triple::x86: {
1513 static const char *const Table[] = {
1514 "GENERIC_RELOC_VANILLA",
1515 "GENERIC_RELOC_PAIR",
1516 "GENERIC_RELOC_SECTDIFF",
1517 "GENERIC_RELOC_PB_LA_PTR",
1518 "GENERIC_RELOC_LOCAL_SECTDIFF",
1519 "GENERIC_RELOC_TLV" };
1520
Eric Christopher13250cb2013-12-06 02:33:38 +00001521 if (RType > 5)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001522 res = "Unknown";
1523 else
1524 res = Table[RType];
1525 break;
1526 }
1527 case Triple::x86_64: {
1528 static const char *const Table[] = {
1529 "X86_64_RELOC_UNSIGNED",
1530 "X86_64_RELOC_SIGNED",
1531 "X86_64_RELOC_BRANCH",
1532 "X86_64_RELOC_GOT_LOAD",
1533 "X86_64_RELOC_GOT",
1534 "X86_64_RELOC_SUBTRACTOR",
1535 "X86_64_RELOC_SIGNED_1",
1536 "X86_64_RELOC_SIGNED_2",
1537 "X86_64_RELOC_SIGNED_4",
1538 "X86_64_RELOC_TLV" };
1539
1540 if (RType > 9)
1541 res = "Unknown";
1542 else
1543 res = Table[RType];
1544 break;
1545 }
1546 case Triple::arm: {
1547 static const char *const Table[] = {
1548 "ARM_RELOC_VANILLA",
1549 "ARM_RELOC_PAIR",
1550 "ARM_RELOC_SECTDIFF",
1551 "ARM_RELOC_LOCAL_SECTDIFF",
1552 "ARM_RELOC_PB_LA_PTR",
1553 "ARM_RELOC_BR24",
1554 "ARM_THUMB_RELOC_BR22",
1555 "ARM_THUMB_32BIT_BRANCH",
1556 "ARM_RELOC_HALF",
1557 "ARM_RELOC_HALF_SECTDIFF" };
1558
1559 if (RType > 9)
1560 res = "Unknown";
1561 else
1562 res = Table[RType];
1563 break;
1564 }
Tim Northover00ed9962014-03-29 10:18:08 +00001565 case Triple::aarch64: {
1566 static const char *const Table[] = {
1567 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
1568 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
1569 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
1570 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
1571 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
1572 "ARM64_RELOC_ADDEND"
1573 };
1574
1575 if (RType >= array_lengthof(Table))
1576 res = "Unknown";
1577 else
1578 res = Table[RType];
1579 break;
1580 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001581 case Triple::ppc: {
1582 static const char *const Table[] = {
1583 "PPC_RELOC_VANILLA",
1584 "PPC_RELOC_PAIR",
1585 "PPC_RELOC_BR14",
1586 "PPC_RELOC_BR24",
1587 "PPC_RELOC_HI16",
1588 "PPC_RELOC_LO16",
1589 "PPC_RELOC_HA16",
1590 "PPC_RELOC_LO14",
1591 "PPC_RELOC_SECTDIFF",
1592 "PPC_RELOC_PB_LA_PTR",
1593 "PPC_RELOC_HI16_SECTDIFF",
1594 "PPC_RELOC_LO16_SECTDIFF",
1595 "PPC_RELOC_HA16_SECTDIFF",
1596 "PPC_RELOC_JBSR",
1597 "PPC_RELOC_LO14_SECTDIFF",
1598 "PPC_RELOC_LOCAL_SECTDIFF" };
1599
Eric Christopher13250cb2013-12-06 02:33:38 +00001600 if (RType > 15)
1601 res = "Unknown";
1602 else
1603 res = Table[RType];
Rafael Espindola56f976f2013-04-18 18:08:55 +00001604 break;
1605 }
1606 case Triple::UnknownArch:
1607 res = "Unknown";
1608 break;
1609 }
1610 Result.append(res.begin(), res.end());
Rafael Espindola56f976f2013-04-18 18:08:55 +00001611}
1612
Keno Fischer281b6942015-05-30 19:44:53 +00001613uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const {
1614 MachO::any_relocation_info RE = getRelocation(Rel);
1615 return getAnyRelocationLength(RE);
1616}
1617
Kevin Enderby980b2582014-06-05 21:21:57 +00001618//
1619// guessLibraryShortName() is passed a name of a dynamic library and returns a
1620// guess on what the short name is. Then name is returned as a substring of the
1621// StringRef Name passed in. The name of the dynamic library is recognized as
1622// a framework if it has one of the two following forms:
1623// Foo.framework/Versions/A/Foo
1624// Foo.framework/Foo
1625// Where A and Foo can be any string. And may contain a trailing suffix
1626// starting with an underbar. If the Name is recognized as a framework then
1627// isFramework is set to true else it is set to false. If the Name has a
1628// suffix then Suffix is set to the substring in Name that contains the suffix
1629// else it is set to a NULL StringRef.
1630//
1631// The Name of the dynamic library is recognized as a library name if it has
1632// one of the two following forms:
1633// libFoo.A.dylib
1634// libFoo.dylib
1635// The library may have a suffix trailing the name Foo of the form:
1636// libFoo_profile.A.dylib
1637// libFoo_profile.dylib
1638//
1639// The Name of the dynamic library is also recognized as a library name if it
1640// has the following form:
1641// Foo.qtx
1642//
1643// If the Name of the dynamic library is none of the forms above then a NULL
1644// StringRef is returned.
1645//
1646StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
1647 bool &isFramework,
1648 StringRef &Suffix) {
1649 StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
1650 size_t a, b, c, d, Idx;
1651
1652 isFramework = false;
1653 Suffix = StringRef();
1654
1655 // Pull off the last component and make Foo point to it
1656 a = Name.rfind('/');
1657 if (a == Name.npos || a == 0)
1658 goto guess_library;
1659 Foo = Name.slice(a+1, Name.npos);
1660
1661 // Look for a suffix starting with a '_'
1662 Idx = Foo.rfind('_');
1663 if (Idx != Foo.npos && Foo.size() >= 2) {
1664 Suffix = Foo.slice(Idx, Foo.npos);
1665 Foo = Foo.slice(0, Idx);
1666 }
1667
1668 // First look for the form Foo.framework/Foo
1669 b = Name.rfind('/', a);
1670 if (b == Name.npos)
1671 Idx = 0;
1672 else
1673 Idx = b+1;
1674 F = Name.slice(Idx, Idx + Foo.size());
1675 DotFramework = Name.slice(Idx + Foo.size(),
1676 Idx + Foo.size() + sizeof(".framework/")-1);
1677 if (F == Foo && DotFramework == ".framework/") {
1678 isFramework = true;
1679 return Foo;
1680 }
1681
1682 // Next look for the form Foo.framework/Versions/A/Foo
1683 if (b == Name.npos)
1684 goto guess_library;
1685 c = Name.rfind('/', b);
1686 if (c == Name.npos || c == 0)
1687 goto guess_library;
1688 V = Name.slice(c+1, Name.npos);
1689 if (!V.startswith("Versions/"))
1690 goto guess_library;
1691 d = Name.rfind('/', c);
1692 if (d == Name.npos)
1693 Idx = 0;
1694 else
1695 Idx = d+1;
1696 F = Name.slice(Idx, Idx + Foo.size());
1697 DotFramework = Name.slice(Idx + Foo.size(),
1698 Idx + Foo.size() + sizeof(".framework/")-1);
1699 if (F == Foo && DotFramework == ".framework/") {
1700 isFramework = true;
1701 return Foo;
1702 }
1703
1704guess_library:
1705 // pull off the suffix after the "." and make a point to it
1706 a = Name.rfind('.');
1707 if (a == Name.npos || a == 0)
1708 return StringRef();
1709 Dylib = Name.slice(a, Name.npos);
1710 if (Dylib != ".dylib")
1711 goto guess_qtx;
1712
1713 // First pull off the version letter for the form Foo.A.dylib if any.
1714 if (a >= 3) {
1715 Dot = Name.slice(a-2, a-1);
1716 if (Dot == ".")
1717 a = a - 2;
1718 }
1719
1720 b = Name.rfind('/', a);
1721 if (b == Name.npos)
1722 b = 0;
1723 else
1724 b = b+1;
1725 // ignore any suffix after an underbar like Foo_profile.A.dylib
1726 Idx = Name.find('_', b);
1727 if (Idx != Name.npos && Idx != b) {
1728 Lib = Name.slice(b, Idx);
1729 Suffix = Name.slice(Idx, a);
1730 }
1731 else
1732 Lib = Name.slice(b, a);
1733 // There are incorrect library names of the form:
1734 // libATS.A_profile.dylib so check for these.
1735 if (Lib.size() >= 3) {
1736 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
1737 if (Dot == ".")
1738 Lib = Lib.slice(0, Lib.size()-2);
1739 }
1740 return Lib;
1741
1742guess_qtx:
1743 Qtx = Name.slice(a, Name.npos);
1744 if (Qtx != ".qtx")
1745 return StringRef();
1746 b = Name.rfind('/', a);
1747 if (b == Name.npos)
1748 Lib = Name.slice(0, a);
1749 else
1750 Lib = Name.slice(b+1, a);
1751 // There are library names of the form: QT.A.qtx so check for these.
1752 if (Lib.size() >= 3) {
1753 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
1754 if (Dot == ".")
1755 Lib = Lib.slice(0, Lib.size()-2);
1756 }
1757 return Lib;
1758}
1759
1760// getLibraryShortNameByIndex() is used to get the short name of the library
1761// for an undefined symbol in a linked Mach-O binary that was linked with the
1762// normal two-level namespace default (that is MH_TWOLEVEL in the header).
1763// It is passed the index (0 - based) of the library as translated from
1764// GET_LIBRARY_ORDINAL (1 - based).
Rafael Espindola3acea392014-06-12 21:46:39 +00001765std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
Nick Kledzikd04bc352014-08-30 00:20:14 +00001766 StringRef &Res) const {
Kevin Enderby980b2582014-06-05 21:21:57 +00001767 if (Index >= Libraries.size())
1768 return object_error::parse_failed;
1769
Kevin Enderby980b2582014-06-05 21:21:57 +00001770 // If the cache of LibrariesShortNames is not built up do that first for
1771 // all the Libraries.
1772 if (LibrariesShortNames.size() == 0) {
1773 for (unsigned i = 0; i < Libraries.size(); i++) {
1774 MachO::dylib_command D =
1775 getStruct<MachO::dylib_command>(this, Libraries[i]);
Nick Kledzik30061302014-09-17 00:25:22 +00001776 if (D.dylib.name >= D.cmdsize)
1777 return object_error::parse_failed;
Kevin Enderby4eff6cd2014-06-20 18:07:34 +00001778 const char *P = (const char *)(Libraries[i]) + D.dylib.name;
Kevin Enderby980b2582014-06-05 21:21:57 +00001779 StringRef Name = StringRef(P);
Nick Kledzik30061302014-09-17 00:25:22 +00001780 if (D.dylib.name+Name.size() >= D.cmdsize)
1781 return object_error::parse_failed;
Kevin Enderby980b2582014-06-05 21:21:57 +00001782 StringRef Suffix;
1783 bool isFramework;
1784 StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
Nick Kledzik30061302014-09-17 00:25:22 +00001785 if (shortName.empty())
Kevin Enderby980b2582014-06-05 21:21:57 +00001786 LibrariesShortNames.push_back(Name);
1787 else
1788 LibrariesShortNames.push_back(shortName);
1789 }
1790 }
1791
1792 Res = LibrariesShortNames[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +00001793 return std::error_code();
Kevin Enderby980b2582014-06-05 21:21:57 +00001794}
1795
Rafael Espindola76ad2322015-07-06 14:55:37 +00001796section_iterator
1797MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const {
1798 DataRefImpl Sec;
1799 Sec.d.a = Rel->getRawDataRefImpl().d.a;
1800 return section_iterator(SectionRef(Sec, this));
1801}
1802
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001803basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
Kevin Enderby1829c682016-01-22 22:49:55 +00001804 DataRefImpl DRI;
1805 MachO::symtab_command Symtab = getSymtabLoadCommand();
1806 if (!SymtabLoadCmd || Symtab.nsyms == 0)
1807 return basic_symbol_iterator(SymbolRef(DRI, this));
1808
Lang Hames36072da2014-05-12 21:39:59 +00001809 return getSymbolByIndex(0);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001810}
1811
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001812basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001813 DataRefImpl DRI;
Kevin Enderby1829c682016-01-22 22:49:55 +00001814 MachO::symtab_command Symtab = getSymtabLoadCommand();
1815 if (!SymtabLoadCmd || Symtab.nsyms == 0)
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001816 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola75c30362013-04-24 19:47:55 +00001817
Rafael Espindola75c30362013-04-24 19:47:55 +00001818 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001819 sizeof(MachO::nlist_64) :
1820 sizeof(MachO::nlist);
1821 unsigned Offset = Symtab.symoff +
1822 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001823 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001824 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001825}
1826
Lang Hames36072da2014-05-12 21:39:59 +00001827basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
Lang Hames36072da2014-05-12 21:39:59 +00001828 MachO::symtab_command Symtab = getSymtabLoadCommand();
Kevin Enderby1829c682016-01-22 22:49:55 +00001829 if (!SymtabLoadCmd || Index >= Symtab.nsyms)
Filipe Cabecinhas40139502015-01-15 22:52:38 +00001830 report_fatal_error("Requested symbol index is out of range.");
Lang Hames36072da2014-05-12 21:39:59 +00001831 unsigned SymbolTableEntrySize =
1832 is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
Kevin Enderby1829c682016-01-22 22:49:55 +00001833 DataRefImpl DRI;
Lang Hames36072da2014-05-12 21:39:59 +00001834 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1835 DRI.p += Index * SymbolTableEntrySize;
1836 return basic_symbol_iterator(SymbolRef(DRI, this));
1837}
1838
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001839uint64_t MachOObjectFile::getSymbolIndex(DataRefImpl Symb) const {
1840 MachO::symtab_command Symtab = getSymtabLoadCommand();
1841 if (!SymtabLoadCmd)
1842 report_fatal_error("getSymbolIndex() called with no symbol table symbol");
1843 unsigned SymbolTableEntrySize =
1844 is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
1845 DataRefImpl DRIstart;
1846 DRIstart.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1847 uint64_t Index = (Symb.p - DRIstart.p) / SymbolTableEntrySize;
1848 return Index;
1849}
1850
Rafael Espindolab5155a52014-02-10 20:24:04 +00001851section_iterator MachOObjectFile::section_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001852 DataRefImpl DRI;
1853 return section_iterator(SectionRef(DRI, this));
1854}
1855
Rafael Espindolab5155a52014-02-10 20:24:04 +00001856section_iterator MachOObjectFile::section_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001857 DataRefImpl DRI;
1858 DRI.d.a = Sections.size();
1859 return section_iterator(SectionRef(DRI, this));
1860}
1861
Rafael Espindola56f976f2013-04-18 18:08:55 +00001862uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola60689982013-04-07 19:05:30 +00001863 return is64Bit() ? 8 : 4;
Eric Christopher7b015c72011-04-22 03:19:48 +00001864}
1865
Rafael Espindola56f976f2013-04-18 18:08:55 +00001866StringRef MachOObjectFile::getFileFormatName() const {
1867 unsigned CPUType = getCPUType(this);
1868 if (!is64Bit()) {
1869 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001870 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001871 return "Mach-O 32-bit i386";
Charles Davis74ec8b02013-08-27 05:00:13 +00001872 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001873 return "Mach-O arm";
Charles Davis74ec8b02013-08-27 05:00:13 +00001874 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001875 return "Mach-O 32-bit ppc";
1876 default:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001877 return "Mach-O 32-bit unknown";
1878 }
1879 }
1880
Rafael Espindola56f976f2013-04-18 18:08:55 +00001881 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001882 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001883 return "Mach-O 64-bit x86-64";
Tim Northover00ed9962014-03-29 10:18:08 +00001884 case llvm::MachO::CPU_TYPE_ARM64:
1885 return "Mach-O arm64";
Charles Davis74ec8b02013-08-27 05:00:13 +00001886 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001887 return "Mach-O 64-bit ppc64";
1888 default:
1889 return "Mach-O 64-bit unknown";
1890 }
1891}
1892
Alexey Samsonove6388e62013-06-18 15:03:28 +00001893Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1894 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001895 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001896 return Triple::x86;
Charles Davis74ec8b02013-08-27 05:00:13 +00001897 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001898 return Triple::x86_64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001899 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001900 return Triple::arm;
Tim Northover00ed9962014-03-29 10:18:08 +00001901 case llvm::MachO::CPU_TYPE_ARM64:
Tim Northovere19bed72014-07-23 12:32:47 +00001902 return Triple::aarch64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001903 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001904 return Triple::ppc;
Charles Davis74ec8b02013-08-27 05:00:13 +00001905 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001906 return Triple::ppc64;
1907 default:
1908 return Triple::UnknownArch;
1909 }
1910}
1911
Tim Northover9e8eb412016-04-22 23:21:13 +00001912Triple MachOObjectFile::getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
1913 const char **McpuDefault) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00001914 if (McpuDefault)
1915 *McpuDefault = nullptr;
1916
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001917 switch (CPUType) {
1918 case MachO::CPU_TYPE_I386:
1919 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1920 case MachO::CPU_SUBTYPE_I386_ALL:
1921 return Triple("i386-apple-darwin");
1922 default:
1923 return Triple();
1924 }
1925 case MachO::CPU_TYPE_X86_64:
1926 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1927 case MachO::CPU_SUBTYPE_X86_64_ALL:
1928 return Triple("x86_64-apple-darwin");
1929 case MachO::CPU_SUBTYPE_X86_64_H:
1930 return Triple("x86_64h-apple-darwin");
1931 default:
1932 return Triple();
1933 }
1934 case MachO::CPU_TYPE_ARM:
1935 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1936 case MachO::CPU_SUBTYPE_ARM_V4T:
1937 return Triple("armv4t-apple-darwin");
1938 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1939 return Triple("armv5e-apple-darwin");
Kevin Enderbyae2a9a22014-08-07 21:30:25 +00001940 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1941 return Triple("xscale-apple-darwin");
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001942 case MachO::CPU_SUBTYPE_ARM_V6:
1943 return Triple("armv6-apple-darwin");
1944 case MachO::CPU_SUBTYPE_ARM_V6M:
Kevin Enderbyec5ca032014-08-18 20:21:02 +00001945 if (McpuDefault)
1946 *McpuDefault = "cortex-m0";
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001947 return Triple("armv6m-apple-darwin");
Kevin Enderbyae2a9a22014-08-07 21:30:25 +00001948 case MachO::CPU_SUBTYPE_ARM_V7:
1949 return Triple("armv7-apple-darwin");
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001950 case MachO::CPU_SUBTYPE_ARM_V7EM:
Kevin Enderbyec5ca032014-08-18 20:21:02 +00001951 if (McpuDefault)
1952 *McpuDefault = "cortex-m4";
Tim Northover9e8eb412016-04-22 23:21:13 +00001953 return Triple("thumbv7em-apple-darwin");
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001954 case MachO::CPU_SUBTYPE_ARM_V7K:
1955 return Triple("armv7k-apple-darwin");
1956 case MachO::CPU_SUBTYPE_ARM_V7M:
Kevin Enderbyec5ca032014-08-18 20:21:02 +00001957 if (McpuDefault)
1958 *McpuDefault = "cortex-m3";
Tim Northover9e8eb412016-04-22 23:21:13 +00001959 return Triple("thumbv7m-apple-darwin");
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001960 case MachO::CPU_SUBTYPE_ARM_V7S:
1961 return Triple("armv7s-apple-darwin");
1962 default:
1963 return Triple();
1964 }
1965 case MachO::CPU_TYPE_ARM64:
1966 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1967 case MachO::CPU_SUBTYPE_ARM64_ALL:
1968 return Triple("arm64-apple-darwin");
1969 default:
1970 return Triple();
1971 }
1972 case MachO::CPU_TYPE_POWERPC:
1973 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1974 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1975 return Triple("ppc-apple-darwin");
1976 default:
1977 return Triple();
1978 }
1979 case MachO::CPU_TYPE_POWERPC64:
Reid Kleckner4da3d572014-06-30 20:12:59 +00001980 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001981 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1982 return Triple("ppc64-apple-darwin");
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001983 default:
1984 return Triple();
1985 }
1986 default:
1987 return Triple();
1988 }
1989}
1990
1991Triple MachOObjectFile::getHostArch() {
1992 return Triple(sys::getDefaultTargetTriple());
1993}
1994
Rafael Espindola72318b42014-08-08 16:30:17 +00001995bool MachOObjectFile::isValidArch(StringRef ArchFlag) {
1996 return StringSwitch<bool>(ArchFlag)
1997 .Case("i386", true)
1998 .Case("x86_64", true)
1999 .Case("x86_64h", true)
2000 .Case("armv4t", true)
2001 .Case("arm", true)
2002 .Case("armv5e", true)
2003 .Case("armv6", true)
2004 .Case("armv6m", true)
Frederic Riss40baa0a2015-06-16 17:37:03 +00002005 .Case("armv7", true)
Rafael Espindola72318b42014-08-08 16:30:17 +00002006 .Case("armv7em", true)
2007 .Case("armv7k", true)
2008 .Case("armv7m", true)
2009 .Case("armv7s", true)
2010 .Case("arm64", true)
2011 .Case("ppc", true)
2012 .Case("ppc64", true)
2013 .Default(false);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00002014}
2015
Alexey Samsonove6388e62013-06-18 15:03:28 +00002016unsigned MachOObjectFile::getArch() const {
2017 return getArch(getCPUType(this));
2018}
2019
Tim Northover9e8eb412016-04-22 23:21:13 +00002020Triple MachOObjectFile::getArchTriple(const char **McpuDefault) const {
2021 return getArchTriple(Header.cputype, Header.cpusubtype, McpuDefault);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00002022}
2023
Rui Ueyamabc654b12013-09-27 21:47:05 +00002024relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00002025 DataRefImpl DRI;
2026 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00002027 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00002028}
2029
Rui Ueyamabc654b12013-09-27 21:47:05 +00002030relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00002031 DataRefImpl DRI;
2032 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00002033 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00002034}
2035
Kevin Enderby273ae012013-06-06 17:20:50 +00002036dice_iterator MachOObjectFile::begin_dices() const {
2037 DataRefImpl DRI;
2038 if (!DataInCodeLoadCmd)
2039 return dice_iterator(DiceRef(DRI, this));
2040
Charles Davis8bdfafd2013-09-01 04:28:48 +00002041 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
2042 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00002043 return dice_iterator(DiceRef(DRI, this));
2044}
2045
2046dice_iterator MachOObjectFile::end_dices() const {
2047 DataRefImpl DRI;
2048 if (!DataInCodeLoadCmd)
2049 return dice_iterator(DiceRef(DRI, this));
2050
Charles Davis8bdfafd2013-09-01 04:28:48 +00002051 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
2052 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00002053 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
2054 return dice_iterator(DiceRef(DRI, this));
2055}
2056
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00002057ExportEntry::ExportEntry(ArrayRef<uint8_t> T)
2058 : Trie(T), Malformed(false), Done(false) {}
Nick Kledzikd04bc352014-08-30 00:20:14 +00002059
2060void ExportEntry::moveToFirst() {
2061 pushNode(0);
2062 pushDownUntilBottom();
2063}
2064
2065void ExportEntry::moveToEnd() {
2066 Stack.clear();
2067 Done = true;
2068}
2069
2070bool ExportEntry::operator==(const ExportEntry &Other) const {
NAKAMURA Takumi84965032015-09-22 11:14:12 +00002071 // Common case, one at end, other iterating from begin.
Nick Kledzikd04bc352014-08-30 00:20:14 +00002072 if (Done || Other.Done)
2073 return (Done == Other.Done);
2074 // Not equal if different stack sizes.
2075 if (Stack.size() != Other.Stack.size())
2076 return false;
2077 // Not equal if different cumulative strings.
Yaron Keren075759a2015-03-30 15:42:36 +00002078 if (!CumulativeString.equals(Other.CumulativeString))
Nick Kledzikd04bc352014-08-30 00:20:14 +00002079 return false;
2080 // Equal if all nodes in both stacks match.
2081 for (unsigned i=0; i < Stack.size(); ++i) {
2082 if (Stack[i].Start != Other.Stack[i].Start)
2083 return false;
2084 }
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00002085 return true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00002086}
2087
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00002088uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr) {
2089 unsigned Count;
2090 uint64_t Result = decodeULEB128(Ptr, &Count);
2091 Ptr += Count;
2092 if (Ptr > Trie.end()) {
2093 Ptr = Trie.end();
Nick Kledzikd04bc352014-08-30 00:20:14 +00002094 Malformed = true;
2095 }
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00002096 return Result;
Nick Kledzikd04bc352014-08-30 00:20:14 +00002097}
2098
2099StringRef ExportEntry::name() const {
Yaron Keren075759a2015-03-30 15:42:36 +00002100 return CumulativeString;
Nick Kledzikd04bc352014-08-30 00:20:14 +00002101}
2102
2103uint64_t ExportEntry::flags() const {
2104 return Stack.back().Flags;
2105}
2106
2107uint64_t ExportEntry::address() const {
2108 return Stack.back().Address;
2109}
2110
2111uint64_t ExportEntry::other() const {
2112 return Stack.back().Other;
2113}
2114
2115StringRef ExportEntry::otherName() const {
2116 const char* ImportName = Stack.back().ImportName;
2117 if (ImportName)
2118 return StringRef(ImportName);
2119 return StringRef();
2120}
2121
2122uint32_t ExportEntry::nodeOffset() const {
2123 return Stack.back().Start - Trie.begin();
2124}
2125
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00002126ExportEntry::NodeState::NodeState(const uint8_t *Ptr)
2127 : Start(Ptr), Current(Ptr), Flags(0), Address(0), Other(0),
2128 ImportName(nullptr), ChildCount(0), NextChildIndex(0),
2129 ParentStringLength(0), IsExportNode(false) {}
Nick Kledzikd04bc352014-08-30 00:20:14 +00002130
2131void ExportEntry::pushNode(uint64_t offset) {
2132 const uint8_t *Ptr = Trie.begin() + offset;
2133 NodeState State(Ptr);
2134 uint64_t ExportInfoSize = readULEB128(State.Current);
2135 State.IsExportNode = (ExportInfoSize != 0);
2136 const uint8_t* Children = State.Current + ExportInfoSize;
2137 if (State.IsExportNode) {
2138 State.Flags = readULEB128(State.Current);
2139 if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
2140 State.Address = 0;
2141 State.Other = readULEB128(State.Current); // dylib ordinal
2142 State.ImportName = reinterpret_cast<const char*>(State.Current);
2143 } else {
2144 State.Address = readULEB128(State.Current);
Nick Kledzik1b591bd2014-08-30 01:57:34 +00002145 if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00002146 State.Other = readULEB128(State.Current);
Nick Kledzikd04bc352014-08-30 00:20:14 +00002147 }
2148 }
2149 State.ChildCount = *Children;
2150 State.Current = Children + 1;
2151 State.NextChildIndex = 0;
2152 State.ParentStringLength = CumulativeString.size();
2153 Stack.push_back(State);
2154}
2155
2156void ExportEntry::pushDownUntilBottom() {
2157 while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
2158 NodeState &Top = Stack.back();
2159 CumulativeString.resize(Top.ParentStringLength);
2160 for (;*Top.Current != 0; Top.Current++) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00002161 char C = *Top.Current;
2162 CumulativeString.push_back(C);
Nick Kledzikd04bc352014-08-30 00:20:14 +00002163 }
2164 Top.Current += 1;
2165 uint64_t childNodeIndex = readULEB128(Top.Current);
2166 Top.NextChildIndex += 1;
2167 pushNode(childNodeIndex);
2168 }
2169 if (!Stack.back().IsExportNode) {
2170 Malformed = true;
2171 moveToEnd();
2172 }
2173}
2174
2175// We have a trie data structure and need a way to walk it that is compatible
2176// with the C++ iterator model. The solution is a non-recursive depth first
2177// traversal where the iterator contains a stack of parent nodes along with a
2178// string that is the accumulation of all edge strings along the parent chain
2179// to this point.
2180//
NAKAMURA Takumi59c74b222014-10-27 08:08:18 +00002181// There is one "export" node for each exported symbol. But because some
Nick Kledzikd04bc352014-08-30 00:20:14 +00002182// symbols may be a prefix of another symbol (e.g. _dup and _dup2), an export
NAKAMURA Takumi84965032015-09-22 11:14:12 +00002183// node may have child nodes too.
Nick Kledzikd04bc352014-08-30 00:20:14 +00002184//
2185// The algorithm for moveNext() is to keep moving down the leftmost unvisited
2186// child until hitting a node with no children (which is an export node or
2187// else the trie is malformed). On the way down, each node is pushed on the
2188// stack ivar. If there is no more ways down, it pops up one and tries to go
2189// down a sibling path until a childless node is reached.
2190void ExportEntry::moveNext() {
2191 if (Stack.empty() || !Stack.back().IsExportNode) {
2192 Malformed = true;
2193 moveToEnd();
2194 return;
2195 }
2196
2197 Stack.pop_back();
2198 while (!Stack.empty()) {
2199 NodeState &Top = Stack.back();
2200 if (Top.NextChildIndex < Top.ChildCount) {
2201 pushDownUntilBottom();
2202 // Now at the next export node.
2203 return;
2204 } else {
2205 if (Top.IsExportNode) {
2206 // This node has no children but is itself an export node.
2207 CumulativeString.resize(Top.ParentStringLength);
2208 return;
2209 }
2210 Stack.pop_back();
2211 }
2212 }
2213 Done = true;
2214}
2215
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00002216iterator_range<export_iterator>
Nick Kledzikd04bc352014-08-30 00:20:14 +00002217MachOObjectFile::exports(ArrayRef<uint8_t> Trie) {
2218 ExportEntry Start(Trie);
Juergen Ributzka4d7f70d2014-12-19 02:31:01 +00002219 if (Trie.size() == 0)
2220 Start.moveToEnd();
2221 else
2222 Start.moveToFirst();
Nick Kledzikd04bc352014-08-30 00:20:14 +00002223
2224 ExportEntry Finish(Trie);
2225 Finish.moveToEnd();
2226
Craig Topper15576e12015-12-06 05:08:07 +00002227 return make_range(export_iterator(Start), export_iterator(Finish));
Nick Kledzikd04bc352014-08-30 00:20:14 +00002228}
2229
2230iterator_range<export_iterator> MachOObjectFile::exports() const {
2231 return exports(getDyldInfoExportsTrie());
2232}
2233
Nick Kledzikac431442014-09-12 21:34:15 +00002234MachORebaseEntry::MachORebaseEntry(ArrayRef<uint8_t> Bytes, bool is64Bit)
2235 : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0),
2236 RemainingLoopCount(0), AdvanceAmount(0), RebaseType(0),
2237 PointerSize(is64Bit ? 8 : 4), Malformed(false), Done(false) {}
2238
2239void MachORebaseEntry::moveToFirst() {
2240 Ptr = Opcodes.begin();
2241 moveNext();
2242}
2243
2244void MachORebaseEntry::moveToEnd() {
2245 Ptr = Opcodes.end();
2246 RemainingLoopCount = 0;
2247 Done = true;
2248}
2249
2250void MachORebaseEntry::moveNext() {
2251 // If in the middle of some loop, move to next rebasing in loop.
2252 SegmentOffset += AdvanceAmount;
2253 if (RemainingLoopCount) {
2254 --RemainingLoopCount;
2255 return;
2256 }
2257 if (Ptr == Opcodes.end()) {
2258 Done = true;
2259 return;
2260 }
2261 bool More = true;
2262 while (More && !Malformed) {
2263 // Parse next opcode and set up next loop.
2264 uint8_t Byte = *Ptr++;
2265 uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK;
2266 uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK;
2267 switch (Opcode) {
2268 case MachO::REBASE_OPCODE_DONE:
2269 More = false;
2270 Done = true;
2271 moveToEnd();
2272 DEBUG_WITH_TYPE("mach-o-rebase", llvm::dbgs() << "REBASE_OPCODE_DONE\n");
2273 break;
2274 case MachO::REBASE_OPCODE_SET_TYPE_IMM:
2275 RebaseType = ImmValue;
2276 DEBUG_WITH_TYPE(
2277 "mach-o-rebase",
2278 llvm::dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: "
2279 << "RebaseType=" << (int) RebaseType << "\n");
2280 break;
2281 case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
2282 SegmentIndex = ImmValue;
2283 SegmentOffset = readULEB128();
2284 DEBUG_WITH_TYPE(
2285 "mach-o-rebase",
2286 llvm::dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
2287 << "SegmentIndex=" << SegmentIndex << ", "
2288 << format("SegmentOffset=0x%06X", SegmentOffset)
2289 << "\n");
2290 break;
2291 case MachO::REBASE_OPCODE_ADD_ADDR_ULEB:
2292 SegmentOffset += readULEB128();
2293 DEBUG_WITH_TYPE("mach-o-rebase",
2294 llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: "
2295 << format("SegmentOffset=0x%06X",
2296 SegmentOffset) << "\n");
2297 break;
2298 case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
2299 SegmentOffset += ImmValue * PointerSize;
2300 DEBUG_WITH_TYPE("mach-o-rebase",
2301 llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: "
2302 << format("SegmentOffset=0x%06X",
2303 SegmentOffset) << "\n");
2304 break;
2305 case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES:
2306 AdvanceAmount = PointerSize;
2307 RemainingLoopCount = ImmValue - 1;
2308 DEBUG_WITH_TYPE(
2309 "mach-o-rebase",
2310 llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: "
2311 << format("SegmentOffset=0x%06X", SegmentOffset)
2312 << ", AdvanceAmount=" << AdvanceAmount
2313 << ", RemainingLoopCount=" << RemainingLoopCount
2314 << "\n");
2315 return;
2316 case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
2317 AdvanceAmount = PointerSize;
2318 RemainingLoopCount = readULEB128() - 1;
2319 DEBUG_WITH_TYPE(
2320 "mach-o-rebase",
2321 llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: "
2322 << format("SegmentOffset=0x%06X", SegmentOffset)
2323 << ", AdvanceAmount=" << AdvanceAmount
2324 << ", RemainingLoopCount=" << RemainingLoopCount
2325 << "\n");
2326 return;
2327 case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
2328 AdvanceAmount = readULEB128() + PointerSize;
2329 RemainingLoopCount = 0;
2330 DEBUG_WITH_TYPE(
2331 "mach-o-rebase",
2332 llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: "
2333 << format("SegmentOffset=0x%06X", SegmentOffset)
2334 << ", AdvanceAmount=" << AdvanceAmount
2335 << ", RemainingLoopCount=" << RemainingLoopCount
2336 << "\n");
2337 return;
2338 case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
2339 RemainingLoopCount = readULEB128() - 1;
2340 AdvanceAmount = readULEB128() + PointerSize;
2341 DEBUG_WITH_TYPE(
2342 "mach-o-rebase",
2343 llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: "
2344 << format("SegmentOffset=0x%06X", SegmentOffset)
2345 << ", AdvanceAmount=" << AdvanceAmount
2346 << ", RemainingLoopCount=" << RemainingLoopCount
2347 << "\n");
2348 return;
2349 default:
2350 Malformed = true;
2351 }
2352 }
2353}
2354
2355uint64_t MachORebaseEntry::readULEB128() {
2356 unsigned Count;
2357 uint64_t Result = decodeULEB128(Ptr, &Count);
2358 Ptr += Count;
2359 if (Ptr > Opcodes.end()) {
2360 Ptr = Opcodes.end();
2361 Malformed = true;
2362 }
2363 return Result;
2364}
2365
2366uint32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; }
2367
2368uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; }
2369
2370StringRef MachORebaseEntry::typeName() const {
2371 switch (RebaseType) {
2372 case MachO::REBASE_TYPE_POINTER:
2373 return "pointer";
2374 case MachO::REBASE_TYPE_TEXT_ABSOLUTE32:
2375 return "text abs32";
2376 case MachO::REBASE_TYPE_TEXT_PCREL32:
2377 return "text rel32";
2378 }
2379 return "unknown";
2380}
2381
2382bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const {
2383 assert(Opcodes == Other.Opcodes && "compare iterators of different files");
2384 return (Ptr == Other.Ptr) &&
2385 (RemainingLoopCount == Other.RemainingLoopCount) &&
2386 (Done == Other.Done);
2387}
2388
2389iterator_range<rebase_iterator>
2390MachOObjectFile::rebaseTable(ArrayRef<uint8_t> Opcodes, bool is64) {
2391 MachORebaseEntry Start(Opcodes, is64);
2392 Start.moveToFirst();
2393
2394 MachORebaseEntry Finish(Opcodes, is64);
2395 Finish.moveToEnd();
2396
Craig Topper15576e12015-12-06 05:08:07 +00002397 return make_range(rebase_iterator(Start), rebase_iterator(Finish));
Nick Kledzikac431442014-09-12 21:34:15 +00002398}
2399
2400iterator_range<rebase_iterator> MachOObjectFile::rebaseTable() const {
2401 return rebaseTable(getDyldInfoRebaseOpcodes(), is64Bit());
2402}
2403
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00002404MachOBindEntry::MachOBindEntry(ArrayRef<uint8_t> Bytes, bool is64Bit, Kind BK)
Nick Kledzik56ebef42014-09-16 01:41:51 +00002405 : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0),
2406 Ordinal(0), Flags(0), Addend(0), RemainingLoopCount(0), AdvanceAmount(0),
2407 BindType(0), PointerSize(is64Bit ? 8 : 4),
2408 TableKind(BK), Malformed(false), Done(false) {}
2409
2410void MachOBindEntry::moveToFirst() {
2411 Ptr = Opcodes.begin();
2412 moveNext();
2413}
2414
2415void MachOBindEntry::moveToEnd() {
2416 Ptr = Opcodes.end();
2417 RemainingLoopCount = 0;
2418 Done = true;
2419}
2420
2421void MachOBindEntry::moveNext() {
2422 // If in the middle of some loop, move to next binding in loop.
2423 SegmentOffset += AdvanceAmount;
2424 if (RemainingLoopCount) {
2425 --RemainingLoopCount;
2426 return;
2427 }
2428 if (Ptr == Opcodes.end()) {
2429 Done = true;
2430 return;
2431 }
2432 bool More = true;
2433 while (More && !Malformed) {
2434 // Parse next opcode and set up next loop.
2435 uint8_t Byte = *Ptr++;
2436 uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK;
2437 uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK;
2438 int8_t SignExtended;
2439 const uint8_t *SymStart;
2440 switch (Opcode) {
2441 case MachO::BIND_OPCODE_DONE:
2442 if (TableKind == Kind::Lazy) {
2443 // Lazying bindings have a DONE opcode between entries. Need to ignore
2444 // it to advance to next entry. But need not if this is last entry.
2445 bool NotLastEntry = false;
2446 for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) {
2447 if (*P) {
2448 NotLastEntry = true;
2449 }
2450 }
2451 if (NotLastEntry)
2452 break;
2453 }
2454 More = false;
2455 Done = true;
2456 moveToEnd();
2457 DEBUG_WITH_TYPE("mach-o-bind", llvm::dbgs() << "BIND_OPCODE_DONE\n");
2458 break;
2459 case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
2460 Ordinal = ImmValue;
2461 DEBUG_WITH_TYPE(
2462 "mach-o-bind",
2463 llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: "
2464 << "Ordinal=" << Ordinal << "\n");
2465 break;
2466 case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
2467 Ordinal = readULEB128();
2468 DEBUG_WITH_TYPE(
2469 "mach-o-bind",
2470 llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: "
2471 << "Ordinal=" << Ordinal << "\n");
2472 break;
2473 case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
2474 if (ImmValue) {
2475 SignExtended = MachO::BIND_OPCODE_MASK | ImmValue;
2476 Ordinal = SignExtended;
2477 } else
2478 Ordinal = 0;
2479 DEBUG_WITH_TYPE(
2480 "mach-o-bind",
2481 llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: "
2482 << "Ordinal=" << Ordinal << "\n");
2483 break;
2484 case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
2485 Flags = ImmValue;
2486 SymStart = Ptr;
2487 while (*Ptr) {
2488 ++Ptr;
2489 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00002490 SymbolName = StringRef(reinterpret_cast<const char*>(SymStart),
2491 Ptr-SymStart);
Nick Kledzika6375362014-09-17 01:51:43 +00002492 ++Ptr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00002493 DEBUG_WITH_TYPE(
2494 "mach-o-bind",
2495 llvm::dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: "
2496 << "SymbolName=" << SymbolName << "\n");
2497 if (TableKind == Kind::Weak) {
2498 if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
2499 return;
2500 }
2501 break;
2502 case MachO::BIND_OPCODE_SET_TYPE_IMM:
2503 BindType = ImmValue;
2504 DEBUG_WITH_TYPE(
2505 "mach-o-bind",
2506 llvm::dbgs() << "BIND_OPCODE_SET_TYPE_IMM: "
2507 << "BindType=" << (int)BindType << "\n");
2508 break;
2509 case MachO::BIND_OPCODE_SET_ADDEND_SLEB:
2510 Addend = readSLEB128();
2511 if (TableKind == Kind::Lazy)
2512 Malformed = true;
2513 DEBUG_WITH_TYPE(
2514 "mach-o-bind",
2515 llvm::dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: "
2516 << "Addend=" << Addend << "\n");
2517 break;
2518 case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
2519 SegmentIndex = ImmValue;
2520 SegmentOffset = readULEB128();
2521 DEBUG_WITH_TYPE(
2522 "mach-o-bind",
2523 llvm::dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
2524 << "SegmentIndex=" << SegmentIndex << ", "
2525 << format("SegmentOffset=0x%06X", SegmentOffset)
2526 << "\n");
2527 break;
2528 case MachO::BIND_OPCODE_ADD_ADDR_ULEB:
2529 SegmentOffset += readULEB128();
2530 DEBUG_WITH_TYPE("mach-o-bind",
2531 llvm::dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: "
2532 << format("SegmentOffset=0x%06X",
2533 SegmentOffset) << "\n");
2534 break;
2535 case MachO::BIND_OPCODE_DO_BIND:
2536 AdvanceAmount = PointerSize;
2537 RemainingLoopCount = 0;
2538 DEBUG_WITH_TYPE("mach-o-bind",
2539 llvm::dbgs() << "BIND_OPCODE_DO_BIND: "
2540 << format("SegmentOffset=0x%06X",
2541 SegmentOffset) << "\n");
2542 return;
2543 case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
Nick Kledzik3b2aa052014-10-18 01:21:02 +00002544 AdvanceAmount = readULEB128() + PointerSize;
Nick Kledzik56ebef42014-09-16 01:41:51 +00002545 RemainingLoopCount = 0;
2546 if (TableKind == Kind::Lazy)
2547 Malformed = true;
2548 DEBUG_WITH_TYPE(
2549 "mach-o-bind",
Nick Kledzik3b2aa052014-10-18 01:21:02 +00002550 llvm::dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: "
Nick Kledzik56ebef42014-09-16 01:41:51 +00002551 << format("SegmentOffset=0x%06X", SegmentOffset)
2552 << ", AdvanceAmount=" << AdvanceAmount
2553 << ", RemainingLoopCount=" << RemainingLoopCount
2554 << "\n");
2555 return;
2556 case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
Nick Kledzik3b2aa052014-10-18 01:21:02 +00002557 AdvanceAmount = ImmValue * PointerSize + PointerSize;
Nick Kledzik56ebef42014-09-16 01:41:51 +00002558 RemainingLoopCount = 0;
2559 if (TableKind == Kind::Lazy)
2560 Malformed = true;
2561 DEBUG_WITH_TYPE("mach-o-bind",
2562 llvm::dbgs()
2563 << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: "
2564 << format("SegmentOffset=0x%06X",
2565 SegmentOffset) << "\n");
2566 return;
2567 case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
2568 RemainingLoopCount = readULEB128() - 1;
2569 AdvanceAmount = readULEB128() + PointerSize;
2570 if (TableKind == Kind::Lazy)
2571 Malformed = true;
2572 DEBUG_WITH_TYPE(
2573 "mach-o-bind",
2574 llvm::dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: "
2575 << format("SegmentOffset=0x%06X", SegmentOffset)
2576 << ", AdvanceAmount=" << AdvanceAmount
2577 << ", RemainingLoopCount=" << RemainingLoopCount
2578 << "\n");
2579 return;
2580 default:
2581 Malformed = true;
2582 }
2583 }
2584}
2585
2586uint64_t MachOBindEntry::readULEB128() {
2587 unsigned Count;
2588 uint64_t Result = decodeULEB128(Ptr, &Count);
2589 Ptr += Count;
2590 if (Ptr > Opcodes.end()) {
2591 Ptr = Opcodes.end();
2592 Malformed = true;
2593 }
2594 return Result;
2595}
2596
2597int64_t MachOBindEntry::readSLEB128() {
2598 unsigned Count;
2599 int64_t Result = decodeSLEB128(Ptr, &Count);
2600 Ptr += Count;
2601 if (Ptr > Opcodes.end()) {
2602 Ptr = Opcodes.end();
2603 Malformed = true;
2604 }
2605 return Result;
2606}
2607
Nick Kledzik56ebef42014-09-16 01:41:51 +00002608uint32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; }
2609
2610uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; }
2611
2612StringRef MachOBindEntry::typeName() const {
2613 switch (BindType) {
2614 case MachO::BIND_TYPE_POINTER:
2615 return "pointer";
2616 case MachO::BIND_TYPE_TEXT_ABSOLUTE32:
2617 return "text abs32";
2618 case MachO::BIND_TYPE_TEXT_PCREL32:
2619 return "text rel32";
2620 }
2621 return "unknown";
2622}
2623
2624StringRef MachOBindEntry::symbolName() const { return SymbolName; }
2625
2626int64_t MachOBindEntry::addend() const { return Addend; }
2627
2628uint32_t MachOBindEntry::flags() const { return Flags; }
2629
2630int MachOBindEntry::ordinal() const { return Ordinal; }
2631
2632bool MachOBindEntry::operator==(const MachOBindEntry &Other) const {
2633 assert(Opcodes == Other.Opcodes && "compare iterators of different files");
2634 return (Ptr == Other.Ptr) &&
2635 (RemainingLoopCount == Other.RemainingLoopCount) &&
2636 (Done == Other.Done);
2637}
2638
2639iterator_range<bind_iterator>
2640MachOObjectFile::bindTable(ArrayRef<uint8_t> Opcodes, bool is64,
2641 MachOBindEntry::Kind BKind) {
2642 MachOBindEntry Start(Opcodes, is64, BKind);
2643 Start.moveToFirst();
2644
2645 MachOBindEntry Finish(Opcodes, is64, BKind);
2646 Finish.moveToEnd();
2647
Craig Topper15576e12015-12-06 05:08:07 +00002648 return make_range(bind_iterator(Start), bind_iterator(Finish));
Nick Kledzik56ebef42014-09-16 01:41:51 +00002649}
2650
2651iterator_range<bind_iterator> MachOObjectFile::bindTable() const {
2652 return bindTable(getDyldInfoBindOpcodes(), is64Bit(),
2653 MachOBindEntry::Kind::Regular);
2654}
2655
2656iterator_range<bind_iterator> MachOObjectFile::lazyBindTable() const {
2657 return bindTable(getDyldInfoLazyBindOpcodes(), is64Bit(),
2658 MachOBindEntry::Kind::Lazy);
2659}
2660
2661iterator_range<bind_iterator> MachOObjectFile::weakBindTable() const {
2662 return bindTable(getDyldInfoWeakBindOpcodes(), is64Bit(),
2663 MachOBindEntry::Kind::Weak);
2664}
2665
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002666MachOObjectFile::load_command_iterator
2667MachOObjectFile::begin_load_commands() const {
2668 return LoadCommands.begin();
2669}
2670
2671MachOObjectFile::load_command_iterator
2672MachOObjectFile::end_load_commands() const {
2673 return LoadCommands.end();
2674}
2675
2676iterator_range<MachOObjectFile::load_command_iterator>
2677MachOObjectFile::load_commands() const {
Craig Topper15576e12015-12-06 05:08:07 +00002678 return make_range(begin_load_commands(), end_load_commands());
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002679}
2680
Rafael Espindola56f976f2013-04-18 18:08:55 +00002681StringRef
2682MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
2683 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
2684 return parseSegmentOrSectionName(Raw.data());
2685}
2686
2687ArrayRef<char>
2688MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Rafael Espindola0d85d102015-05-22 14:59:27 +00002689 assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
Charles Davis8bdfafd2013-09-01 04:28:48 +00002690 const section_base *Base =
2691 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
Craig Toppere1d12942014-08-27 05:25:25 +00002692 return makeArrayRef(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00002693}
2694
2695ArrayRef<char>
2696MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindola0d85d102015-05-22 14:59:27 +00002697 assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
Charles Davis8bdfafd2013-09-01 04:28:48 +00002698 const section_base *Base =
2699 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
Craig Toppere1d12942014-08-27 05:25:25 +00002700 return makeArrayRef(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00002701}
2702
2703bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00002704MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00002705 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00002706 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00002707 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00002708 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00002709}
2710
Eric Christopher1d62c252013-07-22 22:25:07 +00002711unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00002712 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00002713 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00002714 return RE.r_word1 & 0xffffff;
2715 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00002716}
2717
Eric Christopher1d62c252013-07-22 22:25:07 +00002718bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00002719 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00002720 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00002721 return (RE.r_word1 >> 27) & 1;
2722 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00002723}
2724
Eric Christopher1d62c252013-07-22 22:25:07 +00002725bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00002726 const MachO::any_relocation_info &RE) const {
2727 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00002728}
2729
Eric Christopher1d62c252013-07-22 22:25:07 +00002730uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00002731 const MachO::any_relocation_info &RE) const {
2732 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00002733}
2734
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002735uint32_t MachOObjectFile::getScatteredRelocationType(
2736 const MachO::any_relocation_info &RE) const {
2737 return (RE.r_word0 >> 24) & 0xf;
2738}
2739
Eric Christopher1d62c252013-07-22 22:25:07 +00002740unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00002741 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00002742 if (isRelocationScattered(RE))
2743 return getScatteredRelocationAddress(RE);
2744 return getPlainRelocationAddress(RE);
2745}
2746
Charles Davis8bdfafd2013-09-01 04:28:48 +00002747unsigned MachOObjectFile::getAnyRelocationPCRel(
2748 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00002749 if (isRelocationScattered(RE))
2750 return getScatteredRelocationPCRel(this, RE);
2751 return getPlainRelocationPCRel(this, RE);
2752}
2753
Eric Christopher1d62c252013-07-22 22:25:07 +00002754unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00002755 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00002756 if (isRelocationScattered(RE))
2757 return getScatteredRelocationLength(RE);
2758 return getPlainRelocationLength(this, RE);
2759}
2760
2761unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00002762MachOObjectFile::getAnyRelocationType(
2763 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00002764 if (isRelocationScattered(RE))
2765 return getScatteredRelocationType(RE);
2766 return getPlainRelocationType(this, RE);
2767}
2768
Rafael Espindola52501032013-04-30 15:40:54 +00002769SectionRef
Keno Fischerc780e8e2015-05-21 21:24:32 +00002770MachOObjectFile::getAnyRelocationSection(
Charles Davis8bdfafd2013-09-01 04:28:48 +00002771 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00002772 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
Rafael Espindolab5155a52014-02-10 20:24:04 +00002773 return *section_end();
Rafael Espindola9ac06a02015-06-18 22:38:20 +00002774 unsigned SecNum = getPlainRelocationSymbolNum(RE);
2775 if (SecNum == MachO::R_ABS || SecNum > Sections.size())
2776 return *section_end();
Rafael Espindola52501032013-04-30 15:40:54 +00002777 DataRefImpl DRI;
Rafael Espindola9ac06a02015-06-18 22:38:20 +00002778 DRI.d.a = SecNum - 1;
Rafael Espindola52501032013-04-30 15:40:54 +00002779 return SectionRef(DRI, this);
2780}
2781
Charles Davis8bdfafd2013-09-01 04:28:48 +00002782MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindola62a07cb2015-05-22 15:43:00 +00002783 assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
Charles Davis8bdfafd2013-09-01 04:28:48 +00002784 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00002785}
2786
Charles Davis8bdfafd2013-09-01 04:28:48 +00002787MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
Rafael Espindola62a07cb2015-05-22 15:43:00 +00002788 assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
Charles Davis8bdfafd2013-09-01 04:28:48 +00002789 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00002790}
2791
Charles Davis8bdfafd2013-09-01 04:28:48 +00002792MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00002793 unsigned Index) const {
2794 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00002795 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00002796}
2797
Charles Davis8bdfafd2013-09-01 04:28:48 +00002798MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
2799 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00002800 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00002801 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00002802}
2803
Charles Davis8bdfafd2013-09-01 04:28:48 +00002804MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00002805MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00002806 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00002807 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00002808}
2809
Charles Davis8bdfafd2013-09-01 04:28:48 +00002810MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00002811MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00002812 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00002813 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00002814}
2815
Charles Davis8bdfafd2013-09-01 04:28:48 +00002816MachO::linkedit_data_command
2817MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
2818 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00002819}
2820
Charles Davis8bdfafd2013-09-01 04:28:48 +00002821MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00002822MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00002823 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00002824}
2825
Charles Davis8bdfafd2013-09-01 04:28:48 +00002826MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00002827MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00002828 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00002829}
2830
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00002831MachO::linker_option_command
2832MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const {
2833 return getStruct<MachO::linker_option_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00002834}
2835
Jim Grosbach448334a2014-03-18 22:09:05 +00002836MachO::version_min_command
2837MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
2838 return getStruct<MachO::version_min_command>(this, L.Ptr);
2839}
2840
Tim Northover8f9590b2014-06-30 14:40:57 +00002841MachO::dylib_command
2842MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const {
2843 return getStruct<MachO::dylib_command>(this, L.Ptr);
2844}
2845
Kevin Enderby8ae63c12014-09-04 16:54:47 +00002846MachO::dyld_info_command
2847MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const {
2848 return getStruct<MachO::dyld_info_command>(this, L.Ptr);
2849}
2850
2851MachO::dylinker_command
2852MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const {
2853 return getStruct<MachO::dylinker_command>(this, L.Ptr);
2854}
2855
2856MachO::uuid_command
2857MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const {
2858 return getStruct<MachO::uuid_command>(this, L.Ptr);
2859}
2860
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00002861MachO::rpath_command
2862MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const {
2863 return getStruct<MachO::rpath_command>(this, L.Ptr);
2864}
2865
Kevin Enderby8ae63c12014-09-04 16:54:47 +00002866MachO::source_version_command
2867MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const {
2868 return getStruct<MachO::source_version_command>(this, L.Ptr);
2869}
2870
2871MachO::entry_point_command
2872MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const {
2873 return getStruct<MachO::entry_point_command>(this, L.Ptr);
2874}
2875
Kevin Enderby0804f4672014-12-16 23:25:52 +00002876MachO::encryption_info_command
2877MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const {
2878 return getStruct<MachO::encryption_info_command>(this, L.Ptr);
2879}
2880
Kevin Enderby57538292014-12-17 01:01:30 +00002881MachO::encryption_info_command_64
2882MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const {
2883 return getStruct<MachO::encryption_info_command_64>(this, L.Ptr);
2884}
2885
Kevin Enderbyb4b79312014-12-18 19:24:35 +00002886MachO::sub_framework_command
2887MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const {
2888 return getStruct<MachO::sub_framework_command>(this, L.Ptr);
2889}
Tim Northover8f9590b2014-06-30 14:40:57 +00002890
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00002891MachO::sub_umbrella_command
2892MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const {
2893 return getStruct<MachO::sub_umbrella_command>(this, L.Ptr);
2894}
2895
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00002896MachO::sub_library_command
2897MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const {
2898 return getStruct<MachO::sub_library_command>(this, L.Ptr);
2899}
2900
Kevin Enderby186eac32014-12-19 21:06:24 +00002901MachO::sub_client_command
2902MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const {
2903 return getStruct<MachO::sub_client_command>(this, L.Ptr);
2904}
2905
Kevin Enderby52e4ce42014-12-19 22:25:22 +00002906MachO::routines_command
2907MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const {
2908 return getStruct<MachO::routines_command>(this, L.Ptr);
2909}
2910
2911MachO::routines_command_64
2912MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const {
2913 return getStruct<MachO::routines_command_64>(this, L.Ptr);
2914}
2915
Kevin Enderby48ef5342014-12-23 22:56:39 +00002916MachO::thread_command
2917MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const {
2918 return getStruct<MachO::thread_command>(this, L.Ptr);
2919}
2920
Charles Davis8bdfafd2013-09-01 04:28:48 +00002921MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00002922MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +00002923 DataRefImpl Sec;
2924 Sec.d.a = Rel.d.a;
2925 uint32_t Offset;
2926 if (is64Bit()) {
2927 MachO::section_64 Sect = getSection64(Sec);
2928 Offset = Sect.reloff;
2929 } else {
2930 MachO::section Sect = getSection(Sec);
2931 Offset = Sect.reloff;
2932 }
2933
2934 auto P = reinterpret_cast<const MachO::any_relocation_info *>(
2935 getPtr(this, Offset)) + Rel.d.b;
2936 return getStruct<MachO::any_relocation_info>(
2937 this, reinterpret_cast<const char *>(P));
Rafael Espindola56f976f2013-04-18 18:08:55 +00002938}
2939
Charles Davis8bdfafd2013-09-01 04:28:48 +00002940MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00002941MachOObjectFile::getDice(DataRefImpl Rel) const {
2942 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00002943 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00002944}
2945
Alexey Samsonov13415ed2015-06-04 19:22:03 +00002946const MachO::mach_header &MachOObjectFile::getHeader() const {
Alexey Samsonovfa5edc52015-06-04 22:49:55 +00002947 return Header;
Rafael Espindola56f976f2013-04-18 18:08:55 +00002948}
2949
Alexey Samsonov13415ed2015-06-04 19:22:03 +00002950const MachO::mach_header_64 &MachOObjectFile::getHeader64() const {
2951 assert(is64Bit());
2952 return Header64;
Rafael Espindola6e040c02013-04-26 20:07:33 +00002953}
2954
Charles Davis8bdfafd2013-09-01 04:28:48 +00002955uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
2956 const MachO::dysymtab_command &DLC,
2957 unsigned Index) const {
2958 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
2959 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00002960}
2961
Charles Davis8bdfafd2013-09-01 04:28:48 +00002962MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00002963MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
2964 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00002965 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
2966 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00002967}
2968
Charles Davis8bdfafd2013-09-01 04:28:48 +00002969MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002970 if (SymtabLoadCmd)
2971 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
2972
2973 // If there is no SymtabLoadCmd return a load command with zero'ed fields.
2974 MachO::symtab_command Cmd;
2975 Cmd.cmd = MachO::LC_SYMTAB;
2976 Cmd.cmdsize = sizeof(MachO::symtab_command);
2977 Cmd.symoff = 0;
2978 Cmd.nsyms = 0;
2979 Cmd.stroff = 0;
2980 Cmd.strsize = 0;
2981 return Cmd;
Rafael Espindola56f976f2013-04-18 18:08:55 +00002982}
2983
Charles Davis8bdfafd2013-09-01 04:28:48 +00002984MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002985 if (DysymtabLoadCmd)
2986 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
2987
2988 // If there is no DysymtabLoadCmd return a load command with zero'ed fields.
2989 MachO::dysymtab_command Cmd;
2990 Cmd.cmd = MachO::LC_DYSYMTAB;
2991 Cmd.cmdsize = sizeof(MachO::dysymtab_command);
2992 Cmd.ilocalsym = 0;
2993 Cmd.nlocalsym = 0;
2994 Cmd.iextdefsym = 0;
2995 Cmd.nextdefsym = 0;
2996 Cmd.iundefsym = 0;
2997 Cmd.nundefsym = 0;
2998 Cmd.tocoff = 0;
2999 Cmd.ntoc = 0;
3000 Cmd.modtaboff = 0;
3001 Cmd.nmodtab = 0;
3002 Cmd.extrefsymoff = 0;
3003 Cmd.nextrefsyms = 0;
3004 Cmd.indirectsymoff = 0;
3005 Cmd.nindirectsyms = 0;
3006 Cmd.extreloff = 0;
3007 Cmd.nextrel = 0;
3008 Cmd.locreloff = 0;
3009 Cmd.nlocrel = 0;
3010 return Cmd;
Rafael Espindola6e040c02013-04-26 20:07:33 +00003011}
3012
Charles Davis8bdfafd2013-09-01 04:28:48 +00003013MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00003014MachOObjectFile::getDataInCodeLoadCommand() const {
3015 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00003016 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00003017
3018 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00003019 MachO::linkedit_data_command Cmd;
3020 Cmd.cmd = MachO::LC_DATA_IN_CODE;
3021 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
3022 Cmd.dataoff = 0;
3023 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00003024 return Cmd;
3025}
3026
Kevin Enderby9a509442015-01-27 21:28:24 +00003027MachO::linkedit_data_command
3028MachOObjectFile::getLinkOptHintsLoadCommand() const {
3029 if (LinkOptHintsLoadCmd)
3030 return getStruct<MachO::linkedit_data_command>(this, LinkOptHintsLoadCmd);
3031
3032 // If there is no LinkOptHintsLoadCmd return a load command with zero'ed
3033 // fields.
3034 MachO::linkedit_data_command Cmd;
3035 Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT;
3036 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
3037 Cmd.dataoff = 0;
3038 Cmd.datasize = 0;
3039 return Cmd;
3040}
3041
Nick Kledzikd04bc352014-08-30 00:20:14 +00003042ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00003043 if (!DyldInfoLoadCmd)
Craig Topper0013be12015-09-21 05:32:41 +00003044 return None;
Nick Kledzikd04bc352014-08-30 00:20:14 +00003045
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00003046 MachO::dyld_info_command DyldInfo =
3047 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
3048 const uint8_t *Ptr =
3049 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.rebase_off));
Craig Topper0013be12015-09-21 05:32:41 +00003050 return makeArrayRef(Ptr, DyldInfo.rebase_size);
Nick Kledzikd04bc352014-08-30 00:20:14 +00003051}
3052
3053ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00003054 if (!DyldInfoLoadCmd)
Craig Topper0013be12015-09-21 05:32:41 +00003055 return None;
Nick Kledzikd04bc352014-08-30 00:20:14 +00003056
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00003057 MachO::dyld_info_command DyldInfo =
3058 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
3059 const uint8_t *Ptr =
3060 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.bind_off));
Craig Topper0013be12015-09-21 05:32:41 +00003061 return makeArrayRef(Ptr, DyldInfo.bind_size);
Nick Kledzikd04bc352014-08-30 00:20:14 +00003062}
3063
3064ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00003065 if (!DyldInfoLoadCmd)
Craig Topper0013be12015-09-21 05:32:41 +00003066 return None;
Nick Kledzikd04bc352014-08-30 00:20:14 +00003067
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00003068 MachO::dyld_info_command DyldInfo =
3069 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
3070 const uint8_t *Ptr =
3071 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.weak_bind_off));
Craig Topper0013be12015-09-21 05:32:41 +00003072 return makeArrayRef(Ptr, DyldInfo.weak_bind_size);
Nick Kledzikd04bc352014-08-30 00:20:14 +00003073}
3074
3075ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00003076 if (!DyldInfoLoadCmd)
Craig Topper0013be12015-09-21 05:32:41 +00003077 return None;
Nick Kledzikd04bc352014-08-30 00:20:14 +00003078
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00003079 MachO::dyld_info_command DyldInfo =
3080 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
3081 const uint8_t *Ptr =
3082 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.lazy_bind_off));
Craig Topper0013be12015-09-21 05:32:41 +00003083 return makeArrayRef(Ptr, DyldInfo.lazy_bind_size);
Nick Kledzikd04bc352014-08-30 00:20:14 +00003084}
3085
3086ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const {
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00003087 if (!DyldInfoLoadCmd)
Craig Topper0013be12015-09-21 05:32:41 +00003088 return None;
Nick Kledzikd04bc352014-08-30 00:20:14 +00003089
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +00003090 MachO::dyld_info_command DyldInfo =
3091 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
3092 const uint8_t *Ptr =
3093 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.export_off));
Craig Topper0013be12015-09-21 05:32:41 +00003094 return makeArrayRef(Ptr, DyldInfo.export_size);
Nick Kledzikd04bc352014-08-30 00:20:14 +00003095}
3096
Alexander Potapenko6909b5b2014-10-15 23:35:45 +00003097ArrayRef<uint8_t> MachOObjectFile::getUuid() const {
3098 if (!UuidLoadCmd)
Craig Topper0013be12015-09-21 05:32:41 +00003099 return None;
Benjamin Kramer014601d2014-10-24 15:52:05 +00003100 // Returning a pointer is fine as uuid doesn't need endian swapping.
3101 const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid);
Craig Topper0013be12015-09-21 05:32:41 +00003102 return makeArrayRef(reinterpret_cast<const uint8_t *>(Ptr), 16);
Alexander Potapenko6909b5b2014-10-15 23:35:45 +00003103}
Nick Kledzikd04bc352014-08-30 00:20:14 +00003104
Rafael Espindola6e040c02013-04-26 20:07:33 +00003105StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00003106 MachO::symtab_command S = getSymtabLoadCommand();
3107 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00003108}
3109
Rafael Espindola56f976f2013-04-18 18:08:55 +00003110bool MachOObjectFile::is64Bit() const {
3111 return getType() == getMachOType(false, true) ||
Lang Hames84bc8182014-07-15 19:35:22 +00003112 getType() == getMachOType(true, true);
Rafael Espindola56f976f2013-04-18 18:08:55 +00003113}
3114
3115void MachOObjectFile::ReadULEB128s(uint64_t Index,
3116 SmallVectorImpl<uint64_t> &Out) const {
3117 DataExtractor extractor(ObjectFile::getData(), true, 0);
3118
3119 uint32_t offset = Index;
3120 uint64_t data = 0;
3121 while (uint64_t delta = extractor.getULEB128(&offset)) {
3122 data += delta;
3123 Out.push_back(data);
3124 }
3125}
3126
Rafael Espindolac66d7612014-08-17 19:09:37 +00003127bool MachOObjectFile::isRelocatableObject() const {
3128 return getHeader().filetype == MachO::MH_OBJECT;
3129}
3130
Lang Hamesff044b12016-03-25 23:11:52 +00003131Expected<std::unique_ptr<MachOObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00003132ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) {
3133 StringRef Magic = Buffer.getBuffer().slice(0, 4);
Lang Hames82627642016-03-25 21:59:14 +00003134 if (Magic == "\xFE\xED\xFA\xCE")
Lang Hamesff044b12016-03-25 23:11:52 +00003135 return MachOObjectFile::create(Buffer, false, false);
David Blaikieb805f732016-03-28 17:45:48 +00003136 if (Magic == "\xCE\xFA\xED\xFE")
Lang Hamesff044b12016-03-25 23:11:52 +00003137 return MachOObjectFile::create(Buffer, true, false);
David Blaikieb805f732016-03-28 17:45:48 +00003138 if (Magic == "\xFE\xED\xFA\xCF")
Lang Hamesff044b12016-03-25 23:11:52 +00003139 return MachOObjectFile::create(Buffer, false, true);
David Blaikieb805f732016-03-28 17:45:48 +00003140 if (Magic == "\xCF\xFA\xED\xFE")
Lang Hamesff044b12016-03-25 23:11:52 +00003141 return MachOObjectFile::create(Buffer, true, true);
Kevin Enderbyd4e075b2016-05-06 20:16:28 +00003142 return make_error<GenericBinaryError>("Unrecognized MachO magic number",
Justin Bogner2a42da92016-05-05 23:59:57 +00003143 object_error::invalid_file_type);
Rafael Espindola56f976f2013-04-18 18:08:55 +00003144}