1*0b57cec5SDimitry Andric //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This defines a new error_category for the Object library. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "llvm/Object/Error.h" 14*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 15*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 16*0b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h" 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric using namespace llvm; 19*0b57cec5SDimitry Andric using namespace object; 20*0b57cec5SDimitry Andric 21*0b57cec5SDimitry Andric namespace { 22*0b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 23*0b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 24*0b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 25*0b57cec5SDimitry Andric class _object_error_category : public std::error_category { 26*0b57cec5SDimitry Andric public: 27*0b57cec5SDimitry Andric const char* name() const noexcept override; 28*0b57cec5SDimitry Andric std::string message(int ev) const override; 29*0b57cec5SDimitry Andric }; 30*0b57cec5SDimitry Andric } 31*0b57cec5SDimitry Andric name() const32*0b57cec5SDimitry Andricconst char *_object_error_category::name() const noexcept { 33*0b57cec5SDimitry Andric return "llvm.object"; 34*0b57cec5SDimitry Andric } 35*0b57cec5SDimitry Andric message(int EV) const36*0b57cec5SDimitry Andricstd::string _object_error_category::message(int EV) const { 37*0b57cec5SDimitry Andric object_error E = static_cast<object_error>(EV); 38*0b57cec5SDimitry Andric switch (E) { 39*0b57cec5SDimitry Andric case object_error::arch_not_found: 40*0b57cec5SDimitry Andric return "No object file for requested architecture"; 41*0b57cec5SDimitry Andric case object_error::invalid_file_type: 42*0b57cec5SDimitry Andric return "The file was not recognized as a valid object file"; 43*0b57cec5SDimitry Andric case object_error::parse_failed: 44*0b57cec5SDimitry Andric return "Invalid data was encountered while parsing the file"; 45*0b57cec5SDimitry Andric case object_error::unexpected_eof: 46*0b57cec5SDimitry Andric return "The end of the file was unexpectedly encountered"; 47*0b57cec5SDimitry Andric case object_error::string_table_non_null_end: 48*0b57cec5SDimitry Andric return "String table must end with a null terminator"; 49*0b57cec5SDimitry Andric case object_error::invalid_section_index: 50*0b57cec5SDimitry Andric return "Invalid section index"; 51*0b57cec5SDimitry Andric case object_error::bitcode_section_not_found: 52*0b57cec5SDimitry Andric return "Bitcode section not found in object file"; 53*0b57cec5SDimitry Andric case object_error::invalid_symbol_index: 54*0b57cec5SDimitry Andric return "Invalid symbol index"; 55*0b57cec5SDimitry Andric } 56*0b57cec5SDimitry Andric llvm_unreachable("An enumerator of object_error does not have a message " 57*0b57cec5SDimitry Andric "defined."); 58*0b57cec5SDimitry Andric } 59*0b57cec5SDimitry Andric anchor()60*0b57cec5SDimitry Andricvoid BinaryError::anchor() {} 61*0b57cec5SDimitry Andric char BinaryError::ID = 0; 62*0b57cec5SDimitry Andric char GenericBinaryError::ID = 0; 63*0b57cec5SDimitry Andric GenericBinaryError(const Twine & Msg)64*0b57cec5SDimitry AndricGenericBinaryError::GenericBinaryError(const Twine &Msg) : Msg(Msg.str()) {} 65*0b57cec5SDimitry Andric GenericBinaryError(const Twine & Msg,object_error ECOverride)66*0b57cec5SDimitry AndricGenericBinaryError::GenericBinaryError(const Twine &Msg, 67*0b57cec5SDimitry Andric object_error ECOverride) 68*0b57cec5SDimitry Andric : Msg(Msg.str()) { 69*0b57cec5SDimitry Andric setErrorCode(make_error_code(ECOverride)); 70*0b57cec5SDimitry Andric } 71*0b57cec5SDimitry Andric log(raw_ostream & OS) const72*0b57cec5SDimitry Andricvoid GenericBinaryError::log(raw_ostream &OS) const { 73*0b57cec5SDimitry Andric OS << Msg; 74*0b57cec5SDimitry Andric } 75*0b57cec5SDimitry Andric 76*0b57cec5SDimitry Andric static ManagedStatic<_object_error_category> error_category; 77*0b57cec5SDimitry Andric object_category()78*0b57cec5SDimitry Andricconst std::error_category &object::object_category() { 79*0b57cec5SDimitry Andric return *error_category; 80*0b57cec5SDimitry Andric } 81*0b57cec5SDimitry Andric isNotObjectErrorInvalidFileType(llvm::Error Err)82*0b57cec5SDimitry Andricllvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) { 83*0b57cec5SDimitry Andric return handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error { 84*0b57cec5SDimitry Andric // Try to handle 'M'. If successful, return a success value from 85*0b57cec5SDimitry Andric // the handler. 86*0b57cec5SDimitry Andric if (M->convertToErrorCode() == object_error::invalid_file_type) 87*0b57cec5SDimitry Andric return Error::success(); 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric // We failed to handle 'M' - return it from the handler. 90*0b57cec5SDimitry Andric // This value will be passed back from catchErrors and 91*0b57cec5SDimitry Andric // wind up in Err2, where it will be returned from this function. 92*0b57cec5SDimitry Andric return Error(std::move(M)); 93 }); 94 } 95