blob: f6840b8c15d5873c78a7790c3d7e6ef7f04855a5 [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
Rafael Espindola59a0e792013-04-29 22:24:22 +0000498error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
499 uint32_t &Result) const {
500 uint32_t flags;
501 this->getSymbolFlags(DRI, flags);
502 if (flags & SymbolRef::SF_Common) {
503 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
504 Result = 1 << MachO::GET_COMM_ALIGN(Entry.Flags);
505 } else {
506 Result = 0;
507 }
508 return object_error::success;
509}
510
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000511error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
512 uint64_t &Result) const {
513 uint64_t BeginOffset;
514 uint64_t EndOffset = 0;
515 uint8_t SectionIndex;
516
517 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
518 uint64_t Value;
519 getSymbolAddress(DRI, Value);
520
521 BeginOffset = Value;
522
523 SectionIndex = Entry.SectionIndex;
524 if (!SectionIndex) {
525 uint32_t flags = SymbolRef::SF_None;
526 this->getSymbolFlags(DRI, flags);
527 if (flags & SymbolRef::SF_Common)
528 Result = Value;
529 else
530 Result = UnknownAddressOrSize;
531 return object_error::success;
532 }
533 // Unfortunately symbols are unsorted so we need to touch all
534 // symbols from load command
Rafael Espindola802fe932013-04-24 19:47:55 +0000535 error_code ec;
536 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
537 I.increment(ec)) {
538 DataRefImpl DRI = I->getRawDataRefImpl();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000539 Entry = getSymbolTableEntryBase(this, DRI);
540 getSymbolAddress(DRI, Value);
541 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
542 if (!EndOffset || Value < EndOffset)
543 EndOffset = Value;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000544 }
545 if (!EndOffset) {
546 uint64_t Size;
547 DataRefImpl Sec;
548 Sec.d.a = SectionIndex-1;
549 getSectionSize(Sec, Size);
550 getSectionAddress(Sec, EndOffset);
551 EndOffset += Size;
552 }
553 Result = EndOffset - BeginOffset;
554 return object_error::success;
555}
556
557error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
558 SymbolRef::Type &Res) const {
559 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
560 uint8_t n_type = Entry.Type;
561
562 Res = SymbolRef::ST_Other;
563
564 // If this is a STAB debugging symbol, we can do nothing more.
565 if (n_type & MachO::NlistMaskStab) {
566 Res = SymbolRef::ST_Debug;
567 return object_error::success;
568 }
569
570 switch (n_type & MachO::NlistMaskType) {
571 case MachO::NListTypeUndefined :
572 Res = SymbolRef::ST_Unknown;
573 break;
574 case MachO::NListTypeSection :
575 Res = SymbolRef::ST_Function;
576 break;
577 }
578 return object_error::success;
579}
580
581error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
582 char &Res) const {
583 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
584 uint8_t Type = Entry.Type;
585 uint16_t Flags = Entry.Flags;
586
587 char Char;
588 switch (Type & macho::STF_TypeMask) {
589 case macho::STT_Undefined:
590 Char = 'u';
591 break;
592 case macho::STT_Absolute:
593 case macho::STT_Section:
594 Char = 's';
595 break;
596 default:
597 Char = '?';
598 break;
599 }
600
601 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
602 Char = toupper(static_cast<unsigned char>(Char));
603 Res = Char;
604 return object_error::success;
605}
606
607error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
608 uint32_t &Result) const {
609 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
610
611 uint8_t MachOType = Entry.Type;
612 uint16_t MachOFlags = Entry.Flags;
613
614 // TODO: Correctly set SF_ThreadLocal
615 Result = SymbolRef::SF_None;
616
617 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
618 Result |= SymbolRef::SF_Undefined;
619
620 if (MachOFlags & macho::STF_StabsEntryMask)
621 Result |= SymbolRef::SF_FormatSpecific;
622
623 if (MachOType & MachO::NlistMaskExternal) {
624 Result |= SymbolRef::SF_Global;
Rafael Espindola59a0e792013-04-29 22:24:22 +0000625 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) {
626 uint64_t Value;
627 getSymbolAddress(DRI, Value);
628 if (Value)
629 Result |= SymbolRef::SF_Common;
630 }
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000631 }
632
633 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
634 Result |= SymbolRef::SF_Weak;
635
636 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
637 Result |= SymbolRef::SF_Absolute;
638
639 return object_error::success;
640}
641
642error_code
643MachOObjectFile::getSymbolSection(DataRefImpl Symb,
644 section_iterator &Res) const {
645 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
646 uint8_t index = Entry.SectionIndex;
647
648 if (index == 0) {
649 Res = end_sections();
650 } else {
651 DataRefImpl DRI;
652 DRI.d.a = index - 1;
653 Res = section_iterator(SectionRef(DRI, this));
654 }
655
656 return object_error::success;
657}
658
659error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000660 uint64_t &Val) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000661 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
662}
663
664error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
665 SectionRef &Res) const {
666 Sec.d.a++;
667 Res = SectionRef(Sec, this);
668 return object_error::success;
669}
670
671error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000672MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000673 ArrayRef<char> Raw = getSectionRawName(Sec);
674 Result = parseSegmentOrSectionName(Raw.data());
675 return object_error::success;
676}
677
678error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000679MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000680 if (is64Bit()) {
681 macho::Section64 Sect = getSection64(Sec);
682 Res = Sect.Address;
683 } else {
684 macho::Section Sect = getSection(Sec);
685 Res = Sect.Address;
686 }
687 return object_error::success;
688}
689
690error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000691MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000692 if (is64Bit()) {
693 macho::Section64 Sect = getSection64(Sec);
694 Res = Sect.Size;
695 } else {
696 macho::Section Sect = getSection(Sec);
697 Res = Sect.Size;
698 }
699
700 return object_error::success;
701}
702
703error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000704MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000705 uint32_t Offset;
706 uint64_t Size;
707
708 if (is64Bit()) {
709 macho::Section64 Sect = getSection64(Sec);
710 Offset = Sect.Offset;
711 Size = Sect.Size;
712 } else {
713 macho::Section Sect =getSection(Sec);
714 Offset = Sect.Offset;
715 Size = Sect.Size;
716 }
717
718 Res = this->getData().substr(Offset, Size);
719 return object_error::success;
720}
721
722error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000723MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000724 uint32_t Align;
725 if (is64Bit()) {
726 macho::Section64 Sect = getSection64(Sec);
727 Align = Sect.Align;
728 } else {
729 macho::Section Sect = getSection(Sec);
730 Align = Sect.Align;
731 }
732
733 Res = uint64_t(1) << Align;
734 return object_error::success;
735}
736
737error_code
738MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
739 uint32_t Flags = getSectionFlags(this, Sec);
740 Res = Flags & macho::SF_PureInstructions;
741 return object_error::success;
742}
743
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000744error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000745 // FIXME: Unimplemented.
746 Result = false;
747 return object_error::success;
748}
749
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000750error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000751 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000752 Result = false;
753 return object_error::success;
754}
755
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000756error_code
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000757MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000758 bool &Result) const {
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000759 // FIXME: Unimplemented.
760 Result = true;
Preston Gurdc68dda82012-04-12 20:13:57 +0000761 return object_error::success;
762}
763
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000764error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000765 bool &Result) const {
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000766 // FIXME: Unimplemented.
767 Result = false;
768 return object_error::success;
769}
770
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000771error_code
772MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
773 uint32_t Flags = getSectionFlags(this, Sec);
774 unsigned SectionType = Flags & MachO::SectionFlagMaskSectionType;
775 Res = SectionType == MachO::SectionTypeZeroFill ||
776 SectionType == MachO::SectionTypeZeroFillLarge;
777 return object_error::success;
778}
779
780error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000781 bool &Result) const {
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000782 // Consider using the code from isSectionText to look for __const sections.
783 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
784 // to use section attributes to distinguish code from data.
785
786 // FIXME: Unimplemented.
787 Result = false;
788 return object_error::success;
789}
790
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000791error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000792MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000793 bool &Result) const {
794 SymbolRef::Type ST;
795 this->getSymbolType(Symb, ST);
796 if (ST == SymbolRef::ST_Unknown) {
797 Result = false;
798 return object_error::success;
799 }
800
801 uint64_t SectBegin, SectEnd;
802 getSectionAddress(Sec, SectBegin);
803 getSectionSize(Sec, SectEnd);
804 SectEnd += SectBegin;
805
806 uint64_t SymAddr;
807 getSymbolAddress(Symb, SymAddr);
808 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
809
810 return object_error::success;
811}
812
813relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +0000814 uint32_t Offset;
815 if (is64Bit()) {
816 macho::Section64 Sect = getSection64(Sec);
817 Offset = Sect.RelocationTableOffset;
818 } else {
819 macho::Section Sect = getSection(Sec);
820 Offset = Sect.RelocationTableOffset;
821 }
822
823 DataRefImpl Ret;
824 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
825 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000826}
Rafael Espindola335f1d42013-04-08 20:45:01 +0000827
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000828relocation_iterator
829MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +0000830 uint32_t Offset;
831 uint32_t Num;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000832 if (is64Bit()) {
833 macho::Section64 Sect = getSection64(Sec);
Rafael Espindolae5330f72013-04-25 12:45:46 +0000834 Offset = Sect.RelocationTableOffset;
835 Num = Sect.NumRelocationTableEntries;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000836 } else {
837 macho::Section Sect = getSection(Sec);
Rafael Espindolae5330f72013-04-25 12:45:46 +0000838 Offset = Sect.RelocationTableOffset;
839 Num = Sect.NumRelocationTableEntries;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000840 }
Eric Christopher6256b032011-04-22 03:19:48 +0000841
Rafael Espindolae5330f72013-04-25 12:45:46 +0000842 const macho::RelocationEntry *P =
843 reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset));
844
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000845 DataRefImpl Ret;
Rafael Espindolae5330f72013-04-25 12:45:46 +0000846 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000847 return relocation_iterator(RelocationRef(Ret, this));
848}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000849
Rafael Espindolae5330f72013-04-25 12:45:46 +0000850error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
851 RelocationRef &Res) const {
852 const macho::RelocationEntry *P =
853 reinterpret_cast<const macho::RelocationEntry *>(Rel.p);
854 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000855 Res = RelocationRef(Rel, this);
856 return object_error::success;
857}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000858
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000859error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000860MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola956ca722013-04-25 12:28:45 +0000861 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000862}
863
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000864error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000865 uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000866 macho::RelocationEntry RE = getRelocation(Rel);
867 Res = getAnyRelocationAddress(RE);
868 return object_error::success;
David Meyer5c2b4ea2012-03-01 01:36:50 +0000869}
870
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000871error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000872MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000873 macho::RelocationEntry RE = getRelocation(Rel);
874 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
875 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola802fe932013-04-24 19:47:55 +0000876 if (!isExtern) {
877 Res = *end_symbols();
878 return object_error::success;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000879 }
Rafael Espindola802fe932013-04-24 19:47:55 +0000880
881 macho::SymtabLoadCommand S = getSymtabLoadCommand();
882 unsigned SymbolTableEntrySize = is64Bit() ?
883 sizeof(macho::Symbol64TableEntry) :
884 sizeof(macho::SymbolTableEntry);
885 uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize;
886 DataRefImpl Sym;
887 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000888 Res = SymbolRef(Sym, this);
889 return object_error::success;
890}
891
892error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000893 uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000894 macho::RelocationEntry RE = getRelocation(Rel);
895 Res = getAnyRelocationType(RE);
896 return object_error::success;
897}
898
899error_code
900MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
901 SmallVectorImpl<char> &Result) const {
902 StringRef res;
903 uint64_t RType;
904 getRelocationType(Rel, RType);
905
906 unsigned Arch = this->getArch();
907
908 switch (Arch) {
909 case Triple::x86: {
910 static const char *const Table[] = {
911 "GENERIC_RELOC_VANILLA",
912 "GENERIC_RELOC_PAIR",
913 "GENERIC_RELOC_SECTDIFF",
914 "GENERIC_RELOC_PB_LA_PTR",
915 "GENERIC_RELOC_LOCAL_SECTDIFF",
916 "GENERIC_RELOC_TLV" };
917
918 if (RType > 6)
919 res = "Unknown";
920 else
921 res = Table[RType];
922 break;
923 }
924 case Triple::x86_64: {
925 static const char *const Table[] = {
926 "X86_64_RELOC_UNSIGNED",
927 "X86_64_RELOC_SIGNED",
928 "X86_64_RELOC_BRANCH",
929 "X86_64_RELOC_GOT_LOAD",
930 "X86_64_RELOC_GOT",
931 "X86_64_RELOC_SUBTRACTOR",
932 "X86_64_RELOC_SIGNED_1",
933 "X86_64_RELOC_SIGNED_2",
934 "X86_64_RELOC_SIGNED_4",
935 "X86_64_RELOC_TLV" };
936
937 if (RType > 9)
938 res = "Unknown";
939 else
940 res = Table[RType];
941 break;
942 }
943 case Triple::arm: {
944 static const char *const Table[] = {
945 "ARM_RELOC_VANILLA",
946 "ARM_RELOC_PAIR",
947 "ARM_RELOC_SECTDIFF",
948 "ARM_RELOC_LOCAL_SECTDIFF",
949 "ARM_RELOC_PB_LA_PTR",
950 "ARM_RELOC_BR24",
951 "ARM_THUMB_RELOC_BR22",
952 "ARM_THUMB_32BIT_BRANCH",
953 "ARM_RELOC_HALF",
954 "ARM_RELOC_HALF_SECTDIFF" };
955
956 if (RType > 9)
957 res = "Unknown";
958 else
959 res = Table[RType];
960 break;
961 }
962 case Triple::ppc: {
963 static const char *const Table[] = {
964 "PPC_RELOC_VANILLA",
965 "PPC_RELOC_PAIR",
966 "PPC_RELOC_BR14",
967 "PPC_RELOC_BR24",
968 "PPC_RELOC_HI16",
969 "PPC_RELOC_LO16",
970 "PPC_RELOC_HA16",
971 "PPC_RELOC_LO14",
972 "PPC_RELOC_SECTDIFF",
973 "PPC_RELOC_PB_LA_PTR",
974 "PPC_RELOC_HI16_SECTDIFF",
975 "PPC_RELOC_LO16_SECTDIFF",
976 "PPC_RELOC_HA16_SECTDIFF",
977 "PPC_RELOC_JBSR",
978 "PPC_RELOC_LO14_SECTDIFF",
979 "PPC_RELOC_LOCAL_SECTDIFF" };
980
981 res = Table[RType];
982 break;
983 }
984 case Triple::UnknownArch:
985 res = "Unknown";
986 break;
987 }
988 Result.append(res.begin(), res.end());
989 return object_error::success;
990}
991
992error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
993 int64_t &Res) const {
Rafael Espindola8bf80062013-04-11 02:21:31 +0000994 Res = 0;
995 return object_error::success;
996}
997
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000998error_code
999MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001000 SmallVectorImpl<char> &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001001 macho::RelocationEntry RE = getRelocation(Rel);
David Meyer5c2b4ea2012-03-01 01:36:50 +00001002
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001003 unsigned Arch = this->getArch();
Eric Christopher6256b032011-04-22 03:19:48 +00001004
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001005 std::string fmtbuf;
1006 raw_string_ostream fmt(fmtbuf);
1007 unsigned Type = this->getAnyRelocationType(RE);
1008 bool IsPCRel = this->getAnyRelocationPCRel(RE);
1009
1010 // Determine any addends that should be displayed with the relocation.
1011 // These require decoding the relocation type, which is triple-specific.
1012
1013 // X86_64 has entirely custom relocation types.
1014 if (Arch == Triple::x86_64) {
1015 bool isPCRel = getAnyRelocationPCRel(RE);
1016
1017 switch (Type) {
1018 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1019 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1020 printRelocationTargetName(this, RE, fmt);
1021 fmt << "@GOT";
1022 if (isPCRel) fmt << "PCREL";
1023 break;
1024 }
1025 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1026 DataRefImpl RelNext = Rel;
1027 RelNext.d.a++;
1028 macho::RelocationEntry RENext = getRelocation(RelNext);
1029
1030 // X86_64_SUBTRACTOR must be followed by a relocation of type
1031 // X86_64_RELOC_UNSIGNED.
1032 // NOTE: Scattered relocations don't exist on x86_64.
1033 unsigned RType = getAnyRelocationType(RENext);
1034 if (RType != 0)
1035 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1036 "X86_64_RELOC_SUBTRACTOR.");
1037
1038 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1039 // X86_64_SUBTRACTOR contains to the subtrahend.
1040 printRelocationTargetName(this, RENext, fmt);
1041 fmt << "-";
1042 printRelocationTargetName(this, RE, fmt);
1043 break;
1044 }
1045 case macho::RIT_X86_64_TLV:
1046 printRelocationTargetName(this, RE, fmt);
1047 fmt << "@TLV";
1048 if (isPCRel) fmt << "P";
1049 break;
1050 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1051 printRelocationTargetName(this, RE, fmt);
1052 fmt << "-1";
1053 break;
1054 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1055 printRelocationTargetName(this, RE, fmt);
1056 fmt << "-2";
1057 break;
1058 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1059 printRelocationTargetName(this, RE, fmt);
1060 fmt << "-4";
1061 break;
1062 default:
1063 printRelocationTargetName(this, RE, fmt);
1064 break;
1065 }
1066 // X86 and ARM share some relocation types in common.
1067 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1068 // Generic relocation types...
1069 switch (Type) {
1070 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1071 return object_error::success;
1072 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1073 DataRefImpl RelNext = Rel;
1074 RelNext.d.a++;
1075 macho::RelocationEntry RENext = getRelocation(RelNext);
1076
1077 // X86 sect diff's must be followed by a relocation of type
1078 // GENERIC_RELOC_PAIR.
1079 unsigned RType = getAnyRelocationType(RENext);
1080
1081 if (RType != 1)
1082 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1083 "GENERIC_RELOC_SECTDIFF.");
1084
1085 printRelocationTargetName(this, RE, fmt);
1086 fmt << "-";
1087 printRelocationTargetName(this, RENext, fmt);
1088 break;
1089 }
1090 }
1091
1092 if (Arch == Triple::x86) {
1093 // All X86 relocations that need special printing were already
1094 // handled in the generic code.
1095 switch (Type) {
1096 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1097 DataRefImpl RelNext = Rel;
1098 RelNext.d.a++;
1099 macho::RelocationEntry RENext = getRelocation(RelNext);
1100
1101 // X86 sect diff's must be followed by a relocation of type
1102 // GENERIC_RELOC_PAIR.
1103 unsigned RType = getAnyRelocationType(RENext);
1104 if (RType != 1)
1105 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1106 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1107
1108 printRelocationTargetName(this, RE, fmt);
1109 fmt << "-";
1110 printRelocationTargetName(this, RENext, fmt);
1111 break;
1112 }
1113 case macho::RIT_Generic_TLV: {
1114 printRelocationTargetName(this, RE, fmt);
1115 fmt << "@TLV";
1116 if (IsPCRel) fmt << "P";
1117 break;
1118 }
1119 default:
1120 printRelocationTargetName(this, RE, fmt);
1121 }
1122 } else { // ARM-specific relocations
1123 switch (Type) {
1124 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1125 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1126 // Half relocations steal a bit from the length field to encode
1127 // whether this is an upper16 or a lower16 relocation.
1128 bool isUpper = getAnyRelocationLength(RE) >> 1;
1129
1130 if (isUpper)
1131 fmt << ":upper16:(";
1132 else
1133 fmt << ":lower16:(";
1134 printRelocationTargetName(this, RE, fmt);
1135
1136 DataRefImpl RelNext = Rel;
1137 RelNext.d.a++;
1138 macho::RelocationEntry RENext = getRelocation(RelNext);
1139
1140 // ARM half relocs must be followed by a relocation of type
1141 // ARM_RELOC_PAIR.
1142 unsigned RType = getAnyRelocationType(RENext);
1143 if (RType != 1)
1144 report_fatal_error("Expected ARM_RELOC_PAIR after "
1145 "GENERIC_RELOC_HALF");
1146
1147 // NOTE: The half of the target virtual address is stashed in the
1148 // address field of the secondary relocation, but we can't reverse
1149 // engineer the constant offset from it without decoding the movw/movt
1150 // instruction to find the other half in its immediate field.
1151
1152 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1153 // symbol/section pointer of the follow-on relocation.
1154 if (Type == macho::RIT_ARM_HalfDifference) {
1155 fmt << "-";
1156 printRelocationTargetName(this, RENext, fmt);
1157 }
1158
1159 fmt << ")";
1160 break;
1161 }
1162 default: {
1163 printRelocationTargetName(this, RE, fmt);
1164 }
1165 }
1166 }
1167 } else
1168 printRelocationTargetName(this, RE, fmt);
1169
1170 fmt.flush();
1171 Result.append(fmtbuf.begin(), fmtbuf.end());
1172 return object_error::success;
1173}
1174
1175error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001176MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001177 unsigned Arch = getArch();
1178 uint64_t Type;
1179 getRelocationType(Rel, Type);
1180
1181 Result = false;
1182
1183 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1184 // is always hidden.
1185 if (Arch == Triple::x86 || Arch == Triple::arm) {
1186 if (Type == macho::RIT_Pair) Result = true;
1187 } else if (Arch == Triple::x86_64) {
1188 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1189 // an X864_64_RELOC_SUBTRACTOR.
1190 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1191 DataRefImpl RelPrev = Rel;
1192 RelPrev.d.a--;
1193 uint64_t PrevType;
1194 getRelocationType(RelPrev, PrevType);
1195 if (PrevType == macho::RIT_X86_64_Subtractor)
1196 Result = true;
1197 }
1198 }
1199
1200 return object_error::success;
1201}
1202
1203error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001204 LibraryRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001205 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1206}
1207
1208error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001209 StringRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001210 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1211}
1212
1213symbol_iterator MachOObjectFile::begin_symbols() const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001214 DataRefImpl DRI;
Rafael Espindola802fe932013-04-24 19:47:55 +00001215 if (!SymtabLoadCmd)
1216 return symbol_iterator(SymbolRef(DRI, this));
1217
1218 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1219 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001220 return symbol_iterator(SymbolRef(DRI, this));
1221}
1222
1223symbol_iterator MachOObjectFile::end_symbols() const {
1224 DataRefImpl DRI;
Rafael Espindola802fe932013-04-24 19:47:55 +00001225 if (!SymtabLoadCmd)
1226 return symbol_iterator(SymbolRef(DRI, this));
1227
1228 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1229 unsigned SymbolTableEntrySize = is64Bit() ?
1230 sizeof(macho::Symbol64TableEntry) :
1231 sizeof(macho::SymbolTableEntry);
1232 unsigned Offset = Symtab.SymbolTableOffset +
1233 Symtab.NumSymbolTableEntries * SymbolTableEntrySize;
1234 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001235 return symbol_iterator(SymbolRef(DRI, this));
1236}
1237
1238symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1239 // TODO: implement
1240 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1241}
1242
1243symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1244 // TODO: implement
1245 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1246}
1247
1248section_iterator MachOObjectFile::begin_sections() const {
1249 DataRefImpl DRI;
1250 return section_iterator(SectionRef(DRI, this));
1251}
1252
1253section_iterator MachOObjectFile::end_sections() const {
1254 DataRefImpl DRI;
1255 DRI.d.a = Sections.size();
1256 return section_iterator(SectionRef(DRI, this));
1257}
1258
1259library_iterator MachOObjectFile::begin_libraries_needed() const {
1260 // TODO: implement
1261 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1262}
1263
1264library_iterator MachOObjectFile::end_libraries_needed() const {
1265 // TODO: implement
1266 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1267}
1268
1269uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +00001270 return is64Bit() ? 8 : 4;
Eric Christopher6256b032011-04-22 03:19:48 +00001271}
1272
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001273StringRef MachOObjectFile::getFileFormatName() const {
1274 unsigned CPUType = getCPUType(this);
1275 if (!is64Bit()) {
1276 switch (CPUType) {
1277 case llvm::MachO::CPUTypeI386:
1278 return "Mach-O 32-bit i386";
1279 case llvm::MachO::CPUTypeARM:
1280 return "Mach-O arm";
1281 case llvm::MachO::CPUTypePowerPC:
1282 return "Mach-O 32-bit ppc";
1283 default:
1284 assert((CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1285 "64-bit object file when we're not 64-bit?");
1286 return "Mach-O 32-bit unknown";
1287 }
1288 }
1289
1290 // Make sure the cpu type has the correct mask.
1291 assert((CPUType & llvm::MachO::CPUArchABI64)
1292 == llvm::MachO::CPUArchABI64 &&
1293 "32-bit object file when we're 64-bit?");
1294
1295 switch (CPUType) {
1296 case llvm::MachO::CPUTypeX86_64:
1297 return "Mach-O 64-bit x86-64";
1298 case llvm::MachO::CPUTypePowerPC64:
1299 return "Mach-O 64-bit ppc64";
1300 default:
1301 return "Mach-O 64-bit unknown";
1302 }
1303}
1304
1305unsigned MachOObjectFile::getArch() const {
1306 switch (getCPUType(this)) {
1307 case llvm::MachO::CPUTypeI386:
1308 return Triple::x86;
1309 case llvm::MachO::CPUTypeX86_64:
1310 return Triple::x86_64;
1311 case llvm::MachO::CPUTypeARM:
1312 return Triple::arm;
1313 case llvm::MachO::CPUTypePowerPC:
1314 return Triple::ppc;
1315 case llvm::MachO::CPUTypePowerPC64:
1316 return Triple::ppc64;
1317 default:
1318 return Triple::UnknownArch;
1319 }
1320}
1321
1322StringRef MachOObjectFile::getLoadName() const {
1323 // TODO: Implement
1324 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1325}
1326
Rafael Espindola2173e182013-04-26 20:07:33 +00001327relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const {
1328 DataRefImpl DRI;
1329 DRI.d.a = Index;
1330 return getSectionRelBegin(DRI);
1331}
1332
1333relocation_iterator MachOObjectFile::getSectionRelEnd(unsigned Index) const {
1334 DataRefImpl DRI;
1335 DRI.d.a = Index;
1336 return getSectionRelEnd(DRI);
1337}
1338
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001339StringRef
1340MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1341 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1342 return parseSegmentOrSectionName(Raw.data());
1343}
1344
1345ArrayRef<char>
1346MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1347 const SectionBase *Base =
1348 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1349 return ArrayRef<char>(Base->Name);
1350}
1351
1352ArrayRef<char>
1353MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1354 const SectionBase *Base =
1355 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1356 return ArrayRef<char>(Base->SegmentName);
1357}
1358
1359bool
1360MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
1361 const {
1362 if (getCPUType(this) == llvm::MachO::CPUTypeX86_64)
1363 return false;
1364 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
1365}
1366
1367unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const {
1368 if (isLittleEndian())
1369 return RE.Word1 & 0xffffff;
1370 return RE.Word1 >> 8;
1371}
1372
1373bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const {
1374 if (isLittleEndian())
1375 return (RE.Word1 >> 27) & 1;
1376 return (RE.Word1 >> 4) & 1;
1377}
1378
1379bool
1380MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const {
1381 return RE.Word0 >> 31;
1382}
1383
1384uint32_t
1385MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const {
1386 return RE.Word1;
1387}
1388
1389unsigned
1390MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const {
1391 if (isRelocationScattered(RE))
1392 return getScatteredRelocationAddress(RE);
1393 return getPlainRelocationAddress(RE);
1394}
1395
1396unsigned
1397MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
1398 if (isRelocationScattered(RE))
1399 return getScatteredRelocationPCRel(this, RE);
1400 return getPlainRelocationPCRel(this, RE);
1401}
1402
1403unsigned
1404MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const {
1405 if (isRelocationScattered(RE))
1406 return getScatteredRelocationLength(RE);
1407 return getPlainRelocationLength(this, RE);
1408}
1409
1410unsigned
1411MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
1412 if (isRelocationScattered(RE))
1413 return getScatteredRelocationType(RE);
1414 return getPlainRelocationType(this, RE);
1415}
1416
1417MachOObjectFile::LoadCommandInfo
1418MachOObjectFile::getFirstLoadCommandInfo() const {
1419 MachOObjectFile::LoadCommandInfo Load;
1420
1421 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
1422 Load.Ptr = getPtr(this, HeaderSize);
Rafael Espindola143d2232013-04-19 13:45:05 +00001423 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001424 return Load;
1425}
1426
1427MachOObjectFile::LoadCommandInfo
1428MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1429 MachOObjectFile::LoadCommandInfo Next;
1430 Next.Ptr = L.Ptr + L.C.Size;
Rafael Espindola143d2232013-04-19 13:45:05 +00001431 Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001432 return Next;
1433}
1434
1435macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001436 return getStruct<macho::Section>(this, Sections[DRI.d.a]);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001437}
1438
1439macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001440 return getStruct<macho::Section64>(this, Sections[DRI.d.a]);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001441}
1442
Rafael Espindola2173e182013-04-26 20:07:33 +00001443macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L,
1444 unsigned Index) const {
1445 const char *Sec = getSectionPtr(this, L, Index);
1446 return getStruct<macho::Section>(this, Sec);
1447}
1448
1449macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1450 unsigned Index) const {
1451 const char *Sec = getSectionPtr(this, L, Index);
1452 return getStruct<macho::Section64>(this, Sec);
1453}
1454
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001455macho::SymbolTableEntry
1456MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola802fe932013-04-24 19:47:55 +00001457 const char *P = reinterpret_cast<const char *>(DRI.p);
Rafael Espindola143d2232013-04-19 13:45:05 +00001458 return getStruct<macho::SymbolTableEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001459}
1460
1461macho::Symbol64TableEntry
1462MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola802fe932013-04-24 19:47:55 +00001463 const char *P = reinterpret_cast<const char *>(DRI.p);
Rafael Espindola143d2232013-04-19 13:45:05 +00001464 return getStruct<macho::Symbol64TableEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001465}
1466
1467macho::LinkeditDataLoadCommand
1468MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001469 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001470}
1471
Rafael Espindola2173e182013-04-26 20:07:33 +00001472macho::SegmentLoadCommand
1473MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1474 return getStruct<macho::SegmentLoadCommand>(this, L.Ptr);
1475}
1476
1477macho::Segment64LoadCommand
1478MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1479 return getStruct<macho::Segment64LoadCommand>(this, L.Ptr);
1480}
1481
1482macho::LinkerOptionsLoadCommand
1483MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1484 return getStruct<macho::LinkerOptionsLoadCommand>(this, L.Ptr);
1485}
1486
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001487macho::RelocationEntry
1488MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +00001489 const char *P = reinterpret_cast<const char *>(Rel.p);
1490 return getStruct<macho::RelocationEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001491}
1492
1493macho::Header MachOObjectFile::getHeader() const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001494 return getStruct<macho::Header>(this, getPtr(this, 0));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001495}
1496
Rafael Espindola2173e182013-04-26 20:07:33 +00001497macho::Header64Ext MachOObjectFile::getHeader64Ext() const {
1498 return
1499 getStruct<macho::Header64Ext>(this, getPtr(this, sizeof(macho::Header)));
1500}
1501
1502macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry(
1503 const macho::DysymtabLoadCommand &DLC,
1504 unsigned Index) const {
1505 uint64_t Offset = DLC.IndirectSymbolTableOffset +
1506 Index * sizeof(macho::IndirectSymbolTableEntry);
1507 return getStruct<macho::IndirectSymbolTableEntry>(this, getPtr(this, Offset));
1508}
1509
1510macho::DataInCodeTableEntry
1511MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1512 unsigned Index) const {
1513 uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry);
1514 return getStruct<macho::DataInCodeTableEntry>(this, getPtr(this, Offset));
1515}
1516
1517macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001518 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001519}
1520
Rafael Espindola2173e182013-04-26 20:07:33 +00001521macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const {
1522 return getStruct<macho::DysymtabLoadCommand>(this, DysymtabLoadCmd);
1523}
1524
1525StringRef MachOObjectFile::getStringTableData() const {
1526 macho::SymtabLoadCommand S = getSymtabLoadCommand();
1527 return getData().substr(S.StringTableOffset, S.StringTableSize);
1528}
1529
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001530bool MachOObjectFile::is64Bit() const {
1531 return getType() == getMachOType(false, true) ||
1532 getType() == getMachOType(true, true);
1533}
1534
1535void MachOObjectFile::ReadULEB128s(uint64_t Index,
1536 SmallVectorImpl<uint64_t> &Out) const {
1537 DataExtractor extractor(ObjectFile::getData(), true, 0);
1538
1539 uint32_t offset = Index;
1540 uint64_t data = 0;
1541 while (uint64_t delta = extractor.getULEB128(&offset)) {
1542 data += delta;
1543 Out.push_back(data);
1544 }
1545}
1546
1547ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1548 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1549 error_code ec;
1550 ObjectFile *Ret;
1551 if (Magic == "\xFE\xED\xFA\xCE")
1552 Ret = new MachOObjectFile(Buffer, false, false, ec);
1553 else if (Magic == "\xCE\xFA\xED\xFE")
1554 Ret = new MachOObjectFile(Buffer, true, false, ec);
1555 else if (Magic == "\xFE\xED\xFA\xCF")
1556 Ret = new MachOObjectFile(Buffer, false, true, ec);
1557 else if (Magic == "\xCF\xFA\xED\xFE")
1558 Ret = new MachOObjectFile(Buffer, true, true, ec);
1559 else
1560 return NULL;
1561
1562 if (ec)
1563 return NULL;
1564 return Ret;
1565}
1566
Owen Andersonf7c93a32011-10-11 17:32:27 +00001567} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001568} // end namespace llvm