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