blob: dbbc812d14be7b6ff5638cabf6412cd7357c6b44 [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 Espindola77638d92013-04-07 18:08:12 +000061MachOObjectFile::LoadCommandInfo
Rafael Espindola3eff3182013-04-07 16:07:35 +000062MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
Rafael Espindola77638d92013-04-07 18:08:12 +000063 uint64_t Offset;
64 uint64_t NewOffset = MachOObj->getHeaderSize();
65 const MachOFormat::LoadCommand *Load;
66 unsigned I = 0;
67 do {
68 Offset = NewOffset;
69 StringRef Data = MachOObj->getData(Offset,
70 sizeof(MachOFormat::LoadCommand));
71 Load = reinterpret_cast<const MachOFormat::LoadCommand*>(Data.data());
72 NewOffset = Offset + Load->Size;
73 ++I;
74 } while (I != Index + 1);
75
76 LoadCommandInfo Ret = {Load, Offset};
77 return Ret;
Rafael Espindola3eff3182013-04-07 16:07:35 +000078}
79
80void MachOObjectFile::ReadULEB128s(uint64_t Index,
81 SmallVectorImpl<uint64_t> &Out) const {
82 return MachOObj->ReadULEB128s(Index, Out);
83}
84
85const macho::Header &MachOObjectFile::getHeader() const {
86 return MachOObj->getHeader();
87}
88
Eric Christopher6256b032011-04-22 03:19:48 +000089ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000090 error_code ec;
Rafael Espindola6f1f3392013-04-07 16:58:48 +000091 ObjectFile *Ret = new MachOObjectFile(Buffer, ec);
92 if (ec)
Eric Christopher6256b032011-04-22 03:19:48 +000093 return NULL;
Rafael Espindola6f1f3392013-04-07 16:58:48 +000094 return Ret;
Eric Christopher6256b032011-04-22 03:19:48 +000095}
96
97/*===-- Symbols -----------------------------------------------------------===*/
98
Rafael Espindola82a21072013-04-06 03:31:08 +000099const MachOFormat::SymtabLoadCommand *
100MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
101 StringRef Data = MachOObj->getData(LCI.Offset,
102 sizeof(MachOFormat::SymtabLoadCommand));
103 return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
104}
105
Rafael Espindola68d287d2013-04-06 03:50:05 +0000106const MachOFormat::SegmentLoadCommand *
107MachOObjectFile::getSegmentLoadCommand(LoadCommandInfo LCI) const {
108 StringRef Data = MachOObj->getData(LCI.Offset,
109 sizeof(MachOFormat::SegmentLoadCommand));
110 return reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Data.data());
111}
112
Rafael Espindola196abbf2013-04-07 14:40:18 +0000113const MachOFormat::LinkeditDataLoadCommand *
114MachOObjectFile::getLinkeditDataLoadCommand(LoadCommandInfo LCI) const {
115 StringRef Data = MachOObj->getData(LCI.Offset,
116 sizeof(MachOFormat::LinkeditDataLoadCommand));
117 return
118 reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Data.data());
119}
120
Rafael Espindola68d287d2013-04-06 03:50:05 +0000121const MachOFormat::Segment64LoadCommand *
122MachOObjectFile::getSegment64LoadCommand(LoadCommandInfo LCI) const {
123 StringRef Data = MachOObj->getData(LCI.Offset,
124 sizeof(MachOFormat::Segment64LoadCommand));
125 return
126 reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Data.data());
127}
128
Eric Christopher6256b032011-04-22 03:19:48 +0000129void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
130 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
131 while (DRI.d.a < LoadCommandCount) {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000132 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindola77638d92013-04-07 18:08:12 +0000133 if (LCI.Command->Type == macho::LCT_Symtab) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000134 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
135 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000136 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
137 return;
138 }
139
140 DRI.d.a++;
141 DRI.d.b = 0;
142 }
143}
144
Rafael Espindola00555c12013-04-06 01:59:05 +0000145const MachOFormat::SymbolTableEntry *
146MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000147 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000148 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
149 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000150
Rafael Espindola82a21072013-04-06 03:31:08 +0000151 return getSymbolTableEntry(DRI, SymtabLoadCmd);
152}
Eric Christopher6256b032011-04-22 03:19:48 +0000153
Rafael Espindola82a21072013-04-06 03:31:08 +0000154const MachOFormat::SymbolTableEntry *
155MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
156 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola00555c12013-04-06 01:59:05 +0000157 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
158 unsigned Index = DRI.d.b;
159 uint64_t Offset = (SymbolTableOffset +
160 Index * sizeof(macho::SymbolTableEntry));
161 StringRef Data = MachOObj->getData(Offset,
162 sizeof(MachOFormat::SymbolTableEntry));
163 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000164}
165
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000166const MachOFormat::Symbol64TableEntry*
167MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000168 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000169 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
170 getSymtabLoadCommand(LCI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000171
Rafael Espindola82a21072013-04-06 03:31:08 +0000172 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
173}
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000174
Rafael Espindola82a21072013-04-06 03:31:08 +0000175const MachOFormat::Symbol64TableEntry*
176MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
177 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000178 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
179 unsigned Index = DRI.d.b;
180 uint64_t Offset = (SymbolTableOffset +
181 Index * sizeof(macho::Symbol64TableEntry));
182 StringRef Data = MachOObj->getData(Offset,
183 sizeof(MachOFormat::Symbol64TableEntry));
184 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000185}
186
Michael J. Spencer25b15772011-06-25 17:55:23 +0000187error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
188 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000189 DRI.d.b++;
190 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000191 Result = SymbolRef(DRI, this);
192 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000193}
194
Michael J. Spencer25b15772011-06-25 17:55:23 +0000195error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
196 StringRef &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000197 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000198 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
199 getSymtabLoadCommand(LCI);
200
201 StringRef StringTable =
202 MachOObj->getData(SymtabLoadCmd->StringTableOffset,
203 SymtabLoadCmd->StringTableSize);
204
205 uint32_t StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000206 if (MachOObj->is64Bit()) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000207 const MachOFormat::Symbol64TableEntry *Entry =
208 getSymbol64TableEntry(DRI, SymtabLoadCmd);
209 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000210 } else {
Rafael Espindola82a21072013-04-06 03:31:08 +0000211 const MachOFormat::SymbolTableEntry *Entry =
212 getSymbolTableEntry(DRI, SymtabLoadCmd);
213 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000214 }
Rafael Espindola82a21072013-04-06 03:31:08 +0000215
216 const char *Start = &StringTable.data()[StringIndex];
217 Result = StringRef(Start);
218
Michael J. Spencer25b15772011-06-25 17:55:23 +0000219 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000220}
221
Danil Malyshevb0436a72011-11-29 17:40:10 +0000222error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
223 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000224 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000225 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000226 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000227 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000228 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000229 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000230 Result += Section->Offset - Section->Address;
231 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000232 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000233 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000234 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000235 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000236 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000237 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000238 Result += Section->Offset - Section->Address;
239 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000240 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000241
Michael J. Spencer25b15772011-06-25 17:55:23 +0000242 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000243}
244
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000245error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
246 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000247 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000248 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000249 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000250 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000251 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000252 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000253 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000254 return object_error::success;
255}
256
Michael J. Spencer25b15772011-06-25 17:55:23 +0000257error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
258 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000259 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
260 uint64_t BeginOffset;
261 uint64_t EndOffset = 0;
262 uint8_t SectionIndex;
263 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000264 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000265 BeginOffset = Entry->Value;
266 SectionIndex = Entry->SectionIndex;
267 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000268 uint32_t flags = SymbolRef::SF_None;
269 getSymbolFlags(DRI, flags);
270 if (flags & SymbolRef::SF_Common)
271 Result = Entry->Value;
272 else
273 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000274 return object_error::success;
275 }
276 // Unfortunately symbols are unsorted so we need to touch all
277 // symbols from load command
278 DRI.d.b = 0;
279 uint32_t Command = DRI.d.a;
280 while (Command == DRI.d.a) {
281 moveToNextSymbol(DRI);
282 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000283 Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000284 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
285 if (!EndOffset || Entry->Value < EndOffset)
286 EndOffset = Entry->Value;
287 }
288 DRI.d.b++;
289 }
290 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000291 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000292 BeginOffset = Entry->Value;
293 SectionIndex = Entry->SectionIndex;
294 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000295 uint32_t flags = SymbolRef::SF_None;
296 getSymbolFlags(DRI, flags);
297 if (flags & SymbolRef::SF_Common)
298 Result = Entry->Value;
299 else
300 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000301 return object_error::success;
302 }
303 // Unfortunately symbols are unsorted so we need to touch all
304 // symbols from load command
305 DRI.d.b = 0;
306 uint32_t Command = DRI.d.a;
307 while (Command == DRI.d.a) {
308 moveToNextSymbol(DRI);
309 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000310 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000311 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
312 if (!EndOffset || Entry->Value < EndOffset)
313 EndOffset = Entry->Value;
314 }
315 DRI.d.b++;
316 }
317 }
318 if (!EndOffset) {
319 uint64_t Size;
320 getSectionSize(Sections[SectionIndex-1], Size);
321 getSectionAddress(Sections[SectionIndex-1], EndOffset);
322 EndOffset += Size;
323 }
324 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000325 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000326}
327
Michael J. Spencer25b15772011-06-25 17:55:23 +0000328error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
329 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000330 uint8_t Type, Flags;
331 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000332 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000333 Type = Entry->Type;
334 Flags = Entry->Flags;
335 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000336 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000337 Type = Entry->Type;
338 Flags = Entry->Flags;
339 }
Eric Christopher6256b032011-04-22 03:19:48 +0000340
341 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000342 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000343 case macho::STT_Undefined:
344 Char = 'u';
345 break;
346 case macho::STT_Absolute:
347 case macho::STT_Section:
348 Char = 's';
349 break;
350 default:
351 Char = '?';
352 break;
353 }
354
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000355 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000356 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000357 Result = Char;
358 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000359}
360
David Meyerc46255a2012-02-28 23:47:53 +0000361error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
362 uint32_t &Result) const {
363 uint16_t MachOFlags;
364 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000365 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000366 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000367 MachOFlags = Entry->Flags;
368 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000369 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000370 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000371 MachOFlags = Entry->Flags;
372 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000373 }
374
Preston Gurdc68dda82012-04-12 20:13:57 +0000375 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000376 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000377
378 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
379 Result |= SymbolRef::SF_Undefined;
380
David Meyerc46255a2012-02-28 23:47:53 +0000381 if (MachOFlags & macho::STF_StabsEntryMask)
382 Result |= SymbolRef::SF_FormatSpecific;
383
Preston Gurdc68dda82012-04-12 20:13:57 +0000384 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000385 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000386 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
387 Result |= SymbolRef::SF_Common;
388 }
David Meyerc46255a2012-02-28 23:47:53 +0000389
390 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
391 Result |= SymbolRef::SF_Weak;
392
393 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
394 Result |= SymbolRef::SF_Absolute;
395
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000396 return object_error::success;
397}
398
399error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
400 section_iterator &Res) const {
401 uint8_t index;
402 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000403 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000404 index = Entry->SectionIndex;
405 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000406 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000407 index = Entry->SectionIndex;
408 }
409
410 if (index == 0)
411 Res = end_sections();
412 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000413 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000414
415 return object_error::success;
416}
417
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000418error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000419 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000420 uint8_t n_type;
421 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000422 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000423 n_type = Entry->Type;
424 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000425 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000426 n_type = Entry->Type;
427 }
428 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000429
430 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000431 if (n_type & MachO::NlistMaskStab) {
432 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000433 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000434 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000435
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000436 switch (n_type & MachO::NlistMaskType) {
437 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000438 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000439 break;
440 case MachO::NListTypeSection :
441 Res = SymbolRef::ST_Function;
442 break;
443 }
444 return object_error::success;
445}
446
Tim Northovera41dce32012-10-29 10:47:00 +0000447error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
448 uint64_t &Val) const {
449 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
450}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000451
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000452symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000453 // DRI.d.a = segment number; DRI.d.b = symbol index.
454 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000455 moveToNextSymbol(DRI);
456 return symbol_iterator(SymbolRef(DRI, this));
457}
458
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000459symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000460 DataRefImpl DRI;
461 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000462 return symbol_iterator(SymbolRef(DRI, this));
463}
464
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000465symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
466 // TODO: implement
467 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
468}
469
470symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
471 // TODO: implement
472 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
473}
Eric Christopher6256b032011-04-22 03:19:48 +0000474
David Meyer5c2b4ea2012-03-01 01:36:50 +0000475library_iterator MachOObjectFile::begin_libraries_needed() const {
476 // TODO: implement
477 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
478}
479
480library_iterator MachOObjectFile::end_libraries_needed() const {
481 // TODO: implement
482 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
483}
484
David Meyer97f77872012-03-01 22:19:54 +0000485StringRef MachOObjectFile::getLoadName() const {
486 // TODO: Implement
487 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
488}
489
Eric Christopher6256b032011-04-22 03:19:48 +0000490/*===-- Sections ----------------------------------------------------------===*/
491
492void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
493 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
494 while (DRI.d.a < LoadCommandCount) {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000495 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindola77638d92013-04-07 18:08:12 +0000496 if (LCI.Command->Type == macho::LCT_Segment) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000497 const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
498 getSegmentLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000499 if (DRI.d.b < SegmentLoadCmd->NumSections)
500 return;
Rafael Espindola77638d92013-04-07 18:08:12 +0000501 } else if (LCI.Command->Type == macho::LCT_Segment64) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000502 const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
503 getSegment64LoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000504 if (DRI.d.b < Segment64LoadCmd->NumSections)
505 return;
506 }
507
508 DRI.d.a++;
509 DRI.d.b = 0;
510 }
511}
512
Michael J. Spencer25b15772011-06-25 17:55:23 +0000513error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
514 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000515 DRI.d.b++;
516 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000517 Result = SectionRef(DRI, this);
518 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000519}
520
Rafael Espindolaf3051272013-04-07 17:41:59 +0000521static bool is64BitLoadCommand(const MachOObjectFile *MachOObj,
522 DataRefImpl DRI) {
Rafael Espindola77638d92013-04-07 18:08:12 +0000523 MachOObjectFile::LoadCommandInfo LCI =
524 MachOObj->getLoadCommandInfo(DRI.d.a);
525 if (LCI.Command->Type == macho::LCT_Segment64)
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000526 return true;
Rafael Espindola77638d92013-04-07 18:08:12 +0000527 assert(LCI.Command->Type == macho::LCT_Segment && "Unexpected Type.");
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000528 return false;
529}
530
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000531const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000532 assert(!is64BitLoadCommand(this, DRI));
533 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000534 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000535 DRI.d.b * sizeof(MachOFormat::Section);
536 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
537 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000538}
539
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000540std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
541 SectionList::const_iterator loc =
542 std::find(Sections.begin(), Sections.end(), Sec);
543 assert(loc != Sections.end() && "Sec is not a valid section!");
544 return std::distance(Sections.begin(), loc);
545}
546
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000547const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000548MachOObjectFile::getSection64(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000549 assert(is64BitLoadCommand(this, DRI));
550 LoadCommandInfo LCI = getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000551 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000552 DRI.d.b * sizeof(MachOFormat::Section64);
553 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
554 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000555}
556
Rafael Espindolacef81b32012-12-21 03:47:03 +0000557static StringRef parseSegmentOrSectionName(const char *P) {
558 if (P[15] == 0)
559 // Null terminated.
560 return P;
561 // Not null terminated, so this is a 16 char string.
562 return StringRef(P, 16);
563}
564
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000565ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000566 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000567 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000568 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000569 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000570 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000571 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000572 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000573}
574
575error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
576 StringRef &Result) const {
577 ArrayRef<char> Raw = getSectionRawName(DRI);
578 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000579 return object_error::success;
580}
581
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000582ArrayRef<char>
583MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000584 if (is64BitLoadCommand(this, Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000585 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000586 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000587 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000588 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000589 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000590 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000591}
592
593StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
594 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
595 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000596}
597
Michael J. Spencer25b15772011-06-25 17:55:23 +0000598error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
599 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000600 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000601 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000602 Result = Sect->Address;
603 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000604 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000605 Result = Sect->Address;
606 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000607 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000608}
609
Michael J. Spencer25b15772011-06-25 17:55:23 +0000610error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
611 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000612 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000613 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000614 Result = Sect->Size;
615 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000616 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000617 Result = Sect->Size;
618 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000619 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000620}
621
Michael J. Spencer25b15772011-06-25 17:55:23 +0000622error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
623 StringRef &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000624 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000625 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000626 Result = MachOObj->getData(Sect->Offset, Sect->Size);
627 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000628 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000629 Result = MachOObj->getData(Sect->Offset, Sect->Size);
630 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000631 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000632}
633
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000634error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
635 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000636 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000637 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000638 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000639 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000640 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000641 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000642 }
643 return object_error::success;
644}
645
Michael J. Spencer25b15772011-06-25 17:55:23 +0000646error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
647 bool &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000648 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000649 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000650 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000651 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000652 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000653 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000654 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000655 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000656}
657
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000658error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
659 bool &Result) const {
660 // FIXME: Unimplemented.
661 Result = false;
662 return object_error::success;
663}
664
665error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
666 bool &Result) const {
667 // FIXME: Unimplemented.
668 Result = false;
669 return object_error::success;
670}
671
Preston Gurdc68dda82012-04-12 20:13:57 +0000672error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
673 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000674 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000675 Result = true;
676 return object_error::success;
677}
678
679error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000680 bool &Result) const {
681 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000682 Result = false;
683 return object_error::success;
684}
685
686error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
687 bool &Result) const {
688 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000689 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000690 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
691 Result = (SectionType == MachO::SectionTypeZeroFill ||
692 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000693 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000694 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000695 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
696 Result = (SectionType == MachO::SectionTypeZeroFill ||
697 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000698 }
699
700 return object_error::success;
701}
702
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000703error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
704 bool &Result) const {
705 // Consider using the code from isSectionText to look for __const sections.
706 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
707 // to use section attributes to distinguish code from data.
708
709 // FIXME: Unimplemented.
710 Result = false;
711 return object_error::success;
712}
713
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000714error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
715 DataRefImpl Symb,
716 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000717 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000718 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000719 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000720 Result = false;
721 return object_error::success;
722 }
723
724 uint64_t SectBegin, SectEnd;
725 getSectionAddress(Sec, SectBegin);
726 getSectionSize(Sec, SectEnd);
727 SectEnd += SectBegin;
728
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000729 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000730 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000731 uint64_t SymAddr= Entry->Value;
732 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000733 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000734 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000735 uint64_t SymAddr= Entry->Value;
736 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000737 }
Owen Andersoncd749882011-10-12 22:21:32 +0000738
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000739 return object_error::success;
740}
741
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000742relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
743 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000744 ret.d.b = getSectionIndex(Sec);
745 return relocation_iterator(RelocationRef(ret, this));
746}
747relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
748 uint32_t last_reloc;
Rafael Espindolaf3051272013-04-07 17:41:59 +0000749 if (is64BitLoadCommand(this, Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000750 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000751 last_reloc = Sect->NumRelocationTableEntries;
752 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000753 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000754 last_reloc = Sect->NumRelocationTableEntries;
755 }
756 DataRefImpl ret;
757 ret.d.a = last_reloc;
758 ret.d.b = getSectionIndex(Sec);
759 return relocation_iterator(RelocationRef(ret, this));
760}
761
762section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000763 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000764 moveToNextSection(DRI);
765 return section_iterator(SectionRef(DRI, this));
766}
767
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000768section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000769 DataRefImpl DRI;
770 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000771 return section_iterator(SectionRef(DRI, this));
772}
773
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000774/*===-- Relocations -------------------------------------------------------===*/
775
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000776const MachOFormat::RelocationEntry *
777MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000778 uint32_t relOffset;
779 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000780 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000781 relOffset = Sect->RelocationTableOffset;
782 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000783 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000784 relOffset = Sect->RelocationTableOffset;
785 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000786 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
787 StringRef Data =
788 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
789 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000790}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000791
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000792error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
793 RelocationRef &Res) const {
794 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000795 Res = RelocationRef(Rel, this);
796 return object_error::success;
797}
798error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
799 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000800 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000801 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000802 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000803 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000804 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000805 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000806 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000807 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000808 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000809
810 unsigned Arch = getArch();
811 bool isScattered = (Arch != Triple::x86_64) &&
812 (RE->Word0 & macho::RF_Scattered);
813 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000814 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000815 RelAddr = RE->Word0 & 0xFFFFFF;
816 else
817 RelAddr = RE->Word0;
818
819 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000820 return object_error::success;
821}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000822error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
823 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000824 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000825
826 unsigned Arch = getArch();
827 bool isScattered = (Arch != Triple::x86_64) &&
828 (RE->Word0 & macho::RF_Scattered);
829 if (isScattered)
830 Res = RE->Word0 & 0xFFFFFF;
831 else
832 Res = RE->Word0;
833 return object_error::success;
834}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000835error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
836 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000837 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000838 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
839 bool isExtern = (RE->Word1 >> 27) & 1;
840
841 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000842 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000843 if (isExtern) {
844 for (unsigned i = 0; i < SymbolIdx; i++) {
845 Sym.d.b++;
846 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000847 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000848 "Relocation symbol index out of range!");
849 }
850 }
851 Res = SymbolRef(Sym, this);
852 return object_error::success;
853}
854error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000855 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000856 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000857 Res = RE->Word0;
858 Res <<= 32;
859 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000860 return object_error::success;
861}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000862error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
863 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000864 // TODO: Support scattered relocations.
865 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000866 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000867
868 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000869 bool isScattered = (Arch != Triple::x86_64) &&
870 (RE->Word0 & macho::RF_Scattered);
871
872 unsigned r_type;
873 if (isScattered)
874 r_type = (RE->Word0 >> 24) & 0xF;
875 else
876 r_type = (RE->Word1 >> 28) & 0xF;
877
Owen Anderson0135fe12011-10-24 21:44:00 +0000878 switch (Arch) {
879 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000880 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000881 "GENERIC_RELOC_VANILLA",
882 "GENERIC_RELOC_PAIR",
883 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000884 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000885 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000886 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000887
Owen Andersoneb6bd332011-10-27 20:46:09 +0000888 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000889 res = "Unknown";
890 else
891 res = Table[r_type];
892 break;
893 }
894 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000895 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000896 "X86_64_RELOC_UNSIGNED",
897 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000898 "X86_64_RELOC_BRANCH",
899 "X86_64_RELOC_GOT_LOAD",
900 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000901 "X86_64_RELOC_SUBTRACTOR",
902 "X86_64_RELOC_SIGNED_1",
903 "X86_64_RELOC_SIGNED_2",
904 "X86_64_RELOC_SIGNED_4",
905 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000906
Owen Andersond8fa76d2011-10-24 23:20:07 +0000907 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000908 res = "Unknown";
909 else
910 res = Table[r_type];
911 break;
912 }
913 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000914 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000915 "ARM_RELOC_VANILLA",
916 "ARM_RELOC_PAIR",
917 "ARM_RELOC_SECTDIFF",
918 "ARM_RELOC_LOCAL_SECTDIFF",
919 "ARM_RELOC_PB_LA_PTR",
920 "ARM_RELOC_BR24",
921 "ARM_THUMB_RELOC_BR22",
922 "ARM_THUMB_32BIT_BRANCH",
923 "ARM_RELOC_HALF",
924 "ARM_RELOC_HALF_SECTDIFF" };
925
926 if (r_type > 9)
927 res = "Unknown";
928 else
929 res = Table[r_type];
930 break;
931 }
932 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000933 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000934 "PPC_RELOC_VANILLA",
935 "PPC_RELOC_PAIR",
936 "PPC_RELOC_BR14",
937 "PPC_RELOC_BR24",
938 "PPC_RELOC_HI16",
939 "PPC_RELOC_LO16",
940 "PPC_RELOC_HA16",
941 "PPC_RELOC_LO14",
942 "PPC_RELOC_SECTDIFF",
943 "PPC_RELOC_PB_LA_PTR",
944 "PPC_RELOC_HI16_SECTDIFF",
945 "PPC_RELOC_LO16_SECTDIFF",
946 "PPC_RELOC_HA16_SECTDIFF",
947 "PPC_RELOC_JBSR",
948 "PPC_RELOC_LO14_SECTDIFF",
949 "PPC_RELOC_LOCAL_SECTDIFF" };
950
951 res = Table[r_type];
952 break;
953 }
954 case Triple::UnknownArch:
955 res = "Unknown";
956 break;
957 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000958 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000959 return object_error::success;
960}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000961error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
962 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000963 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000964 bool isExtern = (RE->Word1 >> 27) & 1;
965 Res = 0;
966 if (!isExtern) {
967 const uint8_t* sectAddress = base();
968 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000969 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000970 sectAddress += Sect->Offset;
971 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000972 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000973 sectAddress += Sect->Offset;
974 }
975 Res = reinterpret_cast<uintptr_t>(sectAddress);
976 }
977 return object_error::success;
978}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000979
980// Helper to advance a section or symbol iterator multiple increments at a time.
981template<class T>
982error_code advance(T &it, size_t Val) {
983 error_code ec;
984 while (Val--) {
985 it.increment(ec);
986 }
987 return ec;
988}
989
990template<class T>
991void advanceTo(T &it, size_t Val) {
992 if (error_code ec = advance(it, Val))
993 report_fatal_error(ec.message());
994}
995
Owen Anderson1832f4d2011-10-26 20:42:54 +0000996void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000997 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000998 raw_string_ostream &fmt) const {
999 unsigned Arch = getArch();
1000 bool isScattered = (Arch != Triple::x86_64) &&
1001 (RE->Word0 & macho::RF_Scattered);
1002
1003 // Target of a scattered relocation is an address. In the interest of
1004 // generating pretty output, scan through the symbol table looking for a
1005 // symbol that aligns with that address. If we find one, print it.
1006 // Otherwise, we just print the hex address of the target.
1007 if (isScattered) {
1008 uint32_t Val = RE->Word1;
1009
1010 error_code ec;
1011 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
1012 SI.increment(ec)) {
1013 if (ec) report_fatal_error(ec.message());
1014
1015 uint64_t Addr;
1016 StringRef Name;
1017
1018 if ((ec = SI->getAddress(Addr)))
1019 report_fatal_error(ec.message());
1020 if (Addr != Val) continue;
1021 if ((ec = SI->getName(Name)))
1022 report_fatal_error(ec.message());
1023 fmt << Name;
1024 return;
1025 }
1026
Owen Andersonb28bdbf2011-10-27 21:53:50 +00001027 // If we couldn't find a symbol that this relocation refers to, try
1028 // to find a section beginning instead.
1029 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
1030 SI.increment(ec)) {
1031 if (ec) report_fatal_error(ec.message());
1032
1033 uint64_t Addr;
1034 StringRef Name;
1035
1036 if ((ec = SI->getAddress(Addr)))
1037 report_fatal_error(ec.message());
1038 if (Addr != Val) continue;
1039 if ((ec = SI->getName(Name)))
1040 report_fatal_error(ec.message());
1041 fmt << Name;
1042 return;
1043 }
1044
Owen Anderson1832f4d2011-10-26 20:42:54 +00001045 fmt << format("0x%x", Val);
1046 return;
1047 }
1048
1049 StringRef S;
1050 bool isExtern = (RE->Word1 >> 27) & 1;
1051 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001052
1053 if (isExtern) {
1054 symbol_iterator SI = begin_symbols();
1055 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001056 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001057 } else {
1058 section_iterator SI = begin_sections();
1059 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001060 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001061 }
1062
Owen Anderson1832f4d2011-10-26 20:42:54 +00001063 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001064}
1065
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001066error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1067 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001068 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +00001069
Owen Anderson1832f4d2011-10-26 20:42:54 +00001070 unsigned Arch = getArch();
1071 bool isScattered = (Arch != Triple::x86_64) &&
1072 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001073
Owen Anderson013d7562011-10-25 18:48:41 +00001074 std::string fmtbuf;
1075 raw_string_ostream fmt(fmtbuf);
1076
Owen Anderson1832f4d2011-10-26 20:42:54 +00001077 unsigned Type;
1078 if (isScattered)
1079 Type = (RE->Word0 >> 24) & 0xF;
1080 else
1081 Type = (RE->Word1 >> 28) & 0xF;
1082
Owen Andersoneb6bd332011-10-27 20:46:09 +00001083 bool isPCRel;
1084 if (isScattered)
1085 isPCRel = ((RE->Word0 >> 30) & 1);
1086 else
1087 isPCRel = ((RE->Word1 >> 24) & 1);
1088
Owen Andersond8fa76d2011-10-24 23:20:07 +00001089 // Determine any addends that should be displayed with the relocation.
1090 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001091
1092 // X86_64 has entirely custom relocation types.
1093 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001094 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001095
Owen Andersond8fa76d2011-10-24 23:20:07 +00001096 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001097 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1098 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1099 printRelocationTargetName(RE, fmt);
1100 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001101 if (isPCRel) fmt << "PCREL";
1102 break;
1103 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001104 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001105 DataRefImpl RelNext = Rel;
1106 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001107 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001108
1109 // X86_64_SUBTRACTOR must be followed by a relocation of type
1110 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001111 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001112 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001113 if (RType != 0)
1114 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1115 "X86_64_RELOC_SUBTRACTOR.");
1116
Owen Andersonef22f782011-10-26 17:28:49 +00001117 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1118 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001119 printRelocationTargetName(RENext, fmt);
1120 fmt << "-";
1121 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001122 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001123 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001124 case macho::RIT_X86_64_TLV:
1125 printRelocationTargetName(RE, fmt);
1126 fmt << "@TLV";
1127 if (isPCRel) fmt << "P";
1128 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001129 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1130 printRelocationTargetName(RE, fmt);
1131 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001132 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001133 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1134 printRelocationTargetName(RE, fmt);
1135 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001136 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001137 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1138 printRelocationTargetName(RE, fmt);
1139 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001140 break;
1141 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001142 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001143 break;
1144 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001145 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001146 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1147 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001148 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001149 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001150 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001151 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001152 DataRefImpl RelNext = Rel;
1153 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001154 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001155
1156 // X86 sect diff's must be followed by a relocation of type
1157 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001158 bool isNextScattered = (Arch != Triple::x86_64) &&
1159 (RENext->Word0 & macho::RF_Scattered);
1160 unsigned RType;
1161 if (isNextScattered)
1162 RType = (RENext->Word0 >> 24) & 0xF;
1163 else
1164 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001165 if (RType != 1)
1166 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001167 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001168
Owen Anderson1832f4d2011-10-26 20:42:54 +00001169 printRelocationTargetName(RE, fmt);
1170 fmt << "-";
1171 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001172 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001173 }
1174 }
Owen Anderson013d7562011-10-25 18:48:41 +00001175
Owen Anderson1832f4d2011-10-26 20:42:54 +00001176 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001177 // All X86 relocations that need special printing were already
1178 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001179 switch (Type) {
1180 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001181 DataRefImpl RelNext = Rel;
1182 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001183 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001184
1185 // X86 sect diff's must be followed by a relocation of type
1186 // GENERIC_RELOC_PAIR.
1187 bool isNextScattered = (Arch != Triple::x86_64) &&
1188 (RENext->Word0 & macho::RF_Scattered);
1189 unsigned RType;
1190 if (isNextScattered)
1191 RType = (RENext->Word0 >> 24) & 0xF;
1192 else
1193 RType = (RENext->Word1 >> 28) & 0xF;
1194 if (RType != 1)
1195 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1196 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1197
1198 printRelocationTargetName(RE, fmt);
1199 fmt << "-";
1200 printRelocationTargetName(RENext, fmt);
1201 break;
1202 }
1203 case macho::RIT_Generic_TLV: {
1204 printRelocationTargetName(RE, fmt);
1205 fmt << "@TLV";
1206 if (isPCRel) fmt << "P";
1207 break;
1208 }
1209 default:
1210 printRelocationTargetName(RE, fmt);
1211 }
Owen Anderson013d7562011-10-25 18:48:41 +00001212 } else { // ARM-specific relocations
1213 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001214 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1215 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001216 // Half relocations steal a bit from the length field to encode
1217 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001218 bool isUpper;
1219 if (isScattered)
1220 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001221 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001222 isUpper = (RE->Word1 >> 25) & 1;
1223
1224 if (isUpper)
1225 fmt << ":upper16:(";
1226 else
1227 fmt << ":lower16:(";
1228 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001229
Owen Anderson013d7562011-10-25 18:48:41 +00001230 DataRefImpl RelNext = Rel;
1231 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001232 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001233
1234 // ARM half relocs must be followed by a relocation of type
1235 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001236 bool isNextScattered = (Arch != Triple::x86_64) &&
1237 (RENext->Word0 & macho::RF_Scattered);
1238 unsigned RType;
1239 if (isNextScattered)
1240 RType = (RENext->Word0 >> 24) & 0xF;
1241 else
1242 RType = (RENext->Word1 >> 28) & 0xF;
1243
Owen Anderson013d7562011-10-25 18:48:41 +00001244 if (RType != 1)
1245 report_fatal_error("Expected ARM_RELOC_PAIR after "
1246 "GENERIC_RELOC_HALF");
1247
Owen Anderson1832f4d2011-10-26 20:42:54 +00001248 // NOTE: The half of the target virtual address is stashed in the
1249 // address field of the secondary relocation, but we can't reverse
1250 // engineer the constant offset from it without decoding the movw/movt
1251 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001252
1253 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1254 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001255 if (Type == macho::RIT_ARM_HalfDifference) {
1256 fmt << "-";
1257 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001258 }
1259
Owen Anderson013d7562011-10-25 18:48:41 +00001260 fmt << ")";
1261 break;
1262 }
1263 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001264 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001265 }
1266 }
1267 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001268 } else
1269 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001270
Owen Anderson0135fe12011-10-24 21:44:00 +00001271 fmt.flush();
1272 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001273 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001274}
1275
Owen Anderson0685e942011-10-25 20:35:53 +00001276error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1277 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001278 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001279
Owen Anderson0685e942011-10-25 20:35:53 +00001280 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001281 bool isScattered = (Arch != Triple::x86_64) &&
1282 (RE->Word0 & macho::RF_Scattered);
1283 unsigned Type;
1284 if (isScattered)
1285 Type = (RE->Word0 >> 24) & 0xF;
1286 else
1287 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001288
1289 Result = false;
1290
1291 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1292 // is always hidden.
1293 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001294 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001295 } else if (Arch == Triple::x86_64) {
1296 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1297 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001298 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001299 DataRefImpl RelPrev = Rel;
1300 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001301 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001302
1303 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1304
Owen Anderson1832f4d2011-10-26 20:42:54 +00001305 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001306 }
1307 }
1308
1309 return object_error::success;
1310}
1311
David Meyer5c2b4ea2012-03-01 01:36:50 +00001312error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1313 LibraryRef &Res) const {
1314 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1315}
1316
1317error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1318 StringRef &Res) const {
1319 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1320}
1321
1322
Eric Christopher6256b032011-04-22 03:19:48 +00001323/*===-- Miscellaneous -----------------------------------------------------===*/
1324
1325uint8_t MachOObjectFile::getBytesInAddress() const {
1326 return MachOObj->is64Bit() ? 8 : 4;
1327}
1328
1329StringRef MachOObjectFile::getFileFormatName() const {
1330 if (!MachOObj->is64Bit()) {
1331 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001332 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001333 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001334 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001335 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001336 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001337 return "Mach-O 32-bit ppc";
1338 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001339 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001340 "64-bit object file when we're not 64-bit?");
1341 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001342 }
1343 }
1344
Eric Christopherb3e6b042013-02-28 20:26:17 +00001345 // Make sure the cpu type has the correct mask.
1346 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1347 == llvm::MachO::CPUArchABI64 &&
1348 "32-bit object file when we're 64-bit?");
1349
Eric Christopher6256b032011-04-22 03:19:48 +00001350 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001351 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001352 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001353 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001354 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001355 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001356 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001357 }
1358}
1359
1360unsigned MachOObjectFile::getArch() const {
1361 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001362 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001363 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001364 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001365 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001366 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001367 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001368 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001369 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001370 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001371 return Triple::ppc64;
1372 default:
1373 return Triple::UnknownArch;
1374 }
1375}
1376
Owen Andersonf7c93a32011-10-11 17:32:27 +00001377} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001378} // end namespace llvm