blob: 2bd87c970de3675bc82cdc1d9aafde75e86accca [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Triple.h"
Rafael Espindola421305a2013-04-07 20:01:29 +000018#include "llvm/Support/DataExtractor.h"
Owen Andersonbc14bd32011-10-26 20:42:54 +000019#include "llvm/Support/Format.h"
Rafael Espindola56f976f2013-04-18 18:08:55 +000020#include "llvm/Support/Host.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000021#include "llvm/Support/MemoryBuffer.h"
Jakub Staszak84a0ae72013-08-21 01:20:11 +000022#include "llvm/Support/raw_ostream.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000023#include <cctype>
24#include <cstring>
25#include <limits>
26
27using namespace llvm;
28using namespace object;
29
30namespace llvm {
Owen Anderson27c579d2011-10-11 17:32:27 +000031namespace object {
Eric Christopher7b015c72011-04-22 03:19:48 +000032
Charles Davis8bdfafd2013-09-01 04:28:48 +000033struct nlist_base {
34 uint32_t n_strx;
35 uint8_t n_type;
36 uint8_t n_sect;
37 uint16_t n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +000038};
39
Charles Davis8bdfafd2013-09-01 04:28:48 +000040struct section_base {
41 char sectname[16];
42 char segname[16];
Rafael Espindola56f976f2013-04-18 18:08:55 +000043};
44
45template<typename T>
46static void SwapValue(T &Value) {
47 Value = sys::SwapByteOrder(Value);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +000048}
49
Rafael Espindola56f976f2013-04-18 18:08:55 +000050template<typename T>
51static void SwapStruct(T &Value);
52
53template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000054void SwapStruct(MachO::any_relocation_info &H) {
55 SwapValue(H.r_word0);
56 SwapValue(H.r_word1);
Rafael Espindola5ffc0792013-04-07 16:07:35 +000057}
58
Rafael Espindola56f976f2013-04-18 18:08:55 +000059template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000060void SwapStruct(MachO::load_command &L) {
61 SwapValue(L.cmd);
62 SwapValue(L.cmdsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +000063}
Rafael Espindola421305a2013-04-07 20:01:29 +000064
Rafael Espindola56f976f2013-04-18 18:08:55 +000065template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000066void SwapStruct(nlist_base &S) {
67 SwapValue(S.n_strx);
68 SwapValue(S.n_desc);
Rafael Espindola56f976f2013-04-18 18:08:55 +000069}
70
71template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000072void SwapStruct(MachO::section &S) {
73 SwapValue(S.addr);
74 SwapValue(S.size);
75 SwapValue(S.offset);
76 SwapValue(S.align);
77 SwapValue(S.reloff);
78 SwapValue(S.nreloc);
79 SwapValue(S.flags);
80 SwapValue(S.reserved1);
81 SwapValue(S.reserved2);
Rafael Espindola56f976f2013-04-18 18:08:55 +000082}
83
84template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000085void SwapStruct(MachO::section_64 &S) {
86 SwapValue(S.addr);
87 SwapValue(S.size);
88 SwapValue(S.offset);
89 SwapValue(S.align);
90 SwapValue(S.reloff);
91 SwapValue(S.nreloc);
92 SwapValue(S.flags);
93 SwapValue(S.reserved1);
94 SwapValue(S.reserved2);
95 SwapValue(S.reserved3);
Rafael Espindola56f976f2013-04-18 18:08:55 +000096}
97
98template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000099void SwapStruct(MachO::nlist &S) {
100 SwapValue(S.n_strx);
101 SwapValue(S.n_desc);
102 SwapValue(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000103}
104
105template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000106void SwapStruct(MachO::nlist_64 &S) {
107 SwapValue(S.n_strx);
108 SwapValue(S.n_desc);
109 SwapValue(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000110}
111
112template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000113void SwapStruct(MachO::mach_header &H) {
114 SwapValue(H.magic);
115 SwapValue(H.cputype);
116 SwapValue(H.cpusubtype);
117 SwapValue(H.filetype);
118 SwapValue(H.ncmds);
119 SwapValue(H.sizeofcmds);
120 SwapValue(H.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000121}
122
123template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000124void SwapStruct(MachO::mach_header_64 &H) {
125 SwapValue(H.magic);
126 SwapValue(H.cputype);
127 SwapValue(H.cpusubtype);
128 SwapValue(H.filetype);
129 SwapValue(H.ncmds);
130 SwapValue(H.sizeofcmds);
131 SwapValue(H.flags);
132 SwapValue(H.reserved);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000133}
134
135template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000136void SwapStruct(MachO::symtab_command &C) {
137 SwapValue(C.cmd);
138 SwapValue(C.cmdsize);
139 SwapValue(C.symoff);
140 SwapValue(C.nsyms);
141 SwapValue(C.stroff);
142 SwapValue(C.strsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000143}
144
145template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000146void SwapStruct(MachO::dysymtab_command &C) {
147 SwapValue(C.cmd);
148 SwapValue(C.cmdsize);
149 SwapValue(C.ilocalsym);
150 SwapValue(C.nlocalsym);
151 SwapValue(C.iextdefsym);
152 SwapValue(C.nextdefsym);
153 SwapValue(C.iundefsym);
154 SwapValue(C.nundefsym);
155 SwapValue(C.tocoff);
156 SwapValue(C.ntoc);
157 SwapValue(C.modtaboff);
158 SwapValue(C.nmodtab);
159 SwapValue(C.extrefsymoff);
160 SwapValue(C.nextrefsyms);
161 SwapValue(C.indirectsymoff);
162 SwapValue(C.nindirectsyms);
163 SwapValue(C.extreloff);
164 SwapValue(C.nextrel);
165 SwapValue(C.locreloff);
166 SwapValue(C.nlocrel);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000167}
168
169template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000170void SwapStruct(MachO::linkedit_data_command &C) {
171 SwapValue(C.cmd);
172 SwapValue(C.cmdsize);
173 SwapValue(C.dataoff);
174 SwapValue(C.datasize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000175}
176
177template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000178void SwapStruct(MachO::segment_command &C) {
179 SwapValue(C.cmd);
180 SwapValue(C.cmdsize);
181 SwapValue(C.vmaddr);
182 SwapValue(C.vmsize);
183 SwapValue(C.fileoff);
184 SwapValue(C.filesize);
185 SwapValue(C.maxprot);
186 SwapValue(C.initprot);
187 SwapValue(C.nsects);
188 SwapValue(C.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000189}
190
191template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000192void SwapStruct(MachO::segment_command_64 &C) {
193 SwapValue(C.cmd);
194 SwapValue(C.cmdsize);
195 SwapValue(C.vmaddr);
196 SwapValue(C.vmsize);
197 SwapValue(C.fileoff);
198 SwapValue(C.filesize);
199 SwapValue(C.maxprot);
200 SwapValue(C.initprot);
201 SwapValue(C.nsects);
202 SwapValue(C.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000203}
204
Rafael Espindola6e040c02013-04-26 20:07:33 +0000205template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000206void SwapStruct(uint32_t &C) {
207 SwapValue(C);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000208}
209
210template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000211void SwapStruct(MachO::linker_options_command &C) {
212 SwapValue(C.cmd);
213 SwapValue(C.cmdsize);
214 SwapValue(C.count);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000215}
216
217template<>
Jim Grosbach448334a2014-03-18 22:09:05 +0000218void SwapStruct(MachO::version_min_command&C) {
219 SwapValue(C.cmd);
220 SwapValue(C.cmdsize);
221 SwapValue(C.version);
222 SwapValue(C.reserved);
223}
224
225template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000226void SwapStruct(MachO::data_in_code_entry &C) {
227 SwapValue(C.offset);
228 SwapValue(C.length);
229 SwapValue(C.kind);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000230}
231
Rafael Espindola3cdeb172013-04-19 13:45:05 +0000232template<typename T>
233T getStruct(const MachOObjectFile *O, const char *P) {
234 T Cmd;
235 memcpy(&Cmd, P, sizeof(T));
236 if (O->isLittleEndian() != sys::IsLittleEndianHost)
237 SwapStruct(Cmd);
238 return Cmd;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000239}
240
Rafael Espindola56f976f2013-04-18 18:08:55 +0000241static uint32_t
242getSegmentLoadCommandNumSections(const MachOObjectFile *O,
243 const MachOObjectFile::LoadCommandInfo &L) {
244 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000245 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
246 return S.nsects;
Rafael Espindola421305a2013-04-07 20:01:29 +0000247 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000248 MachO::segment_command S = O->getSegmentLoadCommand(L);
249 return S.nsects;
Rafael Espindola5ffc0792013-04-07 16:07:35 +0000250}
251
Rafael Espindola6e040c02013-04-26 20:07:33 +0000252static const char *
253getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
254 unsigned Sec) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000255 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
256
257 bool Is64 = O->is64Bit();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000258 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
259 sizeof(MachO::segment_command);
260 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
261 sizeof(MachO::section);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000262
263 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
Charles Davis1827bd82013-08-27 05:38:30 +0000264 return reinterpret_cast<const char*>(SectionAddr);
Rafael Espindola60689982013-04-07 19:05:30 +0000265}
266
Rafael Espindola56f976f2013-04-18 18:08:55 +0000267static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
268 return O->getData().substr(Offset, 1).data();
Rafael Espindola60689982013-04-07 19:05:30 +0000269}
270
Charles Davis8bdfafd2013-09-01 04:28:48 +0000271static nlist_base
Rafael Espindola56f976f2013-04-18 18:08:55 +0000272getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola75c30362013-04-24 19:47:55 +0000273 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000274 return getStruct<nlist_base>(O, P);
Eric Christopher7b015c72011-04-22 03:19:48 +0000275}
276
Rafael Espindola56f976f2013-04-18 18:08:55 +0000277static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000278 if (P[15] == 0)
279 // Null terminated.
280 return P;
281 // Not null terminated, so this is a 16 char string.
282 return StringRef(P, 16);
283}
284
Rafael Espindola56f976f2013-04-18 18:08:55 +0000285// Helper to advance a section or symbol iterator multiple increments at a time.
286template<class T>
Rafael Espindola5e812af2014-01-30 02:49:50 +0000287static void advance(T &it, size_t Val) {
288 while (Val--)
289 ++it;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000290}
291
292static unsigned getCPUType(const MachOObjectFile *O) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000293 return O->getHeader().cputype;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000294}
295
296static void printRelocationTargetName(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000297 const MachO::any_relocation_info &RE,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000298 raw_string_ostream &fmt) {
299 bool IsScattered = O->isRelocationScattered(RE);
300
301 // Target of a scattered relocation is an address. In the interest of
302 // generating pretty output, scan through the symbol table looking for a
303 // symbol that aligns with that address. If we find one, print it.
304 // Otherwise, we just print the hex address of the target.
305 if (IsScattered) {
306 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
307
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000308 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000309 error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000310 uint64_t Addr;
311 StringRef Name;
312
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000313 if ((ec = Symbol.getAddress(Addr)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000314 report_fatal_error(ec.message());
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000315 if (Addr != Val)
316 continue;
317 if ((ec = Symbol.getName(Name)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000318 report_fatal_error(ec.message());
319 fmt << Name;
320 return;
321 }
322
323 // If we couldn't find a symbol that this relocation refers to, try
324 // to find a section beginning instead.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000325 for (const SectionRef &Section : O->sections()) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000326 error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000327 uint64_t Addr;
328 StringRef Name;
329
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000330 if ((ec = Section.getAddress(Addr)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000331 report_fatal_error(ec.message());
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000332 if (Addr != Val)
333 continue;
334 if ((ec = Section.getName(Name)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000335 report_fatal_error(ec.message());
336 fmt << Name;
337 return;
338 }
339
340 fmt << format("0x%x", Val);
341 return;
342 }
343
344 StringRef S;
345 bool isExtern = O->getPlainRelocationExternal(RE);
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000346 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000347
348 if (isExtern) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000349 symbol_iterator SI = O->symbol_begin();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000350 advance(SI, Val);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000351 SI->getName(S);
352 } else {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000353 section_iterator SI = O->section_begin();
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000354 // Adjust for the fact that sections are 1-indexed.
Rafael Espindola5e812af2014-01-30 02:49:50 +0000355 advance(SI, Val - 1);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000356 SI->getName(S);
357 }
358
359 fmt << S;
360}
361
Charles Davis8bdfafd2013-09-01 04:28:48 +0000362static uint32_t
363getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
364 return RE.r_word0;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000365}
366
367static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000368getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
369 return RE.r_word0 & 0xffffff;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000370}
371
372static bool getPlainRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000373 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000374 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000375 return (RE.r_word1 >> 24) & 1;
376 return (RE.r_word1 >> 7) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000377}
378
379static bool
380getScatteredRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000381 const MachO::any_relocation_info &RE) {
382 return (RE.r_word0 >> 30) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000383}
384
385static unsigned getPlainRelocationLength(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000386 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000387 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000388 return (RE.r_word1 >> 25) & 3;
389 return (RE.r_word1 >> 5) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000390}
391
392static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000393getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
394 return (RE.r_word0 >> 28) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000395}
396
397static unsigned getPlainRelocationType(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000398 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000399 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000400 return RE.r_word1 >> 28;
401 return RE.r_word1 & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000402}
403
Charles Davis8bdfafd2013-09-01 04:28:48 +0000404static unsigned
405getScatteredRelocationType(const MachO::any_relocation_info &RE) {
406 return (RE.r_word0 >> 24) & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000407}
408
409static uint32_t getSectionFlags(const MachOObjectFile *O,
410 DataRefImpl Sec) {
411 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000412 MachO::section_64 Sect = O->getSection64(Sec);
413 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000414 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000415 MachO::section Sect = O->getSection(Sec);
416 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000417}
418
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000419MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
420 bool Is64bits, error_code &EC,
421 bool BufferOwned)
422 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned),
Kevin Enderby273ae012013-06-06 17:20:50 +0000423 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000424 uint32_t LoadCommandCount = this->getHeader().ncmds;
425 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
426 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000427
428 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000429 for (unsigned I = 0; ; ++I) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000430 if (Load.C.cmd == MachO::LC_SYMTAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000431 assert(!SymtabLoadCmd && "Multiple symbol tables");
432 SymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000433 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000434 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
435 DysymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000436 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000437 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
438 DataInCodeLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000439 } else if (Load.C.cmd == SegmentLoadType) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000440 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
441 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000442 const char *Sec = getSectionPtr(this, Load, J);
443 Sections.push_back(Sec);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000444 }
445 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000446
447 if (I == LoadCommandCount - 1)
448 break;
449 else
450 Load = getNextLoadCommandInfo(Load);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000451 }
452}
453
Rafael Espindola5e812af2014-01-30 02:49:50 +0000454void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
Rafael Espindola75c30362013-04-24 19:47:55 +0000455 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000456 sizeof(MachO::nlist_64) :
457 sizeof(MachO::nlist);
Rafael Espindola75c30362013-04-24 19:47:55 +0000458 Symb.p += SymbolTableEntrySize;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000459}
460
461error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
462 StringRef &Res) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000463 StringRef StringTable = getStringTableData();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000464 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
465 const char *Start = &StringTable.data()[Entry.n_strx];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000466 Res = StringRef(Start);
467 return object_error::success;
468}
469
470error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
471 uint64_t &Res) const {
472 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000473 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
474 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000475 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000476 MachO::nlist Entry = getSymbolTableEntry(Symb);
477 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000478 }
479 return object_error::success;
480}
481
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000482error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
483 uint32_t &Result) const {
Rafael Espindola20122a42014-01-31 20:57:12 +0000484 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000485 if (flags & SymbolRef::SF_Common) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000486 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
487 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000488 } else {
489 Result = 0;
490 }
491 return object_error::success;
492}
493
Rafael Espindola56f976f2013-04-18 18:08:55 +0000494error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
495 uint64_t &Result) const {
496 uint64_t BeginOffset;
497 uint64_t EndOffset = 0;
498 uint8_t SectionIndex;
499
Charles Davis8bdfafd2013-09-01 04:28:48 +0000500 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000501 uint64_t Value;
502 getSymbolAddress(DRI, Value);
503
504 BeginOffset = Value;
505
Charles Davis8bdfafd2013-09-01 04:28:48 +0000506 SectionIndex = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000507 if (!SectionIndex) {
Rafael Espindola20122a42014-01-31 20:57:12 +0000508 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000509 if (flags & SymbolRef::SF_Common)
510 Result = Value;
511 else
512 Result = UnknownAddressOrSize;
513 return object_error::success;
514 }
515 // Unfortunately symbols are unsorted so we need to touch all
516 // symbols from load command
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000517 for (const SymbolRef &Symbol : symbols()) {
518 DataRefImpl DRI = Symbol.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000519 Entry = getSymbolTableEntryBase(this, DRI);
520 getSymbolAddress(DRI, Value);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000521 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000522 if (!EndOffset || Value < EndOffset)
523 EndOffset = Value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000524 }
525 if (!EndOffset) {
526 uint64_t Size;
527 DataRefImpl Sec;
528 Sec.d.a = SectionIndex-1;
529 getSectionSize(Sec, Size);
530 getSectionAddress(Sec, EndOffset);
531 EndOffset += Size;
532 }
533 Result = EndOffset - BeginOffset;
534 return object_error::success;
535}
536
537error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
538 SymbolRef::Type &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000539 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
540 uint8_t n_type = Entry.n_type;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000541
542 Res = SymbolRef::ST_Other;
543
544 // If this is a STAB debugging symbol, we can do nothing more.
Charles Davis74ec8b02013-08-27 05:00:13 +0000545 if (n_type & MachO::N_STAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000546 Res = SymbolRef::ST_Debug;
547 return object_error::success;
548 }
549
Charles Davis74ec8b02013-08-27 05:00:13 +0000550 switch (n_type & MachO::N_TYPE) {
551 case MachO::N_UNDF :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000552 Res = SymbolRef::ST_Unknown;
553 break;
Charles Davis74ec8b02013-08-27 05:00:13 +0000554 case MachO::N_SECT :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000555 Res = SymbolRef::ST_Function;
556 break;
557 }
558 return object_error::success;
559}
560
Rafael Espindola20122a42014-01-31 20:57:12 +0000561uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000562 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000563
Charles Davis8bdfafd2013-09-01 04:28:48 +0000564 uint8_t MachOType = Entry.n_type;
565 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000566
Rafael Espindola20122a42014-01-31 20:57:12 +0000567 uint32_t Result = SymbolRef::SF_None;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000568
Charles Davis74ec8b02013-08-27 05:00:13 +0000569 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000570 Result |= SymbolRef::SF_Undefined;
571
Rafael Espindolaa1356322013-11-02 05:03:24 +0000572 if (MachOType & MachO::N_STAB)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000573 Result |= SymbolRef::SF_FormatSpecific;
574
Charles Davis74ec8b02013-08-27 05:00:13 +0000575 if (MachOType & MachO::N_EXT) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000576 Result |= SymbolRef::SF_Global;
Charles Davis74ec8b02013-08-27 05:00:13 +0000577 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000578 uint64_t Value;
579 getSymbolAddress(DRI, Value);
580 if (Value)
581 Result |= SymbolRef::SF_Common;
582 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000583 }
584
Charles Davis74ec8b02013-08-27 05:00:13 +0000585 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000586 Result |= SymbolRef::SF_Weak;
587
Charles Davis74ec8b02013-08-27 05:00:13 +0000588 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000589 Result |= SymbolRef::SF_Absolute;
590
Rafael Espindola20122a42014-01-31 20:57:12 +0000591 return Result;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000592}
593
594error_code
595MachOObjectFile::getSymbolSection(DataRefImpl Symb,
596 section_iterator &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000597 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
598 uint8_t index = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000599
600 if (index == 0) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000601 Res = section_end();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000602 } else {
603 DataRefImpl DRI;
604 DRI.d.a = index - 1;
605 Res = section_iterator(SectionRef(DRI, this));
606 }
607
608 return object_error::success;
609}
610
Rafael Espindola5e812af2014-01-30 02:49:50 +0000611void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000612 Sec.d.a++;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000613}
614
615error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000616MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000617 ArrayRef<char> Raw = getSectionRawName(Sec);
618 Result = parseSegmentOrSectionName(Raw.data());
619 return object_error::success;
620}
621
622error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000623MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000624 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000625 MachO::section_64 Sect = getSection64(Sec);
626 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000627 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000628 MachO::section Sect = getSection(Sec);
629 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000630 }
631 return object_error::success;
632}
633
634error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000635MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000636 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000637 MachO::section_64 Sect = getSection64(Sec);
638 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000639 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000640 MachO::section Sect = getSection(Sec);
641 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000642 }
643
644 return object_error::success;
645}
646
647error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000648MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000649 uint32_t Offset;
650 uint64_t Size;
651
652 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000653 MachO::section_64 Sect = getSection64(Sec);
654 Offset = Sect.offset;
655 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000656 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000657 MachO::section Sect = getSection(Sec);
658 Offset = Sect.offset;
659 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000660 }
661
662 Res = this->getData().substr(Offset, Size);
663 return object_error::success;
664}
665
666error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000667MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000668 uint32_t Align;
669 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000670 MachO::section_64 Sect = getSection64(Sec);
671 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000672 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000673 MachO::section Sect = getSection(Sec);
674 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000675 }
676
677 Res = uint64_t(1) << Align;
678 return object_error::success;
679}
680
681error_code
682MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
683 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000684 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000685 return object_error::success;
686}
687
Rafael Espindola137faa02013-04-24 15:14:22 +0000688error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
Michael J. Spencer800619f2011-09-28 20:57:30 +0000689 // FIXME: Unimplemented.
690 Result = false;
691 return object_error::success;
692}
693
Rafael Espindola137faa02013-04-24 15:14:22 +0000694error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000695 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000696 Result = false;
697 return object_error::success;
698}
699
Rafael Espindolac2413f52013-04-09 14:49:08 +0000700error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000701MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000702 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000703 // FIXME: Unimplemented.
704 Result = true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000705 return object_error::success;
706}
707
Rafael Espindola56f976f2013-04-18 18:08:55 +0000708error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000709 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000710 // FIXME: Unimplemented.
711 Result = false;
712 return object_error::success;
713}
714
Rafael Espindola56f976f2013-04-18 18:08:55 +0000715error_code
716MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
717 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis74ec8b02013-08-27 05:00:13 +0000718 unsigned SectionType = Flags & MachO::SECTION_TYPE;
719 Res = SectionType == MachO::S_ZEROFILL ||
720 SectionType == MachO::S_GB_ZEROFILL;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000721 return object_error::success;
722}
723
724error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000725 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000726 // Consider using the code from isSectionText to look for __const sections.
727 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
728 // to use section attributes to distinguish code from data.
729
730 // FIXME: Unimplemented.
731 Result = false;
732 return object_error::success;
733}
734
Rafael Espindola56f976f2013-04-18 18:08:55 +0000735error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000736MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000737 bool &Result) const {
738 SymbolRef::Type ST;
739 this->getSymbolType(Symb, ST);
740 if (ST == SymbolRef::ST_Unknown) {
741 Result = false;
742 return object_error::success;
743 }
744
745 uint64_t SectBegin, SectEnd;
746 getSectionAddress(Sec, SectBegin);
747 getSectionSize(Sec, SectEnd);
748 SectEnd += SectBegin;
749
750 uint64_t SymAddr;
751 getSymbolAddress(Symb, SymAddr);
752 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
753
754 return object_error::success;
755}
756
Rui Ueyamabc654b12013-09-27 21:47:05 +0000757relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000758 uint32_t Offset;
759 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000760 MachO::section_64 Sect = getSection64(Sec);
761 Offset = Sect.reloff;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000762 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000763 MachO::section Sect = getSection(Sec);
764 Offset = Sect.reloff;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000765 }
766
767 DataRefImpl Ret;
768 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
769 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000770}
Rafael Espindolac0406e12013-04-08 20:45:01 +0000771
Rafael Espindola56f976f2013-04-18 18:08:55 +0000772relocation_iterator
Rui Ueyamabc654b12013-09-27 21:47:05 +0000773MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000774 uint32_t Offset;
775 uint32_t Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000776 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000777 MachO::section_64 Sect = getSection64(Sec);
778 Offset = Sect.reloff;
779 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000780 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000781 MachO::section Sect = getSection(Sec);
782 Offset = Sect.reloff;
783 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000784 }
Eric Christopher7b015c72011-04-22 03:19:48 +0000785
Charles Davis8bdfafd2013-09-01 04:28:48 +0000786 const MachO::any_relocation_info *P =
787 reinterpret_cast<const MachO::any_relocation_info *>(getPtr(this, Offset));
Rafael Espindola04d3f492013-04-25 12:45:46 +0000788
Rafael Espindola56f976f2013-04-18 18:08:55 +0000789 DataRefImpl Ret;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000790 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000791 return relocation_iterator(RelocationRef(Ret, this));
792}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000793
Rafael Espindola5e812af2014-01-30 02:49:50 +0000794void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000795 const MachO::any_relocation_info *P =
796 reinterpret_cast<const MachO::any_relocation_info *>(Rel.p);
Rafael Espindola04d3f492013-04-25 12:45:46 +0000797 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000798}
Owen Anderson171f4852011-10-24 23:20:07 +0000799
Rafael Espindola56f976f2013-04-18 18:08:55 +0000800error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000801MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000802 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000803}
804
Rafael Espindola56f976f2013-04-18 18:08:55 +0000805error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000806 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000807 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000808 Res = getAnyRelocationAddress(RE);
809 return object_error::success;
David Meyer2fc34c52012-03-01 01:36:50 +0000810}
811
Rafael Espindola806f0062013-06-05 01:33:53 +0000812symbol_iterator
813MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000814 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000815 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
816 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola806f0062013-06-05 01:33:53 +0000817 if (!isExtern)
Rafael Espindolab5155a52014-02-10 20:24:04 +0000818 return symbol_end();
Rafael Espindola75c30362013-04-24 19:47:55 +0000819
Charles Davis8bdfafd2013-09-01 04:28:48 +0000820 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +0000821 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000822 sizeof(MachO::nlist_64) :
823 sizeof(MachO::nlist);
824 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +0000825 DataRefImpl Sym;
826 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola806f0062013-06-05 01:33:53 +0000827 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +0000828}
829
830error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000831 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000832 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000833 Res = getAnyRelocationType(RE);
834 return object_error::success;
835}
836
837error_code
838MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
839 SmallVectorImpl<char> &Result) const {
840 StringRef res;
841 uint64_t RType;
842 getRelocationType(Rel, RType);
843
844 unsigned Arch = this->getArch();
845
846 switch (Arch) {
847 case Triple::x86: {
848 static const char *const Table[] = {
849 "GENERIC_RELOC_VANILLA",
850 "GENERIC_RELOC_PAIR",
851 "GENERIC_RELOC_SECTDIFF",
852 "GENERIC_RELOC_PB_LA_PTR",
853 "GENERIC_RELOC_LOCAL_SECTDIFF",
854 "GENERIC_RELOC_TLV" };
855
Eric Christopher13250cb2013-12-06 02:33:38 +0000856 if (RType > 5)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000857 res = "Unknown";
858 else
859 res = Table[RType];
860 break;
861 }
862 case Triple::x86_64: {
863 static const char *const Table[] = {
864 "X86_64_RELOC_UNSIGNED",
865 "X86_64_RELOC_SIGNED",
866 "X86_64_RELOC_BRANCH",
867 "X86_64_RELOC_GOT_LOAD",
868 "X86_64_RELOC_GOT",
869 "X86_64_RELOC_SUBTRACTOR",
870 "X86_64_RELOC_SIGNED_1",
871 "X86_64_RELOC_SIGNED_2",
872 "X86_64_RELOC_SIGNED_4",
873 "X86_64_RELOC_TLV" };
874
875 if (RType > 9)
876 res = "Unknown";
877 else
878 res = Table[RType];
879 break;
880 }
881 case Triple::arm: {
882 static const char *const Table[] = {
883 "ARM_RELOC_VANILLA",
884 "ARM_RELOC_PAIR",
885 "ARM_RELOC_SECTDIFF",
886 "ARM_RELOC_LOCAL_SECTDIFF",
887 "ARM_RELOC_PB_LA_PTR",
888 "ARM_RELOC_BR24",
889 "ARM_THUMB_RELOC_BR22",
890 "ARM_THUMB_32BIT_BRANCH",
891 "ARM_RELOC_HALF",
892 "ARM_RELOC_HALF_SECTDIFF" };
893
894 if (RType > 9)
895 res = "Unknown";
896 else
897 res = Table[RType];
898 break;
899 }
Tim Northover00ed9962014-03-29 10:18:08 +0000900 case Triple::arm64:
901 case Triple::aarch64: {
902 static const char *const Table[] = {
903 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
904 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
905 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
906 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
907 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
908 "ARM64_RELOC_ADDEND"
909 };
910
911 if (RType >= array_lengthof(Table))
912 res = "Unknown";
913 else
914 res = Table[RType];
915 break;
916 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000917 case Triple::ppc: {
918 static const char *const Table[] = {
919 "PPC_RELOC_VANILLA",
920 "PPC_RELOC_PAIR",
921 "PPC_RELOC_BR14",
922 "PPC_RELOC_BR24",
923 "PPC_RELOC_HI16",
924 "PPC_RELOC_LO16",
925 "PPC_RELOC_HA16",
926 "PPC_RELOC_LO14",
927 "PPC_RELOC_SECTDIFF",
928 "PPC_RELOC_PB_LA_PTR",
929 "PPC_RELOC_HI16_SECTDIFF",
930 "PPC_RELOC_LO16_SECTDIFF",
931 "PPC_RELOC_HA16_SECTDIFF",
932 "PPC_RELOC_JBSR",
933 "PPC_RELOC_LO14_SECTDIFF",
934 "PPC_RELOC_LOCAL_SECTDIFF" };
935
Eric Christopher13250cb2013-12-06 02:33:38 +0000936 if (RType > 15)
937 res = "Unknown";
938 else
939 res = Table[RType];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000940 break;
941 }
942 case Triple::UnknownArch:
943 res = "Unknown";
944 break;
945 }
946 Result.append(res.begin(), res.end());
947 return object_error::success;
948}
949
Rafael Espindola56f976f2013-04-18 18:08:55 +0000950error_code
951MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000952 SmallVectorImpl<char> &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000953 MachO::any_relocation_info RE = getRelocation(Rel);
David Meyer2fc34c52012-03-01 01:36:50 +0000954
Rafael Espindola56f976f2013-04-18 18:08:55 +0000955 unsigned Arch = this->getArch();
Eric Christopher7b015c72011-04-22 03:19:48 +0000956
Rafael Espindola56f976f2013-04-18 18:08:55 +0000957 std::string fmtbuf;
958 raw_string_ostream fmt(fmtbuf);
959 unsigned Type = this->getAnyRelocationType(RE);
960 bool IsPCRel = this->getAnyRelocationPCRel(RE);
961
962 // Determine any addends that should be displayed with the relocation.
963 // These require decoding the relocation type, which is triple-specific.
964
965 // X86_64 has entirely custom relocation types.
966 if (Arch == Triple::x86_64) {
967 bool isPCRel = getAnyRelocationPCRel(RE);
968
969 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000970 case MachO::X86_64_RELOC_GOT_LOAD:
971 case MachO::X86_64_RELOC_GOT: {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000972 printRelocationTargetName(this, RE, fmt);
973 fmt << "@GOT";
974 if (isPCRel) fmt << "PCREL";
975 break;
976 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000977 case MachO::X86_64_RELOC_SUBTRACTOR: {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000978 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +0000979 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000980 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000981
Charles Davis8bdfafd2013-09-01 04:28:48 +0000982 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
Rafael Espindola56f976f2013-04-18 18:08:55 +0000983 // X86_64_RELOC_UNSIGNED.
984 // NOTE: Scattered relocations don't exist on x86_64.
985 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000986 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000987 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
988 "X86_64_RELOC_SUBTRACTOR.");
989
Charles Davis8bdfafd2013-09-01 04:28:48 +0000990 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
991 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
Rafael Espindola56f976f2013-04-18 18:08:55 +0000992 printRelocationTargetName(this, RENext, fmt);
993 fmt << "-";
994 printRelocationTargetName(this, RE, fmt);
995 break;
996 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000997 case MachO::X86_64_RELOC_TLV:
Rafael Espindola56f976f2013-04-18 18:08:55 +0000998 printRelocationTargetName(this, RE, fmt);
999 fmt << "@TLV";
1000 if (isPCRel) fmt << "P";
1001 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001002 case MachO::X86_64_RELOC_SIGNED_1:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001003 printRelocationTargetName(this, RE, fmt);
1004 fmt << "-1";
1005 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001006 case MachO::X86_64_RELOC_SIGNED_2:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001007 printRelocationTargetName(this, RE, fmt);
1008 fmt << "-2";
1009 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001010 case MachO::X86_64_RELOC_SIGNED_4:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001011 printRelocationTargetName(this, RE, fmt);
1012 fmt << "-4";
1013 break;
1014 default:
1015 printRelocationTargetName(this, RE, fmt);
1016 break;
1017 }
1018 // X86 and ARM share some relocation types in common.
David Fangb88cdf62013-08-08 20:14:40 +00001019 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1020 Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001021 // Generic relocation types...
1022 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001023 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001024 return object_error::success;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001025 case MachO::GENERIC_RELOC_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001026 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001027 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001028 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001029
1030 // X86 sect diff's must be followed by a relocation of type
1031 // GENERIC_RELOC_PAIR.
1032 unsigned RType = getAnyRelocationType(RENext);
1033
Charles Davis8bdfafd2013-09-01 04:28:48 +00001034 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001035 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1036 "GENERIC_RELOC_SECTDIFF.");
1037
1038 printRelocationTargetName(this, RE, fmt);
1039 fmt << "-";
1040 printRelocationTargetName(this, RENext, fmt);
1041 break;
1042 }
1043 }
1044
David Fangb88cdf62013-08-08 20:14:40 +00001045 if (Arch == Triple::x86 || Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001046 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001047 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001048 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001049 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001050 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001051
1052 // X86 sect diff's must be followed by a relocation of type
1053 // GENERIC_RELOC_PAIR.
1054 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001055 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001056 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1057 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1058
1059 printRelocationTargetName(this, RE, fmt);
1060 fmt << "-";
1061 printRelocationTargetName(this, RENext, fmt);
1062 break;
1063 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001064 case MachO::GENERIC_RELOC_TLV: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001065 printRelocationTargetName(this, RE, fmt);
1066 fmt << "@TLV";
1067 if (IsPCRel) fmt << "P";
1068 break;
1069 }
1070 default:
1071 printRelocationTargetName(this, RE, fmt);
1072 }
1073 } else { // ARM-specific relocations
1074 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001075 case MachO::ARM_RELOC_HALF:
1076 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001077 // Half relocations steal a bit from the length field to encode
1078 // whether this is an upper16 or a lower16 relocation.
1079 bool isUpper = getAnyRelocationLength(RE) >> 1;
1080
1081 if (isUpper)
1082 fmt << ":upper16:(";
1083 else
1084 fmt << ":lower16:(";
1085 printRelocationTargetName(this, RE, fmt);
1086
1087 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001088 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001089 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001090
1091 // ARM half relocs must be followed by a relocation of type
1092 // ARM_RELOC_PAIR.
1093 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001094 if (RType != MachO::ARM_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001095 report_fatal_error("Expected ARM_RELOC_PAIR after "
Charles Davis8bdfafd2013-09-01 04:28:48 +00001096 "ARM_RELOC_HALF");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001097
1098 // NOTE: The half of the target virtual address is stashed in the
1099 // address field of the secondary relocation, but we can't reverse
1100 // engineer the constant offset from it without decoding the movw/movt
1101 // instruction to find the other half in its immediate field.
1102
1103 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1104 // symbol/section pointer of the follow-on relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001105 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001106 fmt << "-";
1107 printRelocationTargetName(this, RENext, fmt);
1108 }
1109
1110 fmt << ")";
1111 break;
1112 }
1113 default: {
1114 printRelocationTargetName(this, RE, fmt);
1115 }
1116 }
1117 }
1118 } else
1119 printRelocationTargetName(this, RE, fmt);
1120
1121 fmt.flush();
1122 Result.append(fmtbuf.begin(), fmtbuf.end());
1123 return object_error::success;
1124}
1125
1126error_code
Rafael Espindola137faa02013-04-24 15:14:22 +00001127MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001128 unsigned Arch = getArch();
1129 uint64_t Type;
1130 getRelocationType(Rel, Type);
1131
1132 Result = false;
1133
1134 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1135 // is always hidden.
David Fangb88cdf62013-08-08 20:14:40 +00001136 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001137 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001138 } else if (Arch == Triple::x86_64) {
1139 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
Eric Christopher1ff26ab62013-07-22 22:25:09 +00001140 // an X86_64_RELOC_SUBTRACTOR.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001141 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001142 DataRefImpl RelPrev = Rel;
1143 RelPrev.d.a--;
1144 uint64_t PrevType;
1145 getRelocationType(RelPrev, PrevType);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001146 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001147 Result = true;
1148 }
1149 }
1150
1151 return object_error::success;
1152}
1153
1154error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001155 LibraryRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001156 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1157}
1158
1159error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001160 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001161 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1162}
1163
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001164basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001165 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001166 if (!SymtabLoadCmd)
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001167 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola75c30362013-04-24 19:47:55 +00001168
Charles Davis8bdfafd2013-09-01 04:28:48 +00001169 MachO::symtab_command Symtab = getSymtabLoadCommand();
1170 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001171 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001172}
1173
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001174basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001175 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001176 if (!SymtabLoadCmd)
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001177 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola75c30362013-04-24 19:47:55 +00001178
Charles Davis8bdfafd2013-09-01 04:28:48 +00001179 MachO::symtab_command Symtab = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +00001180 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001181 sizeof(MachO::nlist_64) :
1182 sizeof(MachO::nlist);
1183 unsigned Offset = Symtab.symoff +
1184 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001185 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001186 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001187}
1188
Rafael Espindolab5155a52014-02-10 20:24:04 +00001189section_iterator MachOObjectFile::section_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001190 DataRefImpl DRI;
1191 return section_iterator(SectionRef(DRI, this));
1192}
1193
Rafael Espindolab5155a52014-02-10 20:24:04 +00001194section_iterator MachOObjectFile::section_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001195 DataRefImpl DRI;
1196 DRI.d.a = Sections.size();
1197 return section_iterator(SectionRef(DRI, this));
1198}
1199
Rafael Espindolab5155a52014-02-10 20:24:04 +00001200library_iterator MachOObjectFile::needed_library_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001201 // TODO: implement
1202 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1203}
1204
Rafael Espindolab5155a52014-02-10 20:24:04 +00001205library_iterator MachOObjectFile::needed_library_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001206 // TODO: implement
1207 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1208}
1209
1210uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola60689982013-04-07 19:05:30 +00001211 return is64Bit() ? 8 : 4;
Eric Christopher7b015c72011-04-22 03:19:48 +00001212}
1213
Rafael Espindola56f976f2013-04-18 18:08:55 +00001214StringRef MachOObjectFile::getFileFormatName() const {
1215 unsigned CPUType = getCPUType(this);
1216 if (!is64Bit()) {
1217 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001218 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001219 return "Mach-O 32-bit i386";
Charles Davis74ec8b02013-08-27 05:00:13 +00001220 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001221 return "Mach-O arm";
Charles Davis74ec8b02013-08-27 05:00:13 +00001222 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001223 return "Mach-O 32-bit ppc";
1224 default:
Charles Davis74ec8b02013-08-27 05:00:13 +00001225 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
Rafael Espindola56f976f2013-04-18 18:08:55 +00001226 "64-bit object file when we're not 64-bit?");
1227 return "Mach-O 32-bit unknown";
1228 }
1229 }
1230
1231 // Make sure the cpu type has the correct mask.
Charles Davis74ec8b02013-08-27 05:00:13 +00001232 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1233 == llvm::MachO::CPU_ARCH_ABI64 &&
Eric Christopher1d62c252013-07-22 22:25:07 +00001234 "32-bit object file when we're 64-bit?");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001235
1236 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001237 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001238 return "Mach-O 64-bit x86-64";
Tim Northover00ed9962014-03-29 10:18:08 +00001239 case llvm::MachO::CPU_TYPE_ARM64:
1240 return "Mach-O arm64";
Charles Davis74ec8b02013-08-27 05:00:13 +00001241 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001242 return "Mach-O 64-bit ppc64";
1243 default:
1244 return "Mach-O 64-bit unknown";
1245 }
1246}
1247
Alexey Samsonove6388e62013-06-18 15:03:28 +00001248Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1249 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001250 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001251 return Triple::x86;
Charles Davis74ec8b02013-08-27 05:00:13 +00001252 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001253 return Triple::x86_64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001254 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001255 return Triple::arm;
Tim Northover00ed9962014-03-29 10:18:08 +00001256 case llvm::MachO::CPU_TYPE_ARM64:
1257 return Triple::arm64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001258 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001259 return Triple::ppc;
Charles Davis74ec8b02013-08-27 05:00:13 +00001260 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001261 return Triple::ppc64;
1262 default:
1263 return Triple::UnknownArch;
1264 }
1265}
1266
Alexey Samsonove6388e62013-06-18 15:03:28 +00001267unsigned MachOObjectFile::getArch() const {
1268 return getArch(getCPUType(this));
1269}
1270
Rafael Espindola56f976f2013-04-18 18:08:55 +00001271StringRef MachOObjectFile::getLoadName() const {
1272 // TODO: Implement
Charles Davis1827bd82013-08-27 05:38:30 +00001273 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001274}
1275
Rui Ueyamabc654b12013-09-27 21:47:05 +00001276relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001277 DataRefImpl DRI;
1278 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001279 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001280}
1281
Rui Ueyamabc654b12013-09-27 21:47:05 +00001282relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001283 DataRefImpl DRI;
1284 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001285 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001286}
1287
Kevin Enderby273ae012013-06-06 17:20:50 +00001288dice_iterator MachOObjectFile::begin_dices() const {
1289 DataRefImpl DRI;
1290 if (!DataInCodeLoadCmd)
1291 return dice_iterator(DiceRef(DRI, this));
1292
Charles Davis8bdfafd2013-09-01 04:28:48 +00001293 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1294 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00001295 return dice_iterator(DiceRef(DRI, this));
1296}
1297
1298dice_iterator MachOObjectFile::end_dices() const {
1299 DataRefImpl DRI;
1300 if (!DataInCodeLoadCmd)
1301 return dice_iterator(DiceRef(DRI, this));
1302
Charles Davis8bdfafd2013-09-01 04:28:48 +00001303 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1304 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00001305 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1306 return dice_iterator(DiceRef(DRI, this));
1307}
1308
Rafael Espindola56f976f2013-04-18 18:08:55 +00001309StringRef
1310MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1311 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1312 return parseSegmentOrSectionName(Raw.data());
1313}
1314
1315ArrayRef<char>
1316MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001317 const section_base *Base =
1318 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1319 return ArrayRef<char>(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001320}
1321
1322ArrayRef<char>
1323MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001324 const section_base *Base =
1325 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1326 return ArrayRef<char>(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001327}
1328
1329bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00001330MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001331 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001332 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001333 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001334 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001335}
1336
Eric Christopher1d62c252013-07-22 22:25:07 +00001337unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001338 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001339 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001340 return RE.r_word1 & 0xffffff;
1341 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001342}
1343
Eric Christopher1d62c252013-07-22 22:25:07 +00001344bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001345 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001346 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001347 return (RE.r_word1 >> 27) & 1;
1348 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001349}
1350
Eric Christopher1d62c252013-07-22 22:25:07 +00001351bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001352 const MachO::any_relocation_info &RE) const {
1353 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001354}
1355
Eric Christopher1d62c252013-07-22 22:25:07 +00001356uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001357 const MachO::any_relocation_info &RE) const {
1358 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001359}
1360
Eric Christopher1d62c252013-07-22 22:25:07 +00001361unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001362 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001363 if (isRelocationScattered(RE))
1364 return getScatteredRelocationAddress(RE);
1365 return getPlainRelocationAddress(RE);
1366}
1367
Charles Davis8bdfafd2013-09-01 04:28:48 +00001368unsigned MachOObjectFile::getAnyRelocationPCRel(
1369 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001370 if (isRelocationScattered(RE))
1371 return getScatteredRelocationPCRel(this, RE);
1372 return getPlainRelocationPCRel(this, RE);
1373}
1374
Eric Christopher1d62c252013-07-22 22:25:07 +00001375unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001376 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001377 if (isRelocationScattered(RE))
1378 return getScatteredRelocationLength(RE);
1379 return getPlainRelocationLength(this, RE);
1380}
1381
1382unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00001383MachOObjectFile::getAnyRelocationType(
1384 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001385 if (isRelocationScattered(RE))
1386 return getScatteredRelocationType(RE);
1387 return getPlainRelocationType(this, RE);
1388}
1389
Rafael Espindola52501032013-04-30 15:40:54 +00001390SectionRef
Charles Davis8bdfafd2013-09-01 04:28:48 +00001391MachOObjectFile::getRelocationSection(
1392 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00001393 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
Rafael Espindolab5155a52014-02-10 20:24:04 +00001394 return *section_end();
Rafael Espindola52501032013-04-30 15:40:54 +00001395 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1396 DataRefImpl DRI;
1397 DRI.d.a = SecNum;
1398 return SectionRef(DRI, this);
1399}
1400
Rafael Espindola56f976f2013-04-18 18:08:55 +00001401MachOObjectFile::LoadCommandInfo
1402MachOObjectFile::getFirstLoadCommandInfo() const {
1403 MachOObjectFile::LoadCommandInfo Load;
1404
Charles Davis8bdfafd2013-09-01 04:28:48 +00001405 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1406 sizeof(MachO::mach_header);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001407 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001408 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001409 return Load;
1410}
1411
1412MachOObjectFile::LoadCommandInfo
1413MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1414 MachOObjectFile::LoadCommandInfo Next;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001415 Next.Ptr = L.Ptr + L.C.cmdsize;
1416 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001417 return Next;
1418}
1419
Charles Davis8bdfafd2013-09-01 04:28:48 +00001420MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1421 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001422}
1423
Charles Davis8bdfafd2013-09-01 04:28:48 +00001424MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1425 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001426}
1427
Charles Davis8bdfafd2013-09-01 04:28:48 +00001428MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00001429 unsigned Index) const {
1430 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001431 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001432}
1433
Charles Davis8bdfafd2013-09-01 04:28:48 +00001434MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1435 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001436 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001437 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001438}
1439
Charles Davis8bdfafd2013-09-01 04:28:48 +00001440MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00001441MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001442 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001443 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001444}
1445
Charles Davis8bdfafd2013-09-01 04:28:48 +00001446MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00001447MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001448 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001449 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001450}
1451
Charles Davis8bdfafd2013-09-01 04:28:48 +00001452MachO::linkedit_data_command
1453MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1454 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001455}
1456
Charles Davis8bdfafd2013-09-01 04:28:48 +00001457MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001458MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001459 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001460}
1461
Charles Davis8bdfafd2013-09-01 04:28:48 +00001462MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00001463MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001464 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001465}
1466
Charles Davis8bdfafd2013-09-01 04:28:48 +00001467MachO::linker_options_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001468MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001469 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001470}
1471
Jim Grosbach448334a2014-03-18 22:09:05 +00001472MachO::version_min_command
1473MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
1474 return getStruct<MachO::version_min_command>(this, L.Ptr);
1475}
1476
Charles Davis8bdfafd2013-09-01 04:28:48 +00001477MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001478MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +00001479 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001480 return getStruct<MachO::any_relocation_info>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001481}
1482
Charles Davis8bdfafd2013-09-01 04:28:48 +00001483MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00001484MachOObjectFile::getDice(DataRefImpl Rel) const {
1485 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001486 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00001487}
1488
Charles Davis8bdfafd2013-09-01 04:28:48 +00001489MachO::mach_header MachOObjectFile::getHeader() const {
1490 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001491}
1492
Charles Davis8bdfafd2013-09-01 04:28:48 +00001493MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1494 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001495}
1496
Charles Davis8bdfafd2013-09-01 04:28:48 +00001497uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1498 const MachO::dysymtab_command &DLC,
1499 unsigned Index) const {
1500 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1501 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001502}
1503
Charles Davis8bdfafd2013-09-01 04:28:48 +00001504MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00001505MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1506 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001507 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1508 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001509}
1510
Charles Davis8bdfafd2013-09-01 04:28:48 +00001511MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1512 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001513}
1514
Charles Davis8bdfafd2013-09-01 04:28:48 +00001515MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1516 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001517}
1518
Charles Davis8bdfafd2013-09-01 04:28:48 +00001519MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00001520MachOObjectFile::getDataInCodeLoadCommand() const {
1521 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00001522 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00001523
1524 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001525 MachO::linkedit_data_command Cmd;
1526 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1527 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1528 Cmd.dataoff = 0;
1529 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00001530 return Cmd;
1531}
1532
Rafael Espindola6e040c02013-04-26 20:07:33 +00001533StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001534 MachO::symtab_command S = getSymtabLoadCommand();
1535 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001536}
1537
Rafael Espindola56f976f2013-04-18 18:08:55 +00001538bool MachOObjectFile::is64Bit() const {
1539 return getType() == getMachOType(false, true) ||
1540 getType() == getMachOType(true, true);
1541}
1542
1543void MachOObjectFile::ReadULEB128s(uint64_t Index,
1544 SmallVectorImpl<uint64_t> &Out) const {
1545 DataExtractor extractor(ObjectFile::getData(), true, 0);
1546
1547 uint32_t offset = Index;
1548 uint64_t data = 0;
1549 while (uint64_t delta = extractor.getULEB128(&offset)) {
1550 data += delta;
1551 Out.push_back(data);
1552 }
1553}
1554
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001555ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer,
1556 bool BufferOwned) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001557 StringRef Magic = Buffer->getBuffer().slice(0, 4);
Rafael Espindola692410e2014-01-21 23:06:54 +00001558 error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001559 std::unique_ptr<MachOObjectFile> Ret;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001560 if (Magic == "\xFE\xED\xFA\xCE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001561 Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001562 else if (Magic == "\xCE\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001563 Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001564 else if (Magic == "\xFE\xED\xFA\xCF")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001565 Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001566 else if (Magic == "\xCF\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001567 Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned));
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001568 else {
1569 delete Buffer;
Rafael Espindola692410e2014-01-21 23:06:54 +00001570 return object_error::parse_failed;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001571 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001572
Rafael Espindola692410e2014-01-21 23:06:54 +00001573 if (EC)
1574 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +00001575 return Ret.release();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001576}
1577
Owen Anderson27c579d2011-10-11 17:32:27 +00001578} // end namespace object
Eric Christopher7b015c72011-04-22 03:19:48 +00001579} // end namespace llvm