1 /*
2 
3   Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4   Portions Copyright (C) 2008-2014 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 */
27 
28 #include "config.h"
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif /* HAVE_STDLIB_H */
35 #include "dwarf_incl.h"
36 #include "dwarf_alloc.h"
37 #include "dwarfstring.h"
38 #include "dwarf_error.h"
39 
40 /* Array to hold string representation of errors. Any time a
41    define is added to the list in libdwarf.h, a string should be
42    added to this Array
43 */
44 #include "dwarf_errmsg_list.h"
45 
46 /*  This function performs error handling as described in the
47     libdwarf consumer document section 3.  Dbg is the Dwarf_debug
48     structure being processed.  Error is a pointer to the pointer
49     to the error descriptor that will be returned.  Errval is an
50     error code listed in dwarf_error.h.
51 
52     If the malloc arena is exhausted we return a pointer to
53     a special static error record.  This special singleton
54     is mostly ignored by dwarf_dealloc().
55     Users should not be storing Dwarf_Error pointers
56     for long so this singleton is only going to cause
57     confusion when callers try to save an out-of-memory
58     Dwarf_Error pointer.
59     The _dwarf_failsafe_error is intended to
60     be an improvement over an abort() call.
61     The failsafe means we will not abort due to
62     a Dwarf_Error struct creation.
63 */
64 
65 /*  The user provides an explanatory string, the error
66     number itself explains little.
67     This prepends DW_DLE_USER_DECLARED_ERROR to the
68     caller-provided string.
69     New in April, 2020 .  Used by dwarfdump in a few
70     circumstances. */
71 void
dwarf_error_creation(Dwarf_Debug dbg,Dwarf_Error * err,char * errmsg)72 dwarf_error_creation(Dwarf_Debug dbg,
73     Dwarf_Error *err,
74     char *errmsg)
75 {
76     dwarfstring m;
77     if(!dbg) {
78         return;
79     }
80     dwarfstring_constructor(&m);
81     dwarfstring_append(&m,"DW_DLE_USER_DECLARED_ERROR: ");
82     dwarfstring_append(&m,errmsg);
83     _dwarf_error_string(dbg,err,
84         DW_DLE_USER_DECLARED_ERROR,
85         dwarfstring_string(&m));
86     dwarfstring_destructor(&m);
87 }
88 
89 
90 void
_dwarf_error(Dwarf_Debug dbg,Dwarf_Error * error,Dwarf_Signed errval)91 _dwarf_error(Dwarf_Debug dbg, Dwarf_Error * error,
92     Dwarf_Signed errval)
93 {
94     _dwarf_error_string(dbg,error,errval,0);
95 }
96 void
_dwarf_error_string(Dwarf_Debug dbg,Dwarf_Error * error,Dwarf_Signed errval,char * msg)97 _dwarf_error_string(Dwarf_Debug dbg, Dwarf_Error * error,
98     Dwarf_Signed errval,char *msg)
99 {
100     Dwarf_Error errptr;
101 
102     /*  Allow NULL dbg on entry, since sometimes that can happen and we
103         want to report the upper-level error, not this one. */
104     if (error) {
105         /*  If dbg is NULL, use the alternate error struct. However,
106             this will overwrite the earlier error. */
107         if (dbg) {
108             errptr =
109                 (Dwarf_Error) _dwarf_get_alloc(dbg, DW_DLA_ERROR, 1);
110             if (!errptr) {
111                 errptr = &_dwarf_failsafe_error;
112                 errptr->er_static_alloc = DE_STATIC;
113             } else {
114                 errptr->er_static_alloc = DE_STANDARD;
115             }
116         } else {
117             /*  We have no dbg to work with. dwarf_init
118                 failed. We hack
119                 up a special area. */
120             errptr = _dwarf_special_no_dbg_error_malloc();
121             if (!errptr) {
122                 errptr = &_dwarf_failsafe_error;
123                 errptr->er_static_alloc = DE_STATIC;
124             } else {
125                 errptr->er_static_alloc = DE_MALLOC;
126             }
127         }
128         errptr->er_errval = errval;
129         if (msg) {
130             dwarfstring *em = 0;
131 
132 #ifdef DEBUG
133 printf("libdwarfdetector ALLOC creating error string %s errval %ld errptr 0x%lx \n",msg,(long)errval,(unsigned long)errptr);
134 #endif
135             em = (dwarfstring *)calloc(1,sizeof(dwarfstring));
136             if (em) {
137                 dwarfstring_constructor(em);
138                 dwarfstring_append(em,msg);
139                 errptr->er_msg = (void*)em;
140             }
141         }
142         *error = errptr;
143         return;
144     }
145 
146     if (dbg != NULL && dbg->de_errhand != NULL) {
147         errptr = (Dwarf_Error) _dwarf_get_alloc(dbg, DW_DLA_ERROR, 1);
148         if (errptr == NULL) {
149             errptr = &_dwarf_failsafe_error;
150             errptr->er_static_alloc = DE_STATIC;
151         }
152         errptr->er_errval = errval;
153         dbg->de_errhand(errptr, dbg->de_errarg);
154         return;
155     }
156     fflush(stdout);
157     fprintf(stdout,
158         "\nNow abort() in libdwarf. "
159         "No error argument or handler available.\n");
160     fflush(stdout);
161     abort();
162 }
163 
164 
165 Dwarf_Unsigned
dwarf_errno(Dwarf_Error error)166 dwarf_errno(Dwarf_Error error)
167 {
168     if (!error) {
169         return (0);
170     }
171     return (error->er_errval);
172 }
173 
174 char*
dwarf_errmsg_by_number(Dwarf_Unsigned errornum)175 dwarf_errmsg_by_number(Dwarf_Unsigned errornum )
176 {
177     if (errornum >=
178         (Dwarf_Signed)(sizeof(_dwarf_errmsgs) / sizeof(char *))) {
179         return "Dwarf_Error value out of range";
180     }
181     return ((char *) _dwarf_errmsgs[errornum]);
182 }
183 
184 
185 /*
186 */
187 char *
dwarf_errmsg(Dwarf_Error error)188 dwarf_errmsg(Dwarf_Error error)
189 {
190     if (!error) {
191         return "Dwarf_Error is NULL";
192     }
193     if (error->er_msg) {
194         return dwarfstring_string(error->er_msg);
195     }
196     return  dwarf_errmsg_by_number(error->er_errval);
197 }
198