llvm.org GIT mirror llvm / c827f2c
Finish cleaning up most of the error handling in libObject’s MachOUniversalBinary and its clients to use the new llvm::Error model for error handling. Changed getAsArchive() from ErrorOr<...> to Expected<...> so now all interfaces there use the new llvm::Error model for return values. In the two places it had if (!Parent) this is actually a program error so changed from returning errorCodeToError(object_error::parse_failed) to calling report_fatal_error() with a message. In getObjectForArch() added error messages to its two llvm::Error return values instead of returning errorCodeToError(object_error::arch_not_found) with no error message. For the llvm-obdump, llvm-nm and llvm-size clients since the only binary files in Mach-O Universal Binaries that are supported are Mach-O files or archives with Mach-O objects, updated their logic to generate an error when a slice contains something like an ELF binary instead of ignoring it. And added a test case for that. The last error stuff to be cleaned up for libObject’s MachOUniversalBinary is the use of errorOrToExpected(Archive::create(ObjBuffer)) which needs Archive::create() to be changed from ErrorOr<...> to Expected<...> first, which I’ll work on next. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274079 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 3 years ago
10 changed file(s) with 90 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
1818 #include "llvm/Object/Archive.h"
1919 #include "llvm/Object/Binary.h"
2020 #include "llvm/Object/MachO.h"
21 #include "llvm/Support/ErrorOr.h"
2221 #include "llvm/Support/MachO.h"
2322
2423 namespace llvm {
104103
105104 Expected> getAsObjectFile() const;
106105
107 ErrorOr> getAsArchive() const;
106 Expected> getAsArchive() const;
108107 };
109108
110109 class object_iterator {
6767 Expected>
6868 MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
6969 if (!Parent)
70 return errorCodeToError(object_error::parse_failed);
70 report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsObjectFile() "
71 "called when Parent is a nullptr");
7172
7273 StringRef ParentData = Parent->getData();
7374 StringRef ObjectData;
8081 return ObjectFile::createMachOObjectFile(ObjBuffer);
8182 }
8283
83 ErrorOr>
84 Expected>
8485 MachOUniversalBinary::ObjectForArch::getAsArchive() const {
8586 if (!Parent)
86 return object_error::parse_failed;
87 report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
88 "called when Parent is a nullptr");
8789
8890 StringRef ParentData = Parent->getData();
8991 StringRef ObjectData;
9395 ObjectData = ParentData.substr(Header64.offset, Header64.size);
9496 StringRef ObjectName = Parent->getFileName();
9597 MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
96 return Archive::create(ObjBuffer);
98 return errorOrToExpected(Archive::create(ObjBuffer));
9799 }
98100
99101 void MachOUniversalBinary::anchor() { }
144146 Expected>
145147 MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
146148 if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
147 return errorCodeToError(object_error::arch_not_found);
149 return make_error(std::move("Unknown architecture "
150 "named: " + ArchName),
151 object_error::arch_not_found);
148152
149153 for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
150154 if (I->getArchTypeName() == ArchName)
151155 return I->getAsObjectFile();
152156 }
153 return errorCodeToError(object_error::arch_not_found);
157 return make_error(std::move("fat file does not "
158 "contain " + ArchName),
159 object_error::arch_not_found);
154160 }
9696
9797 RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat 2>&1 | FileCheck -check-prefix INVALID-FAT %s
9898 INVALID-FAT: truncated or malformed fat file (fat_arch_64 structs would extend past the end of the file)
99
100 RUN: not llvm-objdump -macho -private-headers -arch all %p/Inputs/macho-invalid-fat.obj.elf-x86_64 2>&1 | FileCheck -check-prefix INVALID-FAT-ELF %s
101 INVALID-FAT-ELF: Mach-O universal file: {{.*}}/macho-invalid-fat.obj.elf-x86_64 for architecture x86_64 is not a Mach-O file or an archive file
11701170 error(std::move(E), Filename, ArchFlags.size() > 1 ?
11711171 StringRef(I->getArchTypeName()) : StringRef());
11721172 continue;
1173 } else if (ErrorOr> AOrErr =
1173 } else if (Expected> AOrErr =
11741174 I->getAsArchive()) {
11751175 std::unique_ptr &A = *AOrErr;
11761176 for (Archive::child_iterator AI = A->child_begin(),
12081208 ArchitectureName);
12091209 }
12101210 }
1211 } else {
1212 consumeError(AOrErr.takeError());
1213 error(Filename + " for architecture " +
1214 StringRef(I->getArchTypeName()) +
1215 " is not a Mach-O file or an archive file",
1216 "Mach-O universal file");
12111217 }
12121218 }
12131219 }
12371243 ObjOrErr.takeError())) {
12381244 error(std::move(E), Filename);
12391245 return;
1240 } else if (ErrorOr> AOrErr =
1246 } else if (Expected> AOrErr =
12411247 I->getAsArchive()) {
12421248 std::unique_ptr &A = *AOrErr;
12431249 for (Archive::child_iterator AI = A->child_begin(),
12651271 dumpSymbolNamesFromObject(*O, false, ArchiveName);
12661272 }
12671273 }
1274 } else {
1275 consumeError(AOrErr.takeError());
1276 error(Filename + " for architecture " +
1277 StringRef(I->getArchTypeName()) +
1278 " is not a Mach-O file or an archive file",
1279 "Mach-O universal file");
12681280 }
12691281 return;
12701282 }
13001312 error(std::move(E), Filename, moreThanOneArch ?
13011313 StringRef(I->getArchTypeName()) : StringRef());
13021314 continue;
1303 } else if (ErrorOr> AOrErr = I->getAsArchive()) {
1315 } else if (Expected> AOrErr =
1316 I->getAsArchive()) {
13041317 std::unique_ptr &A = *AOrErr;
13051318 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
13061319 AI != AE; ++AI) {
13351348 dumpSymbolNamesFromObject(*O, false, ArchiveName, ArchitectureName);
13361349 }
13371350 }
1351 } else {
1352 consumeError(AOrErr.takeError());
1353 error(Filename + " for architecture " +
1354 StringRef(I->getArchTypeName()) +
1355 " is not a Mach-O file or an archive file",
1356 "Mach-O universal file");
13381357 }
13391358 }
13401359 return;
16201620 report_error(Filename, StringRef(), std::move(E),
16211621 ArchitectureName);
16221622 continue;
1623 } else if (ErrorOr> AOrErr =
1623 } else if (Expected> AOrErr =
16241624 I->getAsArchive()) {
16251625 std::unique_ptr &A = *AOrErr;
16261626 outs() << "Archive : " << Filename;
16451645 dyn_cast(&*ChildOrErr.get()))
16461646 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
16471647 }
1648 } else {
1649 consumeError(AOrErr.takeError());
1650 error("Mach-O universal file: " + Filename + " for " +
1651 "architecture " + StringRef(I->getArchTypeName()) +
1652 " is not a Mach-O file or an archive file");
16481653 }
16491654 }
16501655 }
16751680 ObjOrErr.takeError())) {
16761681 report_error(Filename, std::move(E));
16771682 continue;
1678 } else if (ErrorOr> AOrErr =
1683 } else if (Expected> AOrErr =
16791684 I->getAsArchive()) {
16801685 std::unique_ptr &A = *AOrErr;
16811686 outs() << "Archive : " << Filename << "\n";
16971702 dyn_cast(&*ChildOrErr.get()))
16981703 ProcessMachO(Filename, O, O->getFileName());
16991704 }
1705 } else {
1706 consumeError(AOrErr.takeError());
1707 error("Mach-O universal file: " + Filename + " for architecture " +
1708 StringRef(I->getArchTypeName()) +
1709 " is not a Mach-O file or an archive file");
17001710 }
17011711 return;
17021712 }
17201730 ObjOrErr.takeError())) {
17211731 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
17221732 continue;
1723 } else if (ErrorOr> AOrErr = I->getAsArchive()) {
1733 } else if (Expected> AOrErr =
1734 I->getAsArchive()) {
17241735 std::unique_ptr &A = *AOrErr;
17251736 outs() << "Archive : " << Filename;
17261737 if (!ArchitectureName.empty())
17461757 ArchitectureName);
17471758 }
17481759 }
1760 } else {
1761 consumeError(AOrErr.takeError());
1762 error("Mach-O universal file: " + Filename + " for architecture " +
1763 StringRef(I->getArchTypeName()) +
1764 " is not a Mach-O file or an archive file");
17491765 }
17501766 }
17511767 return;
263263 exit(1);
264264 }
265265
266 LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) {
267 errs() << ToolName << ": " << Message << ".\n";
268 errs().flush();
269 exit(1);
270 }
271
266272 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
267273 std::error_code EC) {
268274 assert(EC);
8787 void PrintSectionContents(const object::ObjectFile *o);
8888 void PrintSymbolTable(const object::ObjectFile *o, StringRef ArchiveName,
8989 StringRef ArchitectureName = StringRef());
90 LLVM_ATTRIBUTE_NORETURN void error(Twine Message);
9091 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
9192 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, llvm::Error E);
9293 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef FileName,
458458 OS.flush();
459459 reportError(UBinary->getFileName(), Buf);
460460 }
461 else if (ErrorOr> AOrErr = Obj.getAsArchive())
461 else if (Expected> AOrErr = Obj.getAsArchive())
462462 dumpArchive(&*AOrErr.get());
463463 }
464464 }
9494
9595 HadError = true;
9696 errs() << ToolName << ": error reading file: " << ec.message() << ".\n";
97 errs().flush();
98 return true;
99 }
100
101 static bool error(Twine Message) {
102 HadError = true;
103 errs() << ToolName << ": " << Message << ".\n";
97104 errs().flush();
98105 return true;
99106 }
584591 error(std::move(E), file, ArchFlags.size() > 1 ?
585592 StringRef(I->getArchTypeName()) : StringRef());
586593 return;
587 } else if (ErrorOr> AOrErr =
594 } else if (Expected> AOrErr =
588595 I->getAsArchive()) {
589596 std::unique_ptr &UA = *AOrErr;
590597 // This is an archive. Iterate over each member and display its
629636 }
630637 }
631638 }
639 } else {
640 consumeError(AOrErr.takeError());
641 error("Mach-O universal file: " + file + " for architecture " +
642 StringRef(I->getArchTypeName()) +
643 " is not a Mach-O file or an archive file");
632644 }
633645 }
634646 }
670682 } else if (auto E = isNotObjectErrorInvalidFileType(UO.takeError())) {
671683 error(std::move(E), file);
672684 return;
673 } else if (ErrorOr> AOrErr =
685 } else if (Expected> AOrErr =
674686 I->getAsArchive()) {
675687 std::unique_ptr &UA = *AOrErr;
676688 // This is an archive. Iterate over each member and display its
708720 }
709721 }
710722 }
723 } else {
724 consumeError(AOrErr.takeError());
725 error("Mach-O universal file: " + file + " for architecture " +
726 StringRef(I->getArchTypeName()) +
727 " is not a Mach-O file or an archive file");
711728 }
712729 return;
713730 }
743760 error(std::move(E), file, MoreThanOneArch ?
744761 StringRef(I->getArchTypeName()) : StringRef());
745762 return;
746 } else if (ErrorOr> AOrErr =
763 } else if (Expected> AOrErr =
747764 I->getAsArchive()) {
748765 std::unique_ptr &UA = *AOrErr;
749766 // This is an archive. Iterate over each member and display its sizes.
780797 }
781798 }
782799 }
800 } else {
801 consumeError(AOrErr.takeError());
802 error("Mach-O universal file: " + file + " for architecture " +
803 StringRef(I->getArchTypeName()) +
804 " is not a Mach-O file or an archive file");
783805 }
784806 }
785807 } else if (ObjectFile *o = dyn_cast(&Bin)) {