Sanity check read_section_stabs_debugging_info
[binutils-gdb.git] / binutils / rddbg.c
1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
4
5 This file is part of GNU Binutils.
6
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.
11
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.
16
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
20 02110-1301, USA. */
21
22
23 /* This file reads debugging information into a generic form. This
24 file knows how to dig the debugging information out of an object
25 file. */
26
27 #include "sysdep.h"
28 #include "bfd.h"
29 #include "libiberty.h"
30 #include "bucomm.h"
31 #include "debug.h"
32 #include "budbg.h"
33
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);
41
42 /* Read debugging information from a BFD. Returns a generic debugging
43 pointer. */
44
45 void *
46 read_debugging_info (bfd *abfd, asymbol **syms, long symcount,
47 bool no_messages)
48 {
49 void *dhandle;
50 bool found;
51
52 dhandle = debug_init ();
53 if (dhandle == NULL)
54 return NULL;
55
56 if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
57 &found))
58 goto err_exit;
59
60 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
61 {
62 if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle,
63 &found))
64 goto err_exit;
65 }
66
67 /* Try reading the COFF symbols if we didn't find any stabs in COFF
68 sections. */
69 if (! found
70 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
71 && symcount > 0)
72 {
73 if (! parse_coff (abfd, syms, symcount, dhandle))
74 goto err_exit;
75 found = true;
76 }
77
78 if (! found)
79 {
80 if (! no_messages)
81 non_fatal (_("%s: no recognized debugging information"),
82 bfd_get_filename (abfd));
83 err_exit:
84 free (dhandle);
85 return NULL;
86 }
87
88 return dhandle;
89 }
90
91 /* Read stabs in sections debugging information from a BFD. */
92
93 static bool
94 read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
95 void *dhandle, bool *pfound)
96 {
97 static struct
98 {
99 const char *secname;
100 const char *strsecname;
101 }
102 names[] =
103 {
104 { ".stab", ".stabstr" },
105 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" },
106 { "$GDB_SYMBOLS$", "$GDB_STRINGS$" }
107 };
108 unsigned int i;
109 void *shandle;
110
111 *pfound = false;
112 shandle = NULL;
113
114 for (i = 0; i < sizeof names / sizeof names[0]; i++)
115 {
116 asection *sec, *strsec;
117
118 sec = bfd_get_section_by_name (abfd, names[i].secname);
119 strsec = bfd_get_section_by_name (abfd, names[i].strsecname);
120 if (sec != NULL
121 && (bfd_section_flags (sec) & SEC_HAS_CONTENTS) != 0
122 && strsec != NULL
123 && (bfd_section_flags (strsec) & SEC_HAS_CONTENTS) != 0)
124 {
125 bfd_size_type stabsize, strsize;
126 bfd_byte *stabs, *strings;
127 bfd_byte *stab;
128 bfd_size_type stroff, next_stroff;
129
130 if (!bfd_malloc_and_get_section (abfd, sec, &stabs))
131 {
132 fprintf (stderr, "%s: %s: %s\n",
133 bfd_get_filename (abfd), names[i].secname,
134 bfd_errmsg (bfd_get_error ()));
135 free (shandle);
136 return false;
137 }
138
139 if (!bfd_malloc_and_get_section (abfd, strsec, &strings))
140 {
141 fprintf (stderr, "%s: %s: %s\n",
142 bfd_get_filename (abfd), names[i].strsecname,
143 bfd_errmsg (bfd_get_error ()));
144 free (shandle);
145 free (stabs);
146 return false;
147 }
148 /* Zero terminate the strings table, just in case. */
149 strsize = bfd_section_size (strsec);
150 strings [strsize - 1] = 0;
151 if (shandle == NULL)
152 {
153 shandle = start_stab (dhandle, abfd, true, syms, symcount);
154 if (shandle == NULL)
155 {
156 free (strings);
157 free (stabs);
158 return false;
159 }
160 }
161
162 *pfound = true;
163
164 stroff = 0;
165 next_stroff = 0;
166 stabsize = bfd_section_size (sec);
167 /* PR 17512: file: 078-60391-0.001:0.1. */
168 for (stab = stabs; stab <= (stabs + stabsize) - 12; stab += 12)
169 {
170 unsigned int strx;
171 int type;
172 int other ATTRIBUTE_UNUSED;
173 int desc;
174 bfd_vma value;
175
176 /* This code presumes 32 bit values. */
177
178 strx = bfd_get_32 (abfd, stab);
179 type = bfd_get_8 (abfd, stab + 4);
180 other = bfd_get_8 (abfd, stab + 5);
181 desc = bfd_get_16 (abfd, stab + 6);
182 value = bfd_get_32 (abfd, stab + 8);
183
184 if (type == 0)
185 {
186 /* Special type 0 stabs indicate the offset to the
187 next string table. */
188 stroff = next_stroff;
189 next_stroff += value;
190 }
191 else
192 {
193 size_t len;
194 char *f, *s;
195
196 if (stroff + strx >= strsize)
197 {
198 fprintf (stderr, _("%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n"),
199 bfd_get_filename (abfd), names[i].secname,
200 (long) (stab - stabs) / 12, strx, type);
201 continue;
202 }
203
204 s = (char *) strings + stroff + strx;
205 f = NULL;
206
207 /* PR 17512: file: 002-87578-0.001:0.1.
208 It is possible to craft a file where, without the 'strlen (s) > 0',
209 an attempt to read the byte before 'strings' would occur. */
210 while ((len = strlen (s)) > 0
211 && s[len - 1] == '\\'
212 && stab + 16 <= stabs + stabsize)
213 {
214 char *p;
215
216 stab += 12;
217 p = s + len - 1;
218 *p = '\0';
219 strx = stroff + bfd_get_32 (abfd, stab);
220 if (strx >= strsize)
221 {
222 fprintf (stderr, _("%s: %s: stab entry %ld is corrupt\n"),
223 bfd_get_filename (abfd), names[i].secname,
224 (long) (stab - stabs) / 12);
225 break;
226 }
227
228 s = concat (s, (char *) strings + strx,
229 (const char *) NULL);
230
231 /* We have to restore the backslash, because, if
232 the linker is hashing stabs strings, we may
233 see the same string more than once. */
234 *p = '\\';
235
236 free (f);
237 f = s;
238 }
239
240 save_stab (type, desc, value, s);
241
242 if (! parse_stab (dhandle, shandle, type, desc, value, s))
243 {
244 stab_context ();
245 free_saved_stabs ();
246 free (f);
247 free (shandle);
248 free (stabs);
249 free (strings);
250 return false;
251 }
252
253 /* Don't free f, since I think the stabs code
254 expects strings to hang around. This should be
255 straightened out. FIXME. */
256 }
257 }
258
259 free_saved_stabs ();
260 free (stabs);
261
262 /* Don't free strings, since I think the stabs code expects
263 the strings to hang around. This should be straightened
264 out. FIXME. */
265 }
266 }
267
268 if (shandle != NULL)
269 {
270 if (! finish_stab (dhandle, shandle))
271 return false;
272 }
273
274 return true;
275 }
276
277 /* Read stabs in the symbol table. */
278
279 static bool
280 read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
281 void *dhandle, bool *pfound)
282 {
283 void *shandle;
284 asymbol **ps, **symend;
285
286 shandle = NULL;
287 symend = syms + symcount;
288 for (ps = syms; ps < symend; ps++)
289 {
290 symbol_info i;
291
292 bfd_get_symbol_info (abfd, *ps, &i);
293
294 if (i.type == '-')
295 {
296 const char *s;
297 char *f;
298
299 if (shandle == NULL)
300 {
301 shandle = start_stab (dhandle, abfd, false, syms, symcount);
302 if (shandle == NULL)
303 return false;
304 }
305
306 *pfound = true;
307
308 s = i.name;
309 if (s == NULL || strlen (s) < 1)
310 return false;
311 f = NULL;
312
313 while (strlen (s) > 0
314 && s[strlen (s) - 1] == '\\'
315 && ps + 1 < symend)
316 {
317 char *sc, *n;
318
319 ++ps;
320 sc = xstrdup (s);
321 sc[strlen (sc) - 1] = '\0';
322 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
323 free (sc);
324 free (f);
325 f = n;
326 s = n;
327 }
328
329 save_stab (i.stab_type, i.stab_desc, i.value, s);
330
331 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
332 i.value, s))
333 {
334 stab_context ();
335 free_saved_stabs ();
336 return false;
337 }
338
339 /* Don't free f, since I think the stabs code expects
340 strings to hang around. This should be straightened out.
341 FIXME. */
342 }
343 }
344
345 free_saved_stabs ();
346
347 if (shandle != NULL)
348 {
349 if (! finish_stab (dhandle, shandle))
350 return false;
351 }
352
353 return true;
354 }
355 \f
356 /* Record stabs strings, so that we can give some context for errors. */
357
358 #define SAVE_STABS_COUNT (16)
359
360 struct saved_stab
361 {
362 int type;
363 int desc;
364 bfd_vma value;
365 char *string;
366 };
367
368 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
369 static int saved_stabs_index;
370
371 /* Save a stabs string. */
372
373 static void
374 save_stab (int type, int desc, bfd_vma value, const char *string)
375 {
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;
382 }
383
384 /* Provide context for an error. */
385
386 static void
387 stab_context (void)
388 {
389 int i;
390
391 fprintf (stderr, _("Last stabs entries before error:\n"));
392 fprintf (stderr, "n_type n_desc n_value string\n");
393
394 i = saved_stabs_index;
395 do
396 {
397 struct saved_stab *stabp;
398
399 stabp = saved_stabs + i;
400 if (stabp->string != NULL)
401 {
402 const char *s;
403
404 s = bfd_get_stab_name (stabp->type);
405 if (s != NULL)
406 fprintf (stderr, "%-6s", s);
407 else if (stabp->type == 0)
408 fprintf (stderr, "HdrSym");
409 else
410 fprintf (stderr, "%-6d", stabp->type);
411 fprintf (stderr, " %-6d ", stabp->desc);
412 fprintf (stderr, "%08" PRIx64, (uint64_t) stabp->value);
413 if (stabp->type != 0)
414 fprintf (stderr, " %s", stabp->string);
415 fprintf (stderr, "\n");
416 }
417 i = (i + 1) % SAVE_STABS_COUNT;
418 }
419 while (i != saved_stabs_index);
420 }
421
422 /* Free the saved stab strings. */
423
424 static void
425 free_saved_stabs (void)
426 {
427 int i;
428
429 for (i = 0; i < SAVE_STABS_COUNT; i++)
430 {
431 free (saved_stabs[i].string);
432 saved_stabs[i].string = NULL;
433 }
434
435 saved_stabs_index = 0;
436 }