Thread Expected<...> up from createMachOObjectFile() to allow llvm-objdump to produce a real error message

Produce the first specific error message for a malformed Mach-O file describing
the problem instead of the generic message for object_error::parse_failed of
"Invalid data was encountered while parsing the file”.  Many more good error
messages will follow after this first one.

This is built on Lang Hames’ great work of adding the ’Error' class for
structured error handling and threading Error through MachOObjectFile
construction.  And making createMachOObjectFile return Expected<...> .

So to to get the error to the llvm-obdump tool, I changed the stack of
these methods to also return Expected<...> :

  object::ObjectFile::createObjectFile()
  object::SymbolicFile::createSymbolicFile()
  object::createBinary()

Then finally in ParseInputMachO() in MachODump.cpp the error can
be reported and the specific error message can be printed in llvm-objdump
and can be seen in the existing test case for the existing malformed binary
but with the updated error message.

Converting these interfaces to Expected<> from ErrorOr<> does involve
touching a number of places. To contain the changes for now use of
errorToErrorCode() and errorOrToExpected() are used where the callers
are yet to be converted.

Also there some were bugs in the existing code that did not deal with the
old ErrorOr<> return values.  So now with Expected<> since they must be
checked and the error handled, I added a TODO and a comment:
“// TODO: Actually report errors helpfully” and a call something like
consumeError(ObjOrErr.takeError()) so the buggy code will not crash
since needed to deal with the Error.

Note there is one fix also needed to lld/COFF/InputFiles.cpp that goes along
with this that I will commit right after this.  So expect lld not to built
after this commit and before the next one.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265606 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Kevin Enderby 2016-04-06 22:14:09 +00:00
parent 243a05e2ed
commit c6bf9be16d
35 changed files with 163 additions and 95 deletions

View File

@ -42,12 +42,13 @@ public:
PM.run(M);
std::unique_ptr<MemoryBuffer> ObjBuffer(
new ObjectMemoryBuffer(std::move(ObjBufferSV)));
ErrorOr<std::unique_ptr<object::ObjectFile>> Obj =
Expected<std::unique_ptr<object::ObjectFile>> Obj =
object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
// TODO: Actually report errors helpfully.
typedef object::OwningBinary<object::ObjectFile> OwningObj;
if (Obj)
return OwningObj(std::move(*Obj), std::move(ObjBuffer));
// TODO: Actually report errors helpfully.
consumeError(Obj.takeError());
return OwningObj(nullptr, nullptr);
}

View File

@ -124,10 +124,13 @@ private:
if (!ObjBuffer)
return object::OwningBinary<object::ObjectFile>();
ErrorOr<std::unique_ptr<object::ObjectFile>> Obj =
Expected<std::unique_ptr<object::ObjectFile>> Obj =
object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
if (!Obj)
if (!Obj) {
// TODO: Actually report errors helpfully.
consumeError(Obj.takeError());
return object::OwningBinary<object::ObjectFile>();
}
return object::OwningBinary<object::ObjectFile>(std::move(*Obj),
std::move(ObjBuffer));

View File

@ -134,8 +134,8 @@ public:
/// @brief Create a Binary from Source, autodetecting the file type.
///
/// @param Source The data to create the Binary from.
ErrorOr<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
LLVMContext *Context = nullptr);
Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
LLVMContext *Context = nullptr);
template <typename T> class OwningBinary {
std::unique_ptr<T> Bin;
@ -185,7 +185,7 @@ template <typename T> const T* OwningBinary<T>::getBinary() const {
return Bin.get();
}
ErrorOr<OwningBinary<Binary>> createBinary(StringRef Path);
Expected<OwningBinary<Binary>> createBinary(StringRef Path);
}
}

View File

@ -275,12 +275,12 @@ public:
/// @param ObjectPath The path to the object file. ObjectPath.isObject must
/// return true.
/// @brief Create ObjectFile from path.
static ErrorOr<OwningBinary<ObjectFile>>
static Expected<OwningBinary<ObjectFile>>
createObjectFile(StringRef ObjectPath);
static ErrorOr<std::unique_ptr<ObjectFile>>
static Expected<std::unique_ptr<ObjectFile>>
createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type);
static ErrorOr<std::unique_ptr<ObjectFile>>
static Expected<std::unique_ptr<ObjectFile>>
createObjectFile(MemoryBufferRef Object) {
return createObjectFile(Object, sys::fs::file_magic::unknown);
}

