blob: f82ed08250a66278c706d169332d14a8012f8a72 [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)
David Meyer6f9489a2012-03-09 20:41:57 +000032 : ObjectFile(Binary::ID_MachO, Object, ec),
Benjamin Kramer0fcab072011-09-08 20:52:17 +000033 MachOObj(MOO),
34 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
35 DataRefImpl DRI;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000036 moveToNextSection(DRI);
37 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
38 while (DRI.d.a < LoadCommandCount) {
39 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000040 DRI.d.b++;
41 moveToNextSection(DRI);
42 }
43}
44
45
Eric Christopher6256b032011-04-22 03:19:48 +000046ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000047 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000048 std::string Err;
49 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
50 if (!MachOObj)
51 return NULL;
Jim Grosbach596e4742012-11-29 19:14:11 +000052 // MachOObject takes ownership of the Buffer we passed to it, and
53 // MachOObjectFile does, too, so we need to make sure they don't get the
54 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
55 // not a copy of the memory itself), so just make a new copy here for
56 // the MachOObjectFile.
57 MemoryBuffer *NewBuffer =
Benjamin Kramerf56c3e22012-11-29 20:08:03 +000058 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
59 Buffer->getBufferIdentifier(), false);
Jim Grosbach596e4742012-11-29 19:14:11 +000060 return new MachOObjectFile(NewBuffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000061}
62
63/*===-- Symbols -----------------------------------------------------------===*/
64
65void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
66 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
67 while (DRI.d.a < LoadCommandCount) {
68 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
69 if (LCI.Command.Type == macho::LCT_Symtab) {
70 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
71 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
72 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
73 return;
74 }
75
76 DRI.d.a++;
77 DRI.d.b = 0;
78 }
79}
80
Rafael Espindola00555c12013-04-06 01:59:05 +000081const MachOFormat::SymbolTableEntry *
82MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Eric Christopher6256b032011-04-22 03:19:48 +000083 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
84 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
85 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
86
87 if (RegisteredStringTable != DRI.d.a) {
88 MachOObj->RegisterStringTable(*SymtabLoadCmd);
89 RegisteredStringTable = DRI.d.a;
90 }
91
Rafael Espindola00555c12013-04-06 01:59:05 +000092 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
93 unsigned Index = DRI.d.b;
94 uint64_t Offset = (SymbolTableOffset +
95 Index * sizeof(macho::SymbolTableEntry));
96 StringRef Data = MachOObj->getData(Offset,
97 sizeof(MachOFormat::SymbolTableEntry));
98 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +000099}
100
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000101void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
102 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
103 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
104 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
105 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
106
107 if (RegisteredStringTable != DRI.d.a) {
108 MachOObj->RegisterStringTable(*SymtabLoadCmd);
109 RegisteredStringTable = DRI.d.a;
110 }
111
112 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
113 Res);
114}
115
Eric Christopher6256b032011-04-22 03:19:48 +0000116
Michael J. Spencer25b15772011-06-25 17:55:23 +0000117error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
118 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000119 DRI.d.b++;
120 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000121 Result = SymbolRef(DRI, this);
122 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000123}
124
Michael J. Spencer25b15772011-06-25 17:55:23 +0000125error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
126 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000127 if (MachOObj->is64Bit()) {
128 InMemoryStruct<macho::Symbol64TableEntry> Entry;
129 getSymbol64TableEntry(DRI, Entry);
130 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
131 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000132 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000133 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
134 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000135 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000136}
137
Danil Malyshevb0436a72011-11-29 17:40:10 +0000138error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
139 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000140 if (MachOObj->is64Bit()) {
141 InMemoryStruct<macho::Symbol64TableEntry> Entry;
142 getSymbol64TableEntry(DRI, Entry);
143 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000144 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000145 const MachOFormat::Section64 *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000146 getSection64(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000147 Result += Section->Offset - Section->Address;
148 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000149 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000150 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000151 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000152 if (Entry->SectionIndex) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000153 const MachOFormat::Section *Section =
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000154 getSection(Sections[Entry->SectionIndex-1]);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000155 Result += Section->Offset - Section->Address;
156 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000157 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000158
Michael J. Spencer25b15772011-06-25 17:55:23 +0000159 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000160}
161
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000162error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
163 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000164 if (MachOObj->is64Bit()) {
165 InMemoryStruct<macho::Symbol64TableEntry> Entry;
166 getSymbol64TableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000167 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000168 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000169 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Owen Anderson95f8db42011-10-12 22:37:10 +0000170 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000171 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000172 return object_error::success;
173}
174
Michael J. Spencer25b15772011-06-25 17:55:23 +0000175error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
176 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000177 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
178 uint64_t BeginOffset;
179 uint64_t EndOffset = 0;
180 uint8_t SectionIndex;
181 if (MachOObj->is64Bit()) {
182 InMemoryStruct<macho::Symbol64TableEntry> Entry;
183 getSymbol64TableEntry(DRI, Entry);
184 BeginOffset = Entry->Value;
185 SectionIndex = Entry->SectionIndex;
186 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000187 uint32_t flags = SymbolRef::SF_None;
188 getSymbolFlags(DRI, flags);
189 if (flags & SymbolRef::SF_Common)
190 Result = Entry->Value;
191 else
192 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000193 return object_error::success;
194 }
195 // Unfortunately symbols are unsorted so we need to touch all
196 // symbols from load command
197 DRI.d.b = 0;
198 uint32_t Command = DRI.d.a;
199 while (Command == DRI.d.a) {
200 moveToNextSymbol(DRI);
201 if (DRI.d.a < LoadCommandCount) {
202 getSymbol64TableEntry(DRI, Entry);
203 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
204 if (!EndOffset || Entry->Value < EndOffset)
205 EndOffset = Entry->Value;
206 }
207 DRI.d.b++;
208 }
209 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000210 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000211 BeginOffset = Entry->Value;
212 SectionIndex = Entry->SectionIndex;
213 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000214 uint32_t flags = SymbolRef::SF_None;
215 getSymbolFlags(DRI, flags);
216 if (flags & SymbolRef::SF_Common)
217 Result = Entry->Value;
218 else
219 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000220 return object_error::success;
221 }
222 // Unfortunately symbols are unsorted so we need to touch all
223 // symbols from load command
224 DRI.d.b = 0;
225 uint32_t Command = DRI.d.a;
226 while (Command == DRI.d.a) {
227 moveToNextSymbol(DRI);
228 if (DRI.d.a < LoadCommandCount) {
Rafael Espindola00555c12013-04-06 01:59:05 +0000229 Entry = getSymbolTableEntry(DRI);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000230 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
231 if (!EndOffset || Entry->Value < EndOffset)
232 EndOffset = Entry->Value;
233 }
234 DRI.d.b++;
235 }
236 }
237 if (!EndOffset) {
238 uint64_t Size;
239 getSectionSize(Sections[SectionIndex-1], Size);
240 getSectionAddress(Sections[SectionIndex-1], EndOffset);
241 EndOffset += Size;
242 }
243 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000244 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000245}
246
Michael J. Spencer25b15772011-06-25 17:55:23 +0000247error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
248 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000249 uint8_t Type, Flags;
250 if (MachOObj->is64Bit()) {
251 InMemoryStruct<macho::Symbol64TableEntry> Entry;
252 getSymbol64TableEntry(DRI, Entry);
253 Type = Entry->Type;
254 Flags = Entry->Flags;
255 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000256 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000257 Type = Entry->Type;
258 Flags = Entry->Flags;
259 }
Eric Christopher6256b032011-04-22 03:19:48 +0000260
261 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000262 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000263 case macho::STT_Undefined:
264 Char = 'u';
265 break;
266 case macho::STT_Absolute:
267 case macho::STT_Section:
268 Char = 's';
269 break;
270 default:
271 Char = '?';
272 break;
273 }
274
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000275 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000276 Char = toupper(static_cast<unsigned char>(Char));
Michael J. Spencer25b15772011-06-25 17:55:23 +0000277 Result = Char;
278 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000279}
280
David Meyerc46255a2012-02-28 23:47:53 +0000281error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
282 uint32_t &Result) const {
283 uint16_t MachOFlags;
284 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000285 if (MachOObj->is64Bit()) {
286 InMemoryStruct<macho::Symbol64TableEntry> Entry;
287 getSymbol64TableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000288 MachOFlags = Entry->Flags;
289 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000290 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000291 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
David Meyerc46255a2012-02-28 23:47:53 +0000292 MachOFlags = Entry->Flags;
293 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000294 }
295
Preston Gurdc68dda82012-04-12 20:13:57 +0000296 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000297 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000298
299 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
300 Result |= SymbolRef::SF_Undefined;
301
David Meyerc46255a2012-02-28 23:47:53 +0000302 if (MachOFlags & macho::STF_StabsEntryMask)
303 Result |= SymbolRef::SF_FormatSpecific;
304
Preston Gurdc68dda82012-04-12 20:13:57 +0000305 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000306 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000307 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
308 Result |= SymbolRef::SF_Common;
309 }
David Meyerc46255a2012-02-28 23:47:53 +0000310
311 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
312 Result |= SymbolRef::SF_Weak;
313
314 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
315 Result |= SymbolRef::SF_Absolute;
316
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000317 return object_error::success;
318}
319
320error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
321 section_iterator &Res) const {
322 uint8_t index;
323 if (MachOObj->is64Bit()) {
324 InMemoryStruct<macho::Symbol64TableEntry> Entry;
325 getSymbol64TableEntry(Symb, Entry);
326 index = Entry->SectionIndex;
327 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000328 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000329 index = Entry->SectionIndex;
330 }
331
332 if (index == 0)
333 Res = end_sections();
334 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000335 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000336
337 return object_error::success;
338}
339
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000340error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000341 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000342 uint8_t n_type;
343 if (MachOObj->is64Bit()) {
344 InMemoryStruct<macho::Symbol64TableEntry> Entry;
345 getSymbol64TableEntry(Symb, Entry);
346 n_type = Entry->Type;
347 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000348 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000349 n_type = Entry->Type;
350 }
351 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000352
353 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000354 if (n_type & MachO::NlistMaskStab) {
355 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000356 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000357 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000358
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000359 switch (n_type & MachO::NlistMaskType) {
360 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000361 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000362 break;
363 case MachO::NListTypeSection :
364 Res = SymbolRef::ST_Function;
365 break;
366 }
367 return object_error::success;
368}
369
Tim Northovera41dce32012-10-29 10:47:00 +0000370error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
371 uint64_t &Val) const {
372 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
373}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000374
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000375symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000376 // DRI.d.a = segment number; DRI.d.b = symbol index.
377 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000378 moveToNextSymbol(DRI);
379 return symbol_iterator(SymbolRef(DRI, this));
380}
381
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000382symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000383 DataRefImpl DRI;
384 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000385 return symbol_iterator(SymbolRef(DRI, this));
386}
387
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000388symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
389 // TODO: implement
390 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
391}
392
393symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
394 // TODO: implement
395 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
396}
Eric Christopher6256b032011-04-22 03:19:48 +0000397
David Meyer5c2b4ea2012-03-01 01:36:50 +0000398library_iterator MachOObjectFile::begin_libraries_needed() const {
399 // TODO: implement
400 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
401}
402
403library_iterator MachOObjectFile::end_libraries_needed() const {
404 // TODO: implement
405 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
406}
407
David Meyer97f77872012-03-01 22:19:54 +0000408StringRef MachOObjectFile::getLoadName() const {
409 // TODO: Implement
410 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
411}
412
Eric Christopher6256b032011-04-22 03:19:48 +0000413/*===-- Sections ----------------------------------------------------------===*/
414
415void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
416 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
417 while (DRI.d.a < LoadCommandCount) {
418 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
419 if (LCI.Command.Type == macho::LCT_Segment) {
420 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
421 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
422 if (DRI.d.b < SegmentLoadCmd->NumSections)
423 return;
424 } else if (LCI.Command.Type == macho::LCT_Segment64) {
425 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
426 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
427 if (DRI.d.b < Segment64LoadCmd->NumSections)
428 return;
429 }
430
431 DRI.d.a++;
432 DRI.d.b = 0;
433 }
434}
435
Michael J. Spencer25b15772011-06-25 17:55:23 +0000436error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
437 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000438 DRI.d.b++;
439 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000440 Result = SectionRef(DRI, this);
441 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000442}
443
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000444static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
Eric Christopher6256b032011-04-22 03:19:48 +0000445 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000446 if (LCI.Command.Type == macho::LCT_Segment64)
447 return true;
448 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
449 return false;
450}
451
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000452const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000453 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
454 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
455 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000456 DRI.d.b * sizeof(MachOFormat::Section);
457 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
458 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000459}
460
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000461std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
462 SectionList::const_iterator loc =
463 std::find(Sections.begin(), Sections.end(), Sec);
464 assert(loc != Sections.end() && "Sec is not a valid section!");
465 return std::distance(Sections.begin(), loc);
466}
467
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000468const MachOFormat::Section64 *
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000469MachOObjectFile::getSection64(DataRefImpl DRI) const {
470 assert(is64BitLoadCommand(MachOObj.get(), DRI));
Benjamin Kramer7d145782011-07-15 00:14:48 +0000471 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000472 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000473 DRI.d.b * sizeof(MachOFormat::Section64);
474 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
475 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
Benjamin Kramer7d145782011-07-15 00:14:48 +0000476}
477
Rafael Espindolacef81b32012-12-21 03:47:03 +0000478static StringRef parseSegmentOrSectionName(const char *P) {
479 if (P[15] == 0)
480 // Null terminated.
481 return P;
482 // Not null terminated, so this is a 16 char string.
483 return StringRef(P, 16);
484}
485
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000486ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000487 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000488 const MachOFormat::Section64 *sec = getSection64(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000489 return ArrayRef<char>(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000490 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000491 const MachOFormat::Section *sec = getSection(DRI);
Rafael Espindolac1cd6aa2013-04-05 18:18:19 +0000492 return ArrayRef<char>(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000493 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000494}
495
496error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
497 StringRef &Result) const {
498 ArrayRef<char> Raw = getSectionRawName(DRI);
499 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000500 return object_error::success;
501}
502
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000503ArrayRef<char>
504MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000505 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000506 const MachOFormat::Section64 *sec = getSection64(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000507 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000508 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000509 const MachOFormat::Section *sec = getSection(Sec);
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000510 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000511 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000512}
513
514StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
515 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
516 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000517}
518
Michael J. Spencer25b15772011-06-25 17:55:23 +0000519error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
520 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000521 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000522 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000523 Result = Sect->Address;
524 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000525 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000526 Result = Sect->Address;
527 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000528 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000529}
530
Michael J. Spencer25b15772011-06-25 17:55:23 +0000531error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
532 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000533 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000534 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000535 Result = Sect->Size;
536 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000537 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000538 Result = Sect->Size;
539 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000540 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000541}
542
Michael J. Spencer25b15772011-06-25 17:55:23 +0000543error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
544 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000545 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000546 const MachOFormat::Section64 *Sect = getSection64(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000547 Result = MachOObj->getData(Sect->Offset, Sect->Size);
548 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000549 const MachOFormat::Section *Sect = getSection(DRI);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000550 Result = MachOObj->getData(Sect->Offset, Sect->Size);
551 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000552 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000553}
554
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000555error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
556 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000557 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000558 const MachOFormat::Section64 *Sect = getSection64(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000559 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000560 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000561 const MachOFormat::Section *Sect = getSection(DRI);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000562 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000563 }
564 return object_error::success;
565}
566
Michael J. Spencer25b15772011-06-25 17:55:23 +0000567error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
568 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000569 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000570 const MachOFormat::Section64 *Sect = getSection64(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000571 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000572 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000573 const MachOFormat::Section *Sect = getSection(DRI);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000574 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000575 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000576 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000577}
578
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000579error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
580 bool &Result) const {
581 // FIXME: Unimplemented.
582 Result = false;
583 return object_error::success;
584}
585
586error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
587 bool &Result) const {
588 // FIXME: Unimplemented.
589 Result = false;
590 return object_error::success;
591}
592
Preston Gurdc68dda82012-04-12 20:13:57 +0000593error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
594 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000595 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000596 Result = true;
597 return object_error::success;
598}
599
600error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000601 bool &Result) const {
602 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000603 Result = false;
604 return object_error::success;
605}
606
607error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
608 bool &Result) const {
609 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000610 const MachOFormat::Section64 *Sect = getSection64(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000611 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
612 Result = (SectionType == MachO::SectionTypeZeroFill ||
613 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000614 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000615 const MachOFormat::Section *Sect = getSection(DRI);
Eli Friedman41827f92012-05-02 02:31:28 +0000616 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
617 Result = (SectionType == MachO::SectionTypeZeroFill ||
618 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000619 }
620
621 return object_error::success;
622}
623
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000624error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
625 bool &Result) const {
626 // Consider using the code from isSectionText to look for __const sections.
627 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
628 // to use section attributes to distinguish code from data.
629
630 // FIXME: Unimplemented.
631 Result = false;
632 return object_error::success;
633}
634
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000635error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
636 DataRefImpl Symb,
637 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000638 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000639 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000640 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000641 Result = false;
642 return object_error::success;
643 }
644
645 uint64_t SectBegin, SectEnd;
646 getSectionAddress(Sec, SectBegin);
647 getSectionSize(Sec, SectEnd);
648 SectEnd += SectBegin;
649
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000650 if (MachOObj->is64Bit()) {
651 InMemoryStruct<macho::Symbol64TableEntry> Entry;
652 getSymbol64TableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000653 uint64_t SymAddr= Entry->Value;
654 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000655 } else {
Rafael Espindola00555c12013-04-06 01:59:05 +0000656 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
Owen Andersoncd749882011-10-12 22:21:32 +0000657 uint64_t SymAddr= Entry->Value;
658 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000659 }
Owen Andersoncd749882011-10-12 22:21:32 +0000660
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000661 return object_error::success;
662}
663
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000664relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
665 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000666 ret.d.b = getSectionIndex(Sec);
667 return relocation_iterator(RelocationRef(ret, this));
668}
669relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
670 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000671 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000672 const MachOFormat::Section64 *Sect = getSection64(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000673 last_reloc = Sect->NumRelocationTableEntries;
674 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000675 const MachOFormat::Section *Sect = getSection(Sec);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000676 last_reloc = Sect->NumRelocationTableEntries;
677 }
678 DataRefImpl ret;
679 ret.d.a = last_reloc;
680 ret.d.b = getSectionIndex(Sec);
681 return relocation_iterator(RelocationRef(ret, this));
682}
683
684section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000685 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000686 moveToNextSection(DRI);
687 return section_iterator(SectionRef(DRI, this));
688}
689
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000690section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000691 DataRefImpl DRI;
692 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000693 return section_iterator(SectionRef(DRI, this));
694}
695
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000696/*===-- Relocations -------------------------------------------------------===*/
697
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000698const MachOFormat::RelocationEntry *
699MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000700 uint32_t relOffset;
701 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000702 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000703 relOffset = Sect->RelocationTableOffset;
704 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000705 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000706 relOffset = Sect->RelocationTableOffset;
707 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000708 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
709 StringRef Data =
710 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
711 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000712}
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000713
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000714error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
715 RelocationRef &Res) const {
716 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000717 Res = RelocationRef(Rel, this);
718 return object_error::success;
719}
720error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
721 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000722 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000723 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000724 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000725 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000726 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000727 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Owen Anderson0135fe12011-10-24 21:44:00 +0000728 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000729 }
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000730 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000731
732 unsigned Arch = getArch();
733 bool isScattered = (Arch != Triple::x86_64) &&
734 (RE->Word0 & macho::RF_Scattered);
735 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000736 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000737 RelAddr = RE->Word0 & 0xFFFFFF;
738 else
739 RelAddr = RE->Word0;
740
741 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000742 return object_error::success;
743}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000744error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
745 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000746 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000747
748 unsigned Arch = getArch();
749 bool isScattered = (Arch != Triple::x86_64) &&
750 (RE->Word0 & macho::RF_Scattered);
751 if (isScattered)
752 Res = RE->Word0 & 0xFFFFFF;
753 else
754 Res = RE->Word0;
755 return object_error::success;
756}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000757error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
758 SymbolRef &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000759 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000760 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
761 bool isExtern = (RE->Word1 >> 27) & 1;
762
763 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000764 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000765 if (isExtern) {
766 for (unsigned i = 0; i < SymbolIdx; i++) {
767 Sym.d.b++;
768 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000769 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000770 "Relocation symbol index out of range!");
771 }
772 }
773 Res = SymbolRef(Sym, this);
774 return object_error::success;
775}
776error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000777 uint64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000778 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Andersonf8261e72011-10-26 17:10:22 +0000779 Res = RE->Word0;
780 Res <<= 32;
781 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000782 return object_error::success;
783}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000784error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
785 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000786 // TODO: Support scattered relocations.
787 StringRef res;
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000788 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000789
790 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000791 bool isScattered = (Arch != Triple::x86_64) &&
792 (RE->Word0 & macho::RF_Scattered);
793
794 unsigned r_type;
795 if (isScattered)
796 r_type = (RE->Word0 >> 24) & 0xF;
797 else
798 r_type = (RE->Word1 >> 28) & 0xF;
799
Owen Anderson0135fe12011-10-24 21:44:00 +0000800 switch (Arch) {
801 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000802 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000803 "GENERIC_RELOC_VANILLA",
804 "GENERIC_RELOC_PAIR",
805 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000806 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000807 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000808 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000809
Owen Andersoneb6bd332011-10-27 20:46:09 +0000810 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000811 res = "Unknown";
812 else
813 res = Table[r_type];
814 break;
815 }
816 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000817 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000818 "X86_64_RELOC_UNSIGNED",
819 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000820 "X86_64_RELOC_BRANCH",
821 "X86_64_RELOC_GOT_LOAD",
822 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000823 "X86_64_RELOC_SUBTRACTOR",
824 "X86_64_RELOC_SIGNED_1",
825 "X86_64_RELOC_SIGNED_2",
826 "X86_64_RELOC_SIGNED_4",
827 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000828
Owen Andersond8fa76d2011-10-24 23:20:07 +0000829 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000830 res = "Unknown";
831 else
832 res = Table[r_type];
833 break;
834 }
835 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000836 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000837 "ARM_RELOC_VANILLA",
838 "ARM_RELOC_PAIR",
839 "ARM_RELOC_SECTDIFF",
840 "ARM_RELOC_LOCAL_SECTDIFF",
841 "ARM_RELOC_PB_LA_PTR",
842 "ARM_RELOC_BR24",
843 "ARM_THUMB_RELOC_BR22",
844 "ARM_THUMB_32BIT_BRANCH",
845 "ARM_RELOC_HALF",
846 "ARM_RELOC_HALF_SECTDIFF" };
847
848 if (r_type > 9)
849 res = "Unknown";
850 else
851 res = Table[r_type];
852 break;
853 }
854 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000855 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000856 "PPC_RELOC_VANILLA",
857 "PPC_RELOC_PAIR",
858 "PPC_RELOC_BR14",
859 "PPC_RELOC_BR24",
860 "PPC_RELOC_HI16",
861 "PPC_RELOC_LO16",
862 "PPC_RELOC_HA16",
863 "PPC_RELOC_LO14",
864 "PPC_RELOC_SECTDIFF",
865 "PPC_RELOC_PB_LA_PTR",
866 "PPC_RELOC_HI16_SECTDIFF",
867 "PPC_RELOC_LO16_SECTDIFF",
868 "PPC_RELOC_HA16_SECTDIFF",
869 "PPC_RELOC_JBSR",
870 "PPC_RELOC_LO14_SECTDIFF",
871 "PPC_RELOC_LOCAL_SECTDIFF" };
872
873 res = Table[r_type];
874 break;
875 }
876 case Triple::UnknownArch:
877 res = "Unknown";
878 break;
879 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000880 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000881 return object_error::success;
882}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000883error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
884 int64_t &Res) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000885 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000886 bool isExtern = (RE->Word1 >> 27) & 1;
887 Res = 0;
888 if (!isExtern) {
889 const uint8_t* sectAddress = base();
890 if (MachOObj->is64Bit()) {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000891 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000892 sectAddress += Sect->Offset;
893 } else {
Rafael Espindola0e5dc8a2013-04-05 18:45:28 +0000894 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000895 sectAddress += Sect->Offset;
896 }
897 Res = reinterpret_cast<uintptr_t>(sectAddress);
898 }
899 return object_error::success;
900}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000901
902// Helper to advance a section or symbol iterator multiple increments at a time.
903template<class T>
904error_code advance(T &it, size_t Val) {
905 error_code ec;
906 while (Val--) {
907 it.increment(ec);
908 }
909 return ec;
910}
911
912template<class T>
913void advanceTo(T &it, size_t Val) {
914 if (error_code ec = advance(it, Val))
915 report_fatal_error(ec.message());
916}
917
Owen Anderson1832f4d2011-10-26 20:42:54 +0000918void MachOObjectFile::printRelocationTargetName(
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000919 const MachOFormat::RelocationEntry *RE,
Owen Anderson1832f4d2011-10-26 20:42:54 +0000920 raw_string_ostream &fmt) const {
921 unsigned Arch = getArch();
922 bool isScattered = (Arch != Triple::x86_64) &&
923 (RE->Word0 & macho::RF_Scattered);
924
925 // Target of a scattered relocation is an address. In the interest of
926 // generating pretty output, scan through the symbol table looking for a
927 // symbol that aligns with that address. If we find one, print it.
928 // Otherwise, we just print the hex address of the target.
929 if (isScattered) {
930 uint32_t Val = RE->Word1;
931
932 error_code ec;
933 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
934 SI.increment(ec)) {
935 if (ec) report_fatal_error(ec.message());
936
937 uint64_t Addr;
938 StringRef Name;
939
940 if ((ec = SI->getAddress(Addr)))
941 report_fatal_error(ec.message());
942 if (Addr != Val) continue;
943 if ((ec = SI->getName(Name)))
944 report_fatal_error(ec.message());
945 fmt << Name;
946 return;
947 }
948
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000949 // If we couldn't find a symbol that this relocation refers to, try
950 // to find a section beginning instead.
951 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
952 SI.increment(ec)) {
953 if (ec) report_fatal_error(ec.message());
954
955 uint64_t Addr;
956 StringRef Name;
957
958 if ((ec = SI->getAddress(Addr)))
959 report_fatal_error(ec.message());
960 if (Addr != Val) continue;
961 if ((ec = SI->getName(Name)))
962 report_fatal_error(ec.message());
963 fmt << Name;
964 return;
965 }
966
Owen Anderson1832f4d2011-10-26 20:42:54 +0000967 fmt << format("0x%x", Val);
968 return;
969 }
970
971 StringRef S;
972 bool isExtern = (RE->Word1 >> 27) & 1;
973 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000974
975 if (isExtern) {
976 symbol_iterator SI = begin_symbols();
977 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000978 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000979 } else {
980 section_iterator SI = begin_sections();
981 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000982 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000983 }
984
Owen Anderson1832f4d2011-10-26 20:42:54 +0000985 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000986}
987
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000988error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
989 SmallVectorImpl<char> &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +0000990 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0135fe12011-10-24 21:44:00 +0000991
Owen Anderson1832f4d2011-10-26 20:42:54 +0000992 unsigned Arch = getArch();
993 bool isScattered = (Arch != Triple::x86_64) &&
994 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000995
Owen Anderson013d7562011-10-25 18:48:41 +0000996 std::string fmtbuf;
997 raw_string_ostream fmt(fmtbuf);
998
Owen Anderson1832f4d2011-10-26 20:42:54 +0000999 unsigned Type;
1000 if (isScattered)
1001 Type = (RE->Word0 >> 24) & 0xF;
1002 else
1003 Type = (RE->Word1 >> 28) & 0xF;
1004
Owen Andersoneb6bd332011-10-27 20:46:09 +00001005 bool isPCRel;
1006 if (isScattered)
1007 isPCRel = ((RE->Word0 >> 30) & 1);
1008 else
1009 isPCRel = ((RE->Word1 >> 24) & 1);
1010
Owen Andersond8fa76d2011-10-24 23:20:07 +00001011 // Determine any addends that should be displayed with the relocation.
1012 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001013
1014 // X86_64 has entirely custom relocation types.
1015 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001016 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001017
Owen Andersond8fa76d2011-10-24 23:20:07 +00001018 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001019 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1020 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1021 printRelocationTargetName(RE, fmt);
1022 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001023 if (isPCRel) fmt << "PCREL";
1024 break;
1025 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001026 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001027 DataRefImpl RelNext = Rel;
1028 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001029 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001030
1031 // X86_64_SUBTRACTOR must be followed by a relocation of type
1032 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001033 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001034 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001035 if (RType != 0)
1036 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1037 "X86_64_RELOC_SUBTRACTOR.");
1038
Owen Andersonef22f782011-10-26 17:28:49 +00001039 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1040 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001041 printRelocationTargetName(RENext, fmt);
1042 fmt << "-";
1043 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001044 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001045 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001046 case macho::RIT_X86_64_TLV:
1047 printRelocationTargetName(RE, fmt);
1048 fmt << "@TLV";
1049 if (isPCRel) fmt << "P";
1050 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001051 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1052 printRelocationTargetName(RE, fmt);
1053 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001054 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001055 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1056 printRelocationTargetName(RE, fmt);
1057 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001058 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001059 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1060 printRelocationTargetName(RE, fmt);
1061 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001062 break;
1063 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001064 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001065 break;
1066 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001067 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001068 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1069 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001070 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001071 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001072 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001073 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001074 DataRefImpl RelNext = Rel;
1075 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001076 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001077
1078 // X86 sect diff's must be followed by a relocation of type
1079 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001080 bool isNextScattered = (Arch != Triple::x86_64) &&
1081 (RENext->Word0 & macho::RF_Scattered);
1082 unsigned RType;
1083 if (isNextScattered)
1084 RType = (RENext->Word0 >> 24) & 0xF;
1085 else
1086 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001087 if (RType != 1)
1088 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001089 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001090
Owen Anderson1832f4d2011-10-26 20:42:54 +00001091 printRelocationTargetName(RE, fmt);
1092 fmt << "-";
1093 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001094 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001095 }
1096 }
Owen Anderson013d7562011-10-25 18:48:41 +00001097
Owen Anderson1832f4d2011-10-26 20:42:54 +00001098 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001099 // All X86 relocations that need special printing were already
1100 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001101 switch (Type) {
1102 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
Owen Andersoneb6bd332011-10-27 20:46:09 +00001103 DataRefImpl RelNext = Rel;
1104 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001105 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Andersoneb6bd332011-10-27 20:46:09 +00001106
1107 // X86 sect diff's must be followed by a relocation of type
1108 // GENERIC_RELOC_PAIR.
1109 bool isNextScattered = (Arch != Triple::x86_64) &&
1110 (RENext->Word0 & macho::RF_Scattered);
1111 unsigned RType;
1112 if (isNextScattered)
1113 RType = (RENext->Word0 >> 24) & 0xF;
1114 else
1115 RType = (RENext->Word1 >> 28) & 0xF;
1116 if (RType != 1)
1117 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1118 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1119
1120 printRelocationTargetName(RE, fmt);
1121 fmt << "-";
1122 printRelocationTargetName(RENext, fmt);
1123 break;
1124 }
1125 case macho::RIT_Generic_TLV: {
1126 printRelocationTargetName(RE, fmt);
1127 fmt << "@TLV";
1128 if (isPCRel) fmt << "P";
1129 break;
1130 }
1131 default:
1132 printRelocationTargetName(RE, fmt);
1133 }
Owen Anderson013d7562011-10-25 18:48:41 +00001134 } else { // ARM-specific relocations
1135 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001136 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1137 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001138 // Half relocations steal a bit from the length field to encode
1139 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001140 bool isUpper;
1141 if (isScattered)
1142 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001143 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001144 isUpper = (RE->Word1 >> 25) & 1;
1145
1146 if (isUpper)
1147 fmt << ":upper16:(";
1148 else
1149 fmt << ":lower16:(";
1150 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001151
Owen Anderson013d7562011-10-25 18:48:41 +00001152 DataRefImpl RelNext = Rel;
1153 RelNext.d.a++;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001154 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
Owen Anderson013d7562011-10-25 18:48:41 +00001155
1156 // ARM half relocs must be followed by a relocation of type
1157 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001158 bool isNextScattered = (Arch != Triple::x86_64) &&
1159 (RENext->Word0 & macho::RF_Scattered);
1160 unsigned RType;
1161 if (isNextScattered)
1162 RType = (RENext->Word0 >> 24) & 0xF;
1163 else
1164 RType = (RENext->Word1 >> 28) & 0xF;
1165
Owen Anderson013d7562011-10-25 18:48:41 +00001166 if (RType != 1)
1167 report_fatal_error("Expected ARM_RELOC_PAIR after "
1168 "GENERIC_RELOC_HALF");
1169
Owen Anderson1832f4d2011-10-26 20:42:54 +00001170 // NOTE: The half of the target virtual address is stashed in the
1171 // address field of the secondary relocation, but we can't reverse
1172 // engineer the constant offset from it without decoding the movw/movt
1173 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001174
1175 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1176 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001177 if (Type == macho::RIT_ARM_HalfDifference) {
1178 fmt << "-";
1179 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001180 }
1181
Owen Anderson013d7562011-10-25 18:48:41 +00001182 fmt << ")";
1183 break;
1184 }
1185 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001186 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001187 }
1188 }
1189 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001190 } else
1191 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001192
Owen Anderson0135fe12011-10-24 21:44:00 +00001193 fmt.flush();
1194 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001195 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001196}
1197
Owen Anderson0685e942011-10-25 20:35:53 +00001198error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1199 bool &Result) const {
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001200 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Owen Anderson0685e942011-10-25 20:35:53 +00001201
Owen Anderson0685e942011-10-25 20:35:53 +00001202 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001203 bool isScattered = (Arch != Triple::x86_64) &&
1204 (RE->Word0 & macho::RF_Scattered);
1205 unsigned Type;
1206 if (isScattered)
1207 Type = (RE->Word0 >> 24) & 0xF;
1208 else
1209 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001210
1211 Result = false;
1212
1213 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1214 // is always hidden.
1215 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001216 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001217 } else if (Arch == Triple::x86_64) {
1218 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1219 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001220 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001221 DataRefImpl RelPrev = Rel;
1222 RelPrev.d.a--;
Rafael Espindola5cf0f512013-04-06 01:24:11 +00001223 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
Owen Anderson0685e942011-10-25 20:35:53 +00001224
1225 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1226
Owen Anderson1832f4d2011-10-26 20:42:54 +00001227 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001228 }
1229 }
1230
1231 return object_error::success;
1232}
1233
David Meyer5c2b4ea2012-03-01 01:36:50 +00001234error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1235 LibraryRef &Res) const {
1236 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1237}
1238
1239error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1240 StringRef &Res) const {
1241 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1242}
1243
1244
Eric Christopher6256b032011-04-22 03:19:48 +00001245/*===-- Miscellaneous -----------------------------------------------------===*/
1246
1247uint8_t MachOObjectFile::getBytesInAddress() const {
1248 return MachOObj->is64Bit() ? 8 : 4;
1249}
1250
1251StringRef MachOObjectFile::getFileFormatName() const {
1252 if (!MachOObj->is64Bit()) {
1253 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001254 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001255 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001256 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001257 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001258 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001259 return "Mach-O 32-bit ppc";
1260 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001261 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001262 "64-bit object file when we're not 64-bit?");
1263 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001264 }
1265 }
1266
Eric Christopherb3e6b042013-02-28 20:26:17 +00001267 // Make sure the cpu type has the correct mask.
1268 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1269 == llvm::MachO::CPUArchABI64 &&
1270 "32-bit object file when we're 64-bit?");
1271
Eric Christopher6256b032011-04-22 03:19:48 +00001272 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001273 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001274 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001275 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001276 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001277 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001278 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001279 }
1280}
1281
1282unsigned MachOObjectFile::getArch() const {
1283 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001284 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001285 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001286 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001287 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001288 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001289 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001290 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001291 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001292 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001293 return Triple::ppc64;
1294 default:
1295 return Triple::UnknownArch;
1296 }
1297}
1298
Owen Andersonf7c93a32011-10-11 17:32:27 +00001299} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001300} // end namespace llvm