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 
17*0b57cec5SDimitry Andric using namespace llvm;
18*0b57cec5SDimitry Andric using namespace object;
19*0b57cec5SDimitry Andric 
20*0b57cec5SDimitry Andric namespace {
21*0b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It
22*0b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to
23*0b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code.
24*0b57cec5SDimitry Andric class _object_error_category : public std::error_category {
25*0b57cec5SDimitry Andric public:
26*0b57cec5SDimitry Andric   const char* name() const noexcept override;
27*0b57cec5SDimitry Andric   std::string message(int ev) const override;
28*0b57cec5SDimitry Andric };
29*0b57cec5SDimitry Andric }
30*0b57cec5SDimitry Andric 
name() const31*0b57cec5SDimitry Andric const char *_object_error_category::name() const noexcept {
32*0b57cec5SDimitry Andric   return "llvm.object";
33*0b57cec5SDimitry Andric }
34*0b57cec5SDimitry Andric 
message(int EV) const35*0b57cec5SDimitry Andric std::string _object_error_category::message(int EV) const {
36*0b57cec5SDimitry Andric   object_error E = static_cast<object_error>(EV);
37*0b57cec5SDimitry Andric   switch (E) {
38*0b57cec5SDimitry Andric   case object_error::arch_not_found:
39*0b57cec5SDimitry Andric     return "No object file for requested architecture";
40*0b57cec5SDimitry Andric   case object_error::invalid_file_type:
41*0b57cec5SDimitry Andric     return "The file was not recognized as a valid object file";
42*0b57cec5SDimitry Andric   case object_error::parse_failed:
43*0b57cec5SDimitry Andric     return "Invalid data was encountered while parsing the file";
44*0b57cec5SDimitry Andric   case object_error::unexpected_eof:
45*0b57cec5SDimitry Andric     return "The end of the file was unexpectedly encountered";
46*0b57cec5SDimitry Andric   case object_error::string_table_non_null_end:
47*0b57cec5SDimitry Andric     return "String table must end with a null terminator";
48*0b57cec5SDimitry Andric   case object_error::invalid_section_index:
49*0b57cec5SDimitry Andric     return "Invalid section index";
50*0b57cec5SDimitry Andric   case object_error::bitcode_section_not_found:
51*0b57cec5SDimitry Andric     return "Bitcode section not found in object file";
52*0b57cec5SDimitry Andric   case object_error::invalid_symbol_index:
53*0b57cec5SDimitry Andric     return "Invalid symbol index";
54*0b57cec5SDimitry Andric   case object_error::section_stripped:
55*0b57cec5SDimitry Andric     return "Section has been stripped from the object file";
56*0b57cec5SDimitry Andric   }
57*0b57cec5SDimitry Andric   llvm_unreachable("An enumerator of object_error does not have a message "
58*0b57cec5SDimitry Andric                    "defined.");
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric 
anchor()61*0b57cec5SDimitry Andric void BinaryError::anchor() {}
62*0b57cec5SDimitry Andric char BinaryError::ID = 0;
63*0b57cec5SDimitry Andric char GenericBinaryError::ID = 0;
64*0b57cec5SDimitry Andric 
GenericBinaryError(const Twine & Msg)65*0b57cec5SDimitry Andric GenericBinaryError::GenericBinaryError(const Twine &Msg) : Msg(Msg.str()) {}
66*0b57cec5SDimitry Andric 
GenericBinaryError(const Twine & Msg,object_error ECOverride)67*0b57cec5SDimitry Andric GenericBinaryError::GenericBinaryError(const Twine &Msg,
68*0b57cec5SDimitry Andric                                        object_error ECOverride)
69*0b57cec5SDimitry Andric     : Msg(Msg.str()) {
70*0b57cec5SDimitry Andric   setErrorCode(make_error_code(ECOverride));
71*0b57cec5SDimitry Andric }
72*0b57cec5SDimitry Andric 
log(raw_ostream & OS) const73*0b57cec5SDimitry Andric void GenericBinaryError::log(raw_ostream &OS) const {
74*0b57cec5SDimitry Andric   OS << Msg;
75*0b57cec5SDimitry Andric }
76*0b57cec5SDimitry Andric 
object_category()77*0b57cec5SDimitry Andric const std::error_category &object::object_category() {
78*0b57cec5SDimitry Andric   static _object_error_category error_category;
79*0b57cec5SDimitry Andric   return error_category;
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric 
isNotObjectErrorInvalidFileType(llvm::Error Err)82*0b57cec5SDimitry Andric llvm::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