View File

@ -153,15 +153,15 @@ public:
}
// construction aux.
static ErrorOr<std::unique_ptr<SymbolicFile>>
static Expected<std::unique_ptr<SymbolicFile>>
createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
LLVMContext *Context);
static ErrorOr<std::unique_ptr<SymbolicFile>>
static Expected<std::unique_ptr<SymbolicFile>>
createSymbolicFile(MemoryBufferRef Object) {
return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
}
static ErrorOr<OwningBinary<SymbolicFile>>
static Expected<OwningBinary<SymbolicFile>>
createSymbolicFile(StringRef ObjectPath);
static inline bool classof(const Binary *v) {

View File

@ -268,8 +268,11 @@ size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
: DWOFile(), DWOContext(), DWOU(nullptr) {
auto Obj = object::ObjectFile::createObjectFile(DWOPath);
if (!Obj)
if (!Obj) {
// TODO: Actually report errors helpfully.
consumeError(Obj.takeError());
return;
}
DWOFile = std::move(Obj.get());
DWOContext.reset(
cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));

View File

@ -296,8 +296,9 @@ LLVMSymbolizer::getOrCreateObject(const std::string &Path,
const auto &I = BinaryForPath.find(Path);
Binary *Bin = nullptr;
if (I == BinaryForPath.end()) {
ErrorOr<OwningBinary<Binary>> BinOrErr = createBinary(Path);
if (auto EC = BinOrErr.getError()) {
Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path);
if (!BinOrErr) {
auto EC = errorToErrorCode(BinOrErr.takeError());
BinaryForPath.insert(std::make_pair(Path, EC));
return EC;
}

View File

@ -206,8 +206,15 @@ void MCJIT::generateCodeForModule(Module *M) {
// Load the object into the dynamic linker.
// MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
ErrorOr<std::unique_ptr<object::ObjectFile>> LoadedObject =
Expected<std::unique_ptr<object::ObjectFile>> LoadedObject =
object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
if (!LoadedObject) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(LoadedObject.takeError(), OS, "");
OS.flush();
report_fatal_error(Buf);
}
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
Dyld.loadObject(*LoadedObject.get());

View File

@ -245,7 +245,10 @@ Archive::Child::getAsBinary(LLVMContext *Context) const {
if (std::error_code EC = BuffOrErr.getError())
return EC;
return createBinary(BuffOrErr.get(), Context);
auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
if (BinaryOrErr)
return std::move(*BinaryOrErr);
return errorToErrorCode(BinaryOrErr.takeError());
}
ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {

View File

@ -231,11 +231,14 @@ writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind,
LLVMContext Context;
for (unsigned MemberNum = 0, N = Members.size(); MemberNum < N; ++MemberNum) {
MemoryBufferRef MemberBuffer = Buffers[MemberNum];
ErrorOr<std::unique_ptr<object::SymbolicFile>> ObjOrErr =
Expected<std::unique_ptr<object::SymbolicFile>> ObjOrErr =
object::SymbolicFile::createSymbolicFile(
MemberBuffer, sys::fs::file_magic::unknown, &Context);
if (!ObjOrErr)
continue; // FIXME: check only for "not an object file" errors.
if (!ObjOrErr) {
// FIXME: check only for "not an object file" errors.
consumeError(ObjOrErr.takeError());
continue;
}
object::SymbolicFile &Obj = *ObjOrErr.get();
if (!HeaderStartOffset) {

View File

@ -36,13 +36,13 @@ StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
ErrorOr<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
LLVMContext *Context) {
sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer());
switch (Type) {
case sys::fs::file_magic::archive:
return Archive::create(Buffer);
return errorOrToExpected(Archive::create(Buffer));
case sys::fs::file_magic::elf:
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::elf_executable:
@ -65,26 +65,26 @@ ErrorOr<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
case sys::fs::file_magic::bitcode:
return ObjectFile::createSymbolicFile(Buffer, Type, Context);
case sys::fs::file_magic::macho_universal_binary:
return MachOUniversalBinary::create(Buffer);
return errorOrToExpected(MachOUniversalBinary::create(Buffer));
case sys::fs::file_magic::unknown:
case sys::fs::file_magic::windows_resource:
// Unrecognized object file format.
return object_error::invalid_file_type;
return errorCodeToError(object_error::invalid_file_type);
}
llvm_unreachable("Unexpected Binary File Type");
}
ErrorOr<OwningBinary<Binary>> object::createBinary(StringRef Path) {
Expected<OwningBinary<Binary>> object::createBinary(StringRef Path) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFileOrSTDIN(Path);
if (std::error_code EC = FileOrErr.getError())
return EC;
return errorCodeToError(EC);
std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
ErrorOr<std::unique_ptr<Binary>> BinOrErr =
Expected<std::unique_ptr<Binary>> BinOrErr =
createBinary(Buffer->getMemBufferRef());
if (std::error_code EC = BinOrErr.getError())
return EC;
if (!BinOrErr)
return BinOrErr.takeError();
std::unique_ptr<Binary> &Bin = BinOrErr.get();
return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));

View File

@ -285,10 +285,10 @@ ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Ob
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::macho_object:
case sys::fs::file_magic::coff_object: {
ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
Expected<std::unique_ptr<ObjectFile>> ObjFile =
ObjectFile::createObjectFile(Object, Type);
if (!ObjFile)
return ObjFile.getError();
return errorToErrorCode(ObjFile.takeError());
return findBitcodeInObject(*ObjFile->get());
}
default:

View File

@ -192,6 +192,10 @@ static Expected<MachOObjectFile::LoadCommandInfo>
getFirstLoadCommandInfo(const MachOObjectFile *Obj) {
unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64)
: sizeof(MachO::mach_header);
if (sizeof(MachOObjectFile::LoadCommandInfo) > Obj->getHeader().sizeofcmds)
return malformedError(*Obj, "truncated or malformed object (load command "
"0 extends past the end all load commands in the "
"file)");
return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize));
}

