//===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the MachOObjectFile class, which binds the MachOObject
// class to the generic ObjectFile wrapper.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/MachO.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Object/MachOFormat.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cctype>
#include <cstring>
#include <limits>
using namespace llvm;
using namespace object;
namespace llvm {
namespace object {
MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, error_code &ec)
: ObjectFile(Binary::ID_MachO, Object) {
// MachOObject takes ownership of the Buffer we passed to it, and
// MachOObjectFile does, too, so we need to make sure they don't get the
// same object. A MemoryBuffer is cheap (it's just a reference to memory,
// not a copy of the memory itself), so just make a new copy here for
// the MachOObjectFile.
MemoryBuffer *NewBuffer =
MemoryBuffer::getMemBuffer(Object->getBuffer(),
Object->getBufferIdentifier(), false);
std::string ErrorStr;
MachOObj.reset(MachOObject::LoadFromBuffer(NewBuffer, &ErrorStr));
if (!MachOObj) {
ec = object_error::parse_failed;
return;
}
DataRefImpl DRI;
moveToNextSection(DRI);
uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
while (DRI.d.a < LoadCommandCount) {
Sections.push_back(DRI);
DRI.d.b++;
moveToNextSection(DRI);
}
}
bool MachOObjectFile::is64Bit() const {
return MachOObj->is64Bit();
}
const MachOFormat::LoadCommand *
MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
uint64_t Offset;
uint64_t NewOffset = getHeaderSize();
const MachOFormat::LoadCommand *Load;
unsigned I = 0;
do {
Offset = NewOffset;
StringRef Data = getData(Offset, sizeof(MachOFormat::LoadCommand));
Load =