summaryrefslogtreecommitdiff
path: root/libdwarf/dwarf_error.c
blob: 69031b7c698b25ce0821e1f57c97c53216a22495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*

  Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
  Portions Copyright (C) 2008-2011 David Anderson.  All Rights Reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2.1 of the GNU Lesser General Public License 
  as published by the Free Software Foundation.

  This program is distributed in the hope that it would be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

  Further, this software is distributed without any warranty that it is
  free of the rightful claim of any third person regarding infringement 
  or the like.  Any license provided herein, whether implied or 
  otherwise, applies only to this software file.  Patent licenses, if
  any, provided herein do not apply to combinations of this program with 
  other software, or any other product whatsoever.  

  You should have received a copy of the GNU Lesser General Public 
  License along with this program; if not, write the Free Software 
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
  USA.

  Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
  Mountain View, CA 94043, or:

  http://www.sgi.com

  For further information regarding this notice, see:

  http://oss.sgi.com/projects/GenInfo/NoticeExplan

*/



#include "config.h"
#include "dwarf_incl.h"
#ifdef HAVE_ELF_H
#include <elf.h>
#endif

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>

/* Array to hold string representation of errors. Any time a 
   define is added to the list in libdwarf.h, a string should be 
   added to this Array
*/

const char *_dwarf_errmsgs[] = {

    "No error (0)\n",
    "DW_DLE_VMM 1 dwarf format/library version mismatch",
    "DW_DLE_MAP 2 memory map failure",
    "DW_DLE_LEE 3 libelf error",
    "DW_DLE_NDS 4 no debug section",
    "DW_DLE_NLS    5  no line section ",
    "DW_DLE_ID     6  invalid descriptor for query ",
    "DW_DLE_IOF    7  I/O failure ",
    "DW_DLE_MAF    8  memory allocation failure ",
    "DW_DLE_IA     9  invalid argument ",
    "DW_DLE_MDE    10  mangled debugging entry ",
    "DW_DLE_MLE    11  mangled line number entry ",
    "DW_DLE_FNO    12  file not open ",
    "DW_DLE_FNR    13  file not a regular file ",
    "DW_DLE_FWA    14  file open with wrong access ",
    "DW_DLE_NOB    15  not an object file ",
    "DW_DLE_MOF    16  mangled object file header ",
    "DW_DLE_EOLL   17  end of location list entries ",
    "DW_DLE_NOLL   18  no location list section ",
    "DW_DLE_BADOFF 19  Invalid offset ",
    "DW_DLE_EOS    20  end of section  ",
    "DW_DLE_ATRUNC 21  abbreviations section appears truncated",
    "DW_DLE_BADBITC  22  Address size passed to dwarf bad",

    "DW_DLE_DBG_ALLOC 23 Unable to malloc a Dwarf_Debug structure",
    "DW_DLE_FSTAT_ERROR 24 The file fd passed to dwarf_init "
        "cannot be fstat()ed",
    "DW_DLE_FSTAT_MODE_ERROR 25 The file mode bits do not "
        "indicate that the file being opened via "
        "dwarf_init() is a normal file",
    "DW_DLE_INIT_ACCESS_WRONG 26 A call to dwarf_init had an "
        "access of other than DW_DLC_READ",
    "DW_DLE_ELF_BEGIN_ERROR 27 a call to "
        "elf_begin(... ELF_C_READ_MMAP... ) failed",
    "DW_DLE_ELF_GETEHDR_ERROR 28 a call to "
        "elf32_getehdr() or elf64_getehdr() failed",
    "DW_DLE_ELF_GETSHDR_ERROR 29 a call to "
        "elf32_getshdr() or elf64_getshdr() failed",
    "DW_DLE_ELF_STRPTR_ERROR 30 a call to "
        "elf_strptr() failed trying to get a section name",
    "DW_DLE_DEBUG_INFO_DUPLICATE 31  Only one .debug_info  "
        "section is allowed",
    "DW_DLE_DEBUG_INFO_NULL 32 .debug_info section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_ABBREV_DUPLICATE 33 Only one .debug_abbrev  "
        "section is allowed",
    "DW_DLE_DEBUG_ABBREV_NULL 34 .debug_abbrev section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_ARANGES_DUPLICATE 35 Only one .debug_aranges  "
        "section is allowed",
    "DW_DLE_DEBUG_ARANGES_NULL 36 .debug_aranges section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_LINE_DUPLICATE 37 Only one .debug_line  "
        "section is allowed",
    "DW_DLE_DEBUG_LINE_NULL (38) .debug_line section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_LOC_DUPLICATE (39) Only one .debug_loc  "
        "section is allowed",
    "DW_DLE_DEBUG_LOC_NULL (40) .debug_loc section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_MACINFO_DUPLICATE (41) Only one .debug_macinfo  "
        "section is allowed",
    "DW_DLE_DEBUG_MACINFO_NULL (42) .debug_macinfo section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_PUBNAMES_DUPLICATE (43) Only one .debug_pubnames  "
        "section is allowed",
    "DW_DLE_DEBUG_PUBNAMES_NULL (44) .debug_pubnames section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_STR_DUPLICATE (45)  Only one .debug_str  "
        "section is allowed",
    "DW_DLE_DEBUG_STR_NULL (46) .debug_str section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_CU_LENGTH_ERROR (47)",
    "DW_DLE_VERSION_STAMP_ERROR (48)",
    "DW_DLE_ABBREV_OFFSET_ERROR (49)",
    "DW_DLE_ADDRESS_SIZE_ERROR (50)",
    "DW_DLE_DEBUG_INFO_PTR_NULL (51)",
    "DW_DLE_DIE_NULL (52)",
    "DW_DLE_STRING_OFFSET_BAD (53)",
    "DW_DLE_DEBUG_LINE_LENGTH_BAD (54)",
    "DW_DLE_LINE_PROLOG_LENGTH_BAD (55)",
    "DW_DLE_LINE_NUM_OPERANDS_BAD",
    "DW_DLE_LINE_SET_ADDR_ERROR",
    "DW_DLE_LINE_EXT_OPCODE_BAD",
    "DW_DLE_DWARF_LINE_NULL",
    "DW_DLE_INCL_DIR_NUM_BAD",
    "DW_DLE_LINE_FILE_NUM_BAD",
    "DW_DLE_ALLOC_FAIL",
    "DW_DLE_NO_CALLBACK_FUNC",
    "DW_DLE_SECT_ALLOC",
    "DW_DLE_FILE_ENTRY_ALLOC",
    "DW_DLE_LINE_ALLOC",
    "DW_DLE_FPGM_ALLOC",
    "DW_DLE_INCDIR_ALLOC",
    "DW_DLE_STRING_ALLOC",
    "DW_DLE_CHUNK_ALLOC",
    "DW_DLE_BYTEOFF_ERR",
    "DW_DLE_CIE_ALLOC",
    "DW_DLE_FDE_ALLOC",
    "DW_DLE_REGNO_OVFL",
    "DW_DLE_CIE_OFFS_ALLOC",
    "DW_DLE_WRONG_ADDRESS",
    "DW_DLE_EXTRA_NEIGHBORS",
    "DW_DLE_WRONG_TAG",
    "DW_DLE_DIE_ALLOC",
    "DW_DLE_PARENT_EXISTS",
    "DW_DLE_DBG_NULL",
    "DW_DLE_DEBUGLINE_ERROR",
    "DW_DLE_DEBUGFRAME_ERROR",
    "DW_DLE_DEBUGINFO_ERROR",
    "DW_DLE_ATTR_ALLOC",
    "DW_DLE_ABBREV_ALLOC",
    "DW_DLE_OFFSET_UFLW",
    "DW_DLE_ELF_SECT_ERR",
    "DW_DLE_DEBUG_FRAME_LENGTH_BAD",
    "DW_DLE_FRAME_VERSION_BAD",
    "DW_DLE_CIE_RET_ADDR_REG_ERROR",
    "DW_DLE_FDE_NULL",
    "DW_DLE_FDE_DBG_NULL",
    "DW_DLE_CIE_NULL",
    "DW_DLE_CIE_DBG_NULL",
    "DW_DLE_FRAME_TABLE_COL_BAD",
    "DW_DLE_PC_NOT_IN_FDE_RANGE",
    "DW_DLE_CIE_INSTR_EXEC_ERROR",
    "DW_DLE_FRAME_INSTR_EXEC_ERROR",
    "DW_DLE_FDE_PTR_NULL",
    "DW_DLE_RET_OP_LIST_NULL",
    "DW_DLE_LINE_CONTEXT_NULL",
    "DW_DLE_DBG_NO_CU_CONTEXT",
    "DW_DLE_DIE_NO_CU_CONTEXT",
    "DW_DLE_FIRST_DIE_NOT_CU",
    "DW_DLE_NEXT_DIE_PTR_NULL",
    "DW_DLE_DEBUG_FRAME_DUPLICATE  Only one .debug_frame  "
        "section is allowed",
    "DW_DLE_DEBUG_FRAME_NULL  .debug_frame section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_ABBREV_DECODE_ERROR",
    "DW_DLE_DWARF_ABBREV_NULL",
    "DW_DLE_ATTR_NULL",
    "DW_DLE_DIE_BAD",
    "DW_DLE_DIE_ABBREV_BAD",
    "DW_DLE_ATTR_FORM_BAD",
    "DW_DLE_ATTR_NO_CU_CONTEXT",
    "DW_DLE_ATTR_FORM_SIZE_BAD",
    "DW_DLE_ATTR_DBG_NULL",
    "DW_DLE_BAD_REF_FORM",
    "DW_DLE_ATTR_FORM_OFFSET_BAD",
    "DW_DLE_LINE_OFFSET_BAD",
    "DW_DLE_DEBUG_STR_OFFSET_BAD",
    "DW_DLE_STRING_PTR_NULL",
    "DW_DLE_PUBNAMES_VERSION_ERROR",
    "DW_DLE_PUBNAMES_LENGTH_BAD",
    "DW_DLE_GLOBAL_NULL",
    "DW_DLE_GLOBAL_CONTEXT_NULL",
    "DW_DLE_DIR_INDEX_BAD",
    "DW_DLE_LOC_EXPR_BAD",
    "DW_DLE_DIE_LOC_EXPR_BAD",
    "DW_DLE_ADDR_ALLOC",
    "DW_DLE_OFFSET_BAD",
    "DW_DLE_MAKE_CU_CONTEXT_FAIL",
    "DW_DLE_REL_ALLOC",
    "DW_DLE_ARANGE_OFFSET_BAD",
    "DW_DLE_SEGMENT_SIZE_BAD (135) Size of a segment selector should usually be less than 8 (bytes).",
    "DW_DLE_ARANGE_LENGTH_BAD",
    "DW_DLE_ARANGE_DECODE_ERROR",
    "DW_DLE_ARANGES_NULL",
    "DW_DLE_ARANGE_NULL",
    "DW_DLE_NO_FILE_NAME",
    "DW_DLE_NO_COMP_DIR",
    "DW_DLE_CU_ADDRESS_SIZE_BAD",
    "DW_DLE_INPUT_ATTR_BAD",
    "DW_DLE_EXPR_NULL",
    "DW_DLE_BAD_EXPR_OPCODE",
    "DW_DLE_EXPR_LENGTH_BAD",
    "DW_DLE_MULTIPLE_RELOC_IN_EXPR",
    "DW_DLE_ELF_GETIDENT_ERROR",
    "DW_DLE_NO_AT_MIPS_FDE",
    "DW_DLE_NO_CIE_FOR_FDE",
    "DW_DLE_DIE_ABBREV_LIST_NULL",
    "DW_DLE_DEBUG_FUNCNAMES_DUPLICATE",
    "DW_DLE_DEBUG_FUNCNAMES_NULL .debug_funcnames section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_FUNCNAMES_VERSION_ERROR",
    "DW_DLE_DEBUG_FUNCNAMES_LENGTH_BAD",
    "DW_DLE_FUNC_NULL",
    "DW_DLE_FUNC_CONTEXT_NULL",
    "DW_DLE_DEBUG_TYPENAMES_DUPLICATE",
    "DW_DLE_DEBUG_TYPENAMES_NULL .debug_typenames section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_TYPENAMES_VERSION_ERROR",
    "DW_DLE_DEBUG_TYPENAMES_LENGTH_BAD",
    "DW_DLE_TYPE_NULL",
    "DW_DLE_TYPE_CONTEXT_NULL",
    "DW_DLE_DEBUG_VARNAMES_DUPLICATE",
    "DW_DLE_DEBUG_VARNAMES_NULL .debug_varnames section present but "
        "elf_getdata() failed or section is zero-length",
    "DW_DLE_DEBUG_VARNAMES_VERSION_ERROR",
    "DW_DLE_DEBUG_VARNAMES_LENGTH_BAD",
    "DW_DLE_VAR_NULL",
    "DW_DLE_VAR_CONTEXT_NULL",
    "DW_DLE_DEBUG_WEAKNAMES_DUPLICATE",
    "DW_DLE_DEBUG_WEAKNAMES_NULL .debug_weaknames section present but "
        "elf_getdata() failed or section is zero-length",

    "DW_DLE_DEBUG_WEAKNAMES_VERSION_ERROR",
    "DW_DLE_DEBUG_WEAKNAMES_LENGTH_BAD",
    "DW_DLE_WEAK_NULL",
    "DW_DLE_WEAK_CONTEXT_NULL (175)",
    "DW_DLE_LOCDESC_COUNT_WRONG (176)",
    "DW_DLE_MACINFO_STRING_NULL (177)",
    "DW_DLE_MACINFO_STRING_EMPTY (178)",
    "DW_DLE_MACINFO_INTERNAL_ERROR_SPACE (179)",
    "DW_DLE_MACINFO_MALLOC_FAIL (180)",
    "DW_DLE_DEBUGMACINFO_ERROR (181)",
    "DW_DLE_DEBUG_MACRO_LENGTH_BAD (182)",
    "DW_DLE_DEBUG_MACRO_MAX_BAD (183)",
    "DW_DLE_DEBUG_MACRO_INTERNAL_ERR (184)",
    "DW_DLE_DEBUG_MACRO_MALLOC_SPACE (185)",
    "DW_DLE_DEBUG_MACRO_INCONSISTENT (186)",
    "DW_DLE_DF_NO_CIE_AUGMENTATION(187)",
    "DW_DLE_DF_REG_NUM_TOO_HIGH(188)",
    "DW_DLE_DF_MAKE_INSTR_NO_INIT(189)",
    "DW_DLE_DF_NEW_LOC_LESS_OLD_LOC(190)",
    "DW_DLE_DF_POP_EMPTY_STACK(191)",
    "DW_DLE_DF_ALLOC_FAIL(192)",
    "DW_DLE_DF_FRAME_DECODING_ERROR(193)",
    "DW_DLE_DEBUG_LOC_SECTION_SHORT(194)",
    "DW_DLE_FRAME_AUGMENTATION_UNKNOWN(195)",
    "DW_DLE_PUBTYPE_CONTEXT(196)",
    "DW_DLE_DEBUG_PUBTYPES_LENGTH_BAD(197)",
    "DW_DLE_DEBUG_PUBTYPES_VERSION_ERROR(198)",
    "DW_DLE_DEBUG_PUBTYPES_DUPLICATE(199)",
    "DW_DLE_FRAME_CIE_DECODE_ERROR(200)",
    "DW_DLE_FRAME_REGISTER_UNREPRESENTABLE(201)",
    "DW_DLE_FRAME_REGISTER_COUNT_MISMATCH(202)",
    "DW_DLE_LINK_LOOP(203)",
    "DW_DLE_STRP_OFFSET_BAD(204)",
    "DW_DLE_DEBUG_RANGES_DUPLICATE(205)",
    "DW_DLE_DEBUG_RANGES_OFFSET_BAD(206)",
    "DW_DLE_DEBUG_RANGES_MISSING_END(207)",
    "DW_DLE_DEBUG_RANGES_OUT_OF_MEM(208)",
    "DW_DLE_DEBUG_SYMTAB_ERR(209)",
    "DW_DLE_DEBUG_STRTAB_ERR(210)",
    "DW_DLE_RELOC_MISMATCH_INDEX(211)",
    "DW_DLE_RELOC_MISMATCH_RELOC_INDEX(212)",
    "DW_DLE_RELOC_MISMATCH_STRTAB_INDEX(213)",
    "DW_DLE_RELOC_SECTION_MISMATCH(214)",
    "DW_DLE_RELOC_SECTION_MISSING_INDEX(215)",
    "DW_DLE_RELOC_SECTION_LENGTH_ODD(216)",
    "DW_DLE_RELOC_SECTION_PTR_NULL(217)",
    "DW_DLE_RELOC_SECTION_MALLOC_FAIL(218)",
    "DW_DLE_NO_ELF64_SUPPORT(219)",
    "DW_DLE_MISSING_ELF64_SUPPORT(220)",
    "DW_DLE_ORPHAN_FDE(221)",
    "DW_DLE_DUPLICATE_INST_BLOCK(222)",
    "DW_DLE_BAD_REF_SIG8_FORM(223)",
    "DW_DLE_ATTR_EXPRLOC_FORM_BAD(224)",
    "DW_DLE_FORM_SEC_OFFSET_LENGTH_BAD(225)",
    "DW_DLE_NOT_REF_FORM(226)",
    "DW_DLE_DEBUG_FRAME_LENGTH_NOT_MULTIPLE(227)",
    "DW_DLE_REF_SIG8_NOT_HANDLED (228)",
    "DW_DLE_DEBUG_FRAME_POSSIBLE_ADDRESS_BOTCH (229)",
    "DW_DLE_LOC_BAD_TERMINATION (230) the last location operator in an expression is missing some associated data, an operator ended too soon",
    "DW_DLE_SYMTAB_SECTION_LENGTH_ODD (231) so doing relocations seems unsafe",
    "DW_DLE_RELOC_SECTION_SYMBOL_INDEX_BAD (232) so doing a relocation seems unsafe",
    "DW_DLE_RELOC_SECTION_RELOC_TARGET_SIZE_UNKNOWN (233) so doing a relocation is unsafe",
    "DW_DLE_SYMTAB_SECTION_ENTRYSIZE_ZERO(234)",
    "DW_DLE_LINE_NUMBER_HEADER_ERROR (235), a line number program header seems incomplete (perhaps the header_length is wrong?).",
    "DW_DLE_DEBUG_TYPES_NULL (236)",
    "DW_DLE_DEBUG_TYPES_DUPLICATE (237)",
    "DW_DLE_DEBUG_TYPES_ONLY_DWARF4 (238)",
    "DW_DLE_DEBUG_TYPEOFFSET_BAD (239)",
    "DW_DLE_GNU_OPCODE_ERROR (240)",
};




