daily update
[binutils.git] / bfd / dwarf1.c
blob5d6c1e359e512e05fce58af9d02a7897167439c0
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libiberty.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "elf/dwarf.h"
30 /* dwarf1_debug is the starting point for all dwarf1 info. */
32 struct dwarf1_debug
34 /* The bfd we are working with. */
35 bfd* abfd;
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. */
42 char* debug_section;
44 /* Pointer to the end of the .debug_info section memory buffer. */
45 char* debug_section_end;
47 /* The buffer for the .line section. */
48 char* line_section;
50 /* End of that buffer. */
51 char* line_section_end;
53 /* The current or next unread die within the .debug section. */
54 char* currentDie;
57 /* One dwarf1_unit for each parsed compilation unit die. */
59 struct dwarf1_unit
61 /* Linked starting from stash->lastUnit. */
62 struct dwarf1_unit* prev;
64 /* Name of the compilation unit. */
65 char* name;
67 /* The highest and lowest address used in the compilation unit. */
68 unsigned long low_pc;
69 unsigned long high_pc;
71 /* Does this unit have a statement list? */
72 int has_stmt_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. */
78 char* first_child;
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. */
92 struct dwarf1_func
94 /* Linked starting from aUnit->func_list. */
95 struct dwarf1_func* prev;
97 /* Name of function. */
98 char* name;
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. */
106 struct die_info
108 unsigned long length;
109 unsigned long sibling;
110 unsigned long low_pc;
111 unsigned long high_pc;
112 unsigned long stmt_list_offset;
114 char* name;
116 int has_stmt_list;
118 unsigned short tag;
121 /* Parsed line number information. */
122 struct linenumber
124 /* First address in the line. */
125 unsigned long addr;
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;
144 stash->lastUnit = x;
146 return x;
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;
161 return 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. */
171 static bfd_boolean
172 parse_die (bfd * abfd,
173 struct die_info * aDieInfo,
174 char * aDiePtr,
175 char * aDiePtrEnd)
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);
184 xptr += 4;
185 if (aDieInfo->length == 0
186 || (this_die + aDieInfo->length) >= aDiePtrEnd)
187 return FALSE;
188 if (aDieInfo->length < 6)
190 /* Just padding bytes. */
191 aDieInfo->tag = TAG_padding;
192 return TRUE;
195 /* Then the tag. */
196 aDieInfo->tag = bfd_get_16 (abfd, (bfd_byte *) xptr);
197 xptr += 2;
199 /* Then the attributes. */
200 while (xptr < (this_die + aDieInfo->length))
202 unsigned short attr;
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);
208 xptr += 2;
210 switch (FORM_FROM_ATTR (attr))
212 case FORM_DATA2:
213 xptr += 2;
214 break;
215 case FORM_DATA4:
216 case FORM_REF:
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;
224 xptr += 4;
225 break;
226 case FORM_DATA8:
227 xptr += 8;
228 break;
229 case FORM_ADDR:
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);
234 xptr += 4;
235 break;
236 case FORM_BLOCK2:
237 xptr += 2 + bfd_get_16 (abfd, (bfd_byte *) xptr);
238 break;
239 case FORM_BLOCK4:
240 xptr += 4 + bfd_get_32 (abfd, (bfd_byte *) xptr);
241 break;
242 case FORM_STRING:
243 if (attr == AT_name)
244 aDieInfo->name = xptr;
245 xptr += strlen (xptr) + 1;
246 break;
250 return TRUE;
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. */
257 static bfd_boolean
258 parse_line_table (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
260 char* xptr;
262 /* Load the ".line" section from the bfd if we haven't already. */
263 if (stash->line_section == 0)
265 asection *msec;
266 bfd_size_type size;
268 msec = bfd_get_section_by_name (stash->abfd, ".line");
269 if (! msec)
270 return FALSE;
272 size = msec->rawsize ? msec->rawsize : msec->size;
273 stash->line_section = bfd_alloc (stash->abfd, size);
275 if (! stash->line_section)
276 return FALSE;
278 if (! bfd_get_section_contents (stash->abfd, msec, stash->line_section,
279 0, size))
281 stash->line_section = 0;
282 return FALSE;
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;
292 char *tblend;
293 unsigned long base;
294 bfd_size_type amt;
296 /* First comes the length. */
297 tblend = bfd_get_32 (stash->abfd, (bfd_byte *) xptr) + xptr;
298 xptr += 4;
300 /* Then the base address for each address in the table. */
301 base = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
302 xptr += 4;
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++)
314 /* A line number. */
315 aUnit->linenumber_table[eachLine].linenumber
316 = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
317 xptr += 4;
319 /* Skip the position within the line. */
320 xptr += 2;
322 /* And finally the address. */
323 aUnit->linenumber_table[eachLine].addr
324 = base + bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
325 xptr += 4;
329 return TRUE;
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. */
337 static bfd_boolean
338 parse_functions_in_unit (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
340 char* eachDie;
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))
351 return FALSE;
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;
368 else
369 break;
372 return TRUE;
375 /* Find the nearest line to 'addr' in 'aUnit'.
376 Return whether we found the line (or a function) without error. */
378 static bfd_boolean
379 dwarf1_unit_find_nearest_line (struct dwarf1_debug* stash,
380 struct dwarf1_unit* aUnit,
381 unsigned long addr,
382 const char **filename_ptr,
383 const char **functionname_ptr,
384 unsigned int *linenumber_ptr)
386 int line_p = FALSE;
387 int func_p = FALSE;
389 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
391 if (aUnit->has_stmt_list)
393 unsigned long i;
394 struct dwarf1_func* eachFunc;
396 if (! aUnit->linenumber_table)
398 if (! parse_line_table (stash, aUnit))
399 return FALSE;
402 if (! aUnit->func_list)
404 if (! parse_functions_in_unit (stash, aUnit))
405 return FALSE;
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;
415 line_p = TRUE;
416 break;
420 for (eachFunc = aUnit->func_list;
421 eachFunc;
422 eachFunc = eachFunc->prev)
424 if (eachFunc->low_pc <= addr
425 && addr < eachFunc->high_pc)
427 *functionname_ptr = eachFunc->name;
428 func_p = TRUE;
429 break;
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. */
441 bfd_boolean
442 _bfd_dwarf1_find_nearest_line (bfd *abfd,
443 asection *section,
444 asymbol **symbols ATTRIBUTE_UNUSED,
445 bfd_vma offset,
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;
459 *linenumber_ptr = 0;
461 if (! stash)
463 asection *msec;
464 bfd_size_type size = sizeof (struct dwarf1_debug);
466 stash = elf_tdata (abfd)->dwarf1_find_line_info
467 = bfd_zalloc (abfd, size);
469 if (! stash)
470 return FALSE;
472 msec = bfd_get_section_by_name (abfd, ".debug");
473 if (! msec)
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. */
477 return FALSE;
479 size = msec->rawsize ? msec->rawsize : msec->size;
480 stash->debug_section = bfd_alloc (abfd, size);
482 if (! stash->debug_section)
483 return FALSE;
485 if (! bfd_get_section_contents (abfd, msec, stash->debug_section,
486 0, size))
488 stash->debug_section = 0;
489 return FALSE;
492 stash->debug_section_end = stash->debug_section + size;
493 stash->currentDie = stash->debug_section;
494 stash->abfd = abfd;
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)
501 return FALSE;
503 /* Look at the previously parsed units to see if any contain
504 the addr. */
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,
508 filename_ptr,
509 functionname_ptr,
510 linenumber_ptr);
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))
518 return FALSE;
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
532 not it's sibling. */
533 if (aDieInfo.sibling
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;
539 else
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,
544 filename_ptr,
545 functionname_ptr,
546 linenumber_ptr);
549 if (aDieInfo.sibling != 0)
550 stash->currentDie = stash->debug_section + aDieInfo.sibling;
551 else
552 stash->currentDie += aDieInfo.length;
555 return FALSE;