dwarf_macro.c
9.04 KB
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
/*
dwarf_macro.c
$Revision: 1.1.1.1 $ $Date: 2002/05/02 03:29:20 $
Some consumer macro reading functions.
*/
#include <stdio.h>
#include <limits.h>
#include "dwarf_incl.h"
#include "dwarf_macro.h"
#define LEFTPAREN '('
#define RIGHTPAREN ')'
#define SPACE ' '
/*
Given the dwarf macro string, return a pointer to
the value. Returns pointer to 0 byte at end of string
if no value found (meaning the value is the empty string).
Only understands well-formed dwarf macinfo strings.
*/
char *dwarf_find_macro_value_start(char *str)
{
char *lcp;
int funclike = 0;
for(lcp = str; *lcp ; ++lcp) {
switch(*lcp){
case LEFTPAREN:
funclike = 1;
break;
case RIGHTPAREN:
/* lcp+1 must be a space, and
following char is the value */
return lcp+2;
case SPACE:
/* we allow extraneous spaces inside macro parameter
** list, just in case... This is not really needed.
*/
if(!funclike) {
return lcp+1;
}
break;
}
}
/* never found value: returns pointer to the 0 byte at end
of string
*/
return lcp;
}
/*
Try to keep fileindex correct in every Macro_Details
record by tracking file starts and ends.
Uses high water mark: space reused, not freed.
Presumption is that this makes sense for most uses.
STARTERMAX is set so that the array need not be expanded for
most files: it is the initial include file depth.
*/
static Dwarf_Signed *st_base;
static long max;
static long next_to_use;
static int was_fault = 0;
#define STARTERMAX 10
static void _dwarf_reset_index_stack(Dwarf_Debug dbg)
{
next_to_use = 0;
was_fault = 0;
}
static int _dwarf_mac_push_index(Dwarf_Debug dbg, Dwarf_Signed indx)
{
Dwarf_Signed *newbase;
if(next_to_use >= max) {
long new_size;
if(max == 0) {
max = STARTERMAX;
}
new_size = max*2;
newbase = _dwarf_get_alloc(dbg,DW_DLA_STRING,new_size*sizeof(Dwarf_Signed));
if(newbase == 0) {
/* just leave the old array in place */
was_fault = 1;
return DW_DLV_ERROR;
}
memcpy(newbase,st_base,next_to_use*sizeof(Dwarf_Signed));
dwarf_dealloc(dbg,st_base, DW_DLA_STRING);
st_base = newbase;
max = new_size;
}
st_base[next_to_use] = indx;
++next_to_use;
return DW_DLV_OK;
}
static Dwarf_Signed _dwarf_mac_pop_index(Dwarf_Debug dbg)
{
if(was_fault) {
return -1;
}
if(next_to_use > 0) {
next_to_use--;
return(*(st_base + next_to_use));
}
return -1;
}
/* starting at macro_offset in .debug_macinfo,
if maximum_count is 0, treat as if it is infinite.
get macro data up thru
maximum_count entries or the end of a compilation
unit's entries (whichever comes first).
*/
int dwarf_get_macro_details(Dwarf_Debug dbg,
Dwarf_Off macro_offset,
Dwarf_Unsigned maximum_count,
Dwarf_Signed * entry_count,
Dwarf_Macro_Details ** details,
Dwarf_Error * error)
{
Dwarf_Small *macro_base;
Dwarf_Small *pnext;
Dwarf_Small *pstart;
Dwarf_Unsigned endloc;
unsigned char uc;
/* count space used by strings */
unsigned long str_space = 0;
int done = 0;
int res;
unsigned long space_needed;
unsigned long string_offset;
Dwarf_Small * return_data;
Dwarf_Small * pdata;
unsigned long final_count = 0;
Dwarf_Signed fileindex = -1;
Dwarf_Small *latest_str_loc;
unsigned long count;
unsigned long max_count = maximum_count;
_dwarf_reset_index_stack(dbg);
if (dbg == NULL)
{_dwarf_error(NULL, error, DW_DLE_DBG_NULL); return(DW_DLV_ERROR);}
if(max_count < 1) {
_dwarf_error(NULL, error, DW_DLE_DEBUG_MACRO_MAX_BAD);
return(DW_DLV_ERROR);
}
macro_base = dbg->de_debug_macinfo;
if (macro_base == NULL) {
return(DW_DLV_NO_ENTRY);
}
if(macro_offset >= dbg->de_debug_macinfo_size) {
return(DW_DLV_NO_ENTRY);
}
pstart = pnext = macro_base + macro_offset;
if(maximum_count == 0) {
max_count = ULONG_MAX;
}
/* how many entries and how much space will they take? */
endloc = (pnext - macro_base);
if(endloc >= dbg->de_debug_macinfo_size) {
if(endloc == dbg->de_debug_macinfo_size) {
/* normal: found last entry */
return DW_DLV_NO_ENTRY;
}
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_LENGTH_BAD);
return(DW_DLV_ERROR);
}
for(count = 0 ;!done && count < max_count ; ++count) {
unsigned long slen;
Dwarf_Word len;
Dwarf_Unsigned v1;
Dwarf_Unsigned v2;
endloc = (pnext - macro_base)+1;
if( endloc > dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_LENGTH_BAD);
return(DW_DLV_ERROR);
}
uc = *pnext;
++pnext; /* get past the type code */
switch(uc) {
case DW_MACINFO_define:
case DW_MACINFO_undef:
/* line, string */
case DW_MACINFO_vendor_ext:
/* number, string */
v1 = _dwarf_decode_u_leb128(pnext,&len);
pnext += len;
if(((pnext - macro_base)) >= dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
}
slen = strlen((char *)pnext)+1;
pnext += slen;
if(((pnext - macro_base)) >= dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
}
str_space += slen;
break;
case DW_MACINFO_start_file:
/* line, file index */
v1 = _dwarf_decode_u_leb128(pnext,&len);
pnext += len;
if(((pnext - macro_base)) >= dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
}
v1 = _dwarf_decode_u_leb128(pnext,&len);
pnext += len;
if(((pnext - macro_base)) >= dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
}
break;
case DW_MACINFO_end_file:
break; /* no string or number here */
case 0:
done = 1;
/* end of cu's entries */
default:
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
/* bogus macinfo! */
}
}
if(count == 0) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INTERNAL_ERR);
return(DW_DLV_ERROR);
}
/* we have 'count' array entries to allocate and str_space
bytes of string space to provide for.
*/
string_offset = count *sizeof(Dwarf_Macro_Details);
/* extra 2 not really needed */
space_needed = string_offset + str_space + 2;
return_data = pdata = _dwarf_get_alloc(dbg, DW_DLA_STRING, space_needed);
latest_str_loc = pdata + string_offset;
if(pdata == 0) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_MALLOC_SPACE);
return(DW_DLV_ERROR);
}
pstart = pnext = macro_base + macro_offset;
for(final_count = 0 ;!done && final_count < count ; ++final_count) {
unsigned long slen;
Dwarf_Word len;
Dwarf_Unsigned v1;
Dwarf_Unsigned v2;
Dwarf_Macro_Details *pdmd = (Dwarf_Macro_Details *)(pstart +
final_count*sizeof(Dwarf_Macro_Details));
endloc = (pnext - macro_base)+1;
if( endloc > dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_LENGTH_BAD);
return(DW_DLV_ERROR);
}
uc = *pnext;
pdmd->dmd_offset = (pnext -macro_base);
pdmd->dmd_type = uc;
pdmd->dmd_fileindex = fileindex;
pdmd->dmd_lineno = 0;
pdmd->dmd_macro = 0;
++pnext; /* get past the type code */
switch(uc) {
case DW_MACINFO_define:
case DW_MACINFO_undef:
/* line, string */
case DW_MACINFO_vendor_ext:
/* number, string */
v1 = _dwarf_decode_u_leb128(pnext,&len);
pdmd->dmd_lineno = v1;
pnext += len;
if(((pnext - macro_base)) >= dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
}
slen = strlen((char *)pnext)+1;
strcpy((char *)latest_str_loc,(char *)pnext);
pdmd->dmd_macro = (char *)latest_str_loc;
latest_str_loc += slen;
pnext += slen;
if(((pnext - macro_base)) >= dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
}
str_space += slen;
break;
case DW_MACINFO_start_file:
/* line, file index */
v1 = _dwarf_decode_u_leb128(pnext,&len);
pdmd->dmd_lineno = v1;
pnext += len;
if(((pnext - macro_base)) >= dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
}
v1 = _dwarf_decode_u_leb128(pnext,&len);
pdmd->dmd_fileindex = v1;
res = _dwarf_mac_push_index(dbg,fileindex);
/* we ignore the error, we just let fileindex
** be -1 when we pop this one
*/
fileindex = v1;
pnext += len;
if(((pnext - macro_base)) >= dbg->de_debug_macinfo_size ) {
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
}
break;
case DW_MACINFO_end_file:
fileindex = _dwarf_mac_pop_index(dbg);
break; /* no string or number here */
case 0:
done = 1;
/* end of cu's entries */
default:
_dwarf_error(dbg,error,DW_DLE_DEBUG_MACRO_INCONSISTENT);
return(DW_DLV_ERROR);
/* bogus macinfo! */
}
}
*entry_count = count;
*details = (Dwarf_Macro_Details *)return_data;
return DW_DLV_OK;
}