1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright (C) 1995-2024 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 bool read_section_stabs_debugging_info
35 (bfd
*, asymbol
**, long, void *, bool *);
36 static bool read_symbol_stabs_debugging_info
37 (bfd
*, asymbol
**, long, void *, bool *);
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
,
52 dhandle
= debug_init (abfd
);
56 if (!debug_set_filename (dhandle
, bfd_get_filename (abfd
)))
59 if (! read_section_stabs_debugging_info (abfd
, syms
, symcount
, dhandle
,
63 if (bfd_get_flavour (abfd
) == bfd_target_aout_flavour
)
65 if (! read_symbol_stabs_debugging_info (abfd
, syms
, symcount
, dhandle
,
70 /* Try reading the COFF symbols if we didn't find any stabs in COFF
73 && bfd_get_flavour (abfd
) == bfd_target_coff_flavour
76 if (! parse_coff (abfd
, syms
, symcount
, dhandle
))
84 non_fatal (_("%s: no recognized debugging information"),
85 bfd_get_filename (abfd
));
92 /* Read stabs in sections debugging information from a BFD. */
95 read_section_stabs_debugging_info (bfd
*abfd
, asymbol
**syms
, long symcount
,
96 void *dhandle
, bool *pfound
)
101 const char *strsecname
;
105 { ".stab", ".stabstr" },
106 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" },
107 { "$GDB_SYMBOLS$", "$GDB_STRINGS$" }
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
);
123 && (bfd_section_flags (sec
) & SEC_HAS_CONTENTS
) != 0
124 && bfd_section_size (sec
) >= 12
126 && (bfd_section_flags (strsec
) & SEC_HAS_CONTENTS
) != 0)
128 bfd_size_type stabsize
, strsize
;
129 bfd_byte
*stabs
, *strings
;
131 bfd_size_type stroff
, next_stroff
;
133 if (!bfd_malloc_and_get_section (abfd
, sec
, &stabs
))
135 fprintf (stderr
, "%s: %s: %s\n",
136 bfd_get_filename (abfd
), names
[i
].secname
,
137 bfd_errmsg (bfd_get_error ()));
141 if (!bfd_malloc_and_get_section (abfd
, strsec
, &strings
))
143 fprintf (stderr
, "%s: %s: %s\n",
144 bfd_get_filename (abfd
), names
[i
].strsecname
,
145 bfd_errmsg (bfd_get_error ()));
149 /* Zero terminate the strings table, just in case. */
150 strsize
= bfd_section_size (strsec
);
152 strings
[strsize
- 1] = 0;
155 shandle
= start_stab (dhandle
, abfd
, true, syms
, symcount
);
168 stabsize
= bfd_section_size (sec
);
169 /* PR 17512: file: 078-60391-0.001:0.1. */
170 for (stab
= stabs
; stab
<= (stabs
+ stabsize
) - 12; stab
+= 12)
174 int other ATTRIBUTE_UNUSED
;
178 /* This code presumes 32 bit values. */
180 strx
= bfd_get_32 (abfd
, stab
);
181 type
= bfd_get_8 (abfd
, stab
+ 4);
182 other
= bfd_get_8 (abfd
, stab
+ 5);
183 desc
= bfd_get_16 (abfd
, stab
+ 6);
184 value
= bfd_get_32 (abfd
, stab
+ 8);
188 /* Special type 0 stabs indicate the offset to the
189 next string table. */
190 stroff
= next_stroff
;
191 next_stroff
+= value
;
198 if (stroff
+ strx
>= strsize
)
200 fprintf (stderr
, _("%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n"),
201 bfd_get_filename (abfd
), names
[i
].secname
,
202 (long) (stab
- stabs
) / 12, strx
, type
);
206 s
= (char *) strings
+ stroff
+ strx
;
209 /* PR 17512: file: 002-87578-0.001:0.1.
210 It is possible to craft a file where, without the 'strlen (s) > 0',
211 an attempt to read the byte before 'strings' would occur. */
212 while ((len
= strlen (s
)) > 0
213 && s
[len
- 1] == '\\'
214 && stab
+ 16 <= stabs
+ stabsize
)
221 strx
= stroff
+ bfd_get_32 (abfd
, stab
);
224 fprintf (stderr
, _("%s: %s: stab entry %ld is corrupt\n"),
225 bfd_get_filename (abfd
), names
[i
].secname
,
226 (long) (stab
- stabs
) / 12);
230 s
= concat (s
, (char *) strings
+ strx
,
231 (const char *) NULL
);
233 /* We have to restore the backslash, because, if
234 the linker is hashing stabs strings, we may
235 see the same string more than once. */
242 save_stab (type
, desc
, value
, s
);
244 if (!parse_stab (dhandle
, shandle
, type
, desc
, value
, s
))
268 if (! finish_stab (dhandle
, shandle
, ret
))
275 /* Read stabs in the symbol table. */
278 read_symbol_stabs_debugging_info (bfd
*abfd
, asymbol
**syms
, long symcount
,
279 void *dhandle
, bool *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
);
327 save_stab (i
.stab_type
, i
.stab_desc
, i
.value
, s
);
329 if (!parse_stab (dhandle
, shandle
, i
.stab_type
, i
.stab_desc
,
340 bool ret
= ps
>= symend
;
346 if (! finish_stab (dhandle
, shandle
, ret
))
353 /* Record stabs strings, so that we can give some context for errors. */
355 #define SAVE_STABS_COUNT (16)
365 static struct saved_stab saved_stabs
[SAVE_STABS_COUNT
];
366 static int saved_stabs_index
;
368 /* Save a stabs string. */
371 save_stab (int type
, int desc
, bfd_vma value
, const char *string
)
373 free (saved_stabs
[saved_stabs_index
].string
);
374 saved_stabs
[saved_stabs_index
].type
= type
;
375 saved_stabs
[saved_stabs_index
].desc
= desc
;
376 saved_stabs
[saved_stabs_index
].value
= value
;
377 saved_stabs
[saved_stabs_index
].string
= xstrdup (string
);
378 saved_stabs_index
= (saved_stabs_index
+ 1) % SAVE_STABS_COUNT
;
381 /* Provide context for an error. */
388 fprintf (stderr
, _("Last stabs entries before error:\n"));
389 fprintf (stderr
, "n_type n_desc n_value string\n");
391 i
= saved_stabs_index
;
394 struct saved_stab
*stabp
;
396 stabp
= saved_stabs
+ i
;
397 if (stabp
->string
!= NULL
)
401 s
= bfd_get_stab_name (stabp
->type
);
403 fprintf (stderr
, "%-6s", s
);
404 else if (stabp
->type
== 0)
405 fprintf (stderr
, "HdrSym");
407 fprintf (stderr
, "%-6d", stabp
->type
);
408 fprintf (stderr
, " %-6d ", stabp
->desc
);
409 fprintf (stderr
, "%08" PRIx64
, (uint64_t) stabp
->value
);
410 if (stabp
->type
!= 0)
411 fprintf (stderr
, " %s", stabp
->string
);
412 fprintf (stderr
, "\n");
414 i
= (i
+ 1) % SAVE_STABS_COUNT
;
416 while (i
!= saved_stabs_index
);
419 /* Free the saved stab strings. */
422 free_saved_stabs (void)
426 for (i
= 0; i
< SAVE_STABS_COUNT
; i
++)
428 free (saved_stabs
[i
].string
);
429 saved_stabs
[i
].string
= NULL
;
432 saved_stabs_index
= 0;