1 /* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
2 Copyright 1998, 1999, 2000, 2001, 2002, 2004, 2005
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 2 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., 59 Temple Place - Suite 330, Boston, 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 /* List of already parsed compilation units. */
38 struct dwarf1_unit* lastUnit;
40 /* The buffer for the .debug section.
41 Zero indicates that the .debug section failed to load. */
44 /* Pointer to the end of the .debug_info section memory buffer. */
45 char* debug_section_end;
47 /* The buffer for the .line section. */
50 /* End of that buffer. */
51 char* line_section_end;
53 /* The current or next unread die within the .debug section. */
57 /* One dwarf1_unit for each parsed compilation unit die. */
61 /* Linked starting from stash->lastUnit. */
62 struct dwarf1_unit* prev;
64 /* Name of the compilation unit. */
67 /* The highest and lowest address used in the compilation unit. */
69 unsigned long high_pc;
71 /* Does this unit have a statement list? */
74 /* If any, the offset of the line number table in the .line section. */
75 unsigned long stmt_list_offset;
77 /* If non-zero, a pointer to the first child of this unit. */
80 /* How many line entries? */
81 unsigned long line_count;
83 /* The decoded line number table (line_count entries). */
84 struct linenumber* linenumber_table;
86 /* The list of functions in this unit. */
87 struct dwarf1_func* func_list;
90 /* One dwarf1_func for each parsed function die. */
94 /* Linked starting from aUnit->func_list. */
95 struct dwarf1_func* prev;
97 /* Name of function. */
100 /* The highest and lowest address used in the compilation unit. */
101 unsigned long low_pc;
102 unsigned long high_pc;
105 /* Used to return info about a parsed die. */
108 unsigned long length;
109 unsigned long sibling;
110 unsigned long low_pc;
111 unsigned long high_pc;
112 unsigned long stmt_list_offset;
121 /* Parsed line number information. */
124 /* First address in the line. */
127 /* The line number. */
128 unsigned long linenumber;
131 /* Find the form of an attr, from the attr field. */
132 #define FORM_FROM_ATTR(attr) ((attr) & 0xF) /* Implicitly specified. */
134 /* Return a newly allocated dwarf1_unit. It should be cleared and
135 then attached into the 'stash' at 'stash->lastUnit'. */
137 static struct dwarf1_unit*
138 alloc_dwarf1_unit (struct dwarf1_debug* stash)
140 bfd_size_type amt = sizeof (struct dwarf1_unit);
142 struct dwarf1_unit* x = bfd_zalloc (stash->abfd, amt);
143 x->prev = stash->lastUnit;
149 /* Return a newly allocated dwarf1_func. It must be cleared and
150 attached into 'aUnit' at 'aUnit->func_list'. */
152 static struct dwarf1_func *
153 alloc_dwarf1_func (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
155 bfd_size_type amt = sizeof (struct dwarf1_func);
157 struct dwarf1_func* x = bfd_zalloc (stash->abfd, amt);
158 x->prev = aUnit->func_list;
159 aUnit->func_list = x;
164 /* parse_die - parse a Dwarf1 die.
165 Parse the die starting at 'aDiePtr' into 'aDieInfo'.
166 'abfd' must be the bfd from which the section that 'aDiePtr'
167 points to was pulled from.
169 Return FALSE if the die is invalidly formatted; TRUE otherwise. */
172 parse_die (bfd * abfd,
173 struct die_info * aDieInfo,
177 char* this_die = aDiePtr;
178 char* xptr = this_die;
180 memset (aDieInfo, 0, sizeof (* aDieInfo));
182 /* First comes the length. */
183 aDieInfo->length = bfd_get_32 (abfd, (bfd_byte *) xptr);
185 if (aDieInfo->length == 0
186 || (this_die + aDieInfo->length) >= aDiePtrEnd)
188 if (aDieInfo->length < 6)
190 /* Just padding bytes. */
191 aDieInfo->tag = TAG_padding;
196 aDieInfo->tag = bfd_get_16 (abfd, (bfd_byte *) xptr);
199 /* Then the attributes. */
200 while (xptr < (this_die + aDieInfo->length))
204 /* Parse the attribute based on its form. This section
205 must handle all dwarf1 forms, but need only handle the
206 actual attributes that we care about. */
207 attr = bfd_get_16 (abfd, (bfd_byte *) xptr);
210 switch (FORM_FROM_ATTR (attr))
217 if (attr == AT_sibling)
218 aDieInfo->sibling = bfd_get_32 (abfd, (bfd_byte *) xptr);
219 else if (attr == AT_stmt_list)
221 aDieInfo->stmt_list_offset = bfd_get_32 (abfd, (bfd_byte *) xptr);
222 aDieInfo->has_stmt_list = 1;
230 if (attr == AT_low_pc)
231 aDieInfo->low_pc = bfd_get_32 (abfd, (bfd_byte *) xptr);
232 else if (attr == AT_high_pc)
233 aDieInfo->high_pc = bfd_get_32 (abfd, (bfd_byte *) xptr);
237 xptr += 2 + bfd_get_16 (abfd, (bfd_byte *) xptr);
240 xptr += 4 + bfd_get_32 (abfd, (bfd_byte *) xptr);
244 aDieInfo->name = xptr;
245 xptr += strlen (xptr) + 1;
253 /* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
254 into 'aUnit->linenumber_table'. Return FALSE if an error
255 occurs; TRUE otherwise. */
258 parse_line_table (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
262 /* Load the ".line" section from the bfd if we haven't already. */
263 if (stash->line_section == 0)
268 msec = bfd_get_section_by_name (stash->abfd, ".line");
272 size = msec->rawsize ? msec->rawsize : msec->size;
273 stash->line_section = bfd_alloc (stash->abfd, size);
275 if (! stash->line_section)
278 if (! bfd_get_section_contents (stash->abfd, msec, stash->line_section,
281 stash->line_section = 0;
285 stash->line_section_end = stash->line_section + size;
288 xptr = stash->line_section + aUnit->stmt_list_offset;
289 if (xptr < stash->line_section_end)
291 unsigned long eachLine;
296 /* First comes the length. */
297 tblend = bfd_get_32 (stash->abfd, (bfd_byte *) xptr) + xptr;
300 /* Then the base address for each address in the table. */
301 base = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
304 /* How many line entrys?
305 10 = 4 (line number) + 2 (pos in line) + 4 (address in line). */
306 aUnit->line_count = (tblend - xptr) / 10;
308 /* Allocate an array for the entries. */
309 amt = sizeof (struct linenumber) * aUnit->line_count;
310 aUnit->linenumber_table = bfd_alloc (stash->abfd, amt);
312 for (eachLine = 0; eachLine < aUnit->line_count; eachLine++)
315 aUnit->linenumber_table[eachLine].linenumber
316 = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
319 /* Skip the position within the line. */
322 /* And finally the address. */
323 aUnit->linenumber_table[eachLine].addr
324 = base + bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
332 /* Parse each function die in a compilation unit 'aUnit'.
333 The first child die of 'aUnit' should be in 'aUnit->first_child',
334 the result is placed in 'aUnit->func_list'.
335 Return FALSE if error; TRUE otherwise. */
338 parse_functions_in_unit (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
342 if (aUnit->first_child)
343 for (eachDie = aUnit->first_child;
344 eachDie < stash->debug_section_end;
347 struct die_info eachDieInfo;
349 if (! parse_die (stash->abfd, &eachDieInfo, eachDie,
350 stash->debug_section_end))
353 if (eachDieInfo.tag == TAG_global_subroutine
354 || eachDieInfo.tag == TAG_subroutine
355 || eachDieInfo.tag == TAG_inlined_subroutine
356 || eachDieInfo.tag == TAG_entry_point)
358 struct dwarf1_func* aFunc = alloc_dwarf1_func (stash,aUnit);
360 aFunc->name = eachDieInfo.name;
361 aFunc->low_pc = eachDieInfo.low_pc;
362 aFunc->high_pc = eachDieInfo.high_pc;
365 /* Move to next sibling, if none, end loop */
366 if (eachDieInfo.sibling)
367 eachDie = stash->debug_section + eachDieInfo.sibling;
375 /* Find the nearest line to 'addr' in 'aUnit'.
376 Return whether we found the line (or a function) without error. */
379 dwarf1_unit_find_nearest_line (struct dwarf1_debug* stash,
380 struct dwarf1_unit* aUnit,
382 const char **filename_ptr,
383 const char **functionname_ptr,
384 unsigned int *linenumber_ptr)
389 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
391 if (aUnit->has_stmt_list)
394 struct dwarf1_func* eachFunc;
396 if (! aUnit->linenumber_table)
398 if (! parse_line_table (stash, aUnit))
402 if (! aUnit->func_list)
404 if (! parse_functions_in_unit (stash, aUnit))
408 for (i = 0; i < aUnit->line_count; i++)
410 if (aUnit->linenumber_table[i].addr <= addr
411 && addr < aUnit->linenumber_table[i+1].addr)
413 *filename_ptr = aUnit->name;
414 *linenumber_ptr = aUnit->linenumber_table[i].linenumber;
420 for (eachFunc = aUnit->func_list;
422 eachFunc = eachFunc->prev)
424 if (eachFunc->low_pc <= addr
425 && addr < eachFunc->high_pc)
427 *functionname_ptr = eachFunc->name;
435 return line_p || func_p;
438 /* The DWARF 1 version of find_nearest line.
439 Return TRUE if the line is found without error. */
442 _bfd_dwarf1_find_nearest_line (bfd *abfd,
444 asymbol **symbols ATTRIBUTE_UNUSED,
446 const char **filename_ptr,
447 const char **functionname_ptr,
448 unsigned int *linenumber_ptr)
450 struct dwarf1_debug *stash = elf_tdata (abfd)->dwarf1_find_line_info;
452 struct dwarf1_unit* eachUnit;
454 /* What address are we looking for? */
455 unsigned long addr = (unsigned long)(offset + section->vma);
457 *filename_ptr = NULL;
458 *functionname_ptr = NULL;
464 bfd_size_type size = sizeof (struct dwarf1_debug);
466 stash = elf_tdata (abfd)->dwarf1_find_line_info
467 = bfd_zalloc (abfd, size);
472 msec = bfd_get_section_by_name (abfd, ".debug");
474 /* No dwarf1 info. Note that at this point the stash
475 has been allocated, but contains zeros, this lets
476 future calls to this function fail quicker. */
479 size = msec->rawsize ? msec->rawsize : msec->size;
480 stash->debug_section = bfd_alloc (abfd, size);
482 if (! stash->debug_section)
485 if (! bfd_get_section_contents (abfd, msec, stash->debug_section,
488 stash->debug_section = 0;
492 stash->debug_section_end = stash->debug_section + size;
493 stash->currentDie = stash->debug_section;
497 /* A null debug_section indicates that there was no dwarf1 info
498 or that an error occured while setting up the stash. */
500 if (! stash->debug_section)
503 /* Look at the previously parsed units to see if any contain
505 for (eachUnit = stash->lastUnit; eachUnit; eachUnit = eachUnit->prev)
506 if (eachUnit->low_pc <= addr && addr < eachUnit->high_pc)
507 return dwarf1_unit_find_nearest_line (stash, eachUnit, addr,
512 while (stash->currentDie < stash->debug_section_end)
514 struct die_info aDieInfo;
516 if (! parse_die (stash->abfd, &aDieInfo, stash->currentDie,
517 stash->debug_section_end))
520 if (aDieInfo.tag == TAG_compile_unit)
522 struct dwarf1_unit* aUnit
523 = alloc_dwarf1_unit (stash);
525 aUnit->name = aDieInfo.name;
526 aUnit->low_pc = aDieInfo.low_pc;
527 aUnit->high_pc = aDieInfo.high_pc;
528 aUnit->has_stmt_list = aDieInfo.has_stmt_list;
529 aUnit->stmt_list_offset = aDieInfo.stmt_list_offset;
531 /* A die has a child if it's followed by a die that is
534 && stash->currentDie + aDieInfo.length
535 < stash->debug_section_end
536 && stash->currentDie + aDieInfo.length
537 != stash->debug_section + aDieInfo.sibling)
538 aUnit->first_child = stash->currentDie + aDieInfo.length;
540 aUnit->first_child = 0;
542 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
543 return dwarf1_unit_find_nearest_line (stash, aUnit, addr,
549 if (aDieInfo.sibling != 0)
550 stash->currentDie = stash->debug_section + aDieInfo.sibling;
552 stash->currentDie += aDieInfo.length;