blob: b7255d3e40221e8f1a7bd864531a93f1a1683163 [file] [log] [blame]
Eric Christopher6256b032011-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 Andersonf7c93a32011-10-11 17:32:27 +000015#include "llvm/Object/MachO.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/Triple.h"
Eric Christopher6256b032011-04-22 03:19:48 +000017#include "llvm/Object/MachOFormat.h"
Owen Anderson1832f4d2011-10-26 20:42:54 +000018#include "llvm/Support/Format.h"
Eric Christopher6256b032011-04-22 03:19:48 +000019#include "llvm/Support/MemoryBuffer.h"
Eric Christopher6256b032011-04-22 03:19:48 +000020#include <cctype>
21#include <cstring>
22#include <limits>
23
24using namespace llvm;
25using namespace object;
26
27namespace llvm {
Owen Andersonf7c93a32011-10-11 17:32:27 +000028namespace object {
Eric Christopher6256b032011-04-22 03:19:48 +000029
Benjamin Kramer0fcab072011-09-08 20:52:17 +000030MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
31 error_code &ec)
David Meyer6f9489a2012-03-09 20:41:57 +000032 : ObjectFile(Binary::ID_MachO, Object, ec),
Benjamin Kramer0fcab072011-09-08 20:52:17 +000033 MachOObj(MOO),
34 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
35 DataRefImpl DRI;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000036 moveToNextSection(DRI);
37 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
38 while (DRI.d.a < LoadCommandCount) {
39 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000040 DRI.d.b++;
41 moveToNextSection(DRI);
42 }
43}
44
45
Eric Christopher6256b032011-04-22 03:19:48 +000046ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000047 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000048 std::string Err;
49 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
50 if (!MachOObj)
51 return NULL;
Jim Grosbach596e4742012-11-29 19:14:11 +000052 // MachOObject takes ownership of the Buffer we passed to it, and
53 // MachOObjectFile does, too, so we need to make sure they don't get the
54 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
55 // not a copy of the memory itself), so just make a new copy here for
56 // the MachOObjectFile.
57 MemoryBuffer *NewBuffer =
Benjamin Kramerf56c3e22012-11-29 20:08:03 +000058 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
59 Buffer->getBufferIdentifier(), false);
Jim Grosbach596e4742012-11-29 19:14:11 +000060 return new MachOObjectFile(NewBuffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000061}
62
63/*===-- Symbols -----------------------------------------------------------===*/
64
65void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
66 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
67 while (DRI.d.a < LoadCommandCount) {
68 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
69 if (LCI.Command.Type == macho::LCT_Symtab) {
70 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
71 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
72 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
73 return;
74 }
75
76 DRI.d.a++;
77 DRI.d.b = 0;
78 }
79}
80
81void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
82 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
83 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
84 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
85 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
86
87 if (RegisteredStringTable != DRI.d.a) {
88 MachOObj->RegisterStringTable(*SymtabLoadCmd);
89 RegisteredStringTable = DRI.d.a;
90 }
91
92 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
93 Res);
94}
95
Benjamin Kramer32fb2af2011-07-15 17:32:45 +000096void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
97 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
98 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
99 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
100 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
101
102 if (RegisteredStringTable != DRI.d.a) {
103 MachOObj->RegisterStringTable(*SymtabLoadCmd);
104 RegisteredStringTable = DRI.d.a;
105 }
106
107 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
108 Res);
109}
110
Eric Christopher6256b032011-04-22 03:19:48 +0000111
Michael J. Spencer25b15772011-06-25 17:55:23 +0000112error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
113 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000114 DRI.d.b++;
115 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000116 Result = SymbolRef(DRI, this);
117 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000118}
119
Michael J. Spencer25b15772011-06-25 17:55:23 +0000120error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
121 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000122 if (MachOObj->is64Bit()) {
123 InMemoryStruct<macho::Symbol64TableEntry> Entry;
124 getSymbol64TableEntry(DRI, Entry);
125 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
126 } else {
127 InMemoryStruct<macho::SymbolTableEntry> Entry;
128 getSymbolTableEntry(DRI, Entry);
129 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
130 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000131 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000132}
133
Danil Malyshevb0436a72011-11-29 17:40:10 +0000134error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
135 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000136 if (MachOObj->is64Bit()) {
137 InMemoryStruct<macho::Symbol64TableEntry> Entry;
138 getSymbol64TableEntry(DRI, Entry);
139 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000140 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000141 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000142 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000143 Result += Section->Offset - Section->Address;
144 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000145 } else {
146 InMemoryStruct<macho::SymbolTableEntry> Entry;
147 getSymbolTableEntry(DRI, Entry);
148 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000149 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000150 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000151 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000152 Result += Section->Offset - Section->Address;
153 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000154 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000155
Michael J. Spencer25b15772011-06-25 17:55:23 +0000156 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000157}
158
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000159error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
160 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000161 if (MachOObj->is64Bit()) {
162 InMemoryStruct<macho::Symbol64TableEntry> Entry;
163 getSymbol64TableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000164 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000165 } else {
166 InMemoryStruct<macho::SymbolTableEntry> Entry;
167 getSymbolTableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000168 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000169 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000170 return object_error::success;
171}
172
Michael J. Spencer25b15772011-06-25 17:55:23 +0000173error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
174 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000175 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
176 uint64_t BeginOffset;
177 uint64_t EndOffset = 0;
178 uint8_t SectionIndex;
179 if (MachOObj->is64Bit()) {
180 InMemoryStruct<macho::Symbol64TableEntry> Entry;
181 getSymbol64TableEntry(DRI, Entry);
182 BeginOffset = Entry->Value;
183 SectionIndex = Entry->SectionIndex;
184 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000185 uint32_t flags = SymbolRef::SF_None;
186 getSymbolFlags(DRI, flags);
187 if (flags & SymbolRef::SF_Common)
188 Result = Entry->Value;
189 else
190 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000191 return object_error::success;
192 }
193 // Unfortunately symbols are unsorted so we need to touch all
194 // symbols from load command
195 DRI.d.b = 0;
196 uint32_t Command = DRI.d.a;
197 while (Command == DRI.d.a) {
198 moveToNextSymbol(DRI);
199 if (DRI.d.a < LoadCommandCount) {
200 getSymbol64TableEntry(DRI, Entry);
201 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
202 if (!EndOffset || Entry->Value < EndOffset)
203 EndOffset = Entry->Value;
204 }
205 DRI.d.b++;
206 }
207 } else {
208 InMemoryStruct<macho::SymbolTableEntry> Entry;
209 getSymbolTableEntry(DRI, Entry);
210 BeginOffset = Entry->Value;
211 SectionIndex = Entry->SectionIndex;
212 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000213 uint32_t flags = SymbolRef::SF_None;
214 getSymbolFlags(DRI, flags);
215 if (flags & SymbolRef::SF_Common)
216 Result = Entry->Value;
217 else
218 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000219 return object_error::success;
220 }
221 // Unfortunately symbols are unsorted so we need to touch all
222 // symbols from load command
223 DRI.d.b = 0;
224 uint32_t Command = DRI.d.a;
225 while (Command == DRI.d.a) {
226 moveToNextSymbol(DRI);
227 if (DRI.d.a < LoadCommandCount) {
228 getSymbolTableEntry(DRI, Entry);
229 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
230 if (!EndOffset || Entry->Value < EndOffset)
231 EndOffset = Entry->Value;
232 }
233 DRI.d.b++;
234 }
235 }
236 if (!EndOffset) {
237 uint64_t Size;
238 getSectionSize(Sections[SectionIndex-1], Size);
239 getSectionAddress(Sections[SectionIndex-1], EndOffset);
240 EndOffset += Size;
241 }
242 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000243 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000244}
245
Michael J. Spencer25b15772011-06-25 17:55:23 +0000246error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
247 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000248 uint8_t Type, Flags;
249 if (MachOObj->is64Bit()) {
250 InMemoryStruct<macho::Symbol64TableEntry> Entry;
251 getSymbol64TableEntry(DRI, Entry);
252 Type = Entry->Type;
253 Flags = Entry->Flags;
254 } else {
255 InMemoryStruct<macho::SymbolTableEntry> Entry;
256 getSymbolTableEntry(DRI, Entry);
257 Type = Entry->Type;
258 Flags = Entry->Flags;
259 }
Eric Christopher6256b032011-04-22 03:19:48 +0000260
261 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000262 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000263 case macho::STT_Undefined:
264 Char = 'u';
265 break;
266 case macho::STT_Absolute:
267 case macho::STT_Section:
268 Char = 's';
269 break;
270 default:
271 Char = '?';
272 break;
273 }
274
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000275 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000276 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000277 Result = Char;
278 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000279}
280
David Meyerc46255a2012-02-28 23:47:53 +0000281error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
282 uint32_t &Result) const {
283 uint16_t MachOFlags;
284 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000285 if (MachOObj->is64Bit()) {
286 InMemoryStruct<macho::Symbol64TableEntry> Entry;
287 getSymbol64TableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000288 MachOFlags = Entry->Flags;
289 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000290 } else {
291 InMemoryStruct<macho::SymbolTableEntry> Entry;
292 getSymbolTableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000293 MachOFlags = Entry->Flags;
294 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000295 }
296
Preston Gurdc68dda82012-04-12 20:13:57 +0000297 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000298 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000299
300 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
301 Result |= SymbolRef::SF_Undefined;
302
David Meyerc46255a2012-02-28 23:47:53 +0000303 if (MachOFlags & macho::STF_StabsEntryMask)
304 Result |= SymbolRef::SF_FormatSpecific;
305
Preston Gurdc68dda82012-04-12 20:13:57 +0000306 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000307 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000308 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
309 Result |= SymbolRef::SF_Common;
310 }
David Meyerc46255a2012-02-28 23:47:53 +0000311
312 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
313 Result |= SymbolRef::SF_Weak;
314
315 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
316 Result |= SymbolRef::SF_Absolute;
317
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000318 return object_error::success;
319}
320
321error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
322 section_iterator &Res) const {
323 uint8_t index;
324 if (MachOObj->is64Bit()) {
325 InMemoryStruct<macho::Symbol64TableEntry> Entry;
326 getSymbol64TableEntry(Symb, Entry);
327 index = Entry->SectionIndex;
328 } else {
329 InMemoryStruct<macho::SymbolTableEntry> Entry;
330 getSymbolTableEntry(Symb, Entry);
331 index = Entry->SectionIndex;
332 }
333
334 if (index == 0)
335 Res = end_sections();
336 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000337 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000338
339 return object_error::success;
340}
341
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000342error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000343 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000344 uint8_t n_type;
345 if (MachOObj->is64Bit()) {
346 InMemoryStruct<macho::Symbol64TableEntry> Entry;
347 getSymbol64TableEntry(Symb, Entry);
348 n_type = Entry->Type;
349 } else {
350 InMemoryStruct<macho::SymbolTableEntry> Entry;
351 getSymbolTableEntry(Symb, Entry);
352 n_type = Entry->Type;
353 }
354 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000355
356 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000357 if (n_type & MachO::NlistMaskStab) {
358 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000359 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000360 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000361
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000362 switch (n_type & MachO::NlistMaskType) {
363 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000364 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000365 break;
366 case MachO::NListTypeSection :
367 Res = SymbolRef::ST_Function;
368 break;
369 }
370 return object_error::success;
371}
372
Tim Northovera41dce32012-10-29 10:47:00 +0000373error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
374 uint64_t &Val) const {
375 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
376}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000377
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000378symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000379 // DRI.d.a = segment number; DRI.d.b = symbol index.
380 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000381 moveToNextSymbol(DRI);
382 return symbol_iterator(SymbolRef(DRI, this));
383}
384
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000385symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000386 DataRefImpl DRI;
387 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000388 return symbol_iterator(SymbolRef(DRI, this));
389}
390
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000391symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
392 // TODO: implement
393 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
394}
395
396symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
397 // TODO: implement
398 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
399}
Eric Christopher6256b032011-04-22 03:19:48 +0000400
David Meyer5c2b4ea2012-03-01 01:36:50 +0000401library_iterator MachOObjectFile::begin_libraries_needed() const {
402 // TODO: implement
403 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
404}
405
406library_iterator MachOObjectFile::end_libraries_needed() const {
407 // TODO: implement
408 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
409}
410
David Meyer97f77872012-03-01 22:19:54 +0000411StringRef MachOObjectFile::getLoadName() const {
412 // TODO: Implement
413 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
414}
415
Eric Christopher6256b032011-04-22 03:19:48 +0000416/*===-- Sections ----------------------------------------------------------===*/
417
418void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
419 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
420 while (DRI.d.a < LoadCommandCount) {
421 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
422 if (LCI.Command.Type == macho::LCT_Segment) {
423 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
424 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
425 if (DRI.d.b < SegmentLoadCmd->NumSections)
426 return;
427 } else if (LCI.Command.Type == macho::LCT_Segment64) {
428 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
429 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
430 if (DRI.d.b < Segment64LoadCmd->NumSections)
431 return;
432 }
433
434 DRI.d.a++;
435 DRI.d.b = 0;
436 }
437}
438
Michael J. Spencer25b15772011-06-25 17:55:23 +0000439error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
440 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000441 DRI.d.b++;
442 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000443 Result = SectionRef(DRI, this);
444 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000445}
446
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000447static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
Eric Christopher6256b032011-04-22 03:19:48 +0000448 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000449 if (LCI.Command.Type == macho::LCT_Segment64)
450 return true;
451 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
452 return false;
453}
454
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000455const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000456 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
457 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
458 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000459 DRI.d.b * sizeof(MachOFormat::Section);
460 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
461 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000462}
463
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000464std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
465 SectionList::const_iterator loc =
466 std::find(Sections.begin(), Sections.end(), Sec);
467 assert(loc != Sections.end() && "Sec is not a valid section!");
468 return std::distance(Sections.begin(), loc);
469}
470
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000471const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000472MachOObjectFile::getSection64(DataRefImpl DRI) const {
473 assert(is64BitLoadCommand(MachOObj.get(), DRI));
Benjamin Kramer7d145782011-07-15 00:14:48 +0000474 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000475 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000476 DRI.d.b * sizeof(MachOFormat::Section64);
477 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
478 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000479}
480
Rafael Espindolacef81b32012-12-21 03:47:03 +0000481static StringRef parseSegmentOrSectionName(const char *P) {
482 if (P[15] == 0)
483 // Null terminated.
484 return P;
485 // Not null terminated, so this is a 16 char string.
486 return StringRef(P, 16);
487}
488
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000489ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000490 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000491 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000492 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000493 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000494 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000495 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000496 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000497}
498
499error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
500 StringRef &Result) const {
501 ArrayRef<char> Raw = getSectionRawName(DRI);
502 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000503 return object_error::success;
504}
505
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000506ArrayRef<char>
507MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000508 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000509 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000510 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000511 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000512 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000513 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000514 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000515}
516
517StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
518 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
519 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000520}
521
Michael J. Spencer25b15772011-06-25 17:55:23 +0000522error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
523 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000524 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000525 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000526 Result = Sect->Address;
527 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000528 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000529 Result = Sect->Address;
530 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000531 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000532}
533
Michael J. Spencer25b15772011-06-25 17:55:23 +0000534error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
535 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000536 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000537 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000538 Result = Sect->Size;
539 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000540 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000541 Result = Sect->Size;
542 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000543 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000544}
545
Michael J. Spencer25b15772011-06-25 17:55:23 +0000546error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
547 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000548 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000549 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000550 Result = MachOObj->getData(Sect->Offset, Sect->Size);
551 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000552 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000553 Result = MachOObj->getData(Sect->Offset, Sect->Size);
554 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000555 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000556}
557
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000558error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
559 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000560 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000561 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000562 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000563 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000564 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000565 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000566 }
567 return object_error::success;
568}
569
Michael J. Spencer25b15772011-06-25 17:55:23 +0000570error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
571 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000572 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000573 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000574 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000575 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000576 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000577 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000578 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000579 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000580}
581
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000582error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
583 bool &Result) const {
584 // FIXME: Unimplemented.
585 Result = false;
586 return object_error::success;
587}
588
589error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
590 bool &Result) const {
591 // FIXME: Unimplemented.
592 Result = false;
593 return object_error::success;
594}
595
Preston Gurdc68dda82012-04-12 20:13:57 +0000596error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
597 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000598 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000599 Result = true;
600 return object_error::success;
601}
602
603error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000604 bool &Result) const {
605 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000606 Result = false;
607 return object_error::success;
608}
609
610error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
611 bool &Result) const {
612 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000613 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000614 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
615 Result = (SectionType == MachO::SectionTypeZeroFill ||
616 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000617 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000618 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000619 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
620 Result = (SectionType == MachO::SectionTypeZeroFill ||
621 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000622 }
623
624 return object_error::success;
625}
626
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000627error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
628 bool &Result) const {
629 // Consider using the code from isSectionText to look for __const sections.
630 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
631 // to use section attributes to distinguish code from data.
632
633 // FIXME: Unimplemented.
634 Result = false;
635 return object_error::success;
636}
637
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000638error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
639 DataRefImpl Symb,
640 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000641 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000642 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000643 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000644 Result = false;
645 return object_error::success;
646 }
647
648 uint64_t SectBegin, SectEnd;
649 getSectionAddress(Sec, SectBegin);
650 getSectionSize(Sec, SectEnd);
651 SectEnd += SectBegin;
652
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000653 if (MachOObj->is64Bit()) {
654 InMemoryStruct<macho::Symbol64TableEntry> Entry;
655 getSymbol64TableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000656 uint64_t SymAddr= Entry->Value;
657 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000658 } else {
659 InMemoryStruct<macho::SymbolTableEntry> Entry;
660 getSymbolTableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000661 uint64_t SymAddr= Entry->Value;
662 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000663 }
Owen Andersoncd749882011-10-12 22:21:32 +0000664
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000665 return object_error::success;
666}
667
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000668relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
669 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000670 ret.d.b = getSectionIndex(Sec);
671 return relocation_iterator(RelocationRef(ret, this));
672}
673relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
674 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000675 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000676 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000677 last_reloc = Sect->NumRelocationTableEntries;
678 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000679 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000680 last_reloc = Sect->NumRelocationTableEntries;
681 }
682 DataRefImpl ret;
683 ret.d.a = last_reloc;
684 ret.d.b = getSectionIndex(Sec);
685 return relocation_iterator(RelocationRef(ret, this));
686}
687
688section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000689 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000690 moveToNextSection(DRI);
691 return section_iterator(SectionRef(DRI, this));
692}
693
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000694section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000695 DataRefImpl DRI;
696 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000697 return section_iterator(SectionRef(DRI, this));
698}
699
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000700/*===-- Relocations -------------------------------------------------------===*/
701
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000702const MachOFormat::RelocationEntry *
703MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000704 uint32_t relOffset;
705 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000706 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000707 relOffset = Sect->RelocationTableOffset;
708 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000709 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000710 relOffset = Sect->RelocationTableOffset;
711 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000712 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
713 StringRef Data =
714 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
715 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000716}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000717
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000718error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
719 RelocationRef &Res) const {
720 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000721 Res = RelocationRef(Rel, this);
722 return object_error::success;
723}
724error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
725 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000726 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000727 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000728 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000729 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000730 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000731 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000732 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000733 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000734 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000735
736 unsigned Arch = getArch();
737 bool isScattered = (Arch != Triple::x86_64) &&
738 (RE->Word0 & macho::RF_Scattered);
739 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000740 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000741 RelAddr = RE->Word0 & 0xFFFFFF;
742 else
743 RelAddr = RE->Word0;
744
745 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000746 return object_error::success;
747}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000748error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
749 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000750 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000751
752 unsigned Arch = getArch();
753 bool isScattered = (Arch != Triple::x86_64) &&
754 (RE->Word0 & macho::RF_Scattered);
755 if (isScattered)
756 Res = RE->Word0 & 0xFFFFFF;
757 else
758 Res = RE->Word0;
759 return object_error::success;
760}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000761error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
762 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000763 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000764 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
765 bool isExtern = (RE->Word1 >> 27) & 1;
766
767 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000768 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000769 if (isExtern) {
770 for (unsigned i = 0; i < SymbolIdx; i++) {
771 Sym.d.b++;
772 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000773 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000774 "Relocation symbol index out of range!");
775 }
776 }
777 Res = SymbolRef(Sym, this);
778 return object_error::success;
779}
780error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000781 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000782 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000783 Res = RE->Word0;
784 Res <<= 32;
785 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000786 return object_error::success;
787}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000788error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
789 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000790 // TODO: Support scattered relocations.
791 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000792 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000793
794 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000795 bool isScattered = (Arch != Triple::x86_64) &&
796 (RE->Word0 & macho::RF_Scattered);
797
798 unsigned r_type;
799 if (isScattered)
800 r_type = (RE->Word0 >> 24) & 0xF;
801 else
802 r_type = (RE->Word1 >> 28) & 0xF;
803
Owen Anderson0135fe12011-10-24 21:44:00 +0000804 switch (Arch) {
805 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000806 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000807 "GENERIC_RELOC_VANILLA",
808 "GENERIC_RELOC_PAIR",
809 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000810 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000811 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000812 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000813
Owen Andersoneb6bd332011-10-27 20:46:09 +0000814 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000815 res = "Unknown";
816 else
817 res = Table[r_type];
818 break;
819 }
820 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000821 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000822 "X86_64_RELOC_UNSIGNED",
823 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000824 "X86_64_RELOC_BRANCH",
825 "X86_64_RELOC_GOT_LOAD",
826 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000827 "X86_64_RELOC_SUBTRACTOR",
828 "X86_64_RELOC_SIGNED_1",
829 "X86_64_RELOC_SIGNED_2",
830 "X86_64_RELOC_SIGNED_4",
831 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000832
Owen Andersond8fa76d2011-10-24 23:20:07 +0000833 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000834 res = "Unknown";
835 else
836 res = Table[r_type];
837 break;
838 }
839 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000840 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000841 "ARM_RELOC_VANILLA",
842 "ARM_RELOC_PAIR",
843 "ARM_RELOC_SECTDIFF",
844 "ARM_RELOC_LOCAL_SECTDIFF",
845 "ARM_RELOC_PB_LA_PTR",
846 "ARM_RELOC_BR24",
847 "ARM_THUMB_RELOC_BR22",
848 "ARM_THUMB_32BIT_BRANCH",
849 "ARM_RELOC_HALF",
850 "ARM_RELOC_HALF_SECTDIFF" };
851
852 if (r_type > 9)
853 res = "Unknown";
854 else
855 res = Table[r_type];
856 break;
857 }
858 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000859 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000860 "PPC_RELOC_VANILLA",
861 "PPC_RELOC_PAIR",
862 "PPC_RELOC_BR14",
863 "PPC_RELOC_BR24",
864 "PPC_RELOC_HI16",
865 "PPC_RELOC_LO16",
866 "PPC_RELOC_HA16",
867 "PPC_RELOC_LO14",
868 "PPC_RELOC_SECTDIFF",
869 "PPC_RELOC_PB_LA_PTR",
870 "PPC_RELOC_HI16_SECTDIFF",
871 "PPC_RELOC_LO16_SECTDIFF",
872 "PPC_RELOC_HA16_SECTDIFF",
873 "PPC_RELOC_JBSR",
874 "PPC_RELOC_LO14_SECTDIFF",
875 "PPC_RELOC_LOCAL_SECTDIFF" };
876
877 res = Table[r_type];
878 break;
879 }
880 case Triple::UnknownArch:
881 res = "Unknown";
882 break;
883 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000884 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000885 return object_error::success;
886}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000887error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
888 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000889 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000890 bool isExtern = (RE->Word1 >> 27) & 1;
891 Res = 0;
892 if (!isExtern) {
893 const uint8_t* sectAddress = base();
894 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000895 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000896 sectAddress += Sect->Offset;
897 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000898 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000899 sectAddress += Sect->Offset;
900 }
901 Res = reinterpret_cast<uintptr_t>(sectAddress);
902 }
903 return object_error::success;
904}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000905
906// Helper to advance a section or symbol iterator multiple increments at a time.
907template<class T>
908error_code advance(T &it, size_t Val) {
909 error_code ec;
910 while (Val--) {
911 it.increment(ec);
912 }
913 return ec;
914}
915
916template<class T>
917void advanceTo(T &it, size_t Val) {
918 if (error_code ec = advance(it, Val))
919 report_fatal_error(ec.message());
920}
921
Owen Anderson1832f4d2011-10-26 20:42:54 +0000922void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000923 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000924 raw_string_ostream &fmt) const {
925 unsigned Arch = getArch();
926 bool isScattered = (Arch != Triple::x86_64) &&
927 (RE->Word0 & macho::RF_Scattered);
928
929 // Target of a scattered relocation is an address. In the interest of
930 // generating pretty output, scan through the symbol table looking for a
931 // symbol that aligns with that address. If we find one, print it.
932 // Otherwise, we just print the hex address of the target.
933 if (isScattered) {
934 uint32_t Val = RE->Word1;
935
936 error_code ec;
937 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
938 SI.increment(ec)) {
939 if (ec) report_fatal_error(ec.message());
940
941 uint64_t Addr;
942 StringRef Name;
943
944 if ((ec = SI->getAddress(Addr)))
945 report_fatal_error(ec.message());
946 if (Addr != Val) continue;
947 if ((ec = SI->getName(Name)))
948 report_fatal_error(ec.message());
949 fmt << Name;
950 return;
951 }
952
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000953 // If we couldn't find a symbol that this relocation refers to, try
954 // to find a section beginning instead.
955 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
956 SI.increment(ec)) {
957 if (ec) report_fatal_error(ec.message());
958
959 uint64_t Addr;
960 StringRef Name;
961
962 if ((ec = SI->getAddress(Addr)))
963 report_fatal_error(ec.message());
964 if (Addr != Val) continue;
965 if ((ec = SI->getName(Name)))
966 report_fatal_error(ec.message());
967 fmt << Name;
968 return;
969 }
970
Owen Anderson1832f4d2011-10-26 20:42:54 +0000971 fmt << format("0x%x", Val);
972 return;
973 }
974
975 StringRef S;
976 bool isExtern = (RE->Word1 >> 27) & 1;
977 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000978
979 if (isExtern) {
980 symbol_iterator SI = begin_symbols();
981 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000982 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000983 } else {
984 section_iterator SI = begin_sections();
985 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000986 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000987 }
988
Owen Anderson1832f4d2011-10-26 20:42:54 +0000989 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000990}
991
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000992error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
993 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000994 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000995
Owen Anderson1832f4d2011-10-26 20:42:54 +0000996 unsigned Arch = getArch();
997 bool isScattered = (Arch != Triple::x86_64) &&
998 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000999
Owen Anderson013d7562011-10-25 18:48:41 +00001000 std::string fmtbuf;
1001 raw_string_ostream fmt(fmtbuf);
1002
Owen Anderson1832f4d2011-10-26 20:42:54 +00001003 unsigned Type;
1004 if (isScattered)
1005 Type = (RE->Word0 >> 24) & 0xF;
1006 else
1007 Type = (RE->Word1 >> 28) & 0xF;
1008
Owen Andersoneb6bd332011-10-27 20:46:09 +00001009 bool isPCRel;
1010 if (isScattered)
1011 isPCRel = ((RE->Word0 >> 30) & 1);
1012 else
1013 isPCRel = ((RE->Word1 >> 24) & 1);
1014
Owen Andersond8fa76d2011-10-24 23:20:07 +00001015 // Determine any addends that should be displayed with the relocation.
1016 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001017
1018 // X86_64 has entirely custom relocation types.
1019 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001020 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001021
Owen Andersond8fa76d2011-10-24 23:20:07 +00001022 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001023 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1024 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1025 printRelocationTargetName(RE, fmt);
1026 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001027 if (isPCRel) fmt << "PCREL";
1028 break;
1029 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001030 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001031 DataRefImpl RelNext = Rel;
1032 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001033 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001034
1035 // X86_64_SUBTRACTOR must be followed by a relocation of type
1036 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001037 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001038 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001039 if (RType != 0)
1040 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1041 "X86_64_RELOC_SUBTRACTOR.");
1042
Owen Andersonef22f782011-10-26 17:28:49 +00001043 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1044 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001045 printRelocationTargetName(RENext, fmt);
1046 fmt << "-";
1047 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001048 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001049 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001050 case macho::RIT_X86_64_TLV:
1051 printRelocationTargetName(RE, fmt);
1052 fmt << "@TLV";
1053 if (isPCRel) fmt << "P";
1054 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001055 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1056 printRelocationTargetName(RE, fmt);
1057 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001058 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001059 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1060 printRelocationTargetName(RE, fmt);
1061 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001062 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001063 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1064 printRelocationTargetName(RE, fmt);
1065 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001066 break;
1067 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001068 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001069 break;
1070 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001071 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001072 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1073 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001074 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001075 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001076 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001077 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001078 DataRefImpl RelNext = Rel;
1079 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001080 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001081
1082 // X86 sect diff's must be followed by a relocation of type
1083 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001084 bool isNextScattered = (Arch != Triple::x86_64) &&
1085 (RENext->Word0 & macho::RF_Scattered);
1086 unsigned RType;
1087 if (isNextScattered)
1088 RType = (RENext->Word0 >> 24) & 0xF;
1089 else
1090 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001091 if (RType != 1)
1092 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001093 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001094
Owen Anderson1832f4d2011-10-26 20:42:54 +00001095 printRelocationTargetName(RE, fmt);
1096 fmt << "-";
1097 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001098 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001099 }
1100 }
Owen Anderson013d7562011-10-25 18:48:41 +00001101
Owen Anderson1832f4d2011-10-26 20:42:54 +00001102 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001103 // All X86 relocations that need special printing were already
1104 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001105 switch (Type) {
1106 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001107 DataRefImpl RelNext = Rel;
1108 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001109 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001110
1111 // X86 sect diff's must be followed by a relocation of type
1112 // GENERIC_RELOC_PAIR.
1113 bool isNextScattered = (Arch != Triple::x86_64) &&
1114 (RENext->Word0 & macho::RF_Scattered);
1115 unsigned RType;
1116 if (isNextScattered)
1117 RType = (RENext->Word0 >> 24) & 0xF;
1118 else
1119 RType = (RENext->Word1 >> 28) & 0xF;
1120 if (RType != 1)
1121 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1122 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1123
1124 printRelocationTargetName(RE, fmt);
1125 fmt << "-";
1126 printRelocationTargetName(RENext, fmt);
1127 break;
1128 }
1129 case macho::RIT_Generic_TLV: {
1130 printRelocationTargetName(RE, fmt);
1131 fmt << "@TLV";
1132 if (isPCRel) fmt << "P";
1133 break;
1134 }
1135 default:
1136 printRelocationTargetName(RE, fmt);
1137 }
Owen Anderson013d7562011-10-25 18:48:41 +00001138 } else { // ARM-specific relocations
1139 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001140 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1141 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001142 // Half relocations steal a bit from the length field to encode
1143 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001144 bool isUpper;
1145 if (isScattered)
1146 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001147 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001148 isUpper = (RE->Word1 >> 25) & 1;
1149
1150 if (isUpper)
1151 fmt << ":upper16:(";
1152 else
1153 fmt << ":lower16:(";
1154 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001155
Owen Anderson013d7562011-10-25 18:48:41 +00001156 DataRefImpl RelNext = Rel;
1157 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001158 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001159
1160 // ARM half relocs must be followed by a relocation of type
1161 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001162 bool isNextScattered = (Arch != Triple::x86_64) &&
1163 (RENext->Word0 & macho::RF_Scattered);
1164 unsigned RType;
1165 if (isNextScattered)
1166 RType = (RENext->Word0 >> 24) & 0xF;
1167 else
1168 RType = (RENext->Word1 >> 28) & 0xF;
1169
Owen Anderson013d7562011-10-25 18:48:41 +00001170 if (RType != 1)
1171 report_fatal_error("Expected ARM_RELOC_PAIR after "
1172 "GENERIC_RELOC_HALF");
1173
Owen Anderson1832f4d2011-10-26 20:42:54 +00001174 // NOTE: The half of the target virtual address is stashed in the
1175 // address field of the secondary relocation, but we can't reverse
1176 // engineer the constant offset from it without decoding the movw/movt
1177 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001178
1179 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1180 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001181 if (Type == macho::RIT_ARM_HalfDifference) {
1182 fmt << "-";
1183 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001184 }
1185
Owen Anderson013d7562011-10-25 18:48:41 +00001186 fmt << ")";
1187 break;
1188 }
1189 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001190 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001191 }
1192 }
1193 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001194 } else
1195 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001196
Owen Anderson0135fe12011-10-24 21:44:00 +00001197 fmt.flush();
1198 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001199 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001200}
1201
Owen Anderson0685e942011-10-25 20:35:53 +00001202error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1203 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001204 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001205
Owen Anderson0685e942011-10-25 20:35:53 +00001206 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001207 bool isScattered = (Arch != Triple::x86_64) &&
1208 (RE->Word0 & macho::RF_Scattered);
1209 unsigned Type;
1210 if (isScattered)
1211 Type = (RE->Word0 >> 24) & 0xF;
1212 else
1213 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001214
1215 Result = false;
1216
1217 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1218 // is always hidden.
1219 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001220 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001221 } else if (Arch == Triple::x86_64) {
1222 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1223 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001224 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001225 DataRefImpl RelPrev = Rel;
1226 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001227 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001228
1229 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1230
Owen Anderson1832f4d2011-10-26 20:42:54 +00001231 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001232 }
1233 }
1234
1235 return object_error::success;
1236}
1237
David Meyer5c2b4ea2012-03-01 01:36:50 +00001238error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1239 LibraryRef &Res) const {
1240 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1241}
1242
1243error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1244 StringRef &Res) const {
1245 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1246}
1247
1248
Eric Christopher6256b032011-04-22 03:19:48 +00001249/*===-- Miscellaneous -----------------------------------------------------===*/
1250
1251uint8_t MachOObjectFile::getBytesInAddress() const {
1252 return MachOObj->is64Bit() ? 8 : 4;
1253}
1254
1255StringRef MachOObjectFile::getFileFormatName() const {
1256 if (!MachOObj->is64Bit()) {
1257 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001258 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001259 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001260 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001261 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001262 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001263 return "Mach-O 32-bit ppc";
1264 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001265 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001266 "64-bit object file when we're not 64-bit?");
1267 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001268 }
1269 }
1270
Eric Christopherb3e6b042013-02-28 20:26:17 +00001271 // Make sure the cpu type has the correct mask.
1272 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1273 == llvm::MachO::CPUArchABI64 &&
1274 "32-bit object file when we're 64-bit?");
1275
Eric Christopher6256b032011-04-22 03:19:48 +00001276 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001277 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001278 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001279 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001280 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001281 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001282 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001283 }
1284}
1285
1286unsigned MachOObjectFile::getArch() const {
1287 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001288 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001289 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001290 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001291 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001292 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001293 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001294 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001295 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001296 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001297 return Triple::ppc64;
1298 default:
1299 return Triple::UnknownArch;
1300 }
1301}
1302
Owen Andersonf7c93a32011-10-11 17:32:27 +00001303} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001304} // end namespace llvm