1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy */
26164190Sjkoshy
27164190Sjkoshy#include <sys/cdefs.h>
28164190Sjkoshy__FBSDID("$FreeBSD$");
29164190Sjkoshy
30164190Sjkoshy#include <libelf.h>
31164190Sjkoshy#include <string.h>
32164190Sjkoshy
33164190Sjkoshy#include "_libelf.h"
34164190Sjkoshy
35164190Sjkoshy/*
36164190Sjkoshy * Retrieve a human readable translation for an error message.
37164190Sjkoshy */
38164190Sjkoshy
39241720Sedstatic const char *_libelf_errors[] = {
40164190Sjkoshy#define	DEFINE_ERROR(N,S)	[ELF_E_##N] = S
41164190Sjkoshy	DEFINE_ERROR(NONE,	"No Error"),
42164190Sjkoshy	DEFINE_ERROR(ARCHIVE,	"Malformed ar(1) archive"),
43164190Sjkoshy	DEFINE_ERROR(ARGUMENT,	"Invalid argument"),
44164190Sjkoshy	DEFINE_ERROR(CLASS,	"ELF class mismatch"),
45164190Sjkoshy	DEFINE_ERROR(DATA,	"Invalid data buffer descriptor"),
46164190Sjkoshy	DEFINE_ERROR(HEADER,	"Missing or malformed ELF header"),
47164190Sjkoshy	DEFINE_ERROR(IO,	"I/O error"),
48164190Sjkoshy	DEFINE_ERROR(LAYOUT,	"Layout constraint violation"),
49164190Sjkoshy	DEFINE_ERROR(MODE,	"Incorrect ELF descriptor mode"),
50164190Sjkoshy	DEFINE_ERROR(RANGE,	"Value out of range of target"),
51164190Sjkoshy	DEFINE_ERROR(RESOURCE,	"Resource exhaustion"),
52164190Sjkoshy	DEFINE_ERROR(SECTION,	"Invalid section descriptor"),
53164190Sjkoshy	DEFINE_ERROR(SEQUENCE,	"API calls out of sequence"),
54164190Sjkoshy	DEFINE_ERROR(UNIMPL,	"Unimplemented feature"),
55164190Sjkoshy	DEFINE_ERROR(VERSION,	"Unknown ELF API version"),
56164190Sjkoshy	DEFINE_ERROR(NUM,	"Unknown error")
57164190Sjkoshy#undef	DEFINE_ERROR
58164190Sjkoshy};
59164190Sjkoshy
60164190Sjkoshyconst char *
61164190Sjkoshyelf_errmsg(int error)
62164190Sjkoshy{
63164190Sjkoshy	int oserr;
64164190Sjkoshy
65164190Sjkoshy	if (error == 0 && (error = LIBELF_PRIVATE(error)) == 0)
66164190Sjkoshy	    return NULL;
67164190Sjkoshy	else if (error == -1)
68164190Sjkoshy	    error = LIBELF_PRIVATE(error);
69164190Sjkoshy
70164190Sjkoshy	oserr = error >> LIBELF_OS_ERROR_SHIFT;
71164190Sjkoshy	error &= LIBELF_ELF_ERROR_MASK;
72164190Sjkoshy
73164190Sjkoshy	if (error < 0 || error >= ELF_E_NUM)
74164190Sjkoshy		return _libelf_errors[ELF_E_NUM];
75164190Sjkoshy	if (oserr) {
76164190Sjkoshy		strlcpy(LIBELF_PRIVATE(msg), _libelf_errors[error],
77164190Sjkoshy		    sizeof(LIBELF_PRIVATE(msg)));
78165032Sjkoshy		strlcat(LIBELF_PRIVATE(msg), ": ", sizeof(LIBELF_PRIVATE(msg)));
79164190Sjkoshy		strlcat(LIBELF_PRIVATE(msg), strerror(oserr),
80164190Sjkoshy		    sizeof(LIBELF_PRIVATE(msg)));
81164190Sjkoshy		return (const char *)&LIBELF_PRIVATE(msg);
82164190Sjkoshy	}
83164190Sjkoshy	return _libelf_errors[error];
84164190Sjkoshy}
85164190Sjkoshy
86164190Sjkoshy#if	defined(LIBELF_TEST_HOOKS)
87164190Sjkoshy
88164190Sjkoshyconst char *
89164190Sjkoshy_libelf_get_unknown_error_message(void)
90164190Sjkoshy{
91164190Sjkoshy	return _libelf_errors[ELF_E_NUM];
92164190Sjkoshy}
93164190Sjkoshy
94164190Sjkoshyconst char *
95164190Sjkoshy_libelf_get_no_error_message(void)
96164190Sjkoshy{
97164190Sjkoshy	return _libelf_errors[0];
98164190Sjkoshy}
99164190Sjkoshy
100164190Sjkoshy#endif	/* LIBELF_TEST_HOOKS */
101