Commit | Line | Data |
---|---|---|
e1c14599 | 1 | /* rddbg.c -- Read debugging information into a generic form. |
15731fdc | 2 | Copyright (C) 1995, 1996 Free Software Foundation, Inc. |
e1c14599 ILT |
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 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 | ||
32 | static boolean read_section_stabs_debugging_info | |
15731fdc ILT |
33 | PARAMS ((bfd *, asymbol **, long, PTR, boolean *)); |
34 | static boolean read_symbol_stabs_debugging_info | |
35 | PARAMS ((bfd *, asymbol **, long, PTR, boolean *)); | |
36 | static boolean read_ieee_debugging_info PARAMS ((bfd *, PTR, boolean *)); | |
37 | static void save_stab PARAMS ((int, int, bfd_vma, const char *)); | |
38 | static void stab_context PARAMS ((void)); | |
39 | static void free_saved_stabs PARAMS ((void)); | |
e1c14599 ILT |
40 | |
41 | /* Read debugging information from a BFD. Returns a generic debugging | |
42 | pointer. */ | |
43 | ||
44 | PTR | |
15731fdc | 45 | read_debugging_info (abfd, syms, symcount) |
e1c14599 | 46 | bfd *abfd; |
15731fdc ILT |
47 | asymbol **syms; |
48 | long symcount; | |
e1c14599 ILT |
49 | { |
50 | PTR dhandle; | |
51 | boolean found; | |
52 | ||
53 | dhandle = debug_init (); | |
54 | if (dhandle == NULL) | |
55 | return NULL; | |
56 | ||
15731fdc ILT |
57 | if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, |
58 | &found)) | |
e1c14599 ILT |
59 | return NULL; |
60 | ||
15731fdc ILT |
61 | if (bfd_get_flavour (abfd) == bfd_target_aout_flavour) |
62 | { | |
63 | if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, | |
64 | &found)) | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | if (bfd_get_flavour (abfd) == bfd_target_ieee_flavour) | |
69 | { | |
70 | if (! read_ieee_debugging_info (abfd, dhandle, &found)) | |
71 | return NULL; | |
72 | } | |
73 | ||
f32fb3fd ILT |
74 | /* Try reading the COFF symbols if we didn't find any stabs in COFF |
75 | sections. */ | |
76 | if (! found | |
77 | && bfd_get_flavour (abfd) == bfd_target_coff_flavour | |
78 | && symcount > 0) | |
79 | { | |
80 | if (! parse_coff (abfd, syms, symcount, dhandle)) | |
81 | return NULL; | |
82 | found = true; | |
83 | } | |
84 | ||
e1c14599 ILT |
85 | if (! found) |
86 | { | |
87 | fprintf (stderr, "%s: no recognized debugging information\n", | |
88 | bfd_get_filename (abfd)); | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | return dhandle; | |
93 | } | |
94 | ||
95 | /* Read stabs in sections debugging information from a BFD. */ | |
96 | ||
97 | static boolean | |
15731fdc | 98 | read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound) |
e1c14599 | 99 | bfd *abfd; |
15731fdc ILT |
100 | asymbol **syms; |
101 | long symcount; | |
e1c14599 ILT |
102 | PTR dhandle; |
103 | boolean *pfound; | |
104 | { | |
105 | static struct | |
106 | { | |
107 | const char *secname; | |
108 | const char *strsecname; | |
109 | } names[] = { { ".stab", ".stabstr" } }; | |
110 | unsigned int i; | |
111 | PTR shandle; | |
112 | ||
113 | *pfound = false; | |
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 ())); | |
136 | return false; | |
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 ())); | |
146 | return false; | |
147 | } | |
148 | ||
149 | if (shandle == NULL) | |
150 | { | |
15731fdc | 151 | shandle = start_stab (dhandle, abfd, true, syms, symcount); |
e1c14599 ILT |
152 | if (shandle == NULL) |
153 | return false; | |
154 | } | |
155 | ||
156 | *pfound = true; | |
157 | ||
158 | stroff = 0; | |
159 | next_stroff = 0; | |
160 | for (stab = stabs; stab < stabs + stabsize; stab += 12) | |
161 | { | |
162 | bfd_size_type strx; | |
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 | |
179 | next string table. */ | |
180 | stroff = next_stroff; | |
181 | next_stroff += value; | |
182 | } | |
183 | else | |
184 | { | |
185 | char *f, *s; | |
186 | ||
187 | f = NULL; | |
188 | s = (char *) strings + stroff + strx; | |
189 | while (s[strlen (s) - 1] == '\\' | |
190 | && stab + 12 < stabs + stabsize) | |
191 | { | |
192 | stab += 12; | |
193 | s[strlen (s) - 1] = '\0'; | |
194 | s = concat (s, | |
195 | ((char *) strings | |
196 | + stroff | |
197 | + bfd_get_32 (abfd, stab)), | |
198 | (const char *) NULL); | |
199 | if (f != NULL) | |
200 | free (f); | |
201 | f = s; | |
202 | } | |
203 | ||
15731fdc ILT |
204 | save_stab (type, desc, value, s); |
205 | ||
e1c14599 | 206 | if (! parse_stab (dhandle, shandle, type, desc, value, s)) |
15731fdc ILT |
207 | { |
208 | stab_context (); | |
209 | free_saved_stabs (); | |
210 | return false; | |
211 | } | |
e1c14599 ILT |
212 | |
213 | /* Don't free f, since I think the stabs code | |
214 | expects strings to hang around. This should be | |
215 | straightened out. FIXME. */ | |
216 | } | |
217 | } | |
218 | ||
15731fdc | 219 | free_saved_stabs (); |
e1c14599 ILT |
220 | free (stabs); |
221 | ||
222 | /* Don't free strings, since I think the stabs code expects | |
223 | the strings to hang around. This should be straightened | |
224 | out. FIXME. */ | |
225 | } | |
226 | } | |
227 | ||
228 | if (shandle != NULL) | |
229 | { | |
230 | if (! finish_stab (dhandle, shandle)) | |
231 | return false; | |
232 | } | |
233 | ||
234 | return true; | |
235 | } | |
15731fdc ILT |
236 | |
237 | /* Read stabs in the symbol table. */ | |
238 | ||
239 | static boolean | |
240 | read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound) | |
241 | bfd *abfd; | |
242 | asymbol **syms; | |
243 | long symcount; | |
244 | PTR dhandle; | |
245 | boolean *pfound; | |
246 | { | |
247 | PTR shandle; | |
248 | asymbol **ps, **symend; | |
249 | ||
250 | shandle = NULL; | |
251 | symend = syms + symcount; | |
252 | for (ps = syms; ps < symend; ps++) | |
253 | { | |
254 | symbol_info i; | |
255 | ||
256 | bfd_get_symbol_info (abfd, *ps, &i); | |
257 | ||
258 | if (i.type == '-') | |
259 | { | |
260 | const char *s; | |
261 | char *f; | |
262 | ||
263 | if (shandle == NULL) | |
264 | { | |
265 | shandle = start_stab (dhandle, abfd, false, syms, symcount); | |
266 | if (shandle == NULL) | |
267 | return false; | |
268 | } | |
269 | ||
270 | *pfound = true; | |
271 | ||
272 | s = i.name; | |
273 | while (s[strlen (s) - 1] == '\\' | |
274 | && ps + 1 < symend) | |
275 | { | |
276 | char *sc, *n; | |
277 | ||
278 | ++ps; | |
279 | sc = xstrdup (s); | |
280 | sc[strlen (sc) - 1] = '\0'; | |
281 | n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL); | |
282 | free (sc); | |
283 | if (f != NULL) | |
284 | free (f); | |
285 | f = n; | |
286 | s = n; | |
287 | } | |
288 | ||
289 | save_stab (i.stab_type, i.stab_desc, i.value, s); | |
290 | ||
291 | if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc, | |
292 | i.value, s)) | |
293 | { | |
294 | stab_context (); | |
295 | free_saved_stabs (); | |
296 | return false; | |
297 | } | |
298 | ||
299 | free_saved_stabs (); | |
300 | ||
301 | /* Don't free f, since I think the stabs code expects | |
302 | strings to hang around. This should be straightened out. | |
303 | FIXME. */ | |
304 | } | |
305 | } | |
306 | ||
307 | if (shandle != NULL) | |
308 | { | |
309 | if (! finish_stab (dhandle, shandle)) | |
310 | return false; | |
311 | } | |
312 | ||
313 | return true; | |
314 | } | |
315 | ||
316 | /* Read IEEE debugging information. */ | |
317 | ||
318 | static boolean | |
319 | read_ieee_debugging_info (abfd, dhandle, pfound) | |
320 | bfd *abfd; | |
321 | PTR dhandle; | |
322 | boolean *pfound; | |
323 | { | |
324 | asection *dsec; | |
325 | bfd_size_type size; | |
326 | bfd_byte *contents; | |
327 | ||
328 | /* The BFD backend puts the debugging information into a section | |
329 | named .debug. */ | |
330 | ||
331 | dsec = bfd_get_section_by_name (abfd, ".debug"); | |
332 | if (dsec == NULL) | |
333 | return true; | |
334 | ||
335 | size = bfd_section_size (abfd, dsec); | |
336 | contents = (bfd_byte *) xmalloc (size); | |
337 | if (! bfd_get_section_contents (abfd, dsec, contents, 0, size)) | |
338 | return false; | |
339 | ||
340 | if (! parse_ieee (dhandle, abfd, contents, size)) | |
341 | return false; | |
342 | ||
343 | free (contents); | |
344 | ||
345 | *pfound = true; | |
346 | ||
347 | return true; | |
348 | } | |
349 | \f | |
350 | /* Record stabs strings, so that we can give some context for errors. */ | |
351 | ||
352 | #define SAVE_STABS_COUNT (16) | |
353 | ||
354 | struct saved_stab | |
355 | { | |
356 | int type; | |
357 | int desc; | |
358 | bfd_vma value; | |
359 | char *string; | |
360 | }; | |
361 | ||
362 | static struct saved_stab saved_stabs[SAVE_STABS_COUNT]; | |
363 | static int saved_stabs_index; | |
364 | ||
365 | /* Save a stabs string. */ | |
366 | ||
367 | static void | |
368 | save_stab (type, desc, value, string) | |
369 | int type; | |
370 | int desc; | |
371 | bfd_vma value; | |
372 | const char *string; | |
373 | { | |
374 | if (saved_stabs[saved_stabs_index].string != NULL) | |
375 | free (saved_stabs[saved_stabs_index].string); | |
376 | saved_stabs[saved_stabs_index].type = type; | |
377 | saved_stabs[saved_stabs_index].desc = desc; | |
378 | saved_stabs[saved_stabs_index].value = value; | |
379 | saved_stabs[saved_stabs_index].string = xstrdup (string); | |
380 | saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT; | |
381 | } | |
382 | ||
383 | /* Provide context for an error. */ | |
384 | ||
385 | static void | |
386 | stab_context () | |
387 | { | |
388 | int i; | |
389 | ||
390 | fprintf (stderr, "Last stabs entries before error:\n"); | |
391 | fprintf (stderr, "n_type n_desc n_value string\n"); | |
392 | ||
393 | i = saved_stabs_index; | |
394 | do | |
395 | { | |
396 | struct saved_stab *stabp; | |
397 | ||
398 | stabp = saved_stabs + i; | |
399 | if (stabp->string != NULL) | |
400 | { | |
401 | const char *s; | |
402 | ||
403 | s = bfd_get_stab_name (stabp->type); | |
404 | if (s != NULL) | |
405 | fprintf (stderr, "%-6s", s); | |
406 | else if (stabp->type == 0) | |
407 | fprintf (stderr, "HdrSym"); | |
408 | else | |
409 | fprintf (stderr, "%-6d", stabp->type); | |
410 | fprintf (stderr, " %-6d ", stabp->desc); | |
411 | fprintf_vma (stderr, stabp->value); | |
412 | if (stabp->type != 0) | |
413 | fprintf (stderr, " %s", stabp->string); | |
414 | fprintf (stderr, "\n"); | |
415 | } | |
416 | i = (i + 1) % SAVE_STABS_COUNT; | |
417 | } | |
418 | while (i != saved_stabs_index); | |
419 | } | |
420 | ||
421 | /* Free the saved stab strings. */ | |
422 | ||
423 | static void | |
424 | free_saved_stabs () | |
425 | { | |
426 | int i; | |
427 | ||
428 | for (i = 0; i < SAVE_STABS_COUNT; i++) | |
429 | if (saved_stabs[i].string != NULL) | |
430 | free (saved_stabs[i].string); | |
431 | saved_stabs_index = 0; | |
432 | } |