namespace llvm {
namespace object {
-MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
- error_code &ec)
- : ObjectFile(Binary::ID_MachO, Object, ec),
- MachOObj(MOO) {
+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 = MachOObj->getHeader().NumLoadCommands;
return MachOObj->is64Bit();
}
-const LoadCommandInfo &
+const MachOFormat::LoadCommand *
MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
- return MachOObj->getLoadCommandInfo(Index);
+ uint64_t Offset;
+ uint64_t NewOffset = MachOObj->getHeaderSize();
+ const MachOFormat::LoadCommand *Load;
+ unsigned I = 0;
+ do {
+ Offset = NewOffset;
+ StringRef Data = MachOObj->getData(Offset,
+ sizeof(MachOFormat::LoadCommand));
+ Load = reinterpret_cast<const MachOFormat::LoadCommand*>(Data.data());
+ NewOffset = Offset + Load->Size;
+ ++I;
+ } while (I != Index + 1);
+
+ return Load;
}
void MachOObjectFile::ReadULEB128s(uint64_t Index,
ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
error_code ec;
- std::string Err;
- MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
- if (!MachOObj)
+ ObjectFile *Ret = new MachOObjectFile(Buffer, ec);
+ if (ec)
return NULL;
- // 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(Buffer->getBuffer(),
- Buffer->getBufferIdentifier(), false);
- return new MachOObjectFile(NewBuffer, MachOObj, ec);
+ return Ret;
}
/*===-- Symbols -----------------------------------------------------------===*/
-const MachOFormat::SymtabLoadCommand *
-MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
- StringRef Data = MachOObj->getData(LCI.Offset,
- sizeof(MachOFormat::SymtabLoadCommand));
- return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
-}
-
-const MachOFormat::SegmentLoadCommand *
-MachOObjectFile::getSegmentLoadCommand(LoadCommandInfo LCI) const {
- StringRef Data = MachOObj->getData(LCI.Offset,
- sizeof(MachOFormat::SegmentLoadCommand));
- return reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Data.data());
-}
-
-const MachOFormat::LinkeditDataLoadCommand *
-MachOObjectFile::getLinkeditDataLoadCommand(LoadCommandInfo LCI) const {
- StringRef Data = MachOObj->getData(LCI.Offset,
- sizeof(MachOFormat::LinkeditDataLoadCommand));
- return
- reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Data.data());
-}
-
-const MachOFormat::Segment64LoadCommand *
-MachOObjectFile::getSegment64LoadCommand(LoadCommandInfo LCI) const {
- StringRef Data = MachOObj->getData(LCI.Offset,
- sizeof(MachOFormat::Segment64LoadCommand));
- return
- reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Data.data());
-}
-
void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
while (DRI.d.a < LoadCommandCount) {
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- if (LCI.Command.Type == macho::LCT_Symtab) {
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ if (Command->Type == macho::LCT_Symtab) {
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
- getSymtabLoadCommand(LCI);
+ reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
return;
}
const MachOFormat::SymbolTableEntry *
MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
- getSymtabLoadCommand(LCI);
+ reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
return getSymbolTableEntry(DRI, SymtabLoadCmd);
}
const MachOFormat::Symbol64TableEntry*
MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
- getSymtabLoadCommand(LCI);
+ reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
return getSymbol64TableEntry(DRI, SymtabLoadCmd);
}
error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
StringRef &Result) const {
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
- getSymtabLoadCommand(LCI);
+ reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
StringRef StringTable =
MachOObj->getData(SymtabLoadCmd->StringTableOffset,
void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
while (DRI.d.a < LoadCommandCount) {
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- if (LCI.Command.Type == macho::LCT_Segment) {
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ if (Command->Type == macho::LCT_Segment) {
const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
- getSegmentLoadCommand(LCI);
+ reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Command);
if (DRI.d.b < SegmentLoadCmd->NumSections)
return;
- } else if (LCI.Command.Type == macho::LCT_Segment64) {
+ } else if (Command->Type == macho::LCT_Segment64) {
const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
- getSegment64LoadCommand(LCI);
+ reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Command);
if (DRI.d.b < Segment64LoadCmd->NumSections)
return;
}
return object_error::success;
}
-static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- if (LCI.Command.Type == macho::LCT_Segment64)
+static bool is64BitLoadCommand(const MachOObjectFile *MachOObj,
+ DataRefImpl DRI) {
+ const MachOFormat::LoadCommand *Command =
+ MachOObj->getLoadCommandInfo(DRI.d.a);
+ if (Command->Type == macho::LCT_Segment64)
return true;
- assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
+ assert(Command->Type == macho::LCT_Segment && "Unexpected Type.");
return false;
}
const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
- assert(!is64BitLoadCommand(MachOObj.get(), DRI));
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
+ assert(!is64BitLoadCommand(this, DRI));
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
+ uintptr_t SectionAddr = CommandAddr + sizeof(macho::SegmentLoadCommand) +
DRI.d.b * sizeof(MachOFormat::Section);
- StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
- return reinterpret_cast<const MachOFormat::Section*>(Data.data());
+ return reinterpret_cast<const MachOFormat::Section*>(SectionAddr);
}
std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
const MachOFormat::Section64 *
MachOObjectFile::getSection64(DataRefImpl DRI) const {
- assert(is64BitLoadCommand(MachOObj.get(), DRI));
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
+ assert(is64BitLoadCommand(this, DRI));
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
+ uintptr_t SectionAddr = CommandAddr + sizeof(macho::Segment64LoadCommand) +
DRI.d.b * sizeof(MachOFormat::Section64);
- StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
- return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
+ return reinterpret_cast<const MachOFormat::Section64*>(SectionAddr);
}
static StringRef parseSegmentOrSectionName(const char *P) {
}
ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
- if (is64BitLoadCommand(MachOObj.get(), DRI)) {
+ if (is64BitLoadCommand(this, DRI)) {
const MachOFormat::Section64 *sec = getSection64(DRI);
return ArrayRef<char>(sec->Name);
} else {
ArrayRef<char>
MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
- if (is64BitLoadCommand(MachOObj.get(), Sec)) {
+ if (is64BitLoadCommand(this, Sec)) {
const MachOFormat::Section64 *sec = getSection64(Sec);
return ArrayRef<char>(sec->SegmentName, 16);
} else {
error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
uint64_t &Result) const {
- if (is64BitLoadCommand(MachOObj.get(), DRI)) {
+ if (is64BitLoadCommand(this, DRI)) {
const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = Sect->Address;
} else {
error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
uint64_t &Result) const {
- if (is64BitLoadCommand(MachOObj.get(), DRI)) {
+ if (is64BitLoadCommand(this, DRI)) {
const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = Sect->Size;
} else {
error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
StringRef &Result) const {
- if (is64BitLoadCommand(MachOObj.get(), DRI)) {
+ if (is64BitLoadCommand(this, DRI)) {
const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = MachOObj->getData(Sect->Offset, Sect->Size);
} else {
error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
uint64_t &Result) const {
- if (is64BitLoadCommand(MachOObj.get(), DRI)) {
+ if (is64BitLoadCommand(this, DRI)) {
const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = uint64_t(1) << Sect->Align;
} else {
error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
bool &Result) const {
- if (is64BitLoadCommand(MachOObj.get(), DRI)) {
+ if (is64BitLoadCommand(this, DRI)) {
const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = Sect->Flags & macho::SF_PureInstructions;
} else {
}
relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
uint32_t last_reloc;
- if (is64BitLoadCommand(MachOObj.get(), Sec)) {
+ if (is64BitLoadCommand(this, Sec)) {
const MachOFormat::Section64 *Sect = getSection64(Sec);
last_reloc = Sect->NumRelocationTableEntries;
} else {