blob: f48bb8a7fe0925a55e56e3f145b74e95de7b5bd8 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Triple.h"
Rafael Espindola421305a2013-04-07 20:01:29 +000017#include "llvm/Support/DataExtractor.h"
Owen Andersonbc14bd32011-10-26 20:42:54 +000018#include "llvm/Support/Format.h"
Rafael Espindola56f976f2013-04-18 18:08:55 +000019#include "llvm/Support/Host.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000020#include "llvm/Support/MemoryBuffer.h"
Jakub Staszak84a0ae72013-08-21 01:20:11 +000021#include "llvm/Support/raw_ostream.h"
Eric Christopher7b015c72011-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 Anderson27c579d2011-10-11 17:32:27 +000030namespace object {
Eric Christopher7b015c72011-04-22 03:19:48 +000031
Charles Davis8bdfafd2013-09-01 04:28:48 +000032struct nlist_base {
33 uint32_t n_strx;
34 uint8_t n_type;
35 uint8_t n_sect;
36 uint16_t n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +000037};
38
Charles Davis8bdfafd2013-09-01 04:28:48 +000039struct section_base {
40 char sectname[16];
41 char segname[16];
Rafael Espindola56f976f2013-04-18 18:08:55 +000042};
43
44template<typename T>
45static void SwapValue(T &Value) {
46 Value = sys::SwapByteOrder(Value);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +000047}
48
Rafael Espindola56f976f2013-04-18 18:08:55 +000049template<typename T>
50static void SwapStruct(T &Value);
51
52template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000053void SwapStruct(MachO::any_relocation_info &H) {
54 SwapValue(H.r_word0);
55 SwapValue(H.r_word1);
Rafael Espindola5ffc0792013-04-07 16:07:35 +000056}
57
Rafael Espindola56f976f2013-04-18 18:08:55 +000058template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000059void SwapStruct(MachO::load_command &L) {
60 SwapValue(L.cmd);
61 SwapValue(L.cmdsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +000062}
Rafael Espindola421305a2013-04-07 20:01:29 +000063
Rafael Espindola56f976f2013-04-18 18:08:55 +000064template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000065void SwapStruct(nlist_base &S) {
66 SwapValue(S.n_strx);
67 SwapValue(S.n_desc);
Rafael Espindola56f976f2013-04-18 18:08:55 +000068}
69
70template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola56f976f2013-04-18 18:08:55 +000081}
82
83template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola56f976f2013-04-18 18:08:55 +000095}
96
97template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000098void SwapStruct(MachO::nlist &S) {
99 SwapValue(S.n_strx);
100 SwapValue(S.n_desc);
101 SwapValue(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000102}
103
104template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000105void SwapStruct(MachO::nlist_64 &S) {
106 SwapValue(S.n_strx);
107 SwapValue(S.n_desc);
108 SwapValue(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000109}
110
111template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola56f976f2013-04-18 18:08:55 +0000120}
121
122template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola6e040c02013-04-26 20:07:33 +0000132}
133
134template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola56f976f2013-04-18 18:08:55 +0000142}
143
144template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola6e040c02013-04-26 20:07:33 +0000166}
167
168template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000169void SwapStruct(MachO::linkedit_data_command &C) {
170 SwapValue(C.cmd);
171 SwapValue(C.cmdsize);
172 SwapValue(C.dataoff);
173 SwapValue(C.datasize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000174}
175
176template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola56f976f2013-04-18 18:08:55 +0000188}
189
190template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola56f976f2013-04-18 18:08:55 +0000202}
203
Rafael Espindola6e040c02013-04-26 20:07:33 +0000204template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000205void SwapStruct(uint32_t &C) {
206 SwapValue(C);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000207}
208
209template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000210void SwapStruct(MachO::linker_options_command &C) {
211 SwapValue(C.cmd);
212 SwapValue(C.cmdsize);
213 SwapValue(C.count);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000214}
215
216template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000217void SwapStruct(MachO::data_in_code_entry &C) {
218 SwapValue(C.offset);
219 SwapValue(C.length);
220 SwapValue(C.kind);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000221}
222
Rafael Espindola3cdeb172013-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 Espindola56f976f2013-04-18 18:08:55 +0000230}
231
Rafael Espindola56f976f2013-04-18 18:08:55 +0000232static uint32_t
233getSegmentLoadCommandNumSections(const MachOObjectFile *O,
234 const MachOObjectFile::LoadCommandInfo &L) {
235 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000236 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
237 return S.nsects;
Rafael Espindola421305a2013-04-07 20:01:29 +0000238 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000239 MachO::segment_command S = O->getSegmentLoadCommand(L);
240 return S.nsects;
Rafael Espindola5ffc0792013-04-07 16:07:35 +0000241}
242
Rafael Espindola6e040c02013-04-26 20:07:33 +0000243static const char *
244getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
245 unsigned Sec) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000246 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
247
248 bool Is64 = O->is64Bit();
Charles Davis8bdfafd2013-09-01 04:28:48 +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 Espindola56f976f2013-04-18 18:08:55 +0000253
254 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
Charles Davis1827bd82013-08-27 05:38:30 +0000255 return reinterpret_cast<const char*>(SectionAddr);
Rafael Espindola60689982013-04-07 19:05:30 +0000256}
257
Rafael Espindola56f976f2013-04-18 18:08:55 +0000258static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
259 return O->getData().substr(Offset, 1).data();
Rafael Espindola60689982013-04-07 19:05:30 +0000260}
261
Charles Davis8bdfafd2013-09-01 04:28:48 +0000262static nlist_base
Rafael Espindola56f976f2013-04-18 18:08:55 +0000263getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola75c30362013-04-24 19:47:55 +0000264 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000265 return getStruct<nlist_base>(O, P);
Eric Christopher7b015c72011-04-22 03:19:48 +0000266}
267
Rafael Espindola56f976f2013-04-18 18:08:55 +0000268static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolaa9f810b2012-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 Espindola56f976f2013-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 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
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 Bougacha9dab0cc2013-05-14 22:41:29 +0000349 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
Rafael Espindola56f976f2013-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 Bougacha9dab0cc2013-05-14 22:41:29 +0000357 // Adjust for the fact that sections are 1-indexed.
358 advanceTo(SI, Val - 1);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000359 SI->getName(S);
360 }
361
362 fmt << S;
363}
364
Charles Davis8bdfafd2013-09-01 04:28:48 +0000365static uint32_t
366getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
367 return RE.r_word0;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000368}
369
370static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000371getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
372 return RE.r_word0 & 0xffffff;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000373}
374
375static bool getPlainRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000376 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000377 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000378 return (RE.r_word1 >> 24) & 1;
379 return (RE.r_word1 >> 7) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000380}
381
382static bool
383getScatteredRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000384 const MachO::any_relocation_info &RE) {
385 return (RE.r_word0 >> 30) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000386}
387
388static unsigned getPlainRelocationLength(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000389 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000390 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000391 return (RE.r_word1 >> 25) & 3;
392 return (RE.r_word1 >> 5) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000393}
394
395static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000396getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
397 return (RE.r_word0 >> 28) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000398}
399
400static unsigned getPlainRelocationType(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000401 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000402 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000403 return RE.r_word1 >> 28;
404 return RE.r_word1 & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000405}
406
Charles Davis8bdfafd2013-09-01 04:28:48 +0000407static unsigned
408getScatteredRelocationType(const MachO::any_relocation_info &RE) {
409 return (RE.r_word0 >> 24) & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000410}
411
412static uint32_t getSectionFlags(const MachOObjectFile *O,
413 DataRefImpl Sec) {
414 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000415 MachO::section_64 Sect = O->getSection64(Sec);
416 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000417 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000418 MachO::section Sect = O->getSection(Sec);
419 return Sect.flags;
Rafael Espindola56f976f2013-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 Enderby273ae012013-06-06 17:20:50 +0000426 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000427 uint32_t LoadCommandCount = this->getHeader().ncmds;
428 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
429 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000430
431 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000432 for (unsigned I = 0; ; ++I) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000433 if (Load.C.cmd == MachO::LC_SYMTAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000434 assert(!SymtabLoadCmd && "Multiple symbol tables");
435 SymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000436 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000437 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
438 DysymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000439 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000440 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
441 DataInCodeLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000442 } else if (Load.C.cmd == SegmentLoadType) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000443 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
444 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000445 const char *Sec = getSectionPtr(this, Load, J);
446 Sections.push_back(Sec);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000447 }
448 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000449
450 if (I == LoadCommandCount - 1)
451 break;
452 else
453 Load = getNextLoadCommandInfo(Load);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000454 }
455}
456
457error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
458 SymbolRef &Res) const {
Rafael Espindola75c30362013-04-24 19:47:55 +0000459 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000460 sizeof(MachO::nlist_64) :
461 sizeof(MachO::nlist);
Rafael Espindola75c30362013-04-24 19:47:55 +0000462 Symb.p += SymbolTableEntrySize;
Rafael Espindola56f976f2013-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 Espindola6e040c02013-04-26 20:07:33 +0000469 StringRef StringTable = getStringTableData();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000470 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
471 const char *Start = &StringTable.data()[Entry.n_strx];
Rafael Espindola56f976f2013-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 Davis8bdfafd2013-09-01 04:28:48 +0000479 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
480 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000481 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000482 MachO::nlist Entry = getSymbolTableEntry(Symb);
483 Res = Entry.n_value;
Rafael Espindola56f976f2013-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 Davis8bdfafd2013-09-01 04:28:48 +0000491 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000492 getSymbolAddress(Symb, Res);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000493 if (Entry.n_sect) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000494 uint64_t Delta;
495 DataRefImpl SecRel;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000496 SecRel.d.a = Entry.n_sect-1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000497 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000498 MachO::section_64 Sec = getSection64(SecRel);
499 Delta = Sec.offset - Sec.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000500 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000501 MachO::section Sec = getSection(SecRel);
502 Delta = Sec.offset - Sec.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000503 }
504
505 Res += Delta;
506 }
507
508 return object_error::success;
509}
510
Rafael Espindolae4dd2e02013-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 Davis8bdfafd2013-09-01 04:28:48 +0000516 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
517 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000518 } else {
519 Result = 0;
520 }
521 return object_error::success;
522}
523
Rafael Espindola56f976f2013-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 Davis8bdfafd2013-09-01 04:28:48 +0000530 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000531 uint64_t Value;
532 getSymbolAddress(DRI, Value);
533
534 BeginOffset = Value;
535
Charles Davis8bdfafd2013-09-01 04:28:48 +0000536 SectionIndex = Entry.n_sect;
Rafael Espindola56f976f2013-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 Espindola75c30362013-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 Espindola56f976f2013-04-18 18:08:55 +0000552 Entry = getSymbolTableEntryBase(this, DRI);
553 getSymbolAddress(DRI, Value);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000554 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000555 if (!EndOffset || Value < EndOffset)
556 EndOffset = Value;
Rafael Espindola56f976f2013-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 Davis8bdfafd2013-09-01 04:28:48 +0000572 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
573 uint8_t n_type = Entry.n_type;
Rafael Espindola56f976f2013-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 Davis74ec8b02013-08-27 05:00:13 +0000578 if (n_type & MachO::N_STAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000579 Res = SymbolRef::ST_Debug;
580 return object_error::success;
581 }
582
Charles Davis74ec8b02013-08-27 05:00:13 +0000583 switch (n_type & MachO::N_TYPE) {
584 case MachO::N_UNDF :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000585 Res = SymbolRef::ST_Unknown;
586 break;
Charles Davis74ec8b02013-08-27 05:00:13 +0000587 case MachO::N_SECT :
Rafael Espindola56f976f2013-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 Davis8bdfafd2013-09-01 04:28:48 +0000596 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
Rafael Espindolaa1356322013-11-02 05:03:24 +0000597 uint8_t NType = Entry.n_type;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000598
599 char Char;
Rafael Espindolaa1356322013-11-02 05:03:24 +0000600 switch (NType & MachO::N_TYPE) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000601 case MachO::N_UNDF:
Rafael Espindola56f976f2013-04-18 18:08:55 +0000602 Char = 'u';
603 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000604 case MachO::N_ABS:
Rafael Espindola56f976f2013-04-18 18:08:55 +0000605 Char = 's';
606 break;
Rafael Espindolaa1356322013-11-02 05:03:24 +0000607 case MachO::N_SECT: {
608 section_iterator Sec = end_sections();
609 getSymbolSection(Symb, Sec);
610 DataRefImpl Ref = Sec->getRawDataRefImpl();
611 StringRef SectionName;
612 getSectionName(Ref, SectionName);
613 StringRef SegmentName = getSectionFinalSegmentName(Ref);
614 if (SegmentName == "__TEXT" && SectionName == "__text")
615 Char = 't';
616 else
617 Char = 's';
618 }
619 break;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000620 default:
621 Char = '?';
622 break;
623 }
624
Rafael Espindolaa1356322013-11-02 05:03:24 +0000625 if (NType & (MachO::N_EXT | MachO::N_PEXT))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000626 Char = toupper(static_cast<unsigned char>(Char));
627 Res = Char;
628 return object_error::success;
629}
630
631error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
632 uint32_t &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000633 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000634
Charles Davis8bdfafd2013-09-01 04:28:48 +0000635 uint8_t MachOType = Entry.n_type;
636 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000637
638 // TODO: Correctly set SF_ThreadLocal
639 Result = SymbolRef::SF_None;
640
Charles Davis74ec8b02013-08-27 05:00:13 +0000641 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000642 Result |= SymbolRef::SF_Undefined;
643
Rafael Espindolaa1356322013-11-02 05:03:24 +0000644 if (MachOType & MachO::N_STAB)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000645 Result |= SymbolRef::SF_FormatSpecific;
646
Charles Davis74ec8b02013-08-27 05:00:13 +0000647 if (MachOType & MachO::N_EXT) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000648 Result |= SymbolRef::SF_Global;
Charles Davis74ec8b02013-08-27 05:00:13 +0000649 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000650 uint64_t Value;
651 getSymbolAddress(DRI, Value);
652 if (Value)
653 Result |= SymbolRef::SF_Common;
654 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000655 }
656
Charles Davis74ec8b02013-08-27 05:00:13 +0000657 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000658 Result |= SymbolRef::SF_Weak;
659
Charles Davis74ec8b02013-08-27 05:00:13 +0000660 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000661 Result |= SymbolRef::SF_Absolute;
662
663 return object_error::success;
664}
665
666error_code
667MachOObjectFile::getSymbolSection(DataRefImpl Symb,
668 section_iterator &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000669 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
670 uint8_t index = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000671
672 if (index == 0) {
673 Res = end_sections();
674 } else {
675 DataRefImpl DRI;
676 DRI.d.a = index - 1;
677 Res = section_iterator(SectionRef(DRI, this));
678 }
679
680 return object_error::success;
681}
682
683error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
Rafael Espindola137faa02013-04-24 15:14:22 +0000684 uint64_t &Val) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000685 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
686}
687
688error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
689 SectionRef &Res) const {
690 Sec.d.a++;
691 Res = SectionRef(Sec, this);
692 return object_error::success;
693}
694
695error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000696MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000697 ArrayRef<char> Raw = getSectionRawName(Sec);
698 Result = parseSegmentOrSectionName(Raw.data());
699 return object_error::success;
700}
701
702error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000703MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000704 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000705 MachO::section_64 Sect = getSection64(Sec);
706 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000707 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000708 MachO::section Sect = getSection(Sec);
709 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000710 }
711 return object_error::success;
712}
713
714error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000715MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000716 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000717 MachO::section_64 Sect = getSection64(Sec);
718 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000719 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000720 MachO::section Sect = getSection(Sec);
721 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000722 }
723
724 return object_error::success;
725}
726
727error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000728MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000729 uint32_t Offset;
730 uint64_t Size;
731
732 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000733 MachO::section_64 Sect = getSection64(Sec);
734 Offset = Sect.offset;
735 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000736 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000737 MachO::section Sect = getSection(Sec);
738 Offset = Sect.offset;
739 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000740 }
741
742 Res = this->getData().substr(Offset, Size);
743 return object_error::success;
744}
745
746error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000747MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000748 uint32_t Align;
749 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000750 MachO::section_64 Sect = getSection64(Sec);
751 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000752 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000753 MachO::section Sect = getSection(Sec);
754 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000755 }
756
757 Res = uint64_t(1) << Align;
758 return object_error::success;
759}
760
761error_code
762MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
763 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000764 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000765 return object_error::success;
766}
767
Rafael Espindola137faa02013-04-24 15:14:22 +0000768error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
Michael J. Spencer800619f2011-09-28 20:57:30 +0000769 // FIXME: Unimplemented.
770 Result = false;
771 return object_error::success;
772}
773
Rafael Espindola137faa02013-04-24 15:14:22 +0000774error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000775 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000776 Result = false;
777 return object_error::success;
778}
779
Rafael Espindolac2413f52013-04-09 14:49:08 +0000780error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000781MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000782 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000783 // FIXME: Unimplemented.
784 Result = true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000785 return object_error::success;
786}
787
Rafael Espindola56f976f2013-04-18 18:08:55 +0000788error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000789 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000790 // FIXME: Unimplemented.
791 Result = false;
792 return object_error::success;
793}
794
Rafael Espindola56f976f2013-04-18 18:08:55 +0000795error_code
796MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
797 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis74ec8b02013-08-27 05:00:13 +0000798 unsigned SectionType = Flags & MachO::SECTION_TYPE;
799 Res = SectionType == MachO::S_ZEROFILL ||
800 SectionType == MachO::S_GB_ZEROFILL;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000801 return object_error::success;
802}
803
804error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000805 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000806 // Consider using the code from isSectionText to look for __const sections.
807 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
808 // to use section attributes to distinguish code from data.
809
810 // FIXME: Unimplemented.
811 Result = false;
812 return object_error::success;
813}
814
Rafael Espindola56f976f2013-04-18 18:08:55 +0000815error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000816MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000817 bool &Result) const {
818 SymbolRef::Type ST;
819 this->getSymbolType(Symb, ST);
820 if (ST == SymbolRef::ST_Unknown) {
821 Result = false;
822 return object_error::success;
823 }
824
825 uint64_t SectBegin, SectEnd;
826 getSectionAddress(Sec, SectBegin);
827 getSectionSize(Sec, SectEnd);
828 SectEnd += SectBegin;
829
830 uint64_t SymAddr;
831 getSymbolAddress(Symb, SymAddr);
832 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
833
834 return object_error::success;
835}
836
Rui Ueyamabc654b12013-09-27 21:47:05 +0000837relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000838 uint32_t Offset;
839 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000840 MachO::section_64 Sect = getSection64(Sec);
841 Offset = Sect.reloff;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000842 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000843 MachO::section Sect = getSection(Sec);
844 Offset = Sect.reloff;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000845 }
846
847 DataRefImpl Ret;
848 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
849 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000850}
Rafael Espindolac0406e12013-04-08 20:45:01 +0000851
Rafael Espindola56f976f2013-04-18 18:08:55 +0000852relocation_iterator
Rui Ueyamabc654b12013-09-27 21:47:05 +0000853MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000854 uint32_t Offset;
855 uint32_t Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000856 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000857 MachO::section_64 Sect = getSection64(Sec);
858 Offset = Sect.reloff;
859 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000860 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000861 MachO::section Sect = getSection(Sec);
862 Offset = Sect.reloff;
863 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000864 }
Eric Christopher7b015c72011-04-22 03:19:48 +0000865
Charles Davis8bdfafd2013-09-01 04:28:48 +0000866 const MachO::any_relocation_info *P =
867 reinterpret_cast<const MachO::any_relocation_info *>(getPtr(this, Offset));
Rafael Espindola04d3f492013-04-25 12:45:46 +0000868
Rafael Espindola56f976f2013-04-18 18:08:55 +0000869 DataRefImpl Ret;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000870 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000871 return relocation_iterator(RelocationRef(Ret, this));
872}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000873
Rafael Espindola04d3f492013-04-25 12:45:46 +0000874error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
875 RelocationRef &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000876 const MachO::any_relocation_info *P =
877 reinterpret_cast<const MachO::any_relocation_info *>(Rel.p);
Rafael Espindola04d3f492013-04-25 12:45:46 +0000878 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000879 Res = RelocationRef(Rel, this);
880 return object_error::success;
881}
Owen Anderson171f4852011-10-24 23:20:07 +0000882
Rafael Espindola56f976f2013-04-18 18:08:55 +0000883error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000884MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000885 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000886}
887
Rafael Espindola56f976f2013-04-18 18:08:55 +0000888error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000889 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000890 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000891 Res = getAnyRelocationAddress(RE);
892 return object_error::success;
David Meyer2fc34c52012-03-01 01:36:50 +0000893}
894
Rafael Espindola806f0062013-06-05 01:33:53 +0000895symbol_iterator
896MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000897 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000898 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
899 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola806f0062013-06-05 01:33:53 +0000900 if (!isExtern)
901 return end_symbols();
Rafael Espindola75c30362013-04-24 19:47:55 +0000902
Charles Davis8bdfafd2013-09-01 04:28:48 +0000903 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +0000904 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000905 sizeof(MachO::nlist_64) :
906 sizeof(MachO::nlist);
907 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +0000908 DataRefImpl Sym;
909 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola806f0062013-06-05 01:33:53 +0000910 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +0000911}
912
913error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000914 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000915 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000916 Res = getAnyRelocationType(RE);
917 return object_error::success;
918}
919
920error_code
921MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
922 SmallVectorImpl<char> &Result) const {
923 StringRef res;
924 uint64_t RType;
925 getRelocationType(Rel, RType);
926
927 unsigned Arch = this->getArch();
928
929 switch (Arch) {
930 case Triple::x86: {
931 static const char *const Table[] = {
932 "GENERIC_RELOC_VANILLA",
933 "GENERIC_RELOC_PAIR",
934 "GENERIC_RELOC_SECTDIFF",
935 "GENERIC_RELOC_PB_LA_PTR",
936 "GENERIC_RELOC_LOCAL_SECTDIFF",
937 "GENERIC_RELOC_TLV" };
938
939 if (RType > 6)
940 res = "Unknown";
941 else
942 res = Table[RType];
943 break;
944 }
945 case Triple::x86_64: {
946 static const char *const Table[] = {
947 "X86_64_RELOC_UNSIGNED",
948 "X86_64_RELOC_SIGNED",
949 "X86_64_RELOC_BRANCH",
950 "X86_64_RELOC_GOT_LOAD",
951 "X86_64_RELOC_GOT",
952 "X86_64_RELOC_SUBTRACTOR",
953 "X86_64_RELOC_SIGNED_1",
954 "X86_64_RELOC_SIGNED_2",
955 "X86_64_RELOC_SIGNED_4",
956 "X86_64_RELOC_TLV" };
957
958 if (RType > 9)
959 res = "Unknown";
960 else
961 res = Table[RType];
962 break;
963 }
964 case Triple::arm: {
965 static const char *const Table[] = {
966 "ARM_RELOC_VANILLA",
967 "ARM_RELOC_PAIR",
968 "ARM_RELOC_SECTDIFF",
969 "ARM_RELOC_LOCAL_SECTDIFF",
970 "ARM_RELOC_PB_LA_PTR",
971 "ARM_RELOC_BR24",
972 "ARM_THUMB_RELOC_BR22",
973 "ARM_THUMB_32BIT_BRANCH",
974 "ARM_RELOC_HALF",
975 "ARM_RELOC_HALF_SECTDIFF" };
976
977 if (RType > 9)
978 res = "Unknown";
979 else
980 res = Table[RType];
981 break;
982 }
983 case Triple::ppc: {
984 static const char *const Table[] = {
985 "PPC_RELOC_VANILLA",
986 "PPC_RELOC_PAIR",
987 "PPC_RELOC_BR14",
988 "PPC_RELOC_BR24",
989 "PPC_RELOC_HI16",
990 "PPC_RELOC_LO16",
991 "PPC_RELOC_HA16",
992 "PPC_RELOC_LO14",
993 "PPC_RELOC_SECTDIFF",
994 "PPC_RELOC_PB_LA_PTR",
995 "PPC_RELOC_HI16_SECTDIFF",
996 "PPC_RELOC_LO16_SECTDIFF",
997 "PPC_RELOC_HA16_SECTDIFF",
998 "PPC_RELOC_JBSR",
999 "PPC_RELOC_LO14_SECTDIFF",
1000 "PPC_RELOC_LOCAL_SECTDIFF" };
1001
1002 res = Table[RType];
1003 break;
1004 }
1005 case Triple::UnknownArch:
1006 res = "Unknown";
1007 break;
1008 }
1009 Result.append(res.begin(), res.end());
1010 return object_error::success;
1011}
1012
Rafael Espindola56f976f2013-04-18 18:08:55 +00001013error_code
1014MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +00001015 SmallVectorImpl<char> &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001016 MachO::any_relocation_info RE = getRelocation(Rel);
David Meyer2fc34c52012-03-01 01:36:50 +00001017
Rafael Espindola56f976f2013-04-18 18:08:55 +00001018 unsigned Arch = this->getArch();
Eric Christopher7b015c72011-04-22 03:19:48 +00001019
Rafael Espindola56f976f2013-04-18 18:08:55 +00001020 std::string fmtbuf;
1021 raw_string_ostream fmt(fmtbuf);
1022 unsigned Type = this->getAnyRelocationType(RE);
1023 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1024
1025 // Determine any addends that should be displayed with the relocation.
1026 // These require decoding the relocation type, which is triple-specific.
1027
1028 // X86_64 has entirely custom relocation types.
1029 if (Arch == Triple::x86_64) {
1030 bool isPCRel = getAnyRelocationPCRel(RE);
1031
1032 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001033 case MachO::X86_64_RELOC_GOT_LOAD:
1034 case MachO::X86_64_RELOC_GOT: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001035 printRelocationTargetName(this, RE, fmt);
1036 fmt << "@GOT";
1037 if (isPCRel) fmt << "PCREL";
1038 break;
1039 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001040 case MachO::X86_64_RELOC_SUBTRACTOR: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001041 DataRefImpl RelNext = Rel;
1042 RelNext.d.a++;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001043 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001044
Charles Davis8bdfafd2013-09-01 04:28:48 +00001045 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
Rafael Espindola56f976f2013-04-18 18:08:55 +00001046 // X86_64_RELOC_UNSIGNED.
1047 // NOTE: Scattered relocations don't exist on x86_64.
1048 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001049 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001050 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1051 "X86_64_RELOC_SUBTRACTOR.");
1052
Charles Davis8bdfafd2013-09-01 04:28:48 +00001053 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1054 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
Rafael Espindola56f976f2013-04-18 18:08:55 +00001055 printRelocationTargetName(this, RENext, fmt);
1056 fmt << "-";
1057 printRelocationTargetName(this, RE, fmt);
1058 break;
1059 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001060 case MachO::X86_64_RELOC_TLV:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001061 printRelocationTargetName(this, RE, fmt);
1062 fmt << "@TLV";
1063 if (isPCRel) fmt << "P";
1064 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001065 case MachO::X86_64_RELOC_SIGNED_1:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001066 printRelocationTargetName(this, RE, fmt);
1067 fmt << "-1";
1068 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001069 case MachO::X86_64_RELOC_SIGNED_2:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001070 printRelocationTargetName(this, RE, fmt);
1071 fmt << "-2";
1072 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001073 case MachO::X86_64_RELOC_SIGNED_4:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001074 printRelocationTargetName(this, RE, fmt);
1075 fmt << "-4";
1076 break;
1077 default:
1078 printRelocationTargetName(this, RE, fmt);
1079 break;
1080 }
1081 // X86 and ARM share some relocation types in common.
David Fangb88cdf62013-08-08 20:14:40 +00001082 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1083 Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001084 // Generic relocation types...
1085 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001086 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001087 return object_error::success;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001088 case MachO::GENERIC_RELOC_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001089 DataRefImpl RelNext = Rel;
1090 RelNext.d.a++;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001091 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001092
1093 // X86 sect diff's must be followed by a relocation of type
1094 // GENERIC_RELOC_PAIR.
1095 unsigned RType = getAnyRelocationType(RENext);
1096
Charles Davis8bdfafd2013-09-01 04:28:48 +00001097 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001098 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1099 "GENERIC_RELOC_SECTDIFF.");
1100
1101 printRelocationTargetName(this, RE, fmt);
1102 fmt << "-";
1103 printRelocationTargetName(this, RENext, fmt);
1104 break;
1105 }
1106 }
1107
David Fangb88cdf62013-08-08 20:14:40 +00001108 if (Arch == Triple::x86 || Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001109 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001110 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001111 DataRefImpl RelNext = Rel;
1112 RelNext.d.a++;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001113 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001114
1115 // X86 sect diff's must be followed by a relocation of type
1116 // GENERIC_RELOC_PAIR.
1117 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001118 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001119 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1120 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1121
1122 printRelocationTargetName(this, RE, fmt);
1123 fmt << "-";
1124 printRelocationTargetName(this, RENext, fmt);
1125 break;
1126 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001127 case MachO::GENERIC_RELOC_TLV: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001128 printRelocationTargetName(this, RE, fmt);
1129 fmt << "@TLV";
1130 if (IsPCRel) fmt << "P";
1131 break;
1132 }
1133 default:
1134 printRelocationTargetName(this, RE, fmt);
1135 }
1136 } else { // ARM-specific relocations
1137 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001138 case MachO::ARM_RELOC_HALF:
1139 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001140 // Half relocations steal a bit from the length field to encode
1141 // whether this is an upper16 or a lower16 relocation.
1142 bool isUpper = getAnyRelocationLength(RE) >> 1;
1143
1144 if (isUpper)
1145 fmt << ":upper16:(";
1146 else
1147 fmt << ":lower16:(";
1148 printRelocationTargetName(this, RE, fmt);
1149
1150 DataRefImpl RelNext = Rel;
1151 RelNext.d.a++;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001152 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001153
1154 // ARM half relocs must be followed by a relocation of type
1155 // ARM_RELOC_PAIR.
1156 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001157 if (RType != MachO::ARM_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001158 report_fatal_error("Expected ARM_RELOC_PAIR after "
Charles Davis8bdfafd2013-09-01 04:28:48 +00001159 "ARM_RELOC_HALF");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001160
1161 // NOTE: The half of the target virtual address is stashed in the
1162 // address field of the secondary relocation, but we can't reverse
1163 // engineer the constant offset from it without decoding the movw/movt
1164 // instruction to find the other half in its immediate field.
1165
1166 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1167 // symbol/section pointer of the follow-on relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001168 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001169 fmt << "-";
1170 printRelocationTargetName(this, RENext, fmt);
1171 }
1172
1173 fmt << ")";
1174 break;
1175 }
1176 default: {
1177 printRelocationTargetName(this, RE, fmt);
1178 }
1179 }
1180 }
1181 } else
1182 printRelocationTargetName(this, RE, fmt);
1183
1184 fmt.flush();
1185 Result.append(fmtbuf.begin(), fmtbuf.end());
1186 return object_error::success;
1187}
1188
1189error_code
Rafael Espindola137faa02013-04-24 15:14:22 +00001190MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001191 unsigned Arch = getArch();
1192 uint64_t Type;
1193 getRelocationType(Rel, Type);
1194
1195 Result = false;
1196
1197 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1198 // is always hidden.
David Fangb88cdf62013-08-08 20:14:40 +00001199 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001200 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001201 } else if (Arch == Triple::x86_64) {
1202 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
Eric Christopher1ff26ab62013-07-22 22:25:09 +00001203 // an X86_64_RELOC_SUBTRACTOR.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001204 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001205 DataRefImpl RelPrev = Rel;
1206 RelPrev.d.a--;
1207 uint64_t PrevType;
1208 getRelocationType(RelPrev, PrevType);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001209 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001210 Result = true;
1211 }
1212 }
1213
1214 return object_error::success;
1215}
1216
1217error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001218 LibraryRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001219 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1220}
1221
1222error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001223 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001224 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1225}
1226
1227symbol_iterator MachOObjectFile::begin_symbols() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001228 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001229 if (!SymtabLoadCmd)
1230 return symbol_iterator(SymbolRef(DRI, this));
1231
Charles Davis8bdfafd2013-09-01 04:28:48 +00001232 MachO::symtab_command Symtab = getSymtabLoadCommand();
1233 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001234 return symbol_iterator(SymbolRef(DRI, this));
1235}
1236
1237symbol_iterator MachOObjectFile::end_symbols() const {
1238 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001239 if (!SymtabLoadCmd)
1240 return symbol_iterator(SymbolRef(DRI, this));
1241
Charles Davis8bdfafd2013-09-01 04:28:48 +00001242 MachO::symtab_command Symtab = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +00001243 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001244 sizeof(MachO::nlist_64) :
1245 sizeof(MachO::nlist);
1246 unsigned Offset = Symtab.symoff +
1247 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001248 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001249 return symbol_iterator(SymbolRef(DRI, this));
1250}
1251
1252symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1253 // TODO: implement
1254 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1255}
1256
1257symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1258 // TODO: implement
1259 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1260}
1261
1262section_iterator MachOObjectFile::begin_sections() const {
1263 DataRefImpl DRI;
1264 return section_iterator(SectionRef(DRI, this));
1265}
1266
1267section_iterator MachOObjectFile::end_sections() const {
1268 DataRefImpl DRI;
1269 DRI.d.a = Sections.size();
1270 return section_iterator(SectionRef(DRI, this));
1271}
1272
1273library_iterator MachOObjectFile::begin_libraries_needed() const {
1274 // TODO: implement
1275 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1276}
1277
1278library_iterator MachOObjectFile::end_libraries_needed() const {
1279 // TODO: implement
1280 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1281}
1282
1283uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola60689982013-04-07 19:05:30 +00001284 return is64Bit() ? 8 : 4;
Eric Christopher7b015c72011-04-22 03:19:48 +00001285}
1286
Rafael Espindola56f976f2013-04-18 18:08:55 +00001287StringRef MachOObjectFile::getFileFormatName() const {
1288 unsigned CPUType = getCPUType(this);
1289 if (!is64Bit()) {
1290 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001291 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001292 return "Mach-O 32-bit i386";
Charles Davis74ec8b02013-08-27 05:00:13 +00001293 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001294 return "Mach-O arm";
Charles Davis74ec8b02013-08-27 05:00:13 +00001295 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001296 return "Mach-O 32-bit ppc";
1297 default:
Charles Davis74ec8b02013-08-27 05:00:13 +00001298 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
Rafael Espindola56f976f2013-04-18 18:08:55 +00001299 "64-bit object file when we're not 64-bit?");
1300 return "Mach-O 32-bit unknown";
1301 }
1302 }
1303
1304 // Make sure the cpu type has the correct mask.
Charles Davis74ec8b02013-08-27 05:00:13 +00001305 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1306 == llvm::MachO::CPU_ARCH_ABI64 &&
Eric Christopher1d62c252013-07-22 22:25:07 +00001307 "32-bit object file when we're 64-bit?");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001308
1309 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001310 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001311 return "Mach-O 64-bit x86-64";
Charles Davis74ec8b02013-08-27 05:00:13 +00001312 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001313 return "Mach-O 64-bit ppc64";
1314 default:
1315 return "Mach-O 64-bit unknown";
1316 }
1317}
1318
Alexey Samsonove6388e62013-06-18 15:03:28 +00001319Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1320 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001321 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001322 return Triple::x86;
Charles Davis74ec8b02013-08-27 05:00:13 +00001323 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001324 return Triple::x86_64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001325 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001326 return Triple::arm;
Charles Davis74ec8b02013-08-27 05:00:13 +00001327 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001328 return Triple::ppc;
Charles Davis74ec8b02013-08-27 05:00:13 +00001329 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001330 return Triple::ppc64;
1331 default:
1332 return Triple::UnknownArch;
1333 }
1334}
1335
Alexey Samsonove6388e62013-06-18 15:03:28 +00001336unsigned MachOObjectFile::getArch() const {
1337 return getArch(getCPUType(this));
1338}
1339
Rafael Espindola56f976f2013-04-18 18:08:55 +00001340StringRef MachOObjectFile::getLoadName() const {
1341 // TODO: Implement
Charles Davis1827bd82013-08-27 05:38:30 +00001342 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001343}
1344
Rui Ueyamabc654b12013-09-27 21:47:05 +00001345relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001346 DataRefImpl DRI;
1347 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001348 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001349}
1350
Rui Ueyamabc654b12013-09-27 21:47:05 +00001351relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001352 DataRefImpl DRI;
1353 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001354 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001355}
1356
Kevin Enderby273ae012013-06-06 17:20:50 +00001357dice_iterator MachOObjectFile::begin_dices() const {
1358 DataRefImpl DRI;
1359 if (!DataInCodeLoadCmd)
1360 return dice_iterator(DiceRef(DRI, this));
1361
Charles Davis8bdfafd2013-09-01 04:28:48 +00001362 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1363 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00001364 return dice_iterator(DiceRef(DRI, this));
1365}
1366
1367dice_iterator MachOObjectFile::end_dices() const {
1368 DataRefImpl DRI;
1369 if (!DataInCodeLoadCmd)
1370 return dice_iterator(DiceRef(DRI, this));
1371
Charles Davis8bdfafd2013-09-01 04:28:48 +00001372 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1373 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00001374 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1375 return dice_iterator(DiceRef(DRI, this));
1376}
1377
Rafael Espindola56f976f2013-04-18 18:08:55 +00001378StringRef
1379MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1380 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1381 return parseSegmentOrSectionName(Raw.data());
1382}
1383
1384ArrayRef<char>
1385MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001386 const section_base *Base =
1387 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1388 return ArrayRef<char>(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001389}
1390
1391ArrayRef<char>
1392MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001393 const section_base *Base =
1394 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1395 return ArrayRef<char>(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001396}
1397
1398bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00001399MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001400 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001401 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001402 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001403 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001404}
1405
Eric Christopher1d62c252013-07-22 22:25:07 +00001406unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001407 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001408 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001409 return RE.r_word1 & 0xffffff;
1410 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001411}
1412
Eric Christopher1d62c252013-07-22 22:25:07 +00001413bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001414 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001415 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001416 return (RE.r_word1 >> 27) & 1;
1417 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001418}
1419
Eric Christopher1d62c252013-07-22 22:25:07 +00001420bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001421 const MachO::any_relocation_info &RE) const {
1422 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001423}
1424
Eric Christopher1d62c252013-07-22 22:25:07 +00001425uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001426 const MachO::any_relocation_info &RE) const {
1427 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001428}
1429
Eric Christopher1d62c252013-07-22 22:25:07 +00001430unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001431 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001432 if (isRelocationScattered(RE))
1433 return getScatteredRelocationAddress(RE);
1434 return getPlainRelocationAddress(RE);
1435}
1436
Charles Davis8bdfafd2013-09-01 04:28:48 +00001437unsigned MachOObjectFile::getAnyRelocationPCRel(
1438 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001439 if (isRelocationScattered(RE))
1440 return getScatteredRelocationPCRel(this, RE);
1441 return getPlainRelocationPCRel(this, RE);
1442}
1443
Eric Christopher1d62c252013-07-22 22:25:07 +00001444unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001445 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001446 if (isRelocationScattered(RE))
1447 return getScatteredRelocationLength(RE);
1448 return getPlainRelocationLength(this, RE);
1449}
1450
1451unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00001452MachOObjectFile::getAnyRelocationType(
1453 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001454 if (isRelocationScattered(RE))
1455 return getScatteredRelocationType(RE);
1456 return getPlainRelocationType(this, RE);
1457}
1458
Rafael Espindola52501032013-04-30 15:40:54 +00001459SectionRef
Charles Davis8bdfafd2013-09-01 04:28:48 +00001460MachOObjectFile::getRelocationSection(
1461 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00001462 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1463 return *end_sections();
1464 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1465 DataRefImpl DRI;
1466 DRI.d.a = SecNum;
1467 return SectionRef(DRI, this);
1468}
1469
Rafael Espindola56f976f2013-04-18 18:08:55 +00001470MachOObjectFile::LoadCommandInfo
1471MachOObjectFile::getFirstLoadCommandInfo() const {
1472 MachOObjectFile::LoadCommandInfo Load;
1473
Charles Davis8bdfafd2013-09-01 04:28:48 +00001474 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1475 sizeof(MachO::mach_header);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001476 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001477 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001478 return Load;
1479}
1480
1481MachOObjectFile::LoadCommandInfo
1482MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1483 MachOObjectFile::LoadCommandInfo Next;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001484 Next.Ptr = L.Ptr + L.C.cmdsize;
1485 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001486 return Next;
1487}
1488
Charles Davis8bdfafd2013-09-01 04:28:48 +00001489MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1490 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001491}
1492
Charles Davis8bdfafd2013-09-01 04:28:48 +00001493MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1494 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001495}
1496
Charles Davis8bdfafd2013-09-01 04:28:48 +00001497MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00001498 unsigned Index) const {
1499 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001500 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001501}
1502
Charles Davis8bdfafd2013-09-01 04:28:48 +00001503MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1504 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001505 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001506 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001507}
1508
Charles Davis8bdfafd2013-09-01 04:28:48 +00001509MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00001510MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001511 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001512 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001513}
1514
Charles Davis8bdfafd2013-09-01 04:28:48 +00001515MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00001516MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001517 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001518 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001519}
1520
Charles Davis8bdfafd2013-09-01 04:28:48 +00001521MachO::linkedit_data_command
1522MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1523 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001524}
1525
Charles Davis8bdfafd2013-09-01 04:28:48 +00001526MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001527MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001528 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001529}
1530
Charles Davis8bdfafd2013-09-01 04:28:48 +00001531MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00001532MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001533 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001534}
1535
Charles Davis8bdfafd2013-09-01 04:28:48 +00001536MachO::linker_options_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001537MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001538 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001539}
1540
Charles Davis8bdfafd2013-09-01 04:28:48 +00001541MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001542MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +00001543 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001544 return getStruct<MachO::any_relocation_info>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001545}
1546
Charles Davis8bdfafd2013-09-01 04:28:48 +00001547MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00001548MachOObjectFile::getDice(DataRefImpl Rel) const {
1549 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001550 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00001551}
1552
Charles Davis8bdfafd2013-09-01 04:28:48 +00001553MachO::mach_header MachOObjectFile::getHeader() const {
1554 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001555}
1556
Charles Davis8bdfafd2013-09-01 04:28:48 +00001557MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1558 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001559}
1560
Charles Davis8bdfafd2013-09-01 04:28:48 +00001561uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1562 const MachO::dysymtab_command &DLC,
1563 unsigned Index) const {
1564 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1565 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001566}
1567
Charles Davis8bdfafd2013-09-01 04:28:48 +00001568MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00001569MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1570 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001571 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1572 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001573}
1574
Charles Davis8bdfafd2013-09-01 04:28:48 +00001575MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1576 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001577}
1578
Charles Davis8bdfafd2013-09-01 04:28:48 +00001579MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1580 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001581}
1582
Charles Davis8bdfafd2013-09-01 04:28:48 +00001583MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00001584MachOObjectFile::getDataInCodeLoadCommand() const {
1585 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00001586 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00001587
1588 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001589 MachO::linkedit_data_command Cmd;
1590 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1591 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1592 Cmd.dataoff = 0;
1593 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00001594 return Cmd;
1595}
1596
Rafael Espindola6e040c02013-04-26 20:07:33 +00001597StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001598 MachO::symtab_command S = getSymtabLoadCommand();
1599 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001600}
1601
Rafael Espindola56f976f2013-04-18 18:08:55 +00001602bool MachOObjectFile::is64Bit() const {
1603 return getType() == getMachOType(false, true) ||
1604 getType() == getMachOType(true, true);
1605}
1606
1607void MachOObjectFile::ReadULEB128s(uint64_t Index,
1608 SmallVectorImpl<uint64_t> &Out) const {
1609 DataExtractor extractor(ObjectFile::getData(), true, 0);
1610
1611 uint32_t offset = Index;
1612 uint64_t data = 0;
1613 while (uint64_t delta = extractor.getULEB128(&offset)) {
1614 data += delta;
1615 Out.push_back(data);
1616 }
1617}
1618
1619ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1620 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1621 error_code ec;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001622 OwningPtr<ObjectFile> Ret;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001623 if (Magic == "\xFE\xED\xFA\xCE")
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001624 Ret.reset(new MachOObjectFile(Buffer, false, false, ec));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001625 else if (Magic == "\xCE\xFA\xED\xFE")
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001626 Ret.reset(new MachOObjectFile(Buffer, true, false, ec));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001627 else if (Magic == "\xFE\xED\xFA\xCF")
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001628 Ret.reset(new MachOObjectFile(Buffer, false, true, ec));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001629 else if (Magic == "\xCF\xFA\xED\xFE")
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001630 Ret.reset(new MachOObjectFile(Buffer, true, true, ec));
1631 else {
1632 delete Buffer;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001633 return NULL;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001634 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001635
1636 if (ec)
1637 return NULL;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001638 return Ret.take();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001639}
1640
Owen Anderson27c579d2011-10-11 17:32:27 +00001641} // end namespace object
Eric Christopher7b015c72011-04-22 03:19:48 +00001642} // end namespace llvm