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/Object/MachO.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Object/MachOFormat.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/DataExtractor.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/MemoryBuffer.h"
27 using namespace object;
32 MachOObjectFileBase::MachOObjectFileBase(MemoryBuffer *Object,
33 bool IsLittleEndian, bool Is64bits,
35 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object) {
38 bool MachOObjectFileBase::is64Bit() const {
39 return isa<MachOObjectFileLE64>(this) || isa<MachOObjectFileBE64>(this);
42 void MachOObjectFileBase::ReadULEB128s(uint64_t Index,
43 SmallVectorImpl<uint64_t> &Out) const {
44 DataExtractor extractor(ObjectFile::getData(), true, 0);
46 uint32_t offset = Index;
48 while (uint64_t delta = extractor.getULEB128(&offset)) {
54 unsigned MachOObjectFileBase::getHeaderSize() const {
55 return is64Bit() ? macho::Header64Size : macho::Header32Size;
58 StringRef MachOObjectFileBase::getData(size_t Offset, size_t Size) const {
59 return ObjectFile::getData().substr(Offset, Size);
62 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
63 StringRef Magic = Buffer->getBuffer().slice(0, 4);
66 if (Magic == "\xFE\xED\xFA\xCE")
67 Ret = new MachOObjectFileBE32(Buffer, ec);
68 else if (Magic == "\xCE\xFA\xED\xFE")
69 Ret = new MachOObjectFileLE32(Buffer, ec);
70 else if (Magic == "\xFE\xED\xFA\xCF")
71 Ret = new MachOObjectFileBE64(Buffer, ec);
72 else if (Magic == "\xCF\xFA\xED\xFE")
73 Ret = new MachOObjectFileLE64(Buffer, ec);
82 /*===-- Symbols -----------------------------------------------------------===*/
84 error_code MachOObjectFileBase::getSymbolValue(DataRefImpl Symb,
85 uint64_t &Val) const {
86 report_fatal_error("getSymbolValue unimplemented in MachOObjectFileBase");
89 symbol_iterator MachOObjectFileBase::begin_dynamic_symbols() const {
91 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFileBase");
94 symbol_iterator MachOObjectFileBase::end_dynamic_symbols() const {
96 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFileBase");
99 library_iterator MachOObjectFileBase::begin_libraries_needed() const {
101 report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
104 library_iterator MachOObjectFileBase::end_libraries_needed() const {
106 report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
109 StringRef MachOObjectFileBase::getLoadName() const {
111 report_fatal_error("get_load_name() unimplemented in MachOObjectFileBase");
114 /*===-- Sections ----------------------------------------------------------===*/
116 std::size_t MachOObjectFileBase::getSectionIndex(DataRefImpl Sec) const {
117 SectionList::const_iterator loc =
118 std::find(Sections.begin(), Sections.end(), Sec);
119 assert(loc != Sections.end() && "Sec is not a valid section!");
120 return std::distance(Sections.begin(), loc);
123 StringRef MachOObjectFileBase::parseSegmentOrSectionName(const char *P) const {
127 // Not null terminated, so this is a 16 char string.
128 return StringRef(P, 16);
131 error_code MachOObjectFileBase::isSectionData(DataRefImpl DRI,
132 bool &Result) const {
133 // FIXME: Unimplemented.
135 return object_error::success;
138 error_code MachOObjectFileBase::isSectionBSS(DataRefImpl DRI,
139 bool &Result) const {
140 // FIXME: Unimplemented.
142 return object_error::success;
146 MachOObjectFileBase::isSectionRequiredForExecution(DataRefImpl Sec,
147 bool &Result) const {
148 // FIXME: Unimplemented.
150 return object_error::success;
153 error_code MachOObjectFileBase::isSectionVirtual(DataRefImpl Sec,
154 bool &Result) const {
155 // FIXME: Unimplemented.
157 return object_error::success;
160 error_code MachOObjectFileBase::isSectionReadOnlyData(DataRefImpl Sec,
161 bool &Result) const {
162 // Consider using the code from isSectionText to look for __const sections.
163 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
164 // to use section attributes to distinguish code from data.
166 // FIXME: Unimplemented.
168 return object_error::success;
171 relocation_iterator MachOObjectFileBase::getSectionRelBegin(DataRefImpl Sec) const {
173 ret.d.b = getSectionIndex(Sec);
174 return relocation_iterator(RelocationRef(ret, this));
178 /*===-- Relocations -------------------------------------------------------===*/
180 error_code MachOObjectFileBase::getRelocationNext(DataRefImpl Rel,
181 RelocationRef &Res) const {
183 Res = RelocationRef(Rel, this);
184 return object_error::success;
187 error_code MachOObjectFileBase::getLibraryNext(DataRefImpl LibData,
188 LibraryRef &Res) const {
189 report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
192 error_code MachOObjectFileBase::getLibraryPath(DataRefImpl LibData,
193 StringRef &Res) const {
194 report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
197 error_code MachOObjectFileBase::getRelocationAdditionalInfo(DataRefImpl Rel,
198 int64_t &Res) const {
200 return object_error::success;
204 /*===-- Miscellaneous -----------------------------------------------------===*/
206 uint8_t MachOObjectFileBase::getBytesInAddress() const {
207 return is64Bit() ? 8 : 4;
210 } // end namespace object
211 } // end namespace llvm