blob: 33e0d2643bbcca6817e394a3f4202bd7351a2c63 [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"
16#include "llvm/Object/MachOFormat.h"
17#include "llvm/Object/MachOObject.h"
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Support/MemoryBuffer.h"
Eric Christopherf4b2f932011-04-22 06:34:01 +000020#include "llvm/Support/MachO.h"
Benjamin Kramer0fcab072011-09-08 20:52:17 +000021#include "llvm/ADT/SmallVector.h"
Eric Christopher6256b032011-04-22 03:19:48 +000022
23#include <cctype>
24#include <cstring>
25#include <limits>
26
27using namespace llvm;
28using namespace object;
29
30namespace llvm {
31
32typedef MachOObject::LoadCommandInfo LoadCommandInfo;
33
34class MachOObjectFile : public ObjectFile {
35public:
Benjamin Kramer0fcab072011-09-08 20:52:17 +000036 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
Eric Christopher6256b032011-04-22 03:19:48 +000037
38 virtual symbol_iterator begin_symbols() const;
39 virtual symbol_iterator end_symbols() const;
40 virtual section_iterator begin_sections() const;
41 virtual section_iterator end_sections() const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000042 virtual relocation_iterator begin_relocations() const;
43 virtual relocation_iterator end_relocations() const;
Eric Christopher6256b032011-04-22 03:19:48 +000044
45 virtual uint8_t getBytesInAddress() const;
46 virtual StringRef getFileFormatName() const;
47 virtual unsigned getArch() const;
48
49protected:
Michael J. Spencer25b15772011-06-25 17:55:23 +000050 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
51 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +000052 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +000053 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
54 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
55 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
56 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +000057 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
58 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000059
Michael J. Spencer25b15772011-06-25 17:55:23 +000060 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
61 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
62 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
63 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
64 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
65 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +000066 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
67 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +000068 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
69 bool &Result) const;
Eric Christopher6256b032011-04-22 03:19:48 +000070
Benjamin Kramer0fcab072011-09-08 20:52:17 +000071 virtual error_code getRelocationNext(DataRefImpl Rel,
72 RelocationRef &Res) const;
73 virtual error_code getRelocationAddress(DataRefImpl Rel,
74 uint64_t &Res) const;
75 virtual error_code getRelocationSymbol(DataRefImpl Rel,
76 SymbolRef &Res) const;
77 virtual error_code getRelocationType(DataRefImpl Rel,
78 uint32_t &Res) const;
79 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
80 int64_t &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000081private:
82 MachOObject *MachOObj;
83 mutable uint32_t RegisteredStringTable;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000084 typedef SmallVector<DataRefImpl, 1> SectionList;
85 SectionList Sections;
86
Eric Christopher6256b032011-04-22 03:19:48 +000087
88 void moveToNextSection(DataRefImpl &DRI) const;
89 void getSymbolTableEntry(DataRefImpl DRI,
90 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +000091 void getSymbol64TableEntry(DataRefImpl DRI,
92 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000093 void moveToNextSymbol(DataRefImpl &DRI) const;
94 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
Benjamin Kramer7d145782011-07-15 00:14:48 +000095 void getSection64(DataRefImpl DRI,
96 InMemoryStruct<macho::Section64> &Res) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000097 void getRelocation(DataRefImpl Rel,
98 InMemoryStruct<macho::RelocationEntry> &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000099};
100
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000101MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
102 error_code &ec)
103 : ObjectFile(Binary::isMachO, Object, ec),
104 MachOObj(MOO),
105 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
106 DataRefImpl DRI;
107 DRI.d.a = DRI.d.b = 0;
108 moveToNextSection(DRI);
109 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
110 while (DRI.d.a < LoadCommandCount) {
111 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000112 DRI.d.b++;
113 moveToNextSection(DRI);
114 }
115}
116
117
Eric Christopher6256b032011-04-22 03:19:48 +0000118ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000119 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +0000120 std::string Err;
121 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
122 if (!MachOObj)
123 return NULL;
Michael J. Spencer001c9202011-06-25 17:54:50 +0000124 return new MachOObjectFile(Buffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +0000125}
126
127/*===-- Symbols -----------------------------------------------------------===*/
128
129void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
130 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
131 while (DRI.d.a < LoadCommandCount) {
132 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
133 if (LCI.Command.Type == macho::LCT_Symtab) {
134 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
135 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
136 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
137 return;
138 }
139
140 DRI.d.a++;
141 DRI.d.b = 0;
142 }
143}
144
145void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
146 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
147 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
148 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
149 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
150
151 if (RegisteredStringTable != DRI.d.a) {
152 MachOObj->RegisterStringTable(*SymtabLoadCmd);
153 RegisteredStringTable = DRI.d.a;
154 }
155
156 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
157 Res);
158}
159
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000160void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
161 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
162 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
163 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
164 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
165
166 if (RegisteredStringTable != DRI.d.a) {
167 MachOObj->RegisterStringTable(*SymtabLoadCmd);
168 RegisteredStringTable = DRI.d.a;
169 }
170
171 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
172 Res);
173}
174
Eric Christopher6256b032011-04-22 03:19:48 +0000175
Michael J. Spencer25b15772011-06-25 17:55:23 +0000176error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
177 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000178 DRI.d.b++;
179 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000180 Result = SymbolRef(DRI, this);
181 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000182}
183
Michael J. Spencer25b15772011-06-25 17:55:23 +0000184error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
185 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000186 if (MachOObj->is64Bit()) {
187 InMemoryStruct<macho::Symbol64TableEntry> Entry;
188 getSymbol64TableEntry(DRI, Entry);
189 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
190 } else {
191 InMemoryStruct<macho::SymbolTableEntry> Entry;
192 getSymbolTableEntry(DRI, Entry);
193 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
194 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000195 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000196}
197
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000198error_code MachOObjectFile::getSymbolOffset(DataRefImpl DRI,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000199 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000200 if (MachOObj->is64Bit()) {
201 InMemoryStruct<macho::Symbol64TableEntry> Entry;
202 getSymbol64TableEntry(DRI, Entry);
203 Result = Entry->Value;
204 } else {
205 InMemoryStruct<macho::SymbolTableEntry> Entry;
206 getSymbolTableEntry(DRI, Entry);
207 Result = Entry->Value;
208 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000209 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000210}
211
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000212error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
213 uint64_t &Result) const {
214 uint64_t SymbolOffset;
215 uint8_t SectionIndex;
216 if (MachOObj->is64Bit()) {
217 InMemoryStruct<macho::Symbol64TableEntry> Entry;
218 getSymbol64TableEntry(DRI, Entry);
219 SymbolOffset = Entry->Value;
220 SectionIndex = Entry->SectionIndex;
221 } else {
222 InMemoryStruct<macho::SymbolTableEntry> Entry;
223 getSymbolTableEntry(DRI, Entry);
224 SymbolOffset = Entry->Value;
225 SectionIndex = Entry->SectionIndex;
226 }
227 getSectionAddress(Sections[SectionIndex], Result);
228 Result += SymbolOffset;
229
230 return object_error::success;
231}
232
Michael J. Spencer25b15772011-06-25 17:55:23 +0000233error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
234 uint64_t &Result) const {
235 Result = UnknownAddressOrSize;
236 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
Michael J. Spencer25b15772011-06-25 17:55:23 +0000274error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
275 bool &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000276 if (MachOObj->is64Bit()) {
277 InMemoryStruct<macho::Symbol64TableEntry> Entry;
278 getSymbol64TableEntry(DRI, Entry);
279 Result = Entry->Flags & macho::STF_StabsEntryMask;
280 } else {
281 InMemoryStruct<macho::SymbolTableEntry> Entry;
282 getSymbolTableEntry(DRI, Entry);
283 Result = Entry->Flags & macho::STF_StabsEntryMask;
284 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000285 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000286}
287
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000288error_code MachOObjectFile::isSymbolGlobal(DataRefImpl Symb, bool &Res) const {
289
290 if (MachOObj->is64Bit()) {
291 InMemoryStruct<macho::Symbol64TableEntry> Entry;
292 getSymbol64TableEntry(Symb, Entry);
293 Res = Entry->Type & MachO::NlistMaskExternal;
294 } else {
295 InMemoryStruct<macho::SymbolTableEntry> Entry;
296 getSymbolTableEntry(Symb, Entry);
297 Res = Entry->Type & MachO::NlistMaskExternal;
298 }
299 return object_error::success;
300}
301
302error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
303 SymbolRef::SymbolType &Res) const {
304 uint8_t n_type;
305 if (MachOObj->is64Bit()) {
306 InMemoryStruct<macho::Symbol64TableEntry> Entry;
307 getSymbol64TableEntry(Symb, Entry);
308 n_type = Entry->Type;
309 } else {
310 InMemoryStruct<macho::SymbolTableEntry> Entry;
311 getSymbolTableEntry(Symb, Entry);
312 n_type = Entry->Type;
313 }
314 Res = SymbolRef::ST_Other;
315 switch (n_type & MachO::NlistMaskType) {
316 case MachO::NListTypeUndefined :
317 Res = SymbolRef::ST_External;
318 break;
319 case MachO::NListTypeSection :
320 Res = SymbolRef::ST_Function;
321 break;
322 }
323 return object_error::success;
324}
325
326
Eric Christopher6256b032011-04-22 03:19:48 +0000327ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
328 // DRI.d.a = segment number; DRI.d.b = symbol index.
329 DataRefImpl DRI;
330 DRI.d.a = DRI.d.b = 0;
331 moveToNextSymbol(DRI);
332 return symbol_iterator(SymbolRef(DRI, this));
333}
334
335ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
336 DataRefImpl DRI;
337 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
338 DRI.d.b = 0;
339 return symbol_iterator(SymbolRef(DRI, this));
340}
341
342
343/*===-- Sections ----------------------------------------------------------===*/
344
345void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
346 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
347 while (DRI.d.a < LoadCommandCount) {
348 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
349 if (LCI.Command.Type == macho::LCT_Segment) {
350 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
351 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
352 if (DRI.d.b < SegmentLoadCmd->NumSections)
353 return;
354 } else if (LCI.Command.Type == macho::LCT_Segment64) {
355 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
356 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
357 if (DRI.d.b < Segment64LoadCmd->NumSections)
358 return;
359 }
360
361 DRI.d.a++;
362 DRI.d.b = 0;
363 }
364}
365
Michael J. Spencer25b15772011-06-25 17:55:23 +0000366error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
367 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000368 DRI.d.b++;
369 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000370 Result = SectionRef(DRI, this);
371 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000372}
373
374void
375MachOObjectFile::getSection(DataRefImpl DRI,
376 InMemoryStruct<macho::Section> &Res) const {
377 InMemoryStruct<macho::SegmentLoadCommand> SLC;
378 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
379 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
380 MachOObj->ReadSection(LCI, DRI.d.b, Res);
381}
382
Benjamin Kramer7d145782011-07-15 00:14:48 +0000383void
384MachOObjectFile::getSection64(DataRefImpl DRI,
385 InMemoryStruct<macho::Section64> &Res) const {
386 InMemoryStruct<macho::Segment64LoadCommand> SLC;
387 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
388 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
389 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
390}
391
392static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
393 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
394 if (LCI.Command.Type == macho::LCT_Segment64)
395 return true;
396 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
397 return false;
398}
399
Michael J. Spencer25b15772011-06-25 17:55:23 +0000400error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
401 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000402 // FIXME: thread safety.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000403 static char result[34];
Benjamin Kramer7d145782011-07-15 00:14:48 +0000404 if (is64BitLoadCommand(MachOObj, DRI)) {
405 InMemoryStruct<macho::Segment64LoadCommand> SLC;
406 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
407 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
408 InMemoryStruct<macho::Section64> Sect;
409 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
410
Benjamin Kramer291e7672011-07-15 00:29:02 +0000411 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000412 strcat(result, ",");
413 strcat(result, Sect->Name);
414 } else {
415 InMemoryStruct<macho::SegmentLoadCommand> SLC;
416 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
417 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
418 InMemoryStruct<macho::Section> Sect;
419 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
420
Benjamin Kramer291e7672011-07-15 00:29:02 +0000421 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000422 strcat(result, ",");
423 strcat(result, Sect->Name);
424 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000425 Result = StringRef(result);
426 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000427}
428
Michael J. Spencer25b15772011-06-25 17:55:23 +0000429error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
430 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000431 if (is64BitLoadCommand(MachOObj, DRI)) {
432 InMemoryStruct<macho::Section64> Sect;
433 getSection64(DRI, Sect);
434 Result = Sect->Address;
435 } else {
436 InMemoryStruct<macho::Section> Sect;
437 getSection(DRI, Sect);
438 Result = Sect->Address;
439 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000440 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000441}
442
Michael J. Spencer25b15772011-06-25 17:55:23 +0000443error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
444 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000445 if (is64BitLoadCommand(MachOObj, DRI)) {
446 InMemoryStruct<macho::Section64> Sect;
447 getSection64(DRI, Sect);
448 Result = Sect->Size;
449 } else {
450 InMemoryStruct<macho::Section> Sect;
451 getSection(DRI, Sect);
452 Result = Sect->Size;
453 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000454 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000455}
456
Michael J. Spencer25b15772011-06-25 17:55:23 +0000457error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
458 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000459 if (is64BitLoadCommand(MachOObj, DRI)) {
460 InMemoryStruct<macho::Section64> Sect;
461 getSection64(DRI, Sect);
462 Result = MachOObj->getData(Sect->Offset, Sect->Size);
463 } else {
464 InMemoryStruct<macho::Section> Sect;
465 getSection(DRI, Sect);
466 Result = MachOObj->getData(Sect->Offset, Sect->Size);
467 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000468 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000469}
470
Michael J. Spencer25b15772011-06-25 17:55:23 +0000471error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
472 bool &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000473 if (is64BitLoadCommand(MachOObj, DRI)) {
474 InMemoryStruct<macho::Section64> Sect;
475 getSection64(DRI, Sect);
476 Result = !strcmp(Sect->Name, "__text");
477 } else {
478 InMemoryStruct<macho::Section> Sect;
479 getSection(DRI, Sect);
480 Result = !strcmp(Sect->Name, "__text");
481 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000482 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000483}
484
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000485error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
486 bool &Result) const {
487 // FIXME: Unimplemented.
488 Result = false;
489 return object_error::success;
490}
491
492error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
493 bool &Result) const {
494 // FIXME: Unimplemented.
495 Result = false;
496 return object_error::success;
497}
498
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000499error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
500 DataRefImpl Symb,
501 bool &Result) const {
502 if (MachOObj->is64Bit()) {
503 InMemoryStruct<macho::Symbol64TableEntry> Entry;
504 getSymbol64TableEntry(Symb, Entry);
505 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
506 } else {
507 InMemoryStruct<macho::SymbolTableEntry> Entry;
508 getSymbolTableEntry(Symb, Entry);
509 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
510 }
511 return object_error::success;
512}
513
Eric Christopher6256b032011-04-22 03:19:48 +0000514ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
515 DataRefImpl DRI;
516 DRI.d.a = DRI.d.b = 0;
517 moveToNextSection(DRI);
518 return section_iterator(SectionRef(DRI, this));
519}
520
521ObjectFile::section_iterator MachOObjectFile::end_sections() const {
522 DataRefImpl DRI;
523 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
524 DRI.d.b = 0;
525 return section_iterator(SectionRef(DRI, this));
526}
527
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000528/*===-- Relocations -------------------------------------------------------===*/
529
530void MachOObjectFile::
531getRelocation(DataRefImpl Rel,
532 InMemoryStruct<macho::RelocationEntry> &Res) const {
533 uint32_t relOffset;
534 if (MachOObj->is64Bit()) {
535 InMemoryStruct<macho::Section64> Sect;
536 getSection64(Sections[Rel.d.b], Sect);
537 relOffset = Sect->RelocationTableOffset;
538 } else {
539 InMemoryStruct<macho::Section> Sect;
540 getSection(Sections[Rel.d.b], Sect);
541 relOffset = Sect->RelocationTableOffset;
542 }
543 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
544}
545error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
546 RelocationRef &Res) const {
547 ++Rel.d.a;
548 while (Rel.d.b < Sections.size()) {
549 unsigned relocationCount;
550 if (MachOObj->is64Bit()) {
551 InMemoryStruct<macho::Section64> Sect;
552 getSection64(Sections[Rel.d.b], Sect);
553 relocationCount = Sect->NumRelocationTableEntries;
554 } else {
555 InMemoryStruct<macho::Section> Sect;
556 getSection(Sections[Rel.d.b], Sect);
557 relocationCount = Sect->NumRelocationTableEntries;
558 }
559 if (Rel.d.a < relocationCount)
560 break;
561
562 Rel.d.a = 0;
563 ++Rel.d.b;
564 }
565 Res = RelocationRef(Rel, this);
566 return object_error::success;
567}
568error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
569 uint64_t &Res) const {
570 const uint8_t* sectAddress = base();
571 if (MachOObj->is64Bit()) {
572 InMemoryStruct<macho::Section64> Sect;
573 getSection64(Sections[Rel.d.b], Sect);
574 sectAddress += Sect->Offset;
575 } else {
576 InMemoryStruct<macho::Section> Sect;
577 getSection(Sections[Rel.d.b], Sect);
578 sectAddress += Sect->Offset;
579 }
580 InMemoryStruct<macho::RelocationEntry> RE;
581 getRelocation(Rel, RE);
582 Res = reinterpret_cast<uintptr_t>(sectAddress + RE->Word0);
583 return object_error::success;
584}
585error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
586 SymbolRef &Res) const {
587 InMemoryStruct<macho::RelocationEntry> RE;
588 getRelocation(Rel, RE);
589 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
590 bool isExtern = (RE->Word1 >> 27) & 1;
591
592 DataRefImpl Sym;
593 Sym.d.a = Sym.d.b = 0;
594 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000595 if (isExtern) {
596 for (unsigned i = 0; i < SymbolIdx; i++) {
597 Sym.d.b++;
598 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000599 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000600 "Relocation symbol index out of range!");
601 }
602 }
603 Res = SymbolRef(Sym, this);
604 return object_error::success;
605}
606error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
607 uint32_t &Res) const {
608 InMemoryStruct<macho::RelocationEntry> RE;
609 getRelocation(Rel, RE);
610 Res = RE->Word1;
611 return object_error::success;
612}
613error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
614 int64_t &Res) const {
615 InMemoryStruct<macho::RelocationEntry> RE;
616 getRelocation(Rel, RE);
617 bool isExtern = (RE->Word1 >> 27) & 1;
618 Res = 0;
619 if (!isExtern) {
620 const uint8_t* sectAddress = base();
621 if (MachOObj->is64Bit()) {
622 InMemoryStruct<macho::Section64> Sect;
623 getSection64(Sections[Rel.d.b], Sect);
624 sectAddress += Sect->Offset;
625 } else {
626 InMemoryStruct<macho::Section> Sect;
627 getSection(Sections[Rel.d.b], Sect);
628 sectAddress += Sect->Offset;
629 }
630 Res = reinterpret_cast<uintptr_t>(sectAddress);
631 }
632 return object_error::success;
633}
634ObjectFile::relocation_iterator MachOObjectFile::begin_relocations() const {
635 DataRefImpl ret;
636 ret.d.a = ret.d.b = 0;
637 return relocation_iterator(RelocationRef(ret, this));
638}
639ObjectFile::relocation_iterator MachOObjectFile::end_relocations() const {
640 DataRefImpl ret;
641 ret.d.a = 0;
642 ret.d.b = Sections.size();
643 return relocation_iterator(RelocationRef(ret, this));
644}
645
Eric Christopher6256b032011-04-22 03:19:48 +0000646/*===-- Miscellaneous -----------------------------------------------------===*/
647
648uint8_t MachOObjectFile::getBytesInAddress() const {
649 return MachOObj->is64Bit() ? 8 : 4;
650}
651
652StringRef MachOObjectFile::getFileFormatName() const {
653 if (!MachOObj->is64Bit()) {
654 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000655 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000656 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000657 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000658 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000659 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000660 return "Mach-O 32-bit ppc";
661 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000662 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000663 "64-bit object file when we're not 64-bit?");
664 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000665 }
666 }
667
668 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000669 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000670 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000671 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000672 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +0000673 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000674 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000675 "32-bit object file when we're 64-bit?");
676 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000677 }
678}
679
680unsigned MachOObjectFile::getArch() const {
681 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000682 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +0000683 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000684 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +0000685 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000686 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +0000687 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000688 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +0000689 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000690 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +0000691 return Triple::ppc64;
692 default:
693 return Triple::UnknownArch;
694 }
695}
696
697} // end namespace llvm