/*  This function performs error handling as described in the 
    libdwarf consumer document section 3.  Dbg is the Dwarf_debug
    structure being processed.  Error is a pointer to the pointer
    to the error descriptor that will be returned.  Errval is an
    error code listed in dwarf_error.h.  */
void
_dwarf_error(Dwarf_Debug dbg, Dwarf_Error * error, Dwarf_Sword errval)
{
    Dwarf_Error errptr;

    /*  Allow NULL dbg on entry, since sometimes that can happen and we
        want to report the upper-level error, not this one. */
    if (error != NULL) {
        /*  If dbg is NULL, use the alternate error struct. However,
            this will overwrite the earlier error. */
        if (dbg != NULL) {
            errptr =
                (Dwarf_Error) _dwarf_get_alloc(dbg, DW_DLA_ERROR, 1);
            if (errptr == NULL) {
                fprintf(stderr,
                    "Could not allocate Dwarf_Error structure, "
                    "abort() in libdwarf.\n");
                abort();
            }
        } else {
            /*  We have no dbg to work with. dwarf_init failed. We hack
                up a special area. */
            errptr = _dwarf_special_no_dbg_error_malloc();
            if (errptr == NULL) {
                fprintf(stderr,
                    "Could not allocate Dwarf_Error structure, "
                    "abort() in libdwarf..\n");
                abort();
            }
        }

        errptr->er_errval = errval;
        *error = errptr;
        return;
    }

    if (dbg != NULL && dbg->de_errhand != NULL) {
        errptr = (Dwarf_Error) _dwarf_get_alloc(dbg, DW_DLA_ERROR, 1);
        if (errptr == NULL) {
            fprintf(stderr, "Could not allocate Dwarf_Error structure,"
                " abort() in libdwarf.\n");
            abort();
        }
        errptr->er_errval = errval;
        dbg->de_errhand(errptr, dbg->de_errarg);
        return;
    }
    fprintf(stderr,
        "abort() in libdwarf. No error argument, no handler.\n");
    abort();
}


Dwarf_Unsigned
dwarf_errno(Dwarf_Error error)
{
    if (error == NULL) {
        return (0);
    }

    return (error->er_errval);
}


/* 
*/
char *
dwarf_errmsg(Dwarf_Error error)
{
    if (error == NULL) {
        return "Dwarf_Error is NULL";
    }

    if (error->er_errval >= (sizeof(_dwarf_errmsgs) / sizeof(char *))) {
        return "Dwarf_Error value out of range";
    }

    return ((char *) _dwarf_errmsgs[error->er_errval]);
}