blob: 044fd3266049e070b8bdcdd55c8646be0fa66fcb [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"
Eric Christopher6256b032011-04-22 03:19:48 +000021
22#include <cctype>
23#include <cstring>
24#include <limits>
25
26using namespace llvm;
27using namespace object;
28
29namespace llvm {
30
31typedef MachOObject::LoadCommandInfo LoadCommandInfo;
32
33class MachOObjectFile : public ObjectFile {
34public:
Michael J. Spencer001c9202011-06-25 17:54:50 +000035 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec)
36 : ObjectFile(Binary::isMachO, Object, ec),
Eric Christopher6256b032011-04-22 03:19:48 +000037 MachOObj(MOO),
38 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {}
39
40 virtual symbol_iterator begin_symbols() const;
41 virtual symbol_iterator end_symbols() const;
42 virtual section_iterator begin_sections() const;
43 virtual section_iterator end_sections() const;
44
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;
52 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
53 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
54 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
55 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000056
Michael J. Spencer25b15772011-06-25 17:55:23 +000057 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
58 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
59 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
60 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
61 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
62 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000063
64private:
65 MachOObject *MachOObj;
66 mutable uint32_t RegisteredStringTable;
67
68 void moveToNextSection(DataRefImpl &DRI) const;
69 void getSymbolTableEntry(DataRefImpl DRI,
70 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
71 void moveToNextSymbol(DataRefImpl &DRI) const;
72 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
Benjamin Kramer7d145782011-07-15 00:14:48 +000073 void getSection64(DataRefImpl DRI,
74 InMemoryStruct<macho::Section64> &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000075};
76
77ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000078 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000079 std::string Err;
80 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
81 if (!MachOObj)
82 return NULL;
Michael J. Spencer001c9202011-06-25 17:54:50 +000083 return new MachOObjectFile(Buffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000084}
85
86/*===-- Symbols -----------------------------------------------------------===*/
87
88void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
89 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
90 while (DRI.d.a < LoadCommandCount) {
91 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
92 if (LCI.Command.Type == macho::LCT_Symtab) {
93 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
94 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
95 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
96 return;
97 }
98
99 DRI.d.a++;
100 DRI.d.b = 0;
101 }
102}
103
104void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
105 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
106 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
107 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
108 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
109
110 if (RegisteredStringTable != DRI.d.a) {
111 MachOObj->RegisterStringTable(*SymtabLoadCmd);
112 RegisteredStringTable = DRI.d.a;
113 }
114
115 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
116 Res);
117}
118
119
Michael J. Spencer25b15772011-06-25 17:55:23 +0000120error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
121 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000122 DRI.d.b++;
123 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000124 Result = SymbolRef(DRI, this);
125 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000126}
127
Michael J. Spencer25b15772011-06-25 17:55:23 +0000128error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
129 StringRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000130 InMemoryStruct<macho::SymbolTableEntry> Entry;
131 getSymbolTableEntry(DRI, Entry);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000132 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
133 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000134}
135
Michael J. Spencer25b15772011-06-25 17:55:23 +0000136error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
137 uint64_t &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000138 InMemoryStruct<macho::SymbolTableEntry> Entry;
139 getSymbolTableEntry(DRI, Entry);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000140 Result = Entry->Value;
141 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000142}
143
Michael J. Spencer25b15772011-06-25 17:55:23 +0000144error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
145 uint64_t &Result) const {
146 Result = UnknownAddressOrSize;
147 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000148}
149
Michael J. Spencer25b15772011-06-25 17:55:23 +0000150error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
151 char &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000152 InMemoryStruct<macho::SymbolTableEntry> Entry;
153 getSymbolTableEntry(DRI, Entry);
154
155 char Char;
156 switch (Entry->Type & macho::STF_TypeMask) {
157 case macho::STT_Undefined:
158 Char = 'u';
159 break;
160 case macho::STT_Absolute:
161 case macho::STT_Section:
162 Char = 's';
163 break;
164 default:
165 Char = '?';
166 break;
167 }
168
169 if (Entry->Flags & (macho::STF_External | macho::STF_PrivateExtern))
170 Char = toupper(Char);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000171 Result = Char;
172 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000173}
174
Michael J. Spencer25b15772011-06-25 17:55:23 +0000175error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
176 bool &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000177 InMemoryStruct<macho::SymbolTableEntry> Entry;
178 getSymbolTableEntry(DRI, Entry);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000179 Result = Entry->Flags & macho::STF_StabsEntryMask;
180 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000181}
182
183ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
184 // DRI.d.a = segment number; DRI.d.b = symbol index.
185 DataRefImpl DRI;
186 DRI.d.a = DRI.d.b = 0;
187 moveToNextSymbol(DRI);
188 return symbol_iterator(SymbolRef(DRI, this));
189}
190
191ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
192 DataRefImpl DRI;
193 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
194 DRI.d.b = 0;
195 return symbol_iterator(SymbolRef(DRI, this));
196}
197
198
199/*===-- Sections ----------------------------------------------------------===*/
200
201void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
202 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
203 while (DRI.d.a < LoadCommandCount) {
204 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
205 if (LCI.Command.Type == macho::LCT_Segment) {
206 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
207 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
208 if (DRI.d.b < SegmentLoadCmd->NumSections)
209 return;
210 } else if (LCI.Command.Type == macho::LCT_Segment64) {
211 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
212 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
213 if (DRI.d.b < Segment64LoadCmd->NumSections)
214 return;
215 }
216
217 DRI.d.a++;
218 DRI.d.b = 0;
219 }
220}
221
Michael J. Spencer25b15772011-06-25 17:55:23 +0000222error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
223 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000224 DRI.d.b++;
225 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000226 Result = SectionRef(DRI, this);
227 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000228}
229
230void
231MachOObjectFile::getSection(DataRefImpl DRI,
232 InMemoryStruct<macho::Section> &Res) const {
233 InMemoryStruct<macho::SegmentLoadCommand> SLC;
234 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
235 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
236 MachOObj->ReadSection(LCI, DRI.d.b, Res);
237}
238
Benjamin Kramer7d145782011-07-15 00:14:48 +0000239void
240MachOObjectFile::getSection64(DataRefImpl DRI,
241 InMemoryStruct<macho::Section64> &Res) const {
242 InMemoryStruct<macho::Segment64LoadCommand> SLC;
243 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
244 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
245 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
246}
247
248static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
249 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
250 if (LCI.Command.Type == macho::LCT_Segment64)
251 return true;
252 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
253 return false;
254}
255
Michael J. Spencer25b15772011-06-25 17:55:23 +0000256error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
257 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000258 // FIXME: thread safety.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000259 static char result[34];
Benjamin Kramer7d145782011-07-15 00:14:48 +0000260 if (is64BitLoadCommand(MachOObj, DRI)) {
261 InMemoryStruct<macho::Segment64LoadCommand> SLC;
262 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
263 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
264 InMemoryStruct<macho::Section64> Sect;
265 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
266
Benjamin Kramer291e7672011-07-15 00:29:02 +0000267 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000268 strcat(result, ",");
269 strcat(result, Sect->Name);
270 } else {
271 InMemoryStruct<macho::SegmentLoadCommand> SLC;
272 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
273 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
274 InMemoryStruct<macho::Section> Sect;
275 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
276
Benjamin Kramer291e7672011-07-15 00:29:02 +0000277 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000278 strcat(result, ",");
279 strcat(result, Sect->Name);
280 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000281 Result = StringRef(result);
282 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000283}
284
Michael J. Spencer25b15772011-06-25 17:55:23 +0000285error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
286 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000287 if (is64BitLoadCommand(MachOObj, DRI)) {
288 InMemoryStruct<macho::Section64> Sect;
289 getSection64(DRI, Sect);
290 Result = Sect->Address;
291 } else {
292 InMemoryStruct<macho::Section> Sect;
293 getSection(DRI, Sect);
294 Result = Sect->Address;
295 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000296 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000297}
298
Michael J. Spencer25b15772011-06-25 17:55:23 +0000299error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
300 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000301 if (is64BitLoadCommand(MachOObj, DRI)) {
302 InMemoryStruct<macho::Section64> Sect;
303 getSection64(DRI, Sect);
304 Result = Sect->Size;
305 } else {
306 InMemoryStruct<macho::Section> Sect;
307 getSection(DRI, Sect);
308 Result = Sect->Size;
309 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000310 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000311}
312
Michael J. Spencer25b15772011-06-25 17:55:23 +0000313error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
314 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000315 if (is64BitLoadCommand(MachOObj, DRI)) {
316 InMemoryStruct<macho::Section64> Sect;
317 getSection64(DRI, Sect);
318 Result = MachOObj->getData(Sect->Offset, Sect->Size);
319 } else {
320 InMemoryStruct<macho::Section> Sect;
321 getSection(DRI, Sect);
322 Result = MachOObj->getData(Sect->Offset, Sect->Size);
323 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000324 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000325}
326
Michael J. Spencer25b15772011-06-25 17:55:23 +0000327error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
328 bool &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000329 if (is64BitLoadCommand(MachOObj, DRI)) {
330 InMemoryStruct<macho::Section64> Sect;
331 getSection64(DRI, Sect);
332 Result = !strcmp(Sect->Name, "__text");
333 } else {
334 InMemoryStruct<macho::Section> Sect;
335 getSection(DRI, Sect);
336 Result = !strcmp(Sect->Name, "__text");
337 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000338 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000339}
340
341ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
342 DataRefImpl DRI;
343 DRI.d.a = DRI.d.b = 0;
344 moveToNextSection(DRI);
345 return section_iterator(SectionRef(DRI, this));
346}
347
348ObjectFile::section_iterator MachOObjectFile::end_sections() const {
349 DataRefImpl DRI;
350 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
351 DRI.d.b = 0;
352 return section_iterator(SectionRef(DRI, this));
353}
354
355/*===-- Miscellaneous -----------------------------------------------------===*/
356
357uint8_t MachOObjectFile::getBytesInAddress() const {
358 return MachOObj->is64Bit() ? 8 : 4;
359}
360
361StringRef MachOObjectFile::getFileFormatName() const {
362 if (!MachOObj->is64Bit()) {
363 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000364 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000365 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000366 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000367 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000368 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000369 return "Mach-O 32-bit ppc";
370 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000371 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000372 "64-bit object file when we're not 64-bit?");
373 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000374 }
375 }
376
377 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000378 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000379 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000380 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000381 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +0000382 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000383 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000384 "32-bit object file when we're 64-bit?");
385 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000386 }
387}
388
389unsigned MachOObjectFile::getArch() const {
390 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000391 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +0000392 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000393 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +0000394 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000395 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +0000396 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000397 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +0000398 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000399 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +0000400 return Triple::ppc64;
401 default:
402 return Triple::UnknownArch;
403 }
404}
405
406} // end namespace llvm
407