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