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