1 //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
13 //===----------------------------------------------------------------------===//
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"
20 #include "llvm/Support/MachO.h"
27 using namespace object;
31 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
33 class MachOObjectFile : public ObjectFile {
35 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec)
36 : ObjectFile(Binary::isMachO, Object, ec),
38 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {}
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;
45 virtual uint8_t getBytesInAddress() const;
46 virtual StringRef getFileFormatName() const;
47 virtual unsigned getArch() const;
50 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;
57 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;
65 MachOObject *MachOObj;
66 mutable uint32_t RegisteredStringTable;
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 void getSection64(DataRefImpl DRI,
74 InMemoryStruct<macho::Section64> &Res) const;
77 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
80 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
83 return new MachOObjectFile(Buffer, MachOObj, ec);
86 /*===-- Symbols -----------------------------------------------------------===*/
88 void 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)
104 void 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);
110 if (RegisteredStringTable != DRI.d.a) {
111 MachOObj->RegisterStringTable(*SymtabLoadCmd);
112 RegisteredStringTable = DRI.d.a;
115 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
120 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
121 SymbolRef &Result) const {
123 moveToNextSymbol(DRI);
124 Result = SymbolRef(DRI, this);
125 return object_error::success;
128 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
129 StringRef &Result) const {
130 InMemoryStruct<macho::SymbolTableEntry> Entry;
131 getSymbolTableEntry(DRI, Entry);
132 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
133 return object_error::success;
136 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
137 uint64_t &Result) const {
138 InMemoryStruct<macho::SymbolTableEntry> Entry;
139 getSymbolTableEntry(DRI, Entry);
140 Result = Entry->Value;
141 return object_error::success;
144 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
145 uint64_t &Result) const {
146 Result = UnknownAddressOrSize;
147 return object_error::success;
150 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
151 char &Result) const {
152 InMemoryStruct<macho::SymbolTableEntry> Entry;
153 getSymbolTableEntry(DRI, Entry);
156 switch (Entry->Type & macho::STF_TypeMask) {
157 case macho::STT_Undefined:
160 case macho::STT_Absolute:
161 case macho::STT_Section:
169 if (Entry->Flags & (macho::STF_External | macho::STF_PrivateExtern))
170 Char = toupper(Char);
172 return object_error::success;
175 error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
176 bool &Result) const {
177 InMemoryStruct<macho::SymbolTableEntry> Entry;
178 getSymbolTableEntry(DRI, Entry);
179 Result = Entry->Flags & macho::STF_StabsEntryMask;
180 return object_error::success;
183 ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
184 // DRI.d.a = segment number; DRI.d.b = symbol index.
186 DRI.d.a = DRI.d.b = 0;
187 moveToNextSymbol(DRI);
188 return symbol_iterator(SymbolRef(DRI, this));
191 ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
193 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
195 return symbol_iterator(SymbolRef(DRI, this));
199 /*===-- Sections ----------------------------------------------------------===*/
201 void 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)
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)
222 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
223 SectionRef &Result) const {
225 moveToNextSection(DRI);
226 Result = SectionRef(DRI, this);
227 return object_error::success;
231 MachOObjectFile::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);
240 MachOObjectFile::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);
248 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
249 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
250 if (LCI.Command.Type == macho::LCT_Segment64)
252 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
256 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
257 StringRef &Result) const {
258 // FIXME: thread safety.
259 static char result[34];
260 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);
267 strcpy(result, Sect->SegmentName);
269 strcat(result, Sect->Name);
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);
277 strcpy(result, Sect->SegmentName);
279 strcat(result, Sect->Name);
281 Result = StringRef(result);
282 return object_error::success;
285 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
286 uint64_t &Result) const {
287 if (is64BitLoadCommand(MachOObj, DRI)) {
288 InMemoryStruct<macho::Section64> Sect;
289 getSection64(DRI, Sect);
290 Result = Sect->Address;
292 InMemoryStruct<macho::Section> Sect;
293 getSection(DRI, Sect);
294 Result = Sect->Address;
296 return object_error::success;
299 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
300 uint64_t &Result) const {
301 if (is64BitLoadCommand(MachOObj, DRI)) {
302 InMemoryStruct<macho::Section64> Sect;
303 getSection64(DRI, Sect);
306 InMemoryStruct<macho::Section> Sect;
307 getSection(DRI, Sect);
310 return object_error::success;
313 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
314 StringRef &Result) const {
315 if (is64BitLoadCommand(MachOObj, DRI)) {
316 InMemoryStruct<macho::Section64> Sect;
317 getSection64(DRI, Sect);
318 Result = MachOObj->getData(Sect->Offset, Sect->Size);
320 InMemoryStruct<macho::Section> Sect;
321 getSection(DRI, Sect);
322 Result = MachOObj->getData(Sect->Offset, Sect->Size);
324 return object_error::success;
327 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
328 bool &Result) const {
329 if (is64BitLoadCommand(MachOObj, DRI)) {
330 InMemoryStruct<macho::Section64> Sect;
331 getSection64(DRI, Sect);
332 Result = !strcmp(Sect->Name, "__text");
334 InMemoryStruct<macho::Section> Sect;
335 getSection(DRI, Sect);
336 Result = !strcmp(Sect->Name, "__text");
338 return object_error::success;
341 ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
343 DRI.d.a = DRI.d.b = 0;
344 moveToNextSection(DRI);
345 return section_iterator(SectionRef(DRI, this));
348 ObjectFile::section_iterator MachOObjectFile::end_sections() const {
350 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
352 return section_iterator(SectionRef(DRI, this));
355 /*===-- Miscellaneous -----------------------------------------------------===*/
357 uint8_t MachOObjectFile::getBytesInAddress() const {
358 return MachOObj->is64Bit() ? 8 : 4;
361 StringRef MachOObjectFile::getFileFormatName() const {
362 if (!MachOObj->is64Bit()) {
363 switch (MachOObj->getHeader().CPUType) {
364 case llvm::MachO::CPUTypeI386:
365 return "Mach-O 32-bit i386";
366 case llvm::MachO::CPUTypeARM:
368 case llvm::MachO::CPUTypePowerPC:
369 return "Mach-O 32-bit ppc";
371 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
372 "64-bit object file when we're not 64-bit?");
373 return "Mach-O 32-bit unknown";
377 switch (MachOObj->getHeader().CPUType) {
378 case llvm::MachO::CPUTypeX86_64:
379 return "Mach-O 64-bit x86-64";
380 case llvm::MachO::CPUTypePowerPC64:
381 return "Mach-O 64-bit ppc64";
383 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
384 "32-bit object file when we're 64-bit?");
385 return "Mach-O 64-bit unknown";
389 unsigned MachOObjectFile::getArch() const {
390 switch (MachOObj->getHeader().CPUType) {
391 case llvm::MachO::CPUTypeI386:
393 case llvm::MachO::CPUTypeX86_64:
394 return Triple::x86_64;
395 case llvm::MachO::CPUTypeARM:
397 case llvm::MachO::CPUTypePowerPC:
399 case llvm::MachO::CPUTypePowerPC64:
400 return Triple::ppc64;
402 return Triple::UnknownArch;
406 } // end namespace llvm