blob: e20ca2b35338d272455b18d782bb4c823988490c [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) {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000118 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Eric Christopher6256b032011-04-22 03:19:48 +0000119 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 {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000133 LoadCommandInfo LCI = 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 {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000154 LoadCommandInfo LCI = 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 Espindolaf3051272013-04-07 17:41:59 +0000183 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000184 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) {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000481 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Eric Christopher6256b032011-04-22 03:19:48 +0000482 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 Espindolaf3051272013-04-07 17:41:59 +0000507static bool is64BitLoadCommand(const MachOObjectFile *MachOObj,
508 DataRefImpl DRI) {
Eric Christopher6256b032011-04-22 03:19:48 +0000509 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000510 if (LCI.Command.Type == macho::LCT_Segment64)
511 return true;
512 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
513 return false;
514}
515
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000516const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000517 assert(!is64BitLoadCommand(this, DRI));
518 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000519 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000520 DRI.d.b * sizeof(MachOFormat::Section);
521 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
522 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000523}
524
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000525std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
526 SectionList::const_iterator loc =
527 std::find(Sections.begin(), Sections.end(), Sec);
528 assert(loc != Sections.end() && "Sec is not a valid section!");
529 return std::distance(Sections.begin(), loc);
530}
531
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000532const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000533MachOObjectFile::getSection64(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000534 assert(is64BitLoadCommand(this, DRI));
535 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000536 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000537 DRI.d.b * sizeof(MachOFormat::Section64);
538 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
539 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000540}
541
Rafael Espindolacef81b32012-12-21 03:47:03 +0000542static StringRef parseSegmentOrSectionName(const char *P) {
543 if (P[15] == 0)
544 // Null terminated.
545 return P;
546 // Not null terminated, so this is a 16 char string.
547 return StringRef(P, 16);
548}
549
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000550ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000551 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000552 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000553 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000554 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000555 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000556 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000557 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000558}
559
560error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
561 StringRef &Result) const {
562 ArrayRef<char> Raw = getSectionRawName(DRI);
563 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000564 return object_error::success;
565}
566
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000567ArrayRef<char>
568MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000569 if (is64BitLoadCommand(this, Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000570 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000571 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000572 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000573 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000574 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000575 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000576}
577
578StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
579 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
580 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000581}
582
Michael J. Spencer25b15772011-06-25 17:55:23 +0000583error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
584 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000585 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000586 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000587 Result = Sect->Address;
588 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000589 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000590 Result = Sect->Address;
591 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000592 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000593}
594
Michael J. Spencer25b15772011-06-25 17:55:23 +0000595error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
596 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000597 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000598 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000599 Result = Sect->Size;
600 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000601 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000602 Result = Sect->Size;
603 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000604 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000605}
606
Michael J. Spencer25b15772011-06-25 17:55:23 +0000607error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
608 StringRef &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000609 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000610 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000611 Result = MachOObj->getData(Sect->Offset, Sect->Size);
612 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000613 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000614 Result = MachOObj->getData(Sect->Offset, Sect->Size);
615 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000616 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000617}
618
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000619error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
620 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000621 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000622 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000623 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000624 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000625 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000626 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000627 }
628 return object_error::success;
629}
630
Michael J. Spencer25b15772011-06-25 17:55:23 +0000631error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
632 bool &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000633 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000634 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000635 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000636 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000637 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000638 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000639 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000640 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000641}
642
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000643error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
644 bool &Result) const {
645 // FIXME: Unimplemented.
646 Result = false;
647 return object_error::success;
648}
649
650error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
651 bool &Result) const {
652 // FIXME: Unimplemented.
653 Result = false;
654 return object_error::success;
655}
656
Preston Gurdc68dda82012-04-12 20:13:57 +0000657error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
658 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000659 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000660 Result = true;
661 return object_error::success;
662}
663
664error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000665 bool &Result) const {
666 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000667 Result = false;
668 return object_error::success;
669}
670
671error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
672 bool &Result) const {
673 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000674 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000675 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
676 Result = (SectionType == MachO::SectionTypeZeroFill ||
677 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000678 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000679 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000680 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
681 Result = (SectionType == MachO::SectionTypeZeroFill ||
682 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000683 }
684
685 return object_error::success;
686}
687
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000688error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
689 bool &Result) const {
690 // Consider using the code from isSectionText to look for __const sections.
691 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
692 // to use section attributes to distinguish code from data.
693
694 // FIXME: Unimplemented.
695 Result = false;
696 return object_error::success;
697}
698
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000699error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
700 DataRefImpl Symb,
701 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000702 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000703 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000704 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000705 Result = false;
706 return object_error::success;
707 }
708
709 uint64_t SectBegin, SectEnd;
710 getSectionAddress(Sec, SectBegin);
711 getSectionSize(Sec, SectEnd);
712 SectEnd += SectBegin;
713
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000714 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000715 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000716 uint64_t SymAddr= Entry->Value;
717 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000718 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000719 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000720 uint64_t SymAddr= Entry->Value;
721 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000722 }
Owen Andersoncd749882011-10-12 22:21:32 +0000723
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000724 return object_error::success;
725}
726
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000727relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
728 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000729 ret.d.b = getSectionIndex(Sec);
730 return relocation_iterator(RelocationRef(ret, this));
731}
732relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
733 uint32_t last_reloc;
Rafael Espindolaf3051272013-04-07 17:41:59 +0000734 if (is64BitLoadCommand(this, Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000735 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000736 last_reloc = Sect->NumRelocationTableEntries;
737 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000738 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000739 last_reloc = Sect->NumRelocationTableEntries;
740 }
741 DataRefImpl ret;
742 ret.d.a = last_reloc;
743 ret.d.b = getSectionIndex(Sec);
744 return relocation_iterator(RelocationRef(ret, this));
745}
746
747section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000748 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000749 moveToNextSection(DRI);
750 return section_iterator(SectionRef(DRI, this));
751}
752
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000753section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000754 DataRefImpl DRI;
755 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000756 return section_iterator(SectionRef(DRI, this));
757}
758
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000759/*===-- Relocations -------------------------------------------------------===*/
760
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000761const MachOFormat::RelocationEntry *
762MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000763 uint32_t relOffset;
764 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000765 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000766 relOffset = Sect->RelocationTableOffset;
767 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000768 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000769 relOffset = Sect->RelocationTableOffset;
770 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000771 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
772 StringRef Data =
773 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
774 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000775}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000776
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000777error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
778 RelocationRef &Res) const {
779 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000780 Res = RelocationRef(Rel, this);
781 return object_error::success;
782}
783error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
784 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000785 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000786 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000787 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000788 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000789 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000790 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000791 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000792 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000793 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000794
795 unsigned Arch = getArch();
796 bool isScattered = (Arch != Triple::x86_64) &&
797 (RE->Word0 & macho::RF_Scattered);
798 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000799 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000800 RelAddr = RE->Word0 & 0xFFFFFF;
801 else
802 RelAddr = RE->Word0;
803
804 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000805 return object_error::success;
806}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000807error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
808 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000809 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000810
811 unsigned Arch = getArch();
812 bool isScattered = (Arch != Triple::x86_64) &&
813 (RE->Word0 & macho::RF_Scattered);
814 if (isScattered)
815 Res = RE->Word0 & 0xFFFFFF;
816 else
817 Res = RE->Word0;
818 return object_error::success;
819}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000820error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
821 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000822 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000823 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
824 bool isExtern = (RE->Word1 >> 27) & 1;
825
826 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000827 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000828 if (isExtern) {
829 for (unsigned i = 0; i < SymbolIdx; i++) {
830 Sym.d.b++;
831 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000832 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000833 "Relocation symbol index out of range!");
834 }
835 }
836 Res = SymbolRef(Sym, this);
837 return object_error::success;
838}
839error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000840 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000841 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000842 Res = RE->Word0;
843 Res <<= 32;
844 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000845 return object_error::success;
846}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000847error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
848 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000849 // TODO: Support scattered relocations.
850 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000851 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000852
853 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000854 bool isScattered = (Arch != Triple::x86_64) &&
855 (RE->Word0 & macho::RF_Scattered);
856
857 unsigned r_type;
858 if (isScattered)
859 r_type = (RE->Word0 >> 24) & 0xF;
860 else
861 r_type = (RE->Word1 >> 28) & 0xF;
862
Owen Anderson0135fe12011-10-24 21:44:00 +0000863 switch (Arch) {
864 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000865 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000866 "GENERIC_RELOC_VANILLA",
867 "GENERIC_RELOC_PAIR",
868 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000869 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000870 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000871 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000872
Owen Andersoneb6bd332011-10-27 20:46:09 +0000873 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000874 res = "Unknown";
875 else
876 res = Table[r_type];
877 break;
878 }
879 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000880 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000881 "X86_64_RELOC_UNSIGNED",
882 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000883 "X86_64_RELOC_BRANCH",
884 "X86_64_RELOC_GOT_LOAD",
885 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000886 "X86_64_RELOC_SUBTRACTOR",
887 "X86_64_RELOC_SIGNED_1",
888 "X86_64_RELOC_SIGNED_2",
889 "X86_64_RELOC_SIGNED_4",
890 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000891
Owen Andersond8fa76d2011-10-24 23:20:07 +0000892 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000893 res = "Unknown";
894 else
895 res = Table[r_type];
896 break;
897 }
898 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000899 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000900 "ARM_RELOC_VANILLA",
901 "ARM_RELOC_PAIR",
902 "ARM_RELOC_SECTDIFF",
903 "ARM_RELOC_LOCAL_SECTDIFF",
904 "ARM_RELOC_PB_LA_PTR",
905 "ARM_RELOC_BR24",
906 "ARM_THUMB_RELOC_BR22",
907 "ARM_THUMB_32BIT_BRANCH",
908 "ARM_RELOC_HALF",
909 "ARM_RELOC_HALF_SECTDIFF" };
910
911 if (r_type > 9)
912 res = "Unknown";
913 else
914 res = Table[r_type];
915 break;
916 }
917 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000918 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000919 "PPC_RELOC_VANILLA",
920 "PPC_RELOC_PAIR",
921 "PPC_RELOC_BR14",
922 "PPC_RELOC_BR24",
923 "PPC_RELOC_HI16",
924 "PPC_RELOC_LO16",
925 "PPC_RELOC_HA16",
926 "PPC_RELOC_LO14",
927 "PPC_RELOC_SECTDIFF",
928 "PPC_RELOC_PB_LA_PTR",
929 "PPC_RELOC_HI16_SECTDIFF",
930 "PPC_RELOC_LO16_SECTDIFF",
931 "PPC_RELOC_HA16_SECTDIFF",
932 "PPC_RELOC_JBSR",
933 "PPC_RELOC_LO14_SECTDIFF",
934 "PPC_RELOC_LOCAL_SECTDIFF" };
935
936 res = Table[r_type];
937 break;
938 }
939 case Triple::UnknownArch:
940 res = "Unknown";
941 break;
942 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000943 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000944 return object_error::success;
945}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000946error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
947 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000948 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000949 bool isExtern = (RE->Word1 >> 27) & 1;
950 Res = 0;
951 if (!isExtern) {
952 const uint8_t* sectAddress = base();
953 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000954 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000955 sectAddress += Sect->Offset;
956 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000957 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000958 sectAddress += Sect->Offset;
959 }
960 Res = reinterpret_cast<uintptr_t>(sectAddress);
961 }
962 return object_error::success;
963}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000964
965// Helper to advance a section or symbol iterator multiple increments at a time.
966template<class T>
967error_code advance(T &it, size_t Val) {
968 error_code ec;
969 while (Val--) {
970 it.increment(ec);
971 }
972 return ec;
973}
974
975template<class T>
976void advanceTo(T &it, size_t Val) {
977 if (error_code ec = advance(it, Val))
978 report_fatal_error(ec.message());
979}
980
Owen Anderson1832f4d2011-10-26 20:42:54 +0000981void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000982 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000983 raw_string_ostream &fmt) const {
984 unsigned Arch = getArch();
985 bool isScattered = (Arch != Triple::x86_64) &&
986 (RE->Word0 & macho::RF_Scattered);
987
988 // Target of a scattered relocation is an address. In the interest of
989 // generating pretty output, scan through the symbol table looking for a
990 // symbol that aligns with that address. If we find one, print it.
991 // Otherwise, we just print the hex address of the target.
992 if (isScattered) {
993 uint32_t Val = RE->Word1;
994
995 error_code ec;
996 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
997 SI.increment(ec)) {
998 if (ec) report_fatal_error(ec.message());
999
1000 uint64_t Addr;
1001 StringRef Name;
1002
1003 if ((ec = SI->getAddress(Addr)))
1004 report_fatal_error(ec.message());
1005 if (Addr != Val) continue;
1006 if ((ec = SI->getName(Name)))
1007 report_fatal_error(ec.message());
1008 fmt << Name;
1009 return;
1010 }
1011
Owen Andersonb28bdbf2011-10-27 21:53:50 +00001012 // If we couldn't find a symbol that this relocation refers to, try
1013 // to find a section beginning instead.
1014 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
1015 SI.increment(ec)) {
1016 if (ec) report_fatal_error(ec.message());
1017
1018 uint64_t Addr;
1019 StringRef Name;
1020
1021 if ((ec = SI->getAddress(Addr)))
1022 report_fatal_error(ec.message());
1023 if (Addr != Val) continue;
1024 if ((ec = SI->getName(Name)))
1025 report_fatal_error(ec.message());
1026 fmt << Name;
1027 return;
1028 }
1029
Owen Anderson1832f4d2011-10-26 20:42:54 +00001030 fmt << format("0x%x", Val);
1031 return;
1032 }
1033
1034 StringRef S;
1035 bool isExtern = (RE->Word1 >> 27) & 1;
1036 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001037
1038 if (isExtern) {
1039 symbol_iterator SI = begin_symbols();
1040 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001041 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001042 } else {
1043 section_iterator SI = begin_sections();
1044 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001045 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001046 }
1047
Owen Anderson1832f4d2011-10-26 20:42:54 +00001048 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001049}
1050
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001051error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1052 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001053 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +00001054
Owen Anderson1832f4d2011-10-26 20:42:54 +00001055 unsigned Arch = getArch();
1056 bool isScattered = (Arch != Triple::x86_64) &&
1057 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001058
Owen Anderson013d7562011-10-25 18:48:41 +00001059 std::string fmtbuf;
1060 raw_string_ostream fmt(fmtbuf);
1061
Owen Anderson1832f4d2011-10-26 20:42:54 +00001062 unsigned Type;
1063 if (isScattered)
1064 Type = (RE->Word0 >> 24) & 0xF;
1065 else
1066 Type = (RE->Word1 >> 28) & 0xF;
1067
Owen Andersoneb6bd332011-10-27 20:46:09 +00001068 bool isPCRel;
1069 if (isScattered)
1070 isPCRel = ((RE->Word0 >> 30) & 1);
1071 else
1072 isPCRel = ((RE->Word1 >> 24) & 1);
1073
Owen Andersond8fa76d2011-10-24 23:20:07 +00001074 // Determine any addends that should be displayed with the relocation.
1075 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001076
1077 // X86_64 has entirely custom relocation types.
1078 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001079 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001080
Owen Andersond8fa76d2011-10-24 23:20:07 +00001081 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001082 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1083 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1084 printRelocationTargetName(RE, fmt);
1085 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001086 if (isPCRel) fmt << "PCREL";
1087 break;
1088 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001089 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001090 DataRefImpl RelNext = Rel;
1091 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001092 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001093
1094 // X86_64_SUBTRACTOR must be followed by a relocation of type
1095 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001096 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001097 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001098 if (RType != 0)
1099 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1100 "X86_64_RELOC_SUBTRACTOR.");
1101
Owen Andersonef22f782011-10-26 17:28:49 +00001102 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1103 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001104 printRelocationTargetName(RENext, fmt);
1105 fmt << "-";
1106 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001107 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001108 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001109 case macho::RIT_X86_64_TLV:
1110 printRelocationTargetName(RE, fmt);
1111 fmt << "@TLV";
1112 if (isPCRel) fmt << "P";
1113 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001114 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1115 printRelocationTargetName(RE, fmt);
1116 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001117 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001118 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1119 printRelocationTargetName(RE, fmt);
1120 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001121 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001122 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1123 printRelocationTargetName(RE, fmt);
1124 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001125 break;
1126 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001127 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001128 break;
1129 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001130 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001131 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1132 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001133 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001134 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001135 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001136 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001137 DataRefImpl RelNext = Rel;
1138 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001139 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001140
1141 // X86 sect diff's must be followed by a relocation of type
1142 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001143 bool isNextScattered = (Arch != Triple::x86_64) &&
1144 (RENext->Word0 & macho::RF_Scattered);
1145 unsigned RType;
1146 if (isNextScattered)
1147 RType = (RENext->Word0 >> 24) & 0xF;
1148 else
1149 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001150 if (RType != 1)
1151 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001152 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001153
Owen Anderson1832f4d2011-10-26 20:42:54 +00001154 printRelocationTargetName(RE, fmt);
1155 fmt << "-";
1156 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001157 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001158 }
1159 }
Owen Anderson013d7562011-10-25 18:48:41 +00001160
Owen Anderson1832f4d2011-10-26 20:42:54 +00001161 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001162 // All X86 relocations that need special printing were already
1163 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001164 switch (Type) {
1165 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001166 DataRefImpl RelNext = Rel;
1167 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001168 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001169
1170 // X86 sect diff's must be followed by a relocation of type
1171 // GENERIC_RELOC_PAIR.
1172 bool isNextScattered = (Arch != Triple::x86_64) &&
1173 (RENext->Word0 & macho::RF_Scattered);
1174 unsigned RType;
1175 if (isNextScattered)
1176 RType = (RENext->Word0 >> 24) & 0xF;
1177 else
1178 RType = (RENext->Word1 >> 28) & 0xF;
1179 if (RType != 1)
1180 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1181 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1182
1183 printRelocationTargetName(RE, fmt);
1184 fmt << "-";
1185 printRelocationTargetName(RENext, fmt);
1186 break;
1187 }
1188 case macho::RIT_Generic_TLV: {
1189 printRelocationTargetName(RE, fmt);
1190 fmt << "@TLV";
1191 if (isPCRel) fmt << "P";
1192 break;
1193 }
1194 default:
1195 printRelocationTargetName(RE, fmt);
1196 }
Owen Anderson013d7562011-10-25 18:48:41 +00001197 } else { // ARM-specific relocations
1198 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001199 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1200 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001201 // Half relocations steal a bit from the length field to encode
1202 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001203 bool isUpper;
1204 if (isScattered)
1205 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001206 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001207 isUpper = (RE->Word1 >> 25) & 1;
1208
1209 if (isUpper)
1210 fmt << ":upper16:(";
1211 else
1212 fmt << ":lower16:(";
1213 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001214
Owen Anderson013d7562011-10-25 18:48:41 +00001215 DataRefImpl RelNext = Rel;
1216 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001217 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001218
1219 // ARM half relocs must be followed by a relocation of type
1220 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001221 bool isNextScattered = (Arch != Triple::x86_64) &&
1222 (RENext->Word0 & macho::RF_Scattered);
1223 unsigned RType;
1224 if (isNextScattered)
1225 RType = (RENext->Word0 >> 24) & 0xF;
1226 else
1227 RType = (RENext->Word1 >> 28) & 0xF;
1228
Owen Anderson013d7562011-10-25 18:48:41 +00001229 if (RType != 1)
1230 report_fatal_error("Expected ARM_RELOC_PAIR after "
1231 "GENERIC_RELOC_HALF");
1232
Owen Anderson1832f4d2011-10-26 20:42:54 +00001233 // NOTE: The half of the target virtual address is stashed in the
1234 // address field of the secondary relocation, but we can't reverse
1235 // engineer the constant offset from it without decoding the movw/movt
1236 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001237
1238 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1239 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001240 if (Type == macho::RIT_ARM_HalfDifference) {
1241 fmt << "-";
1242 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001243 }
1244
Owen Anderson013d7562011-10-25 18:48:41 +00001245 fmt << ")";
1246 break;
1247 }
1248 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001249 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001250 }
1251 }
1252 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001253 } else
1254 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001255
Owen Anderson0135fe12011-10-24 21:44:00 +00001256 fmt.flush();
1257 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001258 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001259}
1260
Owen Anderson0685e942011-10-25 20:35:53 +00001261error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1262 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001263 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001264
Owen Anderson0685e942011-10-25 20:35:53 +00001265 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001266 bool isScattered = (Arch != Triple::x86_64) &&
1267 (RE->Word0 & macho::RF_Scattered);
1268 unsigned Type;
1269 if (isScattered)
1270 Type = (RE->Word0 >> 24) & 0xF;
1271 else
1272 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001273
1274 Result = false;
1275
1276 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1277 // is always hidden.
1278 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001279 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001280 } else if (Arch == Triple::x86_64) {
1281 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1282 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001283 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001284 DataRefImpl RelPrev = Rel;
1285 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001286 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001287
1288 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1289
Owen Anderson1832f4d2011-10-26 20:42:54 +00001290 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001291 }
1292 }
1293
1294 return object_error::success;
1295}
1296
David Meyer5c2b4ea2012-03-01 01:36:50 +00001297error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1298 LibraryRef &Res) const {
1299 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1300}
1301
1302error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1303 StringRef &Res) const {
1304 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1305}
1306
1307
Eric Christopher6256b032011-04-22 03:19:48 +00001308/*===-- Miscellaneous -----------------------------------------------------===*/
1309
1310uint8_t MachOObjectFile::getBytesInAddress() const {
1311 return MachOObj->is64Bit() ? 8 : 4;
1312}
1313
1314StringRef MachOObjectFile::getFileFormatName() const {
1315 if (!MachOObj->is64Bit()) {
1316 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001317 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001318 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001319 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001320 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001321 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001322 return "Mach-O 32-bit ppc";
1323 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001324 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001325 "64-bit object file when we're not 64-bit?");
1326 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001327 }
1328 }
1329
Eric Christopherb3e6b042013-02-28 20:26:17 +00001330 // Make sure the cpu type has the correct mask.
1331 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1332 == llvm::MachO::CPUArchABI64 &&
1333 "32-bit object file when we're 64-bit?");
1334
Eric Christopher6256b032011-04-22 03:19:48 +00001335 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001336 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001337 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001338 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001339 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001340 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001341 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001342 }
1343}
1344
1345unsigned MachOObjectFile::getArch() const {
1346 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001347 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001348 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001349 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001350 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001351 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001352 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001353 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001354 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001355 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001356 return Triple::ppc64;
1357 default:
1358 return Triple::UnknownArch;
1359 }
1360}
1361
Owen Andersonf7c93a32011-10-11 17:32:27 +00001362} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001363} // end namespace llvm