blob: 37edefe99fa45c8778e76c25f973f94d4768b8b6 [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;
73};
74
75ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000076 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000077 std::string Err;
78 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
79 if (!MachOObj)
80 return NULL;
Michael J. Spencer001c9202011-06-25 17:54:50 +000081 return new MachOObjectFile(Buffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000082}
83
84/*===-- Symbols -----------------------------------------------------------===*/
85
86void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
87 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
88 while (DRI.d.a < LoadCommandCount) {
89 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
90 if (LCI.Command.Type == macho::LCT_Symtab) {
91 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
92 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
93 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
94 return;
95 }
96
97 DRI.d.a++;
98 DRI.d.b = 0;
99 }
100}
101
102void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
103 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
104 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
105 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
106 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
107
108 if (RegisteredStringTable != DRI.d.a) {
109 MachOObj->RegisterStringTable(*SymtabLoadCmd);
110 RegisteredStringTable = DRI.d.a;
111 }
112
113 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
114 Res);
115}
116
117
Michael J. Spencer25b15772011-06-25 17:55:23 +0000118error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
119 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000120 DRI.d.b++;
121 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000122 Result = SymbolRef(DRI, this);
123 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000124}
125
Michael J. Spencer25b15772011-06-25 17:55:23 +0000126error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
127 StringRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000128 InMemoryStruct<macho::SymbolTableEntry> Entry;
129 getSymbolTableEntry(DRI, Entry);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000130 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
131 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000132}
133
Michael J. Spencer25b15772011-06-25 17:55:23 +0000134error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
135 uint64_t &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000136 InMemoryStruct<macho::SymbolTableEntry> Entry;
137 getSymbolTableEntry(DRI, Entry);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000138 Result = Entry->Value;
139 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000140}
141
Michael J. Spencer25b15772011-06-25 17:55:23 +0000142error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
143 uint64_t &Result) const {
144 Result = UnknownAddressOrSize;
145 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000146}
147
Michael J. Spencer25b15772011-06-25 17:55:23 +0000148error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
149 char &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000150 InMemoryStruct<macho::SymbolTableEntry> Entry;
151 getSymbolTableEntry(DRI, Entry);
152
153 char Char;
154 switch (Entry->Type & macho::STF_TypeMask) {
155 case macho::STT_Undefined:
156 Char = 'u';
157 break;
158 case macho::STT_Absolute:
159 case macho::STT_Section:
160 Char = 's';
161 break;
162 default:
163 Char = '?';
164 break;
165 }
166
167 if (Entry->Flags & (macho::STF_External | macho::STF_PrivateExtern))
168 Char = toupper(Char);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000169 Result = Char;
170 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000171}
172
Michael J. Spencer25b15772011-06-25 17:55:23 +0000173error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
174 bool &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000175 InMemoryStruct<macho::SymbolTableEntry> Entry;
176 getSymbolTableEntry(DRI, Entry);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000177 Result = Entry->Flags & macho::STF_StabsEntryMask;
178 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000179}
180
181ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
182 // DRI.d.a = segment number; DRI.d.b = symbol index.
183 DataRefImpl DRI;
184 DRI.d.a = DRI.d.b = 0;
185 moveToNextSymbol(DRI);
186 return symbol_iterator(SymbolRef(DRI, this));
187}
188
189ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
190 DataRefImpl DRI;
191 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
192 DRI.d.b = 0;
193 return symbol_iterator(SymbolRef(DRI, this));
194}
195
196
197/*===-- Sections ----------------------------------------------------------===*/
198
199void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
200 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
201 while (DRI.d.a < LoadCommandCount) {
202 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
203 if (LCI.Command.Type == macho::LCT_Segment) {
204 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
205 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
206 if (DRI.d.b < SegmentLoadCmd->NumSections)
207 return;
208 } else if (LCI.Command.Type == macho::LCT_Segment64) {
209 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
210 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
211 if (DRI.d.b < Segment64LoadCmd->NumSections)
212 return;
213 }
214
215 DRI.d.a++;
216 DRI.d.b = 0;
217 }
218}
219
Michael J. Spencer25b15772011-06-25 17:55:23 +0000220error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
221 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000222 DRI.d.b++;
223 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000224 Result = SectionRef(DRI, this);
225 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000226}
227
228void
229MachOObjectFile::getSection(DataRefImpl DRI,
230 InMemoryStruct<macho::Section> &Res) const {
231 InMemoryStruct<macho::SegmentLoadCommand> SLC;
232 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
233 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
234 MachOObj->ReadSection(LCI, DRI.d.b, Res);
235}
236
Michael J. Spencer25b15772011-06-25 17:55:23 +0000237error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
238 StringRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000239 InMemoryStruct<macho::SegmentLoadCommand> SLC;
240 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
241 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
242 InMemoryStruct<macho::Section> Sect;
243 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
244
Michael J. Spencer25b15772011-06-25 17:55:23 +0000245 static char result[34];
246 strcpy(result, SLC->Name);
247 strcat(result, ",");
248 strcat(result, Sect->Name);
249 Result = StringRef(result);
250 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000251}
252
Michael J. Spencer25b15772011-06-25 17:55:23 +0000253error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
254 uint64_t &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000255 InMemoryStruct<macho::Section> Sect;
256 getSection(DRI, Sect);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000257 Result = Sect->Address;
258 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000259}
260
Michael J. Spencer25b15772011-06-25 17:55:23 +0000261error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
262 uint64_t &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000263 InMemoryStruct<macho::Section> Sect;
264 getSection(DRI, Sect);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000265 Result = Sect->Size;
266 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000267}
268
Michael J. Spencer25b15772011-06-25 17:55:23 +0000269error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
270 StringRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000271 InMemoryStruct<macho::Section> Sect;
272 getSection(DRI, Sect);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000273 Result = MachOObj->getData(Sect->Offset, Sect->Size);
274 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000275}
276
Michael J. Spencer25b15772011-06-25 17:55:23 +0000277error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
278 bool &Result) const {
Benjamin Kramer82ba5b72011-07-15 00:14:46 +0000279 InMemoryStruct<macho::Section> Sect;
280 getSection(DRI, Sect);
281 Result = !strcmp(Sect->Name, "__text");
Michael J. Spencer25b15772011-06-25 17:55:23 +0000282 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000283}
284
285ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
286 DataRefImpl DRI;
287 DRI.d.a = DRI.d.b = 0;
288 moveToNextSection(DRI);
289 return section_iterator(SectionRef(DRI, this));
290}
291
292ObjectFile::section_iterator MachOObjectFile::end_sections() const {
293 DataRefImpl DRI;
294 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
295 DRI.d.b = 0;
296 return section_iterator(SectionRef(DRI, this));
297}
298
299/*===-- Miscellaneous -----------------------------------------------------===*/
300
301uint8_t MachOObjectFile::getBytesInAddress() const {
302 return MachOObj->is64Bit() ? 8 : 4;
303}
304
305StringRef MachOObjectFile::getFileFormatName() const {
306 if (!MachOObj->is64Bit()) {
307 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000308 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000309 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000310 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000311 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000312 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000313 return "Mach-O 32-bit ppc";
314 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000315 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000316 "64-bit object file when we're not 64-bit?");
317 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000318 }
319 }
320
321 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000322 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000323 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000324 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000325 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +0000326 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000327 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000328 "32-bit object file when we're 64-bit?");
329 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000330 }
331}
332
333unsigned MachOObjectFile::getArch() const {
334 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000335 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +0000336 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000337 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +0000338 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000339 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +0000340 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000341 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +0000342 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000343 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +0000344 return Triple::ppc64;
345 default:
346 return Triple::UnknownArch;
347 }
348}
349
350} // end namespace llvm
351