1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright 1995, 1996, 1997, 2000, 2002, 2003, 2005, 2007
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"
34 static bfd_boolean read_section_stabs_debugging_info
35 (bfd
*, asymbol
**, long, void *, bfd_boolean
*);
36 static bfd_boolean read_symbol_stabs_debugging_info
37 (bfd
*, asymbol
**, long, void *, bfd_boolean
*);
38 static bfd_boolean
read_ieee_debugging_info (bfd
*, void *, bfd_boolean
*);
39 static void save_stab (int, int, bfd_vma
, const char *);
40 static void stab_context (void);
41 static void free_saved_stabs (void);
43 /* Read debugging information from a BFD. Returns a generic debugging
47 read_debugging_info (bfd
*abfd
, asymbol
**syms
, long symcount
)
52 dhandle
= debug_init ();
56 if (! read_section_stabs_debugging_info (abfd
, syms
, symcount
, dhandle
,
60 if (bfd_get_flavour (abfd
) == bfd_target_aout_flavour
)
62 if (! read_symbol_stabs_debugging_info (abfd
, syms
, symcount
, dhandle
,
67 if (bfd_get_flavour (abfd
) == bfd_target_ieee_flavour
)
69 if (! read_ieee_debugging_info (abfd
, dhandle
, &found
))
73 /* Try reading the COFF symbols if we didn't find any stabs in COFF
76 && bfd_get_flavour (abfd
) == bfd_target_coff_flavour
79 if (! parse_coff (abfd
, syms
, symcount
, dhandle
))
86 non_fatal (_("%s: no recognized debugging information"),
87 bfd_get_filename (abfd
));
94 /* Read stabs in sections debugging information from a BFD. */
97 read_section_stabs_debugging_info (bfd
*abfd
, asymbol
**syms
, long symcount
,
98 void *dhandle
, bfd_boolean
*pfound
)
103 const char *strsecname
;
107 { ".stab", ".stabstr" },
108 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" },
109 { "$GDB_SYMBOLS$", "$GDB_STRINGS$" }
117 for (i
= 0; i
< sizeof names
/ sizeof names
[0]; i
++)
119 asection
*sec
, *strsec
;
121 sec
= bfd_get_section_by_name (abfd
, names
[i
].secname
);
122 strsec
= bfd_get_section_by_name (abfd
, names
[i
].strsecname
);
123 if (sec
!= NULL
&& strsec
!= NULL
)
125 bfd_size_type stabsize
, strsize
;
126 bfd_byte
*stabs
, *strings
;
128 bfd_size_type stroff
, next_stroff
;
130 stabsize
= bfd_section_size (abfd
, sec
);
131 stabs
= (bfd_byte
*) xmalloc (stabsize
);
132 if (! bfd_get_section_contents (abfd
, sec
, stabs
, 0, stabsize
))
134 fprintf (stderr
, "%s: %s: %s\n",
135 bfd_get_filename (abfd
), names
[i
].secname
,
136 bfd_errmsg (bfd_get_error ()));
140 strsize
= bfd_section_size (abfd
, strsec
);
141 strings
= (bfd_byte
*) xmalloc (strsize
);
142 if (! bfd_get_section_contents (abfd
, strsec
, strings
, 0, strsize
))
144 fprintf (stderr
, "%s: %s: %s\n",
145 bfd_get_filename (abfd
), names
[i
].strsecname
,
146 bfd_errmsg (bfd_get_error ()));
152 shandle
= start_stab (dhandle
, abfd
, TRUE
, syms
, symcount
);
161 for (stab
= stabs
; stab
< stabs
+ stabsize
; stab
+= 12)
169 /* This code presumes 32 bit values. */
171 strx
= bfd_get_32 (abfd
, stab
);
172 type
= bfd_get_8 (abfd
, stab
+ 4);
173 other
= bfd_get_8 (abfd
, stab
+ 5);
174 desc
= bfd_get_16 (abfd
, stab
+ 6);
175 value
= bfd_get_32 (abfd
, stab
+ 8);
179 /* Special type 0 stabs indicate the offset to the
180 next string table. */
181 stroff
= next_stroff
;
182 next_stroff
+= value
;
190 if (stroff
+ strx
> strsize
)
192 fprintf (stderr
, "%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n",
193 bfd_get_filename (abfd
), names
[i
].secname
,
194 (long) (stab
- stabs
) / 12, strx
, type
);
198 s
= (char *) strings
+ stroff
+ strx
;
200 while (s
[strlen (s
) - 1] == '\\'
201 && stab
+ 12 < stabs
+ stabsize
)
206 p
= s
+ strlen (s
) - 1;
211 + bfd_get_32 (abfd
, stab
)),
212 (const char *) NULL
);
214 /* We have to restore the backslash, because, if
215 the linker is hashing stabs strings, we may
216 see the same string more than once. */
224 save_stab (type
, desc
, value
, s
);
226 if (! parse_stab (dhandle
, shandle
, type
, desc
, value
, s
))
233 /* Don't free f, since I think the stabs code
234 expects strings to hang around. This should be
235 straightened out. FIXME. */
242 /* Don't free strings, since I think the stabs code expects
243 the strings to hang around. This should be straightened
250 if (! finish_stab (dhandle
, shandle
))
257 /* Read stabs in the symbol table. */
260 read_symbol_stabs_debugging_info (bfd
*abfd
, asymbol
**syms
, long symcount
,
261 void *dhandle
, bfd_boolean
*pfound
)
264 asymbol
**ps
, **symend
;
267 symend
= syms
+ symcount
;
268 for (ps
= syms
; ps
< symend
; ps
++)
272 bfd_get_symbol_info (abfd
, *ps
, &i
);
281 shandle
= start_stab (dhandle
, abfd
, FALSE
, syms
, symcount
);
290 while (s
[strlen (s
) - 1] == '\\'
297 sc
[strlen (sc
) - 1] = '\0';
298 n
= concat (sc
, bfd_asymbol_name (*ps
), (const char *) NULL
);
306 save_stab (i
.stab_type
, i
.stab_desc
, i
.value
, s
);
308 if (! parse_stab (dhandle
, shandle
, i
.stab_type
, i
.stab_desc
,
316 /* Don't free f, since I think the stabs code expects
317 strings to hang around. This should be straightened out.
326 if (! finish_stab (dhandle
, shandle
))
333 /* Read IEEE debugging information. */
336 read_ieee_debugging_info (bfd
*abfd
, void *dhandle
, bfd_boolean
*pfound
)
342 /* The BFD backend puts the debugging information into a section
345 dsec
= bfd_get_section_by_name (abfd
, ".debug");
349 size
= bfd_section_size (abfd
, dsec
);
350 contents
= (bfd_byte
*) xmalloc (size
);
351 if (! bfd_get_section_contents (abfd
, dsec
, contents
, 0, size
))
354 if (! parse_ieee (dhandle
, abfd
, contents
, size
))
364 /* Record stabs strings, so that we can give some context for errors. */
366 #define SAVE_STABS_COUNT (16)
376 static struct saved_stab saved_stabs
[SAVE_STABS_COUNT
];
377 static int saved_stabs_index
;
379 /* Save a stabs string. */
382 save_stab (int type
, int desc
, bfd_vma value
, 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. */
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
)
413 s
= bfd_get_stab_name (stabp
->type
);
415 fprintf (stderr
, "%-6s", s
);
416 else if (stabp
->type
== 0)
417 fprintf (stderr
, "HdrSym");
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. */
434 free_saved_stabs (void)
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;