blob: 002a98cbe6c762fd64df98c0fbb9d7f736f7bff2 [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 Espindola6ab85a82013-04-07 18:42:06 +000061const MachOFormat::LoadCommand *
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
Rafael Espindola6ab85a82013-04-07 18:42:06 +000076 return Load;
Rafael Espindola3eff3182013-04-07 16:07:35 +000077}
78
79void MachOObjectFile::ReadULEB128s(uint64_t Index,
80 SmallVectorImpl<uint64_t> &Out) const {
81 return MachOObj->ReadULEB128s(Index, Out);
82}
83
84const macho::Header &MachOObjectFile::getHeader() const {
85 return MachOObj->getHeader();
86}
87
Eric Christopher6256b032011-04-22 03:19:48 +000088ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000089 error_code ec;
Rafael Espindola6f1f3392013-04-07 16:58:48 +000090 ObjectFile *Ret = new MachOObjectFile(Buffer, ec);
91 if (ec)
Eric Christopher6256b032011-04-22 03:19:48 +000092 return NULL;
Rafael Espindola6f1f3392013-04-07 16:58:48 +000093 return Ret;
Eric Christopher6256b032011-04-22 03:19:48 +000094}
95
96/*===-- Symbols -----------------------------------------------------------===*/
97
98void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
99 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
100 while (DRI.d.a < LoadCommandCount) {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000101 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
102 if (Command->Type == macho::LCT_Symtab) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000103 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000104 reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
Eric Christopher6256b032011-04-22 03:19:48 +0000105 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
106 return;
107 }
108
109 DRI.d.a++;
110 DRI.d.b = 0;
111 }
112}
113
Rafael Espindola00555c12013-04-06 01:59:05 +0000114const MachOFormat::SymbolTableEntry *
115MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000116 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000117 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000118 reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
Eric Christopher6256b032011-04-22 03:19:48 +0000119
Rafael Espindola82a21072013-04-06 03:31:08 +0000120 return getSymbolTableEntry(DRI, SymtabLoadCmd);
121}
Eric Christopher6256b032011-04-22 03:19:48 +0000122
Rafael Espindola82a21072013-04-06 03:31:08 +0000123const MachOFormat::SymbolTableEntry *
124MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
125 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola00555c12013-04-06 01:59:05 +0000126 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
127 unsigned Index = DRI.d.b;
128 uint64_t Offset = (SymbolTableOffset +
129 Index * sizeof(macho::SymbolTableEntry));
130 StringRef Data = MachOObj->getData(Offset,
131 sizeof(MachOFormat::SymbolTableEntry));
132 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000133}
134
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000135const MachOFormat::Symbol64TableEntry*
136MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000137 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000138 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000139 reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000140
Rafael Espindola82a21072013-04-06 03:31:08 +0000141 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
142}
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000143
Rafael Espindola82a21072013-04-06 03:31:08 +0000144const MachOFormat::Symbol64TableEntry*
145MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
146 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000147 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
148 unsigned Index = DRI.d.b;
149 uint64_t Offset = (SymbolTableOffset +
150 Index * sizeof(macho::Symbol64TableEntry));
151 StringRef Data = MachOObj->getData(Offset,
152 sizeof(MachOFormat::Symbol64TableEntry));
153 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000154}
155
Michael J. Spencer25b15772011-06-25 17:55:23 +0000156error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
157 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000158 DRI.d.b++;
159 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000160 Result = SymbolRef(DRI, this);
161 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000162}
163
Michael J. Spencer25b15772011-06-25 17:55:23 +0000164error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
165 StringRef &Result) const {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000166 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000167 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000168 reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
Rafael Espindola82a21072013-04-06 03:31:08 +0000169
170 StringRef StringTable =
171 MachOObj->getData(SymtabLoadCmd->StringTableOffset,
172 SymtabLoadCmd->StringTableSize);
173
174 uint32_t StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000175 if (MachOObj->is64Bit()) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000176 const MachOFormat::Symbol64TableEntry *Entry =
177 getSymbol64TableEntry(DRI, SymtabLoadCmd);
178 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000179 } else {
Rafael Espindola82a21072013-04-06 03:31:08 +0000180 const MachOFormat::SymbolTableEntry *Entry =
181 getSymbolTableEntry(DRI, SymtabLoadCmd);
182 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000183 }
Rafael Espindola82a21072013-04-06 03:31:08 +0000184
185 const char *Start = &StringTable.data()[StringIndex];
186 Result = StringRef(Start);
187
Michael J. Spencer25b15772011-06-25 17:55:23 +0000188 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000189}
190
Danil Malyshevb0436a72011-11-29 17:40:10 +0000191error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
192 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000193 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000194 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000195 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000196 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000197 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000198 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000199 Result += Section->Offset - Section->Address;
200 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000201 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000202 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000203 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000204 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000205 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000206 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000207 Result += Section->Offset - Section->Address;
208 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000209 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000210
Michael J. Spencer25b15772011-06-25 17:55:23 +0000211 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000212}
213
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000214error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
215 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000216 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000217 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000218 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000219 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000220 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000221 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000222 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000223 return object_error::success;
224}
225
Michael J. Spencer25b15772011-06-25 17:55:23 +0000226error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
227 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000228 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
229 uint64_t BeginOffset;
230 uint64_t EndOffset = 0;
231 uint8_t SectionIndex;
232 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000233 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000234 BeginOffset = Entry->Value;
235 SectionIndex = Entry->SectionIndex;
236 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000237 uint32_t flags = SymbolRef::SF_None;
238 getSymbolFlags(DRI, flags);
239 if (flags & SymbolRef::SF_Common)
240 Result = Entry->Value;
241 else
242 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000243 return object_error::success;
244 }
245 // Unfortunately symbols are unsorted so we need to touch all
246 // symbols from load command
247 DRI.d.b = 0;
248 uint32_t Command = DRI.d.a;
249 while (Command == DRI.d.a) {
250 moveToNextSymbol(DRI);
251 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000252 Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000253 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
254 if (!EndOffset || Entry->Value < EndOffset)
255 EndOffset = Entry->Value;
256 }
257 DRI.d.b++;
258 }
259 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000260 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000261 BeginOffset = Entry->Value;
262 SectionIndex = Entry->SectionIndex;
263 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000264 uint32_t flags = SymbolRef::SF_None;
265 getSymbolFlags(DRI, flags);
266 if (flags & SymbolRef::SF_Common)
267 Result = Entry->Value;
268 else
269 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000270 return object_error::success;
271 }
272 // Unfortunately symbols are unsorted so we need to touch all
273 // symbols from load command
274 DRI.d.b = 0;
275 uint32_t Command = DRI.d.a;
276 while (Command == DRI.d.a) {
277 moveToNextSymbol(DRI);
278 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000279 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000280 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
281 if (!EndOffset || Entry->Value < EndOffset)
282 EndOffset = Entry->Value;
283 }
284 DRI.d.b++;
285 }
286 }
287 if (!EndOffset) {
288 uint64_t Size;
289 getSectionSize(Sections[SectionIndex-1], Size);
290 getSectionAddress(Sections[SectionIndex-1], EndOffset);
291 EndOffset += Size;
292 }
293 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000294 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000295}
296
Michael J. Spencer25b15772011-06-25 17:55:23 +0000297error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
298 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000299 uint8_t Type, Flags;
300 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000301 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000302 Type = Entry->Type;
303 Flags = Entry->Flags;
304 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000305 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000306 Type = Entry->Type;
307 Flags = Entry->Flags;
308 }
Eric Christopher6256b032011-04-22 03:19:48 +0000309
310 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000311 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000312 case macho::STT_Undefined:
313 Char = 'u';
314 break;
315 case macho::STT_Absolute:
316 case macho::STT_Section:
317 Char = 's';
318 break;
319 default:
320 Char = '?';
321 break;
322 }
323
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000324 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000325 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000326 Result = Char;
327 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000328}
329
David Meyerc46255a2012-02-28 23:47:53 +0000330error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
331 uint32_t &Result) const {
332 uint16_t MachOFlags;
333 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000334 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000335 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000336 MachOFlags = Entry->Flags;
337 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000338 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000339 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000340 MachOFlags = Entry->Flags;
341 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000342 }
343
Preston Gurdc68dda82012-04-12 20:13:57 +0000344 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000345 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000346
347 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
348 Result |= SymbolRef::SF_Undefined;
349
David Meyerc46255a2012-02-28 23:47:53 +0000350 if (MachOFlags & macho::STF_StabsEntryMask)
351 Result |= SymbolRef::SF_FormatSpecific;
352
Preston Gurdc68dda82012-04-12 20:13:57 +0000353 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000354 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000355 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
356 Result |= SymbolRef::SF_Common;
357 }
David Meyerc46255a2012-02-28 23:47:53 +0000358
359 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
360 Result |= SymbolRef::SF_Weak;
361
362 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
363 Result |= SymbolRef::SF_Absolute;
364
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000365 return object_error::success;
366}
367
368error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
369 section_iterator &Res) const {
370 uint8_t index;
371 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000372 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000373 index = Entry->SectionIndex;
374 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000375 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000376 index = Entry->SectionIndex;
377 }
378
379 if (index == 0)
380 Res = end_sections();
381 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000382 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000383
384 return object_error::success;
385}
386
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000387error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000388 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000389 uint8_t n_type;
390 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000391 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000392 n_type = Entry->Type;
393 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000394 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000395 n_type = Entry->Type;
396 }
397 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000398
399 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000400 if (n_type & MachO::NlistMaskStab) {
401 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000402 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000403 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000404
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000405 switch (n_type & MachO::NlistMaskType) {
406 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000407 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000408 break;
409 case MachO::NListTypeSection :
410 Res = SymbolRef::ST_Function;
411 break;
412 }
413 return object_error::success;
414}
415
Tim Northovera41dce32012-10-29 10:47:00 +0000416error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
417 uint64_t &Val) const {
418 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
419}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000420
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000421symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000422 // DRI.d.a = segment number; DRI.d.b = symbol index.
423 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000424 moveToNextSymbol(DRI);
425 return symbol_iterator(SymbolRef(DRI, this));
426}
427
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000428symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000429 DataRefImpl DRI;
430 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000431 return symbol_iterator(SymbolRef(DRI, this));
432}
433
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000434symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
435 // TODO: implement
436 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
437}
438
439symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
440 // TODO: implement
441 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
442}
Eric Christopher6256b032011-04-22 03:19:48 +0000443
David Meyer5c2b4ea2012-03-01 01:36:50 +0000444library_iterator MachOObjectFile::begin_libraries_needed() const {
445 // TODO: implement
446 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
447}
448
449library_iterator MachOObjectFile::end_libraries_needed() const {
450 // TODO: implement
451 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
452}
453
David Meyer97f77872012-03-01 22:19:54 +0000454StringRef MachOObjectFile::getLoadName() const {
455 // TODO: Implement
456 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
457}
458
Eric Christopher6256b032011-04-22 03:19:48 +0000459/*===-- Sections ----------------------------------------------------------===*/
460
461void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
462 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
463 while (DRI.d.a < LoadCommandCount) {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000464 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
465 if (Command->Type == macho::LCT_Segment) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000466 const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000467 reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Command);
Eric Christopher6256b032011-04-22 03:19:48 +0000468 if (DRI.d.b < SegmentLoadCmd->NumSections)
469 return;
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000470 } else if (Command->Type == macho::LCT_Segment64) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000471 const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000472 reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Command);
Eric Christopher6256b032011-04-22 03:19:48 +0000473 if (DRI.d.b < Segment64LoadCmd->NumSections)
474 return;
475 }
476
477 DRI.d.a++;
478 DRI.d.b = 0;
479 }
480}
481
Michael J. Spencer25b15772011-06-25 17:55:23 +0000482error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
483 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000484 DRI.d.b++;
485 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000486 Result = SectionRef(DRI, this);
487 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000488}
489
Rafael Espindolaf3051272013-04-07 17:41:59 +0000490static bool is64BitLoadCommand(const MachOObjectFile *MachOObj,
491 DataRefImpl DRI) {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000492 const MachOFormat::LoadCommand *Command =
Rafael Espindola77638d92013-04-07 18:08:12 +0000493 MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000494 if (Command->Type == macho::LCT_Segment64)
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000495 return true;
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000496 assert(Command->Type == macho::LCT_Segment && "Unexpected Type.");
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000497 return false;
498}
499
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000500const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000501 assert(!is64BitLoadCommand(this, DRI));
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000502 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
503 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
504 uintptr_t SectionAddr = CommandAddr + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000505 DRI.d.b * sizeof(MachOFormat::Section);
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000506 return reinterpret_cast<const MachOFormat::Section*>(SectionAddr);
Eric Christopher6256b032011-04-22 03:19:48 +0000507}
508
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000509std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
510 SectionList::const_iterator loc =
511 std::find(Sections.begin(), Sections.end(), Sec);
512 assert(loc != Sections.end() && "Sec is not a valid section!");
513 return std::distance(Sections.begin(), loc);
514}
515
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000516const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000517MachOObjectFile::getSection64(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000518 assert(is64BitLoadCommand(this, DRI));
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000519 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
520 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
521 uintptr_t SectionAddr = CommandAddr + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000522 DRI.d.b * sizeof(MachOFormat::Section64);
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000523 return reinterpret_cast<const MachOFormat::Section64*>(SectionAddr);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000524}
525
Rafael Espindolacef81b32012-12-21 03:47:03 +0000526static StringRef parseSegmentOrSectionName(const char *P) {
527 if (P[15] == 0)
528 // Null terminated.
529 return P;
530 // Not null terminated, so this is a 16 char string.
531 return StringRef(P, 16);
532}
533
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000534ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000535 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000536 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000537 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000538 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000539 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000540 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000541 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000542}
543
544error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
545 StringRef &Result) const {
546 ArrayRef<char> Raw = getSectionRawName(DRI);
547 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000548 return object_error::success;
549}
550
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000551ArrayRef<char>
552MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000553 if (is64BitLoadCommand(this, Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000554 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000555 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000556 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000557 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000558 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000559 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000560}
561
562StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
563 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
564 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000565}
566
Michael J. Spencer25b15772011-06-25 17:55:23 +0000567error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
568 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000569 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000570 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000571 Result = Sect->Address;
572 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000573 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000574 Result = Sect->Address;
575 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000576 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000577}
578
Michael J. Spencer25b15772011-06-25 17:55:23 +0000579error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
580 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000581 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000582 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000583 Result = Sect->Size;
584 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000585 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000586 Result = Sect->Size;
587 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000588 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000589}
590
Michael J. Spencer25b15772011-06-25 17:55:23 +0000591error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
592 StringRef &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000593 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000594 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000595 Result = MachOObj->getData(Sect->Offset, Sect->Size);
596 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000597 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000598 Result = MachOObj->getData(Sect->Offset, Sect->Size);
599 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000600 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000601}
602
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000603error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
604 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000605 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000606 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000607 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000608 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000609 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000610 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000611 }
612 return object_error::success;
613}
614
Michael J. Spencer25b15772011-06-25 17:55:23 +0000615error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
616 bool &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000617 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000618 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000619 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000620 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000621 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000622 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000623 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000624 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000625}
626
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000627error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
628 bool &Result) const {
629 // FIXME: Unimplemented.
630 Result = false;
631 return object_error::success;
632}
633
634error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
635 bool &Result) const {
636 // FIXME: Unimplemented.
637 Result = false;
638 return object_error::success;
639}
640
Preston Gurdc68dda82012-04-12 20:13:57 +0000641error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
642 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000643 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000644 Result = true;
645 return object_error::success;
646}
647
648error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000649 bool &Result) const {
650 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000651 Result = false;
652 return object_error::success;
653}
654
655error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
656 bool &Result) const {
657 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000658 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000659 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
660 Result = (SectionType == MachO::SectionTypeZeroFill ||
661 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000662 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000663 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000664 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
665 Result = (SectionType == MachO::SectionTypeZeroFill ||
666 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000667 }
668
669 return object_error::success;
670}
671
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000672error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
673 bool &Result) const {
674 // Consider using the code from isSectionText to look for __const sections.
675 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
676 // to use section attributes to distinguish code from data.
677
678 // FIXME: Unimplemented.
679 Result = false;
680 return object_error::success;
681}
682
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000683error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
684 DataRefImpl Symb,
685 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000686 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000687 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000688 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000689 Result = false;
690 return object_error::success;
691 }
692
693 uint64_t SectBegin, SectEnd;
694 getSectionAddress(Sec, SectBegin);
695 getSectionSize(Sec, SectEnd);
696 SectEnd += SectBegin;
697
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000698 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000699 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000700 uint64_t SymAddr= Entry->Value;
701 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000702 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000703 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000704 uint64_t SymAddr= Entry->Value;
705 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000706 }
Owen Andersoncd749882011-10-12 22:21:32 +0000707
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000708 return object_error::success;
709}
710
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000711relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
712 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000713 ret.d.b = getSectionIndex(Sec);
714 return relocation_iterator(RelocationRef(ret, this));
715}
716relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
717 uint32_t last_reloc;
Rafael Espindolaf3051272013-04-07 17:41:59 +0000718 if (is64BitLoadCommand(this, Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000719 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000720 last_reloc = Sect->NumRelocationTableEntries;
721 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000722 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000723 last_reloc = Sect->NumRelocationTableEntries;
724 }
725 DataRefImpl ret;
726 ret.d.a = last_reloc;
727 ret.d.b = getSectionIndex(Sec);
728 return relocation_iterator(RelocationRef(ret, this));
729}
730
731section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000732 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000733 moveToNextSection(DRI);
734 return section_iterator(SectionRef(DRI, this));
735}
736
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000737section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000738 DataRefImpl DRI;
739 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000740 return section_iterator(SectionRef(DRI, this));
741}
742
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000743/*===-- Relocations -------------------------------------------------------===*/
744
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000745const MachOFormat::RelocationEntry *
746MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000747 uint32_t relOffset;
748 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000749 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000750 relOffset = Sect->RelocationTableOffset;
751 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000752 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000753 relOffset = Sect->RelocationTableOffset;
754 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000755 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
756 StringRef Data =
757 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
758 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000759}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000760
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000761error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
762 RelocationRef &Res) const {
763 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000764 Res = RelocationRef(Rel, this);
765 return object_error::success;
766}
767error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
768 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000769 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000770 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000771 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000772 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000773 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000774 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000775 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000776 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000777 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000778
779 unsigned Arch = getArch();
780 bool isScattered = (Arch != Triple::x86_64) &&
781 (RE->Word0 & macho::RF_Scattered);
782 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000783 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000784 RelAddr = RE->Word0 & 0xFFFFFF;
785 else
786 RelAddr = RE->Word0;
787
788 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000789 return object_error::success;
790}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000791error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
792 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000793 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000794
795 unsigned Arch = getArch();
796 bool isScattered = (Arch != Triple::x86_64) &&
797 (RE->Word0 & macho::RF_Scattered);
798 if (isScattered)
799 Res = RE->Word0 & 0xFFFFFF;
800 else
801 Res = RE->Word0;
802 return object_error::success;
803}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000804error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
805 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000806 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000807 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
808 bool isExtern = (RE->Word1 >> 27) & 1;
809
810 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000811 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000812 if (isExtern) {
813 for (unsigned i = 0; i < SymbolIdx; i++) {
814 Sym.d.b++;
815 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000816 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000817 "Relocation symbol index out of range!");
818 }
819 }
820 Res = SymbolRef(Sym, this);
821 return object_error::success;
822}
823error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000824 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000825 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000826 Res = RE->Word0;
827 Res <<= 32;
828 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000829 return object_error::success;
830}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000831error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
832 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000833 // TODO: Support scattered relocations.
834 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000835 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000836
837 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000838 bool isScattered = (Arch != Triple::x86_64) &&
839 (RE->Word0 & macho::RF_Scattered);
840
841 unsigned r_type;
842 if (isScattered)
843 r_type = (RE->Word0 >> 24) & 0xF;
844 else
845 r_type = (RE->Word1 >> 28) & 0xF;
846
Owen Anderson0135fe12011-10-24 21:44:00 +0000847 switch (Arch) {
848 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000849 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000850 "GENERIC_RELOC_VANILLA",
851 "GENERIC_RELOC_PAIR",
852 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000853 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000854 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000855 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000856
Owen Andersoneb6bd332011-10-27 20:46:09 +0000857 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000858 res = "Unknown";
859 else
860 res = Table[r_type];
861 break;
862 }
863 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000864 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000865 "X86_64_RELOC_UNSIGNED",
866 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000867 "X86_64_RELOC_BRANCH",
868 "X86_64_RELOC_GOT_LOAD",
869 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000870 "X86_64_RELOC_SUBTRACTOR",
871 "X86_64_RELOC_SIGNED_1",
872 "X86_64_RELOC_SIGNED_2",
873 "X86_64_RELOC_SIGNED_4",
874 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000875
Owen Andersond8fa76d2011-10-24 23:20:07 +0000876 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000877 res = "Unknown";
878 else
879 res = Table[r_type];
880 break;
881 }
882 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000883 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000884 "ARM_RELOC_VANILLA",
885 "ARM_RELOC_PAIR",
886 "ARM_RELOC_SECTDIFF",
887 "ARM_RELOC_LOCAL_SECTDIFF",
888 "ARM_RELOC_PB_LA_PTR",
889 "ARM_RELOC_BR24",
890 "ARM_THUMB_RELOC_BR22",
891 "ARM_THUMB_32BIT_BRANCH",
892 "ARM_RELOC_HALF",
893 "ARM_RELOC_HALF_SECTDIFF" };
894
895 if (r_type > 9)
896 res = "Unknown";
897 else
898 res = Table[r_type];
899 break;
900 }
901 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000902 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000903 "PPC_RELOC_VANILLA",
904 "PPC_RELOC_PAIR",
905 "PPC_RELOC_BR14",
906 "PPC_RELOC_BR24",
907 "PPC_RELOC_HI16",
908 "PPC_RELOC_LO16",
909 "PPC_RELOC_HA16",
910 "PPC_RELOC_LO14",
911 "PPC_RELOC_SECTDIFF",
912 "PPC_RELOC_PB_LA_PTR",
913 "PPC_RELOC_HI16_SECTDIFF",
914 "PPC_RELOC_LO16_SECTDIFF",
915 "PPC_RELOC_HA16_SECTDIFF",
916 "PPC_RELOC_JBSR",
917 "PPC_RELOC_LO14_SECTDIFF",
918 "PPC_RELOC_LOCAL_SECTDIFF" };
919
920 res = Table[r_type];
921 break;
922 }
923 case Triple::UnknownArch:
924 res = "Unknown";
925 break;
926 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000927 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000928 return object_error::success;
929}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000930error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
931 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000932 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000933 bool isExtern = (RE->Word1 >> 27) & 1;
934 Res = 0;
935 if (!isExtern) {
936 const uint8_t* sectAddress = base();
937 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000938 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000939 sectAddress += Sect->Offset;
940 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000941 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000942 sectAddress += Sect->Offset;
943 }
944 Res = reinterpret_cast<uintptr_t>(sectAddress);
945 }
946 return object_error::success;
947}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000948
949// Helper to advance a section or symbol iterator multiple increments at a time.
950template<class T>
951error_code advance(T &it, size_t Val) {
952 error_code ec;
953 while (Val--) {
954 it.increment(ec);
955 }
956 return ec;
957}
958
959template<class T>
960void advanceTo(T &it, size_t Val) {
961 if (error_code ec = advance(it, Val))
962 report_fatal_error(ec.message());
963}
964
Owen Anderson1832f4d2011-10-26 20:42:54 +0000965void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000966 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000967 raw_string_ostream &fmt) const {
968 unsigned Arch = getArch();
969 bool isScattered = (Arch != Triple::x86_64) &&
970 (RE->Word0 & macho::RF_Scattered);
971
972 // Target of a scattered relocation is an address. In the interest of
973 // generating pretty output, scan through the symbol table looking for a
974 // symbol that aligns with that address. If we find one, print it.
975 // Otherwise, we just print the hex address of the target.
976 if (isScattered) {
977 uint32_t Val = RE->Word1;
978
979 error_code ec;
980 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
981 SI.increment(ec)) {
982 if (ec) report_fatal_error(ec.message());
983
984 uint64_t Addr;
985 StringRef Name;
986
987 if ((ec = SI->getAddress(Addr)))
988 report_fatal_error(ec.message());
989 if (Addr != Val) continue;
990 if ((ec = SI->getName(Name)))
991 report_fatal_error(ec.message());
992 fmt << Name;
993 return;
994 }
995
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000996 // If we couldn't find a symbol that this relocation refers to, try
997 // to find a section beginning instead.
998 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
999 SI.increment(ec)) {
1000 if (ec) report_fatal_error(ec.message());
1001
1002 uint64_t Addr;
1003 StringRef Name;
1004
1005 if ((ec = SI->getAddress(Addr)))
1006 report_fatal_error(ec.message());
1007 if (Addr != Val) continue;
1008 if ((ec = SI->getName(Name)))
1009 report_fatal_error(ec.message());
1010 fmt << Name;
1011 return;
1012 }
1013
Owen Anderson1832f4d2011-10-26 20:42:54 +00001014 fmt << format("0x%x", Val);
1015 return;
1016 }
1017
1018 StringRef S;
1019 bool isExtern = (RE->Word1 >> 27) & 1;
1020 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001021
1022 if (isExtern) {
1023 symbol_iterator SI = begin_symbols();
1024 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001025 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001026 } else {
1027 section_iterator SI = begin_sections();
1028 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001029 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001030 }
1031
Owen Anderson1832f4d2011-10-26 20:42:54 +00001032 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001033}
1034
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001035error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1036 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001037 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +00001038
Owen Anderson1832f4d2011-10-26 20:42:54 +00001039 unsigned Arch = getArch();
1040 bool isScattered = (Arch != Triple::x86_64) &&
1041 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001042
Owen Anderson013d7562011-10-25 18:48:41 +00001043 std::string fmtbuf;
1044 raw_string_ostream fmt(fmtbuf);
1045
Owen Anderson1832f4d2011-10-26 20:42:54 +00001046 unsigned Type;
1047 if (isScattered)
1048 Type = (RE->Word0 >> 24) & 0xF;
1049 else
1050 Type = (RE->Word1 >> 28) & 0xF;
1051
Owen Andersoneb6bd332011-10-27 20:46:09 +00001052 bool isPCRel;
1053 if (isScattered)
1054 isPCRel = ((RE->Word0 >> 30) & 1);
1055 else
1056 isPCRel = ((RE->Word1 >> 24) & 1);
1057
Owen Andersond8fa76d2011-10-24 23:20:07 +00001058 // Determine any addends that should be displayed with the relocation.
1059 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001060
1061 // X86_64 has entirely custom relocation types.
1062 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001063 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001064
Owen Andersond8fa76d2011-10-24 23:20:07 +00001065 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001066 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1067 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1068 printRelocationTargetName(RE, fmt);
1069 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001070 if (isPCRel) fmt << "PCREL";
1071 break;
1072 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001073 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001074 DataRefImpl RelNext = Rel;
1075 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001076 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001077
1078 // X86_64_SUBTRACTOR must be followed by a relocation of type
1079 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001080 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001081 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001082 if (RType != 0)
1083 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1084 "X86_64_RELOC_SUBTRACTOR.");
1085
Owen Andersonef22f782011-10-26 17:28:49 +00001086 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1087 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001088 printRelocationTargetName(RENext, fmt);
1089 fmt << "-";
1090 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001091 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001092 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001093 case macho::RIT_X86_64_TLV:
1094 printRelocationTargetName(RE, fmt);
1095 fmt << "@TLV";
1096 if (isPCRel) fmt << "P";
1097 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001098 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1099 printRelocationTargetName(RE, fmt);
1100 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001101 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001102 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1103 printRelocationTargetName(RE, fmt);
1104 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001105 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001106 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1107 printRelocationTargetName(RE, fmt);
1108 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001109 break;
1110 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001111 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001112 break;
1113 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001114 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001115 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1116 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001117 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001118 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001119 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001120 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001121 DataRefImpl RelNext = Rel;
1122 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001123 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001124
1125 // X86 sect diff's must be followed by a relocation of type
1126 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001127 bool isNextScattered = (Arch != Triple::x86_64) &&
1128 (RENext->Word0 & macho::RF_Scattered);
1129 unsigned RType;
1130 if (isNextScattered)
1131 RType = (RENext->Word0 >> 24) & 0xF;
1132 else
1133 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001134 if (RType != 1)
1135 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001136 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001137
Owen Anderson1832f4d2011-10-26 20:42:54 +00001138 printRelocationTargetName(RE, fmt);
1139 fmt << "-";
1140 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001141 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001142 }
1143 }
Owen Anderson013d7562011-10-25 18:48:41 +00001144
Owen Anderson1832f4d2011-10-26 20:42:54 +00001145 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001146 // All X86 relocations that need special printing were already
1147 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001148 switch (Type) {
1149 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001150 DataRefImpl RelNext = Rel;
1151 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001152 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001153
1154 // X86 sect diff's must be followed by a relocation of type
1155 // GENERIC_RELOC_PAIR.
1156 bool isNextScattered = (Arch != Triple::x86_64) &&
1157 (RENext->Word0 & macho::RF_Scattered);
1158 unsigned RType;
1159 if (isNextScattered)
1160 RType = (RENext->Word0 >> 24) & 0xF;
1161 else
1162 RType = (RENext->Word1 >> 28) & 0xF;
1163 if (RType != 1)
1164 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1165 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1166
1167 printRelocationTargetName(RE, fmt);
1168 fmt << "-";
1169 printRelocationTargetName(RENext, fmt);
1170 break;
1171 }
1172 case macho::RIT_Generic_TLV: {
1173 printRelocationTargetName(RE, fmt);
1174 fmt << "@TLV";
1175 if (isPCRel) fmt << "P";
1176 break;
1177 }
1178 default:
1179 printRelocationTargetName(RE, fmt);
1180 }
Owen Anderson013d7562011-10-25 18:48:41 +00001181 } else { // ARM-specific relocations
1182 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001183 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1184 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001185 // Half relocations steal a bit from the length field to encode
1186 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001187 bool isUpper;
1188 if (isScattered)
1189 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001190 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001191 isUpper = (RE->Word1 >> 25) & 1;
1192
1193 if (isUpper)
1194 fmt << ":upper16:(";
1195 else
1196 fmt << ":lower16:(";
1197 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001198
Owen Anderson013d7562011-10-25 18:48:41 +00001199 DataRefImpl RelNext = Rel;
1200 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001201 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001202
1203 // ARM half relocs must be followed by a relocation of type
1204 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001205 bool isNextScattered = (Arch != Triple::x86_64) &&
1206 (RENext->Word0 & macho::RF_Scattered);
1207 unsigned RType;
1208 if (isNextScattered)
1209 RType = (RENext->Word0 >> 24) & 0xF;
1210 else
1211 RType = (RENext->Word1 >> 28) & 0xF;
1212
Owen Anderson013d7562011-10-25 18:48:41 +00001213 if (RType != 1)
1214 report_fatal_error("Expected ARM_RELOC_PAIR after "
1215 "GENERIC_RELOC_HALF");
1216
Owen Anderson1832f4d2011-10-26 20:42:54 +00001217 // NOTE: The half of the target virtual address is stashed in the
1218 // address field of the secondary relocation, but we can't reverse
1219 // engineer the constant offset from it without decoding the movw/movt
1220 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001221
1222 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1223 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001224 if (Type == macho::RIT_ARM_HalfDifference) {
1225 fmt << "-";
1226 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001227 }
1228
Owen Anderson013d7562011-10-25 18:48:41 +00001229 fmt << ")";
1230 break;
1231 }
1232 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001233 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001234 }
1235 }
1236 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001237 } else
1238 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001239
Owen Anderson0135fe12011-10-24 21:44:00 +00001240 fmt.flush();
1241 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001242 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001243}
1244
Owen Anderson0685e942011-10-25 20:35:53 +00001245error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1246 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001247 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001248
Owen Anderson0685e942011-10-25 20:35:53 +00001249 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001250 bool isScattered = (Arch != Triple::x86_64) &&
1251 (RE->Word0 & macho::RF_Scattered);
1252 unsigned Type;
1253 if (isScattered)
1254 Type = (RE->Word0 >> 24) & 0xF;
1255 else
1256 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001257
1258 Result = false;
1259
1260 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1261 // is always hidden.
1262 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001263 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001264 } else if (Arch == Triple::x86_64) {
1265 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1266 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001267 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001268 DataRefImpl RelPrev = Rel;
1269 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001270 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001271
1272 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1273
Owen Anderson1832f4d2011-10-26 20:42:54 +00001274 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001275 }
1276 }
1277
1278 return object_error::success;
1279}
1280
David Meyer5c2b4ea2012-03-01 01:36:50 +00001281error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1282 LibraryRef &Res) const {
1283 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1284}
1285
1286error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1287 StringRef &Res) const {
1288 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1289}
1290
1291
Eric Christopher6256b032011-04-22 03:19:48 +00001292/*===-- Miscellaneous -----------------------------------------------------===*/
1293
1294uint8_t MachOObjectFile::getBytesInAddress() const {
1295 return MachOObj->is64Bit() ? 8 : 4;
1296}
1297
1298StringRef MachOObjectFile::getFileFormatName() const {
1299 if (!MachOObj->is64Bit()) {
1300 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001301 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001302 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001303 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001304 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001305 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001306 return "Mach-O 32-bit ppc";
1307 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001308 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001309 "64-bit object file when we're not 64-bit?");
1310 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001311 }
1312 }
1313
Eric Christopherb3e6b042013-02-28 20:26:17 +00001314 // Make sure the cpu type has the correct mask.
1315 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1316 == llvm::MachO::CPUArchABI64 &&
1317 "32-bit object file when we're 64-bit?");
1318
Eric Christopher6256b032011-04-22 03:19:48 +00001319 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001320 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001321 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001322 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001323 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001324 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001325 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001326 }
1327}
1328
1329unsigned MachOObjectFile::getArch() const {
1330 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001331 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001332 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001333 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001334 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001335 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001336 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001337 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001338 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001339 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001340 return Triple::ppc64;
1341 default:
1342 return Triple::UnknownArch;
1343 }
1344}
1345
Owen Andersonf7c93a32011-10-11 17:32:27 +00001346} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001347} // end namespace llvm