blob: 49e3148ac8f670fd170685be44ce58cd313c3231 [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>
Rafael Espindola5e812af2014-01-30 02:49:50 +0000278static void advance(T &it, size_t Val) {
279 while (Val--)
280 ++it;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000281}
282
283static unsigned getCPUType(const MachOObjectFile *O) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000284 return O->getHeader().cputype;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000285}
286
287static void printRelocationTargetName(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000288 const MachO::any_relocation_info &RE,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000289 raw_string_ostream &fmt) {
290 bool IsScattered = O->isRelocationScattered(RE);
291
292 // Target of a scattered relocation is an address. In the interest of
293 // generating pretty output, scan through the symbol table looking for a
294 // symbol that aligns with that address. If we find one, print it.
295 // Otherwise, we just print the hex address of the target.
296 if (IsScattered) {
297 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
298
Rafael Espindola56f976f2013-04-18 18:08:55 +0000299 for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000300 SI != SE; ++SI) {
301 error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000302 uint64_t Addr;
303 StringRef Name;
304
305 if ((ec = SI->getAddress(Addr)))
306 report_fatal_error(ec.message());
307 if (Addr != Val) continue;
308 if ((ec = SI->getName(Name)))
309 report_fatal_error(ec.message());
310 fmt << Name;
311 return;
312 }
313
314 // If we couldn't find a symbol that this relocation refers to, try
315 // to find a section beginning instead.
316 for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000317 SI != SE; ++SI) {
318 error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000319 uint64_t Addr;
320 StringRef Name;
321
322 if ((ec = SI->getAddress(Addr)))
323 report_fatal_error(ec.message());
324 if (Addr != Val) continue;
325 if ((ec = SI->getName(Name)))
326 report_fatal_error(ec.message());
327 fmt << Name;
328 return;
329 }
330
331 fmt << format("0x%x", Val);
332 return;
333 }
334
335 StringRef S;
336 bool isExtern = O->getPlainRelocationExternal(RE);
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000337 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000338
339 if (isExtern) {
340 symbol_iterator SI = O->begin_symbols();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000341 advance(SI, Val);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000342 SI->getName(S);
343 } else {
344 section_iterator SI = O->begin_sections();
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000345 // Adjust for the fact that sections are 1-indexed.
Rafael Espindola5e812af2014-01-30 02:49:50 +0000346 advance(SI, Val - 1);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000347 SI->getName(S);
348 }
349
350 fmt << S;
351}
352
Charles Davis8bdfafd2013-09-01 04:28:48 +0000353static uint32_t
354getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
355 return RE.r_word0;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000356}
357
358static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000359getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
360 return RE.r_word0 & 0xffffff;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000361}
362
363static bool getPlainRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000364 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000365 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000366 return (RE.r_word1 >> 24) & 1;
367 return (RE.r_word1 >> 7) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000368}
369
370static bool
371getScatteredRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000372 const MachO::any_relocation_info &RE) {
373 return (RE.r_word0 >> 30) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000374}
375
376static unsigned getPlainRelocationLength(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000377 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000378 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000379 return (RE.r_word1 >> 25) & 3;
380 return (RE.r_word1 >> 5) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000381}
382
383static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000384getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
385 return (RE.r_word0 >> 28) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000386}
387
388static unsigned getPlainRelocationType(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 >> 28;
392 return RE.r_word1 & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000393}
394
Charles Davis8bdfafd2013-09-01 04:28:48 +0000395static unsigned
396getScatteredRelocationType(const MachO::any_relocation_info &RE) {
397 return (RE.r_word0 >> 24) & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000398}
399
400static uint32_t getSectionFlags(const MachOObjectFile *O,
401 DataRefImpl Sec) {
402 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000403 MachO::section_64 Sect = O->getSection64(Sec);
404 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000405 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000406 MachO::section Sect = O->getSection(Sec);
407 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000408}
409
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000410MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
411 bool Is64bits, error_code &EC,
412 bool BufferOwned)
413 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned),
Kevin Enderby273ae012013-06-06 17:20:50 +0000414 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000415 uint32_t LoadCommandCount = this->getHeader().ncmds;
416 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
417 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000418
419 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000420 for (unsigned I = 0; ; ++I) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000421 if (Load.C.cmd == MachO::LC_SYMTAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000422 assert(!SymtabLoadCmd && "Multiple symbol tables");
423 SymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000424 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000425 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
426 DysymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000427 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000428 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
429 DataInCodeLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000430 } else if (Load.C.cmd == SegmentLoadType) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000431 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
432 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000433 const char *Sec = getSectionPtr(this, Load, J);
434 Sections.push_back(Sec);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000435 }
436 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000437
438 if (I == LoadCommandCount - 1)
439 break;
440 else
441 Load = getNextLoadCommandInfo(Load);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000442 }
443}
444
Rafael Espindola5e812af2014-01-30 02:49:50 +0000445void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
Rafael Espindola75c30362013-04-24 19:47:55 +0000446 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000447 sizeof(MachO::nlist_64) :
448 sizeof(MachO::nlist);
Rafael Espindola75c30362013-04-24 19:47:55 +0000449 Symb.p += SymbolTableEntrySize;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000450}
451
452error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
453 StringRef &Res) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000454 StringRef StringTable = getStringTableData();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000455 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
456 const char *Start = &StringTable.data()[Entry.n_strx];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000457 Res = StringRef(Start);
458 return object_error::success;
459}
460
461error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
462 uint64_t &Res) const {
463 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000464 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
465 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000466 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000467 MachO::nlist Entry = getSymbolTableEntry(Symb);
468 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000469 }
470 return object_error::success;
471}
472
473error_code
474MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
475 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000476 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000477 getSymbolAddress(Symb, Res);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000478 if (Entry.n_sect) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000479 uint64_t Delta;
480 DataRefImpl SecRel;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000481 SecRel.d.a = Entry.n_sect-1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000482 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000483 MachO::section_64 Sec = getSection64(SecRel);
484 Delta = Sec.offset - Sec.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000485 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000486 MachO::section Sec = getSection(SecRel);
487 Delta = Sec.offset - Sec.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000488 }
489
490 Res += Delta;
491 }
492
493 return object_error::success;
494}
495
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000496error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
497 uint32_t &Result) const {
498 uint32_t flags;
499 this->getSymbolFlags(DRI, flags);
500 if (flags & SymbolRef::SF_Common) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000501 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
502 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000503 } else {
504 Result = 0;
505 }
506 return object_error::success;
507}
508
Rafael Espindola56f976f2013-04-18 18:08:55 +0000509error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
510 uint64_t &Result) const {
511 uint64_t BeginOffset;
512 uint64_t EndOffset = 0;
513 uint8_t SectionIndex;
514
Charles Davis8bdfafd2013-09-01 04:28:48 +0000515 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000516 uint64_t Value;
517 getSymbolAddress(DRI, Value);
518
519 BeginOffset = Value;
520
Charles Davis8bdfafd2013-09-01 04:28:48 +0000521 SectionIndex = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000522 if (!SectionIndex) {
523 uint32_t flags = SymbolRef::SF_None;
524 this->getSymbolFlags(DRI, flags);
525 if (flags & SymbolRef::SF_Common)
526 Result = Value;
527 else
528 Result = UnknownAddressOrSize;
529 return object_error::success;
530 }
531 // Unfortunately symbols are unsorted so we need to touch all
532 // symbols from load command
Rafael Espindola5e812af2014-01-30 02:49:50 +0000533 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E; ++I) {
Rafael Espindola75c30362013-04-24 19:47:55 +0000534 DataRefImpl DRI = I->getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000535 Entry = getSymbolTableEntryBase(this, DRI);
536 getSymbolAddress(DRI, Value);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000537 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000538 if (!EndOffset || Value < EndOffset)
539 EndOffset = Value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000540 }
541 if (!EndOffset) {
542 uint64_t Size;
543 DataRefImpl Sec;
544 Sec.d.a = SectionIndex-1;
545 getSectionSize(Sec, Size);
546 getSectionAddress(Sec, EndOffset);
547 EndOffset += Size;
548 }
549 Result = EndOffset - BeginOffset;
550 return object_error::success;
551}
552
553error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
554 SymbolRef::Type &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000555 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
556 uint8_t n_type = Entry.n_type;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000557
558 Res = SymbolRef::ST_Other;
559
560 // If this is a STAB debugging symbol, we can do nothing more.
Charles Davis74ec8b02013-08-27 05:00:13 +0000561 if (n_type & MachO::N_STAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000562 Res = SymbolRef::ST_Debug;
563 return object_error::success;
564 }
565
Charles Davis74ec8b02013-08-27 05:00:13 +0000566 switch (n_type & MachO::N_TYPE) {
567 case MachO::N_UNDF :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000568 Res = SymbolRef::ST_Unknown;
569 break;
Charles Davis74ec8b02013-08-27 05:00:13 +0000570 case MachO::N_SECT :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000571 Res = SymbolRef::ST_Function;
572 break;
573 }
574 return object_error::success;
575}
576
Rafael Espindola56f976f2013-04-18 18:08:55 +0000577error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
578 uint32_t &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000579 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000580
Charles Davis8bdfafd2013-09-01 04:28:48 +0000581 uint8_t MachOType = Entry.n_type;
582 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000583
584 // TODO: Correctly set SF_ThreadLocal
585 Result = SymbolRef::SF_None;
586
Charles Davis74ec8b02013-08-27 05:00:13 +0000587 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000588 Result |= SymbolRef::SF_Undefined;
589
Rafael Espindolaa1356322013-11-02 05:03:24 +0000590 if (MachOType & MachO::N_STAB)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000591 Result |= SymbolRef::SF_FormatSpecific;
592
Charles Davis74ec8b02013-08-27 05:00:13 +0000593 if (MachOType & MachO::N_EXT) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000594 Result |= SymbolRef::SF_Global;
Charles Davis74ec8b02013-08-27 05:00:13 +0000595 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000596 uint64_t Value;
597 getSymbolAddress(DRI, Value);
598 if (Value)
599 Result |= SymbolRef::SF_Common;
600 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000601 }
602
Charles Davis74ec8b02013-08-27 05:00:13 +0000603 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000604 Result |= SymbolRef::SF_Weak;
605
Charles Davis74ec8b02013-08-27 05:00:13 +0000606 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000607 Result |= SymbolRef::SF_Absolute;
608
609 return object_error::success;
610}
611
612error_code
613MachOObjectFile::getSymbolSection(DataRefImpl Symb,
614 section_iterator &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000615 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
616 uint8_t index = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000617
618 if (index == 0) {
619 Res = end_sections();
620 } else {
621 DataRefImpl DRI;
622 DRI.d.a = index - 1;
623 Res = section_iterator(SectionRef(DRI, this));
624 }
625
626 return object_error::success;
627}
628
629error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
Rafael Espindola137faa02013-04-24 15:14:22 +0000630 uint64_t &Val) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000631 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
632}
633
Rafael Espindola5e812af2014-01-30 02:49:50 +0000634void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000635 Sec.d.a++;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000636}
637
638error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000639MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000640 ArrayRef<char> Raw = getSectionRawName(Sec);
641 Result = parseSegmentOrSectionName(Raw.data());
642 return object_error::success;
643}
644
645error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000646MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000647 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000648 MachO::section_64 Sect = getSection64(Sec);
649 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000650 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000651 MachO::section Sect = getSection(Sec);
652 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000653 }
654 return object_error::success;
655}
656
657error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000658MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000659 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000660 MachO::section_64 Sect = getSection64(Sec);
661 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000662 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000663 MachO::section Sect = getSection(Sec);
664 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000665 }
666
667 return object_error::success;
668}
669
670error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000671MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000672 uint32_t Offset;
673 uint64_t Size;
674
675 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000676 MachO::section_64 Sect = getSection64(Sec);
677 Offset = Sect.offset;
678 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000679 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000680 MachO::section Sect = getSection(Sec);
681 Offset = Sect.offset;
682 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000683 }
684
685 Res = this->getData().substr(Offset, Size);
686 return object_error::success;
687}
688
689error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000690MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000691 uint32_t Align;
692 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000693 MachO::section_64 Sect = getSection64(Sec);
694 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000695 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000696 MachO::section Sect = getSection(Sec);
697 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000698 }
699
700 Res = uint64_t(1) << Align;
701 return object_error::success;
702}
703
704error_code
705MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
706 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000707 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000708 return object_error::success;
709}
710
Rafael Espindola137faa02013-04-24 15:14:22 +0000711error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
Michael J. Spencer800619f2011-09-28 20:57:30 +0000712 // FIXME: Unimplemented.
713 Result = false;
714 return object_error::success;
715}
716
Rafael Espindola137faa02013-04-24 15:14:22 +0000717error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000718 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000719 Result = false;
720 return object_error::success;
721}
722
Rafael Espindolac2413f52013-04-09 14:49:08 +0000723error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000724MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000725 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000726 // FIXME: Unimplemented.
727 Result = true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000728 return object_error::success;
729}
730
Rafael Espindola56f976f2013-04-18 18:08:55 +0000731error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000732 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000733 // FIXME: Unimplemented.
734 Result = false;
735 return object_error::success;
736}
737
Rafael Espindola56f976f2013-04-18 18:08:55 +0000738error_code
739MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
740 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis74ec8b02013-08-27 05:00:13 +0000741 unsigned SectionType = Flags & MachO::SECTION_TYPE;
742 Res = SectionType == MachO::S_ZEROFILL ||
743 SectionType == MachO::S_GB_ZEROFILL;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000744 return object_error::success;
745}
746
747error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000748 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000749 // Consider using the code from isSectionText to look for __const sections.
750 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
751 // to use section attributes to distinguish code from data.
752
753 // FIXME: Unimplemented.
754 Result = false;
755 return object_error::success;
756}
757
Rafael Espindola56f976f2013-04-18 18:08:55 +0000758error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000759MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000760 bool &Result) const {
761 SymbolRef::Type ST;
762 this->getSymbolType(Symb, ST);
763 if (ST == SymbolRef::ST_Unknown) {
764 Result = false;
765 return object_error::success;
766 }
767
768 uint64_t SectBegin, SectEnd;
769 getSectionAddress(Sec, SectBegin);
770 getSectionSize(Sec, SectEnd);
771 SectEnd += SectBegin;
772
773 uint64_t SymAddr;
774 getSymbolAddress(Symb, SymAddr);
775 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
776
777 return object_error::success;
778}
779
Rui Ueyamabc654b12013-09-27 21:47:05 +0000780relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000781 uint32_t Offset;
782 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000783 MachO::section_64 Sect = getSection64(Sec);
784 Offset = Sect.reloff;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000785 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000786 MachO::section Sect = getSection(Sec);
787 Offset = Sect.reloff;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000788 }
789
790 DataRefImpl Ret;
791 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
792 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000793}
Rafael Espindolac0406e12013-04-08 20:45:01 +0000794
Rafael Espindola56f976f2013-04-18 18:08:55 +0000795relocation_iterator
Rui Ueyamabc654b12013-09-27 21:47:05 +0000796MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000797 uint32_t Offset;
798 uint32_t Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000799 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000800 MachO::section_64 Sect = getSection64(Sec);
801 Offset = Sect.reloff;
802 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000803 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000804 MachO::section Sect = getSection(Sec);
805 Offset = Sect.reloff;
806 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000807 }
Eric Christopher7b015c72011-04-22 03:19:48 +0000808
Charles Davis8bdfafd2013-09-01 04:28:48 +0000809 const MachO::any_relocation_info *P =
810 reinterpret_cast<const MachO::any_relocation_info *>(getPtr(this, Offset));
Rafael Espindola04d3f492013-04-25 12:45:46 +0000811
Rafael Espindola56f976f2013-04-18 18:08:55 +0000812 DataRefImpl Ret;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000813 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000814 return relocation_iterator(RelocationRef(Ret, this));
815}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000816
Rafael Espindola5e812af2014-01-30 02:49:50 +0000817void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000818 const MachO::any_relocation_info *P =
819 reinterpret_cast<const MachO::any_relocation_info *>(Rel.p);
Rafael Espindola04d3f492013-04-25 12:45:46 +0000820 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000821}
Owen Anderson171f4852011-10-24 23:20:07 +0000822
Rafael Espindola56f976f2013-04-18 18:08:55 +0000823error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000824MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000825 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000826}
827
Rafael Espindola56f976f2013-04-18 18:08:55 +0000828error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000829 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000830 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000831 Res = getAnyRelocationAddress(RE);
832 return object_error::success;
David Meyer2fc34c52012-03-01 01:36:50 +0000833}
834
Rafael Espindola806f0062013-06-05 01:33:53 +0000835symbol_iterator
836MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000837 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000838 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
839 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola806f0062013-06-05 01:33:53 +0000840 if (!isExtern)
841 return end_symbols();
Rafael Espindola75c30362013-04-24 19:47:55 +0000842
Charles Davis8bdfafd2013-09-01 04:28:48 +0000843 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +0000844 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000845 sizeof(MachO::nlist_64) :
846 sizeof(MachO::nlist);
847 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +0000848 DataRefImpl Sym;
849 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola806f0062013-06-05 01:33:53 +0000850 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +0000851}
852
853error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000854 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000855 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000856 Res = getAnyRelocationType(RE);
857 return object_error::success;
858}
859
860error_code
861MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
862 SmallVectorImpl<char> &Result) const {
863 StringRef res;
864 uint64_t RType;
865 getRelocationType(Rel, RType);
866
867 unsigned Arch = this->getArch();
868
869 switch (Arch) {
870 case Triple::x86: {
871 static const char *const Table[] = {
872 "GENERIC_RELOC_VANILLA",
873 "GENERIC_RELOC_PAIR",
874 "GENERIC_RELOC_SECTDIFF",
875 "GENERIC_RELOC_PB_LA_PTR",
876 "GENERIC_RELOC_LOCAL_SECTDIFF",
877 "GENERIC_RELOC_TLV" };
878
Eric Christopher13250cb2013-12-06 02:33:38 +0000879 if (RType > 5)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000880 res = "Unknown";
881 else
882 res = Table[RType];
883 break;
884 }
885 case Triple::x86_64: {
886 static const char *const Table[] = {
887 "X86_64_RELOC_UNSIGNED",
888 "X86_64_RELOC_SIGNED",
889 "X86_64_RELOC_BRANCH",
890 "X86_64_RELOC_GOT_LOAD",
891 "X86_64_RELOC_GOT",
892 "X86_64_RELOC_SUBTRACTOR",
893 "X86_64_RELOC_SIGNED_1",
894 "X86_64_RELOC_SIGNED_2",
895 "X86_64_RELOC_SIGNED_4",
896 "X86_64_RELOC_TLV" };
897
898 if (RType > 9)
899 res = "Unknown";
900 else
901 res = Table[RType];
902 break;
903 }
904 case Triple::arm: {
905 static const char *const Table[] = {
906 "ARM_RELOC_VANILLA",
907 "ARM_RELOC_PAIR",
908 "ARM_RELOC_SECTDIFF",
909 "ARM_RELOC_LOCAL_SECTDIFF",
910 "ARM_RELOC_PB_LA_PTR",
911 "ARM_RELOC_BR24",
912 "ARM_THUMB_RELOC_BR22",
913 "ARM_THUMB_32BIT_BRANCH",
914 "ARM_RELOC_HALF",
915 "ARM_RELOC_HALF_SECTDIFF" };
916
917 if (RType > 9)
918 res = "Unknown";
919 else
920 res = Table[RType];
921 break;
922 }
923 case Triple::ppc: {
924 static const char *const Table[] = {
925 "PPC_RELOC_VANILLA",
926 "PPC_RELOC_PAIR",
927 "PPC_RELOC_BR14",
928 "PPC_RELOC_BR24",
929 "PPC_RELOC_HI16",
930 "PPC_RELOC_LO16",
931 "PPC_RELOC_HA16",
932 "PPC_RELOC_LO14",
933 "PPC_RELOC_SECTDIFF",
934 "PPC_RELOC_PB_LA_PTR",
935 "PPC_RELOC_HI16_SECTDIFF",
936 "PPC_RELOC_LO16_SECTDIFF",
937 "PPC_RELOC_HA16_SECTDIFF",
938 "PPC_RELOC_JBSR",
939 "PPC_RELOC_LO14_SECTDIFF",
940 "PPC_RELOC_LOCAL_SECTDIFF" };
941
Eric Christopher13250cb2013-12-06 02:33:38 +0000942 if (RType > 15)
943 res = "Unknown";
944 else
945 res = Table[RType];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000946 break;
947 }
948 case Triple::UnknownArch:
949 res = "Unknown";
950 break;
951 }
952 Result.append(res.begin(), res.end());
953 return object_error::success;
954}
955
Rafael Espindola56f976f2013-04-18 18:08:55 +0000956error_code
957MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000958 SmallVectorImpl<char> &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000959 MachO::any_relocation_info RE = getRelocation(Rel);
David Meyer2fc34c52012-03-01 01:36:50 +0000960
Rafael Espindola56f976f2013-04-18 18:08:55 +0000961 unsigned Arch = this->getArch();
Eric Christopher7b015c72011-04-22 03:19:48 +0000962
Rafael Espindola56f976f2013-04-18 18:08:55 +0000963 std::string fmtbuf;
964 raw_string_ostream fmt(fmtbuf);
965 unsigned Type = this->getAnyRelocationType(RE);
966 bool IsPCRel = this->getAnyRelocationPCRel(RE);
967
968 // Determine any addends that should be displayed with the relocation.
969 // These require decoding the relocation type, which is triple-specific.
970
971 // X86_64 has entirely custom relocation types.
972 if (Arch == Triple::x86_64) {
973 bool isPCRel = getAnyRelocationPCRel(RE);
974
975 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000976 case MachO::X86_64_RELOC_GOT_LOAD:
977 case MachO::X86_64_RELOC_GOT: {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000978 printRelocationTargetName(this, RE, fmt);
979 fmt << "@GOT";
980 if (isPCRel) fmt << "PCREL";
981 break;
982 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000983 case MachO::X86_64_RELOC_SUBTRACTOR: {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000984 DataRefImpl RelNext = Rel;
985 RelNext.d.a++;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000986 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000987
Charles Davis8bdfafd2013-09-01 04:28:48 +0000988 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
Rafael Espindola56f976f2013-04-18 18:08:55 +0000989 // X86_64_RELOC_UNSIGNED.
990 // NOTE: Scattered relocations don't exist on x86_64.
991 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000992 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000993 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
994 "X86_64_RELOC_SUBTRACTOR.");
995
Charles Davis8bdfafd2013-09-01 04:28:48 +0000996 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
997 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
Rafael Espindola56f976f2013-04-18 18:08:55 +0000998 printRelocationTargetName(this, RENext, fmt);
999 fmt << "-";
1000 printRelocationTargetName(this, RE, fmt);
1001 break;
1002 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001003 case MachO::X86_64_RELOC_TLV:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001004 printRelocationTargetName(this, RE, fmt);
1005 fmt << "@TLV";
1006 if (isPCRel) fmt << "P";
1007 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001008 case MachO::X86_64_RELOC_SIGNED_1:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001009 printRelocationTargetName(this, RE, fmt);
1010 fmt << "-1";
1011 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001012 case MachO::X86_64_RELOC_SIGNED_2:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001013 printRelocationTargetName(this, RE, fmt);
1014 fmt << "-2";
1015 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001016 case MachO::X86_64_RELOC_SIGNED_4:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001017 printRelocationTargetName(this, RE, fmt);
1018 fmt << "-4";
1019 break;
1020 default:
1021 printRelocationTargetName(this, RE, fmt);
1022 break;
1023 }
1024 // X86 and ARM share some relocation types in common.
David Fangb88cdf62013-08-08 20:14:40 +00001025 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1026 Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001027 // Generic relocation types...
1028 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001029 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001030 return object_error::success;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001031 case MachO::GENERIC_RELOC_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001032 DataRefImpl RelNext = Rel;
1033 RelNext.d.a++;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001034 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001035
1036 // X86 sect diff's must be followed by a relocation of type
1037 // GENERIC_RELOC_PAIR.
1038 unsigned RType = getAnyRelocationType(RENext);
1039
Charles Davis8bdfafd2013-09-01 04:28:48 +00001040 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001041 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1042 "GENERIC_RELOC_SECTDIFF.");
1043
1044 printRelocationTargetName(this, RE, fmt);
1045 fmt << "-";
1046 printRelocationTargetName(this, RENext, fmt);
1047 break;
1048 }
1049 }
1050
David Fangb88cdf62013-08-08 20:14:40 +00001051 if (Arch == Triple::x86 || Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001052 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001053 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001054 DataRefImpl RelNext = Rel;
1055 RelNext.d.a++;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001056 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001057
1058 // X86 sect diff's must be followed by a relocation of type
1059 // GENERIC_RELOC_PAIR.
1060 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001061 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001062 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1063 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1064
1065 printRelocationTargetName(this, RE, fmt);
1066 fmt << "-";
1067 printRelocationTargetName(this, RENext, fmt);
1068 break;
1069 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001070 case MachO::GENERIC_RELOC_TLV: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001071 printRelocationTargetName(this, RE, fmt);
1072 fmt << "@TLV";
1073 if (IsPCRel) fmt << "P";
1074 break;
1075 }
1076 default:
1077 printRelocationTargetName(this, RE, fmt);
1078 }
1079 } else { // ARM-specific relocations
1080 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001081 case MachO::ARM_RELOC_HALF:
1082 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001083 // Half relocations steal a bit from the length field to encode
1084 // whether this is an upper16 or a lower16 relocation.
1085 bool isUpper = getAnyRelocationLength(RE) >> 1;
1086
1087 if (isUpper)
1088 fmt << ":upper16:(";
1089 else
1090 fmt << ":lower16:(";
1091 printRelocationTargetName(this, RE, fmt);
1092
1093 DataRefImpl RelNext = Rel;
1094 RelNext.d.a++;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001095 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001096
1097 // ARM half relocs must be followed by a relocation of type
1098 // ARM_RELOC_PAIR.
1099 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001100 if (RType != MachO::ARM_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001101 report_fatal_error("Expected ARM_RELOC_PAIR after "
Charles Davis8bdfafd2013-09-01 04:28:48 +00001102 "ARM_RELOC_HALF");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001103
1104 // NOTE: The half of the target virtual address is stashed in the
1105 // address field of the secondary relocation, but we can't reverse
1106 // engineer the constant offset from it without decoding the movw/movt
1107 // instruction to find the other half in its immediate field.
1108
1109 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1110 // symbol/section pointer of the follow-on relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001111 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001112 fmt << "-";
1113 printRelocationTargetName(this, RENext, fmt);
1114 }
1115
1116 fmt << ")";
1117 break;
1118 }
1119 default: {
1120 printRelocationTargetName(this, RE, fmt);
1121 }
1122 }
1123 }
1124 } else
1125 printRelocationTargetName(this, RE, fmt);
1126
1127 fmt.flush();
1128 Result.append(fmtbuf.begin(), fmtbuf.end());
1129 return object_error::success;
1130}
1131
1132error_code
Rafael Espindola137faa02013-04-24 15:14:22 +00001133MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001134 unsigned Arch = getArch();
1135 uint64_t Type;
1136 getRelocationType(Rel, Type);
1137
1138 Result = false;
1139
1140 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1141 // is always hidden.
David Fangb88cdf62013-08-08 20:14:40 +00001142 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001143 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001144 } else if (Arch == Triple::x86_64) {
1145 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
Eric Christopher1ff26ab62013-07-22 22:25:09 +00001146 // an X86_64_RELOC_SUBTRACTOR.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001147 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001148 DataRefImpl RelPrev = Rel;
1149 RelPrev.d.a--;
1150 uint64_t PrevType;
1151 getRelocationType(RelPrev, PrevType);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001152 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001153 Result = true;
1154 }
1155 }
1156
1157 return object_error::success;
1158}
1159
1160error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001161 LibraryRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001162 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1163}
1164
1165error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001166 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001167 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1168}
1169
1170symbol_iterator MachOObjectFile::begin_symbols() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001171 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001172 if (!SymtabLoadCmd)
1173 return symbol_iterator(SymbolRef(DRI, this));
1174
Charles Davis8bdfafd2013-09-01 04:28:48 +00001175 MachO::symtab_command Symtab = getSymtabLoadCommand();
1176 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001177 return symbol_iterator(SymbolRef(DRI, this));
1178}
1179
1180symbol_iterator MachOObjectFile::end_symbols() const {
1181 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001182 if (!SymtabLoadCmd)
1183 return symbol_iterator(SymbolRef(DRI, this));
1184
Charles Davis8bdfafd2013-09-01 04:28:48 +00001185 MachO::symtab_command Symtab = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +00001186 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001187 sizeof(MachO::nlist_64) :
1188 sizeof(MachO::nlist);
1189 unsigned Offset = Symtab.symoff +
1190 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001191 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001192 return symbol_iterator(SymbolRef(DRI, this));
1193}
1194
1195symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1196 // TODO: implement
1197 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1198}
1199
1200symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1201 // TODO: implement
1202 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1203}
1204
1205section_iterator MachOObjectFile::begin_sections() const {
1206 DataRefImpl DRI;
1207 return section_iterator(SectionRef(DRI, this));
1208}
1209
1210section_iterator MachOObjectFile::end_sections() const {
1211 DataRefImpl DRI;
1212 DRI.d.a = Sections.size();
1213 return section_iterator(SectionRef(DRI, this));
1214}
1215
1216library_iterator MachOObjectFile::begin_libraries_needed() const {
1217 // TODO: implement
1218 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1219}
1220
1221library_iterator MachOObjectFile::end_libraries_needed() const {
1222 // TODO: implement
1223 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1224}
1225
1226uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola60689982013-04-07 19:05:30 +00001227 return is64Bit() ? 8 : 4;
Eric Christopher7b015c72011-04-22 03:19:48 +00001228}
1229
Rafael Espindola56f976f2013-04-18 18:08:55 +00001230StringRef MachOObjectFile::getFileFormatName() const {
1231 unsigned CPUType = getCPUType(this);
1232 if (!is64Bit()) {
1233 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001234 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001235 return "Mach-O 32-bit i386";
Charles Davis74ec8b02013-08-27 05:00:13 +00001236 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001237 return "Mach-O arm";
Charles Davis74ec8b02013-08-27 05:00:13 +00001238 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001239 return "Mach-O 32-bit ppc";
1240 default:
Charles Davis74ec8b02013-08-27 05:00:13 +00001241 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
Rafael Espindola56f976f2013-04-18 18:08:55 +00001242 "64-bit object file when we're not 64-bit?");
1243 return "Mach-O 32-bit unknown";
1244 }
1245 }
1246
1247 // Make sure the cpu type has the correct mask.
Charles Davis74ec8b02013-08-27 05:00:13 +00001248 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1249 == llvm::MachO::CPU_ARCH_ABI64 &&
Eric Christopher1d62c252013-07-22 22:25:07 +00001250 "32-bit object file when we're 64-bit?");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001251
1252 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001253 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001254 return "Mach-O 64-bit x86-64";
Charles Davis74ec8b02013-08-27 05:00:13 +00001255 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001256 return "Mach-O 64-bit ppc64";
1257 default:
1258 return "Mach-O 64-bit unknown";
1259 }
1260}
1261
Alexey Samsonove6388e62013-06-18 15:03:28 +00001262Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1263 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001264 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001265 return Triple::x86;
Charles Davis74ec8b02013-08-27 05:00:13 +00001266 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001267 return Triple::x86_64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001268 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001269 return Triple::arm;
Charles Davis74ec8b02013-08-27 05:00:13 +00001270 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001271 return Triple::ppc;
Charles Davis74ec8b02013-08-27 05:00:13 +00001272 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001273 return Triple::ppc64;
1274 default:
1275 return Triple::UnknownArch;
1276 }
1277}
1278
Alexey Samsonove6388e62013-06-18 15:03:28 +00001279unsigned MachOObjectFile::getArch() const {
1280 return getArch(getCPUType(this));
1281}
1282
Rafael Espindola56f976f2013-04-18 18:08:55 +00001283StringRef MachOObjectFile::getLoadName() const {
1284 // TODO: Implement
Charles Davis1827bd82013-08-27 05:38:30 +00001285 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001286}
1287
Rui Ueyamabc654b12013-09-27 21:47:05 +00001288relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001289 DataRefImpl DRI;
1290 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001291 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001292}
1293
Rui Ueyamabc654b12013-09-27 21:47:05 +00001294relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001295 DataRefImpl DRI;
1296 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001297 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001298}
1299
Kevin Enderby273ae012013-06-06 17:20:50 +00001300dice_iterator MachOObjectFile::begin_dices() const {
1301 DataRefImpl DRI;
1302 if (!DataInCodeLoadCmd)
1303 return dice_iterator(DiceRef(DRI, this));
1304
Charles Davis8bdfafd2013-09-01 04:28:48 +00001305 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1306 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00001307 return dice_iterator(DiceRef(DRI, this));
1308}
1309
1310dice_iterator MachOObjectFile::end_dices() const {
1311 DataRefImpl DRI;
1312 if (!DataInCodeLoadCmd)
1313 return dice_iterator(DiceRef(DRI, this));
1314
Charles Davis8bdfafd2013-09-01 04:28:48 +00001315 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1316 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00001317 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1318 return dice_iterator(DiceRef(DRI, this));
1319}
1320
Rafael Espindola56f976f2013-04-18 18:08:55 +00001321StringRef
1322MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1323 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1324 return parseSegmentOrSectionName(Raw.data());
1325}
1326
1327ArrayRef<char>
1328MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001329 const section_base *Base =
1330 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1331 return ArrayRef<char>(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001332}
1333
1334ArrayRef<char>
1335MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001336 const section_base *Base =
1337 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1338 return ArrayRef<char>(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001339}
1340
1341bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00001342MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001343 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001344 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001345 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001346 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001347}
1348
Eric Christopher1d62c252013-07-22 22:25:07 +00001349unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001350 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001351 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001352 return RE.r_word1 & 0xffffff;
1353 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001354}
1355
Eric Christopher1d62c252013-07-22 22:25:07 +00001356bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001357 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001358 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001359 return (RE.r_word1 >> 27) & 1;
1360 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001361}
1362
Eric Christopher1d62c252013-07-22 22:25:07 +00001363bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001364 const MachO::any_relocation_info &RE) const {
1365 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001366}
1367
Eric Christopher1d62c252013-07-22 22:25:07 +00001368uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001369 const MachO::any_relocation_info &RE) const {
1370 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001371}
1372
Eric Christopher1d62c252013-07-22 22:25:07 +00001373unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001374 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001375 if (isRelocationScattered(RE))
1376 return getScatteredRelocationAddress(RE);
1377 return getPlainRelocationAddress(RE);
1378}
1379
Charles Davis8bdfafd2013-09-01 04:28:48 +00001380unsigned MachOObjectFile::getAnyRelocationPCRel(
1381 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001382 if (isRelocationScattered(RE))
1383 return getScatteredRelocationPCRel(this, RE);
1384 return getPlainRelocationPCRel(this, RE);
1385}
1386
Eric Christopher1d62c252013-07-22 22:25:07 +00001387unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001388 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001389 if (isRelocationScattered(RE))
1390 return getScatteredRelocationLength(RE);
1391 return getPlainRelocationLength(this, RE);
1392}
1393
1394unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00001395MachOObjectFile::getAnyRelocationType(
1396 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001397 if (isRelocationScattered(RE))
1398 return getScatteredRelocationType(RE);
1399 return getPlainRelocationType(this, RE);
1400}
1401
Rafael Espindola52501032013-04-30 15:40:54 +00001402SectionRef
Charles Davis8bdfafd2013-09-01 04:28:48 +00001403MachOObjectFile::getRelocationSection(
1404 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00001405 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1406 return *end_sections();
1407 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1408 DataRefImpl DRI;
1409 DRI.d.a = SecNum;
1410 return SectionRef(DRI, this);
1411}
1412
Rafael Espindola56f976f2013-04-18 18:08:55 +00001413MachOObjectFile::LoadCommandInfo
1414MachOObjectFile::getFirstLoadCommandInfo() const {
1415 MachOObjectFile::LoadCommandInfo Load;
1416
Charles Davis8bdfafd2013-09-01 04:28:48 +00001417 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1418 sizeof(MachO::mach_header);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001419 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001420 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001421 return Load;
1422}
1423
1424MachOObjectFile::LoadCommandInfo
1425MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1426 MachOObjectFile::LoadCommandInfo Next;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001427 Next.Ptr = L.Ptr + L.C.cmdsize;
1428 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001429 return Next;
1430}
1431
Charles Davis8bdfafd2013-09-01 04:28:48 +00001432MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1433 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001434}
1435
Charles Davis8bdfafd2013-09-01 04:28:48 +00001436MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1437 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001438}
1439
Charles Davis8bdfafd2013-09-01 04:28:48 +00001440MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00001441 unsigned Index) const {
1442 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001443 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001444}
1445
Charles Davis8bdfafd2013-09-01 04:28:48 +00001446MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1447 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001448 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001449 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001450}
1451
Charles Davis8bdfafd2013-09-01 04:28:48 +00001452MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00001453MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001454 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001455 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001456}
1457
Charles Davis8bdfafd2013-09-01 04:28:48 +00001458MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00001459MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001460 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001461 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001462}
1463
Charles Davis8bdfafd2013-09-01 04:28:48 +00001464MachO::linkedit_data_command
1465MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1466 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001467}
1468
Charles Davis8bdfafd2013-09-01 04:28:48 +00001469MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001470MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001471 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001472}
1473
Charles Davis8bdfafd2013-09-01 04:28:48 +00001474MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00001475MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001476 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001477}
1478
Charles Davis8bdfafd2013-09-01 04:28:48 +00001479MachO::linker_options_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001480MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001481 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001482}
1483
Charles Davis8bdfafd2013-09-01 04:28:48 +00001484MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001485MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +00001486 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001487 return getStruct<MachO::any_relocation_info>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001488}
1489
Charles Davis8bdfafd2013-09-01 04:28:48 +00001490MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00001491MachOObjectFile::getDice(DataRefImpl Rel) const {
1492 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001493 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00001494}
1495
Charles Davis8bdfafd2013-09-01 04:28:48 +00001496MachO::mach_header MachOObjectFile::getHeader() const {
1497 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001498}
1499
Charles Davis8bdfafd2013-09-01 04:28:48 +00001500MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1501 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001502}
1503
Charles Davis8bdfafd2013-09-01 04:28:48 +00001504uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1505 const MachO::dysymtab_command &DLC,
1506 unsigned Index) const {
1507 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1508 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001509}
1510
Charles Davis8bdfafd2013-09-01 04:28:48 +00001511MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00001512MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1513 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001514 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1515 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001516}
1517
Charles Davis8bdfafd2013-09-01 04:28:48 +00001518MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1519 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001520}
1521
Charles Davis8bdfafd2013-09-01 04:28:48 +00001522MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1523 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001524}
1525
Charles Davis8bdfafd2013-09-01 04:28:48 +00001526MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00001527MachOObjectFile::getDataInCodeLoadCommand() const {
1528 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00001529 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00001530
1531 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001532 MachO::linkedit_data_command Cmd;
1533 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1534 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1535 Cmd.dataoff = 0;
1536 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00001537 return Cmd;
1538}
1539
Rafael Espindola6e040c02013-04-26 20:07:33 +00001540StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001541 MachO::symtab_command S = getSymtabLoadCommand();
1542 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001543}
1544
Rafael Espindola56f976f2013-04-18 18:08:55 +00001545bool MachOObjectFile::is64Bit() const {
1546 return getType() == getMachOType(false, true) ||
1547 getType() == getMachOType(true, true);
1548}
1549
1550void MachOObjectFile::ReadULEB128s(uint64_t Index,
1551 SmallVectorImpl<uint64_t> &Out) const {
1552 DataExtractor extractor(ObjectFile::getData(), true, 0);
1553
1554 uint32_t offset = Index;
1555 uint64_t data = 0;
1556 while (uint64_t delta = extractor.getULEB128(&offset)) {
1557 data += delta;
1558 Out.push_back(data);
1559 }
1560}
1561
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001562ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer,
1563 bool BufferOwned) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001564 StringRef Magic = Buffer->getBuffer().slice(0, 4);
Rafael Espindola692410e2014-01-21 23:06:54 +00001565 error_code EC;
1566 OwningPtr<MachOObjectFile> Ret;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001567 if (Magic == "\xFE\xED\xFA\xCE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001568 Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001569 else if (Magic == "\xCE\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001570 Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001571 else if (Magic == "\xFE\xED\xFA\xCF")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001572 Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001573 else if (Magic == "\xCF\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001574 Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned));
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001575 else {
1576 delete Buffer;
Rafael Espindola692410e2014-01-21 23:06:54 +00001577 return object_error::parse_failed;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001578 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001579
Rafael Espindola692410e2014-01-21 23:06:54 +00001580 if (EC)
1581 return EC;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001582 return Ret.take();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001583}
1584
Owen Anderson27c579d2011-10-11 17:32:27 +00001585} // end namespace object
Eric Christopher7b015c72011-04-22 03:19:48 +00001586} // end namespace llvm