View File

@ -56,10 +56,10 @@ ModuleSummaryIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::macho_object:
case sys::fs::file_magic::coff_object: {
ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
Expected<std::unique_ptr<ObjectFile>> ObjFile =
ObjectFile::createObjectFile(Object, Type);
if (!ObjFile)
return ObjFile.getError();
return errorToErrorCode(ObjFile.takeError());
return findBitcodeInObject(*ObjFile->get());
}
default:

View File

@ -61,11 +61,14 @@ wrap(const relocation_iterator *SI) {
// ObjectFile creation
LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr(
Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
ObjectFile::createObjectFile(Buf->getMemBufferRef()));
std::unique_ptr<ObjectFile> Obj;
if (!ObjOrErr)
if (!ObjOrErr) {
// TODO: Actually report errors helpfully.
consumeError(ObjOrErr.takeError());
return nullptr;
}
auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
return wrap(Ret);

View File

@ -66,7 +66,7 @@ section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
return section_iterator(SectionRef(Sec, this));
}
ErrorOr<std::unique_ptr<ObjectFile>>
Expected<std::unique_ptr<ObjectFile>>
ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
StringRef Data = Object.getBuffer();
if (Type == sys::fs::file_magic::unknown)
@ -78,13 +78,13 @@ ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
case sys::fs::file_magic::archive:
case sys::fs::file_magic::macho_universal_binary:
case sys::fs::file_magic::windows_resource:
return object_error::invalid_file_type;
return errorCodeToError(object_error::invalid_file_type);
case sys::fs::file_magic::elf:
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::elf_executable:
case sys::fs::file_magic::elf_shared_object:
case sys::fs::file_magic::elf_core:
return createELFObjectFile(Object);
return errorOrToExpected(createELFObjectFile(Object));
case sys::fs::file_magic::macho_object:
case sys::fs::file_magic::macho_executable:
case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
@ -96,27 +96,27 @@ ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
case sys::fs::file_magic::macho_dsym_companion:
case sys::fs::file_magic::macho_kext_bundle:
return expectedToErrorOr(createMachOObjectFile(Object));
return createMachOObjectFile(Object);
case sys::fs::file_magic::coff_object:
case sys::fs::file_magic::coff_import_library:
case sys::fs::file_magic::pecoff_executable:
return createCOFFObjectFile(Object);
return errorOrToExpected(createCOFFObjectFile(Object));
}
llvm_unreachable("Unexpected Object File Type");
}
ErrorOr<OwningBinary<ObjectFile>>
Expected<OwningBinary<ObjectFile>>
ObjectFile::createObjectFile(StringRef ObjectPath) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFile(ObjectPath);
if (std::error_code EC = FileOrErr.getError())
return EC;
return errorCodeToError(EC);
std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
createObjectFile(Buffer->getMemBufferRef());
if (std::error_code EC = ObjOrErr.getError())
return EC;
if (!ObjOrErr)
ObjOrErr.takeError();
std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));

