comparison tools/obj2yaml/obj2yaml.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===// 1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 8
9 #include "obj2yaml.h"
10 #include "Error.h" 10 #include "Error.h"
11 #include "obj2yaml.h"
12 #include "llvm/Object/Archive.h" 11 #include "llvm/Object/Archive.h"
13 #include "llvm/Object/COFF.h" 12 #include "llvm/Object/COFF.h"
13 #include "llvm/Object/Minidump.h"
14 #include "llvm/Support/CommandLine.h" 14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/ManagedStatic.h" 15 #include "llvm/Support/InitLLVM.h"
16 #include "llvm/Support/PrettyStackTrace.h"
17 #include "llvm/Support/Signals.h"
18 16
19 using namespace llvm; 17 using namespace llvm;
20 using namespace llvm::object; 18 using namespace llvm::object;
21 19
22 static std::error_code dumpObject(const ObjectFile &Obj) { 20 static Error dumpObject(const ObjectFile &Obj) {
23 if (Obj.isCOFF()) 21 if (Obj.isCOFF())
24 return coff2yaml(outs(), cast<COFFObjectFile>(Obj)); 22 return errorCodeToError(coff2yaml(outs(), cast<COFFObjectFile>(Obj)));
23
24 if (Obj.isXCOFF())
25 return errorCodeToError(xcoff2yaml(outs(), cast<XCOFFObjectFile>(Obj)));
26
25 if (Obj.isELF()) 27 if (Obj.isELF())
26 return elf2yaml(outs(), Obj); 28 return elf2yaml(outs(), Obj);
29
27 if (Obj.isWasm()) 30 if (Obj.isWasm())
28 return wasm2yaml(outs(), cast<WasmObjectFile>(Obj)); 31 return errorCodeToError(wasm2yaml(outs(), cast<WasmObjectFile>(Obj)));
29 32
30 return obj2yaml_error::unsupported_obj_file_format; 33 return errorCodeToError(obj2yaml_error::unsupported_obj_file_format);
31 } 34 }
32 35
33 static Error dumpInput(StringRef File) { 36 static Error dumpInput(StringRef File) {
34 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File); 37 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
35 if (!BinaryOrErr) 38 if (!BinaryOrErr)
40 // here with the other binary types. 43 // here with the other binary types.
41 if (Binary.isMachO() || Binary.isMachOUniversalBinary()) 44 if (Binary.isMachO() || Binary.isMachOUniversalBinary())
42 return errorCodeToError(macho2yaml(outs(), Binary)); 45 return errorCodeToError(macho2yaml(outs(), Binary));
43 // TODO: If this is an archive, then burst it and dump each entry 46 // TODO: If this is an archive, then burst it and dump each entry
44 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) 47 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
45 return errorCodeToError(dumpObject(*Obj)); 48 return dumpObject(*Obj);
49 if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary))
50 return minidump2yaml(outs(), *Minidump);
46 51
47 return Error::success(); 52 return Error::success();
48 } 53 }
49 54
50 static void reportError(StringRef Input, Error Err) { 55 static void reportError(StringRef Input, Error Err) {
51 if (Input == "-") 56 if (Input == "-")
52 Input = "<stdin>"; 57 Input = "<stdin>";
53 std::string ErrMsg; 58 std::string ErrMsg;
54 raw_string_ostream OS(ErrMsg); 59 raw_string_ostream OS(ErrMsg);
55 logAllUnhandledErrors(std::move(Err), OS, ""); 60 logAllUnhandledErrors(std::move(Err), OS);
56 OS.flush(); 61 OS.flush();
57 errs() << "Error reading file: " << Input << ": " << ErrMsg; 62 errs() << "Error reading file: " << Input << ": " << ErrMsg;
58 errs().flush(); 63 errs().flush();
59 } 64 }
60 65
61 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"), 66 cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
62 cl::init("-")); 67 cl::init("-"));
63 68
64 int main(int argc, char *argv[]) { 69 int main(int argc, char *argv[]) {
70 InitLLVM X(argc, argv);
65 cl::ParseCommandLineOptions(argc, argv); 71 cl::ParseCommandLineOptions(argc, argv);
66 sys::PrintStackTraceOnErrorSignal(argv[0]);
67 PrettyStackTraceProgram X(argc, argv);
68 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
69 72
70 if (Error Err = dumpInput(InputFilename)) { 73 if (Error Err = dumpInput(InputFilename)) {
71 reportError(InputFilename, std::move(Err)); 74 reportError(InputFilename, std::move(Err));
72 return 1; 75 return 1;
73 } 76 }