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: #include "sysdep.h"
29: #include "bfd.h"
30: #include "libiberty.h"
31: #include "safe-ctype.h"
32: #include "demangle.h"
33: #include "debug.h"
34: #include "budbg.h"
35: #include "filenames.h"
36: #include "aout/aout64.h"
37: #include "aout/stab_gnu.h"
38:
39:
40:
41: #define XCOFF_TYPE_COUNT 34
42:
43:
44:
45:
46: struct stab_handle
47: {
48:
49: bfd *abfd;
50:
51: bfd_boolean sections;
52:
53: asymbol **syms;
54:
55: long symcount;
56:
57: char *so_string;
58:
59: bfd_vma so_value;
60:
61:
62: bfd_vma file_start_offset;
63:
64:
65: bfd_vma function_start_offset;
66:
67:
68: int gcc_compiled;
69:
70:
71: bfd_boolean n_opt_found;
72:
73: char *main_filename;
74:
75: struct bincl_file *bincl_stack;
76:
77: struct bincl_file *bincl_list;
78:
79: bfd_boolean within_function;
80:
81:
82:
83: bfd_vma function_end;
84:
85: int block_depth;
86:
87: struct stab_pending_var *pending;
88:
89: unsigned int files;
90:
91: struct stab_types **file_types;
92:
93: debug_type xcoff_types[XCOFF_TYPE_COUNT];
94:
95: struct stab_tag *tags;
96:
97:
98: bfd_boolean self_crossref;
99: };
100:
101:
102:
103:
104: struct stab_pending_var
105: {
106:
107: struct stab_pending_var *next;
108:
109: const char *name;
110:
111: debug_type type;
112:
113: enum debug_var_kind kind;
114:
115: bfd_vma val;
116: };
117:
118:
119:
120:
121: struct stab_types
122: {
123:
124: struct stab_types *next;
125:
126: #define STAB_TYPES_SLOTS (16)
127: debug_type types[STAB_TYPES_SLOTS];
128: };
129:
130:
131:
132:
133: struct stab_tag
134: {
135:
136: struct stab_tag *next;
137:
138: const char *name;
139:
140: enum debug_type_kind kind;
141:
142:
143: debug_type slot;
144:
145: debug_type type;
146: };
147:
148: static char *savestring (const char *, int);
149: static bfd_vma parse_number (const char **, bfd_boolean *);
150: static void bad_stab (const char *);
151: static void warn_stab (const char *, const char *);
152: static bfd_boolean parse_stab_string
153: (void *, struct stab_handle *, int, int, bfd_vma, const char *);
154: static debug_type parse_stab_type
155: (void *, struct stab_handle *, const char *, const char **, debug_type **);
156: static bfd_boolean parse_stab_type_number (const char **, int *);
157: static debug_type parse_stab_range_type
158: (void *, struct stab_handle *, const char *, const char **, const int *);
159: static debug_type parse_stab_sun_builtin_type (void *, const char **);
160: static debug_type parse_stab_sun_floating_type (void *, const char **);
161: static debug_type parse_stab_enum_type (void *, const char **);
162: static debug_type parse_stab_struct_type
163: (void *, struct stab_handle *, const char *, const char **,
164: bfd_boolean, const int *);
165: static bfd_boolean parse_stab_baseclasses
166: (void *, struct stab_handle *, const char **, debug_baseclass **);
167: static bfd_boolean parse_stab_struct_fields
168: (void *, struct stab_handle *, const char **, debug_field **, bfd_boolean *);
169: static bfd_boolean parse_stab_cpp_abbrev
170: (void *, struct stab_handle *, const char **, debug_field *);
171: static bfd_boolean parse_stab_one_struct_field
172: (void *, struct stab_handle *, const char **, const char *,
173: debug_field *, bfd_boolean *);
174: static bfd_boolean parse_stab_members
175: (void *, struct stab_handle *, const char *, const char **, const int *,
176: debug_method **);
177: static debug_type parse_stab_argtypes
178: (void *, struct stab_handle *, debug_type, const char *, const char *,
179: debug_type, const char *, bfd_boolean, bfd_boolean, const char **);
180: static bfd_boolean parse_stab_tilde_field
181: (void *, struct stab_handle *, const char **, const int *, debug_type *,
182: bfd_boolean *);
183: static debug_type parse_stab_array_type
184: (void *, struct stab_handle *, const char **, bfd_boolean);
185: static void push_bincl (struct stab_handle *, const char *, bfd_vma);
186: static const char *pop_bincl (struct stab_handle *);
187: static bfd_boolean find_excl (struct stab_handle *, const char *, bfd_vma);
188: static bfd_boolean stab_record_variable
189: (void *, struct stab_handle *, const char *, debug_type,
190: enum debug_var_kind, bfd_vma);
191: static bfd_boolean stab_emit_pending_vars (void *, struct stab_handle *);
192: static debug_type *stab_find_slot (struct stab_handle *, const int *);
193: static debug_type stab_find_type (void *, struct stab_handle *, const int *);
194: static bfd_boolean stab_record_type
195: (void *, struct stab_handle *, const int *, debug_type);
196: static debug_type stab_xcoff_builtin_type
197: (void *, struct stab_handle *, int);
198: static debug_type stab_find_tagged_type
199: (void *, struct stab_handle *, const char *, int, enum debug_type_kind);
200: static debug_type *stab_demangle_argtypes
201: (void *, struct stab_handle *, const char *, bfd_boolean *, unsigned int);
202: static debug_type *stab_demangle_v3_argtypes
203: (void *, struct stab_handle *, const char *, bfd_boolean *);
204: static debug_type *stab_demangle_v3_arglist
205: (void *, struct stab_handle *, struct demangle_component *, bfd_boolean *);
206: static debug_type stab_demangle_v3_arg
207: (void *, struct stab_handle *, struct demangle_component *, debug_type,
208: bfd_boolean *);
209:
210:
211:
212: static char *
213: savestring (const char *start, int len)
214: {
215: char *ret;
216:
217: ret = (char *) xmalloc (len + 1);
218: memcpy (ret, start, len);
219: ret[len] = '\0';
220: return ret;
221: }
222:
223:
224:
225: static bfd_vma
226: parse_number (const char **pp, bfd_boolean *poverflow)
227: {
228: unsigned long ul;
229: const char *orig;
230:
231: if (poverflow != NULL)
232: *poverflow = FALSE;
233:
234: orig = *pp;
235:
236: errno = 0;
237: ul = strtoul (*pp, (char **) pp, 0);
238: if (ul + 1 != 0 || errno == 0)
239: {
240:
241:
242:
243: if (*orig == '-')
244: return (bfd_vma) (bfd_signed_vma) (long) ul;
245: return (bfd_vma) ul;
246: }
247:
248:
249:
250: if (sizeof (bfd_vma) > sizeof (unsigned long))
251: {
252: const char *p;
253: bfd_boolean neg;
254: int base;
255: bfd_vma over, lastdig;
256: bfd_boolean overflow;
257: bfd_vma v;
258:
259:
260: p = orig;
261:
262: neg = FALSE;
263: if (*p == '+')
264: ++p;
265: else if (*p == '-')
266: {
267: neg = TRUE;
268: ++p;
269: }
270:
271: base = 10;
272: if (*p == '0')
273: {
274: if (p[1] == 'x' || p[1] == 'X')
275: {
276: base = 16;
277: p += 2;
278: }
279: else
280: {
281: base = 8;
282: ++p;
283: }
284: }
285:
286: over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
287: lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
288:
289: overflow = FALSE;
290: v = 0;
291: while (1)
292: {
293: int d;
294:
295: d = *p++;
296: if (ISDIGIT (d))
297: d -= '0';
298: else if (ISUPPER (d))
299: d -= 'A';
300: else if (ISLOWER (d))
301: d -= 'a';
302: else
303: break;
304:
305: if (d >= base)
306: break;
307:
308: if (v > over || (v == over && (bfd_vma) d > lastdig))
309: {
310: overflow = TRUE;
311: break;
312: }
313: }
314:
315: if (! overflow)
316: {
317: if (neg)
318: v = - v;
319: return v;
320: }
321: }
322:
323:
324:
325: if (poverflow != NULL)
326: *poverflow = TRUE;
327: else
328: warn_stab (orig, _("numeric overflow"));
329:
330: return 0;
331: }
332:
333:
334:
335: static void
336: bad_stab (const char *p)
337: {
338: fprintf (stderr, _("Bad stab: %s\n"), p);
339: }
340:
341:
342:
343: static void
344: warn_stab (const char *p, const char *err)
345: {
346: fprintf (stderr, _("Warning: %s: %s\n"), err, p);
347: }
348:
349:
350:
351: void *
352: start_stab (void *dhandle ATTRIBUTE_UNUSED, bfd *abfd, bfd_boolean sections,
353: asymbol **syms, long symcount)
354: {
355: struct stab_handle *ret;
356:
357: ret = (struct stab_handle *) xmalloc (sizeof *ret);
358: memset (ret, 0, sizeof *ret);
359: ret->abfd = abfd;
360: ret->sections = sections;
361: ret->syms = syms;
362: ret->symcount = symcount;
363: ret->files = 1;
364: ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
365: ret->file_types[0] = NULL;
366: ret->function_end = (bfd_vma) -1;
367: return (void *) ret;
368: }
369:
370:
371:
372:
373: bfd_boolean
374: finish_stab (void *dhandle, void *handle)
375: {
376: struct stab_handle *info = (struct stab_handle *) handle;
377: struct stab_tag *st;
378:
379: if (info->within_function)
380: {
381: if (! stab_emit_pending_vars (dhandle, info)
382: || ! debug_end_function (dhandle, info->function_end))
383: return FALSE;
384: info->within_function = FALSE;
385: info->function_end = (bfd_vma) -1;
386: }
387:
388: for (st = info->tags; st != NULL; st = st->next)
389: {
390: enum debug_type_kind kind;
391:
392: kind = st->kind;
393: if (kind == DEBUG_KIND_ILLEGAL)
394: kind = DEBUG_KIND_STRUCT;
395: st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
396: if (st->slot == DEBUG_TYPE_NULL)
397: return FALSE;
398: }
399:
400: return TRUE;
401: }
402:
403:
404:
405: bfd_boolean
406: parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value,
407: const char *string)
408: {
409: struct stab_handle *info = (struct stab_handle *) handle;
410:
411:
412:
413:
414:
415: if (info->so_string != NULL
416: && (type != N_SO || *string == '\0' || value != info->so_value))
417: {
418: if (! debug_set_filename (dhandle, info->so_string))
419: return FALSE;
420: info->main_filename = info->so_string;
421:
422: info->gcc_compiled = 0;
423: info->n_opt_found = FALSE;
424:
425:
426:
427: if (! info->sections)
428: info->file_start_offset = info->so_value;
429:
430:
431:
432:
433: info->files = 1;
434: info->file_types = ((struct stab_types **)
435: xmalloc (sizeof *info->file_types));
436: info->file_types[0] = NULL;
437:
438: info->so_string = NULL;
439:
440:
441: }
442:
443: switch (type)
444: {
445: case N_FN:
446: case N_FN_SEQ:
447: break;
448:
449: case N_LBRAC:
450:
451: if (info->n_opt_found && desc == 1)
452: break;
453:
454: if (! info->within_function)
455: {
456: fprintf (stderr, _("N_LBRAC not within function\n"));
457: return FALSE;
458: }
459:
460:
461: if (! debug_start_block (dhandle,
462: (value
463: + info->file_start_offset
464: + info->function_start_offset)))
465: return FALSE;
466:
467:
468: if (! stab_emit_pending_vars (dhandle, info))
469: return FALSE;
470:
471: ++info->block_depth;
472: break;
473:
474: case N_RBRAC:
475:
476: if (info->n_opt_found && desc == 1)
477: break;
478:
479:
480:
481:
482: if (! stab_emit_pending_vars (dhandle, info))
483: return FALSE;
484:
485:
486: if (! debug_end_block (dhandle,
487: (value
488: + info->file_start_offset
489: + info->function_start_offset)))
490: return FALSE;
491:
492: --info->block_depth;
493: if (info->block_depth < 0)
494: {
495: fprintf (stderr, _("Too many N_RBRACs\n"));
496: return FALSE;
497: }
498: break;
499:
500: case N_SO:
501:
502: if (info->within_function)
503: {
504: bfd_vma endval;
505:
506: endval = value;
507: if (*string != '\0'
508: && info->function_end != (bfd_vma) -1
509: && info->function_end < endval)
510: endval = info->function_end;
511: if (! stab_emit_pending_vars (dhandle, info)
512: || ! debug_end_function (dhandle, endval))
513: return FALSE;
514: info->within_function = FALSE;
515: info->function_end = (bfd_vma) -1;
516: }
517:
518:
519:
520: if (*string == '\0')
521: return TRUE;
522:
523:
524:
525:
526:
527: if (info->so_string == NULL)
528: info->so_string = xstrdup (string);
529: else
530: {
531: char *f;
532:
533: f = info->so_string;
534:
535: if (IS_ABSOLUTE_PATH (string))
536: info->so_string = xstrdup (string);
537: else
538: info->so_string = concat (info->so_string, string,
539: (const char *) NULL);
540: free (f);
541: }
542:
543: info->so_value = value;
544:
545: break;
546:
547: case N_SOL:
548:
549: if (! debug_start_source (dhandle, string))
550: return FALSE;
551: break;
552:
553: case N_BINCL:
554:
555: push_bincl (info, string, value);
556: if (! debug_start_source (dhandle, string))
557: return FALSE;
558: break;
559:
560: case N_EINCL:
561:
562: if (! debug_start_source (dhandle, pop_bincl (info)))
563: return FALSE;
564: break;
565:
566: case N_EXCL:
567:
568:
569: if (! find_excl (info, string, value))
570: return FALSE;
571: break;
572:
573: case N_SLINE:
574: