blob: 45aeaac6b8311bcd4fce33ad906c8d7ab50def48 [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)
David Meyer6f9489a2012-03-09 20:41:57 +000033 : ObjectFile(Binary::ID_MachO, Object, ec),
Benjamin Kramer0fcab072011-09-08 20:52:17 +000034 MachOObj(MOO),
35 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
36 DataRefImpl DRI;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000037 moveToNextSection(DRI);
38 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
39 while (DRI.d.a < LoadCommandCount) {
40 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000041 DRI.d.b++;
42 moveToNextSection(DRI);
43 }
44}
45
46
Eric Christopher6256b032011-04-22 03:19:48 +000047ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000048 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000049 std::string Err;
50 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
51 if (!MachOObj)
52 return NULL;
Michael J. Spencer001c9202011-06-25 17:54:50 +000053 return new MachOObjectFile(Buffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000054}
55
56/*===-- Symbols -----------------------------------------------------------===*/
57
58void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
59 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
60 while (DRI.d.a < LoadCommandCount) {
61 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
62 if (LCI.Command.Type == macho::LCT_Symtab) {
63 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
64 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
65 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
66 return;
67 }
68
69 DRI.d.a++;
70 DRI.d.b = 0;
71 }
72}
73
74void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
75 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
76 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
77 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
78 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
79
80 if (RegisteredStringTable != DRI.d.a) {
81 MachOObj->RegisterStringTable(*SymtabLoadCmd);
82 RegisteredStringTable = DRI.d.a;
83 }
84
85 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
86 Res);
87}
88
Benjamin Kramer32fb2af2011-07-15 17:32:45 +000089void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
90 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
91 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
92 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
93 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
94
95 if (RegisteredStringTable != DRI.d.a) {
96 MachOObj->RegisterStringTable(*SymtabLoadCmd);
97 RegisteredStringTable = DRI.d.a;
98 }
99
100 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
101 Res);
102}
103
Eric Christopher6256b032011-04-22 03:19:48 +0000104
Michael J. Spencer25b15772011-06-25 17:55:23 +0000105error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
106 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000107 DRI.d.b++;
108 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000109 Result = SymbolRef(DRI, this);
110 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000111}
112
Michael J. Spencer25b15772011-06-25 17:55:23 +0000113error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
114 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000115 if (MachOObj->is64Bit()) {
116 InMemoryStruct<macho::Symbol64TableEntry> Entry;
117 getSymbol64TableEntry(DRI, Entry);
118 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
119 } else {
120 InMemoryStruct<macho::SymbolTableEntry> Entry;
121 getSymbolTableEntry(DRI, Entry);
122 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
123 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000124 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000125}
126
Danil Malyshevb0436a72011-11-29 17:40:10 +0000127error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
128 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000129 if (MachOObj->is64Bit()) {
130 InMemoryStruct<macho::Symbol64TableEntry> Entry;
131 getSymbol64TableEntry(DRI, Entry);
132 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000133 if (Entry->SectionIndex) {
134 InMemoryStruct<macho::Section64> Section;
135 getSection64(Sections[Entry->SectionIndex-1], Section);
136 Result += Section->Offset - Section->Address;
137 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000138 } else {
139 InMemoryStruct<macho::SymbolTableEntry> Entry;
140 getSymbolTableEntry(DRI, Entry);
141 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000142 if (Entry->SectionIndex) {
143 InMemoryStruct<macho::Section> Section;
144 getSection(Sections[Entry->SectionIndex-1], Section);
145 Result += Section->Offset - Section->Address;
146 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000147 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000148
Michael J. Spencer25b15772011-06-25 17:55:23 +0000149 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000150}
151
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000152error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
153 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000154 if (MachOObj->is64Bit()) {
155 InMemoryStruct<macho::Symbol64TableEntry> Entry;
156 getSymbol64TableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000157 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000158 } else {
159 InMemoryStruct<macho::SymbolTableEntry> Entry;
160 getSymbolTableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000161 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000162 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000163 return object_error::success;
164}
165
Michael J. Spencer25b15772011-06-25 17:55:23 +0000166error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
167 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000168 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
169 uint64_t BeginOffset;
170 uint64_t EndOffset = 0;
171 uint8_t SectionIndex;
172 if (MachOObj->is64Bit()) {
173 InMemoryStruct<macho::Symbol64TableEntry> Entry;
174 getSymbol64TableEntry(DRI, Entry);
175 BeginOffset = Entry->Value;
176 SectionIndex = Entry->SectionIndex;
177 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000178 uint32_t flags = SymbolRef::SF_None;
179 getSymbolFlags(DRI, flags);
180 if (flags & SymbolRef::SF_Common)
181 Result = Entry->Value;
182 else
183 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000184 return object_error::success;
185 }
186 // Unfortunately symbols are unsorted so we need to touch all
187 // symbols from load command
188 DRI.d.b = 0;
189 uint32_t Command = DRI.d.a;
190 while (Command == DRI.d.a) {
191 moveToNextSymbol(DRI);
192 if (DRI.d.a < LoadCommandCount) {
193 getSymbol64TableEntry(DRI, Entry);
194 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
195 if (!EndOffset || Entry->Value < EndOffset)
196 EndOffset = Entry->Value;
197 }
198 DRI.d.b++;
199 }
200 } else {
201 InMemoryStruct<macho::SymbolTableEntry> Entry;
202 getSymbolTableEntry(DRI, Entry);
203 BeginOffset = Entry->Value;
204 SectionIndex = Entry->SectionIndex;
205 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000206 uint32_t flags = SymbolRef::SF_None;
207 getSymbolFlags(DRI, flags);
208 if (flags & SymbolRef::SF_Common)
209 Result = Entry->Value;
210 else
211 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000212 return object_error::success;
213 }
214 // Unfortunately symbols are unsorted so we need to touch all
215 // symbols from load command
216 DRI.d.b = 0;
217 uint32_t Command = DRI.d.a;
218 while (Command == DRI.d.a) {
219 moveToNextSymbol(DRI);
220 if (DRI.d.a < LoadCommandCount) {
221 getSymbolTableEntry(DRI, Entry);
222 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
223 if (!EndOffset || Entry->Value < EndOffset)
224 EndOffset = Entry->Value;
225 }
226 DRI.d.b++;
227 }
228 }
229 if (!EndOffset) {
230 uint64_t Size;
231 getSectionSize(Sections[SectionIndex-1], Size);
232 getSectionAddress(Sections[SectionIndex-1], EndOffset);
233 EndOffset += Size;
234 }
235 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000236 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000237}
238
Michael J. Spencer25b15772011-06-25 17:55:23 +0000239error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
240 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000241 uint8_t Type, Flags;
242 if (MachOObj->is64Bit()) {
243 InMemoryStruct<macho::Symbol64TableEntry> Entry;
244 getSymbol64TableEntry(DRI, Entry);
245 Type = Entry->Type;
246 Flags = Entry->Flags;
247 } else {
248 InMemoryStruct<macho::SymbolTableEntry> Entry;
249 getSymbolTableEntry(DRI, Entry);
250 Type = Entry->Type;
251 Flags = Entry->Flags;
252 }
Eric Christopher6256b032011-04-22 03:19:48 +0000253
254 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000255 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000256 case macho::STT_Undefined:
257 Char = 'u';
258 break;
259 case macho::STT_Absolute:
260 case macho::STT_Section:
261 Char = 's';
262 break;
263 default:
264 Char = '?';
265 break;
266 }
267
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000268 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Eric Christopher6256b032011-04-22 03:19:48 +0000269 Char = toupper(Char);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000270 Result = Char;
271 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000272}
273
David Meyerc46255a2012-02-28 23:47:53 +0000274error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
275 uint32_t &Result) const {
276 uint16_t MachOFlags;
277 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000278 if (MachOObj->is64Bit()) {
279 InMemoryStruct<macho::Symbol64TableEntry> Entry;
280 getSymbol64TableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000281 MachOFlags = Entry->Flags;
282 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000283 } else {
284 InMemoryStruct<macho::SymbolTableEntry> Entry;
285 getSymbolTableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000286 MachOFlags = Entry->Flags;
287 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000288 }
289
Preston Gurdc68dda82012-04-12 20:13:57 +0000290 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000291 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000292
293 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
294 Result |= SymbolRef::SF_Undefined;
295
David Meyerc46255a2012-02-28 23:47:53 +0000296 if (MachOFlags & macho::STF_StabsEntryMask)
297 Result |= SymbolRef::SF_FormatSpecific;
298
Preston Gurdc68dda82012-04-12 20:13:57 +0000299 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000300 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000301 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
302 Result |= SymbolRef::SF_Common;
303 }
David Meyerc46255a2012-02-28 23:47:53 +0000304
305 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
306 Result |= SymbolRef::SF_Weak;
307
308 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
309 Result |= SymbolRef::SF_Absolute;
310
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000311 return object_error::success;
312}
313
314error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
315 section_iterator &Res) const {
316 uint8_t index;
317 if (MachOObj->is64Bit()) {
318 InMemoryStruct<macho::Symbol64TableEntry> Entry;
319 getSymbol64TableEntry(Symb, Entry);
320 index = Entry->SectionIndex;
321 } else {
322 InMemoryStruct<macho::SymbolTableEntry> Entry;
323 getSymbolTableEntry(Symb, Entry);
324 index = Entry->SectionIndex;
325 }
326
327 if (index == 0)
328 Res = end_sections();
329 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000330 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000331
332 return object_error::success;
333}
334
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000335error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000336 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000337 uint8_t n_type;
338 if (MachOObj->is64Bit()) {
339 InMemoryStruct<macho::Symbol64TableEntry> Entry;
340 getSymbol64TableEntry(Symb, Entry);
341 n_type = Entry->Type;
342 } else {
343 InMemoryStruct<macho::SymbolTableEntry> Entry;
344 getSymbolTableEntry(Symb, Entry);
345 n_type = Entry->Type;
346 }
347 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000348
349 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000350 if (n_type & MachO::NlistMaskStab) {
351 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000352 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000353 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000354
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000355 switch (n_type & MachO::NlistMaskType) {
356 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000357 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000358 break;
359 case MachO::NListTypeSection :
360 Res = SymbolRef::ST_Function;
361 break;
362 }
363 return object_error::success;
364}
365
Tim Northovera41dce32012-10-29 10:47:00 +0000366error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
367 uint64_t &Val) const {
368 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
369}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000370
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000371symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000372 // DRI.d.a = segment number; DRI.d.b = symbol index.
373 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000374 moveToNextSymbol(DRI);
375 return symbol_iterator(SymbolRef(DRI, this));
376}
377
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000378symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000379 DataRefImpl DRI;
380 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000381 return symbol_iterator(SymbolRef(DRI, this));
382}
383
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000384symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
385 // TODO: implement
386 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
387}
388
389symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
390 // TODO: implement
391 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
392}
Eric Christopher6256b032011-04-22 03:19:48 +0000393
David Meyer5c2b4ea2012-03-01 01:36:50 +0000394library_iterator MachOObjectFile::begin_libraries_needed() const {
395 // TODO: implement
396 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
397}
398
399library_iterator MachOObjectFile::end_libraries_needed() const {
400 // TODO: implement
401 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
402}
403
David Meyer97f77872012-03-01 22:19:54 +0000404StringRef MachOObjectFile::getLoadName() const {
405 // TODO: Implement
406 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
407}
408
Eric Christopher6256b032011-04-22 03:19:48 +0000409/*===-- Sections ----------------------------------------------------------===*/
410
411void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
412 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
413 while (DRI.d.a < LoadCommandCount) {
414 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
415 if (LCI.Command.Type == macho::LCT_Segment) {
416 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
417 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
418 if (DRI.d.b < SegmentLoadCmd->NumSections)
419 return;
420 } else if (LCI.Command.Type == macho::LCT_Segment64) {
421 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
422 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
423 if (DRI.d.b < Segment64LoadCmd->NumSections)
424 return;
425 }
426
427 DRI.d.a++;
428 DRI.d.b = 0;
429 }
430}
431
Michael J. Spencer25b15772011-06-25 17:55:23 +0000432error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
433 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000434 DRI.d.b++;
435 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000436 Result = SectionRef(DRI, this);
437 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000438}
439
440void
441MachOObjectFile::getSection(DataRefImpl DRI,
442 InMemoryStruct<macho::Section> &Res) const {
443 InMemoryStruct<macho::SegmentLoadCommand> SLC;
444 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
445 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
446 MachOObj->ReadSection(LCI, DRI.d.b, Res);
447}
448
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000449std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
450 SectionList::const_iterator loc =
451 std::find(Sections.begin(), Sections.end(), Sec);
452 assert(loc != Sections.end() && "Sec is not a valid section!");
453 return std::distance(Sections.begin(), loc);
454}
455
Benjamin Kramer7d145782011-07-15 00:14:48 +0000456void
457MachOObjectFile::getSection64(DataRefImpl DRI,
458 InMemoryStruct<macho::Section64> &Res) const {
459 InMemoryStruct<macho::Segment64LoadCommand> SLC;
460 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
461 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
462 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
463}
464
465static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
466 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
467 if (LCI.Command.Type == macho::LCT_Segment64)
468 return true;
469 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
470 return false;
471}
472
Michael J. Spencer25b15772011-06-25 17:55:23 +0000473error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
474 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000475 // FIXME: thread safety.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000476 static char result[34];
Benjamin Kramer7d145782011-07-15 00:14:48 +0000477 if (is64BitLoadCommand(MachOObj, DRI)) {
478 InMemoryStruct<macho::Segment64LoadCommand> SLC;
479 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
480 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
481 InMemoryStruct<macho::Section64> Sect;
482 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
483
Benjamin Kramer291e7672011-07-15 00:29:02 +0000484 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000485 strcat(result, ",");
486 strcat(result, Sect->Name);
487 } else {
488 InMemoryStruct<macho::SegmentLoadCommand> SLC;
489 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
490 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
491 InMemoryStruct<macho::Section> Sect;
492 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
493
Benjamin Kramer291e7672011-07-15 00:29:02 +0000494 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000495 strcat(result, ",");
496 strcat(result, Sect->Name);
497 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000498 Result = StringRef(result);
499 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000500}
501
Michael J. Spencer25b15772011-06-25 17:55:23 +0000502error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
503 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000504 if (is64BitLoadCommand(MachOObj, DRI)) {
505 InMemoryStruct<macho::Section64> Sect;
506 getSection64(DRI, Sect);
507 Result = Sect->Address;
508 } else {
509 InMemoryStruct<macho::Section> Sect;
510 getSection(DRI, Sect);
511 Result = Sect->Address;
512 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000513 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000514}
515
Michael J. Spencer25b15772011-06-25 17:55:23 +0000516error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
517 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000518 if (is64BitLoadCommand(MachOObj, DRI)) {
519 InMemoryStruct<macho::Section64> Sect;
520 getSection64(DRI, Sect);
521 Result = Sect->Size;
522 } else {
523 InMemoryStruct<macho::Section> Sect;
524 getSection(DRI, Sect);
525 Result = Sect->Size;
526 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000527 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000528}
529
Michael J. Spencer25b15772011-06-25 17:55:23 +0000530error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
531 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000532 if (is64BitLoadCommand(MachOObj, DRI)) {
533 InMemoryStruct<macho::Section64> Sect;
534 getSection64(DRI, Sect);
535 Result = MachOObj->getData(Sect->Offset, Sect->Size);
536 } else {
537 InMemoryStruct<macho::Section> Sect;
538 getSection(DRI, Sect);
539 Result = MachOObj->getData(Sect->Offset, Sect->Size);
540 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000541 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000542}
543
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000544error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
545 uint64_t &Result) const {
546 if (is64BitLoadCommand(MachOObj, DRI)) {
547 InMemoryStruct<macho::Section64> Sect;
548 getSection64(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000549 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000550 } else {
551 InMemoryStruct<macho::Section> Sect;
552 getSection(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000553 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000554 }
555 return object_error::success;
556}
557
Michael J. Spencer25b15772011-06-25 17:55:23 +0000558error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
559 bool &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000560 if (is64BitLoadCommand(MachOObj, DRI)) {
561 InMemoryStruct<macho::Section64> Sect;
562 getSection64(DRI, Sect);
563 Result = !strcmp(Sect->Name, "__text");
564 } else {
565 InMemoryStruct<macho::Section> Sect;
566 getSection(DRI, Sect);
567 Result = !strcmp(Sect->Name, "__text");
568 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000569 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000570}
571
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000572error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
573 bool &Result) const {
574 // FIXME: Unimplemented.
575 Result = false;
576 return object_error::success;
577}
578
579error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
580 bool &Result) const {
581 // FIXME: Unimplemented.
582 Result = false;
583 return object_error::success;
584}
585
Preston Gurdc68dda82012-04-12 20:13:57 +0000586error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
587 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000588 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000589 Result = true;
590 return object_error::success;
591}
592
593error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000594 bool &Result) const {
595 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000596 Result = false;
597 return object_error::success;
598}
599
600error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
601 bool &Result) const {
602 if (MachOObj->is64Bit()) {
603 InMemoryStruct<macho::Section64> Sect;
604 getSection64(DRI, Sect);
Eli Friedman41827f92012-05-02 02:31:28 +0000605 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
606 Result = (SectionType == MachO::SectionTypeZeroFill ||
607 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000608 } else {
609 InMemoryStruct<macho::Section> Sect;
610 getSection(DRI, Sect);
Eli Friedman41827f92012-05-02 02:31:28 +0000611 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
612 Result = (SectionType == MachO::SectionTypeZeroFill ||
613 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000614 }
615
616 return object_error::success;
617}
618
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000619error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
620 bool &Result) const {
621 // Consider using the code from isSectionText to look for __const sections.
622 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
623 // to use section attributes to distinguish code from data.
624
625 // FIXME: Unimplemented.
626 Result = false;
627 return object_error::success;
628}
629
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000630error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
631 DataRefImpl Symb,
632 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000633 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000634 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000635 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000636 Result = false;
637 return object_error::success;
638 }
639
640 uint64_t SectBegin, SectEnd;
641 getSectionAddress(Sec, SectBegin);
642 getSectionSize(Sec, SectEnd);
643 SectEnd += SectBegin;
644
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000645 if (MachOObj->is64Bit()) {
646 InMemoryStruct<macho::Symbol64TableEntry> Entry;
647 getSymbol64TableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000648 uint64_t SymAddr= Entry->Value;
649 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000650 } else {
651 InMemoryStruct<macho::SymbolTableEntry> Entry;
652 getSymbolTableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000653 uint64_t SymAddr= Entry->Value;
654 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000655 }
Owen Andersoncd749882011-10-12 22:21:32 +0000656
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000657 return object_error::success;
658}
659
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000660relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
661 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000662 ret.d.b = getSectionIndex(Sec);
663 return relocation_iterator(RelocationRef(ret, this));
664}
665relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
666 uint32_t last_reloc;
667 if (is64BitLoadCommand(MachOObj, Sec)) {
668 InMemoryStruct<macho::Section64> Sect;
669 getSection64(Sec, Sect);
670 last_reloc = Sect->NumRelocationTableEntries;
671 } else {
672 InMemoryStruct<macho::Section> Sect;
673 getSection(Sec, Sect);
674 last_reloc = Sect->NumRelocationTableEntries;
675 }
676 DataRefImpl ret;
677 ret.d.a = last_reloc;
678 ret.d.b = getSectionIndex(Sec);
679 return relocation_iterator(RelocationRef(ret, this));
680}
681
682section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000683 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000684 moveToNextSection(DRI);
685 return section_iterator(SectionRef(DRI, this));
686}
687
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000688section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000689 DataRefImpl DRI;
690 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000691 return section_iterator(SectionRef(DRI, this));
692}
693
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000694/*===-- Relocations -------------------------------------------------------===*/
695
696void MachOObjectFile::
697getRelocation(DataRefImpl Rel,
698 InMemoryStruct<macho::RelocationEntry> &Res) const {
699 uint32_t relOffset;
700 if (MachOObj->is64Bit()) {
701 InMemoryStruct<macho::Section64> Sect;
702 getSection64(Sections[Rel.d.b], Sect);
703 relOffset = Sect->RelocationTableOffset;
704 } else {
705 InMemoryStruct<macho::Section> Sect;
706 getSection(Sections[Rel.d.b], Sect);
707 relOffset = Sect->RelocationTableOffset;
708 }
709 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
710}
711error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
712 RelocationRef &Res) const {
713 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000714 Res = RelocationRef(Rel, this);
715 return object_error::success;
716}
717error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
718 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000719 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000720 if (MachOObj->is64Bit()) {
721 InMemoryStruct<macho::Section64> Sect;
722 getSection64(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000723 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000724 } else {
725 InMemoryStruct<macho::Section> Sect;
726 getSection(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000727 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000728 }
729 InMemoryStruct<macho::RelocationEntry> RE;
730 getRelocation(Rel, RE);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000731
732 unsigned Arch = getArch();
733 bool isScattered = (Arch != Triple::x86_64) &&
734 (RE->Word0 & macho::RF_Scattered);
735 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000736 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000737 RelAddr = RE->Word0 & 0xFFFFFF;
738 else
739 RelAddr = RE->Word0;
740
741 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000742 return object_error::success;
743}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000744error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
745 uint64_t &Res) const {
746 InMemoryStruct<macho::RelocationEntry> RE;
747 getRelocation(Rel, RE);
748
749 unsigned Arch = getArch();
750 bool isScattered = (Arch != Triple::x86_64) &&
751 (RE->Word0 & macho::RF_Scattered);
752 if (isScattered)
753 Res = RE->Word0 & 0xFFFFFF;
754 else
755 Res = RE->Word0;
756 return object_error::success;
757}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000758error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
759 SymbolRef &Res) const {
760 InMemoryStruct<macho::RelocationEntry> RE;
761 getRelocation(Rel, RE);
762 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
763 bool isExtern = (RE->Word1 >> 27) & 1;
764
765 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000766 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000767 if (isExtern) {
768 for (unsigned i = 0; i < SymbolIdx; i++) {
769 Sym.d.b++;
770 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000771 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000772 "Relocation symbol index out of range!");
773 }
774 }
775 Res = SymbolRef(Sym, this);
776 return object_error::success;
777}
778error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000779 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000780 InMemoryStruct<macho::RelocationEntry> RE;
781 getRelocation(Rel, RE);
Owen Andersonf8261e72011-10-26 17:10:22 +0000782 Res = RE->Word0;
783 Res <<= 32;
784 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000785 return object_error::success;
786}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000787error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
788 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000789 // TODO: Support scattered relocations.
790 StringRef res;
791 InMemoryStruct<macho::RelocationEntry> RE;
792 getRelocation(Rel, RE);
Owen Anderson0135fe12011-10-24 21:44:00 +0000793
794 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000795 bool isScattered = (Arch != Triple::x86_64) &&
796 (RE->Word0 & macho::RF_Scattered);
797
798 unsigned r_type;
799 if (isScattered)
800 r_type = (RE->Word0 >> 24) & 0xF;
801 else
802 r_type = (RE->Word1 >> 28) & 0xF;
803
Owen Anderson0135fe12011-10-24 21:44:00 +0000804 switch (Arch) {
805 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000806 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000807 "GENERIC_RELOC_VANILLA",
808 "GENERIC_RELOC_PAIR",
809 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000810 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000811 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000812 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000813
Owen Andersoneb6bd332011-10-27 20:46:09 +0000814 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000815 res = "Unknown";
816 else
817 res = Table[r_type];
818 break;
819 }
820 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000821 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000822 "X86_64_RELOC_UNSIGNED",
823 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000824 "X86_64_RELOC_BRANCH",
825 "X86_64_RELOC_GOT_LOAD",
826 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000827 "X86_64_RELOC_SUBTRACTOR",
828 "X86_64_RELOC_SIGNED_1",
829 "X86_64_RELOC_SIGNED_2",
830 "X86_64_RELOC_SIGNED_4",
831 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000832
Owen Andersond8fa76d2011-10-24 23:20:07 +0000833 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000834 res = "Unknown";
835 else
836 res = Table[r_type];
837 break;
838 }
839 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000840 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000841 "ARM_RELOC_VANILLA",
842 "ARM_RELOC_PAIR",
843 "ARM_RELOC_SECTDIFF",
844 "ARM_RELOC_LOCAL_SECTDIFF",
845 "ARM_RELOC_PB_LA_PTR",
846 "ARM_RELOC_BR24",
847 "ARM_THUMB_RELOC_BR22",
848 "ARM_THUMB_32BIT_BRANCH",
849 "ARM_RELOC_HALF",
850 "ARM_RELOC_HALF_SECTDIFF" };
851
852 if (r_type > 9)
853 res = "Unknown";
854 else
855 res = Table[r_type];
856 break;
857 }
858 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000859 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000860 "PPC_RELOC_VANILLA",
861 "PPC_RELOC_PAIR",
862 "PPC_RELOC_BR14",
863 "PPC_RELOC_BR24",
864 "PPC_RELOC_HI16",
865 "PPC_RELOC_LO16",
866 "PPC_RELOC_HA16",
867 "PPC_RELOC_LO14",
868 "PPC_RELOC_SECTDIFF",
869 "PPC_RELOC_PB_LA_PTR",
870 "PPC_RELOC_HI16_SECTDIFF",
871 "PPC_RELOC_LO16_SECTDIFF",
872 "PPC_RELOC_HA16_SECTDIFF",
873 "PPC_RELOC_JBSR",
874 "PPC_RELOC_LO14_SECTDIFF",
875 "PPC_RELOC_LOCAL_SECTDIFF" };
876
877 res = Table[r_type];
878 break;
879 }
880 case Triple::UnknownArch:
881 res = "Unknown";
882 break;
883 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000884 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000885 return object_error::success;
886}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000887error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
888 int64_t &Res) const {
889 InMemoryStruct<macho::RelocationEntry> RE;
890 getRelocation(Rel, RE);
891 bool isExtern = (RE->Word1 >> 27) & 1;
892 Res = 0;
893 if (!isExtern) {
894 const uint8_t* sectAddress = base();
895 if (MachOObj->is64Bit()) {
896 InMemoryStruct<macho::Section64> Sect;
897 getSection64(Sections[Rel.d.b], Sect);
898 sectAddress += Sect->Offset;
899 } else {
900 InMemoryStruct<macho::Section> Sect;
901 getSection(Sections[Rel.d.b], Sect);
902 sectAddress += Sect->Offset;
903 }
904 Res = reinterpret_cast<uintptr_t>(sectAddress);
905 }
906 return object_error::success;
907}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000908
909// Helper to advance a section or symbol iterator multiple increments at a time.
910template<class T>
911error_code advance(T &it, size_t Val) {
912 error_code ec;
913 while (Val--) {
914 it.increment(ec);
915 }
916 return ec;
917}
918
919template<class T>
920void advanceTo(T &it, size_t Val) {
921 if (error_code ec = advance(it, Val))
922 report_fatal_error(ec.message());
923}
924
Owen Anderson1832f4d2011-10-26 20:42:54 +0000925void MachOObjectFile::printRelocationTargetName(
926 InMemoryStruct<macho::RelocationEntry>& RE,
927 raw_string_ostream &fmt) const {
928 unsigned Arch = getArch();
929 bool isScattered = (Arch != Triple::x86_64) &&
930 (RE->Word0 & macho::RF_Scattered);
931
932 // Target of a scattered relocation is an address. In the interest of
933 // generating pretty output, scan through the symbol table looking for a
934 // symbol that aligns with that address. If we find one, print it.
935 // Otherwise, we just print the hex address of the target.
936 if (isScattered) {
937 uint32_t Val = RE->Word1;
938
939 error_code ec;
940 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
941 SI.increment(ec)) {
942 if (ec) report_fatal_error(ec.message());
943
944 uint64_t Addr;
945 StringRef Name;
946
947 if ((ec = SI->getAddress(Addr)))
948 report_fatal_error(ec.message());
949 if (Addr != Val) continue;
950 if ((ec = SI->getName(Name)))
951 report_fatal_error(ec.message());
952 fmt << Name;
953 return;
954 }
955
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000956 // If we couldn't find a symbol that this relocation refers to, try
957 // to find a section beginning instead.
958 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
959 SI.increment(ec)) {
960 if (ec) report_fatal_error(ec.message());
961
962 uint64_t Addr;
963 StringRef Name;
964
965 if ((ec = SI->getAddress(Addr)))
966 report_fatal_error(ec.message());
967 if (Addr != Val) continue;
968 if ((ec = SI->getName(Name)))
969 report_fatal_error(ec.message());
970 fmt << Name;
971 return;
972 }
973
Owen Anderson1832f4d2011-10-26 20:42:54 +0000974 fmt << format("0x%x", Val);
975 return;
976 }
977
978 StringRef S;
979 bool isExtern = (RE->Word1 >> 27) & 1;
980 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000981
982 if (isExtern) {
983 symbol_iterator SI = begin_symbols();
984 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000985 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000986 } else {
987 section_iterator SI = begin_sections();
988 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000989 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +0000990 }
991
Owen Anderson1832f4d2011-10-26 20:42:54 +0000992 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000993}
994
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000995error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
996 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000997 InMemoryStruct<macho::RelocationEntry> RE;
998 getRelocation(Rel, RE);
999
Owen Anderson1832f4d2011-10-26 20:42:54 +00001000 unsigned Arch = getArch();
1001 bool isScattered = (Arch != Triple::x86_64) &&
1002 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001003
Owen Anderson013d7562011-10-25 18:48:41 +00001004 std::string fmtbuf;
1005 raw_string_ostream fmt(fmtbuf);
1006
Owen Anderson1832f4d2011-10-26 20:42:54 +00001007 unsigned Type;
1008 if (isScattered)
1009 Type = (RE->Word0 >> 24) & 0xF;
1010 else
1011 Type = (RE->Word1 >> 28) & 0xF;
1012
Owen Andersoneb6bd332011-10-27 20:46:09 +00001013 bool isPCRel;
1014 if (isScattered)
1015 isPCRel = ((RE->Word0 >> 30) & 1);
1016 else
1017 isPCRel = ((RE->Word1 >> 24) & 1);
1018
Owen Andersond8fa76d2011-10-24 23:20:07 +00001019 // Determine any addends that should be displayed with the relocation.
1020 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001021
1022 // X86_64 has entirely custom relocation types.
1023 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001024 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001025
Owen Andersond8fa76d2011-10-24 23:20:07 +00001026 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001027 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1028 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1029 printRelocationTargetName(RE, fmt);
1030 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001031 if (isPCRel) fmt << "PCREL";
1032 break;
1033 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001034 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001035 InMemoryStruct<macho::RelocationEntry> RENext;
1036 DataRefImpl RelNext = Rel;
1037 RelNext.d.a++;
1038 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001039
1040 // X86_64_SUBTRACTOR must be followed by a relocation of type
1041 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001042 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001043 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001044 if (RType != 0)
1045 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1046 "X86_64_RELOC_SUBTRACTOR.");
1047
Owen Andersonef22f782011-10-26 17:28:49 +00001048 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1049 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001050 printRelocationTargetName(RENext, fmt);
1051 fmt << "-";
1052 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001053 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001054 case macho::RIT_X86_64_TLV:
1055 printRelocationTargetName(RE, fmt);
1056 fmt << "@TLV";
1057 if (isPCRel) fmt << "P";
1058 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001059 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1060 printRelocationTargetName(RE, fmt);
1061 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001062 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001063 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1064 printRelocationTargetName(RE, fmt);
1065 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001066 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001067 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1068 printRelocationTargetName(RE, fmt);
1069 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001070 break;
1071 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001072 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001073 break;
1074 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001075 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001076 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1077 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001078 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001079 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001080 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001081 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001082 InMemoryStruct<macho::RelocationEntry> RENext;
1083 DataRefImpl RelNext = Rel;
1084 RelNext.d.a++;
1085 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001086
1087 // X86 sect diff's must be followed by a relocation of type
1088 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001089 bool isNextScattered = (Arch != Triple::x86_64) &&
1090 (RENext->Word0 & macho::RF_Scattered);
1091 unsigned RType;
1092 if (isNextScattered)
1093 RType = (RENext->Word0 >> 24) & 0xF;
1094 else
1095 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001096 if (RType != 1)
1097 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001098 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001099
Owen Anderson1832f4d2011-10-26 20:42:54 +00001100 printRelocationTargetName(RE, fmt);
1101 fmt << "-";
1102 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001103 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001104 }
1105 }
Owen Anderson013d7562011-10-25 18:48:41 +00001106
Owen Anderson1832f4d2011-10-26 20:42:54 +00001107 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001108 // All X86 relocations that need special printing were already
1109 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001110 switch (Type) {
1111 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1112 InMemoryStruct<macho::RelocationEntry> RENext;
1113 DataRefImpl RelNext = Rel;
1114 RelNext.d.a++;
1115 getRelocation(RelNext, RENext);
1116
1117 // X86 sect diff's must be followed by a relocation of type
1118 // GENERIC_RELOC_PAIR.
1119 bool isNextScattered = (Arch != Triple::x86_64) &&
1120 (RENext->Word0 & macho::RF_Scattered);
1121 unsigned RType;
1122 if (isNextScattered)
1123 RType = (RENext->Word0 >> 24) & 0xF;
1124 else
1125 RType = (RENext->Word1 >> 28) & 0xF;
1126 if (RType != 1)
1127 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1128 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1129
1130 printRelocationTargetName(RE, fmt);
1131 fmt << "-";
1132 printRelocationTargetName(RENext, fmt);
1133 break;
1134 }
1135 case macho::RIT_Generic_TLV: {
1136 printRelocationTargetName(RE, fmt);
1137 fmt << "@TLV";
1138 if (isPCRel) fmt << "P";
1139 break;
1140 }
1141 default:
1142 printRelocationTargetName(RE, fmt);
1143 }
Owen Anderson013d7562011-10-25 18:48:41 +00001144 } else { // ARM-specific relocations
1145 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001146 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1147 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001148 // Half relocations steal a bit from the length field to encode
1149 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001150 bool isUpper;
1151 if (isScattered)
1152 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001153 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001154 isUpper = (RE->Word1 >> 25) & 1;
1155
1156 if (isUpper)
1157 fmt << ":upper16:(";
1158 else
1159 fmt << ":lower16:(";
1160 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001161
1162 InMemoryStruct<macho::RelocationEntry> RENext;
1163 DataRefImpl RelNext = Rel;
1164 RelNext.d.a++;
1165 getRelocation(RelNext, RENext);
1166
1167 // ARM half relocs must be followed by a relocation of type
1168 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001169 bool isNextScattered = (Arch != Triple::x86_64) &&
1170 (RENext->Word0 & macho::RF_Scattered);
1171 unsigned RType;
1172 if (isNextScattered)
1173 RType = (RENext->Word0 >> 24) & 0xF;
1174 else
1175 RType = (RENext->Word1 >> 28) & 0xF;
1176
Owen Anderson013d7562011-10-25 18:48:41 +00001177 if (RType != 1)
1178 report_fatal_error("Expected ARM_RELOC_PAIR after "
1179 "GENERIC_RELOC_HALF");
1180
Owen Anderson1832f4d2011-10-26 20:42:54 +00001181 // NOTE: The half of the target virtual address is stashed in the
1182 // address field of the secondary relocation, but we can't reverse
1183 // engineer the constant offset from it without decoding the movw/movt
1184 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001185
1186 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1187 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001188 if (Type == macho::RIT_ARM_HalfDifference) {
1189 fmt << "-";
1190 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001191 }
1192
Owen Anderson013d7562011-10-25 18:48:41 +00001193 fmt << ")";
1194 break;
1195 }
1196 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001197 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001198 }
1199 }
1200 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001201 } else
1202 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001203
Owen Anderson0135fe12011-10-24 21:44:00 +00001204 fmt.flush();
1205 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001206 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001207}
1208
Owen Anderson0685e942011-10-25 20:35:53 +00001209error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1210 bool &Result) const {
1211 InMemoryStruct<macho::RelocationEntry> RE;
1212 getRelocation(Rel, RE);
1213
Owen Anderson0685e942011-10-25 20:35:53 +00001214 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001215 bool isScattered = (Arch != Triple::x86_64) &&
1216 (RE->Word0 & macho::RF_Scattered);
1217 unsigned Type;
1218 if (isScattered)
1219 Type = (RE->Word0 >> 24) & 0xF;
1220 else
1221 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001222
1223 Result = false;
1224
1225 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1226 // is always hidden.
1227 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001228 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001229 } else if (Arch == Triple::x86_64) {
1230 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1231 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001232 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001233 DataRefImpl RelPrev = Rel;
1234 RelPrev.d.a--;
1235 InMemoryStruct<macho::RelocationEntry> REPrev;
1236 getRelocation(RelPrev, REPrev);
1237
1238 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1239
Owen Anderson1832f4d2011-10-26 20:42:54 +00001240 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001241 }
1242 }
1243
1244 return object_error::success;
1245}
1246
David Meyer5c2b4ea2012-03-01 01:36:50 +00001247error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1248 LibraryRef &Res) const {
1249 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1250}
1251
1252error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1253 StringRef &Res) const {
1254 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1255}
1256
1257
Eric Christopher6256b032011-04-22 03:19:48 +00001258/*===-- Miscellaneous -----------------------------------------------------===*/
1259
1260uint8_t MachOObjectFile::getBytesInAddress() const {
1261 return MachOObj->is64Bit() ? 8 : 4;
1262}
1263
1264StringRef MachOObjectFile::getFileFormatName() const {
1265 if (!MachOObj->is64Bit()) {
1266 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001267 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001268 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001269 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001270 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001271 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001272 return "Mach-O 32-bit ppc";
1273 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001274 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001275 "64-bit object file when we're not 64-bit?");
1276 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001277 }
1278 }
1279
1280 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001281 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001282 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001283 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001284 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001285 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001286 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001287 "32-bit object file when we're 64-bit?");
1288 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001289 }
1290}
1291
1292unsigned MachOObjectFile::getArch() const {
1293 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001294 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001295 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001296 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001297 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001298 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001299 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001300 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001301 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001302 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001303 return Triple::ppc64;
1304 default:
1305 return Triple::UnknownArch;
1306 }
1307}
1308
Owen Andersonf7c93a32011-10-11 17:32:27 +00001309} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001310} // end namespace llvm