1 /* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
2 Copyright 1998, 1999, 2000, 2001, 2002 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "libiberty.h"
27 #include "elf/dwarf.h"
29 /* dwarf1_debug is the starting point for all dwarf1 info. */
33 /* The bfd we are working with. */
36 /* List of already parsed compilation units. */
37 struct dwarf1_unit
* lastUnit
;
39 /* The buffer for the .debug section.
40 Zero indicates that the .debug section failed to load. */
43 /* Pointer to the end of the .debug_info section memory buffer. */
44 char* debug_section_end
;
46 /* The buffer for the .line section. */
49 /* End of that buffer. */
50 char* line_section_end
;
52 /* The current or next unread die within the .debug section. */
56 /* One dwarf1_unit for each parsed compilation unit die. */
59 /* Linked starting from stash->lastUnit. */
60 struct dwarf1_unit
* prev
;
62 /* Name of the compilation unit. */
65 /* The highest and lowest address used in the compilation unit. */
67 unsigned long high_pc
;
69 /* Does this unit have a statement list? */
72 /* If any, the offset of the line number table in the .line section. */
73 unsigned long stmt_list_offset
;
75 /* If non-zero, a pointer to the first child of this unit. */
78 /* How many line entries? */
79 unsigned long line_count
;
81 /* The decoded line number table (line_count entries). */
82 struct linenumber
* linenumber_table
;
84 /* The list of functions in this unit. */
85 struct dwarf1_func
* func_list
;
88 /* One dwarf1_func for each parsed function die. */
91 /* Linked starting from aUnit->func_list. */
92 struct dwarf1_func
* prev
;
94 /* Name of function. */
97 /* The highest and lowest address used in the compilation unit. */
99 unsigned long high_pc
;
102 /* Used to return info about a parsed die. */
104 unsigned long length
;
105 unsigned long sibling
;
106 unsigned long low_pc
;
107 unsigned long high_pc
;
108 unsigned long stmt_list_offset
;
117 /* Parsed line number information. */
119 /* First address in the line. */
122 /* The line number. */
123 unsigned long linenumber
;
126 /* Find the form of an attr, from the attr field. */
127 #define FORM_FROM_ATTR(attr) ((attr) & 0xF) /* Implicitly specified */
129 static struct dwarf1_unit
*alloc_dwarf1_unit
130 PARAMS ((struct dwarf1_debug
*));
131 static struct dwarf1_func
*alloc_dwarf1_func
132 PARAMS ((struct dwarf1_debug
*, struct dwarf1_unit
*));
133 static bfd_boolean parse_die
134 PARAMS ((bfd
*, struct die_info
*, char *, char *));
135 static bfd_boolean parse_line_table
136 PARAMS ((struct dwarf1_debug
*, struct dwarf1_unit
*));
137 static bfd_boolean parse_functions_in_unit
138 PARAMS ((struct dwarf1_debug
*, struct dwarf1_unit
*));
139 static bfd_boolean dwarf1_unit_find_nearest_line
140 PARAMS ((struct dwarf1_debug
*, struct dwarf1_unit
*, unsigned long,
141 const char **, const char **, unsigned int *));
143 /* Return a newly allocated dwarf1_unit. It should be cleared and
144 then attached into the 'stash' at 'stash->lastUnit'. */
146 static struct dwarf1_unit
*
147 alloc_dwarf1_unit (stash
)
148 struct dwarf1_debug
* stash
;
150 bfd_size_type amt
= sizeof (struct dwarf1_unit
);
152 struct dwarf1_unit
* x
= (struct dwarf1_unit
*) bfd_zalloc (stash
->abfd
, amt
);
153 x
->prev
= stash
->lastUnit
;
159 /* Return a newly allocated dwarf1_func. It must be cleared and
160 attached into 'aUnit' at 'aUnit->func_list'. */
162 static struct dwarf1_func
*
163 alloc_dwarf1_func (stash
, aUnit
)
164 struct dwarf1_debug
* stash
;
165 struct dwarf1_unit
* aUnit
;
167 bfd_size_type amt
= sizeof (struct dwarf1_func
);
169 struct dwarf1_func
* x
= (struct dwarf1_func
*) bfd_zalloc (stash
->abfd
, amt
);
170 x
->prev
= aUnit
->func_list
;
171 aUnit
->func_list
= x
;
176 /* parse_die - parse a Dwarf1 die.
177 Parse the die starting at 'aDiePtr' into 'aDieInfo'.
178 'abfd' must be the bfd from which the section that 'aDiePtr'
179 points to was pulled from.
181 Return FALSE if the die is invalidly formatted; TRUE otherwise. */
184 parse_die (abfd
, aDieInfo
, aDiePtr
, aDiePtrEnd
)
186 struct die_info
* aDieInfo
;
190 char* this_die
= aDiePtr
;
191 char* xptr
= this_die
;
193 memset (aDieInfo
,0,sizeof (*aDieInfo
));
195 /* First comes the length. */
196 aDieInfo
->length
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
198 if (aDieInfo
->length
== 0
199 || (this_die
+ aDieInfo
->length
) >= aDiePtrEnd
)
201 if (aDieInfo
->length
< 6)
203 /* Just padding bytes. */
204 aDieInfo
->tag
= TAG_padding
;
209 aDieInfo
->tag
= bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
212 /* Then the attributes. */
213 while (xptr
< (this_die
+ aDieInfo
->length
))
217 /* Parse the attribute based on its form. This section
218 must handle all dwarf1 forms, but need only handle the
219 actual attributes that we care about. */
221 attr
= bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
224 switch (FORM_FROM_ATTR (attr
))
231 if (attr
== AT_sibling
)
232 aDieInfo
->sibling
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
233 else if (attr
== AT_stmt_list
)
235 aDieInfo
->stmt_list_offset
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
236 aDieInfo
->has_stmt_list
= 1;
244 if (attr
== AT_low_pc
)
245 aDieInfo
->low_pc
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
246 else if (attr
== AT_high_pc
)
247 aDieInfo
->high_pc
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
251 xptr
+= 2 + bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
254 xptr
+= 4 + bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
258 aDieInfo
->name
= xptr
;
259 xptr
+= strlen (xptr
) + 1;
267 /* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
268 into 'aUnit->linenumber_table'. Return FALSE if an error
269 occurs; TRUE otherwise. */
272 parse_line_table (stash
, aUnit
)
273 struct dwarf1_debug
* stash
;
274 struct dwarf1_unit
* aUnit
;
278 /* Load the ".line" section from the bfd if we haven't already. */
279 if (stash
->line_section
== 0)
284 msec
= bfd_get_section_by_name (stash
->abfd
, ".line");
288 size
= bfd_get_section_size_before_reloc (msec
);
289 stash
->line_section
= (char *) bfd_alloc (stash
->abfd
, size
);
291 if (! stash
->line_section
)
294 if (! bfd_get_section_contents (stash
->abfd
, msec
, stash
->line_section
,
297 stash
->line_section
= 0;
301 stash
->line_section_end
= stash
->line_section
+ size
;
304 xptr
= stash
->line_section
+ aUnit
->stmt_list_offset
;
305 if (xptr
< stash
->line_section_end
)
307 unsigned long eachLine
;
312 /* First comes the length. */
313 tblend
= bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
) + xptr
;
316 /* Then the base address for each address in the table. */
317 base
= bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
320 /* How many line entrys?
321 10 = 4 (line number) + 2 (pos in line) + 4 (address in line) */
322 aUnit
->line_count
= (tblend
- xptr
) / 10;
324 /* Allocate an array for the entries. */
325 amt
= sizeof (struct linenumber
) * aUnit
->line_count
;
326 aUnit
->linenumber_table
= ((struct linenumber
*)
327 bfd_alloc (stash
->abfd
, amt
));
329 for (eachLine
= 0; eachLine
< aUnit
->line_count
; eachLine
++)
332 aUnit
->linenumber_table
[eachLine
].linenumber
333 = bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
336 /* Skip the position within the line. */
339 /* And finally the address. */
340 aUnit
->linenumber_table
[eachLine
].addr
341 = base
+ bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
349 /* Parse each function die in a compilation unit 'aUnit'.
350 The first child die of 'aUnit' should be in 'aUnit->first_child',
351 the result is placed in 'aUnit->func_list'.
352 Return FALSE if error; TRUE otherwise. */
355 parse_functions_in_unit (stash
, aUnit
)
356 struct dwarf1_debug
* stash
;
357 struct dwarf1_unit
* aUnit
;
361 if (aUnit
->first_child
)
362 for (eachDie
= aUnit
->first_child
;
363 eachDie
< stash
->debug_section_end
;
366 struct die_info eachDieInfo
;
368 if (! parse_die (stash
->abfd
, &eachDieInfo
, eachDie
,
369 stash
->debug_section_end
))
372 if (eachDieInfo
.tag
== TAG_global_subroutine
373 || eachDieInfo
.tag
== TAG_subroutine
374 || eachDieInfo
.tag
== TAG_inlined_subroutine
375 || eachDieInfo
.tag
== TAG_entry_point
)
377 struct dwarf1_func
* aFunc
= alloc_dwarf1_func (stash
,aUnit
);
379 aFunc
->name
= eachDieInfo
.name
;
380 aFunc
->low_pc
= eachDieInfo
.low_pc
;
381 aFunc
->high_pc
= eachDieInfo
.high_pc
;
384 /* Move to next sibling, if none, end loop */
385 if (eachDieInfo
.sibling
)
386 eachDie
= stash
->debug_section
+ eachDieInfo
.sibling
;
394 /* Find the nearest line to 'addr' in 'aUnit'.
395 Return whether we found the line (or a function) without error. */
398 dwarf1_unit_find_nearest_line (stash
, aUnit
, addr
,
399 filename_ptr
, functionname_ptr
,
401 struct dwarf1_debug
* stash
;
402 struct dwarf1_unit
* aUnit
;
404 const char **filename_ptr
;
405 const char **functionname_ptr
;
406 unsigned int *linenumber_ptr
;
411 if (aUnit
->low_pc
<= addr
&& addr
< aUnit
->high_pc
)
413 if (aUnit
->has_stmt_list
)
416 struct dwarf1_func
* eachFunc
;
418 if (! aUnit
->linenumber_table
)
420 if (! parse_line_table (stash
, aUnit
))
424 if (! aUnit
->func_list
)
426 if (! parse_functions_in_unit (stash
, aUnit
))
430 for (i
= 0; i
< aUnit
->line_count
; i
++)
432 if (aUnit
->linenumber_table
[i
].addr
<= addr
433 && addr
< aUnit
->linenumber_table
[i
+1].addr
)
435 *filename_ptr
= aUnit
->name
;
436 *linenumber_ptr
= aUnit
->linenumber_table
[i
].linenumber
;
442 for (eachFunc
= aUnit
->func_list
;
444 eachFunc
= eachFunc
->prev
)
446 if (eachFunc
->low_pc
<= addr
447 && addr
< eachFunc
->high_pc
)
449 *functionname_ptr
= eachFunc
->name
;
457 return line_p
|| func_p
;
460 /* The DWARF 1 version of find_nearest line.
461 Return TRUE if the line is found without error. */
464 _bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
465 filename_ptr
, functionname_ptr
, linenumber_ptr
)
468 asymbol
**symbols ATTRIBUTE_UNUSED
;
470 const char **filename_ptr
;
471 const char **functionname_ptr
;
472 unsigned int *linenumber_ptr
;
474 struct dwarf1_debug
*stash
= elf_tdata (abfd
)->dwarf1_find_line_info
;
476 struct dwarf1_unit
* eachUnit
;
478 /* What address are we looking for? */
479 unsigned long addr
= (unsigned long)(offset
+ section
->vma
);
481 *filename_ptr
= NULL
;
482 *functionname_ptr
= NULL
;
488 bfd_size_type size
= sizeof (struct dwarf1_debug
);
490 stash
= elf_tdata (abfd
)->dwarf1_find_line_info
491 = (struct dwarf1_debug
*) bfd_zalloc (abfd
, size
);
496 msec
= bfd_get_section_by_name (abfd
, ".debug");
499 /* No dwarf1 info. Note that at this point the stash
500 has been allocated, but contains zeros, this lets
501 future calls to this function fail quicker. */
505 size
= bfd_get_section_size_before_reloc (msec
);
506 stash
->debug_section
= (char *) bfd_alloc (abfd
, size
);
508 if (! stash
->debug_section
)
511 if (! bfd_get_section_contents (abfd
, msec
, stash
->debug_section
,
514 stash
->debug_section
= 0;
518 stash
->debug_section_end
= stash
->debug_section
+ size
;
519 stash
->currentDie
= stash
->debug_section
;
523 /* A null debug_section indicates that there was no dwarf1 info
524 or that an error occured while setting up the stash. */
526 if (! stash
->debug_section
)
529 /* Look at the previously parsed units to see if any contain
531 for (eachUnit
= stash
->lastUnit
; eachUnit
; eachUnit
= eachUnit
->prev
)
533 if (eachUnit
->low_pc
<= addr
&& addr
< eachUnit
->high_pc
)
534 return dwarf1_unit_find_nearest_line (stash
, eachUnit
, addr
,
540 while (stash
->currentDie
< stash
->debug_section_end
)
542 struct die_info aDieInfo
;
544 if (! parse_die (stash
->abfd
, &aDieInfo
, stash
->currentDie
,
545 stash
->debug_section_end
))
548 if (aDieInfo
.tag
== TAG_compile_unit
)
550 struct dwarf1_unit
* aUnit
551 = alloc_dwarf1_unit (stash
);
553 aUnit
->name
= aDieInfo
.name
;
554 aUnit
->low_pc
= aDieInfo
.low_pc
;
555 aUnit
->high_pc
= aDieInfo
.high_pc
;
556 aUnit
->has_stmt_list
= aDieInfo
.has_stmt_list
;
557 aUnit
->stmt_list_offset
= aDieInfo
.stmt_list_offset
;
559 /* A die has a child if it's followed by a die that is
562 && stash
->currentDie
+ aDieInfo
.length
563 < stash
->debug_section_end
564 && stash
->currentDie
+ aDieInfo
.length
565 != stash
->debug_section
+ aDieInfo
.sibling
)
566 aUnit
->first_child
= stash
->currentDie
+ aDieInfo
.length
;
568 aUnit
->first_child
= 0;
570 if (aUnit
->low_pc
<= addr
&& addr
< aUnit
->high_pc
)
571 return dwarf1_unit_find_nearest_line (stash
, aUnit
, addr
,
577 if (aDieInfo
.sibling
!= 0)
578 stash
->currentDie
= stash
->debug_section
+ aDieInfo
.sibling
;
580 stash
->currentDie
+= aDieInfo
.length
;