15 changes: 10 additions & 5 deletions llvm/tools/obj2yaml/obj2yaml.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
//===------ utils/obj2yaml.cpp - obj2yaml conversion tool -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -18,6 +18,14 @@
using namespace llvm;
using namespace llvm::object;

static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
static cl::bits<RawSegments> RawSegment(
"raw-segment",
cl::desc("Mach-O: dump the raw contents of the listed segments instead of "
"parsing them:"),
cl::values(clEnumVal(data, "__DATA"), clEnumVal(linkedit, "__LINKEDIT")));

static Error dumpObject(const ObjectFile &Obj) {
if (Obj.isCOFF())
return errorCodeToError(coff2yaml(outs(), cast<COFFObjectFile>(Obj)));
Expand Down Expand Up @@ -54,7 +62,7 @@ static Error dumpInput(StringRef File) {
// 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())
return macho2yaml(outs(), Binary);
return macho2yaml(outs(), Binary, RawSegment.getBits());
if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
return dumpObject(*Obj);
if (MinidumpFile *Minidump = dyn_cast<MinidumpFile>(&Binary))
Expand All @@ -74,9 +82,6 @@ static void reportError(StringRef Input, Error Err) {
errs().flush();
}

cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"),
cl::init("-"));

int main(int argc, char *argv[]) {
InitLLVM X(argc, argv);
cl::ParseCommandLineOptions(argc, argv);
Expand Down
5 changes: 3 additions & 2 deletions llvm/tools/obj2yaml/obj2yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
#include "llvm/Support/MemoryBufferRef.h"
#include <system_error>

enum RawSegments : unsigned { none = 0, data = 1, linkedit = 1 << 1 };
std::error_code coff2yaml(llvm::raw_ostream &Out,
const llvm::object::COFFObjectFile &Obj);
llvm::Error elf2yaml(llvm::raw_ostream &Out,
const llvm::object::ObjectFile &Obj);
llvm::Error macho2yaml(llvm::raw_ostream &Out,
const llvm::object::Binary &Obj);
llvm::Error macho2yaml(llvm::raw_ostream &Out, const llvm::object::Binary &Obj,
unsigned RawSegments);
llvm::Error minidump2yaml(llvm::raw_ostream &Out,
const llvm::object::MinidumpFile &Obj);
llvm::Error xcoff2yaml(llvm::raw_ostream &Out,
Expand Down