xref: /openbsd-src/lib/libelf/elf_errmsg.c (revision a1b5ec256a03e12d667837fca8bf42f20675916f)
1*a1b5ec25Sjsg /*-
2*a1b5ec25Sjsg  * Copyright (c) 2006,2008,2011 Joseph Koshy
3*a1b5ec25Sjsg  * All rights reserved.
4*a1b5ec25Sjsg  *
5*a1b5ec25Sjsg  * Redistribution and use in source and binary forms, with or without
6*a1b5ec25Sjsg  * modification, are permitted provided that the following conditions
7*a1b5ec25Sjsg  * are met:
8*a1b5ec25Sjsg  * 1. Redistributions of source code must retain the above copyright
9*a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer.
10*a1b5ec25Sjsg  * 2. Redistributions in binary form must reproduce the above copyright
11*a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer in the
12*a1b5ec25Sjsg  *    documentation and/or other materials provided with the distribution.
13*a1b5ec25Sjsg  *
14*a1b5ec25Sjsg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*a1b5ec25Sjsg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*a1b5ec25Sjsg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*a1b5ec25Sjsg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*a1b5ec25Sjsg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*a1b5ec25Sjsg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*a1b5ec25Sjsg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*a1b5ec25Sjsg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*a1b5ec25Sjsg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*a1b5ec25Sjsg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*a1b5ec25Sjsg  * SUCH DAMAGE.
25*a1b5ec25Sjsg  */
26*a1b5ec25Sjsg 
27*a1b5ec25Sjsg #include <libelf.h>
28*a1b5ec25Sjsg #include <stdio.h>
29*a1b5ec25Sjsg #include <string.h>
30*a1b5ec25Sjsg 
31*a1b5ec25Sjsg #include "_libelf.h"
32*a1b5ec25Sjsg 
33*a1b5ec25Sjsg ELFTC_VCSID("$Id: elf_errmsg.c,v 1.1 2019/02/01 05:27:37 jsg Exp $");
34*a1b5ec25Sjsg 
35*a1b5ec25Sjsg /*
36*a1b5ec25Sjsg  * Retrieve a human readable translation for an error message.
37*a1b5ec25Sjsg  */
38*a1b5ec25Sjsg 
39*a1b5ec25Sjsg static const char *_libelf_errors[] = {
40*a1b5ec25Sjsg #define	DEFINE_ERROR(N,S)	[ELF_E_##N] = S
41*a1b5ec25Sjsg 	DEFINE_ERROR(NONE,	"No Error"),
42*a1b5ec25Sjsg 	DEFINE_ERROR(ARCHIVE,	"Malformed ar(1) archive"),
43*a1b5ec25Sjsg 	DEFINE_ERROR(ARGUMENT,	"Invalid argument"),
44*a1b5ec25Sjsg 	DEFINE_ERROR(CLASS,	"ELF class mismatch"),
45*a1b5ec25Sjsg 	DEFINE_ERROR(DATA,	"Invalid data buffer descriptor"),
46*a1b5ec25Sjsg 	DEFINE_ERROR(HEADER,	"Missing or malformed ELF header"),
47*a1b5ec25Sjsg 	DEFINE_ERROR(IO,	"I/O error"),
48*a1b5ec25Sjsg 	DEFINE_ERROR(LAYOUT,	"Layout constraint violation"),
49*a1b5ec25Sjsg 	DEFINE_ERROR(MODE,	"Incorrect ELF descriptor mode"),
50*a1b5ec25Sjsg 	DEFINE_ERROR(RANGE,	"Value out of range of target"),
51*a1b5ec25Sjsg 	DEFINE_ERROR(RESOURCE,	"Resource exhaustion"),
52*a1b5ec25Sjsg 	DEFINE_ERROR(SECTION,	"Invalid section descriptor"),
53*a1b5ec25Sjsg 	DEFINE_ERROR(SEQUENCE,	"API calls out of sequence"),
54*a1b5ec25Sjsg 	DEFINE_ERROR(UNIMPL,	"Unimplemented feature"),
55*a1b5ec25Sjsg 	DEFINE_ERROR(VERSION,	"Unknown ELF API version"),
56*a1b5ec25Sjsg 	DEFINE_ERROR(NUM,	"Unknown error")
57*a1b5ec25Sjsg #undef	DEFINE_ERROR
58*a1b5ec25Sjsg };
59*a1b5ec25Sjsg 
60*a1b5ec25Sjsg const char *
elf_errmsg(int error)61*a1b5ec25Sjsg elf_errmsg(int error)
62*a1b5ec25Sjsg {
63*a1b5ec25Sjsg 	int oserr;
64*a1b5ec25Sjsg 
65*a1b5ec25Sjsg 	if (error == ELF_E_NONE &&
66*a1b5ec25Sjsg 	    (error = LIBELF_PRIVATE(error)) == 0)
67*a1b5ec25Sjsg 	    return NULL;
68*a1b5ec25Sjsg 	else if (error == -1)
69*a1b5ec25Sjsg 	    error = LIBELF_PRIVATE(error);
70*a1b5ec25Sjsg 
71*a1b5ec25Sjsg 	oserr = error >> LIBELF_OS_ERROR_SHIFT;
72*a1b5ec25Sjsg 	error &= LIBELF_ELF_ERROR_MASK;
73*a1b5ec25Sjsg 
74*a1b5ec25Sjsg 	if (error < ELF_E_NONE || error >= ELF_E_NUM)
75*a1b5ec25Sjsg 		return _libelf_errors[ELF_E_NUM];
76*a1b5ec25Sjsg 	if (oserr) {
77*a1b5ec25Sjsg 		(void) snprintf((char *) LIBELF_PRIVATE(msg),
78*a1b5ec25Sjsg 		    sizeof(LIBELF_PRIVATE(msg)), "%s: %s",
79*a1b5ec25Sjsg 		    _libelf_errors[error], strerror(oserr));
80*a1b5ec25Sjsg 		return (const char *)&LIBELF_PRIVATE(msg);
81*a1b5ec25Sjsg 	}
82*a1b5ec25Sjsg 	return _libelf_errors[error];
83*a1b5ec25Sjsg }
84