blob: 1aae85ab36c3c659252586e680d089638fbad6da [file] [log] [blame]
Owen Andersonf7c93a32011-10-11 17:32:27 +00001//===- MachO.h - MachO object file implementation ---------------*- 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 declares the MachOObjectFile class, which binds the MachOObject
11// class to the generic ObjectFile wrapper.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_OBJECT_MACHO_H
16#define LLVM_OBJECT_MACHO_H
17
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Object/MachOObject.h"
20#include "llvm/Support/MachO.h"
Owen Anderson1832f4d2011-10-26 20:42:54 +000021#include "llvm/Support/raw_ostream.h"
Owen Andersonf7c93a32011-10-11 17:32:27 +000022#include "llvm/ADT/SmallVector.h"
23
24namespace llvm {
25namespace object {
26
27typedef MachOObject::LoadCommandInfo LoadCommandInfo;
28
29class MachOObjectFile : public ObjectFile {
30public:
31 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
32
33 virtual symbol_iterator begin_symbols() const;
34 virtual symbol_iterator end_symbols() const;
Michael J. Spencerdfa18962012-02-28 00:40:37 +000035 virtual symbol_iterator begin_dynamic_symbols() const;
36 virtual symbol_iterator end_dynamic_symbols() const;
David Meyer5c2b4ea2012-03-01 01:36:50 +000037 virtual library_iterator begin_libraries_needed() const;
38 virtual library_iterator end_libraries_needed() const;
Owen Andersonf7c93a32011-10-11 17:32:27 +000039 virtual section_iterator begin_sections() const;
40 virtual section_iterator end_sections() const;
41
42 virtual uint8_t getBytesInAddress() const;
43 virtual StringRef getFileFormatName() const;
44 virtual unsigned getArch() const;
David Meyer97f77872012-03-01 22:19:54 +000045 virtual StringRef getLoadName() const;
Owen Andersonf7c93a32011-10-11 17:32:27 +000046
Owen Andersondf3f9292011-10-17 21:37:03 +000047 MachOObject *getObject() { return MachOObj; }
48
Michael J. Spencerab6bcf32011-10-17 23:53:37 +000049 static inline bool classof(const Binary *v) {
50 return v->getType() == isMachO;
51 }
52 static inline bool classof(const MachOObjectFile *v) { return true; }
53
Owen Andersonf7c93a32011-10-11 17:32:27 +000054protected:
55 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
56 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
Danil Malyshevb0436a72011-11-29 17:40:10 +000057 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
Owen Andersonf7c93a32011-10-11 17:32:27 +000058 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
59 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
60 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
David Meyerc46255a2012-02-28 23:47:53 +000061 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
Michael J. Spencer1130a792011-10-17 20:19:29 +000062 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +000063 virtual error_code getSymbolSection(DataRefImpl Symb,
64 section_iterator &Res) const;
Owen Andersonf7c93a32011-10-11 17:32:27 +000065
66 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
67 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
68 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
69 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
70 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
71 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
72 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
73 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
74 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
75 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
76 bool &Result) const;
77 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
78 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
79
80 virtual error_code getRelocationNext(DataRefImpl Rel,
81 RelocationRef &Res) const;
82 virtual error_code getRelocationAddress(DataRefImpl Rel,
83 uint64_t &Res) const;
Danil Malyshevb0436a72011-11-29 17:40:10 +000084 virtual error_code getRelocationOffset(DataRefImpl Rel,
85 uint64_t &Res) const;
Owen Andersonf7c93a32011-10-11 17:32:27 +000086 virtual error_code getRelocationSymbol(DataRefImpl Rel,
87 SymbolRef &Res) const;
88 virtual error_code getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +000089 uint64_t &Res) const;
Owen Andersonf7c93a32011-10-11 17:32:27 +000090 virtual error_code getRelocationTypeName(DataRefImpl Rel,
91 SmallVectorImpl<char> &Result) const;
92 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
93 int64_t &Res) const;
94 virtual error_code getRelocationValueString(DataRefImpl Rel,
95 SmallVectorImpl<char> &Result) const;
Owen Anderson0685e942011-10-25 20:35:53 +000096 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
Owen Andersonf7c93a32011-10-11 17:32:27 +000097
David Meyer5c2b4ea2012-03-01 01:36:50 +000098 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
99 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
100
Owen Andersonf7c93a32011-10-11 17:32:27 +0000101private:
102 MachOObject *MachOObj;
103 mutable uint32_t RegisteredStringTable;
104 typedef SmallVector<DataRefImpl, 1> SectionList;
105 SectionList Sections;
106
107
108 void moveToNextSection(DataRefImpl &DRI) const;
109 void getSymbolTableEntry(DataRefImpl DRI,
110 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
111 void getSymbol64TableEntry(DataRefImpl DRI,
112 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
113 void moveToNextSymbol(DataRefImpl &DRI) const;
114 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
115 void getSection64(DataRefImpl DRI,
116 InMemoryStruct<macho::Section64> &Res) const;
117 void getRelocation(DataRefImpl Rel,
118 InMemoryStruct<macho::RelocationEntry> &Res) const;
119 std::size_t getSectionIndex(DataRefImpl Sec) const;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000120
Owen Anderson1832f4d2011-10-26 20:42:54 +0000121 void printRelocationTargetName(InMemoryStruct<macho::RelocationEntry>& RE,
122 raw_string_ostream &fmt) const;
Owen Andersonf7c93a32011-10-11 17:32:27 +0000123};
124
125}
126}
127
128#endif
129