Convert to C90
[binutils.git] / binutils / rddbg.c
blob2f6ec6e3ea1b924aca63da7012cb8cb390767829
1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright 1995, 1996, 1997, 2000, 2002 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
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
20 02111-1307, USA. */
22 /* This file reads debugging information into a generic form. This
23 file knows how to dig the debugging information out of an object
24 file. */
26 #include "bfd.h"
27 #include "bucomm.h"
28 #include "libiberty.h"
29 #include "debug.h"
30 #include "budbg.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 *));
38 static void save_stab
39 PARAMS ((int, int, bfd_vma, const char *));
40 static void stab_context
41 PARAMS ((void));
42 static void free_saved_stabs
43 PARAMS ((void));
45 /* Read debugging information from a BFD. Returns a generic debugging
46 pointer. */
48 PTR
49 read_debugging_info (abfd, syms, symcount)
50 bfd *abfd;
51 asymbol **syms;
52 long symcount;
54 PTR dhandle;
55 bfd_boolean found;
57 dhandle = debug_init ();
58 if (dhandle == NULL)
59 return NULL;
61 if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
62 &found))
63 return NULL;
65 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
67 if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle,
68 &found))
69 return NULL;
72 if (bfd_get_flavour (abfd) == bfd_target_ieee_flavour)
74 if (! read_ieee_debugging_info (abfd, dhandle, &found))
75 return NULL;
78 /* Try reading the COFF symbols if we didn't find any stabs in COFF
79 sections. */
80 if (! found
81 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
82 && symcount > 0)
84 if (! parse_coff (abfd, syms, symcount, dhandle))
85 return NULL;
86 found = TRUE;
89 if (! found)
91 non_fatal (_("%s: no recognized debugging information"),
92 bfd_get_filename (abfd));
93 return NULL;
96 return dhandle;
99 /* Read stabs in sections debugging information from a BFD. */
101 static bfd_boolean
102 read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound)
103 bfd *abfd;
104 asymbol **syms;
105 long symcount;
106 PTR dhandle;
107 bfd_boolean *pfound;
109 static struct
111 const char *secname;
112 const char *strsecname;
113 } names[] = { { ".stab", ".stabstr" },
114 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" } };
115 unsigned int i;
116 PTR shandle;
118 *pfound = FALSE;
119 shandle = NULL;
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;
131 bfd_byte *stab;
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 ()));
141 return FALSE;
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 ()));
151 return FALSE;
154 if (shandle == NULL)
156 shandle = start_stab (dhandle, abfd, TRUE, syms, symcount);
157 if (shandle == NULL)
158 return FALSE;
161 *pfound = TRUE;
163 stroff = 0;
164 next_stroff = 0;
165 for (stab = stabs; stab < stabs + stabsize; stab += 12)
167 unsigned int strx;
168 int type;
169 int other;
170 int desc;
171 bfd_vma value;
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);
181 if (type == 0)
183 /* Special type 0 stabs indicate the offset to the
184 next string table. */
185 stroff = next_stroff;
186 next_stroff += value;
188 else
190 char *f, *s;
192 f = NULL;
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);
199 continue;
202 s = (char *) strings + stroff + strx;
204 while (s[strlen (s) - 1] == '\\'
205 && stab + 12 < stabs + stabsize)
207 char *p;
209 stab += 12;
210 p = s + strlen (s) - 1;
211 *p = '\0';
212 s = concat (s,
213 ((char *) strings
214 + stroff
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. */
221 *p = '\\';
223 if (f != NULL)
224 free (f);
225 f = s;
228 save_stab (type, desc, value, s);
230 if (! parse_stab (dhandle, shandle, type, desc, value, s))
232 stab_context ();
233 free_saved_stabs ();
234 return FALSE;
237 /* Don't free f, since I think the stabs code
238 expects strings to hang around. This should be
239 straightened out. FIXME. */
243 free_saved_stabs ();
244 free (stabs);
246 /* Don't free strings, since I think the stabs code expects
247 the strings to hang around. This should be straightened
248 out. FIXME. */
252 if (shandle != NULL)
254 if (! finish_stab (dhandle, shandle))
255 return FALSE;
258 return TRUE;
261 /* Read stabs in the symbol table. */
263 static bfd_boolean
264 read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound)
265 bfd *abfd;
266 asymbol **syms;
267 long symcount;
268 PTR dhandle;
269 bfd_boolean *pfound;
271 PTR shandle;
272 asymbol **ps, **symend;
274 shandle = NULL;
275 symend = syms + symcount;
276 for (ps = syms; ps < symend; ps++)
278 symbol_info i;
280 bfd_get_symbol_info (abfd, *ps, &i);
282 if (i.type == '-')
284 const char *s;
285 char *f;
287 if (shandle == NULL)
289 shandle = start_stab (dhandle, abfd, FALSE, syms, symcount);
290 if (shandle == NULL)
291 return FALSE;
294 *pfound = TRUE;
296 s = i.name;
297 f = NULL;
298 while (s[strlen (s) - 1] == '\\'
299 && ps + 1 < symend)
301 char *sc, *n;
303 ++ps;
304 sc = xstrdup (s);
305 sc[strlen (sc) - 1] = '\0';
306 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
307 free (sc);
308 if (f != NULL)
309 free (f);
310 f = n;
311 s = n;
314 save_stab (i.stab_type, i.stab_desc, i.value, s);
316 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
317 i.value, s))
319 stab_context ();
320 free_saved_stabs ();
321 return FALSE;
324 /* Don't free f, since I think the stabs code expects
325 strings to hang around. This should be straightened out.
326 FIXME. */
330 free_saved_stabs ();
332 if (shandle != NULL)
334 if (! finish_stab (dhandle, shandle))
335 return FALSE;
338 return TRUE;
341 /* Read IEEE debugging information. */
343 static bfd_boolean
344 read_ieee_debugging_info (abfd, dhandle, pfound)
345 bfd *abfd;
346 PTR dhandle;
347 bfd_boolean *pfound;
349 asection *dsec;
350 bfd_size_type size;
351 bfd_byte *contents;
353 /* The BFD backend puts the debugging information into a section
354 named .debug. */
356 dsec = bfd_get_section_by_name (abfd, ".debug");
357 if (dsec == NULL)
358 return TRUE;
360 size = bfd_section_size (abfd, dsec);
361 contents = (bfd_byte *) xmalloc (size);
362 if (! bfd_get_section_contents (abfd, dsec, contents, 0, size))
363 return FALSE;
365 if (! parse_ieee (dhandle, abfd, contents, size))
366 return FALSE;
368 free (contents);
370 *pfound = TRUE;
372 return TRUE;
375 /* Record stabs strings, so that we can give some context for errors. */
377 #define SAVE_STABS_COUNT (16)
379 struct saved_stab
381 int type;
382 int desc;
383 bfd_vma value;
384 char *string;
387 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
388 static int saved_stabs_index;
390 /* Save a stabs string. */
392 static void
393 save_stab (type, desc, value, string)
394 int type;
395 int desc;
396 bfd_vma value;
397 const char *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. */
410 static void
411 stab_context ()
413 int i;
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)
426 const char *s;
428 s = bfd_get_stab_name (stabp->type);
429 if (s != NULL)
430 fprintf (stderr, "%-6s", s);
431 else if (stabp->type == 0)
432 fprintf (stderr, "HdrSym");
433 else
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. */
448 static void
449 free_saved_stabs ()
451 int i;
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;