blob: 1fb0c13407344e6d23325654f0baefe8ffb74a9f [file] [log] [blame]
Eric Christopher7b015c72011-04-22 03:19:48 +00001//===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the MachOObjectFile class, which binds the MachOObject
11// class to the generic ObjectFile wrapper.
12//
13//===----------------------------------------------------------------------===//
14
Owen Anderson27c579d2011-10-11 17:32:27 +000015#include "llvm/Object/MachO.h"
Tim Northover00ed9962014-03-29 10:18:08 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Triple.h"
Rafael Espindola421305a2013-04-07 20:01:29 +000018#include "llvm/Support/DataExtractor.h"
Owen Andersonbc14bd32011-10-26 20:42:54 +000019#include "llvm/Support/Format.h"
Rafael Espindola56f976f2013-04-18 18:08:55 +000020#include "llvm/Support/Host.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000021#include "llvm/Support/MemoryBuffer.h"
Jakub Staszak84a0ae72013-08-21 01:20:11 +000022#include "llvm/Support/raw_ostream.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000023#include <cctype>
24#include <cstring>
25#include <limits>
26
27using namespace llvm;
28using namespace object;
29
30namespace llvm {
Owen Anderson27c579d2011-10-11 17:32:27 +000031namespace object {
Eric Christopher7b015c72011-04-22 03:19:48 +000032
Charles Davis8bdfafd2013-09-01 04:28:48 +000033struct nlist_base {
34 uint32_t n_strx;
35 uint8_t n_type;
36 uint8_t n_sect;
37 uint16_t n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +000038};
39
Charles Davis8bdfafd2013-09-01 04:28:48 +000040struct section_base {
41 char sectname[16];
42 char segname[16];
Rafael Espindola56f976f2013-04-18 18:08:55 +000043};
44
45template<typename T>
46static void SwapValue(T &Value) {
47 Value = sys::SwapByteOrder(Value);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +000048}
49
Rafael Espindola56f976f2013-04-18 18:08:55 +000050template<typename T>
51static void SwapStruct(T &Value);
52
53template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000054void SwapStruct(MachO::any_relocation_info &H) {
55 SwapValue(H.r_word0);
56 SwapValue(H.r_word1);
Rafael Espindola5ffc0792013-04-07 16:07:35 +000057}
58
Rafael Espindola56f976f2013-04-18 18:08:55 +000059template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000060void SwapStruct(MachO::load_command &L) {
61 SwapValue(L.cmd);
62 SwapValue(L.cmdsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +000063}
Rafael Espindola421305a2013-04-07 20:01:29 +000064
Rafael Espindola56f976f2013-04-18 18:08:55 +000065template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000066void SwapStruct(nlist_base &S) {
67 SwapValue(S.n_strx);
68 SwapValue(S.n_desc);
Rafael Espindola56f976f2013-04-18 18:08:55 +000069}
70
71template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000072void SwapStruct(MachO::section &S) {
73 SwapValue(S.addr);
74 SwapValue(S.size);
75 SwapValue(S.offset);
76 SwapValue(S.align);
77 SwapValue(S.reloff);
78 SwapValue(S.nreloc);
79 SwapValue(S.flags);
80 SwapValue(S.reserved1);
81 SwapValue(S.reserved2);
Rafael Espindola56f976f2013-04-18 18:08:55 +000082}
83
84template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000085void SwapStruct(MachO::section_64 &S) {
86 SwapValue(S.addr);
87 SwapValue(S.size);
88 SwapValue(S.offset);
89 SwapValue(S.align);
90 SwapValue(S.reloff);
91 SwapValue(S.nreloc);
92 SwapValue(S.flags);
93 SwapValue(S.reserved1);
94 SwapValue(S.reserved2);
95 SwapValue(S.reserved3);
Rafael Espindola56f976f2013-04-18 18:08:55 +000096}
97
98template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000099void SwapStruct(MachO::nlist &S) {
100 SwapValue(S.n_strx);
101 SwapValue(S.n_desc);
102 SwapValue(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000103}
104
105template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000106void SwapStruct(MachO::nlist_64 &S) {
107 SwapValue(S.n_strx);
108 SwapValue(S.n_desc);
109 SwapValue(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000110}
111
112template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000113void SwapStruct(MachO::mach_header &H) {
114 SwapValue(H.magic);
115 SwapValue(H.cputype);
116 SwapValue(H.cpusubtype);
117 SwapValue(H.filetype);
118 SwapValue(H.ncmds);
119 SwapValue(H.sizeofcmds);
120 SwapValue(H.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000121}
122
123template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000124void SwapStruct(MachO::mach_header_64 &H) {
125 SwapValue(H.magic);
126 SwapValue(H.cputype);
127 SwapValue(H.cpusubtype);
128 SwapValue(H.filetype);
129 SwapValue(H.ncmds);
130 SwapValue(H.sizeofcmds);
131 SwapValue(H.flags);
132 SwapValue(H.reserved);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000133}
134
135template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000136void SwapStruct(MachO::symtab_command &C) {
137 SwapValue(C.cmd);
138 SwapValue(C.cmdsize);
139 SwapValue(C.symoff);
140 SwapValue(C.nsyms);
141 SwapValue(C.stroff);
142 SwapValue(C.strsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000143}
144
145template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000146void SwapStruct(MachO::dysymtab_command &C) {
147 SwapValue(C.cmd);
148 SwapValue(C.cmdsize);
149 SwapValue(C.ilocalsym);
150 SwapValue(C.nlocalsym);
151 SwapValue(C.iextdefsym);
152 SwapValue(C.nextdefsym);
153 SwapValue(C.iundefsym);
154 SwapValue(C.nundefsym);
155 SwapValue(C.tocoff);
156 SwapValue(C.ntoc);
157 SwapValue(C.modtaboff);
158 SwapValue(C.nmodtab);
159 SwapValue(C.extrefsymoff);
160 SwapValue(C.nextrefsyms);
161 SwapValue(C.indirectsymoff);
162 SwapValue(C.nindirectsyms);
163 SwapValue(C.extreloff);
164 SwapValue(C.nextrel);
165 SwapValue(C.locreloff);
166 SwapValue(C.nlocrel);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000167}
168
169template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000170void SwapStruct(MachO::linkedit_data_command &C) {
171 SwapValue(C.cmd);
172 SwapValue(C.cmdsize);
173 SwapValue(C.dataoff);
174 SwapValue(C.datasize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000175}
176
177template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000178void SwapStruct(MachO::segment_command &C) {
179 SwapValue(C.cmd);
180 SwapValue(C.cmdsize);
181 SwapValue(C.vmaddr);
182 SwapValue(C.vmsize);
183 SwapValue(C.fileoff);
184 SwapValue(C.filesize);
185 SwapValue(C.maxprot);
186 SwapValue(C.initprot);
187 SwapValue(C.nsects);
188 SwapValue(C.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000189}
190
191template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000192void SwapStruct(MachO::segment_command_64 &C) {
193 SwapValue(C.cmd);
194 SwapValue(C.cmdsize);
195 SwapValue(C.vmaddr);
196 SwapValue(C.vmsize);
197 SwapValue(C.fileoff);
198 SwapValue(C.filesize);
199 SwapValue(C.maxprot);
200 SwapValue(C.initprot);
201 SwapValue(C.nsects);
202 SwapValue(C.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000203}
204
Rafael Espindola6e040c02013-04-26 20:07:33 +0000205template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000206void SwapStruct(uint32_t &C) {
207 SwapValue(C);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000208}
209
210template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000211void SwapStruct(MachO::linker_options_command &C) {
212 SwapValue(C.cmd);
213 SwapValue(C.cmdsize);
214 SwapValue(C.count);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000215}
216
217template<>
Jim Grosbach448334a2014-03-18 22:09:05 +0000218void SwapStruct(MachO::version_min_command&C) {
219 SwapValue(C.cmd);
220 SwapValue(C.cmdsize);
221 SwapValue(C.version);
222 SwapValue(C.reserved);
223}
224
225template<>
Kevin Enderby980b2582014-06-05 21:21:57 +0000226void SwapStruct(MachO::dylib_command&C) {
227 SwapValue(C.cmd);
228 SwapValue(C.cmdsize);
229 SwapValue(C.dylib.name);
230 SwapValue(C.dylib.timestamp);
231 SwapValue(C.dylib.current_version);
232 SwapValue(C.dylib.compatibility_version);
233}
234
235template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000236void SwapStruct(MachO::data_in_code_entry &C) {
237 SwapValue(C.offset);
238 SwapValue(C.length);
239 SwapValue(C.kind);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000240}
241
Rafael Espindola3cdeb172013-04-19 13:45:05 +0000242template<typename T>
243T getStruct(const MachOObjectFile *O, const char *P) {
244 T Cmd;
245 memcpy(&Cmd, P, sizeof(T));
246 if (O->isLittleEndian() != sys::IsLittleEndianHost)
247 SwapStruct(Cmd);
248 return Cmd;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000249}
250
Rafael Espindola56f976f2013-04-18 18:08:55 +0000251static uint32_t
252getSegmentLoadCommandNumSections(const MachOObjectFile *O,
253 const MachOObjectFile::LoadCommandInfo &L) {
254 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000255 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
256 return S.nsects;
Rafael Espindola421305a2013-04-07 20:01:29 +0000257 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000258 MachO::segment_command S = O->getSegmentLoadCommand(L);
259 return S.nsects;
Rafael Espindola5ffc0792013-04-07 16:07:35 +0000260}
261
Rafael Espindola6e040c02013-04-26 20:07:33 +0000262static const char *
263getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
264 unsigned Sec) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000265 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
266
267 bool Is64 = O->is64Bit();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000268 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
269 sizeof(MachO::segment_command);
270 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
271 sizeof(MachO::section);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000272
273 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
Charles Davis1827bd82013-08-27 05:38:30 +0000274 return reinterpret_cast<const char*>(SectionAddr);
Rafael Espindola60689982013-04-07 19:05:30 +0000275}
276
Rafael Espindola56f976f2013-04-18 18:08:55 +0000277static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
278 return O->getData().substr(Offset, 1).data();
Rafael Espindola60689982013-04-07 19:05:30 +0000279}
280
Charles Davis8bdfafd2013-09-01 04:28:48 +0000281static nlist_base
Rafael Espindola56f976f2013-04-18 18:08:55 +0000282getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola75c30362013-04-24 19:47:55 +0000283 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000284 return getStruct<nlist_base>(O, P);
Eric Christopher7b015c72011-04-22 03:19:48 +0000285}
286
Rafael Espindola56f976f2013-04-18 18:08:55 +0000287static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000288 if (P[15] == 0)
289 // Null terminated.
290 return P;
291 // Not null terminated, so this is a 16 char string.
292 return StringRef(P, 16);
293}
294
Rafael Espindola56f976f2013-04-18 18:08:55 +0000295// Helper to advance a section or symbol iterator multiple increments at a time.
296template<class T>
Rafael Espindola5e812af2014-01-30 02:49:50 +0000297static void advance(T &it, size_t Val) {
298 while (Val--)
299 ++it;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000300}
301
302static unsigned getCPUType(const MachOObjectFile *O) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000303 return O->getHeader().cputype;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000304}
305
306static void printRelocationTargetName(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000307 const MachO::any_relocation_info &RE,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000308 raw_string_ostream &fmt) {
309 bool IsScattered = O->isRelocationScattered(RE);
310
311 // Target of a scattered relocation is an address. In the interest of
312 // generating pretty output, scan through the symbol table looking for a
313 // symbol that aligns with that address. If we find one, print it.
314 // Otherwise, we just print the hex address of the target.
315 if (IsScattered) {
316 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
317
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000318 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000319 error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000320 uint64_t Addr;
321 StringRef Name;
322
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000323 if ((ec = Symbol.getAddress(Addr)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000324 report_fatal_error(ec.message());
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000325 if (Addr != Val)
326 continue;
327 if ((ec = Symbol.getName(Name)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000328 report_fatal_error(ec.message());
329 fmt << Name;
330 return;
331 }
332
333 // If we couldn't find a symbol that this relocation refers to, try
334 // to find a section beginning instead.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000335 for (const SectionRef &Section : O->sections()) {
Rafael Espindola5e812af2014-01-30 02:49:50 +0000336 error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000337 uint64_t Addr;
338 StringRef Name;
339
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000340 if ((ec = Section.getAddress(Addr)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000341 report_fatal_error(ec.message());
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000342 if (Addr != Val)
343 continue;
344 if ((ec = Section.getName(Name)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000345 report_fatal_error(ec.message());
346 fmt << Name;
347 return;
348 }
349
350 fmt << format("0x%x", Val);
351 return;
352 }
353
354 StringRef S;
355 bool isExtern = O->getPlainRelocationExternal(RE);
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000356 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000357
358 if (isExtern) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000359 symbol_iterator SI = O->symbol_begin();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000360 advance(SI, Val);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000361 SI->getName(S);
362 } else {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000363 section_iterator SI = O->section_begin();
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000364 // Adjust for the fact that sections are 1-indexed.
Rafael Espindola5e812af2014-01-30 02:49:50 +0000365 advance(SI, Val - 1);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000366 SI->getName(S);
367 }
368
369 fmt << S;
370}
371
Charles Davis8bdfafd2013-09-01 04:28:48 +0000372static uint32_t
373getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
374 return RE.r_word0;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000375}
376
377static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000378getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
379 return RE.r_word0 & 0xffffff;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000380}
381
382static bool getPlainRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000383 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000384 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000385 return (RE.r_word1 >> 24) & 1;
386 return (RE.r_word1 >> 7) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000387}
388
389static bool
390getScatteredRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000391 const MachO::any_relocation_info &RE) {
392 return (RE.r_word0 >> 30) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000393}
394
395static unsigned getPlainRelocationLength(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000396 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000397 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000398 return (RE.r_word1 >> 25) & 3;
399 return (RE.r_word1 >> 5) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000400}
401
402static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000403getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
404 return (RE.r_word0 >> 28) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000405}
406
407static unsigned getPlainRelocationType(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000408 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000409 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000410 return RE.r_word1 >> 28;
411 return RE.r_word1 & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000412}
413
Charles Davis8bdfafd2013-09-01 04:28:48 +0000414static unsigned
415getScatteredRelocationType(const MachO::any_relocation_info &RE) {
416 return (RE.r_word0 >> 24) & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000417}
418
419static uint32_t getSectionFlags(const MachOObjectFile *O,
420 DataRefImpl Sec) {
421 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000422 MachO::section_64 Sect = O->getSection64(Sec);
423 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000424 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000425 MachO::section Sect = O->getSection(Sec);
426 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000427}
428
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000429MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
430 bool Is64bits, error_code &EC,
431 bool BufferOwned)
432 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned),
Craig Topper2617dcc2014-04-15 06:32:26 +0000433 SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
434 DataInCodeLoadCmd(nullptr) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000435 uint32_t LoadCommandCount = this->getHeader().ncmds;
436 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
437 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000438
439 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000440 for (unsigned I = 0; ; ++I) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000441 if (Load.C.cmd == MachO::LC_SYMTAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000442 assert(!SymtabLoadCmd && "Multiple symbol tables");
443 SymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000444 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000445 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
446 DysymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000447 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000448 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
449 DataInCodeLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000450 } else if (Load.C.cmd == SegmentLoadType) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000451 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
452 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000453 const char *Sec = getSectionPtr(this, Load, J);
454 Sections.push_back(Sec);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000455 }
Kevin Enderby980b2582014-06-05 21:21:57 +0000456 } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB ||
457 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
458 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
459 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
460 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
461 Libraries.push_back(Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000462 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000463
464 if (I == LoadCommandCount - 1)
465 break;
466 else
467 Load = getNextLoadCommandInfo(Load);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000468 }
469}
470
Rafael Espindola5e812af2014-01-30 02:49:50 +0000471void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
Rafael Espindola75c30362013-04-24 19:47:55 +0000472 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000473 sizeof(MachO::nlist_64) :
474 sizeof(MachO::nlist);
Rafael Espindola75c30362013-04-24 19:47:55 +0000475 Symb.p += SymbolTableEntrySize;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000476}
477
478error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
479 StringRef &Res) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000480 StringRef StringTable = getStringTableData();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000481 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
482 const char *Start = &StringTable.data()[Entry.n_strx];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000483 Res = StringRef(Start);
484 return object_error::success;
485}
486
Kevin Enderby980b2582014-06-05 21:21:57 +0000487// getIndirectName() returns the name of the alias'ed symbol who's string table
488// index is in the n_value field.
489error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
490 StringRef &Res) const {
491 StringRef StringTable = getStringTableData();
492 uint64_t NValue;
493 if (is64Bit()) {
494 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
495 NValue = Entry.n_value;
496 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
497 return object_error::parse_failed;
498 } else {
499 MachO::nlist Entry = getSymbolTableEntry(Symb);
500 NValue = Entry.n_value;
501 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
502 return object_error::parse_failed;
503 }
504 if (NValue >= StringTable.size())
505 return object_error::parse_failed;
506 const char *Start = &StringTable.data()[NValue];
507 Res = StringRef(Start);
508 return object_error::success;
509}
510
Rafael Espindola56f976f2013-04-18 18:08:55 +0000511error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
512 uint64_t &Res) const {
513 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000514 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000515 if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
516 Entry.n_value == 0)
517 Res = UnknownAddressOrSize;
518 else
519 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000520 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000521 MachO::nlist Entry = getSymbolTableEntry(Symb);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000522 if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
523 Entry.n_value == 0)
524 Res = UnknownAddressOrSize;
525 else
526 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000527 }
528 return object_error::success;
529}
530
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000531error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
532 uint32_t &Result) const {
Rafael Espindola20122a42014-01-31 20:57:12 +0000533 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000534 if (flags & SymbolRef::SF_Common) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000535 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
536 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000537 } else {
538 Result = 0;
539 }
540 return object_error::success;
541}
542
Rafael Espindola56f976f2013-04-18 18:08:55 +0000543error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
544 uint64_t &Result) const {
545 uint64_t BeginOffset;
546 uint64_t EndOffset = 0;
547 uint8_t SectionIndex;
548
Charles Davis8bdfafd2013-09-01 04:28:48 +0000549 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000550 uint64_t Value;
551 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000552 if (Value == UnknownAddressOrSize) {
553 Result = UnknownAddressOrSize;
554 return object_error::success;
555 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000556
557 BeginOffset = Value;
558
Charles Davis8bdfafd2013-09-01 04:28:48 +0000559 SectionIndex = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000560 if (!SectionIndex) {
Rafael Espindola20122a42014-01-31 20:57:12 +0000561 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000562 if (flags & SymbolRef::SF_Common)
563 Result = Value;
564 else
565 Result = UnknownAddressOrSize;
566 return object_error::success;
567 }
568 // Unfortunately symbols are unsorted so we need to touch all
569 // symbols from load command
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000570 for (const SymbolRef &Symbol : symbols()) {
571 DataRefImpl DRI = Symbol.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000572 Entry = getSymbolTableEntryBase(this, DRI);
573 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000574 if (Value == UnknownAddressOrSize)
575 continue;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000576 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000577 if (!EndOffset || Value < EndOffset)
578 EndOffset = Value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000579 }
580 if (!EndOffset) {
581 uint64_t Size;
582 DataRefImpl Sec;
583 Sec.d.a = SectionIndex-1;
584 getSectionSize(Sec, Size);
585 getSectionAddress(Sec, EndOffset);
586 EndOffset += Size;
587 }
588 Result = EndOffset - BeginOffset;
589 return object_error::success;
590}
591
592error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
593 SymbolRef::Type &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000594 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
595 uint8_t n_type = Entry.n_type;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000596
597 Res = SymbolRef::ST_Other;
598
599 // If this is a STAB debugging symbol, we can do nothing more.
Charles Davis74ec8b02013-08-27 05:00:13 +0000600 if (n_type & MachO::N_STAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000601 Res = SymbolRef::ST_Debug;
602 return object_error::success;
603 }
604
Charles Davis74ec8b02013-08-27 05:00:13 +0000605 switch (n_type & MachO::N_TYPE) {
606 case MachO::N_UNDF :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000607 Res = SymbolRef::ST_Unknown;
608 break;
Charles Davis74ec8b02013-08-27 05:00:13 +0000609 case MachO::N_SECT :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000610 Res = SymbolRef::ST_Function;
611 break;
612 }
613 return object_error::success;
614}
615
Rafael Espindola20122a42014-01-31 20:57:12 +0000616uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000617 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000618
Charles Davis8bdfafd2013-09-01 04:28:48 +0000619 uint8_t MachOType = Entry.n_type;
620 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000621
Rafael Espindola20122a42014-01-31 20:57:12 +0000622 uint32_t Result = SymbolRef::SF_None;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000623
Charles Davis74ec8b02013-08-27 05:00:13 +0000624 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000625 Result |= SymbolRef::SF_Undefined;
626
Tim Northovereaef0742014-05-30 13:22:59 +0000627 if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
628 Result |= SymbolRef::SF_Indirect;
629
Rafael Espindolaa1356322013-11-02 05:03:24 +0000630 if (MachOType & MachO::N_STAB)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000631 Result |= SymbolRef::SF_FormatSpecific;
632
Charles Davis74ec8b02013-08-27 05:00:13 +0000633 if (MachOType & MachO::N_EXT) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000634 Result |= SymbolRef::SF_Global;
Charles Davis74ec8b02013-08-27 05:00:13 +0000635 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000636 uint64_t Value;
637 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000638 if (Value && Value != UnknownAddressOrSize)
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000639 Result |= SymbolRef::SF_Common;
640 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000641 }
642
Charles Davis74ec8b02013-08-27 05:00:13 +0000643 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000644 Result |= SymbolRef::SF_Weak;
645
Charles Davis74ec8b02013-08-27 05:00:13 +0000646 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000647 Result |= SymbolRef::SF_Absolute;
648
Rafael Espindola20122a42014-01-31 20:57:12 +0000649 return Result;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000650}
651
652error_code
653MachOObjectFile::getSymbolSection(DataRefImpl Symb,
654 section_iterator &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000655 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
656 uint8_t index = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000657
658 if (index == 0) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000659 Res = section_end();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000660 } else {
661 DataRefImpl DRI;
662 DRI.d.a = index - 1;
663 Res = section_iterator(SectionRef(DRI, this));
664 }
665
666 return object_error::success;
667}
668
Rafael Espindola5e812af2014-01-30 02:49:50 +0000669void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000670 Sec.d.a++;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000671}
672
673error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000674MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000675 ArrayRef<char> Raw = getSectionRawName(Sec);
676 Result = parseSegmentOrSectionName(Raw.data());
677 return object_error::success;
678}
679
680error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000681MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000682 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000683 MachO::section_64 Sect = getSection64(Sec);
684 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000685 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000686 MachO::section Sect = getSection(Sec);
687 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000688 }
689 return object_error::success;
690}
691
692error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000693MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000694 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000695 MachO::section_64 Sect = getSection64(Sec);
696 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000697 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000698 MachO::section Sect = getSection(Sec);
699 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000700 }
701
702 return object_error::success;
703}
704
705error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000706MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000707 uint32_t Offset;
708 uint64_t Size;
709
710 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000711 MachO::section_64 Sect = getSection64(Sec);
712 Offset = Sect.offset;
713 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000714 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000715 MachO::section Sect = getSection(Sec);
716 Offset = Sect.offset;
717 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000718 }
719
720 Res = this->getData().substr(Offset, Size);
721 return object_error::success;
722}
723
724error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000725MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000726 uint32_t Align;
727 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000728 MachO::section_64 Sect = getSection64(Sec);
729 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000730 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000731 MachO::section Sect = getSection(Sec);
732 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000733 }
734
735 Res = uint64_t(1) << Align;
736 return object_error::success;
737}
738
739error_code
740MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
741 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000742 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000743 return object_error::success;
744}
745
Kevin Enderby403258f2014-05-19 20:36:02 +0000746error_code MachOObjectFile::isSectionData(DataRefImpl Sec, bool &Result) const {
747 uint32_t Flags = getSectionFlags(this, Sec);
748 unsigned SectionType = Flags & MachO::SECTION_TYPE;
749 Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
750 !(SectionType == MachO::S_ZEROFILL ||
751 SectionType == MachO::S_GB_ZEROFILL);
Michael J. Spencer800619f2011-09-28 20:57:30 +0000752 return object_error::success;
753}
754
Kevin Enderby403258f2014-05-19 20:36:02 +0000755error_code MachOObjectFile::isSectionBSS(DataRefImpl Sec, bool &Result) const {
756 uint32_t Flags = getSectionFlags(this, Sec);
757 unsigned SectionType = Flags & MachO::SECTION_TYPE;
758 Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
759 (SectionType == MachO::S_ZEROFILL ||
760 SectionType == MachO::S_GB_ZEROFILL);
Preston Gurd2138ef62012-04-12 20:13:57 +0000761 return object_error::success;
762}
763
Rafael Espindolac2413f52013-04-09 14:49:08 +0000764error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000765MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000766 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000767 // FIXME: Unimplemented.
768 Result = true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000769 return object_error::success;
770}
771
Rafael Espindola56f976f2013-04-18 18:08:55 +0000772error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000773 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000774 // FIXME: Unimplemented.
775 Result = false;
776 return object_error::success;
777}
778
Rafael Espindola56f976f2013-04-18 18:08:55 +0000779error_code
780MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
781 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis74ec8b02013-08-27 05:00:13 +0000782 unsigned SectionType = Flags & MachO::SECTION_TYPE;
783 Res = SectionType == MachO::S_ZEROFILL ||
784 SectionType == MachO::S_GB_ZEROFILL;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000785 return object_error::success;
786}
787
788error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000789 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000790 // Consider using the code from isSectionText to look for __const sections.
791 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
792 // to use section attributes to distinguish code from data.
793
794 // FIXME: Unimplemented.
795 Result = false;
796 return object_error::success;
797}
798
Rafael Espindola56f976f2013-04-18 18:08:55 +0000799error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000800MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000801 bool &Result) const {
802 SymbolRef::Type ST;
803 this->getSymbolType(Symb, ST);
804 if (ST == SymbolRef::ST_Unknown) {
805 Result = false;
806 return object_error::success;
807 }
808
809 uint64_t SectBegin, SectEnd;
810 getSectionAddress(Sec, SectBegin);
811 getSectionSize(Sec, SectEnd);
812 SectEnd += SectBegin;
813
814 uint64_t SymAddr;
815 getSymbolAddress(Symb, SymAddr);
816 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
817
818 return object_error::success;
819}
820
Rui Ueyamabc654b12013-09-27 21:47:05 +0000821relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000822 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +0000823 Ret.d.a = Sec.d.a;
824 Ret.d.b = 0;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000825 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000826}
Rafael Espindolac0406e12013-04-08 20:45:01 +0000827
Rafael Espindola56f976f2013-04-18 18:08:55 +0000828relocation_iterator
Rui Ueyamabc654b12013-09-27 21:47:05 +0000829MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000830 uint32_t Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000831 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000832 MachO::section_64 Sect = getSection64(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000833 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000834 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000835 MachO::section Sect = getSection(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000836 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000837 }
Eric Christopher7b015c72011-04-22 03:19:48 +0000838
Rafael Espindola56f976f2013-04-18 18:08:55 +0000839 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +0000840 Ret.d.a = Sec.d.a;
841 Ret.d.b = Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000842 return relocation_iterator(RelocationRef(Ret, this));
843}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000844
Rafael Espindola5e812af2014-01-30 02:49:50 +0000845void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +0000846 ++Rel.d.b;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000847}
Owen Anderson171f4852011-10-24 23:20:07 +0000848
Rafael Espindola56f976f2013-04-18 18:08:55 +0000849error_code
Rafael Espindola137faa02013-04-24 15:14:22 +0000850MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola72475462014-04-04 00:31:12 +0000851 uint64_t Offset;
852 getRelocationOffset(Rel, Offset);
Rafael Espindola7e91bc92014-04-03 23:54:35 +0000853
854 DataRefImpl Sec;
855 Sec.d.a = Rel.d.a;
856 uint64_t SecAddress;
857 getSectionAddress(Sec, SecAddress);
858 Res = SecAddress + Offset;
859 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000860}
861
Rafael Espindola56f976f2013-04-18 18:08:55 +0000862error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000863 uint64_t &Res) const {
Rafael Espindola72475462014-04-04 00:31:12 +0000864 assert(getHeader().filetype == MachO::MH_OBJECT &&
865 "Only implemented for MH_OBJECT");
Charles Davis8bdfafd2013-09-01 04:28:48 +0000866 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000867 Res = getAnyRelocationAddress(RE);
868 return object_error::success;
David Meyer2fc34c52012-03-01 01:36:50 +0000869}
870
Rafael Espindola806f0062013-06-05 01:33:53 +0000871symbol_iterator
872MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000873 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000874 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
875 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola806f0062013-06-05 01:33:53 +0000876 if (!isExtern)
Rafael Espindolab5155a52014-02-10 20:24:04 +0000877 return symbol_end();
Rafael Espindola75c30362013-04-24 19:47:55 +0000878
Charles Davis8bdfafd2013-09-01 04:28:48 +0000879 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +0000880 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000881 sizeof(MachO::nlist_64) :
882 sizeof(MachO::nlist);
883 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +0000884 DataRefImpl Sym;
885 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola806f0062013-06-05 01:33:53 +0000886 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +0000887}
888
889error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +0000890 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000891 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000892 Res = getAnyRelocationType(RE);
893 return object_error::success;
894}
895
896error_code
897MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
898 SmallVectorImpl<char> &Result) const {
899 StringRef res;
900 uint64_t RType;
901 getRelocationType(Rel, RType);
902
903 unsigned Arch = this->getArch();
904
905 switch (Arch) {
906 case Triple::x86: {
907 static const char *const Table[] = {
908 "GENERIC_RELOC_VANILLA",
909 "GENERIC_RELOC_PAIR",
910 "GENERIC_RELOC_SECTDIFF",
911 "GENERIC_RELOC_PB_LA_PTR",
912 "GENERIC_RELOC_LOCAL_SECTDIFF",
913 "GENERIC_RELOC_TLV" };
914
Eric Christopher13250cb2013-12-06 02:33:38 +0000915 if (RType > 5)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000916 res = "Unknown";
917 else
918 res = Table[RType];
919 break;
920 }
921 case Triple::x86_64: {
922 static const char *const Table[] = {
923 "X86_64_RELOC_UNSIGNED",
924 "X86_64_RELOC_SIGNED",
925 "X86_64_RELOC_BRANCH",
926 "X86_64_RELOC_GOT_LOAD",
927 "X86_64_RELOC_GOT",
928 "X86_64_RELOC_SUBTRACTOR",
929 "X86_64_RELOC_SIGNED_1",
930 "X86_64_RELOC_SIGNED_2",
931 "X86_64_RELOC_SIGNED_4",
932 "X86_64_RELOC_TLV" };
933
934 if (RType > 9)
935 res = "Unknown";
936 else
937 res = Table[RType];
938 break;
939 }
940 case Triple::arm: {
941 static const char *const Table[] = {
942 "ARM_RELOC_VANILLA",
943 "ARM_RELOC_PAIR",
944 "ARM_RELOC_SECTDIFF",
945 "ARM_RELOC_LOCAL_SECTDIFF",
946 "ARM_RELOC_PB_LA_PTR",
947 "ARM_RELOC_BR24",
948 "ARM_THUMB_RELOC_BR22",
949 "ARM_THUMB_32BIT_BRANCH",
950 "ARM_RELOC_HALF",
951 "ARM_RELOC_HALF_SECTDIFF" };
952
953 if (RType > 9)
954 res = "Unknown";
955 else
956 res = Table[RType];
957 break;
958 }
Tim Northover00ed9962014-03-29 10:18:08 +0000959 case Triple::arm64:
960 case Triple::aarch64: {
961 static const char *const Table[] = {
962 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
963 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
964 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
965 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
966 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
967 "ARM64_RELOC_ADDEND"
968 };
969
970 if (RType >= array_lengthof(Table))
971 res = "Unknown";
972 else
973 res = Table[RType];
974 break;
975 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000976 case Triple::ppc: {
977 static const char *const Table[] = {
978 "PPC_RELOC_VANILLA",
979 "PPC_RELOC_PAIR",
980 "PPC_RELOC_BR14",
981 "PPC_RELOC_BR24",
982 "PPC_RELOC_HI16",
983 "PPC_RELOC_LO16",
984 "PPC_RELOC_HA16",
985 "PPC_RELOC_LO14",
986 "PPC_RELOC_SECTDIFF",
987 "PPC_RELOC_PB_LA_PTR",
988 "PPC_RELOC_HI16_SECTDIFF",
989 "PPC_RELOC_LO16_SECTDIFF",
990 "PPC_RELOC_HA16_SECTDIFF",
991 "PPC_RELOC_JBSR",
992 "PPC_RELOC_LO14_SECTDIFF",
993 "PPC_RELOC_LOCAL_SECTDIFF" };
994
Eric Christopher13250cb2013-12-06 02:33:38 +0000995 if (RType > 15)
996 res = "Unknown";
997 else
998 res = Table[RType];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000999 break;
1000 }
1001 case Triple::UnknownArch:
1002 res = "Unknown";
1003 break;
1004 }
1005 Result.append(res.begin(), res.end());
1006 return object_error::success;
1007}
1008
Rafael Espindola56f976f2013-04-18 18:08:55 +00001009error_code
1010MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +00001011 SmallVectorImpl<char> &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001012 MachO::any_relocation_info RE = getRelocation(Rel);
David Meyer2fc34c52012-03-01 01:36:50 +00001013
Rafael Espindola56f976f2013-04-18 18:08:55 +00001014 unsigned Arch = this->getArch();
Eric Christopher7b015c72011-04-22 03:19:48 +00001015
Rafael Espindola56f976f2013-04-18 18:08:55 +00001016 std::string fmtbuf;
1017 raw_string_ostream fmt(fmtbuf);
1018 unsigned Type = this->getAnyRelocationType(RE);
1019 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1020
1021 // Determine any addends that should be displayed with the relocation.
1022 // These require decoding the relocation type, which is triple-specific.
1023
1024 // X86_64 has entirely custom relocation types.
1025 if (Arch == Triple::x86_64) {
1026 bool isPCRel = getAnyRelocationPCRel(RE);
1027
1028 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001029 case MachO::X86_64_RELOC_GOT_LOAD:
1030 case MachO::X86_64_RELOC_GOT: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001031 printRelocationTargetName(this, RE, fmt);
1032 fmt << "@GOT";
1033 if (isPCRel) fmt << "PCREL";
1034 break;
1035 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001036 case MachO::X86_64_RELOC_SUBTRACTOR: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001037 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001038 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001039 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001040
Charles Davis8bdfafd2013-09-01 04:28:48 +00001041 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
Rafael Espindola56f976f2013-04-18 18:08:55 +00001042 // X86_64_RELOC_UNSIGNED.
1043 // NOTE: Scattered relocations don't exist on x86_64.
1044 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001045 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001046 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1047 "X86_64_RELOC_SUBTRACTOR.");
1048
Charles Davis8bdfafd2013-09-01 04:28:48 +00001049 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1050 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
Rafael Espindola56f976f2013-04-18 18:08:55 +00001051 printRelocationTargetName(this, RENext, fmt);
1052 fmt << "-";
1053 printRelocationTargetName(this, RE, fmt);
1054 break;
1055 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001056 case MachO::X86_64_RELOC_TLV:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001057 printRelocationTargetName(this, RE, fmt);
1058 fmt << "@TLV";
1059 if (isPCRel) fmt << "P";
1060 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001061 case MachO::X86_64_RELOC_SIGNED_1:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001062 printRelocationTargetName(this, RE, fmt);
1063 fmt << "-1";
1064 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001065 case MachO::X86_64_RELOC_SIGNED_2:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001066 printRelocationTargetName(this, RE, fmt);
1067 fmt << "-2";
1068 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001069 case MachO::X86_64_RELOC_SIGNED_4:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001070 printRelocationTargetName(this, RE, fmt);
1071 fmt << "-4";
1072 break;
1073 default:
1074 printRelocationTargetName(this, RE, fmt);
1075 break;
1076 }
1077 // X86 and ARM share some relocation types in common.
David Fangb88cdf62013-08-08 20:14:40 +00001078 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1079 Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001080 // Generic relocation types...
1081 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001082 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001083 return object_error::success;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001084 case MachO::GENERIC_RELOC_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001085 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001086 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001087 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001088
1089 // X86 sect diff's must be followed by a relocation of type
1090 // GENERIC_RELOC_PAIR.
1091 unsigned RType = getAnyRelocationType(RENext);
1092
Charles Davis8bdfafd2013-09-01 04:28:48 +00001093 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001094 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1095 "GENERIC_RELOC_SECTDIFF.");
1096
1097 printRelocationTargetName(this, RE, fmt);
1098 fmt << "-";
1099 printRelocationTargetName(this, RENext, fmt);
1100 break;
1101 }
1102 }
1103
David Fangb88cdf62013-08-08 20:14:40 +00001104 if (Arch == Triple::x86 || Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001105 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001106 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001107 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001108 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001109 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001110
1111 // X86 sect diff's must be followed by a relocation of type
1112 // GENERIC_RELOC_PAIR.
1113 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001114 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001115 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1116 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1117
1118 printRelocationTargetName(this, RE, fmt);
1119 fmt << "-";
1120 printRelocationTargetName(this, RENext, fmt);
1121 break;
1122 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001123 case MachO::GENERIC_RELOC_TLV: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001124 printRelocationTargetName(this, RE, fmt);
1125 fmt << "@TLV";
1126 if (IsPCRel) fmt << "P";
1127 break;
1128 }
1129 default:
1130 printRelocationTargetName(this, RE, fmt);
1131 }
1132 } else { // ARM-specific relocations
1133 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001134 case MachO::ARM_RELOC_HALF:
1135 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001136 // Half relocations steal a bit from the length field to encode
1137 // whether this is an upper16 or a lower16 relocation.
1138 bool isUpper = getAnyRelocationLength(RE) >> 1;
1139
1140 if (isUpper)
1141 fmt << ":upper16:(";
1142 else
1143 fmt << ":lower16:(";
1144 printRelocationTargetName(this, RE, fmt);
1145
1146 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001147 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001148 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001149
1150 // ARM half relocs must be followed by a relocation of type
1151 // ARM_RELOC_PAIR.
1152 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001153 if (RType != MachO::ARM_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001154 report_fatal_error("Expected ARM_RELOC_PAIR after "
Charles Davis8bdfafd2013-09-01 04:28:48 +00001155 "ARM_RELOC_HALF");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001156
1157 // NOTE: The half of the target virtual address is stashed in the
1158 // address field of the secondary relocation, but we can't reverse
1159 // engineer the constant offset from it without decoding the movw/movt
1160 // instruction to find the other half in its immediate field.
1161
1162 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1163 // symbol/section pointer of the follow-on relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001164 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001165 fmt << "-";
1166 printRelocationTargetName(this, RENext, fmt);
1167 }
1168
1169 fmt << ")";
1170 break;
1171 }
1172 default: {
1173 printRelocationTargetName(this, RE, fmt);
1174 }
1175 }
1176 }
1177 } else
1178 printRelocationTargetName(this, RE, fmt);
1179
1180 fmt.flush();
1181 Result.append(fmtbuf.begin(), fmtbuf.end());
1182 return object_error::success;
1183}
1184
1185error_code
Rafael Espindola137faa02013-04-24 15:14:22 +00001186MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001187 unsigned Arch = getArch();
1188 uint64_t Type;
1189 getRelocationType(Rel, Type);
1190
1191 Result = false;
1192
1193 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1194 // is always hidden.
David Fangb88cdf62013-08-08 20:14:40 +00001195 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001196 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001197 } else if (Arch == Triple::x86_64) {
1198 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
Eric Christopher1ff26ab62013-07-22 22:25:09 +00001199 // an X86_64_RELOC_SUBTRACTOR.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001200 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001201 DataRefImpl RelPrev = Rel;
1202 RelPrev.d.a--;
1203 uint64_t PrevType;
1204 getRelocationType(RelPrev, PrevType);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001205 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001206 Result = true;
1207 }
1208 }
1209
1210 return object_error::success;
1211}
1212
1213error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001214 LibraryRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001215 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1216}
1217
1218error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindola137faa02013-04-24 15:14:22 +00001219 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001220 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1221}
1222
Kevin Enderby980b2582014-06-05 21:21:57 +00001223//
1224// guessLibraryShortName() is passed a name of a dynamic library and returns a
1225// guess on what the short name is. Then name is returned as a substring of the
1226// StringRef Name passed in. The name of the dynamic library is recognized as
1227// a framework if it has one of the two following forms:
1228// Foo.framework/Versions/A/Foo
1229// Foo.framework/Foo
1230// Where A and Foo can be any string. And may contain a trailing suffix
1231// starting with an underbar. If the Name is recognized as a framework then
1232// isFramework is set to true else it is set to false. If the Name has a
1233// suffix then Suffix is set to the substring in Name that contains the suffix
1234// else it is set to a NULL StringRef.
1235//
1236// The Name of the dynamic library is recognized as a library name if it has
1237// one of the two following forms:
1238// libFoo.A.dylib
1239// libFoo.dylib
1240// The library may have a suffix trailing the name Foo of the form:
1241// libFoo_profile.A.dylib
1242// libFoo_profile.dylib
1243//
1244// The Name of the dynamic library is also recognized as a library name if it
1245// has the following form:
1246// Foo.qtx
1247//
1248// If the Name of the dynamic library is none of the forms above then a NULL
1249// StringRef is returned.
1250//
1251StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
1252 bool &isFramework,
1253 StringRef &Suffix) {
1254 StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
1255 size_t a, b, c, d, Idx;
1256
1257 isFramework = false;
1258 Suffix = StringRef();
1259
1260 // Pull off the last component and make Foo point to it
1261 a = Name.rfind('/');
1262 if (a == Name.npos || a == 0)
1263 goto guess_library;
1264 Foo = Name.slice(a+1, Name.npos);
1265
1266 // Look for a suffix starting with a '_'
1267 Idx = Foo.rfind('_');
1268 if (Idx != Foo.npos && Foo.size() >= 2) {
1269 Suffix = Foo.slice(Idx, Foo.npos);
1270 Foo = Foo.slice(0, Idx);
1271 }
1272
1273 // First look for the form Foo.framework/Foo
1274 b = Name.rfind('/', a);
1275 if (b == Name.npos)
1276 Idx = 0;
1277 else
1278 Idx = b+1;
1279 F = Name.slice(Idx, Idx + Foo.size());
1280 DotFramework = Name.slice(Idx + Foo.size(),
1281 Idx + Foo.size() + sizeof(".framework/")-1);
1282 if (F == Foo && DotFramework == ".framework/") {
1283 isFramework = true;
1284 return Foo;
1285 }
1286
1287 // Next look for the form Foo.framework/Versions/A/Foo
1288 if (b == Name.npos)
1289 goto guess_library;
1290 c = Name.rfind('/', b);
1291 if (c == Name.npos || c == 0)
1292 goto guess_library;
1293 V = Name.slice(c+1, Name.npos);
1294 if (!V.startswith("Versions/"))
1295 goto guess_library;
1296 d = Name.rfind('/', c);
1297 if (d == Name.npos)
1298 Idx = 0;
1299 else
1300 Idx = d+1;
1301 F = Name.slice(Idx, Idx + Foo.size());
1302 DotFramework = Name.slice(Idx + Foo.size(),
1303 Idx + Foo.size() + sizeof(".framework/")-1);
1304 if (F == Foo && DotFramework == ".framework/") {
1305 isFramework = true;
1306 return Foo;
1307 }
1308
1309guess_library:
1310 // pull off the suffix after the "." and make a point to it
1311 a = Name.rfind('.');
1312 if (a == Name.npos || a == 0)
1313 return StringRef();
1314 Dylib = Name.slice(a, Name.npos);
1315 if (Dylib != ".dylib")
1316 goto guess_qtx;
1317
1318 // First pull off the version letter for the form Foo.A.dylib if any.
1319 if (a >= 3) {
1320 Dot = Name.slice(a-2, a-1);
1321 if (Dot == ".")
1322 a = a - 2;
1323 }
1324
1325 b = Name.rfind('/', a);
1326 if (b == Name.npos)
1327 b = 0;
1328 else
1329 b = b+1;
1330 // ignore any suffix after an underbar like Foo_profile.A.dylib
1331 Idx = Name.find('_', b);
1332 if (Idx != Name.npos && Idx != b) {
1333 Lib = Name.slice(b, Idx);
1334 Suffix = Name.slice(Idx, a);
1335 }
1336 else
1337 Lib = Name.slice(b, a);
1338 // There are incorrect library names of the form:
1339 // libATS.A_profile.dylib so check for these.
1340 if (Lib.size() >= 3) {
1341 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
1342 if (Dot == ".")
1343 Lib = Lib.slice(0, Lib.size()-2);
1344 }
1345 return Lib;
1346
1347guess_qtx:
1348 Qtx = Name.slice(a, Name.npos);
1349 if (Qtx != ".qtx")
1350 return StringRef();
1351 b = Name.rfind('/', a);
1352 if (b == Name.npos)
1353 Lib = Name.slice(0, a);
1354 else
1355 Lib = Name.slice(b+1, a);
1356 // There are library names of the form: QT.A.qtx so check for these.
1357 if (Lib.size() >= 3) {
1358 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
1359 if (Dot == ".")
1360 Lib = Lib.slice(0, Lib.size()-2);
1361 }
1362 return Lib;
1363}
1364
1365// getLibraryShortNameByIndex() is used to get the short name of the library
1366// for an undefined symbol in a linked Mach-O binary that was linked with the
1367// normal two-level namespace default (that is MH_TWOLEVEL in the header).
1368// It is passed the index (0 - based) of the library as translated from
1369// GET_LIBRARY_ORDINAL (1 - based).
1370error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
1371 StringRef &Res) {
1372 if (Index >= Libraries.size())
1373 return object_error::parse_failed;
1374
1375 MachO::dylib_command D =
1376 getStruct<MachO::dylib_command>(this, Libraries[Index]);
1377 if (D.dylib.name >= D.cmdsize)
1378 return object_error::parse_failed;
1379
1380 // If the cache of LibrariesShortNames is not built up do that first for
1381 // all the Libraries.
1382 if (LibrariesShortNames.size() == 0) {
1383 for (unsigned i = 0; i < Libraries.size(); i++) {
1384 MachO::dylib_command D =
1385 getStruct<MachO::dylib_command>(this, Libraries[i]);
1386 if (D.dylib.name >= D.cmdsize) {
1387 LibrariesShortNames.push_back(StringRef());
1388 continue;
1389 }
1390 char *P = (char *)(Libraries[i]) + D.dylib.name;
1391 StringRef Name = StringRef(P);
1392 StringRef Suffix;
1393 bool isFramework;
1394 StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
1395 if (shortName == StringRef())
1396 LibrariesShortNames.push_back(Name);
1397 else
1398 LibrariesShortNames.push_back(shortName);
1399 }
1400 }
1401
1402 Res = LibrariesShortNames[Index];
1403 return object_error::success;
1404}
1405
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001406basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
Lang Hames36072da2014-05-12 21:39:59 +00001407 return getSymbolByIndex(0);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001408}
1409
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001410basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001411 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001412 if (!SymtabLoadCmd)
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001413 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola75c30362013-04-24 19:47:55 +00001414
Charles Davis8bdfafd2013-09-01 04:28:48 +00001415 MachO::symtab_command Symtab = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +00001416 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001417 sizeof(MachO::nlist_64) :
1418 sizeof(MachO::nlist);
1419 unsigned Offset = Symtab.symoff +
1420 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001421 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001422 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001423}
1424
Lang Hames36072da2014-05-12 21:39:59 +00001425basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
1426 DataRefImpl DRI;
1427 if (!SymtabLoadCmd)
1428 return basic_symbol_iterator(SymbolRef(DRI, this));
1429
1430 MachO::symtab_command Symtab = getSymtabLoadCommand();
1431 assert(Index < Symtab.nsyms && "Requested symbol index is out of range.");
1432 unsigned SymbolTableEntrySize =
1433 is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
1434 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1435 DRI.p += Index * SymbolTableEntrySize;
1436 return basic_symbol_iterator(SymbolRef(DRI, this));
1437}
1438
Rafael Espindolab5155a52014-02-10 20:24:04 +00001439section_iterator MachOObjectFile::section_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001440 DataRefImpl DRI;
1441 return section_iterator(SectionRef(DRI, this));
1442}
1443
Rafael Espindolab5155a52014-02-10 20:24:04 +00001444section_iterator MachOObjectFile::section_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001445 DataRefImpl DRI;
1446 DRI.d.a = Sections.size();
1447 return section_iterator(SectionRef(DRI, this));
1448}
1449
Rafael Espindolab5155a52014-02-10 20:24:04 +00001450library_iterator MachOObjectFile::needed_library_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001451 // TODO: implement
1452 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1453}
1454
Rafael Espindolab5155a52014-02-10 20:24:04 +00001455library_iterator MachOObjectFile::needed_library_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001456 // TODO: implement
1457 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1458}
1459
1460uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola60689982013-04-07 19:05:30 +00001461 return is64Bit() ? 8 : 4;
Eric Christopher7b015c72011-04-22 03:19:48 +00001462}
1463
Rafael Espindola56f976f2013-04-18 18:08:55 +00001464StringRef MachOObjectFile::getFileFormatName() const {
1465 unsigned CPUType = getCPUType(this);
1466 if (!is64Bit()) {
1467 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001468 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001469 return "Mach-O 32-bit i386";
Charles Davis74ec8b02013-08-27 05:00:13 +00001470 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001471 return "Mach-O arm";
Charles Davis74ec8b02013-08-27 05:00:13 +00001472 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001473 return "Mach-O 32-bit ppc";
1474 default:
Charles Davis74ec8b02013-08-27 05:00:13 +00001475 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
Rafael Espindola56f976f2013-04-18 18:08:55 +00001476 "64-bit object file when we're not 64-bit?");
1477 return "Mach-O 32-bit unknown";
1478 }
1479 }
1480
1481 // Make sure the cpu type has the correct mask.
Charles Davis74ec8b02013-08-27 05:00:13 +00001482 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1483 == llvm::MachO::CPU_ARCH_ABI64 &&
Eric Christopher1d62c252013-07-22 22:25:07 +00001484 "32-bit object file when we're 64-bit?");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001485
1486 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001487 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001488 return "Mach-O 64-bit x86-64";
Tim Northover00ed9962014-03-29 10:18:08 +00001489 case llvm::MachO::CPU_TYPE_ARM64:
1490 return "Mach-O arm64";
Charles Davis74ec8b02013-08-27 05:00:13 +00001491 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001492 return "Mach-O 64-bit ppc64";
1493 default:
1494 return "Mach-O 64-bit unknown";
1495 }
1496}
1497
Alexey Samsonove6388e62013-06-18 15:03:28 +00001498Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1499 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001500 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001501 return Triple::x86;
Charles Davis74ec8b02013-08-27 05:00:13 +00001502 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001503 return Triple::x86_64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001504 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001505 return Triple::arm;
Tim Northover00ed9962014-03-29 10:18:08 +00001506 case llvm::MachO::CPU_TYPE_ARM64:
1507 return Triple::arm64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001508 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001509 return Triple::ppc;
Charles Davis74ec8b02013-08-27 05:00:13 +00001510 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001511 return Triple::ppc64;
1512 default:
1513 return Triple::UnknownArch;
1514 }
1515}
1516
Alexey Samsonove6388e62013-06-18 15:03:28 +00001517unsigned MachOObjectFile::getArch() const {
1518 return getArch(getCPUType(this));
1519}
1520
Rafael Espindola56f976f2013-04-18 18:08:55 +00001521StringRef MachOObjectFile::getLoadName() const {
1522 // TODO: Implement
Charles Davis1827bd82013-08-27 05:38:30 +00001523 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001524}
1525
Rui Ueyamabc654b12013-09-27 21:47:05 +00001526relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001527 DataRefImpl DRI;
1528 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001529 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001530}
1531
Rui Ueyamabc654b12013-09-27 21:47:05 +00001532relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001533 DataRefImpl DRI;
1534 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001535 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001536}
1537
Kevin Enderby273ae012013-06-06 17:20:50 +00001538dice_iterator MachOObjectFile::begin_dices() const {
1539 DataRefImpl DRI;
1540 if (!DataInCodeLoadCmd)
1541 return dice_iterator(DiceRef(DRI, this));
1542
Charles Davis8bdfafd2013-09-01 04:28:48 +00001543 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1544 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00001545 return dice_iterator(DiceRef(DRI, this));
1546}
1547
1548dice_iterator MachOObjectFile::end_dices() const {
1549 DataRefImpl DRI;
1550 if (!DataInCodeLoadCmd)
1551 return dice_iterator(DiceRef(DRI, this));
1552
Charles Davis8bdfafd2013-09-01 04:28:48 +00001553 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1554 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00001555 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1556 return dice_iterator(DiceRef(DRI, this));
1557}
1558
Rafael Espindola56f976f2013-04-18 18:08:55 +00001559StringRef
1560MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1561 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1562 return parseSegmentOrSectionName(Raw.data());
1563}
1564
1565ArrayRef<char>
1566MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001567 const section_base *Base =
1568 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1569 return ArrayRef<char>(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001570}
1571
1572ArrayRef<char>
1573MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001574 const section_base *Base =
1575 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1576 return ArrayRef<char>(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001577}
1578
1579bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00001580MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001581 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001582 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001583 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001584 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001585}
1586
Eric Christopher1d62c252013-07-22 22:25:07 +00001587unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001588 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001589 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001590 return RE.r_word1 & 0xffffff;
1591 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001592}
1593
Eric Christopher1d62c252013-07-22 22:25:07 +00001594bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001595 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001596 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001597 return (RE.r_word1 >> 27) & 1;
1598 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001599}
1600
Eric Christopher1d62c252013-07-22 22:25:07 +00001601bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001602 const MachO::any_relocation_info &RE) const {
1603 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001604}
1605
Eric Christopher1d62c252013-07-22 22:25:07 +00001606uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001607 const MachO::any_relocation_info &RE) const {
1608 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001609}
1610
Eric Christopher1d62c252013-07-22 22:25:07 +00001611unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001612 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001613 if (isRelocationScattered(RE))
1614 return getScatteredRelocationAddress(RE);
1615 return getPlainRelocationAddress(RE);
1616}
1617
Charles Davis8bdfafd2013-09-01 04:28:48 +00001618unsigned MachOObjectFile::getAnyRelocationPCRel(
1619 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001620 if (isRelocationScattered(RE))
1621 return getScatteredRelocationPCRel(this, RE);
1622 return getPlainRelocationPCRel(this, RE);
1623}
1624
Eric Christopher1d62c252013-07-22 22:25:07 +00001625unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001626 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001627 if (isRelocationScattered(RE))
1628 return getScatteredRelocationLength(RE);
1629 return getPlainRelocationLength(this, RE);
1630}
1631
1632unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00001633MachOObjectFile::getAnyRelocationType(
1634 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001635 if (isRelocationScattered(RE))
1636 return getScatteredRelocationType(RE);
1637 return getPlainRelocationType(this, RE);
1638}
1639
Rafael Espindola52501032013-04-30 15:40:54 +00001640SectionRef
Charles Davis8bdfafd2013-09-01 04:28:48 +00001641MachOObjectFile::getRelocationSection(
1642 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00001643 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
Rafael Espindolab5155a52014-02-10 20:24:04 +00001644 return *section_end();
Rafael Espindola52501032013-04-30 15:40:54 +00001645 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1646 DataRefImpl DRI;
1647 DRI.d.a = SecNum;
1648 return SectionRef(DRI, this);
1649}
1650
Rafael Espindola56f976f2013-04-18 18:08:55 +00001651MachOObjectFile::LoadCommandInfo
1652MachOObjectFile::getFirstLoadCommandInfo() const {
1653 MachOObjectFile::LoadCommandInfo Load;
1654
Charles Davis8bdfafd2013-09-01 04:28:48 +00001655 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1656 sizeof(MachO::mach_header);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001657 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001658 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001659 return Load;
1660}
1661
1662MachOObjectFile::LoadCommandInfo
1663MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1664 MachOObjectFile::LoadCommandInfo Next;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001665 Next.Ptr = L.Ptr + L.C.cmdsize;
1666 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001667 return Next;
1668}
1669
Charles Davis8bdfafd2013-09-01 04:28:48 +00001670MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1671 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001672}
1673
Charles Davis8bdfafd2013-09-01 04:28:48 +00001674MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1675 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001676}
1677
Charles Davis8bdfafd2013-09-01 04:28:48 +00001678MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00001679 unsigned Index) const {
1680 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001681 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001682}
1683
Charles Davis8bdfafd2013-09-01 04:28:48 +00001684MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1685 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001686 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001687 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001688}
1689
Charles Davis8bdfafd2013-09-01 04:28:48 +00001690MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00001691MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001692 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001693 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001694}
1695
Charles Davis8bdfafd2013-09-01 04:28:48 +00001696MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00001697MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001698 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001699 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001700}
1701
Charles Davis8bdfafd2013-09-01 04:28:48 +00001702MachO::linkedit_data_command
1703MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1704 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001705}
1706
Charles Davis8bdfafd2013-09-01 04:28:48 +00001707MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001708MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001709 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001710}
1711
Charles Davis8bdfafd2013-09-01 04:28:48 +00001712MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00001713MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001714 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001715}
1716
Charles Davis8bdfafd2013-09-01 04:28:48 +00001717MachO::linker_options_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001718MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001719 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001720}
1721
Jim Grosbach448334a2014-03-18 22:09:05 +00001722MachO::version_min_command
1723MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
1724 return getStruct<MachO::version_min_command>(this, L.Ptr);
1725}
1726
Charles Davis8bdfafd2013-09-01 04:28:48 +00001727MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001728MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +00001729 DataRefImpl Sec;
1730 Sec.d.a = Rel.d.a;
1731 uint32_t Offset;
1732 if (is64Bit()) {
1733 MachO::section_64 Sect = getSection64(Sec);
1734 Offset = Sect.reloff;
1735 } else {
1736 MachO::section Sect = getSection(Sec);
1737 Offset = Sect.reloff;
1738 }
1739
1740 auto P = reinterpret_cast<const MachO::any_relocation_info *>(
1741 getPtr(this, Offset)) + Rel.d.b;
1742 return getStruct<MachO::any_relocation_info>(
1743 this, reinterpret_cast<const char *>(P));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001744}
1745
Charles Davis8bdfafd2013-09-01 04:28:48 +00001746MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00001747MachOObjectFile::getDice(DataRefImpl Rel) const {
1748 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001749 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00001750}
1751
Charles Davis8bdfafd2013-09-01 04:28:48 +00001752MachO::mach_header MachOObjectFile::getHeader() const {
1753 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001754}
1755
Charles Davis8bdfafd2013-09-01 04:28:48 +00001756MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1757 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001758}
1759
Charles Davis8bdfafd2013-09-01 04:28:48 +00001760uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1761 const MachO::dysymtab_command &DLC,
1762 unsigned Index) const {
1763 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1764 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001765}
1766
Charles Davis8bdfafd2013-09-01 04:28:48 +00001767MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00001768MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1769 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001770 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1771 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001772}
1773
Charles Davis8bdfafd2013-09-01 04:28:48 +00001774MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1775 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001776}
1777
Charles Davis8bdfafd2013-09-01 04:28:48 +00001778MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1779 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001780}
1781
Charles Davis8bdfafd2013-09-01 04:28:48 +00001782MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00001783MachOObjectFile::getDataInCodeLoadCommand() const {
1784 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00001785 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00001786
1787 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001788 MachO::linkedit_data_command Cmd;
1789 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1790 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1791 Cmd.dataoff = 0;
1792 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00001793 return Cmd;
1794}
1795
Rafael Espindola6e040c02013-04-26 20:07:33 +00001796StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001797 MachO::symtab_command S = getSymtabLoadCommand();
1798 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001799}
1800
Rafael Espindola56f976f2013-04-18 18:08:55 +00001801bool MachOObjectFile::is64Bit() const {
1802 return getType() == getMachOType(false, true) ||
1803 getType() == getMachOType(true, true);
1804}
1805
1806void MachOObjectFile::ReadULEB128s(uint64_t Index,
1807 SmallVectorImpl<uint64_t> &Out) const {
1808 DataExtractor extractor(ObjectFile::getData(), true, 0);
1809
1810 uint32_t offset = Index;
1811 uint64_t data = 0;
1812 while (uint64_t delta = extractor.getULEB128(&offset)) {
1813 data += delta;
1814 Out.push_back(data);
1815 }
1816}
1817
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001818ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer,
1819 bool BufferOwned) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001820 StringRef Magic = Buffer->getBuffer().slice(0, 4);
Rafael Espindola692410e2014-01-21 23:06:54 +00001821 error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001822 std::unique_ptr<MachOObjectFile> Ret;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001823 if (Magic == "\xFE\xED\xFA\xCE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001824 Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001825 else if (Magic == "\xCE\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001826 Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001827 else if (Magic == "\xFE\xED\xFA\xCF")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001828 Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001829 else if (Magic == "\xCF\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001830 Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned));
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001831 else {
1832 delete Buffer;
Rafael Espindola692410e2014-01-21 23:06:54 +00001833 return object_error::parse_failed;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001834 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001835
Rafael Espindola692410e2014-01-21 23:06:54 +00001836 if (EC)
1837 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +00001838 return Ret.release();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001839}
1840
Owen Anderson27c579d2011-10-11 17:32:27 +00001841} // end namespace object
Eric Christopher7b015c72011-04-22 03:19:48 +00001842} // end namespace llvm