1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright 1995, 1996, 1997, 2000, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 /* This file reads debugging information into a generic form. This
24 file knows how to dig the debugging information out of an object
29 #include "libiberty.h"
33 static bfd_boolean read_section_stabs_debugging_info
34 (bfd *, asymbol **, long, void *, bfd_boolean *);
35 static bfd_boolean read_symbol_stabs_debugging_info
36 (bfd *, asymbol **, long, void *, bfd_boolean *);
37 static bfd_boolean read_ieee_debugging_info (bfd *, void *, bfd_boolean *);
38 static void save_stab (int, int, bfd_vma, const char *);
39 static void stab_context (void);
40 static void free_saved_stabs (void);
42 /* Read debugging information from a BFD. Returns a generic debugging
46 read_debugging_info (bfd *abfd, asymbol **syms, long symcount)
51 dhandle = debug_init ();
55 if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
59 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
61 if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle,
66 if (bfd_get_flavour (abfd) == bfd_target_ieee_flavour)
68 if (! read_ieee_debugging_info (abfd, dhandle, &found))
72 /* Try reading the COFF symbols if we didn't find any stabs in COFF
75 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
78 if (! parse_coff (abfd, syms, symcount, dhandle))
85 non_fatal (_("%s: no recognized debugging information"),
86 bfd_get_filename (abfd));
93 /* Read stabs in sections debugging information from a BFD. */
96 read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
97 void *dhandle, bfd_boolean *pfound)
102 const char *strsecname;
103 } names[] = { { ".stab", ".stabstr" },
104 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" } };
111 for (i = 0; i < sizeof names / sizeof names[0]; i++)
113 asection *sec, *strsec;
115 sec = bfd_get_section_by_name (abfd, names[i].secname);
116 strsec = bfd_get_section_by_name (abfd, names[i].strsecname);
117 if (sec != NULL && strsec != NULL)
119 bfd_size_type stabsize, strsize;
120 bfd_byte *stabs, *strings;
122 bfd_size_type stroff, next_stroff;
124 stabsize = bfd_section_size (abfd, sec);
125 stabs = (bfd_byte *) xmalloc (stabsize);
126 if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
128 fprintf (stderr, "%s: %s: %s\n",
129 bfd_get_filename (abfd), names[i].secname,
130 bfd_errmsg (bfd_get_error ()));
134 strsize = bfd_section_size (abfd, strsec);
135 strings = (bfd_byte *) xmalloc (strsize);
136 if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
138 fprintf (stderr, "%s: %s: %s\n",
139 bfd_get_filename (abfd), names[i].strsecname,
140 bfd_errmsg (bfd_get_error ()));
146 shandle = start_stab (dhandle, abfd, TRUE, syms, symcount);
155 for (stab = stabs; stab < stabs + stabsize; stab += 12)
163 /* This code presumes 32 bit values. */
165 strx = bfd_get_32 (abfd, stab);
166 type = bfd_get_8 (abfd, stab + 4);
167 other = bfd_get_8 (abfd, stab + 5);
168 desc = bfd_get_16 (abfd, stab + 6);
169 value = bfd_get_32 (abfd, stab + 8);
173 /* Special type 0 stabs indicate the offset to the
174 next string table. */
175 stroff = next_stroff;
176 next_stroff += value;
184 if (stroff + strx > strsize)
186 fprintf (stderr, "%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n",
187 bfd_get_filename (abfd), names[i].secname,
188 (long) (stab - stabs) / 12, strx, type);
192 s = (char *) strings + stroff + strx;
194 while (s[strlen (s) - 1] == '\\'
195 && stab + 12 < stabs + stabsize)
200 p = s + strlen (s) - 1;
205 + bfd_get_32 (abfd, stab)),
206 (const char *) NULL);
208 /* We have to restore the backslash, because, if
209 the linker is hashing stabs strings, we may
210 see the same string more than once. */
218 save_stab (type, desc, value, s);
220 if (! parse_stab (dhandle, shandle, type, desc, value, s))
227 /* Don't free f, since I think the stabs code
228 expects strings to hang around. This should be
229 straightened out. FIXME. */
236 /* Don't free strings, since I think the stabs code expects
237 the strings to hang around. This should be straightened
244 if (! finish_stab (dhandle, shandle))
251 /* Read stabs in the symbol table. */
254 read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
255 void *dhandle, bfd_boolean *pfound)
258 asymbol **ps, **symend;
261 symend = syms + symcount;
262 for (ps = syms; ps < symend; ps++)
266 bfd_get_symbol_info (abfd, *ps, &i);
275 shandle = start_stab (dhandle, abfd, FALSE, syms, symcount);
284 while (s[strlen (s) - 1] == '\\'
291 sc[strlen (sc) - 1] = '\0';
292 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
300 save_stab (i.stab_type, i.stab_desc, i.value, s);
302 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
310 /* Don't free f, since I think the stabs code expects
311 strings to hang around. This should be straightened out.
320 if (! finish_stab (dhandle, shandle))
327 /* Read IEEE debugging information. */
330 read_ieee_debugging_info (bfd *abfd, void *dhandle, bfd_boolean *pfound)
336 /* The BFD backend puts the debugging information into a section
339 dsec = bfd_get_section_by_name (abfd, ".debug");
343 size = bfd_section_size (abfd, dsec);
344 contents = (bfd_byte *) xmalloc (size);
345 if (! bfd_get_section_contents (abfd, dsec, contents, 0, size))
348 if (! parse_ieee (dhandle, abfd, contents, size))
358 /* Record stabs strings, so that we can give some context for errors. */
360 #define SAVE_STABS_COUNT (16)
370 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
371 static int saved_stabs_index;
373 /* Save a stabs string. */
376 save_stab (int type, int desc, bfd_vma value, const char *string)
378 if (saved_stabs[saved_stabs_index].string != NULL)
379 free (saved_stabs[saved_stabs_index].string);
380 saved_stabs[saved_stabs_index].type = type;
381 saved_stabs[saved_stabs_index].desc = desc;
382 saved_stabs[saved_stabs_index].value = value;
383 saved_stabs[saved_stabs_index].string = xstrdup (string);
384 saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
387 /* Provide context for an error. */
394 fprintf (stderr, _("Last stabs entries before error:\n"));
395 fprintf (stderr, "n_type n_desc n_value string\n");
397 i = saved_stabs_index;
400 struct saved_stab *stabp;
402 stabp = saved_stabs + i;
403 if (stabp->string != NULL)
407 s = bfd_get_stab_name (stabp->type);
409 fprintf (stderr, "%-6s", s);
410 else if (stabp->type == 0)
411 fprintf (stderr, "HdrSym");
413 fprintf (stderr, "%-6d", stabp->type);
414 fprintf (stderr, " %-6d ", stabp->desc);
415 fprintf_vma (stderr, stabp->value);
416 if (stabp->type != 0)
417 fprintf (stderr, " %s", stabp->string);
418 fprintf (stderr, "\n");
420 i = (i + 1) % SAVE_STABS_COUNT;
422 while (i != saved_stabs_index);
425 /* Free the saved stab strings. */
428 free_saved_stabs (void)
432 for (i = 0; i < SAVE_STABS_COUNT; i++)
434 if (saved_stabs[i].string != NULL)
436 free (saved_stabs[i].string);
437 saved_stabs[i].string = NULL;
441 saved_stabs_index = 0;