blob: 94827693e9983e083479e6ce4c2397b61951e0d2 [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),
Rafael Espindola82a21072013-04-06 03:31:08 +000033 MachOObj(MOO) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +000034 DataRefImpl DRI;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000035 moveToNextSection(DRI);
36 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
37 while (DRI.d.a < LoadCommandCount) {
38 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000039 DRI.d.b++;
40 moveToNextSection(DRI);
41 }
42}
43
44
Eric Christopher6256b032011-04-22 03:19:48 +000045ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000046 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000047 std::string Err;
48 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
49 if (!MachOObj)
50 return NULL;
Jim Grosbach596e4742012-11-29 19:14:11 +000051 // MachOObject takes ownership of the Buffer we passed to it, and
52 // MachOObjectFile does, too, so we need to make sure they don't get the
53 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
54 // not a copy of the memory itself), so just make a new copy here for
55 // the MachOObjectFile.
56 MemoryBuffer *NewBuffer =
Benjamin Kramerf56c3e22012-11-29 20:08:03 +000057 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
58 Buffer->getBufferIdentifier(), false);
Jim Grosbach596e4742012-11-29 19:14:11 +000059 return new MachOObjectFile(NewBuffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000060}
61
62/*===-- Symbols -----------------------------------------------------------===*/
63
Rafael Espindola82a21072013-04-06 03:31:08 +000064const MachOFormat::SymtabLoadCommand *
65MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
66 StringRef Data = MachOObj->getData(LCI.Offset,
67 sizeof(MachOFormat::SymtabLoadCommand));
68 return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
69}
70
Rafael Espindola68d287d2013-04-06 03:50:05 +000071const MachOFormat::SegmentLoadCommand *
72MachOObjectFile::getSegmentLoadCommand(LoadCommandInfo LCI) const {
73 StringRef Data = MachOObj->getData(LCI.Offset,
74 sizeof(MachOFormat::SegmentLoadCommand));
75 return reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Data.data());
76}
77
Rafael Espindola196abbf2013-04-07 14:40:18 +000078const MachOFormat::LinkeditDataLoadCommand *
79MachOObjectFile::getLinkeditDataLoadCommand(LoadCommandInfo LCI) const {
80 StringRef Data = MachOObj->getData(LCI.Offset,
81 sizeof(MachOFormat::LinkeditDataLoadCommand));
82 return
83 reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Data.data());
84}
85
Rafael Espindola68d287d2013-04-06 03:50:05 +000086const MachOFormat::Segment64LoadCommand *
87MachOObjectFile::getSegment64LoadCommand(LoadCommandInfo LCI) const {
88 StringRef Data = MachOObj->getData(LCI.Offset,
89 sizeof(MachOFormat::Segment64LoadCommand));
90 return
91 reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Data.data());
92}
93
Eric Christopher6256b032011-04-22 03:19:48 +000094void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
95 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
96 while (DRI.d.a < LoadCommandCount) {
97 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
98 if (LCI.Command.Type == macho::LCT_Symtab) {
Rafael Espindola82a21072013-04-06 03:31:08 +000099 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
100 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000101 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
102 return;
103 }
104
105 DRI.d.a++;
106 DRI.d.b = 0;
107 }
108}
109
Rafael Espindola00555c12013-04-06 01:59:05 +0000110const MachOFormat::SymbolTableEntry *
111MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000112 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000113 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
114 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000115
Rafael Espindola82a21072013-04-06 03:31:08 +0000116 return getSymbolTableEntry(DRI, SymtabLoadCmd);
117}
Eric Christopher6256b032011-04-22 03:19:48 +0000118
Rafael Espindola82a21072013-04-06 03:31:08 +0000119const MachOFormat::SymbolTableEntry *
120MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
121 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola00555c12013-04-06 01:59:05 +0000122 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
123 unsigned Index = DRI.d.b;
124 uint64_t Offset = (SymbolTableOffset +
125 Index * sizeof(macho::SymbolTableEntry));
126 StringRef Data = MachOObj->getData(Offset,
127 sizeof(MachOFormat::SymbolTableEntry));
128 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000129}
130
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000131const MachOFormat::Symbol64TableEntry*
132MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000133 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000134 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
135 getSymtabLoadCommand(LCI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000136
Rafael Espindola82a21072013-04-06 03:31:08 +0000137 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
138}
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000139
Rafael Espindola82a21072013-04-06 03:31:08 +0000140const MachOFormat::Symbol64TableEntry*
141MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
142 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000143 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
144 unsigned Index = DRI.d.b;
145 uint64_t Offset = (SymbolTableOffset +
146 Index * sizeof(macho::Symbol64TableEntry));
147 StringRef Data = MachOObj->getData(Offset,
148 sizeof(MachOFormat::Symbol64TableEntry));
149 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000150}
151
Michael J. Spencer25b15772011-06-25 17:55:23 +0000152error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
153 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000154 DRI.d.b++;
155 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000156 Result = SymbolRef(DRI, this);
157 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000158}
159
Michael J. Spencer25b15772011-06-25 17:55:23 +0000160error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
161 StringRef &Result) const {
Rafael Espindola82a21072013-04-06 03:31:08 +0000162 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
163 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
164 getSymtabLoadCommand(LCI);
165
166 StringRef StringTable =
167 MachOObj->getData(SymtabLoadCmd->StringTableOffset,
168 SymtabLoadCmd->StringTableSize);
169
170 uint32_t StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000171 if (MachOObj->is64Bit()) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000172 const MachOFormat::Symbol64TableEntry *Entry =
173 getSymbol64TableEntry(DRI, SymtabLoadCmd);
174 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000175 } else {
Rafael Espindola82a21072013-04-06 03:31:08 +0000176 const MachOFormat::SymbolTableEntry *Entry =
177 getSymbolTableEntry(DRI, SymtabLoadCmd);
178 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000179 }
Rafael Espindola82a21072013-04-06 03:31:08 +0000180
181 const char *Start = &StringTable.data()[StringIndex];
182 Result = StringRef(Start);
183
Michael J. Spencer25b15772011-06-25 17:55:23 +0000184 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000185}
186
Danil Malyshevb0436a72011-11-29 17:40:10 +0000187error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
188 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000189 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000190 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000191 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000192 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000193 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000194 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000195 Result += Section->Offset - Section->Address;
196 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000197 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000198 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000199 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000200 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000201 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000202 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000203 Result += Section->Offset - Section->Address;
204 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000205 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000206
Michael J. Spencer25b15772011-06-25 17:55:23 +0000207 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000208}
209
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000210error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
211 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000212 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000213 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000214 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000215 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000216 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000217 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000218 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000219 return object_error::success;
220}
221
Michael J. Spencer25b15772011-06-25 17:55:23 +0000222error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
223 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000224 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
225 uint64_t BeginOffset;
226 uint64_t EndOffset = 0;
227 uint8_t SectionIndex;
228 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000229 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000230 BeginOffset = Entry->Value;
231 SectionIndex = Entry->SectionIndex;
232 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000233 uint32_t flags = SymbolRef::SF_None;
234 getSymbolFlags(DRI, flags);
235 if (flags & SymbolRef::SF_Common)
236 Result = Entry->Value;
237 else
238 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000239 return object_error::success;
240 }
241 // Unfortunately symbols are unsorted so we need to touch all
242 // symbols from load command
243 DRI.d.b = 0;
244 uint32_t Command = DRI.d.a;
245 while (Command == DRI.d.a) {
246 moveToNextSymbol(DRI);
247 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000248 Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000249 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
250 if (!EndOffset || Entry->Value < EndOffset)
251 EndOffset = Entry->Value;
252 }
253 DRI.d.b++;
254 }
255 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000256 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000257 BeginOffset = Entry->Value;
258 SectionIndex = Entry->SectionIndex;
259 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000260 uint32_t flags = SymbolRef::SF_None;
261 getSymbolFlags(DRI, flags);
262 if (flags & SymbolRef::SF_Common)
263 Result = Entry->Value;
264 else
265 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000266 return object_error::success;
267 }
268 // Unfortunately symbols are unsorted so we need to touch all
269 // symbols from load command
270 DRI.d.b = 0;
271 uint32_t Command = DRI.d.a;
272 while (Command == DRI.d.a) {
273 moveToNextSymbol(DRI);
274 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000275 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000276 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
277 if (!EndOffset || Entry->Value < EndOffset)
278 EndOffset = Entry->Value;
279 }
280 DRI.d.b++;
281 }
282 }
283 if (!EndOffset) {
284 uint64_t Size;
285 getSectionSize(Sections[SectionIndex-1], Size);
286 getSectionAddress(Sections[SectionIndex-1], EndOffset);
287 EndOffset += Size;
288 }
289 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000290 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000291}
292
Michael J. Spencer25b15772011-06-25 17:55:23 +0000293error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
294 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000295 uint8_t Type, Flags;
296 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000297 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000298 Type = Entry->Type;
299 Flags = Entry->Flags;
300 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000301 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000302 Type = Entry->Type;
303 Flags = Entry->Flags;
304 }
Eric Christopher6256b032011-04-22 03:19:48 +0000305
306 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000307 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000308 case macho::STT_Undefined:
309 Char = 'u';
310 break;
311 case macho::STT_Absolute:
312 case macho::STT_Section:
313 Char = 's';
314 break;
315 default:
316 Char = '?';
317 break;
318 }
319
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000320 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000321 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000322 Result = Char;
323 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000324}
325
David Meyerc46255a2012-02-28 23:47:53 +0000326error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
327 uint32_t &Result) const {
328 uint16_t MachOFlags;
329 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000330 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000331 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000332 MachOFlags = Entry->Flags;
333 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000334 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000335 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000336 MachOFlags = Entry->Flags;
337 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000338 }
339
Preston Gurdc68dda82012-04-12 20:13:57 +0000340 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000341 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000342
343 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
344 Result |= SymbolRef::SF_Undefined;
345
David Meyerc46255a2012-02-28 23:47:53 +0000346 if (MachOFlags & macho::STF_StabsEntryMask)
347 Result |= SymbolRef::SF_FormatSpecific;
348
Preston Gurdc68dda82012-04-12 20:13:57 +0000349 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000350 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000351 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
352 Result |= SymbolRef::SF_Common;
353 }
David Meyerc46255a2012-02-28 23:47:53 +0000354
355 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
356 Result |= SymbolRef::SF_Weak;
357
358 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
359 Result |= SymbolRef::SF_Absolute;
360
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000361 return object_error::success;
362}
363
364error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
365 section_iterator &Res) const {
366 uint8_t index;
367 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000368 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000369 index = Entry->SectionIndex;
370 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000371 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000372 index = Entry->SectionIndex;
373 }
374
375 if (index == 0)
376 Res = end_sections();
377 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000378 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000379
380 return object_error::success;
381}
382
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000383error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000384 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000385 uint8_t n_type;
386 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000387 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000388 n_type = Entry->Type;
389 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000390 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000391 n_type = Entry->Type;
392 }
393 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000394
395 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000396 if (n_type & MachO::NlistMaskStab) {
397 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000398 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000399 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000400
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000401 switch (n_type & MachO::NlistMaskType) {
402 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000403 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000404 break;
405 case MachO::NListTypeSection :
406 Res = SymbolRef::ST_Function;
407 break;
408 }
409 return object_error::success;
410}
411
Tim Northovera41dce32012-10-29 10:47:00 +0000412error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
413 uint64_t &Val) const {
414 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
415}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000416
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000417symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000418 // DRI.d.a = segment number; DRI.d.b = symbol index.
419 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000420 moveToNextSymbol(DRI);
421 return symbol_iterator(SymbolRef(DRI, this));
422}
423
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000424symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000425 DataRefImpl DRI;
426 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000427 return symbol_iterator(SymbolRef(DRI, this));
428}
429
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000430symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
431 // TODO: implement
432 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
433}
434
435symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
436 // TODO: implement
437 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
438}
Eric Christopher6256b032011-04-22 03:19:48 +0000439
David Meyer5c2b4ea2012-03-01 01:36:50 +0000440library_iterator MachOObjectFile::begin_libraries_needed() const {
441 // TODO: implement
442 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
443}
444
445library_iterator MachOObjectFile::end_libraries_needed() const {
446 // TODO: implement
447 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
448}
449
David Meyer97f77872012-03-01 22:19:54 +0000450StringRef MachOObjectFile::getLoadName() const {
451 // TODO: Implement
452 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
453}
454
Eric Christopher6256b032011-04-22 03:19:48 +0000455/*===-- Sections ----------------------------------------------------------===*/
456
457void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
458 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
459 while (DRI.d.a < LoadCommandCount) {
460 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
461 if (LCI.Command.Type == macho::LCT_Segment) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000462 const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
463 getSegmentLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000464 if (DRI.d.b < SegmentLoadCmd->NumSections)
465 return;
466 } else if (LCI.Command.Type == macho::LCT_Segment64) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000467 const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
468 getSegment64LoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000469 if (DRI.d.b < Segment64LoadCmd->NumSections)
470 return;
471 }
472
473 DRI.d.a++;
474 DRI.d.b = 0;
475 }
476}
477
Michael J. Spencer25b15772011-06-25 17:55:23 +0000478error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
479 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000480 DRI.d.b++;
481 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000482 Result = SectionRef(DRI, this);
483 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000484}
485
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000486static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
Eric Christopher6256b032011-04-22 03:19:48 +0000487 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000488 if (LCI.Command.Type == macho::LCT_Segment64)
489 return true;
490 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
491 return false;
492}
493
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000494const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000495 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
496 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
497 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000498 DRI.d.b * sizeof(MachOFormat::Section);
499 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
500 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000501}
502
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000503std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
504 SectionList::const_iterator loc =
505 std::find(Sections.begin(), Sections.end(), Sec);
506 assert(loc != Sections.end() && "Sec is not a valid section!");
507 return std::distance(Sections.begin(), loc);
508}
509
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000510const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000511MachOObjectFile::getSection64(DataRefImpl DRI) const {
512 assert(is64BitLoadCommand(MachOObj.get(), DRI));
Benjamin Kramer7d145782011-07-15 00:14:48 +0000513 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000514 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000515 DRI.d.b * sizeof(MachOFormat::Section64);
516 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
517 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000518}
519
Rafael Espindolacef81b32012-12-21 03:47:03 +0000520static StringRef parseSegmentOrSectionName(const char *P) {
521 if (P[15] == 0)
522 // Null terminated.
523 return P;
524 // Not null terminated, so this is a 16 char string.
525 return StringRef(P, 16);
526}
527
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000528ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) 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 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000531 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000532 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000533 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000534 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000535 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000536}
537
538error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
539 StringRef &Result) const {
540 ArrayRef<char> Raw = getSectionRawName(DRI);
541 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000542 return object_error::success;
543}
544
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000545ArrayRef<char>
546MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000547 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000548 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000549 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000550 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000551 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000552 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000553 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000554}
555
556StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
557 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
558 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000559}
560
Michael J. Spencer25b15772011-06-25 17:55:23 +0000561error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
562 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000563 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000564 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000565 Result = Sect->Address;
566 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000567 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000568 Result = Sect->Address;
569 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000570 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000571}
572
Michael J. Spencer25b15772011-06-25 17:55:23 +0000573error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
574 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000575 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000576 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000577 Result = Sect->Size;
578 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000579 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000580 Result = Sect->Size;
581 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000582 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000583}
584
Michael J. Spencer25b15772011-06-25 17:55:23 +0000585error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
586 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000587 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000588 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000589 Result = MachOObj->getData(Sect->Offset, Sect->Size);
590 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000591 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000592 Result = MachOObj->getData(Sect->Offset, Sect->Size);
593 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000594 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000595}
596
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000597error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
598 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000599 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000600 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000601 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000602 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000603 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000604 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000605 }
606 return object_error::success;
607}
608
Michael J. Spencer25b15772011-06-25 17:55:23 +0000609error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
610 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000611 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000612 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000613 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000614 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000615 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000616 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000617 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000618 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000619}
620
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000621error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
622 bool &Result) const {
623 // FIXME: Unimplemented.
624 Result = false;
625 return object_error::success;
626}
627
628error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
629 bool &Result) const {
630 // FIXME: Unimplemented.
631 Result = false;
632 return object_error::success;
633}
634
Preston Gurdc68dda82012-04-12 20:13:57 +0000635error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
636 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000637 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000638 Result = true;
639 return object_error::success;
640}
641
642error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000643 bool &Result) const {
644 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000645 Result = false;
646 return object_error::success;
647}
648
649error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
650 bool &Result) const {
651 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000652 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000653 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
654 Result = (SectionType == MachO::SectionTypeZeroFill ||
655 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000656 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000657 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000658 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
659 Result = (SectionType == MachO::SectionTypeZeroFill ||
660 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000661 }
662
663 return object_error::success;
664}
665
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000666error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
667 bool &Result) const {
668 // Consider using the code from isSectionText to look for __const sections.
669 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
670 // to use section attributes to distinguish code from data.
671
672 // FIXME: Unimplemented.
673 Result = false;
674 return object_error::success;
675}
676
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000677error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
678 DataRefImpl Symb,
679 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000680 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000681 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000682 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000683 Result = false;
684 return object_error::success;
685 }
686
687 uint64_t SectBegin, SectEnd;
688 getSectionAddress(Sec, SectBegin);
689 getSectionSize(Sec, SectEnd);
690 SectEnd += SectBegin;
691
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000692 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000693 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000694 uint64_t SymAddr= Entry->Value;
695 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000696 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000697 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000698 uint64_t SymAddr= Entry->Value;
699 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000700 }
Owen Andersoncd749882011-10-12 22:21:32 +0000701
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000702 return object_error::success;
703}
704
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000705relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
706 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000707 ret.d.b = getSectionIndex(Sec);
708 return relocation_iterator(RelocationRef(ret, this));
709}
710relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
711 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000712 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000713 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000714 last_reloc = Sect->NumRelocationTableEntries;
715 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000716 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000717 last_reloc = Sect->NumRelocationTableEntries;
718 }
719 DataRefImpl ret;
720 ret.d.a = last_reloc;
721 ret.d.b = getSectionIndex(Sec);
722 return relocation_iterator(RelocationRef(ret, this));
723}
724
725section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000726 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000727 moveToNextSection(DRI);
728 return section_iterator(SectionRef(DRI, this));
729}
730
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000731section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000732 DataRefImpl DRI;
733 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000734 return section_iterator(SectionRef(DRI, this));
735}
736
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000737/*===-- Relocations -------------------------------------------------------===*/
738
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000739const MachOFormat::RelocationEntry *
740MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000741 uint32_t relOffset;
742 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000743 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000744 relOffset = Sect->RelocationTableOffset;
745 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000746 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000747 relOffset = Sect->RelocationTableOffset;
748 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000749 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
750 StringRef Data =
751 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
752 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000753}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000754
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000755error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
756 RelocationRef &Res) const {
757 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000758 Res = RelocationRef(Rel, this);
759 return object_error::success;
760}
761error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
762 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000763 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000764 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000765 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000766 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000767 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000768 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000769 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000770 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000771 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000772
773 unsigned Arch = getArch();
774 bool isScattered = (Arch != Triple::x86_64) &&
775 (RE->Word0 & macho::RF_Scattered);
776 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000777 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000778 RelAddr = RE->Word0 & 0xFFFFFF;
779 else
780 RelAddr = RE->Word0;
781
782 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000783 return object_error::success;
784}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000785error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
786 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000787 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000788
789 unsigned Arch = getArch();
790 bool isScattered = (Arch != Triple::x86_64) &&
791 (RE->Word0 & macho::RF_Scattered);
792 if (isScattered)
793 Res = RE->Word0 & 0xFFFFFF;
794 else
795 Res = RE->Word0;
796 return object_error::success;
797}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000798error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
799 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000800 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000801 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
802 bool isExtern = (RE->Word1 >> 27) & 1;
803
804 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000805 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000806 if (isExtern) {
807 for (unsigned i = 0; i < SymbolIdx; i++) {
808 Sym.d.b++;
809 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000810 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000811 "Relocation symbol index out of range!");
812 }
813 }
814 Res = SymbolRef(Sym, this);
815 return object_error::success;
816}
817error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000818 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000819 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000820 Res = RE->Word0;
821 Res <<= 32;
822 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000823 return object_error::success;
824}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000825error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
826 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000827 // TODO: Support scattered relocations.
828 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000829 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000830
831 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000832 bool isScattered = (Arch != Triple::x86_64) &&
833 (RE->Word0 & macho::RF_Scattered);
834
835 unsigned r_type;
836 if (isScattered)
837 r_type = (RE->Word0 >> 24) & 0xF;
838 else
839 r_type = (RE->Word1 >> 28) & 0xF;
840
Owen Anderson0135fe12011-10-24 21:44:00 +0000841 switch (Arch) {
842 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000843 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000844 "GENERIC_RELOC_VANILLA",
845 "GENERIC_RELOC_PAIR",
846 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000847 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000848 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000849 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000850
Owen Andersoneb6bd332011-10-27 20:46:09 +0000851 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000852 res = "Unknown";
853 else
854 res = Table[r_type];
855 break;
856 }
857 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000858 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000859 "X86_64_RELOC_UNSIGNED",
860 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000861 "X86_64_RELOC_BRANCH",
862 "X86_64_RELOC_GOT_LOAD",
863 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000864 "X86_64_RELOC_SUBTRACTOR",
865 "X86_64_RELOC_SIGNED_1",
866 "X86_64_RELOC_SIGNED_2",
867 "X86_64_RELOC_SIGNED_4",
868 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000869
Owen Andersond8fa76d2011-10-24 23:20:07 +0000870 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000871 res = "Unknown";
872 else
873 res = Table[r_type];
874 break;
875 }
876 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000877 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000878 "ARM_RELOC_VANILLA",
879 "ARM_RELOC_PAIR",
880 "ARM_RELOC_SECTDIFF",
881 "ARM_RELOC_LOCAL_SECTDIFF",
882 "ARM_RELOC_PB_LA_PTR",
883 "ARM_RELOC_BR24",
884 "ARM_THUMB_RELOC_BR22",
885 "ARM_THUMB_32BIT_BRANCH",
886 "ARM_RELOC_HALF",
887 "ARM_RELOC_HALF_SECTDIFF" };
888
889 if (r_type > 9)
890 res = "Unknown";
891 else
892 res = Table[r_type];
893 break;
894 }
895 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000896 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000897 "PPC_RELOC_VANILLA",
898 "PPC_RELOC_PAIR",
899 "PPC_RELOC_BR14",
900 "PPC_RELOC_BR24",
901 "PPC_RELOC_HI16",
902 "PPC_RELOC_LO16",
903 "PPC_RELOC_HA16",
904 "PPC_RELOC_LO14",
905 "PPC_RELOC_SECTDIFF",
906 "PPC_RELOC_PB_LA_PTR",
907 "PPC_RELOC_HI16_SECTDIFF",
908 "PPC_RELOC_LO16_SECTDIFF",
909 "PPC_RELOC_HA16_SECTDIFF",
910 "PPC_RELOC_JBSR",
911 "PPC_RELOC_LO14_SECTDIFF",
912 "PPC_RELOC_LOCAL_SECTDIFF" };
913
914 res = Table[r_type];
915 break;
916 }
917 case Triple::UnknownArch:
918 res = "Unknown";
919 break;
920 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000921 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000922 return object_error::success;
923}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000924error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
925 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000926 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000927 bool isExtern = (RE->Word1 >> 27) & 1;
928 Res = 0;
929 if (!isExtern) {
930 const uint8_t* sectAddress = base();
931 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000932 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000933 sectAddress += Sect->Offset;
934 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000935 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000936 sectAddress += Sect->Offset;
937 }
938 Res = reinterpret_cast<uintptr_t>(sectAddress);
939 }
940 return object_error::success;
941}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000942
943// Helper to advance a section or symbol iterator multiple increments at a time.
944template<class T>
945error_code advance(T &it, size_t Val) {
946 error_code ec;
947 while (Val--) {
948 it.increment(ec);
949 }
950 return ec;
951}
952
953template<class T>
954void advanceTo(T &it, size_t Val) {
955 if (error_code ec = advance(it, Val))
956 report_fatal_error(ec.message());
957}
958
Owen Anderson1832f4d2011-10-26 20:42:54 +0000959void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000960 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000961 raw_string_ostream &fmt) const {
962 unsigned Arch = getArch();
963 bool isScattered = (Arch != Triple::x86_64) &&
964 (RE->Word0 & macho::RF_Scattered);
965
966 // Target of a scattered relocation is an address. In the interest of
967 // generating pretty output, scan through the symbol table looking for a
968 // symbol that aligns with that address. If we find one, print it.
969 // Otherwise, we just print the hex address of the target.
970 if (isScattered) {
971 uint32_t Val = RE->Word1;
972
973 error_code ec;
974 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
975 SI.increment(ec)) {
976 if (ec) report_fatal_error(ec.message());
977
978 uint64_t Addr;
979 StringRef Name;
980
981 if ((ec = SI->getAddress(Addr)))
982 report_fatal_error(ec.message());
983 if (Addr != Val) continue;
984 if ((ec = SI->getName(Name)))
985 report_fatal_error(ec.message());
986 fmt << Name;
987 return;
988 }
989
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000990 // If we couldn't find a symbol that this relocation refers to, try
991 // to find a section beginning instead.
992 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
993 SI.increment(ec)) {
994 if (ec) report_fatal_error(ec.message());
995
996 uint64_t Addr;
997 StringRef Name;
998
999 if ((ec = SI->getAddress(Addr)))
1000 report_fatal_error(ec.message());
1001 if (Addr != Val) continue;
1002 if ((ec = SI->getName(Name)))
1003 report_fatal_error(ec.message());
1004 fmt << Name;
1005 return;
1006 }
1007
Owen Anderson1832f4d2011-10-26 20:42:54 +00001008 fmt << format("0x%x", Val);
1009 return;
1010 }
1011
1012 StringRef S;
1013 bool isExtern = (RE->Word1 >> 27) & 1;
1014 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001015
1016 if (isExtern) {
1017 symbol_iterator SI = begin_symbols();
1018 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001019 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001020 } else {
1021 section_iterator SI = begin_sections();
1022 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001023 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001024 }
1025
Owen Anderson1832f4d2011-10-26 20:42:54 +00001026 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001027}
1028
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001029error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1030 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001031 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +00001032
Owen Anderson1832f4d2011-10-26 20:42:54 +00001033 unsigned Arch = getArch();
1034 bool isScattered = (Arch != Triple::x86_64) &&
1035 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001036
Owen Anderson013d7562011-10-25 18:48:41 +00001037 std::string fmtbuf;
1038 raw_string_ostream fmt(fmtbuf);
1039
Owen Anderson1832f4d2011-10-26 20:42:54 +00001040 unsigned Type;
1041 if (isScattered)
1042 Type = (RE->Word0 >> 24) & 0xF;
1043 else
1044 Type = (RE->Word1 >> 28) & 0xF;
1045
Owen Andersoneb6bd332011-10-27 20:46:09 +00001046 bool isPCRel;
1047 if (isScattered)
1048 isPCRel = ((RE->Word0 >> 30) & 1);
1049 else
1050 isPCRel = ((RE->Word1 >> 24) & 1);
1051
Owen Andersond8fa76d2011-10-24 23:20:07 +00001052 // Determine any addends that should be displayed with the relocation.
1053 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001054
1055 // X86_64 has entirely custom relocation types.
1056 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001057 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001058
Owen Andersond8fa76d2011-10-24 23:20:07 +00001059 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001060 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1061 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1062 printRelocationTargetName(RE, fmt);
1063 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001064 if (isPCRel) fmt << "PCREL";
1065 break;
1066 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001067 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001068 DataRefImpl RelNext = Rel;
1069 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001070 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001071
1072 // X86_64_SUBTRACTOR must be followed by a relocation of type
1073 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001074 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001075 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001076 if (RType != 0)
1077 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1078 "X86_64_RELOC_SUBTRACTOR.");
1079
Owen Andersonef22f782011-10-26 17:28:49 +00001080 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1081 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001082 printRelocationTargetName(RENext, fmt);
1083 fmt << "-";
1084 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001085 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001086 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001087 case macho::RIT_X86_64_TLV:
1088 printRelocationTargetName(RE, fmt);
1089 fmt << "@TLV";
1090 if (isPCRel) fmt << "P";
1091 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001092 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1093 printRelocationTargetName(RE, fmt);
1094 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001095 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001096 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1097 printRelocationTargetName(RE, fmt);
1098 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001099 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001100 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1101 printRelocationTargetName(RE, fmt);
1102 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001103 break;
1104 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001105 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001106 break;
1107 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001108 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001109 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1110 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001111 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001112 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001113 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001114 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001115 DataRefImpl RelNext = Rel;
1116 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001117 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001118
1119 // X86 sect diff's must be followed by a relocation of type
1120 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001121 bool isNextScattered = (Arch != Triple::x86_64) &&
1122 (RENext->Word0 & macho::RF_Scattered);
1123 unsigned RType;
1124 if (isNextScattered)
1125 RType = (RENext->Word0 >> 24) & 0xF;
1126 else
1127 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001128 if (RType != 1)
1129 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001130 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001131
Owen Anderson1832f4d2011-10-26 20:42:54 +00001132 printRelocationTargetName(RE, fmt);
1133 fmt << "-";
1134 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001135 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001136 }
1137 }
Owen Anderson013d7562011-10-25 18:48:41 +00001138
Owen Anderson1832f4d2011-10-26 20:42:54 +00001139 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001140 // All X86 relocations that need special printing were already
1141 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001142 switch (Type) {
1143 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001144 DataRefImpl RelNext = Rel;
1145 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001146 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001147
1148 // X86 sect diff's must be followed by a relocation of type
1149 // GENERIC_RELOC_PAIR.
1150 bool isNextScattered = (Arch != Triple::x86_64) &&
1151 (RENext->Word0 & macho::RF_Scattered);
1152 unsigned RType;
1153 if (isNextScattered)
1154 RType = (RENext->Word0 >> 24) & 0xF;
1155 else
1156 RType = (RENext->Word1 >> 28) & 0xF;
1157 if (RType != 1)
1158 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1159 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1160
1161 printRelocationTargetName(RE, fmt);
1162 fmt << "-";
1163 printRelocationTargetName(RENext, fmt);
1164 break;
1165 }
1166 case macho::RIT_Generic_TLV: {
1167 printRelocationTargetName(RE, fmt);
1168 fmt << "@TLV";
1169 if (isPCRel) fmt << "P";
1170 break;
1171 }
1172 default:
1173 printRelocationTargetName(RE, fmt);
1174 }
Owen Anderson013d7562011-10-25 18:48:41 +00001175 } else { // ARM-specific relocations
1176 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001177 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1178 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001179 // Half relocations steal a bit from the length field to encode
1180 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001181 bool isUpper;
1182 if (isScattered)
1183 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001184 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001185 isUpper = (RE->Word1 >> 25) & 1;
1186
1187 if (isUpper)
1188 fmt << ":upper16:(";
1189 else
1190 fmt << ":lower16:(";
1191 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001192
Owen Anderson013d7562011-10-25 18:48:41 +00001193 DataRefImpl RelNext = Rel;
1194 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001195 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001196
1197 // ARM half relocs must be followed by a relocation of type
1198 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001199 bool isNextScattered = (Arch != Triple::x86_64) &&
1200 (RENext->Word0 & macho::RF_Scattered);
1201 unsigned RType;
1202 if (isNextScattered)
1203 RType = (RENext->Word0 >> 24) & 0xF;
1204 else
1205 RType = (RENext->Word1 >> 28) & 0xF;
1206
Owen Anderson013d7562011-10-25 18:48:41 +00001207 if (RType != 1)
1208 report_fatal_error("Expected ARM_RELOC_PAIR after "
1209 "GENERIC_RELOC_HALF");
1210
Owen Anderson1832f4d2011-10-26 20:42:54 +00001211 // NOTE: The half of the target virtual address is stashed in the
1212 // address field of the secondary relocation, but we can't reverse
1213 // engineer the constant offset from it without decoding the movw/movt
1214 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001215
1216 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1217 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001218 if (Type == macho::RIT_ARM_HalfDifference) {
1219 fmt << "-";
1220 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001221 }
1222
Owen Anderson013d7562011-10-25 18:48:41 +00001223 fmt << ")";
1224 break;
1225 }
1226 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001227 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001228 }
1229 }
1230 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001231 } else
1232 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001233
Owen Anderson0135fe12011-10-24 21:44:00 +00001234 fmt.flush();
1235 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001236 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001237}
1238
Owen Anderson0685e942011-10-25 20:35:53 +00001239error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1240 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001241 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001242
Owen Anderson0685e942011-10-25 20:35:53 +00001243 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001244 bool isScattered = (Arch != Triple::x86_64) &&
1245 (RE->Word0 & macho::RF_Scattered);
1246 unsigned Type;
1247 if (isScattered)
1248 Type = (RE->Word0 >> 24) & 0xF;
1249 else
1250 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001251
1252 Result = false;
1253
1254 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1255 // is always hidden.
1256 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001257 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001258 } else if (Arch == Triple::x86_64) {
1259 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1260 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001261 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001262 DataRefImpl RelPrev = Rel;
1263 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001264 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001265
1266 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1267
Owen Anderson1832f4d2011-10-26 20:42:54 +00001268 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001269 }
1270 }
1271
1272 return object_error::success;
1273}
1274
David Meyer5c2b4ea2012-03-01 01:36:50 +00001275error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1276 LibraryRef &Res) const {
1277 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1278}
1279
1280error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1281 StringRef &Res) const {
1282 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1283}
1284
1285
Eric Christopher6256b032011-04-22 03:19:48 +00001286/*===-- Miscellaneous -----------------------------------------------------===*/
1287
1288uint8_t MachOObjectFile::getBytesInAddress() const {
1289 return MachOObj->is64Bit() ? 8 : 4;
1290}
1291
1292StringRef MachOObjectFile::getFileFormatName() const {
1293 if (!MachOObj->is64Bit()) {
1294 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001295 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001296 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001297 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001298 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001299 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001300 return "Mach-O 32-bit ppc";
1301 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001302 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001303 "64-bit object file when we're not 64-bit?");
1304 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001305 }
1306 }
1307
Eric Christopherb3e6b042013-02-28 20:26:17 +00001308 // Make sure the cpu type has the correct mask.
1309 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1310 == llvm::MachO::CPUArchABI64 &&
1311 "32-bit object file when we're 64-bit?");
1312
Eric Christopher6256b032011-04-22 03:19:48 +00001313 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001314 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001315 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001316 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001317 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001318 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001319 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001320 }
1321}
1322
1323unsigned MachOObjectFile::getArch() const {
1324 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001325 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001326 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001327 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001328 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001329 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001330 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001331 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001332 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001333 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001334 return Triple::ppc64;
1335 default:
1336 return Triple::UnknownArch;
1337 }
1338}
1339
Owen Andersonf7c93a32011-10-11 17:32:27 +00001340} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001341} // end namespace llvm