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