1 /*-
2  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "_libdwarf.h"
28 
29 ELFTC_VCSID("$Id: dwarf_errmsg.c 2975 2014-01-21 20:08:04Z kaiwang27 $");
30 
31 static const char *_libdwarf_errors[] = {
32 #define	DEFINE_ERROR(N,S)		[DW_DLE_##N] = S
33 	DEFINE_ERROR(NONE, "No Error"),
34 	DEFINE_ERROR(ERROR, "An error"),
35 	DEFINE_ERROR(NO_ENTRY, "No entry found"),
36 	DEFINE_ERROR(ARGUMENT, "Invalid argument"),
37 	DEFINE_ERROR(DEBUG_INFO_NULL, "Debug info NULL"),
38 	DEFINE_ERROR(MEMORY, "Insufficient memory"),
39 	DEFINE_ERROR(ELF, "ELF error"),
40 	DEFINE_ERROR(CU_LENGTH_ERROR, "Invalid compilation unit data"),
41 	DEFINE_ERROR(VERSION_STAMP_ERROR, "Unsupported version"),
42 	DEFINE_ERROR(DEBUG_ABBREV_NULL, "Abbrev not found"),
43 	DEFINE_ERROR(DIE_NO_CU_CONTEXT,	"No current compilation unit"),
44 	DEFINE_ERROR(LOC_EXPR_BAD, "Invalid location expression"),
45 	DEFINE_ERROR(EXPR_LENGTH_BAD, "Invalid DWARF expression length"),
46 	DEFINE_ERROR(DEBUG_LOC_SECTION_SHORT, "Loclist section too short"),
47 	DEFINE_ERROR(ATTR_FORM_BAD, "Invalid attribute form"),
48 	DEFINE_ERROR(DEBUG_LINE_LENGTH_BAD, "Line info section too short"),
49 	DEFINE_ERROR(LINE_FILE_NUM_BAD, "Invalid file number."),
50 	DEFINE_ERROR(DIR_INDEX_BAD, "Invalid dir index."),
51 	DEFINE_ERROR(DEBUG_FRAME_LENGTH_BAD, "Frame section too short"),
52 	DEFINE_ERROR(NO_CIE_FOR_FDE, "FDE without corresponding CIE"),
53 	DEFINE_ERROR(FRAME_AUGMENTATION_UNKNOWN, "Unknown CIE augmentation"),
54 	DEFINE_ERROR(FRAME_INSTR_EXEC_ERROR, "Frame instruction exec error"),
55 	DEFINE_ERROR(FRAME_VERSION_BAD, "Unsupported frame section version"),
56 	DEFINE_ERROR(FRAME_TABLE_COL_BAD, "Invalid table column value"),
57 	DEFINE_ERROR(DF_REG_NUM_TOO_HIGH, "Register number too large"),
58 	DEFINE_ERROR(PC_NOT_IN_FDE_RANGE, "PC requested not in the FDE range"),
59 	DEFINE_ERROR(ARANGE_OFFSET_BAD, "Invalid address range offset"),
60 	DEFINE_ERROR(DEBUG_MACRO_INCONSISTENT, "Invalid macinfo data"),
61 	DEFINE_ERROR(ELF_SECT_ERR, "Application callback failed"),
62 	DEFINE_ERROR(NUM, "Unknown DWARF error")
63 #undef	DEFINE_ERROR
64 };
65 
66 const char *
dwarf_errmsg_(Dwarf_Error * error)67 dwarf_errmsg_(Dwarf_Error *error)
68 {
69 	const char *p;
70 
71 	if (error == NULL)
72 		return NULL;
73 
74 	if (error->err_error < 0 || error->err_error >= DW_DLE_NUM)
75 		return _libdwarf_errors[DW_DLE_NUM];
76 	else if (error->err_error == DW_DLE_NONE)
77 		return _libdwarf_errors[DW_DLE_NONE];
78 	else
79 		p = _libdwarf_errors[error->err_error];
80 
81 	if (error->err_error == DW_DLE_ELF)
82 		snprintf(error->err_msg, sizeof(error->err_msg),
83 		    "ELF error : %s [%s(%d)]", elf_errmsg(error->err_elferror),
84 		    error->err_func, error->err_line);
85 	else
86 		snprintf(error->err_msg, sizeof(error->err_msg),
87 		    "%s [%s(%d)]", p, error->err_func, error->err_line);
88 
89 	return (const char *) error->err_msg;
90 }
91