blob: d0a56aa930eb76d68d62dffdf60269ee1e5919c5 [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),
Craig Topper2617dcc2014-04-15 06:32:26 +0000423 SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
424 DataInCodeLoadCmd(nullptr) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000425 uint32_t LoadCommandCount = this->getHeader().ncmds;
426 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
427 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000428
429 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000430 for (unsigned I = 0; ; ++I) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000431 if (Load.C.cmd == MachO::LC_SYMTAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000432 assert(!SymtabLoadCmd && "Multiple symbol tables");
433 SymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000434 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000435 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
436 DysymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000437 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000438 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
439 DataInCodeLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000440 } else if (Load.C.cmd == SegmentLoadType) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000441 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
442 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000443 const char *Sec = getSectionPtr(this, Load, J);
444 Sections.push_back(Sec);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000445 }
446 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000447
448 if (I == LoadCommandCount - 1)
449 break;
450 else
451 Load = getNextLoadCommandInfo(Load);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000452 }
453}
454
Rafael Espindola5e812af2014-01-30 02:49:50 +0000455void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
Rafael Espindola75c30362013-04-24 19:47:55 +0000456 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000457 sizeof(MachO::nlist_64) :
458 sizeof(MachO::nlist);
Rafael Espindola75c30362013-04-24 19:47:55 +0000459 Symb.p += SymbolTableEntrySize;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000460}
461
462error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
463 StringRef &Res) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000464 StringRef StringTable = getStringTableData();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000465 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
466 const char *Start = &StringTable.data()[Entry.n_strx];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000467 Res = StringRef(Start);
468 return object_error::success;
469}
470
471error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
472 uint64_t &Res) const {
473 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000474 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
475 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000476 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000477 MachO::nlist Entry = getSymbolTableEntry(Symb);
478 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000479 }
480 return object_error::success;
481}
482
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000483error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
484 uint32_t &Result) const {
Rafael Espindola20122a42014-01-31 20:57:12 +0000485 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000486 if (flags & SymbolRef::SF_Common) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000487 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
488 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000489 } else {
490 Result = 0;
491 }
492 return object_error::success;
493}
494
Rafael Espindola56f976f2013-04-18 18:08:55 +0000495error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
496 uint64_t &Result) const {
497 uint64_t BeginOffset;
498 uint64_t EndOffset = 0;
499 uint8_t SectionIndex;
500
Charles Davis8bdfafd2013-09-01 04:28:48 +0000501 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000502 uint64_t Value;
503 getSymbolAddress(DRI, Value);
504
505 BeginOffset = Value;
506
Charles Davis8bdfafd2013-09-01 04:28:48 +0000507 SectionIndex = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000508 if (!SectionIndex) {
Rafael Espindola20122a42014-01-31 20:57:12 +0000509 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000510 if (flags & SymbolRef::SF_Common)
511 Result = Value;
512 else
513 Result = UnknownAddressOrSize;
514 return object_error::success;
515 }
516 // Unfortunately symbols are unsorted so we need to touch all
517 // symbols from load command
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000518 for (const SymbolRef &Symbol : symbols()) {
519 DataRefImpl DRI = Symbol.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000520 Entry = getSymbolTableEntryBase(this, DRI);
521 getSymbolAddress(DRI, Value);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000522 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000523 if (!EndOffset || Value < EndOffset)
524 EndOffset = Value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000525 }
526 if (!EndOffset) {
527 uint64_t Size;
528 DataRefImpl Sec;
529 Sec.d.a = SectionIndex-1;
530 getSectionSize(Sec, Size);
531 getSectionAddress(Sec, EndOffset);
532 EndOffset += Size;
533 }
534 Result = EndOffset - BeginOffset;
535 return object_error::success;
536}
537
538error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
539 SymbolRef::Type &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000540 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
541 uint8_t n_type = Entry.n_type;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000542
543 Res = SymbolRef::ST_Other;
544
545 // If this is a STAB debugging symbol, we can do nothing more.
Charles Davis74ec8b02013-08-27 05:00:13 +0000546 if (n_type & MachO::N_STAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000547 Res = SymbolRef::ST_Debug;
548 return object_error::success;
549 }
550
Charles Davis74ec8b02013-08-27 05:00:13 +0000551 switch (n_type & MachO::N_TYPE) {
552 case MachO::N_UNDF :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000553 Res = SymbolRef::ST_Unknown;
554 break;
Charles Davis74ec8b02013-08-27 05:00:13 +0000555 case MachO::N_SECT :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000556 Res = SymbolRef::ST_Function;
557 break;
558 }
559 return object_error::success;
560}
561
Rafael Espindola20122a42014-01-31 20:57:12 +0000562uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000563 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000564
Charles Davis8bdfafd2013-09-01 04:28:48 +0000565 uint8_t MachOType = Entry.n_type;
566 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000567
Rafael Espindola20122a42014-01-31 20:57:12 +0000568 uint32_t Result = SymbolRef::SF_None;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000569
Charles Davis74ec8b02013-08-27 05:00:13 +0000570 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000571 Result |= SymbolRef::SF_Undefined;
572
Rafael Espindolaa1356322013-11-02 05:03:24 +0000573 if (MachOType & MachO::N_STAB)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000574 Result |= SymbolRef::SF_FormatSpecific;
575
Charles Davis74ec8b02013-08-27 05:00:13 +0000576 if (MachOType & MachO::N_EXT) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000577 Result |= SymbolRef::SF_Global;
Charles Davis74ec8b02013-08-27 05:00:13 +0000578 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000579 uint64_t Value;
580 getSymbolAddress(DRI, Value);
581 if (Value)
582 Result |= SymbolRef::SF_Common;
583 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000584 }
585
Charles Davis74ec8b02013-08-27 05:00:13 +0000586 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000587 Result |= SymbolRef::SF_Weak;
588
Charles Davis74ec8b02013-08-27 05:00:13 +0000589 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000590 Result |= SymbolRef::SF_Absolute;
591
Rafael Espindola20122a42014-01-31 20:57:12 +0000592 return Result;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000593}
594
595error_code
596MachOObjectFile::getSymbolSection(DataRefImpl Symb,
597 section_iterator &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000598 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
599 uint8_t index = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000600
601 if (index == 0) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000602 Res = section_end();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000603 } else {
604 DataRefImpl DRI;
605 DRI.d.a = index - 1;
606 Res = section_iterator(SectionRef(DRI, this));
607 }
608
609 return object_error::success;
610}
611
Rafael Espindola5e812af2014-01-30 02:49:50 +0000612void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000613 Sec.d.a++;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000614}
615
616error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000617MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000618 ArrayRef<char> Raw = getSectionRawName(Sec);
619 Result = parseSegmentOrSectionName(Raw.data());
620 return object_error::success;
621}
622
623error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000624MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000625 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000626 MachO::section_64 Sect = getSection64(Sec);
627 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000628 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000629 MachO::section Sect = getSection(Sec);
630 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000631 }
632 return object_error::success;
633}
634
635error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000636MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000637 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000638 MachO::section_64 Sect = getSection64(Sec);
639 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000640 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000641 MachO::section Sect = getSection(Sec);
642 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000643 }
644
645 return object_error::success;
646}
647
648error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000649MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000650 uint32_t Offset;
651 uint64_t Size;
652
653 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000654 MachO::section_64 Sect = getSection64(Sec);
655 Offset = Sect.offset;
656 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000657 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000658 MachO::section Sect = getSection(Sec);
659 Offset = Sect.offset;
660 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000661 }
662
663 Res = this->getData().substr(Offset, Size);
664 return object_error::success;
665}
666
667error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000668MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000669 uint32_t Align;
670 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000671 MachO::section_64 Sect = getSection64(Sec);
672 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000673 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000674 MachO::section Sect = getSection(Sec);
675 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000676 }
677
678 Res = uint64_t(1) << Align;
679 return object_error::success;
680}
681
682error_code
683MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
684 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000685 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000686 return object_error::success;
687}
688
Rafael Espindola137faa02013-04-24 15:14:22 +0000689error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
Michael J. Spencer800619f2011-09-28 20:57:30 +0000690 // FIXME: Unimplemented.
691 Result = false;
692 return object_error::success;
693}
694
Rafael Espindola137faa02013-04-24 15:14:22 +0000695error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000696 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000697 Result = false;
698 return object_error::success;
699}
700
Rafael Espindolac2413f52013-04-09 14:49:08 +0000701error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000702MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000703 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000704 // FIXME: Unimplemented.
705 Result = true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000706 return object_error::success;
707}
708
Rafael Espindola56f976f2013-04-18 18:08:55 +0000709error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000710 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000711 // FIXME: Unimplemented.
712 Result = false;
713 return object_error::success;
714}
715
Rafael Espindola56f976f2013-04-18 18:08:55 +0000716error_code
717MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
718 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis74ec8b02013-08-27 05:00:13 +0000719 unsigned SectionType = Flags & MachO::SECTION_TYPE;
720 Res = SectionType == MachO::S_ZEROFILL ||
721 SectionType == MachO::S_GB_ZEROFILL;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000722 return object_error::success;
723}
724
725error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000726 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000727 // Consider using the code from isSectionText to look for __const sections.
728 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
729 // to use section attributes to distinguish code from data.
730
731 // FIXME: Unimplemented.
732 Result = false;
733 return object_error::success;
734}
735
Rafael Espindola56f976f2013-04-18 18:08:55 +0000736error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000737MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000738 bool &Result) const {
739 SymbolRef::Type ST;
740 this->getSymbolType(Symb, ST);
741 if (ST == SymbolRef::ST_Unknown) {
742 Result = false;
743 return object_error::success;
744 }
745
746 uint64_t SectBegin, SectEnd;
747 getSectionAddress(Sec, SectBegin);
748 getSectionSize(Sec, SectEnd);
749 SectEnd += SectBegin;
750
751 uint64_t SymAddr;
752 getSymbolAddress(Symb, SymAddr);
753 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
754
755 return object_error::success;
756}
757
Rui Ueyamabc654b12013-09-27 21:47:05 +0000758relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000759 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +0000760 Ret.d.a = Sec.d.a;
761 Ret.d.b = 0;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000762 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000763}
Rafael Espindolac0406e12013-04-08 20:45:01 +0000764
Rafael Espindola56f976f2013-04-18 18:08:55 +0000765relocation_iterator
Rui Ueyamabc654b12013-09-27 21:47:05 +0000766MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000767 uint32_t Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000768 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000769 MachO::section_64 Sect = getSection64(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000770 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000771 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000772 MachO::section Sect = getSection(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000773 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000774 }
Eric Christopher7b015c72011-04-22 03:19:48 +0000775
Rafael Espindola56f976f2013-04-18 18:08:55 +0000776 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +0000777 Ret.d.a = Sec.d.a;
778 Ret.d.b = Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000779 return relocation_iterator(RelocationRef(Ret, this));
780}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000781
Rafael Espindola5e812af2014-01-30 02:49:50 +0000782void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +0000783 ++Rel.d.b;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000784}
Owen Anderson171f4852011-10-24 23:20:07 +0000785
Rafael Espindola56f976f2013-04-18 18:08:55 +0000786error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000787MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola72475462014-04-04 00:31:12 +0000788 uint64_t Offset;
789 getRelocationOffset(Rel, Offset);
Rafael Espindola7e91bc92014-04-03 23:54:35 +0000790
791 DataRefImpl Sec;
792 Sec.d.a = Rel.d.a;
793 uint64_t SecAddress;
794 getSectionAddress(Sec, SecAddress);
795 Res = SecAddress + Offset;
796 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000797}
798
Rafael Espindola56f976f2013-04-18 18:08:55 +0000799error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000800 uint64_t &Res) const {
Rafael Espindola72475462014-04-04 00:31:12 +0000801 assert(getHeader().filetype == MachO::MH_OBJECT &&
802 "Only implemented for MH_OBJECT");
Charles Davis8bdfafd2013-09-01 04:28:48 +0000803 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000804 Res = getAnyRelocationAddress(RE);
805 return object_error::success;
David Meyer2fc34c52012-03-01 01:36:50 +0000806}
807
Rafael Espindola806f0062013-06-05 01:33:53 +0000808symbol_iterator
809MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000810 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000811 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
812 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola806f0062013-06-05 01:33:53 +0000813 if (!isExtern)
Rafael Espindolab5155a52014-02-10 20:24:04 +0000814 return symbol_end();
Rafael Espindola75c30362013-04-24 19:47:55 +0000815
Charles Davis8bdfafd2013-09-01 04:28:48 +0000816 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +0000817 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000818 sizeof(MachO::nlist_64) :
819 sizeof(MachO::nlist);
820 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +0000821 DataRefImpl Sym;
822 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola806f0062013-06-05 01:33:53 +0000823 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +0000824}
825
826error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000827 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000828 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000829 Res = getAnyRelocationType(RE);
830 return object_error::success;
831}
832
833error_code
834MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
835 SmallVectorImpl<char> &Result) const {
836 StringRef res;
837 uint64_t RType;
838 getRelocationType(Rel, RType);
839
840 unsigned Arch = this->getArch();
841
842 switch (Arch) {
843 case Triple::x86: {
844 static const char *const Table[] = {
845 "GENERIC_RELOC_VANILLA",
846 "GENERIC_RELOC_PAIR",
847 "GENERIC_RELOC_SECTDIFF",
848 "GENERIC_RELOC_PB_LA_PTR",
849 "GENERIC_RELOC_LOCAL_SECTDIFF",
850 "GENERIC_RELOC_TLV" };
851
Eric Christopher13250cb2013-12-06 02:33:38 +0000852 if (RType > 5)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000853 res = "Unknown";
854 else
855 res = Table[RType];
856 break;
857 }
858 case Triple::x86_64: {
859 static const char *const Table[] = {
860 "X86_64_RELOC_UNSIGNED",
861 "X86_64_RELOC_SIGNED",
862 "X86_64_RELOC_BRANCH",
863 "X86_64_RELOC_GOT_LOAD",
864 "X86_64_RELOC_GOT",
865 "X86_64_RELOC_SUBTRACTOR",
866 "X86_64_RELOC_SIGNED_1",
867 "X86_64_RELOC_SIGNED_2",
868 "X86_64_RELOC_SIGNED_4",
869 "X86_64_RELOC_TLV" };
870
871 if (RType > 9)
872 res = "Unknown";
873 else
874 res = Table[RType];
875 break;
876 }
877 case Triple::arm: {
878 static const char *const Table[] = {
879 "ARM_RELOC_VANILLA",
880 "ARM_RELOC_PAIR",
881 "ARM_RELOC_SECTDIFF",
882 "ARM_RELOC_LOCAL_SECTDIFF",
883 "ARM_RELOC_PB_LA_PTR",
884 "ARM_RELOC_BR24",
885 "ARM_THUMB_RELOC_BR22",
886 "ARM_THUMB_32BIT_BRANCH",
887 "ARM_RELOC_HALF",
888 "ARM_RELOC_HALF_SECTDIFF" };
889
890 if (RType > 9)
891 res = "Unknown";
892 else
893 res = Table[RType];
894 break;
895 }
Tim Northover00ed9962014-03-29 10:18:08 +0000896 case Triple::arm64:
897 case Triple::aarch64: {
898 static const char *const Table[] = {
899 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
900 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
901 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
902 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
903 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
904 "ARM64_RELOC_ADDEND"
905 };
906
907 if (RType >= array_lengthof(Table))
908 res = "Unknown";
909 else
910 res = Table[RType];
911 break;
912 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000913 case Triple::ppc: {
914 static const char *const Table[] = {
915 "PPC_RELOC_VANILLA",
916 "PPC_RELOC_PAIR",
917 "PPC_RELOC_BR14",
918 "PPC_RELOC_BR24",
919 "PPC_RELOC_HI16",
920 "PPC_RELOC_LO16",
921 "PPC_RELOC_HA16",
922 "PPC_RELOC_LO14",
923 "PPC_RELOC_SECTDIFF",
924 "PPC_RELOC_PB_LA_PTR",
925 "PPC_RELOC_HI16_SECTDIFF",
926 "PPC_RELOC_LO16_SECTDIFF",
927 "PPC_RELOC_HA16_SECTDIFF",
928 "PPC_RELOC_JBSR",
929 "PPC_RELOC_LO14_SECTDIFF",
930 "PPC_RELOC_LOCAL_SECTDIFF" };
931
Eric Christopher13250cb2013-12-06 02:33:38 +0000932 if (RType > 15)
933 res = "Unknown";
934 else
935 res = Table[RType];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000936 break;
937 }
938 case Triple::UnknownArch:
939 res = "Unknown";
940 break;
941 }
942 Result.append(res.begin(), res.end());
943 return object_error::success;
944}
945
Rafael Espindola56f976f2013-04-18 18:08:55 +0000946error_code
947MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000948 SmallVectorImpl<char> &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000949 MachO::any_relocation_info RE = getRelocation(Rel);
David Meyer2fc34c52012-03-01 01:36:50 +0000950
Rafael Espindola56f976f2013-04-18 18:08:55 +0000951 unsigned Arch = this->getArch();
Eric Christopher7b015c72011-04-22 03:19:48 +0000952
Rafael Espindola56f976f2013-04-18 18:08:55 +0000953 std::string fmtbuf;
954 raw_string_ostream fmt(fmtbuf);
955 unsigned Type = this->getAnyRelocationType(RE);
956 bool IsPCRel = this->getAnyRelocationPCRel(RE);
957
958 // Determine any addends that should be displayed with the relocation.
959 // These require decoding the relocation type, which is triple-specific.
960
961 // X86_64 has entirely custom relocation types.
962 if (Arch == Triple::x86_64) {
963 bool isPCRel = getAnyRelocationPCRel(RE);
964
965 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000966 case MachO::X86_64_RELOC_GOT_LOAD:
967 case MachO::X86_64_RELOC_GOT: {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000968 printRelocationTargetName(this, RE, fmt);
969 fmt << "@GOT";
970 if (isPCRel) fmt << "PCREL";
971 break;
972 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000973 case MachO::X86_64_RELOC_SUBTRACTOR: {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000974 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +0000975 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000976 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000977
Charles Davis8bdfafd2013-09-01 04:28:48 +0000978 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
Rafael Espindola56f976f2013-04-18 18:08:55 +0000979 // X86_64_RELOC_UNSIGNED.
980 // NOTE: Scattered relocations don't exist on x86_64.
981 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000982 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000983 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
984 "X86_64_RELOC_SUBTRACTOR.");
985
Charles Davis8bdfafd2013-09-01 04:28:48 +0000986 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
987 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
Rafael Espindola56f976f2013-04-18 18:08:55 +0000988 printRelocationTargetName(this, RENext, fmt);
989 fmt << "-";
990 printRelocationTargetName(this, RE, fmt);
991 break;
992 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000993 case MachO::X86_64_RELOC_TLV:
Rafael Espindola56f976f2013-04-18 18:08:55 +0000994 printRelocationTargetName(this, RE, fmt);
995 fmt << "@TLV";
996 if (isPCRel) fmt << "P";
997 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000998 case MachO::X86_64_RELOC_SIGNED_1:
Rafael Espindola56f976f2013-04-18 18:08:55 +0000999 printRelocationTargetName(this, RE, fmt);
1000 fmt << "-1";
1001 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001002 case MachO::X86_64_RELOC_SIGNED_2:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001003 printRelocationTargetName(this, RE, fmt);
1004 fmt << "-2";
1005 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001006 case MachO::X86_64_RELOC_SIGNED_4:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001007 printRelocationTargetName(this, RE, fmt);
1008 fmt << "-4";
1009 break;
1010 default:
1011 printRelocationTargetName(this, RE, fmt);
1012 break;
1013 }
1014 // X86 and ARM share some relocation types in common.
David Fangb88cdf62013-08-08 20:14:40 +00001015 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1016 Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001017 // Generic relocation types...
1018 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001019 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001020 return object_error::success;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001021 case MachO::GENERIC_RELOC_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001022 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001023 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001024 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001025
1026 // X86 sect diff's must be followed by a relocation of type
1027 // GENERIC_RELOC_PAIR.
1028 unsigned RType = getAnyRelocationType(RENext);
1029
Charles Davis8bdfafd2013-09-01 04:28:48 +00001030 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001031 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1032 "GENERIC_RELOC_SECTDIFF.");
1033
1034 printRelocationTargetName(this, RE, fmt);
1035 fmt << "-";
1036 printRelocationTargetName(this, RENext, fmt);
1037 break;
1038 }
1039 }
1040
David Fangb88cdf62013-08-08 20:14:40 +00001041 if (Arch == Triple::x86 || Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001042 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001043 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001044 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001045 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001046 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001047
1048 // X86 sect diff's must be followed by a relocation of type
1049 // GENERIC_RELOC_PAIR.
1050 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001051 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001052 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1053 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1054
1055 printRelocationTargetName(this, RE, fmt);
1056 fmt << "-";
1057 printRelocationTargetName(this, RENext, fmt);
1058 break;
1059 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001060 case MachO::GENERIC_RELOC_TLV: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001061 printRelocationTargetName(this, RE, fmt);
1062 fmt << "@TLV";
1063 if (IsPCRel) fmt << "P";
1064 break;
1065 }
1066 default:
1067 printRelocationTargetName(this, RE, fmt);
1068 }
1069 } else { // ARM-specific relocations
1070 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001071 case MachO::ARM_RELOC_HALF:
1072 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001073 // Half relocations steal a bit from the length field to encode
1074 // whether this is an upper16 or a lower16 relocation.
1075 bool isUpper = getAnyRelocationLength(RE) >> 1;
1076
1077 if (isUpper)
1078 fmt << ":upper16:(";
1079 else
1080 fmt << ":lower16:(";
1081 printRelocationTargetName(this, RE, fmt);
1082
1083 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001084 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001085 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001086
1087 // ARM half relocs must be followed by a relocation of type
1088 // ARM_RELOC_PAIR.
1089 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001090 if (RType != MachO::ARM_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001091 report_fatal_error("Expected ARM_RELOC_PAIR after "
Charles Davis8bdfafd2013-09-01 04:28:48 +00001092 "ARM_RELOC_HALF");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001093
1094 // NOTE: The half of the target virtual address is stashed in the
1095 // address field of the secondary relocation, but we can't reverse
1096 // engineer the constant offset from it without decoding the movw/movt
1097 // instruction to find the other half in its immediate field.
1098
1099 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1100 // symbol/section pointer of the follow-on relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001101 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001102 fmt << "-";
1103 printRelocationTargetName(this, RENext, fmt);
1104 }
1105
1106 fmt << ")";
1107 break;
1108 }
1109 default: {
1110 printRelocationTargetName(this, RE, fmt);
1111 }
1112 }
1113 }
1114 } else
1115 printRelocationTargetName(this, RE, fmt);
1116
1117 fmt.flush();
1118 Result.append(fmtbuf.begin(), fmtbuf.end());
1119 return object_error::success;
1120}
1121
1122error_code
Rafael Espindola137faa02013-04-24 15:14:22 +00001123MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001124 unsigned Arch = getArch();
1125 uint64_t Type;
1126 getRelocationType(Rel, Type);
1127
1128 Result = false;
1129
1130 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1131 // is always hidden.
David Fangb88cdf62013-08-08 20:14:40 +00001132 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001133 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001134 } else if (Arch == Triple::x86_64) {
1135 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
Eric Christopher1ff26ab62013-07-22 22:25:09 +00001136 // an X86_64_RELOC_SUBTRACTOR.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001137 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001138 DataRefImpl RelPrev = Rel;
1139 RelPrev.d.a--;
1140 uint64_t PrevType;
1141 getRelocationType(RelPrev, PrevType);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001142 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001143 Result = true;
1144 }
1145 }
1146
1147 return object_error::success;
1148}
1149
1150error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001151 LibraryRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001152 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1153}
1154
1155error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001156 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001157 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1158}
1159
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001160basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001161 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001162 if (!SymtabLoadCmd)
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001163 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola75c30362013-04-24 19:47:55 +00001164
Charles Davis8bdfafd2013-09-01 04:28:48 +00001165 MachO::symtab_command Symtab = getSymtabLoadCommand();
1166 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001167 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001168}
1169
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001170basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001171 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001172 if (!SymtabLoadCmd)
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001173 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola75c30362013-04-24 19:47:55 +00001174
Charles Davis8bdfafd2013-09-01 04:28:48 +00001175 MachO::symtab_command Symtab = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +00001176 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001177 sizeof(MachO::nlist_64) :
1178 sizeof(MachO::nlist);
1179 unsigned Offset = Symtab.symoff +
1180 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001181 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001182 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001183}
1184
Rafael Espindolab5155a52014-02-10 20:24:04 +00001185section_iterator MachOObjectFile::section_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001186 DataRefImpl DRI;
1187 return section_iterator(SectionRef(DRI, this));
1188}
1189
Rafael Espindolab5155a52014-02-10 20:24:04 +00001190section_iterator MachOObjectFile::section_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001191 DataRefImpl DRI;
1192 DRI.d.a = Sections.size();
1193 return section_iterator(SectionRef(DRI, this));
1194}
1195
Rafael Espindolab5155a52014-02-10 20:24:04 +00001196library_iterator MachOObjectFile::needed_library_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001197 // TODO: implement
1198 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1199}
1200
Rafael Espindolab5155a52014-02-10 20:24:04 +00001201library_iterator MachOObjectFile::needed_library_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001202 // TODO: implement
1203 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1204}
1205
1206uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola60689982013-04-07 19:05:30 +00001207 return is64Bit() ? 8 : 4;
Eric Christopher7b015c72011-04-22 03:19:48 +00001208}
1209
Rafael Espindola56f976f2013-04-18 18:08:55 +00001210StringRef MachOObjectFile::getFileFormatName() const {
1211 unsigned CPUType = getCPUType(this);
1212 if (!is64Bit()) {
1213 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001214 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001215 return "Mach-O 32-bit i386";
Charles Davis74ec8b02013-08-27 05:00:13 +00001216 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001217 return "Mach-O arm";
Charles Davis74ec8b02013-08-27 05:00:13 +00001218 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001219 return "Mach-O 32-bit ppc";
1220 default:
Charles Davis74ec8b02013-08-27 05:00:13 +00001221 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
Rafael Espindola56f976f2013-04-18 18:08:55 +00001222 "64-bit object file when we're not 64-bit?");
1223 return "Mach-O 32-bit unknown";
1224 }
1225 }
1226
1227 // Make sure the cpu type has the correct mask.
Charles Davis74ec8b02013-08-27 05:00:13 +00001228 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1229 == llvm::MachO::CPU_ARCH_ABI64 &&
Eric Christopher1d62c252013-07-22 22:25:07 +00001230 "32-bit object file when we're 64-bit?");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001231
1232 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001233 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001234 return "Mach-O 64-bit x86-64";
Tim Northover00ed9962014-03-29 10:18:08 +00001235 case llvm::MachO::CPU_TYPE_ARM64:
1236 return "Mach-O arm64";
Charles Davis74ec8b02013-08-27 05:00:13 +00001237 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001238 return "Mach-O 64-bit ppc64";
1239 default:
1240 return "Mach-O 64-bit unknown";
1241 }
1242}
1243
Alexey Samsonove6388e62013-06-18 15:03:28 +00001244Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1245 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001246 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001247 return Triple::x86;
Charles Davis74ec8b02013-08-27 05:00:13 +00001248 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001249 return Triple::x86_64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001250 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001251 return Triple::arm;
Tim Northover00ed9962014-03-29 10:18:08 +00001252 case llvm::MachO::CPU_TYPE_ARM64:
1253 return Triple::arm64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001254 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001255 return Triple::ppc;
Charles Davis74ec8b02013-08-27 05:00:13 +00001256 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001257 return Triple::ppc64;
1258 default:
1259 return Triple::UnknownArch;
1260 }
1261}
1262
Alexey Samsonove6388e62013-06-18 15:03:28 +00001263unsigned MachOObjectFile::getArch() const {
1264 return getArch(getCPUType(this));
1265}
1266
Rafael Espindola56f976f2013-04-18 18:08:55 +00001267StringRef MachOObjectFile::getLoadName() const {
1268 // TODO: Implement
Charles Davis1827bd82013-08-27 05:38:30 +00001269 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001270}
1271
Rui Ueyamabc654b12013-09-27 21:47:05 +00001272relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001273 DataRefImpl DRI;
1274 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001275 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001276}
1277
Rui Ueyamabc654b12013-09-27 21:47:05 +00001278relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001279 DataRefImpl DRI;
1280 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001281 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001282}
1283
Kevin Enderby273ae012013-06-06 17:20:50 +00001284dice_iterator MachOObjectFile::begin_dices() const {
1285 DataRefImpl DRI;
1286 if (!DataInCodeLoadCmd)
1287 return dice_iterator(DiceRef(DRI, this));
1288
Charles Davis8bdfafd2013-09-01 04:28:48 +00001289 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1290 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00001291 return dice_iterator(DiceRef(DRI, this));
1292}
1293
1294dice_iterator MachOObjectFile::end_dices() const {
1295 DataRefImpl DRI;
1296 if (!DataInCodeLoadCmd)
1297 return dice_iterator(DiceRef(DRI, this));
1298
Charles Davis8bdfafd2013-09-01 04:28:48 +00001299 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1300 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00001301 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1302 return dice_iterator(DiceRef(DRI, this));
1303}
1304
Rafael Espindola56f976f2013-04-18 18:08:55 +00001305StringRef
1306MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1307 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1308 return parseSegmentOrSectionName(Raw.data());
1309}
1310
1311ArrayRef<char>
1312MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001313 const section_base *Base =
1314 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1315 return ArrayRef<char>(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001316}
1317
1318ArrayRef<char>
1319MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001320 const section_base *Base =
1321 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1322 return ArrayRef<char>(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001323}
1324
1325bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00001326MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001327 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001328 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001329 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001330 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001331}
1332
Eric Christopher1d62c252013-07-22 22:25:07 +00001333unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001334 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001335 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001336 return RE.r_word1 & 0xffffff;
1337 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001338}
1339
Eric Christopher1d62c252013-07-22 22:25:07 +00001340bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001341 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001342 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001343 return (RE.r_word1 >> 27) & 1;
1344 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001345}
1346
Eric Christopher1d62c252013-07-22 22:25:07 +00001347bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001348 const MachO::any_relocation_info &RE) const {
1349 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001350}
1351
Eric Christopher1d62c252013-07-22 22:25:07 +00001352uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001353 const MachO::any_relocation_info &RE) const {
1354 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001355}
1356
Eric Christopher1d62c252013-07-22 22:25:07 +00001357unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001358 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001359 if (isRelocationScattered(RE))
1360 return getScatteredRelocationAddress(RE);
1361 return getPlainRelocationAddress(RE);
1362}
1363
Charles Davis8bdfafd2013-09-01 04:28:48 +00001364unsigned MachOObjectFile::getAnyRelocationPCRel(
1365 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001366 if (isRelocationScattered(RE))
1367 return getScatteredRelocationPCRel(this, RE);
1368 return getPlainRelocationPCRel(this, RE);
1369}
1370
Eric Christopher1d62c252013-07-22 22:25:07 +00001371unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001372 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001373 if (isRelocationScattered(RE))
1374 return getScatteredRelocationLength(RE);
1375 return getPlainRelocationLength(this, RE);
1376}
1377
1378unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00001379MachOObjectFile::getAnyRelocationType(
1380 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001381 if (isRelocationScattered(RE))
1382 return getScatteredRelocationType(RE);
1383 return getPlainRelocationType(this, RE);
1384}
1385
Rafael Espindola52501032013-04-30 15:40:54 +00001386SectionRef
Charles Davis8bdfafd2013-09-01 04:28:48 +00001387MachOObjectFile::getRelocationSection(
1388 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00001389 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
Rafael Espindolab5155a52014-02-10 20:24:04 +00001390 return *section_end();
Rafael Espindola52501032013-04-30 15:40:54 +00001391 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1392 DataRefImpl DRI;
1393 DRI.d.a = SecNum;
1394 return SectionRef(DRI, this);
1395}
1396
Rafael Espindola56f976f2013-04-18 18:08:55 +00001397MachOObjectFile::LoadCommandInfo
1398MachOObjectFile::getFirstLoadCommandInfo() const {
1399 MachOObjectFile::LoadCommandInfo Load;
1400
Charles Davis8bdfafd2013-09-01 04:28:48 +00001401 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1402 sizeof(MachO::mach_header);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001403 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001404 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001405 return Load;
1406}
1407
1408MachOObjectFile::LoadCommandInfo
1409MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1410 MachOObjectFile::LoadCommandInfo Next;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001411 Next.Ptr = L.Ptr + L.C.cmdsize;
1412 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001413 return Next;
1414}
1415
Charles Davis8bdfafd2013-09-01 04:28:48 +00001416MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1417 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001418}
1419
Charles Davis8bdfafd2013-09-01 04:28:48 +00001420MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1421 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001422}
1423
Charles Davis8bdfafd2013-09-01 04:28:48 +00001424MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00001425 unsigned Index) const {
1426 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001427 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001428}
1429
Charles Davis8bdfafd2013-09-01 04:28:48 +00001430MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1431 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001432 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001433 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001434}
1435
Charles Davis8bdfafd2013-09-01 04:28:48 +00001436MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00001437MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001438 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001439 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001440}
1441
Charles Davis8bdfafd2013-09-01 04:28:48 +00001442MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00001443MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001444 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001445 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001446}
1447
Charles Davis8bdfafd2013-09-01 04:28:48 +00001448MachO::linkedit_data_command
1449MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1450 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001451}
1452
Charles Davis8bdfafd2013-09-01 04:28:48 +00001453MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001454MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001455 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001456}
1457
Charles Davis8bdfafd2013-09-01 04:28:48 +00001458MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00001459MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001460 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001461}
1462
Charles Davis8bdfafd2013-09-01 04:28:48 +00001463MachO::linker_options_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001464MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001465 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001466}
1467
Jim Grosbach448334a2014-03-18 22:09:05 +00001468MachO::version_min_command
1469MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
1470 return getStruct<MachO::version_min_command>(this, L.Ptr);
1471}
1472
Charles Davis8bdfafd2013-09-01 04:28:48 +00001473MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001474MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +00001475 DataRefImpl Sec;
1476 Sec.d.a = Rel.d.a;
1477 uint32_t Offset;
1478 if (is64Bit()) {
1479 MachO::section_64 Sect = getSection64(Sec);
1480 Offset = Sect.reloff;
1481 } else {
1482 MachO::section Sect = getSection(Sec);
1483 Offset = Sect.reloff;
1484 }
1485
1486 auto P = reinterpret_cast<const MachO::any_relocation_info *>(
1487 getPtr(this, Offset)) + Rel.d.b;
1488 return getStruct<MachO::any_relocation_info>(
1489 this, reinterpret_cast<const char *>(P));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001490}
1491
Charles Davis8bdfafd2013-09-01 04:28:48 +00001492MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00001493MachOObjectFile::getDice(DataRefImpl Rel) const {
1494 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001495 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00001496}
1497
Charles Davis8bdfafd2013-09-01 04:28:48 +00001498MachO::mach_header MachOObjectFile::getHeader() const {
1499 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001500}
1501
Charles Davis8bdfafd2013-09-01 04:28:48 +00001502MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1503 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001504}
1505
Charles Davis8bdfafd2013-09-01 04:28:48 +00001506uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1507 const MachO::dysymtab_command &DLC,
1508 unsigned Index) const {
1509 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1510 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001511}
1512
Charles Davis8bdfafd2013-09-01 04:28:48 +00001513MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00001514MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1515 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001516 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1517 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001518}
1519
Charles Davis8bdfafd2013-09-01 04:28:48 +00001520MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1521 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001522}
1523
Charles Davis8bdfafd2013-09-01 04:28:48 +00001524MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1525 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001526}
1527
Charles Davis8bdfafd2013-09-01 04:28:48 +00001528MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00001529MachOObjectFile::getDataInCodeLoadCommand() const {
1530 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00001531 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00001532
1533 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001534 MachO::linkedit_data_command Cmd;
1535 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1536 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1537 Cmd.dataoff = 0;
1538 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00001539 return Cmd;
1540}
1541
Rafael Espindola6e040c02013-04-26 20:07:33 +00001542StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001543 MachO::symtab_command S = getSymtabLoadCommand();
1544 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001545}
1546
Rafael Espindola56f976f2013-04-18 18:08:55 +00001547bool MachOObjectFile::is64Bit() const {
1548 return getType() == getMachOType(false, true) ||
1549 getType() == getMachOType(true, true);
1550}
1551
1552void MachOObjectFile::ReadULEB128s(uint64_t Index,
1553 SmallVectorImpl<uint64_t> &Out) const {
1554 DataExtractor extractor(ObjectFile::getData(), true, 0);
1555
1556 uint32_t offset = Index;
1557 uint64_t data = 0;
1558 while (uint64_t delta = extractor.getULEB128(&offset)) {
1559 data += delta;
1560 Out.push_back(data);
1561 }
1562}
1563
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001564ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer,
1565 bool BufferOwned) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001566 StringRef Magic = Buffer->getBuffer().slice(0, 4);
Rafael Espindola692410e2014-01-21 23:06:54 +00001567 error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001568 std::unique_ptr<MachOObjectFile> Ret;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001569 if (Magic == "\xFE\xED\xFA\xCE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001570 Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001571 else if (Magic == "\xCE\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001572 Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001573 else if (Magic == "\xFE\xED\xFA\xCF")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001574 Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001575 else if (Magic == "\xCF\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001576 Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned));
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001577 else {
1578 delete Buffer;
Rafael Espindola692410e2014-01-21 23:06:54 +00001579 return object_error::parse_failed;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001580 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001581
Rafael Espindola692410e2014-01-21 23:06:54 +00001582 if (EC)
1583 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +00001584 return Ret.release();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001585}
1586
Owen Anderson27c579d2011-10-11 17:32:27 +00001587} // end namespace object
Eric Christopher7b015c72011-04-22 03:19:48 +00001588} // end namespace llvm