blob: 297418c95a97fb75f07d0e4dcdd092a7add3a356 [file] [log] [blame]
Michael J. Spencere4446922011-06-25 17:42:56 +00001//===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This defines a new error_category for the Object library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Object/Error.h"
15#include "llvm/Support/ErrorHandling.h"
Chris Bieneman684981e2014-09-19 22:09:18 +000016#include "llvm/Support/ManagedStatic.h"
Michael J. Spencere4446922011-06-25 17:42:56 +000017
18using namespace llvm;
19using namespace object;
20
21namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +000022class _object_error_category : public std::error_category {
Michael J. Spencere4446922011-06-25 17:42:56 +000023public:
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +000024 const char* name() const LLVM_NOEXCEPT override;
Justin Bognere3bfdc42014-03-15 04:05:59 +000025 std::string message(int ev) const override;
Michael J. Spencere4446922011-06-25 17:42:56 +000026};
27}
28
Abramo Bagnara9c2f73e2014-08-18 07:48:18 +000029const char *_object_error_category::name() const LLVM_NOEXCEPT {
Michael J. Spencere4446922011-06-25 17:42:56 +000030 return "llvm.object";
31}
32
Rafael Espindolae00fec82014-06-03 05:26:12 +000033std::string _object_error_category::message(int EV) const {
34 object_error E = static_cast<object_error>(EV);
Rafael Espindolae107ade2013-06-18 13:30:31 +000035 switch (E) {
Alexey Samsonove6388e62013-06-18 15:03:28 +000036 case object_error::arch_not_found:
37 return "No object file for requested architecture";
Michael J. Spencere4446922011-06-25 17:42:56 +000038 case object_error::invalid_file_type:
39 return "The file was not recognized as a valid object file";
40 case object_error::parse_failed:
41 return "Invalid data was encountered while parsing the file";
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000042 case object_error::unexpected_eof:
43 return "The end of the file was unexpectedly encountered";
Rafael Espindola6a1bfb22015-06-29 14:39:25 +000044 case object_error::string_table_non_null_end:
45 return "String table must end with a null terminator";
Rafael Espindola6def3042015-07-01 12:56:27 +000046 case object_error::invalid_section_index:
47 return "Invalid section index";
Peter Collingbourne10039c02014-09-18 21:28:49 +000048 case object_error::bitcode_section_not_found:
49 return "Bitcode section not found in object file";
Alexey Samsonov074da9b2015-06-04 20:08:52 +000050 case object_error::macho_load_segment_too_many_sections:
51 return "Mach-O segment load command contains too many sections";
52 case object_error::macho_load_segment_too_small:
53 return "Mach-O segment load command size is too small";
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
Lang Hames9e964f32016-03-25 17:25:34 +000059char BinaryError::ID = 0;
60char GenericBinaryError::ID = 0;
61
62GenericBinaryError::GenericBinaryError(std::string FileName, Twine Msg)
63 : FileName(std::move(FileName)), Msg(Msg.str()) {}
64
65GenericBinaryError::GenericBinaryError(std::string FileName, Twine Msg, object_error ECOverride)
66 : FileName(std::move(FileName)), Msg(Msg.str()) {
67 setErrorCode(make_error_code(ECOverride));
68}
69
70void GenericBinaryError::log(raw_ostream &OS) const {
71 OS << "Error in " << FileName << ": " << Msg;
72}
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}