bfd/
[deliverable/binutils-gdb.git] / bfd / dwarf1.c
CommitLineData
252b5132 1/* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
a7b97311 2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
252b5132 3
98591c73 4Written by Gavin Romig-Koch of Cygnus Solutions (gavin@cygnus.com).
252b5132
RH
5
6This file is part of BFD.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or (at
11your option) any later version.
12
13This program is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22#include "bfd.h"
23#include "sysdep.h"
24#include "libiberty.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
27#include "elf/dwarf.h"
28
98591c73 29/* dwarf1_debug is the starting point for all dwarf1 info. */
252b5132
RH
30
31struct dwarf1_debug {
32
98591c73 33 /* The bfd we are working with. */
252b5132
RH
34 bfd* abfd;
35
98591c73 36 /* List of already parsed compilation units. */
252b5132
RH
37 struct dwarf1_unit* lastUnit;
38
98591c73
KH
39 /* The buffer for the .debug section.
40 Zero indicates that the .debug section failed to load. */
252b5132
RH
41 char* debug_section;
42
98591c73 43 /* Pointer to the end of the .debug_info section memory buffer. */
252b5132
RH
44 char* debug_section_end;
45
98591c73 46 /* The buffer for the .line section. */
252b5132
RH
47 char* line_section;
48
98591c73 49 /* End of that buffer. */
252b5132
RH
50 char* line_section_end;
51
98591c73 52 /* The current or next unread die within the .debug section. */
252b5132
RH
53 char* currentDie;
54};
55
98591c73 56/* One dwarf1_unit for each parsed compilation unit die. */
252b5132
RH
57
58struct dwarf1_unit {
98591c73 59 /* Linked starting from stash->lastUnit. */
252b5132
RH
60 struct dwarf1_unit* prev;
61
98591c73 62 /* Name of the compilation unit. */
252b5132
RH
63 char* name;
64
98591c73 65 /* The highest and lowest address used in the compilation unit. */
252b5132
RH
66 unsigned long low_pc;
67 unsigned long high_pc;
68
69 /* Does this unit have a statement list? */
70 int has_stmt_list;
71
98591c73 72 /* If any, the offset of the line number table in the .line section. */
252b5132
RH
73 unsigned long stmt_list_offset;
74
98591c73 75 /* If non-zero, a pointer to the first child of this unit. */
252b5132
RH
76 char* first_child;
77
78 /* How many line entries? */
79 unsigned long line_count;
80
98591c73 81 /* The decoded line number table (line_count entries). */
252b5132
RH
82 struct linenumber* linenumber_table;
83
98591c73 84 /* The list of functions in this unit. */
252b5132
RH
85 struct dwarf1_func* func_list;
86};
87
252b5132
RH
88/* One dwarf1_func for each parsed function die. */
89
90struct dwarf1_func {
98591c73 91 /* Linked starting from aUnit->func_list. */
252b5132 92 struct dwarf1_func* prev;
98591c73
KH
93
94 /* Name of function. */
252b5132 95 char* name;
98591c73
KH
96
97 /* The highest and lowest address used in the compilation unit. */
252b5132
RH
98 unsigned long low_pc;
99 unsigned long high_pc;
100};
101
98591c73 102/* Used to return info about a parsed die. */
252b5132
RH
103struct die_info {
104 unsigned long length;
105 unsigned long sibling;
106 unsigned long low_pc;
107 unsigned long high_pc;
108 unsigned long stmt_list_offset;
98591c73
KH
109
110 char* name;
111
252b5132
RH
112 int has_stmt_list;
113
114 unsigned short tag;
115};
116
98591c73 117/* Parsed line number information. */
252b5132 118struct linenumber {
98591c73 119 /* First address in the line. */
252b5132
RH
120 unsigned long addr;
121
98591c73 122 /* The line number. */
252b5132
RH
123 unsigned long linenumber;
124};
125
98591c73 126/* Find the form of an attr, from the attr field. */
252b5132
RH
127#define FORM_FROM_ATTR(attr) ((attr) & 0xF) /* Implicitly specified */
128
a7b97311
AM
129static struct dwarf1_unit *alloc_dwarf1_unit PARAMS ((struct dwarf1_debug *));
130static struct dwarf1_func *alloc_dwarf1_func
131 PARAMS ((struct dwarf1_debug *, struct dwarf1_unit *));
bb731fb6 132static boolean parse_die PARAMS ((bfd *, struct die_info *, char *, char *));
a7b97311
AM
133static boolean parse_line_table
134 PARAMS ((struct dwarf1_debug *, struct dwarf1_unit *));
135static boolean parse_functions_in_unit
136 PARAMS ((struct dwarf1_debug *, struct dwarf1_unit *));
137static boolean dwarf1_unit_find_nearest_line
138 PARAMS ((struct dwarf1_debug *, struct dwarf1_unit *, unsigned long,
139 const char **, const char **, unsigned int *));
140
252b5132 141/* Return a newly allocated dwarf1_unit. It should be cleared and
98591c73 142 then attached into the 'stash' at 'stash->lastUnit'. */
252b5132
RH
143
144static struct dwarf1_unit*
145alloc_dwarf1_unit (stash)
146 struct dwarf1_debug* stash;
147{
dc810e39
AM
148 bfd_size_type amt = sizeof (struct dwarf1_unit);
149
150 struct dwarf1_unit* x = (struct dwarf1_unit*) bfd_zalloc (stash->abfd, amt);
252b5132
RH
151 x->prev = stash->lastUnit;
152 stash->lastUnit = x;
153
154 return x;
155}
156
157/* Return a newly allocated dwarf1_func. It must be cleared and
98591c73 158 attached into 'aUnit' at 'aUnit->func_list'. */
252b5132
RH
159
160static struct dwarf1_func*
161alloc_dwarf1_func (stash, aUnit)
162 struct dwarf1_debug* stash;
163 struct dwarf1_unit* aUnit;
164{
dc810e39
AM
165 bfd_size_type amt = sizeof (struct dwarf1_func);
166
167 struct dwarf1_func* x = (struct dwarf1_func*) bfd_zalloc (stash->abfd, amt);
252b5132
RH
168 x->prev = aUnit->func_list;
169 aUnit->func_list = x;
98591c73 170
252b5132
RH
171 return x;
172}
173
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'
98591c73 177 points to was pulled from.
252b5132 178
98591c73 179 Return false if the die is invalidly formatted; true otherwise. */
252b5132
RH
180
181static boolean
bb731fb6 182parse_die (abfd, aDieInfo, aDiePtr, aDiePtrEnd)
252b5132
RH
183 bfd* abfd;
184 struct die_info* aDieInfo;
185 char* aDiePtr;
bb731fb6 186 char* aDiePtrEnd;
252b5132
RH
187{
188 char* this_die = aDiePtr;
189 char* xptr = this_die;
190
98591c73 191 memset (aDieInfo,0,sizeof (*aDieInfo));
252b5132 192
98591c73 193 /* First comes the length. */
b7af50e3 194 aDieInfo->length = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132 195 xptr += 4;
bb731fb6
L
196 if (aDieInfo->length == 0
197 || (this_die + aDieInfo->length) >= aDiePtrEnd)
a67a8777 198 return false;
252b5132
RH
199 if (aDieInfo->length < 6)
200 {
98591c73 201 /* Just padding bytes. */
252b5132
RH
202 aDieInfo->tag = TAG_padding;
203 return true;
204 }
205
98591c73 206 /* Then the tag. */
b7af50e3 207 aDieInfo->tag = bfd_get_16 (abfd, (bfd_byte *) xptr);
252b5132 208 xptr += 2;
98591c73
KH
209
210 /* Then the attributes. */
252b5132
RH
211 while (xptr < (this_die + aDieInfo->length))
212 {
213 unsigned short attr;
98591c73
KH
214
215 /* Parse the attribute based on its form. This section
252b5132 216 must handle all dwarf1 forms, but need only handle the
98591c73 217 actual attributes that we care about. */
252b5132 218
b7af50e3 219 attr = bfd_get_16 (abfd, (bfd_byte *) xptr);
252b5132 220 xptr += 2;
98591c73 221
252b5132
RH
222 switch (FORM_FROM_ATTR (attr))
223 {
224 case FORM_DATA2:
225 xptr += 2;
226 break;
227 case FORM_DATA4:
228 case FORM_REF:
229 if (attr == AT_sibling)
b7af50e3 230 aDieInfo->sibling = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132
RH
231 else if (attr == AT_stmt_list)
232 {
b7af50e3 233 aDieInfo->stmt_list_offset = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132
RH
234 aDieInfo->has_stmt_list = 1;
235 }
236 xptr += 4;
237 break;
238 case FORM_DATA8:
239 xptr += 8;
240 break;
241 case FORM_ADDR:
242 if (attr == AT_low_pc)
b7af50e3 243 aDieInfo->low_pc = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132 244 else if (attr == AT_high_pc)
b7af50e3 245 aDieInfo->high_pc = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132
RH
246 xptr += 4;
247 break;
248 case FORM_BLOCK2:
b7af50e3 249 xptr += 2 + bfd_get_16 (abfd, (bfd_byte *) xptr);
252b5132
RH
250 break;
251 case FORM_BLOCK4:
b7af50e3 252 xptr += 4 + bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132
RH
253 break;
254 case FORM_STRING:
255 if (attr == AT_name)
256 aDieInfo->name = xptr;
257 xptr += strlen (xptr) + 1;
258 break;
259 }
260 }
261
262 return true;
263}
264
265/* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
98591c73
KH
266 into 'aUnit->linenumber_table'. Return false if an error
267 occurs; true otherwise. */
268
252b5132
RH
269static boolean
270parse_line_table (stash, aUnit)
271 struct dwarf1_debug* stash;
272 struct dwarf1_unit* aUnit;
273{
274 char* xptr;
275
98591c73 276 /* Load the ".line" section from the bfd if we haven't already. */
252b5132
RH
277 if (stash->line_section == 0)
278 {
279 asection *msec;
dc810e39 280 bfd_size_type size;
98591c73 281
252b5132
RH
282 msec = bfd_get_section_by_name (stash->abfd, ".line");
283 if (! msec)
284 return false;
98591c73 285
252b5132 286 size = bfd_get_section_size_before_reloc (msec);
b7af50e3 287 stash->line_section = (char *) bfd_alloc (stash->abfd, size);
98591c73 288
252b5132
RH
289 if (! stash->line_section)
290 return false;
291
dc810e39
AM
292 if (! bfd_get_section_contents (stash->abfd, msec, stash->line_section,
293 (bfd_vma) 0, size))
252b5132
RH
294 {
295 stash->line_section = 0;
296 return false;
297 }
298
299 stash->line_section_end = stash->line_section + size;
300 }
301
302 xptr = stash->line_section + aUnit->stmt_list_offset;
303 if (xptr < stash->line_section_end)
304 {
7442e600 305 unsigned long eachLine;
dc810e39 306 char *tblend;
252b5132 307 unsigned long base;
dc810e39 308 bfd_size_type amt;
252b5132 309
98591c73 310 /* First comes the length. */
b7af50e3 311 tblend = bfd_get_32 (stash->abfd, (bfd_byte *) xptr) + xptr;
252b5132
RH
312 xptr += 4;
313
98591c73 314 /* Then the base address for each address in the table. */
b7af50e3 315 base = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
316 xptr += 4;
317
318 /* How many line entrys?
319 10 = 4 (line number) + 2 (pos in line) + 4 (address in line) */
320 aUnit->line_count = (tblend - xptr) / 10;
321
98591c73 322 /* Allocate an array for the entries. */
dc810e39
AM
323 amt = sizeof (struct linenumber) * aUnit->line_count;
324 aUnit->linenumber_table = ((struct linenumber *)
325 bfd_alloc (stash->abfd, amt));
98591c73 326
252b5132
RH
327 for (eachLine = 0; eachLine < aUnit->line_count; eachLine++)
328 {
98591c73 329 /* A line number. */
252b5132 330 aUnit->linenumber_table[eachLine].linenumber
b7af50e3 331 = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
332 xptr += 4;
333
98591c73 334 /* Skip the position within the line. */
252b5132
RH
335 xptr += 2;
336
98591c73
KH
337 /* And finally the address. */
338 aUnit->linenumber_table[eachLine].addr
b7af50e3 339 = base + bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
340 xptr += 4;
341 }
342 }
343
344 return true;
345}
346
347/* Parse each function die in a compilation unit 'aUnit'.
348 The first child die of 'aUnit' should be in 'aUnit->first_child',
349 the result is placed in 'aUnit->func_list'.
98591c73 350 Return false if error; true otherwise. */
252b5132
RH
351
352static boolean
353parse_functions_in_unit (stash, aUnit)
354 struct dwarf1_debug* stash;
355 struct dwarf1_unit* aUnit;
356{
357 char* eachDie;
358
359 if (aUnit->first_child)
360 for (eachDie = aUnit->first_child;
361 eachDie < stash->debug_section_end;
362 )
363 {
364 struct die_info eachDieInfo;
98591c73 365
bb731fb6
L
366 if (! parse_die (stash->abfd, &eachDieInfo, eachDie,
367 stash->debug_section_end))
252b5132 368 return false;
98591c73 369
252b5132
RH
370 if (eachDieInfo.tag == TAG_global_subroutine
371 || eachDieInfo.tag == TAG_subroutine
372 || eachDieInfo.tag == TAG_inlined_subroutine
373 || eachDieInfo.tag == TAG_entry_point)
374 {
375 struct dwarf1_func* aFunc = alloc_dwarf1_func (stash,aUnit);
98591c73 376
252b5132
RH
377 aFunc->name = eachDieInfo.name;
378 aFunc->low_pc = eachDieInfo.low_pc;
379 aFunc->high_pc = eachDieInfo.high_pc;
380 }
98591c73 381
252b5132
RH
382 /* Move to next sibling, if none, end loop */
383 if (eachDieInfo.sibling)
384 eachDie = stash->debug_section + eachDieInfo.sibling;
385 else
386 break;
387 }
98591c73 388
252b5132
RH
389 return true;
390}
391
392/* Find the nearest line to 'addr' in 'aUnit'.
98591c73 393 Return whether we found the line (or a function) without error. */
252b5132
RH
394
395static boolean
98591c73 396dwarf1_unit_find_nearest_line (stash, aUnit, addr,
252b5132
RH
397 filename_ptr, functionname_ptr,
398 linenumber_ptr)
399 struct dwarf1_debug* stash;
400 struct dwarf1_unit* aUnit;
401 unsigned long addr;
402 const char **filename_ptr;
403 const char **functionname_ptr;
404 unsigned int *linenumber_ptr;
405{
406 int line_p = false;
407 int func_p = false;
408
409 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
410 {
411 if (aUnit->has_stmt_list)
412 {
7442e600 413 unsigned long i;
252b5132
RH
414 struct dwarf1_func* eachFunc;
415
416 if (! aUnit->linenumber_table)
417 {
418 if (! parse_line_table (stash, aUnit))
419 return false;
420 }
421
422 if (! aUnit->func_list)
423 {
424 if (! parse_functions_in_unit (stash, aUnit))
425 return false;
426 }
427
428 for (i = 0; i < aUnit->line_count; i++)
429 {
430 if (aUnit->linenumber_table[i].addr <= addr
431 && addr < aUnit->linenumber_table[i+1].addr)
432 {
433 *filename_ptr = aUnit->name;
434 *linenumber_ptr = aUnit->linenumber_table[i].linenumber;
435 line_p = true;
436 break;
437 }
438 }
439
98591c73
KH
440 for (eachFunc = aUnit->func_list;
441 eachFunc;
252b5132
RH
442 eachFunc = eachFunc->prev)
443 {
444 if (eachFunc->low_pc <= addr
445 && addr < eachFunc->high_pc)
446 {
447 *functionname_ptr = eachFunc->name;
448 func_p = true;
449 break;
450 }
451 }
452 }
453 }
454
455 return line_p || func_p;
456}
457
252b5132 458/* The DWARF 1 version of find_nearest line.
98591c73 459 Return true if the line is found without error. */
252b5132
RH
460
461boolean
462_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
463 filename_ptr, functionname_ptr, linenumber_ptr)
464 bfd *abfd;
465 asection *section;
7442e600 466 asymbol **symbols ATTRIBUTE_UNUSED;
252b5132
RH
467 bfd_vma offset;
468 const char **filename_ptr;
469 const char **functionname_ptr;
470 unsigned int *linenumber_ptr;
471{
472 struct dwarf1_debug *stash = elf_tdata (abfd)->dwarf1_find_line_info;
473
474 struct dwarf1_unit* eachUnit;
475
476 /* What address are we looking for? */
1548c54f 477 unsigned long addr = (unsigned long)(offset + section->vma);
252b5132
RH
478
479 *filename_ptr = NULL;
480 *functionname_ptr = NULL;
481 *linenumber_ptr = 0;
252b5132
RH
482
483 if (! stash)
484 {
485 asection *msec;
dc810e39 486 bfd_size_type size = sizeof (struct dwarf1_debug);
98591c73 487
dc810e39
AM
488 stash = elf_tdata (abfd)->dwarf1_find_line_info
489 = (struct dwarf1_debug *) bfd_zalloc (abfd, size);
98591c73 490
252b5132
RH
491 if (! stash)
492 return false;
98591c73 493
252b5132
RH
494 msec = bfd_get_section_by_name (abfd, ".debug");
495 if (! msec)
496 {
497 /* No dwarf1 info. Note that at this point the stash
498 has been allocated, but contains zeros, this lets
98591c73 499 future calls to this function fail quicker. */
252b5132
RH
500 return false;
501 }
502
503 size = bfd_get_section_size_before_reloc (msec);
b7af50e3 504 stash->debug_section = (char *) bfd_alloc (abfd, size);
98591c73 505
252b5132
RH
506 if (! stash->debug_section)
507 return false;
508
dc810e39
AM
509 if (! bfd_get_section_contents (abfd, msec, stash->debug_section,
510 (bfd_vma) 0, size))
252b5132
RH
511 {
512 stash->debug_section = 0;
513 return false;
514 }
515
516 stash->debug_section_end = stash->debug_section + size;
517 stash->currentDie = stash->debug_section;
518 stash->abfd = abfd;
519 }
520
521 /* A null debug_section indicates that there was no dwarf1 info
98591c73 522 or that an error occured while setting up the stash. */
252b5132
RH
523
524 if (! stash->debug_section)
525 return false;
252b5132
RH
526
527 /* Look at the previously parsed units to see if any contain
98591c73 528 the addr. */
252b5132
RH
529 for (eachUnit = stash->lastUnit; eachUnit; eachUnit = eachUnit->prev)
530 {
531 if (eachUnit->low_pc <= addr && addr < eachUnit->high_pc)
532 return dwarf1_unit_find_nearest_line (stash, eachUnit, addr,
98591c73
KH
533 filename_ptr,
534 functionname_ptr,
252b5132
RH
535 linenumber_ptr);
536 }
537
538 while (stash->currentDie < stash->debug_section_end)
539 {
540 struct die_info aDieInfo;
541
bb731fb6
L
542 if (! parse_die (stash->abfd, &aDieInfo, stash->currentDie,
543 stash->debug_section_end))
252b5132 544 return false;
98591c73 545
252b5132
RH
546 if (aDieInfo.tag == TAG_compile_unit)
547 {
548 struct dwarf1_unit* aUnit
549 = alloc_dwarf1_unit (stash);
98591c73 550
252b5132
RH
551 aUnit->name = aDieInfo.name;
552 aUnit->low_pc = aDieInfo.low_pc;
553 aUnit->high_pc = aDieInfo.high_pc;
554 aUnit->has_stmt_list = aDieInfo.has_stmt_list;
555 aUnit->stmt_list_offset = aDieInfo.stmt_list_offset;
98591c73 556
252b5132 557 /* A die has a child if it's followed by a die that is
98591c73
KH
558 not it's sibling. */
559 if (aDieInfo.sibling
560 && stash->currentDie + aDieInfo.length
252b5132 561 < stash->debug_section_end
98591c73 562 && stash->currentDie + aDieInfo.length
252b5132
RH
563 != stash->debug_section + aDieInfo.sibling)
564 aUnit->first_child = stash->currentDie + aDieInfo.length;
565 else
566 aUnit->first_child = 0;
567
568 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
98591c73
KH
569 return dwarf1_unit_find_nearest_line (stash, aUnit, addr,
570 filename_ptr,
571 functionname_ptr,
252b5132
RH
572 linenumber_ptr);
573 }
98591c73 574
252b5132
RH
575 if (aDieInfo.sibling != 0)
576 stash->currentDie = stash->debug_section + aDieInfo.sibling;
577 else
578 stash->currentDie += aDieInfo.length;
579 }
580
581 return false;
582}
583
252b5132 584/* EOF */
This page took 0.157793 seconds and 4 git commands to generate.