llvm.org GIT mirror llvm / c6bf9be
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 Kevin Enderby 3 years ago
35 changed file(s) with 163 addition(s) and 95 deletion(s). Raw diff Collapse all Expand all
4141 PM.run(M);
4242 std::unique_ptr ObjBuffer(
4343 new ObjectMemoryBuffer(std::move(ObjBufferSV)));
44 ErrorOr> Obj =
44 Expected> Obj =
4545 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
46 // TODO: Actually report errors helpfully.
4746 typedef object::OwningBinary OwningObj;
4847 if (Obj)
4948 return OwningObj(std::move(*Obj), std::move(ObjBuffer));
49 // TODO: Actually report errors helpfully.
50 consumeError(Obj.takeError());
5051 return OwningObj(nullptr, nullptr);
5152 }
5253
123123 if (!ObjBuffer)
124124 return object::OwningBinary();
125125
126 ErrorOr> Obj =
126 Expected> Obj =
127127 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
128 if (!Obj)
128 if (!Obj) {
129 // TODO: Actually report errors helpfully.
130 consumeError(Obj.takeError());
129131 return object::OwningBinary();
132 }
130133
131134 return object::OwningBinary(std::move(*Obj),
132135 std::move(ObjBuffer));
133133 /// @brief Create a Binary from Source, autodetecting the file type.
134134 ///
135135 /// @param Source The data to create the Binary from.
136 ErrorOr> createBinary(MemoryBufferRef Source,
137 LLVMContext *Context = nullptr);
136 Expected> createBinary(MemoryBufferRef Source,
137 LLVMContext *Context = nullptr);
138138
139139 template class OwningBinary {
140140 std::unique_ptr Bin;
184184 return Bin.get();
185185 }
186186
187 ErrorOr> createBinary(StringRef Path);
187 Expected> createBinary(StringRef Path);
188188 }
189189 }
190190
274274 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
275275 /// return true.
276276 /// @brief Create ObjectFile from path.
277 static ErrorOr>
277 static Expected>
278278 createObjectFile(StringRef ObjectPath);
279279
280 static ErrorOr>
280 static Expected>
281281 createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type);
282 static ErrorOr>
282 static Expected>
283283 createObjectFile(MemoryBufferRef Object) {
284284 return createObjectFile(Object, sys::fs::file_magic::unknown);
285285 }
152152 }
153153
154154 // construction aux.
155 static ErrorOr>
155 static Expected>
156156 createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
157157 LLVMContext *Context);
158158
159 static ErrorOr>
159 static Expected>
160160 createSymbolicFile(MemoryBufferRef Object) {
161161 return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
162162 }
163 static ErrorOr>
163 static Expected>
164164 createSymbolicFile(StringRef ObjectPath);
165165
166166 static inline bool classof(const Binary *v) {
267267 DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
268268 : DWOFile(), DWOContext(), DWOU(nullptr) {
269269 auto Obj = object::ObjectFile::createObjectFile(DWOPath);
270 if (!Obj)
270 if (!Obj) {
271 // TODO: Actually report errors helpfully.
272 consumeError(Obj.takeError());
271273 return;
274 }
272275 DWOFile = std::move(Obj.get());
273276 DWOContext.reset(
274277 cast(new DWARFContextInMemory(*DWOFile.getBinary())));
295295 const auto &I = BinaryForPath.find(Path);
296296 Binary *Bin = nullptr;
297297 if (I == BinaryForPath.end()) {
298 ErrorOr> BinOrErr = createBinary(Path);
299 if (auto EC = BinOrErr.getError()) {
298 Expected> BinOrErr = createBinary(Path);
299 if (!BinOrErr) {
300 auto EC = errorToErrorCode(BinOrErr.takeError());
300301 BinaryForPath.insert(std::make_pair(Path, EC));
301302 return EC;
302303 }
205205
206206 // Load the object into the dynamic linker.
207207 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
208 ErrorOr> LoadedObject =
208 Expected> LoadedObject =
209209 object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
210 if (!LoadedObject) {
211 std::string Buf;
212 raw_string_ostream OS(Buf);
213 logAllUnhandledErrors(LoadedObject.takeError(), OS, "");
214 OS.flush();
215 report_fatal_error(Buf);
216 }
210217 std::unique_ptr L =
211218 Dyld.loadObject(*LoadedObject.get());
212219
244244 if (std::error_code EC = BuffOrErr.getError())
245245 return EC;
246246
247 return createBinary(BuffOrErr.get(), Context);
247 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
248 if (BinaryOrErr)
249 return std::move(*BinaryOrErr);
250 return errorToErrorCode(BinaryOrErr.takeError());
248251 }
249252
250253 ErrorOr> Archive::create(MemoryBufferRef Source) {
230230 LLVMContext Context;
231231 for (unsigned MemberNum = 0, N = Members.size(); MemberNum < N; ++MemberNum) {
232232 MemoryBufferRef MemberBuffer = Buffers[MemberNum];
233 ErrorOr> ObjOrErr =
233 Expected> ObjOrErr =
234234 object::SymbolicFile::createSymbolicFile(
235235 MemberBuffer, sys::fs::file_magic::unknown, &Context);
236 if (!ObjOrErr)
237 continue; // FIXME: check only for "not an object file" errors.
236 if (!ObjOrErr) {
237 // FIXME: check only for "not an object file" errors.
238 consumeError(ObjOrErr.takeError());
239 continue;
240 }
238241 object::SymbolicFile &Obj = *ObjOrErr.get();
239242
240243 if (!HeaderStartOffset) {
3535
3636 MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
3737
38 ErrorOr> object::createBinary(MemoryBufferRef Buffer,
38 Expected> object::createBinary(MemoryBufferRef Buffer,
3939 LLVMContext *Context) {
4040 sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer());
4141
4242 switch (Type) {
4343 case sys::fs::file_magic::archive:
44 return Archive::create(Buffer);
44 return errorOrToExpected(Archive::create(Buffer));
4545 case sys::fs::file_magic::elf:
4646 case sys::fs::file_magic::elf_relocatable:
4747 case sys::fs::file_magic::elf_executable:
6464 case sys::fs::file_magic::bitcode:
6565 return ObjectFile::createSymbolicFile(Buffer, Type, Context);
6666 case sys::fs::file_magic::macho_universal_binary:
67 return MachOUniversalBinary::create(Buffer);
67 return errorOrToExpected(MachOUniversalBinary::create(Buffer));
6868 case sys::fs::file_magic::unknown:
6969 case sys::fs::file_magic::windows_resource:
7070 // Unrecognized object file format.
71 return object_error::invalid_file_type;
71 return errorCodeToError(object_error::invalid_file_type);
7272 }
7373 llvm_unreachable("Unexpected Binary File Type");
7474 }
7575
76 ErrorOr> object::createBinary(StringRef Path) {
76 Expected> object::createBinary(StringRef Path) {
7777 ErrorOr> FileOrErr =
7878 MemoryBuffer::getFileOrSTDIN(Path);
7979 if (std::error_code EC = FileOrErr.getError())
80 return EC;
80 return errorCodeToError(EC);
8181 std::unique_ptr &Buffer = FileOrErr.get();
8282
83 ErrorOr> BinOrErr =
83 Expected> BinOrErr =
8484 createBinary(Buffer->getMemBufferRef());
85 if (std::error_code EC = BinOrErr.getError())
86 return EC;
85 if (!BinOrErr)
86 return BinOrErr.takeError();
8787 std::unique_ptr &Bin = BinOrErr.get();
8888
8989 return OwningBinary(std::move(Bin), std::move(Buffer));
284284 case sys::fs::file_magic::elf_relocatable:
285285 case sys::fs::file_magic::macho_object:
286286 case sys::fs::file_magic::coff_object: {
287 ErrorOr> ObjFile =
287 Expected> ObjFile =
288288 ObjectFile::createObjectFile(Object, Type);
289289 if (!ObjFile)
290 return ObjFile.getError();
290 return errorToErrorCode(ObjFile.takeError());
291291 return findBitcodeInObject(*ObjFile->get());
292292 }
293293 default:
191191 getFirstLoadCommandInfo(const MachOObjectFile *Obj) {
192192 unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64)
193193 : sizeof(MachO::mach_header);
194 if (sizeof(MachOObjectFile::LoadCommandInfo) > Obj->getHeader().sizeofcmds)
195 return malformedError(*Obj, "truncated or malformed object (load command "
196 "0 extends past the end all load commands in the "
197 "file)");
194198 return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize));
195199 }
196200
5555 case sys::fs::file_magic::elf_relocatable:
5656 case sys::fs::file_magic::macho_object:
5757 case sys::fs::file_magic::coff_object: {
58 ErrorOr> ObjFile =
58 Expected> ObjFile =
5959 ObjectFile::createObjectFile(Object, Type);
6060 if (!ObjFile)
61 return ObjFile.getError();
61 return errorToErrorCode(ObjFile.takeError());
6262 return findBitcodeInObject(*ObjFile->get());
6363 }
6464 default:
6060 // ObjectFile creation
6161 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
6262 std::unique_ptr Buf(unwrap(MemBuf));
63 ErrorOr> ObjOrErr(
63 Expected> ObjOrErr(
6464 ObjectFile::createObjectFile(Buf->getMemBufferRef()));
6565 std::unique_ptr Obj;
66 if (!ObjOrErr)
66 if (!ObjOrErr) {
67 // TODO: Actually report errors helpfully.
68 consumeError(ObjOrErr.takeError());
6769 return nullptr;
70 }
6871
6972 auto *Ret = new OwningBinary(std::move(ObjOrErr.get()), std::move(Buf));
7073 return wrap(Ret);
6565 return section_iterator(SectionRef(Sec, this));
6666 }
6767
68 ErrorOr>
68 Expected>
6969 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
7070 StringRef Data = Object.getBuffer();
7171 if (Type == sys::fs::file_magic::unknown)
7777 case sys::fs::file_magic::archive:
7878 case sys::fs::file_magic::macho_universal_binary:
7979 case sys::fs::file_magic::windows_resource:
80 return object_error::invalid_file_type;
80 return errorCodeToError(object_error::invalid_file_type);
8181 case sys::fs::file_magic::elf:
8282 case sys::fs::file_magic::elf_relocatable:
8383 case sys::fs::file_magic::elf_executable:
8484 case sys::fs::file_magic::elf_shared_object:
8585 case sys::fs::file_magic::elf_core:
86 return createELFObjectFile(Object);
86 return errorOrToExpected(createELFObjectFile(Object));
8787 case sys::fs::file_magic::macho_object:
8888 case sys::fs::file_magic::macho_executable:
8989 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
9595 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
9696 case sys::fs::file_magic::macho_dsym_companion:
9797 case sys::fs::file_magic::macho_kext_bundle:
98 return expectedToErrorOr(createMachOObjectFile(Object));
98 return createMachOObjectFile(Object);
9999 case sys::fs::file_magic::coff_object:
100100 case sys::fs::file_magic::coff_import_library:
101101 case sys::fs::file_magic::pecoff_executable:
102 return createCOFFObjectFile(Object);
102 return errorOrToExpected(createCOFFObjectFile(Object));
103103 }
104104 llvm_unreachable("Unexpected Object File Type");
105105 }
106106
107 ErrorOr>
107 Expected>
108108 ObjectFile::createObjectFile(StringRef ObjectPath) {
109109 ErrorOr> FileOrErr =
110110 MemoryBuffer::getFile(ObjectPath);
111111 if (std::error_code EC = FileOrErr.getError())
112 return EC;
112 return errorCodeToError(EC);
113113 std::unique_ptr Buffer = std::move(FileOrErr.get());
114114
115 ErrorOr> ObjOrErr =
115 Expected> ObjOrErr =
116116 createObjectFile(Buffer->getMemBufferRef());
117 if (std::error_code EC = ObjOrErr.getError())
118 return EC;
117 if (!ObjOrErr)
118 ObjOrErr.takeError();
119119 std::unique_ptr Obj = std::move(ObjOrErr.get());
120120
121121 return OwningBinary(std::move(Obj), std::move(Buffer));
2525
2626 SymbolicFile::~SymbolicFile() {}
2727
28 ErrorOr> SymbolicFile::createSymbolicFile(
28 Expected> SymbolicFile::createSymbolicFile(
2929 MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) {
3030 StringRef Data = Object.getBuffer();
3131 if (Type == sys::fs::file_magic::unknown)
3434 switch (Type) {
3535 case sys::fs::file_magic::bitcode:
3636 if (Context)
37 return IRObjectFile::create(Object, *Context);
37 return errorOrToExpected(IRObjectFile::create(Object, *Context));
3838 // Fallthrough
3939 case sys::fs::file_magic::unknown:
4040 case sys::fs::file_magic::archive:
4141 case sys::fs::file_magic::macho_universal_binary:
4242 case sys::fs::file_magic::windows_resource:
43 return object_error::invalid_file_type;
43 return errorCodeToError(object_error::invalid_file_type);
4444 case sys::fs::file_magic::elf:
4545 case sys::fs::file_magic::elf_executable:
4646 case sys::fs::file_magic::elf_shared_object:
6262 case sys::fs::file_magic::elf_relocatable:
6363 case sys::fs::file_magic::macho_object:
6464 case sys::fs::file_magic::coff_object: {
65 ErrorOr> Obj =
65 Expected> Obj =
6666 ObjectFile::createObjectFile(Object, Type);
6767 if (!Obj || !Context)
6868 return std::move(Obj);
7272 if (!BCData)
7373 return std::move(Obj);
7474
75 return IRObjectFile::create(
76 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
77 *Context);
75 return errorOrToExpected(IRObjectFile::create(
76 MemoryBufferRef(BCData->getBuffer(),
77 Object.getBufferIdentifier()), *Context));
7878 }
7979 }
8080 llvm_unreachable("Unexpected Binary File Type");
505505 StringRef &CoverageMapping, uint8_t &BytesInAddress,
506506 support::endianness &Endian, StringRef Arch) {
507507 auto BinOrErr = object::createBinary(ObjectBuffer);
508 if (std::error_code EC = BinOrErr.getError())
509 return EC;
508 if (!BinOrErr)
509 return errorToErrorCode(BinOrErr.takeError());
510510 auto Bin = std::move(BinOrErr.get());
511511 std::unique_ptr OF;
512512 if (auto *Universal = dyn_cast(Bin.get())) {
44 RUN: | FileCheck -check-prefix ZERO-NCMDS %s
55 ZERO-NCMDS: MH_MAGIC_64 0 0 0x00 OBJECT 0 0 0x00000000
66
7 RUN: not llvm-objdump -private-headers %p/Inputs/macho64-invalid-incomplete-load-command 2>&1 \
7 RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho64-invalid-incomplete-load-command 2>&1 \
88 RUN: | FileCheck -check-prefix INCOMPLETE-LOADC %s
9 INCOMPLETE-LOADC: Invalid data was encountered while parsing the file.
9 INCOMPLETE-LOADC: truncated or malformed object (load command 0 extends past the end all load commands in the file)
1010
1111 RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-too-small-load-command 2>&1 \
1212 RUN: | FileCheck -check-prefix SMALL-LOADC-SIZE %s
0 RUN: not llvm-objdump -macho -s %p/Inputs/malformed-macho.bin 2>&1 | FileCheck %s -check-prefix=MALFORMED
1 MALFORMED: '{{.*}}': The file was not recognized as a valid object file
1 MALFORMED: The file was not recognized as a valid object file
195195 CurrentObjectFiles.clear();
196196 for (auto MemBuf : *ErrOrMemBufferRefs) {
197197 auto ErrOrObjectFile = object::ObjectFile::createObjectFile(MemBuf);
198 if (auto Err = ErrOrObjectFile.getError())
199 return Err;
198 if (!ErrOrObjectFile)
199 return errorToErrorCode(ErrOrObjectFile.takeError());
200200
201201 Objects.push_back(ErrOrObjectFile->get());
202202 CurrentObjectFiles.push_back(std::move(*ErrOrObjectFile));
500500 }
501501
502502 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
503 ErrorOr> Obj =
503 Expected> Obj =
504504 object::ObjectFile::createObjectFile(ExtraObjects[i]);
505505 if (!Obj) {
506 // TODO: Actually report errors helpfully.
507 consumeError(Obj.takeError());
506508 Err.print(argv[0], errs());
507509 return 1;
508510 }
3535 cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
3636
3737 auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
38 if (auto Err = ObjErr.getError()) {
39 errs() << "error: " << Err.message() << "\n";
38 if (!ObjErr) {
39 std::string Buf;
40 raw_string_ostream OS(Buf);
41 logAllUnhandledErrors(ObjErr.takeError(), OS, "");
42 OS.flush();
43 errs() << "error: " << Buf;
4044 return 1;
4145 }
4246 ObjectFile *OF = ObjErr.get().getBinary();
501501
502502 static void dumpInput(StringRef File) {
503503 // Attempt to open the binary.
504 ErrorOr> BinaryOrErr = createBinary(File);
505 if (std::error_code EC = BinaryOrErr.getError()) {
504 Expected> BinaryOrErr = createBinary(File);
505 if (!BinaryOrErr) {
506 auto EC = errorToErrorCode(BinaryOrErr.takeError());
506507 reportError(File, EC);
507508 return;
508509 }
9595 error(Filename, BuffOrErr.getError());
9696 std::unique_ptr Buff = std::move(BuffOrErr.get());
9797
98 ErrorOr> BinOrErr =
98 Expected> BinOrErr =
9999 object::createBinary(Buff->getMemBufferRef());
100 error(Filename, BinOrErr.getError());
100 if (!BinOrErr)
101 error(Filename, errorToErrorCode(BinOrErr.takeError()));
101102
102103 if (auto *Obj = dyn_cast(BinOrErr->get()))
103104 DumpObjectFile(*Obj, Filename);
392392 for (const auto &Input : Inputs) {
393393 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
394394 if (!ErrOrObj)
395 return ErrOrObj.getError();
395 return errorToErrorCode(ErrOrObj.takeError());
396396
397397 UnitIndexEntry CurEntry = {};
398398
10201020 return;
10211021
10221022 LLVMContext &Context = getGlobalContext();
1023 ErrorOr> BinaryOrErr = createBinary(
1023 Expected> BinaryOrErr = createBinary(
10241024 BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context);
1025 if (error(BinaryOrErr.getError(), Filename))
1025 if (!BinaryOrErr) {
1026 error(errorToErrorCode(BinaryOrErr.takeError()), Filename);
10261027 return;
1028 }
10271029 Binary &Bin = *BinaryOrErr.get();
10281030
10291031 if (Archive *A = dyn_cast(&Bin)) {
14961496 }
14971497
14981498 // Attempt to open the binary.
1499 ErrorOr> BinaryOrErr = createBinary(Filename);
1500 if (std::error_code EC = BinaryOrErr.getError())
1501 report_error(Filename, EC);
1499 Expected> BinaryOrErr = createBinary(Filename);
1500 if (!BinaryOrErr)
1501 report_error(Filename, BinaryOrErr.takeError());
15021502 Binary &Bin = *BinaryOrErr.get().getBinary();
15031503
15041504 if (Archive *A = dyn_cast(&Bin)) {
269269 exit(1);
270270 }
271271
272 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
273 llvm::Error E) {
274 assert(E);
275 std::string Buf;
276 raw_string_ostream OS(Buf);
277 logAllUnhandledErrors(std::move(E), OS, "");
278 OS.flush();
279 errs() << ToolName << ": " << Buf;
280 exit(1);
281 }
282
272283 static const Target *getTarget(const ObjectFile *Obj = nullptr) {
273284 // Figure out the target triple.
274285 llvm::Triple TheTriple("unknown-unknown-unknown");
16101621 }
16111622
16121623 // Attempt to open the binary.
1613 ErrorOr> BinaryOrErr = createBinary(file);
1614 if (std::error_code EC = BinaryOrErr.getError())
1615 report_error(file, EC);
1624 Expected> BinaryOrErr = createBinary(file);
1625 if (!BinaryOrErr)
1626 report_error(file, errorToErrorCode(BinaryOrErr.takeError()));
16161627 Binary &Binary = *BinaryOrErr.get().getBinary();
16171628
16181629 if (Archive *a = dyn_cast(&Binary))
8484 void PrintSectionContents(const object::ObjectFile *o);
8585 void PrintSymbolTable(const object::ObjectFile *o);
8686 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
87 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, llvm::Error E);
8788
8889 } // end namespace llvm
8990
428428 static void dumpInput(StringRef File) {
429429
430430 // Attempt to open the binary.
431 ErrorOr> BinaryOrErr = createBinary(File);
432 if (std::error_code EC = BinaryOrErr.getError())
433 reportError(File, EC);
431 Expected> BinaryOrErr = createBinary(File);
432 if (!BinaryOrErr)
433 reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
434434 Binary &Binary = *BinaryOrErr.get().getBinary();
435435
436436 if (Archive *Arc = dyn_cast(&Binary))
291291 if (std::error_code EC = InputBuffer.getError())
292292 ErrorAndExit("unable to read input: '" + EC.message() + "'");
293293
294 ErrorOr> MaybeObj(
294 Expected> MaybeObj(
295295 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
296296
297 if (std::error_code EC = MaybeObj.getError())
298 ErrorAndExit("unable to create object file: '" + EC.message() + "'");
297 if (!MaybeObj) {
298 std::string Buf;
299 raw_string_ostream OS(Buf);
300 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
301 OS.flush();
302 ErrorAndExit("unable to create object file: '" + Buf + "'");
303 }
299304
300305 ObjectFile &Obj = **MaybeObj;
301306
400405 MemoryBuffer::getFileOrSTDIN(File);
401406 if (std::error_code EC = InputBuffer.getError())
402407 ErrorAndExit("unable to read input: '" + EC.message() + "'");
403 ErrorOr> MaybeObj(
408 Expected> MaybeObj(
404409 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
405410
406 if (std::error_code EC = MaybeObj.getError())
407 ErrorAndExit("unable to create object file: '" + EC.message() + "'");
411 if (!MaybeObj) {
412 std::string Buf;
413 raw_string_ostream OS(Buf);
414 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
415 OS.flush();
416 ErrorAndExit("unable to create object file: '" + Buf + "'");
417 }
408418
409419 ObjectFile &Obj = **MaybeObj;
410420
664674 if (std::error_code EC = InputBuffer.getError())
665675 ErrorAndExit("unable to read input: '" + EC.message() + "'");
666676
667 ErrorOr> MaybeObj(
677 Expected> MaybeObj(
668678 ObjectFile::createObjectFile((*InputBuffer)->getMemBufferRef()));
669679
670 if (std::error_code EC = MaybeObj.getError())
671 ErrorAndExit("unable to create object file: '" + EC.message() + "'");
680 if (!MaybeObj) {
681 std::string Buf;
682 raw_string_ostream OS(Buf);
683 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
684 OS.flush();
685 ErrorAndExit("unable to create object file: '" + Buf + "'");
686 }
672687
673688 ObjectFile &Obj = **MaybeObj;
674689
462462 static void printFileSectionSizes(StringRef file) {
463463
464464 // Attempt to open the binary.
465 ErrorOr> BinaryOrErr = createBinary(file);
466 if (error(BinaryOrErr.getError()))
465 Expected> BinaryOrErr = createBinary(file);
466 if (!BinaryOrErr) {
467 error(errorToErrorCode(BinaryOrErr.takeError()));
467468 return;
469 }
468470 Binary &Bin = *BinaryOrErr.get().getBinary();
469471
470472 if (Archive *a = dyn_cast(&Bin)) {
2828 }
2929
3030 static std::error_code dumpInput(StringRef File) {
31 ErrorOr> BinaryOrErr = createBinary(File);
32 if (std::error_code EC = BinaryOrErr.getError())
33 return EC;
31 Expected> BinaryOrErr = createBinary(File);
32 if (!BinaryOrErr)
33 return errorToErrorCode(BinaryOrErr.takeError());
3434
3535 Binary &Binary = *BinaryOrErr.get().getBinary();
3636 // TODO: If this is an archive, then burst it and dump each entry
421421 static void
422422 visitObjectFiles(std::string FileName,
423423 std::function Fn) {
424 ErrorOr> BinaryOrErr =
424 Expected> BinaryOrErr =
425425 object::createBinary(FileName);
426 FailIfError(BinaryOrErr);
426 if (!BinaryOrErr)
427 FailIfError(errorToErrorCode(BinaryOrErr.takeError()));
427428
428429 object::Binary &Binary = *BinaryOrErr.get().getBinary();
429430 if (object::Archive *A = dyn_cast(&Binary))