xref: /titanic_50/usr/src/lib/libdwarf/common/dwarf_error.c (revision f3e7f55e73a39377d55a030f124cc86b3b66a9cc)
1 /*
2 
3   Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4   Portions Copyright (C) 2008-2010 David Anderson.  All Rights Reserved.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of version 2.1 of the GNU Lesser General Public License
8   as published by the Free Software Foundation.
9 
10   This program is distributed in the hope that it would be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 
14   Further, this software is distributed without any warranty that it is
15   free of the rightful claim of any third person regarding infringement
16   or the like.  Any license provided herein, whether implied or
17   otherwise, applies only to this software file.  Patent licenses, if
18   any, provided herein do not apply to combinations of this program with
19   other software, or any other product whatsoever.
20 
21   You should have received a copy of the GNU Lesser General Public
22   License along with this program; if not, write the Free Software
23   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
24   USA.
25 
26   Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
27   Mountain View, CA 94043, or:
28 
29   http://www.sgi.com
30 
31   For further information regarding this notice, see:
32 
33   http://oss.sgi.com/projects/GenInfo/NoticeExplan
34 
35 */
36 
37 
38 
39 #include "config.h"
40 #include "dwarf_incl.h"
41 #ifdef HAVE_ELF_H
42 #include <elf.h>
43 #endif
44 
45 #include <stdio.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <stdlib.h>
49 
50 /* Array to hold string representation of errors. Any time a
51    define is added to the list in libdwarf.h, a string should be
52    added to this Array
53 */
54 
55 const char *_dwarf_errmsgs[] = {
56 
57     "No error (0)\n",
58     "DW_DLE_VMM 1 dwarf format/library version mismatch",
59     "DW_DLE_MAP 2 memory map failure",
60     "DW_DLE_LEE 3 libelf error",
61     "DW_DLE_NDS 4 no debug section",
62     "DW_DLE_NLS    5  no line section ",
63     "DW_DLE_ID     6  invalid descriptor for query ",
64     "DW_DLE_IOF    7  I/O failure ",
65     "DW_DLE_MAF    8  memory allocation failure ",
66     "DW_DLE_IA     9  invalid argument ",
67     "DW_DLE_MDE    10  mangled debugging entry ",
68     "DW_DLE_MLE    11  mangled line number entry ",
69     "DW_DLE_FNO    12  file not open ",
70     "DW_DLE_FNR    13  file not a regular file ",
71     "DW_DLE_FWA    14  file open with wrong access ",
72     "DW_DLE_NOB    15  not an object file ",
73     "DW_DLE_MOF    16  mangled object file header ",
74     "DW_DLE_EOLL   17  end of location list entries ",
75     "DW_DLE_NOLL   18  no location list section ",
76     "DW_DLE_BADOFF 19  Invalid offset ",
77     "DW_DLE_EOS    20  end of section  ",
78     "DW_DLE_ATRUNC 21  abbreviations section appears truncated",
79     "DW_DLE_BADBITC  22  Address size passed to dwarf bad",
80 
81     "DW_DLE_DBG_ALLOC 23 Unable to malloc a Dwarf_Debug structure",
82     "DW_DLE_FSTAT_ERROR 24 The file fd passed to dwarf_init "
83         "cannot be fstat()ed",
84     "DW_DLE_FSTAT_MODE_ERROR 25 The file mode bits do not "
85         "indicate that the file being opened via "
86         "dwarf_init() is a normal file",
87     "DW_DLE_INIT_ACCESS_WRONG 26 A call to dwarf_init had an "
88         "access of other than DW_DLC_READ",
89     "DW_DLE_ELF_BEGIN_ERROR 27 a call to "
90         "elf_begin(... ELF_C_READ_MMAP... ) failed",
91     "DW_DLE_ELF_GETEHDR_ERROR 28 a call to "
92         "elf32_getehdr() or elf64_getehdr() failed",
93     "DW_DLE_ELF_GETSHDR_ERROR 29 a call to "
94         "elf32_getshdr() or elf64_getshdr() failed",
95     "DW_DLE_ELF_STRPTR_ERROR 30 a call to "
96         "elf_strptr() failed trying to get a section name",
97     "DW_DLE_DEBUG_INFO_DUPLICATE 31  Only one .debug_info  "
98         "section is allowed",
99     "DW_DLE_DEBUG_INFO_NULL 32 .debug_info section present but "
100         "elf_getdata() failed or section is zero-length",
101     "DW_DLE_DEBUG_ABBREV_DUPLICATE 33 Only one .debug_abbrev  "
102         "section is allowed",
103     "DW_DLE_DEBUG_ABBREV_NULL 34 .debug_abbrev section present but "
104         "elf_getdata() failed or section is zero-length",
105     "DW_DLE_DEBUG_ARANGES_DUPLICATE 35 Only one .debug_aranges  "
106         "section is allowed",
107     "DW_DLE_DEBUG_ARANGES_NULL 36 .debug_aranges section present but "
108         "elf_getdata() failed or section is zero-length",
109     "DW_DLE_DEBUG_LINE_DUPLICATE 37 Only one .debug_line  "
110         "section is allowed",
111     "DW_DLE_DEBUG_LINE_NULL (38) .debug_line section present but "
112         "elf_getdata() failed or section is zero-length",
113     "DW_DLE_DEBUG_LOC_DUPLICATE (39) Only one .debug_loc  "
114         "section is allowed",
115     "DW_DLE_DEBUG_LOC_NULL (40) .debug_loc section present but "
116         "elf_getdata() failed or section is zero-length",
117     "DW_DLE_DEBUG_MACINFO_DUPLICATE (41) Only one .debug_macinfo  "
118         "section is allowed",
119     "DW_DLE_DEBUG_MACINFO_NULL (42) .debug_macinfo section present but "
120         "elf_getdata() failed or section is zero-length",
121     "DW_DLE_DEBUG_PUBNAMES_DUPLICATE (43) Only one .debug_pubnames  "
122         "section is allowed",
123     "DW_DLE_DEBUG_PUBNAMES_NULL (44) .debug_pubnames section present but "
124         "elf_getdata() failed or section is zero-length",
125     "DW_DLE_DEBUG_STR_DUPLICATE (45)  Only one .debug_str  "
126         "section is allowed",
127     "DW_DLE_DEBUG_STR_NULL (46) .debug_str section present but "
128         "elf_getdata() failed or section is zero-length",
129     "DW_DLE_CU_LENGTH_ERROR (47)",
130     "DW_DLE_VERSION_STAMP_ERROR (48)",
131     "DW_DLE_ABBREV_OFFSET_ERROR (49)",
132     "DW_DLE_ADDRESS_SIZE_ERROR (50)",
133     "DW_DLE_DEBUG_INFO_PTR_NULL (51)",
134     "DW_DLE_DIE_NULL (52)",
135     "DW_DLE_STRING_OFFSET_BAD (53)",
136     "DW_DLE_DEBUG_LINE_LENGTH_BAD (54)",
137     "DW_DLE_LINE_PROLOG_LENGTH_BAD (55)",
138     "DW_DLE_LINE_NUM_OPERANDS_BAD",
139     "DW_DLE_LINE_SET_ADDR_ERROR",
140     "DW_DLE_LINE_EXT_OPCODE_BAD",
141     "DW_DLE_DWARF_LINE_NULL",
142     "DW_DLE_INCL_DIR_NUM_BAD",
143     "DW_DLE_LINE_FILE_NUM_BAD",
144     "DW_DLE_ALLOC_FAIL",
145     "DW_DLE_NO_CALLBACK_FUNC",
146     "DW_DLE_SECT_ALLOC",
147     "DW_DLE_FILE_ENTRY_ALLOC",
148     "DW_DLE_LINE_ALLOC",
149     "DW_DLE_FPGM_ALLOC",
150     "DW_DLE_INCDIR_ALLOC",
151     "DW_DLE_STRING_ALLOC",
152     "DW_DLE_CHUNK_ALLOC",
153     "DW_DLE_BYTEOFF_ERR",
154     "DW_DLE_CIE_ALLOC",
155     "DW_DLE_FDE_ALLOC",
156     "DW_DLE_REGNO_OVFL",
157     "DW_DLE_CIE_OFFS_ALLOC",
158     "DW_DLE_WRONG_ADDRESS",
159     "DW_DLE_EXTRA_NEIGHBORS",
160     "DW_DLE_WRONG_TAG",
161     "DW_DLE_DIE_ALLOC",
162     "DW_DLE_PARENT_EXISTS",
163     "DW_DLE_DBG_NULL",
164     "DW_DLE_DEBUGLINE_ERROR",
165     "DW_DLE_DEBUGFRAME_ERROR",
166     "DW_DLE_DEBUGINFO_ERROR",
167     "DW_DLE_ATTR_ALLOC",
168     "DW_DLE_ABBREV_ALLOC",
169     "DW_DLE_OFFSET_UFLW",
170     "DW_DLE_ELF_SECT_ERR",
171     "DW_DLE_DEBUG_FRAME_LENGTH_BAD",
172     "DW_DLE_FRAME_VERSION_BAD",
173     "DW_DLE_CIE_RET_ADDR_REG_ERROR",
174     "DW_DLE_FDE_NULL",
175     "DW_DLE_FDE_DBG_NULL",
176     "DW_DLE_CIE_NULL",
177     "DW_DLE_CIE_DBG_NULL",
178     "DW_DLE_FRAME_TABLE_COL_BAD",
179     "DW_DLE_PC_NOT_IN_FDE_RANGE",
180     "DW_DLE_CIE_INSTR_EXEC_ERROR",
181     "DW_DLE_FRAME_INSTR_EXEC_ERROR",
182     "DW_DLE_FDE_PTR_NULL",
183     "DW_DLE_RET_OP_LIST_NULL",
184     "DW_DLE_LINE_CONTEXT_NULL",
185     "DW_DLE_DBG_NO_CU_CONTEXT",
186     "DW_DLE_DIE_NO_CU_CONTEXT",
187     "DW_DLE_FIRST_DIE_NOT_CU",
188     "DW_DLE_NEXT_DIE_PTR_NULL",
189     "DW_DLE_DEBUG_FRAME_DUPLICATE  Only one .debug_frame  "
190         "section is allowed",
191     "DW_DLE_DEBUG_FRAME_NULL  .debug_frame section present but "
192         "elf_getdata() failed or section is zero-length",
193     "DW_DLE_ABBREV_DECODE_ERROR",
194     "DW_DLE_DWARF_ABBREV_NULL",
195     "DW_DLE_ATTR_NULL",
196     "DW_DLE_DIE_BAD",
197     "DW_DLE_DIE_ABBREV_BAD",
198     "DW_DLE_ATTR_FORM_BAD",
199     "DW_DLE_ATTR_NO_CU_CONTEXT",
200     "DW_DLE_ATTR_FORM_SIZE_BAD",
201     "DW_DLE_ATTR_DBG_NULL",
202     "DW_DLE_BAD_REF_FORM",
203     "DW_DLE_ATTR_FORM_OFFSET_BAD",
204     "DW_DLE_LINE_OFFSET_BAD",
205     "DW_DLE_DEBUG_STR_OFFSET_BAD",
206     "DW_DLE_STRING_PTR_NULL",
207     "DW_DLE_PUBNAMES_VERSION_ERROR",
208     "DW_DLE_PUBNAMES_LENGTH_BAD",
209     "DW_DLE_GLOBAL_NULL",
210     "DW_DLE_GLOBAL_CONTEXT_NULL",
211     "DW_DLE_DIR_INDEX_BAD",
212     "DW_DLE_LOC_EXPR_BAD",
213     "DW_DLE_DIE_LOC_EXPR_BAD",
214     "DW_DLE_ADDR_ALLOC",
215     "DW_DLE_OFFSET_BAD",
216     "DW_DLE_MAKE_CU_CONTEXT_FAIL",
217     "DW_DLE_REL_ALLOC",
218     "DW_DLE_ARANGE_OFFSET_BAD",
219     "DW_DLE_SEGMENT_SIZE_BAD",
220     "DW_DLE_ARANGE_LENGTH_BAD",
221     "DW_DLE_ARANGE_DECODE_ERROR",
222     "DW_DLE_ARANGES_NULL",
223     "DW_DLE_ARANGE_NULL",
224     "DW_DLE_NO_FILE_NAME",
225     "DW_DLE_NO_COMP_DIR",
226     "DW_DLE_CU_ADDRESS_SIZE_BAD",
227     "DW_DLE_INPUT_ATTR_BAD",
228     "DW_DLE_EXPR_NULL",
229     "DW_DLE_BAD_EXPR_OPCODE",
230     "DW_DLE_EXPR_LENGTH_BAD",
231     "DW_DLE_MULTIPLE_RELOC_IN_EXPR",
232     "DW_DLE_ELF_GETIDENT_ERROR",
233     "DW_DLE_NO_AT_MIPS_FDE",
234     "DW_DLE_NO_CIE_FOR_FDE",
235     "DW_DLE_DIE_ABBREV_LIST_NULL",
236     "DW_DLE_DEBUG_FUNCNAMES_DUPLICATE",
237     "DW_DLE_DEBUG_FUNCNAMES_NULL .debug_funcnames section present but "
238         "elf_getdata() failed or section is zero-length",
239     "DW_DLE_DEBUG_FUNCNAMES_VERSION_ERROR",
240     "DW_DLE_DEBUG_FUNCNAMES_LENGTH_BAD",
241     "DW_DLE_FUNC_NULL",
242     "DW_DLE_FUNC_CONTEXT_NULL",
243     "DW_DLE_DEBUG_TYPENAMES_DUPLICATE",
244     "DW_DLE_DEBUG_TYPENAMES_NULL .debug_typenames section present but "
245         "elf_getdata() failed or section is zero-length",
246     "DW_DLE_DEBUG_TYPENAMES_VERSION_ERROR",
247     "DW_DLE_DEBUG_TYPENAMES_LENGTH_BAD",
248     "DW_DLE_TYPE_NULL",
249     "DW_DLE_TYPE_CONTEXT_NULL",
250     "DW_DLE_DEBUG_VARNAMES_DUPLICATE",
251     "DW_DLE_DEBUG_VARNAMES_NULL .debug_varnames section present but "
252         "elf_getdata() failed or section is zero-length",
253     "DW_DLE_DEBUG_VARNAMES_VERSION_ERROR",
254     "DW_DLE_DEBUG_VARNAMES_LENGTH_BAD",
255     "DW_DLE_VAR_NULL",
256     "DW_DLE_VAR_CONTEXT_NULL",
257     "DW_DLE_DEBUG_WEAKNAMES_DUPLICATE",
258     "DW_DLE_DEBUG_WEAKNAMES_NULL .debug_weaknames section present but "
259         "elf_getdata() failed or section is zero-length",
260 
261     "DW_DLE_DEBUG_WEAKNAMES_VERSION_ERROR",
262     "DW_DLE_DEBUG_WEAKNAMES_LENGTH_BAD",
263     "DW_DLE_WEAK_NULL",
264     "DW_DLE_WEAK_CONTEXT_NULL (175)",
265     "DW_DLE_LOCDESC_COUNT_WRONG (176)",
266     "DW_DLE_MACINFO_STRING_NULL (177)",
267     "DW_DLE_MACINFO_STRING_EMPTY (178)",
268     "DW_DLE_MACINFO_INTERNAL_ERROR_SPACE (179)",
269     "DW_DLE_MACINFO_MALLOC_FAIL (180)",
270     "DW_DLE_DEBUGMACINFO_ERROR (181)",
271     "DW_DLE_DEBUG_MACRO_LENGTH_BAD (182)",
272     "DW_DLE_DEBUG_MACRO_MAX_BAD (183)",
273     "DW_DLE_DEBUG_MACRO_INTERNAL_ERR (184)",
274     "DW_DLE_DEBUG_MACRO_MALLOC_SPACE (185)",
275     "DW_DLE_DEBUG_MACRO_INCONSISTENT (186)",
276     "DW_DLE_DF_NO_CIE_AUGMENTATION(187)",
277     "DW_DLE_DF_REG_NUM_TOO_HIGH(188)",
278     "DW_DLE_DF_MAKE_INSTR_NO_INIT(189)",
279     "DW_DLE_DF_NEW_LOC_LESS_OLD_LOC(190)",
280     "DW_DLE_DF_POP_EMPTY_STACK(191)",
281     "DW_DLE_DF_ALLOC_FAIL(192)",
282     "DW_DLE_DF_FRAME_DECODING_ERROR(193)",
283     "DW_DLE_DEBUG_LOC_SECTION_SHORT(194)",
284     "DW_DLE_FRAME_AUGMENTATION_UNKNOWN(195)",
285     "DW_DLE_PUBTYPE_CONTEXT(196)",
286     "DW_DLE_DEBUG_PUBTYPES_LENGTH_BAD(197)",
287     "DW_DLE_DEBUG_PUBTYPES_VERSION_ERROR(198)",
288     "DW_DLE_DEBUG_PUBTYPES_DUPLICATE(199)",
289     "DW_DLE_FRAME_CIE_DECODE_ERROR(200)",
290     "DW_DLE_FRAME_REGISTER_UNREPRESENTABLE(201)",
291     "DW_DLE_FRAME_REGISTER_COUNT_MISMATCH(202)",
292     "DW_DLE_LINK_LOOP(203)",
293     "DW_DLE_STRP_OFFSET_BAD(204)",
294     "DW_DLE_DEBUG_RANGES_DUPLICATE(205)",
295     "DW_DLE_DEBUG_RANGES_OFFSET_BAD(206)",
296     "DW_DLE_DEBUG_RANGES_MISSING_END(207)",
297     "DW_DLE_DEBUG_RANGES_OUT_OF_MEM(208)",
298     "DW_DLE_DEBUG_SYMTAB_ERR(209)",
299     "DW_DLE_DEBUG_STRTAB_ERR(210)",
300     "DW_DLE_RELOC_MISMATCH_INDEX(211)",
301     "DW_DLE_RELOC_MISMATCH_RELOC_INDEX(212)",
302     "DW_DLE_RELOC_MISMATCH_STRTAB_INDEX(213)",
303     "DW_DLE_RELOC_SECTION_MISMATCH(214)",
304     "DW_DLE_RELOC_SECTION_MISSING_INDEX(215)",
305     "DW_DLE_RELOC_SECTION_LENGTH_ODD(216)",
306     "DW_DLE_RELOC_SECTION_PTR_NULL(217)",
307     "DW_DLE_RELOC_SECTION_MALLOC_FAIL(218)",
308     "DW_DLE_NO_ELF64_SUPPORT(219)",
309     "DW_DLE_MISSING_ELF64_SUPPORT(220)",
310     "DW_DLE_ORPHAN_FDE(221)",
311     "DW_DLE_DUPLICATE_INST_BLOCK(222)",
312     "DW_DLE_BAD_REF_SIG8_FORM(223)",
313     "DW_DLE_ATTR_EXPRLOC_FORM_BAD(224)",
314     "DW_DLE_FORM_SEC_OFFSET_LENGTH_BAD(225)",
315     "DW_DLE_NOT_REF_FORM(226)",
316     "DW_DLE_DEBUG_FRAME_LENGTH_NOT_MULTIPLE(227)"
317 };
318 
319 
320 
321 
322 /*
323     This function performs error handling as described in the
324     libdwarf consumer document section 3.  Dbg is the Dwarf_debug
325     structure being processed.  Error is a pointer to the pointer
326     to the error descriptor that will be returned.  Errval is an
327     error code listed in dwarf_error.h.
328 */
329 void
_dwarf_error(Dwarf_Debug dbg,Dwarf_Error * error,Dwarf_Sword errval)330 _dwarf_error(Dwarf_Debug dbg, Dwarf_Error * error, Dwarf_Sword errval)
331 {
332     Dwarf_Error errptr;
333 
334     /*
335        Allow NULL dbg on entry, since sometimes that can happen and we
336        want to report the upper-level error, not this one. */
337     if (error != NULL) {
338 
339         /*
340            If dbg is NULL, use the alternate error struct. However,
341            this will overwrite the earlier error. */
342         if (dbg != NULL) {
343             errptr =
344                 (Dwarf_Error) _dwarf_get_alloc(dbg, DW_DLA_ERROR, 1);
345             if (errptr == NULL) {
346                 fprintf(stderr,
347                         "Could not allocate Dwarf_Error structure, "
348                         "abort() in libdwarf.\n");
349                 abort();
350             }
351         } else {
352             /* We have no dbg to work with. dwarf_init failed. We hack
353                up a special area. */
354             errptr = _dwarf_special_no_dbg_error_malloc();
355             if (errptr == NULL) {
356                 fprintf(stderr,
357                         "Could not allocate Dwarf_Error structure, "
358                         "abort() in libdwarf..\n");
359                 abort();
360             }
361         }
362 
363         errptr->er_errval = errval;
364         *error = errptr;
365         return;
366     }
367 
368     if (dbg != NULL && dbg->de_errhand != NULL) {
369         errptr = (Dwarf_Error) _dwarf_get_alloc(dbg, DW_DLA_ERROR, 1);
370         if (errptr == NULL) {
371             fprintf(stderr, "Could not allocate Dwarf_Error structure,"
372                     " abort() in libdwarf.\n");
373             abort();
374         }
375         errptr->er_errval = errval;
376         dbg->de_errhand(errptr, dbg->de_errarg);
377         return;
378     }
379     fprintf(stderr,
380             "abort() in libdwarf. No error argument, no handler.\n");
381     abort();
382 }
383 
384 
385 Dwarf_Unsigned
dwarf_errno(Dwarf_Error error)386 dwarf_errno(Dwarf_Error error)
387 {
388     if (error == NULL) {
389         return (0);
390     }
391 
392     return (error->er_errval);
393 }
394 
395 
396 /*
397 */
398 char *
dwarf_errmsg(Dwarf_Error error)399 dwarf_errmsg(Dwarf_Error error)
400 {
401     if (error == NULL) {
402         return "Dwarf_Error is NULL";
403     }
404 
405     if (error->er_errval > (sizeof(_dwarf_errmsgs) / sizeof(char *))) {
406         return "Dwarf_Error value out of range";
407     }
408 
409     return ((char *) _dwarf_errmsgs[error->er_errval]);
410 }
411