blob: a072b0f28ca1033c178c7d82d730204cb6d2f769 [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 {
Rafael Espindola3acea392014-06-12 21:46:39 +000031
Owen Anderson27c579d2011-10-11 17:32:27 +000032namespace object {
Eric Christopher7b015c72011-04-22 03:19:48 +000033
Charles Davis8bdfafd2013-09-01 04:28:48 +000034struct nlist_base {
35 uint32_t n_strx;
36 uint8_t n_type;
37 uint8_t n_sect;
38 uint16_t n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +000039};
40
Charles Davis8bdfafd2013-09-01 04:28:48 +000041struct section_base {
42 char sectname[16];
43 char segname[16];
Rafael Espindola56f976f2013-04-18 18:08:55 +000044};
45
46template<typename T>
Rafael Espindola56f976f2013-04-18 18:08:55 +000047static void SwapStruct(T &Value);
48
49template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000050void SwapStruct(MachO::any_relocation_info &H) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000051 sys::swapByteOrder(H.r_word0);
52 sys::swapByteOrder(H.r_word1);
Rafael Espindola5ffc0792013-04-07 16:07:35 +000053}
54
Rafael Espindola56f976f2013-04-18 18:08:55 +000055template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000056void SwapStruct(MachO::load_command &L) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000057 sys::swapByteOrder(L.cmd);
58 sys::swapByteOrder(L.cmdsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +000059}
Rafael Espindola421305a2013-04-07 20:01:29 +000060
Rafael Espindola56f976f2013-04-18 18:08:55 +000061template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000062void SwapStruct(nlist_base &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000063 sys::swapByteOrder(S.n_strx);
64 sys::swapByteOrder(S.n_desc);
Rafael Espindola56f976f2013-04-18 18:08:55 +000065}
66
67template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000068void SwapStruct(MachO::section &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000069 sys::swapByteOrder(S.addr);
70 sys::swapByteOrder(S.size);
71 sys::swapByteOrder(S.offset);
72 sys::swapByteOrder(S.align);
73 sys::swapByteOrder(S.reloff);
74 sys::swapByteOrder(S.nreloc);
75 sys::swapByteOrder(S.flags);
76 sys::swapByteOrder(S.reserved1);
77 sys::swapByteOrder(S.reserved2);
Rafael Espindola56f976f2013-04-18 18:08:55 +000078}
79
80template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000081void SwapStruct(MachO::section_64 &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000082 sys::swapByteOrder(S.addr);
83 sys::swapByteOrder(S.size);
84 sys::swapByteOrder(S.offset);
85 sys::swapByteOrder(S.align);
86 sys::swapByteOrder(S.reloff);
87 sys::swapByteOrder(S.nreloc);
88 sys::swapByteOrder(S.flags);
89 sys::swapByteOrder(S.reserved1);
90 sys::swapByteOrder(S.reserved2);
91 sys::swapByteOrder(S.reserved3);
Rafael Espindola56f976f2013-04-18 18:08:55 +000092}
93
94template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000095void SwapStruct(MachO::nlist &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000096 sys::swapByteOrder(S.n_strx);
97 sys::swapByteOrder(S.n_desc);
98 sys::swapByteOrder(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +000099}
100
101template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000102void SwapStruct(MachO::nlist_64 &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000103 sys::swapByteOrder(S.n_strx);
104 sys::swapByteOrder(S.n_desc);
105 sys::swapByteOrder(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000106}
107
108template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000109void SwapStruct(MachO::mach_header &H) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000110 sys::swapByteOrder(H.magic);
111 sys::swapByteOrder(H.cputype);
112 sys::swapByteOrder(H.cpusubtype);
113 sys::swapByteOrder(H.filetype);
114 sys::swapByteOrder(H.ncmds);
115 sys::swapByteOrder(H.sizeofcmds);
116 sys::swapByteOrder(H.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000117}
118
119template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000120void SwapStruct(MachO::mach_header_64 &H) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000121 sys::swapByteOrder(H.magic);
122 sys::swapByteOrder(H.cputype);
123 sys::swapByteOrder(H.cpusubtype);
124 sys::swapByteOrder(H.filetype);
125 sys::swapByteOrder(H.ncmds);
126 sys::swapByteOrder(H.sizeofcmds);
127 sys::swapByteOrder(H.flags);
128 sys::swapByteOrder(H.reserved);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000129}
130
131template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000132void SwapStruct(MachO::symtab_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000133 sys::swapByteOrder(C.cmd);
134 sys::swapByteOrder(C.cmdsize);
135 sys::swapByteOrder(C.symoff);
136 sys::swapByteOrder(C.nsyms);
137 sys::swapByteOrder(C.stroff);
138 sys::swapByteOrder(C.strsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000139}
140
141template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000142void SwapStruct(MachO::dysymtab_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000143 sys::swapByteOrder(C.cmd);
144 sys::swapByteOrder(C.cmdsize);
145 sys::swapByteOrder(C.ilocalsym);
146 sys::swapByteOrder(C.nlocalsym);
147 sys::swapByteOrder(C.iextdefsym);
148 sys::swapByteOrder(C.nextdefsym);
149 sys::swapByteOrder(C.iundefsym);
150 sys::swapByteOrder(C.nundefsym);
151 sys::swapByteOrder(C.tocoff);
152 sys::swapByteOrder(C.ntoc);
153 sys::swapByteOrder(C.modtaboff);
154 sys::swapByteOrder(C.nmodtab);
155 sys::swapByteOrder(C.extrefsymoff);
156 sys::swapByteOrder(C.nextrefsyms);
157 sys::swapByteOrder(C.indirectsymoff);
158 sys::swapByteOrder(C.nindirectsyms);
159 sys::swapByteOrder(C.extreloff);
160 sys::swapByteOrder(C.nextrel);
161 sys::swapByteOrder(C.locreloff);
162 sys::swapByteOrder(C.nlocrel);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000163}
164
165template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000166void SwapStruct(MachO::linkedit_data_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000167 sys::swapByteOrder(C.cmd);
168 sys::swapByteOrder(C.cmdsize);
169 sys::swapByteOrder(C.dataoff);
170 sys::swapByteOrder(C.datasize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000171}
172
173template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000174void SwapStruct(MachO::segment_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000175 sys::swapByteOrder(C.cmd);
176 sys::swapByteOrder(C.cmdsize);
177 sys::swapByteOrder(C.vmaddr);
178 sys::swapByteOrder(C.vmsize);
179 sys::swapByteOrder(C.fileoff);
180 sys::swapByteOrder(C.filesize);
181 sys::swapByteOrder(C.maxprot);
182 sys::swapByteOrder(C.initprot);
183 sys::swapByteOrder(C.nsects);
184 sys::swapByteOrder(C.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000185}
186
187template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000188void SwapStruct(MachO::segment_command_64 &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000189 sys::swapByteOrder(C.cmd);
190 sys::swapByteOrder(C.cmdsize);
191 sys::swapByteOrder(C.vmaddr);
192 sys::swapByteOrder(C.vmsize);
193 sys::swapByteOrder(C.fileoff);
194 sys::swapByteOrder(C.filesize);
195 sys::swapByteOrder(C.maxprot);
196 sys::swapByteOrder(C.initprot);
197 sys::swapByteOrder(C.nsects);
198 sys::swapByteOrder(C.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000199}
200
Rafael Espindola6e040c02013-04-26 20:07:33 +0000201template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000202void SwapStruct(uint32_t &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000203 sys::swapByteOrder(C);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000204}
205
206template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000207void SwapStruct(MachO::linker_options_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000208 sys::swapByteOrder(C.cmd);
209 sys::swapByteOrder(C.cmdsize);
210 sys::swapByteOrder(C.count);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000211}
212
213template<>
Jim Grosbach448334a2014-03-18 22:09:05 +0000214void SwapStruct(MachO::version_min_command&C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000215 sys::swapByteOrder(C.cmd);
216 sys::swapByteOrder(C.cmdsize);
217 sys::swapByteOrder(C.version);
218 sys::swapByteOrder(C.reserved);
Jim Grosbach448334a2014-03-18 22:09:05 +0000219}
220
221template<>
Kevin Enderby980b2582014-06-05 21:21:57 +0000222void SwapStruct(MachO::dylib_command&C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000223 sys::swapByteOrder(C.cmd);
224 sys::swapByteOrder(C.cmdsize);
225 sys::swapByteOrder(C.dylib.name);
226 sys::swapByteOrder(C.dylib.timestamp);
227 sys::swapByteOrder(C.dylib.current_version);
228 sys::swapByteOrder(C.dylib.compatibility_version);
Kevin Enderby980b2582014-06-05 21:21:57 +0000229}
230
231template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000232void SwapStruct(MachO::data_in_code_entry &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000233 sys::swapByteOrder(C.offset);
234 sys::swapByteOrder(C.length);
235 sys::swapByteOrder(C.kind);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000236}
237
Rafael Espindola3cdeb172013-04-19 13:45:05 +0000238template<typename T>
239T getStruct(const MachOObjectFile *O, const char *P) {
240 T Cmd;
241 memcpy(&Cmd, P, sizeof(T));
242 if (O->isLittleEndian() != sys::IsLittleEndianHost)
243 SwapStruct(Cmd);
244 return Cmd;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000245}
246
Rafael Espindola56f976f2013-04-18 18:08:55 +0000247static uint32_t
248getSegmentLoadCommandNumSections(const MachOObjectFile *O,
249 const MachOObjectFile::LoadCommandInfo &L) {
250 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000251 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
252 return S.nsects;
Rafael Espindola421305a2013-04-07 20:01:29 +0000253 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000254 MachO::segment_command S = O->getSegmentLoadCommand(L);
255 return S.nsects;
Rafael Espindola5ffc0792013-04-07 16:07:35 +0000256}
257
Rafael Espindola6e040c02013-04-26 20:07:33 +0000258static const char *
259getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
260 unsigned Sec) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000261 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
262
263 bool Is64 = O->is64Bit();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000264 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
265 sizeof(MachO::segment_command);
266 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
267 sizeof(MachO::section);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000268
269 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
Charles Davis1827bd82013-08-27 05:38:30 +0000270 return reinterpret_cast<const char*>(SectionAddr);
Rafael Espindola60689982013-04-07 19:05:30 +0000271}
272
Rafael Espindola56f976f2013-04-18 18:08:55 +0000273static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
274 return O->getData().substr(Offset, 1).data();
Rafael Espindola60689982013-04-07 19:05:30 +0000275}
276
Charles Davis8bdfafd2013-09-01 04:28:48 +0000277static nlist_base
Rafael Espindola56f976f2013-04-18 18:08:55 +0000278getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola75c30362013-04-24 19:47:55 +0000279 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000280 return getStruct<nlist_base>(O, P);
Eric Christopher7b015c72011-04-22 03:19:48 +0000281}
282
Rafael Espindola56f976f2013-04-18 18:08:55 +0000283static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000284 if (P[15] == 0)
285 // Null terminated.
286 return P;
287 // Not null terminated, so this is a 16 char string.
288 return StringRef(P, 16);
289}
290
Rafael Espindola56f976f2013-04-18 18:08:55 +0000291// Helper to advance a section or symbol iterator multiple increments at a time.
292template<class T>
Rafael Espindola5e812af2014-01-30 02:49:50 +0000293static void advance(T &it, size_t Val) {
294 while (Val--)
295 ++it;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000296}
297
298static unsigned getCPUType(const MachOObjectFile *O) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000299 return O->getHeader().cputype;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000300}
301
302static void printRelocationTargetName(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000303 const MachO::any_relocation_info &RE,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000304 raw_string_ostream &fmt) {
305 bool IsScattered = O->isRelocationScattered(RE);
306
307 // Target of a scattered relocation is an address. In the interest of
308 // generating pretty output, scan through the symbol table looking for a
309 // symbol that aligns with that address. If we find one, print it.
310 // Otherwise, we just print the hex address of the target.
311 if (IsScattered) {
312 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
313
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000314 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola3acea392014-06-12 21:46:39 +0000315 std::error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000316 uint64_t Addr;
317 StringRef Name;
318
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000319 if ((ec = Symbol.getAddress(Addr)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000320 report_fatal_error(ec.message());
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000321 if (Addr != Val)
322 continue;
323 if ((ec = Symbol.getName(Name)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000324 report_fatal_error(ec.message());
325 fmt << Name;
326 return;
327 }
328
329 // If we couldn't find a symbol that this relocation refers to, try
330 // to find a section beginning instead.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000331 for (const SectionRef &Section : O->sections()) {
Rafael Espindola3acea392014-06-12 21:46:39 +0000332 std::error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000333 uint64_t Addr;
334 StringRef Name;
335
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000336 if ((ec = Section.getAddress(Addr)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000337 report_fatal_error(ec.message());
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000338 if (Addr != Val)
339 continue;
340 if ((ec = Section.getName(Name)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000341 report_fatal_error(ec.message());
342 fmt << Name;
343 return;
344 }
345
346 fmt << format("0x%x", Val);
347 return;
348 }
349
350 StringRef S;
351 bool isExtern = O->getPlainRelocationExternal(RE);
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000352 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000353
354 if (isExtern) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000355 symbol_iterator SI = O->symbol_begin();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000356 advance(SI, Val);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000357 SI->getName(S);
358 } else {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000359 section_iterator SI = O->section_begin();
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000360 // Adjust for the fact that sections are 1-indexed.
Rafael Espindola5e812af2014-01-30 02:49:50 +0000361 advance(SI, Val - 1);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000362 SI->getName(S);
363 }
364
365 fmt << S;
366}
367
Charles Davis8bdfafd2013-09-01 04:28:48 +0000368static uint32_t
369getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
370 return RE.r_word0;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000371}
372
373static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000374getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
375 return RE.r_word0 & 0xffffff;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000376}
377
378static bool getPlainRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000379 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000380 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000381 return (RE.r_word1 >> 24) & 1;
382 return (RE.r_word1 >> 7) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000383}
384
385static bool
386getScatteredRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000387 const MachO::any_relocation_info &RE) {
388 return (RE.r_word0 >> 30) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000389}
390
391static unsigned getPlainRelocationLength(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000392 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000393 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000394 return (RE.r_word1 >> 25) & 3;
395 return (RE.r_word1 >> 5) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000396}
397
398static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000399getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
400 return (RE.r_word0 >> 28) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000401}
402
403static unsigned getPlainRelocationType(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000404 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000405 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000406 return RE.r_word1 >> 28;
407 return RE.r_word1 & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000408}
409
Charles Davis8bdfafd2013-09-01 04:28:48 +0000410static unsigned
411getScatteredRelocationType(const MachO::any_relocation_info &RE) {
412 return (RE.r_word0 >> 24) & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000413}
414
415static uint32_t getSectionFlags(const MachOObjectFile *O,
416 DataRefImpl Sec) {
417 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000418 MachO::section_64 Sect = O->getSection64(Sec);
419 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000420 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000421 MachO::section Sect = O->getSection(Sec);
422 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000423}
424
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000425MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
Rafael Espindola3acea392014-06-12 21:46:39 +0000426 bool Is64bits, std::error_code &EC,
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000427 bool BufferOwned)
428 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned),
Craig Topper2617dcc2014-04-15 06:32:26 +0000429 SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
430 DataInCodeLoadCmd(nullptr) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000431 uint32_t LoadCommandCount = this->getHeader().ncmds;
432 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
433 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000434
435 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000436 for (unsigned I = 0; ; ++I) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000437 if (Load.C.cmd == MachO::LC_SYMTAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000438 assert(!SymtabLoadCmd && "Multiple symbol tables");
439 SymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000440 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000441 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
442 DysymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000443 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000444 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
445 DataInCodeLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000446 } else if (Load.C.cmd == SegmentLoadType) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000447 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
448 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000449 const char *Sec = getSectionPtr(this, Load, J);
450 Sections.push_back(Sec);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000451 }
Kevin Enderby980b2582014-06-05 21:21:57 +0000452 } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB ||
453 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
454 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
455 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
456 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
457 Libraries.push_back(Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000458 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000459
460 if (I == LoadCommandCount - 1)
461 break;
462 else
463 Load = getNextLoadCommandInfo(Load);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000464 }
465}
466
Rafael Espindola5e812af2014-01-30 02:49:50 +0000467void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
Rafael Espindola75c30362013-04-24 19:47:55 +0000468 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000469 sizeof(MachO::nlist_64) :
470 sizeof(MachO::nlist);
Rafael Espindola75c30362013-04-24 19:47:55 +0000471 Symb.p += SymbolTableEntrySize;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000472}
473
Rafael Espindola3acea392014-06-12 21:46:39 +0000474std::error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
475 StringRef &Res) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000476 StringRef StringTable = getStringTableData();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000477 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
478 const char *Start = &StringTable.data()[Entry.n_strx];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000479 Res = StringRef(Start);
480 return object_error::success;
481}
482
Kevin Enderby980b2582014-06-05 21:21:57 +0000483// getIndirectName() returns the name of the alias'ed symbol who's string table
484// index is in the n_value field.
Rafael Espindola3acea392014-06-12 21:46:39 +0000485std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
486 StringRef &Res) const {
Kevin Enderby980b2582014-06-05 21:21:57 +0000487 StringRef StringTable = getStringTableData();
488 uint64_t NValue;
489 if (is64Bit()) {
490 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
491 NValue = Entry.n_value;
492 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
493 return object_error::parse_failed;
494 } else {
495 MachO::nlist Entry = getSymbolTableEntry(Symb);
496 NValue = Entry.n_value;
497 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
498 return object_error::parse_failed;
499 }
500 if (NValue >= StringTable.size())
501 return object_error::parse_failed;
502 const char *Start = &StringTable.data()[NValue];
503 Res = StringRef(Start);
504 return object_error::success;
505}
506
Rafael Espindola3acea392014-06-12 21:46:39 +0000507std::error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
508 uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000509 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000510 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000511 if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
512 Entry.n_value == 0)
513 Res = UnknownAddressOrSize;
514 else
515 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000516 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000517 MachO::nlist Entry = getSymbolTableEntry(Symb);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000518 if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
519 Entry.n_value == 0)
520 Res = UnknownAddressOrSize;
521 else
522 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000523 }
524 return object_error::success;
525}
526
Rafael Espindola3acea392014-06-12 21:46:39 +0000527std::error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
528 uint32_t &Result) const {
Rafael Espindola20122a42014-01-31 20:57:12 +0000529 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000530 if (flags & SymbolRef::SF_Common) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000531 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
532 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000533 } else {
534 Result = 0;
535 }
536 return object_error::success;
537}
538
Rafael Espindola3acea392014-06-12 21:46:39 +0000539std::error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
540 uint64_t &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000541 uint64_t BeginOffset;
542 uint64_t EndOffset = 0;
543 uint8_t SectionIndex;
544
Charles Davis8bdfafd2013-09-01 04:28:48 +0000545 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000546 uint64_t Value;
547 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000548 if (Value == UnknownAddressOrSize) {
549 Result = UnknownAddressOrSize;
550 return object_error::success;
551 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000552
553 BeginOffset = Value;
554
Charles Davis8bdfafd2013-09-01 04:28:48 +0000555 SectionIndex = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000556 if (!SectionIndex) {
Rafael Espindola20122a42014-01-31 20:57:12 +0000557 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000558 if (flags & SymbolRef::SF_Common)
559 Result = Value;
560 else
561 Result = UnknownAddressOrSize;
562 return object_error::success;
563 }
564 // Unfortunately symbols are unsorted so we need to touch all
565 // symbols from load command
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000566 for (const SymbolRef &Symbol : symbols()) {
567 DataRefImpl DRI = Symbol.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000568 Entry = getSymbolTableEntryBase(this, DRI);
569 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000570 if (Value == UnknownAddressOrSize)
571 continue;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000572 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000573 if (!EndOffset || Value < EndOffset)
574 EndOffset = Value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000575 }
576 if (!EndOffset) {
577 uint64_t Size;
578 DataRefImpl Sec;
579 Sec.d.a = SectionIndex-1;
580 getSectionSize(Sec, Size);
581 getSectionAddress(Sec, EndOffset);
582 EndOffset += Size;
583 }
584 Result = EndOffset - BeginOffset;
585 return object_error::success;
586}
587
Rafael Espindola3acea392014-06-12 21:46:39 +0000588std::error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
589 SymbolRef::Type &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000590 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
591 uint8_t n_type = Entry.n_type;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000592
593 Res = SymbolRef::ST_Other;
594
595 // If this is a STAB debugging symbol, we can do nothing more.
Charles Davis74ec8b02013-08-27 05:00:13 +0000596 if (n_type & MachO::N_STAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000597 Res = SymbolRef::ST_Debug;
598 return object_error::success;
599 }
600
Charles Davis74ec8b02013-08-27 05:00:13 +0000601 switch (n_type & MachO::N_TYPE) {
602 case MachO::N_UNDF :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000603 Res = SymbolRef::ST_Unknown;
604 break;
Charles Davis74ec8b02013-08-27 05:00:13 +0000605 case MachO::N_SECT :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000606 Res = SymbolRef::ST_Function;
607 break;
608 }
609 return object_error::success;
610}
611
Rafael Espindola20122a42014-01-31 20:57:12 +0000612uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000613 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000614
Charles Davis8bdfafd2013-09-01 04:28:48 +0000615 uint8_t MachOType = Entry.n_type;
616 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000617
Rafael Espindola20122a42014-01-31 20:57:12 +0000618 uint32_t Result = SymbolRef::SF_None;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000619
Charles Davis74ec8b02013-08-27 05:00:13 +0000620 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000621 Result |= SymbolRef::SF_Undefined;
622
Tim Northovereaef0742014-05-30 13:22:59 +0000623 if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
624 Result |= SymbolRef::SF_Indirect;
625
Rafael Espindolaa1356322013-11-02 05:03:24 +0000626 if (MachOType & MachO::N_STAB)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000627 Result |= SymbolRef::SF_FormatSpecific;
628
Charles Davis74ec8b02013-08-27 05:00:13 +0000629 if (MachOType & MachO::N_EXT) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000630 Result |= SymbolRef::SF_Global;
Charles Davis74ec8b02013-08-27 05:00:13 +0000631 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000632 uint64_t Value;
633 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000634 if (Value && Value != UnknownAddressOrSize)
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000635 Result |= SymbolRef::SF_Common;
636 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000637 }
638
Charles Davis74ec8b02013-08-27 05:00:13 +0000639 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000640 Result |= SymbolRef::SF_Weak;
641
Charles Davis74ec8b02013-08-27 05:00:13 +0000642 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000643 Result |= SymbolRef::SF_Absolute;
644
Rafael Espindola20122a42014-01-31 20:57:12 +0000645 return Result;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000646}
647
Rafael Espindola3acea392014-06-12 21:46:39 +0000648std::error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
649 section_iterator &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000650 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
651 uint8_t index = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000652
653 if (index == 0) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000654 Res = section_end();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000655 } else {
656 DataRefImpl DRI;
657 DRI.d.a = index - 1;
658 Res = section_iterator(SectionRef(DRI, this));
659 }
660
661 return object_error::success;
662}
663
Rafael Espindola5e812af2014-01-30 02:49:50 +0000664void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000665 Sec.d.a++;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000666}
667
Rafael Espindola3acea392014-06-12 21:46:39 +0000668std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
669 StringRef &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000670 ArrayRef<char> Raw = getSectionRawName(Sec);
671 Result = parseSegmentOrSectionName(Raw.data());
672 return object_error::success;
673}
674
Rafael Espindola3acea392014-06-12 21:46:39 +0000675std::error_code MachOObjectFile::getSectionAddress(DataRefImpl Sec,
676 uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000677 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000678 MachO::section_64 Sect = getSection64(Sec);
679 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000680 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000681 MachO::section Sect = getSection(Sec);
682 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000683 }
684 return object_error::success;
685}
686
Rafael Espindola3acea392014-06-12 21:46:39 +0000687std::error_code MachOObjectFile::getSectionSize(DataRefImpl Sec,
688 uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000689 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000690 MachO::section_64 Sect = getSection64(Sec);
691 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000692 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000693 MachO::section Sect = getSection(Sec);
694 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000695 }
696
697 return object_error::success;
698}
699
Rafael Espindola3acea392014-06-12 21:46:39 +0000700std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
701 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000702 uint32_t Offset;
703 uint64_t Size;
704
705 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000706 MachO::section_64 Sect = getSection64(Sec);
707 Offset = Sect.offset;
708 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000709 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000710 MachO::section Sect = getSection(Sec);
711 Offset = Sect.offset;
712 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000713 }
714
715 Res = this->getData().substr(Offset, Size);
716 return object_error::success;
717}
718
Rafael Espindola3acea392014-06-12 21:46:39 +0000719std::error_code MachOObjectFile::getSectionAlignment(DataRefImpl Sec,
720 uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000721 uint32_t Align;
722 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000723 MachO::section_64 Sect = getSection64(Sec);
724 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000725 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000726 MachO::section Sect = getSection(Sec);
727 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000728 }
729
730 Res = uint64_t(1) << Align;
731 return object_error::success;
732}
733
Rafael Espindola3acea392014-06-12 21:46:39 +0000734std::error_code MachOObjectFile::isSectionText(DataRefImpl Sec,
735 bool &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000736 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000737 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000738 return object_error::success;
739}
740
Rafael Espindola3acea392014-06-12 21:46:39 +0000741std::error_code MachOObjectFile::isSectionData(DataRefImpl Sec,
742 bool &Result) const {
Kevin Enderby403258f2014-05-19 20:36:02 +0000743 uint32_t Flags = getSectionFlags(this, Sec);
744 unsigned SectionType = Flags & MachO::SECTION_TYPE;
745 Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
746 !(SectionType == MachO::S_ZEROFILL ||
747 SectionType == MachO::S_GB_ZEROFILL);
Michael J. Spencer800619f2011-09-28 20:57:30 +0000748 return object_error::success;
749}
750
Rafael Espindola3acea392014-06-12 21:46:39 +0000751std::error_code MachOObjectFile::isSectionBSS(DataRefImpl Sec,
752 bool &Result) const {
Kevin Enderby403258f2014-05-19 20:36:02 +0000753 uint32_t Flags = getSectionFlags(this, Sec);
754 unsigned SectionType = Flags & MachO::SECTION_TYPE;
755 Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
756 (SectionType == MachO::S_ZEROFILL ||
757 SectionType == MachO::S_GB_ZEROFILL);
Preston Gurd2138ef62012-04-12 20:13:57 +0000758 return object_error::success;
759}
760
Rafael Espindola3acea392014-06-12 21:46:39 +0000761std::error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000762MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000763 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000764 // FIXME: Unimplemented.
765 Result = true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000766 return object_error::success;
767}
768
Rafael Espindola3acea392014-06-12 21:46:39 +0000769std::error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
770 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000771 // FIXME: Unimplemented.
772 Result = false;
773 return object_error::success;
774}
775
Rafael Espindola3acea392014-06-12 21:46:39 +0000776std::error_code MachOObjectFile::isSectionZeroInit(DataRefImpl Sec,
777 bool &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000778 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis74ec8b02013-08-27 05:00:13 +0000779 unsigned SectionType = Flags & MachO::SECTION_TYPE;
780 Res = SectionType == MachO::S_ZEROFILL ||
781 SectionType == MachO::S_GB_ZEROFILL;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000782 return object_error::success;
783}
784
Rafael Espindola3acea392014-06-12 21:46:39 +0000785std::error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
786 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000787 // Consider using the code from isSectionText to look for __const sections.
788 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
789 // to use section attributes to distinguish code from data.
790
791 // FIXME: Unimplemented.
792 Result = false;
793 return object_error::success;
794}
795
Rafael Espindola3acea392014-06-12 21:46:39 +0000796std::error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
797 DataRefImpl Symb,
798 bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000799 SymbolRef::Type ST;
800 this->getSymbolType(Symb, ST);
801 if (ST == SymbolRef::ST_Unknown) {
802 Result = false;
803 return object_error::success;
804 }
805
806 uint64_t SectBegin, SectEnd;
807 getSectionAddress(Sec, SectBegin);
808 getSectionSize(Sec, SectEnd);
809 SectEnd += SectBegin;
810
811 uint64_t SymAddr;
812 getSymbolAddress(Symb, SymAddr);
813 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
814
815 return object_error::success;
816}
817
Rui Ueyamabc654b12013-09-27 21:47:05 +0000818relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000819 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +0000820 Ret.d.a = Sec.d.a;
821 Ret.d.b = 0;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000822 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000823}
Rafael Espindolac0406e12013-04-08 20:45:01 +0000824
Rafael Espindola56f976f2013-04-18 18:08:55 +0000825relocation_iterator
Rui Ueyamabc654b12013-09-27 21:47:05 +0000826MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000827 uint32_t Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000828 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000829 MachO::section_64 Sect = getSection64(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000830 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000831 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000832 MachO::section Sect = getSection(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000833 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000834 }
Eric Christopher7b015c72011-04-22 03:19:48 +0000835
Rafael Espindola56f976f2013-04-18 18:08:55 +0000836 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +0000837 Ret.d.a = Sec.d.a;
838 Ret.d.b = Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000839 return relocation_iterator(RelocationRef(Ret, this));
840}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000841
Rafael Espindola5e812af2014-01-30 02:49:50 +0000842void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +0000843 ++Rel.d.b;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000844}
Owen Anderson171f4852011-10-24 23:20:07 +0000845
Rafael Espindola3acea392014-06-12 21:46:39 +0000846std::error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
847 uint64_t &Res) const {
Rafael Espindola72475462014-04-04 00:31:12 +0000848 uint64_t Offset;
849 getRelocationOffset(Rel, Offset);
Rafael Espindola7e91bc92014-04-03 23:54:35 +0000850
851 DataRefImpl Sec;
852 Sec.d.a = Rel.d.a;
853 uint64_t SecAddress;
854 getSectionAddress(Sec, SecAddress);
855 Res = SecAddress + Offset;
856 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000857}
858
Rafael Espindola3acea392014-06-12 21:46:39 +0000859std::error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
860 uint64_t &Res) const {
Rafael Espindola72475462014-04-04 00:31:12 +0000861 assert(getHeader().filetype == MachO::MH_OBJECT &&
862 "Only implemented for MH_OBJECT");
Charles Davis8bdfafd2013-09-01 04:28:48 +0000863 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000864 Res = getAnyRelocationAddress(RE);
865 return object_error::success;
David Meyer2fc34c52012-03-01 01:36:50 +0000866}
867
Rafael Espindola806f0062013-06-05 01:33:53 +0000868symbol_iterator
869MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000870 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000871 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
872 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola806f0062013-06-05 01:33:53 +0000873 if (!isExtern)
Rafael Espindolab5155a52014-02-10 20:24:04 +0000874 return symbol_end();
Rafael Espindola75c30362013-04-24 19:47:55 +0000875
Charles Davis8bdfafd2013-09-01 04:28:48 +0000876 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +0000877 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000878 sizeof(MachO::nlist_64) :
879 sizeof(MachO::nlist);
880 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +0000881 DataRefImpl Sym;
882 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola806f0062013-06-05 01:33:53 +0000883 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +0000884}
885
Rafael Espindola3acea392014-06-12 21:46:39 +0000886std::error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
887 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000888 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000889 Res = getAnyRelocationType(RE);
890 return object_error::success;
891}
892
Rafael Espindola3acea392014-06-12 21:46:39 +0000893std::error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000894MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
895 SmallVectorImpl<char> &Result) const {
896 StringRef res;
897 uint64_t RType;
898 getRelocationType(Rel, RType);
899
900 unsigned Arch = this->getArch();
901
902 switch (Arch) {
903 case Triple::x86: {
904 static const char *const Table[] = {
905 "GENERIC_RELOC_VANILLA",
906 "GENERIC_RELOC_PAIR",
907 "GENERIC_RELOC_SECTDIFF",
908 "GENERIC_RELOC_PB_LA_PTR",
909 "GENERIC_RELOC_LOCAL_SECTDIFF",
910 "GENERIC_RELOC_TLV" };
911
Eric Christopher13250cb2013-12-06 02:33:38 +0000912 if (RType > 5)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000913 res = "Unknown";
914 else
915 res = Table[RType];
916 break;
917 }
918 case Triple::x86_64: {
919 static const char *const Table[] = {
920 "X86_64_RELOC_UNSIGNED",
921 "X86_64_RELOC_SIGNED",
922 "X86_64_RELOC_BRANCH",
923 "X86_64_RELOC_GOT_LOAD",
924 "X86_64_RELOC_GOT",
925 "X86_64_RELOC_SUBTRACTOR",
926 "X86_64_RELOC_SIGNED_1",
927 "X86_64_RELOC_SIGNED_2",
928 "X86_64_RELOC_SIGNED_4",
929 "X86_64_RELOC_TLV" };
930
931 if (RType > 9)
932 res = "Unknown";
933 else
934 res = Table[RType];
935 break;
936 }
937 case Triple::arm: {
938 static const char *const Table[] = {
939 "ARM_RELOC_VANILLA",
940 "ARM_RELOC_PAIR",
941 "ARM_RELOC_SECTDIFF",
942 "ARM_RELOC_LOCAL_SECTDIFF",
943 "ARM_RELOC_PB_LA_PTR",
944 "ARM_RELOC_BR24",
945 "ARM_THUMB_RELOC_BR22",
946 "ARM_THUMB_32BIT_BRANCH",
947 "ARM_RELOC_HALF",
948 "ARM_RELOC_HALF_SECTDIFF" };
949
950 if (RType > 9)
951 res = "Unknown";
952 else
953 res = Table[RType];
954 break;
955 }
Tim Northover00ed9962014-03-29 10:18:08 +0000956 case Triple::arm64:
957 case Triple::aarch64: {
958 static const char *const Table[] = {
959 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
960 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
961 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
962 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
963 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
964 "ARM64_RELOC_ADDEND"
965 };
966
967 if (RType >= array_lengthof(Table))
968 res = "Unknown";
969 else
970 res = Table[RType];
971 break;
972 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000973 case Triple::ppc: {
974 static const char *const Table[] = {
975 "PPC_RELOC_VANILLA",
976 "PPC_RELOC_PAIR",
977 "PPC_RELOC_BR14",
978 "PPC_RELOC_BR24",
979 "PPC_RELOC_HI16",
980 "PPC_RELOC_LO16",
981 "PPC_RELOC_HA16",
982 "PPC_RELOC_LO14",
983 "PPC_RELOC_SECTDIFF",
984 "PPC_RELOC_PB_LA_PTR",
985 "PPC_RELOC_HI16_SECTDIFF",
986 "PPC_RELOC_LO16_SECTDIFF",
987 "PPC_RELOC_HA16_SECTDIFF",
988 "PPC_RELOC_JBSR",
989 "PPC_RELOC_LO14_SECTDIFF",
990 "PPC_RELOC_LOCAL_SECTDIFF" };
991
Eric Christopher13250cb2013-12-06 02:33:38 +0000992 if (RType > 15)
993 res = "Unknown";
994 else
995 res = Table[RType];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000996 break;
997 }
998 case Triple::UnknownArch:
999 res = "Unknown";
1000 break;
1001 }
1002 Result.append(res.begin(), res.end());
1003 return object_error::success;
1004}
1005
Rafael Espindola3acea392014-06-12 21:46:39 +00001006std::error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +00001007MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +00001008 SmallVectorImpl<char> &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001009 MachO::any_relocation_info RE = getRelocation(Rel);
David Meyer2fc34c52012-03-01 01:36:50 +00001010
Rafael Espindola56f976f2013-04-18 18:08:55 +00001011 unsigned Arch = this->getArch();
Eric Christopher7b015c72011-04-22 03:19:48 +00001012
Rafael Espindola56f976f2013-04-18 18:08:55 +00001013 std::string fmtbuf;
1014 raw_string_ostream fmt(fmtbuf);
1015 unsigned Type = this->getAnyRelocationType(RE);
1016 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1017
1018 // Determine any addends that should be displayed with the relocation.
1019 // These require decoding the relocation type, which is triple-specific.
1020
1021 // X86_64 has entirely custom relocation types.
1022 if (Arch == Triple::x86_64) {
1023 bool isPCRel = getAnyRelocationPCRel(RE);
1024
1025 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001026 case MachO::X86_64_RELOC_GOT_LOAD:
1027 case MachO::X86_64_RELOC_GOT: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001028 printRelocationTargetName(this, RE, fmt);
1029 fmt << "@GOT";
1030 if (isPCRel) fmt << "PCREL";
1031 break;
1032 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001033 case MachO::X86_64_RELOC_SUBTRACTOR: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001034 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001035 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001036 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001037
Charles Davis8bdfafd2013-09-01 04:28:48 +00001038 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
Rafael Espindola56f976f2013-04-18 18:08:55 +00001039 // X86_64_RELOC_UNSIGNED.
1040 // NOTE: Scattered relocations don't exist on x86_64.
1041 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001042 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001043 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1044 "X86_64_RELOC_SUBTRACTOR.");
1045
Charles Davis8bdfafd2013-09-01 04:28:48 +00001046 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1047 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
Rafael Espindola56f976f2013-04-18 18:08:55 +00001048 printRelocationTargetName(this, RENext, fmt);
1049 fmt << "-";
1050 printRelocationTargetName(this, RE, fmt);
1051 break;
1052 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001053 case MachO::X86_64_RELOC_TLV:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001054 printRelocationTargetName(this, RE, fmt);
1055 fmt << "@TLV";
1056 if (isPCRel) fmt << "P";
1057 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001058 case MachO::X86_64_RELOC_SIGNED_1:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001059 printRelocationTargetName(this, RE, fmt);
1060 fmt << "-1";
1061 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001062 case MachO::X86_64_RELOC_SIGNED_2:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001063 printRelocationTargetName(this, RE, fmt);
1064 fmt << "-2";
1065 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001066 case MachO::X86_64_RELOC_SIGNED_4:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001067 printRelocationTargetName(this, RE, fmt);
1068 fmt << "-4";
1069 break;
1070 default:
1071 printRelocationTargetName(this, RE, fmt);
1072 break;
1073 }
1074 // X86 and ARM share some relocation types in common.
David Fangb88cdf62013-08-08 20:14:40 +00001075 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1076 Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001077 // Generic relocation types...
1078 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001079 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001080 return object_error::success;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001081 case MachO::GENERIC_RELOC_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001082 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001083 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001084 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001085
1086 // X86 sect diff's must be followed by a relocation of type
1087 // GENERIC_RELOC_PAIR.
1088 unsigned RType = getAnyRelocationType(RENext);
1089
Charles Davis8bdfafd2013-09-01 04:28:48 +00001090 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001091 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1092 "GENERIC_RELOC_SECTDIFF.");
1093
1094 printRelocationTargetName(this, RE, fmt);
1095 fmt << "-";
1096 printRelocationTargetName(this, RENext, fmt);
1097 break;
1098 }
1099 }
1100
David Fangb88cdf62013-08-08 20:14:40 +00001101 if (Arch == Triple::x86 || Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001102 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001103 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001104 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001105 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001106 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001107
1108 // X86 sect diff's must be followed by a relocation of type
1109 // GENERIC_RELOC_PAIR.
1110 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001111 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001112 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1113 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1114
1115 printRelocationTargetName(this, RE, fmt);
1116 fmt << "-";
1117 printRelocationTargetName(this, RENext, fmt);
1118 break;
1119 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001120 case MachO::GENERIC_RELOC_TLV: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001121 printRelocationTargetName(this, RE, fmt);
1122 fmt << "@TLV";
1123 if (IsPCRel) fmt << "P";
1124 break;
1125 }
1126 default:
1127 printRelocationTargetName(this, RE, fmt);
1128 }
1129 } else { // ARM-specific relocations
1130 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001131 case MachO::ARM_RELOC_HALF:
1132 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001133 // Half relocations steal a bit from the length field to encode
1134 // whether this is an upper16 or a lower16 relocation.
1135 bool isUpper = getAnyRelocationLength(RE) >> 1;
1136
1137 if (isUpper)
1138 fmt << ":upper16:(";
1139 else
1140 fmt << ":lower16:(";
1141 printRelocationTargetName(this, RE, fmt);
1142
1143 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001144 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001145 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001146
1147 // ARM half relocs must be followed by a relocation of type
1148 // ARM_RELOC_PAIR.
1149 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001150 if (RType != MachO::ARM_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001151 report_fatal_error("Expected ARM_RELOC_PAIR after "
Charles Davis8bdfafd2013-09-01 04:28:48 +00001152 "ARM_RELOC_HALF");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001153
1154 // NOTE: The half of the target virtual address is stashed in the
1155 // address field of the secondary relocation, but we can't reverse
1156 // engineer the constant offset from it without decoding the movw/movt
1157 // instruction to find the other half in its immediate field.
1158
1159 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1160 // symbol/section pointer of the follow-on relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001161 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001162 fmt << "-";
1163 printRelocationTargetName(this, RENext, fmt);
1164 }
1165
1166 fmt << ")";
1167 break;
1168 }
1169 default: {
1170 printRelocationTargetName(this, RE, fmt);
1171 }
1172 }
1173 }
1174 } else
1175 printRelocationTargetName(this, RE, fmt);
1176
1177 fmt.flush();
1178 Result.append(fmtbuf.begin(), fmtbuf.end());
1179 return object_error::success;
1180}
1181
Rafael Espindola3acea392014-06-12 21:46:39 +00001182std::error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1183 bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001184 unsigned Arch = getArch();
1185 uint64_t Type;
1186 getRelocationType(Rel, Type);
1187
1188 Result = false;
1189
1190 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1191 // is always hidden.
David Fangb88cdf62013-08-08 20:14:40 +00001192 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001193 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001194 } else if (Arch == Triple::x86_64) {
1195 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
Eric Christopher1ff26ab62013-07-22 22:25:09 +00001196 // an X86_64_RELOC_SUBTRACTOR.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001197 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001198 DataRefImpl RelPrev = Rel;
1199 RelPrev.d.a--;
1200 uint64_t PrevType;
1201 getRelocationType(RelPrev, PrevType);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001202 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001203 Result = true;
1204 }
1205 }
1206
1207 return object_error::success;
1208}
1209
Rafael Espindola3acea392014-06-12 21:46:39 +00001210std::error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1211 LibraryRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001212 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1213}
1214
Rafael Espindola3acea392014-06-12 21:46:39 +00001215std::error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1216 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001217 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1218}
1219
Kevin Enderby980b2582014-06-05 21:21:57 +00001220//
1221// guessLibraryShortName() is passed a name of a dynamic library and returns a
1222// guess on what the short name is. Then name is returned as a substring of the
1223// StringRef Name passed in. The name of the dynamic library is recognized as
1224// a framework if it has one of the two following forms:
1225// Foo.framework/Versions/A/Foo
1226// Foo.framework/Foo
1227// Where A and Foo can be any string. And may contain a trailing suffix
1228// starting with an underbar. If the Name is recognized as a framework then
1229// isFramework is set to true else it is set to false. If the Name has a
1230// suffix then Suffix is set to the substring in Name that contains the suffix
1231// else it is set to a NULL StringRef.
1232//
1233// The Name of the dynamic library is recognized as a library name if it has
1234// one of the two following forms:
1235// libFoo.A.dylib
1236// libFoo.dylib
1237// The library may have a suffix trailing the name Foo of the form:
1238// libFoo_profile.A.dylib
1239// libFoo_profile.dylib
1240//
1241// The Name of the dynamic library is also recognized as a library name if it
1242// has the following form:
1243// Foo.qtx
1244//
1245// If the Name of the dynamic library is none of the forms above then a NULL
1246// StringRef is returned.
1247//
1248StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
1249 bool &isFramework,
1250 StringRef &Suffix) {
1251 StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
1252 size_t a, b, c, d, Idx;
1253
1254 isFramework = false;
1255 Suffix = StringRef();
1256
1257 // Pull off the last component and make Foo point to it
1258 a = Name.rfind('/');
1259 if (a == Name.npos || a == 0)
1260 goto guess_library;
1261 Foo = Name.slice(a+1, Name.npos);
1262
1263 // Look for a suffix starting with a '_'
1264 Idx = Foo.rfind('_');
1265 if (Idx != Foo.npos && Foo.size() >= 2) {
1266 Suffix = Foo.slice(Idx, Foo.npos);
1267 Foo = Foo.slice(0, Idx);
1268 }
1269
1270 // First look for the form Foo.framework/Foo
1271 b = Name.rfind('/', a);
1272 if (b == Name.npos)
1273 Idx = 0;
1274 else
1275 Idx = b+1;
1276 F = Name.slice(Idx, Idx + Foo.size());
1277 DotFramework = Name.slice(Idx + Foo.size(),
1278 Idx + Foo.size() + sizeof(".framework/")-1);
1279 if (F == Foo && DotFramework == ".framework/") {
1280 isFramework = true;
1281 return Foo;
1282 }
1283
1284 // Next look for the form Foo.framework/Versions/A/Foo
1285 if (b == Name.npos)
1286 goto guess_library;
1287 c = Name.rfind('/', b);
1288 if (c == Name.npos || c == 0)
1289 goto guess_library;
1290 V = Name.slice(c+1, Name.npos);
1291 if (!V.startswith("Versions/"))
1292 goto guess_library;
1293 d = Name.rfind('/', c);
1294 if (d == Name.npos)
1295 Idx = 0;
1296 else
1297 Idx = d+1;
1298 F = Name.slice(Idx, Idx + Foo.size());
1299 DotFramework = Name.slice(Idx + Foo.size(),
1300 Idx + Foo.size() + sizeof(".framework/")-1);
1301 if (F == Foo && DotFramework == ".framework/") {
1302 isFramework = true;
1303 return Foo;
1304 }
1305
1306guess_library:
1307 // pull off the suffix after the "." and make a point to it
1308 a = Name.rfind('.');
1309 if (a == Name.npos || a == 0)
1310 return StringRef();
1311 Dylib = Name.slice(a, Name.npos);
1312 if (Dylib != ".dylib")
1313 goto guess_qtx;
1314
1315 // First pull off the version letter for the form Foo.A.dylib if any.
1316 if (a >= 3) {
1317 Dot = Name.slice(a-2, a-1);
1318 if (Dot == ".")
1319 a = a - 2;
1320 }
1321
1322 b = Name.rfind('/', a);
1323 if (b == Name.npos)
1324 b = 0;
1325 else
1326 b = b+1;
1327 // ignore any suffix after an underbar like Foo_profile.A.dylib
1328 Idx = Name.find('_', b);
1329 if (Idx != Name.npos && Idx != b) {
1330 Lib = Name.slice(b, Idx);
1331 Suffix = Name.slice(Idx, a);
1332 }
1333 else
1334 Lib = Name.slice(b, a);
1335 // There are incorrect library names of the form:
1336 // libATS.A_profile.dylib so check for these.
1337 if (Lib.size() >= 3) {
1338 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
1339 if (Dot == ".")
1340 Lib = Lib.slice(0, Lib.size()-2);
1341 }
1342 return Lib;
1343
1344guess_qtx:
1345 Qtx = Name.slice(a, Name.npos);
1346 if (Qtx != ".qtx")
1347 return StringRef();
1348 b = Name.rfind('/', a);
1349 if (b == Name.npos)
1350 Lib = Name.slice(0, a);
1351 else
1352 Lib = Name.slice(b+1, a);
1353 // There are library names of the form: QT.A.qtx so check for these.
1354 if (Lib.size() >= 3) {
1355 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
1356 if (Dot == ".")
1357 Lib = Lib.slice(0, Lib.size()-2);
1358 }
1359 return Lib;
1360}
1361
1362// getLibraryShortNameByIndex() is used to get the short name of the library
1363// for an undefined symbol in a linked Mach-O binary that was linked with the
1364// normal two-level namespace default (that is MH_TWOLEVEL in the header).
1365// It is passed the index (0 - based) of the library as translated from
1366// GET_LIBRARY_ORDINAL (1 - based).
Rafael Espindola3acea392014-06-12 21:46:39 +00001367std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
1368 StringRef &Res) {
Kevin Enderby980b2582014-06-05 21:21:57 +00001369 if (Index >= Libraries.size())
1370 return object_error::parse_failed;
1371
1372 MachO::dylib_command D =
1373 getStruct<MachO::dylib_command>(this, Libraries[Index]);
1374 if (D.dylib.name >= D.cmdsize)
1375 return object_error::parse_failed;
1376
1377 // If the cache of LibrariesShortNames is not built up do that first for
1378 // all the Libraries.
1379 if (LibrariesShortNames.size() == 0) {
1380 for (unsigned i = 0; i < Libraries.size(); i++) {
1381 MachO::dylib_command D =
1382 getStruct<MachO::dylib_command>(this, Libraries[i]);
1383 if (D.dylib.name >= D.cmdsize) {
1384 LibrariesShortNames.push_back(StringRef());
1385 continue;
1386 }
Kevin Enderby4eff6cd2014-06-20 18:07:34 +00001387 const char *P = (const char *)(Libraries[i]) + D.dylib.name;
Kevin Enderby980b2582014-06-05 21:21:57 +00001388 StringRef Name = StringRef(P);
1389 StringRef Suffix;
1390 bool isFramework;
1391 StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
1392 if (shortName == StringRef())
1393 LibrariesShortNames.push_back(Name);
1394 else
1395 LibrariesShortNames.push_back(shortName);
1396 }
1397 }
1398
1399 Res = LibrariesShortNames[Index];
1400 return object_error::success;
1401}
1402
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001403basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
Lang Hames36072da2014-05-12 21:39:59 +00001404 return getSymbolByIndex(0);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001405}
1406
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001407basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001408 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001409 if (!SymtabLoadCmd)
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001410 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola75c30362013-04-24 19:47:55 +00001411
Charles Davis8bdfafd2013-09-01 04:28:48 +00001412 MachO::symtab_command Symtab = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +00001413 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001414 sizeof(MachO::nlist_64) :
1415 sizeof(MachO::nlist);
1416 unsigned Offset = Symtab.symoff +
1417 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001418 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001419 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001420}
1421
Lang Hames36072da2014-05-12 21:39:59 +00001422basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
1423 DataRefImpl DRI;
1424 if (!SymtabLoadCmd)
1425 return basic_symbol_iterator(SymbolRef(DRI, this));
1426
1427 MachO::symtab_command Symtab = getSymtabLoadCommand();
1428 assert(Index < Symtab.nsyms && "Requested symbol index is out of range.");
1429 unsigned SymbolTableEntrySize =
1430 is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
1431 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1432 DRI.p += Index * SymbolTableEntrySize;
1433 return basic_symbol_iterator(SymbolRef(DRI, this));
1434}
1435
Rafael Espindolab5155a52014-02-10 20:24:04 +00001436section_iterator MachOObjectFile::section_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001437 DataRefImpl DRI;
1438 return section_iterator(SectionRef(DRI, this));
1439}
1440
Rafael Espindolab5155a52014-02-10 20:24:04 +00001441section_iterator MachOObjectFile::section_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001442 DataRefImpl DRI;
1443 DRI.d.a = Sections.size();
1444 return section_iterator(SectionRef(DRI, this));
1445}
1446
Rafael Espindolab5155a52014-02-10 20:24:04 +00001447library_iterator MachOObjectFile::needed_library_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001448 // TODO: implement
1449 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1450}
1451
Rafael Espindolab5155a52014-02-10 20:24:04 +00001452library_iterator MachOObjectFile::needed_library_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001453 // TODO: implement
1454 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1455}
1456
1457uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola60689982013-04-07 19:05:30 +00001458 return is64Bit() ? 8 : 4;
Eric Christopher7b015c72011-04-22 03:19:48 +00001459}
1460
Rafael Espindola56f976f2013-04-18 18:08:55 +00001461StringRef MachOObjectFile::getFileFormatName() const {
1462 unsigned CPUType = getCPUType(this);
1463 if (!is64Bit()) {
1464 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001465 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001466 return "Mach-O 32-bit i386";
Charles Davis74ec8b02013-08-27 05:00:13 +00001467 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001468 return "Mach-O arm";
Charles Davis74ec8b02013-08-27 05:00:13 +00001469 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001470 return "Mach-O 32-bit ppc";
1471 default:
Charles Davis74ec8b02013-08-27 05:00:13 +00001472 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
Rafael Espindola56f976f2013-04-18 18:08:55 +00001473 "64-bit object file when we're not 64-bit?");
1474 return "Mach-O 32-bit unknown";
1475 }
1476 }
1477
1478 // Make sure the cpu type has the correct mask.
Charles Davis74ec8b02013-08-27 05:00:13 +00001479 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1480 == llvm::MachO::CPU_ARCH_ABI64 &&
Eric Christopher1d62c252013-07-22 22:25:07 +00001481 "32-bit object file when we're 64-bit?");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001482
1483 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001484 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001485 return "Mach-O 64-bit x86-64";
Tim Northover00ed9962014-03-29 10:18:08 +00001486 case llvm::MachO::CPU_TYPE_ARM64:
1487 return "Mach-O arm64";
Charles Davis74ec8b02013-08-27 05:00:13 +00001488 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001489 return "Mach-O 64-bit ppc64";
1490 default:
1491 return "Mach-O 64-bit unknown";
1492 }
1493}
1494
Alexey Samsonove6388e62013-06-18 15:03:28 +00001495Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1496 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001497 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001498 return Triple::x86;
Charles Davis74ec8b02013-08-27 05:00:13 +00001499 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001500 return Triple::x86_64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001501 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001502 return Triple::arm;
Tim Northover00ed9962014-03-29 10:18:08 +00001503 case llvm::MachO::CPU_TYPE_ARM64:
1504 return Triple::arm64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001505 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001506 return Triple::ppc;
Charles Davis74ec8b02013-08-27 05:00:13 +00001507 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001508 return Triple::ppc64;
1509 default:
1510 return Triple::UnknownArch;
1511 }
1512}
1513
Alexey Samsonove6388e62013-06-18 15:03:28 +00001514unsigned MachOObjectFile::getArch() const {
1515 return getArch(getCPUType(this));
1516}
1517
Rafael Espindola56f976f2013-04-18 18:08:55 +00001518StringRef MachOObjectFile::getLoadName() const {
1519 // TODO: Implement
Charles Davis1827bd82013-08-27 05:38:30 +00001520 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001521}
1522
Rui Ueyamabc654b12013-09-27 21:47:05 +00001523relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001524 DataRefImpl DRI;
1525 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001526 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001527}
1528
Rui Ueyamabc654b12013-09-27 21:47:05 +00001529relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001530 DataRefImpl DRI;
1531 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001532 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001533}
1534
Kevin Enderby273ae012013-06-06 17:20:50 +00001535dice_iterator MachOObjectFile::begin_dices() const {
1536 DataRefImpl DRI;
1537 if (!DataInCodeLoadCmd)
1538 return dice_iterator(DiceRef(DRI, this));
1539
Charles Davis8bdfafd2013-09-01 04:28:48 +00001540 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1541 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00001542 return dice_iterator(DiceRef(DRI, this));
1543}
1544
1545dice_iterator MachOObjectFile::end_dices() const {
1546 DataRefImpl DRI;
1547 if (!DataInCodeLoadCmd)
1548 return dice_iterator(DiceRef(DRI, this));
1549
Charles Davis8bdfafd2013-09-01 04:28:48 +00001550 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1551 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00001552 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1553 return dice_iterator(DiceRef(DRI, this));
1554}
1555
Rafael Espindola56f976f2013-04-18 18:08:55 +00001556StringRef
1557MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1558 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1559 return parseSegmentOrSectionName(Raw.data());
1560}
1561
1562ArrayRef<char>
1563MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001564 const section_base *Base =
1565 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1566 return ArrayRef<char>(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001567}
1568
1569ArrayRef<char>
1570MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001571 const section_base *Base =
1572 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1573 return ArrayRef<char>(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001574}
1575
1576bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00001577MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001578 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001579 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001580 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001581 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001582}
1583
Eric Christopher1d62c252013-07-22 22:25:07 +00001584unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001585 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001586 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001587 return RE.r_word1 & 0xffffff;
1588 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001589}
1590
Eric Christopher1d62c252013-07-22 22:25:07 +00001591bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001592 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001593 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001594 return (RE.r_word1 >> 27) & 1;
1595 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001596}
1597
Eric Christopher1d62c252013-07-22 22:25:07 +00001598bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001599 const MachO::any_relocation_info &RE) const {
1600 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001601}
1602
Eric Christopher1d62c252013-07-22 22:25:07 +00001603uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001604 const MachO::any_relocation_info &RE) const {
1605 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001606}
1607
Eric Christopher1d62c252013-07-22 22:25:07 +00001608unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001609 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001610 if (isRelocationScattered(RE))
1611 return getScatteredRelocationAddress(RE);
1612 return getPlainRelocationAddress(RE);
1613}
1614
Charles Davis8bdfafd2013-09-01 04:28:48 +00001615unsigned MachOObjectFile::getAnyRelocationPCRel(
1616 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001617 if (isRelocationScattered(RE))
1618 return getScatteredRelocationPCRel(this, RE);
1619 return getPlainRelocationPCRel(this, RE);
1620}
1621
Eric Christopher1d62c252013-07-22 22:25:07 +00001622unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001623 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001624 if (isRelocationScattered(RE))
1625 return getScatteredRelocationLength(RE);
1626 return getPlainRelocationLength(this, RE);
1627}
1628
1629unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00001630MachOObjectFile::getAnyRelocationType(
1631 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001632 if (isRelocationScattered(RE))
1633 return getScatteredRelocationType(RE);
1634 return getPlainRelocationType(this, RE);
1635}
1636
Rafael Espindola52501032013-04-30 15:40:54 +00001637SectionRef
Charles Davis8bdfafd2013-09-01 04:28:48 +00001638MachOObjectFile::getRelocationSection(
1639 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00001640 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
Rafael Espindolab5155a52014-02-10 20:24:04 +00001641 return *section_end();
Rafael Espindola52501032013-04-30 15:40:54 +00001642 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1643 DataRefImpl DRI;
1644 DRI.d.a = SecNum;
1645 return SectionRef(DRI, this);
1646}
1647
Rafael Espindola56f976f2013-04-18 18:08:55 +00001648MachOObjectFile::LoadCommandInfo
1649MachOObjectFile::getFirstLoadCommandInfo() const {
1650 MachOObjectFile::LoadCommandInfo Load;
1651
Charles Davis8bdfafd2013-09-01 04:28:48 +00001652 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1653 sizeof(MachO::mach_header);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001654 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001655 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001656 return Load;
1657}
1658
1659MachOObjectFile::LoadCommandInfo
1660MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1661 MachOObjectFile::LoadCommandInfo Next;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001662 Next.Ptr = L.Ptr + L.C.cmdsize;
1663 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001664 return Next;
1665}
1666
Charles Davis8bdfafd2013-09-01 04:28:48 +00001667MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1668 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001669}
1670
Charles Davis8bdfafd2013-09-01 04:28:48 +00001671MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1672 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001673}
1674
Charles Davis8bdfafd2013-09-01 04:28:48 +00001675MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00001676 unsigned Index) const {
1677 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001678 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001679}
1680
Charles Davis8bdfafd2013-09-01 04:28:48 +00001681MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1682 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001683 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001684 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001685}
1686
Charles Davis8bdfafd2013-09-01 04:28:48 +00001687MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00001688MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001689 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001690 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001691}
1692
Charles Davis8bdfafd2013-09-01 04:28:48 +00001693MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00001694MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001695 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001696 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001697}
1698
Charles Davis8bdfafd2013-09-01 04:28:48 +00001699MachO::linkedit_data_command
1700MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1701 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001702}
1703
Charles Davis8bdfafd2013-09-01 04:28:48 +00001704MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001705MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001706 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001707}
1708
Charles Davis8bdfafd2013-09-01 04:28:48 +00001709MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00001710MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001711 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001712}
1713
Charles Davis8bdfafd2013-09-01 04:28:48 +00001714MachO::linker_options_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001715MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001716 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001717}
1718
Jim Grosbach448334a2014-03-18 22:09:05 +00001719MachO::version_min_command
1720MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
1721 return getStruct<MachO::version_min_command>(this, L.Ptr);
1722}
1723
Charles Davis8bdfafd2013-09-01 04:28:48 +00001724MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001725MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +00001726 DataRefImpl Sec;
1727 Sec.d.a = Rel.d.a;
1728 uint32_t Offset;
1729 if (is64Bit()) {
1730 MachO::section_64 Sect = getSection64(Sec);
1731 Offset = Sect.reloff;
1732 } else {
1733 MachO::section Sect = getSection(Sec);
1734 Offset = Sect.reloff;
1735 }
1736
1737 auto P = reinterpret_cast<const MachO::any_relocation_info *>(
1738 getPtr(this, Offset)) + Rel.d.b;
1739 return getStruct<MachO::any_relocation_info>(
1740 this, reinterpret_cast<const char *>(P));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001741}
1742
Charles Davis8bdfafd2013-09-01 04:28:48 +00001743MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00001744MachOObjectFile::getDice(DataRefImpl Rel) const {
1745 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001746 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00001747}
1748
Charles Davis8bdfafd2013-09-01 04:28:48 +00001749MachO::mach_header MachOObjectFile::getHeader() const {
1750 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001751}
1752
Charles Davis8bdfafd2013-09-01 04:28:48 +00001753MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1754 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001755}
1756
Charles Davis8bdfafd2013-09-01 04:28:48 +00001757uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1758 const MachO::dysymtab_command &DLC,
1759 unsigned Index) const {
1760 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1761 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001762}
1763
Charles Davis8bdfafd2013-09-01 04:28:48 +00001764MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00001765MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1766 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001767 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1768 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001769}
1770
Charles Davis8bdfafd2013-09-01 04:28:48 +00001771MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1772 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001773}
1774
Charles Davis8bdfafd2013-09-01 04:28:48 +00001775MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1776 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001777}
1778
Charles Davis8bdfafd2013-09-01 04:28:48 +00001779MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00001780MachOObjectFile::getDataInCodeLoadCommand() const {
1781 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00001782 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00001783
1784 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001785 MachO::linkedit_data_command Cmd;
1786 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1787 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1788 Cmd.dataoff = 0;
1789 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00001790 return Cmd;
1791}
1792
Rafael Espindola6e040c02013-04-26 20:07:33 +00001793StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001794 MachO::symtab_command S = getSymtabLoadCommand();
1795 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001796}
1797
Rafael Espindola56f976f2013-04-18 18:08:55 +00001798bool MachOObjectFile::is64Bit() const {
1799 return getType() == getMachOType(false, true) ||
1800 getType() == getMachOType(true, true);
1801}
1802
1803void MachOObjectFile::ReadULEB128s(uint64_t Index,
1804 SmallVectorImpl<uint64_t> &Out) const {
1805 DataExtractor extractor(ObjectFile::getData(), true, 0);
1806
1807 uint32_t offset = Index;
1808 uint64_t data = 0;
1809 while (uint64_t delta = extractor.getULEB128(&offset)) {
1810 data += delta;
1811 Out.push_back(data);
1812 }
1813}
1814
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001815ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer,
1816 bool BufferOwned) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001817 StringRef Magic = Buffer->getBuffer().slice(0, 4);
Rafael Espindola3acea392014-06-12 21:46:39 +00001818 std::error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001819 std::unique_ptr<MachOObjectFile> Ret;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001820 if (Magic == "\xFE\xED\xFA\xCE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001821 Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001822 else if (Magic == "\xCE\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001823 Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001824 else if (Magic == "\xFE\xED\xFA\xCF")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001825 Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001826 else if (Magic == "\xCF\xFA\xED\xFE")
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001827 Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned));
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001828 else {
1829 delete Buffer;
Rafael Espindola692410e2014-01-21 23:06:54 +00001830 return object_error::parse_failed;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001831 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001832
Rafael Espindola692410e2014-01-21 23:06:54 +00001833 if (EC)
1834 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +00001835 return Ret.release();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001836}
1837
Owen Anderson27c579d2011-10-11 17:32:27 +00001838} // end namespace object
Eric Christopher7b015c72011-04-22 03:19:48 +00001839} // end namespace llvm