Remove sh5 and sh64 support
[deliverable/binutils-gdb.git] / binutils / rddbg.c
1 /* rddbg.c -- Read debugging information into a generic form.
2 Copyright (C) 1995-2018 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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
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 "sysdep.h"
28 #include "bfd.h"
29 #include "libiberty.h"
30 #include "bucomm.h"
31 #include "debug.h"
32 #include "budbg.h"
33
34 static bfd_boolean read_section_stabs_debugging_info
35 (bfd *, asymbol **, long, void *, bfd_boolean *);
36 static bfd_boolean read_symbol_stabs_debugging_info
37 (bfd *, asymbol **, long, 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);
41
42 /* Read debugging information from a BFD. Returns a generic debugging
43 pointer. */
44
45 void *
46 read_debugging_info (bfd *abfd, asymbol **syms, long symcount, bfd_boolean no_messages)
47 {
48 void *dhandle;
49 bfd_boolean found;
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 /* Try reading the COFF symbols if we didn't find any stabs in COFF
67 sections. */
68 if (! found
69 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
70 && symcount > 0)
71 {
72 if (! parse_coff (abfd, syms, symcount, dhandle))
73 return NULL;
74 found = TRUE;
75 }
76
77 if (! found)
78 {
79 if (! no_messages)
80 non_fatal (_("%s: no recognized debugging information"),
81 bfd_get_filename (abfd));
82 return NULL;
83 }
84
85 return dhandle;
86 }
87
88 /* Read stabs in sections debugging information from a BFD. */
89
90 static bfd_boolean
91 read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
92 void *dhandle, bfd_boolean *pfound)
93 {
94 static struct
95 {
96 const char *secname;
97 const char *strsecname;
98 }
99 names[] =
100 {
101 { ".stab", ".stabstr" },
102 { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" },
103 { "$GDB_SYMBOLS$", "$GDB_STRINGS$" }
104 };
105 unsigned int i;
106 void *shandle;
107
108 *pfound = FALSE;
109 shandle = NULL;
110
111 for (i = 0; i < sizeof names / sizeof names[0]; i++)
112 {
113 asection *sec, *strsec;
114
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)
118 {
119 bfd_size_type stabsize, strsize;
120 bfd_byte *stabs, *strings;
121 bfd_byte *stab;
122 bfd_size_type stroff, next_stroff;
123
124 stabsize = bfd_section_size (abfd, sec);
125 stabs = (bfd_byte *) xmalloc (stabsize);
126 if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
127 {
128 fprintf (stderr, "%s: %s: %s\n",
129 bfd_get_filename (abfd), names[i].secname,
130 bfd_errmsg (bfd_get_error ()));
131 return FALSE;
132 }
133
134 strsize = bfd_section_size (abfd, strsec);
135 strings = (bfd_byte *) xmalloc (strsize + 1);
136 if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
137 {
138 fprintf (stderr, "%s: %s: %s\n",
139 bfd_get_filename (abfd), names[i].strsecname,
140 bfd_errmsg (bfd_get_error ()));
141 return FALSE;
142 }
143 /* Zero terminate the strings table, just in case. */
144 strings [strsize] = 0;
145 if (shandle == NULL)
146 {
147 shandle = start_stab (dhandle, abfd, TRUE, syms, symcount);
148 if (shandle == NULL)
149 return FALSE;
150 }
151
152 *pfound = TRUE;
153
154 stroff = 0;
155 next_stroff = 0;
156 /* PR 17512: file: 078-60391-0.001:0.1. */
157 for (stab = stabs; stab <= (stabs + stabsize) - 12; stab += 12)
158 {
159 unsigned int strx;
160 int type;
161 int other ATTRIBUTE_UNUSED;
162 int desc;
163 bfd_vma value;
164
165 /* This code presumes 32 bit values. */
166
167 strx = bfd_get_32 (abfd, stab);
168 type = bfd_get_8 (abfd, stab + 4);
169 other = bfd_get_8 (abfd, stab + 5);
170 desc = bfd_get_16 (abfd, stab + 6);
171 value = bfd_get_32 (abfd, stab + 8);
172
173 if (type == 0)
174 {
175 /* Special type 0 stabs indicate the offset to the
176 next string table. */
177 stroff = next_stroff;
178 next_stroff += value;
179 }
180 else
181 {
182 size_t len;
183 char *f, *s;
184
185 if (stroff + strx >= strsize)
186 {
187 fprintf (stderr, _("%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n"),
188 bfd_get_filename (abfd), names[i].secname,
189 (long) (stab - stabs) / 12, strx, type);
190 continue;
191 }
192
193 s = (char *) strings + stroff + strx;
194 f = NULL;
195
196 /* PR 17512: file: 002-87578-0.001:0.1.
197 It is possible to craft a file where, without the 'strlen (s) > 0',
198 an attempt to read the byte before 'strings' would occur. */
199 while ((len = strlen (s)) > 0
200 && s[len - 1] == '\\'
201 && stab + 12 < stabs + stabsize)
202 {
203 char *p;
204
205 stab += 12;
206 p = s + len - 1;
207 *p = '\0';
208 strx = stroff + bfd_get_32 (abfd, stab);
209 if (strx >= strsize)
210 {
211 fprintf (stderr, _("%s: %s: stab entry %ld is corrupt\n"),
212 bfd_get_filename (abfd), names[i].secname,
213 (long) (stab - stabs) / 12);
214 break;
215 }
216 else
217 s = concat (s, (char *) strings + strx,
218 (const char *) NULL);
219
220 /* We have to restore the backslash, because, if
221 the linker is hashing stabs strings, we may
222 see the same string more than once. */
223 *p = '\\';
224
225 if (f != NULL)
226 free (f);
227 f = s;
228 }
229
230 save_stab (type, desc, value, s);
231
232 if (! parse_stab (dhandle, shandle, type, desc, value, s))
233 {
234 stab_context ();
235 free_saved_stabs ();
236 return FALSE;
237 }
238
239 /* Don't free f, since I think the stabs code
240 expects strings to hang around. This should be
241 straightened out. FIXME. */
242 }
243 }
244
245 free_saved_stabs ();
246 free (stabs);
247
248 /* Don't free strings, since I think the stabs code expects
249 the strings to hang around. This should be straightened
250 out. FIXME. */
251 }
252 }
253
254 if (shandle != NULL)
255 {
256 if (! finish_stab (dhandle, shandle))
257 return FALSE;
258 }
259
260 return TRUE;
261 }
262
263 /* Read stabs in the symbol table. */
264
265 static bfd_boolean
266 read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
267 void *dhandle, bfd_boolean *pfound)
268 {
269 void *shandle;
270 asymbol **ps, **symend;
271
272 shandle = NULL;
273 symend = syms + symcount;
274 for (ps = syms; ps < symend; ps++)
275 {
276 symbol_info i;
277
278 bfd_get_symbol_info (abfd, *ps, &i);
279
280 if (i.type == '-')
281 {
282 const char *s;
283 char *f;
284
285 if (shandle == NULL)
286 {
287 shandle = start_stab (dhandle, abfd, FALSE, syms, symcount);
288 if (shandle == NULL)
289 return FALSE;
290 }
291
292 *pfound = TRUE;
293
294 s = i.name;
295 if (s == NULL || strlen (s) < 1)
296 return FALSE;
297 f = NULL;
298
299 while (strlen (s) > 0
300 && s[strlen (s) - 1] == '\\'
301 && ps + 1 < symend)
302 {
303 char *sc, *n;
304
305 ++ps;
306 sc = xstrdup (s);
307 sc[strlen (sc) - 1] = '\0';
308 n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
309 free (sc);
310 if (f != NULL)
311 free (f);
312 f = n;
313 s = n;
314 }
315
316 save_stab (i.stab_type, i.stab_desc, i.value, s);
317
318 if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
319 i.value, s))
320 {
321 stab_context ();
322 free_saved_stabs ();
323 return FALSE;
324 }
325
326 /* Don't free f, since I think the stabs code expects
327 strings to hang around. This should be straightened out.
328 FIXME. */
329 }
330 }
331
332 free_saved_stabs ();
333
334 if (shandle != NULL)
335 {
336 if (! finish_stab (dhandle, shandle))
337 return FALSE;
338 }
339
340 return TRUE;
341 }
342 \f
343 /* Record stabs strings, so that we can give some context for errors. */
344
345 #define SAVE_STABS_COUNT (16)
346
347 struct saved_stab
348 {
349 int type;
350 int desc;
351 bfd_vma value;
352 char *string;
353 };
354
355 static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
356 static int saved_stabs_index;
357
358 /* Save a stabs string. */
359
360 static void
361 save_stab (int type, int desc, bfd_vma value, const char *string)
362 {
363 if (saved_stabs[saved_stabs_index].string != NULL)
364 free (saved_stabs[saved_stabs_index].string);
365 saved_stabs[saved_stabs_index].type = type;
366 saved_stabs[saved_stabs_index].desc = desc;
367 saved_stabs[saved_stabs_index].value = value;
368 saved_stabs[saved_stabs_index].string = xstrdup (string);
369 saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
370 }
371
372 /* Provide context for an error. */
373
374 static void
375 stab_context (void)
376 {
377 int i;
378
379 fprintf (stderr, _("Last stabs entries before error:\n"));
380 fprintf (stderr, "n_type n_desc n_value string\n");
381
382 i = saved_stabs_index;
383 do
384 {
385 struct saved_stab *stabp;
386
387 stabp = saved_stabs + i;
388 if (stabp->string != NULL)
389 {
390 const char *s;
391
392 s = bfd_get_stab_name (stabp->type);
393 if (s != NULL)
394 fprintf (stderr, "%-6s", s);
395 else if (stabp->type == 0)
396 fprintf (stderr, "HdrSym");
397 else
398 fprintf (stderr, "%-6d", stabp->type);
399 fprintf (stderr, " %-6d ", stabp->desc);
400 fprintf_vma (stderr, stabp->value);
401 if (stabp->type != 0)
402 fprintf (stderr, " %s", stabp->string);
403 fprintf (stderr, "\n");
404 }
405 i = (i + 1) % SAVE_STABS_COUNT;
406 }
407 while (i != saved_stabs_index);
408 }
409
410 /* Free the saved stab strings. */
411
412 static void
413 free_saved_stabs (void)
414 {
415 int i;
416
417 for (i = 0; i < SAVE_STABS_COUNT; i++)
418 {
419 if (saved_stabs[i].string != NULL)
420 {
421 free (saved_stabs[i].string);
422 saved_stabs[i].string = NULL;
423 }
424 }
425
426 saved_stabs_index = 0;
427 }
This page took 0.049115 seconds and 4 git commands to generate.