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/ADT/SmallVector.h"
13 #include "llvm/Support/DataExtractor.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/Host.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Support/SwapByteOrder.h"
21 using namespace llvm::object;
23 /* Translation Utilities */
26 static void SwapValue(T &Value) {
27 Value = sys::SwapByteOrder(Value);
31 static void SwapStruct(T &Value);
34 static void ReadInMemoryStruct(const MachOObject &MOO,
35 StringRef Buffer, uint64_t Base,
36 InMemoryStruct<T> &Res) {
37 typedef T struct_type;
38 uint64_t Size = sizeof(struct_type);
40 // Check that the buffer contains the expected data.
41 if (Base + Size > Buffer.size()) {
46 // Check whether we can return a direct pointer.
48 const_cast<struct_type *>((const struct_type *)(Buffer.data() + Base));
49 if (!MOO.isSwappedEndian()) {
54 // Otherwise, copy the struct and translate the values.
61 MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
63 : Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_),
64 IsSwappedEndian(IsLittleEndian != sys::isLittleEndianHost()),
65 HasStringTable(false), LoadCommands(0), NumLoadedCommands(0) {
66 // Load the common header.
67 memcpy(&Header, Buffer->getBuffer().data(), sizeof(Header));
68 if (IsSwappedEndian) {
69 SwapValue(Header.Magic);
70 SwapValue(Header.CPUType);
71 SwapValue(Header.CPUSubtype);
72 SwapValue(Header.FileType);
73 SwapValue(Header.NumLoadCommands);
74 SwapValue(Header.SizeOfLoadCommands);
75 SwapValue(Header.Flags);
79 memcpy(&Header64Ext, Buffer->getBuffer().data() + sizeof(Header),
81 if (IsSwappedEndian) {
82 SwapValue(Header64Ext.Reserved);
86 // Create the load command array if sane.
87 if (getHeader().NumLoadCommands < (1 << 20))
88 LoadCommands = new LoadCommandInfo[getHeader().NumLoadCommands];
91 MachOObject::~MachOObject() {
92 delete [] LoadCommands;
95 MachOObject *MachOObject::LoadFromBuffer(MemoryBuffer *Buffer,
96 std::string *ErrorStr) {
97 // First, check the magic value and initialize the basic object info.
98 bool IsLittleEndian = false, Is64Bit = false;
99 StringRef Magic = Buffer->getBuffer().slice(0, 4);
100 if (Magic == "\xFE\xED\xFA\xCE") {
101 } else if (Magic == "\xCE\xFA\xED\xFE") {
102 IsLittleEndian = true;
103 } else if (Magic == "\xFE\xED\xFA\xCF") {
105 } else if (Magic == "\xCF\xFA\xED\xFE") {
106 IsLittleEndian = true;
109 if (ErrorStr) *ErrorStr = "not a Mach object file (invalid magic)";
113 // Ensure that the at least the full header is present.
114 unsigned HeaderSize = Is64Bit ? macho::Header64Size : macho::Header32Size;
115 if (Buffer->getBufferSize() < HeaderSize) {
116 if (ErrorStr) *ErrorStr = "not a Mach object file (invalid header)";
120 OwningPtr<MachOObject> Object(new MachOObject(Buffer, IsLittleEndian,
123 // Check for bogus number of load commands.
124 if (Object->getHeader().NumLoadCommands >= (1 << 20)) {
125 if (ErrorStr) *ErrorStr = "not a Mach object file (unreasonable header)";
129 if (ErrorStr) *ErrorStr = "";
130 return Object.take();
133 StringRef MachOObject::getData(size_t Offset, size_t Size) const {
134 return Buffer->getBuffer().substr(Offset,Size);
137 void MachOObject::RegisterStringTable(macho::SymtabLoadCommand &SLC) {
138 HasStringTable = true;
139 StringTable = Buffer->getBuffer().substr(SLC.StringTableOffset,
140 SLC.StringTableSize);
143 const MachOObject::LoadCommandInfo &
144 MachOObject::getLoadCommandInfo(unsigned Index) const {
145 assert(Index < getHeader().NumLoadCommands && "Invalid index!");
147 // Load the command, if necessary.
148 if (Index >= NumLoadedCommands) {
151 Offset = getHeaderSize();
153 const LoadCommandInfo &Prev = getLoadCommandInfo(Index - 1);
154 Offset = Prev.Offset + Prev.Command.Size;
157 LoadCommandInfo &Info = LoadCommands[Index];
158 memcpy(&Info.Command, Buffer->getBuffer().data() + Offset,
159 sizeof(macho::LoadCommand));
160 if (IsSwappedEndian) {
161 SwapValue(Info.Command.Type);
162 SwapValue(Info.Command.Size);
164 Info.Offset = Offset;
165 NumLoadedCommands = Index + 1;
168 return LoadCommands[Index];
172 void SwapStruct(macho::SegmentLoadCommand &Value) {
173 SwapValue(Value.Type);
174 SwapValue(Value.Size);
175 SwapValue(Value.VMAddress);
176 SwapValue(Value.VMSize);
177 SwapValue(Value.FileOffset);
178 SwapValue(Value.FileSize);
179 SwapValue(Value.MaxVMProtection);
180 SwapValue(Value.InitialVMProtection);
181 SwapValue(Value.NumSections);
182 SwapValue(Value.Flags);
184 void MachOObject::ReadSegmentLoadCommand(const LoadCommandInfo &LCI,
185 InMemoryStruct<macho::SegmentLoadCommand> &Res) const {
186 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
190 void SwapStruct(macho::Segment64LoadCommand &Value) {
191 SwapValue(Value.Type);
192 SwapValue(Value.Size);
193 SwapValue(Value.VMAddress);
194 SwapValue(Value.VMSize);
195 SwapValue(Value.FileOffset);
196 SwapValue(Value.FileSize);
197 SwapValue(Value.MaxVMProtection);
198 SwapValue(Value.InitialVMProtection);
199 SwapValue(Value.NumSections);
200 SwapValue(Value.Flags);
202 void MachOObject::ReadSegment64LoadCommand(const LoadCommandInfo &LCI,
203 InMemoryStruct<macho::Segment64LoadCommand> &Res) const {
204 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
208 void SwapStruct(macho::SymtabLoadCommand &Value) {
209 SwapValue(Value.Type);
210 SwapValue(Value.Size);
211 SwapValue(Value.SymbolTableOffset);
212 SwapValue(Value.NumSymbolTableEntries);
213 SwapValue(Value.StringTableOffset);
214 SwapValue(Value.StringTableSize);
216 void MachOObject::ReadSymtabLoadCommand(const LoadCommandInfo &LCI,
217 InMemoryStruct<macho::SymtabLoadCommand> &Res) const {
218 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
222 void SwapStruct(macho::DysymtabLoadCommand &Value) {
223 SwapValue(Value.Type);
224 SwapValue(Value.Size);
225 SwapValue(Value.LocalSymbolsIndex);
226 SwapValue(Value.NumLocalSymbols);
227 SwapValue(Value.ExternalSymbolsIndex);
228 SwapValue(Value.NumExternalSymbols);
229 SwapValue(Value.UndefinedSymbolsIndex);
230 SwapValue(Value.NumUndefinedSymbols);
231 SwapValue(Value.TOCOffset);
232 SwapValue(Value.NumTOCEntries);
233 SwapValue(Value.ModuleTableOffset);
234 SwapValue(Value.NumModuleTableEntries);
235 SwapValue(Value.ReferenceSymbolTableOffset);
236 SwapValue(Value.NumReferencedSymbolTableEntries);
237 SwapValue(Value.IndirectSymbolTableOffset);
238 SwapValue(Value.NumIndirectSymbolTableEntries);
239 SwapValue(Value.ExternalRelocationTableOffset);
240 SwapValue(Value.NumExternalRelocationTableEntries);
241 SwapValue(Value.LocalRelocationTableOffset);
242 SwapValue(Value.NumLocalRelocationTableEntries);
244 void MachOObject::ReadDysymtabLoadCommand(const LoadCommandInfo &LCI,
245 InMemoryStruct<macho::DysymtabLoadCommand> &Res) const {
246 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
250 void SwapStruct(macho::LinkeditDataLoadCommand &Value) {
251 SwapValue(Value.Type);
252 SwapValue(Value.Size);
253 SwapValue(Value.DataOffset);
254 SwapValue(Value.DataSize);
256 void MachOObject::ReadLinkeditDataLoadCommand(const LoadCommandInfo &LCI,
257 InMemoryStruct<macho::LinkeditDataLoadCommand> &Res) const {
258 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
262 void SwapStruct(macho::IndirectSymbolTableEntry &Value) {
263 SwapValue(Value.Index);
266 MachOObject::ReadIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
268 InMemoryStruct<macho::IndirectSymbolTableEntry> &Res) const {
269 uint64_t Offset = (DLC.IndirectSymbolTableOffset +
270 Index * sizeof(macho::IndirectSymbolTableEntry));
271 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
276 void SwapStruct(macho::Section &Value) {
277 SwapValue(Value.Address);
278 SwapValue(Value.Size);
279 SwapValue(Value.Offset);
280 SwapValue(Value.Align);
281 SwapValue(Value.RelocationTableOffset);
282 SwapValue(Value.NumRelocationTableEntries);
283 SwapValue(Value.Flags);
284 SwapValue(Value.Reserved1);
285 SwapValue(Value.Reserved2);
287 void MachOObject::ReadSection(const LoadCommandInfo &LCI,
289 InMemoryStruct<macho::Section> &Res) const {
290 assert(LCI.Command.Type == macho::LCT_Segment &&
291 "Unexpected load command info!");
292 uint64_t Offset = (LCI.Offset + sizeof(macho::SegmentLoadCommand) +
293 Index * sizeof(macho::Section));
294 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
298 void SwapStruct(macho::Section64 &Value) {
299 SwapValue(Value.Address);
300 SwapValue(Value.Size);
301 SwapValue(Value.Offset);
302 SwapValue(Value.Align);
303 SwapValue(Value.RelocationTableOffset);
304 SwapValue(Value.NumRelocationTableEntries);
305 SwapValue(Value.Flags);
306 SwapValue(Value.Reserved1);
307 SwapValue(Value.Reserved2);
308 SwapValue(Value.Reserved3);
310 void MachOObject::ReadSection64(const LoadCommandInfo &LCI,
312 InMemoryStruct<macho::Section64> &Res) const {
313 assert(LCI.Command.Type == macho::LCT_Segment64 &&
314 "Unexpected load command info!");
315 uint64_t Offset = (LCI.Offset + sizeof(macho::Segment64LoadCommand) +
316 Index * sizeof(macho::Section64));
317 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
321 void SwapStruct(macho::RelocationEntry &Value) {
322 SwapValue(Value.Word0);
323 SwapValue(Value.Word1);
325 void MachOObject::ReadRelocationEntry(uint64_t RelocationTableOffset,
327 InMemoryStruct<macho::RelocationEntry> &Res) const {
328 uint64_t Offset = (RelocationTableOffset +
329 Index * sizeof(macho::RelocationEntry));
330 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
334 void SwapStruct(macho::SymbolTableEntry &Value) {
335 SwapValue(Value.StringIndex);
336 SwapValue(Value.Flags);
337 SwapValue(Value.Value);
339 void MachOObject::ReadSymbolTableEntry(uint64_t SymbolTableOffset,
341 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
342 uint64_t Offset = (SymbolTableOffset +
343 Index * sizeof(macho::SymbolTableEntry));
344 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
348 void SwapStruct(macho::Symbol64TableEntry &Value) {
349 SwapValue(Value.StringIndex);
350 SwapValue(Value.Flags);
351 SwapValue(Value.Value);
353 void MachOObject::ReadSymbol64TableEntry(uint64_t SymbolTableOffset,
355 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
356 uint64_t Offset = (SymbolTableOffset +
357 Index * sizeof(macho::Symbol64TableEntry));
358 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
362 void SwapStruct(macho::DataInCodeTableEntry &Value) {
363 SwapValue(Value.Offset);
364 SwapValue(Value.Length);
365 SwapValue(Value.Kind);
367 void MachOObject::ReadDataInCodeTableEntry(uint64_t TableOffset,
369 InMemoryStruct<macho::DataInCodeTableEntry> &Res) const {
370 uint64_t Offset = (TableOffset +
371 Index * sizeof(macho::DataInCodeTableEntry));
372 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
375 void MachOObject::ReadULEB128s(uint64_t Index,
376 SmallVectorImpl<uint64_t> &Out) const {
377 DataExtractor extractor(Buffer->getBuffer(), true, 0);
379 uint32_t offset = Index;
381 while (uint64_t delta = extractor.getULEB128(&offset)) {
388 // Object Dumping Facilities
389 void MachOObject::dump() const { print(dbgs()); dbgs() << '\n'; }
390 void MachOObject::dumpHeader() const { printHeader(dbgs()); dbgs() << '\n'; }
392 void MachOObject::printHeader(raw_ostream &O) const {
393 O << "('cputype', " << Header.CPUType << ")\n";
394 O << "('cpusubtype', " << Header.CPUSubtype << ")\n";
395 O << "('filetype', " << Header.FileType << ")\n";
396 O << "('num_load_commands', " << Header.NumLoadCommands << ")\n";
397 O << "('load_commands_size', " << Header.SizeOfLoadCommands << ")\n";
398 O << "('flag', " << Header.Flags << ")\n";
400 // Print extended header if 64-bit.
402 O << "('reserved', " << Header64Ext.Reserved << ")\n";
405 void MachOObject::print(raw_ostream &O) const {
408 O << "Load Commands:\n";