1 //===- MachOObject.cpp - Mach-O Object File Wrapper -----------------------===//
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 #include "llvm/Object/MachOObject.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/Support/Host.h"
14 #include "llvm/Support/SwapByteOrder.h"
17 using namespace llvm::object;
19 /* Translation Utilities */
22 static void SwapValue(T &Value) {
23 Value = sys::SwapByteOrder(Value);
27 static void SwapStruct(T &Value);
30 static void ReadInMemoryStruct(const MachOObject &MOO,
31 StringRef Buffer, uint64_t Base,
32 InMemoryStruct<T> &Res) {
33 typedef T struct_type;
34 uint64_t Size = sizeof(struct_type);
36 // Check that the buffer contains the expected data.
37 if (Base + Size > Buffer.size()) {
42 // Check whether we can return a direct pointer.
43 struct_type *Ptr = (struct_type *) (Buffer.data() + Base);
44 if (!MOO.isSwappedEndian()) {
49 // Otherwise, copy the struct and translate the values.
56 MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
58 : Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_),
59 IsSwappedEndian(IsLittleEndian != sys::isLittleEndianHost()),
60 HasStringTable(false), LoadCommands(0), NumLoadedCommands(0) {
61 // Load the common header.
62 memcpy(&Header, Buffer->getBuffer().data(), sizeof(Header));
63 if (IsSwappedEndian) {
64 SwapValue(Header.Magic);
65 SwapValue(Header.CPUType);
66 SwapValue(Header.CPUSubtype);
67 SwapValue(Header.FileType);
68 SwapValue(Header.NumLoadCommands);
69 SwapValue(Header.SizeOfLoadCommands);
70 SwapValue(Header.Flags);
74 memcpy(&Header64Ext, Buffer->getBuffer().data() + sizeof(Header),
76 if (IsSwappedEndian) {
77 SwapValue(Header64Ext.Reserved);
81 // Create the load command array if sane.
82 if (getHeader().NumLoadCommands < (1 << 20))
83 LoadCommands = new LoadCommandInfo[getHeader().NumLoadCommands];
86 MachOObject::~MachOObject() {
87 delete [] LoadCommands;
90 MachOObject *MachOObject::LoadFromBuffer(MemoryBuffer *Buffer,
91 std::string *ErrorStr) {
92 // First, check the magic value and initialize the basic object info.
93 bool IsLittleEndian = false, Is64Bit = false;
94 StringRef Magic = Buffer->getBuffer().slice(0, 4);
95 if (Magic == "\xFE\xED\xFA\xCE") {
96 } else if (Magic == "\xCE\xFA\xED\xFE") {
97 IsLittleEndian = true;
98 } else if (Magic == "\xFE\xED\xFA\xCF") {
100 } else if (Magic == "\xCF\xFA\xED\xFE") {
101 IsLittleEndian = true;
104 if (ErrorStr) *ErrorStr = "not a Mach object file (invalid magic)";
108 // Ensure that the at least the full header is present.
109 unsigned HeaderSize = Is64Bit ? macho::Header64Size : macho::Header32Size;
110 if (Buffer->getBufferSize() < HeaderSize) {
111 if (ErrorStr) *ErrorStr = "not a Mach object file (invalid header)";
115 OwningPtr<MachOObject> Object(new MachOObject(Buffer, IsLittleEndian,
118 // Check for bogus number of load commands.
119 if (Object->getHeader().NumLoadCommands >= (1 << 20)) {
120 if (ErrorStr) *ErrorStr = "not a Mach object file (unreasonable header)";
124 if (ErrorStr) *ErrorStr = "";
125 return Object.take();
128 StringRef MachOObject::getData(size_t Offset, size_t Size) const {
129 return Buffer->getBuffer().substr(Offset,Size);
132 void MachOObject::RegisterStringTable(macho::SymtabLoadCommand &SLC) {
133 HasStringTable = true;
134 StringTable = Buffer->getBuffer().substr(SLC.StringTableOffset,
135 SLC.StringTableSize);
138 const MachOObject::LoadCommandInfo &
139 MachOObject::getLoadCommandInfo(unsigned Index) const {
140 assert(Index < getHeader().NumLoadCommands && "Invalid index!");
142 // Load the command, if necessary.
143 if (Index >= NumLoadedCommands) {
146 Offset = getHeaderSize();
148 const LoadCommandInfo &Prev = getLoadCommandInfo(Index - 1);
149 Offset = Prev.Offset + Prev.Command.Size;
152 LoadCommandInfo &Info = LoadCommands[Index];
153 memcpy(&Info.Command, Buffer->getBuffer().data() + Offset,
154 sizeof(macho::LoadCommand));
155 if (IsSwappedEndian) {
156 SwapValue(Info.Command.Type);
157 SwapValue(Info.Command.Size);
159 Info.Offset = Offset;
160 NumLoadedCommands = Index + 1;
163 return LoadCommands[Index];
167 void SwapStruct(macho::SegmentLoadCommand &Value) {
168 SwapValue(Value.Type);
169 SwapValue(Value.Size);
170 SwapValue(Value.VMAddress);
171 SwapValue(Value.VMSize);
172 SwapValue(Value.FileOffset);
173 SwapValue(Value.FileSize);
174 SwapValue(Value.MaxVMProtection);
175 SwapValue(Value.InitialVMProtection);
176 SwapValue(Value.NumSections);
177 SwapValue(Value.Flags);
179 void MachOObject::ReadSegmentLoadCommand(const LoadCommandInfo &LCI,
180 InMemoryStruct<macho::SegmentLoadCommand> &Res) const {
181 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
185 void SwapStruct(macho::Segment64LoadCommand &Value) {
186 SwapValue(Value.Type);
187 SwapValue(Value.Size);
188 SwapValue(Value.VMAddress);
189 SwapValue(Value.VMSize);
190 SwapValue(Value.FileOffset);
191 SwapValue(Value.FileSize);
192 SwapValue(Value.MaxVMProtection);
193 SwapValue(Value.InitialVMProtection);
194 SwapValue(Value.NumSections);
195 SwapValue(Value.Flags);
197 void MachOObject::ReadSegment64LoadCommand(const LoadCommandInfo &LCI,
198 InMemoryStruct<macho::Segment64LoadCommand> &Res) const {
199 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
203 void SwapStruct(macho::SymtabLoadCommand &Value) {
204 SwapValue(Value.Type);
205 SwapValue(Value.Size);
206 SwapValue(Value.SymbolTableOffset);
207 SwapValue(Value.NumSymbolTableEntries);
208 SwapValue(Value.StringTableOffset);
209 SwapValue(Value.StringTableSize);
211 void MachOObject::ReadSymtabLoadCommand(const LoadCommandInfo &LCI,
212 InMemoryStruct<macho::SymtabLoadCommand> &Res) const {
213 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
217 void SwapStruct(macho::DysymtabLoadCommand &Value) {
218 SwapValue(Value.Type);
219 SwapValue(Value.Size);
220 SwapValue(Value.LocalSymbolsIndex);
221 SwapValue(Value.NumLocalSymbols);
222 SwapValue(Value.ExternalSymbolsIndex);
223 SwapValue(Value.NumExternalSymbols);
224 SwapValue(Value.UndefinedSymbolsIndex);
225 SwapValue(Value.NumUndefinedSymbols);
226 SwapValue(Value.TOCOffset);
227 SwapValue(Value.NumTOCEntries);
228 SwapValue(Value.ModuleTableOffset);
229 SwapValue(Value.NumModuleTableEntries);
230 SwapValue(Value.ReferenceSymbolTableOffset);
231 SwapValue(Value.NumReferencedSymbolTableEntries);
232 SwapValue(Value.IndirectSymbolTableOffset);
233 SwapValue(Value.NumIndirectSymbolTableEntries);
234 SwapValue(Value.ExternalRelocationTableOffset);
235 SwapValue(Value.NumExternalRelocationTableEntries);
236 SwapValue(Value.LocalRelocationTableOffset);
237 SwapValue(Value.NumLocalRelocationTableEntries);
239 void MachOObject::ReadDysymtabLoadCommand(const LoadCommandInfo &LCI,
240 InMemoryStruct<macho::DysymtabLoadCommand> &Res) const {
241 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
245 void SwapStruct(macho::IndirectSymbolTableEntry &Value) {
246 SwapValue(Value.Index);
249 MachOObject::ReadIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
251 InMemoryStruct<macho::IndirectSymbolTableEntry> &Res) const {
252 uint64_t Offset = (DLC.IndirectSymbolTableOffset +
253 Index * sizeof(macho::IndirectSymbolTableEntry));
254 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
259 void SwapStruct(macho::Section &Value) {
260 SwapValue(Value.Address);
261 SwapValue(Value.Size);
262 SwapValue(Value.Offset);
263 SwapValue(Value.Align);
264 SwapValue(Value.RelocationTableOffset);
265 SwapValue(Value.NumRelocationTableEntries);
266 SwapValue(Value.Flags);
267 SwapValue(Value.Reserved1);
268 SwapValue(Value.Reserved2);
270 void MachOObject::ReadSection(const LoadCommandInfo &LCI,
272 InMemoryStruct<macho::Section> &Res) const {
273 assert(LCI.Command.Type == macho::LCT_Segment &&
274 "Unexpected load command info!");
275 uint64_t Offset = (LCI.Offset + sizeof(macho::SegmentLoadCommand) +
276 Index * sizeof(macho::Section));
277 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
281 void SwapStruct(macho::Section64 &Value) {
282 SwapValue(Value.Address);
283 SwapValue(Value.Size);
284 SwapValue(Value.Offset);
285 SwapValue(Value.Align);
286 SwapValue(Value.RelocationTableOffset);
287 SwapValue(Value.NumRelocationTableEntries);
288 SwapValue(Value.Flags);
289 SwapValue(Value.Reserved1);
290 SwapValue(Value.Reserved2);
291 SwapValue(Value.Reserved3);
293 void MachOObject::ReadSection64(const LoadCommandInfo &LCI,
295 InMemoryStruct<macho::Section64> &Res) const {
296 assert(LCI.Command.Type == macho::LCT_Segment64 &&
297 "Unexpected load command info!");
298 uint64_t Offset = (LCI.Offset + sizeof(macho::Segment64LoadCommand) +
299 Index * sizeof(macho::Section64));
300 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
304 void SwapStruct(macho::RelocationEntry &Value) {
305 SwapValue(Value.Word0);
306 SwapValue(Value.Word1);
308 void MachOObject::ReadRelocationEntry(uint64_t RelocationTableOffset,
310 InMemoryStruct<macho::RelocationEntry> &Res) const {
311 uint64_t Offset = (RelocationTableOffset +
312 Index * sizeof(macho::RelocationEntry));
313 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
317 void SwapStruct(macho::SymbolTableEntry &Value) {
318 SwapValue(Value.StringIndex);
319 SwapValue(Value.Flags);
320 SwapValue(Value.Value);
322 void MachOObject::ReadSymbolTableEntry(uint64_t SymbolTableOffset,
324 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
325 uint64_t Offset = (SymbolTableOffset +
326 Index * sizeof(macho::SymbolTableEntry));
327 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
331 void SwapStruct(macho::Symbol64TableEntry &Value) {
332 SwapValue(Value.StringIndex);
333 SwapValue(Value.Flags);
334 SwapValue(Value.Value);
336 void MachOObject::ReadSymbol64TableEntry(uint64_t SymbolTableOffset,
338 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
339 uint64_t Offset = (SymbolTableOffset +
340 Index * sizeof(macho::Symbol64TableEntry));
341 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);