blob: 4d9186490fee6d4f0373362a3c3080a7380b70b3 [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
Rafael Espindola00555c12013-04-06 01:59:05 +000081const MachOFormat::SymbolTableEntry *
82MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Eric Christopher6256b032011-04-22 03:19:48 +000083 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
Rafael Espindola00555c12013-04-06 01:59:05 +000092 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
93 unsigned Index = DRI.d.b;
94 uint64_t Offset = (SymbolTableOffset +
95 Index * sizeof(macho::SymbolTableEntry));
96 StringRef Data = MachOObj->getData(Offset,
97 sizeof(MachOFormat::SymbolTableEntry));
98 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +000099}
100
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000101const MachOFormat::Symbol64TableEntry*
102MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000103 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
104 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
105 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
106
107 if (RegisteredStringTable != DRI.d.a) {
108 MachOObj->RegisterStringTable(*SymtabLoadCmd);
109 RegisteredStringTable = DRI.d.a;
110 }
111
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000112 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
113 unsigned Index = DRI.d.b;
114 uint64_t Offset = (SymbolTableOffset +
115 Index * sizeof(macho::Symbol64TableEntry));
116 StringRef Data = MachOObj->getData(Offset,
117 sizeof(MachOFormat::Symbol64TableEntry));
118 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000119}
120
Michael J. Spencer25b15772011-06-25 17:55:23 +0000121error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
122 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000123 DRI.d.b++;
124 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000125 Result = SymbolRef(DRI, this);
126 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000127}
128
Michael J. Spencer25b15772011-06-25 17:55:23 +0000129error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
130 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000131 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000132 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000133 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
134 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000135 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000136 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
137 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000138 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000139}
140
Danil Malyshevb0436a72011-11-29 17:40:10 +0000141error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
142 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000143 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000144 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000145 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000146 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000147 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000148 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000149 Result += Section->Offset - Section->Address;
150 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000151 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000152 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000153 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000154 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000155 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000156 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000157 Result += Section->Offset - Section->Address;
158 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000159 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000160
Michael J. Spencer25b15772011-06-25 17:55:23 +0000161 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000162}
163
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000164error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
165 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000166 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000167 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000168 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000169 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000170 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000171 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000172 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000173 return object_error::success;
174}
175
Michael J. Spencer25b15772011-06-25 17:55:23 +0000176error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
177 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000178 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
179 uint64_t BeginOffset;
180 uint64_t EndOffset = 0;
181 uint8_t SectionIndex;
182 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000183 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000184 BeginOffset = Entry->Value;
185 SectionIndex = Entry->SectionIndex;
186 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000187 uint32_t flags = SymbolRef::SF_None;
188 getSymbolFlags(DRI, flags);
189 if (flags & SymbolRef::SF_Common)
190 Result = Entry->Value;
191 else
192 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000193 return object_error::success;
194 }
195 // Unfortunately symbols are unsorted so we need to touch all
196 // symbols from load command
197 DRI.d.b = 0;
198 uint32_t Command = DRI.d.a;
199 while (Command == DRI.d.a) {
200 moveToNextSymbol(DRI);
201 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000202 Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000203 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
204 if (!EndOffset || Entry->Value < EndOffset)
205 EndOffset = Entry->Value;
206 }
207 DRI.d.b++;
208 }
209 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000210 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000211 BeginOffset = Entry->Value;
212 SectionIndex = Entry->SectionIndex;
213 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000214 uint32_t flags = SymbolRef::SF_None;
215 getSymbolFlags(DRI, flags);
216 if (flags & SymbolRef::SF_Common)
217 Result = Entry->Value;
218 else
219 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000220 return object_error::success;
221 }
222 // Unfortunately symbols are unsorted so we need to touch all
223 // symbols from load command
224 DRI.d.b = 0;
225 uint32_t Command = DRI.d.a;
226 while (Command == DRI.d.a) {
227 moveToNextSymbol(DRI);
228 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000229 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000230 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
231 if (!EndOffset || Entry->Value < EndOffset)
232 EndOffset = Entry->Value;
233 }
234 DRI.d.b++;
235 }
236 }
237 if (!EndOffset) {
238 uint64_t Size;
239 getSectionSize(Sections[SectionIndex-1], Size);
240 getSectionAddress(Sections[SectionIndex-1], EndOffset);
241 EndOffset += Size;
242 }
243 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000244 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000245}
246
Michael J. Spencer25b15772011-06-25 17:55:23 +0000247error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
248 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000249 uint8_t Type, Flags;
250 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000251 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000252 Type = Entry->Type;
253 Flags = Entry->Flags;
254 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000255 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000256 Type = Entry->Type;
257 Flags = Entry->Flags;
258 }
Eric Christopher6256b032011-04-22 03:19:48 +0000259
260 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000261 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000262 case macho::STT_Undefined:
263 Char = 'u';
264 break;
265 case macho::STT_Absolute:
266 case macho::STT_Section:
267 Char = 's';
268 break;
269 default:
270 Char = '?';
271 break;
272 }
273
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000274 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000275 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000276 Result = Char;
277 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000278}
279
David Meyerc46255a2012-02-28 23:47:53 +0000280error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
281 uint32_t &Result) const {
282 uint16_t MachOFlags;
283 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000284 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000285 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000286 MachOFlags = Entry->Flags;
287 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000288 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000289 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000290 MachOFlags = Entry->Flags;
291 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000292 }
293
Preston Gurdc68dda82012-04-12 20:13:57 +0000294 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000295 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000296
297 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
298 Result |= SymbolRef::SF_Undefined;
299
David Meyerc46255a2012-02-28 23:47:53 +0000300 if (MachOFlags & macho::STF_StabsEntryMask)
301 Result |= SymbolRef::SF_FormatSpecific;
302
Preston Gurdc68dda82012-04-12 20:13:57 +0000303 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000304 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000305 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
306 Result |= SymbolRef::SF_Common;
307 }
David Meyerc46255a2012-02-28 23:47:53 +0000308
309 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
310 Result |= SymbolRef::SF_Weak;
311
312 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
313 Result |= SymbolRef::SF_Absolute;
314
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000315 return object_error::success;
316}
317
318error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
319 section_iterator &Res) const {
320 uint8_t index;
321 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000322 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000323 index = Entry->SectionIndex;
324 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000325 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000326 index = Entry->SectionIndex;
327 }
328
329 if (index == 0)
330 Res = end_sections();
331 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000332 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000333
334 return object_error::success;
335}
336
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000337error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000338 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000339 uint8_t n_type;
340 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000341 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000342 n_type = Entry->Type;
343 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000344 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000345 n_type = Entry->Type;
346 }
347 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000348
349 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000350 if (n_type & MachO::NlistMaskStab) {
351 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000352 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000353 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000354
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000355 switch (n_type & MachO::NlistMaskType) {
356 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000357 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000358 break;
359 case MachO::NListTypeSection :
360 Res = SymbolRef::ST_Function;
361 break;
362 }
363 return object_error::success;
364}
365
Tim Northovera41dce32012-10-29 10:47:00 +0000366error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
367 uint64_t &Val) const {
368 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
369}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000370
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000371symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000372 // DRI.d.a = segment number; DRI.d.b = symbol index.
373 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000374 moveToNextSymbol(DRI);
375 return symbol_iterator(SymbolRef(DRI, this));
376}
377
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000378symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000379 DataRefImpl DRI;
380 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000381 return symbol_iterator(SymbolRef(DRI, this));
382}
383
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000384symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
385 // TODO: implement
386 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
387}
388
389symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
390 // TODO: implement
391 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
392}
Eric Christopher6256b032011-04-22 03:19:48 +0000393
David Meyer5c2b4ea2012-03-01 01:36:50 +0000394library_iterator MachOObjectFile::begin_libraries_needed() const {
395 // TODO: implement
396 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
397}
398
399library_iterator MachOObjectFile::end_libraries_needed() const {
400 // TODO: implement
401 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
402}
403
David Meyer97f77872012-03-01 22:19:54 +0000404StringRef MachOObjectFile::getLoadName() const {
405 // TODO: Implement
406 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
407}
408
Eric Christopher6256b032011-04-22 03:19:48 +0000409/*===-- Sections ----------------------------------------------------------===*/
410
411void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
412 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
413 while (DRI.d.a < LoadCommandCount) {
414 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
415 if (LCI.Command.Type == macho::LCT_Segment) {
416 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
417 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
418 if (DRI.d.b < SegmentLoadCmd->NumSections)
419 return;
420 } else if (LCI.Command.Type == macho::LCT_Segment64) {
421 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
422 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
423 if (DRI.d.b < Segment64LoadCmd->NumSections)
424 return;
425 }
426
427 DRI.d.a++;
428 DRI.d.b = 0;
429 }
430}
431
Michael J. Spencer25b15772011-06-25 17:55:23 +0000432error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
433 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000434 DRI.d.b++;
435 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000436 Result = SectionRef(DRI, this);
437 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000438}
439
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000440static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
Eric Christopher6256b032011-04-22 03:19:48 +0000441 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000442 if (LCI.Command.Type == macho::LCT_Segment64)
443 return true;
444 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
445 return false;
446}
447
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000448const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000449 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
450 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
451 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000452 DRI.d.b * sizeof(MachOFormat::Section);
453 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
454 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000455}
456
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000457std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
458 SectionList::const_iterator loc =
459 std::find(Sections.begin(), Sections.end(), Sec);
460 assert(loc != Sections.end() && "Sec is not a valid section!");
461 return std::distance(Sections.begin(), loc);
462}
463
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000464const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000465MachOObjectFile::getSection64(DataRefImpl DRI) const {
466 assert(is64BitLoadCommand(MachOObj.get(), DRI));
Benjamin Kramer7d145782011-07-15 00:14:48 +0000467 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000468 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000469 DRI.d.b * sizeof(MachOFormat::Section64);
470 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
471 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000472}
473
Rafael Espindolacef81b32012-12-21 03:47:03 +0000474static StringRef parseSegmentOrSectionName(const char *P) {
475 if (P[15] == 0)
476 // Null terminated.
477 return P;
478 // Not null terminated, so this is a 16 char string.
479 return StringRef(P, 16);
480}
481
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000482ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000483 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000484 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000485 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000486 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000487 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000488 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000489 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000490}
491
492error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
493 StringRef &Result) const {
494 ArrayRef<char> Raw = getSectionRawName(DRI);
495 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000496 return object_error::success;
497}
498
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000499ArrayRef<char>
500MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000501 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000502 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000503 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000504 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000505 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000506 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000507 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000508}
509
510StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
511 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
512 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000513}
514
Michael J. Spencer25b15772011-06-25 17:55:23 +0000515error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
516 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000517 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000518 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000519 Result = Sect->Address;
520 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000521 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000522 Result = Sect->Address;
523 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000524 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000525}
526
Michael J. Spencer25b15772011-06-25 17:55:23 +0000527error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
528 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000529 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000530 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000531 Result = Sect->Size;
532 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000533 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000534 Result = Sect->Size;
535 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000536 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000537}
538
Michael J. Spencer25b15772011-06-25 17:55:23 +0000539error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
540 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000541 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000542 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000543 Result = MachOObj->getData(Sect->Offset, Sect->Size);
544 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000545 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000546 Result = MachOObj->getData(Sect->Offset, Sect->Size);
547 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000548 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000549}
550
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000551error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
552 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000553 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000554 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000555 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000556 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000557 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000558 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000559 }
560 return object_error::success;
561}
562
Michael J. Spencer25b15772011-06-25 17:55:23 +0000563error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
564 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000565 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000566 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000567 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000568 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000569 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000570 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000571 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000572 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000573}
574
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000575error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
576 bool &Result) const {
577 // FIXME: Unimplemented.
578 Result = false;
579 return object_error::success;
580}
581
582error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
583 bool &Result) const {
584 // FIXME: Unimplemented.
585 Result = false;
586 return object_error::success;
587}
588
Preston Gurdc68dda82012-04-12 20:13:57 +0000589error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
590 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000591 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000592 Result = true;
593 return object_error::success;
594}
595
596error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000597 bool &Result) const {
598 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000599 Result = false;
600 return object_error::success;
601}
602
603error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
604 bool &Result) const {
605 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000606 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000607 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
608 Result = (SectionType == MachO::SectionTypeZeroFill ||
609 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000610 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000611 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000612 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
613 Result = (SectionType == MachO::SectionTypeZeroFill ||
614 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000615 }
616
617 return object_error::success;
618}
619
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000620error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
621 bool &Result) const {
622 // Consider using the code from isSectionText to look for __const sections.
623 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
624 // to use section attributes to distinguish code from data.
625
626 // FIXME: Unimplemented.
627 Result = false;
628 return object_error::success;
629}
630
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000631error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
632 DataRefImpl Symb,
633 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000634 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000635 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000636 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000637 Result = false;
638 return object_error::success;
639 }
640
641 uint64_t SectBegin, SectEnd;
642 getSectionAddress(Sec, SectBegin);
643 getSectionSize(Sec, SectEnd);
644 SectEnd += SectBegin;
645
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000646 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000647 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000648 uint64_t SymAddr= Entry->Value;
649 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000650 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000651 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000652 uint64_t SymAddr= Entry->Value;
653 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000654 }
Owen Andersoncd749882011-10-12 22:21:32 +0000655
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000656 return object_error::success;
657}
658
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000659relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
660 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000661 ret.d.b = getSectionIndex(Sec);
662 return relocation_iterator(RelocationRef(ret, this));
663}
664relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
665 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000666 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000667 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000668 last_reloc = Sect->NumRelocationTableEntries;
669 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000670 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000671 last_reloc = Sect->NumRelocationTableEntries;
672 }
673 DataRefImpl ret;
674 ret.d.a = last_reloc;
675 ret.d.b = getSectionIndex(Sec);
676 return relocation_iterator(RelocationRef(ret, this));
677}
678
679section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000680 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000681 moveToNextSection(DRI);
682 return section_iterator(SectionRef(DRI, this));
683}
684
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000685section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000686 DataRefImpl DRI;
687 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000688 return section_iterator(SectionRef(DRI, this));
689}
690
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000691/*===-- Relocations -------------------------------------------------------===*/
692
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000693const MachOFormat::RelocationEntry *
694MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000695 uint32_t relOffset;
696 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000697 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000698 relOffset = Sect->RelocationTableOffset;
699 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000700 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000701 relOffset = Sect->RelocationTableOffset;
702 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000703 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
704 StringRef Data =
705 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
706 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000707}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000708
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000709error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
710 RelocationRef &Res) const {
711 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000712 Res = RelocationRef(Rel, this);
713 return object_error::success;
714}
715error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
716 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000717 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000718 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000719 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000720 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000721 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000722 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000723 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000724 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000725 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000726
727 unsigned Arch = getArch();
728 bool isScattered = (Arch != Triple::x86_64) &&
729 (RE->Word0 & macho::RF_Scattered);
730 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000731 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000732 RelAddr = RE->Word0 & 0xFFFFFF;
733 else
734 RelAddr = RE->Word0;
735
736 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000737 return object_error::success;
738}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000739error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
740 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000741 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000742
743 unsigned Arch = getArch();
744 bool isScattered = (Arch != Triple::x86_64) &&
745 (RE->Word0 & macho::RF_Scattered);
746 if (isScattered)
747 Res = RE->Word0 & 0xFFFFFF;
748 else
749 Res = RE->Word0;
750 return object_error::success;
751}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000752error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
753 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000754 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000755 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
756 bool isExtern = (RE->Word1 >> 27) & 1;
757
758 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000759 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000760 if (isExtern) {
761 for (unsigned i = 0; i < SymbolIdx; i++) {
762 Sym.d.b++;
763 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000764 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000765 "Relocation symbol index out of range!");
766 }
767 }
768 Res = SymbolRef(Sym, this);
769 return object_error::success;
770}
771error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000772 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000773 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000774 Res = RE->Word0;
775 Res <<= 32;
776 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000777 return object_error::success;
778}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000779error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
780 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000781 // TODO: Support scattered relocations.
782 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000783 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000784
785 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000786 bool isScattered = (Arch != Triple::x86_64) &&
787 (RE->Word0 & macho::RF_Scattered);
788
789 unsigned r_type;
790 if (isScattered)
791 r_type = (RE->Word0 >> 24) & 0xF;
792 else
793 r_type = (RE->Word1 >> 28) & 0xF;
794
Owen Anderson0135fe12011-10-24 21:44:00 +0000795 switch (Arch) {
796 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000797 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000798 "GENERIC_RELOC_VANILLA",
799 "GENERIC_RELOC_PAIR",
800 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000801 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000802 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000803 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000804
Owen Andersoneb6bd332011-10-27 20:46:09 +0000805 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000806 res = "Unknown";
807 else
808 res = Table[r_type];
809 break;
810 }
811 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000812 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000813 "X86_64_RELOC_UNSIGNED",
814 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000815 "X86_64_RELOC_BRANCH",
816 "X86_64_RELOC_GOT_LOAD",
817 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000818 "X86_64_RELOC_SUBTRACTOR",
819 "X86_64_RELOC_SIGNED_1",
820 "X86_64_RELOC_SIGNED_2",
821 "X86_64_RELOC_SIGNED_4",
822 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000823
Owen Andersond8fa76d2011-10-24 23:20:07 +0000824 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000825 res = "Unknown";
826 else
827 res = Table[r_type];
828 break;
829 }
830 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000831 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000832 "ARM_RELOC_VANILLA",
833 "ARM_RELOC_PAIR",
834 "ARM_RELOC_SECTDIFF",
835 "ARM_RELOC_LOCAL_SECTDIFF",
836 "ARM_RELOC_PB_LA_PTR",
837 "ARM_RELOC_BR24",
838 "ARM_THUMB_RELOC_BR22",
839 "ARM_THUMB_32BIT_BRANCH",
840 "ARM_RELOC_HALF",
841 "ARM_RELOC_HALF_SECTDIFF" };
842
843 if (r_type > 9)
844 res = "Unknown";
845 else
846 res = Table[r_type];
847 break;
848 }
849 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000850 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000851 "PPC_RELOC_VANILLA",
852 "PPC_RELOC_PAIR",
853 "PPC_RELOC_BR14",
854 "PPC_RELOC_BR24",
855 "PPC_RELOC_HI16",
856 "PPC_RELOC_LO16",
857 "PPC_RELOC_HA16",
858 "PPC_RELOC_LO14",
859 "PPC_RELOC_SECTDIFF",
860 "PPC_RELOC_PB_LA_PTR",
861 "PPC_RELOC_HI16_SECTDIFF",
862 "PPC_RELOC_LO16_SECTDIFF",
863 "PPC_RELOC_HA16_SECTDIFF",
864 "PPC_RELOC_JBSR",
865 "PPC_RELOC_LO14_SECTDIFF",
866 "PPC_RELOC_LOCAL_SECTDIFF" };
867
868 res = Table[r_type];
869 break;
870 }
871 case Triple::UnknownArch:
872 res = "Unknown";
873 break;
874 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000875 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000876 return object_error::success;
877}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000878error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
879 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000880 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000881 bool isExtern = (RE->Word1 >> 27) & 1;
882 Res = 0;
883 if (!isExtern) {
884 const uint8_t* sectAddress = base();
885 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000886 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000887 sectAddress += Sect->Offset;
888 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000889 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000890 sectAddress += Sect->Offset;
891 }
892 Res = reinterpret_cast<uintptr_t>(sectAddress);
893 }
894 return object_error::success;
895}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000896
897// Helper to advance a section or symbol iterator multiple increments at a time.
898template<class T>
899error_code advance(T &it, size_t Val) {
900 error_code ec;
901 while (Val--) {
902 it.increment(ec);
903 }
904 return ec;
905}
906
907template<class T>
908void advanceTo(T &it, size_t Val) {
909 if (error_code ec = advance(it, Val))
910 report_fatal_error(ec.message());
911}
912
Owen Anderson1832f4d2011-10-26 20:42:54 +0000913void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000914 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000915 raw_string_ostream &fmt) const {
916 unsigned Arch = getArch();
917 bool isScattered = (Arch != Triple::x86_64) &&
918 (RE->Word0 & macho::RF_Scattered);
919
920 // Target of a scattered relocation is an address. In the interest of
921 // generating pretty output, scan through the symbol table looking for a
922 // symbol that aligns with that address. If we find one, print it.
923 // Otherwise, we just print the hex address of the target.
924 if (isScattered) {
925 uint32_t Val = RE->Word1;
926
927 error_code ec;
928 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
929 SI.increment(ec)) {
930 if (ec) report_fatal_error(ec.message());
931
932 uint64_t Addr;
933 StringRef Name;
934
935 if ((ec = SI->getAddress(Addr)))
936 report_fatal_error(ec.message());
937 if (Addr != Val) continue;
938 if ((ec = SI->getName(Name)))
939 report_fatal_error(ec.message());
940 fmt << Name;
941 return;
942 }
943
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000944 // If we couldn't find a symbol that this relocation refers to, try
945 // to find a section beginning instead.
946 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
947 SI.increment(ec)) {
948 if (ec) report_fatal_error(ec.message());
949
950 uint64_t Addr;
951 StringRef Name;
952
953 if ((ec = SI->getAddress(Addr)))
954 report_fatal_error(ec.message());
955 if (Addr != Val) continue;
956 if ((ec = SI->getName(Name)))
957 report_fatal_error(ec.message());
958 fmt << Name;
959 return;
960 }
961
Owen Anderson1832f4d2011-10-26 20:42:54 +0000962 fmt << format("0x%x", Val);
963 return;
964 }
965
966 StringRef S;
967 bool isExtern = (RE->Word1 >> 27) & 1;
968 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000969
970 if (isExtern) {
971 symbol_iterator SI = begin_symbols();
972 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000973 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000974 } else {
975 section_iterator SI = begin_sections();
976 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000977 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000978 }
979
Owen Anderson1832f4d2011-10-26 20:42:54 +0000980 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000981}
982
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000983error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
984 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000985 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000986
Owen Anderson1832f4d2011-10-26 20:42:54 +0000987 unsigned Arch = getArch();
988 bool isScattered = (Arch != Triple::x86_64) &&
989 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000990
Owen Anderson013d7562011-10-25 18:48:41 +0000991 std::string fmtbuf;
992 raw_string_ostream fmt(fmtbuf);
993
Owen Anderson1832f4d2011-10-26 20:42:54 +0000994 unsigned Type;
995 if (isScattered)
996 Type = (RE->Word0 >> 24) & 0xF;
997 else
998 Type = (RE->Word1 >> 28) & 0xF;
999
Owen Andersoneb6bd332011-10-27 20:46:09 +00001000 bool isPCRel;
1001 if (isScattered)
1002 isPCRel = ((RE->Word0 >> 30) & 1);
1003 else
1004 isPCRel = ((RE->Word1 >> 24) & 1);
1005
Owen Andersond8fa76d2011-10-24 23:20:07 +00001006 // Determine any addends that should be displayed with the relocation.
1007 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001008
1009 // X86_64 has entirely custom relocation types.
1010 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001011 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001012
Owen Andersond8fa76d2011-10-24 23:20:07 +00001013 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001014 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1015 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1016 printRelocationTargetName(RE, fmt);
1017 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001018 if (isPCRel) fmt << "PCREL";
1019 break;
1020 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001021 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001022 DataRefImpl RelNext = Rel;
1023 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001024 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001025
1026 // X86_64_SUBTRACTOR must be followed by a relocation of type
1027 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001028 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001029 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001030 if (RType != 0)
1031 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1032 "X86_64_RELOC_SUBTRACTOR.");
1033
Owen Andersonef22f782011-10-26 17:28:49 +00001034 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1035 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001036 printRelocationTargetName(RENext, fmt);
1037 fmt << "-";
1038 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001039 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001040 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001041 case macho::RIT_X86_64_TLV:
1042 printRelocationTargetName(RE, fmt);
1043 fmt << "@TLV";
1044 if (isPCRel) fmt << "P";
1045 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001046 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1047 printRelocationTargetName(RE, fmt);
1048 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001049 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001050 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1051 printRelocationTargetName(RE, fmt);
1052 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001053 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001054 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1055 printRelocationTargetName(RE, fmt);
1056 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001057 break;
1058 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001059 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001060 break;
1061 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001062 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001063 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1064 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001065 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001066 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001067 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001068 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001069 DataRefImpl RelNext = Rel;
1070 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001071 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001072
1073 // X86 sect diff's must be followed by a relocation of type
1074 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001075 bool isNextScattered = (Arch != Triple::x86_64) &&
1076 (RENext->Word0 & macho::RF_Scattered);
1077 unsigned RType;
1078 if (isNextScattered)
1079 RType = (RENext->Word0 >> 24) & 0xF;
1080 else
1081 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001082 if (RType != 1)
1083 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001084 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001085
Owen Anderson1832f4d2011-10-26 20:42:54 +00001086 printRelocationTargetName(RE, fmt);
1087 fmt << "-";
1088 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001089 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001090 }
1091 }
Owen Anderson013d7562011-10-25 18:48:41 +00001092
Owen Anderson1832f4d2011-10-26 20:42:54 +00001093 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001094 // All X86 relocations that need special printing were already
1095 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001096 switch (Type) {
1097 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001098 DataRefImpl RelNext = Rel;
1099 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001100 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001101
1102 // X86 sect diff's must be followed by a relocation of type
1103 // GENERIC_RELOC_PAIR.
1104 bool isNextScattered = (Arch != Triple::x86_64) &&
1105 (RENext->Word0 & macho::RF_Scattered);
1106 unsigned RType;
1107 if (isNextScattered)
1108 RType = (RENext->Word0 >> 24) & 0xF;
1109 else
1110 RType = (RENext->Word1 >> 28) & 0xF;
1111 if (RType != 1)
1112 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1113 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1114
1115 printRelocationTargetName(RE, fmt);
1116 fmt << "-";
1117 printRelocationTargetName(RENext, fmt);
1118 break;
1119 }
1120 case macho::RIT_Generic_TLV: {
1121 printRelocationTargetName(RE, fmt);
1122 fmt << "@TLV";
1123 if (isPCRel) fmt << "P";
1124 break;
1125 }
1126 default:
1127 printRelocationTargetName(RE, fmt);
1128 }
Owen Anderson013d7562011-10-25 18:48:41 +00001129 } else { // ARM-specific relocations
1130 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001131 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1132 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001133 // Half relocations steal a bit from the length field to encode
1134 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001135 bool isUpper;
1136 if (isScattered)
1137 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001138 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001139 isUpper = (RE->Word1 >> 25) & 1;
1140
1141 if (isUpper)
1142 fmt << ":upper16:(";
1143 else
1144 fmt << ":lower16:(";
1145 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001146
Owen Anderson013d7562011-10-25 18:48:41 +00001147 DataRefImpl RelNext = Rel;
1148 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001149 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001150
1151 // ARM half relocs must be followed by a relocation of type
1152 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001153 bool isNextScattered = (Arch != Triple::x86_64) &&
1154 (RENext->Word0 & macho::RF_Scattered);
1155 unsigned RType;
1156 if (isNextScattered)
1157 RType = (RENext->Word0 >> 24) & 0xF;
1158 else
1159 RType = (RENext->Word1 >> 28) & 0xF;
1160
Owen Anderson013d7562011-10-25 18:48:41 +00001161 if (RType != 1)
1162 report_fatal_error("Expected ARM_RELOC_PAIR after "
1163 "GENERIC_RELOC_HALF");
1164
Owen Anderson1832f4d2011-10-26 20:42:54 +00001165 // NOTE: The half of the target virtual address is stashed in the
1166 // address field of the secondary relocation, but we can't reverse
1167 // engineer the constant offset from it without decoding the movw/movt
1168 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001169
1170 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1171 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001172 if (Type == macho::RIT_ARM_HalfDifference) {
1173 fmt << "-";
1174 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001175 }
1176
Owen Anderson013d7562011-10-25 18:48:41 +00001177 fmt << ")";
1178 break;
1179 }
1180 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001181 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001182 }
1183 }
1184 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001185 } else
1186 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001187
Owen Anderson0135fe12011-10-24 21:44:00 +00001188 fmt.flush();
1189 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001190 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001191}
1192
Owen Anderson0685e942011-10-25 20:35:53 +00001193error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1194 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001195 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001196
Owen Anderson0685e942011-10-25 20:35:53 +00001197 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001198 bool isScattered = (Arch != Triple::x86_64) &&
1199 (RE->Word0 & macho::RF_Scattered);
1200 unsigned Type;
1201 if (isScattered)
1202 Type = (RE->Word0 >> 24) & 0xF;
1203 else
1204 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001205
1206 Result = false;
1207
1208 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1209 // is always hidden.
1210 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001211 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001212 } else if (Arch == Triple::x86_64) {
1213 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1214 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001215 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001216 DataRefImpl RelPrev = Rel;
1217 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001218 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001219
1220 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1221
Owen Anderson1832f4d2011-10-26 20:42:54 +00001222 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001223 }
1224 }
1225
1226 return object_error::success;
1227}
1228
David Meyer5c2b4ea2012-03-01 01:36:50 +00001229error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1230 LibraryRef &Res) const {
1231 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1232}
1233
1234error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1235 StringRef &Res) const {
1236 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1237}
1238
1239
Eric Christopher6256b032011-04-22 03:19:48 +00001240/*===-- Miscellaneous -----------------------------------------------------===*/
1241
1242uint8_t MachOObjectFile::getBytesInAddress() const {
1243 return MachOObj->is64Bit() ? 8 : 4;
1244}
1245
1246StringRef MachOObjectFile::getFileFormatName() const {
1247 if (!MachOObj->is64Bit()) {
1248 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001249 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001250 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001251 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001252 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001253 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001254 return "Mach-O 32-bit ppc";
1255 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001256 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001257 "64-bit object file when we're not 64-bit?");
1258 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001259 }
1260 }
1261
Eric Christopherb3e6b042013-02-28 20:26:17 +00001262 // Make sure the cpu type has the correct mask.
1263 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1264 == llvm::MachO::CPUArchABI64 &&
1265 "32-bit object file when we're 64-bit?");
1266
Eric Christopher6256b032011-04-22 03:19:48 +00001267 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001268 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001269 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001270 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001271 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001272 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001273 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001274 }
1275}
1276
1277unsigned MachOObjectFile::getArch() const {
1278 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001279 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001280 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001281 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001282 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001283 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001284 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001285 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001286 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001287 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001288 return Triple::ppc64;
1289 default:
1290 return Triple::UnknownArch;
1291 }
1292}
1293
Owen Andersonf7c93a32011-10-11 17:32:27 +00001294} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001295} // end namespace llvm