diff --git a/llvm/tools/llvm-readobj/COFFDumper.cpp b/llvm/tools/llvm-readobj/COFFDumper.cpp --- a/llvm/tools/llvm-readobj/COFFDumper.cpp +++ b/llvm/tools/llvm-readobj/COFFDumper.cpp @@ -239,15 +239,9 @@ namespace llvm { -std::error_code createCOFFDumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result) { - const COFFObjectFile *COFFObj = dyn_cast(Obj); - if (!COFFObj) - return readobj_error::unsupported_obj_file_format; - - Result.reset(new COFFDumper(COFFObj, Writer)); - return readobj_error::success; +std::unique_ptr createCOFFDumper(const object::COFFObjectFile &Obj, + ScopedPrinter &Writer) { + return std::make_unique(&Obj, Writer); } } // namespace llvm diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -987,33 +987,27 @@ namespace llvm { template -static std::error_code createELFDumper(const ELFObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result) { - Result.reset(new ELFDumper(Obj, Writer)); - return readobj_error::success; +static std::unique_ptr createELFDumper(const ELFObjectFile &Obj, + ScopedPrinter &Writer) { + return std::make_unique>(&Obj, Writer); } -std::error_code createELFDumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result) { +std::unique_ptr createELFDumper(const object::ELFObjectFileBase &Obj, + ScopedPrinter &Writer) { // Little-endian 32-bit - if (const ELF32LEObjectFile *ELFObj = dyn_cast(Obj)) - return createELFDumper(ELFObj, Writer, Result); + if (const ELF32LEObjectFile *ELFObj = dyn_cast(&Obj)) + return createELFDumper(*ELFObj, Writer); // Big-endian 32-bit - if (const ELF32BEObjectFile *ELFObj = dyn_cast(Obj)) - return createELFDumper(ELFObj, Writer, Result); + if (const ELF32BEObjectFile *ELFObj = dyn_cast(&Obj)) + return createELFDumper(*ELFObj, Writer); // Little-endian 64-bit - if (const ELF64LEObjectFile *ELFObj = dyn_cast(Obj)) - return createELFDumper(ELFObj, Writer, Result); + if (const ELF64LEObjectFile *ELFObj = dyn_cast(&Obj)) + return createELFDumper(*ELFObj, Writer); // Big-endian 64-bit - if (const ELF64BEObjectFile *ELFObj = dyn_cast(Obj)) - return createELFDumper(ELFObj, Writer, Result); - - return readobj_error::unsupported_obj_file_format; + return createELFDumper(*cast(&Obj), Writer); } } // end namespace llvm diff --git a/llvm/tools/llvm-readobj/MachODumper.cpp b/llvm/tools/llvm-readobj/MachODumper.cpp --- a/llvm/tools/llvm-readobj/MachODumper.cpp +++ b/llvm/tools/llvm-readobj/MachODumper.cpp @@ -68,15 +68,9 @@ namespace llvm { -std::error_code createMachODumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result) { - const MachOObjectFile *MachOObj = dyn_cast(Obj); - if (!MachOObj) - return readobj_error::unsupported_obj_file_format; - - Result.reset(new MachODumper(MachOObj, Writer)); - return readobj_error::success; +std::unique_ptr createMachODumper(const object::MachOObjectFile &Obj, + ScopedPrinter &Writer) { + return std::make_unique(&Obj, Writer); } } // namespace llvm diff --git a/llvm/tools/llvm-readobj/ObjDumper.h b/llvm/tools/llvm-readobj/ObjDumper.h --- a/llvm/tools/llvm-readobj/ObjDumper.h +++ b/llvm/tools/llvm-readobj/ObjDumper.h @@ -20,6 +20,8 @@ namespace object { class COFFImportFile; class ObjectFile; +class XCOFFObjectFile; +class ELFObjectFileBase; } namespace codeview { class GlobalTypeTableBuilder; @@ -113,25 +115,20 @@ virtual void printSectionMapping() {} }; -std::error_code createCOFFDumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result); +std::unique_ptr createCOFFDumper(const object::COFFObjectFile &Obj, + ScopedPrinter &Writer); -std::error_code createELFDumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result); +std::unique_ptr createELFDumper(const object::ELFObjectFileBase &Obj, + ScopedPrinter &Writer); -std::error_code createMachODumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result); +std::unique_ptr createMachODumper(const object::MachOObjectFile &Obj, + ScopedPrinter &Writer); -std::error_code createWasmDumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result); +std::unique_ptr createWasmDumper(const object::WasmObjectFile &Obj, + ScopedPrinter &Writer); -std::error_code createXCOFFDumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result); +std::unique_ptr createXCOFFDumper(const object::XCOFFObjectFile &Obj, + ScopedPrinter &Writer); void dumpCOFFImportFile(const object::COFFImportFile *File, ScopedPrinter &Writer); diff --git a/llvm/tools/llvm-readobj/WasmDumper.cpp b/llvm/tools/llvm-readobj/WasmDumper.cpp --- a/llvm/tools/llvm-readobj/WasmDumper.cpp +++ b/llvm/tools/llvm-readobj/WasmDumper.cpp @@ -241,14 +241,9 @@ namespace llvm { -std::error_code createWasmDumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result) { - const auto *WasmObj = dyn_cast(Obj); - assert(WasmObj && "createWasmDumper called with non-wasm object"); - - Result.reset(new WasmDumper(WasmObj, Writer)); - return readobj_error::success; +std::unique_ptr createWasmDumper(const object::WasmObjectFile &Obj, + ScopedPrinter &Writer) { + return std::make_unique(&Obj, Writer); } } // namespace llvm diff --git a/llvm/tools/llvm-readobj/XCOFFDumper.cpp b/llvm/tools/llvm-readobj/XCOFFDumper.cpp --- a/llvm/tools/llvm-readobj/XCOFFDumper.cpp +++ b/llvm/tools/llvm-readobj/XCOFFDumper.cpp @@ -515,14 +515,8 @@ } namespace llvm { -std::error_code createXCOFFDumper(const object::ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result) { - const XCOFFObjectFile *XObj = dyn_cast(Obj); - if (!XObj) - return readobj_error::unsupported_obj_file_format; - - Result.reset(new XCOFFDumper(*XObj, Writer)); - return readobj_error::success; +std::unique_ptr +createXCOFFDumper(const object::XCOFFObjectFile &XObj, ScopedPrinter &Writer) { + return std::make_unique(XObj, Writer); } } // namespace llvm diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp --- a/llvm/tools/llvm-readobj/llvm-readobj.cpp +++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp @@ -26,13 +26,17 @@ #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h" #include "llvm/Object/Archive.h" #include "llvm/Object/COFFImportFile.h" +#include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/MachOUniversal.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/Object/Wasm.h" #include "llvm/Object/WindowsResource.h" +#include "llvm/Object/XCOFFObjectFile.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/InitLLVM.h" @@ -424,48 +428,50 @@ static ReadObjTypeTableBuilder CVTypes; /// Creates an format-specific object file dumper. -static std::error_code createDumper(const ObjectFile *Obj, - ScopedPrinter &Writer, - std::unique_ptr &Result) { - if (!Obj) - return readobj_error::unsupported_file_format; - - if (Obj->isCOFF()) - return createCOFFDumper(Obj, Writer, Result); - if (Obj->isELF()) - return createELFDumper(Obj, Writer, Result); - if (Obj->isMachO()) - return createMachODumper(Obj, Writer, Result); - if (Obj->isWasm()) - return createWasmDumper(Obj, Writer, Result); - if (Obj->isXCOFF()) - return createXCOFFDumper(Obj, Writer, Result); - - return readobj_error::unsupported_obj_file_format; +static Expected> +createDumper(const ObjectFile &Obj, ScopedPrinter &Writer) { + if (const COFFObjectFile *COFFObj = dyn_cast(&Obj)) + return createCOFFDumper(*COFFObj, Writer); + + if (const ELFObjectFileBase *ELFObj = dyn_cast(&Obj)) + return createELFDumper(*ELFObj, Writer); + + if (const MachOObjectFile *MachOObj = dyn_cast(&Obj)) + return createMachODumper(*MachOObj, Writer); + + if (const WasmObjectFile *WasmObj = dyn_cast(&Obj)) + return createWasmDumper(*WasmObj, Writer); + + if (const XCOFFObjectFile *XObj = dyn_cast(&Obj)) + return createXCOFFDumper(*XObj, Writer); + + return createStringError(errc::invalid_argument, + "unsupported object file format"); } /// Dumps the specified object file. -static void dumpObject(const ObjectFile *Obj, ScopedPrinter &Writer, +static void dumpObject(const ObjectFile &Obj, ScopedPrinter &Writer, const Archive *A = nullptr) { std::string FileStr = - A ? Twine(A->getFileName() + "(" + Obj->getFileName() + ")").str() - : Obj->getFileName().str(); + A ? Twine(A->getFileName() + "(" + Obj.getFileName() + ")").str() + : Obj.getFileName().str(); - std::unique_ptr Dumper; - if (std::error_code EC = createDumper(Obj, Writer, Dumper)) - reportError(errorCodeToError(EC), FileStr); + ObjDumper *Dumper; + Expected> DumperOrErr = createDumper(Obj, Writer); + if (!DumperOrErr) + reportError(DumperOrErr.takeError(), FileStr); + Dumper = (*DumperOrErr).get(); if (opts::Output == opts::LLVM || opts::InputFilenames.size() > 1 || A) { Writer.startLine() << "\n"; Writer.printString("File", FileStr); } if (opts::Output == opts::LLVM) { - Writer.printString("Format", Obj->getFileFormatName()); - Writer.printString("Arch", Triple::getArchTypeName( - (llvm::Triple::ArchType)Obj->getArch())); + Writer.printString("Format", Obj.getFileFormatName()); + Writer.printString("Arch", Triple::getArchTypeName(Obj.getArch())); Writer.printString( "AddressSize", - std::string(formatv("{0}bit", 8 * Obj->getBytesInAddress()))); + std::string(formatv("{0}bit", 8 * Obj.getBytesInAddress()))); Dumper->printLoadName(); } @@ -490,16 +496,16 @@ if (opts::Symbols || opts::DynamicSymbols) Dumper->printSymbols(opts::Symbols, opts::DynamicSymbols); if (!opts::StringDump.empty()) - Dumper->printSectionsAsString(Obj, opts::StringDump); + Dumper->printSectionsAsString(&Obj, opts::StringDump); if (!opts::HexDump.empty()) - Dumper->printSectionsAsHex(Obj, opts::HexDump); + Dumper->printSectionsAsHex(&Obj, opts::HexDump); if (opts::HashTable) Dumper->printHashTable(); if (opts::GnuHashTable) - Dumper->printGnuHashTable(Obj); + Dumper->printGnuHashTable(&Obj); if (opts::VersionInfo) Dumper->printVersionInfo(); - if (Obj->isELF()) { + if (Obj.isELF()) { if (opts::DependentLibraries) Dumper->printDependentLibs(); if (opts::ELFLinkerOptions) @@ -517,7 +523,7 @@ if (opts::Notes) Dumper->printNotes(); } - if (Obj->isCOFF()) { + if (Obj.isCOFF()) { if (opts::COFFImports) Dumper->printCOFFImports(); if (opts::COFFExports) @@ -543,7 +549,7 @@ CVTypes.GlobalIDTable, CVTypes.GlobalTypeTable, opts::CodeViewEnableGHash); } - if (Obj->isMachO()) { + if (Obj.isMachO()) { if (opts::MachODataInCode) Dumper->printMachODataInCode(); if (opts::MachOIndirectSymbols) @@ -574,7 +580,7 @@ continue; } if (ObjectFile *Obj = dyn_cast(&*ChildOrErr.get())) - dumpObject(Obj, Writer, Arc); + dumpObject(*Obj, Writer, Arc); else if (COFFImportFile *Imp = dyn_cast(&*ChildOrErr.get())) dumpCOFFImportFile(Imp, Writer); else @@ -591,7 +597,7 @@ for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) { Expected> ObjOrErr = Obj.getAsObjectFile(); if (ObjOrErr) - dumpObject(&*ObjOrErr.get(), Writer); + dumpObject(*ObjOrErr.get(), Writer); else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) reportError(ObjOrErr.takeError(), UBinary->getFileName()); else if (Expected> AOrErr = Obj.getAsArchive()) @@ -622,7 +628,7 @@ dyn_cast(&Binary)) dumpMachOUniversalBinary(UBinary, Writer); else if (ObjectFile *Obj = dyn_cast(&Binary)) - dumpObject(Obj, Writer); + dumpObject(*Obj, Writer); else if (COFFImportFile *Import = dyn_cast(&Binary)) dumpCOFFImportFile(Import, Writer); else if (WindowsResource *WinRes = dyn_cast(&Binary))