1 /* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
2 Copyright 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007
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 /* List of already parsed compilation units. */
39 struct dwarf1_unit
* lastUnit
;
41 /* The buffer for the .debug section.
42 Zero indicates that the .debug section failed to load. */
45 /* Pointer to the end of the .debug_info section memory buffer. */
46 char* debug_section_end
;
48 /* The buffer for the .line section. */
51 /* End of that buffer. */
52 char* line_section_end
;
54 /* The current or next unread die within the .debug section. */
58 /* One dwarf1_unit for each parsed compilation unit die. */
62 /* Linked starting from stash->lastUnit. */
63 struct dwarf1_unit
* prev
;
65 /* Name of the compilation unit. */
68 /* The highest and lowest address used in the compilation unit. */
70 unsigned long high_pc
;
72 /* Does this unit have a statement list? */
75 /* If any, the offset of the line number table in the .line section. */
76 unsigned long stmt_list_offset
;
78 /* If non-zero, a pointer to the first child of this unit. */
81 /* How many line entries? */
82 unsigned long line_count
;
84 /* The decoded line number table (line_count entries). */
85 struct linenumber
* linenumber_table
;
87 /* The list of functions in this unit. */
88 struct dwarf1_func
* func_list
;
91 /* One dwarf1_func for each parsed function die. */
95 /* Linked starting from aUnit->func_list. */
96 struct dwarf1_func
* prev
;
98 /* Name of function. */
101 /* The highest and lowest address used in the compilation unit. */
102 unsigned long low_pc
;
103 unsigned long high_pc
;
106 /* Used to return info about a parsed die. */
109 unsigned long length
;
110 unsigned long sibling
;
111 unsigned long low_pc
;
112 unsigned long high_pc
;
113 unsigned long stmt_list_offset
;
122 /* Parsed line number information. */
125 /* First address in the line. */
128 /* The line number. */
129 unsigned long linenumber
;
132 /* Find the form of an attr, from the attr field. */
133 #define FORM_FROM_ATTR(attr) ((attr) & 0xF) /* Implicitly specified. */
135 /* Return a newly allocated dwarf1_unit. It should be cleared and
136 then attached into the 'stash' at 'stash->lastUnit'. */
138 static struct dwarf1_unit
*
139 alloc_dwarf1_unit (struct dwarf1_debug
* stash
)
141 bfd_size_type amt
= sizeof (struct dwarf1_unit
);
143 struct dwarf1_unit
* x
= bfd_zalloc (stash
->abfd
, amt
);
144 x
->prev
= stash
->lastUnit
;
150 /* Return a newly allocated dwarf1_func. It must be cleared and
151 attached into 'aUnit' at 'aUnit->func_list'. */
153 static struct dwarf1_func
*
154 alloc_dwarf1_func (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
156 bfd_size_type amt
= sizeof (struct dwarf1_func
);
158 struct dwarf1_func
* x
= bfd_zalloc (stash
->abfd
, amt
);
159 x
->prev
= aUnit
->func_list
;
160 aUnit
->func_list
= x
;
165 /* parse_die - parse a Dwarf1 die.
166 Parse the die starting at 'aDiePtr' into 'aDieInfo'.
167 'abfd' must be the bfd from which the section that 'aDiePtr'
168 points to was pulled from.
170 Return FALSE if the die is invalidly formatted; TRUE otherwise. */
173 parse_die (bfd
* abfd
,
174 struct die_info
* aDieInfo
,
178 char* this_die
= aDiePtr
;
179 char* xptr
= this_die
;
181 memset (aDieInfo
, 0, sizeof (* aDieInfo
));
183 /* First comes the length. */
184 aDieInfo
->length
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
186 if (aDieInfo
->length
== 0
187 || (this_die
+ aDieInfo
->length
) >= aDiePtrEnd
)
189 if (aDieInfo
->length
< 6)
191 /* Just padding bytes. */
192 aDieInfo
->tag
= TAG_padding
;
197 aDieInfo
->tag
= bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
200 /* Then the attributes. */
201 while (xptr
< (this_die
+ aDieInfo
->length
))
205 /* Parse the attribute based on its form. This section
206 must handle all dwarf1 forms, but need only handle the
207 actual attributes that we care about. */
208 attr
= bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
211 switch (FORM_FROM_ATTR (attr
))
218 if (attr
== AT_sibling
)
219 aDieInfo
->sibling
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
220 else if (attr
== AT_stmt_list
)
222 aDieInfo
->stmt_list_offset
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
223 aDieInfo
->has_stmt_list
= 1;
231 if (attr
== AT_low_pc
)
232 aDieInfo
->low_pc
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
233 else if (attr
== AT_high_pc
)
234 aDieInfo
->high_pc
= bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
238 xptr
+= 2 + bfd_get_16 (abfd
, (bfd_byte
*) xptr
);
241 xptr
+= 4 + bfd_get_32 (abfd
, (bfd_byte
*) xptr
);
245 aDieInfo
->name
= xptr
;
246 xptr
+= strlen (xptr
) + 1;
254 /* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
255 into 'aUnit->linenumber_table'. Return FALSE if an error
256 occurs; TRUE otherwise. */
259 parse_line_table (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
263 /* Load the ".line" section from the bfd if we haven't already. */
264 if (stash
->line_section
== 0)
269 msec
= bfd_get_section_by_name (stash
->abfd
, ".line");
273 size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
274 stash
->line_section
= bfd_alloc (stash
->abfd
, size
);
276 if (! stash
->line_section
)
279 if (! bfd_get_section_contents (stash
->abfd
, msec
, stash
->line_section
,
282 stash
->line_section
= 0;
286 stash
->line_section_end
= stash
->line_section
+ size
;
289 xptr
= stash
->line_section
+ aUnit
->stmt_list_offset
;
290 if (xptr
< stash
->line_section_end
)
292 unsigned long eachLine
;
297 /* First comes the length. */
298 tblend
= bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
) + xptr
;
301 /* Then the base address for each address in the table. */
302 base
= bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
305 /* How many line entrys?
306 10 = 4 (line number) + 2 (pos in line) + 4 (address in line). */
307 aUnit
->line_count
= (tblend
- xptr
) / 10;
309 /* Allocate an array for the entries. */
310 amt
= sizeof (struct linenumber
) * aUnit
->line_count
;
311 aUnit
->linenumber_table
= bfd_alloc (stash
->abfd
, amt
);
313 for (eachLine
= 0; eachLine
< aUnit
->line_count
; eachLine
++)
316 aUnit
->linenumber_table
[eachLine
].linenumber
317 = bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
320 /* Skip the position within the line. */
323 /* And finally the address. */
324 aUnit
->linenumber_table
[eachLine
].addr
325 = base
+ bfd_get_32 (stash
->abfd
, (bfd_byte
*) xptr
);
333 /* Parse each function die in a compilation unit 'aUnit'.
334 The first child die of 'aUnit' should be in 'aUnit->first_child',
335 the result is placed in 'aUnit->func_list'.
336 Return FALSE if error; TRUE otherwise. */
339 parse_functions_in_unit (struct dwarf1_debug
* stash
, struct dwarf1_unit
* aUnit
)
343 if (aUnit
->first_child
)
344 for (eachDie
= aUnit
->first_child
;
345 eachDie
< stash
->debug_section_end
;
348 struct die_info eachDieInfo
;
350 if (! parse_die (stash
->abfd
, &eachDieInfo
, eachDie
,
351 stash
->debug_section_end
))
354 if (eachDieInfo
.tag
== TAG_global_subroutine
355 || eachDieInfo
.tag
== TAG_subroutine
356 || eachDieInfo
.tag
== TAG_inlined_subroutine
357 || eachDieInfo
.tag
== TAG_entry_point
)
359 struct dwarf1_func
* aFunc
= alloc_dwarf1_func (stash
,aUnit
);
361 aFunc
->name
= eachDieInfo
.name
;
362 aFunc
->low_pc
= eachDieInfo
.low_pc
;
363 aFunc
->high_pc
= eachDieInfo
.high_pc
;
366 /* Move to next sibling, if none, end loop */
367 if (eachDieInfo
.sibling
)
368 eachDie
= stash
->debug_section
+ eachDieInfo
.sibling
;
376 /* Find the nearest line to 'addr' in 'aUnit'.
377 Return whether we found the line (or a function) without error. */
380 dwarf1_unit_find_nearest_line (struct dwarf1_debug
* stash
,
381 struct dwarf1_unit
* aUnit
,
383 const char **filename_ptr
,
384 const char **functionname_ptr
,
385 unsigned int *linenumber_ptr
)
390 if (aUnit
->low_pc
<= addr
&& addr
< aUnit
->high_pc
)
392 if (aUnit
->has_stmt_list
)
395 struct dwarf1_func
* eachFunc
;
397 if (! aUnit
->linenumber_table
)
399 if (! parse_line_table (stash
, aUnit
))
403 if (! aUnit
->func_list
)
405 if (! parse_functions_in_unit (stash
, aUnit
))
409 for (i
= 0; i
< aUnit
->line_count
; i
++)
411 if (aUnit
->linenumber_table
[i
].addr
<= addr
412 && addr
< aUnit
->linenumber_table
[i
+1].addr
)
414 *filename_ptr
= aUnit
->name
;
415 *linenumber_ptr
= aUnit
->linenumber_table
[i
].linenumber
;
421 for (eachFunc
= aUnit
->func_list
;
423 eachFunc
= eachFunc
->prev
)
425 if (eachFunc
->low_pc
<= addr
426 && addr
< eachFunc
->high_pc
)
428 *functionname_ptr
= eachFunc
->name
;
436 return line_p
|| func_p
;
439 /* The DWARF 1 version of find_nearest line.
440 Return TRUE if the line is found without error. */
443 _bfd_dwarf1_find_nearest_line (bfd
*abfd
,
445 asymbol
**symbols ATTRIBUTE_UNUSED
,
447 const char **filename_ptr
,
448 const char **functionname_ptr
,
449 unsigned int *linenumber_ptr
)
451 struct dwarf1_debug
*stash
= elf_tdata (abfd
)->dwarf1_find_line_info
;
453 struct dwarf1_unit
* eachUnit
;
455 /* What address are we looking for? */
456 unsigned long addr
= (unsigned long)(offset
+ section
->vma
);
458 *filename_ptr
= NULL
;
459 *functionname_ptr
= NULL
;
465 bfd_size_type size
= sizeof (struct dwarf1_debug
);
467 stash
= elf_tdata (abfd
)->dwarf1_find_line_info
468 = bfd_zalloc (abfd
, size
);
473 msec
= bfd_get_section_by_name (abfd
, ".debug");
475 /* No dwarf1 info. Note that at this point the stash
476 has been allocated, but contains zeros, this lets
477 future calls to this function fail quicker. */
480 size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
481 stash
->debug_section
= bfd_alloc (abfd
, size
);
483 if (! stash
->debug_section
)
486 if (! bfd_get_section_contents (abfd
, msec
, stash
->debug_section
,
489 stash
->debug_section
= 0;
493 stash
->debug_section_end
= stash
->debug_section
+ size
;
494 stash
->currentDie
= stash
->debug_section
;
498 /* A null debug_section indicates that there was no dwarf1 info
499 or that an error occured while setting up the stash. */
501 if (! stash
->debug_section
)
504 /* Look at the previously parsed units to see if any contain
506 for (eachUnit
= stash
->lastUnit
; eachUnit
; eachUnit
= eachUnit
->prev
)
507 if (eachUnit
->low_pc
<= addr
&& addr
< eachUnit
->high_pc
)
508 return dwarf1_unit_find_nearest_line (stash
, eachUnit
, addr
,
513 while (stash
->currentDie
< stash
->debug_section_end
)
515 struct die_info aDieInfo
;
517 if (! parse_die (stash
->abfd
, &aDieInfo
, stash
->currentDie
,
518 stash
->debug_section_end
))
521 if (aDieInfo
.tag
== TAG_compile_unit
)
523 struct dwarf1_unit
* aUnit
524 = alloc_dwarf1_unit (stash
);
526 aUnit
->name
= aDieInfo
.name
;
527 aUnit
->low_pc
= aDieInfo
.low_pc
;
528 aUnit
->high_pc
= aDieInfo
.high_pc
;
529 aUnit
->has_stmt_list
= aDieInfo
.has_stmt_list
;
530 aUnit
->stmt_list_offset
= aDieInfo
.stmt_list_offset
;
532 /* A die has a child if it's followed by a die that is
535 && stash
->currentDie
+ aDieInfo
.length
536 < stash
->debug_section_end
537 && stash
->currentDie
+ aDieInfo
.length
538 != stash
->debug_section
+ aDieInfo
.sibling
)
539 aUnit
->first_child
= stash
->currentDie
+ aDieInfo
.length
;
541 aUnit
->first_child
= 0;
543 if (aUnit
->low_pc
<= addr
&& addr
< aUnit
->high_pc
)
544 return dwarf1_unit_find_nearest_line (stash
, aUnit
, addr
,
550 if (aDieInfo
.sibling
!= 0)
551 stash
->currentDie
= stash
->debug_section
+ aDieInfo
.sibling
;
553 stash
->currentDie
+= aDieInfo
.length
;
This page took 0.040231 seconds and 4 git commands to generate.