blob: f2a009000c58de4879fe907f0cb5c7a12f1bdf31 [file] [log] [blame]
Michael J. Spencere4446922011-06-25 17:42:56 +00001//===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael J. Spencere4446922011-06-25 17:42:56 +00006//
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/Support/ErrorHandling.h"
Chris Bieneman684981e2014-09-19 22:09:18 +000015#include "llvm/Support/ManagedStatic.h"
Michael J. Spencere4446922011-06-25 17:42:56 +000016
17using namespace llvm;
18using namespace object;
19
20namespace {
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000021// FIXME: This class is only here to support the transition to llvm::Error. It
22// will be removed once this transition is complete. Clients should prefer to
23// deal with the Error value directly, rather than converting to error_code.
Rafael Espindola25188c92014-06-12 01:45:43 +000024class _object_error_category : public std::error_category {
Michael J. Spencere4446922011-06-25 17:42:56 +000025public:
Reid Kleckner990504e2016-10-19 23:52:38 +000026 const char* name() const noexcept override;
Justin Bognere3bfdc42014-03-15 04:05:59 +000027 std::string message(int ev) const override;
Michael J. Spencere4446922011-06-25 17:42:56 +000028};
29}
30
Reid Kleckner990504e2016-10-19 23:52:38 +000031const char *_object_error_category::name() const noexcept {
Michael J. Spencere4446922011-06-25 17:42:56 +000032 return "llvm.object";
33}
34
Rafael Espindolae00fec82014-06-03 05:26:12 +000035std::string _object_error_category::message(int EV) const {
36 object_error E = static_cast<object_error>(EV);
Rafael Espindolae107ade2013-06-18 13:30:31 +000037 switch (E) {
Alexey Samsonove6388e62013-06-18 15:03:28 +000038 case object_error::arch_not_found:
39 return "No object file for requested architecture";
Michael J. Spencere4446922011-06-25 17:42:56 +000040 case object_error::invalid_file_type:
41 return "The file was not recognized as a valid object file";
42 case object_error::parse_failed:
43 return "Invalid data was encountered while parsing the file";
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000044 case object_error::unexpected_eof:
45 return "The end of the file was unexpectedly encountered";
Rafael Espindola6a1bfb22015-06-29 14:39:25 +000046 case object_error::string_table_non_null_end:
47 return "String table must end with a null terminator";
Rafael Espindola6def3042015-07-01 12:56:27 +000048 case object_error::invalid_section_index:
49 return "Invalid section index";
Peter Collingbourne10039c02014-09-18 21:28:49 +000050 case object_error::bitcode_section_not_found:
51 return "Bitcode section not found in object file";
George Rimare1924f02016-11-03 08:40:55 +000052 case object_error::invalid_symbol_index:
53 return "Invalid symbol index";
Michael J. Spencere4446922011-06-25 17:42:56 +000054 }
Rafael Espindolae107ade2013-06-18 13:30:31 +000055 llvm_unreachable("An enumerator of object_error does not have a message "
56 "defined.");
Michael J. Spencere4446922011-06-25 17:42:56 +000057}
58
Richard Trieua87b70d2018-12-29 02:02:13 +000059void BinaryError::anchor() {}
Lang Hames9e964f32016-03-25 17:25:34 +000060char BinaryError::ID = 0;
61char GenericBinaryError::ID = 0;
62
Zachary Turner41a9ee92017-10-11 23:54:34 +000063GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {}
Lang Hames9e964f32016-03-25 17:25:34 +000064
Zachary Turner41a9ee92017-10-11 23:54:34 +000065GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride)
Kevin Enderbyd4e075b2016-05-06 20:16:28 +000066 : Msg(Msg.str()) {
Lang Hames9e964f32016-03-25 17:25:34 +000067 setErrorCode(make_error_code(ECOverride));
68}
69
70void GenericBinaryError::log(raw_ostream &OS) const {
Kevin Enderbyd4e075b2016-05-06 20:16:28 +000071 OS << Msg;
Lang Hames9e964f32016-03-25 17:25:34 +000072}
73
Chris Bieneman684981e2014-09-19 22:09:18 +000074static ManagedStatic<_object_error_category> error_category;
75
Rafael Espindola25188c92014-06-12 01:45:43 +000076const std::error_category &object::object_category() {
Chris Bieneman684981e2014-09-19 22:09:18 +000077 return *error_category;
Michael J. Spencere4446922011-06-25 17:42:56 +000078}
Lang Hames8a63b2a2016-05-17 21:38:53 +000079
80llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) {
81 if (auto Err2 =
Mehdi Amini41af4302016-11-11 04:28:40 +000082 handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error {
83 // Try to handle 'M'. If successful, return a success value from
84 // the handler.
85 if (M->convertToErrorCode() == object_error::invalid_file_type)
86 return Error::success();
Lang Hames8a63b2a2016-05-17 21:38:53 +000087
Mehdi Amini41af4302016-11-11 04:28:40 +000088 // We failed to handle 'M' - return it from the handler.
89 // This value will be passed back from catchErrors and
90 // wind up in Err2, where it will be returned from this function.
91 return Error(std::move(M));
92 }))
Lang Hames8a63b2a2016-05-17 21:38:53 +000093 return Err2;
94 return Err;
95}