elf_errmsg.c revision 164190
1164426Ssam/*- 2164426Ssam * Copyright (c) 2006 Joseph Koshy 3164426Ssam * All rights reserved. 4164426Ssam * 5164426Ssam * Redistribution and use in source and binary forms, with or without 6164426Ssam * modification, are permitted provided that the following conditions 7164426Ssam * are met: 8164426Ssam * 1. Redistributions of source code must retain the above copyright 9164426Ssam * notice, this list of conditions and the following disclaimer. 10164426Ssam * 2. Redistributions in binary form must reproduce the above copyright 11164426Ssam * notice, this list of conditions and the following disclaimer in the 12164426Ssam * documentation and/or other materials provided with the distribution. 13164426Ssam * 14164426Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15164426Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16164426Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17164426Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18164426Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19164426Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20164426Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21164426Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22164426Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23164426Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24164426Ssam * SUCH DAMAGE. 25164426Ssam */ 26164426Ssam 27164426Ssam#include <sys/cdefs.h> 28164426Ssam__FBSDID("$FreeBSD: head/lib/libelf/elf_errmsg.c 164190 2006-11-11 17:16:35Z jkoshy $"); 29164426Ssam 30164426Ssam#include <libelf.h> 31164426Ssam#include <string.h> 32164426Ssam 33164426Ssam#include "_libelf.h" 34164426Ssam 35164426Ssam/* 36164426Ssam * Retrieve a human readable translation for an error message. 37164426Ssam */ 38164426Ssam 39164426Ssamconst char *_libelf_errors[] = { 40164426Ssam#define DEFINE_ERROR(N,S) [ELF_E_##N] = S 41164426Ssam DEFINE_ERROR(NONE, "No Error"), 42164426Ssam DEFINE_ERROR(ARCHIVE, "Malformed ar(1) archive"), 43164426Ssam DEFINE_ERROR(ARGUMENT, "Invalid argument"), 44164426Ssam DEFINE_ERROR(CLASS, "ELF class mismatch"), 45164426Ssam DEFINE_ERROR(DATA, "Invalid data buffer descriptor"), 46164426Ssam DEFINE_ERROR(HEADER, "Missing or malformed ELF header"), 47164426Ssam DEFINE_ERROR(IO, "I/O error"), 48164426Ssam DEFINE_ERROR(LAYOUT, "Layout constraint violation"), 49164426Ssam DEFINE_ERROR(MODE, "Incorrect ELF descriptor mode"), 50164426Ssam DEFINE_ERROR(RANGE, "Value out of range of target"), 51164426Ssam DEFINE_ERROR(RESOURCE, "Resource exhaustion"), 52164426Ssam DEFINE_ERROR(SECTION, "Invalid section descriptor"), 53164426Ssam DEFINE_ERROR(SEQUENCE, "API calls out of sequence"), 54164426Ssam DEFINE_ERROR(UNIMPL, "Unimplemented feature"), 55164426Ssam DEFINE_ERROR(VERSION, "Unknown ELF API version"), 56164426Ssam DEFINE_ERROR(NUM, "Unknown error") 57164426Ssam#undef DEFINE_ERROR 58164426Ssam}; 59164426Ssam 60164426Ssamconst char * 61164426Ssamelf_errmsg(int error) 62164426Ssam{ 63164426Ssam int oserr; 64164426Ssam 65164426Ssam if (error == 0 && (error = LIBELF_PRIVATE(error)) == 0) 66164426Ssam return NULL; 67164426Ssam else if (error == -1) 68164426Ssam error = LIBELF_PRIVATE(error); 69164426Ssam 70164426Ssam oserr = error >> LIBELF_OS_ERROR_SHIFT; 71164426Ssam error &= LIBELF_ELF_ERROR_MASK; 72164426Ssam 73164426Ssam if (error < 0 || error >= ELF_E_NUM) 74164426Ssam return _libelf_errors[ELF_E_NUM]; 75164426Ssam if (oserr) { 76164426Ssam strlcpy(LIBELF_PRIVATE(msg), _libelf_errors[error], 77164426Ssam sizeof(LIBELF_PRIVATE(msg))); 78164426Ssam strlcat(LIBELF_PRIVATE(msg), ":", sizeof(LIBELF_PRIVATE(msg))); 79164426Ssam strlcat(LIBELF_PRIVATE(msg), strerror(oserr), 80164426Ssam sizeof(LIBELF_PRIVATE(msg))); 81164426Ssam return (const char *)&LIBELF_PRIVATE(msg); 82164426Ssam } 83164426Ssam return _libelf_errors[error]; 84164426Ssam} 85164426Ssam 86164426Ssam#if defined(LIBELF_TEST_HOOKS) 87164426Ssam 88164426Ssamconst char * 89164426Ssam_libelf_get_unknown_error_message(void) 90164426Ssam{ 91166064Scognet return _libelf_errors[ELF_E_NUM]; 92166064Scognet} 93166064Scognet 94166064Scognetconst char * 95166064Scognet_libelf_get_no_error_message(void) 96166064Scognet{ 97164426Ssam return _libelf_errors[0]; 98164426Ssam} 99164426Ssam 100164426Ssam#endif /* LIBELF_TEST_HOOKS */ 101164426Ssam