]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* rddbg.c -- Read debugging information into a generic form. |
82704155 | 2 | Copyright (C) 1995-2019 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Ian Lance Taylor <[email protected]>. |
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 | |
32866df7 | 9 | the Free Software Foundation; either version 3 of the License, or |
252b5132 RH |
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 | |
b43b5d5f NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA |
20 | 02110-1301, USA. */ | |
252b5132 | 21 | |
32866df7 | 22 | |
252b5132 RH |
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 | ||
3db64b00 | 27 | #include "sysdep.h" |
252b5132 | 28 | #include "bfd.h" |
252b5132 | 29 | #include "libiberty.h" |
3db64b00 | 30 | #include "bucomm.h" |
252b5132 RH |
31 | #include "debug.h" |
32 | #include "budbg.h" | |
33 | ||
b34976b6 | 34 | static bfd_boolean read_section_stabs_debugging_info |
2da42df6 | 35 | (bfd *, asymbol **, long, void *, bfd_boolean *); |
b34976b6 | 36 | static bfd_boolean read_symbol_stabs_debugging_info |
2da42df6 | 37 | (bfd *, asymbol **, long, void *, bfd_boolean *); |
2da42df6 AJ |
38 | static void save_stab (int, int, bfd_vma, const char *); |
39 | static void stab_context (void); | |
40 | static void free_saved_stabs (void); | |
252b5132 RH |
41 | |
42 | /* Read debugging information from a BFD. Returns a generic debugging | |
43 | pointer. */ | |
44 | ||
2da42df6 | 45 | void * |
b922d590 | 46 | read_debugging_info (bfd *abfd, asymbol **syms, long symcount, bfd_boolean no_messages) |
252b5132 | 47 | { |
2da42df6 | 48 | void *dhandle; |
b34976b6 | 49 | bfd_boolean found; |
252b5132 RH |
50 | |
51 | dhandle = debug_init (); | |
52 | if (dhandle == NULL) | |
53 | return NULL; | |
54 | ||
55 | if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, | |
56 | &found)) | |
57 | return NULL; | |
58 | ||
59 | if (bfd_get_flavour (abfd) == bfd_target_aout_flavour) | |
60 | { | |
61 | if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, | |
62 | &found)) | |
63 | return NULL; | |
64 | } | |
65 | ||
252b5132 RH |
66 | /* Try reading the COFF symbols if we didn't find any stabs in COFF |
67 | sections. */ | |
68 | if (! found | |
69 | && bfd_get_flavour (abfd) == bfd_target_coff_flavour | |
70 | && symcount > 0) | |
71 | { | |
72 | if (! parse_coff (abfd, syms, symcount, dhandle)) | |
73 | return NULL; | |
b34976b6 | 74 | found = TRUE; |
252b5132 RH |
75 | } |
76 | ||
77 | if (! found) | |
78 | { | |
b922d590 NC |
79 | if (! no_messages) |
80 | non_fatal (_("%s: no recognized debugging information"), | |
81 | bfd_get_filename (abfd)); | |
252b5132 RH |
82 | return NULL; |
83 | } | |
84 | ||
85 | return dhandle; | |
86 | } | |
87 | ||
88 | /* Read stabs in sections debugging information from a BFD. */ | |
89 | ||
b34976b6 | 90 | static bfd_boolean |
2da42df6 AJ |
91 | read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, |
92 | void *dhandle, bfd_boolean *pfound) | |
252b5132 RH |
93 | { |
94 | static struct | |
95 | { | |
96 | const char *secname; | |
97 | const char *strsecname; | |
7806762e NC |
98 | } |
99 | names[] = | |
100 | { | |
101 | { ".stab", ".stabstr" }, | |
102 | { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" }, | |
103 | { "$GDB_SYMBOLS$", "$GDB_STRINGS$" } | |
104 | }; | |
252b5132 | 105 | unsigned int i; |
2da42df6 | 106 | void *shandle; |
252b5132 | 107 | |
b34976b6 | 108 | *pfound = FALSE; |
252b5132 RH |
109 | shandle = NULL; |
110 | ||
111 | for (i = 0; i < sizeof names / sizeof names[0]; i++) | |
112 | { | |
113 | asection *sec, *strsec; | |
114 | ||
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) | |
118 | { | |
119 | bfd_size_type stabsize, strsize; | |
120 | bfd_byte *stabs, *strings; | |
121 | bfd_byte *stab; | |
122 | bfd_size_type stroff, next_stroff; | |
123 | ||
fd361982 | 124 | stabsize = bfd_section_size (sec); |
252b5132 RH |
125 | stabs = (bfd_byte *) xmalloc (stabsize); |
126 | if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize)) | |
127 | { | |
128 | fprintf (stderr, "%s: %s: %s\n", | |
129 | bfd_get_filename (abfd), names[i].secname, | |
130 | bfd_errmsg (bfd_get_error ())); | |
e3d39609 NC |
131 | free (shandle); |
132 | free (stabs); | |
b34976b6 | 133 | return FALSE; |
252b5132 RH |
134 | } |
135 | ||
fd361982 | 136 | strsize = bfd_section_size (strsec); |
f41e4712 | 137 | strings = (bfd_byte *) xmalloc (strsize + 1); |
252b5132 RH |
138 | if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize)) |
139 | { | |
140 | fprintf (stderr, "%s: %s: %s\n", | |
141 | bfd_get_filename (abfd), names[i].strsecname, | |
142 | bfd_errmsg (bfd_get_error ())); | |
e3d39609 NC |
143 | free (shandle); |
144 | free (strings); | |
145 | free (stabs); | |
b34976b6 | 146 | return FALSE; |
252b5132 | 147 | } |
f41e4712 NC |
148 | /* Zero terminate the strings table, just in case. */ |
149 | strings [strsize] = 0; | |
252b5132 RH |
150 | if (shandle == NULL) |
151 | { | |
b34976b6 | 152 | shandle = start_stab (dhandle, abfd, TRUE, syms, symcount); |
252b5132 | 153 | if (shandle == NULL) |
e3d39609 NC |
154 | { |
155 | free (strings); | |
156 | free (stabs); | |
157 | return FALSE; | |
158 | } | |
252b5132 RH |
159 | } |
160 | ||
b34976b6 | 161 | *pfound = TRUE; |
252b5132 RH |
162 | |
163 | stroff = 0; | |
164 | next_stroff = 0; | |
f41e4712 NC |
165 | /* PR 17512: file: 078-60391-0.001:0.1. */ |
166 | for (stab = stabs; stab <= (stabs + stabsize) - 12; stab += 12) | |
252b5132 | 167 | { |
37cc8ec1 | 168 | unsigned int strx; |
252b5132 | 169 | int type; |
3d540e93 | 170 | int other ATTRIBUTE_UNUSED; |
252b5132 RH |
171 | int desc; |
172 | bfd_vma value; | |
173 | ||
174 | /* This code presumes 32 bit values. */ | |
175 | ||
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); | |
181 | ||
182 | if (type == 0) | |
183 | { | |
184 | /* Special type 0 stabs indicate the offset to the | |
f3931575 | 185 | next string table. */ |
252b5132 RH |
186 | stroff = next_stroff; |
187 | next_stroff += value; | |
188 | } | |
189 | else | |
190 | { | |
f41e4712 | 191 | size_t len; |
252b5132 RH |
192 | char *f, *s; |
193 | ||
f41e4712 | 194 | if (stroff + strx >= strsize) |
3b7aaf81 | 195 | { |
f41e4712 | 196 | fprintf (stderr, _("%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n"), |
3b7aaf81 | 197 | bfd_get_filename (abfd), names[i].secname, |
22d82235 | 198 | (long) (stab - stabs) / 12, strx, type); |
3b7aaf81 NC |
199 | continue; |
200 | } | |
53c7db4b | 201 | |
252b5132 | 202 | s = (char *) strings + stroff + strx; |
f41e4712 | 203 | f = NULL; |
53c7db4b | 204 | |
f41e4712 NC |
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] == '\\' | |
252b5132 RH |
210 | && stab + 12 < stabs + stabsize) |
211 | { | |
212 | char *p; | |
213 | ||
214 | stab += 12; | |
f41e4712 | 215 | p = s + len - 1; |
252b5132 | 216 | *p = '\0'; |
f41e4712 NC |
217 | strx = stroff + bfd_get_32 (abfd, stab); |
218 | if (strx >= strsize) | |
219 | { | |
220 | fprintf (stderr, _("%s: %s: stab entry %ld is corrupt\n"), | |
221 | bfd_get_filename (abfd), names[i].secname, | |
222 | (long) (stab - stabs) / 12); | |
223 | break; | |
224 | } | |
e3d39609 NC |
225 | |
226 | s = concat (s, (char *) strings + strx, | |
227 | (const char *) NULL); | |
252b5132 RH |
228 | |
229 | /* We have to restore the backslash, because, if | |
f3931575 AM |
230 | the linker is hashing stabs strings, we may |
231 | see the same string more than once. */ | |
252b5132 RH |
232 | *p = '\\'; |
233 | ||
e3d39609 | 234 | free (f); |
252b5132 RH |
235 | f = s; |
236 | } | |
237 | ||
238 | save_stab (type, desc, value, s); | |
239 | ||
240 | if (! parse_stab (dhandle, shandle, type, desc, value, s)) | |
241 | { | |
242 | stab_context (); | |
243 | free_saved_stabs (); | |
e3d39609 NC |
244 | free (f); |
245 | free (shandle); | |
246 | free (stabs); | |
247 | free (strings); | |
b34976b6 | 248 | return FALSE; |
252b5132 RH |
249 | } |
250 | ||
251 | /* Don't free f, since I think the stabs code | |
f3931575 AM |
252 | expects strings to hang around. This should be |
253 | straightened out. FIXME. */ | |
252b5132 RH |
254 | } |
255 | } | |
256 | ||
257 | free_saved_stabs (); | |
258 | free (stabs); | |
259 | ||
260 | /* Don't free strings, since I think the stabs code expects | |
f3931575 AM |
261 | the strings to hang around. This should be straightened |
262 | out. FIXME. */ | |
252b5132 RH |
263 | } |
264 | } | |
265 | ||
266 | if (shandle != NULL) | |
267 | { | |
268 | if (! finish_stab (dhandle, shandle)) | |
b34976b6 | 269 | return FALSE; |
252b5132 RH |
270 | } |
271 | ||
b34976b6 | 272 | return TRUE; |
252b5132 RH |
273 | } |
274 | ||
275 | /* Read stabs in the symbol table. */ | |
276 | ||
b34976b6 | 277 | static bfd_boolean |
2da42df6 AJ |
278 | read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, |
279 | void *dhandle, bfd_boolean *pfound) | |
252b5132 | 280 | { |
2da42df6 | 281 | void *shandle; |
252b5132 RH |
282 | asymbol **ps, **symend; |
283 | ||
284 | shandle = NULL; | |
285 | symend = syms + symcount; | |
286 | for (ps = syms; ps < symend; ps++) | |
287 | { | |
288 | symbol_info i; | |
289 | ||
290 | bfd_get_symbol_info (abfd, *ps, &i); | |
291 | ||
292 | if (i.type == '-') | |
293 | { | |
294 | const char *s; | |
295 | char *f; | |
296 | ||
297 | if (shandle == NULL) | |
298 | { | |
b34976b6 | 299 | shandle = start_stab (dhandle, abfd, FALSE, syms, symcount); |
252b5132 | 300 | if (shandle == NULL) |
b34976b6 | 301 | return FALSE; |
252b5132 RH |
302 | } |
303 | ||
b34976b6 | 304 | *pfound = TRUE; |
252b5132 RH |
305 | |
306 | s = i.name; | |
92134dc1 NC |
307 | if (s == NULL || strlen (s) < 1) |
308 | return FALSE; | |
252b5132 | 309 | f = NULL; |
92134dc1 | 310 | |
ca4cf9b9 NC |
311 | while (strlen (s) > 0 |
312 | && s[strlen (s) - 1] == '\\' | |
252b5132 RH |
313 | && ps + 1 < symend) |
314 | { | |
315 | char *sc, *n; | |
316 | ||
317 | ++ps; | |
318 | sc = xstrdup (s); | |
319 | sc[strlen (sc) - 1] = '\0'; | |
320 | n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL); | |
321 | free (sc); | |
322 | if (f != NULL) | |
323 | free (f); | |
324 | f = n; | |
325 | s = n; | |
326 | } | |
327 | ||
328 | save_stab (i.stab_type, i.stab_desc, i.value, s); | |
329 | ||
330 | if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc, | |
331 | i.value, s)) | |
332 | { | |
333 | stab_context (); | |
334 | free_saved_stabs (); | |
b34976b6 | 335 | return FALSE; |
252b5132 RH |
336 | } |
337 | ||
338 | /* Don't free f, since I think the stabs code expects | |
339 | strings to hang around. This should be straightened out. | |
340 | FIXME. */ | |
341 | } | |
342 | } | |
343 | ||
344 | free_saved_stabs (); | |
345 | ||
346 | if (shandle != NULL) | |
347 | { | |
348 | if (! finish_stab (dhandle, shandle)) | |
b34976b6 | 349 | return FALSE; |
252b5132 RH |
350 | } |
351 | ||
b34976b6 | 352 | return TRUE; |
252b5132 | 353 | } |
252b5132 RH |
354 | \f |
355 | /* Record stabs strings, so that we can give some context for errors. */ | |
356 | ||
357 | #define SAVE_STABS_COUNT (16) | |
358 | ||
359 | struct saved_stab | |
360 | { | |
361 | int type; | |
362 | int desc; | |
363 | bfd_vma value; | |
364 | char *string; | |
365 | }; | |
366 | ||
367 | static struct saved_stab saved_stabs[SAVE_STABS_COUNT]; | |
368 | static int saved_stabs_index; | |
369 | ||
370 | /* Save a stabs string. */ | |
371 | ||
372 | static void | |
2da42df6 | 373 | save_stab (int type, int desc, bfd_vma value, const char *string) |
252b5132 RH |
374 | { |
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; | |
382 | } | |
383 | ||
384 | /* Provide context for an error. */ | |
385 | ||
386 | static void | |
2da42df6 | 387 | stab_context (void) |
252b5132 RH |
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_vma (stderr, 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 | |
2da42df6 | 425 | free_saved_stabs (void) |
252b5132 RH |
426 | { |
427 | int i; | |
428 | ||
429 | for (i = 0; i < SAVE_STABS_COUNT; i++) | |
430 | { | |
431 | if (saved_stabs[i].string != NULL) | |
432 | { | |
433 | free (saved_stabs[i].string); | |
434 | saved_stabs[i].string = NULL; | |
435 | } | |
436 | } | |
437 | ||
438 | saved_stabs_index = 0; | |
439 | } |