Initial revision
[binutils.git] / binutils / rddbg.c
blob9428c37ad5ce713bf1c644e1cf317d6721892fc0
1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright (C) 1995, 96, 1997, 1998 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 boolean read_section_stabs_debugging_info
33 PARAMS ((bfd *, asymbol **, long, PTR, boolean *));
34 static boolean read_symbol_stabs_debugging_info
35 PARAMS ((bfd *, asymbol **, long, PTR, boolean *));
36 static boolean read_ieee_debugging_info PARAMS ((bfd *, PTR, boolean *));
37 static void save_stab PARAMS ((int, int, bfd_vma, const char *));
38 static void stab_context PARAMS ((void));
39 static void free_saved_stabs PARAMS ((void));
41 /* Read debugging information from a BFD. Returns a generic debugging
42 pointer. */
44 PTR
45 read_debugging_info (abfd, syms, symcount)
46 bfd *abfd;
47 asymbol **syms;
48 long symcount;
50 PTR dhandle;
51 boolean found;
53 dhandle = debug_init ();
54 if (dhandle == NULL)
55 return NULL;
57 if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
58 &found))
59 return NULL;
61 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
63 if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle,
64 &found))
65 return NULL;
68 if (bfd_get_flavour (abfd) == bfd_target_ieee_flavour)
70 if (! read_ieee_debugging_info (abfd, dhandle, &found))
71 return NULL;
74 /* Try reading the COFF symbols if we didn't find any stabs in COFF
75 sections. */
76 if (! found
77 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
78 && symcount > 0)
80 if (! parse_coff (abfd, syms, symcount, dhandle))
81 return NULL;
82 found = true;
85 if (! found)
87 fprintf (stderr, _("%s: no recognized debugging information\n"),
88 bfd_get_filename (abfd));
89 return NULL;
92 return dhandle;
95 /* Read stabs in sections debugging information from a BFD. */
97 static boolean
98 read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound)
99 bfd *abfd;
100 asymbol **syms;
101 long symcount;
102 PTR dhandle;
103 boolean *pfound;
105 static struct
107 const char *secname;
108 const char *strsecname;
109 } names[] = { { ".stab", ".stabstr" } };
110 unsigned int i;
111 PTR shandle;
113 *pfound = false;
114 shandle = NULL;
116 for (i = 0; i < sizeof names / sizeof names[0]; i++)
118 asection *sec, *strsec;
120 sec = bfd_get_section_by_name (abfd, names[i].secname);
121 strsec = bfd_get_section_by_name (abfd, names[i].strsecname);
122 if (sec != NULL && strsec != NULL)
124 bfd_size_type stabsize, strsize;
125 bfd_byte *stabs, *strings;
126 bfd_byte *stab;
127 bfd_size_type stroff, next_stroff;
129 stabsize = bfd_section_size (abfd, sec);
130 stabs = (bfd_byte *) xmalloc (stabsize);
131 if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
133 fprintf (stderr, "%s: %s: %s\n",
134 bfd_get_filename (abfd), names[i].secname,
135 bfd_errmsg (bfd_get_error ()));
136 return false;
139 strsize = bfd_section_size (abfd, strsec);
140 strings = (bfd_byte *) xmalloc (strsize);
141 if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
143 fprintf (stderr, "%s: %s: %s\n",
144 bfd_get_filename (abfd), names[i].strsecname,
145 bfd_errmsg (bfd_get_error ()));
146 return false;
149 if (shandle == NULL)
151 shandle = start_stab (dhandle, abfd, true, syms, symcount);
152 if (shandle == NULL)
153 return false;
156 *pfound = true;
158 stroff = 0;
159 next_stroff = 0;
160 for (stab = stabs; stab < stabs + stabsize; stab += 12)
162 bfd_size_type strx;
163 int type;
164 int other;
165 int desc;
166 bfd_vma value;
168 /* This code presumes 32 bit values. */
170 strx = bfd_get_32 (abfd, stab);
171 type = bfd_get_8 (abfd, stab + 4);
172 other = bfd_get_8 (abfd, stab + 5);
173 desc = bfd_get_16 (abfd, stab + 6);
174 value = bfd_get_32 (abfd, stab + 8);
176 if (type == 0)
178 /* Special type 0 stabs indicate the offset to the
179 next string table. */
180 stroff = next_stroff;
181 next_stroff += value;
183 else
185 char *f, *s;
187 f = NULL;
188 s = (char *) strings + stroff + strx;
189 while (s[strlen (s) - 1] == '\\'
190 && stab + 12 < stabs + stabsize)
192 char *p;
194 stab += 12;
195 p = s + strlen (s) - 1;
196 *p = '\0';
197 s = concat (s,
198 ((char *) strings
199 + stroff
200 + bfd_get_32 (abfd, stab)),
201 (const char *) NULL);
203 /* We have to restore the backslash, because, if
204 the linker is hashing stabs strings, we may
205 see the same string more than once. */
206 *p = '\\';
208 if (f != NULL)
209 free (f);
210 f = s;
213 save_stab (type, desc, value, s);
215 if (! parse_stab (dhandle, shandle, type, desc, value, s))
217 stab_context ();
218 free_saved_stabs ();
219 return false;
222 /* Don't free f, since I think the stabs code
223 expects strings to hang around. This should be
224 straightened out. FIXME. */
228 free_saved_stabs ();
229 free (stabs);
231 /* Don't free strings, since I think the stabs code expects
232 the strings to hang around. This should be straightened
233 out. FIXME. */
237 if (shandle != NULL)
239 if (! finish_stab (dhandle, shandle))
240 return false;
243 return true;
246 /* Read stabs in the symbol table. */
248 static boolean
249 read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound)
250 bfd *abfd;
251 asymbol **syms;
252 long symcount;
253 PTR dhandle;
254 boolean *pfound;
256 PTR shandle;
257 asymbol **ps, **symend;
259 shandle = NULL;
260 symend = syms + symcount;
261 for (ps = syms; ps < symend; ps++)
263 symbol_info i;
265 bfd_get_symbol_info (abfd, *ps, &i);
267 if (i.type == '-')
269 const char *s;
270 char *f;
272 if (shandle == NULL)
274 shandle = start_stab (dhandle, abfd, false, syms, symcount);
275 if (shandle == NULL)
276 return false;
279 *pfound = true;
281 s = i.name;
282 f = NULL;
283 while (s[strlen (s) - 1] == '\\'
284 && ps + 1 < symend)
286 char *sc, *n;
288 ++ps;
289 sc = xstrdup (s);
290 sc[strlen (sc) - 1] = '\0';
291 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
292 free (sc);
293 if (f != NULL)
294 free (f);
295 f = n;
296 s = n;
299 save_stab (i.stab_type, i.stab_desc, i.value, s);
301 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
302 i.value, s))
304 stab_context ();
305 free_saved_stabs ();
306 return false;
309 /* Don't free f, since I think the stabs code expects
310 strings to hang around. This should be straightened out.
311 FIXME. */
315 free_saved_stabs ();
317 if (shandle != NULL)
319 if (! finish_stab (dhandle, shandle))
320 return false;
323 return true;
326 /* Read IEEE debugging information. */
328 static boolean
329 read_ieee_debugging_info (abfd, dhandle, pfound)
330 bfd *abfd;
331 PTR dhandle;
332 boolean *pfound;
334 asection *dsec;
335 bfd_size_type size;
336 bfd_byte *contents;
338 /* The BFD backend puts the debugging information into a section
339 named .debug. */
341 dsec = bfd_get_section_by_name (abfd, ".debug");
342 if (dsec == NULL)
343 return true;
345 size = bfd_section_size (abfd, dsec);
346 contents = (bfd_byte *) xmalloc (size);
347 if (! bfd_get_section_contents (abfd, dsec, contents, 0, size))
348 return false;
350 if (! parse_ieee (dhandle, abfd, contents, size))
351 return false;
353 free (contents);
355 *pfound = true;
357 return true;
360 /* Record stabs strings, so that we can give some context for errors. */
362 #define SAVE_STABS_COUNT (16)
364 struct saved_stab
366 int type;
367 int desc;
368 bfd_vma value;
369 char *string;
372 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
373 static int saved_stabs_index;
375 /* Save a stabs string. */
377 static void
378 save_stab (type, desc, value, string)
379 int type;
380 int desc;
381 bfd_vma value;
382 const char *string;
384 if (saved_stabs[saved_stabs_index].string != NULL)
385 free (saved_stabs[saved_stabs_index].string);
386 saved_stabs[saved_stabs_index].type = type;
387 saved_stabs[saved_stabs_index].desc = desc;
388 saved_stabs[saved_stabs_index].value = value;
389 saved_stabs[saved_stabs_index].string = xstrdup (string);
390 saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
393 /* Provide context for an error. */
395 static void
396 stab_context ()
398 int i;
400 fprintf (stderr, _("Last stabs entries before error:\n"));
401 fprintf (stderr, "n_type n_desc n_value string\n");
403 i = saved_stabs_index;
406 struct saved_stab *stabp;
408 stabp = saved_stabs + i;
409 if (stabp->string != NULL)
411 const char *s;
413 s = bfd_get_stab_name (stabp->type);
414 if (s != NULL)
415 fprintf (stderr, "%-6s", s);
416 else if (stabp->type == 0)
417 fprintf (stderr, "HdrSym");
418 else
419 fprintf (stderr, "%-6d", stabp->type);
420 fprintf (stderr, " %-6d ", stabp->desc);
421 fprintf_vma (stderr, stabp->value);
422 if (stabp->type != 0)
423 fprintf (stderr, " %s", stabp->string);
424 fprintf (stderr, "\n");
426 i = (i + 1) % SAVE_STABS_COUNT;
428 while (i != saved_stabs_index);
431 /* Free the saved stab strings. */
433 static void
434 free_saved_stabs ()
436 int i;
438 for (i = 0; i < SAVE_STABS_COUNT; i++)
440 if (saved_stabs[i].string != NULL)
442 free (saved_stabs[i].string);
443 saved_stabs[i].string = NULL;
447 saved_stabs_index = 0;