blob: 86677231c4e6d09ee500dbc591d3e3bfda1732fd [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
Rafael Espindola0be4eaf2013-04-07 15:46:05 +000044bool MachOObjectFile::is64Bit() const {
45 return MachOObj->is64Bit();
46}
Benjamin Kramer0fcab072011-09-08 20:52:17 +000047
Eric Christopher6256b032011-04-22 03:19:48 +000048ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000049 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000050 std::string Err;
51 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
52 if (!MachOObj)
53 return NULL;
Jim Grosbach596e4742012-11-29 19:14:11 +000054 // MachOObject takes ownership of the Buffer we passed to it, and
55 // MachOObjectFile does, too, so we need to make sure they don't get the
56 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
57 // not a copy of the memory itself), so just make a new copy here for
58 // the MachOObjectFile.
59 MemoryBuffer *NewBuffer =
Benjamin Kramerf56c3e22012-11-29 20:08:03 +000060 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
61 Buffer->getBufferIdentifier(), false);
Jim Grosbach596e4742012-11-29 19:14:11 +000062 return new MachOObjectFile(NewBuffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000063}
64
65/*===-- Symbols -----------------------------------------------------------===*/
66
Rafael Espindola82a21072013-04-06 03:31:08 +000067const MachOFormat::SymtabLoadCommand *
68MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
69 StringRef Data = MachOObj->getData(LCI.Offset,
70 sizeof(MachOFormat::SymtabLoadCommand));
71 return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
72}
73
Rafael Espindola68d287d2013-04-06 03:50:05 +000074const MachOFormat::SegmentLoadCommand *
75MachOObjectFile::getSegmentLoadCommand(LoadCommandInfo LCI) const {
76 StringRef Data = MachOObj->getData(LCI.Offset,
77 sizeof(MachOFormat::SegmentLoadCommand));
78 return reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Data.data());
79}
80
Rafael Espindola196abbf2013-04-07 14:40:18 +000081const MachOFormat::LinkeditDataLoadCommand *
82MachOObjectFile::getLinkeditDataLoadCommand(LoadCommandInfo LCI) const {
83 StringRef Data = MachOObj->getData(LCI.Offset,
84 sizeof(MachOFormat::LinkeditDataLoadCommand));
85 return
86 reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Data.data());
87}
88
Rafael Espindola68d287d2013-04-06 03:50:05 +000089const MachOFormat::Segment64LoadCommand *
90MachOObjectFile::getSegment64LoadCommand(LoadCommandInfo LCI) const {
91 StringRef Data = MachOObj->getData(LCI.Offset,
92 sizeof(MachOFormat::Segment64LoadCommand));
93 return
94 reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Data.data());
95}
96
Eric Christopher6256b032011-04-22 03:19:48 +000097void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
98 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
99 while (DRI.d.a < LoadCommandCount) {
100 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
101 if (LCI.Command.Type == macho::LCT_Symtab) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000102 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
103 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000104 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
105 return;
106 }
107
108 DRI.d.a++;
109 DRI.d.b = 0;
110 }
111}
112
Rafael Espindola00555c12013-04-06 01:59:05 +0000113const MachOFormat::SymbolTableEntry *
114MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000115 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000116 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
117 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000118
Rafael Espindola82a21072013-04-06 03:31:08 +0000119 return getSymbolTableEntry(DRI, SymtabLoadCmd);
120}
Eric Christopher6256b032011-04-22 03:19:48 +0000121
Rafael Espindola82a21072013-04-06 03:31:08 +0000122const MachOFormat::SymbolTableEntry *
123MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
124 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola00555c12013-04-06 01:59:05 +0000125 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
126 unsigned Index = DRI.d.b;
127 uint64_t Offset = (SymbolTableOffset +
128 Index * sizeof(macho::SymbolTableEntry));
129 StringRef Data = MachOObj->getData(Offset,
130 sizeof(MachOFormat::SymbolTableEntry));
131 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000132}
133
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000134const MachOFormat::Symbol64TableEntry*
135MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000136 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000137 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
138 getSymtabLoadCommand(LCI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000139
Rafael Espindola82a21072013-04-06 03:31:08 +0000140 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
141}
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000142
Rafael Espindola82a21072013-04-06 03:31:08 +0000143const MachOFormat::Symbol64TableEntry*
144MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
145 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000146 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
147 unsigned Index = DRI.d.b;
148 uint64_t Offset = (SymbolTableOffset +
149 Index * sizeof(macho::Symbol64TableEntry));
150 StringRef Data = MachOObj->getData(Offset,
151 sizeof(MachOFormat::Symbol64TableEntry));
152 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000153}
154
Michael J. Spencer25b15772011-06-25 17:55:23 +0000155error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
156 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000157 DRI.d.b++;
158 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000159 Result = SymbolRef(DRI, this);
160 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000161}
162
Michael J. Spencer25b15772011-06-25 17:55:23 +0000163error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
164 StringRef &Result) const {
Rafael Espindola82a21072013-04-06 03:31:08 +0000165 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
166 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
167 getSymtabLoadCommand(LCI);
168
169 StringRef StringTable =
170 MachOObj->getData(SymtabLoadCmd->StringTableOffset,
171 SymtabLoadCmd->StringTableSize);
172
173 uint32_t StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000174 if (MachOObj->is64Bit()) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000175 const MachOFormat::Symbol64TableEntry *Entry =
176 getSymbol64TableEntry(DRI, SymtabLoadCmd);
177 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000178 } else {
Rafael Espindola82a21072013-04-06 03:31:08 +0000179 const MachOFormat::SymbolTableEntry *Entry =
180 getSymbolTableEntry(DRI, SymtabLoadCmd);
181 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000182 }
Rafael Espindola82a21072013-04-06 03:31:08 +0000183
184 const char *Start = &StringTable.data()[StringIndex];
185 Result = StringRef(Start);
186
Michael J. Spencer25b15772011-06-25 17:55:23 +0000187 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000188}
189
Danil Malyshevb0436a72011-11-29 17:40:10 +0000190error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
191 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000192 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000193 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000194 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000195 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000196 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000197 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000198 Result += Section->Offset - Section->Address;
199 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000200 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000201 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000202 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000203 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000204 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000205 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000206 Result += Section->Offset - Section->Address;
207 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000208 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000209
Michael J. Spencer25b15772011-06-25 17:55:23 +0000210 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000211}
212
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000213error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
214 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000215 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000216 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000217 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000218 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000219 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000220 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000221 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000222 return object_error::success;
223}
224
Michael J. Spencer25b15772011-06-25 17:55:23 +0000225error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
226 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000227 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
228 uint64_t BeginOffset;
229 uint64_t EndOffset = 0;
230 uint8_t SectionIndex;
231 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000232 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000233 BeginOffset = Entry->Value;
234 SectionIndex = Entry->SectionIndex;
235 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000236 uint32_t flags = SymbolRef::SF_None;
237 getSymbolFlags(DRI, flags);
238 if (flags & SymbolRef::SF_Common)
239 Result = Entry->Value;
240 else
241 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000242 return object_error::success;
243 }
244 // Unfortunately symbols are unsorted so we need to touch all
245 // symbols from load command
246 DRI.d.b = 0;
247 uint32_t Command = DRI.d.a;
248 while (Command == DRI.d.a) {
249 moveToNextSymbol(DRI);
250 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000251 Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000252 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
253 if (!EndOffset || Entry->Value < EndOffset)
254 EndOffset = Entry->Value;
255 }
256 DRI.d.b++;
257 }
258 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000259 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000260 BeginOffset = Entry->Value;
261 SectionIndex = Entry->SectionIndex;
262 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000263 uint32_t flags = SymbolRef::SF_None;
264 getSymbolFlags(DRI, flags);
265 if (flags & SymbolRef::SF_Common)
266 Result = Entry->Value;
267 else
268 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000269 return object_error::success;
270 }
271 // Unfortunately symbols are unsorted so we need to touch all
272 // symbols from load command
273 DRI.d.b = 0;
274 uint32_t Command = DRI.d.a;
275 while (Command == DRI.d.a) {
276 moveToNextSymbol(DRI);
277 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000278 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000279 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
280 if (!EndOffset || Entry->Value < EndOffset)
281 EndOffset = Entry->Value;
282 }
283 DRI.d.b++;
284 }
285 }
286 if (!EndOffset) {
287 uint64_t Size;
288 getSectionSize(Sections[SectionIndex-1], Size);
289 getSectionAddress(Sections[SectionIndex-1], EndOffset);
290 EndOffset += Size;
291 }
292 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000293 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000294}
295
Michael J. Spencer25b15772011-06-25 17:55:23 +0000296error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
297 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000298 uint8_t Type, Flags;
299 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000300 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000301 Type = Entry->Type;
302 Flags = Entry->Flags;
303 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000304 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000305 Type = Entry->Type;
306 Flags = Entry->Flags;
307 }
Eric Christopher6256b032011-04-22 03:19:48 +0000308
309 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000310 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000311 case macho::STT_Undefined:
312 Char = 'u';
313 break;
314 case macho::STT_Absolute:
315 case macho::STT_Section:
316 Char = 's';
317 break;
318 default:
319 Char = '?';
320 break;
321 }
322
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000323 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000324 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000325 Result = Char;
326 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000327}
328
David Meyerc46255a2012-02-28 23:47:53 +0000329error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
330 uint32_t &Result) const {
331 uint16_t MachOFlags;
332 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000333 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000334 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000335 MachOFlags = Entry->Flags;
336 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000337 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000338 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000339 MachOFlags = Entry->Flags;
340 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000341 }
342
Preston Gurdc68dda82012-04-12 20:13:57 +0000343 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000344 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000345
346 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
347 Result |= SymbolRef::SF_Undefined;
348
David Meyerc46255a2012-02-28 23:47:53 +0000349 if (MachOFlags & macho::STF_StabsEntryMask)
350 Result |= SymbolRef::SF_FormatSpecific;
351
Preston Gurdc68dda82012-04-12 20:13:57 +0000352 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000353 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000354 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
355 Result |= SymbolRef::SF_Common;
356 }
David Meyerc46255a2012-02-28 23:47:53 +0000357
358 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
359 Result |= SymbolRef::SF_Weak;
360
361 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
362 Result |= SymbolRef::SF_Absolute;
363
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000364 return object_error::success;
365}
366
367error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
368 section_iterator &Res) const {
369 uint8_t index;
370 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000371 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000372 index = Entry->SectionIndex;
373 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000374 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000375 index = Entry->SectionIndex;
376 }
377
378 if (index == 0)
379 Res = end_sections();
380 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000381 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000382
383 return object_error::success;
384}
385
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000386error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000387 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000388 uint8_t n_type;
389 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000390 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000391 n_type = Entry->Type;
392 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000393 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000394 n_type = Entry->Type;
395 }
396 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000397
398 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000399 if (n_type & MachO::NlistMaskStab) {
400 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000401 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000402 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000403
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000404 switch (n_type & MachO::NlistMaskType) {
405 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000406 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000407 break;
408 case MachO::NListTypeSection :
409 Res = SymbolRef::ST_Function;
410 break;
411 }
412 return object_error::success;
413}
414
Tim Northovera41dce32012-10-29 10:47:00 +0000415error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
416 uint64_t &Val) const {
417 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
418}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000419
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000420symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000421 // DRI.d.a = segment number; DRI.d.b = symbol index.
422 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000423 moveToNextSymbol(DRI);
424 return symbol_iterator(SymbolRef(DRI, this));
425}
426
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000427symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000428 DataRefImpl DRI;
429 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000430 return symbol_iterator(SymbolRef(DRI, this));
431}
432
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000433symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
434 // TODO: implement
435 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
436}
437
438symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
439 // TODO: implement
440 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
441}
Eric Christopher6256b032011-04-22 03:19:48 +0000442
David Meyer5c2b4ea2012-03-01 01:36:50 +0000443library_iterator MachOObjectFile::begin_libraries_needed() const {
444 // TODO: implement
445 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
446}
447
448library_iterator MachOObjectFile::end_libraries_needed() const {
449 // TODO: implement
450 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
451}
452
David Meyer97f77872012-03-01 22:19:54 +0000453StringRef MachOObjectFile::getLoadName() const {
454 // TODO: Implement
455 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
456}
457
Eric Christopher6256b032011-04-22 03:19:48 +0000458/*===-- Sections ----------------------------------------------------------===*/
459
460void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
461 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
462 while (DRI.d.a < LoadCommandCount) {
463 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
464 if (LCI.Command.Type == macho::LCT_Segment) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000465 const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
466 getSegmentLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000467 if (DRI.d.b < SegmentLoadCmd->NumSections)
468 return;
469 } else if (LCI.Command.Type == macho::LCT_Segment64) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000470 const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
471 getSegment64LoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000472 if (DRI.d.b < Segment64LoadCmd->NumSections)
473 return;
474 }
475
476 DRI.d.a++;
477 DRI.d.b = 0;
478 }
479}
480
Michael J. Spencer25b15772011-06-25 17:55:23 +0000481error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
482 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000483 DRI.d.b++;
484 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000485 Result = SectionRef(DRI, this);
486 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000487}
488
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000489static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
Eric Christopher6256b032011-04-22 03:19:48 +0000490 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000491 if (LCI.Command.Type == macho::LCT_Segment64)
492 return true;
493 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
494 return false;
495}
496
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000497const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000498 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
499 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
500 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000501 DRI.d.b * sizeof(MachOFormat::Section);
502 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
503 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000504}
505
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000506std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
507 SectionList::const_iterator loc =
508 std::find(Sections.begin(), Sections.end(), Sec);
509 assert(loc != Sections.end() && "Sec is not a valid section!");
510 return std::distance(Sections.begin(), loc);
511}
512
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000513const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000514MachOObjectFile::getSection64(DataRefImpl DRI) const {
515 assert(is64BitLoadCommand(MachOObj.get(), DRI));
Benjamin Kramer7d145782011-07-15 00:14:48 +0000516 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000517 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000518 DRI.d.b * sizeof(MachOFormat::Section64);
519 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
520 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000521}
522
Rafael Espindolacef81b32012-12-21 03:47:03 +0000523static StringRef parseSegmentOrSectionName(const char *P) {
524 if (P[15] == 0)
525 // Null terminated.
526 return P;
527 // Not null terminated, so this is a 16 char string.
528 return StringRef(P, 16);
529}
530
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000531ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000532 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000533 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000534 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000535 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000536 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000537 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000538 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000539}
540
541error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
542 StringRef &Result) const {
543 ArrayRef<char> Raw = getSectionRawName(DRI);
544 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000545 return object_error::success;
546}
547
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000548ArrayRef<char>
549MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000550 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000551 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000552 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000553 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000554 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000555 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000556 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000557}
558
559StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
560 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
561 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000562}
563
Michael J. Spencer25b15772011-06-25 17:55:23 +0000564error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
565 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000566 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000567 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000568 Result = Sect->Address;
569 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000570 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000571 Result = Sect->Address;
572 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000573 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000574}
575
Michael J. Spencer25b15772011-06-25 17:55:23 +0000576error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
577 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000578 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000579 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000580 Result = Sect->Size;
581 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000582 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000583 Result = Sect->Size;
584 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000585 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000586}
587
Michael J. Spencer25b15772011-06-25 17:55:23 +0000588error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
589 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000590 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000591 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000592 Result = MachOObj->getData(Sect->Offset, Sect->Size);
593 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000594 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000595 Result = MachOObj->getData(Sect->Offset, Sect->Size);
596 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000597 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000598}
599
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000600error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
601 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000602 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000603 const MachOFormat::Section64 *Sect = getSection64(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 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000606 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000607 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000608 }
609 return object_error::success;
610}
611
Michael J. Spencer25b15772011-06-25 17:55:23 +0000612error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
613 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000614 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000615 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000616 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000617 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000618 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000619 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000620 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000621 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000622}
623
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000624error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
625 bool &Result) const {
626 // FIXME: Unimplemented.
627 Result = false;
628 return object_error::success;
629}
630
631error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
632 bool &Result) const {
633 // FIXME: Unimplemented.
634 Result = false;
635 return object_error::success;
636}
637
Preston Gurdc68dda82012-04-12 20:13:57 +0000638error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
639 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000640 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000641 Result = true;
642 return object_error::success;
643}
644
645error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000646 bool &Result) const {
647 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000648 Result = false;
649 return object_error::success;
650}
651
652error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
653 bool &Result) const {
654 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000655 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000656 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
657 Result = (SectionType == MachO::SectionTypeZeroFill ||
658 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000659 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000660 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000661 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
662 Result = (SectionType == MachO::SectionTypeZeroFill ||
663 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000664 }
665
666 return object_error::success;
667}
668
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000669error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
670 bool &Result) const {
671 // Consider using the code from isSectionText to look for __const sections.
672 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
673 // to use section attributes to distinguish code from data.
674
675 // FIXME: Unimplemented.
676 Result = false;
677 return object_error::success;
678}
679
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000680error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
681 DataRefImpl Symb,
682 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000683 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000684 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000685 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000686 Result = false;
687 return object_error::success;
688 }
689
690 uint64_t SectBegin, SectEnd;
691 getSectionAddress(Sec, SectBegin);
692 getSectionSize(Sec, SectEnd);
693 SectEnd += SectBegin;
694
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000695 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000696 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000697 uint64_t SymAddr= Entry->Value;
698 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000699 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000700 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000701 uint64_t SymAddr= Entry->Value;
702 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000703 }
Owen Andersoncd749882011-10-12 22:21:32 +0000704
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000705 return object_error::success;
706}
707
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000708relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
709 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000710 ret.d.b = getSectionIndex(Sec);
711 return relocation_iterator(RelocationRef(ret, this));
712}
713relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
714 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000715 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000716 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000717 last_reloc = Sect->NumRelocationTableEntries;
718 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000719 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000720 last_reloc = Sect->NumRelocationTableEntries;
721 }
722 DataRefImpl ret;
723 ret.d.a = last_reloc;
724 ret.d.b = getSectionIndex(Sec);
725 return relocation_iterator(RelocationRef(ret, this));
726}
727
728section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000729 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000730 moveToNextSection(DRI);
731 return section_iterator(SectionRef(DRI, this));
732}
733
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000734section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000735 DataRefImpl DRI;
736 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000737 return section_iterator(SectionRef(DRI, this));
738}
739
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000740/*===-- Relocations -------------------------------------------------------===*/
741
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000742const MachOFormat::RelocationEntry *
743MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000744 uint32_t relOffset;
745 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000746 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000747 relOffset = Sect->RelocationTableOffset;
748 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000749 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000750 relOffset = Sect->RelocationTableOffset;
751 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000752 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
753 StringRef Data =
754 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
755 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000756}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000757
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000758error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
759 RelocationRef &Res) const {
760 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000761 Res = RelocationRef(Rel, this);
762 return object_error::success;
763}
764error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
765 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000766 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000767 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000768 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000769 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000770 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000771 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000772 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000773 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000774 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000775
776 unsigned Arch = getArch();
777 bool isScattered = (Arch != Triple::x86_64) &&
778 (RE->Word0 & macho::RF_Scattered);
779 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000780 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000781 RelAddr = RE->Word0 & 0xFFFFFF;
782 else
783 RelAddr = RE->Word0;
784
785 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000786 return object_error::success;
787}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000788error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
789 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000790 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000791
792 unsigned Arch = getArch();
793 bool isScattered = (Arch != Triple::x86_64) &&
794 (RE->Word0 & macho::RF_Scattered);
795 if (isScattered)
796 Res = RE->Word0 & 0xFFFFFF;
797 else
798 Res = RE->Word0;
799 return object_error::success;
800}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000801error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
802 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000803 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000804 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
805 bool isExtern = (RE->Word1 >> 27) & 1;
806
807 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000808 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000809 if (isExtern) {
810 for (unsigned i = 0; i < SymbolIdx; i++) {
811 Sym.d.b++;
812 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000813 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000814 "Relocation symbol index out of range!");
815 }
816 }
817 Res = SymbolRef(Sym, this);
818 return object_error::success;
819}
820error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000821 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000822 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000823 Res = RE->Word0;
824 Res <<= 32;
825 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000826 return object_error::success;
827}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000828error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
829 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000830 // TODO: Support scattered relocations.
831 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000832 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000833
834 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000835 bool isScattered = (Arch != Triple::x86_64) &&
836 (RE->Word0 & macho::RF_Scattered);
837
838 unsigned r_type;
839 if (isScattered)
840 r_type = (RE->Word0 >> 24) & 0xF;
841 else
842 r_type = (RE->Word1 >> 28) & 0xF;
843
Owen Anderson0135fe12011-10-24 21:44:00 +0000844 switch (Arch) {
845 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000846 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000847 "GENERIC_RELOC_VANILLA",
848 "GENERIC_RELOC_PAIR",
849 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000850 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000851 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000852 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000853
Owen Andersoneb6bd332011-10-27 20:46:09 +0000854 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000855 res = "Unknown";
856 else
857 res = Table[r_type];
858 break;
859 }
860 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000861 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000862 "X86_64_RELOC_UNSIGNED",
863 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000864 "X86_64_RELOC_BRANCH",
865 "X86_64_RELOC_GOT_LOAD",
866 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000867 "X86_64_RELOC_SUBTRACTOR",
868 "X86_64_RELOC_SIGNED_1",
869 "X86_64_RELOC_SIGNED_2",
870 "X86_64_RELOC_SIGNED_4",
871 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000872
Owen Andersond8fa76d2011-10-24 23:20:07 +0000873 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000874 res = "Unknown";
875 else
876 res = Table[r_type];
877 break;
878 }
879 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000880 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000881 "ARM_RELOC_VANILLA",
882 "ARM_RELOC_PAIR",
883 "ARM_RELOC_SECTDIFF",
884 "ARM_RELOC_LOCAL_SECTDIFF",
885 "ARM_RELOC_PB_LA_PTR",
886 "ARM_RELOC_BR24",
887 "ARM_THUMB_RELOC_BR22",
888 "ARM_THUMB_32BIT_BRANCH",
889 "ARM_RELOC_HALF",
890 "ARM_RELOC_HALF_SECTDIFF" };
891
892 if (r_type > 9)
893 res = "Unknown";
894 else
895 res = Table[r_type];
896 break;
897 }
898 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000899 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000900 "PPC_RELOC_VANILLA",
901 "PPC_RELOC_PAIR",
902 "PPC_RELOC_BR14",
903 "PPC_RELOC_BR24",
904 "PPC_RELOC_HI16",
905 "PPC_RELOC_LO16",
906 "PPC_RELOC_HA16",
907 "PPC_RELOC_LO14",
908 "PPC_RELOC_SECTDIFF",
909 "PPC_RELOC_PB_LA_PTR",
910 "PPC_RELOC_HI16_SECTDIFF",
911 "PPC_RELOC_LO16_SECTDIFF",
912 "PPC_RELOC_HA16_SECTDIFF",
913 "PPC_RELOC_JBSR",
914 "PPC_RELOC_LO14_SECTDIFF",
915 "PPC_RELOC_LOCAL_SECTDIFF" };
916
917 res = Table[r_type];
918 break;
919 }
920 case Triple::UnknownArch:
921 res = "Unknown";
922 break;
923 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000924 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000925 return object_error::success;
926}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000927error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
928 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000929 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000930 bool isExtern = (RE->Word1 >> 27) & 1;
931 Res = 0;
932 if (!isExtern) {
933 const uint8_t* sectAddress = base();
934 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000935 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000936 sectAddress += Sect->Offset;
937 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000938 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000939 sectAddress += Sect->Offset;
940 }
941 Res = reinterpret_cast<uintptr_t>(sectAddress);
942 }
943 return object_error::success;
944}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000945
946// Helper to advance a section or symbol iterator multiple increments at a time.
947template<class T>
948error_code advance(T &it, size_t Val) {
949 error_code ec;
950 while (Val--) {
951 it.increment(ec);
952 }
953 return ec;
954}
955
956template<class T>
957void advanceTo(T &it, size_t Val) {
958 if (error_code ec = advance(it, Val))
959 report_fatal_error(ec.message());
960}
961
Owen Anderson1832f4d2011-10-26 20:42:54 +0000962void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000963 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000964 raw_string_ostream &fmt) const {
965 unsigned Arch = getArch();
966 bool isScattered = (Arch != Triple::x86_64) &&
967 (RE->Word0 & macho::RF_Scattered);
968
969 // Target of a scattered relocation is an address. In the interest of
970 // generating pretty output, scan through the symbol table looking for a
971 // symbol that aligns with that address. If we find one, print it.
972 // Otherwise, we just print the hex address of the target.
973 if (isScattered) {
974 uint32_t Val = RE->Word1;
975
976 error_code ec;
977 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
978 SI.increment(ec)) {
979 if (ec) report_fatal_error(ec.message());
980
981 uint64_t Addr;
982 StringRef Name;
983
984 if ((ec = SI->getAddress(Addr)))
985 report_fatal_error(ec.message());
986 if (Addr != Val) continue;
987 if ((ec = SI->getName(Name)))
988 report_fatal_error(ec.message());
989 fmt << Name;
990 return;
991 }
992
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000993 // If we couldn't find a symbol that this relocation refers to, try
994 // to find a section beginning instead.
995 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
996 SI.increment(ec)) {
997 if (ec) report_fatal_error(ec.message());
998
999 uint64_t Addr;
1000 StringRef Name;
1001
1002 if ((ec = SI->getAddress(Addr)))
1003 report_fatal_error(ec.message());
1004 if (Addr != Val) continue;
1005 if ((ec = SI->getName(Name)))
1006 report_fatal_error(ec.message());
1007 fmt << Name;
1008 return;
1009 }
1010
Owen Anderson1832f4d2011-10-26 20:42:54 +00001011 fmt << format("0x%x", Val);
1012 return;
1013 }
1014
1015 StringRef S;
1016 bool isExtern = (RE->Word1 >> 27) & 1;
1017 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001018
1019 if (isExtern) {
1020 symbol_iterator SI = begin_symbols();
1021 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001022 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001023 } else {
1024 section_iterator SI = begin_sections();
1025 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001026 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001027 }
1028
Owen Anderson1832f4d2011-10-26 20:42:54 +00001029 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001030}
1031
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001032error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1033 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001034 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +00001035
Owen Anderson1832f4d2011-10-26 20:42:54 +00001036 unsigned Arch = getArch();
1037 bool isScattered = (Arch != Triple::x86_64) &&
1038 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001039
Owen Anderson013d7562011-10-25 18:48:41 +00001040 std::string fmtbuf;
1041 raw_string_ostream fmt(fmtbuf);
1042
Owen Anderson1832f4d2011-10-26 20:42:54 +00001043 unsigned Type;
1044 if (isScattered)
1045 Type = (RE->Word0 >> 24) & 0xF;
1046 else
1047 Type = (RE->Word1 >> 28) & 0xF;
1048
Owen Andersoneb6bd332011-10-27 20:46:09 +00001049 bool isPCRel;
1050 if (isScattered)
1051 isPCRel = ((RE->Word0 >> 30) & 1);
1052 else
1053 isPCRel = ((RE->Word1 >> 24) & 1);
1054
Owen Andersond8fa76d2011-10-24 23:20:07 +00001055 // Determine any addends that should be displayed with the relocation.
1056 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001057
1058 // X86_64 has entirely custom relocation types.
1059 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001060 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001061
Owen Andersond8fa76d2011-10-24 23:20:07 +00001062 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001063 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1064 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1065 printRelocationTargetName(RE, fmt);
1066 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001067 if (isPCRel) fmt << "PCREL";
1068 break;
1069 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001070 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001071 DataRefImpl RelNext = Rel;
1072 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001073 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001074
1075 // X86_64_SUBTRACTOR must be followed by a relocation of type
1076 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001077 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001078 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001079 if (RType != 0)
1080 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1081 "X86_64_RELOC_SUBTRACTOR.");
1082
Owen Andersonef22f782011-10-26 17:28:49 +00001083 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1084 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001085 printRelocationTargetName(RENext, fmt);
1086 fmt << "-";
1087 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001088 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001089 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001090 case macho::RIT_X86_64_TLV:
1091 printRelocationTargetName(RE, fmt);
1092 fmt << "@TLV";
1093 if (isPCRel) fmt << "P";
1094 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001095 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1096 printRelocationTargetName(RE, fmt);
1097 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001098 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001099 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1100 printRelocationTargetName(RE, fmt);
1101 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001102 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001103 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1104 printRelocationTargetName(RE, fmt);
1105 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001106 break;
1107 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001108 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001109 break;
1110 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001111 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001112 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1113 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001114 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001115 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001116 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001117 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001118 DataRefImpl RelNext = Rel;
1119 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001120 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001121
1122 // X86 sect diff's must be followed by a relocation of type
1123 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001124 bool isNextScattered = (Arch != Triple::x86_64) &&
1125 (RENext->Word0 & macho::RF_Scattered);
1126 unsigned RType;
1127 if (isNextScattered)
1128 RType = (RENext->Word0 >> 24) & 0xF;
1129 else
1130 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001131 if (RType != 1)
1132 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001133 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001134
Owen Anderson1832f4d2011-10-26 20:42:54 +00001135 printRelocationTargetName(RE, fmt);
1136 fmt << "-";
1137 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001138 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001139 }
1140 }
Owen Anderson013d7562011-10-25 18:48:41 +00001141
Owen Anderson1832f4d2011-10-26 20:42:54 +00001142 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001143 // All X86 relocations that need special printing were already
1144 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001145 switch (Type) {
1146 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001147 DataRefImpl RelNext = Rel;
1148 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001149 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001150
1151 // X86 sect diff's must be followed by a relocation of type
1152 // GENERIC_RELOC_PAIR.
1153 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 if (RType != 1)
1161 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1162 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1163
1164 printRelocationTargetName(RE, fmt);
1165 fmt << "-";
1166 printRelocationTargetName(RENext, fmt);
1167 break;
1168 }
1169 case macho::RIT_Generic_TLV: {
1170 printRelocationTargetName(RE, fmt);
1171 fmt << "@TLV";
1172 if (isPCRel) fmt << "P";
1173 break;
1174 }
1175 default:
1176 printRelocationTargetName(RE, fmt);
1177 }
Owen Anderson013d7562011-10-25 18:48:41 +00001178 } else { // ARM-specific relocations
1179 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001180 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1181 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001182 // Half relocations steal a bit from the length field to encode
1183 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001184 bool isUpper;
1185 if (isScattered)
1186 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001187 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001188 isUpper = (RE->Word1 >> 25) & 1;
1189
1190 if (isUpper)
1191 fmt << ":upper16:(";
1192 else
1193 fmt << ":lower16:(";
1194 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001195
Owen Anderson013d7562011-10-25 18:48:41 +00001196 DataRefImpl RelNext = Rel;
1197 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001198 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001199
1200 // ARM half relocs must be followed by a relocation of type
1201 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001202 bool isNextScattered = (Arch != Triple::x86_64) &&
1203 (RENext->Word0 & macho::RF_Scattered);
1204 unsigned RType;
1205 if (isNextScattered)
1206 RType = (RENext->Word0 >> 24) & 0xF;
1207 else
1208 RType = (RENext->Word1 >> 28) & 0xF;
1209
Owen Anderson013d7562011-10-25 18:48:41 +00001210 if (RType != 1)
1211 report_fatal_error("Expected ARM_RELOC_PAIR after "
1212 "GENERIC_RELOC_HALF");
1213
Owen Anderson1832f4d2011-10-26 20:42:54 +00001214 // NOTE: The half of the target virtual address is stashed in the
1215 // address field of the secondary relocation, but we can't reverse
1216 // engineer the constant offset from it without decoding the movw/movt
1217 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001218
1219 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1220 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001221 if (Type == macho::RIT_ARM_HalfDifference) {
1222 fmt << "-";
1223 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001224 }
1225
Owen Anderson013d7562011-10-25 18:48:41 +00001226 fmt << ")";
1227 break;
1228 }
1229 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001230 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001231 }
1232 }
1233 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001234 } else
1235 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001236
Owen Anderson0135fe12011-10-24 21:44:00 +00001237 fmt.flush();
1238 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001239 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001240}
1241
Owen Anderson0685e942011-10-25 20:35:53 +00001242error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1243 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001244 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001245
Owen Anderson0685e942011-10-25 20:35:53 +00001246 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001247 bool isScattered = (Arch != Triple::x86_64) &&
1248 (RE->Word0 & macho::RF_Scattered);
1249 unsigned Type;
1250 if (isScattered)
1251 Type = (RE->Word0 >> 24) & 0xF;
1252 else
1253 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001254
1255 Result = false;
1256
1257 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1258 // is always hidden.
1259 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001260 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001261 } else if (Arch == Triple::x86_64) {
1262 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1263 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001264 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001265 DataRefImpl RelPrev = Rel;
1266 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001267 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001268
1269 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1270
Owen Anderson1832f4d2011-10-26 20:42:54 +00001271 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001272 }
1273 }
1274
1275 return object_error::success;
1276}
1277
David Meyer5c2b4ea2012-03-01 01:36:50 +00001278error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1279 LibraryRef &Res) const {
1280 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1281}
1282
1283error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1284 StringRef &Res) const {
1285 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1286}
1287
1288
Eric Christopher6256b032011-04-22 03:19:48 +00001289/*===-- Miscellaneous -----------------------------------------------------===*/
1290
1291uint8_t MachOObjectFile::getBytesInAddress() const {
1292 return MachOObj->is64Bit() ? 8 : 4;
1293}
1294
1295StringRef MachOObjectFile::getFileFormatName() const {
1296 if (!MachOObj->is64Bit()) {
1297 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001298 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001299 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001300 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001301 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001302 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001303 return "Mach-O 32-bit ppc";
1304 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001305 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001306 "64-bit object file when we're not 64-bit?");
1307 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001308 }
1309 }
1310
Eric Christopherb3e6b042013-02-28 20:26:17 +00001311 // Make sure the cpu type has the correct mask.
1312 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1313 == llvm::MachO::CPUArchABI64 &&
1314 "32-bit object file when we're 64-bit?");
1315
Eric Christopher6256b032011-04-22 03:19:48 +00001316 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001317 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001318 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001319 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001320 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001321 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001322 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001323 }
1324}
1325
1326unsigned MachOObjectFile::getArch() const {
1327 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001328 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001329 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001330 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001331 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001332 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001333 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001334 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001335 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001336 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001337 return Triple::ppc64;
1338 default:
1339 return Triple::UnknownArch;
1340 }
1341}
1342
Owen Andersonf7c93a32011-10-11 17:32:27 +00001343} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001344} // end namespace llvm