blob: c817942ca1a5ab20a44e781517ee51354cc96d32 [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
Rafael Espindola6f1f3392013-04-07 16:58:48 +000030MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, error_code &ec)
31 : ObjectFile(Binary::ID_MachO, Object) {
32 // MachOObject takes ownership of the Buffer we passed to it, and
33 // MachOObjectFile does, too, so we need to make sure they don't get the
34 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
35 // not a copy of the memory itself), so just make a new copy here for
36 // the MachOObjectFile.
37 MemoryBuffer *NewBuffer =
38 MemoryBuffer::getMemBuffer(Object->getBuffer(),
39 Object->getBufferIdentifier(), false);
40 std::string ErrorStr;
41 MachOObj.reset(MachOObject::LoadFromBuffer(NewBuffer, &ErrorStr));
42 if (!MachOObj) {
43 ec = object_error::parse_failed;
44 return;
45 }
46
Benjamin Kramer0fcab072011-09-08 20:52:17 +000047 DataRefImpl DRI;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000048 moveToNextSection(DRI);
49 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
50 while (DRI.d.a < LoadCommandCount) {
51 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000052 DRI.d.b++;
53 moveToNextSection(DRI);
54 }
55}
56
Rafael Espindola0be4eaf2013-04-07 15:46:05 +000057bool MachOObjectFile::is64Bit() const {
58 return MachOObj->is64Bit();
59}
Benjamin Kramer0fcab072011-09-08 20:52:17 +000060
Rafael Espindola3eff3182013-04-07 16:07:35 +000061const LoadCommandInfo &
62MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
63 return MachOObj->getLoadCommandInfo(Index);
64}
65
66void MachOObjectFile::ReadULEB128s(uint64_t Index,
67 SmallVectorImpl<uint64_t> &Out) const {
68 return MachOObj->ReadULEB128s(Index, Out);
69}
70
71const macho::Header &MachOObjectFile::getHeader() const {
72 return MachOObj->getHeader();
73}
74
Eric Christopher6256b032011-04-22 03:19:48 +000075ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000076 error_code ec;
Rafael Espindola6f1f3392013-04-07 16:58:48 +000077 ObjectFile *Ret = new MachOObjectFile(Buffer, ec);
78 if (ec)
Eric Christopher6256b032011-04-22 03:19:48 +000079 return NULL;
Rafael Espindola6f1f3392013-04-07 16:58:48 +000080 return Ret;
Eric Christopher6256b032011-04-22 03:19:48 +000081}
82
83/*===-- Symbols -----------------------------------------------------------===*/
84
Rafael Espindola82a21072013-04-06 03:31:08 +000085const MachOFormat::SymtabLoadCommand *
86MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
87 StringRef Data = MachOObj->getData(LCI.Offset,
88 sizeof(MachOFormat::SymtabLoadCommand));
89 return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
90}
91
Rafael Espindola68d287d2013-04-06 03:50:05 +000092const MachOFormat::SegmentLoadCommand *
93MachOObjectFile::getSegmentLoadCommand(LoadCommandInfo LCI) const {
94 StringRef Data = MachOObj->getData(LCI.Offset,
95 sizeof(MachOFormat::SegmentLoadCommand));
96 return reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Data.data());
97}
98
Rafael Espindola196abbf2013-04-07 14:40:18 +000099const MachOFormat::LinkeditDataLoadCommand *
100MachOObjectFile::getLinkeditDataLoadCommand(LoadCommandInfo LCI) const {
101 StringRef Data = MachOObj->getData(LCI.Offset,
102 sizeof(MachOFormat::LinkeditDataLoadCommand));
103 return
104 reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Data.data());
105}
106
Rafael Espindola68d287d2013-04-06 03:50:05 +0000107const MachOFormat::Segment64LoadCommand *
108MachOObjectFile::getSegment64LoadCommand(LoadCommandInfo LCI) const {
109 StringRef Data = MachOObj->getData(LCI.Offset,
110 sizeof(MachOFormat::Segment64LoadCommand));
111 return
112 reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Data.data());
113}
114
Eric Christopher6256b032011-04-22 03:19:48 +0000115void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
116 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
117 while (DRI.d.a < LoadCommandCount) {
118 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
119 if (LCI.Command.Type == macho::LCT_Symtab) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000120 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
121 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000122 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
123 return;
124 }
125
126 DRI.d.a++;
127 DRI.d.b = 0;
128 }
129}
130
Rafael Espindola00555c12013-04-06 01:59:05 +0000131const MachOFormat::SymbolTableEntry *
132MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000133 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000134 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
135 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000136
Rafael Espindola82a21072013-04-06 03:31:08 +0000137 return getSymbolTableEntry(DRI, SymtabLoadCmd);
138}
Eric Christopher6256b032011-04-22 03:19:48 +0000139
Rafael Espindola82a21072013-04-06 03:31:08 +0000140const MachOFormat::SymbolTableEntry *
141MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
142 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola00555c12013-04-06 01:59:05 +0000143 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
144 unsigned Index = DRI.d.b;
145 uint64_t Offset = (SymbolTableOffset +
146 Index * sizeof(macho::SymbolTableEntry));
147 StringRef Data = MachOObj->getData(Offset,
148 sizeof(MachOFormat::SymbolTableEntry));
149 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000150}
151
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000152const MachOFormat::Symbol64TableEntry*
153MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000154 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000155 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
156 getSymtabLoadCommand(LCI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000157
Rafael Espindola82a21072013-04-06 03:31:08 +0000158 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
159}
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000160
Rafael Espindola82a21072013-04-06 03:31:08 +0000161const MachOFormat::Symbol64TableEntry*
162MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
163 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000164 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
165 unsigned Index = DRI.d.b;
166 uint64_t Offset = (SymbolTableOffset +
167 Index * sizeof(macho::Symbol64TableEntry));
168 StringRef Data = MachOObj->getData(Offset,
169 sizeof(MachOFormat::Symbol64TableEntry));
170 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000171}
172
Michael J. Spencer25b15772011-06-25 17:55:23 +0000173error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
174 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000175 DRI.d.b++;
176 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000177 Result = SymbolRef(DRI, this);
178 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000179}
180
Michael J. Spencer25b15772011-06-25 17:55:23 +0000181error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
182 StringRef &Result) const {
Rafael Espindola82a21072013-04-06 03:31:08 +0000183 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
184 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
185 getSymtabLoadCommand(LCI);
186
187 StringRef StringTable =
188 MachOObj->getData(SymtabLoadCmd->StringTableOffset,
189 SymtabLoadCmd->StringTableSize);
190
191 uint32_t StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000192 if (MachOObj->is64Bit()) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000193 const MachOFormat::Symbol64TableEntry *Entry =
194 getSymbol64TableEntry(DRI, SymtabLoadCmd);
195 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000196 } else {
Rafael Espindola82a21072013-04-06 03:31:08 +0000197 const MachOFormat::SymbolTableEntry *Entry =
198 getSymbolTableEntry(DRI, SymtabLoadCmd);
199 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000200 }
Rafael Espindola82a21072013-04-06 03:31:08 +0000201
202 const char *Start = &StringTable.data()[StringIndex];
203 Result = StringRef(Start);
204
Michael J. Spencer25b15772011-06-25 17:55:23 +0000205 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000206}
207
Danil Malyshevb0436a72011-11-29 17:40:10 +0000208error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
209 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000210 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000211 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000212 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000213 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000214 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000215 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000216 Result += Section->Offset - Section->Address;
217 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000218 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000219 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000220 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000221 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000222 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000223 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000224 Result += Section->Offset - Section->Address;
225 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000226 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000227
Michael J. Spencer25b15772011-06-25 17:55:23 +0000228 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000229}
230
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000231error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
232 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000233 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000234 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000235 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000236 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000237 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000238 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000239 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000240 return object_error::success;
241}
242
Michael J. Spencer25b15772011-06-25 17:55:23 +0000243error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
244 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000245 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
246 uint64_t BeginOffset;
247 uint64_t EndOffset = 0;
248 uint8_t SectionIndex;
249 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000250 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000251 BeginOffset = Entry->Value;
252 SectionIndex = Entry->SectionIndex;
253 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000254 uint32_t flags = SymbolRef::SF_None;
255 getSymbolFlags(DRI, flags);
256 if (flags & SymbolRef::SF_Common)
257 Result = Entry->Value;
258 else
259 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000260 return object_error::success;
261 }
262 // Unfortunately symbols are unsorted so we need to touch all
263 // symbols from load command
264 DRI.d.b = 0;
265 uint32_t Command = DRI.d.a;
266 while (Command == DRI.d.a) {
267 moveToNextSymbol(DRI);
268 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000269 Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000270 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
271 if (!EndOffset || Entry->Value < EndOffset)
272 EndOffset = Entry->Value;
273 }
274 DRI.d.b++;
275 }
276 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000277 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000278 BeginOffset = Entry->Value;
279 SectionIndex = Entry->SectionIndex;
280 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000281 uint32_t flags = SymbolRef::SF_None;
282 getSymbolFlags(DRI, flags);
283 if (flags & SymbolRef::SF_Common)
284 Result = Entry->Value;
285 else
286 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000287 return object_error::success;
288 }
289 // Unfortunately symbols are unsorted so we need to touch all
290 // symbols from load command
291 DRI.d.b = 0;
292 uint32_t Command = DRI.d.a;
293 while (Command == DRI.d.a) {
294 moveToNextSymbol(DRI);
295 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000296 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000297 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
298 if (!EndOffset || Entry->Value < EndOffset)
299 EndOffset = Entry->Value;
300 }
301 DRI.d.b++;
302 }
303 }
304 if (!EndOffset) {
305 uint64_t Size;
306 getSectionSize(Sections[SectionIndex-1], Size);
307 getSectionAddress(Sections[SectionIndex-1], EndOffset);
308 EndOffset += Size;
309 }
310 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000311 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000312}
313
Michael J. Spencer25b15772011-06-25 17:55:23 +0000314error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
315 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000316 uint8_t Type, Flags;
317 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000318 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000319 Type = Entry->Type;
320 Flags = Entry->Flags;
321 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000322 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000323 Type = Entry->Type;
324 Flags = Entry->Flags;
325 }
Eric Christopher6256b032011-04-22 03:19:48 +0000326
327 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000328 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000329 case macho::STT_Undefined:
330 Char = 'u';
331 break;
332 case macho::STT_Absolute:
333 case macho::STT_Section:
334 Char = 's';
335 break;
336 default:
337 Char = '?';
338 break;
339 }
340
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000341 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000342 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000343 Result = Char;
344 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000345}
346
David Meyerc46255a2012-02-28 23:47:53 +0000347error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
348 uint32_t &Result) const {
349 uint16_t MachOFlags;
350 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000351 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000352 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000353 MachOFlags = Entry->Flags;
354 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000355 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000356 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000357 MachOFlags = Entry->Flags;
358 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000359 }
360
Preston Gurdc68dda82012-04-12 20:13:57 +0000361 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000362 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000363
364 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
365 Result |= SymbolRef::SF_Undefined;
366
David Meyerc46255a2012-02-28 23:47:53 +0000367 if (MachOFlags & macho::STF_StabsEntryMask)
368 Result |= SymbolRef::SF_FormatSpecific;
369
Preston Gurdc68dda82012-04-12 20:13:57 +0000370 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000371 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000372 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
373 Result |= SymbolRef::SF_Common;
374 }
David Meyerc46255a2012-02-28 23:47:53 +0000375
376 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
377 Result |= SymbolRef::SF_Weak;
378
379 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
380 Result |= SymbolRef::SF_Absolute;
381
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000382 return object_error::success;
383}
384
385error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
386 section_iterator &Res) const {
387 uint8_t index;
388 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000389 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000390 index = Entry->SectionIndex;
391 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000392 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000393 index = Entry->SectionIndex;
394 }
395
396 if (index == 0)
397 Res = end_sections();
398 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000399 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000400
401 return object_error::success;
402}
403
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000404error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000405 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000406 uint8_t n_type;
407 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000408 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000409 n_type = Entry->Type;
410 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000411 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000412 n_type = Entry->Type;
413 }
414 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000415
416 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000417 if (n_type & MachO::NlistMaskStab) {
418 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000419 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000420 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000421
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000422 switch (n_type & MachO::NlistMaskType) {
423 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000424 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000425 break;
426 case MachO::NListTypeSection :
427 Res = SymbolRef::ST_Function;
428 break;
429 }
430 return object_error::success;
431}
432
Tim Northovera41dce32012-10-29 10:47:00 +0000433error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
434 uint64_t &Val) const {
435 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
436}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000437
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000438symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000439 // DRI.d.a = segment number; DRI.d.b = symbol index.
440 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000441 moveToNextSymbol(DRI);
442 return symbol_iterator(SymbolRef(DRI, this));
443}
444
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000445symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000446 DataRefImpl DRI;
447 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000448 return symbol_iterator(SymbolRef(DRI, this));
449}
450
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000451symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
452 // TODO: implement
453 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
454}
455
456symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
457 // TODO: implement
458 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
459}
Eric Christopher6256b032011-04-22 03:19:48 +0000460
David Meyer5c2b4ea2012-03-01 01:36:50 +0000461library_iterator MachOObjectFile::begin_libraries_needed() const {
462 // TODO: implement
463 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
464}
465
466library_iterator MachOObjectFile::end_libraries_needed() const {
467 // TODO: implement
468 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
469}
470
David Meyer97f77872012-03-01 22:19:54 +0000471StringRef MachOObjectFile::getLoadName() const {
472 // TODO: Implement
473 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
474}
475
Eric Christopher6256b032011-04-22 03:19:48 +0000476/*===-- Sections ----------------------------------------------------------===*/
477
478void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
479 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
480 while (DRI.d.a < LoadCommandCount) {
481 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
482 if (LCI.Command.Type == macho::LCT_Segment) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000483 const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
484 getSegmentLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000485 if (DRI.d.b < SegmentLoadCmd->NumSections)
486 return;
487 } else if (LCI.Command.Type == macho::LCT_Segment64) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000488 const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
489 getSegment64LoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000490 if (DRI.d.b < Segment64LoadCmd->NumSections)
491 return;
492 }
493
494 DRI.d.a++;
495 DRI.d.b = 0;
496 }
497}
498
Michael J. Spencer25b15772011-06-25 17:55:23 +0000499error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
500 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000501 DRI.d.b++;
502 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000503 Result = SectionRef(DRI, this);
504 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000505}
506
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000507static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
Eric Christopher6256b032011-04-22 03:19:48 +0000508 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000509 if (LCI.Command.Type == macho::LCT_Segment64)
510 return true;
511 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
512 return false;
513}
514
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000515const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000516 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
517 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
518 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000519 DRI.d.b * sizeof(MachOFormat::Section);
520 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
521 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000522}
523
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000524std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
525 SectionList::const_iterator loc =
526 std::find(Sections.begin(), Sections.end(), Sec);
527 assert(loc != Sections.end() && "Sec is not a valid section!");
528 return std::distance(Sections.begin(), loc);
529}
530
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000531const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000532MachOObjectFile::getSection64(DataRefImpl DRI) const {
533 assert(is64BitLoadCommand(MachOObj.get(), DRI));
Benjamin Kramer7d145782011-07-15 00:14:48 +0000534 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000535 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000536 DRI.d.b * sizeof(MachOFormat::Section64);
537 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
538 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000539}
540
Rafael Espindolacef81b32012-12-21 03:47:03 +0000541static StringRef parseSegmentOrSectionName(const char *P) {
542 if (P[15] == 0)
543 // Null terminated.
544 return P;
545 // Not null terminated, so this is a 16 char string.
546 return StringRef(P, 16);
547}
548
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000549ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000550 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000551 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000552 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000553 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000554 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000555 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000556 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000557}
558
559error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
560 StringRef &Result) const {
561 ArrayRef<char> Raw = getSectionRawName(DRI);
562 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000563 return object_error::success;
564}
565
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000566ArrayRef<char>
567MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000568 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000569 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000570 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000571 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000572 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000573 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000574 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000575}
576
577StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
578 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
579 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000580}
581
Michael J. Spencer25b15772011-06-25 17:55:23 +0000582error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
583 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000584 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000585 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000586 Result = Sect->Address;
587 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000588 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000589 Result = Sect->Address;
590 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000591 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000592}
593
Michael J. Spencer25b15772011-06-25 17:55:23 +0000594error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
595 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000596 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000597 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000598 Result = Sect->Size;
599 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000600 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000601 Result = Sect->Size;
602 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000603 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000604}
605
Michael J. Spencer25b15772011-06-25 17:55:23 +0000606error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
607 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000608 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000609 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000610 Result = MachOObj->getData(Sect->Offset, Sect->Size);
611 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000612 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000613 Result = MachOObj->getData(Sect->Offset, Sect->Size);
614 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000615 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000616}
617
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000618error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
619 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000620 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000621 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000622 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000623 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000624 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000625 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000626 }
627 return object_error::success;
628}
629
Michael J. Spencer25b15772011-06-25 17:55:23 +0000630error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
631 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000632 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000633 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000634 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000635 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000636 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000637 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000638 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000639 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000640}
641
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000642error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
643 bool &Result) const {
644 // FIXME: Unimplemented.
645 Result = false;
646 return object_error::success;
647}
648
649error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
650 bool &Result) const {
651 // FIXME: Unimplemented.
652 Result = false;
653 return object_error::success;
654}
655
Preston Gurdc68dda82012-04-12 20:13:57 +0000656error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
657 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000658 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000659 Result = true;
660 return object_error::success;
661}
662
663error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000664 bool &Result) const {
665 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000666 Result = false;
667 return object_error::success;
668}
669
670error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
671 bool &Result) const {
672 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000673 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000674 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
675 Result = (SectionType == MachO::SectionTypeZeroFill ||
676 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000677 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000678 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000679 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
680 Result = (SectionType == MachO::SectionTypeZeroFill ||
681 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000682 }
683
684 return object_error::success;
685}
686
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000687error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
688 bool &Result) const {
689 // Consider using the code from isSectionText to look for __const sections.
690 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
691 // to use section attributes to distinguish code from data.
692
693 // FIXME: Unimplemented.
694 Result = false;
695 return object_error::success;
696}
697
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000698error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
699 DataRefImpl Symb,
700 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000701 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000702 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000703 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000704 Result = false;
705 return object_error::success;
706 }
707
708 uint64_t SectBegin, SectEnd;
709 getSectionAddress(Sec, SectBegin);
710 getSectionSize(Sec, SectEnd);
711 SectEnd += SectBegin;
712
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000713 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000714 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000715 uint64_t SymAddr= Entry->Value;
716 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000717 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000718 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000719 uint64_t SymAddr= Entry->Value;
720 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000721 }
Owen Andersoncd749882011-10-12 22:21:32 +0000722
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000723 return object_error::success;
724}
725
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000726relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
727 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000728 ret.d.b = getSectionIndex(Sec);
729 return relocation_iterator(RelocationRef(ret, this));
730}
731relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
732 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000733 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000734 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000735 last_reloc = Sect->NumRelocationTableEntries;
736 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000737 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000738 last_reloc = Sect->NumRelocationTableEntries;
739 }
740 DataRefImpl ret;
741 ret.d.a = last_reloc;
742 ret.d.b = getSectionIndex(Sec);
743 return relocation_iterator(RelocationRef(ret, this));
744}
745
746section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000747 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000748 moveToNextSection(DRI);
749 return section_iterator(SectionRef(DRI, this));
750}
751
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000752section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000753 DataRefImpl DRI;
754 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000755 return section_iterator(SectionRef(DRI, this));
756}
757
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000758/*===-- Relocations -------------------------------------------------------===*/
759
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000760const MachOFormat::RelocationEntry *
761MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000762 uint32_t relOffset;
763 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000764 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000765 relOffset = Sect->RelocationTableOffset;
766 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000767 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000768 relOffset = Sect->RelocationTableOffset;
769 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000770 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
771 StringRef Data =
772 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
773 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000774}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000775
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000776error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
777 RelocationRef &Res) const {
778 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000779 Res = RelocationRef(Rel, this);
780 return object_error::success;
781}
782error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
783 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000784 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000785 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000786 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000787 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000788 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000789 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000790 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000791 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000792 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000793
794 unsigned Arch = getArch();
795 bool isScattered = (Arch != Triple::x86_64) &&
796 (RE->Word0 & macho::RF_Scattered);
797 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000798 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000799 RelAddr = RE->Word0 & 0xFFFFFF;
800 else
801 RelAddr = RE->Word0;
802
803 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000804 return object_error::success;
805}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000806error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
807 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000808 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000809
810 unsigned Arch = getArch();
811 bool isScattered = (Arch != Triple::x86_64) &&
812 (RE->Word0 & macho::RF_Scattered);
813 if (isScattered)
814 Res = RE->Word0 & 0xFFFFFF;
815 else
816 Res = RE->Word0;
817 return object_error::success;
818}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000819error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
820 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000821 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000822 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
823 bool isExtern = (RE->Word1 >> 27) & 1;
824
825 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000826 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000827 if (isExtern) {
828 for (unsigned i = 0; i < SymbolIdx; i++) {
829 Sym.d.b++;
830 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000831 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000832 "Relocation symbol index out of range!");
833 }
834 }
835 Res = SymbolRef(Sym, this);
836 return object_error::success;
837}
838error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000839 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000840 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000841 Res = RE->Word0;
842 Res <<= 32;
843 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000844 return object_error::success;
845}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000846error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
847 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000848 // TODO: Support scattered relocations.
849 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000850 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000851
852 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000853 bool isScattered = (Arch != Triple::x86_64) &&
854 (RE->Word0 & macho::RF_Scattered);
855
856 unsigned r_type;
857 if (isScattered)
858 r_type = (RE->Word0 >> 24) & 0xF;
859 else
860 r_type = (RE->Word1 >> 28) & 0xF;
861
Owen Anderson0135fe12011-10-24 21:44:00 +0000862 switch (Arch) {
863 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000864 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000865 "GENERIC_RELOC_VANILLA",
866 "GENERIC_RELOC_PAIR",
867 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000868 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000869 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000870 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000871
Owen Andersoneb6bd332011-10-27 20:46:09 +0000872 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000873 res = "Unknown";
874 else
875 res = Table[r_type];
876 break;
877 }
878 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000879 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000880 "X86_64_RELOC_UNSIGNED",
881 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000882 "X86_64_RELOC_BRANCH",
883 "X86_64_RELOC_GOT_LOAD",
884 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000885 "X86_64_RELOC_SUBTRACTOR",
886 "X86_64_RELOC_SIGNED_1",
887 "X86_64_RELOC_SIGNED_2",
888 "X86_64_RELOC_SIGNED_4",
889 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000890
Owen Andersond8fa76d2011-10-24 23:20:07 +0000891 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000892 res = "Unknown";
893 else
894 res = Table[r_type];
895 break;
896 }
897 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000898 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000899 "ARM_RELOC_VANILLA",
900 "ARM_RELOC_PAIR",
901 "ARM_RELOC_SECTDIFF",
902 "ARM_RELOC_LOCAL_SECTDIFF",
903 "ARM_RELOC_PB_LA_PTR",
904 "ARM_RELOC_BR24",
905 "ARM_THUMB_RELOC_BR22",
906 "ARM_THUMB_32BIT_BRANCH",
907 "ARM_RELOC_HALF",
908 "ARM_RELOC_HALF_SECTDIFF" };
909
910 if (r_type > 9)
911 res = "Unknown";
912 else
913 res = Table[r_type];
914 break;
915 }
916 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000917 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000918 "PPC_RELOC_VANILLA",
919 "PPC_RELOC_PAIR",
920 "PPC_RELOC_BR14",
921 "PPC_RELOC_BR24",
922 "PPC_RELOC_HI16",
923 "PPC_RELOC_LO16",
924 "PPC_RELOC_HA16",
925 "PPC_RELOC_LO14",
926 "PPC_RELOC_SECTDIFF",
927 "PPC_RELOC_PB_LA_PTR",
928 "PPC_RELOC_HI16_SECTDIFF",
929 "PPC_RELOC_LO16_SECTDIFF",
930 "PPC_RELOC_HA16_SECTDIFF",
931 "PPC_RELOC_JBSR",
932 "PPC_RELOC_LO14_SECTDIFF",
933 "PPC_RELOC_LOCAL_SECTDIFF" };
934
935 res = Table[r_type];
936 break;
937 }
938 case Triple::UnknownArch:
939 res = "Unknown";
940 break;
941 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000942 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000943 return object_error::success;
944}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000945error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
946 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000947 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000948 bool isExtern = (RE->Word1 >> 27) & 1;
949 Res = 0;
950 if (!isExtern) {
951 const uint8_t* sectAddress = base();
952 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000953 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000954 sectAddress += Sect->Offset;
955 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000956 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000957 sectAddress += Sect->Offset;
958 }
959 Res = reinterpret_cast<uintptr_t>(sectAddress);
960 }
961 return object_error::success;
962}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000963
964// Helper to advance a section or symbol iterator multiple increments at a time.
965template<class T>
966error_code advance(T &it, size_t Val) {
967 error_code ec;
968 while (Val--) {
969 it.increment(ec);
970 }
971 return ec;
972}
973
974template<class T>
975void advanceTo(T &it, size_t Val) {
976 if (error_code ec = advance(it, Val))
977 report_fatal_error(ec.message());
978}
979
Owen Anderson1832f4d2011-10-26 20:42:54 +0000980void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000981 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000982 raw_string_ostream &fmt) const {
983 unsigned Arch = getArch();
984 bool isScattered = (Arch != Triple::x86_64) &&
985 (RE->Word0 & macho::RF_Scattered);
986
987 // Target of a scattered relocation is an address. In the interest of
988 // generating pretty output, scan through the symbol table looking for a
989 // symbol that aligns with that address. If we find one, print it.
990 // Otherwise, we just print the hex address of the target.
991 if (isScattered) {
992 uint32_t Val = RE->Word1;
993
994 error_code ec;
995 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); 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 Andersonb28bdbf2011-10-27 21:53:50 +00001011 // If we couldn't find a symbol that this relocation refers to, try
1012 // to find a section beginning instead.
1013 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
1014 SI.increment(ec)) {
1015 if (ec) report_fatal_error(ec.message());
1016
1017 uint64_t Addr;
1018 StringRef Name;
1019
1020 if ((ec = SI->getAddress(Addr)))
1021 report_fatal_error(ec.message());
1022 if (Addr != Val) continue;
1023 if ((ec = SI->getName(Name)))
1024 report_fatal_error(ec.message());
1025 fmt << Name;
1026 return;
1027 }
1028
Owen Anderson1832f4d2011-10-26 20:42:54 +00001029 fmt << format("0x%x", Val);
1030 return;
1031 }
1032
1033 StringRef S;
1034 bool isExtern = (RE->Word1 >> 27) & 1;
1035 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001036
1037 if (isExtern) {
1038 symbol_iterator SI = begin_symbols();
1039 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001040 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001041 } else {
1042 section_iterator SI = begin_sections();
1043 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001044 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001045 }
1046
Owen Anderson1832f4d2011-10-26 20:42:54 +00001047 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001048}
1049
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001050error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1051 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001052 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +00001053
Owen Anderson1832f4d2011-10-26 20:42:54 +00001054 unsigned Arch = getArch();
1055 bool isScattered = (Arch != Triple::x86_64) &&
1056 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001057
Owen Anderson013d7562011-10-25 18:48:41 +00001058 std::string fmtbuf;
1059 raw_string_ostream fmt(fmtbuf);
1060
Owen Anderson1832f4d2011-10-26 20:42:54 +00001061 unsigned Type;
1062 if (isScattered)
1063 Type = (RE->Word0 >> 24) & 0xF;
1064 else
1065 Type = (RE->Word1 >> 28) & 0xF;
1066
Owen Andersoneb6bd332011-10-27 20:46:09 +00001067 bool isPCRel;
1068 if (isScattered)
1069 isPCRel = ((RE->Word0 >> 30) & 1);
1070 else
1071 isPCRel = ((RE->Word1 >> 24) & 1);
1072
Owen Andersond8fa76d2011-10-24 23:20:07 +00001073 // Determine any addends that should be displayed with the relocation.
1074 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001075
1076 // X86_64 has entirely custom relocation types.
1077 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001078 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001079
Owen Andersond8fa76d2011-10-24 23:20:07 +00001080 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001081 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1082 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1083 printRelocationTargetName(RE, fmt);
1084 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001085 if (isPCRel) fmt << "PCREL";
1086 break;
1087 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001088 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001089 DataRefImpl RelNext = Rel;
1090 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001091 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001092
1093 // X86_64_SUBTRACTOR must be followed by a relocation of type
1094 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001095 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001096 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001097 if (RType != 0)
1098 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1099 "X86_64_RELOC_SUBTRACTOR.");
1100
Owen Andersonef22f782011-10-26 17:28:49 +00001101 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1102 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001103 printRelocationTargetName(RENext, fmt);
1104 fmt << "-";
1105 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001106 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001107 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001108 case macho::RIT_X86_64_TLV:
1109 printRelocationTargetName(RE, fmt);
1110 fmt << "@TLV";
1111 if (isPCRel) fmt << "P";
1112 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001113 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1114 printRelocationTargetName(RE, fmt);
1115 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001116 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001117 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1118 printRelocationTargetName(RE, fmt);
1119 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001120 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001121 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1122 printRelocationTargetName(RE, fmt);
1123 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001124 break;
1125 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001126 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001127 break;
1128 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001129 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001130 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1131 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001132 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001133 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001134 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001135 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001136 DataRefImpl RelNext = Rel;
1137 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001138 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001139
1140 // X86 sect diff's must be followed by a relocation of type
1141 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001142 bool isNextScattered = (Arch != Triple::x86_64) &&
1143 (RENext->Word0 & macho::RF_Scattered);
1144 unsigned RType;
1145 if (isNextScattered)
1146 RType = (RENext->Word0 >> 24) & 0xF;
1147 else
1148 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001149 if (RType != 1)
1150 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001151 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001152
Owen Anderson1832f4d2011-10-26 20:42:54 +00001153 printRelocationTargetName(RE, fmt);
1154 fmt << "-";
1155 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001156 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001157 }
1158 }
Owen Anderson013d7562011-10-25 18:48:41 +00001159
Owen Anderson1832f4d2011-10-26 20:42:54 +00001160 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001161 // All X86 relocations that need special printing were already
1162 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001163 switch (Type) {
1164 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001165 DataRefImpl RelNext = Rel;
1166 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001167 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001168
1169 // X86 sect diff's must be followed by a relocation of type
1170 // GENERIC_RELOC_PAIR.
1171 bool isNextScattered = (Arch != Triple::x86_64) &&
1172 (RENext->Word0 & macho::RF_Scattered);
1173 unsigned RType;
1174 if (isNextScattered)
1175 RType = (RENext->Word0 >> 24) & 0xF;
1176 else
1177 RType = (RENext->Word1 >> 28) & 0xF;
1178 if (RType != 1)
1179 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1180 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1181
1182 printRelocationTargetName(RE, fmt);
1183 fmt << "-";
1184 printRelocationTargetName(RENext, fmt);
1185 break;
1186 }
1187 case macho::RIT_Generic_TLV: {
1188 printRelocationTargetName(RE, fmt);
1189 fmt << "@TLV";
1190 if (isPCRel) fmt << "P";
1191 break;
1192 }
1193 default:
1194 printRelocationTargetName(RE, fmt);
1195 }
Owen Anderson013d7562011-10-25 18:48:41 +00001196 } else { // ARM-specific relocations
1197 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001198 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1199 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001200 // Half relocations steal a bit from the length field to encode
1201 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001202 bool isUpper;
1203 if (isScattered)
1204 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001205 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001206 isUpper = (RE->Word1 >> 25) & 1;
1207
1208 if (isUpper)
1209 fmt << ":upper16:(";
1210 else
1211 fmt << ":lower16:(";
1212 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001213
Owen Anderson013d7562011-10-25 18:48:41 +00001214 DataRefImpl RelNext = Rel;
1215 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001216 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001217
1218 // ARM half relocs must be followed by a relocation of type
1219 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001220 bool isNextScattered = (Arch != Triple::x86_64) &&
1221 (RENext->Word0 & macho::RF_Scattered);
1222 unsigned RType;
1223 if (isNextScattered)
1224 RType = (RENext->Word0 >> 24) & 0xF;
1225 else
1226 RType = (RENext->Word1 >> 28) & 0xF;
1227
Owen Anderson013d7562011-10-25 18:48:41 +00001228 if (RType != 1)
1229 report_fatal_error("Expected ARM_RELOC_PAIR after "
1230 "GENERIC_RELOC_HALF");
1231
Owen Anderson1832f4d2011-10-26 20:42:54 +00001232 // NOTE: The half of the target virtual address is stashed in the
1233 // address field of the secondary relocation, but we can't reverse
1234 // engineer the constant offset from it without decoding the movw/movt
1235 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001236
1237 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1238 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001239 if (Type == macho::RIT_ARM_HalfDifference) {
1240 fmt << "-";
1241 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001242 }
1243
Owen Anderson013d7562011-10-25 18:48:41 +00001244 fmt << ")";
1245 break;
1246 }
1247 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001248 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001249 }
1250 }
1251 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001252 } else
1253 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001254
Owen Anderson0135fe12011-10-24 21:44:00 +00001255 fmt.flush();
1256 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001257 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001258}
1259
Owen Anderson0685e942011-10-25 20:35:53 +00001260error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1261 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001262 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001263
Owen Anderson0685e942011-10-25 20:35:53 +00001264 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001265 bool isScattered = (Arch != Triple::x86_64) &&
1266 (RE->Word0 & macho::RF_Scattered);
1267 unsigned Type;
1268 if (isScattered)
1269 Type = (RE->Word0 >> 24) & 0xF;
1270 else
1271 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001272
1273 Result = false;
1274
1275 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1276 // is always hidden.
1277 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001278 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001279 } else if (Arch == Triple::x86_64) {
1280 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1281 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001282 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001283 DataRefImpl RelPrev = Rel;
1284 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001285 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001286
1287 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1288
Owen Anderson1832f4d2011-10-26 20:42:54 +00001289 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001290 }
1291 }
1292
1293 return object_error::success;
1294}
1295
David Meyer5c2b4ea2012-03-01 01:36:50 +00001296error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1297 LibraryRef &Res) const {
1298 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1299}
1300
1301error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1302 StringRef &Res) const {
1303 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1304}
1305
1306
Eric Christopher6256b032011-04-22 03:19:48 +00001307/*===-- Miscellaneous -----------------------------------------------------===*/
1308
1309uint8_t MachOObjectFile::getBytesInAddress() const {
1310 return MachOObj->is64Bit() ? 8 : 4;
1311}
1312
1313StringRef MachOObjectFile::getFileFormatName() const {
1314 if (!MachOObj->is64Bit()) {
1315 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001316 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001317 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001318 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001319 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001320 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001321 return "Mach-O 32-bit ppc";
1322 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001323 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001324 "64-bit object file when we're not 64-bit?");
1325 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001326 }
1327 }
1328
Eric Christopherb3e6b042013-02-28 20:26:17 +00001329 // Make sure the cpu type has the correct mask.
1330 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1331 == llvm::MachO::CPUArchABI64 &&
1332 "32-bit object file when we're 64-bit?");
1333
Eric Christopher6256b032011-04-22 03:19:48 +00001334 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001335 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001336 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001337 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001338 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001339 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001340 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001341 }
1342}
1343
1344unsigned MachOObjectFile::getArch() const {
1345 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001346 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001347 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001348 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001349 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001350 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001351 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001352 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001353 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001354 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001355 return Triple::ppc64;
1356 default:
1357 return Triple::UnknownArch;
1358 }
1359}
1360
Owen Andersonf7c93a32011-10-11 17:32:27 +00001361} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001362} // end namespace llvm