blob: 0359a8855b327308460da8a4de5e915f46433b23 [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;
42
43 virtual uint8_t getBytesInAddress() const;
44 virtual StringRef getFileFormatName() const;
45 virtual unsigned getArch() const;
46
47protected:
Michael J. Spencer25b15772011-06-25 17:55:23 +000048 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
49 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +000050 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +000051 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
52 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
53 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
54 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +000055 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
56 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000057
Michael J. Spencer25b15772011-06-25 17:55:23 +000058 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
59 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
60 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
61 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
62 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
Michael J. Spencere2f2f072011-10-10 21:55:43 +000063 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +000064 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +000065 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
66 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +000067 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
68 bool &Result) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000069 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
70 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
Eric Christopher6256b032011-04-22 03:19:48 +000071
Benjamin Kramer0fcab072011-09-08 20:52:17 +000072 virtual error_code getRelocationNext(DataRefImpl Rel,
73 RelocationRef &Res) const;
74 virtual error_code getRelocationAddress(DataRefImpl Rel,
75 uint64_t &Res) const;
76 virtual error_code getRelocationSymbol(DataRefImpl Rel,
77 SymbolRef &Res) const;
78 virtual error_code getRelocationType(DataRefImpl Rel,
79 uint32_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000080 virtual error_code getRelocationTypeName(DataRefImpl Rel,
81 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000082 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
83 int64_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000084 virtual error_code getRelocationValueString(DataRefImpl Rel,
85 SmallVectorImpl<char> &Result) const;
86
Eric Christopher6256b032011-04-22 03:19:48 +000087private:
88 MachOObject *MachOObj;
89 mutable uint32_t RegisteredStringTable;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000090 typedef SmallVector<DataRefImpl, 1> SectionList;
91 SectionList Sections;
92
Eric Christopher6256b032011-04-22 03:19:48 +000093
94 void moveToNextSection(DataRefImpl &DRI) const;
95 void getSymbolTableEntry(DataRefImpl DRI,
96 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +000097 void getSymbol64TableEntry(DataRefImpl DRI,
98 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000099 void moveToNextSymbol(DataRefImpl &DRI) const;
100 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000101 void getSection64(DataRefImpl DRI,
102 InMemoryStruct<macho::Section64> &Res) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000103 void getRelocation(DataRefImpl Rel,
104 InMemoryStruct<macho::RelocationEntry> &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000105 std::size_t getSectionIndex(DataRefImpl Sec) const;
Eric Christopher6256b032011-04-22 03:19:48 +0000106};
107
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000108MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
109 error_code &ec)
110 : ObjectFile(Binary::isMachO, Object, ec),
111 MachOObj(MOO),
112 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
113 DataRefImpl DRI;
114 DRI.d.a = DRI.d.b = 0;
115 moveToNextSection(DRI);
116 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
117 while (DRI.d.a < LoadCommandCount) {
118 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000119 DRI.d.b++;
120 moveToNextSection(DRI);
121 }
122}
123
124
Eric Christopher6256b032011-04-22 03:19:48 +0000125ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000126 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +0000127 std::string Err;
128 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
129 if (!MachOObj)
130 return NULL;
Michael J. Spencer001c9202011-06-25 17:54:50 +0000131 return new MachOObjectFile(Buffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +0000132}
133
134/*===-- Symbols -----------------------------------------------------------===*/
135
136void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
137 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
138 while (DRI.d.a < LoadCommandCount) {
139 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
140 if (LCI.Command.Type == macho::LCT_Symtab) {
141 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
142 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
143 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
144 return;
145 }
146
147 DRI.d.a++;
148 DRI.d.b = 0;
149 }
150}
151
152void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
153 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
154 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
155 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
156 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
157
158 if (RegisteredStringTable != DRI.d.a) {
159 MachOObj->RegisterStringTable(*SymtabLoadCmd);
160 RegisteredStringTable = DRI.d.a;
161 }
162
163 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
164 Res);
165}
166
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000167void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
168 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
169 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
170 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
171 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
172
173 if (RegisteredStringTable != DRI.d.a) {
174 MachOObj->RegisterStringTable(*SymtabLoadCmd);
175 RegisteredStringTable = DRI.d.a;
176 }
177
178 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
179 Res);
180}
181
Eric Christopher6256b032011-04-22 03:19:48 +0000182
Michael J. Spencer25b15772011-06-25 17:55:23 +0000183error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
184 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000185 DRI.d.b++;
186 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000187 Result = SymbolRef(DRI, this);
188 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000189}
190
Michael J. Spencer25b15772011-06-25 17:55:23 +0000191error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
192 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000193 if (MachOObj->is64Bit()) {
194 InMemoryStruct<macho::Symbol64TableEntry> Entry;
195 getSymbol64TableEntry(DRI, Entry);
196 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
197 } else {
198 InMemoryStruct<macho::SymbolTableEntry> Entry;
199 getSymbolTableEntry(DRI, Entry);
200 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
201 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000202 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000203}
204
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000205error_code MachOObjectFile::getSymbolOffset(DataRefImpl DRI,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000206 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000207 if (MachOObj->is64Bit()) {
208 InMemoryStruct<macho::Symbol64TableEntry> Entry;
209 getSymbol64TableEntry(DRI, Entry);
210 Result = Entry->Value;
211 } else {
212 InMemoryStruct<macho::SymbolTableEntry> Entry;
213 getSymbolTableEntry(DRI, Entry);
214 Result = Entry->Value;
215 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000216 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000217}
218
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000219error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
220 uint64_t &Result) const {
221 uint64_t SymbolOffset;
222 uint8_t SectionIndex;
223 if (MachOObj->is64Bit()) {
224 InMemoryStruct<macho::Symbol64TableEntry> Entry;
225 getSymbol64TableEntry(DRI, Entry);
226 SymbolOffset = Entry->Value;
227 SectionIndex = Entry->SectionIndex;
228 } else {
229 InMemoryStruct<macho::SymbolTableEntry> Entry;
230 getSymbolTableEntry(DRI, Entry);
231 SymbolOffset = Entry->Value;
232 SectionIndex = Entry->SectionIndex;
233 }
234 getSectionAddress(Sections[SectionIndex], Result);
235 Result += SymbolOffset;
236
237 return object_error::success;
238}
239
Michael J. Spencer25b15772011-06-25 17:55:23 +0000240error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
241 uint64_t &Result) const {
242 Result = UnknownAddressOrSize;
243 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000244}
245
Michael J. Spencer25b15772011-06-25 17:55:23 +0000246error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
247 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000248 uint8_t Type, Flags;
249 if (MachOObj->is64Bit()) {
250 InMemoryStruct<macho::Symbol64TableEntry> Entry;
251 getSymbol64TableEntry(DRI, Entry);
252 Type = Entry->Type;
253 Flags = Entry->Flags;
254 } else {
255 InMemoryStruct<macho::SymbolTableEntry> Entry;
256 getSymbolTableEntry(DRI, Entry);
257 Type = Entry->Type;
258 Flags = Entry->Flags;
259 }
Eric Christopher6256b032011-04-22 03:19:48 +0000260
261 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000262 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000263 case macho::STT_Undefined:
264 Char = 'u';
265 break;
266 case macho::STT_Absolute:
267 case macho::STT_Section:
268 Char = 's';
269 break;
270 default:
271 Char = '?';
272 break;
273 }
274
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000275 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Eric Christopher6256b032011-04-22 03:19:48 +0000276 Char = toupper(Char);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000277 Result = Char;
278 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000279}
280
Michael J. Spencer25b15772011-06-25 17:55:23 +0000281error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
282 bool &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000283 if (MachOObj->is64Bit()) {
284 InMemoryStruct<macho::Symbol64TableEntry> Entry;
285 getSymbol64TableEntry(DRI, Entry);
286 Result = Entry->Flags & macho::STF_StabsEntryMask;
287 } else {
288 InMemoryStruct<macho::SymbolTableEntry> Entry;
289 getSymbolTableEntry(DRI, Entry);
290 Result = Entry->Flags & macho::STF_StabsEntryMask;
291 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000292 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000293}
294
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000295error_code MachOObjectFile::isSymbolGlobal(DataRefImpl Symb, bool &Res) const {
296
297 if (MachOObj->is64Bit()) {
298 InMemoryStruct<macho::Symbol64TableEntry> Entry;
299 getSymbol64TableEntry(Symb, Entry);
300 Res = Entry->Type & MachO::NlistMaskExternal;
301 } else {
302 InMemoryStruct<macho::SymbolTableEntry> Entry;
303 getSymbolTableEntry(Symb, Entry);
304 Res = Entry->Type & MachO::NlistMaskExternal;
305 }
306 return object_error::success;
307}
308
309error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
310 SymbolRef::SymbolType &Res) const {
311 uint8_t n_type;
312 if (MachOObj->is64Bit()) {
313 InMemoryStruct<macho::Symbol64TableEntry> Entry;
314 getSymbol64TableEntry(Symb, Entry);
315 n_type = Entry->Type;
316 } else {
317 InMemoryStruct<macho::SymbolTableEntry> Entry;
318 getSymbolTableEntry(Symb, Entry);
319 n_type = Entry->Type;
320 }
321 Res = SymbolRef::ST_Other;
322 switch (n_type & MachO::NlistMaskType) {
323 case MachO::NListTypeUndefined :
324 Res = SymbolRef::ST_External;
325 break;
326 case MachO::NListTypeSection :
327 Res = SymbolRef::ST_Function;
328 break;
329 }
330 return object_error::success;
331}
332
333
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000334symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000335 // DRI.d.a = segment number; DRI.d.b = symbol index.
336 DataRefImpl DRI;
337 DRI.d.a = DRI.d.b = 0;
338 moveToNextSymbol(DRI);
339 return symbol_iterator(SymbolRef(DRI, this));
340}
341
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000342symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000343 DataRefImpl DRI;
344 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
345 DRI.d.b = 0;
346 return symbol_iterator(SymbolRef(DRI, this));
347}
348
349
350/*===-- Sections ----------------------------------------------------------===*/
351
352void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
353 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
354 while (DRI.d.a < LoadCommandCount) {
355 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
356 if (LCI.Command.Type == macho::LCT_Segment) {
357 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
358 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
359 if (DRI.d.b < SegmentLoadCmd->NumSections)
360 return;
361 } else if (LCI.Command.Type == macho::LCT_Segment64) {
362 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
363 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
364 if (DRI.d.b < Segment64LoadCmd->NumSections)
365 return;
366 }
367
368 DRI.d.a++;
369 DRI.d.b = 0;
370 }
371}
372
Michael J. Spencer25b15772011-06-25 17:55:23 +0000373error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
374 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000375 DRI.d.b++;
376 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000377 Result = SectionRef(DRI, this);
378 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000379}
380
381void
382MachOObjectFile::getSection(DataRefImpl DRI,
383 InMemoryStruct<macho::Section> &Res) const {
384 InMemoryStruct<macho::SegmentLoadCommand> SLC;
385 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
386 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
387 MachOObj->ReadSection(LCI, DRI.d.b, Res);
388}
389
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000390std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
391 SectionList::const_iterator loc =
392 std::find(Sections.begin(), Sections.end(), Sec);
393 assert(loc != Sections.end() && "Sec is not a valid section!");
394 return std::distance(Sections.begin(), loc);
395}
396
Benjamin Kramer7d145782011-07-15 00:14:48 +0000397void
398MachOObjectFile::getSection64(DataRefImpl DRI,
399 InMemoryStruct<macho::Section64> &Res) const {
400 InMemoryStruct<macho::Segment64LoadCommand> SLC;
401 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
402 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
403 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
404}
405
406static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
407 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
408 if (LCI.Command.Type == macho::LCT_Segment64)
409 return true;
410 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
411 return false;
412}
413
Michael J. Spencer25b15772011-06-25 17:55:23 +0000414error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
415 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000416 // FIXME: thread safety.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000417 static char result[34];
Benjamin Kramer7d145782011-07-15 00:14:48 +0000418 if (is64BitLoadCommand(MachOObj, DRI)) {
419 InMemoryStruct<macho::Segment64LoadCommand> SLC;
420 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
421 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
422 InMemoryStruct<macho::Section64> Sect;
423 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
424
Benjamin Kramer291e7672011-07-15 00:29:02 +0000425 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000426 strcat(result, ",");
427 strcat(result, Sect->Name);
428 } else {
429 InMemoryStruct<macho::SegmentLoadCommand> SLC;
430 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
431 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
432 InMemoryStruct<macho::Section> Sect;
433 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
434
Benjamin Kramer291e7672011-07-15 00:29:02 +0000435 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000436 strcat(result, ",");
437 strcat(result, Sect->Name);
438 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000439 Result = StringRef(result);
440 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000441}
442
Michael J. Spencer25b15772011-06-25 17:55:23 +0000443error_code MachOObjectFile::getSectionAddress(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->Address;
449 } else {
450 InMemoryStruct<macho::Section> Sect;
451 getSection(DRI, Sect);
452 Result = Sect->Address;
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::getSectionSize(DataRefImpl DRI,
458 uint64_t &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 = Sect->Size;
463 } else {
464 InMemoryStruct<macho::Section> Sect;
465 getSection(DRI, Sect);
466 Result = 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::getSectionContents(DataRefImpl DRI,
472 StringRef &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 = MachOObj->getData(Sect->Offset, Sect->Size);
477 } else {
478 InMemoryStruct<macho::Section> Sect;
479 getSection(DRI, Sect);
480 Result = MachOObj->getData(Sect->Offset, Sect->Size);
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. Spencere2f2f072011-10-10 21:55:43 +0000485error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
486 uint64_t &Result) const {
487 if (is64BitLoadCommand(MachOObj, DRI)) {
488 InMemoryStruct<macho::Section64> Sect;
489 getSection64(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000490 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000491 } else {
492 InMemoryStruct<macho::Section> Sect;
493 getSection(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000494 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000495 }
496 return object_error::success;
497}
498
Michael J. Spencer25b15772011-06-25 17:55:23 +0000499error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
500 bool &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000501 if (is64BitLoadCommand(MachOObj, DRI)) {
502 InMemoryStruct<macho::Section64> Sect;
503 getSection64(DRI, Sect);
504 Result = !strcmp(Sect->Name, "__text");
505 } else {
506 InMemoryStruct<macho::Section> Sect;
507 getSection(DRI, Sect);
508 Result = !strcmp(Sect->Name, "__text");
509 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000510 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000511}
512
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000513error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
514 bool &Result) const {
515 // FIXME: Unimplemented.
516 Result = false;
517 return object_error::success;
518}
519
520error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
521 bool &Result) const {
522 // FIXME: Unimplemented.
523 Result = false;
524 return object_error::success;
525}
526
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000527error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
528 DataRefImpl Symb,
529 bool &Result) const {
530 if (MachOObj->is64Bit()) {
531 InMemoryStruct<macho::Symbol64TableEntry> Entry;
532 getSymbol64TableEntry(Symb, Entry);
533 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
534 } else {
535 InMemoryStruct<macho::SymbolTableEntry> Entry;
536 getSymbolTableEntry(Symb, Entry);
537 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
538 }
539 return object_error::success;
540}
541
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000542relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
543 DataRefImpl ret;
544 ret.d.a = 0;
545 ret.d.b = getSectionIndex(Sec);
546 return relocation_iterator(RelocationRef(ret, this));
547}
548relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
549 uint32_t last_reloc;
550 if (is64BitLoadCommand(MachOObj, Sec)) {
551 InMemoryStruct<macho::Section64> Sect;
552 getSection64(Sec, Sect);
553 last_reloc = Sect->NumRelocationTableEntries;
554 } else {
555 InMemoryStruct<macho::Section> Sect;
556 getSection(Sec, Sect);
557 last_reloc = Sect->NumRelocationTableEntries;
558 }
559 DataRefImpl ret;
560 ret.d.a = last_reloc;
561 ret.d.b = getSectionIndex(Sec);
562 return relocation_iterator(RelocationRef(ret, this));
563}
564
565section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000566 DataRefImpl DRI;
567 DRI.d.a = DRI.d.b = 0;
568 moveToNextSection(DRI);
569 return section_iterator(SectionRef(DRI, this));
570}
571
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000572section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000573 DataRefImpl DRI;
574 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
575 DRI.d.b = 0;
576 return section_iterator(SectionRef(DRI, this));
577}
578
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000579/*===-- Relocations -------------------------------------------------------===*/
580
581void MachOObjectFile::
582getRelocation(DataRefImpl Rel,
583 InMemoryStruct<macho::RelocationEntry> &Res) const {
584 uint32_t relOffset;
585 if (MachOObj->is64Bit()) {
586 InMemoryStruct<macho::Section64> Sect;
587 getSection64(Sections[Rel.d.b], Sect);
588 relOffset = Sect->RelocationTableOffset;
589 } else {
590 InMemoryStruct<macho::Section> Sect;
591 getSection(Sections[Rel.d.b], Sect);
592 relOffset = Sect->RelocationTableOffset;
593 }
594 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
595}
596error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
597 RelocationRef &Res) const {
598 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000599 Res = RelocationRef(Rel, this);
600 return object_error::success;
601}
602error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
603 uint64_t &Res) const {
604 const uint8_t* sectAddress = base();
605 if (MachOObj->is64Bit()) {
606 InMemoryStruct<macho::Section64> Sect;
607 getSection64(Sections[Rel.d.b], Sect);
608 sectAddress += Sect->Offset;
609 } else {
610 InMemoryStruct<macho::Section> Sect;
611 getSection(Sections[Rel.d.b], Sect);
612 sectAddress += Sect->Offset;
613 }
614 InMemoryStruct<macho::RelocationEntry> RE;
615 getRelocation(Rel, RE);
616 Res = reinterpret_cast<uintptr_t>(sectAddress + RE->Word0);
617 return object_error::success;
618}
619error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
620 SymbolRef &Res) const {
621 InMemoryStruct<macho::RelocationEntry> RE;
622 getRelocation(Rel, RE);
623 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
624 bool isExtern = (RE->Word1 >> 27) & 1;
625
626 DataRefImpl Sym;
627 Sym.d.a = Sym.d.b = 0;
628 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000629 if (isExtern) {
630 for (unsigned i = 0; i < SymbolIdx; i++) {
631 Sym.d.b++;
632 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000633 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000634 "Relocation symbol index out of range!");
635 }
636 }
637 Res = SymbolRef(Sym, this);
638 return object_error::success;
639}
640error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
641 uint32_t &Res) const {
642 InMemoryStruct<macho::RelocationEntry> RE;
643 getRelocation(Rel, RE);
644 Res = RE->Word1;
645 return object_error::success;
646}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000647error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
648 SmallVectorImpl<char> &Result) const {
649 return object_error::success;
650}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000651error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
652 int64_t &Res) const {
653 InMemoryStruct<macho::RelocationEntry> RE;
654 getRelocation(Rel, RE);
655 bool isExtern = (RE->Word1 >> 27) & 1;
656 Res = 0;
657 if (!isExtern) {
658 const uint8_t* sectAddress = base();
659 if (MachOObj->is64Bit()) {
660 InMemoryStruct<macho::Section64> Sect;
661 getSection64(Sections[Rel.d.b], Sect);
662 sectAddress += Sect->Offset;
663 } else {
664 InMemoryStruct<macho::Section> Sect;
665 getSection(Sections[Rel.d.b], Sect);
666 sectAddress += Sect->Offset;
667 }
668 Res = reinterpret_cast<uintptr_t>(sectAddress);
669 }
670 return object_error::success;
671}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000672error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
673 SmallVectorImpl<char> &Result) const {
674 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000675}
676
Eric Christopher6256b032011-04-22 03:19:48 +0000677/*===-- Miscellaneous -----------------------------------------------------===*/
678
679uint8_t MachOObjectFile::getBytesInAddress() const {
680 return MachOObj->is64Bit() ? 8 : 4;
681}
682
683StringRef MachOObjectFile::getFileFormatName() const {
684 if (!MachOObj->is64Bit()) {
685 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000686 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000687 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000688 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000689 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000690 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000691 return "Mach-O 32-bit ppc";
692 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000693 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000694 "64-bit object file when we're not 64-bit?");
695 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000696 }
697 }
698
699 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000700 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000701 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000702 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000703 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +0000704 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000705 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000706 "32-bit object file when we're 64-bit?");
707 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000708 }
709}
710
711unsigned MachOObjectFile::getArch() const {
712 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000713 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +0000714 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000715 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +0000716 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000717 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +0000718 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000719 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +0000720 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000721 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +0000722 return Triple::ppc64;
723 default:
724 return Triple::UnknownArch;
725 }
726}
727
728} // end namespace llvm