summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2013-04-13 01:45:40 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2013-04-13 01:45:40 +0000
commitda2a2372c6ae715befae7f086afe769dd80814f3 (patch)
treeb9e1c0966e7c73d6ee1bee63a61bab5ceeee4d4d /tools
parent3d60241c3e86973be281660bc5971c3a46cfdc47 (diff)
downloadllvm-da2a2372c6ae715befae7f086afe769dd80814f3.tar.gz
llvm-da2a2372c6ae715befae7f086afe769dd80814f3.tar.bz2
llvm-da2a2372c6ae715befae7f086afe769dd80814f3.tar.xz
Finish templating MachObjectFile over endianness.
We are now able to handle big endian macho files in llvm-readobject. Thanks to David Fang for providing the object files. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179440 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/llvm-objdump/MachODump.cpp48
-rw-r--r--tools/llvm-objdump/llvm-objdump.cpp18
-rw-r--r--tools/llvm-readobj/MachODumper.cpp132
3 files changed, 128 insertions, 70 deletions
diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp
index 89847d0499..4b6cb5f54c 100644
--- a/tools/llvm-objdump/MachODump.cpp
+++ b/tools/llvm-objdump/MachODump.cpp
@@ -27,6 +27,7 @@
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Object/MachO.h"
+#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
@@ -183,11 +184,14 @@ static void emitDOTFile(const char *FileName, const MCFunction &f,
Out << "}\n";
}
-static void getSectionsAndSymbols(const MachOObjectFileBase::Header *Header,
- MachOObjectFileBase *MachOObj,
- std::vector<SectionRef> &Sections,
- std::vector<SymbolRef> &Symbols,
- SmallVectorImpl<uint64_t> &FoundFns) {
+template<endianness E>
+static void
+getSectionsAndSymbols(const typename MachOObjectFileMiddle<E>::Header *Header,
+ const MachOObjectFileMiddle<E> *MachOObj,
+ std::vector<SectionRef> &Sections,
+ std::vector<SymbolRef> &Symbols,
+ SmallVectorImpl<uint64_t> &FoundFns) {
+ typedef MachOObjectFileMiddle<E> ObjType;
error_code ec;
for (symbol_iterator SI = MachOObj->begin_symbols(),
SE = MachOObj->end_symbols(); SI != SE; SI.increment(ec))
@@ -202,19 +206,23 @@ static void getSectionsAndSymbols(const MachOObjectFileBase::Header *Header,
}
for (unsigned i = 0; i != Header->NumLoadCommands; ++i) {
- const MachOObjectFileBase::LoadCommand *Command =
+ const typename ObjType::LoadCommand *Command =
MachOObj->getLoadCommandInfo(i);
if (Command->Type == macho::LCT_FunctionStarts) {
// We found a function starts segment, parse the addresses for later
// consumption.
- const MachOObjectFileBase::LinkeditDataLoadCommand *LLC =
- reinterpret_cast<const MachOObjectFileBase::LinkeditDataLoadCommand*>(Command);
+ const typename ObjType::LinkeditDataLoadCommand *LLC =
+ reinterpret_cast<const typename ObjType::LinkeditDataLoadCommand*>(Command);
MachOObj->ReadULEB128s(LLC->DataOffset, FoundFns);
}
}
}
+template<endianness E>
+static void DisassembleInputMachO2(StringRef Filename,
+ MachOObjectFileMiddle<E> *MachOOF);
+
void llvm::DisassembleInputMachO(StringRef Filename) {
OwningPtr<MemoryBuffer> Buff;
@@ -226,7 +234,18 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
OwningPtr<MachOObjectFileBase> MachOOF(static_cast<MachOObjectFileBase*>(
ObjectFile::createMachOObjectFile(Buff.take())));
- const Target *TheTarget = GetTarget(MachOOF.get());
+ if (MachOObjectFileLE *O = dyn_cast<MachOObjectFileLE>(MachOOF.get())) {
+ DisassembleInputMachO2(Filename, O);
+ return;
+ }
+ MachOObjectFileBE *O = cast<MachOObjectFileBE>(MachOOF.get());
+ DisassembleInputMachO2(Filename, O);
+}
+
+template<endianness E>
+static void DisassembleInputMachO2(StringRef Filename,
+ MachOObjectFileMiddle<E> *MachOOF) {
+ const Target *TheTarget = GetTarget(MachOOF);
if (!TheTarget) {
// GetTarget prints out stuff.
return;
@@ -254,13 +273,14 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
outs() << '\n' << Filename << ":\n\n";
- const MachOObjectFileBase::Header *Header = MachOOF->getHeader();
+ const typename MachOObjectFileMiddle<E>::Header *Header =
+ MachOOF->getHeader();
std::vector<SectionRef> Sections;
std::vector<SymbolRef> Symbols;
SmallVector<uint64_t, 8> FoundFns;
- getSectionsAndSymbols(Header, MachOOF.get(), Sections, Symbols, FoundFns);
+ getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns);
// Make a copy of the unsorted symbol list. FIXME: duplication
std::vector<SymbolRef> UnsortedSymbols(Symbols);
@@ -274,7 +294,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
#endif
OwningPtr<DIContext> diContext;
- ObjectFile *DbgObj = MachOOF.get();
+ ObjectFile *DbgObj = MachOOF;
// Try to find debug info and set up the DIContext for it.
if (UseDbg) {
// A separate DSym file path was specified, parse it as a macho file,
@@ -563,7 +583,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
Relocs[j].second.getName(SymName);
outs() << "\t# " << SymName << ' ';
- DumpAddress(Addr, Sections, MachOOF.get(), outs());
+ DumpAddress(Addr, Sections, MachOOF, outs());
}
// If this instructions contains an address, see if we can evaluate
@@ -572,7 +592,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
Inst.Address,
Inst.Size);
if (targ != -1ULL)
- DumpAddress(targ, Sections, MachOOF.get(), outs());
+ DumpAddress(targ, Sections, MachOOF, outs());
// Print debug info.
if (diContext) {
diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp
index 4790d7a5fe..5a0519ddc1 100644
--- a/tools/llvm-objdump/llvm-objdump.cpp
+++ b/tools/llvm-objdump/llvm-objdump.cpp
@@ -191,6 +191,14 @@ bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
return a_addr < b_addr;
}
+StringRef
+getSectionFinalSegmentName(const MachOObjectFileBase *MachO, DataRefImpl DR) {
+ if (const MachOObjectFileLE *O = dyn_cast<MachOObjectFileLE>(MachO))
+ return O->getSectionFinalSegmentName(DR);
+ const MachOObjectFileBE *O = dyn_cast<MachOObjectFileBE>(MachO);
+ return O->getSectionFinalSegmentName(DR);
+}
+
static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
const Target *TheTarget = getTarget(Obj);
// getTarget() will have already issued a diagnostic if necessary, so
@@ -255,9 +263,10 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
StringRef SegmentName = "";
- if (const MachOObjectFileBase *MachO = dyn_cast<const MachOObjectFileBase>(Obj)) {
+ if (const MachOObjectFileBase *MachO =
+ dyn_cast<const MachOObjectFileBase>(Obj)) {
DataRefImpl DR = i->getRawDataRefImpl();
- SegmentName = MachO->getSectionFinalSegmentName(DR);
+ SegmentName = getSectionFinalSegmentName(MachO, DR);
}
StringRef name;
if (error(i->getName(name))) break;
@@ -591,9 +600,10 @@ static void PrintSymbolTable(const ObjectFile *o) {
else if (Section == o->end_sections())
outs() << "*UND*";
else {
- if (const MachOObjectFileBase *MachO = dyn_cast<const MachOObjectFileBase>(o)) {
+ if (const MachOObjectFileBase *MachO =
+ dyn_cast<const MachOObjectFileBase>(o)) {
DataRefImpl DR = Section->getRawDataRefImpl();
- StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
+ StringRef SegmentName = getSectionFinalSegmentName(MachO, DR);
outs() << SegmentName << ",";
}
StringRef SectionName;
diff --git a/tools/llvm-readobj/MachODumper.cpp b/tools/llvm-readobj/MachODumper.cpp
index 69f1d63b12..d207eabb52 100644
--- a/tools/llvm-readobj/MachODumper.cpp
+++ b/tools/llvm-readobj/MachODumper.cpp
@@ -27,7 +27,7 @@ namespace {
class MachODumper : public ObjDumper {
public:
- MachODumper(const llvm::object::MachOObjectFileBase *Obj, StreamWriter& Writer)
+ MachODumper(const MachOObjectFileBase *Obj, StreamWriter& Writer)
: ObjDumper(Writer)
, Obj(Obj) { }
@@ -43,7 +43,14 @@ private:
void printRelocation(section_iterator SecI, relocation_iterator RelI);
- const llvm::object::MachOObjectFileBase *Obj;
+ template<support::endianness E>
+ void printRelocation(const MachOObjectFileMiddle<E> *Obj,
+ section_iterator SecI, relocation_iterator RelI);
+
+ template<support::endianness E>
+ void printSections(const MachOObjectFileMiddle<E> *Obj);
+
+ const MachOObjectFileBase *Obj;
};
} // namespace
@@ -157,58 +164,59 @@ namespace {
};
}
+template<class MachOT>
+static void getSection(const MachOObjectFile<MachOT> *Obj,
+ DataRefImpl DRI,
+ MachOSection &Section) {
+ const typename MachOObjectFile<MachOT>::Section *Sect = Obj->getSection(DRI);
+ Section.Address = Sect->Address;
+ Section.Size = Sect->Size;
+ Section.Offset = Sect->Offset;
+ Section.Alignment = Sect->Align;
+ Section.RelocationTableOffset = Sect->RelocationTableOffset;
+ Section.NumRelocationTableEntries = Sect->NumRelocationTableEntries;
+ Section.Flags = Sect->Flags;
+ Section.Reserved1 = Sect->Reserved1;
+ Section.Reserved2 = Sect->Reserved2;
+}
+
static void getSection(const MachOObjectFileBase *Obj,
DataRefImpl DRI,
MachOSection &Section) {
- if (const MachOObjectFile64Le *O = dyn_cast<MachOObjectFile64Le>(Obj)) {
- const MachOObjectFile64Le::Section *Sect = O->getSection(DRI);
-
- Section.Address = Sect->Address;
- Section.Size = Sect->Size;
- Section.Offset = Sect->Offset;
- Section.Alignment = Sect->Align;
- Section.RelocationTableOffset = Sect->RelocationTableOffset;
- Section.NumRelocationTableEntries = Sect->NumRelocationTableEntries;
- Section.Flags = Sect->Flags;
- Section.Reserved1 = Sect->Reserved1;
- Section.Reserved2 = Sect->Reserved2;
- } else {
- const MachOObjectFile32Le *O2 = cast<MachOObjectFile32Le>(Obj);
- const MachOObjectFile32Le::Section *Sect = O2->getSection(DRI);
-
- Section.Address = Sect->Address;
- Section.Size = Sect->Size;
- Section.Offset = Sect->Offset;
- Section.Alignment = Sect->Align;
- Section.RelocationTableOffset = Sect->RelocationTableOffset;
- Section.NumRelocationTableEntries = Sect->NumRelocationTableEntries;
- Section.Flags = Sect->Flags;
- Section.Reserved1 = Sect->Reserved1;
- Section.Reserved2 = Sect->Reserved2;
- }
+ if (const MachOObjectFileLE32 *O = dyn_cast<MachOObjectFileLE32>(Obj))
+ return getSection(O, DRI, Section);
+ if (const MachOObjectFileLE64 *O = dyn_cast<MachOObjectFileLE64>(Obj))
+ return getSection(O, DRI, Section);
+ if (const MachOObjectFileBE32 *O = dyn_cast<MachOObjectFileBE32>(Obj))
+ return getSection(O, DRI, Section);
+ const MachOObjectFileBE64 *O = cast<MachOObjectFileBE64>(Obj);
+ getSection(O, DRI, Section);
+}
+
+template<class MachOT>
+static void getSymbol(const MachOObjectFile<MachOT> *Obj,
+ DataRefImpl DRI,
+ MachOSymbol &Symbol) {
+ const typename MachOObjectFile<MachOT>::SymbolTableEntry *Entry =
+ Obj->getSymbolTableEntry(DRI);
+ Symbol.StringIndex = Entry->StringIndex;
+ Symbol.Type = Entry->Type;
+ Symbol.SectionIndex = Entry->SectionIndex;
+ Symbol.Flags = Entry->Flags;
+ Symbol.Value = Entry->Value;
}
static void getSymbol(const MachOObjectFileBase *Obj,
DataRefImpl DRI,
MachOSymbol &Symbol) {
- if (const MachOObjectFile64Le *O = dyn_cast<MachOObjectFile64Le>(Obj)) {
- const MachOObjectFile64Le::SymbolTableEntry *Entry =
- O->getSymbolTableEntry(DRI);
- Symbol.StringIndex = Entry->StringIndex;
- Symbol.Type = Entry->Type;
- Symbol.SectionIndex = Entry->SectionIndex;
- Symbol.Flags = Entry->Flags;
- Symbol.Value = Entry->Value;
- } else {
- const MachOObjectFile32Le *O2 = cast<MachOObjectFile32Le>(Obj);
- const MachOObjectFile32Le::SymbolTableEntry *Entry =
- O2->getSymbolTableEntry(DRI);
- Symbol.StringIndex = Entry->StringIndex;
- Symbol.Type = Entry->Type;
- Symbol.SectionIndex = Entry->SectionIndex;
- Symbol.Flags = Entry->Flags;
- Symbol.Value = Entry->Value;
- }
+ if (const MachOObjectFileLE32 *O = dyn_cast<MachOObjectFileLE32>(Obj))
+ return getSymbol(O, DRI, Symbol);
+ if (const MachOObjectFileLE64 *O = dyn_cast<MachOObjectFileLE64>(Obj))
+ return getSymbol(O, DRI, Symbol);
+ if (const MachOObjectFileBE32 *O = dyn_cast<MachOObjectFileBE32>(Obj))
+ return getSymbol(O, DRI, Symbol);
+ const MachOObjectFileBE64 *O = cast<MachOObjectFileBE64>(Obj);
+ getSymbol(O, DRI, Symbol);
}
void MachODumper::printFileHeaders() {
@@ -216,6 +224,14 @@ void MachODumper::printFileHeaders() {
}
void MachODumper::printSections() {
+ if (const MachOObjectFileLE *O = dyn_cast<MachOObjectFileLE>(Obj))
+ return printSections(O);
+ const MachOObjectFileBE *O = cast<MachOObjectFileBE>(Obj);
+ return printSections(O);
+}
+
+template<support::endianness E>
+void MachODumper::printSections(const MachOObjectFileMiddle<E> *Obj) {
ListScope Group(W, "Sections");
int SectionIndex = -1;
@@ -328,6 +344,16 @@ void MachODumper::printRelocations() {
void MachODumper::printRelocation(section_iterator SecI,
relocation_iterator RelI) {
+ if (const MachOObjectFileLE *O = dyn_cast<MachOObjectFileLE>(Obj))
+ return printRelocation(O, SecI, RelI);
+ const MachOObjectFileBE *O = cast<MachOObjectFileBE>(Obj);
+ return printRelocation(O, SecI, RelI);
+}
+
+template<support::endianness E>
+void MachODumper::printRelocation(const MachOObjectFileMiddle<E> *Obj,
+ section_iterator SecI,
+ relocation_iterator RelI) {
uint64_t Offset;
SmallString<32> RelocName;
StringRef SymbolName;
@@ -338,14 +364,15 @@ void MachODumper::printRelocation(section_iterator SecI,
if (error(Symbol.getName(SymbolName))) return;
DataRefImpl DR = RelI->getRawDataRefImpl();
- const MachOObjectFileBase::RelocationEntry *RE = Obj->getRelocation(DR);
- bool IsScattered = Obj->isScattered(RE);
+ const typename MachOObjectFileMiddle<E>::RelocationEntry *RE =
+ Obj->getRelocation(DR);
+ bool IsScattered = Obj->isRelocationScattered(RE);
if (opts::ExpandRelocs) {
DictScope Group(W, "Relocation");
W.printHex("Offset", Offset);
- W.printNumber("PCRel", Obj->isPCRel(RE));
- W.printNumber("Length", Obj->getLength(RE));
+ W.printNumber("PCRel", Obj->isRelocationPCRel(RE));
+ W.printNumber("Length", Obj->getRelocationLength(RE));
if (IsScattered)
W.printString("Extern", StringRef("N/A"));
else
@@ -356,8 +383,8 @@ void MachODumper::printRelocation(section_iterator SecI,
} else {
raw_ostream& OS = W.startLine();
OS << W.hex(Offset)
- << " " << Obj->isPCRel(RE)
- << " " << Obj->getLength(RE);
+ << " " << Obj->isRelocationPCRel(RE)
+ << " " << Obj->getRelocationLength(RE);
if (IsScattered)
OS << " n/a";
else
@@ -399,6 +426,7 @@ void MachODumper::printSymbol(symbol_iterator SymI) {
StringRef SectionName;
section_iterator SecI(Obj->end_sections());
if (error(SymI->getSection(SecI)) ||
+ SecI == Obj->end_sections() ||
error(SecI->getName(SectionName)))
SectionName = "";