elf_errmsg.c revision 165032
1139823Simp/*- 2133920Sandre * Copyright (c) 2006 Joseph Koshy 3133920Sandre * All rights reserved. 4133920Sandre * 5133920Sandre * Redistribution and use in source and binary forms, with or without 6133920Sandre * modification, are permitted provided that the following conditions 7133920Sandre * are met: 8133920Sandre * 1. Redistributions of source code must retain the above copyright 9133920Sandre * notice, this list of conditions and the following disclaimer. 10133920Sandre * 2. Redistributions in binary form must reproduce the above copyright 11133920Sandre * notice, this list of conditions and the following disclaimer in the 12133920Sandre * documentation and/or other materials provided with the distribution. 13133920Sandre * 14133920Sandre * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15133920Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16133920Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17133920Sandre * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18133920Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19133920Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20133920Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21133920Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22133920Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23133920Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24133920Sandre * SUCH DAMAGE. 25133920Sandre */ 26133920Sandre 27172467Ssilby#include <sys/cdefs.h> 28172467Ssilby__FBSDID("$FreeBSD: head/lib/libelf/elf_errmsg.c 165032 2006-12-09 16:27:15Z jkoshy $"); 29172467Ssilby 30134346Sru#include <libelf.h> 31133920Sandre#include <string.h> 32133920Sandre 33133920Sandre#include "_libelf.h" 34133920Sandre 35133920Sandre/* 36133920Sandre * Retrieve a human readable translation for an error message. 37134383Sandre */ 38152928Sume 39133920Sandreconst char *_libelf_errors[] = { 40133920Sandre#define DEFINE_ERROR(N,S) [ELF_E_##N] = S 41133920Sandre DEFINE_ERROR(NONE, "No Error"), 42133920Sandre DEFINE_ERROR(ARCHIVE, "Malformed ar(1) archive"), 43133920Sandre DEFINE_ERROR(ARGUMENT, "Invalid argument"), 44133920Sandre DEFINE_ERROR(CLASS, "ELF class mismatch"), 45133920Sandre DEFINE_ERROR(DATA, "Invalid data buffer descriptor"), 46185895Szec DEFINE_ERROR(HEADER, "Missing or malformed ELF header"), 47185895Szec DEFINE_ERROR(IO, "I/O error"), 48133920Sandre DEFINE_ERROR(LAYOUT, "Layout constraint violation"), 49133920Sandre DEFINE_ERROR(MODE, "Incorrect ELF descriptor mode"), 50133920Sandre DEFINE_ERROR(RANGE, "Value out of range of target"), 51133920Sandre DEFINE_ERROR(RESOURCE, "Resource exhaustion"), 52181803Sbz DEFINE_ERROR(SECTION, "Invalid section descriptor"), 53133920Sandre DEFINE_ERROR(SEQUENCE, "API calls out of sequence"), 54133920Sandre DEFINE_ERROR(UNIMPL, "Unimplemented feature"), 55133920Sandre DEFINE_ERROR(VERSION, "Unknown ELF API version"), 56133920Sandre DEFINE_ERROR(NUM, "Unknown error") 57133920Sandre#undef DEFINE_ERROR 58133920Sandre}; 59133920Sandre 60133920Sandreconst char * 61133920Sandreelf_errmsg(int error) 62133920Sandre{ 63133920Sandre int oserr; 64133920Sandre 65133920Sandre if (error == 0 && (error = LIBELF_PRIVATE(error)) == 0) 66133920Sandre return NULL; 67141351Sglebius else if (error == -1) 68141351Sglebius error = LIBELF_PRIVATE(error); 69133920Sandre 70133920Sandre oserr = error >> LIBELF_OS_ERROR_SHIFT; 71158470Smlaier error &= LIBELF_ELF_ERROR_MASK; 72158470Smlaier 73158470Smlaier if (error < 0 || error >= ELF_E_NUM) 74158470Smlaier return _libelf_errors[ELF_E_NUM]; 75133920Sandre if (oserr) { 76158470Smlaier strlcpy(LIBELF_PRIVATE(msg), _libelf_errors[error], 77158470Smlaier sizeof(LIBELF_PRIVATE(msg))); 78133920Sandre strlcat(LIBELF_PRIVATE(msg), ": ", sizeof(LIBELF_PRIVATE(msg))); 79133920Sandre strlcat(LIBELF_PRIVATE(msg), strerror(oserr), 80133920Sandre sizeof(LIBELF_PRIVATE(msg))); 81136714Sandre return (const char *)&LIBELF_PRIVATE(msg); 82136714Sandre } 83136714Sandre return _libelf_errors[error]; 84141351Sglebius} 85141351Sglebius 86141351Sglebius#if defined(LIBELF_TEST_HOOKS) 87136714Sandre 88136714Sandreconst char * 89133920Sandre_libelf_get_unknown_error_message(void) 90133920Sandre{ 91133920Sandre return _libelf_errors[ELF_E_NUM]; 92133920Sandre} 93135920Smlaier 94135920Smlaierconst char * 95133920Sandre_libelf_get_no_error_message(void) 96133920Sandre{ 97141351Sglebius return _libelf_errors[0]; 98133920Sandre} 99133920Sandre 100133920Sandre#endif /* LIBELF_TEST_HOOKS */ 101140224Sglebius