blob: 7fc02fb893f932096892d98481326c36ef507515 [file] [log] [blame]
Eric Christopher7b015c72011-04-22 03:19:48 +00001//===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the MachOObjectFile class, which binds the MachOObject
11// class to the generic ObjectFile wrapper.
12//
13//===----------------------------------------------------------------------===//
14
Owen Anderson27c579d2011-10-11 17:32:27 +000015#include "llvm/Object/MachO.h"
Tim Northover00ed9962014-03-29 10:18:08 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Triple.h"
Rafael Espindola421305a2013-04-07 20:01:29 +000018#include "llvm/Support/DataExtractor.h"
Owen Andersonbc14bd32011-10-26 20:42:54 +000019#include "llvm/Support/Format.h"
Rafael Espindola56f976f2013-04-18 18:08:55 +000020#include "llvm/Support/Host.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000021#include "llvm/Support/MemoryBuffer.h"
Jakub Staszak84a0ae72013-08-21 01:20:11 +000022#include "llvm/Support/raw_ostream.h"
Eric Christopher7b015c72011-04-22 03:19:48 +000023#include <cctype>
24#include <cstring>
25#include <limits>
26
27using namespace llvm;
28using namespace object;
29
30namespace llvm {
Rafael Espindola3acea392014-06-12 21:46:39 +000031
Owen Anderson27c579d2011-10-11 17:32:27 +000032namespace object {
Eric Christopher7b015c72011-04-22 03:19:48 +000033
Charles Davis8bdfafd2013-09-01 04:28:48 +000034struct nlist_base {
35 uint32_t n_strx;
36 uint8_t n_type;
37 uint8_t n_sect;
38 uint16_t n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +000039};
40
Charles Davis8bdfafd2013-09-01 04:28:48 +000041struct section_base {
42 char sectname[16];
43 char segname[16];
Rafael Espindola56f976f2013-04-18 18:08:55 +000044};
45
46template<typename T>
Rafael Espindola56f976f2013-04-18 18:08:55 +000047static void SwapStruct(T &Value);
48
49template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000050void SwapStruct(MachO::any_relocation_info &H) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000051 sys::swapByteOrder(H.r_word0);
52 sys::swapByteOrder(H.r_word1);
Rafael Espindola5ffc0792013-04-07 16:07:35 +000053}
54
Rafael Espindola56f976f2013-04-18 18:08:55 +000055template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000056void SwapStruct(MachO::load_command &L) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000057 sys::swapByteOrder(L.cmd);
58 sys::swapByteOrder(L.cmdsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +000059}
Rafael Espindola421305a2013-04-07 20:01:29 +000060
Rafael Espindola56f976f2013-04-18 18:08:55 +000061template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000062void SwapStruct(nlist_base &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000063 sys::swapByteOrder(S.n_strx);
64 sys::swapByteOrder(S.n_desc);
Rafael Espindola56f976f2013-04-18 18:08:55 +000065}
66
67template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000068void SwapStruct(MachO::section &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000069 sys::swapByteOrder(S.addr);
70 sys::swapByteOrder(S.size);
71 sys::swapByteOrder(S.offset);
72 sys::swapByteOrder(S.align);
73 sys::swapByteOrder(S.reloff);
74 sys::swapByteOrder(S.nreloc);
75 sys::swapByteOrder(S.flags);
76 sys::swapByteOrder(S.reserved1);
77 sys::swapByteOrder(S.reserved2);
Rafael Espindola56f976f2013-04-18 18:08:55 +000078}
79
80template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000081void SwapStruct(MachO::section_64 &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000082 sys::swapByteOrder(S.addr);
83 sys::swapByteOrder(S.size);
84 sys::swapByteOrder(S.offset);
85 sys::swapByteOrder(S.align);
86 sys::swapByteOrder(S.reloff);
87 sys::swapByteOrder(S.nreloc);
88 sys::swapByteOrder(S.flags);
89 sys::swapByteOrder(S.reserved1);
90 sys::swapByteOrder(S.reserved2);
91 sys::swapByteOrder(S.reserved3);
Rafael Espindola56f976f2013-04-18 18:08:55 +000092}
93
94template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +000095void SwapStruct(MachO::nlist &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +000096 sys::swapByteOrder(S.n_strx);
97 sys::swapByteOrder(S.n_desc);
98 sys::swapByteOrder(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +000099}
100
101template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000102void SwapStruct(MachO::nlist_64 &S) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000103 sys::swapByteOrder(S.n_strx);
104 sys::swapByteOrder(S.n_desc);
105 sys::swapByteOrder(S.n_value);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000106}
107
108template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000109void SwapStruct(MachO::mach_header &H) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000110 sys::swapByteOrder(H.magic);
111 sys::swapByteOrder(H.cputype);
112 sys::swapByteOrder(H.cpusubtype);
113 sys::swapByteOrder(H.filetype);
114 sys::swapByteOrder(H.ncmds);
115 sys::swapByteOrder(H.sizeofcmds);
116 sys::swapByteOrder(H.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000117}
118
119template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000120void SwapStruct(MachO::mach_header_64 &H) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000121 sys::swapByteOrder(H.magic);
122 sys::swapByteOrder(H.cputype);
123 sys::swapByteOrder(H.cpusubtype);
124 sys::swapByteOrder(H.filetype);
125 sys::swapByteOrder(H.ncmds);
126 sys::swapByteOrder(H.sizeofcmds);
127 sys::swapByteOrder(H.flags);
128 sys::swapByteOrder(H.reserved);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000129}
130
131template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000132void SwapStruct(MachO::symtab_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000133 sys::swapByteOrder(C.cmd);
134 sys::swapByteOrder(C.cmdsize);
135 sys::swapByteOrder(C.symoff);
136 sys::swapByteOrder(C.nsyms);
137 sys::swapByteOrder(C.stroff);
138 sys::swapByteOrder(C.strsize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000139}
140
141template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000142void SwapStruct(MachO::dysymtab_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000143 sys::swapByteOrder(C.cmd);
144 sys::swapByteOrder(C.cmdsize);
145 sys::swapByteOrder(C.ilocalsym);
146 sys::swapByteOrder(C.nlocalsym);
147 sys::swapByteOrder(C.iextdefsym);
148 sys::swapByteOrder(C.nextdefsym);
149 sys::swapByteOrder(C.iundefsym);
150 sys::swapByteOrder(C.nundefsym);
151 sys::swapByteOrder(C.tocoff);
152 sys::swapByteOrder(C.ntoc);
153 sys::swapByteOrder(C.modtaboff);
154 sys::swapByteOrder(C.nmodtab);
155 sys::swapByteOrder(C.extrefsymoff);
156 sys::swapByteOrder(C.nextrefsyms);
157 sys::swapByteOrder(C.indirectsymoff);
158 sys::swapByteOrder(C.nindirectsyms);
159 sys::swapByteOrder(C.extreloff);
160 sys::swapByteOrder(C.nextrel);
161 sys::swapByteOrder(C.locreloff);
162 sys::swapByteOrder(C.nlocrel);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000163}
164
165template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000166void SwapStruct(MachO::linkedit_data_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000167 sys::swapByteOrder(C.cmd);
168 sys::swapByteOrder(C.cmdsize);
169 sys::swapByteOrder(C.dataoff);
170 sys::swapByteOrder(C.datasize);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000171}
172
173template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000174void SwapStruct(MachO::segment_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000175 sys::swapByteOrder(C.cmd);
176 sys::swapByteOrder(C.cmdsize);
177 sys::swapByteOrder(C.vmaddr);
178 sys::swapByteOrder(C.vmsize);
179 sys::swapByteOrder(C.fileoff);
180 sys::swapByteOrder(C.filesize);
181 sys::swapByteOrder(C.maxprot);
182 sys::swapByteOrder(C.initprot);
183 sys::swapByteOrder(C.nsects);
184 sys::swapByteOrder(C.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000185}
186
187template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000188void SwapStruct(MachO::segment_command_64 &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000189 sys::swapByteOrder(C.cmd);
190 sys::swapByteOrder(C.cmdsize);
191 sys::swapByteOrder(C.vmaddr);
192 sys::swapByteOrder(C.vmsize);
193 sys::swapByteOrder(C.fileoff);
194 sys::swapByteOrder(C.filesize);
195 sys::swapByteOrder(C.maxprot);
196 sys::swapByteOrder(C.initprot);
197 sys::swapByteOrder(C.nsects);
198 sys::swapByteOrder(C.flags);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000199}
200
Rafael Espindola6e040c02013-04-26 20:07:33 +0000201template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000202void SwapStruct(uint32_t &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000203 sys::swapByteOrder(C);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000204}
205
206template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000207void SwapStruct(MachO::linker_options_command &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000208 sys::swapByteOrder(C.cmd);
209 sys::swapByteOrder(C.cmdsize);
210 sys::swapByteOrder(C.count);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000211}
212
213template<>
Jim Grosbach448334a2014-03-18 22:09:05 +0000214void SwapStruct(MachO::version_min_command&C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000215 sys::swapByteOrder(C.cmd);
216 sys::swapByteOrder(C.cmdsize);
217 sys::swapByteOrder(C.version);
218 sys::swapByteOrder(C.reserved);
Jim Grosbach448334a2014-03-18 22:09:05 +0000219}
220
221template<>
Kevin Enderby980b2582014-06-05 21:21:57 +0000222void SwapStruct(MachO::dylib_command&C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000223 sys::swapByteOrder(C.cmd);
224 sys::swapByteOrder(C.cmdsize);
225 sys::swapByteOrder(C.dylib.name);
226 sys::swapByteOrder(C.dylib.timestamp);
227 sys::swapByteOrder(C.dylib.current_version);
228 sys::swapByteOrder(C.dylib.compatibility_version);
Kevin Enderby980b2582014-06-05 21:21:57 +0000229}
230
231template<>
Charles Davis8bdfafd2013-09-01 04:28:48 +0000232void SwapStruct(MachO::data_in_code_entry &C) {
Artyom Skrobovc7b42532014-06-14 13:49:57 +0000233 sys::swapByteOrder(C.offset);
234 sys::swapByteOrder(C.length);
235 sys::swapByteOrder(C.kind);
Rafael Espindola6e040c02013-04-26 20:07:33 +0000236}
237
Rafael Espindola3cdeb172013-04-19 13:45:05 +0000238template<typename T>
239T getStruct(const MachOObjectFile *O, const char *P) {
240 T Cmd;
241 memcpy(&Cmd, P, sizeof(T));
242 if (O->isLittleEndian() != sys::IsLittleEndianHost)
243 SwapStruct(Cmd);
244 return Cmd;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000245}
246
Rafael Espindola56f976f2013-04-18 18:08:55 +0000247static uint32_t
248getSegmentLoadCommandNumSections(const MachOObjectFile *O,
249 const MachOObjectFile::LoadCommandInfo &L) {
250 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000251 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
252 return S.nsects;
Rafael Espindola421305a2013-04-07 20:01:29 +0000253 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000254 MachO::segment_command S = O->getSegmentLoadCommand(L);
255 return S.nsects;
Rafael Espindola5ffc0792013-04-07 16:07:35 +0000256}
257
Rafael Espindola6e040c02013-04-26 20:07:33 +0000258static const char *
259getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
260 unsigned Sec) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000261 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
262
263 bool Is64 = O->is64Bit();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000264 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
265 sizeof(MachO::segment_command);
266 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
267 sizeof(MachO::section);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000268
269 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
Charles Davis1827bd82013-08-27 05:38:30 +0000270 return reinterpret_cast<const char*>(SectionAddr);
Rafael Espindola60689982013-04-07 19:05:30 +0000271}
272
Rafael Espindola56f976f2013-04-18 18:08:55 +0000273static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
274 return O->getData().substr(Offset, 1).data();
Rafael Espindola60689982013-04-07 19:05:30 +0000275}
276
Charles Davis8bdfafd2013-09-01 04:28:48 +0000277static nlist_base
Rafael Espindola56f976f2013-04-18 18:08:55 +0000278getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola75c30362013-04-24 19:47:55 +0000279 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000280 return getStruct<nlist_base>(O, P);
Eric Christopher7b015c72011-04-22 03:19:48 +0000281}
282
Rafael Espindola56f976f2013-04-18 18:08:55 +0000283static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000284 if (P[15] == 0)
285 // Null terminated.
286 return P;
287 // Not null terminated, so this is a 16 char string.
288 return StringRef(P, 16);
289}
290
Rafael Espindola56f976f2013-04-18 18:08:55 +0000291// Helper to advance a section or symbol iterator multiple increments at a time.
292template<class T>
Rafael Espindola5e812af2014-01-30 02:49:50 +0000293static void advance(T &it, size_t Val) {
294 while (Val--)
295 ++it;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000296}
297
298static unsigned getCPUType(const MachOObjectFile *O) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000299 return O->getHeader().cputype;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000300}
301
302static void printRelocationTargetName(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000303 const MachO::any_relocation_info &RE,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000304 raw_string_ostream &fmt) {
305 bool IsScattered = O->isRelocationScattered(RE);
306
307 // Target of a scattered relocation is an address. In the interest of
308 // generating pretty output, scan through the symbol table looking for a
309 // symbol that aligns with that address. If we find one, print it.
310 // Otherwise, we just print the hex address of the target.
311 if (IsScattered) {
312 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
313
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000314 for (const SymbolRef &Symbol : O->symbols()) {
Rafael Espindola3acea392014-06-12 21:46:39 +0000315 std::error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000316 uint64_t Addr;
317 StringRef Name;
318
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000319 if ((ec = Symbol.getAddress(Addr)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000320 report_fatal_error(ec.message());
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000321 if (Addr != Val)
322 continue;
323 if ((ec = Symbol.getName(Name)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000324 report_fatal_error(ec.message());
325 fmt << Name;
326 return;
327 }
328
329 // If we couldn't find a symbol that this relocation refers to, try
330 // to find a section beginning instead.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000331 for (const SectionRef &Section : O->sections()) {
Rafael Espindola3acea392014-06-12 21:46:39 +0000332 std::error_code ec;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000333 uint64_t Addr;
334 StringRef Name;
335
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000336 if ((ec = Section.getAddress(Addr)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000337 report_fatal_error(ec.message());
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000338 if (Addr != Val)
339 continue;
340 if ((ec = Section.getName(Name)))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000341 report_fatal_error(ec.message());
342 fmt << Name;
343 return;
344 }
345
346 fmt << format("0x%x", Val);
347 return;
348 }
349
350 StringRef S;
351 bool isExtern = O->getPlainRelocationExternal(RE);
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000352 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000353
354 if (isExtern) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000355 symbol_iterator SI = O->symbol_begin();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000356 advance(SI, Val);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000357 SI->getName(S);
358 } else {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000359 section_iterator SI = O->section_begin();
Ahmed Bougacha9dab0cc2013-05-14 22:41:29 +0000360 // Adjust for the fact that sections are 1-indexed.
Rafael Espindola5e812af2014-01-30 02:49:50 +0000361 advance(SI, Val - 1);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000362 SI->getName(S);
363 }
364
365 fmt << S;
366}
367
Charles Davis8bdfafd2013-09-01 04:28:48 +0000368static uint32_t
369getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
370 return RE.r_word0;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000371}
372
373static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000374getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
375 return RE.r_word0 & 0xffffff;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000376}
377
378static bool getPlainRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000379 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000380 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000381 return (RE.r_word1 >> 24) & 1;
382 return (RE.r_word1 >> 7) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000383}
384
385static bool
386getScatteredRelocationPCRel(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000387 const MachO::any_relocation_info &RE) {
388 return (RE.r_word0 >> 30) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000389}
390
391static unsigned getPlainRelocationLength(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000392 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000393 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000394 return (RE.r_word1 >> 25) & 3;
395 return (RE.r_word1 >> 5) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000396}
397
398static unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +0000399getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
400 return (RE.r_word0 >> 28) & 3;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000401}
402
403static unsigned getPlainRelocationType(const MachOObjectFile *O,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000404 const MachO::any_relocation_info &RE) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000405 if (O->isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +0000406 return RE.r_word1 >> 28;
407 return RE.r_word1 & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000408}
409
Charles Davis8bdfafd2013-09-01 04:28:48 +0000410static unsigned
411getScatteredRelocationType(const MachO::any_relocation_info &RE) {
412 return (RE.r_word0 >> 24) & 0xf;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000413}
414
415static uint32_t getSectionFlags(const MachOObjectFile *O,
416 DataRefImpl Sec) {
417 if (O->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000418 MachO::section_64 Sect = O->getSection64(Sec);
419 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000420 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000421 MachO::section Sect = O->getSection(Sec);
422 return Sect.flags;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000423}
424
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000425MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000426 bool Is64bits, std::error_code &EC)
427 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
Craig Topper2617dcc2014-04-15 06:32:26 +0000428 SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
429 DataInCodeLoadCmd(nullptr) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000430 uint32_t LoadCommandCount = this->getHeader().ncmds;
431 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
432 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000433
434 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000435 for (unsigned I = 0; ; ++I) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000436 if (Load.C.cmd == MachO::LC_SYMTAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000437 assert(!SymtabLoadCmd && "Multiple symbol tables");
438 SymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000439 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000440 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
441 DysymtabLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000442 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000443 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
444 DataInCodeLoadCmd = Load.Ptr;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000445 } else if (Load.C.cmd == SegmentLoadType) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000446 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
447 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000448 const char *Sec = getSectionPtr(this, Load, J);
449 Sections.push_back(Sec);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000450 }
Kevin Enderby980b2582014-06-05 21:21:57 +0000451 } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB ||
452 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
453 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
454 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
455 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
456 Libraries.push_back(Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000457 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000458
459 if (I == LoadCommandCount - 1)
460 break;
461 else
462 Load = getNextLoadCommandInfo(Load);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000463 }
464}
465
Rafael Espindola5e812af2014-01-30 02:49:50 +0000466void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
Rafael Espindola75c30362013-04-24 19:47:55 +0000467 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000468 sizeof(MachO::nlist_64) :
469 sizeof(MachO::nlist);
Rafael Espindola75c30362013-04-24 19:47:55 +0000470 Symb.p += SymbolTableEntrySize;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000471}
472
Rafael Espindola3acea392014-06-12 21:46:39 +0000473std::error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
474 StringRef &Res) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +0000475 StringRef StringTable = getStringTableData();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000476 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
477 const char *Start = &StringTable.data()[Entry.n_strx];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000478 Res = StringRef(Start);
479 return object_error::success;
480}
481
Kevin Enderby980b2582014-06-05 21:21:57 +0000482// getIndirectName() returns the name of the alias'ed symbol who's string table
483// index is in the n_value field.
Rafael Espindola3acea392014-06-12 21:46:39 +0000484std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
485 StringRef &Res) const {
Kevin Enderby980b2582014-06-05 21:21:57 +0000486 StringRef StringTable = getStringTableData();
487 uint64_t NValue;
488 if (is64Bit()) {
489 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
490 NValue = Entry.n_value;
491 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
492 return object_error::parse_failed;
493 } else {
494 MachO::nlist Entry = getSymbolTableEntry(Symb);
495 NValue = Entry.n_value;
496 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
497 return object_error::parse_failed;
498 }
499 if (NValue >= StringTable.size())
500 return object_error::parse_failed;
501 const char *Start = &StringTable.data()[NValue];
502 Res = StringRef(Start);
503 return object_error::success;
504}
505
Rafael Espindola3acea392014-06-12 21:46:39 +0000506std::error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
507 uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000508 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000509 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000510 if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
511 Entry.n_value == 0)
512 Res = UnknownAddressOrSize;
513 else
514 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000515 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000516 MachO::nlist Entry = getSymbolTableEntry(Symb);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000517 if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
518 Entry.n_value == 0)
519 Res = UnknownAddressOrSize;
520 else
521 Res = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000522 }
523 return object_error::success;
524}
525
Rafael Espindola3acea392014-06-12 21:46:39 +0000526std::error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
527 uint32_t &Result) const {
Rafael Espindola20122a42014-01-31 20:57:12 +0000528 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000529 if (flags & SymbolRef::SF_Common) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000530 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
531 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000532 } else {
533 Result = 0;
534 }
535 return object_error::success;
536}
537
Rafael Espindola3acea392014-06-12 21:46:39 +0000538std::error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
539 uint64_t &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000540 uint64_t BeginOffset;
541 uint64_t EndOffset = 0;
542 uint8_t SectionIndex;
543
Charles Davis8bdfafd2013-09-01 04:28:48 +0000544 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000545 uint64_t Value;
546 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000547 if (Value == UnknownAddressOrSize) {
548 Result = UnknownAddressOrSize;
549 return object_error::success;
550 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000551
552 BeginOffset = Value;
553
Charles Davis8bdfafd2013-09-01 04:28:48 +0000554 SectionIndex = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000555 if (!SectionIndex) {
Rafael Espindola20122a42014-01-31 20:57:12 +0000556 uint32_t flags = getSymbolFlags(DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000557 if (flags & SymbolRef::SF_Common)
558 Result = Value;
559 else
560 Result = UnknownAddressOrSize;
561 return object_error::success;
562 }
563 // Unfortunately symbols are unsorted so we need to touch all
564 // symbols from load command
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000565 for (const SymbolRef &Symbol : symbols()) {
566 DataRefImpl DRI = Symbol.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000567 Entry = getSymbolTableEntryBase(this, DRI);
568 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000569 if (Value == UnknownAddressOrSize)
570 continue;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000571 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000572 if (!EndOffset || Value < EndOffset)
573 EndOffset = Value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000574 }
575 if (!EndOffset) {
576 uint64_t Size;
577 DataRefImpl Sec;
578 Sec.d.a = SectionIndex-1;
579 getSectionSize(Sec, Size);
580 getSectionAddress(Sec, EndOffset);
581 EndOffset += Size;
582 }
583 Result = EndOffset - BeginOffset;
584 return object_error::success;
585}
586
Rafael Espindola3acea392014-06-12 21:46:39 +0000587std::error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
588 SymbolRef::Type &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000589 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
590 uint8_t n_type = Entry.n_type;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000591
592 Res = SymbolRef::ST_Other;
593
594 // If this is a STAB debugging symbol, we can do nothing more.
Charles Davis74ec8b02013-08-27 05:00:13 +0000595 if (n_type & MachO::N_STAB) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000596 Res = SymbolRef::ST_Debug;
597 return object_error::success;
598 }
599
Charles Davis74ec8b02013-08-27 05:00:13 +0000600 switch (n_type & MachO::N_TYPE) {
601 case MachO::N_UNDF :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000602 Res = SymbolRef::ST_Unknown;
603 break;
Charles Davis74ec8b02013-08-27 05:00:13 +0000604 case MachO::N_SECT :
Rafael Espindola56f976f2013-04-18 18:08:55 +0000605 Res = SymbolRef::ST_Function;
606 break;
607 }
608 return object_error::success;
609}
610
Rafael Espindola20122a42014-01-31 20:57:12 +0000611uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000612 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000613
Charles Davis8bdfafd2013-09-01 04:28:48 +0000614 uint8_t MachOType = Entry.n_type;
615 uint16_t MachOFlags = Entry.n_desc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000616
Rafael Espindola20122a42014-01-31 20:57:12 +0000617 uint32_t Result = SymbolRef::SF_None;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000618
Charles Davis74ec8b02013-08-27 05:00:13 +0000619 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000620 Result |= SymbolRef::SF_Undefined;
621
Tim Northovereaef0742014-05-30 13:22:59 +0000622 if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
623 Result |= SymbolRef::SF_Indirect;
624
Rafael Espindolaa1356322013-11-02 05:03:24 +0000625 if (MachOType & MachO::N_STAB)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000626 Result |= SymbolRef::SF_FormatSpecific;
627
Charles Davis74ec8b02013-08-27 05:00:13 +0000628 if (MachOType & MachO::N_EXT) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000629 Result |= SymbolRef::SF_Global;
Charles Davis74ec8b02013-08-27 05:00:13 +0000630 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000631 uint64_t Value;
632 getSymbolAddress(DRI, Value);
Kevin Enderby1b985af2014-05-20 23:04:47 +0000633 if (Value && Value != UnknownAddressOrSize)
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000634 Result |= SymbolRef::SF_Common;
635 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000636 }
637
Charles Davis74ec8b02013-08-27 05:00:13 +0000638 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
Rafael Espindola56f976f2013-04-18 18:08:55 +0000639 Result |= SymbolRef::SF_Weak;
640
Charles Davis74ec8b02013-08-27 05:00:13 +0000641 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000642 Result |= SymbolRef::SF_Absolute;
643
Rafael Espindola20122a42014-01-31 20:57:12 +0000644 return Result;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000645}
646
Rafael Espindola3acea392014-06-12 21:46:39 +0000647std::error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
648 section_iterator &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000649 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
650 uint8_t index = Entry.n_sect;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000651
652 if (index == 0) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000653 Res = section_end();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000654 } else {
655 DataRefImpl DRI;
656 DRI.d.a = index - 1;
657 Res = section_iterator(SectionRef(DRI, this));
658 }
659
660 return object_error::success;
661}
662
Rafael Espindola5e812af2014-01-30 02:49:50 +0000663void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000664 Sec.d.a++;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000665}
666
Rafael Espindola3acea392014-06-12 21:46:39 +0000667std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
668 StringRef &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000669 ArrayRef<char> Raw = getSectionRawName(Sec);
670 Result = parseSegmentOrSectionName(Raw.data());
671 return object_error::success;
672}
673
Rafael Espindola3acea392014-06-12 21:46:39 +0000674std::error_code MachOObjectFile::getSectionAddress(DataRefImpl Sec,
675 uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000676 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000677 MachO::section_64 Sect = getSection64(Sec);
678 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000679 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000680 MachO::section Sect = getSection(Sec);
681 Res = Sect.addr;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000682 }
683 return object_error::success;
684}
685
Rafael Espindola3acea392014-06-12 21:46:39 +0000686std::error_code MachOObjectFile::getSectionSize(DataRefImpl Sec,
687 uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000688 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000689 MachO::section_64 Sect = getSection64(Sec);
690 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000691 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000692 MachO::section Sect = getSection(Sec);
693 Res = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000694 }
695
696 return object_error::success;
697}
698
Rafael Espindola3acea392014-06-12 21:46:39 +0000699std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
700 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000701 uint32_t Offset;
702 uint64_t Size;
703
704 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000705 MachO::section_64 Sect = getSection64(Sec);
706 Offset = Sect.offset;
707 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000708 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000709 MachO::section Sect = getSection(Sec);
710 Offset = Sect.offset;
711 Size = Sect.size;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000712 }
713
714 Res = this->getData().substr(Offset, Size);
715 return object_error::success;
716}
717
Rafael Espindola3acea392014-06-12 21:46:39 +0000718std::error_code MachOObjectFile::getSectionAlignment(DataRefImpl Sec,
719 uint64_t &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000720 uint32_t Align;
721 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000722 MachO::section_64 Sect = getSection64(Sec);
723 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000724 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000725 MachO::section Sect = getSection(Sec);
726 Align = Sect.align;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000727 }
728
729 Res = uint64_t(1) << Align;
730 return object_error::success;
731}
732
Rafael Espindola3acea392014-06-12 21:46:39 +0000733std::error_code MachOObjectFile::isSectionText(DataRefImpl Sec,
734 bool &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000735 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000736 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000737 return object_error::success;
738}
739
Rafael Espindola3acea392014-06-12 21:46:39 +0000740std::error_code MachOObjectFile::isSectionData(DataRefImpl Sec,
741 bool &Result) const {
Kevin Enderby403258f2014-05-19 20:36:02 +0000742 uint32_t Flags = getSectionFlags(this, Sec);
743 unsigned SectionType = Flags & MachO::SECTION_TYPE;
744 Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
745 !(SectionType == MachO::S_ZEROFILL ||
746 SectionType == MachO::S_GB_ZEROFILL);
Michael J. Spencer800619f2011-09-28 20:57:30 +0000747 return object_error::success;
748}
749
Rafael Espindola3acea392014-06-12 21:46:39 +0000750std::error_code MachOObjectFile::isSectionBSS(DataRefImpl Sec,
751 bool &Result) const {
Kevin Enderby403258f2014-05-19 20:36:02 +0000752 uint32_t Flags = getSectionFlags(this, Sec);
753 unsigned SectionType = Flags & MachO::SECTION_TYPE;
754 Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
755 (SectionType == MachO::S_ZEROFILL ||
756 SectionType == MachO::S_GB_ZEROFILL);
Preston Gurd2138ef62012-04-12 20:13:57 +0000757 return object_error::success;
758}
759
Rafael Espindola3acea392014-06-12 21:46:39 +0000760std::error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000761MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindola137faa02013-04-24 15:14:22 +0000762 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000763 // FIXME: Unimplemented.
764 Result = true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000765 return object_error::success;
766}
767
Rafael Espindola3acea392014-06-12 21:46:39 +0000768std::error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
769 bool &Result) const {
Rafael Espindolac2413f52013-04-09 14:49:08 +0000770 // FIXME: Unimplemented.
771 Result = false;
772 return object_error::success;
773}
774
Rafael Espindola3acea392014-06-12 21:46:39 +0000775std::error_code MachOObjectFile::isSectionZeroInit(DataRefImpl Sec,
776 bool &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000777 uint32_t Flags = getSectionFlags(this, Sec);
Charles Davis74ec8b02013-08-27 05:00:13 +0000778 unsigned SectionType = Flags & MachO::SECTION_TYPE;
779 Res = SectionType == MachO::S_ZEROFILL ||
780 SectionType == MachO::S_GB_ZEROFILL;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000781 return object_error::success;
782}
783
Rafael Espindola3acea392014-06-12 21:46:39 +0000784std::error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
785 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000786 // Consider using the code from isSectionText to look for __const sections.
787 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
788 // to use section attributes to distinguish code from data.
789
790 // FIXME: Unimplemented.
791 Result = false;
792 return object_error::success;
793}
794
Rafael Espindola3acea392014-06-12 21:46:39 +0000795std::error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
796 DataRefImpl Symb,
797 bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000798 SymbolRef::Type ST;
799 this->getSymbolType(Symb, ST);
800 if (ST == SymbolRef::ST_Unknown) {
801 Result = false;
802 return object_error::success;
803 }
804
805 uint64_t SectBegin, SectEnd;
806 getSectionAddress(Sec, SectBegin);
807 getSectionSize(Sec, SectEnd);
808 SectEnd += SectBegin;
809
810 uint64_t SymAddr;
811 getSymbolAddress(Symb, SymAddr);
812 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
813
814 return object_error::success;
815}
816
Rui Ueyamabc654b12013-09-27 21:47:05 +0000817relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000818 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +0000819 Ret.d.a = Sec.d.a;
820 Ret.d.b = 0;
Rafael Espindola04d3f492013-04-25 12:45:46 +0000821 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000822}
Rafael Espindolac0406e12013-04-08 20:45:01 +0000823
Rafael Espindola56f976f2013-04-18 18:08:55 +0000824relocation_iterator
Rui Ueyamabc654b12013-09-27 21:47:05 +0000825MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
Rafael Espindola04d3f492013-04-25 12:45:46 +0000826 uint32_t Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000827 if (is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000828 MachO::section_64 Sect = getSection64(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000829 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000830 } else {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000831 MachO::section Sect = getSection(Sec);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000832 Num = Sect.nreloc;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000833 }
Eric Christopher7b015c72011-04-22 03:19:48 +0000834
Rafael Espindola56f976f2013-04-18 18:08:55 +0000835 DataRefImpl Ret;
Rafael Espindola128b8112014-04-03 23:51:28 +0000836 Ret.d.a = Sec.d.a;
837 Ret.d.b = Num;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000838 return relocation_iterator(RelocationRef(Ret, this));
839}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000840
Rafael Espindola5e812af2014-01-30 02:49:50 +0000841void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +0000842 ++Rel.d.b;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000843}
Owen Anderson171f4852011-10-24 23:20:07 +0000844
Rafael Espindola3acea392014-06-12 21:46:39 +0000845std::error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
846 uint64_t &Res) const {
Rafael Espindola72475462014-04-04 00:31:12 +0000847 uint64_t Offset;
848 getRelocationOffset(Rel, Offset);
Rafael Espindola7e91bc92014-04-03 23:54:35 +0000849
850 DataRefImpl Sec;
851 Sec.d.a = Rel.d.a;
852 uint64_t SecAddress;
853 getSectionAddress(Sec, SecAddress);
854 Res = SecAddress + Offset;
855 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000856}
857
Rafael Espindola3acea392014-06-12 21:46:39 +0000858std::error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
859 uint64_t &Res) const {
Rafael Espindola72475462014-04-04 00:31:12 +0000860 assert(getHeader().filetype == MachO::MH_OBJECT &&
861 "Only implemented for MH_OBJECT");
Charles Davis8bdfafd2013-09-01 04:28:48 +0000862 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000863 Res = getAnyRelocationAddress(RE);
864 return object_error::success;
David Meyer2fc34c52012-03-01 01:36:50 +0000865}
866
Rafael Espindola806f0062013-06-05 01:33:53 +0000867symbol_iterator
868MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000869 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000870 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
871 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola806f0062013-06-05 01:33:53 +0000872 if (!isExtern)
Rafael Espindolab5155a52014-02-10 20:24:04 +0000873 return symbol_end();
Rafael Espindola75c30362013-04-24 19:47:55 +0000874
Charles Davis8bdfafd2013-09-01 04:28:48 +0000875 MachO::symtab_command S = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +0000876 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +0000877 sizeof(MachO::nlist_64) :
878 sizeof(MachO::nlist);
879 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +0000880 DataRefImpl Sym;
881 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindola806f0062013-06-05 01:33:53 +0000882 return symbol_iterator(SymbolRef(Sym, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +0000883}
884
Rafael Espindola3acea392014-06-12 21:46:39 +0000885std::error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
886 uint64_t &Res) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000887 MachO::any_relocation_info RE = getRelocation(Rel);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000888 Res = getAnyRelocationType(RE);
889 return object_error::success;
890}
891
Rafael Espindola3acea392014-06-12 21:46:39 +0000892std::error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +0000893MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
894 SmallVectorImpl<char> &Result) const {
895 StringRef res;
896 uint64_t RType;
897 getRelocationType(Rel, RType);
898
899 unsigned Arch = this->getArch();
900
901 switch (Arch) {
902 case Triple::x86: {
903 static const char *const Table[] = {
904 "GENERIC_RELOC_VANILLA",
905 "GENERIC_RELOC_PAIR",
906 "GENERIC_RELOC_SECTDIFF",
907 "GENERIC_RELOC_PB_LA_PTR",
908 "GENERIC_RELOC_LOCAL_SECTDIFF",
909 "GENERIC_RELOC_TLV" };
910
Eric Christopher13250cb2013-12-06 02:33:38 +0000911 if (RType > 5)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000912 res = "Unknown";
913 else
914 res = Table[RType];
915 break;
916 }
917 case Triple::x86_64: {
918 static const char *const Table[] = {
919 "X86_64_RELOC_UNSIGNED",
920 "X86_64_RELOC_SIGNED",
921 "X86_64_RELOC_BRANCH",
922 "X86_64_RELOC_GOT_LOAD",
923 "X86_64_RELOC_GOT",
924 "X86_64_RELOC_SUBTRACTOR",
925 "X86_64_RELOC_SIGNED_1",
926 "X86_64_RELOC_SIGNED_2",
927 "X86_64_RELOC_SIGNED_4",
928 "X86_64_RELOC_TLV" };
929
930 if (RType > 9)
931 res = "Unknown";
932 else
933 res = Table[RType];
934 break;
935 }
936 case Triple::arm: {
937 static const char *const Table[] = {
938 "ARM_RELOC_VANILLA",
939 "ARM_RELOC_PAIR",
940 "ARM_RELOC_SECTDIFF",
941 "ARM_RELOC_LOCAL_SECTDIFF",
942 "ARM_RELOC_PB_LA_PTR",
943 "ARM_RELOC_BR24",
944 "ARM_THUMB_RELOC_BR22",
945 "ARM_THUMB_32BIT_BRANCH",
946 "ARM_RELOC_HALF",
947 "ARM_RELOC_HALF_SECTDIFF" };
948
949 if (RType > 9)
950 res = "Unknown";
951 else
952 res = Table[RType];
953 break;
954 }
Tim Northover00ed9962014-03-29 10:18:08 +0000955 case Triple::arm64:
956 case Triple::aarch64: {
957 static const char *const Table[] = {
958 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
959 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
960 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
961 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
962 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
963 "ARM64_RELOC_ADDEND"
964 };
965
966 if (RType >= array_lengthof(Table))
967 res = "Unknown";
968 else
969 res = Table[RType];
970 break;
971 }
Rafael Espindola56f976f2013-04-18 18:08:55 +0000972 case Triple::ppc: {
973 static const char *const Table[] = {
974 "PPC_RELOC_VANILLA",
975 "PPC_RELOC_PAIR",
976 "PPC_RELOC_BR14",
977 "PPC_RELOC_BR24",
978 "PPC_RELOC_HI16",
979 "PPC_RELOC_LO16",
980 "PPC_RELOC_HA16",
981 "PPC_RELOC_LO14",
982 "PPC_RELOC_SECTDIFF",
983 "PPC_RELOC_PB_LA_PTR",
984 "PPC_RELOC_HI16_SECTDIFF",
985 "PPC_RELOC_LO16_SECTDIFF",
986 "PPC_RELOC_HA16_SECTDIFF",
987 "PPC_RELOC_JBSR",
988 "PPC_RELOC_LO14_SECTDIFF",
989 "PPC_RELOC_LOCAL_SECTDIFF" };
990
Eric Christopher13250cb2013-12-06 02:33:38 +0000991 if (RType > 15)
992 res = "Unknown";
993 else
994 res = Table[RType];
Rafael Espindola56f976f2013-04-18 18:08:55 +0000995 break;
996 }
997 case Triple::UnknownArch:
998 res = "Unknown";
999 break;
1000 }
1001 Result.append(res.begin(), res.end());
1002 return object_error::success;
1003}
1004
Rafael Espindola3acea392014-06-12 21:46:39 +00001005std::error_code
Rafael Espindola56f976f2013-04-18 18:08:55 +00001006MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindola137faa02013-04-24 15:14:22 +00001007 SmallVectorImpl<char> &Result) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001008 MachO::any_relocation_info RE = getRelocation(Rel);
David Meyer2fc34c52012-03-01 01:36:50 +00001009
Rafael Espindola56f976f2013-04-18 18:08:55 +00001010 unsigned Arch = this->getArch();
Eric Christopher7b015c72011-04-22 03:19:48 +00001011
Rafael Espindola56f976f2013-04-18 18:08:55 +00001012 std::string fmtbuf;
1013 raw_string_ostream fmt(fmtbuf);
1014 unsigned Type = this->getAnyRelocationType(RE);
1015 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1016
1017 // Determine any addends that should be displayed with the relocation.
1018 // These require decoding the relocation type, which is triple-specific.
1019
1020 // X86_64 has entirely custom relocation types.
1021 if (Arch == Triple::x86_64) {
1022 bool isPCRel = getAnyRelocationPCRel(RE);
1023
1024 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001025 case MachO::X86_64_RELOC_GOT_LOAD:
1026 case MachO::X86_64_RELOC_GOT: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001027 printRelocationTargetName(this, RE, fmt);
1028 fmt << "@GOT";
1029 if (isPCRel) fmt << "PCREL";
1030 break;
1031 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001032 case MachO::X86_64_RELOC_SUBTRACTOR: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001033 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001034 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001035 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001036
Charles Davis8bdfafd2013-09-01 04:28:48 +00001037 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
Rafael Espindola56f976f2013-04-18 18:08:55 +00001038 // X86_64_RELOC_UNSIGNED.
1039 // NOTE: Scattered relocations don't exist on x86_64.
1040 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001041 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001042 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1043 "X86_64_RELOC_SUBTRACTOR.");
1044
Charles Davis8bdfafd2013-09-01 04:28:48 +00001045 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1046 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
Rafael Espindola56f976f2013-04-18 18:08:55 +00001047 printRelocationTargetName(this, RENext, fmt);
1048 fmt << "-";
1049 printRelocationTargetName(this, RE, fmt);
1050 break;
1051 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001052 case MachO::X86_64_RELOC_TLV:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001053 printRelocationTargetName(this, RE, fmt);
1054 fmt << "@TLV";
1055 if (isPCRel) fmt << "P";
1056 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001057 case MachO::X86_64_RELOC_SIGNED_1:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001058 printRelocationTargetName(this, RE, fmt);
1059 fmt << "-1";
1060 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001061 case MachO::X86_64_RELOC_SIGNED_2:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001062 printRelocationTargetName(this, RE, fmt);
1063 fmt << "-2";
1064 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001065 case MachO::X86_64_RELOC_SIGNED_4:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001066 printRelocationTargetName(this, RE, fmt);
1067 fmt << "-4";
1068 break;
1069 default:
1070 printRelocationTargetName(this, RE, fmt);
1071 break;
1072 }
1073 // X86 and ARM share some relocation types in common.
David Fangb88cdf62013-08-08 20:14:40 +00001074 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1075 Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001076 // Generic relocation types...
1077 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001078 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001079 return object_error::success;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001080 case MachO::GENERIC_RELOC_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001081 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001082 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001083 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001084
1085 // X86 sect diff's must be followed by a relocation of type
1086 // GENERIC_RELOC_PAIR.
1087 unsigned RType = getAnyRelocationType(RENext);
1088
Charles Davis8bdfafd2013-09-01 04:28:48 +00001089 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001090 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1091 "GENERIC_RELOC_SECTDIFF.");
1092
1093 printRelocationTargetName(this, RE, fmt);
1094 fmt << "-";
1095 printRelocationTargetName(this, RENext, fmt);
1096 break;
1097 }
1098 }
1099
David Fangb88cdf62013-08-08 20:14:40 +00001100 if (Arch == Triple::x86 || Arch == Triple::ppc) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001101 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001102 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001103 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001104 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001105 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001106
1107 // X86 sect diff's must be followed by a relocation of type
1108 // GENERIC_RELOC_PAIR.
1109 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001110 if (RType != MachO::GENERIC_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001111 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1112 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1113
1114 printRelocationTargetName(this, RE, fmt);
1115 fmt << "-";
1116 printRelocationTargetName(this, RENext, fmt);
1117 break;
1118 }
Charles Davis8bdfafd2013-09-01 04:28:48 +00001119 case MachO::GENERIC_RELOC_TLV: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001120 printRelocationTargetName(this, RE, fmt);
1121 fmt << "@TLV";
1122 if (IsPCRel) fmt << "P";
1123 break;
1124 }
1125 default:
1126 printRelocationTargetName(this, RE, fmt);
1127 }
1128 } else { // ARM-specific relocations
1129 switch (Type) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001130 case MachO::ARM_RELOC_HALF:
1131 case MachO::ARM_RELOC_HALF_SECTDIFF: {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001132 // Half relocations steal a bit from the length field to encode
1133 // whether this is an upper16 or a lower16 relocation.
1134 bool isUpper = getAnyRelocationLength(RE) >> 1;
1135
1136 if (isUpper)
1137 fmt << ":upper16:(";
1138 else
1139 fmt << ":lower16:(";
1140 printRelocationTargetName(this, RE, fmt);
1141
1142 DataRefImpl RelNext = Rel;
Rafael Espindola0cc9ba12014-04-03 23:20:02 +00001143 moveRelocationNext(RelNext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001144 MachO::any_relocation_info RENext = getRelocation(RelNext);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001145
1146 // ARM half relocs must be followed by a relocation of type
1147 // ARM_RELOC_PAIR.
1148 unsigned RType = getAnyRelocationType(RENext);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001149 if (RType != MachO::ARM_RELOC_PAIR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001150 report_fatal_error("Expected ARM_RELOC_PAIR after "
Charles Davis8bdfafd2013-09-01 04:28:48 +00001151 "ARM_RELOC_HALF");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001152
1153 // NOTE: The half of the target virtual address is stashed in the
1154 // address field of the secondary relocation, but we can't reverse
1155 // engineer the constant offset from it without decoding the movw/movt
1156 // instruction to find the other half in its immediate field.
1157
1158 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1159 // symbol/section pointer of the follow-on relocation.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001160 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001161 fmt << "-";
1162 printRelocationTargetName(this, RENext, fmt);
1163 }
1164
1165 fmt << ")";
1166 break;
1167 }
1168 default: {
1169 printRelocationTargetName(this, RE, fmt);
1170 }
1171 }
1172 }
1173 } else
1174 printRelocationTargetName(this, RE, fmt);
1175
1176 fmt.flush();
1177 Result.append(fmtbuf.begin(), fmtbuf.end());
1178 return object_error::success;
1179}
1180
Rafael Espindola3acea392014-06-12 21:46:39 +00001181std::error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1182 bool &Result) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001183 unsigned Arch = getArch();
1184 uint64_t Type;
1185 getRelocationType(Rel, Type);
1186
1187 Result = false;
1188
1189 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1190 // is always hidden.
David Fangb88cdf62013-08-08 20:14:40 +00001191 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001192 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001193 } else if (Arch == Triple::x86_64) {
1194 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
Eric Christopher1ff26ab62013-07-22 22:25:09 +00001195 // an X86_64_RELOC_SUBTRACTOR.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001196 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001197 DataRefImpl RelPrev = Rel;
1198 RelPrev.d.a--;
1199 uint64_t PrevType;
1200 getRelocationType(RelPrev, PrevType);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001201 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001202 Result = true;
1203 }
1204 }
1205
1206 return object_error::success;
1207}
1208
Rafael Espindola3acea392014-06-12 21:46:39 +00001209std::error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1210 LibraryRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001211 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1212}
1213
Rafael Espindola3acea392014-06-12 21:46:39 +00001214std::error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1215 StringRef &Res) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001216 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1217}
1218
Kevin Enderby980b2582014-06-05 21:21:57 +00001219//
1220// guessLibraryShortName() is passed a name of a dynamic library and returns a
1221// guess on what the short name is. Then name is returned as a substring of the
1222// StringRef Name passed in. The name of the dynamic library is recognized as
1223// a framework if it has one of the two following forms:
1224// Foo.framework/Versions/A/Foo
1225// Foo.framework/Foo
1226// Where A and Foo can be any string. And may contain a trailing suffix
1227// starting with an underbar. If the Name is recognized as a framework then
1228// isFramework is set to true else it is set to false. If the Name has a
1229// suffix then Suffix is set to the substring in Name that contains the suffix
1230// else it is set to a NULL StringRef.
1231//
1232// The Name of the dynamic library is recognized as a library name if it has
1233// one of the two following forms:
1234// libFoo.A.dylib
1235// libFoo.dylib
1236// The library may have a suffix trailing the name Foo of the form:
1237// libFoo_profile.A.dylib
1238// libFoo_profile.dylib
1239//
1240// The Name of the dynamic library is also recognized as a library name if it
1241// has the following form:
1242// Foo.qtx
1243//
1244// If the Name of the dynamic library is none of the forms above then a NULL
1245// StringRef is returned.
1246//
1247StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
1248 bool &isFramework,
1249 StringRef &Suffix) {
1250 StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
1251 size_t a, b, c, d, Idx;
1252
1253 isFramework = false;
1254 Suffix = StringRef();
1255
1256 // Pull off the last component and make Foo point to it
1257 a = Name.rfind('/');
1258 if (a == Name.npos || a == 0)
1259 goto guess_library;
1260 Foo = Name.slice(a+1, Name.npos);
1261
1262 // Look for a suffix starting with a '_'
1263 Idx = Foo.rfind('_');
1264 if (Idx != Foo.npos && Foo.size() >= 2) {
1265 Suffix = Foo.slice(Idx, Foo.npos);
1266 Foo = Foo.slice(0, Idx);
1267 }
1268
1269 // First look for the form Foo.framework/Foo
1270 b = Name.rfind('/', a);
1271 if (b == Name.npos)
1272 Idx = 0;
1273 else
1274 Idx = b+1;
1275 F = Name.slice(Idx, Idx + Foo.size());
1276 DotFramework = Name.slice(Idx + Foo.size(),
1277 Idx + Foo.size() + sizeof(".framework/")-1);
1278 if (F == Foo && DotFramework == ".framework/") {
1279 isFramework = true;
1280 return Foo;
1281 }
1282
1283 // Next look for the form Foo.framework/Versions/A/Foo
1284 if (b == Name.npos)
1285 goto guess_library;
1286 c = Name.rfind('/', b);
1287 if (c == Name.npos || c == 0)
1288 goto guess_library;
1289 V = Name.slice(c+1, Name.npos);
1290 if (!V.startswith("Versions/"))
1291 goto guess_library;
1292 d = Name.rfind('/', c);
1293 if (d == Name.npos)
1294 Idx = 0;
1295 else
1296 Idx = d+1;
1297 F = Name.slice(Idx, Idx + Foo.size());
1298 DotFramework = Name.slice(Idx + Foo.size(),
1299 Idx + Foo.size() + sizeof(".framework/")-1);
1300 if (F == Foo && DotFramework == ".framework/") {
1301 isFramework = true;
1302 return Foo;
1303 }
1304
1305guess_library:
1306 // pull off the suffix after the "." and make a point to it
1307 a = Name.rfind('.');
1308 if (a == Name.npos || a == 0)
1309 return StringRef();
1310 Dylib = Name.slice(a, Name.npos);
1311 if (Dylib != ".dylib")
1312 goto guess_qtx;
1313
1314 // First pull off the version letter for the form Foo.A.dylib if any.
1315 if (a >= 3) {
1316 Dot = Name.slice(a-2, a-1);
1317 if (Dot == ".")
1318 a = a - 2;
1319 }
1320
1321 b = Name.rfind('/', a);
1322 if (b == Name.npos)
1323 b = 0;
1324 else
1325 b = b+1;
1326 // ignore any suffix after an underbar like Foo_profile.A.dylib
1327 Idx = Name.find('_', b);
1328 if (Idx != Name.npos && Idx != b) {
1329 Lib = Name.slice(b, Idx);
1330 Suffix = Name.slice(Idx, a);
1331 }
1332 else
1333 Lib = Name.slice(b, a);
1334 // There are incorrect library names of the form:
1335 // libATS.A_profile.dylib so check for these.
1336 if (Lib.size() >= 3) {
1337 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
1338 if (Dot == ".")
1339 Lib = Lib.slice(0, Lib.size()-2);
1340 }
1341 return Lib;
1342
1343guess_qtx:
1344 Qtx = Name.slice(a, Name.npos);
1345 if (Qtx != ".qtx")
1346 return StringRef();
1347 b = Name.rfind('/', a);
1348 if (b == Name.npos)
1349 Lib = Name.slice(0, a);
1350 else
1351 Lib = Name.slice(b+1, a);
1352 // There are library names of the form: QT.A.qtx so check for these.
1353 if (Lib.size() >= 3) {
1354 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
1355 if (Dot == ".")
1356 Lib = Lib.slice(0, Lib.size()-2);
1357 }
1358 return Lib;
1359}
1360
1361// getLibraryShortNameByIndex() is used to get the short name of the library
1362// for an undefined symbol in a linked Mach-O binary that was linked with the
1363// normal two-level namespace default (that is MH_TWOLEVEL in the header).
1364// It is passed the index (0 - based) of the library as translated from
1365// GET_LIBRARY_ORDINAL (1 - based).
Rafael Espindola3acea392014-06-12 21:46:39 +00001366std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
1367 StringRef &Res) {
Kevin Enderby980b2582014-06-05 21:21:57 +00001368 if (Index >= Libraries.size())
1369 return object_error::parse_failed;
1370
1371 MachO::dylib_command D =
1372 getStruct<MachO::dylib_command>(this, Libraries[Index]);
1373 if (D.dylib.name >= D.cmdsize)
1374 return object_error::parse_failed;
1375
1376 // If the cache of LibrariesShortNames is not built up do that first for
1377 // all the Libraries.
1378 if (LibrariesShortNames.size() == 0) {
1379 for (unsigned i = 0; i < Libraries.size(); i++) {
1380 MachO::dylib_command D =
1381 getStruct<MachO::dylib_command>(this, Libraries[i]);
1382 if (D.dylib.name >= D.cmdsize) {
1383 LibrariesShortNames.push_back(StringRef());
1384 continue;
1385 }
Kevin Enderby4eff6cd2014-06-20 18:07:34 +00001386 const char *P = (const char *)(Libraries[i]) + D.dylib.name;
Kevin Enderby980b2582014-06-05 21:21:57 +00001387 StringRef Name = StringRef(P);
1388 StringRef Suffix;
1389 bool isFramework;
1390 StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
1391 if (shortName == StringRef())
1392 LibrariesShortNames.push_back(Name);
1393 else
1394 LibrariesShortNames.push_back(shortName);
1395 }
1396 }
1397
1398 Res = LibrariesShortNames[Index];
1399 return object_error::success;
1400}
1401
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001402basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
Lang Hames36072da2014-05-12 21:39:59 +00001403 return getSymbolByIndex(0);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001404}
1405
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001406basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001407 DataRefImpl DRI;
Rafael Espindola75c30362013-04-24 19:47:55 +00001408 if (!SymtabLoadCmd)
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001409 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola75c30362013-04-24 19:47:55 +00001410
Charles Davis8bdfafd2013-09-01 04:28:48 +00001411 MachO::symtab_command Symtab = getSymtabLoadCommand();
Rafael Espindola75c30362013-04-24 19:47:55 +00001412 unsigned SymbolTableEntrySize = is64Bit() ?
Charles Davis8bdfafd2013-09-01 04:28:48 +00001413 sizeof(MachO::nlist_64) :
1414 sizeof(MachO::nlist);
1415 unsigned Offset = Symtab.symoff +
1416 Symtab.nsyms * SymbolTableEntrySize;
Rafael Espindola75c30362013-04-24 19:47:55 +00001417 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001418 return basic_symbol_iterator(SymbolRef(DRI, this));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001419}
1420
Lang Hames36072da2014-05-12 21:39:59 +00001421basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
1422 DataRefImpl DRI;
1423 if (!SymtabLoadCmd)
1424 return basic_symbol_iterator(SymbolRef(DRI, this));
1425
1426 MachO::symtab_command Symtab = getSymtabLoadCommand();
1427 assert(Index < Symtab.nsyms && "Requested symbol index is out of range.");
1428 unsigned SymbolTableEntrySize =
1429 is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
1430 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1431 DRI.p += Index * SymbolTableEntrySize;
1432 return basic_symbol_iterator(SymbolRef(DRI, this));
1433}
1434
Rafael Espindolab5155a52014-02-10 20:24:04 +00001435section_iterator MachOObjectFile::section_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001436 DataRefImpl DRI;
1437 return section_iterator(SectionRef(DRI, this));
1438}
1439
Rafael Espindolab5155a52014-02-10 20:24:04 +00001440section_iterator MachOObjectFile::section_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001441 DataRefImpl DRI;
1442 DRI.d.a = Sections.size();
1443 return section_iterator(SectionRef(DRI, this));
1444}
1445
Rafael Espindolab5155a52014-02-10 20:24:04 +00001446library_iterator MachOObjectFile::needed_library_begin() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001447 // TODO: implement
1448 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1449}
1450
Rafael Espindolab5155a52014-02-10 20:24:04 +00001451library_iterator MachOObjectFile::needed_library_end() const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001452 // TODO: implement
1453 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1454}
1455
1456uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola60689982013-04-07 19:05:30 +00001457 return is64Bit() ? 8 : 4;
Eric Christopher7b015c72011-04-22 03:19:48 +00001458}
1459
Rafael Espindola56f976f2013-04-18 18:08:55 +00001460StringRef MachOObjectFile::getFileFormatName() const {
1461 unsigned CPUType = getCPUType(this);
1462 if (!is64Bit()) {
1463 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001464 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001465 return "Mach-O 32-bit i386";
Charles Davis74ec8b02013-08-27 05:00:13 +00001466 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001467 return "Mach-O arm";
Charles Davis74ec8b02013-08-27 05:00:13 +00001468 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001469 return "Mach-O 32-bit ppc";
1470 default:
Charles Davis74ec8b02013-08-27 05:00:13 +00001471 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
Rafael Espindola56f976f2013-04-18 18:08:55 +00001472 "64-bit object file when we're not 64-bit?");
1473 return "Mach-O 32-bit unknown";
1474 }
1475 }
1476
1477 // Make sure the cpu type has the correct mask.
Charles Davis74ec8b02013-08-27 05:00:13 +00001478 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1479 == llvm::MachO::CPU_ARCH_ABI64 &&
Eric Christopher1d62c252013-07-22 22:25:07 +00001480 "32-bit object file when we're 64-bit?");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001481
1482 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001483 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001484 return "Mach-O 64-bit x86-64";
Tim Northover00ed9962014-03-29 10:18:08 +00001485 case llvm::MachO::CPU_TYPE_ARM64:
1486 return "Mach-O arm64";
Charles Davis74ec8b02013-08-27 05:00:13 +00001487 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001488 return "Mach-O 64-bit ppc64";
1489 default:
1490 return "Mach-O 64-bit unknown";
1491 }
1492}
1493
Alexey Samsonove6388e62013-06-18 15:03:28 +00001494Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1495 switch (CPUType) {
Charles Davis74ec8b02013-08-27 05:00:13 +00001496 case llvm::MachO::CPU_TYPE_I386:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001497 return Triple::x86;
Charles Davis74ec8b02013-08-27 05:00:13 +00001498 case llvm::MachO::CPU_TYPE_X86_64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001499 return Triple::x86_64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001500 case llvm::MachO::CPU_TYPE_ARM:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001501 return Triple::arm;
Tim Northover00ed9962014-03-29 10:18:08 +00001502 case llvm::MachO::CPU_TYPE_ARM64:
1503 return Triple::arm64;
Charles Davis74ec8b02013-08-27 05:00:13 +00001504 case llvm::MachO::CPU_TYPE_POWERPC:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001505 return Triple::ppc;
Charles Davis74ec8b02013-08-27 05:00:13 +00001506 case llvm::MachO::CPU_TYPE_POWERPC64:
Rafael Espindola56f976f2013-04-18 18:08:55 +00001507 return Triple::ppc64;
1508 default:
1509 return Triple::UnknownArch;
1510 }
1511}
1512
Alexey Samsonove6388e62013-06-18 15:03:28 +00001513unsigned MachOObjectFile::getArch() const {
1514 return getArch(getCPUType(this));
1515}
1516
Rafael Espindola56f976f2013-04-18 18:08:55 +00001517StringRef MachOObjectFile::getLoadName() const {
1518 // TODO: Implement
Charles Davis1827bd82013-08-27 05:38:30 +00001519 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
Rafael Espindola56f976f2013-04-18 18:08:55 +00001520}
1521
Rui Ueyamabc654b12013-09-27 21:47:05 +00001522relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001523 DataRefImpl DRI;
1524 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001525 return section_rel_begin(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001526}
1527
Rui Ueyamabc654b12013-09-27 21:47:05 +00001528relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001529 DataRefImpl DRI;
1530 DRI.d.a = Index;
Rui Ueyamabc654b12013-09-27 21:47:05 +00001531 return section_rel_end(DRI);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001532}
1533
Kevin Enderby273ae012013-06-06 17:20:50 +00001534dice_iterator MachOObjectFile::begin_dices() const {
1535 DataRefImpl DRI;
1536 if (!DataInCodeLoadCmd)
1537 return dice_iterator(DiceRef(DRI, this));
1538
Charles Davis8bdfafd2013-09-01 04:28:48 +00001539 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1540 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
Kevin Enderby273ae012013-06-06 17:20:50 +00001541 return dice_iterator(DiceRef(DRI, this));
1542}
1543
1544dice_iterator MachOObjectFile::end_dices() const {
1545 DataRefImpl DRI;
1546 if (!DataInCodeLoadCmd)
1547 return dice_iterator(DiceRef(DRI, this));
1548
Charles Davis8bdfafd2013-09-01 04:28:48 +00001549 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1550 unsigned Offset = DicLC.dataoff + DicLC.datasize;
Kevin Enderby273ae012013-06-06 17:20:50 +00001551 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1552 return dice_iterator(DiceRef(DRI, this));
1553}
1554
Rafael Espindola56f976f2013-04-18 18:08:55 +00001555StringRef
1556MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1557 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1558 return parseSegmentOrSectionName(Raw.data());
1559}
1560
1561ArrayRef<char>
1562MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001563 const section_base *Base =
1564 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1565 return ArrayRef<char>(Base->sectname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001566}
1567
1568ArrayRef<char>
1569MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001570 const section_base *Base =
1571 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1572 return ArrayRef<char>(Base->segname);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001573}
1574
1575bool
Charles Davis8bdfafd2013-09-01 04:28:48 +00001576MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001577 const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001578 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
Rafael Espindola56f976f2013-04-18 18:08:55 +00001579 return false;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001580 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001581}
1582
Eric Christopher1d62c252013-07-22 22:25:07 +00001583unsigned MachOObjectFile::getPlainRelocationSymbolNum(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001584 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001585 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001586 return RE.r_word1 & 0xffffff;
1587 return RE.r_word1 >> 8;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001588}
1589
Eric Christopher1d62c252013-07-22 22:25:07 +00001590bool MachOObjectFile::getPlainRelocationExternal(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001591 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001592 if (isLittleEndian())
Charles Davis8bdfafd2013-09-01 04:28:48 +00001593 return (RE.r_word1 >> 27) & 1;
1594 return (RE.r_word1 >> 4) & 1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001595}
1596
Eric Christopher1d62c252013-07-22 22:25:07 +00001597bool MachOObjectFile::getScatteredRelocationScattered(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001598 const MachO::any_relocation_info &RE) const {
1599 return RE.r_word0 >> 31;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001600}
1601
Eric Christopher1d62c252013-07-22 22:25:07 +00001602uint32_t MachOObjectFile::getScatteredRelocationValue(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001603 const MachO::any_relocation_info &RE) const {
1604 return RE.r_word1;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001605}
1606
Eric Christopher1d62c252013-07-22 22:25:07 +00001607unsigned MachOObjectFile::getAnyRelocationAddress(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001608 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001609 if (isRelocationScattered(RE))
1610 return getScatteredRelocationAddress(RE);
1611 return getPlainRelocationAddress(RE);
1612}
1613
Charles Davis8bdfafd2013-09-01 04:28:48 +00001614unsigned MachOObjectFile::getAnyRelocationPCRel(
1615 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001616 if (isRelocationScattered(RE))
1617 return getScatteredRelocationPCRel(this, RE);
1618 return getPlainRelocationPCRel(this, RE);
1619}
1620
Eric Christopher1d62c252013-07-22 22:25:07 +00001621unsigned MachOObjectFile::getAnyRelocationLength(
Charles Davis8bdfafd2013-09-01 04:28:48 +00001622 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001623 if (isRelocationScattered(RE))
1624 return getScatteredRelocationLength(RE);
1625 return getPlainRelocationLength(this, RE);
1626}
1627
1628unsigned
Charles Davis8bdfafd2013-09-01 04:28:48 +00001629MachOObjectFile::getAnyRelocationType(
1630 const MachO::any_relocation_info &RE) const {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001631 if (isRelocationScattered(RE))
1632 return getScatteredRelocationType(RE);
1633 return getPlainRelocationType(this, RE);
1634}
1635
Rafael Espindola52501032013-04-30 15:40:54 +00001636SectionRef
Charles Davis8bdfafd2013-09-01 04:28:48 +00001637MachOObjectFile::getRelocationSection(
1638 const MachO::any_relocation_info &RE) const {
Rafael Espindola52501032013-04-30 15:40:54 +00001639 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
Rafael Espindolab5155a52014-02-10 20:24:04 +00001640 return *section_end();
Rafael Espindola52501032013-04-30 15:40:54 +00001641 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1642 DataRefImpl DRI;
1643 DRI.d.a = SecNum;
1644 return SectionRef(DRI, this);
1645}
1646
Rafael Espindola56f976f2013-04-18 18:08:55 +00001647MachOObjectFile::LoadCommandInfo
1648MachOObjectFile::getFirstLoadCommandInfo() const {
1649 MachOObjectFile::LoadCommandInfo Load;
1650
Charles Davis8bdfafd2013-09-01 04:28:48 +00001651 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1652 sizeof(MachO::mach_header);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001653 Load.Ptr = getPtr(this, HeaderSize);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001654 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001655 return Load;
1656}
1657
1658MachOObjectFile::LoadCommandInfo
1659MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1660 MachOObjectFile::LoadCommandInfo Next;
Charles Davis8bdfafd2013-09-01 04:28:48 +00001661 Next.Ptr = L.Ptr + L.C.cmdsize;
1662 Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001663 return Next;
1664}
1665
Charles Davis8bdfafd2013-09-01 04:28:48 +00001666MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1667 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001668}
1669
Charles Davis8bdfafd2013-09-01 04:28:48 +00001670MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1671 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001672}
1673
Charles Davis8bdfafd2013-09-01 04:28:48 +00001674MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
Rafael Espindola6e040c02013-04-26 20:07:33 +00001675 unsigned Index) const {
1676 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001677 return getStruct<MachO::section>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001678}
1679
Charles Davis8bdfafd2013-09-01 04:28:48 +00001680MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1681 unsigned Index) const {
Rafael Espindola6e040c02013-04-26 20:07:33 +00001682 const char *Sec = getSectionPtr(this, L, Index);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001683 return getStruct<MachO::section_64>(this, Sec);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001684}
1685
Charles Davis8bdfafd2013-09-01 04:28:48 +00001686MachO::nlist
Rafael Espindola56f976f2013-04-18 18:08:55 +00001687MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001688 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001689 return getStruct<MachO::nlist>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001690}
1691
Charles Davis8bdfafd2013-09-01 04:28:48 +00001692MachO::nlist_64
Rafael Espindola56f976f2013-04-18 18:08:55 +00001693MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola75c30362013-04-24 19:47:55 +00001694 const char *P = reinterpret_cast<const char *>(DRI.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001695 return getStruct<MachO::nlist_64>(this, P);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001696}
1697
Charles Davis8bdfafd2013-09-01 04:28:48 +00001698MachO::linkedit_data_command
1699MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1700 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001701}
1702
Charles Davis8bdfafd2013-09-01 04:28:48 +00001703MachO::segment_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001704MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001705 return getStruct<MachO::segment_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001706}
1707
Charles Davis8bdfafd2013-09-01 04:28:48 +00001708MachO::segment_command_64
Rafael Espindola6e040c02013-04-26 20:07:33 +00001709MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001710 return getStruct<MachO::segment_command_64>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001711}
1712
Charles Davis8bdfafd2013-09-01 04:28:48 +00001713MachO::linker_options_command
Rafael Espindola6e040c02013-04-26 20:07:33 +00001714MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001715 return getStruct<MachO::linker_options_command>(this, L.Ptr);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001716}
1717
Jim Grosbach448334a2014-03-18 22:09:05 +00001718MachO::version_min_command
1719MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
1720 return getStruct<MachO::version_min_command>(this, L.Ptr);
1721}
1722
Charles Davis8bdfafd2013-09-01 04:28:48 +00001723MachO::any_relocation_info
Rafael Espindola56f976f2013-04-18 18:08:55 +00001724MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindola128b8112014-04-03 23:51:28 +00001725 DataRefImpl Sec;
1726 Sec.d.a = Rel.d.a;
1727 uint32_t Offset;
1728 if (is64Bit()) {
1729 MachO::section_64 Sect = getSection64(Sec);
1730 Offset = Sect.reloff;
1731 } else {
1732 MachO::section Sect = getSection(Sec);
1733 Offset = Sect.reloff;
1734 }
1735
1736 auto P = reinterpret_cast<const MachO::any_relocation_info *>(
1737 getPtr(this, Offset)) + Rel.d.b;
1738 return getStruct<MachO::any_relocation_info>(
1739 this, reinterpret_cast<const char *>(P));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001740}
1741
Charles Davis8bdfafd2013-09-01 04:28:48 +00001742MachO::data_in_code_entry
Kevin Enderby273ae012013-06-06 17:20:50 +00001743MachOObjectFile::getDice(DataRefImpl Rel) const {
1744 const char *P = reinterpret_cast<const char *>(Rel.p);
Charles Davis8bdfafd2013-09-01 04:28:48 +00001745 return getStruct<MachO::data_in_code_entry>(this, P);
Kevin Enderby273ae012013-06-06 17:20:50 +00001746}
1747
Charles Davis8bdfafd2013-09-01 04:28:48 +00001748MachO::mach_header MachOObjectFile::getHeader() const {
1749 return getStruct<MachO::mach_header>(this, getPtr(this, 0));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001750}
1751
Charles Davis8bdfafd2013-09-01 04:28:48 +00001752MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1753 return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001754}
1755
Charles Davis8bdfafd2013-09-01 04:28:48 +00001756uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1757 const MachO::dysymtab_command &DLC,
1758 unsigned Index) const {
1759 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1760 return getStruct<uint32_t>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001761}
1762
Charles Davis8bdfafd2013-09-01 04:28:48 +00001763MachO::data_in_code_entry
Rafael Espindola6e040c02013-04-26 20:07:33 +00001764MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1765 unsigned Index) const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001766 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1767 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
Rafael Espindola6e040c02013-04-26 20:07:33 +00001768}
1769
Charles Davis8bdfafd2013-09-01 04:28:48 +00001770MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1771 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
Rafael Espindola56f976f2013-04-18 18:08:55 +00001772}
1773
Charles Davis8bdfafd2013-09-01 04:28:48 +00001774MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1775 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001776}
1777
Charles Davis8bdfafd2013-09-01 04:28:48 +00001778MachO::linkedit_data_command
Kevin Enderby273ae012013-06-06 17:20:50 +00001779MachOObjectFile::getDataInCodeLoadCommand() const {
1780 if (DataInCodeLoadCmd)
Charles Davis8bdfafd2013-09-01 04:28:48 +00001781 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
Kevin Enderby273ae012013-06-06 17:20:50 +00001782
1783 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
Charles Davis8bdfafd2013-09-01 04:28:48 +00001784 MachO::linkedit_data_command Cmd;
1785 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1786 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1787 Cmd.dataoff = 0;
1788 Cmd.datasize = 0;
Kevin Enderby273ae012013-06-06 17:20:50 +00001789 return Cmd;
1790}
1791
Rafael Espindola6e040c02013-04-26 20:07:33 +00001792StringRef MachOObjectFile::getStringTableData() const {
Charles Davis8bdfafd2013-09-01 04:28:48 +00001793 MachO::symtab_command S = getSymtabLoadCommand();
1794 return getData().substr(S.stroff, S.strsize);
Rafael Espindola6e040c02013-04-26 20:07:33 +00001795}
1796
Rafael Espindola56f976f2013-04-18 18:08:55 +00001797bool MachOObjectFile::is64Bit() const {
1798 return getType() == getMachOType(false, true) ||
1799 getType() == getMachOType(true, true);
1800}
1801
1802void MachOObjectFile::ReadULEB128s(uint64_t Index,
1803 SmallVectorImpl<uint64_t> &Out) const {
1804 DataExtractor extractor(ObjectFile::getData(), true, 0);
1805
1806 uint32_t offset = Index;
1807 uint64_t data = 0;
1808 while (uint64_t delta = extractor.getULEB128(&offset)) {
1809 data += delta;
1810 Out.push_back(data);
1811 }
1812}
1813
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00001814ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Rafael Espindola56f976f2013-04-18 18:08:55 +00001815 StringRef Magic = Buffer->getBuffer().slice(0, 4);
Rafael Espindola3acea392014-06-12 21:46:39 +00001816 std::error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001817 std::unique_ptr<MachOObjectFile> Ret;
Rafael Espindola56f976f2013-04-18 18:08:55 +00001818 if (Magic == "\xFE\xED\xFA\xCE")
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00001819 Ret.reset(new MachOObjectFile(Buffer, false, false, EC));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001820 else if (Magic == "\xCE\xFA\xED\xFE")
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00001821 Ret.reset(new MachOObjectFile(Buffer, true, false, EC));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001822 else if (Magic == "\xFE\xED\xFA\xCF")
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00001823 Ret.reset(new MachOObjectFile(Buffer, false, true, EC));
Rafael Espindola56f976f2013-04-18 18:08:55 +00001824 else if (Magic == "\xCF\xFA\xED\xFE")
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +00001825 Ret.reset(new MachOObjectFile(Buffer, true, true, EC));
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001826 else {
1827 delete Buffer;
Rafael Espindola692410e2014-01-21 23:06:54 +00001828 return object_error::parse_failed;
Benjamin Kramer097e09a2013-08-03 22:16:37 +00001829 }
Rafael Espindola56f976f2013-04-18 18:08:55 +00001830
Rafael Espindola692410e2014-01-21 23:06:54 +00001831 if (EC)
1832 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +00001833 return Ret.release();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001834}
1835
Owen Anderson27c579d2011-10-11 17:32:27 +00001836} // end namespace object
Eric Christopher7b015c72011-04-22 03:19:48 +00001837} // end namespace llvm