blob: 459572a57344db7368c0c6f05cb8743e9989ec22 [file] [log] [blame]
Marshall Clowc57b8882012-06-19 18:02:35 +00001//===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "obj2yaml.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000011#include "Error.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000012#include "llvm/Object/Archive.h"
13#include "llvm/Object/COFF.h"
Marshall Clowc57b8882012-06-19 18:02:35 +000014#include "llvm/Support/CommandLine.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000015#include "llvm/Support/InitLLVM.h"
Marshall Clowc57b8882012-06-19 18:02:35 +000016
Chandler Carruth97034bb2013-04-08 08:55:14 +000017using namespace llvm;
Simon Atanasyan111454f2014-05-07 05:18:51 +000018using namespace llvm::object;
Chandler Carruth97034bb2013-04-08 08:55:14 +000019
Rafael Espindola1ad45022014-06-13 03:07:50 +000020static std::error_code dumpObject(const ObjectFile &Obj) {
Simon Atanasyan111454f2014-05-07 05:18:51 +000021 if (Obj.isCOFF())
22 return coff2yaml(outs(), cast<COFFObjectFile>(Obj));
Simon Atanasyanb8236e52014-05-14 05:07:47 +000023 if (Obj.isELF())
24 return elf2yaml(outs(), Obj);
Derek Schuff349a48f2017-03-30 19:44:09 +000025 if (Obj.isWasm())
26 return wasm2yaml(outs(), cast<WasmObjectFile>(Obj));
Simon Atanasyan111454f2014-05-07 05:18:51 +000027
28 return obj2yaml_error::unsupported_obj_file_format;
Chandler Carruth97034bb2013-04-08 08:55:14 +000029}
Marshall Clowc57b8882012-06-19 18:02:35 +000030
Sam Cleggb856c162017-06-16 23:29:54 +000031static Error dumpInput(StringRef File) {
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +000032 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
33 if (!BinaryOrErr)
Sam Cleggb856c162017-06-16 23:29:54 +000034 return BinaryOrErr.takeError();
Simon Atanasyan111454f2014-05-07 05:18:51 +000035
Rafael Espindola548f2b62014-08-19 18:44:46 +000036 Binary &Binary = *BinaryOrErr.get().getBinary();
Chris Bienemane92acf12016-06-24 20:42:28 +000037 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
38 // here with the other binary types.
39 if (Binary.isMachO() || Binary.isMachOUniversalBinary())
Sam Cleggb856c162017-06-16 23:29:54 +000040 return errorCodeToError(macho2yaml(outs(), Binary));
Simon Atanasyan111454f2014-05-07 05:18:51 +000041 // TODO: If this is an archive, then burst it and dump each entry
Rafael Espindola9aa0b5e2014-08-01 14:31:55 +000042 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
Sam Cleggb856c162017-06-16 23:29:54 +000043 return errorCodeToError(dumpObject(*Obj));
Simon Atanasyan111454f2014-05-07 05:18:51 +000044
Sam Cleggb856c162017-06-16 23:29:54 +000045 return Error::success();
46}
47
48static void reportError(StringRef Input, Error Err) {
49 if (Input == "-")
50 Input = "<stdin>";
51 std::string ErrMsg;
52 raw_string_ostream OS(ErrMsg);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +000053 logAllUnhandledErrors(std::move(Err), OS);
Sam Cleggb856c162017-06-16 23:29:54 +000054 OS.flush();
55 errs() << "Error reading file: " << Input << ": " << ErrMsg;
56 errs().flush();
Simon Atanasyan111454f2014-05-07 05:18:51 +000057}
Marshall Clowc57b8882012-06-19 18:02:35 +000058
Chandler Carruth724a7b12013-04-08 08:39:59 +000059cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
60 cl::init("-"));
61
62int main(int argc, char *argv[]) {
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000063 InitLLVM X(argc, argv);
Marshall Clowc57b8882012-06-19 18:02:35 +000064 cl::ParseCommandLineOptions(argc, argv);
Marshall Clowc57b8882012-06-19 18:02:35 +000065
Sam Cleggb856c162017-06-16 23:29:54 +000066 if (Error Err = dumpInput(InputFilename)) {
67 reportError(InputFilename, std::move(Err));
Simon Atanasyan111454f2014-05-07 05:18:51 +000068 return 1;
Marshall Clowc57b8882012-06-19 18:02:35 +000069 }
70
71 return 0;
72}