View File

@ -26,7 +26,7 @@ SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
SymbolicFile::~SymbolicFile() {}
ErrorOr<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) {
StringRef Data = Object.getBuffer();
if (Type == sys::fs::file_magic::unknown)
@ -35,13 +35,13 @@ ErrorOr<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
switch (Type) {
case sys::fs::file_magic::bitcode:
if (Context)
return IRObjectFile::create(Object, *Context);
return errorOrToExpected(IRObjectFile::create(Object, *Context));
// Fallthrough
case sys::fs::file_magic::unknown:
case sys::fs::file_magic::archive:
case sys::fs::file_magic::macho_universal_binary:
case sys::fs::file_magic::windows_resource:
return object_error::invalid_file_type;
return errorCodeToError(object_error::invalid_file_type);
case sys::fs::file_magic::elf:
case sys::fs::file_magic::elf_executable:
case sys::fs::file_magic::elf_shared_object:
@ -63,7 +63,7 @@ ErrorOr<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::macho_object:
case sys::fs::file_magic::coff_object: {
ErrorOr<std::unique_ptr<ObjectFile>> Obj =
Expected<std::unique_ptr<ObjectFile>> Obj =
ObjectFile::createObjectFile(Object, Type);
if (!Obj || !Context)
return std::move(Obj);
@ -73,9 +73,9 @@ ErrorOr<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
if (!BCData)
return std::move(Obj);
return IRObjectFile::create(
MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
*Context);
return errorOrToExpected(IRObjectFile::create(
MemoryBufferRef(BCData->getBuffer(),
Object.getBufferIdentifier()), *Context));
}
}
llvm_unreachable("Unexpected Binary File Type");

View File

@ -506,8 +506,8 @@ loadBinaryFormat(MemoryBufferRef ObjectBuffer, InstrProfSymtab &ProfileNames,
StringRef &CoverageMapping, uint8_t &BytesInAddress,
support::endianness &Endian, StringRef Arch) {
auto BinOrErr = object::createBinary(ObjectBuffer);
if (std::error_code EC = BinOrErr.getError())
return EC;
if (!BinOrErr)
return errorToErrorCode(BinOrErr.takeError());
auto Bin = std::move(BinOrErr.get());
std::unique_ptr<ObjectFile> OF;
if (auto *Universal = dyn_cast<object::MachOUniversalBinary>(Bin.get())) {

View File

@ -5,9 +5,9 @@ RUN: llvm-objdump -private-headers %p/Inputs/macho-invalid-zero-ncmds -macho \
RUN: | FileCheck -check-prefix ZERO-NCMDS %s
ZERO-NCMDS: MH_MAGIC_64 0 0 0x00 OBJECT 0 0 0x00000000
RUN: not llvm-objdump -private-headers %p/Inputs/macho64-invalid-incomplete-load-command 2>&1 \
RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho64-invalid-incomplete-load-command 2>&1 \
RUN: | FileCheck -check-prefix INCOMPLETE-LOADC %s
INCOMPLETE-LOADC: Invalid data was encountered while parsing the file.
INCOMPLETE-LOADC: truncated or malformed object (load command 0 extends past the end all load commands in the file)
RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-too-small-load-command 2>&1 \
RUN: | FileCheck -check-prefix SMALL-LOADC-SIZE %s

View File

@ -1,2 +1,2 @@
RUN: not llvm-objdump -macho -s %p/Inputs/malformed-macho.bin 2>&1 | FileCheck %s -check-prefix=MALFORMED
MALFORMED: '{{.*}}': The file was not recognized as a valid object file
MALFORMED: The file was not recognized as a valid object file

View File

@ -196,8 +196,8 @@ BinaryHolder::GetObjectFiles(StringRef Filename, sys::TimeValue Timestamp) {
CurrentObjectFiles.clear();
for (auto MemBuf : *ErrOrMemBufferRefs) {
auto ErrOrObjectFile = object::ObjectFile::createObjectFile(MemBuf);
if (auto Err = ErrOrObjectFile.getError())
return Err;
if (!ErrOrObjectFile)
return errorToErrorCode(ErrOrObjectFile.takeError());
Objects.push_back(ErrOrObjectFile->get());
CurrentObjectFiles.push_back(std::move(*ErrOrObjectFile));

View File

@ -501,9 +501,11 @@ int main(int argc, char **argv, char * const *envp) {
}
for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
ErrorOr<object::OwningBinary<object::ObjectFile>> Obj =
Expected<object::OwningBinary<object::ObjectFile>> Obj =
object::ObjectFile::createObjectFile(ExtraObjects[i]);
if (!Obj) {
// TODO: Actually report errors helpfully.
consumeError(Obj.takeError());
Err.print(argv[0], errs());
return 1;
}

View File

@ -36,8 +36,12 @@ int convertForTestingMain(int argc, const char *argv[]) {
cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
if (auto Err = ObjErr.getError()) {
errs() << "error: " << Err.message() << "\n";
if (!ObjErr) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(ObjErr.takeError(), OS, "");
OS.flush();
errs() << "error: " << Buf;
return 1;
}
ObjectFile *OF = ObjErr.get().getBinary();

View File

@ -502,8 +502,9 @@ static void dumpArchive(const Archive *Arc) {
static void dumpInput(StringRef File) {
// Attempt to open the binary.
ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (std::error_code EC = BinaryOrErr.getError()) {
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (!BinaryOrErr) {
auto EC = errorToErrorCode(BinaryOrErr.takeError());
reportError(File, EC);
return;
}

View File

@ -96,9 +96,10 @@ static void DumpInput(StringRef Filename) {
error(Filename, BuffOrErr.getError());
std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
ErrorOr<std::unique_ptr<Binary>> BinOrErr =
Expected<std::unique_ptr<Binary>> BinOrErr =
object::createBinary(Buff->getMemBufferRef());
error(Filename, BinOrErr.getError());
if (!BinOrErr)
error(Filename, errorToErrorCode(BinOrErr.takeError()));
if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
DumpObjectFile(*Obj, Filename);

View File

@ -393,7 +393,7 @@ static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
for (const auto &Input : Inputs) {
auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
if (!ErrOrObj)
return ErrOrObj.getError();
return errorToErrorCode(ErrOrObj.takeError());
UnitIndexEntry CurEntry = {};

View File

@ -1021,10 +1021,12 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
return;
LLVMContext &Context = getGlobalContext();
ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
Expected<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context);
if (error(BinaryOrErr.getError(), Filename))
if (!BinaryOrErr) {
error(errorToErrorCode(BinaryOrErr.takeError()), Filename);
return;
}
Binary &Bin = *BinaryOrErr.get();
if (Archive *A = dyn_cast<Archive>(&Bin)) {

View File

@ -1497,9 +1497,9 @@ void llvm::ParseInputMachO(StringRef Filename) {
}
// Attempt to open the binary.
ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
if (std::error_code EC = BinaryOrErr.getError())
report_error(Filename, EC);
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
if (!BinaryOrErr)
report_error(Filename, BinaryOrErr.takeError());
Binary &Bin = *BinaryOrErr.get().getBinary();
if (Archive *A = dyn_cast<Archive>(&Bin)) {

View File

@ -270,6 +270,17 @@ LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
exit(1);
}
LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
llvm::Error E) {
assert(E);
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(std::move(E), OS, "");
OS.flush();
errs() << ToolName << ": " << Buf;
exit(1);
}
static const Target *getTarget(const ObjectFile *Obj = nullptr) {
// Figure out the target triple.
llvm::Triple TheTriple("unknown-unknown-unknown");
@ -1611,9 +1622,9 @@ static void DumpInput(StringRef file) {
}
// Attempt to open the binary.
ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
if (std::error_code EC = BinaryOrErr.getError())
report_error(file, EC);
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
if (!BinaryOrErr)
report_error(file, errorToErrorCode(BinaryOrErr.takeError()));
Binary &Binary = *BinaryOrErr.get().getBinary();
if (Archive *a = dyn_cast<Archive>(&Binary))

View File

@ -85,6 +85,7 @@ void PrintSectionHeaders(const object::ObjectFile *o);
void PrintSectionContents(const object::ObjectFile *o);
void PrintSymbolTable(const object::ObjectFile *o);
LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, llvm::Error E);
} // end namespace llvm

View File

@ -429,9 +429,9 @@ static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
static void dumpInput(StringRef File) {
// Attempt to open the binary.
ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (std::error_code EC = BinaryOrErr.getError())
reportError(File, EC);
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (!BinaryOrErr)
reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
Binary &Binary = *BinaryOrErr.get().getBinary();
if (Archive *Arc = dyn_cast<Archive>(&Binary))

View File

@ -292,11 +292,16 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) {
if (std::error_code EC = InputBuffer.getError())
ErrorAndExit("unable to read input: '" + EC.message() + "'");
ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
Expected<std::unique_ptr<ObjectFile>> MaybeObj(
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
if (std::error_code EC = MaybeObj.getError())
ErrorAndExit("unable to create object file: '" + EC.message() + "'");
if (!MaybeObj) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
OS.flush();
ErrorAndExit("unable to create object file: '" + Buf + "'");
}
ObjectFile &Obj = **MaybeObj;
@ -401,11 +406,16 @@ static int executeInput() {
MemoryBuffer::getFileOrSTDIN(File);
if (std::error_code EC = InputBuffer.getError())
ErrorAndExit("unable to read input: '" + EC.message() + "'");
ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
Expected<std::unique_ptr<ObjectFile>> MaybeObj(
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
if (std::error_code EC = MaybeObj.getError())
ErrorAndExit("unable to create object file: '" + EC.message() + "'");
if (!MaybeObj) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
OS.flush();
ErrorAndExit("unable to create object file: '" + Buf + "'");
}
ObjectFile &Obj = **MaybeObj;
@ -665,11 +675,16 @@ static int linkAndVerify() {
if (std::error_code EC = InputBuffer.getError())
ErrorAndExit("unable to read input: '" + EC.message() + "'");
ErrorOr<std::unique_ptr<ObjectFile>> MaybeObj(
Expected<std::unique_ptr<ObjectFile>> MaybeObj(
ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
if (std::error_code EC = MaybeObj.getError())
ErrorAndExit("unable to create object file: '" + EC.message() + "'");
if (!MaybeObj) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
OS.flush();
ErrorAndExit("unable to create object file: '" + Buf + "'");
}
ObjectFile &Obj = **MaybeObj;

View File

@ -463,9 +463,11 @@ static bool checkMachOAndArchFlags(ObjectFile *o, StringRef file) {
static void printFileSectionSizes(StringRef file) {
// Attempt to open the binary.
ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
if (error(BinaryOrErr.getError()))
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
if (!BinaryOrErr) {
error(errorToErrorCode(BinaryOrErr.takeError()));
return;
}
Binary &Bin = *BinaryOrErr.get().getBinary();
if (Archive *a = dyn_cast<Archive>(&Bin)) {

View File

@ -29,9 +29,9 @@ static std::error_code dumpObject(const ObjectFile &Obj) {
}
static std::error_code dumpInput(StringRef File) {
ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (std::error_code EC = BinaryOrErr.getError())
return EC;
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (!BinaryOrErr)
return errorToErrorCode(BinaryOrErr.takeError());
Binary &Binary = *BinaryOrErr.get().getBinary();
// TODO: If this is an archive, then burst it and dump each entry

View File

@ -422,9 +422,10 @@ visitObjectFiles(const object::Archive &A,
static void
visitObjectFiles(std::string FileName,
std::function<void(const object::ObjectFile &)> Fn) {
ErrorOr<object::OwningBinary<object::Binary>> BinaryOrErr =
Expected<object::OwningBinary<object::Binary>> BinaryOrErr =
object::createBinary(FileName);
FailIfError(BinaryOrErr);
if (!BinaryOrErr)
FailIfError(errorToErrorCode(BinaryOrErr.takeError()));
object::Binary &Binary = *BinaryOrErr.get().getBinary();
if (object::Archive *A = dyn_cast<object::Archive>(&Binary))