1411bd29eSDimitry Andric //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===// 2411bd29eSDimitry Andric // 3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6411bd29eSDimitry Andric // 7411bd29eSDimitry Andric //===----------------------------------------------------------------------===// 8411bd29eSDimitry Andric // 9411bd29eSDimitry Andric // This defines a new error_category for the Object library. 10411bd29eSDimitry Andric // 11411bd29eSDimitry Andric //===----------------------------------------------------------------------===// 12411bd29eSDimitry Andric 13411bd29eSDimitry Andric #include "llvm/Object/Error.h" 14cfca06d7SDimitry Andric #include "llvm/ADT/Twine.h" 15411bd29eSDimitry Andric #include "llvm/Support/ErrorHandling.h" 16411bd29eSDimitry Andric 17411bd29eSDimitry Andric using namespace llvm; 18411bd29eSDimitry Andric using namespace object; 19411bd29eSDimitry Andric 20411bd29eSDimitry Andric namespace { 2101095a5dSDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 2201095a5dSDimitry Andric // will be removed once this transition is complete. Clients should prefer to 2301095a5dSDimitry Andric // deal with the Error value directly, rather than converting to error_code. 245ca98fd9SDimitry Andric class _object_error_category : public std::error_category { 25411bd29eSDimitry Andric public: 26b915e9e0SDimitry Andric const char* name() const noexcept override; 275ca98fd9SDimitry Andric std::string message(int ev) const override; 28411bd29eSDimitry Andric }; 29411bd29eSDimitry Andric } 30411bd29eSDimitry Andric name() const31b915e9e0SDimitry Andricconst char *_object_error_category::name() const noexcept { 32411bd29eSDimitry Andric return "llvm.object"; 33411bd29eSDimitry Andric } 34411bd29eSDimitry Andric message(int EV) const355ca98fd9SDimitry Andricstd::string _object_error_category::message(int EV) const { 365ca98fd9SDimitry Andric object_error E = static_cast<object_error>(EV); 37f8af5cf6SDimitry Andric switch (E) { 38f8af5cf6SDimitry Andric case object_error::arch_not_found: 39f8af5cf6SDimitry Andric return "No object file for requested architecture"; 40411bd29eSDimitry Andric case object_error::invalid_file_type: 41411bd29eSDimitry Andric return "The file was not recognized as a valid object file"; 42411bd29eSDimitry Andric case object_error::parse_failed: 43411bd29eSDimitry Andric return "Invalid data was encountered while parsing the file"; 44411bd29eSDimitry Andric case object_error::unexpected_eof: 45411bd29eSDimitry Andric return "The end of the file was unexpectedly encountered"; 461a82d4c0SDimitry Andric case object_error::string_table_non_null_end: 471a82d4c0SDimitry Andric return "String table must end with a null terminator"; 481a82d4c0SDimitry Andric case object_error::invalid_section_index: 491a82d4c0SDimitry Andric return "Invalid section index"; 5067c32a98SDimitry Andric case object_error::bitcode_section_not_found: 5167c32a98SDimitry Andric return "Bitcode section not found in object file"; 52b915e9e0SDimitry Andric case object_error::invalid_symbol_index: 53b915e9e0SDimitry Andric return "Invalid symbol index"; 54145449b1SDimitry Andric case object_error::section_stripped: 55145449b1SDimitry Andric return "Section has been stripped from the object file"; 56f8af5cf6SDimitry Andric } 57411bd29eSDimitry Andric llvm_unreachable("An enumerator of object_error does not have a message " 58411bd29eSDimitry Andric "defined."); 59411bd29eSDimitry Andric } 60411bd29eSDimitry Andric anchor()61d8e91e46SDimitry Andricvoid BinaryError::anchor() {} 6201095a5dSDimitry Andric char BinaryError::ID = 0; 6301095a5dSDimitry Andric char GenericBinaryError::ID = 0; 6401095a5dSDimitry Andric GenericBinaryError(const Twine & Msg)65cfca06d7SDimitry AndricGenericBinaryError::GenericBinaryError(const Twine &Msg) : Msg(Msg.str()) {} 6601095a5dSDimitry Andric GenericBinaryError(const Twine & Msg,object_error ECOverride)67cfca06d7SDimitry AndricGenericBinaryError::GenericBinaryError(const Twine &Msg, 68cfca06d7SDimitry Andric object_error ECOverride) 6901095a5dSDimitry Andric : Msg(Msg.str()) { 7001095a5dSDimitry Andric setErrorCode(make_error_code(ECOverride)); 7101095a5dSDimitry Andric } 7201095a5dSDimitry Andric log(raw_ostream & OS) const7301095a5dSDimitry Andricvoid GenericBinaryError::log(raw_ostream &OS) const { 7401095a5dSDimitry Andric OS << Msg; 7501095a5dSDimitry Andric } 7601095a5dSDimitry Andric object_category()775ca98fd9SDimitry Andricconst std::error_category &object::object_category() { 781f917f69SDimitry Andric static _object_error_category error_category; 791f917f69SDimitry Andric return error_category; 80411bd29eSDimitry Andric } 8101095a5dSDimitry Andric isNotObjectErrorInvalidFileType(llvm::Error Err)8201095a5dSDimitry Andricllvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) { 83e6d15924SDimitry Andric return handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error { 8401095a5dSDimitry Andric // Try to handle 'M'. If successful, return a success value from 8501095a5dSDimitry Andric // the handler. 8601095a5dSDimitry Andric if (M->convertToErrorCode() == object_error::invalid_file_type) 8701095a5dSDimitry Andric return Error::success(); 8801095a5dSDimitry Andric 8901095a5dSDimitry Andric // We failed to handle 'M' - return it from the handler. 9001095a5dSDimitry Andric // This value will be passed back from catchErrors and 9101095a5dSDimitry Andric // wind up in Err2, where it will be returned from this function. 9201095a5dSDimitry Andric return Error(std::move(M)); 93e6d15924SDimitry Andric }); 9401095a5dSDimitry Andric } 95