blob: 75735418cba83b0abaccf7c38d20f905c48d0d7f [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);
Rafael Espindola433611b2013-04-07 19:26:57 +000049 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000050 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 {
Rafael Espindola49698a12013-04-07 19:38:15 +000058 StringRef Magic = getData(0, 4);
59 return (Magic == "\xFE\xED\xFA\xCF") || (Magic == "\xCF\xFA\xED\xFE");
Rafael Espindola0be4eaf2013-04-07 15:46:05 +000060}
Benjamin Kramer0fcab072011-09-08 20:52:17 +000061
Rafael Espindola6ab85a82013-04-07 18:42:06 +000062const MachOFormat::LoadCommand *
Rafael Espindola3eff3182013-04-07 16:07:35 +000063MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
Rafael Espindola77638d92013-04-07 18:08:12 +000064 uint64_t Offset;
Rafael Espindola0f08eb12013-04-07 19:05:30 +000065 uint64_t NewOffset = getHeaderSize();
Rafael Espindola77638d92013-04-07 18:08:12 +000066 const MachOFormat::LoadCommand *Load;
67 unsigned I = 0;
68 do {
69 Offset = NewOffset;
Rafael Espindola0f08eb12013-04-07 19:05:30 +000070 StringRef Data = getData(Offset, sizeof(MachOFormat::LoadCommand));
Rafael Espindola77638d92013-04-07 18:08:12 +000071 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
Rafael Espindola433611b2013-04-07 19:26:57 +000084const MachOFormat::Header *MachOObjectFile::getHeader() const {
85 StringRef Data = getData(0, sizeof(MachOFormat::Header));
86 return reinterpret_cast<const MachOFormat::Header*>(Data.data());
Rafael Espindola3eff3182013-04-07 16:07:35 +000087}
88
Rafael Espindola0f08eb12013-04-07 19:05:30 +000089unsigned MachOObjectFile::getHeaderSize() const {
Rafael Espindolac90cc182013-04-07 19:31:49 +000090 return is64Bit() ? macho::Header64Size : macho::Header32Size;
Rafael Espindola0f08eb12013-04-07 19:05:30 +000091}
92
93StringRef MachOObjectFile::getData(size_t Offset, size_t Size) const {
Rafael Espindolaf1cc8002013-04-07 19:42:15 +000094 return ObjectFile::getData().substr(Offset, Size);
Rafael Espindola0f08eb12013-04-07 19:05:30 +000095}
96
Eric Christopher6256b032011-04-22 03:19:48 +000097ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000098 error_code ec;
Rafael Espindola6f1f3392013-04-07 16:58:48 +000099 ObjectFile *Ret = new MachOObjectFile(Buffer, ec);
100 if (ec)
Eric Christopher6256b032011-04-22 03:19:48 +0000101 return NULL;
Rafael Espindola6f1f3392013-04-07 16:58:48 +0000102 return Ret;
Eric Christopher6256b032011-04-22 03:19:48 +0000103}
104
105/*===-- Symbols -----------------------------------------------------------===*/
106
107void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
Rafael Espindola433611b2013-04-07 19:26:57 +0000108 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000109 while (DRI.d.a < LoadCommandCount) {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000110 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
111 if (Command->Type == macho::LCT_Symtab) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000112 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000113 reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
Eric Christopher6256b032011-04-22 03:19:48 +0000114 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
115 return;
116 }
117
118 DRI.d.a++;
119 DRI.d.b = 0;
120 }
121}
122
Rafael Espindola00555c12013-04-06 01:59:05 +0000123const MachOFormat::SymbolTableEntry *
124MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000125 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000126 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000127 reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
Eric Christopher6256b032011-04-22 03:19:48 +0000128
Rafael Espindola82a21072013-04-06 03:31:08 +0000129 return getSymbolTableEntry(DRI, SymtabLoadCmd);
130}
Eric Christopher6256b032011-04-22 03:19:48 +0000131
Rafael Espindola82a21072013-04-06 03:31:08 +0000132const MachOFormat::SymbolTableEntry *
133MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
134 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola00555c12013-04-06 01:59:05 +0000135 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
136 unsigned Index = DRI.d.b;
137 uint64_t Offset = (SymbolTableOffset +
138 Index * sizeof(macho::SymbolTableEntry));
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000139 StringRef Data = getData(Offset, sizeof(MachOFormat::SymbolTableEntry));
Rafael Espindola00555c12013-04-06 01:59:05 +0000140 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000141}
142
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000143const MachOFormat::Symbol64TableEntry*
144MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000145 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000146 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000147 reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000148
Rafael Espindola82a21072013-04-06 03:31:08 +0000149 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
150}
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000151
Rafael Espindola82a21072013-04-06 03:31:08 +0000152const MachOFormat::Symbol64TableEntry*
153MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
154 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000155 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
156 unsigned Index = DRI.d.b;
157 uint64_t Offset = (SymbolTableOffset +
158 Index * sizeof(macho::Symbol64TableEntry));
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000159 StringRef Data = getData(Offset, sizeof(MachOFormat::Symbol64TableEntry));
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000160 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000161}
162
Michael J. Spencer25b15772011-06-25 17:55:23 +0000163error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
164 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000165 DRI.d.b++;
166 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000167 Result = SymbolRef(DRI, this);
168 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000169}
170
Michael J. Spencer25b15772011-06-25 17:55:23 +0000171error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
172 StringRef &Result) const {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000173 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000174 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000175 reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
Rafael Espindola82a21072013-04-06 03:31:08 +0000176
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000177 StringRef StringTable = getData(SymtabLoadCmd->StringTableOffset,
178 SymtabLoadCmd->StringTableSize);
Rafael Espindola82a21072013-04-06 03:31:08 +0000179
180 uint32_t StringIndex;
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000181 if (is64Bit()) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000182 const MachOFormat::Symbol64TableEntry *Entry =
183 getSymbol64TableEntry(DRI, SymtabLoadCmd);
184 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000185 } else {
Rafael Espindola82a21072013-04-06 03:31:08 +0000186 const MachOFormat::SymbolTableEntry *Entry =
187 getSymbolTableEntry(DRI, SymtabLoadCmd);
188 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000189 }
Rafael Espindola82a21072013-04-06 03:31:08 +0000190
191 const char *Start = &StringTable.data()[StringIndex];
192 Result = StringRef(Start);
193
Michael J. Spencer25b15772011-06-25 17:55:23 +0000194 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000195}
196
Danil Malyshevb0436a72011-11-29 17:40:10 +0000197error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
198 uint64_t &Result) const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000199 if (is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000200 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000201 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000202 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000203 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000204 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000205 Result += Section->Offset - Section->Address;
206 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000207 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000208 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000209 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000210 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000211 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000212 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000213 Result += Section->Offset - Section->Address;
214 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000215 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000216
Michael J. Spencer25b15772011-06-25 17:55:23 +0000217 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000218}
219
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000220error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
221 uint64_t &Result) const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000222 if (is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000223 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000224 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000225 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000226 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000227 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000228 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000229 return object_error::success;
230}
231
Michael J. Spencer25b15772011-06-25 17:55:23 +0000232error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
233 uint64_t &Result) const {
Rafael Espindola433611b2013-04-07 19:26:57 +0000234 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000235 uint64_t BeginOffset;
236 uint64_t EndOffset = 0;
237 uint8_t SectionIndex;
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000238 if (is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000239 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000240 BeginOffset = Entry->Value;
241 SectionIndex = Entry->SectionIndex;
242 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000243 uint32_t flags = SymbolRef::SF_None;
244 getSymbolFlags(DRI, flags);
245 if (flags & SymbolRef::SF_Common)
246 Result = Entry->Value;
247 else
248 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000249 return object_error::success;
250 }
251 // Unfortunately symbols are unsorted so we need to touch all
252 // symbols from load command
253 DRI.d.b = 0;
254 uint32_t Command = DRI.d.a;
255 while (Command == DRI.d.a) {
256 moveToNextSymbol(DRI);
257 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000258 Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000259 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
260 if (!EndOffset || Entry->Value < EndOffset)
261 EndOffset = Entry->Value;
262 }
263 DRI.d.b++;
264 }
265 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000266 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000267 BeginOffset = Entry->Value;
268 SectionIndex = Entry->SectionIndex;
269 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000270 uint32_t flags = SymbolRef::SF_None;
271 getSymbolFlags(DRI, flags);
272 if (flags & SymbolRef::SF_Common)
273 Result = Entry->Value;
274 else
275 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000276 return object_error::success;
277 }
278 // Unfortunately symbols are unsorted so we need to touch all
279 // symbols from load command
280 DRI.d.b = 0;
281 uint32_t Command = DRI.d.a;
282 while (Command == DRI.d.a) {
283 moveToNextSymbol(DRI);
284 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000285 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000286 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
287 if (!EndOffset || Entry->Value < EndOffset)
288 EndOffset = Entry->Value;
289 }
290 DRI.d.b++;
291 }
292 }
293 if (!EndOffset) {
294 uint64_t Size;
295 getSectionSize(Sections[SectionIndex-1], Size);
296 getSectionAddress(Sections[SectionIndex-1], EndOffset);
297 EndOffset += Size;
298 }
299 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000300 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000301}
302
Michael J. Spencer25b15772011-06-25 17:55:23 +0000303error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
304 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000305 uint8_t Type, Flags;
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000306 if (is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000307 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000308 Type = Entry->Type;
309 Flags = Entry->Flags;
310 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000311 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000312 Type = Entry->Type;
313 Flags = Entry->Flags;
314 }
Eric Christopher6256b032011-04-22 03:19:48 +0000315
316 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000317 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000318 case macho::STT_Undefined:
319 Char = 'u';
320 break;
321 case macho::STT_Absolute:
322 case macho::STT_Section:
323 Char = 's';
324 break;
325 default:
326 Char = '?';
327 break;
328 }
329
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000330 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000331 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000332 Result = Char;
333 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000334}
335
David Meyerc46255a2012-02-28 23:47:53 +0000336error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
337 uint32_t &Result) const {
338 uint16_t MachOFlags;
339 uint8_t MachOType;
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000340 if (is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000341 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000342 MachOFlags = Entry->Flags;
343 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000344 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000345 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000346 MachOFlags = Entry->Flags;
347 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000348 }
349
Preston Gurdc68dda82012-04-12 20:13:57 +0000350 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000351 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000352
353 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
354 Result |= SymbolRef::SF_Undefined;
355
David Meyerc46255a2012-02-28 23:47:53 +0000356 if (MachOFlags & macho::STF_StabsEntryMask)
357 Result |= SymbolRef::SF_FormatSpecific;
358
Preston Gurdc68dda82012-04-12 20:13:57 +0000359 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000360 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000361 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
362 Result |= SymbolRef::SF_Common;
363 }
David Meyerc46255a2012-02-28 23:47:53 +0000364
365 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
366 Result |= SymbolRef::SF_Weak;
367
368 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
369 Result |= SymbolRef::SF_Absolute;
370
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000371 return object_error::success;
372}
373
374error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
375 section_iterator &Res) const {
376 uint8_t index;
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000377 if (is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000378 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000379 index = Entry->SectionIndex;
380 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000381 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000382 index = Entry->SectionIndex;
383 }
384
385 if (index == 0)
386 Res = end_sections();
387 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000388 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000389
390 return object_error::success;
391}
392
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000393error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000394 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000395 uint8_t n_type;
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000396 if (is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000397 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000398 n_type = Entry->Type;
399 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000400 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000401 n_type = Entry->Type;
402 }
403 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000404
405 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000406 if (n_type & MachO::NlistMaskStab) {
407 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000408 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000409 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000410
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000411 switch (n_type & MachO::NlistMaskType) {
412 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000413 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000414 break;
415 case MachO::NListTypeSection :
416 Res = SymbolRef::ST_Function;
417 break;
418 }
419 return object_error::success;
420}
421
Tim Northovera41dce32012-10-29 10:47:00 +0000422error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
423 uint64_t &Val) const {
424 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
425}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000426
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000427symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000428 // DRI.d.a = segment number; DRI.d.b = symbol index.
429 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000430 moveToNextSymbol(DRI);
431 return symbol_iterator(SymbolRef(DRI, this));
432}
433
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000434symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000435 DataRefImpl DRI;
Rafael Espindola433611b2013-04-07 19:26:57 +0000436 DRI.d.a = getHeader()->NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000437 return symbol_iterator(SymbolRef(DRI, this));
438}
439
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000440symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
441 // TODO: implement
442 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
443}
444
445symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
446 // TODO: implement
447 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
448}
Eric Christopher6256b032011-04-22 03:19:48 +0000449
David Meyer5c2b4ea2012-03-01 01:36:50 +0000450library_iterator MachOObjectFile::begin_libraries_needed() const {
451 // TODO: implement
452 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
453}
454
455library_iterator MachOObjectFile::end_libraries_needed() const {
456 // TODO: implement
457 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
458}
459
David Meyer97f77872012-03-01 22:19:54 +0000460StringRef MachOObjectFile::getLoadName() const {
461 // TODO: Implement
462 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
463}
464
Eric Christopher6256b032011-04-22 03:19:48 +0000465/*===-- Sections ----------------------------------------------------------===*/
466
467void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
Rafael Espindola433611b2013-04-07 19:26:57 +0000468 uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000469 while (DRI.d.a < LoadCommandCount) {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000470 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
471 if (Command->Type == macho::LCT_Segment) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000472 const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000473 reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Command);
Eric Christopher6256b032011-04-22 03:19:48 +0000474 if (DRI.d.b < SegmentLoadCmd->NumSections)
475 return;
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000476 } else if (Command->Type == macho::LCT_Segment64) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000477 const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000478 reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Command);
Eric Christopher6256b032011-04-22 03:19:48 +0000479 if (DRI.d.b < Segment64LoadCmd->NumSections)
480 return;
481 }
482
483 DRI.d.a++;
484 DRI.d.b = 0;
485 }
486}
487
Michael J. Spencer25b15772011-06-25 17:55:23 +0000488error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
489 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000490 DRI.d.b++;
491 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000492 Result = SectionRef(DRI, this);
493 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000494}
495
Rafael Espindolaf3051272013-04-07 17:41:59 +0000496static bool is64BitLoadCommand(const MachOObjectFile *MachOObj,
497 DataRefImpl DRI) {
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000498 const MachOFormat::LoadCommand *Command =
Rafael Espindola77638d92013-04-07 18:08:12 +0000499 MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000500 if (Command->Type == macho::LCT_Segment64)
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000501 return true;
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000502 assert(Command->Type == macho::LCT_Segment && "Unexpected Type.");
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000503 return false;
504}
505
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000506const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000507 assert(!is64BitLoadCommand(this, DRI));
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000508 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
509 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
510 uintptr_t SectionAddr = CommandAddr + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000511 DRI.d.b * sizeof(MachOFormat::Section);
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000512 return reinterpret_cast<const MachOFormat::Section*>(SectionAddr);
Eric Christopher6256b032011-04-22 03:19:48 +0000513}
514
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000515std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
516 SectionList::const_iterator loc =
517 std::find(Sections.begin(), Sections.end(), Sec);
518 assert(loc != Sections.end() && "Sec is not a valid section!");
519 return std::distance(Sections.begin(), loc);
520}
521
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000522const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000523MachOObjectFile::getSection64(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000524 assert(is64BitLoadCommand(this, DRI));
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000525 const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
526 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
527 uintptr_t SectionAddr = CommandAddr + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000528 DRI.d.b * sizeof(MachOFormat::Section64);
Rafael Espindola6ab85a82013-04-07 18:42:06 +0000529 return reinterpret_cast<const MachOFormat::Section64*>(SectionAddr);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000530}
531
Rafael Espindolacef81b32012-12-21 03:47:03 +0000532static StringRef parseSegmentOrSectionName(const char *P) {
533 if (P[15] == 0)
534 // Null terminated.
535 return P;
536 // Not null terminated, so this is a 16 char string.
537 return StringRef(P, 16);
538}
539
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000540ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000541 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000542 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000543 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000544 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000545 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000546 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000547 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000548}
549
550error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
551 StringRef &Result) const {
552 ArrayRef<char> Raw = getSectionRawName(DRI);
553 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000554 return object_error::success;
555}
556
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000557ArrayRef<char>
558MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000559 if (is64BitLoadCommand(this, Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000560 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000561 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000562 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000563 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000564 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000565 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000566}
567
568StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
569 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
570 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000571}
572
Michael J. Spencer25b15772011-06-25 17:55:23 +0000573error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
574 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000575 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000576 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000577 Result = Sect->Address;
578 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000579 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000580 Result = Sect->Address;
581 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000582 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000583}
584
Michael J. Spencer25b15772011-06-25 17:55:23 +0000585error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
586 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000587 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000588 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000589 Result = Sect->Size;
590 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000591 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000592 Result = Sect->Size;
593 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000594 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000595}
596
Michael J. Spencer25b15772011-06-25 17:55:23 +0000597error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
598 StringRef &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000599 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000600 const MachOFormat::Section64 *Sect = getSection64(DRI);
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000601 Result = getData(Sect->Offset, Sect->Size);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000602 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000603 const MachOFormat::Section *Sect = getSection(DRI);
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000604 Result = getData(Sect->Offset, Sect->Size);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000605 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000606 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000607}
608
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000609error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
610 uint64_t &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000611 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000612 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000613 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000614 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000615 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000616 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000617 }
618 return object_error::success;
619}
620
Michael J. Spencer25b15772011-06-25 17:55:23 +0000621error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
622 bool &Result) const {
Rafael Espindolaf3051272013-04-07 17:41:59 +0000623 if (is64BitLoadCommand(this, DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000624 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000625 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000626 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000627 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000628 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000629 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000630 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000631}
632
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000633error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
634 bool &Result) const {
635 // FIXME: Unimplemented.
636 Result = false;
637 return object_error::success;
638}
639
640error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
641 bool &Result) const {
642 // FIXME: Unimplemented.
643 Result = false;
644 return object_error::success;
645}
646
Preston Gurdc68dda82012-04-12 20:13:57 +0000647error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
648 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000649 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000650 Result = true;
651 return object_error::success;
652}
653
654error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000655 bool &Result) const {
656 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000657 Result = false;
658 return object_error::success;
659}
660
661error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
662 bool &Result) const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000663 if (is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000664 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000665 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
666 Result = (SectionType == MachO::SectionTypeZeroFill ||
667 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000668 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000669 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000670 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
671 Result = (SectionType == MachO::SectionTypeZeroFill ||
672 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000673 }
674
675 return object_error::success;
676}
677
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000678error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
679 bool &Result) const {
680 // Consider using the code from isSectionText to look for __const sections.
681 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
682 // to use section attributes to distinguish code from data.
683
684 // FIXME: Unimplemented.
685 Result = false;
686 return object_error::success;
687}
688
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000689error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
690 DataRefImpl Symb,
691 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000692 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000693 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000694 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000695 Result = false;
696 return object_error::success;
697 }
698
699 uint64_t SectBegin, SectEnd;
700 getSectionAddress(Sec, SectBegin);
701 getSectionSize(Sec, SectEnd);
702 SectEnd += SectBegin;
703
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000704 if (is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000705 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000706 uint64_t SymAddr= Entry->Value;
707 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000708 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000709 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000710 uint64_t SymAddr= Entry->Value;
711 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000712 }
Owen Andersoncd749882011-10-12 22:21:32 +0000713
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000714 return object_error::success;
715}
716
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000717relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
718 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000719 ret.d.b = getSectionIndex(Sec);
720 return relocation_iterator(RelocationRef(ret, this));
721}
722relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
723 uint32_t last_reloc;
Rafael Espindolaf3051272013-04-07 17:41:59 +0000724 if (is64BitLoadCommand(this, Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000725 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000726 last_reloc = Sect->NumRelocationTableEntries;
727 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000728 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000729 last_reloc = Sect->NumRelocationTableEntries;
730 }
731 DataRefImpl ret;
732 ret.d.a = last_reloc;
733 ret.d.b = getSectionIndex(Sec);
734 return relocation_iterator(RelocationRef(ret, this));
735}
736
737section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000738 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000739 moveToNextSection(DRI);
740 return section_iterator(SectionRef(DRI, this));
741}
742
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000743section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000744 DataRefImpl DRI;
Rafael Espindola433611b2013-04-07 19:26:57 +0000745 DRI.d.a = getHeader()->NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000746 return section_iterator(SectionRef(DRI, this));
747}
748
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000749/*===-- Relocations -------------------------------------------------------===*/
750
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000751const MachOFormat::RelocationEntry *
752MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000753 uint32_t relOffset;
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000754 if (is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000755 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000756 relOffset = Sect->RelocationTableOffset;
757 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000758 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000759 relOffset = Sect->RelocationTableOffset;
760 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000761 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000762 StringRef Data = getData(Offset, sizeof(MachOFormat::RelocationEntry));
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000763 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000764}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000765
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000766error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
767 RelocationRef &Res) const {
768 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000769 Res = RelocationRef(Rel, this);
770 return object_error::success;
771}
772error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
773 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000774 const uint8_t* sectAddress = 0;
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000775 if (is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000776 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000777 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000778 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000779 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000780 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000781 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000782 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000783
784 unsigned Arch = getArch();
785 bool isScattered = (Arch != Triple::x86_64) &&
786 (RE->Word0 & macho::RF_Scattered);
787 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000788 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000789 RelAddr = RE->Word0 & 0xFFFFFF;
790 else
791 RelAddr = RE->Word0;
792
793 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000794 return object_error::success;
795}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000796error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
797 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000798 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000799
800 unsigned Arch = getArch();
801 bool isScattered = (Arch != Triple::x86_64) &&
802 (RE->Word0 & macho::RF_Scattered);
803 if (isScattered)
804 Res = RE->Word0 & 0xFFFFFF;
805 else
806 Res = RE->Word0;
807 return object_error::success;
808}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000809error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
810 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000811 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000812 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
813 bool isExtern = (RE->Word1 >> 27) & 1;
814
815 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000816 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000817 if (isExtern) {
818 for (unsigned i = 0; i < SymbolIdx; i++) {
819 Sym.d.b++;
820 moveToNextSymbol(Sym);
Rafael Espindola433611b2013-04-07 19:26:57 +0000821 assert(Sym.d.a < getHeader()->NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000822 "Relocation symbol index out of range!");
823 }
824 }
825 Res = SymbolRef(Sym, this);
826 return object_error::success;
827}
828error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000829 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000830 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000831 Res = RE->Word0;
832 Res <<= 32;
833 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000834 return object_error::success;
835}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000836error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
837 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000838 // TODO: Support scattered relocations.
839 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000840 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000841
842 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000843 bool isScattered = (Arch != Triple::x86_64) &&
844 (RE->Word0 & macho::RF_Scattered);
845
846 unsigned r_type;
847 if (isScattered)
848 r_type = (RE->Word0 >> 24) & 0xF;
849 else
850 r_type = (RE->Word1 >> 28) & 0xF;
851
Owen Anderson0135fe12011-10-24 21:44:00 +0000852 switch (Arch) {
853 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000854 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000855 "GENERIC_RELOC_VANILLA",
856 "GENERIC_RELOC_PAIR",
857 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000858 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000859 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000860 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000861
Owen Andersoneb6bd332011-10-27 20:46:09 +0000862 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000863 res = "Unknown";
864 else
865 res = Table[r_type];
866 break;
867 }
868 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000869 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000870 "X86_64_RELOC_UNSIGNED",
871 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000872 "X86_64_RELOC_BRANCH",
873 "X86_64_RELOC_GOT_LOAD",
874 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000875 "X86_64_RELOC_SUBTRACTOR",
876 "X86_64_RELOC_SIGNED_1",
877 "X86_64_RELOC_SIGNED_2",
878 "X86_64_RELOC_SIGNED_4",
879 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000880
Owen Andersond8fa76d2011-10-24 23:20:07 +0000881 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000882 res = "Unknown";
883 else
884 res = Table[r_type];
885 break;
886 }
887 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000888 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000889 "ARM_RELOC_VANILLA",
890 "ARM_RELOC_PAIR",
891 "ARM_RELOC_SECTDIFF",
892 "ARM_RELOC_LOCAL_SECTDIFF",
893 "ARM_RELOC_PB_LA_PTR",
894 "ARM_RELOC_BR24",
895 "ARM_THUMB_RELOC_BR22",
896 "ARM_THUMB_32BIT_BRANCH",
897 "ARM_RELOC_HALF",
898 "ARM_RELOC_HALF_SECTDIFF" };
899
900 if (r_type > 9)
901 res = "Unknown";
902 else
903 res = Table[r_type];
904 break;
905 }
906 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000907 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000908 "PPC_RELOC_VANILLA",
909 "PPC_RELOC_PAIR",
910 "PPC_RELOC_BR14",
911 "PPC_RELOC_BR24",
912 "PPC_RELOC_HI16",
913 "PPC_RELOC_LO16",
914 "PPC_RELOC_HA16",
915 "PPC_RELOC_LO14",
916 "PPC_RELOC_SECTDIFF",
917 "PPC_RELOC_PB_LA_PTR",
918 "PPC_RELOC_HI16_SECTDIFF",
919 "PPC_RELOC_LO16_SECTDIFF",
920 "PPC_RELOC_HA16_SECTDIFF",
921 "PPC_RELOC_JBSR",
922 "PPC_RELOC_LO14_SECTDIFF",
923 "PPC_RELOC_LOCAL_SECTDIFF" };
924
925 res = Table[r_type];
926 break;
927 }
928 case Triple::UnknownArch:
929 res = "Unknown";
930 break;
931 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000932 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000933 return object_error::success;
934}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000935error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
936 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000937 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000938 bool isExtern = (RE->Word1 >> 27) & 1;
939 Res = 0;
940 if (!isExtern) {
941 const uint8_t* sectAddress = base();
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000942 if (is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000943 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000944 sectAddress += Sect->Offset;
945 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000946 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000947 sectAddress += Sect->Offset;
948 }
949 Res = reinterpret_cast<uintptr_t>(sectAddress);
950 }
951 return object_error::success;
952}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000953
954// Helper to advance a section or symbol iterator multiple increments at a time.
955template<class T>
956error_code advance(T &it, size_t Val) {
957 error_code ec;
958 while (Val--) {
959 it.increment(ec);
960 }
961 return ec;
962}
963
964template<class T>
965void advanceTo(T &it, size_t Val) {
966 if (error_code ec = advance(it, Val))
967 report_fatal_error(ec.message());
968}
969
Owen Anderson1832f4d2011-10-26 20:42:54 +0000970void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000971 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000972 raw_string_ostream &fmt) const {
973 unsigned Arch = getArch();
974 bool isScattered = (Arch != Triple::x86_64) &&
975 (RE->Word0 & macho::RF_Scattered);
976
977 // Target of a scattered relocation is an address. In the interest of
978 // generating pretty output, scan through the symbol table looking for a
979 // symbol that aligns with that address. If we find one, print it.
980 // Otherwise, we just print the hex address of the target.
981 if (isScattered) {
982 uint32_t Val = RE->Word1;
983
984 error_code ec;
985 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
986 SI.increment(ec)) {
987 if (ec) report_fatal_error(ec.message());
988
989 uint64_t Addr;
990 StringRef Name;
991
992 if ((ec = SI->getAddress(Addr)))
993 report_fatal_error(ec.message());
994 if (Addr != Val) continue;
995 if ((ec = SI->getName(Name)))
996 report_fatal_error(ec.message());
997 fmt << Name;
998 return;
999 }
1000
Owen Andersonb28bdbf2011-10-27 21:53:50 +00001001 // If we couldn't find a symbol that this relocation refers to, try
1002 // to find a section beginning instead.
1003 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
1004 SI.increment(ec)) {
1005 if (ec) report_fatal_error(ec.message());
1006
1007 uint64_t Addr;
1008 StringRef Name;
1009
1010 if ((ec = SI->getAddress(Addr)))
1011 report_fatal_error(ec.message());
1012 if (Addr != Val) continue;
1013 if ((ec = SI->getName(Name)))
1014 report_fatal_error(ec.message());
1015 fmt << Name;
1016 return;
1017 }
1018
Owen Anderson1832f4d2011-10-26 20:42:54 +00001019 fmt << format("0x%x", Val);
1020 return;
1021 }
1022
1023 StringRef S;
1024 bool isExtern = (RE->Word1 >> 27) & 1;
1025 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001026
1027 if (isExtern) {
1028 symbol_iterator SI = begin_symbols();
1029 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001030 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001031 } else {
1032 section_iterator SI = begin_sections();
1033 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001034 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001035 }
1036
Owen Anderson1832f4d2011-10-26 20:42:54 +00001037 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001038}
1039
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001040error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1041 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001042 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +00001043
Owen Anderson1832f4d2011-10-26 20:42:54 +00001044 unsigned Arch = getArch();
1045 bool isScattered = (Arch != Triple::x86_64) &&
1046 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001047
Owen Anderson013d7562011-10-25 18:48:41 +00001048 std::string fmtbuf;
1049 raw_string_ostream fmt(fmtbuf);
1050
Owen Anderson1832f4d2011-10-26 20:42:54 +00001051 unsigned Type;
1052 if (isScattered)
1053 Type = (RE->Word0 >> 24) & 0xF;
1054 else
1055 Type = (RE->Word1 >> 28) & 0xF;
1056
Owen Andersoneb6bd332011-10-27 20:46:09 +00001057 bool isPCRel;
1058 if (isScattered)
1059 isPCRel = ((RE->Word0 >> 30) & 1);
1060 else
1061 isPCRel = ((RE->Word1 >> 24) & 1);
1062
Owen Andersond8fa76d2011-10-24 23:20:07 +00001063 // Determine any addends that should be displayed with the relocation.
1064 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001065
1066 // X86_64 has entirely custom relocation types.
1067 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001068 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001069
Owen Andersond8fa76d2011-10-24 23:20:07 +00001070 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001071 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1072 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1073 printRelocationTargetName(RE, fmt);
1074 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001075 if (isPCRel) fmt << "PCREL";
1076 break;
1077 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001078 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001079 DataRefImpl RelNext = Rel;
1080 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001081 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001082
1083 // X86_64_SUBTRACTOR must be followed by a relocation of type
1084 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001085 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001086 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001087 if (RType != 0)
1088 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1089 "X86_64_RELOC_SUBTRACTOR.");
1090
Owen Andersonef22f782011-10-26 17:28:49 +00001091 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1092 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001093 printRelocationTargetName(RENext, fmt);
1094 fmt << "-";
1095 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001096 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001097 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001098 case macho::RIT_X86_64_TLV:
1099 printRelocationTargetName(RE, fmt);
1100 fmt << "@TLV";
1101 if (isPCRel) fmt << "P";
1102 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001103 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1104 printRelocationTargetName(RE, fmt);
1105 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001106 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001107 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1108 printRelocationTargetName(RE, fmt);
1109 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001110 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001111 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1112 printRelocationTargetName(RE, fmt);
1113 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001114 break;
1115 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001116 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001117 break;
1118 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001119 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001120 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1121 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001122 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001123 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001124 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001125 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001126 DataRefImpl RelNext = Rel;
1127 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001128 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001129
1130 // X86 sect diff's must be followed by a relocation of type
1131 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001132 bool isNextScattered = (Arch != Triple::x86_64) &&
1133 (RENext->Word0 & macho::RF_Scattered);
1134 unsigned RType;
1135 if (isNextScattered)
1136 RType = (RENext->Word0 >> 24) & 0xF;
1137 else
1138 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001139 if (RType != 1)
1140 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001141 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001142
Owen Anderson1832f4d2011-10-26 20:42:54 +00001143 printRelocationTargetName(RE, fmt);
1144 fmt << "-";
1145 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001146 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001147 }
1148 }
Owen Anderson013d7562011-10-25 18:48:41 +00001149
Owen Anderson1832f4d2011-10-26 20:42:54 +00001150 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001151 // All X86 relocations that need special printing were already
1152 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001153 switch (Type) {
1154 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001155 DataRefImpl RelNext = Rel;
1156 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001157 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001158
1159 // X86 sect diff's must be followed by a relocation of type
1160 // GENERIC_RELOC_PAIR.
1161 bool isNextScattered = (Arch != Triple::x86_64) &&
1162 (RENext->Word0 & macho::RF_Scattered);
1163 unsigned RType;
1164 if (isNextScattered)
1165 RType = (RENext->Word0 >> 24) & 0xF;
1166 else
1167 RType = (RENext->Word1 >> 28) & 0xF;
1168 if (RType != 1)
1169 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1170 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1171
1172 printRelocationTargetName(RE, fmt);
1173 fmt << "-";
1174 printRelocationTargetName(RENext, fmt);
1175 break;
1176 }
1177 case macho::RIT_Generic_TLV: {
1178 printRelocationTargetName(RE, fmt);
1179 fmt << "@TLV";
1180 if (isPCRel) fmt << "P";
1181 break;
1182 }
1183 default:
1184 printRelocationTargetName(RE, fmt);
1185 }
Owen Anderson013d7562011-10-25 18:48:41 +00001186 } else { // ARM-specific relocations
1187 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001188 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1189 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001190 // Half relocations steal a bit from the length field to encode
1191 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001192 bool isUpper;
1193 if (isScattered)
1194 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001195 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001196 isUpper = (RE->Word1 >> 25) & 1;
1197
1198 if (isUpper)
1199 fmt << ":upper16:(";
1200 else
1201 fmt << ":lower16:(";
1202 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001203
Owen Anderson013d7562011-10-25 18:48:41 +00001204 DataRefImpl RelNext = Rel;
1205 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001206 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001207
1208 // ARM half relocs must be followed by a relocation of type
1209 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001210 bool isNextScattered = (Arch != Triple::x86_64) &&
1211 (RENext->Word0 & macho::RF_Scattered);
1212 unsigned RType;
1213 if (isNextScattered)
1214 RType = (RENext->Word0 >> 24) & 0xF;
1215 else
1216 RType = (RENext->Word1 >> 28) & 0xF;
1217
Owen Anderson013d7562011-10-25 18:48:41 +00001218 if (RType != 1)
1219 report_fatal_error("Expected ARM_RELOC_PAIR after "
1220 "GENERIC_RELOC_HALF");
1221
Owen Anderson1832f4d2011-10-26 20:42:54 +00001222 // NOTE: The half of the target virtual address is stashed in the
1223 // address field of the secondary relocation, but we can't reverse
1224 // engineer the constant offset from it without decoding the movw/movt
1225 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001226
1227 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1228 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001229 if (Type == macho::RIT_ARM_HalfDifference) {
1230 fmt << "-";
1231 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001232 }
1233
Owen Anderson013d7562011-10-25 18:48:41 +00001234 fmt << ")";
1235 break;
1236 }
1237 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001238 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001239 }
1240 }
1241 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001242 } else
1243 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001244
Owen Anderson0135fe12011-10-24 21:44:00 +00001245 fmt.flush();
1246 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001247 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001248}
1249
Owen Anderson0685e942011-10-25 20:35:53 +00001250error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1251 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001252 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001253
Owen Anderson0685e942011-10-25 20:35:53 +00001254 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001255 bool isScattered = (Arch != Triple::x86_64) &&
1256 (RE->Word0 & macho::RF_Scattered);
1257 unsigned Type;
1258 if (isScattered)
1259 Type = (RE->Word0 >> 24) & 0xF;
1260 else
1261 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001262
1263 Result = false;
1264
1265 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1266 // is always hidden.
1267 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001268 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001269 } else if (Arch == Triple::x86_64) {
1270 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1271 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001272 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001273 DataRefImpl RelPrev = Rel;
1274 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001275 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001276
1277 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1278
Owen Anderson1832f4d2011-10-26 20:42:54 +00001279 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001280 }
1281 }
1282
1283 return object_error::success;
1284}
1285
David Meyer5c2b4ea2012-03-01 01:36:50 +00001286error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1287 LibraryRef &Res) const {
1288 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1289}
1290
1291error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1292 StringRef &Res) const {
1293 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1294}
1295
1296
Eric Christopher6256b032011-04-22 03:19:48 +00001297/*===-- Miscellaneous -----------------------------------------------------===*/
1298
1299uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +00001300 return is64Bit() ? 8 : 4;
Eric Christopher6256b032011-04-22 03:19:48 +00001301}
1302
1303StringRef MachOObjectFile::getFileFormatName() const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +00001304 if (!is64Bit()) {
Rafael Espindola433611b2013-04-07 19:26:57 +00001305 switch (getHeader()->CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001306 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001307 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001308 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001309 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001310 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001311 return "Mach-O 32-bit ppc";
1312 default:
Rafael Espindola433611b2013-04-07 19:26:57 +00001313 assert((getHeader()->CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001314 "64-bit object file when we're not 64-bit?");
1315 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001316 }
1317 }
1318
Eric Christopherb3e6b042013-02-28 20:26:17 +00001319 // Make sure the cpu type has the correct mask.
Rafael Espindola433611b2013-04-07 19:26:57 +00001320 assert((getHeader()->CPUType & llvm::MachO::CPUArchABI64)
Eric Christopherb3e6b042013-02-28 20:26:17 +00001321 == llvm::MachO::CPUArchABI64 &&
1322 "32-bit object file when we're 64-bit?");
1323
Rafael Espindola433611b2013-04-07 19:26:57 +00001324 switch (getHeader()->CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001325 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001326 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001327 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001328 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001329 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001330 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001331 }
1332}
1333
1334unsigned MachOObjectFile::getArch() const {
Rafael Espindola433611b2013-04-07 19:26:57 +00001335 switch (getHeader()->CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001336 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001337 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001338 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001339 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001340 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001341 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001342 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001343 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001344 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001345 return Triple::ppc64;
1346 default:
1347 return Triple::UnknownArch;
1348 }
1349}
1350
Owen Andersonf7c93a32011-10-11 17:32:27 +00001351} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001352} // end namespace llvm