blob: 6f888faf9c9f58abc13276afc28b117948f3dd6d [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
Benjamin Kramer0fcab072011-09-08 20:52:17 +000030MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
31 error_code &ec)
Rafael Espindola2c6f9972013-04-07 16:40:00 +000032 : ObjectFile(Binary::ID_MachO, Object),
Rafael Espindola82a21072013-04-06 03:31:08 +000033 MachOObj(MOO) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +000034 DataRefImpl DRI;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000035 moveToNextSection(DRI);
36 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
37 while (DRI.d.a < LoadCommandCount) {
38 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000039 DRI.d.b++;
40 moveToNextSection(DRI);
41 }
42}
43
Rafael Espindola0be4eaf2013-04-07 15:46:05 +000044bool MachOObjectFile::is64Bit() const {
45 return MachOObj->is64Bit();
46}
Benjamin Kramer0fcab072011-09-08 20:52:17 +000047
Rafael Espindola3eff3182013-04-07 16:07:35 +000048const LoadCommandInfo &
49MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
50 return MachOObj->getLoadCommandInfo(Index);
51}
52
53void MachOObjectFile::ReadULEB128s(uint64_t Index,
54 SmallVectorImpl<uint64_t> &Out) const {
55 return MachOObj->ReadULEB128s(Index, Out);
56}
57
58const macho::Header &MachOObjectFile::getHeader() const {
59 return MachOObj->getHeader();
60}
61
Eric Christopher6256b032011-04-22 03:19:48 +000062ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000063 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000064 std::string Err;
65 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
66 if (!MachOObj)
67 return NULL;
Jim Grosbach596e4742012-11-29 19:14:11 +000068 // MachOObject takes ownership of the Buffer we passed to it, and
69 // MachOObjectFile does, too, so we need to make sure they don't get the
70 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
71 // not a copy of the memory itself), so just make a new copy here for
72 // the MachOObjectFile.
73 MemoryBuffer *NewBuffer =
Benjamin Kramerf56c3e22012-11-29 20:08:03 +000074 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
75 Buffer->getBufferIdentifier(), false);
Jim Grosbach596e4742012-11-29 19:14:11 +000076 return new MachOObjectFile(NewBuffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000077}
78
79/*===-- Symbols -----------------------------------------------------------===*/
80
Rafael Espindola82a21072013-04-06 03:31:08 +000081const MachOFormat::SymtabLoadCommand *
82MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
83 StringRef Data = MachOObj->getData(LCI.Offset,
84 sizeof(MachOFormat::SymtabLoadCommand));
85 return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
86}
87
Rafael Espindola68d287d2013-04-06 03:50:05 +000088const MachOFormat::SegmentLoadCommand *
89MachOObjectFile::getSegmentLoadCommand(LoadCommandInfo LCI) const {
90 StringRef Data = MachOObj->getData(LCI.Offset,
91 sizeof(MachOFormat::SegmentLoadCommand));
92 return reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Data.data());
93}
94
Rafael Espindola196abbf2013-04-07 14:40:18 +000095const MachOFormat::LinkeditDataLoadCommand *
96MachOObjectFile::getLinkeditDataLoadCommand(LoadCommandInfo LCI) const {
97 StringRef Data = MachOObj->getData(LCI.Offset,
98 sizeof(MachOFormat::LinkeditDataLoadCommand));
99 return
100 reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Data.data());
101}
102
Rafael Espindola68d287d2013-04-06 03:50:05 +0000103const MachOFormat::Segment64LoadCommand *
104MachOObjectFile::getSegment64LoadCommand(LoadCommandInfo LCI) const {
105 StringRef Data = MachOObj->getData(LCI.Offset,
106 sizeof(MachOFormat::Segment64LoadCommand));
107 return
108 reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Data.data());
109}
110
Eric Christopher6256b032011-04-22 03:19:48 +0000111void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
112 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
113 while (DRI.d.a < LoadCommandCount) {
114 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
115 if (LCI.Command.Type == macho::LCT_Symtab) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000116 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
117 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000118 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
119 return;
120 }
121
122 DRI.d.a++;
123 DRI.d.b = 0;
124 }
125}
126
Rafael Espindola00555c12013-04-06 01:59:05 +0000127const MachOFormat::SymbolTableEntry *
128MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000129 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000130 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
131 getSymtabLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000132
Rafael Espindola82a21072013-04-06 03:31:08 +0000133 return getSymbolTableEntry(DRI, SymtabLoadCmd);
134}
Eric Christopher6256b032011-04-22 03:19:48 +0000135
Rafael Espindola82a21072013-04-06 03:31:08 +0000136const MachOFormat::SymbolTableEntry *
137MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
138 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola00555c12013-04-06 01:59:05 +0000139 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
140 unsigned Index = DRI.d.b;
141 uint64_t Offset = (SymbolTableOffset +
142 Index * sizeof(macho::SymbolTableEntry));
143 StringRef Data = MachOObj->getData(Offset,
144 sizeof(MachOFormat::SymbolTableEntry));
145 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000146}
147
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000148const MachOFormat::Symbol64TableEntry*
149MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000150 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindola82a21072013-04-06 03:31:08 +0000151 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
152 getSymtabLoadCommand(LCI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000153
Rafael Espindola82a21072013-04-06 03:31:08 +0000154 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
155}
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000156
Rafael Espindola82a21072013-04-06 03:31:08 +0000157const MachOFormat::Symbol64TableEntry*
158MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
159 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000160 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
161 unsigned Index = DRI.d.b;
162 uint64_t Offset = (SymbolTableOffset +
163 Index * sizeof(macho::Symbol64TableEntry));
164 StringRef Data = MachOObj->getData(Offset,
165 sizeof(MachOFormat::Symbol64TableEntry));
166 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000167}
168
Michael J. Spencer25b15772011-06-25 17:55:23 +0000169error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
170 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000171 DRI.d.b++;
172 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000173 Result = SymbolRef(DRI, this);
174 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000175}
176
Michael J. Spencer25b15772011-06-25 17:55:23 +0000177error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
178 StringRef &Result) const {
Rafael Espindola82a21072013-04-06 03:31:08 +0000179 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
180 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
181 getSymtabLoadCommand(LCI);
182
183 StringRef StringTable =
184 MachOObj->getData(SymtabLoadCmd->StringTableOffset,
185 SymtabLoadCmd->StringTableSize);
186
187 uint32_t StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000188 if (MachOObj->is64Bit()) {
Rafael Espindola82a21072013-04-06 03:31:08 +0000189 const MachOFormat::Symbol64TableEntry *Entry =
190 getSymbol64TableEntry(DRI, SymtabLoadCmd);
191 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000192 } else {
Rafael Espindola82a21072013-04-06 03:31:08 +0000193 const MachOFormat::SymbolTableEntry *Entry =
194 getSymbolTableEntry(DRI, SymtabLoadCmd);
195 StringIndex = Entry->StringIndex;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000196 }
Rafael Espindola82a21072013-04-06 03:31:08 +0000197
198 const char *Start = &StringTable.data()[StringIndex];
199 Result = StringRef(Start);
200
Michael J. Spencer25b15772011-06-25 17:55:23 +0000201 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000202}
203
Danil Malyshevb0436a72011-11-29 17:40:10 +0000204error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
205 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000206 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000207 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000208 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000209 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000210 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000211 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000212 Result += Section->Offset - Section->Address;
213 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000214 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000215 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000216 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000217 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000218 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000219 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000220 Result += Section->Offset - Section->Address;
221 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000222 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000223
Michael J. Spencer25b15772011-06-25 17:55:23 +0000224 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000225}
226
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000227error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
228 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000229 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000230 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000231 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000232 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000233 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000234 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000235 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000236 return object_error::success;
237}
238
Michael J. Spencer25b15772011-06-25 17:55:23 +0000239error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
240 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000241 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
242 uint64_t BeginOffset;
243 uint64_t EndOffset = 0;
244 uint8_t SectionIndex;
245 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000246 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000247 BeginOffset = Entry->Value;
248 SectionIndex = Entry->SectionIndex;
249 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000250 uint32_t flags = SymbolRef::SF_None;
251 getSymbolFlags(DRI, flags);
252 if (flags & SymbolRef::SF_Common)
253 Result = Entry->Value;
254 else
255 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000256 return object_error::success;
257 }
258 // Unfortunately symbols are unsorted so we need to touch all
259 // symbols from load command
260 DRI.d.b = 0;
261 uint32_t Command = DRI.d.a;
262 while (Command == DRI.d.a) {
263 moveToNextSymbol(DRI);
264 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000265 Entry = getSymbol64TableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000266 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
267 if (!EndOffset || Entry->Value < EndOffset)
268 EndOffset = Entry->Value;
269 }
270 DRI.d.b++;
271 }
272 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000273 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000274 BeginOffset = Entry->Value;
275 SectionIndex = Entry->SectionIndex;
276 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000277 uint32_t flags = SymbolRef::SF_None;
278 getSymbolFlags(DRI, flags);
279 if (flags & SymbolRef::SF_Common)
280 Result = Entry->Value;
281 else
282 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000283 return object_error::success;
284 }
285 // Unfortunately symbols are unsorted so we need to touch all
286 // symbols from load command
287 DRI.d.b = 0;
288 uint32_t Command = DRI.d.a;
289 while (Command == DRI.d.a) {
290 moveToNextSymbol(DRI);
291 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000292 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000293 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
294 if (!EndOffset || Entry->Value < EndOffset)
295 EndOffset = Entry->Value;
296 }
297 DRI.d.b++;
298 }
299 }
300 if (!EndOffset) {
301 uint64_t Size;
302 getSectionSize(Sections[SectionIndex-1], Size);
303 getSectionAddress(Sections[SectionIndex-1], EndOffset);
304 EndOffset += Size;
305 }
306 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000307 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000308}
309
Michael J. Spencer25b15772011-06-25 17:55:23 +0000310error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
311 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000312 uint8_t Type, Flags;
313 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000314 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000315 Type = Entry->Type;
316 Flags = Entry->Flags;
317 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000318 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000319 Type = Entry->Type;
320 Flags = Entry->Flags;
321 }
Eric Christopher6256b032011-04-22 03:19:48 +0000322
323 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000324 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000325 case macho::STT_Undefined:
326 Char = 'u';
327 break;
328 case macho::STT_Absolute:
329 case macho::STT_Section:
330 Char = 's';
331 break;
332 default:
333 Char = '?';
334 break;
335 }
336
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000337 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000338 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000339 Result = Char;
340 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000341}
342
David Meyerc46255a2012-02-28 23:47:53 +0000343error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
344 uint32_t &Result) const {
345 uint16_t MachOFlags;
346 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000347 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000348 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000349 MachOFlags = Entry->Flags;
350 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000351 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000352 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000353 MachOFlags = Entry->Flags;
354 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000355 }
356
Preston Gurdc68dda82012-04-12 20:13:57 +0000357 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000358 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000359
360 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
361 Result |= SymbolRef::SF_Undefined;
362
David Meyerc46255a2012-02-28 23:47:53 +0000363 if (MachOFlags & macho::STF_StabsEntryMask)
364 Result |= SymbolRef::SF_FormatSpecific;
365
Preston Gurdc68dda82012-04-12 20:13:57 +0000366 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000367 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000368 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
369 Result |= SymbolRef::SF_Common;
370 }
David Meyerc46255a2012-02-28 23:47:53 +0000371
372 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
373 Result |= SymbolRef::SF_Weak;
374
375 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
376 Result |= SymbolRef::SF_Absolute;
377
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000378 return object_error::success;
379}
380
381error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
382 section_iterator &Res) const {
383 uint8_t index;
384 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000385 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000386 index = Entry->SectionIndex;
387 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000388 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000389 index = Entry->SectionIndex;
390 }
391
392 if (index == 0)
393 Res = end_sections();
394 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000395 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000396
397 return object_error::success;
398}
399
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000400error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000401 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000402 uint8_t n_type;
403 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000404 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000405 n_type = Entry->Type;
406 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000407 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000408 n_type = Entry->Type;
409 }
410 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000411
412 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000413 if (n_type & MachO::NlistMaskStab) {
414 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000415 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000416 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000417
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000418 switch (n_type & MachO::NlistMaskType) {
419 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000420 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000421 break;
422 case MachO::NListTypeSection :
423 Res = SymbolRef::ST_Function;
424 break;
425 }
426 return object_error::success;
427}
428
Tim Northovera41dce32012-10-29 10:47:00 +0000429error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
430 uint64_t &Val) const {
431 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
432}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000433
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000434symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000435 // DRI.d.a = segment number; DRI.d.b = symbol index.
436 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000437 moveToNextSymbol(DRI);
438 return symbol_iterator(SymbolRef(DRI, this));
439}
440
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000441symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000442 DataRefImpl DRI;
443 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000444 return symbol_iterator(SymbolRef(DRI, this));
445}
446
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000447symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
448 // TODO: implement
449 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
450}
451
452symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
453 // TODO: implement
454 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
455}
Eric Christopher6256b032011-04-22 03:19:48 +0000456
David Meyer5c2b4ea2012-03-01 01:36:50 +0000457library_iterator MachOObjectFile::begin_libraries_needed() const {
458 // TODO: implement
459 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
460}
461
462library_iterator MachOObjectFile::end_libraries_needed() const {
463 // TODO: implement
464 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
465}
466
David Meyer97f77872012-03-01 22:19:54 +0000467StringRef MachOObjectFile::getLoadName() const {
468 // TODO: Implement
469 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
470}
471
Eric Christopher6256b032011-04-22 03:19:48 +0000472/*===-- Sections ----------------------------------------------------------===*/
473
474void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
475 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
476 while (DRI.d.a < LoadCommandCount) {
477 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
478 if (LCI.Command.Type == macho::LCT_Segment) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000479 const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
480 getSegmentLoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000481 if (DRI.d.b < SegmentLoadCmd->NumSections)
482 return;
483 } else if (LCI.Command.Type == macho::LCT_Segment64) {
Rafael Espindola68d287d2013-04-06 03:50:05 +0000484 const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
485 getSegment64LoadCommand(LCI);
Eric Christopher6256b032011-04-22 03:19:48 +0000486 if (DRI.d.b < Segment64LoadCmd->NumSections)
487 return;
488 }
489
490 DRI.d.a++;
491 DRI.d.b = 0;
492 }
493}
494
Michael J. Spencer25b15772011-06-25 17:55:23 +0000495error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
496 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000497 DRI.d.b++;
498 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000499 Result = SectionRef(DRI, this);
500 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000501}
502
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000503static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
Eric Christopher6256b032011-04-22 03:19:48 +0000504 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000505 if (LCI.Command.Type == macho::LCT_Segment64)
506 return true;
507 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
508 return false;
509}
510
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000511const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000512 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
513 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
514 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000515 DRI.d.b * sizeof(MachOFormat::Section);
516 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
517 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000518}
519
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000520std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
521 SectionList::const_iterator loc =
522 std::find(Sections.begin(), Sections.end(), Sec);
523 assert(loc != Sections.end() && "Sec is not a valid section!");
524 return std::distance(Sections.begin(), loc);
525}
526
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000527const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000528MachOObjectFile::getSection64(DataRefImpl DRI) const {
529 assert(is64BitLoadCommand(MachOObj.get(), DRI));
Benjamin Kramer7d145782011-07-15 00:14:48 +0000530 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000531 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000532 DRI.d.b * sizeof(MachOFormat::Section64);
533 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
534 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000535}
536
Rafael Espindolacef81b32012-12-21 03:47:03 +0000537static StringRef parseSegmentOrSectionName(const char *P) {
538 if (P[15] == 0)
539 // Null terminated.
540 return P;
541 // Not null terminated, so this is a 16 char string.
542 return StringRef(P, 16);
543}
544
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000545ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000546 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000547 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000548 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000549 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000550 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000551 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000552 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000553}
554
555error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
556 StringRef &Result) const {
557 ArrayRef<char> Raw = getSectionRawName(DRI);
558 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000559 return object_error::success;
560}
561
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000562ArrayRef<char>
563MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000564 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000565 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000566 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000567 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000568 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000569 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000570 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000571}
572
573StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
574 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
575 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000576}
577
Michael J. Spencer25b15772011-06-25 17:55:23 +0000578error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
579 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000580 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000581 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000582 Result = Sect->Address;
583 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000584 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000585 Result = Sect->Address;
586 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000587 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000588}
589
Michael J. Spencer25b15772011-06-25 17:55:23 +0000590error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
591 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000592 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000593 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000594 Result = Sect->Size;
595 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000596 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000597 Result = Sect->Size;
598 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000599 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000600}
601
Michael J. Spencer25b15772011-06-25 17:55:23 +0000602error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
603 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000604 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000605 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000606 Result = MachOObj->getData(Sect->Offset, Sect->Size);
607 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000608 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000609 Result = MachOObj->getData(Sect->Offset, Sect->Size);
610 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000611 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000612}
613
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000614error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
615 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000616 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000617 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000618 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000619 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000620 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000621 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000622 }
623 return object_error::success;
624}
625
Michael J. Spencer25b15772011-06-25 17:55:23 +0000626error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
627 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000628 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000629 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000630 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000631 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000632 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000633 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000634 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000635 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000636}
637
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000638error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
639 bool &Result) const {
640 // FIXME: Unimplemented.
641 Result = false;
642 return object_error::success;
643}
644
645error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
646 bool &Result) const {
647 // FIXME: Unimplemented.
648 Result = false;
649 return object_error::success;
650}
651
Preston Gurdc68dda82012-04-12 20:13:57 +0000652error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
653 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000654 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000655 Result = true;
656 return object_error::success;
657}
658
659error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000660 bool &Result) const {
661 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000662 Result = false;
663 return object_error::success;
664}
665
666error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
667 bool &Result) const {
668 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000669 const MachOFormat::Section64 *Sect = getSection64(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 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000674 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000675 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
676 Result = (SectionType == MachO::SectionTypeZeroFill ||
677 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000678 }
679
680 return object_error::success;
681}
682
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000683error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
684 bool &Result) const {
685 // Consider using the code from isSectionText to look for __const sections.
686 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
687 // to use section attributes to distinguish code from data.
688
689 // FIXME: Unimplemented.
690 Result = false;
691 return object_error::success;
692}
693
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000694error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
695 DataRefImpl Symb,
696 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000697 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000698 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000699 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000700 Result = false;
701 return object_error::success;
702 }
703
704 uint64_t SectBegin, SectEnd;
705 getSectionAddress(Sec, SectBegin);
706 getSectionSize(Sec, SectEnd);
707 SectEnd += SectBegin;
708
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000709 if (MachOObj->is64Bit()) {
Rafael Espindola05b5bdd2013-04-06 02:15:44 +0000710 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000711 uint64_t SymAddr= Entry->Value;
712 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000713 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000714 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000715 uint64_t SymAddr= Entry->Value;
716 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000717 }
Owen Andersoncd749882011-10-12 22:21:32 +0000718
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000719 return object_error::success;
720}
721
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000722relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
723 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000724 ret.d.b = getSectionIndex(Sec);
725 return relocation_iterator(RelocationRef(ret, this));
726}
727relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
728 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000729 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000730 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000731 last_reloc = Sect->NumRelocationTableEntries;
732 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000733 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000734 last_reloc = Sect->NumRelocationTableEntries;
735 }
736 DataRefImpl ret;
737 ret.d.a = last_reloc;
738 ret.d.b = getSectionIndex(Sec);
739 return relocation_iterator(RelocationRef(ret, this));
740}
741
742section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000743 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000744 moveToNextSection(DRI);
745 return section_iterator(SectionRef(DRI, this));
746}
747
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000748section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000749 DataRefImpl DRI;
750 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000751 return section_iterator(SectionRef(DRI, this));
752}
753
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000754/*===-- Relocations -------------------------------------------------------===*/
755
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000756const MachOFormat::RelocationEntry *
757MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000758 uint32_t relOffset;
759 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000760 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000761 relOffset = Sect->RelocationTableOffset;
762 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000763 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000764 relOffset = Sect->RelocationTableOffset;
765 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000766 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
767 StringRef Data =
768 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
769 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000770}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000771
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000772error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
773 RelocationRef &Res) const {
774 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000775 Res = RelocationRef(Rel, this);
776 return object_error::success;
777}
778error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
779 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000780 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000781 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000782 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000783 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000784 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000785 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000786 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000787 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000788 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000789
790 unsigned Arch = getArch();
791 bool isScattered = (Arch != Triple::x86_64) &&
792 (RE->Word0 & macho::RF_Scattered);
793 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000794 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000795 RelAddr = RE->Word0 & 0xFFFFFF;
796 else
797 RelAddr = RE->Word0;
798
799 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000800 return object_error::success;
801}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000802error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
803 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000804 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000805
806 unsigned Arch = getArch();
807 bool isScattered = (Arch != Triple::x86_64) &&
808 (RE->Word0 & macho::RF_Scattered);
809 if (isScattered)
810 Res = RE->Word0 & 0xFFFFFF;
811 else
812 Res = RE->Word0;
813 return object_error::success;
814}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000815error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
816 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000817 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000818 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
819 bool isExtern = (RE->Word1 >> 27) & 1;
820
821 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000822 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000823 if (isExtern) {
824 for (unsigned i = 0; i < SymbolIdx; i++) {
825 Sym.d.b++;
826 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000827 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000828 "Relocation symbol index out of range!");
829 }
830 }
831 Res = SymbolRef(Sym, this);
832 return object_error::success;
833}
834error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000835 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000836 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000837 Res = RE->Word0;
838 Res <<= 32;
839 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000840 return object_error::success;
841}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000842error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
843 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000844 // TODO: Support scattered relocations.
845 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000846 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000847
848 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000849 bool isScattered = (Arch != Triple::x86_64) &&
850 (RE->Word0 & macho::RF_Scattered);
851
852 unsigned r_type;
853 if (isScattered)
854 r_type = (RE->Word0 >> 24) & 0xF;
855 else
856 r_type = (RE->Word1 >> 28) & 0xF;
857
Owen Anderson0135fe12011-10-24 21:44:00 +0000858 switch (Arch) {
859 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000860 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000861 "GENERIC_RELOC_VANILLA",
862 "GENERIC_RELOC_PAIR",
863 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000864 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000865 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000866 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000867
Owen Andersoneb6bd332011-10-27 20:46:09 +0000868 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000869 res = "Unknown";
870 else
871 res = Table[r_type];
872 break;
873 }
874 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000875 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000876 "X86_64_RELOC_UNSIGNED",
877 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000878 "X86_64_RELOC_BRANCH",
879 "X86_64_RELOC_GOT_LOAD",
880 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000881 "X86_64_RELOC_SUBTRACTOR",
882 "X86_64_RELOC_SIGNED_1",
883 "X86_64_RELOC_SIGNED_2",
884 "X86_64_RELOC_SIGNED_4",
885 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000886
Owen Andersond8fa76d2011-10-24 23:20:07 +0000887 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000888 res = "Unknown";
889 else
890 res = Table[r_type];
891 break;
892 }
893 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000894 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000895 "ARM_RELOC_VANILLA",
896 "ARM_RELOC_PAIR",
897 "ARM_RELOC_SECTDIFF",
898 "ARM_RELOC_LOCAL_SECTDIFF",
899 "ARM_RELOC_PB_LA_PTR",
900 "ARM_RELOC_BR24",
901 "ARM_THUMB_RELOC_BR22",
902 "ARM_THUMB_32BIT_BRANCH",
903 "ARM_RELOC_HALF",
904 "ARM_RELOC_HALF_SECTDIFF" };
905
906 if (r_type > 9)
907 res = "Unknown";
908 else
909 res = Table[r_type];
910 break;
911 }
912 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000913 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000914 "PPC_RELOC_VANILLA",
915 "PPC_RELOC_PAIR",
916 "PPC_RELOC_BR14",
917 "PPC_RELOC_BR24",
918 "PPC_RELOC_HI16",
919 "PPC_RELOC_LO16",
920 "PPC_RELOC_HA16",
921 "PPC_RELOC_LO14",
922 "PPC_RELOC_SECTDIFF",
923 "PPC_RELOC_PB_LA_PTR",
924 "PPC_RELOC_HI16_SECTDIFF",
925 "PPC_RELOC_LO16_SECTDIFF",
926 "PPC_RELOC_HA16_SECTDIFF",
927 "PPC_RELOC_JBSR",
928 "PPC_RELOC_LO14_SECTDIFF",
929 "PPC_RELOC_LOCAL_SECTDIFF" };
930
931 res = Table[r_type];
932 break;
933 }
934 case Triple::UnknownArch:
935 res = "Unknown";
936 break;
937 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000938 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000939 return object_error::success;
940}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000941error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
942 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000943 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000944 bool isExtern = (RE->Word1 >> 27) & 1;
945 Res = 0;
946 if (!isExtern) {
947 const uint8_t* sectAddress = base();
948 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000949 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000950 sectAddress += Sect->Offset;
951 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000952 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000953 sectAddress += Sect->Offset;
954 }
955 Res = reinterpret_cast<uintptr_t>(sectAddress);
956 }
957 return object_error::success;
958}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000959
960// Helper to advance a section or symbol iterator multiple increments at a time.
961template<class T>
962error_code advance(T &it, size_t Val) {
963 error_code ec;
964 while (Val--) {
965 it.increment(ec);
966 }
967 return ec;
968}
969
970template<class T>
971void advanceTo(T &it, size_t Val) {
972 if (error_code ec = advance(it, Val))
973 report_fatal_error(ec.message());
974}
975
Owen Anderson1832f4d2011-10-26 20:42:54 +0000976void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000977 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000978 raw_string_ostream &fmt) const {
979 unsigned Arch = getArch();
980 bool isScattered = (Arch != Triple::x86_64) &&
981 (RE->Word0 & macho::RF_Scattered);
982
983 // Target of a scattered relocation is an address. In the interest of
984 // generating pretty output, scan through the symbol table looking for a
985 // symbol that aligns with that address. If we find one, print it.
986 // Otherwise, we just print the hex address of the target.
987 if (isScattered) {
988 uint32_t Val = RE->Word1;
989
990 error_code ec;
991 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
992 SI.increment(ec)) {
993 if (ec) report_fatal_error(ec.message());
994
995 uint64_t Addr;
996 StringRef Name;
997
998 if ((ec = SI->getAddress(Addr)))
999 report_fatal_error(ec.message());
1000 if (Addr != Val) continue;
1001 if ((ec = SI->getName(Name)))
1002 report_fatal_error(ec.message());
1003 fmt << Name;
1004 return;
1005 }
1006
Owen Andersonb28bdbf2011-10-27 21:53:50 +00001007 // If we couldn't find a symbol that this relocation refers to, try
1008 // to find a section beginning instead.
1009 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
1010 SI.increment(ec)) {
1011 if (ec) report_fatal_error(ec.message());
1012
1013 uint64_t Addr;
1014 StringRef Name;
1015
1016 if ((ec = SI->getAddress(Addr)))
1017 report_fatal_error(ec.message());
1018 if (Addr != Val) continue;
1019 if ((ec = SI->getName(Name)))
1020 report_fatal_error(ec.message());
1021 fmt << Name;
1022 return;
1023 }
1024
Owen Anderson1832f4d2011-10-26 20:42:54 +00001025 fmt << format("0x%x", Val);
1026 return;
1027 }
1028
1029 StringRef S;
1030 bool isExtern = (RE->Word1 >> 27) & 1;
1031 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001032
1033 if (isExtern) {
1034 symbol_iterator SI = begin_symbols();
1035 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001036 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001037 } else {
1038 section_iterator SI = begin_sections();
1039 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001040 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001041 }
1042
Owen Anderson1832f4d2011-10-26 20:42:54 +00001043 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001044}
1045
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001046error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1047 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001048 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +00001049
Owen Anderson1832f4d2011-10-26 20:42:54 +00001050 unsigned Arch = getArch();
1051 bool isScattered = (Arch != Triple::x86_64) &&
1052 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001053
Owen Anderson013d7562011-10-25 18:48:41 +00001054 std::string fmtbuf;
1055 raw_string_ostream fmt(fmtbuf);
1056
Owen Anderson1832f4d2011-10-26 20:42:54 +00001057 unsigned Type;
1058 if (isScattered)
1059 Type = (RE->Word0 >> 24) & 0xF;
1060 else
1061 Type = (RE->Word1 >> 28) & 0xF;
1062
Owen Andersoneb6bd332011-10-27 20:46:09 +00001063 bool isPCRel;
1064 if (isScattered)
1065 isPCRel = ((RE->Word0 >> 30) & 1);
1066 else
1067 isPCRel = ((RE->Word1 >> 24) & 1);
1068
Owen Andersond8fa76d2011-10-24 23:20:07 +00001069 // Determine any addends that should be displayed with the relocation.
1070 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001071
1072 // X86_64 has entirely custom relocation types.
1073 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001074 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001075
Owen Andersond8fa76d2011-10-24 23:20:07 +00001076 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001077 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1078 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1079 printRelocationTargetName(RE, fmt);
1080 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001081 if (isPCRel) fmt << "PCREL";
1082 break;
1083 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001084 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001085 DataRefImpl RelNext = Rel;
1086 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001087 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001088
1089 // X86_64_SUBTRACTOR must be followed by a relocation of type
1090 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001091 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001092 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001093 if (RType != 0)
1094 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1095 "X86_64_RELOC_SUBTRACTOR.");
1096
Owen Andersonef22f782011-10-26 17:28:49 +00001097 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1098 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001099 printRelocationTargetName(RENext, fmt);
1100 fmt << "-";
1101 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001102 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001103 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001104 case macho::RIT_X86_64_TLV:
1105 printRelocationTargetName(RE, fmt);
1106 fmt << "@TLV";
1107 if (isPCRel) fmt << "P";
1108 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001109 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1110 printRelocationTargetName(RE, fmt);
1111 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001112 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001113 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1114 printRelocationTargetName(RE, fmt);
1115 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001116 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001117 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1118 printRelocationTargetName(RE, fmt);
1119 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001120 break;
1121 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001122 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001123 break;
1124 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001125 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001126 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1127 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001128 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001129 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001130 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001131 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001132 DataRefImpl RelNext = Rel;
1133 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001134 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001135
1136 // X86 sect diff's must be followed by a relocation of type
1137 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001138 bool isNextScattered = (Arch != Triple::x86_64) &&
1139 (RENext->Word0 & macho::RF_Scattered);
1140 unsigned RType;
1141 if (isNextScattered)
1142 RType = (RENext->Word0 >> 24) & 0xF;
1143 else
1144 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001145 if (RType != 1)
1146 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001147 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001148
Owen Anderson1832f4d2011-10-26 20:42:54 +00001149 printRelocationTargetName(RE, fmt);
1150 fmt << "-";
1151 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001152 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001153 }
1154 }
Owen Anderson013d7562011-10-25 18:48:41 +00001155
Owen Anderson1832f4d2011-10-26 20:42:54 +00001156 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001157 // All X86 relocations that need special printing were already
1158 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001159 switch (Type) {
1160 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001161 DataRefImpl RelNext = Rel;
1162 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001163 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001164
1165 // X86 sect diff's must be followed by a relocation of type
1166 // GENERIC_RELOC_PAIR.
1167 bool isNextScattered = (Arch != Triple::x86_64) &&
1168 (RENext->Word0 & macho::RF_Scattered);
1169 unsigned RType;
1170 if (isNextScattered)
1171 RType = (RENext->Word0 >> 24) & 0xF;
1172 else
1173 RType = (RENext->Word1 >> 28) & 0xF;
1174 if (RType != 1)
1175 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1176 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1177
1178 printRelocationTargetName(RE, fmt);
1179 fmt << "-";
1180 printRelocationTargetName(RENext, fmt);
1181 break;
1182 }
1183 case macho::RIT_Generic_TLV: {
1184 printRelocationTargetName(RE, fmt);
1185 fmt << "@TLV";
1186 if (isPCRel) fmt << "P";
1187 break;
1188 }
1189 default:
1190 printRelocationTargetName(RE, fmt);
1191 }
Owen Anderson013d7562011-10-25 18:48:41 +00001192 } else { // ARM-specific relocations
1193 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001194 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1195 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001196 // Half relocations steal a bit from the length field to encode
1197 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001198 bool isUpper;
1199 if (isScattered)
1200 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001201 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001202 isUpper = (RE->Word1 >> 25) & 1;
1203
1204 if (isUpper)
1205 fmt << ":upper16:(";
1206 else
1207 fmt << ":lower16:(";
1208 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001209
Owen Anderson013d7562011-10-25 18:48:41 +00001210 DataRefImpl RelNext = Rel;
1211 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001212 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001213
1214 // ARM half relocs must be followed by a relocation of type
1215 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001216 bool isNextScattered = (Arch != Triple::x86_64) &&
1217 (RENext->Word0 & macho::RF_Scattered);
1218 unsigned RType;
1219 if (isNextScattered)
1220 RType = (RENext->Word0 >> 24) & 0xF;
1221 else
1222 RType = (RENext->Word1 >> 28) & 0xF;
1223
Owen Anderson013d7562011-10-25 18:48:41 +00001224 if (RType != 1)
1225 report_fatal_error("Expected ARM_RELOC_PAIR after "
1226 "GENERIC_RELOC_HALF");
1227
Owen Anderson1832f4d2011-10-26 20:42:54 +00001228 // NOTE: The half of the target virtual address is stashed in the
1229 // address field of the secondary relocation, but we can't reverse
1230 // engineer the constant offset from it without decoding the movw/movt
1231 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001232
1233 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1234 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001235 if (Type == macho::RIT_ARM_HalfDifference) {
1236 fmt << "-";
1237 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001238 }
1239
Owen Anderson013d7562011-10-25 18:48:41 +00001240 fmt << ")";
1241 break;
1242 }
1243 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001244 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001245 }
1246 }
1247 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001248 } else
1249 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001250
Owen Anderson0135fe12011-10-24 21:44:00 +00001251 fmt.flush();
1252 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001253 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001254}
1255
Owen Anderson0685e942011-10-25 20:35:53 +00001256error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1257 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001258 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001259
Owen Anderson0685e942011-10-25 20:35:53 +00001260 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001261 bool isScattered = (Arch != Triple::x86_64) &&
1262 (RE->Word0 & macho::RF_Scattered);
1263 unsigned Type;
1264 if (isScattered)
1265 Type = (RE->Word0 >> 24) & 0xF;
1266 else
1267 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001268
1269 Result = false;
1270
1271 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1272 // is always hidden.
1273 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001274 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001275 } else if (Arch == Triple::x86_64) {
1276 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1277 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001278 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001279 DataRefImpl RelPrev = Rel;
1280 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001281 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001282
1283 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1284
Owen Anderson1832f4d2011-10-26 20:42:54 +00001285 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001286 }
1287 }
1288
1289 return object_error::success;
1290}
1291
David Meyer5c2b4ea2012-03-01 01:36:50 +00001292error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1293 LibraryRef &Res) const {
1294 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1295}
1296
1297error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1298 StringRef &Res) const {
1299 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1300}
1301
1302
Eric Christopher6256b032011-04-22 03:19:48 +00001303/*===-- Miscellaneous -----------------------------------------------------===*/
1304
1305uint8_t MachOObjectFile::getBytesInAddress() const {
1306 return MachOObj->is64Bit() ? 8 : 4;
1307}
1308
1309StringRef MachOObjectFile::getFileFormatName() const {
1310 if (!MachOObj->is64Bit()) {
1311 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001312 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001313 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001314 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001315 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001316 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001317 return "Mach-O 32-bit ppc";
1318 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001319 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001320 "64-bit object file when we're not 64-bit?");
1321 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001322 }
1323 }
1324
Eric Christopherb3e6b042013-02-28 20:26:17 +00001325 // Make sure the cpu type has the correct mask.
1326 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1327 == llvm::MachO::CPUArchABI64 &&
1328 "32-bit object file when we're 64-bit?");
1329
Eric Christopher6256b032011-04-22 03:19:48 +00001330 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001331 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001332 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001333 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001334 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001335 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001336 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001337 }
1338}
1339
1340unsigned MachOObjectFile::getArch() const {
1341 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001342 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001343 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001344 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001345 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001346 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001347 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001348 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001349 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001350 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001351 return Triple::ppc64;
1352 default:
1353 return Triple::UnknownArch;
1354 }
1355}
1356
Owen Andersonf7c93a32011-10-11 17:32:27 +00001357} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001358} // end namespace llvm