Debian Codegraph @

src/lib/libdwarf/dwarf_macro.c

raw
1 /*
2 Copyright (C) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2020 David Anderson. All Rights Reserved.
4 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
5
6 This program is free software; you can redistribute it
7 and/or modify it under the terms of version 2.1 of the
8 GNU Lesser General Public License as published by the Free
9 Software Foundation.
10
11 This program is distributed in the hope that it would be
12 useful, but WITHOUT ANY WARRANTY; without even the implied
13 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.
15
16 Further, this software is distributed without any warranty
17 that it is free of the rightful claim of any third person
18 regarding infringement or the like. Any license provided
19 herein, whether implied or otherwise, applies only to this
20 software file. Patent licenses, if any, provided herein
21 do not apply to combinations of this program with other
22 software, or any other product whatsoever.
23
24 You should have received a copy of the GNU Lesser General
25 Public License along with this program; if not, write the
26 Free Software Foundation, Inc., 51 Franklin Street - Fifth
27 Floor, Boston MA 02110-1301, USA.
28
29 */
30
31 #include <config.h>
32
33 #include <limits.h> /* ULONG_MAX */
34 #include <string.h> /* memcpy() strlen() */
35
36 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
37 #include "stdafx.h"
38 #endif /* HAVE_STDAFX_H */
39
40 #include "dwarf.h"
41 #include "libdwarf.h"
42 #include "libdwarf_private.h"
43 #include "dwarf_base_types.h"
44 #include "dwarf_safe_strcpy.h"
45 #include "dwarf_opaque.h"
46 #include "dwarf_alloc.h"
47 #include "dwarf_error.h"
48 #include "dwarf_util.h"
49 #include "dwarf_macro.h"
50
51 #define LEFTPAREN '('
52 #define RIGHTPAREN ')'
53 #define SPACE ' '
54
55 /* Given the dwarf macro string, return a pointer to
56 the value. Returns pointer to 0 byte at end of string
57 if no value found (meaning the value is the empty string).
58
59 Only understands well-formed .debug_macinfo
60 and .debug_macro strings.
61
62 */
63 char *
64 dwarf_find_macro_value_start(char *str)
65 {
66 char *lcp;
67 int funclike = 0;
68
69 for (lcp = str; *lcp; ++lcp) {
70 switch (*lcp) {
71 case LEFTPAREN:
72 ++funclike;
73 break;
74 case RIGHTPAREN:
75 --funclike;
76 break;
77 case SPACE:
78 /* We allow extraneous spaces inside macro parameter **
79 list, just in case... This is not really needed. */
80 if (!funclike) {
81 return lcp + 1;
82 }
83 break;
84 default:
85 break;
86 }
87 }
88 /* Never found value: returns pointer to the 0 byte at end of
89 string.
90 Or maybe the parentheses are unbalanced! Compiler error?
91 */
92 return lcp;
93 }
94
95 /*
96 Try to keep fileindex correct in every Macro_Details
97 record by tracking file starts and ends.
98 Uses high water mark: space reused, not freed.
99 Presumption is that this makes sense for most uses.
100 STARTERMAX is set so that the array need not be expanded for
101 most files: it is the initial include file depth.
102 */
103 struct macro_stack_s {
104 Dwarf_Signed *st_base;
105 long st_max;
106 long st_next_to_use;
107 int st_was_fault;
108 };
109
110 static void _dwarf_reset_index_macro_stack(struct macro_stack_s *ms);
111 static void
112 free_macro_stack(Dwarf_Debug dbg, struct macro_stack_s *ms)
113 {
114 dwarf_dealloc(dbg,ms->st_base,DW_DLA_STRING);
115 _dwarf_reset_index_macro_stack(ms);
116 }
117
118 #define STARTERMAX 10
119 static void
120 _dwarf_reset_index_macro_stack(struct macro_stack_s *ms)
121 {
122 ms->st_base = 0;
123 ms->st_max = 0;
124 ms->st_next_to_use = 0;
125 ms->st_was_fault = 0;
126 }
127 static int
128 _dwarf_macro_stack_push_index(Dwarf_Debug dbg, Dwarf_Signed indx,
129 struct macro_stack_s *ms)
130 {
131
132 if (!ms->st_max || ms->st_next_to_use >= ms->st_max) {
133 long new_size = ms->st_max;
134 Dwarf_Signed *newbase = 0;
135
136 if (!new_size) {
137 new_size = STARTERMAX;
138 }
139 new_size = new_size * 2;
140 newbase =
141 (Dwarf_Signed *)_dwarf_get_alloc(dbg, DW_DLA_STRING,
142 new_size * sizeof(Dwarf_Signed));
143 if (!newbase) {
144 /* just leave the old array in place */
145 ms->st_was_fault = 1;
146 return DW_DLV_ERROR;
147 }
148 if (ms->st_base) {
149 memcpy(newbase, ms->st_base,
150 ms->st_next_to_use * sizeof(*newbase));
151 dwarf_dealloc(dbg, ms->st_base, DW_DLA_STRING);
152 }
153 ms->st_base = newbase;
154 ms->st_max = new_size;
155 }
156 ms->st_base[ms->st_next_to_use] = indx;
157 ++ms->st_next_to_use;
158 return DW_DLV_OK;
159 }
160
161 static Dwarf_Signed
162 _dwarf_macro_stack_pop_index(struct macro_stack_s *ms)
163 {
164 if (ms->st_was_fault) {
165 return -1;
166 }
167 if (ms->st_next_to_use > 0) {
168 ms->st_next_to_use--;
169 return (ms->st_base[ms->st_next_to_use]);
170 } else {
171 ms->st_was_fault = 1;
172 }
173 return -1;
174 }
175
176 /* Starting at macro_offset in .debug_macinfo,
177 if maximum_count is 0, treat as if it is infinite.
178 get macro data up thru
179 maximum_count entries or the end of a compilation
180 unit's entries (whichever comes first).
181
182 .debug_macinfo never appears in a .dwp Package File.
183 So offset adjustment for such is not needed.
184 */
185
186 int
187 dwarf_get_macro_details(Dwarf_Debug dbg,
188 Dwarf_Off macro_offset,
189 Dwarf_Unsigned maximum_count,
190 Dwarf_Signed * entry_count,
191 Dwarf_Macro_Details ** details,
192 Dwarf_Error * error)
193 {
194 Dwarf_Small *macro_base = 0;
195 Dwarf_Small *macro_end = 0;
196 Dwarf_Small *pnext = 0;
197 Dwarf_Unsigned endloc = 0;
198 unsigned char uc = 0;
199 unsigned long depth = 0;
200 /* By section 6.3.2 Dwarf3 draft 8/9,
201 the base file should appear as
202 DW_MACINFO_start_file. See
203 http://gcc.gnu.org/ml/gcc-bugs/2005-02/msg03442.html
204 on "[Bug debug/20253] New: [3.4/4.0 regression]:
205 Macro debug info broken due to lexer change" for how
206 gcc is broken in some versions. We no longer use
207 depth as a stopping point, it's not needed as a
208 stopping point anyway. */
209 int res = 0;
210 /* count space used by strings */
211 unsigned long str_space = 0;
212 int done = 0;
213 unsigned long space_needed = 0;
214 unsigned long space_used = 0;
215 unsigned long string_offset = 0;
216 Dwarf_Small *return_data = 0;
217 Dwarf_Small *pdata = 0;
218 unsigned long final_count = 0;
219 Dwarf_Signed fileindex = -1;
220 Dwarf_Small *latest_str_loc = 0;
221 struct macro_stack_s msdata;
222
223 unsigned long count = 0;
224 unsigned long max_count = (unsigned long) maximum_count;
225 _dwarf_reset_index_macro_stack(&msdata);
226 if (IS_INVALID_DBG(dbg)) {
227 _dwarf_error_string(NULL, error, DW_DLE_DBG_NULL,
228 "DW_DLE_DBG_NULL: Either null or it contains"
229 "a stale Dwarf_Debug pointer");
230 free_macro_stack(dbg,&msdata);
231 return DW_DLV_ERROR;
232 }
233
234 res = _dwarf_load_section(dbg, &dbg->de_debug_macinfo,error);
235 if (res != DW_DLV_OK) {
236 free_macro_stack(dbg,&msdata);
237 return res;
238 }
239 if (!dbg->de_debug_abbrev.dss_size) {
240 free_macro_stack(dbg,&msdata);
241 return DW_DLV_NO_ENTRY;
242 }
243
244 macro_base = dbg->de_debug_macinfo.dss_data;
245 if (macro_base == NULL) {
246 free_macro_stack(dbg,&msdata);
247 return DW_DLV_NO_ENTRY;
248 }
249 if (macro_offset >= dbg->de_debug_macinfo.dss_size) {
250 free_macro_stack(dbg,&msdata);
251 return DW_DLV_NO_ENTRY;
252 }
253 macro_end = macro_base + dbg->de_debug_macinfo.dss_size;
254 /* FIXME debugfission is NOT handled here. */
255
256 pnext = macro_base + macro_offset;
257 if (maximum_count == 0) {
258 max_count = ULONG_MAX;
259 }
260
261 /* how many entries and how much space will they take? */
262 endloc = (pnext - macro_base);
263 if (endloc >= dbg->de_debug_macinfo.dss_size) {
264 if (endloc == dbg->de_debug_macinfo.dss_size) {
265 /* normal: found last entry */
266 free_macro_stack(dbg,&msdata);
267 return DW_DLV_NO_ENTRY;
268 }
269 _dwarf_error(dbg, error, DW_DLE_DEBUG_MACRO_LENGTH_BAD);
270 free_macro_stack(dbg,&msdata);
271 return DW_DLV_ERROR;
272 }
273 for (count = 0; !done && count < max_count; ++count) {
274 unsigned long slen = 0;
275
276 uc = *pnext;
277 ++pnext; /* get past the type code */
278 switch (uc) {
279 case DW_MACINFO_define:
280 case DW_MACINFO_undef:
281 /* line, string */
282 case DW_MACINFO_vendor_ext:
283 /* number, string */
284 SKIP_LEB128_CK(pnext,dbg,error,
285 macro_end);
286 if (((Dwarf_Unsigned)(pnext - macro_base)) >=
287 dbg->de_debug_macinfo.dss_size) {
288 free_macro_stack(dbg,&msdata);
289 _dwarf_error(dbg, error,
290 DW_DLE_DEBUG_MACRO_INCONSISTENT);
291 return DW_DLV_ERROR;
292 }
293 res = _dwarf_check_string_valid(dbg,
294 macro_base,pnext,macro_end,
295 DW_DLE_MACINFO_STRING_BAD,error);
296 if (res != DW_DLV_OK) {
297 return res;
298 }
299 slen = (unsigned long)strlen((char *) pnext) + 1;
300 pnext += slen;
301 if (((Dwarf_Unsigned)(pnext - macro_base)) >=
302 dbg->de_debug_macinfo.dss_size) {
303 free_macro_stack(dbg,&msdata);
304 _dwarf_error(dbg, error,
305 DW_DLE_DEBUG_MACRO_INCONSISTENT);
306 return DW_DLV_ERROR;
307 }
308 str_space += slen;
309 break;
310 case DW_MACINFO_start_file:
311 /* line, file index */
312 SKIP_LEB128_CK(pnext,dbg,error,
313 macro_end);
314 if (((Dwarf_Unsigned)(pnext - macro_base)) >=
315 dbg->de_debug_macinfo.dss_size) {
316 free_macro_stack(dbg,&msdata);
317 _dwarf_error(dbg, error,
318 DW_DLE_DEBUG_MACRO_INCONSISTENT);
319 return DW_DLV_ERROR;
320 }
321 SKIP_LEB128_CK(pnext,dbg,error,
322 macro_end);
323 if (((Dwarf_Unsigned)(pnext - macro_base)) >=
324 dbg->de_debug_macinfo.dss_size) {
325 free_macro_stack(dbg,&msdata);
326 _dwarf_error(dbg, error,
327 DW_DLE_DEBUG_MACRO_INCONSISTENT);
328 return DW_DLV_ERROR;
329 }
330 ++depth;
331 break;
332
333 case DW_MACINFO_end_file:
334 if (depth) {
335 --depth;
336 }
337 if (!depth) {
338 /* done = 1; no, do not stop here,
339 at least one gcc had
340 the wrong depth settings in the
341 gcc 3.4 timeframe. */
342 }
343 /* no string or number here */
344 break;
345 case 0:
346 /* end of cu's entries */
347 done = 1;
348 break;
349 default:
350 free_macro_stack(dbg,&msdata);
351 _dwarf_error(dbg, error, DW_DLE_DEBUG_MACRO_INCONSISTENT);
352 return DW_DLV_ERROR;
353 /* bogus macinfo! */
354 }
355
356 endloc = (pnext - macro_base);
357 if (endloc == dbg->de_debug_macinfo.dss_size) {
358 done = 1;
359 } else if (endloc > dbg->de_debug_macinfo.dss_size) {
360 _dwarf_error(dbg, error, DW_DLE_DEBUG_MACRO_LENGTH_BAD);
361 free_macro_stack(dbg,&msdata);
362 return DW_DLV_ERROR;
363 }
364 }
365 /* ASSERT: The above loop will never let us get here
366 with count < 1. No need to test for a zero count.
367
368 We have 'count' array entries to allocate and
369 str_space bytes of string space to provide for. */
370
371 string_offset = count * sizeof(Dwarf_Macro_Details);
372
373 /* extra 2 not really needed */
374 space_needed = string_offset + str_space + 2;
375 space_used = 0;
376 return_data = pdata = (Dwarf_Small *)_dwarf_get_alloc(
377 dbg, DW_DLA_STRING, space_needed);
378 latest_str_loc = pdata + string_offset;
379 if (pdata == 0) {
380 free_macro_stack(dbg,&msdata);
381 _dwarf_error(dbg, error, DW_DLE_DEBUG_MACRO_MALLOC_SPACE);
382 return DW_DLV_ERROR;
383 }
384 pnext = macro_base + macro_offset;
385
386 done = 0;
387
388 /* A series ends with a type code of 0. */
389
390 for (final_count = 0; !done && final_count < count;
391 ++final_count) {
392 unsigned long slen = 0;
393 Dwarf_Unsigned v1 = 0;
394 Dwarf_Macro_Details *pdmd = (Dwarf_Macro_Details *) (pdata +
395 (final_count * sizeof (Dwarf_Macro_Details)));
396
397 endloc = (pnext - macro_base);
398 if (endloc > dbg->de_debug_macinfo.dss_size) {
399 dwarf_dealloc(dbg,return_data,DW_DLA_STRING);
400 free_macro_stack(dbg,&msdata);
401 _dwarf_error(dbg, error, DW_DLE_DEBUG_MACRO_LENGTH_BAD);
402 return DW_DLV_ERROR;
403 }
404 uc = *pnext;
405 pdmd->dmd_offset = (pnext - macro_base);
406 pdmd->dmd_type = uc;
407 pdmd->dmd_fileindex = fileindex;
408 pdmd->dmd_lineno = 0;
409 pdmd->dmd_macro = 0;
410 ++pnext; /* get past the type code */
411 switch (uc) {
412 case DW_MACINFO_define:
413 case DW_MACINFO_undef:
414 /* line, string */
415 case DW_MACINFO_vendor_ext:
416 /* number, string */
417 res = _dwarf_leb128_uword_wrapper(dbg,&pnext,
418 macro_end,&v1,error);
419 if (res != DW_DLV_OK) {
420 free_macro_stack(dbg,&msdata);
421 dwarf_dealloc(dbg, return_data, DW_DLA_STRING);
422 return res;
423 }
424 pdmd->dmd_lineno = v1;
425
426 if (((Dwarf_Unsigned)(pnext - macro_base)) >=
427 dbg->de_debug_macinfo.dss_size) {
428 free_macro_stack(dbg,&msdata);
429 dwarf_dealloc(dbg, return_data, DW_DLA_STRING);
430 _dwarf_error(dbg, error,
431 DW_DLE_DEBUG_MACRO_INCONSISTENT);
432 return DW_DLV_ERROR;
433 }
434 res = _dwarf_check_string_valid(dbg,
435 macro_base,pnext,macro_end,
436 DW_DLE_MACINFO_STRING_BAD,error);
437 if (res != DW_DLV_OK) {
438 dwarf_dealloc(dbg,return_data,DW_DLA_STRING);
439 return res;
440 }
441 slen = (unsigned long)strlen((char *) pnext) + 1;
442 _dwarf_safe_strcpy((char *)latest_str_loc,
443 space_needed - space_used,
444 (const char *)pnext,slen-1);
445 pdmd->dmd_macro = (char *) latest_str_loc;
446 latest_str_loc += slen;
447 space_used +=slen;
448 pnext += slen;
449 if (((Dwarf_Unsigned)(pnext - macro_base)) >=
450 dbg->de_debug_macinfo.dss_size) {
451 free_macro_stack(dbg,&msdata);
452 dwarf_dealloc(dbg, return_data, DW_DLA_STRING);
453 _dwarf_error(dbg, error,
454 DW_DLE_DEBUG_MACRO_INCONSISTENT);
455 return DW_DLV_ERROR;
456 }
457 break;
458 case DW_MACINFO_start_file:
459 /* Line, file index */
460 res = _dwarf_leb128_uword_wrapper(dbg,&pnext,
461 macro_end,&v1,error);
462 if (res != DW_DLV_OK) {
463 free_macro_stack(dbg,&msdata);
464 dwarf_dealloc(dbg, return_data, DW_DLA_STRING);
465 return res;
466 }
467 pdmd->dmd_lineno = v1;
468 if (((Dwarf_Unsigned)(pnext - macro_base)) >=
469 dbg->de_debug_macinfo.dss_size) {
470 free_macro_stack(dbg,&msdata);
471 dwarf_dealloc(dbg, return_data, DW_DLA_STRING);
472 _dwarf_error(dbg, error,
473 DW_DLE_DEBUG_MACRO_INCONSISTENT);
474 return DW_DLV_ERROR;
475 }
476 res = _dwarf_leb128_uword_wrapper(dbg,&pnext,
477 macro_end,&v1,error);
478 if (res != DW_DLV_OK) {
479 free_macro_stack(dbg,&msdata);
480 dwarf_dealloc(dbg, return_data, DW_DLA_STRING);
481 return res;
482 }
483 pdmd->dmd_fileindex = v1;
484 (void) _dwarf_macro_stack_push_index(dbg, fileindex,
485 &msdata);
486 /* We ignore the error, we just let
487 fileindex ** be -1 when
488 we pop this one. */
489 fileindex = v1;
490 if (((Dwarf_Unsigned)(pnext - macro_base)) >=
491 dbg->de_debug_macinfo.dss_size) {
492 free_macro_stack(dbg,&msdata);
493 dwarf_dealloc(dbg, return_data, DW_DLA_STRING);
494 _dwarf_error(dbg, error,
495 DW_DLE_DEBUG_MACRO_INCONSISTENT);
496 return DW_DLV_ERROR;
497 }
498 break;
499
500 case DW_MACINFO_end_file:
501 fileindex = _dwarf_macro_stack_pop_index(&msdata);
502 break; /* no string or number here */
503 case 0:
504 /* Type code of 0 means the end of cu's entries. */
505 done = 1;
506 break;
507 default:
508 /* Bogus macinfo! */
509 dwarf_dealloc(dbg, return_data, DW_DLA_STRING);
510 free_macro_stack(dbg,&msdata);
511 _dwarf_error(dbg, error, DW_DLE_DEBUG_MACRO_INCONSISTENT);
512 return DW_DLV_ERROR;
513 }
514 }
515 *entry_count = count;
516 *details = (Dwarf_Macro_Details *) return_data;
517 free_macro_stack(dbg,&msdata);
518 return DW_DLV_OK;
519 }