1 /* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
2 Copyright 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 Written by Gavin Romig-Koch of Cygnus Solutions (gavin@cygnus.com).
7 This file is part of BFD.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
26 #include "libiberty.h"
29 #include "elf/dwarf.h"
31 /* dwarf1_debug is the starting point for all dwarf1 info. */
35 /* The bfd we are working with. */
38 /* Pointer to the symbol table. */
41 /* List of already parsed compilation units. */
42 struct dwarf1_unit
* lastUnit
;
44 /* The buffer for the .debug section.
45 Zero indicates that the .debug section failed to load. */
46 bfd_byte
*debug_section
;
48 /* Pointer to the end of the .debug_info section memory buffer. */
49 bfd_byte
*debug_section_end
;
51 /* The buffer for the .line section. */
52 bfd_byte
*line_section
;
54 /* End of that buffer. */
55 bfd_byte
*line_section_end
;
57 /* The current or next unread die within the .debug section. */
61 /* One dwarf1_unit for each parsed compilation unit die. */
65 /* Linked starting from stash->lastUnit. */
66 struct dwarf1_unit
* prev
;
68 /* Name of the compilation unit. */
71 /* The highest and lowest address used in the compilation unit. */
73 unsigned long high_pc
;
75 /* Does this unit have a statement list? */
78 /* If any, the offset of the line number table in the .line section. */
79 unsigned long stmt_list_offset
;
81 /* If non-zero, a pointer to the first child of this unit. */
82 bfd_byte
*first_child
;
84 /* How many line entries? */
85 unsigned long line_count
;
87 /* The decoded line number table (line_count entries). */
88 struct linenumber
* linenumber_table
;
90 /* The list of functions in this unit. */
91 struct dwarf1_func
* func_list
;
94 /* One dwarf1_func for each parsed function die. */
98 /* Linked starting from aUnit->func_list. */
99 struct dwarf1_func
* prev
;
101 /* Name of function. */
104 /* The highest and lowest address used in the compilation unit. */
105 unsigned long low_pc
;
106 unsigned long high_pc
;
109 /* Used to return info about a parsed die. */
112 unsigned long length
;
113 unsigned long sibling
;
114 unsigned long low_pc
;
115 unsigned long high_pc
;
116 unsigned long stmt_list_offset
;
125 /* Parsed line number information. */
128 /* First address in the line. */
131 /* The line number. */
132 unsigned long linenumber
;
135 /* Find the form of an attr, from the attr field. */
136 #define FORM_FROM_ATTR(attr) ((attr) & 0xF) /* Implicitly specified. */
138 /* Return a newly allocated dwarf1_unit. It should be cleared and
139 then attached into the 'stash' at 'stash->lastUnit'. */
141 static struct dwarf1_unit
*
142 alloc_dwarf1_unit (struct dwarf1_debug
* stash
)
144 bfd_size_type amt
= sizeof (struct dwarf1_unit
);
146 struct dwarf1_unit
* x
= (struct dwarf1_unit
*) bfd_zalloc (stash
->abfd
, amt
);
149 x
->prev
= stash
->lastUnit
;
156 /* Return a newly allocated dwarf1_func. It must be cleared and
157 attached into 'aUnit' at 'aUnit->func_list'. */
159 static struct dwarf1_func
*
160 alloc_dwarf1_func (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
162 bfd_size_type amt
= sizeof (struct dwarf1_func
);
164 struct dwarf1_func
* x
= (struct dwarf1_func
*) bfd_zalloc (stash
->abfd
, amt
);
167 x
->prev
= aUnit
->func_list
;
168 aUnit
->func_list
= x
;
174 /* parse_die - parse a Dwarf1 die.
175 Parse the die starting at 'aDiePtr' into 'aDieInfo'.
176 'abfd' must be the bfd from which the section that 'aDiePtr'
177 points to was pulled from.
179 Return FALSE if the die is invalidly formatted; TRUE otherwise. */
182 parse_die (bfd
* abfd
,
183 struct die_info
* aDieInfo
,
185 bfd_byte
* aDiePtrEnd
)
187 bfd_byte
*this_die
= aDiePtr
;
188 bfd_byte
*xptr
= this_die
;
190 memset (aDieInfo
, 0, sizeof (* aDieInfo
));
192 /* First comes the length. */
193 aDieInfo
->length
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
195 if (aDieInfo
->length
== 0
196 || (this_die
+ aDieInfo
->length
) >= aDiePtrEnd
)
198 if (aDieInfo
->length
< 6)
200 /* Just padding bytes. */
201 aDieInfo
->tag
= TAG_padding
;
206 aDieInfo
->tag
= bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
209 /* Then the attributes. */
210 while (xptr
< (this_die
+ aDieInfo
->length
))
214 /* Parse the attribute based on its form. This section
215 must handle all dwarf1 forms, but need only handle the
216 actual attributes that we care about. */
217 attr
= bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
220 switch (FORM_FROM_ATTR (attr
))
227 if (attr
== AT_sibling
)
228 aDieInfo
->sibling
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
229 else if (attr
== AT_stmt_list
)
231 aDieInfo
->stmt_list_offset
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
232 aDieInfo
->has_stmt_list
= 1;
240 if (attr
== AT_low_pc
)
241 aDieInfo
->low_pc
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
242 else if (attr
== AT_high_pc
)
243 aDieInfo
->high_pc
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
247 xptr
+= 2 + bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
250 xptr
+= 4 + bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
254 aDieInfo
->name
= (char *) xptr
;
255 xptr
+= strlen ((char *) xptr
) + 1;
263 /* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
264 into 'aUnit->linenumber_table'. Return FALSE if an error
265 occurs; TRUE otherwise. */
268 parse_line_table (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
272 /* Load the ".line" section from the bfd if we haven't already. */
273 if (stash
->line_section
== 0)
278 msec
= bfd_get_section_by_name (stash
->abfd
, ".line");
282 size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
284 = bfd_simple_get_relocated_section_contents
285 (stash
->abfd
, msec
, NULL
, stash
->syms
);
287 if (! stash
->line_section
)
290 stash
->line_section_end
= stash
->line_section
+ size
;
293 xptr
= stash
->line_section
+ aUnit
->stmt_list_offset
;
294 if (xptr
< stash
->line_section_end
)
296 unsigned long eachLine
;
301 /* First comes the length. */
302 tblend
= bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
) + xptr
;
305 /* Then the base address for each address in the table. */
306 base
= bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
309 /* How many line entrys?
310 10 = 4 (line number) + 2 (pos in line) + 4 (address in line). */
311 aUnit
->line_count
= (tblend
- xptr
) / 10;
313 /* Allocate an array for the entries. */
314 amt
= sizeof (struct linenumber
) * aUnit
->line_count
;
315 aUnit
->linenumber_table
= (struct linenumber
*) bfd_alloc (stash
->abfd
,
317 if (!aUnit
->linenumber_table
)
320 for (eachLine
= 0; eachLine
< aUnit
->line_count
; eachLine
++)
323 aUnit
->linenumber_table
[eachLine
].linenumber
324 = bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
327 /* Skip the position within the line. */
330 /* And finally the address. */
331 aUnit
->linenumber_table
[eachLine
].addr
332 = base
+ bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
340 /* Parse each function die in a compilation unit 'aUnit'.
341 The first child die of 'aUnit' should be in 'aUnit->first_child',
342 the result is placed in 'aUnit->func_list'.
343 Return FALSE if error; TRUE otherwise. */
346 parse_functions_in_unit (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
350 if (aUnit
->first_child
)
351 for (eachDie
= aUnit
->first_child
;
352 eachDie
< stash
->debug_section_end
;
355 struct die_info eachDieInfo
;
357 if (! parse_die (stash
->abfd
, &eachDieInfo
, eachDie
,
358 stash
->debug_section_end
))
361 if (eachDieInfo
.tag
== TAG_global_subroutine
362 || eachDieInfo
.tag
== TAG_subroutine
363 || eachDieInfo
.tag
== TAG_inlined_subroutine
364 || eachDieInfo
.tag
== TAG_entry_point
)
366 struct dwarf1_func
* aFunc
= alloc_dwarf1_func (stash
,aUnit
);
370 aFunc
->name
= eachDieInfo
.name
;
371 aFunc
->low_pc
= eachDieInfo
.low_pc
;
372 aFunc
->high_pc
= eachDieInfo
.high_pc
;
375 /* Move to next sibling, if none, end loop */
376 if (eachDieInfo
.sibling
)
377 eachDie
= stash
->debug_section
+ eachDieInfo
.sibling
;
385 /* Find the nearest line to 'addr' in 'aUnit'.
386 Return whether we found the line (or a function) without error. */
389 dwarf1_unit_find_nearest_line (struct dwarf1_debug
* stash
,
390 struct dwarf1_unit
* aUnit
,
392 const char **filename_ptr
,
393 const char **functionname_ptr
,
394 unsigned int *linenumber_ptr
)
399 if (aUnit
->low_pc
<= addr
&& addr
< aUnit
->high_pc
)
401 if (aUnit
->has_stmt_list
)
404 struct dwarf1_func
* eachFunc
;
406 if (! aUnit
->linenumber_table
)
408 if (! parse_line_table (stash
, aUnit
))
412 if (! aUnit
->func_list
)
414 if (! parse_functions_in_unit (stash
, aUnit
))
418 for (i
= 0; i
< aUnit
->line_count
; i
++)
420 if (aUnit
->linenumber_table
[i
].addr
<= addr
421 && addr
< aUnit
->linenumber_table
[i
+1].addr
)
423 *filename_ptr
= aUnit
->name
;
424 *linenumber_ptr
= aUnit
->linenumber_table
[i
].linenumber
;
430 for (eachFunc
= aUnit
->func_list
;
432 eachFunc
= eachFunc
->prev
)
434 if (eachFunc
->low_pc
<= addr
435 && addr
< eachFunc
->high_pc
)
437 *functionname_ptr
= eachFunc
->name
;
445 return line_p
|| func_p
;
448 /* The DWARF 1 version of find_nearest line.
449 Return TRUE if the line is found without error. */
452 _bfd_dwarf1_find_nearest_line (bfd
*abfd
,
456 const char **filename_ptr
,
457 const char **functionname_ptr
,
458 unsigned int *linenumber_ptr
)
460 struct dwarf1_debug
*stash
= elf_tdata (abfd
)->dwarf1_find_line_info
;
462 struct dwarf1_unit
* eachUnit
;
464 /* What address are we looking for? */
465 unsigned long addr
= (unsigned long)(offset
+ section
->vma
);
467 *filename_ptr
= NULL
;
468 *functionname_ptr
= NULL
;
474 bfd_size_type size
= sizeof (struct dwarf1_debug
);
476 stash
= elf_tdata (abfd
)->dwarf1_find_line_info
477 = (struct dwarf1_debug
*) bfd_zalloc (abfd
, size
);
482 msec
= bfd_get_section_by_name (abfd
, ".debug");
484 /* No dwarf1 info. Note that at this point the stash
485 has been allocated, but contains zeros, this lets
486 future calls to this function fail quicker. */
489 size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
491 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
494 if (! stash
->debug_section
)
497 stash
->debug_section_end
= stash
->debug_section
+ size
;
498 stash
->currentDie
= stash
->debug_section
;
500 stash
->syms
= symbols
;
503 /* A null debug_section indicates that there was no dwarf1 info
504 or that an error occured while setting up the stash. */
506 if (! stash
->debug_section
)
509 /* Look at the previously parsed units to see if any contain
511 for (eachUnit
= stash
->lastUnit
; eachUnit
; eachUnit
= eachUnit
->prev
)
512 if (eachUnit
->low_pc
<= addr
&& addr
< eachUnit
->high_pc
)
513 return dwarf1_unit_find_nearest_line (stash
, eachUnit
, addr
,
518 while (stash
->currentDie
< stash
->debug_section_end
)
520 struct die_info aDieInfo
;
522 if (! parse_die (stash
->abfd
, &aDieInfo
, stash
->currentDie
,
523 stash
->debug_section_end
))
526 if (aDieInfo
.tag
== TAG_compile_unit
)
528 struct dwarf1_unit
* aUnit
529 = alloc_dwarf1_unit (stash
);
533 aUnit
->name
= aDieInfo
.name
;
534 aUnit
->low_pc
= aDieInfo
.low_pc
;
535 aUnit
->high_pc
= aDieInfo
.high_pc
;
536 aUnit
->has_stmt_list
= aDieInfo
.has_stmt_list
;
537 aUnit
->stmt_list_offset
= aDieInfo
.stmt_list_offset
;
539 /* A die has a child if it's followed by a die that is
542 && stash
->currentDie
+ aDieInfo
.length
543 < stash
->debug_section_end
544 && stash
->currentDie
+ aDieInfo
.length
545 != stash
->debug_section
+ aDieInfo
.sibling
)
546 aUnit
->first_child
= stash
->currentDie
+ aDieInfo
.length
;
548 aUnit
->first_child
= 0;
550 if (aUnit
->low_pc
<= addr
&& addr
< aUnit
->high_pc
)
551 return dwarf1_unit_find_nearest_line (stash
, aUnit
, addr
,
557 if (aDieInfo
.sibling
!= 0)
558 stash
->currentDie
= stash
->debug_section
+ aDieInfo
.sibling
;
560 stash
->currentDie
+= aDieInfo
.length
;