1 /* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
4 Written by Gavin Romig-Koch of Cygnus Solutions (gavin@cygnus.com).
6 This file is part of BFD.
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
10 the Free Software Foundation; either version 3 of the License, or (at
11 your option) any later version.
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.
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
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 #include "libiberty.h"
28 #include "elf/dwarf.h"
30 /* dwarf1_debug is the starting point for all dwarf1 info. */
34 /* The bfd we are working with. */
37 /* Pointer to the symbol table. */
40 /* List of already parsed compilation units. */
41 struct dwarf1_unit
* lastUnit
;
43 /* The buffer for the .debug section.
44 Zero indicates that the .debug section failed to load. */
45 bfd_byte
*debug_section
;
47 /* Pointer to the end of the .debug_info section memory buffer. */
48 bfd_byte
*debug_section_end
;
50 /* The buffer for the .line section. */
51 bfd_byte
*line_section
;
53 /* End of that buffer. */
54 bfd_byte
*line_section_end
;
56 /* The current or next unread die within the .debug section. */
60 /* One dwarf1_unit for each parsed compilation unit die. */
64 /* Linked starting from stash->lastUnit. */
65 struct dwarf1_unit
* prev
;
67 /* Name of the compilation unit. */
70 /* The highest and lowest address used in the compilation unit. */
72 unsigned long high_pc
;
74 /* Does this unit have a statement list? */
77 /* If any, the offset of the line number table in the .line section. */
78 unsigned long stmt_list_offset
;
80 /* If non-zero, a pointer to the first child of this unit. */
81 bfd_byte
*first_child
;
83 /* How many line entries? */
84 unsigned long line_count
;
86 /* The decoded line number table (line_count entries). */
87 struct linenumber
* linenumber_table
;
89 /* The list of functions in this unit. */
90 struct dwarf1_func
* func_list
;
93 /* One dwarf1_func for each parsed function die. */
97 /* Linked starting from aUnit->func_list. */
98 struct dwarf1_func
* prev
;
100 /* Name of function. */
103 /* The highest and lowest address used in the compilation unit. */
104 unsigned long low_pc
;
105 unsigned long high_pc
;
108 /* Used to return info about a parsed die. */
111 unsigned long length
;
112 unsigned long sibling
;
113 unsigned long low_pc
;
114 unsigned long high_pc
;
115 unsigned long stmt_list_offset
;
124 /* Parsed line number information. */
127 /* First address in the line. */
130 /* The line number. */
131 unsigned long linenumber
;
134 /* Find the form of an attr, from the attr field. */
135 #define FORM_FROM_ATTR(attr) ((attr) & 0xF) /* Implicitly specified. */
137 /* Return a newly allocated dwarf1_unit. It should be cleared and
138 then attached into the 'stash' at 'stash->lastUnit'. */
140 static struct dwarf1_unit
*
141 alloc_dwarf1_unit (struct dwarf1_debug
* stash
)
143 size_t amt
= sizeof (struct dwarf1_unit
);
145 struct dwarf1_unit
* x
= (struct dwarf1_unit
*) bfd_zalloc (stash
->abfd
, amt
);
148 x
->prev
= stash
->lastUnit
;
155 /* Return a newly allocated dwarf1_func. It must be cleared and
156 attached into 'aUnit' at 'aUnit->func_list'. */
158 static struct dwarf1_func
*
159 alloc_dwarf1_func (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
161 size_t amt
= sizeof (struct dwarf1_func
);
163 struct dwarf1_func
* x
= (struct dwarf1_func
*) bfd_zalloc (stash
->abfd
, amt
);
166 x
->prev
= aUnit
->func_list
;
167 aUnit
->func_list
= x
;
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'
176 points to was pulled from.
178 Return FALSE if the die is invalidly formatted; TRUE otherwise. */
181 parse_die (bfd
* abfd
,
182 struct die_info
* aDieInfo
,
184 bfd_byte
* aDiePtrEnd
)
186 bfd_byte
*this_die
= aDiePtr
;
187 bfd_byte
*xptr
= this_die
;
189 memset (aDieInfo
, 0, sizeof (* aDieInfo
));
191 /* First comes the length. */
192 if (xptr
+ 4 > aDiePtrEnd
)
194 aDieInfo
->length
= bfd_get_32 (abfd
, xptr
);
196 if (aDieInfo
->length
<= 4
197 || (size_t) (aDiePtrEnd
- this_die
) < aDieInfo
->length
)
199 aDiePtrEnd
= this_die
+ aDieInfo
->length
;
200 if (aDieInfo
->length
< 6)
202 /* Just padding bytes. */
203 aDieInfo
->tag
= TAG_padding
;
208 if (xptr
+ 2 > aDiePtrEnd
)
210 aDieInfo
->tag
= bfd_get_16 (abfd
, xptr
);
213 /* Then the attributes. */
214 while (xptr
+ 2 <= aDiePtrEnd
)
216 unsigned int block_len
;
219 /* Parse the attribute based on its form. This section
220 must handle all dwarf1 forms, but need only handle the
221 actual attributes that we care about. */
222 attr
= bfd_get_16 (abfd
, xptr
);
225 switch (FORM_FROM_ATTR (attr
))
232 if (xptr
+ 4 <= aDiePtrEnd
)
234 if (attr
== AT_sibling
)
235 aDieInfo
->sibling
= bfd_get_32 (abfd
, xptr
);
236 else if (attr
== AT_stmt_list
)
238 aDieInfo
->stmt_list_offset
= bfd_get_32 (abfd
, xptr
);
239 aDieInfo
->has_stmt_list
= 1;
248 if (xptr
+ 4 <= aDiePtrEnd
)
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
);
258 if (xptr
+ 2 <= aDiePtrEnd
)
260 block_len
= bfd_get_16 (abfd
, xptr
);
261 if ((size_t) (aDiePtrEnd
- xptr
) < block_len
)
268 if (xptr
+ 4 <= aDiePtrEnd
)
270 block_len
= bfd_get_32 (abfd
, xptr
);
271 if ((size_t) (aDiePtrEnd
- xptr
) < block_len
)
279 aDieInfo
->name
= (char *) xptr
;
280 xptr
+= strnlen ((char *) xptr
, aDiePtrEnd
- xptr
) + 1;
288 /* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
289 into 'aUnit->linenumber_table'. Return FALSE if an error
290 occurs; TRUE otherwise. */
293 parse_line_table (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
297 /* Load the ".line" section from the bfd if we haven't already. */
298 if (stash
->line_section
== 0)
303 msec
= bfd_get_section_by_name (stash
->abfd
, ".line");
304 if (! msec
|| (msec
->flags
& SEC_HAS_CONTENTS
) == 0)
307 size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
309 = bfd_simple_get_relocated_section_contents (stash
->abfd
, msec
, NULL
,
312 if (! stash
->line_section
)
315 stash
->line_section_end
= stash
->line_section
+ size
;
318 xptr
= stash
->line_section
+ aUnit
->stmt_list_offset
;
319 if (xptr
+ 8 <= stash
->line_section_end
)
321 unsigned long eachLine
;
326 /* First comes the length. */
327 tblend
= bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
) + xptr
;
330 /* Then the base address for each address in the table. */
331 base
= bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
334 /* How many line entrys?
335 10 = 4 (line number) + 2 (pos in line) + 4 (address in line). */
336 aUnit
->line_count
= (tblend
- xptr
) / 10;
338 /* Allocate an array for the entries. */
339 amt
= sizeof (struct linenumber
) * aUnit
->line_count
;
340 aUnit
->linenumber_table
= (struct linenumber
*) bfd_alloc (stash
->abfd
,
342 if (!aUnit
->linenumber_table
)
345 for (eachLine
= 0; eachLine
< aUnit
->line_count
; eachLine
++)
347 if (xptr
+ 10 > stash
->line_section_end
)
349 aUnit
->line_count
= eachLine
;
353 aUnit
->linenumber_table
[eachLine
].linenumber
354 = bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
357 /* Skip the position within the line. */
360 /* And finally the address. */
361 aUnit
->linenumber_table
[eachLine
].addr
362 = base
+ bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
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'.
373 Return FALSE if error; TRUE otherwise. */
376 parse_functions_in_unit (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
380 if (aUnit
->first_child
)
381 for (eachDie
= aUnit
->first_child
;
382 eachDie
< stash
->debug_section_end
;
385 struct die_info eachDieInfo
;
387 if (! parse_die (stash
->abfd
, &eachDieInfo
, eachDie
,
388 stash
->debug_section_end
))
391 if (eachDieInfo
.tag
== TAG_global_subroutine
392 || eachDieInfo
.tag
== TAG_subroutine
393 || eachDieInfo
.tag
== TAG_inlined_subroutine
394 || eachDieInfo
.tag
== TAG_entry_point
)
396 struct dwarf1_func
* aFunc
= alloc_dwarf1_func (stash
,aUnit
);
400 aFunc
->name
= eachDieInfo
.name
;
401 aFunc
->low_pc
= eachDieInfo
.low_pc
;
402 aFunc
->high_pc
= eachDieInfo
.high_pc
;
405 /* Move to next sibling, if none, end loop */
406 if (eachDieInfo
.sibling
)
407 eachDie
= stash
->debug_section
+ eachDieInfo
.sibling
;
415 /* Find the nearest line to 'addr' in 'aUnit'.
416 Return whether we found the line (or a function) without error. */
419 dwarf1_unit_find_nearest_line (struct dwarf1_debug
* stash
,
420 struct dwarf1_unit
* aUnit
,
422 const char **filename_ptr
,
423 const char **functionname_ptr
,
424 unsigned int *linenumber_ptr
)
429 if (aUnit
->low_pc
<= addr
&& addr
< aUnit
->high_pc
)
431 if (aUnit
->has_stmt_list
)
434 struct dwarf1_func
* eachFunc
;
436 if (! aUnit
->linenumber_table
)
438 if (! parse_line_table (stash
, aUnit
))
442 if (! aUnit
->func_list
)
444 if (! parse_functions_in_unit (stash
, aUnit
))
448 for (i
= 0; i
< aUnit
->line_count
; i
++)
450 if (aUnit
->linenumber_table
[i
].addr
<= addr
451 && addr
< aUnit
->linenumber_table
[i
+1].addr
)
453 *filename_ptr
= aUnit
->name
;
454 *linenumber_ptr
= aUnit
->linenumber_table
[i
].linenumber
;
460 for (eachFunc
= aUnit
->func_list
;
462 eachFunc
= eachFunc
->prev
)
464 if (eachFunc
->low_pc
<= addr
465 && addr
< eachFunc
->high_pc
)
467 *functionname_ptr
= eachFunc
->name
;
475 return line_p
|| func_p
;
478 /* The DWARF 1 version of find_nearest line.
479 Return TRUE if the line is found without error. */
482 _bfd_dwarf1_find_nearest_line (bfd
*abfd
,
486 const char **filename_ptr
,
487 const char **functionname_ptr
,
488 unsigned int *linenumber_ptr
)
490 struct dwarf1_debug
*stash
= elf_tdata (abfd
)->dwarf1_find_line_info
;
492 struct dwarf1_unit
* eachUnit
;
494 /* What address are we looking for? */
495 unsigned long addr
= (unsigned long)(offset
+ section
->vma
);
497 *filename_ptr
= NULL
;
498 *functionname_ptr
= NULL
;
504 bfd_size_type size
= sizeof (struct dwarf1_debug
);
506 stash
= elf_tdata (abfd
)->dwarf1_find_line_info
507 = (struct dwarf1_debug
*) bfd_zalloc (abfd
, size
);
512 msec
= bfd_get_section_by_name (abfd
, ".debug");
514 || (msec
->flags
& SEC_HAS_CONTENTS
) == 0)
515 /* No dwarf1 info. Note that at this point the stash
516 has been allocated, but contains zeros, this lets
517 future calls to this function fail quicker. */
520 size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
522 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
525 if (! stash
->debug_section
)
528 stash
->debug_section_end
= stash
->debug_section
+ size
;
529 stash
->currentDie
= stash
->debug_section
;
531 stash
->syms
= symbols
;
534 /* A null debug_section indicates that there was no dwarf1 info
535 or that an error occured while setting up the stash. */
537 if (! stash
->debug_section
)
540 /* Look at the previously parsed units to see if any contain
542 for (eachUnit
= stash
->lastUnit
; eachUnit
; eachUnit
= eachUnit
->prev
)
543 if (eachUnit
->low_pc
<= addr
&& addr
< eachUnit
->high_pc
)
544 return dwarf1_unit_find_nearest_line (stash
, eachUnit
, addr
,
549 while (stash
->currentDie
< stash
->debug_section_end
)
551 struct die_info aDieInfo
;
553 if (! parse_die (stash
->abfd
, &aDieInfo
, stash
->currentDie
,
554 stash
->debug_section_end
))
557 if (aDieInfo
.tag
== TAG_compile_unit
)
559 struct dwarf1_unit
* aUnit
560 = alloc_dwarf1_unit (stash
);
564 aUnit
->name
= aDieInfo
.name
;
565 aUnit
->low_pc
= aDieInfo
.low_pc
;
566 aUnit
->high_pc
= aDieInfo
.high_pc
;
567 aUnit
->has_stmt_list
= aDieInfo
.has_stmt_list
;
568 aUnit
->stmt_list_offset
= aDieInfo
.stmt_list_offset
;
570 /* A die has a child if it's followed by a die that is
573 && stash
->currentDie
+ aDieInfo
.length
574 < stash
->debug_section_end
575 && stash
->currentDie
+ aDieInfo
.length
576 != stash
->debug_section
+ aDieInfo
.sibling
)
577 aUnit
->first_child
= stash
->currentDie
+ aDieInfo
.length
;
579 aUnit
->first_child
= 0;
581 if (aUnit
->low_pc
<= addr
&& addr
< aUnit
->high_pc
)
582 return dwarf1_unit_find_nearest_line (stash
, aUnit
, addr
,
588 if (aDieInfo
.sibling
!= 0)
589 stash
->currentDie
= stash
->debug_section
+ aDieInfo
.sibling
;
591 stash
->currentDie
+= aDieInfo
.length
;
598 _bfd_dwarf1_cleanup_debug_info (bfd
*abfd ATTRIBUTE_UNUSED
, void **pinfo
)
600 struct dwarf1_debug
* stash
= *pinfo
;
605 free (stash
->debug_section
);
606 free (stash
->line_section
);