blob: bb6ca93176ddb673daa844123001f4c01cb3d87a [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"
Rafael Espindola8764c892013-04-07 20:01:29 +000018#include "llvm/Support/DataExtractor.h"
Owen Anderson1832f4d2011-10-26 20:42:54 +000019#include "llvm/Support/Format.h"
Rafael Espindolafd7aa382013-04-18 18:08:55 +000020#include "llvm/Support/Host.h"
Eric Christopher6256b032011-04-22 03:19:48 +000021#include "llvm/Support/MemoryBuffer.h"
Eric Christopher6256b032011-04-22 03:19:48 +000022#include <cctype>
23#include <cstring>
24#include <limits>
25
26using namespace llvm;
27using namespace object;
28
29namespace llvm {
Owen Andersonf7c93a32011-10-11 17:32:27 +000030namespace object {
Eric Christopher6256b032011-04-22 03:19:48 +000031
Rafael Espindolafd7aa382013-04-18 18:08:55 +000032struct SymbolTableEntryBase {
33 uint32_t StringIndex;
34 uint8_t Type;
35 uint8_t SectionIndex;
36 uint16_t Flags;
37};
38
39struct SectionBase {
40 char Name[16];
41 char SegmentName[16];
42};
43
44template<typename T>
45static void SwapValue(T &Value) {
46 Value = sys::SwapByteOrder(Value);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000047}
48
Rafael Espindolafd7aa382013-04-18 18:08:55 +000049template<typename T>
50static void SwapStruct(T &Value);
51
52template<>
53void SwapStruct(macho::RelocationEntry &H) {
54 SwapValue(H.Word0);
55 SwapValue(H.Word1);
Rafael Espindola3eff3182013-04-07 16:07:35 +000056}
57
Rafael Espindolafd7aa382013-04-18 18:08:55 +000058template<>
59void SwapStruct(macho::LoadCommand &L) {
60 SwapValue(L.Type);
61 SwapValue(L.Size);
62}
Rafael Espindola8764c892013-04-07 20:01:29 +000063
Rafael Espindolafd7aa382013-04-18 18:08:55 +000064template<>
65void SwapStruct(SymbolTableEntryBase &S) {
66 SwapValue(S.StringIndex);
67 SwapValue(S.Flags);
68}
69
70template<>
71void SwapStruct(macho::Section &S) {
72 SwapValue(S.Address);
73 SwapValue(S.Size);
74 SwapValue(S.Offset);
75 SwapValue(S.Align);
76 SwapValue(S.RelocationTableOffset);
77 SwapValue(S.NumRelocationTableEntries);
78 SwapValue(S.Flags);
79 SwapValue(S.Reserved1);
80 SwapValue(S.Reserved2);
81}
82
83template<>
84void SwapStruct(macho::Section64 &S) {
85 SwapValue(S.Address);
86 SwapValue(S.Size);
87 SwapValue(S.Offset);
88 SwapValue(S.Align);
89 SwapValue(S.RelocationTableOffset);
90 SwapValue(S.NumRelocationTableEntries);
91 SwapValue(S.Flags);
92 SwapValue(S.Reserved1);
93 SwapValue(S.Reserved2);
94 SwapValue(S.Reserved3);
95}
96
97template<>
98void SwapStruct(macho::SymbolTableEntry &S) {
99 SwapValue(S.StringIndex);
100 SwapValue(S.Flags);
101 SwapValue(S.Value);
102}
103
104template<>
105void SwapStruct(macho::Symbol64TableEntry &S) {
106 SwapValue(S.StringIndex);
107 SwapValue(S.Flags);
108 SwapValue(S.Value);
109}
110
111template<>
112void SwapStruct(macho::Header &H) {
113 SwapValue(H.Magic);
114 SwapValue(H.CPUType);
115 SwapValue(H.CPUSubtype);
116 SwapValue(H.FileType);
117 SwapValue(H.NumLoadCommands);
118 SwapValue(H.SizeOfLoadCommands);
119 SwapValue(H.Flags);
120}
121
122template<>
Rafael Espindola2173e182013-04-26 20:07:33 +0000123void SwapStruct(macho::Header64Ext &E) {
124 SwapValue(E.Reserved);
125}
126
127template<>
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000128void SwapStruct(macho::SymtabLoadCommand &C) {
129 SwapValue(C.Type);
130 SwapValue(C.Size);
131 SwapValue(C.SymbolTableOffset);
132 SwapValue(C.NumSymbolTableEntries);
133 SwapValue(C.StringTableOffset);
134 SwapValue(C.StringTableSize);
135}
136
137template<>
Rafael Espindola2173e182013-04-26 20:07:33 +0000138void SwapStruct(macho::DysymtabLoadCommand &C) {
139 SwapValue(C.Type);
140 SwapValue(C.Size);
141 SwapValue(C.LocalSymbolsIndex);
142 SwapValue(C.NumLocalSymbols);
143 SwapValue(C.ExternalSymbolsIndex);
144 SwapValue(C.NumExternalSymbols);
145 SwapValue(C.UndefinedSymbolsIndex);
146 SwapValue(C.NumUndefinedSymbols);
147 SwapValue(C.TOCOffset);
148 SwapValue(C.NumTOCEntries);
149 SwapValue(C.ModuleTableOffset);
150 SwapValue(C.NumModuleTableEntries);
151 SwapValue(C.ReferenceSymbolTableOffset);
152 SwapValue(C.NumReferencedSymbolTableEntries);
153 SwapValue(C.IndirectSymbolTableOffset);
154 SwapValue(C.NumIndirectSymbolTableEntries);
155 SwapValue(C.ExternalRelocationTableOffset);
156 SwapValue(C.NumExternalRelocationTableEntries);
157 SwapValue(C.LocalRelocationTableOffset);
158 SwapValue(C.NumLocalRelocationTableEntries);
159}
160
161template<>
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000162void SwapStruct(macho::LinkeditDataLoadCommand &C) {
163 SwapValue(C.Type);
164 SwapValue(C.Size);
165 SwapValue(C.DataOffset);
166 SwapValue(C.DataSize);
167}
168
169template<>
170void SwapStruct(macho::SegmentLoadCommand &C) {
171 SwapValue(C.Type);
172 SwapValue(C.Size);
173 SwapValue(C.VMAddress);
174 SwapValue(C.VMSize);
175 SwapValue(C.FileOffset);
176 SwapValue(C.FileSize);
177 SwapValue(C.MaxVMProtection);
178 SwapValue(C.InitialVMProtection);
179 SwapValue(C.NumSections);
180 SwapValue(C.Flags);
181}
182
183template<>
184void SwapStruct(macho::Segment64LoadCommand &C) {
185 SwapValue(C.Type);
186 SwapValue(C.Size);
187 SwapValue(C.VMAddress);
188 SwapValue(C.VMSize);
189 SwapValue(C.FileOffset);
190 SwapValue(C.FileSize);
191 SwapValue(C.MaxVMProtection);
192 SwapValue(C.InitialVMProtection);
193 SwapValue(C.NumSections);
194 SwapValue(C.Flags);
195}
196
Rafael Espindola2173e182013-04-26 20:07:33 +0000197template<>
198void SwapStruct(macho::IndirectSymbolTableEntry &C) {
199 SwapValue(C.Index);
200}
201
202template<>
203void SwapStruct(macho::LinkerOptionsLoadCommand &C) {
204 SwapValue(C.Type);
205 SwapValue(C.Size);
206 SwapValue(C.Count);
207}
208
209template<>
210void SwapStruct(macho::DataInCodeTableEntry &C) {
211 SwapValue(C.Offset);
212 SwapValue(C.Length);
213 SwapValue(C.Kind);
214}
215
Rafael Espindola143d2232013-04-19 13:45:05 +0000216template<typename T>
217T getStruct(const MachOObjectFile *O, const char *P) {
218 T Cmd;
219 memcpy(&Cmd, P, sizeof(T));
220 if (O->isLittleEndian() != sys::IsLittleEndianHost)
221 SwapStruct(Cmd);
222 return Cmd;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000223}
224
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000225static uint32_t
226getSegmentLoadCommandNumSections(const MachOObjectFile *O,
227 const MachOObjectFile::LoadCommandInfo &L) {
228 if (O->is64Bit()) {
Rafael Espindola2173e182013-04-26 20:07:33 +0000229 macho::Segment64LoadCommand S = O->getSegment64LoadCommand(L);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000230 return S.NumSections;
Rafael Espindola8764c892013-04-07 20:01:29 +0000231 }
Rafael Espindola2173e182013-04-26 20:07:33 +0000232 macho::SegmentLoadCommand S = O->getSegmentLoadCommand(L);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000233 return S.NumSections;
Rafael Espindola3eff3182013-04-07 16:07:35 +0000234}
235
Rafael Espindola2173e182013-04-26 20:07:33 +0000236static const char *
237getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
238 unsigned Sec) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000239 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
240
241 bool Is64 = O->is64Bit();
242 unsigned SegmentLoadSize = Is64 ? sizeof(macho::Segment64LoadCommand) :
243 sizeof(macho::SegmentLoadCommand);
244 unsigned SectionSize = Is64 ? sizeof(macho::Section64) :
245 sizeof(macho::Section);
246
247 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
Rafael Espindola2173e182013-04-26 20:07:33 +0000248 return reinterpret_cast<const char*>(SectionAddr);
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000249}
250
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000251static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
252 return O->getData().substr(Offset, 1).data();
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000253}
254
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000255static SymbolTableEntryBase
256getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola802fe932013-04-24 19:47:55 +0000257 const char *P = reinterpret_cast<const char *>(DRI.p);
Rafael Espindola143d2232013-04-19 13:45:05 +0000258 return getStruct<SymbolTableEntryBase>(O, P);
Eric Christopher6256b032011-04-22 03:19:48 +0000259}
260
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000261static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000262 if (P[15] == 0)
263 // Null terminated.
264 return P;
265 // Not null terminated, so this is a 16 char string.
266 return StringRef(P, 16);
267}
268
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000269// Helper to advance a section or symbol iterator multiple increments at a time.
270template<class T>
271static error_code advance(T &it, size_t Val) {
272 error_code ec;
273 while (Val--) {
274 it.increment(ec);
275 }
276 return ec;
277}
278
279template<class T>
280static void advanceTo(T &it, size_t Val) {
281 if (error_code ec = advance(it, Val))
282 report_fatal_error(ec.message());
283}
284
285static unsigned getCPUType(const MachOObjectFile *O) {
286 return O->getHeader().CPUType;
287}
288
289static void printRelocationTargetName(const MachOObjectFile *O,
290 const macho::RelocationEntry &RE,
291 raw_string_ostream &fmt) {
292 bool IsScattered = O->isRelocationScattered(RE);
293
294 // Target of a scattered relocation is an address. In the interest of
295 // generating pretty output, scan through the symbol table looking for a
296 // symbol that aligns with that address. If we find one, print it.
297 // Otherwise, we just print the hex address of the target.
298 if (IsScattered) {
299 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
300
301 error_code ec;
302 for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
303 SI != SE; SI.increment(ec)) {
304 if (ec) report_fatal_error(ec.message());
305
306 uint64_t Addr;
307 StringRef Name;
308
309 if ((ec = SI->getAddress(Addr)))
310 report_fatal_error(ec.message());
311 if (Addr != Val) continue;
312 if ((ec = SI->getName(Name)))
313 report_fatal_error(ec.message());
314 fmt << Name;
315 return;
316 }
317
318 // If we couldn't find a symbol that this relocation refers to, try
319 // to find a section beginning instead.
320 for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
321 SI != SE; SI.increment(ec)) {
322 if (ec) report_fatal_error(ec.message());
323
324 uint64_t Addr;
325 StringRef Name;
326
327 if ((ec = SI->getAddress(Addr)))
328 report_fatal_error(ec.message());
329 if (Addr != Val) continue;
330 if ((ec = SI->getName(Name)))
331 report_fatal_error(ec.message());
332 fmt << Name;
333 return;
334 }
335
336 fmt << format("0x%x", Val);
337 return;
338 }
339
340 StringRef S;
341 bool isExtern = O->getPlainRelocationExternal(RE);
342 uint64_t Val = O->getAnyRelocationAddress(RE);
343
344 if (isExtern) {
345 symbol_iterator SI = O->begin_symbols();
346 advanceTo(SI, Val);
347 SI->getName(S);
348 } else {
349 section_iterator SI = O->begin_sections();
350 advanceTo(SI, Val);
351 SI->getName(S);
352 }
353
354 fmt << S;
355}
356
357static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) {
358 return RE.Word0;
359}
360
361static unsigned
362getScatteredRelocationAddress(const macho::RelocationEntry &RE) {
363 return RE.Word0 & 0xffffff;
364}
365
366static bool getPlainRelocationPCRel(const MachOObjectFile *O,
367 const macho::RelocationEntry &RE) {
368 if (O->isLittleEndian())
369 return (RE.Word1 >> 24) & 1;
370 return (RE.Word1 >> 7) & 1;
371}
372
373static bool
374getScatteredRelocationPCRel(const MachOObjectFile *O,
375 const macho::RelocationEntry &RE) {
376 return (RE.Word0 >> 30) & 1;
377}
378
379static unsigned getPlainRelocationLength(const MachOObjectFile *O,
380 const macho::RelocationEntry &RE) {
381 if (O->isLittleEndian())
382 return (RE.Word1 >> 25) & 3;
383 return (RE.Word1 >> 5) & 3;
384}
385
386static unsigned
387getScatteredRelocationLength(const macho::RelocationEntry &RE) {
388 return (RE.Word0 >> 28) & 3;
389}
390
391static unsigned getPlainRelocationType(const MachOObjectFile *O,
392 const macho::RelocationEntry &RE) {
393 if (O->isLittleEndian())
394 return RE.Word1 >> 28;
395 return RE.Word1 & 0xf;
396}
397
398static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) {
399 return (RE.Word0 >> 24) & 0xf;
400}
401
402static uint32_t getSectionFlags(const MachOObjectFile *O,
403 DataRefImpl Sec) {
404 if (O->is64Bit()) {
405 macho::Section64 Sect = O->getSection64(Sec);
406 return Sect.Flags;
407 }
408 macho::Section Sect = O->getSection(Sec);
409 return Sect.Flags;
410}
411
412MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
413 bool IsLittleEndian, bool Is64bits,
414 error_code &ec)
415 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
Rafael Espindola2173e182013-04-26 20:07:33 +0000416 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000417 uint32_t LoadCommandCount = this->getHeader().NumLoadCommands;
418 macho::LoadCommandType SegmentLoadType = is64Bit() ?
419 macho::LCT_Segment64 : macho::LCT_Segment;
420
421 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindoladb5f9272013-04-19 11:36:47 +0000422 for (unsigned I = 0; ; ++I) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000423 if (Load.C.Type == macho::LCT_Symtab) {
424 assert(!SymtabLoadCmd && "Multiple symbol tables");
425 SymtabLoadCmd = Load.Ptr;
Rafael Espindola2173e182013-04-26 20:07:33 +0000426 } else if (Load.C.Type == macho::LCT_Dysymtab) {
427 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
428 DysymtabLoadCmd = Load.Ptr;
429 } else if (Load.C.Type == SegmentLoadType) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000430 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
431 for (unsigned J = 0; J < NumSections; ++J) {
Rafael Espindola2173e182013-04-26 20:07:33 +0000432 const char *Sec = getSectionPtr(this, Load, J);
433 Sections.push_back(Sec);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000434 }
435 }
Rafael Espindoladb5f9272013-04-19 11:36:47 +0000436
437 if (I == LoadCommandCount - 1)
438 break;
439 else
440 Load = getNextLoadCommandInfo(Load);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000441 }
442}
443
444error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
445 SymbolRef &Res) const {
Rafael Espindola802fe932013-04-24 19:47:55 +0000446 unsigned SymbolTableEntrySize = is64Bit() ?
447 sizeof(macho::Symbol64TableEntry) :
448 sizeof(macho::SymbolTableEntry);
449 Symb.p += SymbolTableEntrySize;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000450 Res = SymbolRef(Symb, this);
451 return object_error::success;
452}
453
454error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
455 StringRef &Res) const {
Rafael Espindola2173e182013-04-26 20:07:33 +0000456 StringRef StringTable = getStringTableData();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000457 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
Rafael Espindola2173e182013-04-26 20:07:33 +0000458 const char *Start = &StringTable.data()[Entry.StringIndex];
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000459 Res = StringRef(Start);
460 return object_error::success;
461}
462
463error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
464 uint64_t &Res) const {
465 if (is64Bit()) {
466 macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb);
467 Res = Entry.Value;
468 } else {
469 macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb);
470 Res = Entry.Value;
471 }
472 return object_error::success;
473}
474
475error_code
476MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
477 uint64_t &Res) const {
478 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
479 getSymbolAddress(Symb, Res);
480 if (Entry.SectionIndex) {
481 uint64_t Delta;
482 DataRefImpl SecRel;
483 SecRel.d.a = Entry.SectionIndex-1;
484 if (is64Bit()) {
485 macho::Section64 Sec = getSection64(SecRel);
486 Delta = Sec.Offset - Sec.Address;
487 } else {
488 macho::Section Sec = getSection(SecRel);
489 Delta = Sec.Offset - Sec.Address;
490 }
491
492 Res += Delta;
493 }
494
495 return object_error::success;
496}
497
498error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
499 uint64_t &Result) const {
500 uint64_t BeginOffset;
501 uint64_t EndOffset = 0;
502 uint8_t SectionIndex;
503
504 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
505 uint64_t Value;
506 getSymbolAddress(DRI, Value);
507
508 BeginOffset = Value;
509
510 SectionIndex = Entry.SectionIndex;
511 if (!SectionIndex) {
512 uint32_t flags = SymbolRef::SF_None;
513 this->getSymbolFlags(DRI, flags);
514 if (flags & SymbolRef::SF_Common)
515 Result = Value;
516 else
517 Result = UnknownAddressOrSize;
518 return object_error::success;
519 }
520 // Unfortunately symbols are unsorted so we need to touch all
521 // symbols from load command
Rafael Espindola802fe932013-04-24 19:47:55 +0000522 error_code ec;
523 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
524 I.increment(ec)) {
525 DataRefImpl DRI = I->getRawDataRefImpl();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000526 Entry = getSymbolTableEntryBase(this, DRI);
527 getSymbolAddress(DRI, Value);
528 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
529 if (!EndOffset || Value < EndOffset)
530 EndOffset = Value;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000531 }
532 if (!EndOffset) {
533 uint64_t Size;
534 DataRefImpl Sec;
535 Sec.d.a = SectionIndex-1;
536 getSectionSize(Sec, Size);
537 getSectionAddress(Sec, EndOffset);
538 EndOffset += Size;
539 }
540 Result = EndOffset - BeginOffset;
541 return object_error::success;
542}
543
544error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
545 SymbolRef::Type &Res) const {
546 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
547 uint8_t n_type = Entry.Type;
548
549 Res = SymbolRef::ST_Other;
550
551 // If this is a STAB debugging symbol, we can do nothing more.
552 if (n_type & MachO::NlistMaskStab) {
553 Res = SymbolRef::ST_Debug;
554 return object_error::success;
555 }
556
557 switch (n_type & MachO::NlistMaskType) {
558 case MachO::NListTypeUndefined :
559 Res = SymbolRef::ST_Unknown;
560 break;
561 case MachO::NListTypeSection :
562 Res = SymbolRef::ST_Function;
563 break;
564 }
565 return object_error::success;
566}
567
568error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
569 char &Res) const {
570 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
571 uint8_t Type = Entry.Type;
572 uint16_t Flags = Entry.Flags;
573
574 char Char;
575 switch (Type & macho::STF_TypeMask) {
576 case macho::STT_Undefined:
577 Char = 'u';
578 break;
579 case macho::STT_Absolute:
580 case macho::STT_Section:
581 Char = 's';
582 break;
583 default:
584 Char = '?';
585 break;
586 }
587
588 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
589 Char = toupper(static_cast<unsigned char>(Char));
590 Res = Char;
591 return object_error::success;
592}
593
594error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
595 uint32_t &Result) const {
596 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
597
598 uint8_t MachOType = Entry.Type;
599 uint16_t MachOFlags = Entry.Flags;
600
601 // TODO: Correctly set SF_ThreadLocal
602 Result = SymbolRef::SF_None;
603
604 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
605 Result |= SymbolRef::SF_Undefined;
606
607 if (MachOFlags & macho::STF_StabsEntryMask)
608 Result |= SymbolRef::SF_FormatSpecific;
609
610 if (MachOType & MachO::NlistMaskExternal) {
611 Result |= SymbolRef::SF_Global;
612 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
613 Result |= SymbolRef::SF_Common;
614 }
615
616 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
617 Result |= SymbolRef::SF_Weak;
618
619 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
620 Result |= SymbolRef::SF_Absolute;
621
622 return object_error::success;
623}
624
625error_code
626MachOObjectFile::getSymbolSection(DataRefImpl Symb,
627 section_iterator &Res) const {
628 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
629 uint8_t index = Entry.SectionIndex;
630
631 if (index == 0) {
632 Res = end_sections();
633 } else {
634 DataRefImpl DRI;
635 DRI.d.a = index - 1;
636 Res = section_iterator(SectionRef(DRI, this));
637 }
638
639 return object_error::success;
640}
641
642error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000643 uint64_t &Val) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000644 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
645}
646
647error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
648 SectionRef &Res) const {
649 Sec.d.a++;
650 Res = SectionRef(Sec, this);
651 return object_error::success;
652}
653
654error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000655MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000656 ArrayRef<char> Raw = getSectionRawName(Sec);
657 Result = parseSegmentOrSectionName(Raw.data());
658 return object_error::success;
659}
660
661error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000662MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000663 if (is64Bit()) {
664 macho::Section64 Sect = getSection64(Sec);
665 Res = Sect.Address;
666 } else {
667 macho::Section Sect = getSection(Sec);
668 Res = Sect.Address;
669 }
670 return object_error::success;
671}
672
673error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000674MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000675 if (is64Bit()) {
676 macho::Section64 Sect = getSection64(Sec);
677 Res = Sect.Size;
678 } else {
679 macho::Section Sect = getSection(Sec);
680 Res = Sect.Size;
681 }
682
683 return object_error::success;
684}
685
686error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000687MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000688 uint32_t Offset;
689 uint64_t Size;
690
691 if (is64Bit()) {
692 macho::Section64 Sect = getSection64(Sec);
693 Offset = Sect.Offset;
694 Size = Sect.Size;
695 } else {
696 macho::Section Sect =getSection(Sec);
697 Offset = Sect.Offset;
698 Size = Sect.Size;
699 }
700
701 Res = this->getData().substr(Offset, Size);
702 return object_error::success;
703}
704
705error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000706MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000707 uint32_t Align;
708 if (is64Bit()) {
709 macho::Section64 Sect = getSection64(Sec);
710 Align = Sect.Align;
711 } else {
712 macho::Section Sect = getSection(Sec);
713 Align = Sect.Align;
714 }
715
716 Res = uint64_t(1) << Align;
717 return object_error::success;
718}
719
720error_code
721MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
722 uint32_t Flags = getSectionFlags(this, Sec);
723 Res = Flags & macho::SF_PureInstructions;
724 return object_error::success;
725}
726
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000727error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000728 // FIXME: Unimplemented.
729 Result = false;
730 return object_error::success;
731}
732
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000733error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000734 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000735 Result = false;
736 return object_error::success;
737}
738
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000739error_code
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000740MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000741 bool &Result) const {
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000742 // FIXME: Unimplemented.
743 Result = true;
Preston Gurdc68dda82012-04-12 20:13:57 +0000744 return object_error::success;
745}
746
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000747error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000748 bool &Result) const {
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000749 // FIXME: Unimplemented.
750 Result = false;
751 return object_error::success;
752}
753
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000754error_code
755MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
756 uint32_t Flags = getSectionFlags(this, Sec);
757 unsigned SectionType = Flags & MachO::SectionFlagMaskSectionType;
758 Res = SectionType == MachO::SectionTypeZeroFill ||
759 SectionType == MachO::SectionTypeZeroFillLarge;
760 return object_error::success;
761}
762
763error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000764 bool &Result) const {
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000765 // Consider using the code from isSectionText to look for __const sections.
766 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
767 // to use section attributes to distinguish code from data.
768
769 // FIXME: Unimplemented.
770 Result = false;
771 return object_error::success;
772}
773
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000774error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000775MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000776 bool &Result) const {
777 SymbolRef::Type ST;
778 this->getSymbolType(Symb, ST);
779 if (ST == SymbolRef::ST_Unknown) {
780 Result = false;
781 return object_error::success;
782 }
783
784 uint64_t SectBegin, SectEnd;
785 getSectionAddress(Sec, SectBegin);
786 getSectionSize(Sec, SectEnd);
787 SectEnd += SectBegin;
788
789 uint64_t SymAddr;
790 getSymbolAddress(Symb, SymAddr);
791 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
792
793 return object_error::success;
794}
795
796relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +0000797 uint32_t Offset;
798 if (is64Bit()) {
799 macho::Section64 Sect = getSection64(Sec);
800 Offset = Sect.RelocationTableOffset;
801 } else {
802 macho::Section Sect = getSection(Sec);
803 Offset = Sect.RelocationTableOffset;
804 }
805
806 DataRefImpl Ret;
807 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
808 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000809}
Rafael Espindola335f1d42013-04-08 20:45:01 +0000810
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000811relocation_iterator
812MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +0000813 uint32_t Offset;
814 uint32_t Num;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000815 if (is64Bit()) {
816 macho::Section64 Sect = getSection64(Sec);
Rafael Espindolae5330f72013-04-25 12:45:46 +0000817 Offset = Sect.RelocationTableOffset;
818 Num = Sect.NumRelocationTableEntries;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000819 } else {
820 macho::Section Sect = getSection(Sec);
Rafael Espindolae5330f72013-04-25 12:45:46 +0000821 Offset = Sect.RelocationTableOffset;
822 Num = Sect.NumRelocationTableEntries;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000823 }
Eric Christopher6256b032011-04-22 03:19:48 +0000824
Rafael Espindolae5330f72013-04-25 12:45:46 +0000825 const macho::RelocationEntry *P =
826 reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset));
827
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000828 DataRefImpl Ret;
Rafael Espindolae5330f72013-04-25 12:45:46 +0000829 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000830 return relocation_iterator(RelocationRef(Ret, this));
831}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000832
Rafael Espindolae5330f72013-04-25 12:45:46 +0000833error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
834 RelocationRef &Res) const {
835 const macho::RelocationEntry *P =
836 reinterpret_cast<const macho::RelocationEntry *>(Rel.p);
837 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000838 Res = RelocationRef(Rel, this);
839 return object_error::success;
840}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000841
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000842error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000843MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola956ca722013-04-25 12:28:45 +0000844 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000845}
846
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000847error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000848 uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000849 macho::RelocationEntry RE = getRelocation(Rel);
850 Res = getAnyRelocationAddress(RE);
851 return object_error::success;
David Meyer5c2b4ea2012-03-01 01:36:50 +0000852}
853
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000854error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000855MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000856 macho::RelocationEntry RE = getRelocation(Rel);
857 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
858 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola802fe932013-04-24 19:47:55 +0000859 if (!isExtern) {
860 Res = *end_symbols();
861 return object_error::success;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000862 }
Rafael Espindola802fe932013-04-24 19:47:55 +0000863
864 macho::SymtabLoadCommand S = getSymtabLoadCommand();
865 unsigned SymbolTableEntrySize = is64Bit() ?
866 sizeof(macho::Symbol64TableEntry) :
867 sizeof(macho::SymbolTableEntry);
868 uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize;
869 DataRefImpl Sym;
870 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000871 Res = SymbolRef(Sym, this);
872 return object_error::success;
873}
874
875error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000876 uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000877 macho::RelocationEntry RE = getRelocation(Rel);
878 Res = getAnyRelocationType(RE);
879 return object_error::success;
880}
881
882error_code
883MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
884 SmallVectorImpl<char> &Result) const {
885 StringRef res;
886 uint64_t RType;
887 getRelocationType(Rel, RType);
888
889 unsigned Arch = this->getArch();
890
891 switch (Arch) {
892 case Triple::x86: {
893 static const char *const Table[] = {
894 "GENERIC_RELOC_VANILLA",
895 "GENERIC_RELOC_PAIR",
896 "GENERIC_RELOC_SECTDIFF",
897 "GENERIC_RELOC_PB_LA_PTR",
898 "GENERIC_RELOC_LOCAL_SECTDIFF",
899 "GENERIC_RELOC_TLV" };
900
901 if (RType > 6)
902 res = "Unknown";
903 else
904 res = Table[RType];
905 break;
906 }
907 case Triple::x86_64: {
908 static const char *const Table[] = {
909 "X86_64_RELOC_UNSIGNED",
910 "X86_64_RELOC_SIGNED",
911 "X86_64_RELOC_BRANCH",
912 "X86_64_RELOC_GOT_LOAD",
913 "X86_64_RELOC_GOT",
914 "X86_64_RELOC_SUBTRACTOR",
915 "X86_64_RELOC_SIGNED_1",
916 "X86_64_RELOC_SIGNED_2",
917 "X86_64_RELOC_SIGNED_4",
918 "X86_64_RELOC_TLV" };
919
920 if (RType > 9)
921 res = "Unknown";
922 else
923 res = Table[RType];
924 break;
925 }
926 case Triple::arm: {
927 static const char *const Table[] = {
928 "ARM_RELOC_VANILLA",
929 "ARM_RELOC_PAIR",
930 "ARM_RELOC_SECTDIFF",
931 "ARM_RELOC_LOCAL_SECTDIFF",
932 "ARM_RELOC_PB_LA_PTR",
933 "ARM_RELOC_BR24",
934 "ARM_THUMB_RELOC_BR22",
935 "ARM_THUMB_32BIT_BRANCH",
936 "ARM_RELOC_HALF",
937 "ARM_RELOC_HALF_SECTDIFF" };
938
939 if (RType > 9)
940 res = "Unknown";
941 else
942 res = Table[RType];
943 break;
944 }
945 case Triple::ppc: {
946 static const char *const Table[] = {
947 "PPC_RELOC_VANILLA",
948 "PPC_RELOC_PAIR",
949 "PPC_RELOC_BR14",
950 "PPC_RELOC_BR24",
951 "PPC_RELOC_HI16",
952 "PPC_RELOC_LO16",
953 "PPC_RELOC_HA16",
954 "PPC_RELOC_LO14",
955 "PPC_RELOC_SECTDIFF",
956 "PPC_RELOC_PB_LA_PTR",
957 "PPC_RELOC_HI16_SECTDIFF",
958 "PPC_RELOC_LO16_SECTDIFF",
959 "PPC_RELOC_HA16_SECTDIFF",
960 "PPC_RELOC_JBSR",
961 "PPC_RELOC_LO14_SECTDIFF",
962 "PPC_RELOC_LOCAL_SECTDIFF" };
963
964 res = Table[RType];
965 break;
966 }
967 case Triple::UnknownArch:
968 res = "Unknown";
969 break;
970 }
971 Result.append(res.begin(), res.end());
972 return object_error::success;
973}
974
975error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
976 int64_t &Res) const {
Rafael Espindola8bf80062013-04-11 02:21:31 +0000977 Res = 0;
978 return object_error::success;
979}
980
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000981error_code
982MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000983 SmallVectorImpl<char> &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000984 macho::RelocationEntry RE = getRelocation(Rel);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000985
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000986 unsigned Arch = this->getArch();
Eric Christopher6256b032011-04-22 03:19:48 +0000987
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000988 std::string fmtbuf;
989 raw_string_ostream fmt(fmtbuf);
990 unsigned Type = this->getAnyRelocationType(RE);
991 bool IsPCRel = this->getAnyRelocationPCRel(RE);
992
993 // Determine any addends that should be displayed with the relocation.
994 // These require decoding the relocation type, which is triple-specific.
995
996 // X86_64 has entirely custom relocation types.
997 if (Arch == Triple::x86_64) {
998 bool isPCRel = getAnyRelocationPCRel(RE);
999
1000 switch (Type) {
1001 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1002 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1003 printRelocationTargetName(this, RE, fmt);
1004 fmt << "@GOT";
1005 if (isPCRel) fmt << "PCREL";
1006 break;
1007 }
1008 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1009 DataRefImpl RelNext = Rel;
1010 RelNext.d.a++;
1011 macho::RelocationEntry RENext = getRelocation(RelNext);
1012
1013 // X86_64_SUBTRACTOR must be followed by a relocation of type
1014 // X86_64_RELOC_UNSIGNED.
1015 // NOTE: Scattered relocations don't exist on x86_64.
1016 unsigned RType = getAnyRelocationType(RENext);
1017 if (RType != 0)
1018 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1019 "X86_64_RELOC_SUBTRACTOR.");
1020
1021 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1022 // X86_64_SUBTRACTOR contains to the subtrahend.
1023 printRelocationTargetName(this, RENext, fmt);
1024 fmt << "-";
1025 printRelocationTargetName(this, RE, fmt);
1026 break;
1027 }
1028 case macho::RIT_X86_64_TLV:
1029 printRelocationTargetName(this, RE, fmt);
1030 fmt << "@TLV";
1031 if (isPCRel) fmt << "P";
1032 break;
1033 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1034 printRelocationTargetName(this, RE, fmt);
1035 fmt << "-1";
1036 break;
1037 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1038 printRelocationTargetName(this, RE, fmt);
1039 fmt << "-2";
1040 break;
1041 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1042 printRelocationTargetName(this, RE, fmt);
1043 fmt << "-4";
1044 break;
1045 default:
1046 printRelocationTargetName(this, RE, fmt);
1047 break;
1048 }
1049 // X86 and ARM share some relocation types in common.
1050 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1051 // Generic relocation types...
1052 switch (Type) {
1053 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1054 return object_error::success;
1055 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1056 DataRefImpl RelNext = Rel;
1057 RelNext.d.a++;
1058 macho::RelocationEntry RENext = getRelocation(RelNext);
1059
1060 // X86 sect diff's must be followed by a relocation of type
1061 // GENERIC_RELOC_PAIR.
1062 unsigned RType = getAnyRelocationType(RENext);
1063
1064 if (RType != 1)
1065 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1066 "GENERIC_RELOC_SECTDIFF.");
1067
1068 printRelocationTargetName(this, RE, fmt);
1069 fmt << "-";
1070 printRelocationTargetName(this, RENext, fmt);
1071 break;
1072 }
1073 }
1074
1075 if (Arch == Triple::x86) {
1076 // All X86 relocations that need special printing were already
1077 // handled in the generic code.
1078 switch (Type) {
1079 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1080 DataRefImpl RelNext = Rel;
1081 RelNext.d.a++;
1082 macho::RelocationEntry RENext = getRelocation(RelNext);
1083
1084 // X86 sect diff's must be followed by a relocation of type
1085 // GENERIC_RELOC_PAIR.
1086 unsigned RType = getAnyRelocationType(RENext);
1087 if (RType != 1)
1088 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1089 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1090
1091 printRelocationTargetName(this, RE, fmt);
1092 fmt << "-";
1093 printRelocationTargetName(this, RENext, fmt);
1094 break;
1095 }
1096 case macho::RIT_Generic_TLV: {
1097 printRelocationTargetName(this, RE, fmt);
1098 fmt << "@TLV";
1099 if (IsPCRel) fmt << "P";
1100 break;
1101 }
1102 default:
1103 printRelocationTargetName(this, RE, fmt);
1104 }
1105 } else { // ARM-specific relocations
1106 switch (Type) {
1107 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1108 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1109 // Half relocations steal a bit from the length field to encode
1110 // whether this is an upper16 or a lower16 relocation.
1111 bool isUpper = getAnyRelocationLength(RE) >> 1;
1112
1113 if (isUpper)
1114 fmt << ":upper16:(";
1115 else
1116 fmt << ":lower16:(";
1117 printRelocationTargetName(this, RE, fmt);
1118
1119 DataRefImpl RelNext = Rel;
1120 RelNext.d.a++;
1121 macho::RelocationEntry RENext = getRelocation(RelNext);
1122
1123 // ARM half relocs must be followed by a relocation of type
1124 // ARM_RELOC_PAIR.
1125 unsigned RType = getAnyRelocationType(RENext);
1126 if (RType != 1)
1127 report_fatal_error("Expected ARM_RELOC_PAIR after "
1128 "GENERIC_RELOC_HALF");
1129
1130 // NOTE: The half of the target virtual address is stashed in the
1131 // address field of the secondary relocation, but we can't reverse
1132 // engineer the constant offset from it without decoding the movw/movt
1133 // instruction to find the other half in its immediate field.
1134
1135 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1136 // symbol/section pointer of the follow-on relocation.
1137 if (Type == macho::RIT_ARM_HalfDifference) {
1138 fmt << "-";
1139 printRelocationTargetName(this, RENext, fmt);
1140 }
1141
1142 fmt << ")";
1143 break;
1144 }
1145 default: {
1146 printRelocationTargetName(this, RE, fmt);
1147 }
1148 }
1149 }
1150 } else
1151 printRelocationTargetName(this, RE, fmt);
1152
1153 fmt.flush();
1154 Result.append(fmtbuf.begin(), fmtbuf.end());
1155 return object_error::success;
1156}
1157
1158error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001159MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001160 unsigned Arch = getArch();
1161 uint64_t Type;
1162 getRelocationType(Rel, Type);
1163
1164 Result = false;
1165
1166 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1167 // is always hidden.
1168 if (Arch == Triple::x86 || Arch == Triple::arm) {
1169 if (Type == macho::RIT_Pair) Result = true;
1170 } else if (Arch == Triple::x86_64) {
1171 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1172 // an X864_64_RELOC_SUBTRACTOR.
1173 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1174 DataRefImpl RelPrev = Rel;
1175 RelPrev.d.a--;
1176 uint64_t PrevType;
1177 getRelocationType(RelPrev, PrevType);
1178 if (PrevType == macho::RIT_X86_64_Subtractor)
1179 Result = true;
1180 }
1181 }
1182
1183 return object_error::success;
1184}
1185
1186error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001187 LibraryRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001188 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1189}
1190
1191error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001192 StringRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001193 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1194}
1195
1196symbol_iterator MachOObjectFile::begin_symbols() const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001197 DataRefImpl DRI;
Rafael Espindola802fe932013-04-24 19:47:55 +00001198 if (!SymtabLoadCmd)
1199 return symbol_iterator(SymbolRef(DRI, this));
1200
1201 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1202 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001203 return symbol_iterator(SymbolRef(DRI, this));
1204}
1205
1206symbol_iterator MachOObjectFile::end_symbols() const {
1207 DataRefImpl DRI;
Rafael Espindola802fe932013-04-24 19:47:55 +00001208 if (!SymtabLoadCmd)
1209 return symbol_iterator(SymbolRef(DRI, this));
1210
1211 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1212 unsigned SymbolTableEntrySize = is64Bit() ?
1213 sizeof(macho::Symbol64TableEntry) :
1214 sizeof(macho::SymbolTableEntry);
1215 unsigned Offset = Symtab.SymbolTableOffset +
1216 Symtab.NumSymbolTableEntries * SymbolTableEntrySize;
1217 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001218 return symbol_iterator(SymbolRef(DRI, this));
1219}
1220
1221symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1222 // TODO: implement
1223 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1224}
1225
1226symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1227 // TODO: implement
1228 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1229}
1230
1231section_iterator MachOObjectFile::begin_sections() const {
1232 DataRefImpl DRI;
1233 return section_iterator(SectionRef(DRI, this));
1234}
1235
1236section_iterator MachOObjectFile::end_sections() const {
1237 DataRefImpl DRI;
1238 DRI.d.a = Sections.size();
1239 return section_iterator(SectionRef(DRI, this));
1240}
1241
1242library_iterator MachOObjectFile::begin_libraries_needed() const {
1243 // TODO: implement
1244 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1245}
1246
1247library_iterator MachOObjectFile::end_libraries_needed() const {
1248 // TODO: implement
1249 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1250}
1251
1252uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +00001253 return is64Bit() ? 8 : 4;
Eric Christopher6256b032011-04-22 03:19:48 +00001254}
1255
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001256StringRef MachOObjectFile::getFileFormatName() const {
1257 unsigned CPUType = getCPUType(this);
1258 if (!is64Bit()) {
1259 switch (CPUType) {
1260 case llvm::MachO::CPUTypeI386:
1261 return "Mach-O 32-bit i386";
1262 case llvm::MachO::CPUTypeARM:
1263 return "Mach-O arm";
1264 case llvm::MachO::CPUTypePowerPC:
1265 return "Mach-O 32-bit ppc";
1266 default:
1267 assert((CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1268 "64-bit object file when we're not 64-bit?");
1269 return "Mach-O 32-bit unknown";
1270 }
1271 }
1272
1273 // Make sure the cpu type has the correct mask.
1274 assert((CPUType & llvm::MachO::CPUArchABI64)
1275 == llvm::MachO::CPUArchABI64 &&
1276 "32-bit object file when we're 64-bit?");
1277
1278 switch (CPUType) {
1279 case llvm::MachO::CPUTypeX86_64:
1280 return "Mach-O 64-bit x86-64";
1281 case llvm::MachO::CPUTypePowerPC64:
1282 return "Mach-O 64-bit ppc64";
1283 default:
1284 return "Mach-O 64-bit unknown";
1285 }
1286}
1287
1288unsigned MachOObjectFile::getArch() const {
1289 switch (getCPUType(this)) {
1290 case llvm::MachO::CPUTypeI386:
1291 return Triple::x86;
1292 case llvm::MachO::CPUTypeX86_64:
1293 return Triple::x86_64;
1294 case llvm::MachO::CPUTypeARM:
1295 return Triple::arm;
1296 case llvm::MachO::CPUTypePowerPC:
1297 return Triple::ppc;
1298 case llvm::MachO::CPUTypePowerPC64:
1299 return Triple::ppc64;
1300 default:
1301 return Triple::UnknownArch;
1302 }
1303}
1304
1305StringRef MachOObjectFile::getLoadName() const {
1306 // TODO: Implement
1307 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1308}
1309
Rafael Espindola2173e182013-04-26 20:07:33 +00001310relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const {
1311 DataRefImpl DRI;
1312 DRI.d.a = Index;
1313 return getSectionRelBegin(DRI);
1314}
1315
1316relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const {
1317 DataRefImpl DRI;
1318 DRI.d.a = Index;
1319 return getSectionRelEnd(DRI);
1320}
1321
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001322StringRef
1323MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1324 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1325 return parseSegmentOrSectionName(Raw.data());
1326}
1327
1328ArrayRef<char>
1329MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1330 const SectionBase *Base =
1331 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1332 return ArrayRef<char>(Base->Name);
1333}
1334
1335ArrayRef<char>
1336MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1337 const SectionBase *Base =
1338 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1339 return ArrayRef<char>(Base->SegmentName);
1340}
1341
1342bool
1343MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
1344 const {
1345 if (getCPUType(this) == llvm::MachO::CPUTypeX86_64)
1346 return false;
1347 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
1348}
1349
1350unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const {
1351 if (isLittleEndian())
1352 return RE.Word1 & 0xffffff;
1353 return RE.Word1 >> 8;
1354}
1355
1356bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const {
1357 if (isLittleEndian())
1358 return (RE.Word1 >> 27) & 1;
1359 return (RE.Word1 >> 4) & 1;
1360}
1361
1362bool
1363MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const {
1364 return RE.Word0 >> 31;
1365}
1366
1367uint32_t
1368MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const {
1369 return RE.Word1;
1370}
1371
1372unsigned
1373MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const {
1374 if (isRelocationScattered(RE))
1375 return getScatteredRelocationAddress(RE);
1376 return getPlainRelocationAddress(RE);
1377}
1378
1379unsigned
1380MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
1381 if (isRelocationScattered(RE))
1382 return getScatteredRelocationPCRel(this, RE);
1383 return getPlainRelocationPCRel(this, RE);
1384}
1385
1386unsigned
1387MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const {
1388 if (isRelocationScattered(RE))
1389 return getScatteredRelocationLength(RE);
1390 return getPlainRelocationLength(this, RE);
1391}
1392
1393unsigned
1394MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
1395 if (isRelocationScattered(RE))
1396 return getScatteredRelocationType(RE);
1397 return getPlainRelocationType(this, RE);
1398}
1399
1400MachOObjectFile::LoadCommandInfo
1401MachOObjectFile::getFirstLoadCommandInfo() const {
1402 MachOObjectFile::LoadCommandInfo Load;
1403
1404 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
1405 Load.Ptr = getPtr(this, HeaderSize);
Rafael Espindola143d2232013-04-19 13:45:05 +00001406 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001407 return Load;
1408}
1409
1410MachOObjectFile::LoadCommandInfo
1411MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1412 MachOObjectFile::LoadCommandInfo Next;
1413 Next.Ptr = L.Ptr + L.C.Size;
Rafael Espindola143d2232013-04-19 13:45:05 +00001414 Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001415 return Next;
1416}
1417
1418macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001419 return getStruct<macho::Section>(this, Sections[DRI.d.a]);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001420}
1421
1422macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001423 return getStruct<macho::Section64>(this, Sections[DRI.d.a]);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001424}
1425
Rafael Espindola2173e182013-04-26 20:07:33 +00001426macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L,
1427 unsigned Index) const {
1428 const char *Sec = getSectionPtr(this, L, Index);
1429 return getStruct<macho::Section>(this, Sec);
1430}
1431
1432macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1433 unsigned Index) const {
1434 const char *Sec = getSectionPtr(this, L, Index);
1435 return getStruct<macho::Section64>(this, Sec);
1436}
1437
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001438macho::SymbolTableEntry
1439MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola802fe932013-04-24 19:47:55 +00001440 const char *P = reinterpret_cast<const char *>(DRI.p);
Rafael Espindola143d2232013-04-19 13:45:05 +00001441 return getStruct<macho::SymbolTableEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001442}
1443
1444macho::Symbol64TableEntry
1445MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola802fe932013-04-24 19:47:55 +00001446 const char *P = reinterpret_cast<const char *>(DRI.p);
Rafael Espindola143d2232013-04-19 13:45:05 +00001447 return getStruct<macho::Symbol64TableEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001448}
1449
1450macho::LinkeditDataLoadCommand
1451MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001452 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001453}
1454
Rafael Espindola2173e182013-04-26 20:07:33 +00001455macho::SegmentLoadCommand
1456MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1457 return getStruct<macho::SegmentLoadCommand>(this, L.Ptr);
1458}
1459
1460macho::Segment64LoadCommand
1461MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1462 return getStruct<macho::Segment64LoadCommand>(this, L.Ptr);
1463}
1464
1465macho::LinkerOptionsLoadCommand
1466MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1467 return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr);
1468}
1469
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001470macho::RelocationEntry
1471MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +00001472 const char *P = reinterpret_cast<const char *>(Rel.p);
1473 return getStruct<macho::RelocationEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001474}
1475
1476macho::Header MachOObjectFile::getHeader() const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001477 return getStruct<macho::Header>(this, getPtr(this, 0));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001478}
1479
Rafael Espindola2173e182013-04-26 20:07:33 +00001480macho::Header64Ext MachOObjectFile::getHeader64Ext() const {
1481 return
1482 getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header)));
1483}
1484
1485macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry(
1486 const macho::DysymtabLoadCommand &DLC,
1487 unsigned Index) const {
1488 uint64_t Offset = DLC.IndirectSymbolTableOffset +
1489 Index * sizeof(macho::IndirectSymbolTableEntry);
1490 return getStruct<macho::IndirectSymbolTableEntry>(this, getPtr(this, Offset));
1491}
1492
1493macho::DataInCodeTableEntry
1494MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1495 unsigned Index) const {
1496 uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry);
1497 return getStruct<macho::DataInCodeTableEntry>(this, getPtr(this, Offset));
1498}
1499
1500macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001501 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001502}
1503
Rafael Espindola2173e182013-04-26 20:07:33 +00001504macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const {
1505 return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd);
1506}
1507
1508StringRef MachOObjectFile::getStringTableData() const {
1509 macho::SymtabLoadCommand S = getSymtabLoadCommand();
1510 return getData().substr(S.StringTableOffset, S.StringTableSize);
1511}
1512
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001513bool MachOObjectFile::is64Bit() const {
1514 return getType() == getMachOType(false, true) ||
1515 getType() == getMachOType(true, true);
1516}
1517
1518void MachOObjectFile::ReadULEB128s(uint64_t Index,
1519 SmallVectorImpl<uint64_t> &Out) const {
1520 DataExtractor extractor(ObjectFile::getData(), true, 0);
1521
1522 uint32_t offset = Index;
1523 uint64_t data = 0;
1524 while (uint64_t delta = extractor.getULEB128(&offset)) {
1525 data += delta;
1526 Out.push_back(data);
1527 }
1528}
1529
1530ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1531 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1532 error_code ec;
1533 ObjectFile *Ret;
1534 if (Magic == "\xFE\xED\xFA\xCE")
1535 Ret = new MachOObjectFile(Buffer, false, false, ec);
1536 else if (Magic == "\xCE\xFA\xED\xFE")
1537 Ret = new MachOObjectFile(Buffer, true, false, ec);
1538 else if (Magic == "\xFE\xED\xFA\xCF")
1539 Ret = new MachOObjectFile(Buffer, false, true, ec);
1540 else if (Magic == "\xCF\xFA\xED\xFE")
1541 Ret = new MachOObjectFile(Buffer, true, true, ec);
1542 else
1543 return NULL;
1544
1545 if (ec)
1546 return NULL;
1547 return Ret;
1548}
1549
Owen Andersonf7c93a32011-10-11 17:32:27 +00001550} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001551} // end namespace llvm