Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 1 | //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This defines a new error_category for the Object library. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Object/Error.h" |
| 14 | #include "llvm/Support/ErrorHandling.h" |
Chris Bieneman | 684981e | 2014-09-19 22:09:18 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | using namespace object; |
| 19 | |
| 20 | namespace { |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 21 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 22 | // will be removed once this transition is complete. Clients should prefer to |
| 23 | // deal with the Error value directly, rather than converting to error_code. |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 24 | class _object_error_category : public std::error_category { |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 25 | public: |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 26 | const char* name() const noexcept override; |
Justin Bogner | e3bfdc4 | 2014-03-15 04:05:59 +0000 | [diff] [blame] | 27 | std::string message(int ev) const override; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 28 | }; |
| 29 | } |
| 30 | |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 31 | const char *_object_error_category::name() const noexcept { |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 32 | return "llvm.object"; |
| 33 | } |
| 34 | |
Rafael Espindola | e00fec8 | 2014-06-03 05:26:12 +0000 | [diff] [blame] | 35 | std::string _object_error_category::message(int EV) const { |
| 36 | object_error E = static_cast<object_error>(EV); |
Rafael Espindola | e107ade | 2013-06-18 13:30:31 +0000 | [diff] [blame] | 37 | switch (E) { |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 38 | case object_error::arch_not_found: |
| 39 | return "No object file for requested architecture"; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 40 | case object_error::invalid_file_type: |
| 41 | return "The file was not recognized as a valid object file"; |
| 42 | case object_error::parse_failed: |
| 43 | return "Invalid data was encountered while parsing the file"; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 44 | case object_error::unexpected_eof: |
| 45 | return "The end of the file was unexpectedly encountered"; |
Rafael Espindola | 6a1bfb2 | 2015-06-29 14:39:25 +0000 | [diff] [blame] | 46 | case object_error::string_table_non_null_end: |
| 47 | return "String table must end with a null terminator"; |
Rafael Espindola | 6def304 | 2015-07-01 12:56:27 +0000 | [diff] [blame] | 48 | case object_error::invalid_section_index: |
| 49 | return "Invalid section index"; |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 50 | case object_error::bitcode_section_not_found: |
| 51 | return "Bitcode section not found in object file"; |
George Rimar | e1924f0 | 2016-11-03 08:40:55 +0000 | [diff] [blame] | 52 | case object_error::invalid_symbol_index: |
| 53 | return "Invalid symbol index"; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 54 | } |
Rafael Espindola | e107ade | 2013-06-18 13:30:31 +0000 | [diff] [blame] | 55 | llvm_unreachable("An enumerator of object_error does not have a message " |
| 56 | "defined."); |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Richard Trieu | a87b70d | 2018-12-29 02:02:13 +0000 | [diff] [blame] | 59 | void BinaryError::anchor() {} |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 60 | char BinaryError::ID = 0; |
| 61 | char GenericBinaryError::ID = 0; |
| 62 | |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 63 | GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {} |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 64 | |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 65 | GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride) |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 66 | : Msg(Msg.str()) { |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 67 | setErrorCode(make_error_code(ECOverride)); |
| 68 | } |
| 69 | |
| 70 | void GenericBinaryError::log(raw_ostream &OS) const { |
Kevin Enderby | d4e075b | 2016-05-06 20:16:28 +0000 | [diff] [blame] | 71 | OS << Msg; |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Bieneman | 684981e | 2014-09-19 22:09:18 +0000 | [diff] [blame] | 74 | static ManagedStatic<_object_error_category> error_category; |
| 75 | |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 76 | const std::error_category &object::object_category() { |
Chris Bieneman | 684981e | 2014-09-19 22:09:18 +0000 | [diff] [blame] | 77 | return *error_category; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 78 | } |
Lang Hames | 8a63b2a | 2016-05-17 21:38:53 +0000 | [diff] [blame] | 79 | |
| 80 | llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) { |
| 81 | if (auto Err2 = |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 82 | handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error { |
| 83 | // Try to handle 'M'. If successful, return a success value from |
| 84 | // the handler. |
| 85 | if (M->convertToErrorCode() == object_error::invalid_file_type) |
| 86 | return Error::success(); |
Lang Hames | 8a63b2a | 2016-05-17 21:38:53 +0000 | [diff] [blame] | 87 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 88 | // We failed to handle 'M' - return it from the handler. |
| 89 | // This value will be passed back from catchErrors and |
| 90 | // wind up in Err2, where it will be returned from this function. |
| 91 | return Error(std::move(M)); |
| 92 | })) |
Lang Hames | 8a63b2a | 2016-05-17 21:38:53 +0000 | [diff] [blame] | 93 | return Err2; |
| 94 | return Err; |
| 95 | } |