file ro.gmo was initially added on branch binutils-2_15-branch.
[binutils.git] / binutils / rddbg.c
blob75ddab54488d18af35251ef9d7be3f1b78c475f3
1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright 1995, 1996, 1997, 2000, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of GNU Binutils.
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.
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.
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
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
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. */
27 #include "bfd.h"
28 #include "bucomm.h"
29 #include "libiberty.h"
30 #include "debug.h"
31 #include "budbg.h"
33 static bfd_boolean read_section_stabs_debugging_info
34 (bfd *, asymbol **, long, void *, bfd_boolean *);
35 static bfd_boolean read_symbol_stabs_debugging_info
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);
42 /* Read debugging information from a BFD. Returns a generic debugging
43 pointer. */
45 void *
46 read_debugging_info (bfd *abfd, asymbol **syms, long symcount)
48 void *dhandle;
49 bfd_boolean found;
51 dhandle = debug_init ();
52 if (dhandle == NULL)
53 return NULL;
55 if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
56 &found))
57 return NULL;
59 if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
61 if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle,
62 &found))
63 return NULL;
66 if (bfd_get_flavour (abfd) == bfd_target_ieee_flavour)
68 if (! read_ieee_debugging_info (abfd, dhandle, &found))
69 return NULL;
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)
78 if (! parse_coff (abfd, syms, symcount, dhandle))
79 return NULL;
80 found = TRUE;
83 if (! found)
85 non_fatal (_("%s: no recognized debugging information"),
86 bfd_get_filename (abfd));
87 return NULL;
90 return dhandle;
93 /* Read stabs in sections debugging information from a BFD. */
95 static bfd_boolean
96 read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
97 void *dhandle, bfd_boolean *pfound)
99 static struct
101 const char *secname;
102 const char *strsecname;
103 } names[] = { { ".stab", ".stabstr" },
104 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" } };
105 unsigned int i;
106 void *shandle;
108 *pfound = FALSE;
109 shandle = NULL;
111 for (i = 0; i < sizeof names / sizeof names[0]; i++)
113 asection *sec, *strsec;
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)
119 bfd_size_type stabsize, strsize;
120 bfd_byte *stabs, *strings;
121 bfd_byte *stab;
122 bfd_size_type stroff, next_stroff;
124 stabsize = bfd_section_size (abfd, sec);
125 stabs = (bfd_byte *) xmalloc (stabsize);
126 if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
128 fprintf (stderr, "%s: %s: %s\n",
129 bfd_get_filename (abfd), names[i].secname,
130 bfd_errmsg (bfd_get_error ()));
131 return FALSE;
134 strsize = bfd_section_size (abfd, strsec);
135 strings = (bfd_byte *) xmalloc (strsize);
136 if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
138 fprintf (stderr, "%s: %s: %s\n",
139 bfd_get_filename (abfd), names[i].strsecname,
140 bfd_errmsg (bfd_get_error ()));
141 return FALSE;
144 if (shandle == NULL)
146 shandle = start_stab (dhandle, abfd, TRUE, syms, symcount);
147 if (shandle == NULL)
148 return FALSE;
151 *pfound = TRUE;
153 stroff = 0;
154 next_stroff = 0;
155 for (stab = stabs; stab < stabs + stabsize; stab += 12)
157 unsigned int strx;
158 int type;
159 int other;
160 int desc;
161 bfd_vma value;
163 /* This code presumes 32 bit values. */
165 strx = bfd_get_32 (abfd, stab);
166 type = bfd_get_8 (abfd, stab + 4);
167 other = bfd_get_8 (abfd, stab + 5);
168 desc = bfd_get_16 (abfd, stab + 6);
169 value = bfd_get_32 (abfd, stab + 8);
171 if (type == 0)
173 /* Special type 0 stabs indicate the offset to the
174 next string table. */
175 stroff = next_stroff;
176 next_stroff += value;
178 else
180 char *f, *s;
182 f = NULL;
184 if (stroff + strx > strsize)
186 fprintf (stderr, "%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n",
187 bfd_get_filename (abfd), names[i].secname,
188 (long) (stab - stabs) / 12, strx, type);
189 continue;
192 s = (char *) strings + stroff + strx;
194 while (s[strlen (s) - 1] == '\\'
195 && stab + 12 < stabs + stabsize)
197 char *p;
199 stab += 12;
200 p = s + strlen (s) - 1;
201 *p = '\0';
202 s = concat (s,
203 ((char *) strings
204 + stroff
205 + bfd_get_32 (abfd, stab)),
206 (const char *) NULL);
208 /* We have to restore the backslash, because, if
209 the linker is hashing stabs strings, we may
210 see the same string more than once. */
211 *p = '\\';
213 if (f != NULL)
214 free (f);
215 f = s;
218 save_stab (type, desc, value, s);
220 if (! parse_stab (dhandle, shandle, type, desc, value, s))
222 stab_context ();
223 free_saved_stabs ();
224 return FALSE;
227 /* Don't free f, since I think the stabs code
228 expects strings to hang around. This should be
229 straightened out. FIXME. */
233 free_saved_stabs ();
234 free (stabs);
236 /* Don't free strings, since I think the stabs code expects
237 the strings to hang around. This should be straightened
238 out. FIXME. */
242 if (shandle != NULL)
244 if (! finish_stab (dhandle, shandle))
245 return FALSE;
248 return TRUE;
251 /* Read stabs in the symbol table. */
253 static bfd_boolean
254 read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
255 void *dhandle, bfd_boolean *pfound)
257 void *shandle;
258 asymbol **ps, **symend;
260 shandle = NULL;
261 symend = syms + symcount;
262 for (ps = syms; ps < symend; ps++)
264 symbol_info i;
266 bfd_get_symbol_info (abfd, *ps, &i);
268 if (i.type == '-')
270 const char *s;
271 char *f;
273 if (shandle == NULL)
275 shandle = start_stab (dhandle, abfd, FALSE, syms, symcount);
276 if (shandle == NULL)
277 return FALSE;
280 *pfound = TRUE;
282 s = i.name;
283 f = NULL;
284 while (s[strlen (s) - 1] == '\\'
285 && ps + 1 < symend)
287 char *sc, *n;
289 ++ps;
290 sc = xstrdup (s);
291 sc[strlen (sc) - 1] = '\0';
292 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
293 free (sc);
294 if (f != NULL)
295 free (f);
296 f = n;
297 s = n;
300 save_stab (i.stab_type, i.stab_desc, i.value, s);
302 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
303 i.value, s))
305 stab_context ();
306 free_saved_stabs ();
307 return FALSE;
310 /* Don't free f, since I think the stabs code expects
311 strings to hang around. This should be straightened out.
312 FIXME. */
316 free_saved_stabs ();
318 if (shandle != NULL)
320 if (! finish_stab (dhandle, shandle))
321 return FALSE;
324 return TRUE;
327 /* Read IEEE debugging information. */
329 static bfd_boolean
330 read_ieee_debugging_info (bfd *abfd, void *dhandle, bfd_boolean *pfound)
332 asection *dsec;
333 bfd_size_type size;
334 bfd_byte *contents;
336 /* The BFD backend puts the debugging information into a section
337 named .debug. */
339 dsec = bfd_get_section_by_name (abfd, ".debug");
340 if (dsec == NULL)
341 return TRUE;
343 size = bfd_section_size (abfd, dsec);
344 contents = (bfd_byte *) xmalloc (size);
345 if (! bfd_get_section_contents (abfd, dsec, contents, 0, size))
346 return FALSE;
348 if (! parse_ieee (dhandle, abfd, contents, size))
349 return FALSE;
351 free (contents);
353 *pfound = TRUE;
355 return TRUE;
358 /* Record stabs strings, so that we can give some context for errors. */
360 #define SAVE_STABS_COUNT (16)
362 struct saved_stab
364 int type;
365 int desc;
366 bfd_vma value;
367 char *string;
370 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
371 static int saved_stabs_index;
373 /* Save a stabs string. */
375 static void
376 save_stab (int type, int desc, bfd_vma value, const char *string)
378 if (saved_stabs[saved_stabs_index].string != NULL)
379 free (saved_stabs[saved_stabs_index].string);
380 saved_stabs[saved_stabs_index].type = type;
381 saved_stabs[saved_stabs_index].desc = desc;
382 saved_stabs[saved_stabs_index].value = value;
383 saved_stabs[saved_stabs_index].string = xstrdup (string);
384 saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
387 /* Provide context for an error. */
389 static void
390 stab_context (void)
392 int i;
394 fprintf (stderr, _("Last stabs entries before error:\n"));
395 fprintf (stderr, "n_type n_desc n_value string\n");
397 i = saved_stabs_index;
400 struct saved_stab *stabp;
402 stabp = saved_stabs + i;
403 if (stabp->string != NULL)
405 const char *s;
407 s = bfd_get_stab_name (stabp->type);
408 if (s != NULL)
409 fprintf (stderr, "%-6s", s);
410 else if (stabp->type == 0)
411 fprintf (stderr, "HdrSym");
412 else
413 fprintf (stderr, "%-6d", stabp->type);
414 fprintf (stderr, " %-6d ", stabp->desc);
415 fprintf_vma (stderr, stabp->value);
416 if (stabp->type != 0)
417 fprintf (stderr, " %s", stabp->string);
418 fprintf (stderr, "\n");
420 i = (i + 1) % SAVE_STABS_COUNT;
422 while (i != saved_stabs_index);
425 /* Free the saved stab strings. */
427 static void
428 free_saved_stabs (void)
430 int i;
432 for (i = 0; i < SAVE_STABS_COUNT; i++)
434 if (saved_stabs[i].string != NULL)
436 free (saved_stabs[i].string);
437 saved_stabs[i].string = NULL;
441 saved_stabs_index = 0;