]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/dwarf1.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / dwarf1.c
CommitLineData
252b5132 1/* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
a2c58332 2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
252b5132 3
116c20d2 4 Written by Gavin Romig-Koch of Cygnus Solutions (gavin@cygnus.com).
252b5132 5
116c20d2 6 This file is part of BFD.
252b5132 7
116c20d2
NC
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
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or (at
116c20d2 11 your option) any later version.
252b5132 12
116c20d2
NC
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
252b5132 17
116c20d2
NC
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
cd123cb7
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
252b5132 22
252b5132 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
252b5132
RH
25#include "libiberty.h"
26#include "libbfd.h"
27#include "elf-bfd.h"
28#include "elf/dwarf.h"
29
98591c73 30/* dwarf1_debug is the starting point for all dwarf1 info. */
252b5132 31
116c20d2
NC
32struct dwarf1_debug
33{
98591c73 34 /* The bfd we are working with. */
252b5132
RH
35 bfd* abfd;
36
ab1579fd
NS
37 /* Pointer to the symbol table. */
38 asymbol** syms;
39
98591c73 40 /* List of already parsed compilation units. */
252b5132
RH
41 struct dwarf1_unit* lastUnit;
42
98591c73
KH
43 /* The buffer for the .debug section.
44 Zero indicates that the .debug section failed to load. */
ab1579fd 45 bfd_byte *debug_section;
252b5132 46
98591c73 47 /* Pointer to the end of the .debug_info section memory buffer. */
ab1579fd 48 bfd_byte *debug_section_end;
252b5132 49
98591c73 50 /* The buffer for the .line section. */
ab1579fd 51 bfd_byte *line_section;
252b5132 52
98591c73 53 /* End of that buffer. */
ab1579fd 54 bfd_byte *line_section_end;
252b5132 55
98591c73 56 /* The current or next unread die within the .debug section. */
ab1579fd 57 bfd_byte *currentDie;
252b5132
RH
58};
59
98591c73 60/* One dwarf1_unit for each parsed compilation unit die. */
252b5132 61
116c20d2
NC
62struct dwarf1_unit
63{
98591c73 64 /* Linked starting from stash->lastUnit. */
252b5132
RH
65 struct dwarf1_unit* prev;
66
98591c73 67 /* Name of the compilation unit. */
ab1579fd 68 char *name;
252b5132 69
98591c73 70 /* The highest and lowest address used in the compilation unit. */
252b5132
RH
71 unsigned long low_pc;
72 unsigned long high_pc;
73
116c20d2 74 /* Does this unit have a statement list? */
252b5132
RH
75 int has_stmt_list;
76
98591c73 77 /* If any, the offset of the line number table in the .line section. */
252b5132
RH
78 unsigned long stmt_list_offset;
79
98591c73 80 /* If non-zero, a pointer to the first child of this unit. */
ab1579fd 81 bfd_byte *first_child;
252b5132 82
116c20d2 83 /* How many line entries? */
252b5132
RH
84 unsigned long line_count;
85
98591c73 86 /* The decoded line number table (line_count entries). */
252b5132
RH
87 struct linenumber* linenumber_table;
88
98591c73 89 /* The list of functions in this unit. */
252b5132
RH
90 struct dwarf1_func* func_list;
91};
92
252b5132
RH
93/* One dwarf1_func for each parsed function die. */
94
116c20d2
NC
95struct dwarf1_func
96{
98591c73 97 /* Linked starting from aUnit->func_list. */
252b5132 98 struct dwarf1_func* prev;
98591c73
KH
99
100 /* Name of function. */
252b5132 101 char* name;
98591c73
KH
102
103 /* The highest and lowest address used in the compilation unit. */
252b5132
RH
104 unsigned long low_pc;
105 unsigned long high_pc;
106};
107
98591c73 108/* Used to return info about a parsed die. */
116c20d2
NC
109struct die_info
110{
252b5132
RH
111 unsigned long length;
112 unsigned long sibling;
113 unsigned long low_pc;
114 unsigned long high_pc;
115 unsigned long stmt_list_offset;
98591c73
KH
116
117 char* name;
118
252b5132
RH
119 int has_stmt_list;
120
121 unsigned short tag;
122};
123
98591c73 124/* Parsed line number information. */
116c20d2
NC
125struct linenumber
126{
98591c73 127 /* First address in the line. */
252b5132
RH
128 unsigned long addr;
129
98591c73 130 /* The line number. */
252b5132
RH
131 unsigned long linenumber;
132};
133
98591c73 134/* Find the form of an attr, from the attr field. */
116c20d2 135#define FORM_FROM_ATTR(attr) ((attr) & 0xF) /* Implicitly specified. */
a7b97311 136
252b5132 137/* Return a newly allocated dwarf1_unit. It should be cleared and
98591c73 138 then attached into the 'stash' at 'stash->lastUnit'. */
252b5132
RH
139
140static struct dwarf1_unit*
116c20d2 141alloc_dwarf1_unit (struct dwarf1_debug* stash)
252b5132 142{
986f0783 143 size_t amt = sizeof (struct dwarf1_unit);
dc810e39 144
a50b1753 145 struct dwarf1_unit* x = (struct dwarf1_unit *) bfd_zalloc (stash->abfd, amt);
ab1579fd
NS
146 if (x)
147 {
148 x->prev = stash->lastUnit;
149 stash->lastUnit = x;
150 }
252b5132
RH
151
152 return x;
153}
154
155/* Return a newly allocated dwarf1_func. It must be cleared and
98591c73 156 attached into 'aUnit' at 'aUnit->func_list'. */
252b5132 157
116c20d2
NC
158static struct dwarf1_func *
159alloc_dwarf1_func (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
252b5132 160{
986f0783 161 size_t amt = sizeof (struct dwarf1_func);
dc810e39 162
a50b1753 163 struct dwarf1_func* x = (struct dwarf1_func *) bfd_zalloc (stash->abfd, amt);
ab1579fd
NS
164 if (x)
165 {
166 x->prev = aUnit->func_list;
167 aUnit->func_list = x;
168 }
98591c73 169
252b5132
RH
170 return x;
171}
172
173/* parse_die - parse a Dwarf1 die.
174 Parse the die starting at 'aDiePtr' into 'aDieInfo'.
175 'abfd' must be the bfd from which the section that 'aDiePtr'
98591c73 176 points to was pulled from.
252b5132 177
b34976b6 178 Return FALSE if the die is invalidly formatted; TRUE otherwise. */
252b5132 179
0a1b45a2 180static bool
07d6d2b8 181parse_die (bfd * abfd,
116c20d2 182 struct die_info * aDieInfo,
07d6d2b8
AM
183 bfd_byte * aDiePtr,
184 bfd_byte * aDiePtrEnd)
252b5132 185{
ab1579fd
NS
186 bfd_byte *this_die = aDiePtr;
187 bfd_byte *xptr = this_die;
252b5132 188
116c20d2 189 memset (aDieInfo, 0, sizeof (* aDieInfo));
252b5132 190
98591c73 191 /* First comes the length. */
1da5c9a4 192 if (xptr + 4 > aDiePtrEnd)
0a1b45a2 193 return false;
1da5c9a4 194 aDieInfo->length = bfd_get_32 (abfd, xptr);
252b5132 195 xptr += 4;
05f62e0c
AM
196 if (aDieInfo->length <= 4
197 || (size_t) (aDiePtrEnd - this_die) < aDieInfo->length)
0a1b45a2 198 return false;
1da5c9a4 199 aDiePtrEnd = this_die + aDieInfo->length;
252b5132
RH
200 if (aDieInfo->length < 6)
201 {
98591c73 202 /* Just padding bytes. */
252b5132 203 aDieInfo->tag = TAG_padding;
0a1b45a2 204 return true;
252b5132
RH
205 }
206
98591c73 207 /* Then the tag. */
1da5c9a4 208 if (xptr + 2 > aDiePtrEnd)
0a1b45a2 209 return false;
1da5c9a4 210 aDieInfo->tag = bfd_get_16 (abfd, xptr);
252b5132 211 xptr += 2;
98591c73
KH
212
213 /* Then the attributes. */
1da5c9a4 214 while (xptr + 2 <= aDiePtrEnd)
252b5132 215 {
eef10466 216 unsigned int block_len;
252b5132 217 unsigned short attr;
98591c73
KH
218
219 /* Parse the attribute based on its form. This section
07d6d2b8 220 must handle all dwarf1 forms, but need only handle the
98591c73 221 actual attributes that we care about. */
1da5c9a4 222 attr = bfd_get_16 (abfd, xptr);
252b5132 223 xptr += 2;
98591c73 224
252b5132
RH
225 switch (FORM_FROM_ATTR (attr))
226 {
227 case FORM_DATA2:
228 xptr += 2;
229 break;
230 case FORM_DATA4:
231 case FORM_REF:
1da5c9a4 232 if (xptr + 4 <= aDiePtrEnd)
252b5132 233 {
1da5c9a4
AM
234 if (attr == AT_sibling)
235 aDieInfo->sibling = bfd_get_32 (abfd, xptr);
236 else if (attr == AT_stmt_list)
237 {
238 aDieInfo->stmt_list_offset = bfd_get_32 (abfd, xptr);
239 aDieInfo->has_stmt_list = 1;
240 }
252b5132
RH
241 }
242 xptr += 4;
243 break;
244 case FORM_DATA8:
245 xptr += 8;
246 break;
247 case FORM_ADDR:
1da5c9a4
AM
248 if (xptr + 4 <= aDiePtrEnd)
249 {
250 if (attr == AT_low_pc)
251 aDieInfo->low_pc = bfd_get_32 (abfd, xptr);
252 else if (attr == AT_high_pc)
253 aDieInfo->high_pc = bfd_get_32 (abfd, xptr);
254 }
252b5132
RH
255 xptr += 4;
256 break;
257 case FORM_BLOCK2:
1da5c9a4 258 if (xptr + 2 <= aDiePtrEnd)
eef10466
NC
259 {
260 block_len = bfd_get_16 (abfd, xptr);
05f62e0c 261 if ((size_t) (aDiePtrEnd - xptr) < block_len)
0a1b45a2 262 return false;
eef10466
NC
263 xptr += block_len;
264 }
1da5c9a4 265 xptr += 2;
252b5132
RH
266 break;
267 case FORM_BLOCK4:
1da5c9a4 268 if (xptr + 4 <= aDiePtrEnd)
eef10466
NC
269 {
270 block_len = bfd_get_32 (abfd, xptr);
05f62e0c 271 if ((size_t) (aDiePtrEnd - xptr) < block_len)
0a1b45a2 272 return false;
eef10466
NC
273 xptr += block_len;
274 }
1da5c9a4 275 xptr += 4;
252b5132
RH
276 break;
277 case FORM_STRING:
278 if (attr == AT_name)
20b1d3e3 279 aDieInfo->name = (char *) xptr;
1da5c9a4 280 xptr += strnlen ((char *) xptr, aDiePtrEnd - xptr) + 1;
252b5132
RH
281 break;
282 }
283 }
284
0a1b45a2 285 return true;
252b5132
RH
286}
287
288/* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
b34976b6
AM
289 into 'aUnit->linenumber_table'. Return FALSE if an error
290 occurs; TRUE otherwise. */
98591c73 291
0a1b45a2 292static bool
116c20d2 293parse_line_table (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
252b5132 294{
ab1579fd 295 bfd_byte *xptr;
252b5132 296
98591c73 297 /* Load the ".line" section from the bfd if we haven't already. */
252b5132
RH
298 if (stash->line_section == 0)
299 {
300 asection *msec;
dc810e39 301 bfd_size_type size;
98591c73 302
252b5132
RH
303 msec = bfd_get_section_by_name (stash->abfd, ".line");
304 if (! msec)
0a1b45a2 305 return false;
98591c73 306
eea6121a 307 size = msec->rawsize ? msec->rawsize : msec->size;
ab1579fd
NS
308 stash->line_section
309 = bfd_simple_get_relocated_section_contents
310 (stash->abfd, msec, NULL, stash->syms);
98591c73 311
252b5132 312 if (! stash->line_section)
0a1b45a2 313 return false;
252b5132 314
252b5132
RH
315 stash->line_section_end = stash->line_section + size;
316 }
317
318 xptr = stash->line_section + aUnit->stmt_list_offset;
1da5c9a4 319 if (xptr + 8 <= stash->line_section_end)
252b5132 320 {
7442e600 321 unsigned long eachLine;
ab1579fd 322 bfd_byte *tblend;
252b5132 323 unsigned long base;
dc810e39 324 bfd_size_type amt;
252b5132 325
98591c73 326 /* First comes the length. */
b7af50e3 327 tblend = bfd_get_32 (stash->abfd, (bfd_byte *) xptr) + xptr;
252b5132
RH
328 xptr += 4;
329
98591c73 330 /* Then the base address for each address in the table. */
b7af50e3 331 base = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
332 xptr += 4;
333
334 /* How many line entrys?
116c20d2 335 10 = 4 (line number) + 2 (pos in line) + 4 (address in line). */
252b5132
RH
336 aUnit->line_count = (tblend - xptr) / 10;
337
98591c73 338 /* Allocate an array for the entries. */
dc810e39 339 amt = sizeof (struct linenumber) * aUnit->line_count;
a50b1753 340 aUnit->linenumber_table = (struct linenumber *) bfd_alloc (stash->abfd,
07d6d2b8 341 amt);
ab1579fd 342 if (!aUnit->linenumber_table)
0a1b45a2 343 return false;
98591c73 344
252b5132
RH
345 for (eachLine = 0; eachLine < aUnit->line_count; eachLine++)
346 {
1da5c9a4
AM
347 if (xptr + 10 > stash->line_section_end)
348 {
349 aUnit->line_count = eachLine;
350 break;
351 }
98591c73 352 /* A line number. */
252b5132 353 aUnit->linenumber_table[eachLine].linenumber
b7af50e3 354 = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
355 xptr += 4;
356
98591c73 357 /* Skip the position within the line. */
252b5132
RH
358 xptr += 2;
359
98591c73
KH
360 /* And finally the address. */
361 aUnit->linenumber_table[eachLine].addr
b7af50e3 362 = base + bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
363 xptr += 4;
364 }
365 }
366
0a1b45a2 367 return true;
252b5132
RH
368}
369
370/* Parse each function die in a compilation unit 'aUnit'.
371 The first child die of 'aUnit' should be in 'aUnit->first_child',
372 the result is placed in 'aUnit->func_list'.
b34976b6 373 Return FALSE if error; TRUE otherwise. */
252b5132 374
0a1b45a2 375static bool
116c20d2 376parse_functions_in_unit (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
252b5132 377{
ab1579fd 378 bfd_byte *eachDie;
252b5132
RH
379
380 if (aUnit->first_child)
381 for (eachDie = aUnit->first_child;
07d6d2b8 382 eachDie < stash->debug_section_end;
252b5132
RH
383 )
384 {
385 struct die_info eachDieInfo;
98591c73 386
bb731fb6
L
387 if (! parse_die (stash->abfd, &eachDieInfo, eachDie,
388 stash->debug_section_end))
0a1b45a2 389 return false;
98591c73 390
252b5132
RH
391 if (eachDieInfo.tag == TAG_global_subroutine
392 || eachDieInfo.tag == TAG_subroutine
393 || eachDieInfo.tag == TAG_inlined_subroutine
394 || eachDieInfo.tag == TAG_entry_point)
395 {
396 struct dwarf1_func* aFunc = alloc_dwarf1_func (stash,aUnit);
ab1579fd 397 if (!aFunc)
0a1b45a2 398 return false;
98591c73 399
252b5132
RH
400 aFunc->name = eachDieInfo.name;
401 aFunc->low_pc = eachDieInfo.low_pc;
402 aFunc->high_pc = eachDieInfo.high_pc;
403 }
98591c73 404
252b5132
RH
405 /* Move to next sibling, if none, end loop */
406 if (eachDieInfo.sibling)
407 eachDie = stash->debug_section + eachDieInfo.sibling;
408 else
409 break;
410 }
98591c73 411
0a1b45a2 412 return true;
252b5132
RH
413}
414
415/* Find the nearest line to 'addr' in 'aUnit'.
98591c73 416 Return whether we found the line (or a function) without error. */
252b5132 417
0a1b45a2 418static bool
116c20d2
NC
419dwarf1_unit_find_nearest_line (struct dwarf1_debug* stash,
420 struct dwarf1_unit* aUnit,
421 unsigned long addr,
422 const char **filename_ptr,
423 const char **functionname_ptr,
424 unsigned int *linenumber_ptr)
252b5132 425{
0a1b45a2
AM
426 int line_p = false;
427 int func_p = false;
252b5132
RH
428
429 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
430 {
431 if (aUnit->has_stmt_list)
432 {
7442e600 433 unsigned long i;
252b5132
RH
434 struct dwarf1_func* eachFunc;
435
436 if (! aUnit->linenumber_table)
437 {
438 if (! parse_line_table (stash, aUnit))
0a1b45a2 439 return false;
252b5132
RH
440 }
441
442 if (! aUnit->func_list)
443 {
444 if (! parse_functions_in_unit (stash, aUnit))
0a1b45a2 445 return false;
252b5132
RH
446 }
447
448 for (i = 0; i < aUnit->line_count; i++)
449 {
450 if (aUnit->linenumber_table[i].addr <= addr
451 && addr < aUnit->linenumber_table[i+1].addr)
452 {
453 *filename_ptr = aUnit->name;
454 *linenumber_ptr = aUnit->linenumber_table[i].linenumber;
0a1b45a2 455 line_p = true;
252b5132
RH
456 break;
457 }
458 }
459
98591c73
KH
460 for (eachFunc = aUnit->func_list;
461 eachFunc;
252b5132
RH
462 eachFunc = eachFunc->prev)
463 {
464 if (eachFunc->low_pc <= addr
465 && addr < eachFunc->high_pc)
466 {
467 *functionname_ptr = eachFunc->name;
0a1b45a2 468 func_p = true;
252b5132
RH
469 break;
470 }
471 }
472 }
473 }
474
475 return line_p || func_p;
476}
477
252b5132 478/* The DWARF 1 version of find_nearest line.
b34976b6 479 Return TRUE if the line is found without error. */
252b5132 480
0a1b45a2 481bool
116c20d2 482_bfd_dwarf1_find_nearest_line (bfd *abfd,
ab1579fd 483 asymbol **symbols,
fb167eb2 484 asection *section,
116c20d2
NC
485 bfd_vma offset,
486 const char **filename_ptr,
487 const char **functionname_ptr,
488 unsigned int *linenumber_ptr)
252b5132
RH
489{
490 struct dwarf1_debug *stash = elf_tdata (abfd)->dwarf1_find_line_info;
491
492 struct dwarf1_unit* eachUnit;
493
494 /* What address are we looking for? */
1548c54f 495 unsigned long addr = (unsigned long)(offset + section->vma);
252b5132
RH
496
497 *filename_ptr = NULL;
498 *functionname_ptr = NULL;
499 *linenumber_ptr = 0;
252b5132
RH
500
501 if (! stash)
502 {
503 asection *msec;
dc810e39 504 bfd_size_type size = sizeof (struct dwarf1_debug);
98591c73 505
dc810e39 506 stash = elf_tdata (abfd)->dwarf1_find_line_info
a50b1753 507 = (struct dwarf1_debug *) bfd_zalloc (abfd, size);
98591c73 508
252b5132 509 if (! stash)
0a1b45a2 510 return false;
98591c73 511
252b5132
RH
512 msec = bfd_get_section_by_name (abfd, ".debug");
513 if (! msec)
116c20d2
NC
514 /* No dwarf1 info. Note that at this point the stash
515 has been allocated, but contains zeros, this lets
516 future calls to this function fail quicker. */
0a1b45a2 517 return false;
252b5132 518
eea6121a 519 size = msec->rawsize ? msec->rawsize : msec->size;
ab1579fd
NS
520 stash->debug_section
521 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
522 symbols);
98591c73 523
252b5132 524 if (! stash->debug_section)
0a1b45a2 525 return false;
252b5132 526
252b5132
RH
527 stash->debug_section_end = stash->debug_section + size;
528 stash->currentDie = stash->debug_section;
529 stash->abfd = abfd;
ab1579fd 530 stash->syms = symbols;
252b5132
RH
531 }
532
533 /* A null debug_section indicates that there was no dwarf1 info
98591c73 534 or that an error occured while setting up the stash. */
252b5132
RH
535
536 if (! stash->debug_section)
0a1b45a2 537 return false;
252b5132
RH
538
539 /* Look at the previously parsed units to see if any contain
98591c73 540 the addr. */
252b5132 541 for (eachUnit = stash->lastUnit; eachUnit; eachUnit = eachUnit->prev)
116c20d2
NC
542 if (eachUnit->low_pc <= addr && addr < eachUnit->high_pc)
543 return dwarf1_unit_find_nearest_line (stash, eachUnit, addr,
544 filename_ptr,
545 functionname_ptr,
546 linenumber_ptr);
252b5132
RH
547
548 while (stash->currentDie < stash->debug_section_end)
549 {
550 struct die_info aDieInfo;
551
bb731fb6
L
552 if (! parse_die (stash->abfd, &aDieInfo, stash->currentDie,
553 stash->debug_section_end))
0a1b45a2 554 return false;
98591c73 555
252b5132
RH
556 if (aDieInfo.tag == TAG_compile_unit)
557 {
558 struct dwarf1_unit* aUnit
559 = alloc_dwarf1_unit (stash);
ab1579fd 560 if (!aUnit)
0a1b45a2 561 return false;
98591c73 562
252b5132
RH
563 aUnit->name = aDieInfo.name;
564 aUnit->low_pc = aDieInfo.low_pc;
565 aUnit->high_pc = aDieInfo.high_pc;
566 aUnit->has_stmt_list = aDieInfo.has_stmt_list;
567 aUnit->stmt_list_offset = aDieInfo.stmt_list_offset;
98591c73 568
252b5132 569 /* A die has a child if it's followed by a die that is
98591c73
KH
570 not it's sibling. */
571 if (aDieInfo.sibling
572 && stash->currentDie + aDieInfo.length
07d6d2b8 573 < stash->debug_section_end
98591c73 574 && stash->currentDie + aDieInfo.length
07d6d2b8 575 != stash->debug_section + aDieInfo.sibling)
252b5132
RH
576 aUnit->first_child = stash->currentDie + aDieInfo.length;
577 else
578 aUnit->first_child = 0;
579
580 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
98591c73
KH
581 return dwarf1_unit_find_nearest_line (stash, aUnit, addr,
582 filename_ptr,
583 functionname_ptr,
252b5132
RH
584 linenumber_ptr);
585 }
98591c73 586
252b5132
RH
587 if (aDieInfo.sibling != 0)
588 stash->currentDie = stash->debug_section + aDieInfo.sibling;
589 else
590 stash->currentDie += aDieInfo.length;
591 }
592
0a1b45a2 593 return false;
252b5132 594}