Coverage Report

Created: 2025-06-24 06:45

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