//
//===----------------------------------------------------------------------===//
-#include "llvm/ADT/Triple.h"
#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>
namespace llvm {
namespace object {
-MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
- error_code &ec)
- : ObjectFile(Binary::ID_MachO, Object, ec),
- MachOObj(MOO),
- RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
+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;
}
}
+bool MachOObjectFile::is64Bit() const {
+ return MachOObj->is64Bit();
+}
+
+const MachOFormat::LoadCommand *
+MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
+ 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,
+ SmallVectorImpl<uint64_t> &Out) const {
+ return MachOObj->ReadULEB128s(Index, Out);
+}
+
+const macho::Header &MachOObjectFile::getHeader() const {
+ return MachOObj->getHeader();
+}
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;
- return new MachOObjectFile(Buffer, MachOObj, ec);
+ return Ret;
}
/*===-- Symbols -----------------------------------------------------------===*/
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) {
- InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
- MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ if (Command->Type == macho::LCT_Symtab) {
+ const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
+ reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
return;
}
}
}
-void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
- InMemoryStruct<macho::SymbolTableEntry> &Res) const {
- InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
+const MachOFormat::SymbolTableEntry *
+MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
+ reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
- if (RegisteredStringTable != DRI.d.a) {
- MachOObj->RegisterStringTable(*SymtabLoadCmd);
- RegisteredStringTable = DRI.d.a;
- }
-
- MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
- Res);
+ return getSymbolTableEntry(DRI, SymtabLoadCmd);
}
-void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
- InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
- InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
+const MachOFormat::SymbolTableEntry *
+MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
+ const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
+ uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
+ unsigned Index = DRI.d.b;
+ uint64_t Offset = (SymbolTableOffset +
+ Index * sizeof(macho::SymbolTableEntry));
+ StringRef Data = MachOObj->getData(Offset,
+ sizeof(MachOFormat::SymbolTableEntry));
+ return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
+}
- if (RegisteredStringTable != DRI.d.a) {
- MachOObj->RegisterStringTable(*SymtabLoadCmd);
- RegisteredStringTable = DRI.d.a;
- }
+const MachOFormat::Symbol64TableEntry*
+MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
+ reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
- MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
- Res);
+ return getSymbol64TableEntry(DRI, SymtabLoadCmd);
}
+const MachOFormat::Symbol64TableEntry*
+MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
+ const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
+ uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
+ unsigned Index = DRI.d.b;
+ uint64_t Offset = (SymbolTableOffset +
+ Index * sizeof(macho::Symbol64TableEntry));
+ StringRef Data = MachOObj->getData(Offset,
+ sizeof(MachOFormat::Symbol64TableEntry));
+ return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
+}
error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
SymbolRef &Result) const {
error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
StringRef &Result) const {
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
+ reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Command);
+
+ StringRef StringTable =
+ MachOObj->getData(SymtabLoadCmd->StringTableOffset,
+ SymtabLoadCmd->StringTableSize);
+
+ uint32_t StringIndex;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(DRI, Entry);
- Result = MachOObj->getStringAtIndex(Entry->StringIndex);
+ const MachOFormat::Symbol64TableEntry *Entry =
+ getSymbol64TableEntry(DRI, SymtabLoadCmd);
+ StringIndex = Entry->StringIndex;
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(DRI, Entry);
- Result = MachOObj->getStringAtIndex(Entry->StringIndex);
+ const MachOFormat::SymbolTableEntry *Entry =
+ getSymbolTableEntry(DRI, SymtabLoadCmd);
+ StringIndex = Entry->StringIndex;
}
+
+ const char *Start = &StringTable.data()[StringIndex];
+ Result = StringRef(Start);
+
return object_error::success;
}
error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
uint64_t &Result) const {
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(DRI, Entry);
+ const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Result = Entry->Value;
if (Entry->SectionIndex) {
- InMemoryStruct<macho::Section64> Section;
- getSection64(Sections[Entry->SectionIndex-1], Section);
+ const MachOFormat::Section64 *Section =
+ getSection64(Sections[Entry->SectionIndex-1]);
Result += Section->Offset - Section->Address;
}
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(DRI, Entry);
+ const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Result = Entry->Value;
if (Entry->SectionIndex) {
- InMemoryStruct<macho::Section> Section;
- getSection(Sections[Entry->SectionIndex-1], Section);
+ const MachOFormat::Section *Section =
+ getSection(Sections[Entry->SectionIndex-1]);
Result += Section->Offset - Section->Address;
}
}
error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
uint64_t &Result) const {
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(DRI, Entry);
+ const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Result = Entry->Value;
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(DRI, Entry);
+ const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Result = Entry->Value;
}
return object_error::success;
uint64_t EndOffset = 0;
uint8_t SectionIndex;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(DRI, Entry);
+ const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
BeginOffset = Entry->Value;
SectionIndex = Entry->SectionIndex;
if (!SectionIndex) {
while (Command == DRI.d.a) {
moveToNextSymbol(DRI);
if (DRI.d.a < LoadCommandCount) {
- getSymbol64TableEntry(DRI, Entry);
+ Entry = getSymbol64TableEntry(DRI);
if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
if (!EndOffset || Entry->Value < EndOffset)
EndOffset = Entry->Value;
DRI.d.b++;
}
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(DRI, Entry);
+ const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
BeginOffset = Entry->Value;
SectionIndex = Entry->SectionIndex;
if (!SectionIndex) {
while (Command == DRI.d.a) {
moveToNextSymbol(DRI);
if (DRI.d.a < LoadCommandCount) {
- getSymbolTableEntry(DRI, Entry);
+ Entry = getSymbolTableEntry(DRI);
if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
if (!EndOffset || Entry->Value < EndOffset)
EndOffset = Entry->Value;
char &Result) const {
uint8_t Type, Flags;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(DRI, Entry);
+ const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
Type = Entry->Type;
Flags = Entry->Flags;
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(DRI, Entry);
+ const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
Type = Entry->Type;
Flags = Entry->Flags;
}
}
if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
- Char = toupper(Char);
+ Char = toupper(static_cast<unsigned char>(Char));
Result = Char;
return object_error::success;
}
uint16_t MachOFlags;
uint8_t MachOType;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(DRI, Entry);
+ const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
MachOFlags = Entry->Flags;
MachOType = Entry->Type;
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(DRI, Entry);
+ const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
MachOFlags = Entry->Flags;
MachOType = Entry->Type;
}
section_iterator &Res) const {
uint8_t index;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(Symb, Entry);
+ const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
index = Entry->SectionIndex;
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(Symb, Entry);
+ const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
index = Entry->SectionIndex;
}
SymbolRef::Type &Res) const {
uint8_t n_type;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(Symb, Entry);
+ const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
n_type = Entry->Type;
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(Symb, Entry);
+ const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
n_type = Entry->Type;
}
Res = SymbolRef::ST_Other;
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) {
- InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
- MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
+ const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
+ if (Command->Type == macho::LCT_Segment) {
+ const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
+ reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Command);
if (DRI.d.b < SegmentLoadCmd->NumSections)
return;
- } else if (LCI.Command.Type == macho::LCT_Segment64) {
- InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
- MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
+ } else if (Command->Type == macho::LCT_Segment64) {
+ const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
+ reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Command);
if (DRI.d.b < Segment64LoadCmd->NumSections)
return;
}
return object_error::success;
}
-void
-MachOObjectFile::getSection(DataRefImpl DRI,
- InMemoryStruct<macho::Section> &Res) const {
- InMemoryStruct<macho::SegmentLoadCommand> SLC;
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- MachOObj->ReadSegmentLoadCommand(LCI, SLC);
- MachOObj->ReadSection(LCI, DRI.d.b, Res);
+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(Command->Type == macho::LCT_Segment && "Unexpected Type.");
+ return false;
+}
+
+const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
+ 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);
+ return reinterpret_cast<const MachOFormat::Section*>(SectionAddr);
}
std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
return std::distance(Sections.begin(), loc);
}
-void
-MachOObjectFile::getSection64(DataRefImpl DRI,
- InMemoryStruct<macho::Section64> &Res) const {
- InMemoryStruct<macho::Segment64LoadCommand> SLC;
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- MachOObj->ReadSegment64LoadCommand(LCI, SLC);
- MachOObj->ReadSection64(LCI, DRI.d.b, Res);
+const MachOFormat::Section64 *
+MachOObjectFile::getSection64(DataRefImpl DRI) const {
+ 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);
+ return reinterpret_cast<const MachOFormat::Section64*>(SectionAddr);
}
-static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- if (LCI.Command.Type == macho::LCT_Segment64)
- return true;
- assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
- return false;
+static StringRef parseSegmentOrSectionName(const char *P) {
+ if (P[15] == 0)
+ // Null terminated.
+ return P;
+ // Not null terminated, so this is a 16 char string.
+ return StringRef(P, 16);
+}
+
+ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
+ if (is64BitLoadCommand(this, DRI)) {
+ const MachOFormat::Section64 *sec = getSection64(DRI);
+ return ArrayRef<char>(sec->Name);
+ } else {
+ const MachOFormat::Section *sec = getSection(DRI);
+ return ArrayRef<char>(sec->Name);
+ }
}
error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
StringRef &Result) const {
- // FIXME: thread safety.
- static char result[34];
- if (is64BitLoadCommand(MachOObj, DRI)) {
- InMemoryStruct<macho::Segment64LoadCommand> SLC;
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- MachOObj->ReadSegment64LoadCommand(LCI, SLC);
- InMemoryStruct<macho::Section64> Sect;
- MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
-
- strcpy(result, Sect->SegmentName);
- strcat(result, ",");
- strcat(result, Sect->Name);
+ ArrayRef<char> Raw = getSectionRawName(DRI);
+ Result = parseSegmentOrSectionName(Raw.data());
+ return object_error::success;
+}
+
+ArrayRef<char>
+MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
+ if (is64BitLoadCommand(this, Sec)) {
+ const MachOFormat::Section64 *sec = getSection64(Sec);
+ return ArrayRef<char>(sec->SegmentName, 16);
} else {
- InMemoryStruct<macho::SegmentLoadCommand> SLC;
- LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
- MachOObj->ReadSegmentLoadCommand(LCI, SLC);
- InMemoryStruct<macho::Section> Sect;
- MachOObj->ReadSection(LCI, DRI.d.b, Sect);
-
- strcpy(result, Sect->SegmentName);
- strcat(result, ",");
- strcat(result, Sect->Name);
+ const MachOFormat::Section *sec = getSection(Sec);
+ return ArrayRef<char>(sec->SegmentName);
}
- Result = StringRef(result);
- return object_error::success;
+}
+
+StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
+ ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
+ return parseSegmentOrSectionName(Raw.data());
}
error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
uint64_t &Result) const {
- if (is64BitLoadCommand(MachOObj, DRI)) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(DRI, Sect);
+ if (is64BitLoadCommand(this, DRI)) {
+ const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = Sect->Address;
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(DRI, Sect);
+ const MachOFormat::Section *Sect = getSection(DRI);
Result = Sect->Address;
}
return object_error::success;
error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
uint64_t &Result) const {
- if (is64BitLoadCommand(MachOObj, DRI)) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(DRI, Sect);
+ if (is64BitLoadCommand(this, DRI)) {
+ const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = Sect->Size;
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(DRI, Sect);
+ const MachOFormat::Section *Sect = getSection(DRI);
Result = Sect->Size;
}
return object_error::success;
error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
StringRef &Result) const {
- if (is64BitLoadCommand(MachOObj, DRI)) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(DRI, Sect);
+ if (is64BitLoadCommand(this, DRI)) {
+ const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = MachOObj->getData(Sect->Offset, Sect->Size);
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(DRI, Sect);
+ const MachOFormat::Section *Sect = getSection(DRI);
Result = MachOObj->getData(Sect->Offset, Sect->Size);
}
return object_error::success;
error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
uint64_t &Result) const {
- if (is64BitLoadCommand(MachOObj, DRI)) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(DRI, Sect);
+ if (is64BitLoadCommand(this, DRI)) {
+ const MachOFormat::Section64 *Sect = getSection64(DRI);
Result = uint64_t(1) << Sect->Align;
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(DRI, Sect);
+ const MachOFormat::Section *Sect = getSection(DRI);
Result = uint64_t(1) << Sect->Align;
}
return object_error::success;
error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
bool &Result) const {
- if (is64BitLoadCommand(MachOObj, DRI)) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(DRI, Sect);
- Result = !strcmp(Sect->Name, "__text");
+ if (is64BitLoadCommand(this, DRI)) {
+ const MachOFormat::Section64 *Sect = getSection64(DRI);
+ Result = Sect->Flags & macho::SF_PureInstructions;
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(DRI, Sect);
- Result = !strcmp(Sect->Name, "__text");
+ const MachOFormat::Section *Sect = getSection(DRI);
+ Result = Sect->Flags & macho::SF_PureInstructions;
}
return object_error::success;
}
error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
bool &Result) const {
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(DRI, Sect);
+ const MachOFormat::Section64 *Sect = getSection64(DRI);
unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
Result = (SectionType == MachO::SectionTypeZeroFill ||
SectionType == MachO::SectionTypeZeroFillLarge);
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(DRI, Sect);
+ const MachOFormat::Section *Sect = getSection(DRI);
unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
Result = (SectionType == MachO::SectionTypeZeroFill ||
SectionType == MachO::SectionTypeZeroFillLarge);
SectEnd += SectBegin;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Symbol64TableEntry> Entry;
- getSymbol64TableEntry(Symb, Entry);
+ const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
uint64_t SymAddr= Entry->Value;
Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
} else {
- InMemoryStruct<macho::SymbolTableEntry> Entry;
- getSymbolTableEntry(Symb, Entry);
+ const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
uint64_t SymAddr= Entry->Value;
Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
}
}
relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
uint32_t last_reloc;
- if (is64BitLoadCommand(MachOObj, Sec)) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(Sec, Sect);
+ if (is64BitLoadCommand(this, Sec)) {
+ const MachOFormat::Section64 *Sect = getSection64(Sec);
last_reloc = Sect->NumRelocationTableEntries;
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(Sec, Sect);
+ const MachOFormat::Section *Sect = getSection(Sec);
last_reloc = Sect->NumRelocationTableEntries;
}
DataRefImpl ret;
/*===-- Relocations -------------------------------------------------------===*/
-void MachOObjectFile::
-getRelocation(DataRefImpl Rel,
- InMemoryStruct<macho::RelocationEntry> &Res) const {
+const MachOFormat::RelocationEntry *
+MachOObjectFile::getRelocation(DataRefImpl Rel) const {
uint32_t relOffset;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(Sections[Rel.d.b], Sect);
+ const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
relOffset = Sect->RelocationTableOffset;
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(Sections[Rel.d.b], Sect);
+ const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
relOffset = Sect->RelocationTableOffset;
}
- MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
+ uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
+ StringRef Data =
+ MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
+ return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
}
+
error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
RelocationRef &Res) const {
++Rel.d.a;
uint64_t &Res) const {
const uint8_t* sectAddress = 0;
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(Sections[Rel.d.b], Sect);
+ const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
sectAddress += Sect->Address;
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(Sections[Rel.d.b], Sect);
+ const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
sectAddress += Sect->Address;
}
- InMemoryStruct<macho::RelocationEntry> RE;
- getRelocation(Rel, RE);
+ const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
unsigned Arch = getArch();
bool isScattered = (Arch != Triple::x86_64) &&
}
error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const {
- InMemoryStruct<macho::RelocationEntry> RE;
- getRelocation(Rel, RE);
+ const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
unsigned Arch = getArch();
bool isScattered = (Arch != Triple::x86_64) &&
}
error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
SymbolRef &Res) const {
- InMemoryStruct<macho::RelocationEntry> RE;
- getRelocation(Rel, RE);
+ const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
uint32_t SymbolIdx = RE->Word1 & 0xffffff;
bool isExtern = (RE->Word1 >> 27) & 1;
}
error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
uint64_t &Res) const {
- InMemoryStruct<macho::RelocationEntry> RE;
- getRelocation(Rel, RE);
+ const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
Res = RE->Word0;
Res <<= 32;
Res |= RE->Word1;
SmallVectorImpl<char> &Result) const {
// TODO: Support scattered relocations.
StringRef res;
- InMemoryStruct<macho::RelocationEntry> RE;
- getRelocation(Rel, RE);
+ const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
unsigned Arch = getArch();
bool isScattered = (Arch != Triple::x86_64) &&
}
error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
int64_t &Res) const {
- InMemoryStruct<macho::RelocationEntry> RE;
- getRelocation(Rel, RE);
+ const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
bool isExtern = (RE->Word1 >> 27) & 1;
Res = 0;
if (!isExtern) {
const uint8_t* sectAddress = base();
if (MachOObj->is64Bit()) {
- InMemoryStruct<macho::Section64> Sect;
- getSection64(Sections[Rel.d.b], Sect);
+ const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
sectAddress += Sect->Offset;
} else {
- InMemoryStruct<macho::Section> Sect;
- getSection(Sections[Rel.d.b], Sect);
+ const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
sectAddress += Sect->Offset;
}
Res = reinterpret_cast<uintptr_t>(sectAddress);
}
void MachOObjectFile::printRelocationTargetName(
- InMemoryStruct<macho::RelocationEntry>& RE,
+ const MachOFormat::RelocationEntry *RE,
raw_string_ostream &fmt) const {
unsigned Arch = getArch();
bool isScattered = (Arch != Triple::x86_64) &&
error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const {
- InMemoryStruct<macho::RelocationEntry> RE;
- getRelocation(Rel, RE);
+ const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
unsigned Arch = getArch();
bool isScattered = (Arch != Triple::x86_64) &&
break;
}
case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
- InMemoryStruct<macho::RelocationEntry> RENext;
DataRefImpl RelNext = Rel;
RelNext.d.a++;
- getRelocation(RelNext, RENext);
+ const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
// X86_64_SUBTRACTOR must be followed by a relocation of type
// X86_64_RELOC_UNSIGNED.
printRelocationTargetName(RENext, fmt);
fmt << "-";
printRelocationTargetName(RE, fmt);
+ break;
}
case macho::RIT_X86_64_TLV:
printRelocationTargetName(RE, fmt);
case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
return object_error::success;
case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
- InMemoryStruct<macho::RelocationEntry> RENext;
DataRefImpl RelNext = Rel;
RelNext.d.a++;
- getRelocation(RelNext, RENext);
+ const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
// X86 sect diff's must be followed by a relocation of type
// GENERIC_RELOC_PAIR.
// handled in the generic code.
switch (Type) {
case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
- InMemoryStruct<macho::RelocationEntry> RENext;
DataRefImpl RelNext = Rel;
RelNext.d.a++;
- getRelocation(RelNext, RENext);
+ const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
// X86 sect diff's must be followed by a relocation of type
// GENERIC_RELOC_PAIR.
fmt << ":lower16:(";
printRelocationTargetName(RE, fmt);
- InMemoryStruct<macho::RelocationEntry> RENext;
DataRefImpl RelNext = Rel;
RelNext.d.a++;
- getRelocation(RelNext, RENext);
+ const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
// ARM half relocs must be followed by a relocation of type
// ARM_RELOC_PAIR.
error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
bool &Result) const {
- InMemoryStruct<macho::RelocationEntry> RE;
- getRelocation(Rel, RE);
+ const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
unsigned Arch = getArch();
bool isScattered = (Arch != Triple::x86_64) &&
if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
DataRefImpl RelPrev = Rel;
RelPrev.d.a--;
- InMemoryStruct<macho::RelocationEntry> REPrev;
- getRelocation(RelPrev, REPrev);
+ const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
}
}
+ // Make sure the cpu type has the correct mask.
+ assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
+ == llvm::MachO::CPUArchABI64 &&
+ "32-bit object file when we're 64-bit?");
+
switch (MachOObj->getHeader().CPUType) {
case llvm::MachO::CPUTypeX86_64:
return "Mach-O 64-bit x86-64";
case llvm::MachO::CPUTypePowerPC64:
return "Mach-O 64-bit ppc64";
default:
- assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
- "32-bit object file when we're 64-bit?");
return "Mach-O 64-bit unknown";
}
}