A mostly cosmetic tidy up of warnings and error message reporting.
[deliverable/binutils-gdb.git] / binutils / rddbg.c
1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright (C) 1995, 96, 97, 98, 2000 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 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
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));
40
41 /* Read debugging information from a BFD. Returns a generic debugging
42 pointer. */
43
44 PTR
45 read_debugging_info (abfd, syms, symcount)
46 bfd *abfd;
47 asymbol **syms;
48 long symcount;
49 {
50 PTR dhandle;
51 boolean found;
52
53 dhandle = debug_init ();
54 if (dhandle == NULL)
55 return NULL;
56
57 if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
58 &found))
59 return NULL;
60
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
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
85 if (! found)
86 {
87 non_fatal (_("%s: no recognized debugging information"),
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
98 read_section_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound)
99 bfd *abfd;
100 asymbol **syms;
101 long symcount;
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 {
151 shandle = start_stab (dhandle, abfd, true, syms, symcount);
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 unsigned int 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
189 if (stroff + strx > strsize)
190 {
191 fprintf (stderr, "%s: %s: stab entry %d is corrupt, strx = 0x%x, type = %d\n",
192 bfd_get_filename (abfd), names[i].secname,
193 (stab - stabs) / 12, strx, type);
194 continue;
195 }
196
197 s = (char *) strings + stroff + strx;
198
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
214 the linker is hashing stabs strings, we may
215 see the same string more than once. */
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 ();
229 return false;
230 }
231
232 /* Don't free f, since I think the stabs code
233 expects strings to hang around. This should be
234 straightened out. FIXME. */
235 }
236 }
237
238 free_saved_stabs ();
239 free (stabs);
240
241 /* Don't free strings, since I think the stabs code expects
242 the strings to hang around. This should be straightened
243 out. FIXME. */
244 }
245 }
246
247 if (shandle != NULL)
248 {
249 if (! finish_stab (dhandle, shandle))
250 return false;
251 }
252
253 return true;
254 }
255
256 /* Read stabs in the symbol table. */
257
258 static boolean
259 read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle, pfound)
260 bfd *abfd;
261 asymbol **syms;
262 long symcount;
263 PTR dhandle;
264 boolean *pfound;
265 {
266 PTR shandle;
267 asymbol **ps, **symend;
268
269 shandle = NULL;
270 symend = syms + symcount;
271 for (ps = syms; ps < symend; ps++)
272 {
273 symbol_info i;
274
275 bfd_get_symbol_info (abfd, *ps, &i);
276
277 if (i.type == '-')
278 {
279 const char *s;
280 char *f;
281
282 if (shandle == NULL)
283 {
284 shandle = start_stab (dhandle, abfd, false, syms, symcount);
285 if (shandle == NULL)
286 return false;
287 }
288
289 *pfound = true;
290
291 s = i.name;
292 f = NULL;
293 while (s[strlen (s) - 1] == '\\'
294 && ps + 1 < symend)
295 {
296 char *sc, *n;
297
298 ++ps;
299 sc = xstrdup (s);
300 sc[strlen (sc) - 1] = '\0';
301 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
302 free (sc);
303 if (f != NULL)
304 free (f);
305 f = n;
306 s = n;
307 }
308
309 save_stab (i.stab_type, i.stab_desc, i.value, s);
310
311 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
312 i.value, s))
313 {
314 stab_context ();
315 free_saved_stabs ();
316 return false;
317 }
318
319 /* Don't free f, since I think the stabs code expects
320 strings to hang around. This should be straightened out.
321 FIXME. */
322 }
323 }
324
325 free_saved_stabs ();
326
327 if (shandle != NULL)
328 {
329 if (! finish_stab (dhandle, shandle))
330 return false;
331 }
332
333 return true;
334 }
335
336 /* Read IEEE debugging information. */
337
338 static boolean
339 read_ieee_debugging_info (abfd, dhandle, pfound)
340 bfd *abfd;
341 PTR dhandle;
342 boolean *pfound;
343 {
344 asection *dsec;
345 bfd_size_type size;
346 bfd_byte *contents;
347
348 /* The BFD backend puts the debugging information into a section
349 named .debug. */
350
351 dsec = bfd_get_section_by_name (abfd, ".debug");
352 if (dsec == NULL)
353 return true;
354
355 size = bfd_section_size (abfd, dsec);
356 contents = (bfd_byte *) xmalloc (size);
357 if (! bfd_get_section_contents (abfd, dsec, contents, 0, size))
358 return false;
359
360 if (! parse_ieee (dhandle, abfd, contents, size))
361 return false;
362
363 free (contents);
364
365 *pfound = true;
366
367 return true;
368 }
369 \f
370 /* Record stabs strings, so that we can give some context for errors. */
371
372 #define SAVE_STABS_COUNT (16)
373
374 struct saved_stab
375 {
376 int type;
377 int desc;
378 bfd_vma value;
379 char *string;
380 };
381
382 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
383 static int saved_stabs_index;
384
385 /* Save a stabs string. */
386
387 static void
388 save_stab (type, desc, value, string)
389 int type;
390 int desc;
391 bfd_vma value;
392 const char *string;
393 {
394 if (saved_stabs[saved_stabs_index].string != NULL)
395 free (saved_stabs[saved_stabs_index].string);
396 saved_stabs[saved_stabs_index].type = type;
397 saved_stabs[saved_stabs_index].desc = desc;
398 saved_stabs[saved_stabs_index].value = value;
399 saved_stabs[saved_stabs_index].string = xstrdup (string);
400 saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
401 }
402
403 /* Provide context for an error. */
404
405 static void
406 stab_context ()
407 {
408 int i;
409
410 fprintf (stderr, _("Last stabs entries before error:\n"));
411 fprintf (stderr, "n_type n_desc n_value string\n");
412
413 i = saved_stabs_index;
414 do
415 {
416 struct saved_stab *stabp;
417
418 stabp = saved_stabs + i;
419 if (stabp->string != NULL)
420 {
421 const char *s;
422
423 s = bfd_get_stab_name (stabp->type);
424 if (s != NULL)
425 fprintf (stderr, "%-6s", s);
426 else if (stabp->type == 0)
427 fprintf (stderr, "HdrSym");
428 else
429 fprintf (stderr, "%-6d", stabp->type);
430 fprintf (stderr, " %-6d ", stabp->desc);
431 fprintf_vma (stderr, stabp->value);
432 if (stabp->type != 0)
433 fprintf (stderr, " %s", stabp->string);
434 fprintf (stderr, "\n");
435 }
436 i = (i + 1) % SAVE_STABS_COUNT;
437 }
438 while (i != saved_stabs_index);
439 }
440
441 /* Free the saved stab strings. */
442
443 static void
444 free_saved_stabs ()
445 {
446 int i;
447
448 for (i = 0; i < SAVE_STABS_COUNT; i++)
449 {
450 if (saved_stabs[i].string != NULL)
451 {
452 free (saved_stabs[i].string);
453 saved_stabs[i].string = NULL;
454 }
455 }
456
457 saved_stabs_index = 0;
458 }
This page took 0.039208 seconds and 5 git commands to generate.