1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright 1995, 1996, 1997, 2000, 2002 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 /* This file reads debugging information into a generic form. This
23 file knows how to dig the debugging information out of an object
28 #include "libiberty.h"
32 static bfd_boolean read_section_stabs_debugging_info
33 PARAMS ((bfd *, asymbol **, long, PTR, bfd_boolean *));
34 static bfd_boolean read_symbol_stabs_debugging_info
35 PARAMS ((bfd *, asymbol **, long, PTR, bfd_boolean *));
36 static bfd_boolean read_ieee_debugging_info
37 PARAMS ((bfd *, PTR, bfd_boolean *));
39 PARAMS ((int, int, bfd_vma, const char *));
40 static void stab_context
42 static void free_saved_stabs
45 /* Read debugging information from a BFD. Returns a generic debugging
49 read_debugging_info (abfd, syms, symcount)
57 dhandle = debug_init ();
61 if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
65 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
67 if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle,
72 if (bfd_get_flavour (abfd) == bfd_target_ieee_flavour)
74 if (! read_ieee_debugging_info (abfd, dhandle, &found))
78 /* Try reading the COFF symbols if we didn't find any stabs in COFF
81 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
84 if (! parse_coff (abfd, syms, symcount, dhandle))
91 non_fatal (_("%s: no recognized debugging information"),
92 bfd_get_filename (abfd));
99 /* Read stabs in sections debugging information from a BFD. */
102 read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound)
112 const char *strsecname;
113 } names[] = { { ".stab", ".stabstr" },
114 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" } };
121 for (i = 0; i < sizeof names / sizeof names[0]; i++)
123 asection *sec, *strsec;
125 sec = bfd_get_section_by_name (abfd, names[i].secname);
126 strsec = bfd_get_section_by_name (abfd, names[i].strsecname);
127 if (sec != NULL && strsec != NULL)
129 bfd_size_type stabsize, strsize;
130 bfd_byte *stabs, *strings;
132 bfd_size_type stroff, next_stroff;
134 stabsize = bfd_section_size (abfd, sec);
135 stabs = (bfd_byte *) xmalloc (stabsize);
136 if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
138 fprintf (stderr, "%s: %s: %s\n",
139 bfd_get_filename (abfd), names[i].secname,
140 bfd_errmsg (bfd_get_error ()));
144 strsize = bfd_section_size (abfd, strsec);
145 strings = (bfd_byte *) xmalloc (strsize);
146 if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
148 fprintf (stderr, "%s: %s: %s\n",
149 bfd_get_filename (abfd), names[i].strsecname,
150 bfd_errmsg (bfd_get_error ()));
156 shandle = start_stab (dhandle, abfd, TRUE, syms, symcount);
165 for (stab = stabs; stab < stabs + stabsize; stab += 12)
173 /* This code presumes 32 bit values. */
175 strx = bfd_get_32 (abfd, stab);
176 type = bfd_get_8 (abfd, stab + 4);
177 other = bfd_get_8 (abfd, stab + 5);
178 desc = bfd_get_16 (abfd, stab + 6);
179 value = bfd_get_32 (abfd, stab + 8);
183 /* Special type 0 stabs indicate the offset to the
184 next string table. */
185 stroff = next_stroff;
186 next_stroff += value;
194 if (stroff + strx > strsize)
196 fprintf (stderr, "%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n",
197 bfd_get_filename (abfd), names[i].secname,
198 (long) (stab - stabs) / 12, strx, type);
202 s = (char *) strings + stroff + strx;
204 while (s[strlen (s) - 1] == '\\'
205 && stab + 12 < stabs + stabsize)
210 p = s + strlen (s) - 1;
215 + bfd_get_32 (abfd, stab)),
216 (const char *) NULL);
218 /* We have to restore the backslash, because, if
219 the linker is hashing stabs strings, we may
220 see the same string more than once. */
228 save_stab (type, desc, value, s);
230 if (! parse_stab (dhandle, shandle, type, desc, value, s))
237 /* Don't free f, since I think the stabs code
238 expects strings to hang around. This should be
239 straightened out. FIXME. */
246 /* Don't free strings, since I think the stabs code expects
247 the strings to hang around. This should be straightened
254 if (! finish_stab (dhandle, shandle))
261 /* Read stabs in the symbol table. */
264 read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound)
272 asymbol **ps, **symend;
275 symend = syms + symcount;
276 for (ps = syms; ps < symend; ps++)
280 bfd_get_symbol_info (abfd, *ps, &i);
289 shandle = start_stab (dhandle, abfd, FALSE, syms, symcount);
298 while (s[strlen (s) - 1] == '\\'
305 sc[strlen (sc) - 1] = '\0';
306 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
314 save_stab (i.stab_type, i.stab_desc, i.value, s);
316 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
324 /* Don't free f, since I think the stabs code expects
325 strings to hang around. This should be straightened out.
334 if (! finish_stab (dhandle, shandle))
341 /* Read IEEE debugging information. */
344 read_ieee_debugging_info (abfd, dhandle, pfound)
353 /* The BFD backend puts the debugging information into a section
356 dsec = bfd_get_section_by_name (abfd, ".debug");
360 size = bfd_section_size (abfd, dsec);
361 contents = (bfd_byte *) xmalloc (size);
362 if (! bfd_get_section_contents (abfd, dsec, contents, 0, size))
365 if (! parse_ieee (dhandle, abfd, contents, size))
375 /* Record stabs strings, so that we can give some context for errors. */
377 #define SAVE_STABS_COUNT (16)
387 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
388 static int saved_stabs_index;
390 /* Save a stabs string. */
393 save_stab (type, desc, value, string)
399 if (saved_stabs[saved_stabs_index].string != NULL)
400 free (saved_stabs[saved_stabs_index].string);
401 saved_stabs[saved_stabs_index].type = type;
402 saved_stabs[saved_stabs_index].desc = desc;
403 saved_stabs[saved_stabs_index].value = value;
404 saved_stabs[saved_stabs_index].string = xstrdup (string);
405 saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
408 /* Provide context for an error. */
415 fprintf (stderr, _("Last stabs entries before error:\n"));
416 fprintf (stderr, "n_type n_desc n_value string\n");
418 i = saved_stabs_index;
421 struct saved_stab *stabp;
423 stabp = saved_stabs + i;
424 if (stabp->string != NULL)
428 s = bfd_get_stab_name (stabp->type);
430 fprintf (stderr, "%-6s", s);
431 else if (stabp->type == 0)
432 fprintf (stderr, "HdrSym");
434 fprintf (stderr, "%-6d", stabp->type);
435 fprintf (stderr, " %-6d ", stabp->desc);
436 fprintf_vma (stderr, stabp->value);
437 if (stabp->type != 0)
438 fprintf (stderr, " %s", stabp->string);
439 fprintf (stderr, "\n");
441 i = (i + 1) % SAVE_STABS_COUNT;
443 while (i != saved_stabs_index);
446 /* Free the saved stab strings. */
453 for (i = 0; i < SAVE_STABS_COUNT; i++)
455 if (saved_stabs[i].string != NULL)
457 free (saved_stabs[i].string);
458 saved_stabs[i].string = NULL;
462 saved_stabs_index = 0;