10b57cec5SDimitry Andric //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This defines a new error_category for the Object library. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/Object/Error.h" 145ffd83dbSDimitry Andric #include "llvm/ADT/Twine.h" 150b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric using namespace llvm; 180b57cec5SDimitry Andric using namespace object; 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric namespace { 210b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 220b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 230b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 240b57cec5SDimitry Andric class _object_error_category : public std::error_category { 250b57cec5SDimitry Andric public: 260b57cec5SDimitry Andric const char* name() const noexcept override; 270b57cec5SDimitry Andric std::string message(int ev) const override; 280b57cec5SDimitry Andric }; 290b57cec5SDimitry Andric } 300b57cec5SDimitry Andric name() const310b57cec5SDimitry Andricconst char *_object_error_category::name() const noexcept { 320b57cec5SDimitry Andric return "llvm.object"; 330b57cec5SDimitry Andric } 340b57cec5SDimitry Andric message(int EV) const350b57cec5SDimitry Andricstd::string _object_error_category::message(int EV) const { 360b57cec5SDimitry Andric object_error E = static_cast<object_error>(EV); 370b57cec5SDimitry Andric switch (E) { 380b57cec5SDimitry Andric case object_error::arch_not_found: 390b57cec5SDimitry Andric return "No object file for requested architecture"; 400b57cec5SDimitry Andric case object_error::invalid_file_type: 410b57cec5SDimitry Andric return "The file was not recognized as a valid object file"; 420b57cec5SDimitry Andric case object_error::parse_failed: 430b57cec5SDimitry Andric return "Invalid data was encountered while parsing the file"; 440b57cec5SDimitry Andric case object_error::unexpected_eof: 450b57cec5SDimitry Andric return "The end of the file was unexpectedly encountered"; 460b57cec5SDimitry Andric case object_error::string_table_non_null_end: 470b57cec5SDimitry Andric return "String table must end with a null terminator"; 480b57cec5SDimitry Andric case object_error::invalid_section_index: 490b57cec5SDimitry Andric return "Invalid section index"; 500b57cec5SDimitry Andric case object_error::bitcode_section_not_found: 510b57cec5SDimitry Andric return "Bitcode section not found in object file"; 520b57cec5SDimitry Andric case object_error::invalid_symbol_index: 530b57cec5SDimitry Andric return "Invalid symbol index"; 5481ad6265SDimitry Andric case object_error::section_stripped: 5581ad6265SDimitry Andric return "Section has been stripped from the object file"; 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric llvm_unreachable("An enumerator of object_error does not have a message " 580b57cec5SDimitry Andric "defined."); 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric anchor()610b57cec5SDimitry Andricvoid BinaryError::anchor() {} 620b57cec5SDimitry Andric char BinaryError::ID = 0; 630b57cec5SDimitry Andric char GenericBinaryError::ID = 0; 640b57cec5SDimitry Andric GenericBinaryError(const Twine & Msg)655ffd83dbSDimitry AndricGenericBinaryError::GenericBinaryError(const Twine &Msg) : Msg(Msg.str()) {} 660b57cec5SDimitry Andric GenericBinaryError(const Twine & Msg,object_error ECOverride)675ffd83dbSDimitry AndricGenericBinaryError::GenericBinaryError(const Twine &Msg, 685ffd83dbSDimitry Andric object_error ECOverride) 690b57cec5SDimitry Andric : Msg(Msg.str()) { 700b57cec5SDimitry Andric setErrorCode(make_error_code(ECOverride)); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric log(raw_ostream & OS) const730b57cec5SDimitry Andricvoid GenericBinaryError::log(raw_ostream &OS) const { 740b57cec5SDimitry Andric OS << Msg; 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric object_category()770b57cec5SDimitry Andricconst std::error_category &object::object_category() { 78753f127fSDimitry Andric static _object_error_category error_category; 79753f127fSDimitry Andric return error_category; 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric isNotObjectErrorInvalidFileType(llvm::Error Err)820b57cec5SDimitry Andricllvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) { 830b57cec5SDimitry Andric return handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error { 840b57cec5SDimitry Andric // Try to handle 'M'. If successful, return a success value from 850b57cec5SDimitry Andric // the handler. 860b57cec5SDimitry Andric if (M->convertToErrorCode() == object_error::invalid_file_type) 870b57cec5SDimitry Andric return Error::success(); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric // We failed to handle 'M' - return it from the handler. 900b57cec5SDimitry Andric // This value will be passed back from catchErrors and 910b57cec5SDimitry Andric // wind up in Err2, where it will be returned from this function. 920b57cec5SDimitry Andric return Error(std::move(M)); 930b57cec5SDimitry Andric }); 940b57cec5SDimitry Andric } 95