2012-06-19 20:02:35 +02:00
|
|
|
//===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-06-19 20:02:35 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "obj2yaml.h"
|
2020-10-20 14:12:28 +02:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2012-12-04 11:37:14 +01:00
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
2019-04-02 13:58:37 +02:00
|
|
|
#include "llvm/Object/Minidump.h"
|
2012-06-19 20:02:35 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2020-08-25 13:55:25 +02:00
|
|
|
#include "llvm/Support/Errc.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2012-06-19 20:02:35 +02:00
|
|
|
|
2013-04-08 10:55:14 +02:00
|
|
|
using namespace llvm;
|
2014-05-07 07:18:51 +02:00
|
|
|
using namespace llvm::object;
|
2013-04-08 10:55:14 +02:00
|
|
|
|
2019-07-15 13:53:39 +02:00
|
|
|
static Error dumpObject(const ObjectFile &Obj) {
|
2014-05-07 07:18:51 +02:00
|
|
|
if (Obj.isCOFF())
|
2019-07-15 13:53:39 +02:00
|
|
|
return errorCodeToError(coff2yaml(outs(), cast<COFFObjectFile>(Obj)));
|
[XCOFF] Add functionality for parsing AIX XCOFF object file headers
Summary:
1. Add functionality for parsing AIX XCOFF object files headers.
2. Only support 32-bit AIX XCOFF object files in this patch.
3. Print out the AIX XCOFF object file header in YAML format.
Reviewers: sfertile, hubert.reinterpretcast, jasonliu, mstorsjo, zturner, rnk
Reviewed By: sfertile, hubert.reinterpretcast
Subscribers: jsji, mgorny, hiraditya, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59419
Patch by Digger Lin
llvm-svn: 357663
2019-04-04 02:53:21 +02:00
|
|
|
|
|
|
|
if (Obj.isXCOFF())
|
2019-07-15 13:53:39 +02:00
|
|
|
return errorCodeToError(xcoff2yaml(outs(), cast<XCOFFObjectFile>(Obj)));
|
[XCOFF] Add functionality for parsing AIX XCOFF object file headers
Summary:
1. Add functionality for parsing AIX XCOFF object files headers.
2. Only support 32-bit AIX XCOFF object files in this patch.
3. Print out the AIX XCOFF object file header in YAML format.
Reviewers: sfertile, hubert.reinterpretcast, jasonliu, mstorsjo, zturner, rnk
Reviewed By: sfertile, hubert.reinterpretcast
Subscribers: jsji, mgorny, hiraditya, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59419
Patch by Digger Lin
llvm-svn: 357663
2019-04-04 02:53:21 +02:00
|
|
|
|
2014-05-14 07:07:47 +02:00
|
|
|
if (Obj.isELF())
|
|
|
|
return elf2yaml(outs(), Obj);
|
2019-07-15 13:53:39 +02:00
|
|
|
|
2017-03-30 21:44:09 +02:00
|
|
|
if (Obj.isWasm())
|
2019-07-15 13:53:39 +02:00
|
|
|
return errorCodeToError(wasm2yaml(outs(), cast<WasmObjectFile>(Obj)));
|
2014-05-07 07:18:51 +02:00
|
|
|
|
2020-08-25 13:55:25 +02:00
|
|
|
llvm_unreachable("unexpected object file format");
|
2013-04-08 10:55:14 +02:00
|
|
|
}
|
2012-06-19 20:02:35 +02:00
|
|
|
|
2017-06-17 01:29:54 +02:00
|
|
|
static Error dumpInput(StringRef File) {
|
2020-10-20 14:12:28 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 14:47:25 +01:00
|
|
|
MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
|
2020-10-20 14:12:28 +02:00
|
|
|
/*RequiresNullTerminator=*/false);
|
|
|
|
if (std::error_code EC = FileOrErr.getError())
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
|
|
|
|
MemoryBufferRef MemBuf = Buffer->getMemBufferRef();
|
|
|
|
if (file_magic::archive == identify_magic(MemBuf.getBuffer()))
|
|
|
|
return archive2yaml(outs(), MemBuf);
|
2014-05-07 07:18:51 +02:00
|
|
|
|
2020-10-20 14:12:28 +02:00
|
|
|
Expected<std::unique_ptr<Binary>> BinOrErr =
|
|
|
|
createBinary(MemBuf, /*Context=*/nullptr);
|
|
|
|
if (!BinOrErr)
|
|
|
|
return BinOrErr.takeError();
|
|
|
|
|
|
|
|
Binary &Binary = *BinOrErr->get();
|
2016-06-24 22:42:28 +02:00
|
|
|
// Universal MachO is not a subclass of ObjectFile, so it needs to be handled
|
|
|
|
// here with the other binary types.
|
|
|
|
if (Binary.isMachO() || Binary.isMachOUniversalBinary())
|
2019-12-25 08:05:56 +01:00
|
|
|
return macho2yaml(outs(), Binary);
|
2014-08-01 16:31:55 +02:00
|
|
|
if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
|
2019-07-15 13:53:39 +02:00
|
|
|
return dumpObject(*Obj);
|
2019-04-02 13:58:37 +02:00
|
|
|
if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary))
|
|
|
|
return minidump2yaml(outs(), *Minidump);
|
2014-05-07 07:18:51 +02:00
|
|
|
|
2017-06-17 01:29:54 +02:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportError(StringRef Input, Error Err) {
|
|
|
|
if (Input == "-")
|
|
|
|
Input = "<stdin>";
|
|
|
|
std::string ErrMsg;
|
|
|
|
raw_string_ostream OS(ErrMsg);
|
2018-11-11 02:46:03 +01:00
|
|
|
logAllUnhandledErrors(std::move(Err), OS);
|
2017-06-17 01:29:54 +02:00
|
|
|
OS.flush();
|
|
|
|
errs() << "Error reading file: " << Input << ": " << ErrMsg;
|
|
|
|
errs().flush();
|
2014-05-07 07:18:51 +02:00
|
|
|
}
|
2013-04-08 10:39:59 +02:00
|
|
|
|
|
|
|
cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
|
|
|
|
cl::init("-"));
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2018-04-13 20:26:06 +02:00
|
|
|
InitLLVM X(argc, argv);
|
2012-06-19 20:02:35 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv);
|
|
|
|
|
2017-06-17 01:29:54 +02:00
|
|
|
if (Error Err = dumpInput(InputFilename)) {
|
|
|
|
reportError(InputFilename, std::move(Err));
|
2014-05-07 07:18:51 +02:00
|
|
|
return 1;
|
2012-06-19 20:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|