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