2626#include " llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
2727#include " llvm/Object/Archive.h"
2828#include " llvm/Object/COFFImportFile.h"
29+ #include " llvm/Object/ELFObjectFile.h"
2930#include " llvm/Object/MachOUniversal.h"
3031#include " llvm/Object/ObjectFile.h"
32+ #include " llvm/Object/Wasm.h"
3133#include " llvm/Object/WindowsResource.h"
34+ #include " llvm/Object/XCOFFObjectFile.h"
3235#include " llvm/Support/Casting.h"
3336#include " llvm/Support/CommandLine.h"
3437#include " llvm/Support/DataTypes.h"
3538#include " llvm/Support/Debug.h"
39+ #include " llvm/Support/Errc.h"
3640#include " llvm/Support/FileSystem.h"
3741#include " llvm/Support/FormatVariadic.h"
3842#include " llvm/Support/InitLLVM.h"
@@ -424,48 +428,50 @@ struct ReadObjTypeTableBuilder {
424428static ReadObjTypeTableBuilder CVTypes;
425429
426430// / Creates an format-specific object file dumper.
427- static std::error_code createDumper (const ObjectFile *Obj,
428- ScopedPrinter &Writer,
429- std::unique_ptr<ObjDumper> &Result) {
430- if (!Obj)
431- return readobj_error::unsupported_file_format;
432-
433- if (Obj->isCOFF ())
434- return createCOFFDumper (Obj, Writer, Result);
435- if (Obj->isELF ())
436- return createELFDumper (Obj, Writer, Result);
437- if (Obj->isMachO ())
438- return createMachODumper (Obj, Writer, Result);
439- if (Obj->isWasm ())
440- return createWasmDumper (Obj, Writer, Result);
441- if (Obj->isXCOFF ())
442- return createXCOFFDumper (Obj, Writer, Result);
443-
444- return readobj_error::unsupported_obj_file_format;
431+ static Expected<std::unique_ptr<ObjDumper>>
432+ createDumper (const ObjectFile &Obj, ScopedPrinter &Writer) {
433+ if (const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(&Obj))
434+ return createCOFFDumper (*COFFObj, Writer);
435+
436+ if (const ELFObjectFileBase *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj))
437+ return createELFDumper (*ELFObj, Writer);
438+
439+ if (const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(&Obj))
440+ return createMachODumper (*MachOObj, Writer);
441+
442+ if (const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(&Obj))
443+ return createWasmDumper (*WasmObj, Writer);
444+
445+ if (const XCOFFObjectFile *XObj = dyn_cast<XCOFFObjectFile>(&Obj))
446+ return createXCOFFDumper (*XObj, Writer);
447+
448+ return createStringError (errc::invalid_argument,
449+ " unsupported object file format" );
445450}
446451
447452// / Dumps the specified object file.
448- static void dumpObject (const ObjectFile * Obj, ScopedPrinter &Writer,
453+ static void dumpObject (const ObjectFile & Obj, ScopedPrinter &Writer,
449454 const Archive *A = nullptr ) {
450455 std::string FileStr =
451- A ? Twine (A->getFileName () + " (" + Obj-> getFileName () + " )" ).str ()
452- : Obj-> getFileName ().str ();
456+ A ? Twine (A->getFileName () + " (" + Obj. getFileName () + " )" ).str ()
457+ : Obj. getFileName ().str ();
453458
454- std::unique_ptr<ObjDumper> Dumper;
455- if (std::error_code EC = createDumper (Obj, Writer, Dumper))
456- reportError (errorCodeToError (EC), FileStr);
459+ ObjDumper *Dumper;
460+ Expected<std::unique_ptr<ObjDumper>> DumperOrErr = createDumper (Obj, Writer);
461+ if (!DumperOrErr)
462+ reportError (DumperOrErr.takeError (), FileStr);
463+ Dumper = (*DumperOrErr).get ();
457464
458465 if (opts::Output == opts::LLVM || opts::InputFilenames.size () > 1 || A) {
459466 Writer.startLine () << " \n " ;
460467 Writer.printString (" File" , FileStr);
461468 }
462469 if (opts::Output == opts::LLVM) {
463- Writer.printString (" Format" , Obj->getFileFormatName ());
464- Writer.printString (" Arch" , Triple::getArchTypeName (
465- (llvm::Triple::ArchType)Obj->getArch ()));
470+ Writer.printString (" Format" , Obj.getFileFormatName ());
471+ Writer.printString (" Arch" , Triple::getArchTypeName (Obj.getArch ()));
466472 Writer.printString (
467473 " AddressSize" ,
468- std::string (formatv (" {0}bit" , 8 * Obj-> getBytesInAddress ())));
474+ std::string (formatv (" {0}bit" , 8 * Obj. getBytesInAddress ())));
469475 Dumper->printLoadName ();
470476 }
471477
@@ -490,16 +496,16 @@ static void dumpObject(const ObjectFile *Obj, ScopedPrinter &Writer,
490496 if (opts::Symbols || opts::DynamicSymbols)
491497 Dumper->printSymbols (opts::Symbols, opts::DynamicSymbols);
492498 if (!opts::StringDump.empty ())
493- Dumper->printSectionsAsString (Obj, opts::StringDump);
499+ Dumper->printSectionsAsString (& Obj, opts::StringDump);
494500 if (!opts::HexDump.empty ())
495- Dumper->printSectionsAsHex (Obj, opts::HexDump);
501+ Dumper->printSectionsAsHex (& Obj, opts::HexDump);
496502 if (opts::HashTable)
497503 Dumper->printHashTable ();
498504 if (opts::GnuHashTable)
499- Dumper->printGnuHashTable (Obj);
505+ Dumper->printGnuHashTable (& Obj);
500506 if (opts::VersionInfo)
501507 Dumper->printVersionInfo ();
502- if (Obj-> isELF ()) {
508+ if (Obj. isELF ()) {
503509 if (opts::DependentLibraries)
504510 Dumper->printDependentLibs ();
505511 if (opts::ELFLinkerOptions)
@@ -517,7 +523,7 @@ static void dumpObject(const ObjectFile *Obj, ScopedPrinter &Writer,
517523 if (opts::Notes)
518524 Dumper->printNotes ();
519525 }
520- if (Obj-> isCOFF ()) {
526+ if (Obj. isCOFF ()) {
521527 if (opts::COFFImports)
522528 Dumper->printCOFFImports ();
523529 if (opts::COFFExports)
@@ -543,7 +549,7 @@ static void dumpObject(const ObjectFile *Obj, ScopedPrinter &Writer,
543549 CVTypes.GlobalIDTable , CVTypes.GlobalTypeTable ,
544550 opts::CodeViewEnableGHash);
545551 }
546- if (Obj-> isMachO ()) {
552+ if (Obj. isMachO ()) {
547553 if (opts::MachODataInCode)
548554 Dumper->printMachODataInCode ();
549555 if (opts::MachOIndirectSymbols)
@@ -574,7 +580,7 @@ static void dumpArchive(const Archive *Arc, ScopedPrinter &Writer) {
574580 continue ;
575581 }
576582 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get ()))
577- dumpObject (Obj, Writer, Arc);
583+ dumpObject (* Obj, Writer, Arc);
578584 else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(&*ChildOrErr.get ()))
579585 dumpCOFFImportFile (Imp, Writer);
580586 else
@@ -591,7 +597,7 @@ static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary,
591597 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects ()) {
592598 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile ();
593599 if (ObjOrErr)
594- dumpObject (& *ObjOrErr.get (), Writer);
600+ dumpObject (*ObjOrErr.get (), Writer);
595601 else if (auto E = isNotObjectErrorInvalidFileType (ObjOrErr.takeError ()))
596602 reportError (ObjOrErr.takeError (), UBinary->getFileName ());
597603 else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive ())
@@ -622,7 +628,7 @@ static void dumpInput(StringRef File, ScopedPrinter &Writer) {
622628 dyn_cast<MachOUniversalBinary>(&Binary))
623629 dumpMachOUniversalBinary (UBinary, Writer);
624630 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
625- dumpObject (Obj, Writer);
631+ dumpObject (* Obj, Writer);
626632 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
627633 dumpCOFFImportFile (Import, Writer);
628634 else if (WindowsResource *WinRes = dyn_cast<WindowsResource>(&Binary))
0 commit comments