2008-01-30 Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
[binutils.git] / bfd / dwarf1.c
blob6f02cb8593b427c2ac91435f5d63c08585687ee8
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. */
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "libiberty.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf/dwarf.h"
31 /* dwarf1_debug is the starting point for all dwarf1 info. */
33 struct dwarf1_debug
35 /* The bfd we are working with. */
36 bfd* abfd;
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. */
43 char* debug_section;
45 /* Pointer to the end of the .debug_info section memory buffer. */
46 char* debug_section_end;
48 /* The buffer for the .line section. */
49 char* line_section;
51 /* End of that buffer. */
52 char* line_section_end;
54 /* The current or next unread die within the .debug section. */
55 char* currentDie;
58 /* One dwarf1_unit for each parsed compilation unit die. */
60 struct dwarf1_unit
62 /* Linked starting from stash->lastUnit. */
63 struct dwarf1_unit* prev;
65 /* Name of the compilation unit. */
66 char* name;
68 /* The highest and lowest address used in the compilation unit. */
69 unsigned long low_pc;
70 unsigned long high_pc;
72 /* Does this unit have a statement list? */
73 int has_stmt_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. */
79 char* first_child;
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. */
93 struct dwarf1_func
95 /* Linked starting from aUnit->func_list. */
96 struct dwarf1_func* prev;
98 /* Name of function. */
99 char* name;
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. */
107 struct die_info
109 unsigned long length;
110 unsigned long sibling;
111 unsigned long low_pc;
112 unsigned long high_pc;
113 unsigned long stmt_list_offset;
115 char* name;
117 int has_stmt_list;
119 unsigned short tag;
122 /* Parsed line number information. */
123 struct linenumber
125 /* First address in the line. */
126 unsigned long addr;
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;
145 stash->lastUnit = x;
147 return x;
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;
162 return 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. */
172 static bfd_boolean
173 parse_die (bfd * abfd,
174 struct die_info * aDieInfo,
175 char * aDiePtr,
176 char * aDiePtrEnd)
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);
185 xptr += 4;
186 if (aDieInfo->length == 0
187 || (this_die + aDieInfo->length) >= aDiePtrEnd)
188 return FALSE;
189 if (aDieInfo->length < 6)
191 /* Just padding bytes. */
192 aDieInfo->tag = TAG_padding;
193 return TRUE;
196 /* Then the tag. */
197 aDieInfo->tag = bfd_get_16 (abfd, (bfd_byte *) xptr);
198 xptr += 2;
200 /* Then the attributes. */
201 while (xptr < (this_die + aDieInfo->length))
203 unsigned short attr;
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);
209 xptr += 2;
211 switch (FORM_FROM_ATTR (attr))
213 case FORM_DATA2:
214 xptr += 2;
215 break;
216 case FORM_DATA4:
217 case FORM_REF:
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;
225 xptr += 4;
226 break;
227 case FORM_DATA8:
228 xptr += 8;
229 break;
230 case FORM_ADDR:
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);
235 xptr += 4;
236 break;
237 case FORM_BLOCK2:
238 xptr += 2 + bfd_get_16 (abfd, (bfd_byte *) xptr);
239 break;
240 case FORM_BLOCK4:
241 xptr += 4 + bfd_get_32 (abfd, (bfd_byte *) xptr);
242 break;
243 case FORM_STRING:
244 if (attr == AT_name)
245 aDieInfo->name = xptr;
246 xptr += strlen (xptr) + 1;
247 break;
251 return TRUE;
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. */
258 static bfd_boolean
259 parse_line_table (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
261 char* xptr;
263 /* Load the ".line" section from the bfd if we haven't already. */
264 if (stash->line_section == 0)
266 asection *msec;
267 bfd_size_type size;
269 msec = bfd_get_section_by_name (stash->abfd, ".line");
270 if (! msec)
271 return FALSE;
273 size = msec->rawsize ? msec->rawsize : msec->size;
274 stash->line_section = bfd_alloc (stash->abfd, size);
276 if (! stash->line_section)
277 return FALSE;
279 if (! bfd_get_section_contents (stash->abfd, msec, stash->line_section,
280 0, size))
282 stash->line_section = 0;
283 return FALSE;
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;
293 char *tblend;
294 unsigned long base;
295 bfd_size_type amt;
297 /* First comes the length. */
298 tblend = bfd_get_32 (stash->abfd, (bfd_byte *) xptr) + xptr;
299 xptr += 4;
301 /* Then the base address for each address in the table. */
302 base = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
303 xptr += 4;
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++)
315 /* A line number. */
316 aUnit->linenumber_table[eachLine].linenumber
317 = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
318 xptr += 4;
320 /* Skip the position within the line. */
321 xptr += 2;
323 /* And finally the address. */
324 aUnit->linenumber_table[eachLine].addr
325 = base + bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
326 xptr += 4;
330 return TRUE;
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. */
338 static bfd_boolean
339 parse_functions_in_unit (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
341 char* eachDie;
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))
352 return FALSE;
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;
369 else
370 break;
373 return TRUE;
376 /* Find the nearest line to 'addr' in 'aUnit'.
377 Return whether we found the line (or a function) without error. */
379 static bfd_boolean
380 dwarf1_unit_find_nearest_line (struct dwarf1_debug* stash,
381 struct dwarf1_unit* aUnit,
382 unsigned long addr,
383 const char **filename_ptr,
384 const char **functionname_ptr,
385 unsigned int *linenumber_ptr)
387 int line_p = FALSE;
388 int func_p = FALSE;
390 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
392 if (aUnit->has_stmt_list)
394 unsigned long i;
395 struct dwarf1_func* eachFunc;
397 if (! aUnit->linenumber_table)
399 if (! parse_line_table (stash, aUnit))
400 return FALSE;
403 if (! aUnit->func_list)
405 if (! parse_functions_in_unit (stash, aUnit))
406 return FALSE;
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;
416 line_p = TRUE;
417 break;
421 for (eachFunc = aUnit->func_list;
422 eachFunc;
423 eachFunc = eachFunc->prev)
425 if (eachFunc->low_pc <= addr
426 && addr < eachFunc->high_pc)
428 *functionname_ptr = eachFunc->name;
429 func_p = TRUE;
430 break;
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. */
442 bfd_boolean
443 _bfd_dwarf1_find_nearest_line (bfd *abfd,
444 asection *section,
445 asymbol **symbols ATTRIBUTE_UNUSED,
446 bfd_vma offset,
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;
460 *linenumber_ptr = 0;
462 if (! stash)
464 asection *msec;
465 bfd_size_type size = sizeof (struct dwarf1_debug);
467 stash = elf_tdata (abfd)->dwarf1_find_line_info
468 = bfd_zalloc (abfd, size);
470 if (! stash)
471 return FALSE;
473 msec = bfd_get_section_by_name (abfd, ".debug");
474 if (! msec)
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. */
478 return FALSE;
480 size = msec->rawsize ? msec->rawsize : msec->size;
481 stash->debug_section = bfd_alloc (abfd, size);
483 if (! stash->debug_section)
484 return FALSE;
486 if (! bfd_get_section_contents (abfd, msec, stash->debug_section,
487 0, size))
489 stash->debug_section = 0;
490 return FALSE;
493 stash->debug_section_end = stash->debug_section + size;
494 stash->currentDie = stash->debug_section;
495 stash->abfd = abfd;
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)
502 return FALSE;
504 /* Look at the previously parsed units to see if any contain
505 the addr. */
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,
509 filename_ptr,
510 functionname_ptr,
511 linenumber_ptr);
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))
519 return FALSE;
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
533 not it's sibling. */
534 if (aDieInfo.sibling
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;
540 else
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,
545 filename_ptr,
546 functionname_ptr,
547 linenumber_ptr);
550 if (aDieInfo.sibling != 0)
551 stash->currentDie = stash->debug_section + aDieInfo.sibling;
552 else
553 stash->currentDie += aDieInfo.length;
556 return FALSE;