From da2a2372c6ae715befae7f086afe769dd80814f3 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Sat, 13 Apr 2013 01:45:40 +0000 Subject: 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 --- tools/llvm-readobj/MachODumper.cpp | 132 ++++++++++++++++++++++--------------- 1 file changed, 80 insertions(+), 52 deletions(-) (limited to 'tools/llvm-readobj') diff --git a/tools/llvm-readobj/MachODumper.cpp b/tools/llvm-readobj/MachODumper.cpp index 69f1d63..d207eab 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 + void printRelocation(const MachOObjectFileMiddle *Obj, + section_iterator SecI, relocation_iterator RelI); + + template + void printSections(const MachOObjectFileMiddle *Obj); + + const MachOObjectFileBase *Obj; }; } // namespace @@ -157,58 +164,59 @@ namespace { }; } +template +static void getSection(const MachOObjectFile *Obj, + DataRefImpl DRI, + MachOSection &Section) { + const typename MachOObjectFile::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(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(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(Obj)) + return getSection(O, DRI, Section); + if (const MachOObjectFileLE64 *O = dyn_cast(Obj)) + return getSection(O, DRI, Section); + if (const MachOObjectFileBE32 *O = dyn_cast(Obj)) + return getSection(O, DRI, Section); + const MachOObjectFileBE64 *O = cast(Obj); + getSection(O, DRI, Section); +} + +template +static void getSymbol(const MachOObjectFile *Obj, + DataRefImpl DRI, + MachOSymbol &Symbol) { + const typename MachOObjectFile::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(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(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(Obj)) + return getSymbol(O, DRI, Symbol); + if (const MachOObjectFileLE64 *O = dyn_cast(Obj)) + return getSymbol(O, DRI, Symbol); + if (const MachOObjectFileBE32 *O = dyn_cast(Obj)) + return getSymbol(O, DRI, Symbol); + const MachOObjectFileBE64 *O = cast(Obj); + getSymbol(O, DRI, Symbol); } void MachODumper::printFileHeaders() { @@ -216,6 +224,14 @@ void MachODumper::printFileHeaders() { } void MachODumper::printSections() { + if (const MachOObjectFileLE *O = dyn_cast(Obj)) + return printSections(O); + const MachOObjectFileBE *O = cast(Obj); + return printSections(O); +} + +template +void MachODumper::printSections(const MachOObjectFileMiddle *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(Obj)) + return printRelocation(O, SecI, RelI); + const MachOObjectFileBE *O = cast(Obj); + return printRelocation(O, SecI, RelI); +} + +template +void MachODumper::printRelocation(const MachOObjectFileMiddle *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::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 = ""; -- cgit v1.1