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