[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / tools / obj2yaml / obj2yaml.cpp
blob9c7a3385850dbb6dde401a4b0749ba4c29e68f66
1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "obj2yaml.h"
10 #include "llvm/BinaryFormat/Magic.h"
11 #include "llvm/Object/Archive.h"
12 #include "llvm/Object/COFF.h"
13 #include "llvm/Object/Minidump.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/Errc.h"
16 #include "llvm/Support/InitLLVM.h"
18 using namespace llvm;
19 using namespace llvm::object;
21 static cl::opt<std::string>
22 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
23 static cl::bits<RawSegments> RawSegment(
24 "raw-segment",
25 cl::desc("Mach-O: dump the raw contents of the listed segments instead of "
26 "parsing them:"),
27 cl::values(clEnumVal(data, "__DATA"), clEnumVal(linkedit, "__LINKEDIT")));
29 static Error dumpObject(const ObjectFile &Obj) {
30 if (Obj.isCOFF())
31 return errorCodeToError(coff2yaml(outs(), cast<COFFObjectFile>(Obj)));
33 if (Obj.isXCOFF())
34 return xcoff2yaml(outs(), cast<XCOFFObjectFile>(Obj));
36 if (Obj.isELF())
37 return elf2yaml(outs(), Obj);
39 if (Obj.isWasm())
40 return errorCodeToError(wasm2yaml(outs(), cast<WasmObjectFile>(Obj)));
42 llvm_unreachable("unexpected object file format");
45 static Error dumpInput(StringRef File) {
46 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
47 MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
48 /*RequiresNullTerminator=*/false);
49 if (std::error_code EC = FileOrErr.getError())
50 return errorCodeToError(EC);
51 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
52 MemoryBufferRef MemBuf = Buffer->getMemBufferRef();
53 if (file_magic::archive == identify_magic(MemBuf.getBuffer()))
54 return archive2yaml(outs(), MemBuf);
56 Expected<std::unique_ptr<Binary>> BinOrErr =
57 createBinary(MemBuf, /*Context=*/nullptr);
58 if (!BinOrErr)
59 return BinOrErr.takeError();
61 Binary &Binary = *BinOrErr->get();
62 // Universal MachO is not a subclass of ObjectFile, so it needs to be handled
63 // here with the other binary types.
64 if (Binary.isMachO() || Binary.isMachOUniversalBinary())
65 return macho2yaml(outs(), Binary, RawSegment.getBits());
66 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
67 return dumpObject(*Obj);
68 if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary))
69 return minidump2yaml(outs(), *Minidump);
71 return Error::success();
74 static void reportError(StringRef Input, Error Err) {
75 if (Input == "-")
76 Input = "<stdin>";
77 std::string ErrMsg;
78 raw_string_ostream OS(ErrMsg);
79 logAllUnhandledErrors(std::move(Err), OS);
80 OS.flush();
81 errs() << "Error reading file: " << Input << ": " << ErrMsg;
82 errs().flush();
85 int main(int argc, char *argv[]) {
86 InitLLVM X(argc, argv);
87 cl::ParseCommandLineOptions(argc, argv);
89 if (Error Err = dumpInput(InputFilename)) {
90 reportError(InputFilename, std::move(Err));
91 return 1;
94 return 0;