1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright (C) 1995-2019 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 void save_stab (int, int, bfd_vma, const char *);
39 static void stab_context (void);
40 static void free_saved_stabs (void);
42 /* Read debugging information from a BFD. Returns a generic debugging
46 read_debugging_info (bfd *abfd, asymbol **syms, long symcount, bfd_boolean no_messages)
51 dhandle = debug_init ();
55 if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
59 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
61 if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle,
66 /* Try reading the COFF symbols if we didn't find any stabs in COFF
69 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
72 if (! parse_coff (abfd, syms, symcount, dhandle))
80 non_fatal (_("%s: no recognized debugging information"),
81 bfd_get_filename (abfd));
88 /* Read stabs in sections debugging information from a BFD. */
91 read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
92 void *dhandle, bfd_boolean *pfound)
97 const char *strsecname;
101 { ".stab", ".stabstr" },
102 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" },
103 { "$GDB_SYMBOLS$", "$GDB_STRINGS$" }
111 for (i = 0; i < sizeof names / sizeof names[0]; i++)
113 asection *sec, *strsec;
115 sec = bfd_get_section_by_name (abfd, names[i].secname);
116 strsec = bfd_get_section_by_name (abfd, names[i].strsecname);
117 if (sec != NULL && strsec != NULL)
119 bfd_size_type stabsize, strsize;
120 bfd_byte *stabs, *strings;
122 bfd_size_type stroff, next_stroff;
124 stabsize = bfd_section_size (abfd, sec);
125 stabs = (bfd_byte *) xmalloc (stabsize);
126 if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
128 fprintf (stderr, "%s: %s: %s\n",
129 bfd_get_filename (abfd), names[i].secname,
130 bfd_errmsg (bfd_get_error ()));
136 strsize = bfd_section_size (abfd, strsec);
137 strings = (bfd_byte *) xmalloc (strsize + 1);
138 if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
140 fprintf (stderr, "%s: %s: %s\n",
141 bfd_get_filename (abfd), names[i].strsecname,
142 bfd_errmsg (bfd_get_error ()));
148 /* Zero terminate the strings table, just in case. */
149 strings [strsize] = 0;
152 shandle = start_stab (dhandle, abfd, TRUE, syms, symcount);
165 /* PR 17512: file: 078-60391-0.001:0.1. */
166 for (stab = stabs; stab <= (stabs + stabsize) - 12; stab += 12)
170 int other ATTRIBUTE_UNUSED;
174 /* This code presumes 32 bit values. */
176 strx = bfd_get_32 (abfd, stab);
177 type = bfd_get_8 (abfd, stab + 4);
178 other = bfd_get_8 (abfd, stab + 5);
179 desc = bfd_get_16 (abfd, stab + 6);
180 value = bfd_get_32 (abfd, stab + 8);
184 /* Special type 0 stabs indicate the offset to the
185 next string table. */
186 stroff = next_stroff;
187 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;
205 /* PR 17512: file: 002-87578-0.001:0.1.
206 It is possible to craft a file where, without the 'strlen (s) > 0',
207 an attempt to read the byte before 'strings' would occur. */
208 while ((len = strlen (s)) > 0
209 && s[len - 1] == '\\'
210 && stab + 12 < stabs + stabsize)
217 strx = stroff + bfd_get_32 (abfd, stab);
220 fprintf (stderr, _("%s: %s: stab entry %ld is corrupt\n"),
221 bfd_get_filename (abfd), names[i].secname,
222 (long) (stab - stabs) / 12);
226 s = concat (s, (char *) strings + strx,
227 (const char *) NULL);
229 /* We have to restore the backslash, because, if
230 the linker is hashing stabs strings, we may
231 see the same string more than once. */
238 save_stab (type, desc, value, s);
240 if (! parse_stab (dhandle, shandle, type, desc, value, s))
251 /* Don't free f, since I think the stabs code
252 expects strings to hang around. This should be
253 straightened out. FIXME. */
260 /* Don't free strings, since I think the stabs code expects
261 the strings to hang around. This should be straightened
268 if (! finish_stab (dhandle, shandle))
275 /* Read stabs in the symbol table. */
278 read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
279 void *dhandle, bfd_boolean *pfound)
282 asymbol **ps, **symend;
285 symend = syms + symcount;
286 for (ps = syms; ps < symend; ps++)
290 bfd_get_symbol_info (abfd, *ps, &i);
299 shandle = start_stab (dhandle, abfd, FALSE, syms, symcount);
307 if (s == NULL || strlen (s) < 1)
311 while (strlen (s) > 0
312 && s[strlen (s) - 1] == '\\'
319 sc[strlen (sc) - 1] = '\0';
320 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
328 save_stab (i.stab_type, i.stab_desc, i.value, s);
330 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
338 /* Don't free f, since I think the stabs code expects
339 strings to hang around. This should be straightened out.
348 if (! finish_stab (dhandle, shandle))
355 /* Record stabs strings, so that we can give some context for errors. */
357 #define SAVE_STABS_COUNT (16)
367 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
368 static int saved_stabs_index;
370 /* Save a stabs string. */
373 save_stab (int type, int desc, bfd_vma value, const char *string)
375 if (saved_stabs[saved_stabs_index].string != NULL)
376 free (saved_stabs[saved_stabs_index].string);
377 saved_stabs[saved_stabs_index].type = type;
378 saved_stabs[saved_stabs_index].desc = desc;
379 saved_stabs[saved_stabs_index].value = value;
380 saved_stabs[saved_stabs_index].string = xstrdup (string);
381 saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
384 /* Provide context for an error. */
391 fprintf (stderr, _("Last stabs entries before error:\n"));
392 fprintf (stderr, "n_type n_desc n_value string\n");
394 i = saved_stabs_index;
397 struct saved_stab *stabp;
399 stabp = saved_stabs + i;
400 if (stabp->string != NULL)
404 s = bfd_get_stab_name (stabp->type);
406 fprintf (stderr, "%-6s", s);
407 else if (stabp->type == 0)
408 fprintf (stderr, "HdrSym");
410 fprintf (stderr, "%-6d", stabp->type);
411 fprintf (stderr, " %-6d ", stabp->desc);
412 fprintf_vma (stderr, stabp->value);
413 if (stabp->type != 0)
414 fprintf (stderr, " %s", stabp->string);
415 fprintf (stderr, "\n");
417 i = (i + 1) % SAVE_STABS_COUNT;
419 while (i != saved_stabs_index);
422 /* Free the saved stab strings. */
425 free_saved_stabs (void)
429 for (i = 0; i < SAVE_STABS_COUNT; i++)
431 if (saved_stabs[i].string != NULL)
433 free (saved_stabs[i].string);
434 saved_stabs[i].string = NULL;
438 saved_stabs_index = 0;