blob: 9df558c6456ea67153ca81521623f4b7ab911da6 [file] [log] [blame]
Eric Christopher6256b032011-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 Andersonf7c93a32011-10-11 17:32:27 +000015#include "llvm/Object/MachO.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/Triple.h"
Rafael Espindola8764c892013-04-07 20:01:29 +000017#include "llvm/Support/DataExtractor.h"
Owen Anderson1832f4d2011-10-26 20:42:54 +000018#include "llvm/Support/Format.h"
Rafael Espindolafd7aa382013-04-18 18:08:55 +000019#include "llvm/Support/Host.h"
Eric Christopher6256b032011-04-22 03:19:48 +000020#include "llvm/Support/MemoryBuffer.h"
Jakub Staszak0856d3d2013-08-21 01:20:11 +000021#include "llvm/Support/raw_ostream.h"
Eric Christopher6256b032011-04-22 03:19:48 +000022#include <cctype>
23#include <cstring>
24#include <limits>
25
26using namespace llvm;
27using namespace object;
28
29namespace llvm {
Owen Andersonf7c93a32011-10-11 17:32:27 +000030namespace object {
Eric Christopher6256b032011-04-22 03:19:48 +000031
Charles Davis9c3dd1b2013-08-27 05:00:43 +000032struct nlist_base {
33 uint32_t n_strx;
34 uint8_t n_type;
35 uint8_t n_sect;
36 uint16_t n_desc;
Rafael Espindolafd7aa382013-04-18 18:08:55 +000037};
38
Charles Davis9c3dd1b2013-08-27 05:00:43 +000039struct section_base {
40 char sectname[16];
41 char segname[16];
Rafael Espindolafd7aa382013-04-18 18:08:55 +000042};
43
44template<typename T>
45static void SwapValue(T &Value) {
46 Value = sys::SwapByteOrder(Value);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000047}
48
Rafael Espindolafd7aa382013-04-18 18:08:55 +000049template<typename T>
50static void SwapStruct(T &Value);
51
52template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +000053void SwapStruct(MachO::any_relocation_info &H) {
54 SwapValue(H.r_word0);
55 SwapValue(H.r_word1);
Rafael Espindola3eff3182013-04-07 16:07:35 +000056}
57
Rafael Espindolafd7aa382013-04-18 18:08:55 +000058template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +000059void SwapStruct(MachO::load_command &L) {
60 SwapValue(L.cmd);
61 SwapValue(L.cmdsize);
Rafael Espindolafd7aa382013-04-18 18:08:55 +000062}
Rafael Espindola8764c892013-04-07 20:01:29 +000063
Rafael Espindolafd7aa382013-04-18 18:08:55 +000064template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +000065void SwapStruct(nlist_base &S) {
66 SwapValue(S.n_strx);
67 SwapValue(S.n_desc);
Rafael Espindolafd7aa382013-04-18 18:08:55 +000068}
69
70template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +000071void SwapStruct(MachO::section &S) {
72 SwapValue(S.addr);
73 SwapValue(S.size);
74 SwapValue(S.offset);
75 SwapValue(S.align);
76 SwapValue(S.reloff);
77 SwapValue(S.nreloc);
78 SwapValue(S.flags);
79 SwapValue(S.reserved1);
80 SwapValue(S.reserved2);
Rafael Espindolafd7aa382013-04-18 18:08:55 +000081}
82
83template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +000084void SwapStruct(MachO::section_64 &S) {
85 SwapValue(S.addr);
86 SwapValue(S.size);
87 SwapValue(S.offset);
88 SwapValue(S.align);
89 SwapValue(S.reloff);
90 SwapValue(S.nreloc);
91 SwapValue(S.flags);
92 SwapValue(S.reserved1);
93 SwapValue(S.reserved2);
94 SwapValue(S.reserved3);
Rafael Espindolafd7aa382013-04-18 18:08:55 +000095}
96
97template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +000098void SwapStruct(MachO::nlist &S) {
99 SwapValue(S.n_strx);
100 SwapValue(S.n_desc);
101 SwapValue(S.n_value);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000102}
103
104template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000105void SwapStruct(MachO::nlist_64 &S) {
106 SwapValue(S.n_strx);
107 SwapValue(S.n_desc);
108 SwapValue(S.n_value);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000109}
110
111template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000112void SwapStruct(MachO::mach_header &H) {
113 SwapValue(H.magic);
114 SwapValue(H.cputype);
115 SwapValue(H.cpusubtype);
116 SwapValue(H.filetype);
117 SwapValue(H.ncmds);
118 SwapValue(H.sizeofcmds);
119 SwapValue(H.flags);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000120}
121
122template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000123void SwapStruct(MachO::mach_header_64 &H) {
124 SwapValue(H.magic);
125 SwapValue(H.cputype);
126 SwapValue(H.cpusubtype);
127 SwapValue(H.filetype);
128 SwapValue(H.ncmds);
129 SwapValue(H.sizeofcmds);
130 SwapValue(H.flags);
131 SwapValue(H.reserved);
Rafael Espindola2173e182013-04-26 20:07:33 +0000132}
133
134template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000135void SwapStruct(MachO::symtab_command &C) {
136 SwapValue(C.cmd);
137 SwapValue(C.cmdsize);
138 SwapValue(C.symoff);
139 SwapValue(C.nsyms);
140 SwapValue(C.stroff);
141 SwapValue(C.strsize);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000142}
143
144template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000145void SwapStruct(MachO::dysymtab_command &C) {
146 SwapValue(C.cmd);
147 SwapValue(C.cmdsize);
148 SwapValue(C.ilocalsym);
149 SwapValue(C.nlocalsym);
150 SwapValue(C.iextdefsym);
151 SwapValue(C.nextdefsym);
152 SwapValue(C.iundefsym);
153 SwapValue(C.nundefsym);
154 SwapValue(C.tocoff);
155 SwapValue(C.ntoc);
156 SwapValue(C.modtaboff);
157 SwapValue(C.nmodtab);
158 SwapValue(C.extrefsymoff);
159 SwapValue(C.nextrefsyms);
160 SwapValue(C.indirectsymoff);
161 SwapValue(C.nindirectsyms);
162 SwapValue(C.extreloff);
163 SwapValue(C.nextrel);
164 SwapValue(C.locreloff);
165 SwapValue(C.nlocrel);
Rafael Espindola2173e182013-04-26 20:07:33 +0000166}
167
168template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000169void SwapStruct(MachO::linkedit_data_command &C) {
170 SwapValue(C.cmd);
171 SwapValue(C.cmdsize);
172 SwapValue(C.dataoff);
173 SwapValue(C.datasize);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000174}
175
176template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000177void SwapStruct(MachO::segment_command &C) {
178 SwapValue(C.cmd);
179 SwapValue(C.cmdsize);
180 SwapValue(C.vmaddr);
181 SwapValue(C.vmsize);
182 SwapValue(C.fileoff);
183 SwapValue(C.filesize);
184 SwapValue(C.maxprot);
185 SwapValue(C.initprot);
186 SwapValue(C.nsects);
187 SwapValue(C.flags);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000188}
189
190template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000191void SwapStruct(MachO::segment_command_64 &C) {
192 SwapValue(C.cmd);
193 SwapValue(C.cmdsize);
194 SwapValue(C.vmaddr);
195 SwapValue(C.vmsize);
196 SwapValue(C.fileoff);
197 SwapValue(C.filesize);
198 SwapValue(C.maxprot);
199 SwapValue(C.initprot);
200 SwapValue(C.nsects);
201 SwapValue(C.flags);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000202}
203
Rafael Espindola2173e182013-04-26 20:07:33 +0000204template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000205void SwapStruct(uint32_t &C) {
206 SwapValue(C);
Rafael Espindola2173e182013-04-26 20:07:33 +0000207}
208
209template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000210void SwapStruct(MachO::linker_options_command &C) {
211 SwapValue(C.cmd);
212 SwapValue(C.cmdsize);
213 SwapValue(C.count);
Rafael Espindola2173e182013-04-26 20:07:33 +0000214}
215
216template<>
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000217void SwapStruct(MachO::data_in_code_entry &C) {
218 SwapValue(C.offset);
219 SwapValue(C.length);
220 SwapValue(C.kind);
Rafael Espindola2173e182013-04-26 20:07:33 +0000221}
222
Rafael Espindola143d2232013-04-19 13:45:05 +0000223template<typename T>
224T getStruct(const MachOObjectFile *O, const char *P) {
225 T Cmd;
226 memcpy(&Cmd, P, sizeof(T));
227 if (O->isLittleEndian() != sys::IsLittleEndianHost)
228 SwapStruct(Cmd);
229 return Cmd;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000230}
231
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000232static uint32_t
233getSegmentLoadCommandNumSections(const MachOObjectFile *O,
234 const MachOObjectFile::LoadCommandInfo &L) {
235 if (O->is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000236 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
237 return S.nsects;
Rafael Espindola8764c892013-04-07 20:01:29 +0000238 }
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000239 MachO::segment_command S = O->getSegmentLoadCommand(L);
240 return S.nsects;
Rafael Espindola3eff3182013-04-07 16:07:35 +0000241}
242
Rafael Espindola2173e182013-04-26 20:07:33 +0000243static const char *
244getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
245 unsigned Sec) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000246 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
247
248 bool Is64 = O->is64Bit();
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000249 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
250 sizeof(MachO::segment_command);
251 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
252 sizeof(MachO::section);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000253
254 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000255 return reinterpret_cast<const char *>(SectionAddr);
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000256}
257
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000258static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
259 return O->getData().substr(Offset, 1).data();
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000260}
261
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000262static nlist_base
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000263getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola802fe932013-04-24 19:47:55 +0000264 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000265 return getStruct<nlist_base>(O, P);
Eric Christopher6256b032011-04-22 03:19:48 +0000266}
267
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000268static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000269 if (P[15] == 0)
270 // Null terminated.
271 return P;
272 // Not null terminated, so this is a 16 char string.
273 return StringRef(P, 16);
274}
275
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000276// Helper to advance a section or symbol iterator multiple increments at a time.
277template<class T>
278static error_code advance(T &it, size_t Val) {
279 error_code ec;
280 while (Val--) {
281 it.increment(ec);
282 }
283 return ec;
284}
285
286template<class T>
287static void advanceTo(T &it, size_t Val) {
288 if (error_code ec = advance(it, Val))
289 report_fatal_error(ec.message());
290}
291
292static unsigned getCPUType(const MachOObjectFile *O) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000293 return O->getHeader().cputype;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000294}
295
296static void printRelocationTargetName(const MachOObjectFile *O,
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000297 const MachO::any_relocation_info &RE,
Rafael Espindolafd7aa382013-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
308 error_code ec;
309 for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
310 SI != SE; SI.increment(ec)) {
311 if (ec) report_fatal_error(ec.message());
312
313 uint64_t Addr;
314 StringRef Name;
315
316 if ((ec = SI->getAddress(Addr)))
317 report_fatal_error(ec.message());
318 if (Addr != Val) continue;
319 if ((ec = SI->getName(Name)))
320 report_fatal_error(ec.message());
321 fmt << Name;
322 return;
323 }
324
325 // If we couldn't find a symbol that this relocation refers to, try
326 // to find a section beginning instead.
327 for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
328 SI != SE; SI.increment(ec)) {
329 if (ec) report_fatal_error(ec.message());
330
331 uint64_t Addr;
332 StringRef Name;
333
334 if ((ec = SI->getAddress(Addr)))
335 report_fatal_error(ec.message());
336 if (Addr != Val) continue;
337 if ((ec = SI->getName(Name)))
338 report_fatal_error(ec.message());
339 fmt << Name;
340 return;
341 }
342
343 fmt << format("0x%x", Val);
344 return;
345 }
346
347 StringRef S;
348 bool isExtern = O->getPlainRelocationExternal(RE);
Ahmed Bougachaebb9f172013-05-14 22:41:29 +0000349 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000350
351 if (isExtern) {
352 symbol_iterator SI = O->begin_symbols();
353 advanceTo(SI, Val);
354 SI->getName(S);
355 } else {
356 section_iterator SI = O->begin_sections();
Ahmed Bougachaebb9f172013-05-14 22:41:29 +0000357 // Adjust for the fact that sections are 1-indexed.
358 advanceTo(SI, Val - 1);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000359 SI->getName(S);
360 }
361
362 fmt << S;
363}
364
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000365static uint32_t
366getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
367 return RE.r_word0;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000368}
369
370static unsigned
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000371getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
372 return RE.r_word0 & 0xffffff;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000373}
374
375static bool getPlainRelocationPCRel(const MachOObjectFile *O,
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000376 const MachO::any_relocation_info &RE) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000377 if (O->isLittleEndian())
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000378 return (RE.r_word1 >> 24) & 1;
379 return (RE.r_word1 >> 7) & 1;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000380}
381
382static bool
383getScatteredRelocationPCRel(const MachOObjectFile *O,
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000384 const MachO::any_relocation_info &RE) {
385 return (RE.r_word0 >> 30) & 1;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000386}
387
388static unsigned getPlainRelocationLength(const MachOObjectFile *O,
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000389 const MachO::any_relocation_info &RE) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000390 if (O->isLittleEndian())
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000391 return (RE.r_word1 >> 25) & 3;
392 return (RE.r_word1 >> 5) & 3;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000393}
394
395static unsigned
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000396getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
397 return (RE.r_word0 >> 28) & 3;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000398}
399
400static unsigned getPlainRelocationType(const MachOObjectFile *O,
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000401 const MachO::any_relocation_info &RE) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000402 if (O->isLittleEndian())
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000403 return RE.r_word1 >> 28;
404 return RE.r_word1 & 0xf;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000405}
406
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000407static unsigned
408getScatteredRelocationType(const MachO::any_relocation_info &RE) {
409 return (RE.r_word0 >> 24) & 0xf;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000410}
411
412static uint32_t getSectionFlags(const MachOObjectFile *O,
413 DataRefImpl Sec) {
414 if (O->is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000415 MachO::section_64 Sect = O->getSection64(Sec);
416 return Sect.flags;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000417 }
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000418 MachO::section Sect = O->getSection(Sec);
419 return Sect.flags;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000420}
421
422MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
423 bool IsLittleEndian, bool Is64bits,
424 error_code &ec)
425 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
Kevin Enderby54154f32013-06-06 17:20:50 +0000426 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000427 uint32_t LoadCommandCount = this->getHeader().ncmds;
428 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
429 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000430
431 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindoladb5f9272013-04-19 11:36:47 +0000432 for (unsigned I = 0; ; ++I) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000433 if (Load.C.cmd == MachO::LC_SYMTAB) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000434 assert(!SymtabLoadCmd && "Multiple symbol tables");
435 SymtabLoadCmd = Load.Ptr;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000436 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Rafael Espindola2173e182013-04-26 20:07:33 +0000437 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
438 DysymtabLoadCmd = Load.Ptr;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000439 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby54154f32013-06-06 17:20:50 +0000440 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
441 DataInCodeLoadCmd = Load.Ptr;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000442 } else if (Load.C.cmd == SegmentLoadType) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000443 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
444 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola2173e182013-04-26 20:07:33 +0000445 const char *Sec = getSectionPtr(this, Load, J);
446 Sections.push_back(Sec);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000447 }
448 }
Rafael Espindoladb5f9272013-04-19 11:36:47 +0000449
450 if (I == LoadCommandCount - 1)
451 break;
452 else
453 Load = getNextLoadCommandInfo(Load);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000454 }
455}
456
457error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
458 SymbolRef &Res) const {
Rafael Espindola802fe932013-04-24 19:47:55 +0000459 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000460 sizeof(MachO::nlist_64) :
461 sizeof(MachO::nlist);
Rafael Espindola802fe932013-04-24 19:47:55 +0000462 Symb.p += SymbolTableEntrySize;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000463 Res = SymbolRef(Symb, this);
464 return object_error::success;
465}
466
467error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
468 StringRef &Res) const {
Rafael Espindola2173e182013-04-26 20:07:33 +0000469 StringRef StringTable = getStringTableData();
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000470 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
471 const char *Start = &StringTable.data()[Entry.n_strx];
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000472 Res = StringRef(Start);
473 return object_error::success;
474}
475
476error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
477 uint64_t &Res) const {
478 if (is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000479 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
480 Res = Entry.n_value;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000481 } else {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000482 MachO::nlist Entry = getSymbolTableEntry(Symb);
483 Res = Entry.n_value;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000484 }
485 return object_error::success;
486}
487
488error_code
489MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
490 uint64_t &Res) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000491 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000492 getSymbolAddress(Symb, Res);
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000493 if (Entry.n_sect) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000494 uint64_t Delta;
495 DataRefImpl SecRel;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000496 SecRel.d.a = Entry.n_sect-1;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000497 if (is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000498 MachO::section_64 Sec = getSection64(SecRel);
499 Delta = Sec.offset - Sec.addr;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000500 } else {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000501 MachO::section Sec = getSection(SecRel);
502 Delta = Sec.offset - Sec.addr;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000503 }
504
505 Res += Delta;
506 }
507
508 return object_error::success;
509}
510
Rafael Espindola59a0e792013-04-29 22:24:22 +0000511error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
512 uint32_t &Result) const {
513 uint32_t flags;
514 this->getSymbolFlags(DRI, flags);
515 if (flags & SymbolRef::SF_Common) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000516 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
517 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindola59a0e792013-04-29 22:24:22 +0000518 } else {
519 Result = 0;
520 }
521 return object_error::success;
522}
523
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000524error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
525 uint64_t &Result) const {
526 uint64_t BeginOffset;
527 uint64_t EndOffset = 0;
528 uint8_t SectionIndex;
529
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000530 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000531 uint64_t Value;
532 getSymbolAddress(DRI, Value);
533
534 BeginOffset = Value;
535
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000536 SectionIndex = Entry.n_sect;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000537 if (!SectionIndex) {
538 uint32_t flags = SymbolRef::SF_None;
539 this->getSymbolFlags(DRI, flags);
540 if (flags & SymbolRef::SF_Common)
541 Result = Value;
542 else
543 Result = UnknownAddressOrSize;
544 return object_error::success;
545 }
546 // Unfortunately symbols are unsorted so we need to touch all
547 // symbols from load command
Rafael Espindola802fe932013-04-24 19:47:55 +0000548 error_code ec;
549 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
550 I.increment(ec)) {
551 DataRefImpl DRI = I->getRawDataRefImpl();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000552 Entry = getSymbolTableEntryBase(this, DRI);
553 getSymbolAddress(DRI, Value);
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000554 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000555 if (!EndOffset || Value < EndOffset)
556 EndOffset = Value;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000557 }
558 if (!EndOffset) {
559 uint64_t Size;
560 DataRefImpl Sec;
561 Sec.d.a = SectionIndex-1;
562 getSectionSize(Sec, Size);
563 getSectionAddress(Sec, EndOffset);
564 EndOffset += Size;
565 }
566 Result = EndOffset - BeginOffset;
567 return object_error::success;
568}
569
570error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
571 SymbolRef::Type &Res) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000572 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
573 uint8_t n_type = Entry.n_type;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000574
575 Res = SymbolRef::ST_Other;
576
577 // If this is a STAB debugging symbol, we can do nothing more.
Charles Davisbf778d02013-08-27 05:00:13 +0000578 if (n_type & MachO::N_STAB) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000579 Res = SymbolRef::ST_Debug;
580 return object_error::success;
581 }
582
Charles Davisbf778d02013-08-27 05:00:13 +0000583 switch (n_type & MachO::N_TYPE) {
584 case MachO::N_UNDF :
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000585 Res = SymbolRef::ST_Unknown;
586 break;
Charles Davisbf778d02013-08-27 05:00:13 +0000587 case MachO::N_SECT :
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000588 Res = SymbolRef::ST_Function;
589 break;
590 }
591 return object_error::success;
592}
593
594error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
595 char &Res) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000596 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
597 uint8_t Type = Entry.n_type;
598 uint16_t Flags = Entry.n_desc;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000599
600 char Char;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000601 switch (Type & MachO::N_TYPE) {
602 case MachO::N_UNDF:
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000603 Char = 'u';
604 break;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000605 case MachO::N_ABS:
606 case MachO::N_SECT:
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000607 Char = 's';
608 break;
609 default:
610 Char = '?';
611 break;
612 }
613
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000614 if (Flags & (MachO::N_EXT | MachO::N_PEXT))
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000615 Char = toupper(static_cast<unsigned char>(Char));
616 Res = Char;
617 return object_error::success;
618}
619
620error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
621 uint32_t &Result) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000622 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000623
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000624 uint8_t MachOType = Entry.n_type;
625 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000626
627 // TODO: Correctly set SF_ThreadLocal
628 Result = SymbolRef::SF_None;
629
Charles Davisbf778d02013-08-27 05:00:13 +0000630 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000631 Result |= SymbolRef::SF_Undefined;
632
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000633 if (MachOFlags & MachO::N_STAB)
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000634 Result |= SymbolRef::SF_FormatSpecific;
635
Charles Davisbf778d02013-08-27 05:00:13 +0000636 if (MachOType & MachO::N_EXT) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000637 Result |= SymbolRef::SF_Global;
Charles Davisbf778d02013-08-27 05:00:13 +0000638 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindola59a0e792013-04-29 22:24:22 +0000639 uint64_t Value;
640 getSymbolAddress(DRI, Value);
641 if (Value)
642 Result |= SymbolRef::SF_Common;
643 }
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000644 }
645
Charles Davisbf778d02013-08-27 05:00:13 +0000646 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000647 Result |= SymbolRef::SF_Weak;
648
Charles Davisbf778d02013-08-27 05:00:13 +0000649 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000650 Result |= SymbolRef::SF_Absolute;
651
652 return object_error::success;
653}
654
655error_code
656MachOObjectFile::getSymbolSection(DataRefImpl Symb,
657 section_iterator &Res) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000658 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
659 uint8_t index = Entry.n_sect;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000660
661 if (index == 0) {
662 Res = end_sections();
663 } else {
664 DataRefImpl DRI;
665 DRI.d.a = index - 1;
666 Res = section_iterator(SectionRef(DRI, this));
667 }
668
669 return object_error::success;
670}
671
672error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000673 uint64_t &Val) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000674 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
675}
676
677error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
678 SectionRef &Res) const {
679 Sec.d.a++;
680 Res = SectionRef(Sec, this);
681 return object_error::success;
682}
683
684error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000685MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000686 ArrayRef<char> Raw = getSectionRawName(Sec);
687 Result = parseSegmentOrSectionName(Raw.data());
688 return object_error::success;
689}
690
691error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000692MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000693 if (is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000694 MachO::section_64 Sect = getSection64(Sec);
695 Res = Sect.addr;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000696 } else {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000697 MachO::section Sect = getSection(Sec);
698 Res = Sect.addr;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000699 }
700 return object_error::success;
701}
702
703error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000704MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000705 if (is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000706 MachO::section_64 Sect = getSection64(Sec);
707 Res = Sect.size;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000708 } else {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000709 MachO::section Sect = getSection(Sec);
710 Res = Sect.size;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000711 }
712
713 return object_error::success;
714}
715
716error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000717MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000718 uint32_t Offset;
719 uint64_t Size;
720
721 if (is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000722 MachO::section_64 Sect = getSection64(Sec);
723 Offset = Sect.offset;
724 Size = Sect.size;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000725 } else {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000726 MachO::section Sect = getSection(Sec);
727 Offset = Sect.offset;
728 Size = Sect.size;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000729 }
730
731 Res = this->getData().substr(Offset, Size);
732 return object_error::success;
733}
734
735error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000736MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000737 uint32_t Align;
738 if (is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000739 MachO::section_64 Sect = getSection64(Sec);
740 Align = Sect.align;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000741 } else {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000742 MachO::section Sect = getSection(Sec);
743 Align = Sect.align;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000744 }
745
746 Res = uint64_t(1) << Align;
747 return object_error::success;
748}
749
750error_code
751MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
752 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000753 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000754 return object_error::success;
755}
756
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000757error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000758 // FIXME: Unimplemented.
759 Result = false;
760 return object_error::success;
761}
762
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000763error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000764 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000765 Result = false;
766 return object_error::success;
767}
768
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000769error_code
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000770MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000771 bool &Result) const {
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000772 // FIXME: Unimplemented.
773 Result = true;
Preston Gurdc68dda82012-04-12 20:13:57 +0000774 return object_error::success;
775}
776
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000777error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000778 bool &Result) const {
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000779 // FIXME: Unimplemented.
780 Result = false;
781 return object_error::success;
782}
783
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000784error_code
785MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
786 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davisbf778d02013-08-27 05:00:13 +0000787 unsigned SectionType = Flags & MachO::SECTION_TYPE;
788 Res = SectionType == MachO::S_ZEROFILL ||
789 SectionType == MachO::S_GB_ZEROFILL;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000790 return object_error::success;
791}
792
793error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000794 bool &Result) const {
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000795 // Consider using the code from isSectionText to look for __const sections.
796 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
797 // to use section attributes to distinguish code from data.
798
799 // FIXME: Unimplemented.
800 Result = false;
801 return object_error::success;
802}
803
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000804error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000805MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000806 bool &Result) const {
807 SymbolRef::Type ST;
808 this->getSymbolType(Symb, ST);
809 if (ST == SymbolRef::ST_Unknown) {
810 Result = false;
811 return object_error::success;
812 }
813
814 uint64_t SectBegin, SectEnd;
815 getSectionAddress(Sec, SectBegin);
816 getSectionSize(Sec, SectEnd);
817 SectEnd += SectBegin;
818
819 uint64_t SymAddr;
820 getSymbolAddress(Symb, SymAddr);
821 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
822
823 return object_error::success;
824}
825
826relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +0000827 uint32_t Offset;
828 if (is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000829 MachO::section_64 Sect = getSection64(Sec);
830 Offset = Sect.reloff;
Rafael Espindolae5330f72013-04-25 12:45:46 +0000831 } else {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000832 MachO::section Sect = getSection(Sec);
833 Offset = Sect.reloff;
Rafael Espindolae5330f72013-04-25 12:45:46 +0000834 }
835
836 DataRefImpl Ret;
837 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
838 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000839}
Rafael Espindola335f1d42013-04-08 20:45:01 +0000840
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000841relocation_iterator
842MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +0000843 uint32_t Offset;
844 uint32_t Num;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000845 if (is64Bit()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000846 MachO::section_64 Sect = getSection64(Sec);
847 Offset = Sect.reloff;
848 Num = Sect.nreloc;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000849 } else {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000850 MachO::section Sect = getSection(Sec);
851 Offset = Sect.reloff;
852 Num = Sect.nreloc;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000853 }
Eric Christopher6256b032011-04-22 03:19:48 +0000854
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000855 const MachO::any_relocation_info *P =
856 reinterpret_cast<const MachO::any_relocation_info *>(getPtr(this, Offset));
Rafael Espindolae5330f72013-04-25 12:45:46 +0000857
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000858 DataRefImpl Ret;
Rafael Espindolae5330f72013-04-25 12:45:46 +0000859 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000860 return relocation_iterator(RelocationRef(Ret, this));
861}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000862
Rafael Espindolae5330f72013-04-25 12:45:46 +0000863error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
864 RelocationRef &Res) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000865 const MachO::any_relocation_info *P =
866 reinterpret_cast<const MachO::any_relocation_info *>(Rel.p);
Rafael Espindolae5330f72013-04-25 12:45:46 +0000867 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000868 Res = RelocationRef(Rel, this);
869 return object_error::success;
870}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000871
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000872error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000873MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola956ca722013-04-25 12:28:45 +0000874 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000875}
876
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000877error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000878 uint64_t &Res) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000879 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000880 Res = getAnyRelocationAddress(RE);
881 return object_error::success;
David Meyer5c2b4ea2012-03-01 01:36:50 +0000882}
883
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000884symbol_iterator
885MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000886 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000887 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
888 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000889 if (!isExtern)
890 return end_symbols();
Rafael Espindola802fe932013-04-24 19:47:55 +0000891
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000892 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola802fe932013-04-24 19:47:55 +0000893 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000894 sizeof(MachO::nlist_64) :
895 sizeof(MachO::nlist);
896 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola802fe932013-04-24 19:47:55 +0000897 DataRefImpl Sym;
898 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000899 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000900}
901
902error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000903 uint64_t &Res) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000904 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000905 Res = getAnyRelocationType(RE);
906 return object_error::success;
907}
908
909error_code
910MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
911 SmallVectorImpl<char> &Result) const {
912 StringRef res;
913 uint64_t RType;
914 getRelocationType(Rel, RType);
915
916 unsigned Arch = this->getArch();
917
918 switch (Arch) {
919 case Triple::x86: {
920 static const char *const Table[] = {
921 "GENERIC_RELOC_VANILLA",
922 "GENERIC_RELOC_PAIR",
923 "GENERIC_RELOC_SECTDIFF",
924 "GENERIC_RELOC_PB_LA_PTR",
925 "GENERIC_RELOC_LOCAL_SECTDIFF",
926 "GENERIC_RELOC_TLV" };
927
928 if (RType > 6)
929 res = "Unknown";
930 else
931 res = Table[RType];
932 break;
933 }
934 case Triple::x86_64: {
935 static const char *const Table[] = {
936 "X86_64_RELOC_UNSIGNED",
937 "X86_64_RELOC_SIGNED",
938 "X86_64_RELOC_BRANCH",
939 "X86_64_RELOC_GOT_LOAD",
940 "X86_64_RELOC_GOT",
941 "X86_64_RELOC_SUBTRACTOR",
942 "X86_64_RELOC_SIGNED_1",
943 "X86_64_RELOC_SIGNED_2",
944 "X86_64_RELOC_SIGNED_4",
945 "X86_64_RELOC_TLV" };
946
947 if (RType > 9)
948 res = "Unknown";
949 else
950 res = Table[RType];
951 break;
952 }
953 case Triple::arm: {
954 static const char *const Table[] = {
955 "ARM_RELOC_VANILLA",
956 "ARM_RELOC_PAIR",
957 "ARM_RELOC_SECTDIFF",
958 "ARM_RELOC_LOCAL_SECTDIFF",
959 "ARM_RELOC_PB_LA_PTR",
960 "ARM_RELOC_BR24",
961 "ARM_THUMB_RELOC_BR22",
962 "ARM_THUMB_32BIT_BRANCH",
963 "ARM_RELOC_HALF",
964 "ARM_RELOC_HALF_SECTDIFF" };
965
966 if (RType > 9)
967 res = "Unknown";
968 else
969 res = Table[RType];
970 break;
971 }
972 case Triple::ppc: {
973 static const char *const Table[] = {
974 "PPC_RELOC_VANILLA",
975 "PPC_RELOC_PAIR",
976 "PPC_RELOC_BR14",
977 "PPC_RELOC_BR24",
978 "PPC_RELOC_HI16",
979 "PPC_RELOC_LO16",
980 "PPC_RELOC_HA16",
981 "PPC_RELOC_LO14",
982 "PPC_RELOC_SECTDIFF",
983 "PPC_RELOC_PB_LA_PTR",
984 "PPC_RELOC_HI16_SECTDIFF",
985 "PPC_RELOC_LO16_SECTDIFF",
986 "PPC_RELOC_HA16_SECTDIFF",
987 "PPC_RELOC_JBSR",
988 "PPC_RELOC_LO14_SECTDIFF",
989 "PPC_RELOC_LOCAL_SECTDIFF" };
990
991 res = Table[RType];
992 break;
993 }
994 case Triple::UnknownArch:
995 res = "Unknown";
996 break;
997 }
998 Result.append(res.begin(), res.end());
999 return object_error::success;
1000}
1001
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001002error_code
1003MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001004 SmallVectorImpl<char> &Result) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001005 MachO::any_relocation_info RE = getRelocation(Rel);
David Meyer5c2b4ea2012-03-01 01:36:50 +00001006
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001007 unsigned Arch = this->getArch();
Eric Christopher6256b032011-04-22 03:19:48 +00001008
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001009 std::string fmtbuf;
1010 raw_string_ostream fmt(fmtbuf);
1011 unsigned Type = this->getAnyRelocationType(RE);
1012 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1013
1014 // Determine any addends that should be displayed with the relocation.
1015 // These require decoding the relocation type, which is triple-specific.
1016
1017 // X86_64 has entirely custom relocation types.
1018 if (Arch == Triple::x86_64) {
1019 bool isPCRel = getAnyRelocationPCRel(RE);
1020
1021 switch (Type) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001022 case MachO::X86_64_RELOC_GOT_LOAD:
1023 case MachO::X86_64_RELOC_GOT: {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001024 printRelocationTargetName(this, RE, fmt);
1025 fmt << "@GOT";
1026 if (isPCRel) fmt << "PCREL";
1027 break;
1028 }
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001029 case MachO::X86_64_RELOC_SUBTRACTOR: {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001030 DataRefImpl RelNext = Rel;
1031 RelNext.d.a++;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001032 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001033
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001034 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001035 // X86_64_RELOC_UNSIGNED.
1036 // NOTE: Scattered relocations don't exist on x86_64.
1037 unsigned RType = getAnyRelocationType(RENext);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001038 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001039 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1040 "X86_64_RELOC_SUBTRACTOR.");
1041
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001042 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1043 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001044 printRelocationTargetName(this, RENext, fmt);
1045 fmt << "-";
1046 printRelocationTargetName(this, RE, fmt);
1047 break;
1048 }
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001049 case MachO::X86_64_RELOC_TLV:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001050 printRelocationTargetName(this, RE, fmt);
1051 fmt << "@TLV";
1052 if (isPCRel) fmt << "P";
1053 break;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001054 case MachO::X86_64_RELOC_SIGNED_1:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001055 printRelocationTargetName(this, RE, fmt);
1056 fmt << "-1";
1057 break;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001058 case MachO::X86_64_RELOC_SIGNED_2:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001059 printRelocationTargetName(this, RE, fmt);
1060 fmt << "-2";
1061 break;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001062 case MachO::X86_64_RELOC_SIGNED_4:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001063 printRelocationTargetName(this, RE, fmt);
1064 fmt << "-4";
1065 break;
1066 default:
1067 printRelocationTargetName(this, RE, fmt);
1068 break;
1069 }
1070 // X86 and ARM share some relocation types in common.
David Fangd4f9d052013-08-08 20:14:40 +00001071 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1072 Arch == Triple::ppc) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001073 // Generic relocation types...
1074 switch (Type) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001075 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001076 return object_error::success;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001077 case MachO::GENERIC_RELOC_SECTDIFF: {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001078 DataRefImpl RelNext = Rel;
1079 RelNext.d.a++;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001080 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001081
1082 // X86 sect diff's must be followed by a relocation of type
1083 // GENERIC_RELOC_PAIR.
1084 unsigned RType = getAnyRelocationType(RENext);
1085
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001086 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001087 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1088 "GENERIC_RELOC_SECTDIFF.");
1089
1090 printRelocationTargetName(this, RE, fmt);
1091 fmt << "-";
1092 printRelocationTargetName(this, RENext, fmt);
1093 break;
1094 }
1095 }
1096
David Fangd4f9d052013-08-08 20:14:40 +00001097 if (Arch == Triple::x86 || Arch == Triple::ppc) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001098 switch (Type) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001099 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001100 DataRefImpl RelNext = Rel;
1101 RelNext.d.a++;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001102 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001103
1104 // X86 sect diff's must be followed by a relocation of type
1105 // GENERIC_RELOC_PAIR.
1106 unsigned RType = getAnyRelocationType(RENext);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001107 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001108 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1109 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1110
1111 printRelocationTargetName(this, RE, fmt);
1112 fmt << "-";
1113 printRelocationTargetName(this, RENext, fmt);
1114 break;
1115 }
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001116 case MachO::GENERIC_RELOC_TLV: {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001117 printRelocationTargetName(this, RE, fmt);
1118 fmt << "@TLV";
1119 if (IsPCRel) fmt << "P";
1120 break;
1121 }
1122 default:
1123 printRelocationTargetName(this, RE, fmt);
1124 }
1125 } else { // ARM-specific relocations
1126 switch (Type) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001127 case MachO::ARM_RELOC_HALF:
1128 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001129 // Half relocations steal a bit from the length field to encode
1130 // whether this is an upper16 or a lower16 relocation.
1131 bool isUpper = getAnyRelocationLength(RE) >> 1;
1132
1133 if (isUpper)
1134 fmt << ":upper16:(";
1135 else
1136 fmt << ":lower16:(";
1137 printRelocationTargetName(this, RE, fmt);
1138
1139 DataRefImpl RelNext = Rel;
1140 RelNext.d.a++;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001141 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001142
1143 // ARM half relocs must be followed by a relocation of type
1144 // ARM_RELOC_PAIR.
1145 unsigned RType = getAnyRelocationType(RENext);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001146 if (RType != MachO::ARM_RELOC_PAIR)
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001147 report_fatal_error("Expected ARM_RELOC_PAIR after "
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001148 "ARM_RELOC_HALF");
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001149
1150 // NOTE: The half of the target virtual address is stashed in the
1151 // address field of the secondary relocation, but we can't reverse
1152 // engineer the constant offset from it without decoding the movw/movt
1153 // instruction to find the other half in its immediate field.
1154
1155 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1156 // symbol/section pointer of the follow-on relocation.
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001157 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001158 fmt << "-";
1159 printRelocationTargetName(this, RENext, fmt);
1160 }
1161
1162 fmt << ")";
1163 break;
1164 }
1165 default: {
1166 printRelocationTargetName(this, RE, fmt);
1167 }
1168 }
1169 }
1170 } else
1171 printRelocationTargetName(this, RE, fmt);
1172
1173 fmt.flush();
1174 Result.append(fmtbuf.begin(), fmtbuf.end());
1175 return object_error::success;
1176}
1177
1178error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001179MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001180 unsigned Arch = getArch();
1181 uint64_t Type;
1182 getRelocationType(Rel, Type);
1183
1184 Result = false;
1185
1186 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1187 // is always hidden.
David Fangd4f9d052013-08-08 20:14:40 +00001188 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001189 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001190 } else if (Arch == Triple::x86_64) {
1191 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
Eric Christopherbd8f8a32013-07-22 22:25:09 +00001192 // an X86_64_RELOC_SUBTRACTOR.
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001193 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001194 DataRefImpl RelPrev = Rel;
1195 RelPrev.d.a--;
1196 uint64_t PrevType;
1197 getRelocationType(RelPrev, PrevType);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001198 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001199 Result = true;
1200 }
1201 }
1202
1203 return object_error::success;
1204}
1205
1206error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001207 LibraryRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001208 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1209}
1210
1211error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001212 StringRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001213 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1214}
1215
1216symbol_iterator MachOObjectFile::begin_symbols() const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001217 DataRefImpl DRI;
Rafael Espindola802fe932013-04-24 19:47:55 +00001218 if (!SymtabLoadCmd)
1219 return symbol_iterator(SymbolRef(DRI, this));
1220
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001221 MachO::symtab_command Symtab = getSymtabLoadCommand();
1222 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001223 return symbol_iterator(SymbolRef(DRI, this));
1224}
1225
1226symbol_iterator MachOObjectFile::end_symbols() const {
1227 DataRefImpl DRI;
Rafael Espindola802fe932013-04-24 19:47:55 +00001228 if (!SymtabLoadCmd)
1229 return symbol_iterator(SymbolRef(DRI, this));
1230
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001231 MachO::symtab_command Symtab = getSymtabLoadCommand();
Rafael Espindola802fe932013-04-24 19:47:55 +00001232 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001233 sizeof(MachO::nlist_64) :
1234 sizeof(MachO::nlist);
1235 unsigned Offset = Symtab.symoff +
1236 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola802fe932013-04-24 19:47:55 +00001237 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001238 return symbol_iterator(SymbolRef(DRI, this));
1239}
1240
1241symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1242 // TODO: implement
1243 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1244}
1245
1246symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1247 // TODO: implement
1248 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1249}
1250
1251section_iterator MachOObjectFile::begin_sections() const {
1252 DataRefImpl DRI;
1253 return section_iterator(SectionRef(DRI, this));
1254}
1255
1256section_iterator MachOObjectFile::end_sections() const {
1257 DataRefImpl DRI;
1258 DRI.d.a = Sections.size();
1259 return section_iterator(SectionRef(DRI, this));
1260}
1261
1262library_iterator MachOObjectFile::begin_libraries_needed() const {
1263 // TODO: implement
1264 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1265}
1266
1267library_iterator MachOObjectFile::end_libraries_needed() const {
1268 // TODO: implement
1269 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1270}
1271
1272uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +00001273 return is64Bit() ? 8 : 4;
Eric Christopher6256b032011-04-22 03:19:48 +00001274}
1275
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001276StringRef MachOObjectFile::getFileFormatName() const {
1277 unsigned CPUType = getCPUType(this);
1278 if (!is64Bit()) {
1279 switch (CPUType) {
Charles Davisbf778d02013-08-27 05:00:13 +00001280 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001281 return "Mach-O 32-bit i386";
Charles Davisbf778d02013-08-27 05:00:13 +00001282 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001283 return "Mach-O arm";
Charles Davisbf778d02013-08-27 05:00:13 +00001284 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001285 return "Mach-O 32-bit ppc";
1286 default:
Charles Davisbf778d02013-08-27 05:00:13 +00001287 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001288 "64-bit object file when we're not 64-bit?");
1289 return "Mach-O 32-bit unknown";
1290 }
1291 }
1292
1293 // Make sure the cpu type has the correct mask.
Charles Davisbf778d02013-08-27 05:00:13 +00001294 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1295 == llvm::MachO::CPU_ARCH_ABI64 &&
Eric Christopher60d65912013-07-22 22:25:07 +00001296 "32-bit object file when we're 64-bit?");
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001297
1298 switch (CPUType) {
Charles Davisbf778d02013-08-27 05:00:13 +00001299 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001300 return "Mach-O 64-bit x86-64";
Charles Davisbf778d02013-08-27 05:00:13 +00001301 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001302 return "Mach-O 64-bit ppc64";
1303 default:
1304 return "Mach-O 64-bit unknown";
1305 }
1306}
1307
Alexey Samsonov9c22f872013-06-18 15:03:28 +00001308Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1309 switch (CPUType) {
Charles Davisbf778d02013-08-27 05:00:13 +00001310 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001311 return Triple::x86;
Charles Davisbf778d02013-08-27 05:00:13 +00001312 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001313 return Triple::x86_64;
Charles Davisbf778d02013-08-27 05:00:13 +00001314 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001315 return Triple::arm;
Charles Davisbf778d02013-08-27 05:00:13 +00001316 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001317 return Triple::ppc;
Charles Davisbf778d02013-08-27 05:00:13 +00001318 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001319 return Triple::ppc64;
1320 default:
1321 return Triple::UnknownArch;
1322 }
1323}
1324
Alexey Samsonov9c22f872013-06-18 15:03:28 +00001325unsigned MachOObjectFile::getArch() const {
1326 return getArch(getCPUType(this));
1327}
1328
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001329StringRef MachOObjectFile::getLoadName() const {
1330 // TODO: Implement
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001331 report_fatal_error("Load name unimplemented in MachOObjectFile");
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001332}
1333
Rafael Espindola2173e182013-04-26 20:07:33 +00001334relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const {
1335 DataRefImpl DRI;
1336 DRI.d.a = Index;
1337 return getSectionRelBegin(DRI);
1338}
1339
1340relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const {
1341 DataRefImpl DRI;
1342 DRI.d.a = Index;
1343 return getSectionRelEnd(DRI);
1344}
1345
Kevin Enderby54154f32013-06-06 17:20:50 +00001346dice_iterator MachOObjectFile::begin_dices() const {
1347 DataRefImpl DRI;
1348 if (!DataInCodeLoadCmd)
1349 return dice_iterator(DiceRef(DRI, this));
1350
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001351 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1352 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby54154f32013-06-06 17:20:50 +00001353 return dice_iterator(DiceRef(DRI, this));
1354}
1355
1356dice_iterator MachOObjectFile::end_dices() const {
1357 DataRefImpl DRI;
1358 if (!DataInCodeLoadCmd)
1359 return dice_iterator(DiceRef(DRI, this));
1360
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001361 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1362 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby54154f32013-06-06 17:20:50 +00001363 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1364 return dice_iterator(DiceRef(DRI, this));
1365}
1366
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001367StringRef
1368MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1369 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1370 return parseSegmentOrSectionName(Raw.data());
1371}
1372
1373ArrayRef<char>
1374MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001375 const section_base *Base =
1376 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1377 return ArrayRef<char>(Base->sectname);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001378}
1379
1380ArrayRef<char>
1381MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001382 const section_base *Base =
1383 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1384 return ArrayRef<char>(Base->segname);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001385}
1386
1387bool
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001388MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001389 const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001390 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001391 return false;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001392 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001393}
1394
Eric Christopher60d65912013-07-22 22:25:07 +00001395unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001396 const MachO::any_relocation_info &RE) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001397 if (isLittleEndian())
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001398 return RE.r_word1 & 0xffffff;
1399 return RE.r_word1 >> 8;
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001400}
1401
Eric Christopher60d65912013-07-22 22:25:07 +00001402bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001403 const MachO::any_relocation_info &RE) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001404 if (isLittleEndian())
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001405 return (RE.r_word1 >> 27) & 1;
1406 return (RE.r_word1 >> 4) & 1;
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001407}
1408
Eric Christopher60d65912013-07-22 22:25:07 +00001409bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001410 const MachO::any_relocation_info &RE) const {
1411 return RE.r_word0 >> 31;
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001412}
1413
Eric Christopher60d65912013-07-22 22:25:07 +00001414uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001415 const MachO::any_relocation_info &RE) const {
1416 return RE.r_word1;
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001417}
1418
Eric Christopher60d65912013-07-22 22:25:07 +00001419unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001420 const MachO::any_relocation_info &RE) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001421 if (isRelocationScattered(RE))
1422 return getScatteredRelocationAddress(RE);
1423 return getPlainRelocationAddress(RE);
1424}
1425
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001426unsigned MachOObjectFile::getAnyRelocationPCRel(
1427 const MachO::any_relocation_info &RE) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001428 if (isRelocationScattered(RE))
1429 return getScatteredRelocationPCRel(this, RE);
1430 return getPlainRelocationPCRel(this, RE);
1431}
1432
Eric Christopher60d65912013-07-22 22:25:07 +00001433unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001434 const MachO::any_relocation_info &RE) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001435 if (isRelocationScattered(RE))
1436 return getScatteredRelocationLength(RE);
1437 return getPlainRelocationLength(this, RE);
1438}
1439
1440unsigned
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001441MachOObjectFile::getAnyRelocationType(
1442 const MachO::any_relocation_info &RE) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001443 if (isRelocationScattered(RE))
1444 return getScatteredRelocationType(RE);
1445 return getPlainRelocationType(this, RE);
1446}
1447
Rafael Espindolae87dadc2013-04-30 15:40:54 +00001448SectionRef
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001449MachOObjectFile::getRelocationSection(
1450 const MachO::any_relocation_info &RE) const {
Rafael Espindolae87dadc2013-04-30 15:40:54 +00001451 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1452 return *end_sections();
1453 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1454 DataRefImpl DRI;
1455 DRI.d.a = SecNum;
1456 return SectionRef(DRI, this);
1457}
1458
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001459MachOObjectFile::LoadCommandInfo
1460MachOObjectFile::getFirstLoadCommandInfo() const {
1461 MachOObjectFile::LoadCommandInfo Load;
1462
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001463 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1464 sizeof(MachO::mach_header);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001465 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001466 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001467 return Load;
1468}
1469
1470MachOObjectFile::LoadCommandInfo
1471MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1472 MachOObjectFile::LoadCommandInfo Next;
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001473 Next.Ptr = L.Ptr + L.C.cmdsize;
1474 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001475 return Next;
1476}
1477
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001478MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1479 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001480}
1481
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001482MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1483 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001484}
1485
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001486MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola2173e182013-04-26 20:07:33 +00001487 unsigned Index) const {
1488 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001489 return getStruct<MachO::section>(this, Sec);
Rafael Espindola2173e182013-04-26 20:07:33 +00001490}
1491
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001492MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1493 unsigned Index) const {
Rafael Espindola2173e182013-04-26 20:07:33 +00001494 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001495 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola2173e182013-04-26 20:07:33 +00001496}
1497
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001498MachO::nlist
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001499MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola802fe932013-04-24 19:47:55 +00001500 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001501 return getStruct<MachO::nlist>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001502}
1503
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001504MachO::nlist_64
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001505MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola802fe932013-04-24 19:47:55 +00001506 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001507 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001508}
1509
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001510MachO::linkedit_data_command
1511MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1512 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001513}
1514
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001515MachO::segment_command
Rafael Espindola2173e182013-04-26 20:07:33 +00001516MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001517 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola2173e182013-04-26 20:07:33 +00001518}
1519
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001520MachO::segment_command_64
Rafael Espindola2173e182013-04-26 20:07:33 +00001521MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001522 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola2173e182013-04-26 20:07:33 +00001523}
1524
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001525MachO::linker_options_command
Rafael Espindola2173e182013-04-26 20:07:33 +00001526MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001527 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola2173e182013-04-26 20:07:33 +00001528}
1529
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001530MachO::any_relocation_info
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001531MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +00001532 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001533 return getStruct<MachO::any_relocation_info>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001534}
1535
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001536MachO::data_in_code_entry
Kevin Enderby54154f32013-06-06 17:20:50 +00001537MachOObjectFile::getDice(DataRefImpl Rel) const {
1538 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001539 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby54154f32013-06-06 17:20:50 +00001540}
1541
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001542MachO::mach_header MachOObjectFile::getHeader() const {
1543 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001544}
1545
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001546MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1547 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola2173e182013-04-26 20:07:33 +00001548}
1549
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001550uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1551 const MachO::dysymtab_command &DLC,
1552 unsigned Index) const {
1553 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1554 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola2173e182013-04-26 20:07:33 +00001555}
1556
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001557MachO::data_in_code_entry
Rafael Espindola2173e182013-04-26 20:07:33 +00001558MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1559 unsigned Index) const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001560 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1561 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola2173e182013-04-26 20:07:33 +00001562}
1563
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001564MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1565 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001566}
1567
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001568MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1569 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola2173e182013-04-26 20:07:33 +00001570}
1571
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001572MachO::linkedit_data_command
Kevin Enderby54154f32013-06-06 17:20:50 +00001573MachOObjectFile::getDataInCodeLoadCommand() const {
1574 if (DataInCodeLoadCmd)
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001575 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby54154f32013-06-06 17:20:50 +00001576
1577 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001578 MachO::linkedit_data_command Cmd;
1579 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1580 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1581 Cmd.dataoff = 0;
1582 Cmd.datasize = 0;
Kevin Enderby54154f32013-06-06 17:20:50 +00001583 return Cmd;
1584}
1585
Rafael Espindola2173e182013-04-26 20:07:33 +00001586StringRef MachOObjectFile::getStringTableData() const {
Charles Davis9c3dd1b2013-08-27 05:00:43 +00001587 MachO::symtab_command S = getSymtabLoadCommand();
1588 return getData().substr(S.stroff, S.strsize);
Rafael Espindola2173e182013-04-26 20:07:33 +00001589}
1590
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001591bool MachOObjectFile::is64Bit() const {
1592 return getType() == getMachOType(false, true) ||
1593 getType() == getMachOType(true, true);
1594}
1595
1596void MachOObjectFile::ReadULEB128s(uint64_t Index,
1597 SmallVectorImpl<uint64_t> &Out) const {
1598 DataExtractor extractor(ObjectFile::getData(), true, 0);
1599
1600 uint32_t offset = Index;
1601 uint64_t data = 0;
1602 while (uint64_t delta = extractor.getULEB128(&offset)) {
1603 data += delta;
1604 Out.push_back(data);
1605 }
1606}
1607
1608ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1609 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1610 error_code ec;
Benjamin Kramer782fdce2013-08-03 22:16:37 +00001611 OwningPtr<ObjectFile> Ret;
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001612 if (Magic == "\xFE\xED\xFA\xCE")
Benjamin Kramer782fdce2013-08-03 22:16:37 +00001613 Ret.reset(new MachOObjectFile(Buffer, false, false, ec));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001614 else if (Magic == "\xCE\xFA\xED\xFE")
Benjamin Kramer782fdce2013-08-03 22:16:37 +00001615 Ret.reset(new MachOObjectFile(Buffer, true, false, ec));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001616 else if (Magic == "\xFE\xED\xFA\xCF")
Benjamin Kramer782fdce2013-08-03 22:16:37 +00001617 Ret.reset(new MachOObjectFile(Buffer, false, true, ec));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001618 else if (Magic == "\xCF\xFA\xED\xFE")
Benjamin Kramer782fdce2013-08-03 22:16:37 +00001619 Ret.reset(new MachOObjectFile(Buffer, true, true, ec));
1620 else {
1621 delete Buffer;
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001622 return NULL;
Benjamin Kramer782fdce2013-08-03 22:16:37 +00001623 }
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001624
1625 if (ec)
1626 return NULL;
Benjamin Kramer782fdce2013-08-03 22:16:37 +00001627 return Ret.take();
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001628}
1629
Owen Andersonf7c93a32011-10-11 17:32:27 +00001630} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001631} // end namespace llvm