1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright (C) 1995-2014 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 3 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., 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
, bfd_boolean no_messages
)
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
))
87 non_fatal (_("%s: no recognized debugging information"),
88 bfd_get_filename (abfd
));
95 /* Read stabs in sections debugging information from a BFD. */
98 read_section_stabs_debugging_info (bfd
*abfd
, asymbol
**syms
, long symcount
,
99 void *dhandle
, bfd_boolean
*pfound
)
104 const char *strsecname
;
108 { ".stab", ".stabstr" },
109 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" },
110 { "$GDB_SYMBOLS$", "$GDB_STRINGS$" }
118 for (i
= 0; i
< sizeof names
/ sizeof names
[0]; i
++)
120 asection
*sec
, *strsec
;
122 sec
= bfd_get_section_by_name (abfd
, names
[i
].secname
);
123 strsec
= bfd_get_section_by_name (abfd
, names
[i
].strsecname
);
124 if (sec
!= NULL
&& strsec
!= NULL
)
126 bfd_size_type stabsize
, strsize
;
127 bfd_byte
*stabs
, *strings
;
129 bfd_size_type stroff
, next_stroff
;
131 stabsize
= bfd_section_size (abfd
, sec
);
132 stabs
= (bfd_byte
*) xmalloc (stabsize
);
133 if (! bfd_get_section_contents (abfd
, sec
, stabs
, 0, stabsize
))
135 fprintf (stderr
, "%s: %s: %s\n",
136 bfd_get_filename (abfd
), names
[i
].secname
,
137 bfd_errmsg (bfd_get_error ()));
141 strsize
= bfd_section_size (abfd
, strsec
);
142 strings
= (bfd_byte
*) xmalloc (strsize
+ 1);
143 if (! bfd_get_section_contents (abfd
, strsec
, strings
, 0, strsize
))
145 fprintf (stderr
, "%s: %s: %s\n",
146 bfd_get_filename (abfd
), names
[i
].strsecname
,
147 bfd_errmsg (bfd_get_error ()));
150 /* Zero terminate the strings table, just in case. */
151 strings
[strsize
] = 0;
154 shandle
= start_stab (dhandle
, abfd
, TRUE
, syms
, symcount
);
163 /* PR 17512: file: 078-60391-0.001:0.1. */
164 for (stab
= stabs
; stab
<= (stabs
+ stabsize
) - 12; stab
+= 12)
168 int other ATTRIBUTE_UNUSED
;
172 /* This code presumes 32 bit values. */
174 strx
= bfd_get_32 (abfd
, stab
);
175 type
= bfd_get_8 (abfd
, stab
+ 4);
176 other
= bfd_get_8 (abfd
, stab
+ 5);
177 desc
= bfd_get_16 (abfd
, stab
+ 6);
178 value
= bfd_get_32 (abfd
, stab
+ 8);
182 /* Special type 0 stabs indicate the offset to the
183 next string table. */
184 stroff
= next_stroff
;
185 next_stroff
+= value
;
192 if (stroff
+ strx
>= strsize
)
194 fprintf (stderr
, _("%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n"),
195 bfd_get_filename (abfd
), names
[i
].secname
,
196 (long) (stab
- stabs
) / 12, strx
, type
);
200 s
= (char *) strings
+ stroff
+ strx
;
203 /* PR 17512: file: 002-87578-0.001:0.1.
204 It is possible to craft a file where, without the 'strlen (s) > 0',
205 an attempt to read the byte before 'strings' would occur. */
206 while ((len
= strlen (s
)) > 0
207 && s
[len
- 1] == '\\'
208 && stab
+ 12 < stabs
+ stabsize
)
215 strx
= stroff
+ bfd_get_32 (abfd
, stab
);
218 fprintf (stderr
, _("%s: %s: stab entry %ld is corrupt\n"),
219 bfd_get_filename (abfd
), names
[i
].secname
,
220 (long) (stab
- stabs
) / 12);
224 s
= concat (s
, (char *) strings
+ strx
,
225 (const char *) NULL
);
227 /* We have to restore the backslash, because, if
228 the linker is hashing stabs strings, we may
229 see the same string more than once. */
237 save_stab (type
, desc
, value
, s
);
239 if (! parse_stab (dhandle
, shandle
, type
, desc
, value
, s
))
246 /* Don't free f, since I think the stabs code
247 expects strings to hang around. This should be
248 straightened out. FIXME. */
255 /* Don't free strings, since I think the stabs code expects
256 the strings to hang around. This should be straightened
263 if (! finish_stab (dhandle
, shandle
))
270 /* Read stabs in the symbol table. */
273 read_symbol_stabs_debugging_info (bfd
*abfd
, asymbol
**syms
, long symcount
,
274 void *dhandle
, bfd_boolean
*pfound
)
277 asymbol
**ps
, **symend
;
280 symend
= syms
+ symcount
;
281 for (ps
= syms
; ps
< symend
; ps
++)
285 bfd_get_symbol_info (abfd
, *ps
, &i
);
294 shandle
= start_stab (dhandle
, abfd
, FALSE
, syms
, symcount
);
303 while (s
[strlen (s
) - 1] == '\\'
310 sc
[strlen (sc
) - 1] = '\0';
311 n
= concat (sc
, bfd_asymbol_name (*ps
), (const char *) NULL
);
319 save_stab (i
.stab_type
, i
.stab_desc
, i
.value
, s
);
321 if (! parse_stab (dhandle
, shandle
, i
.stab_type
, i
.stab_desc
,
329 /* Don't free f, since I think the stabs code expects
330 strings to hang around. This should be straightened out.
339 if (! finish_stab (dhandle
, shandle
))
346 /* Read IEEE debugging information. */
349 read_ieee_debugging_info (bfd
*abfd
, void *dhandle
, bfd_boolean
*pfound
)
355 /* The BFD backend puts the debugging information into a section
358 dsec
= bfd_get_section_by_name (abfd
, ".debug");
362 size
= bfd_section_size (abfd
, dsec
);
363 contents
= (bfd_byte
*) xmalloc (size
);
364 if (! bfd_get_section_contents (abfd
, dsec
, contents
, 0, size
))
367 if (! parse_ieee (dhandle
, abfd
, contents
, size
))
377 /* Record stabs strings, so that we can give some context for errors. */
379 #define SAVE_STABS_COUNT (16)
389 static struct saved_stab saved_stabs
[SAVE_STABS_COUNT
];
390 static int saved_stabs_index
;
392 /* Save a stabs string. */
395 save_stab (int type
, int desc
, bfd_vma value
, const char *string
)
397 if (saved_stabs
[saved_stabs_index
].string
!= NULL
)
398 free (saved_stabs
[saved_stabs_index
].string
);
399 saved_stabs
[saved_stabs_index
].type
= type
;
400 saved_stabs
[saved_stabs_index
].desc
= desc
;
401 saved_stabs
[saved_stabs_index
].value
= value
;
402 saved_stabs
[saved_stabs_index
].string
= xstrdup (string
);
403 saved_stabs_index
= (saved_stabs_index
+ 1) % SAVE_STABS_COUNT
;
406 /* Provide context for an error. */
413 fprintf (stderr
, _("Last stabs entries before error:\n"));
414 fprintf (stderr
, "n_type n_desc n_value string\n");
416 i
= saved_stabs_index
;
419 struct saved_stab
*stabp
;
421 stabp
= saved_stabs
+ i
;
422 if (stabp
->string
!= NULL
)
426 s
= bfd_get_stab_name (stabp
->type
);
428 fprintf (stderr
, "%-6s", s
);
429 else if (stabp
->type
== 0)
430 fprintf (stderr
, "HdrSym");
432 fprintf (stderr
, "%-6d", stabp
->type
);
433 fprintf (stderr
, " %-6d ", stabp
->desc
);
434 fprintf_vma (stderr
, stabp
->value
);
435 if (stabp
->type
!= 0)
436 fprintf (stderr
, " %s", stabp
->string
);
437 fprintf (stderr
, "\n");
439 i
= (i
+ 1) % SAVE_STABS_COUNT
;
441 while (i
!= saved_stabs_index
);
444 /* Free the saved stab strings. */
447 free_saved_stabs (void)
451 for (i
= 0; i
< SAVE_STABS_COUNT
; i
++)
453 if (saved_stabs
[i
].string
!= NULL
)
455 free (saved_stabs
[i
].string
);
456 saved_stabs
[i
].string
= NULL
;
460 saved_stabs_index
= 0;