blob: 360ab1676c46558b9733ad217b217f44c907344a [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
15#include "llvm/ADT/Triple.h"
Owen Andersonf7c93a32011-10-11 17:32:27 +000016#include "llvm/Object/MachO.h"
Eric Christopher6256b032011-04-22 03:19:48 +000017#include "llvm/Object/MachOFormat.h"
Owen Anderson1832f4d2011-10-26 20:42:54 +000018#include "llvm/Support/Format.h"
Eric Christopher6256b032011-04-22 03:19:48 +000019#include "llvm/Support/MemoryBuffer.h"
20
21#include <cctype>
22#include <cstring>
23#include <limits>
24
25using namespace llvm;
26using namespace object;
27
28namespace llvm {
Owen Andersonf7c93a32011-10-11 17:32:27 +000029namespace object {
Eric Christopher6256b032011-04-22 03:19:48 +000030
Benjamin Kramer0fcab072011-09-08 20:52:17 +000031MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
32 error_code &ec)
33 : ObjectFile(Binary::isMachO, Object, ec),
34 MachOObj(MOO),
35 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
36 DataRefImpl DRI;
37 DRI.d.a = DRI.d.b = 0;
38 moveToNextSection(DRI);
39 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
40 while (DRI.d.a < LoadCommandCount) {
41 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000042 DRI.d.b++;
43 moveToNextSection(DRI);
44 }
45}
46
47
Eric Christopher6256b032011-04-22 03:19:48 +000048ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000049 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000050 std::string Err;
51 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
52 if (!MachOObj)
53 return NULL;
Michael J. Spencer001c9202011-06-25 17:54:50 +000054 return new MachOObjectFile(Buffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000055}
56
57/*===-- Symbols -----------------------------------------------------------===*/
58
59void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
60 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
61 while (DRI.d.a < LoadCommandCount) {
62 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
63 if (LCI.Command.Type == macho::LCT_Symtab) {
64 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
65 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
66 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
67 return;
68 }
69
70 DRI.d.a++;
71 DRI.d.b = 0;
72 }
73}
74
75void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
76 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
77 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
78 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
79 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
80
81 if (RegisteredStringTable != DRI.d.a) {
82 MachOObj->RegisterStringTable(*SymtabLoadCmd);
83 RegisteredStringTable = DRI.d.a;
84 }
85
86 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
87 Res);
88}
89
Benjamin Kramer32fb2af2011-07-15 17:32:45 +000090void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
91 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
92 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
93 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
94 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
95
96 if (RegisteredStringTable != DRI.d.a) {
97 MachOObj->RegisterStringTable(*SymtabLoadCmd);
98 RegisteredStringTable = DRI.d.a;
99 }
100
101 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
102 Res);
103}
104
Eric Christopher6256b032011-04-22 03:19:48 +0000105
Michael J. Spencer25b15772011-06-25 17:55:23 +0000106error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
107 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000108 DRI.d.b++;
109 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000110 Result = SymbolRef(DRI, this);
111 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000112}
113
Michael J. Spencer25b15772011-06-25 17:55:23 +0000114error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
115 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000116 if (MachOObj->is64Bit()) {
117 InMemoryStruct<macho::Symbol64TableEntry> Entry;
118 getSymbol64TableEntry(DRI, Entry);
119 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
120 } else {
121 InMemoryStruct<macho::SymbolTableEntry> Entry;
122 getSymbolTableEntry(DRI, Entry);
123 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
124 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000125 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000126}
127
Danil Malyshevb0436a72011-11-29 17:40:10 +0000128error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
129 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000130 if (MachOObj->is64Bit()) {
131 InMemoryStruct<macho::Symbol64TableEntry> Entry;
132 getSymbol64TableEntry(DRI, Entry);
133 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000134 if (Entry->SectionIndex) {
135 InMemoryStruct<macho::Section64> Section;
136 getSection64(Sections[Entry->SectionIndex-1], Section);
137 Result += Section->Offset - Section->Address;
138 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000139 } else {
140 InMemoryStruct<macho::SymbolTableEntry> Entry;
141 getSymbolTableEntry(DRI, Entry);
142 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000143 if (Entry->SectionIndex) {
144 InMemoryStruct<macho::Section> Section;
145 getSection(Sections[Entry->SectionIndex-1], Section);
146 Result += Section->Offset - Section->Address;
147 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000148 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000149
Michael J. Spencer25b15772011-06-25 17:55:23 +0000150 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000151}
152
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000153error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
154 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000155 if (MachOObj->is64Bit()) {
156 InMemoryStruct<macho::Symbol64TableEntry> Entry;
157 getSymbol64TableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000158 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000159 } else {
160 InMemoryStruct<macho::SymbolTableEntry> Entry;
161 getSymbolTableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000162 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000163 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000164 return object_error::success;
165}
166
Michael J. Spencer25b15772011-06-25 17:55:23 +0000167error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
168 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000169 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
170 uint64_t BeginOffset;
171 uint64_t EndOffset = 0;
172 uint8_t SectionIndex;
173 if (MachOObj->is64Bit()) {
174 InMemoryStruct<macho::Symbol64TableEntry> Entry;
175 getSymbol64TableEntry(DRI, Entry);
176 BeginOffset = Entry->Value;
177 SectionIndex = Entry->SectionIndex;
178 if (!SectionIndex) {
179 Result = UnknownAddressOrSize;
180 return object_error::success;
181 }
182 // Unfortunately symbols are unsorted so we need to touch all
183 // symbols from load command
184 DRI.d.b = 0;
185 uint32_t Command = DRI.d.a;
186 while (Command == DRI.d.a) {
187 moveToNextSymbol(DRI);
188 if (DRI.d.a < LoadCommandCount) {
189 getSymbol64TableEntry(DRI, Entry);
190 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
191 if (!EndOffset || Entry->Value < EndOffset)
192 EndOffset = Entry->Value;
193 }
194 DRI.d.b++;
195 }
196 } else {
197 InMemoryStruct<macho::SymbolTableEntry> Entry;
198 getSymbolTableEntry(DRI, Entry);
199 BeginOffset = Entry->Value;
200 SectionIndex = Entry->SectionIndex;
201 if (!SectionIndex) {
202 Result = UnknownAddressOrSize;
203 return object_error::success;
204 }
205 // Unfortunately symbols are unsorted so we need to touch all
206 // symbols from load command
207 DRI.d.b = 0;
208 uint32_t Command = DRI.d.a;
209 while (Command == DRI.d.a) {
210 moveToNextSymbol(DRI);
211 if (DRI.d.a < LoadCommandCount) {
212 getSymbolTableEntry(DRI, Entry);
213 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
214 if (!EndOffset || Entry->Value < EndOffset)
215 EndOffset = Entry->Value;
216 }
217 DRI.d.b++;
218 }
219 }
220 if (!EndOffset) {
221 uint64_t Size;
222 getSectionSize(Sections[SectionIndex-1], Size);
223 getSectionAddress(Sections[SectionIndex-1], EndOffset);
224 EndOffset += Size;
225 }
226 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000227 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000228}
229
Michael J. Spencer25b15772011-06-25 17:55:23 +0000230error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
231 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000232 uint8_t Type, Flags;
233 if (MachOObj->is64Bit()) {
234 InMemoryStruct<macho::Symbol64TableEntry> Entry;
235 getSymbol64TableEntry(DRI, Entry);
236 Type = Entry->Type;
237 Flags = Entry->Flags;
238 } else {
239 InMemoryStruct<macho::SymbolTableEntry> Entry;
240 getSymbolTableEntry(DRI, Entry);
241 Type = Entry->Type;
242 Flags = Entry->Flags;
243 }
Eric Christopher6256b032011-04-22 03:19:48 +0000244
245 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000246 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000247 case macho::STT_Undefined:
248 Char = 'u';
249 break;
250 case macho::STT_Absolute:
251 case macho::STT_Section:
252 Char = 's';
253 break;
254 default:
255 Char = '?';
256 break;
257 }
258
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000259 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Eric Christopher6256b032011-04-22 03:19:48 +0000260 Char = toupper(Char);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000261 Result = Char;
262 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000263}
264
David Meyerc46255a2012-02-28 23:47:53 +0000265error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
266 uint32_t &Result) const {
267 uint16_t MachOFlags;
268 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000269 if (MachOObj->is64Bit()) {
270 InMemoryStruct<macho::Symbol64TableEntry> Entry;
271 getSymbol64TableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000272 MachOFlags = Entry->Flags;
273 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000274 } else {
275 InMemoryStruct<macho::SymbolTableEntry> Entry;
276 getSymbolTableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000277 MachOFlags = Entry->Flags;
278 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000279 }
280
David Meyer2c677272012-02-29 02:11:55 +0000281 // TODO: Correctly set SF_ThreadLocal and SF_Common.
David Meyerc46255a2012-02-28 23:47:53 +0000282 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000283
284 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
285 Result |= SymbolRef::SF_Undefined;
286
David Meyerc46255a2012-02-28 23:47:53 +0000287 if (MachOFlags & macho::STF_StabsEntryMask)
288 Result |= SymbolRef::SF_FormatSpecific;
289
290 if (MachOType & MachO::NlistMaskExternal)
291 Result |= SymbolRef::SF_Global;
292
293 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
294 Result |= SymbolRef::SF_Weak;
295
296 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
297 Result |= SymbolRef::SF_Absolute;
298
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000299 return object_error::success;
300}
301
302error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
303 section_iterator &Res) const {
304 uint8_t index;
305 if (MachOObj->is64Bit()) {
306 InMemoryStruct<macho::Symbol64TableEntry> Entry;
307 getSymbol64TableEntry(Symb, Entry);
308 index = Entry->SectionIndex;
309 } else {
310 InMemoryStruct<macho::SymbolTableEntry> Entry;
311 getSymbolTableEntry(Symb, Entry);
312 index = Entry->SectionIndex;
313 }
314
315 if (index == 0)
316 Res = end_sections();
317 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000318 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000319
320 return object_error::success;
321}
322
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000323error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000324 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000325 uint8_t n_type;
326 if (MachOObj->is64Bit()) {
327 InMemoryStruct<macho::Symbol64TableEntry> Entry;
328 getSymbol64TableEntry(Symb, Entry);
329 n_type = Entry->Type;
330 } else {
331 InMemoryStruct<macho::SymbolTableEntry> Entry;
332 getSymbolTableEntry(Symb, Entry);
333 n_type = Entry->Type;
334 }
335 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000336
337 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000338 if (n_type & MachO::NlistMaskStab) {
339 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000340 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000341 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000342
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000343 switch (n_type & MachO::NlistMaskType) {
344 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000345 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000346 break;
347 case MachO::NListTypeSection :
348 Res = SymbolRef::ST_Function;
349 break;
350 }
351 return object_error::success;
352}
353
354
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000355symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000356 // DRI.d.a = segment number; DRI.d.b = symbol index.
357 DataRefImpl DRI;
358 DRI.d.a = DRI.d.b = 0;
359 moveToNextSymbol(DRI);
360 return symbol_iterator(SymbolRef(DRI, this));
361}
362
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000363symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000364 DataRefImpl DRI;
365 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
366 DRI.d.b = 0;
367 return symbol_iterator(SymbolRef(DRI, this));
368}
369
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000370symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
371 // TODO: implement
372 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
373}
374
375symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
376 // TODO: implement
377 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
378}
Eric Christopher6256b032011-04-22 03:19:48 +0000379
David Meyer5c2b4ea2012-03-01 01:36:50 +0000380library_iterator MachOObjectFile::begin_libraries_needed() const {
381 // TODO: implement
382 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
383}
384
385library_iterator MachOObjectFile::end_libraries_needed() const {
386 // TODO: implement
387 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
388}
389
Eric Christopher6256b032011-04-22 03:19:48 +0000390/*===-- Sections ----------------------------------------------------------===*/
391
392void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
393 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
394 while (DRI.d.a < LoadCommandCount) {
395 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
396 if (LCI.Command.Type == macho::LCT_Segment) {
397 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
398 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
399 if (DRI.d.b < SegmentLoadCmd->NumSections)
400 return;
401 } else if (LCI.Command.Type == macho::LCT_Segment64) {
402 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
403 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
404 if (DRI.d.b < Segment64LoadCmd->NumSections)
405 return;
406 }
407
408 DRI.d.a++;
409 DRI.d.b = 0;
410 }
411}
412
Michael J. Spencer25b15772011-06-25 17:55:23 +0000413error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
414 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000415 DRI.d.b++;
416 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000417 Result = SectionRef(DRI, this);
418 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000419}
420
421void
422MachOObjectFile::getSection(DataRefImpl DRI,
423 InMemoryStruct<macho::Section> &Res) const {
424 InMemoryStruct<macho::SegmentLoadCommand> SLC;
425 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
426 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
427 MachOObj->ReadSection(LCI, DRI.d.b, Res);
428}
429
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000430std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
431 SectionList::const_iterator loc =
432 std::find(Sections.begin(), Sections.end(), Sec);
433 assert(loc != Sections.end() && "Sec is not a valid section!");
434 return std::distance(Sections.begin(), loc);
435}
436
Benjamin Kramer7d145782011-07-15 00:14:48 +0000437void
438MachOObjectFile::getSection64(DataRefImpl DRI,
439 InMemoryStruct<macho::Section64> &Res) const {
440 InMemoryStruct<macho::Segment64LoadCommand> SLC;
441 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
442 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
443 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
444}
445
446static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
447 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
448 if (LCI.Command.Type == macho::LCT_Segment64)
449 return true;
450 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
451 return false;
452}
453
Michael J. Spencer25b15772011-06-25 17:55:23 +0000454error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
455 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000456 // FIXME: thread safety.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000457 static char result[34];
Benjamin Kramer7d145782011-07-15 00:14:48 +0000458 if (is64BitLoadCommand(MachOObj, DRI)) {
459 InMemoryStruct<macho::Segment64LoadCommand> SLC;
460 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
461 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
462 InMemoryStruct<macho::Section64> Sect;
463 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
464
Benjamin Kramer291e7672011-07-15 00:29:02 +0000465 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000466 strcat(result, ",");
467 strcat(result, Sect->Name);
468 } else {
469 InMemoryStruct<macho::SegmentLoadCommand> SLC;
470 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
471 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
472 InMemoryStruct<macho::Section> Sect;
473 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
474
Benjamin Kramer291e7672011-07-15 00:29:02 +0000475 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000476 strcat(result, ",");
477 strcat(result, Sect->Name);
478 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000479 Result = StringRef(result);
480 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000481}
482
Michael J. Spencer25b15772011-06-25 17:55:23 +0000483error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
484 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000485 if (is64BitLoadCommand(MachOObj, DRI)) {
486 InMemoryStruct<macho::Section64> Sect;
487 getSection64(DRI, Sect);
488 Result = Sect->Address;
489 } else {
490 InMemoryStruct<macho::Section> Sect;
491 getSection(DRI, Sect);
492 Result = Sect->Address;
493 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000494 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000495}
496
Michael J. Spencer25b15772011-06-25 17:55:23 +0000497error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
498 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000499 if (is64BitLoadCommand(MachOObj, DRI)) {
500 InMemoryStruct<macho::Section64> Sect;
501 getSection64(DRI, Sect);
502 Result = Sect->Size;
503 } else {
504 InMemoryStruct<macho::Section> Sect;
505 getSection(DRI, Sect);
506 Result = Sect->Size;
507 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000508 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000509}
510
Michael J. Spencer25b15772011-06-25 17:55:23 +0000511error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
512 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000513 if (is64BitLoadCommand(MachOObj, DRI)) {
514 InMemoryStruct<macho::Section64> Sect;
515 getSection64(DRI, Sect);
516 Result = MachOObj->getData(Sect->Offset, Sect->Size);
517 } else {
518 InMemoryStruct<macho::Section> Sect;
519 getSection(DRI, Sect);
520 Result = MachOObj->getData(Sect->Offset, Sect->Size);
521 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000522 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000523}
524
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000525error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
526 uint64_t &Result) const {
527 if (is64BitLoadCommand(MachOObj, DRI)) {
528 InMemoryStruct<macho::Section64> Sect;
529 getSection64(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000530 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000531 } else {
532 InMemoryStruct<macho::Section> Sect;
533 getSection(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000534 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000535 }
536 return object_error::success;
537}
538
Michael J. Spencer25b15772011-06-25 17:55:23 +0000539error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
540 bool &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000541 if (is64BitLoadCommand(MachOObj, DRI)) {
542 InMemoryStruct<macho::Section64> Sect;
543 getSection64(DRI, Sect);
544 Result = !strcmp(Sect->Name, "__text");
545 } else {
546 InMemoryStruct<macho::Section> Sect;
547 getSection(DRI, Sect);
548 Result = !strcmp(Sect->Name, "__text");
549 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000550 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000551}
552
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000553error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
554 bool &Result) const {
555 // FIXME: Unimplemented.
556 Result = false;
557 return object_error::success;
558}
559
560error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
561 bool &Result) const {
562 // FIXME: Unimplemented.
563 Result = false;
564 return object_error::success;
565}
566
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000567error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
568 DataRefImpl Symb,
569 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000570 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000571 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000572 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000573 Result = false;
574 return object_error::success;
575 }
576
577 uint64_t SectBegin, SectEnd;
578 getSectionAddress(Sec, SectBegin);
579 getSectionSize(Sec, SectEnd);
580 SectEnd += SectBegin;
581
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000582 if (MachOObj->is64Bit()) {
583 InMemoryStruct<macho::Symbol64TableEntry> Entry;
584 getSymbol64TableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000585 uint64_t SymAddr= Entry->Value;
586 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000587 } else {
588 InMemoryStruct<macho::SymbolTableEntry> Entry;
589 getSymbolTableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000590 uint64_t SymAddr= Entry->Value;
591 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000592 }
Owen Andersoncd749882011-10-12 22:21:32 +0000593
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000594 return object_error::success;
595}
596
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000597relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
598 DataRefImpl ret;
599 ret.d.a = 0;
600 ret.d.b = getSectionIndex(Sec);
601 return relocation_iterator(RelocationRef(ret, this));
602}
603relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
604 uint32_t last_reloc;
605 if (is64BitLoadCommand(MachOObj, Sec)) {
606 InMemoryStruct<macho::Section64> Sect;
607 getSection64(Sec, Sect);
608 last_reloc = Sect->NumRelocationTableEntries;
609 } else {
610 InMemoryStruct<macho::Section> Sect;
611 getSection(Sec, Sect);
612 last_reloc = Sect->NumRelocationTableEntries;
613 }
614 DataRefImpl ret;
615 ret.d.a = last_reloc;
616 ret.d.b = getSectionIndex(Sec);
617 return relocation_iterator(RelocationRef(ret, this));
618}
619
620section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000621 DataRefImpl DRI;
622 DRI.d.a = DRI.d.b = 0;
623 moveToNextSection(DRI);
624 return section_iterator(SectionRef(DRI, this));
625}
626
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000627section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000628 DataRefImpl DRI;
629 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
630 DRI.d.b = 0;
631 return section_iterator(SectionRef(DRI, this));
632}
633
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000634/*===-- Relocations -------------------------------------------------------===*/
635
636void MachOObjectFile::
637getRelocation(DataRefImpl Rel,
638 InMemoryStruct<macho::RelocationEntry> &Res) const {
639 uint32_t relOffset;
640 if (MachOObj->is64Bit()) {
641 InMemoryStruct<macho::Section64> Sect;
642 getSection64(Sections[Rel.d.b], Sect);
643 relOffset = Sect->RelocationTableOffset;
644 } else {
645 InMemoryStruct<macho::Section> Sect;
646 getSection(Sections[Rel.d.b], Sect);
647 relOffset = Sect->RelocationTableOffset;
648 }
649 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
650}
651error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
652 RelocationRef &Res) const {
653 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000654 Res = RelocationRef(Rel, this);
655 return object_error::success;
656}
657error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
658 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000659 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000660 if (MachOObj->is64Bit()) {
661 InMemoryStruct<macho::Section64> Sect;
662 getSection64(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000663 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000664 } else {
665 InMemoryStruct<macho::Section> Sect;
666 getSection(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000667 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000668 }
669 InMemoryStruct<macho::RelocationEntry> RE;
670 getRelocation(Rel, RE);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000671
672 unsigned Arch = getArch();
673 bool isScattered = (Arch != Triple::x86_64) &&
674 (RE->Word0 & macho::RF_Scattered);
675 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000676 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000677 RelAddr = RE->Word0 & 0xFFFFFF;
678 else
679 RelAddr = RE->Word0;
680
681 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000682 return object_error::success;
683}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000684error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
685 uint64_t &Res) const {
686 InMemoryStruct<macho::RelocationEntry> RE;
687 getRelocation(Rel, RE);
688
689 unsigned Arch = getArch();
690 bool isScattered = (Arch != Triple::x86_64) &&
691 (RE->Word0 & macho::RF_Scattered);
692 if (isScattered)
693 Res = RE->Word0 & 0xFFFFFF;
694 else
695 Res = RE->Word0;
696 return object_error::success;
697}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000698error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
699 SymbolRef &Res) const {
700 InMemoryStruct<macho::RelocationEntry> RE;
701 getRelocation(Rel, RE);
702 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
703 bool isExtern = (RE->Word1 >> 27) & 1;
704
705 DataRefImpl Sym;
706 Sym.d.a = Sym.d.b = 0;
707 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000708 if (isExtern) {
709 for (unsigned i = 0; i < SymbolIdx; i++) {
710 Sym.d.b++;
711 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000712 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000713 "Relocation symbol index out of range!");
714 }
715 }
716 Res = SymbolRef(Sym, this);
717 return object_error::success;
718}
719error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000720 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000721 InMemoryStruct<macho::RelocationEntry> RE;
722 getRelocation(Rel, RE);
Owen Andersonf8261e72011-10-26 17:10:22 +0000723 Res = RE->Word0;
724 Res <<= 32;
725 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000726 return object_error::success;
727}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000728error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
729 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000730 // TODO: Support scattered relocations.
731 StringRef res;
732 InMemoryStruct<macho::RelocationEntry> RE;
733 getRelocation(Rel, RE);
Owen Anderson0135fe12011-10-24 21:44:00 +0000734
735 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000736 bool isScattered = (Arch != Triple::x86_64) &&
737 (RE->Word0 & macho::RF_Scattered);
738
739 unsigned r_type;
740 if (isScattered)
741 r_type = (RE->Word0 >> 24) & 0xF;
742 else
743 r_type = (RE->Word1 >> 28) & 0xF;
744
Owen Anderson0135fe12011-10-24 21:44:00 +0000745 switch (Arch) {
746 case Triple::x86: {
747 const char* Table[] = {
748 "GENERIC_RELOC_VANILLA",
749 "GENERIC_RELOC_PAIR",
750 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000751 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000752 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000753 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000754
Owen Andersoneb6bd332011-10-27 20:46:09 +0000755 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000756 res = "Unknown";
757 else
758 res = Table[r_type];
759 break;
760 }
761 case Triple::x86_64: {
762 const char* Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000763 "X86_64_RELOC_UNSIGNED",
764 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000765 "X86_64_RELOC_BRANCH",
766 "X86_64_RELOC_GOT_LOAD",
767 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000768 "X86_64_RELOC_SUBTRACTOR",
769 "X86_64_RELOC_SIGNED_1",
770 "X86_64_RELOC_SIGNED_2",
771 "X86_64_RELOC_SIGNED_4",
772 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000773
Owen Andersond8fa76d2011-10-24 23:20:07 +0000774 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000775 res = "Unknown";
776 else
777 res = Table[r_type];
778 break;
779 }
780 case Triple::arm: {
781 const char* Table[] = {
782 "ARM_RELOC_VANILLA",
783 "ARM_RELOC_PAIR",
784 "ARM_RELOC_SECTDIFF",
785 "ARM_RELOC_LOCAL_SECTDIFF",
786 "ARM_RELOC_PB_LA_PTR",
787 "ARM_RELOC_BR24",
788 "ARM_THUMB_RELOC_BR22",
789 "ARM_THUMB_32BIT_BRANCH",
790 "ARM_RELOC_HALF",
791 "ARM_RELOC_HALF_SECTDIFF" };
792
793 if (r_type > 9)
794 res = "Unknown";
795 else
796 res = Table[r_type];
797 break;
798 }
799 case Triple::ppc: {
800 const char* Table[] = {
801 "PPC_RELOC_VANILLA",
802 "PPC_RELOC_PAIR",
803 "PPC_RELOC_BR14",
804 "PPC_RELOC_BR24",
805 "PPC_RELOC_HI16",
806 "PPC_RELOC_LO16",
807 "PPC_RELOC_HA16",
808 "PPC_RELOC_LO14",
809 "PPC_RELOC_SECTDIFF",
810 "PPC_RELOC_PB_LA_PTR",
811 "PPC_RELOC_HI16_SECTDIFF",
812 "PPC_RELOC_LO16_SECTDIFF",
813 "PPC_RELOC_HA16_SECTDIFF",
814 "PPC_RELOC_JBSR",
815 "PPC_RELOC_LO14_SECTDIFF",
816 "PPC_RELOC_LOCAL_SECTDIFF" };
817
818 res = Table[r_type];
819 break;
820 }
821 case Triple::UnknownArch:
822 res = "Unknown";
823 break;
824 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000825 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000826 return object_error::success;
827}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000828error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
829 int64_t &Res) const {
830 InMemoryStruct<macho::RelocationEntry> RE;
831 getRelocation(Rel, RE);
832 bool isExtern = (RE->Word1 >> 27) & 1;
833 Res = 0;
834 if (!isExtern) {
835 const uint8_t* sectAddress = base();
836 if (MachOObj->is64Bit()) {
837 InMemoryStruct<macho::Section64> Sect;
838 getSection64(Sections[Rel.d.b], Sect);
839 sectAddress += Sect->Offset;
840 } else {
841 InMemoryStruct<macho::Section> Sect;
842 getSection(Sections[Rel.d.b], Sect);
843 sectAddress += Sect->Offset;
844 }
845 Res = reinterpret_cast<uintptr_t>(sectAddress);
846 }
847 return object_error::success;
848}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000849
850// Helper to advance a section or symbol iterator multiple increments at a time.
851template<class T>
852error_code advance(T &it, size_t Val) {
853 error_code ec;
854 while (Val--) {
855 it.increment(ec);
856 }
857 return ec;
858}
859
860template<class T>
861void advanceTo(T &it, size_t Val) {
862 if (error_code ec = advance(it, Val))
863 report_fatal_error(ec.message());
864}
865
Owen Anderson1832f4d2011-10-26 20:42:54 +0000866void MachOObjectFile::printRelocationTargetName(
867 InMemoryStruct<macho::RelocationEntry>& RE,
868 raw_string_ostream &fmt) const {
869 unsigned Arch = getArch();
870 bool isScattered = (Arch != Triple::x86_64) &&
871 (RE->Word0 & macho::RF_Scattered);
872
873 // Target of a scattered relocation is an address. In the interest of
874 // generating pretty output, scan through the symbol table looking for a
875 // symbol that aligns with that address. If we find one, print it.
876 // Otherwise, we just print the hex address of the target.
877 if (isScattered) {
878 uint32_t Val = RE->Word1;
879
880 error_code ec;
881 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
882 SI.increment(ec)) {
883 if (ec) report_fatal_error(ec.message());
884
885 uint64_t Addr;
886 StringRef Name;
887
888 if ((ec = SI->getAddress(Addr)))
889 report_fatal_error(ec.message());
890 if (Addr != Val) continue;
891 if ((ec = SI->getName(Name)))
892 report_fatal_error(ec.message());
893 fmt << Name;
894 return;
895 }
896
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000897 // If we couldn't find a symbol that this relocation refers to, try
898 // to find a section beginning instead.
899 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
900 SI.increment(ec)) {
901 if (ec) report_fatal_error(ec.message());
902
903 uint64_t Addr;
904 StringRef Name;
905
906 if ((ec = SI->getAddress(Addr)))
907 report_fatal_error(ec.message());
908 if (Addr != Val) continue;
909 if ((ec = SI->getName(Name)))
910 report_fatal_error(ec.message());
911 fmt << Name;
912 return;
913 }
914
Owen Anderson1832f4d2011-10-26 20:42:54 +0000915 fmt << format("0x%x", Val);
916 return;
917 }
918
919 StringRef S;
920 bool isExtern = (RE->Word1 >> 27) & 1;
921 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000922
923 if (isExtern) {
924 symbol_iterator SI = begin_symbols();
925 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000926 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000927 } else {
928 section_iterator SI = begin_sections();
929 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000930 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000931 }
932
Owen Anderson1832f4d2011-10-26 20:42:54 +0000933 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000934}
935
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000936error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
937 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000938 InMemoryStruct<macho::RelocationEntry> RE;
939 getRelocation(Rel, RE);
940
Owen Anderson1832f4d2011-10-26 20:42:54 +0000941 unsigned Arch = getArch();
942 bool isScattered = (Arch != Triple::x86_64) &&
943 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000944
Owen Anderson013d7562011-10-25 18:48:41 +0000945 std::string fmtbuf;
946 raw_string_ostream fmt(fmtbuf);
947
Owen Anderson1832f4d2011-10-26 20:42:54 +0000948 unsigned Type;
949 if (isScattered)
950 Type = (RE->Word0 >> 24) & 0xF;
951 else
952 Type = (RE->Word1 >> 28) & 0xF;
953
Owen Andersoneb6bd332011-10-27 20:46:09 +0000954 bool isPCRel;
955 if (isScattered)
956 isPCRel = ((RE->Word0 >> 30) & 1);
957 else
958 isPCRel = ((RE->Word1 >> 24) & 1);
959
Owen Andersond8fa76d2011-10-24 23:20:07 +0000960 // Determine any addends that should be displayed with the relocation.
961 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +0000962
963 // X86_64 has entirely custom relocation types.
964 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +0000965 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +0000966
Owen Andersond8fa76d2011-10-24 23:20:07 +0000967 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +0000968 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
969 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
970 printRelocationTargetName(RE, fmt);
971 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +0000972 if (isPCRel) fmt << "PCREL";
973 break;
974 }
Owen Anderson1832f4d2011-10-26 20:42:54 +0000975 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +0000976 InMemoryStruct<macho::RelocationEntry> RENext;
977 DataRefImpl RelNext = Rel;
978 RelNext.d.a++;
979 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000980
981 // X86_64_SUBTRACTOR must be followed by a relocation of type
982 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +0000983 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +0000984 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000985 if (RType != 0)
986 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
987 "X86_64_RELOC_SUBTRACTOR.");
988
Owen Andersonef22f782011-10-26 17:28:49 +0000989 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
990 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +0000991 printRelocationTargetName(RENext, fmt);
992 fmt << "-";
993 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000994 }
Owen Andersoneb6bd332011-10-27 20:46:09 +0000995 case macho::RIT_X86_64_TLV:
996 printRelocationTargetName(RE, fmt);
997 fmt << "@TLV";
998 if (isPCRel) fmt << "P";
999 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001000 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1001 printRelocationTargetName(RE, fmt);
1002 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001003 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001004 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1005 printRelocationTargetName(RE, fmt);
1006 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001007 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001008 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1009 printRelocationTargetName(RE, fmt);
1010 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001011 break;
1012 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001013 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001014 break;
1015 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001016 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001017 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1018 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001019 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001020 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001021 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001022 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001023 InMemoryStruct<macho::RelocationEntry> RENext;
1024 DataRefImpl RelNext = Rel;
1025 RelNext.d.a++;
1026 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001027
1028 // X86 sect diff's must be followed by a relocation of type
1029 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001030 bool isNextScattered = (Arch != Triple::x86_64) &&
1031 (RENext->Word0 & macho::RF_Scattered);
1032 unsigned RType;
1033 if (isNextScattered)
1034 RType = (RENext->Word0 >> 24) & 0xF;
1035 else
1036 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001037 if (RType != 1)
1038 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001039 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001040
Owen Anderson1832f4d2011-10-26 20:42:54 +00001041 printRelocationTargetName(RE, fmt);
1042 fmt << "-";
1043 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001044 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001045 }
1046 }
Owen Anderson013d7562011-10-25 18:48:41 +00001047
Owen Anderson1832f4d2011-10-26 20:42:54 +00001048 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001049 // All X86 relocations that need special printing were already
1050 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001051 switch (Type) {
1052 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1053 InMemoryStruct<macho::RelocationEntry> RENext;
1054 DataRefImpl RelNext = Rel;
1055 RelNext.d.a++;
1056 getRelocation(RelNext, RENext);
1057
1058 // X86 sect diff's must be followed by a relocation of type
1059 // GENERIC_RELOC_PAIR.
1060 bool isNextScattered = (Arch != Triple::x86_64) &&
1061 (RENext->Word0 & macho::RF_Scattered);
1062 unsigned RType;
1063 if (isNextScattered)
1064 RType = (RENext->Word0 >> 24) & 0xF;
1065 else
1066 RType = (RENext->Word1 >> 28) & 0xF;
1067 if (RType != 1)
1068 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1069 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1070
1071 printRelocationTargetName(RE, fmt);
1072 fmt << "-";
1073 printRelocationTargetName(RENext, fmt);
1074 break;
1075 }
1076 case macho::RIT_Generic_TLV: {
1077 printRelocationTargetName(RE, fmt);
1078 fmt << "@TLV";
1079 if (isPCRel) fmt << "P";
1080 break;
1081 }
1082 default:
1083 printRelocationTargetName(RE, fmt);
1084 }
Owen Anderson013d7562011-10-25 18:48:41 +00001085 } else { // ARM-specific relocations
1086 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001087 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1088 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001089 // Half relocations steal a bit from the length field to encode
1090 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001091 bool isUpper;
1092 if (isScattered)
1093 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001094 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001095 isUpper = (RE->Word1 >> 25) & 1;
1096
1097 if (isUpper)
1098 fmt << ":upper16:(";
1099 else
1100 fmt << ":lower16:(";
1101 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001102
1103 InMemoryStruct<macho::RelocationEntry> RENext;
1104 DataRefImpl RelNext = Rel;
1105 RelNext.d.a++;
1106 getRelocation(RelNext, RENext);
1107
1108 // ARM half relocs must be followed by a relocation of type
1109 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001110 bool isNextScattered = (Arch != Triple::x86_64) &&
1111 (RENext->Word0 & macho::RF_Scattered);
1112 unsigned RType;
1113 if (isNextScattered)
1114 RType = (RENext->Word0 >> 24) & 0xF;
1115 else
1116 RType = (RENext->Word1 >> 28) & 0xF;
1117
Owen Anderson013d7562011-10-25 18:48:41 +00001118 if (RType != 1)
1119 report_fatal_error("Expected ARM_RELOC_PAIR after "
1120 "GENERIC_RELOC_HALF");
1121
Owen Anderson1832f4d2011-10-26 20:42:54 +00001122 // NOTE: The half of the target virtual address is stashed in the
1123 // address field of the secondary relocation, but we can't reverse
1124 // engineer the constant offset from it without decoding the movw/movt
1125 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001126
1127 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1128 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001129 if (Type == macho::RIT_ARM_HalfDifference) {
1130 fmt << "-";
1131 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001132 }
1133
Owen Anderson013d7562011-10-25 18:48:41 +00001134 fmt << ")";
1135 break;
1136 }
1137 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001138 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001139 }
1140 }
1141 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001142 } else
1143 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001144
Owen Anderson0135fe12011-10-24 21:44:00 +00001145 fmt.flush();
1146 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001147 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001148}
1149
Owen Anderson0685e942011-10-25 20:35:53 +00001150error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1151 bool &Result) const {
1152 InMemoryStruct<macho::RelocationEntry> RE;
1153 getRelocation(Rel, RE);
1154
Owen Anderson0685e942011-10-25 20:35:53 +00001155 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001156 bool isScattered = (Arch != Triple::x86_64) &&
1157 (RE->Word0 & macho::RF_Scattered);
1158 unsigned Type;
1159 if (isScattered)
1160 Type = (RE->Word0 >> 24) & 0xF;
1161 else
1162 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001163
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) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001169 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001170 } 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.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001173 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001174 DataRefImpl RelPrev = Rel;
1175 RelPrev.d.a--;
1176 InMemoryStruct<macho::RelocationEntry> REPrev;
1177 getRelocation(RelPrev, REPrev);
1178
1179 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1180
Owen Anderson1832f4d2011-10-26 20:42:54 +00001181 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001182 }
1183 }
1184
1185 return object_error::success;
1186}
1187
David Meyer5c2b4ea2012-03-01 01:36:50 +00001188error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1189 LibraryRef &Res) const {
1190 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1191}
1192
1193error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1194 StringRef &Res) const {
1195 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1196}
1197
1198
Eric Christopher6256b032011-04-22 03:19:48 +00001199/*===-- Miscellaneous -----------------------------------------------------===*/
1200
1201uint8_t MachOObjectFile::getBytesInAddress() const {
1202 return MachOObj->is64Bit() ? 8 : 4;
1203}
1204
1205StringRef MachOObjectFile::getFileFormatName() const {
1206 if (!MachOObj->is64Bit()) {
1207 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001208 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001209 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001210 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001211 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001212 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001213 return "Mach-O 32-bit ppc";
1214 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001215 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001216 "64-bit object file when we're not 64-bit?");
1217 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001218 }
1219 }
1220
1221 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001222 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001223 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001224 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001225 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001226 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001227 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001228 "32-bit object file when we're 64-bit?");
1229 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001230 }
1231}
1232
1233unsigned MachOObjectFile::getArch() const {
1234 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001235 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001236 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001237 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001238 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001239 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001240 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001241 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001242 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001243 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001244 return Triple::ppc64;
1245 default:
1246 return Triple::UnknownArch;
1247 }
1248}
1249
Owen Andersonf7c93a32011-10-11 17:32:27 +00001250} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001251} // end namespace llvm