]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* rddbg.c -- Read debugging information into a generic form. |
f3931575 | 2 | Copyright 1995, 1996, 1997, 2000, 2002 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 | |
9 | the Free Software Foundation; either version 2 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., 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | /* This file reads debugging information into a generic form. This | |
23 | file knows how to dig the debugging information out of an object | |
24 | file. */ | |
25 | ||
26 | #include "bfd.h" | |
27 | #include "bucomm.h" | |
28 | #include "libiberty.h" | |
29 | #include "debug.h" | |
30 | #include "budbg.h" | |
31 | ||
b34976b6 AM |
32 | static bfd_boolean read_section_stabs_debugging_info |
33 | PARAMS ((bfd *, asymbol **, long, PTR, bfd_boolean *)); | |
34 | static bfd_boolean read_symbol_stabs_debugging_info | |
35 | PARAMS ((bfd *, asymbol **, long, PTR, bfd_boolean *)); | |
36 | static bfd_boolean read_ieee_debugging_info | |
37 | PARAMS ((bfd *, PTR, bfd_boolean *)); | |
38 | static void save_stab | |
39 | PARAMS ((int, int, bfd_vma, const char *)); | |
40 | static void stab_context | |
41 | PARAMS ((void)); | |
42 | static void free_saved_stabs | |
43 | PARAMS ((void)); | |
252b5132 RH |
44 | |
45 | /* Read debugging information from a BFD. Returns a generic debugging | |
46 | pointer. */ | |
47 | ||
48 | PTR | |
49 | read_debugging_info (abfd, syms, symcount) | |
50 | bfd *abfd; | |
51 | asymbol **syms; | |
52 | long symcount; | |
53 | { | |
54 | PTR dhandle; | |
b34976b6 | 55 | bfd_boolean found; |
252b5132 RH |
56 | |
57 | dhandle = debug_init (); | |
58 | if (dhandle == NULL) | |
59 | return NULL; | |
60 | ||
61 | if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, | |
62 | &found)) | |
63 | return NULL; | |
64 | ||
65 | if (bfd_get_flavour (abfd) == bfd_target_aout_flavour) | |
66 | { | |
67 | if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, | |
68 | &found)) | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | if (bfd_get_flavour (abfd) == bfd_target_ieee_flavour) | |
73 | { | |
74 | if (! read_ieee_debugging_info (abfd, dhandle, &found)) | |
75 | return NULL; | |
76 | } | |
77 | ||
78 | /* Try reading the COFF symbols if we didn't find any stabs in COFF | |
79 | sections. */ | |
80 | if (! found | |
81 | && bfd_get_flavour (abfd) == bfd_target_coff_flavour | |
82 | && symcount > 0) | |
83 | { | |
84 | if (! parse_coff (abfd, syms, symcount, dhandle)) | |
85 | return NULL; | |
b34976b6 | 86 | found = TRUE; |
252b5132 RH |
87 | } |
88 | ||
89 | if (! found) | |
90 | { | |
37cc8ec1 AM |
91 | non_fatal (_("%s: no recognized debugging information"), |
92 | bfd_get_filename (abfd)); | |
252b5132 RH |
93 | return NULL; |
94 | } | |
95 | ||
96 | return dhandle; | |
97 | } | |
98 | ||
99 | /* Read stabs in sections debugging information from a BFD. */ | |
100 | ||
b34976b6 | 101 | static bfd_boolean |
252b5132 RH |
102 | read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound) |
103 | bfd *abfd; | |
104 | asymbol **syms; | |
105 | long symcount; | |
106 | PTR dhandle; | |
b34976b6 | 107 | bfd_boolean *pfound; |
252b5132 RH |
108 | { |
109 | static struct | |
110 | { | |
111 | const char *secname; | |
112 | const char *strsecname; | |
f3931575 AM |
113 | } names[] = { { ".stab", ".stabstr" }, |
114 | { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" } }; | |
252b5132 RH |
115 | unsigned int i; |
116 | PTR shandle; | |
117 | ||
b34976b6 | 118 | *pfound = FALSE; |
252b5132 RH |
119 | shandle = NULL; |
120 | ||
121 | for (i = 0; i < sizeof names / sizeof names[0]; i++) | |
122 | { | |
123 | asection *sec, *strsec; | |
124 | ||
125 | sec = bfd_get_section_by_name (abfd, names[i].secname); | |
126 | strsec = bfd_get_section_by_name (abfd, names[i].strsecname); | |
127 | if (sec != NULL && strsec != NULL) | |
128 | { | |
129 | bfd_size_type stabsize, strsize; | |
130 | bfd_byte *stabs, *strings; | |
131 | bfd_byte *stab; | |
132 | bfd_size_type stroff, next_stroff; | |
133 | ||
134 | stabsize = bfd_section_size (abfd, sec); | |
135 | stabs = (bfd_byte *) xmalloc (stabsize); | |
136 | if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize)) | |
137 | { | |
138 | fprintf (stderr, "%s: %s: %s\n", | |
139 | bfd_get_filename (abfd), names[i].secname, | |
140 | bfd_errmsg (bfd_get_error ())); | |
b34976b6 | 141 | return FALSE; |
252b5132 RH |
142 | } |
143 | ||
144 | strsize = bfd_section_size (abfd, strsec); | |
145 | strings = (bfd_byte *) xmalloc (strsize); | |
146 | if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize)) | |
147 | { | |
148 | fprintf (stderr, "%s: %s: %s\n", | |
149 | bfd_get_filename (abfd), names[i].strsecname, | |
150 | bfd_errmsg (bfd_get_error ())); | |
b34976b6 | 151 | return FALSE; |
252b5132 RH |
152 | } |
153 | ||
154 | if (shandle == NULL) | |
155 | { | |
b34976b6 | 156 | shandle = start_stab (dhandle, abfd, TRUE, syms, symcount); |
252b5132 | 157 | if (shandle == NULL) |
b34976b6 | 158 | return FALSE; |
252b5132 RH |
159 | } |
160 | ||
b34976b6 | 161 | *pfound = TRUE; |
252b5132 RH |
162 | |
163 | stroff = 0; | |
164 | next_stroff = 0; | |
165 | for (stab = stabs; stab < stabs + stabsize; stab += 12) | |
166 | { | |
37cc8ec1 | 167 | unsigned int strx; |
252b5132 RH |
168 | int type; |
169 | int other; | |
170 | int desc; | |
171 | bfd_vma value; | |
172 | ||
173 | /* This code presumes 32 bit values. */ | |
174 | ||
175 | strx = bfd_get_32 (abfd, stab); | |
176 | type = bfd_get_8 (abfd, stab + 4); | |
177 | other = bfd_get_8 (abfd, stab + 5); | |
178 | desc = bfd_get_16 (abfd, stab + 6); | |
179 | value = bfd_get_32 (abfd, stab + 8); | |
180 | ||
181 | if (type == 0) | |
182 | { | |
183 | /* Special type 0 stabs indicate the offset to the | |
f3931575 | 184 | next string table. */ |
252b5132 RH |
185 | stroff = next_stroff; |
186 | next_stroff += value; | |
187 | } | |
188 | else | |
189 | { | |
190 | char *f, *s; | |
191 | ||
192 | f = NULL; | |
53c7db4b | 193 | |
3b7aaf81 NC |
194 | if (stroff + strx > strsize) |
195 | { | |
22d82235 | 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; |
53c7db4b | 203 | |
252b5132 RH |
204 | while (s[strlen (s) - 1] == '\\' |
205 | && stab + 12 < stabs + stabsize) | |
206 | { | |
207 | char *p; | |
208 | ||
209 | stab += 12; | |
210 | p = s + strlen (s) - 1; | |
211 | *p = '\0'; | |
212 | s = concat (s, | |
213 | ((char *) strings | |
214 | + stroff | |
215 | + bfd_get_32 (abfd, stab)), | |
216 | (const char *) NULL); | |
217 | ||
218 | /* We have to restore the backslash, because, if | |
f3931575 AM |
219 | the linker is hashing stabs strings, we may |
220 | see the same string more than once. */ | |
252b5132 RH |
221 | *p = '\\'; |
222 | ||
223 | if (f != NULL) | |
224 | free (f); | |
225 | f = s; | |
226 | } | |
227 | ||
228 | save_stab (type, desc, value, s); | |
229 | ||
230 | if (! parse_stab (dhandle, shandle, type, desc, value, s)) | |
231 | { | |
232 | stab_context (); | |
233 | free_saved_stabs (); | |
b34976b6 | 234 | return FALSE; |
252b5132 RH |
235 | } |
236 | ||
237 | /* Don't free f, since I think the stabs code | |
f3931575 AM |
238 | expects strings to hang around. This should be |
239 | straightened out. FIXME. */ | |
252b5132 RH |
240 | } |
241 | } | |
242 | ||
243 | free_saved_stabs (); | |
244 | free (stabs); | |
245 | ||
246 | /* Don't free strings, since I think the stabs code expects | |
f3931575 AM |
247 | the strings to hang around. This should be straightened |
248 | out. FIXME. */ | |
252b5132 RH |
249 | } |
250 | } | |
251 | ||
252 | if (shandle != NULL) | |
253 | { | |
254 | if (! finish_stab (dhandle, shandle)) | |
b34976b6 | 255 | return FALSE; |
252b5132 RH |
256 | } |
257 | ||
b34976b6 | 258 | return TRUE; |
252b5132 RH |
259 | } |
260 | ||
261 | /* Read stabs in the symbol table. */ | |
262 | ||
b34976b6 | 263 | static bfd_boolean |
252b5132 RH |
264 | read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound) |
265 | bfd *abfd; | |
266 | asymbol **syms; | |
267 | long symcount; | |
268 | PTR dhandle; | |
b34976b6 | 269 | bfd_boolean *pfound; |
252b5132 RH |
270 | { |
271 | PTR shandle; | |
272 | asymbol **ps, **symend; | |
273 | ||
274 | shandle = NULL; | |
275 | symend = syms + symcount; | |
276 | for (ps = syms; ps < symend; ps++) | |
277 | { | |
278 | symbol_info i; | |
279 | ||
280 | bfd_get_symbol_info (abfd, *ps, &i); | |
281 | ||
282 | if (i.type == '-') | |
283 | { | |
284 | const char *s; | |
285 | char *f; | |
286 | ||
287 | if (shandle == NULL) | |
288 | { | |
b34976b6 | 289 | shandle = start_stab (dhandle, abfd, FALSE, syms, symcount); |
252b5132 | 290 | if (shandle == NULL) |
b34976b6 | 291 | return FALSE; |
252b5132 RH |
292 | } |
293 | ||
b34976b6 | 294 | *pfound = TRUE; |
252b5132 RH |
295 | |
296 | s = i.name; | |
297 | f = NULL; | |
298 | while (s[strlen (s) - 1] == '\\' | |
299 | && ps + 1 < symend) | |
300 | { | |
301 | char *sc, *n; | |
302 | ||
303 | ++ps; | |
304 | sc = xstrdup (s); | |
305 | sc[strlen (sc) - 1] = '\0'; | |
306 | n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL); | |
307 | free (sc); | |
308 | if (f != NULL) | |
309 | free (f); | |
310 | f = n; | |
311 | s = n; | |
312 | } | |
313 | ||
314 | save_stab (i.stab_type, i.stab_desc, i.value, s); | |
315 | ||
316 | if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc, | |
317 | i.value, s)) | |
318 | { | |
319 | stab_context (); | |
320 | free_saved_stabs (); | |
b34976b6 | 321 | return FALSE; |
252b5132 RH |
322 | } |
323 | ||
324 | /* Don't free f, since I think the stabs code expects | |
325 | strings to hang around. This should be straightened out. | |
326 | FIXME. */ | |
327 | } | |
328 | } | |
329 | ||
330 | free_saved_stabs (); | |
331 | ||
332 | if (shandle != NULL) | |
333 | { | |
334 | if (! finish_stab (dhandle, shandle)) | |
b34976b6 | 335 | return FALSE; |
252b5132 RH |
336 | } |
337 | ||
b34976b6 | 338 | return TRUE; |
252b5132 RH |
339 | } |
340 | ||
341 | /* Read IEEE debugging information. */ | |
342 | ||
b34976b6 | 343 | static bfd_boolean |
252b5132 RH |
344 | read_ieee_debugging_info (abfd, dhandle, pfound) |
345 | bfd *abfd; | |
346 | PTR dhandle; | |
b34976b6 | 347 | bfd_boolean *pfound; |
252b5132 RH |
348 | { |
349 | asection *dsec; | |
350 | bfd_size_type size; | |
351 | bfd_byte *contents; | |
352 | ||
353 | /* The BFD backend puts the debugging information into a section | |
354 | named .debug. */ | |
355 | ||
356 | dsec = bfd_get_section_by_name (abfd, ".debug"); | |
357 | if (dsec == NULL) | |
b34976b6 | 358 | return TRUE; |
252b5132 RH |
359 | |
360 | size = bfd_section_size (abfd, dsec); | |
361 | contents = (bfd_byte *) xmalloc (size); | |
362 | if (! bfd_get_section_contents (abfd, dsec, contents, 0, size)) | |
b34976b6 | 363 | return FALSE; |
252b5132 RH |
364 | |
365 | if (! parse_ieee (dhandle, abfd, contents, size)) | |
b34976b6 | 366 | return FALSE; |
252b5132 RH |
367 | |
368 | free (contents); | |
369 | ||
b34976b6 | 370 | *pfound = TRUE; |
252b5132 | 371 | |
b34976b6 | 372 | return TRUE; |
252b5132 RH |
373 | } |
374 | \f | |
375 | /* Record stabs strings, so that we can give some context for errors. */ | |
376 | ||
377 | #define SAVE_STABS_COUNT (16) | |
378 | ||
379 | struct saved_stab | |
380 | { | |
381 | int type; | |
382 | int desc; | |
383 | bfd_vma value; | |
384 | char *string; | |
385 | }; | |
386 | ||
387 | static struct saved_stab saved_stabs[SAVE_STABS_COUNT]; | |
388 | static int saved_stabs_index; | |
389 | ||
390 | /* Save a stabs string. */ | |
391 | ||
392 | static void | |
393 | save_stab (type, desc, value, string) | |
394 | int type; | |
395 | int desc; | |
396 | bfd_vma value; | |
397 | const char *string; | |
398 | { | |
399 | if (saved_stabs[saved_stabs_index].string != NULL) | |
400 | free (saved_stabs[saved_stabs_index].string); | |
401 | saved_stabs[saved_stabs_index].type = type; | |
402 | saved_stabs[saved_stabs_index].desc = desc; | |
403 | saved_stabs[saved_stabs_index].value = value; | |
404 | saved_stabs[saved_stabs_index].string = xstrdup (string); | |
405 | saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT; | |
406 | } | |
407 | ||
408 | /* Provide context for an error. */ | |
409 | ||
410 | static void | |
411 | stab_context () | |
412 | { | |
413 | int i; | |
414 | ||
415 | fprintf (stderr, _("Last stabs entries before error:\n")); | |
416 | fprintf (stderr, "n_type n_desc n_value string\n"); | |
417 | ||
418 | i = saved_stabs_index; | |
419 | do | |
420 | { | |
421 | struct saved_stab *stabp; | |
422 | ||
423 | stabp = saved_stabs + i; | |
424 | if (stabp->string != NULL) | |
425 | { | |
426 | const char *s; | |
427 | ||
428 | s = bfd_get_stab_name (stabp->type); | |
429 | if (s != NULL) | |
430 | fprintf (stderr, "%-6s", s); | |
431 | else if (stabp->type == 0) | |
432 | fprintf (stderr, "HdrSym"); | |
433 | else | |
434 | fprintf (stderr, "%-6d", stabp->type); | |
435 | fprintf (stderr, " %-6d ", stabp->desc); | |
436 | fprintf_vma (stderr, stabp->value); | |
437 | if (stabp->type != 0) | |
438 | fprintf (stderr, " %s", stabp->string); | |
439 | fprintf (stderr, "\n"); | |
440 | } | |
441 | i = (i + 1) % SAVE_STABS_COUNT; | |
442 | } | |
443 | while (i != saved_stabs_index); | |
444 | } | |
445 | ||
446 | /* Free the saved stab strings. */ | |
447 | ||
448 | static void | |
449 | free_saved_stabs () | |
450 | { | |
451 | int i; | |
452 | ||
453 | for (i = 0; i < SAVE_STABS_COUNT; i++) | |
454 | { | |
455 | if (saved_stabs[i].string != NULL) | |
456 | { | |
457 | free (saved_stabs[i].string); | |
458 | saved_stabs[i].string = NULL; | |
459 | } | |
460 | } | |
461 | ||
462 | saved_stabs_index = 0; | |
463 | } |