blob: 4919114d2349752607514bc2fbe5c070ee31de3d [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,
Alp Tokere69170a2014-06-26 22:52:05 +0000304 raw_string_ostream &fmt) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000305 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 Espindola2e60ca92014-06-24 13:56:32 +0000425MachOObjectFile::MachOObjectFile(std::unique_ptr<MemoryBuffer> Object,
426 bool IsLittleEndian, bool Is64bits,
427 std::error_code &EC)
428 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), std::move(Object)),
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);
Tim Northover07f99fb2014-07-04 10:57:56 +0000871 if (isRelocationScattered(RE))
872 return symbol_end();
873
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
Rafael Espindola3acea392014-06-12 21:46:39 +0000889std::error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
890 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
Rafael Espindola3acea392014-06-12 21:46:39 +0000896std::error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000897MachOObjectFile::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 Espindola3acea392014-06-12 21:46:39 +00001009std::error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +00001010MachOObjectFile::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
Alp Tokere69170a2014-06-26 22:52:05 +00001016 std::string fmtbuf;
1017 raw_string_ostream fmt(fmtbuf);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001018 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
Alp Tokere69170a2014-06-26 22:52:05 +00001180 fmt.flush();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001181 Result.append(fmtbuf.begin(), fmtbuf.end());
1182 return object_error::success;
1183}
1184
Rafael Espindola3acea392014-06-12 21:46:39 +00001185std::error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1186 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
Rafael Espindola3acea392014-06-12 21:46:39 +00001213std::error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1214 LibraryRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001215 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1216}
1217
Rafael Espindola3acea392014-06-12 21:46:39 +00001218std::error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1219 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).
Rafael Espindola3acea392014-06-12 21:46:39 +00001370std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
1371 StringRef &Res) {
Kevin Enderby980b2582014-06-05 21:21:57 +00001372 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 }
Kevin Enderby4eff6cd2014-06-20 18:07:34 +00001390 const char *P = (const char *)(Libraries[i]) + D.dylib.name;
Kevin Enderby980b2582014-06-05 21:21:57 +00001391 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
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001517Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType) {
1518 switch (CPUType) {
1519 case MachO::CPU_TYPE_I386:
1520 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1521 case MachO::CPU_SUBTYPE_I386_ALL:
1522 return Triple("i386-apple-darwin");
1523 default:
1524 return Triple();
1525 }
1526 case MachO::CPU_TYPE_X86_64:
1527 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1528 case MachO::CPU_SUBTYPE_X86_64_ALL:
1529 return Triple("x86_64-apple-darwin");
1530 case MachO::CPU_SUBTYPE_X86_64_H:
1531 return Triple("x86_64h-apple-darwin");
1532 default:
1533 return Triple();
1534 }
1535 case MachO::CPU_TYPE_ARM:
1536 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1537 case MachO::CPU_SUBTYPE_ARM_V4T:
1538 return Triple("armv4t-apple-darwin");
1539 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1540 return Triple("armv5e-apple-darwin");
1541 case MachO::CPU_SUBTYPE_ARM_V6:
1542 return Triple("armv6-apple-darwin");
1543 case MachO::CPU_SUBTYPE_ARM_V6M:
1544 return Triple("armv6m-apple-darwin");
1545 case MachO::CPU_SUBTYPE_ARM_V7EM:
1546 return Triple("armv7em-apple-darwin");
1547 case MachO::CPU_SUBTYPE_ARM_V7K:
1548 return Triple("armv7k-apple-darwin");
1549 case MachO::CPU_SUBTYPE_ARM_V7M:
1550 return Triple("armv7m-apple-darwin");
1551 case MachO::CPU_SUBTYPE_ARM_V7S:
1552 return Triple("armv7s-apple-darwin");
1553 default:
1554 return Triple();
1555 }
1556 case MachO::CPU_TYPE_ARM64:
1557 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1558 case MachO::CPU_SUBTYPE_ARM64_ALL:
1559 return Triple("arm64-apple-darwin");
1560 default:
1561 return Triple();
1562 }
1563 case MachO::CPU_TYPE_POWERPC:
1564 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1565 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1566 return Triple("ppc-apple-darwin");
1567 default:
1568 return Triple();
1569 }
1570 case MachO::CPU_TYPE_POWERPC64:
Reid Kleckner4da3d572014-06-30 20:12:59 +00001571 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001572 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1573 return Triple("ppc64-apple-darwin");
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001574 default:
1575 return Triple();
1576 }
1577 default:
1578 return Triple();
1579 }
1580}
1581
1582Triple MachOObjectFile::getHostArch() {
1583 return Triple(sys::getDefaultTargetTriple());
1584}
1585
1586Triple MachOObjectFile::getArch(StringRef ArchFlag) {
1587 if (ArchFlag == "i386")
1588 return Triple("i386-apple-darwin");
1589 else if (ArchFlag == "x86_64")
1590 return Triple("x86_64-apple-darwin");
1591 else if (ArchFlag == "x86_64h")
1592 return Triple("x86_64h-apple-darwin");
1593 else if (ArchFlag == "armv4t" || ArchFlag == "arm")
1594 return Triple("armv4t-apple-darwin");
1595 else if (ArchFlag == "armv5e")
1596 return Triple("armv5e-apple-darwin");
1597 else if (ArchFlag == "armv6")
1598 return Triple("armv6-apple-darwin");
1599 else if (ArchFlag == "armv6m")
1600 return Triple("armv6m-apple-darwin");
1601 else if (ArchFlag == "armv7em")
1602 return Triple("armv7em-apple-darwin");
1603 else if (ArchFlag == "armv7k")
1604 return Triple("armv7k-apple-darwin");
1605 else if (ArchFlag == "armv7k")
1606 return Triple("armv7m-apple-darwin");
1607 else if (ArchFlag == "armv7s")
1608 return Triple("armv7s-apple-darwin");
1609 else if (ArchFlag == "arm64")
1610 return Triple("arm64-apple-darwin");
1611 else if (ArchFlag == "ppc")
1612 return Triple("ppc-apple-darwin");
1613 else if (ArchFlag == "ppc64")
1614 return Triple("ppc64-apple-darwin");
1615 else
1616 return Triple();
1617}
1618
Alexey Samsonove6388e62013-06-18 15:03:28 +00001619unsigned MachOObjectFile::getArch() const {
1620 return getArch(getCPUType(this));
1621}
1622
Rafael Espindola56f976f2013-04-18 18:08:55 +00001623StringRef MachOObjectFile::getLoadName() const {
1624 // TODO: Implement
Charles Davis1827bd82013-08-27 05:38:30 +00001625 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001626}
1627
Rui Ueyamabc654b12013-09-27 21:47:05 +00001628relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001629 DataRefImpl DRI;
1630 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001631 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001632}
1633
Rui Ueyamabc654b12013-09-27 21:47:05 +00001634relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001635 DataRefImpl DRI;
1636 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001637 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001638}
1639
Kevin Enderby273ae012013-06-06 17:20:50 +00001640dice_iterator MachOObjectFile::begin_dices() const {
1641 DataRefImpl DRI;
1642 if (!DataInCodeLoadCmd)
1643 return dice_iterator(DiceRef(DRI, this));
1644
Charles Davis8bdfafd2013-09-01 04:28:48 +00001645 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1646 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00001647 return dice_iterator(DiceRef(DRI, this));
1648}
1649
1650dice_iterator MachOObjectFile::end_dices() const {
1651 DataRefImpl DRI;
1652 if (!DataInCodeLoadCmd)
1653 return dice_iterator(DiceRef(DRI, this));
1654
Charles Davis8bdfafd2013-09-01 04:28:48 +00001655 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1656 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00001657 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1658 return dice_iterator(DiceRef(DRI, this));
1659}
1660
Rafael Espindola56f976f2013-04-18 18:08:55 +00001661StringRef
1662MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1663 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1664 return parseSegmentOrSectionName(Raw.data());
1665}
1666
1667ArrayRef<char>
1668MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001669 const section_base *Base =
1670 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1671 return ArrayRef<char>(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001672}
1673
1674ArrayRef<char>
1675MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001676 const section_base *Base =
1677 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1678 return ArrayRef<char>(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001679}
1680
1681bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00001682MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001683 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001684 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001685 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001686 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001687}
1688
Eric Christopher1d62c252013-07-22 22:25:07 +00001689unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001690 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001691 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001692 return RE.r_word1 & 0xffffff;
1693 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001694}
1695
Eric Christopher1d62c252013-07-22 22:25:07 +00001696bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001697 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001698 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001699 return (RE.r_word1 >> 27) & 1;
1700 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001701}
1702
Eric Christopher1d62c252013-07-22 22:25:07 +00001703bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001704 const MachO::any_relocation_info &RE) const {
1705 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001706}
1707
Eric Christopher1d62c252013-07-22 22:25:07 +00001708uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001709 const MachO::any_relocation_info &RE) const {
1710 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001711}
1712
Eric Christopher1d62c252013-07-22 22:25:07 +00001713unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001714 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001715 if (isRelocationScattered(RE))
1716 return getScatteredRelocationAddress(RE);
1717 return getPlainRelocationAddress(RE);
1718}
1719
Charles Davis8bdfafd2013-09-01 04:28:48 +00001720unsigned MachOObjectFile::getAnyRelocationPCRel(
1721 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001722 if (isRelocationScattered(RE))
1723 return getScatteredRelocationPCRel(this, RE);
1724 return getPlainRelocationPCRel(this, RE);
1725}
1726
Eric Christopher1d62c252013-07-22 22:25:07 +00001727unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001728 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001729 if (isRelocationScattered(RE))
1730 return getScatteredRelocationLength(RE);
1731 return getPlainRelocationLength(this, RE);
1732}
1733
1734unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00001735MachOObjectFile::getAnyRelocationType(
1736 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001737 if (isRelocationScattered(RE))
1738 return getScatteredRelocationType(RE);
1739 return getPlainRelocationType(this, RE);
1740}
1741
Rafael Espindola52501032013-04-30 15:40:54 +00001742SectionRef
Charles Davis8bdfafd2013-09-01 04:28:48 +00001743MachOObjectFile::getRelocationSection(
1744 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00001745 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
Rafael Espindolab5155a52014-02-10 20:24:04 +00001746 return *section_end();
Rafael Espindola52501032013-04-30 15:40:54 +00001747 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1748 DataRefImpl DRI;
1749 DRI.d.a = SecNum;
1750 return SectionRef(DRI, this);
1751}
1752
Rafael Espindola56f976f2013-04-18 18:08:55 +00001753MachOObjectFile::LoadCommandInfo
1754MachOObjectFile::getFirstLoadCommandInfo() const {
1755 MachOObjectFile::LoadCommandInfo Load;
1756
Charles Davis8bdfafd2013-09-01 04:28:48 +00001757 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1758 sizeof(MachO::mach_header);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001759 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001760 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001761 return Load;
1762}
1763
1764MachOObjectFile::LoadCommandInfo
1765MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1766 MachOObjectFile::LoadCommandInfo Next;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001767 Next.Ptr = L.Ptr + L.C.cmdsize;
1768 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001769 return Next;
1770}
1771
Charles Davis8bdfafd2013-09-01 04:28:48 +00001772MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1773 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001774}
1775
Charles Davis8bdfafd2013-09-01 04:28:48 +00001776MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1777 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001778}
1779
Charles Davis8bdfafd2013-09-01 04:28:48 +00001780MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00001781 unsigned Index) const {
1782 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001783 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001784}
1785
Charles Davis8bdfafd2013-09-01 04:28:48 +00001786MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1787 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001788 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001789 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001790}
1791
Charles Davis8bdfafd2013-09-01 04:28:48 +00001792MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00001793MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001794 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001795 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001796}
1797
Charles Davis8bdfafd2013-09-01 04:28:48 +00001798MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00001799MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001800 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001801 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001802}
1803
Charles Davis8bdfafd2013-09-01 04:28:48 +00001804MachO::linkedit_data_command
1805MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1806 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001807}
1808
Charles Davis8bdfafd2013-09-01 04:28:48 +00001809MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001810MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001811 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001812}
1813
Charles Davis8bdfafd2013-09-01 04:28:48 +00001814MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00001815MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001816 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001817}
1818
Charles Davis8bdfafd2013-09-01 04:28:48 +00001819MachO::linker_options_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001820MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001821 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001822}
1823
Jim Grosbach448334a2014-03-18 22:09:05 +00001824MachO::version_min_command
1825MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
1826 return getStruct<MachO::version_min_command>(this, L.Ptr);
1827}
1828
Tim Northover8f9590b2014-06-30 14:40:57 +00001829MachO::dylib_command
1830MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const {
1831 return getStruct<MachO::dylib_command>(this, L.Ptr);
1832}
1833
1834
Charles Davis8bdfafd2013-09-01 04:28:48 +00001835MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001836MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +00001837 DataRefImpl Sec;
1838 Sec.d.a = Rel.d.a;
1839 uint32_t Offset;
1840 if (is64Bit()) {
1841 MachO::section_64 Sect = getSection64(Sec);
1842 Offset = Sect.reloff;
1843 } else {
1844 MachO::section Sect = getSection(Sec);
1845 Offset = Sect.reloff;
1846 }
1847
1848 auto P = reinterpret_cast<const MachO::any_relocation_info *>(
1849 getPtr(this, Offset)) + Rel.d.b;
1850 return getStruct<MachO::any_relocation_info>(
1851 this, reinterpret_cast<const char *>(P));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001852}
1853
Charles Davis8bdfafd2013-09-01 04:28:48 +00001854MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00001855MachOObjectFile::getDice(DataRefImpl Rel) const {
1856 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001857 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00001858}
1859
Charles Davis8bdfafd2013-09-01 04:28:48 +00001860MachO::mach_header MachOObjectFile::getHeader() const {
1861 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001862}
1863
Charles Davis8bdfafd2013-09-01 04:28:48 +00001864MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1865 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001866}
1867
Charles Davis8bdfafd2013-09-01 04:28:48 +00001868uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1869 const MachO::dysymtab_command &DLC,
1870 unsigned Index) const {
1871 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1872 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001873}
1874
Charles Davis8bdfafd2013-09-01 04:28:48 +00001875MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00001876MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1877 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001878 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1879 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001880}
1881
Charles Davis8bdfafd2013-09-01 04:28:48 +00001882MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1883 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001884}
1885
Charles Davis8bdfafd2013-09-01 04:28:48 +00001886MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1887 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001888}
1889
Charles Davis8bdfafd2013-09-01 04:28:48 +00001890MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00001891MachOObjectFile::getDataInCodeLoadCommand() const {
1892 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00001893 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00001894
1895 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001896 MachO::linkedit_data_command Cmd;
1897 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1898 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1899 Cmd.dataoff = 0;
1900 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00001901 return Cmd;
1902}
1903
Rafael Espindola6e040c02013-04-26 20:07:33 +00001904StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001905 MachO::symtab_command S = getSymtabLoadCommand();
1906 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001907}
1908
Rafael Espindola56f976f2013-04-18 18:08:55 +00001909bool MachOObjectFile::is64Bit() const {
1910 return getType() == getMachOType(false, true) ||
Lang Hames41b192f2014-06-25 00:20:53 +00001911 getType() == getMachOType(true, true);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001912}
1913
1914void MachOObjectFile::ReadULEB128s(uint64_t Index,
1915 SmallVectorImpl<uint64_t> &Out) const {
1916 DataExtractor extractor(ObjectFile::getData(), true, 0);
1917
1918 uint32_t offset = Index;
1919 uint64_t data = 0;
1920 while (uint64_t delta = extractor.getULEB128(&offset)) {
1921 data += delta;
1922 Out.push_back(data);
1923 }
1924}
1925
Lang Hames41b192f2014-06-25 00:20:53 +00001926const char *MachOObjectFile::getSectionPointer(DataRefImpl Rel) const {
1927 return Sections[Rel.d.a];
1928}
1929
Rafael Espindola6304e942014-06-23 22:00:37 +00001930ErrorOr<ObjectFile *>
1931ObjectFile::createMachOObjectFile(std::unique_ptr<MemoryBuffer> &Buffer) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001932 StringRef Magic = Buffer->getBuffer().slice(0, 4);
Rafael Espindola3acea392014-06-12 21:46:39 +00001933 std::error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001934 std::unique_ptr<MachOObjectFile> Ret;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001935 if (Magic == "\xFE\xED\xFA\xCE")
Rafael Espindola2e60ca92014-06-24 13:56:32 +00001936 Ret.reset(new MachOObjectFile(std::move(Buffer), false, false, EC));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001937 else if (Magic == "\xCE\xFA\xED\xFE")
Rafael Espindola2e60ca92014-06-24 13:56:32 +00001938 Ret.reset(new MachOObjectFile(std::move(Buffer), true, false, EC));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001939 else if (Magic == "\xFE\xED\xFA\xCF")
Rafael Espindola2e60ca92014-06-24 13:56:32 +00001940 Ret.reset(new MachOObjectFile(std::move(Buffer), false, true, EC));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001941 else if (Magic == "\xCF\xFA\xED\xFE")
Rafael Espindola2e60ca92014-06-24 13:56:32 +00001942 Ret.reset(new MachOObjectFile(std::move(Buffer), true, true, EC));
Rafael Espindola6304e942014-06-23 22:00:37 +00001943 else
Rafael Espindola692410e2014-01-21 23:06:54 +00001944 return object_error::parse_failed;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001945
Rafael Espindola692410e2014-01-21 23:06:54 +00001946 if (EC)
1947 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +00001948 return Ret.release();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001949}
1950
Owen Anderson27c579d2011-10-11 17:32:27 +00001951} // end namespace object
Eric Christopher7b015c72011-04-22 03:19:48 +00001952} // end namespace llvm