Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 1 | //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines a new error_category for the Object library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Object/Error.h" |
| 15 | #include "llvm/Support/ErrorHandling.h" |
Chris Bieneman | 684981e | 2014-09-19 22:09:18 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace object; |
| 20 | |
| 21 | namespace { |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 22 | class _object_error_category : public std::error_category { |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 23 | public: |
Rafael Espindola | f5d07fa | 2014-06-10 21:26:47 +0000 | [diff] [blame] | 24 | const char* name() const LLVM_NOEXCEPT override; |
Justin Bogner | e3bfdc4 | 2014-03-15 04:05:59 +0000 | [diff] [blame] | 25 | std::string message(int ev) const override; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 26 | }; |
| 27 | } |
| 28 | |
Abramo Bagnara | 9c2f73e | 2014-08-18 07:48:18 +0000 | [diff] [blame] | 29 | const char *_object_error_category::name() const LLVM_NOEXCEPT { |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 30 | return "llvm.object"; |
| 31 | } |
| 32 | |
Rafael Espindola | e00fec8 | 2014-06-03 05:26:12 +0000 | [diff] [blame] | 33 | std::string _object_error_category::message(int EV) const { |
| 34 | object_error E = static_cast<object_error>(EV); |
Rafael Espindola | e107ade | 2013-06-18 13:30:31 +0000 | [diff] [blame] | 35 | switch (E) { |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 36 | case object_error::arch_not_found: |
| 37 | return "No object file for requested architecture"; |
Michael J. Spencer | e444692 | 2011-06-25 17:42:56 +0000 | [diff] [blame] | 38 | case object_error::invalid_file_type: |
| 39 | return "The file was not recognized as a valid object file"; |
| 40 | case object_error::parse_failed: |
| 41 | return "Invalid data was encountered while parsing the file"; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 42 | case object_error::unexpected_eof: |
| 43 | return "The end of the file was unexpectedly encountered"; |
Rafael Espindola | 6a1bfb2 | 2015-06-29 14:39:25 +0000 | [diff] [blame] | 44 | case object_error::string_table_non_null_end: |
| 45 | return "String table must end with a null terminator"; |
Rafael Espindola | 6def304 | 2015-07-01 12:56:27 +0000 | [diff] [blame] | 46 | case object_error::invalid_section_index: |
| 47 | return "Invalid section index"; |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 48 | case object_error::bitcode_section_not_found: |
| 49 | return "Bitcode section not found in object file"; |
Alexey Samsonov | 074da9b | 2015-06-04 20:08:52 +0000 | [diff] [blame] | 50 | case object_error::macho_load_segment_too_many_sections: |
| 51 | return "Mach-O segment load command contains too many sections"; |
| 52 | case object_error::macho_load_segment_too_small: |
| 53 | return "Mach-O segment load command size is too small"; |
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 | |
Lang Hames | 9e964f3 | 2016-03-25 17:25:34 +0000 | [diff] [blame] | 59 | char BinaryError::ID = 0; |
| 60 | char GenericBinaryError::ID = 0; |
| 61 | |
| 62 | GenericBinaryError::GenericBinaryError(std::string FileName, Twine Msg) |
| 63 | : FileName(std::move(FileName)), Msg(Msg.str()) {} |
| 64 | |
| 65 | GenericBinaryError::GenericBinaryError(std::string FileName, Twine Msg, object_error ECOverride) |
| 66 | : FileName(std::move(FileName)), Msg(Msg.str()) { |
| 67 | setErrorCode(make_error_code(ECOverride)); |
| 68 | } |
| 69 | |
| 70 | void GenericBinaryError::log(raw_ostream &OS) const { |
| 71 | OS << "Error in " << FileName << ": " << Msg; |
| 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 | } |