* symbols.c (symbol_X_add_number): Change return type to "offsetT *".
[deliverable/binutils-gdb.git] / bfd / dwarf1.c
CommitLineData
252b5132 1/* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
911d08a7 2 Copyright 1998, 1999, 2000, 2001, 2002, 2004 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
b34976b6
AM
129static struct dwarf1_unit *alloc_dwarf1_unit
130 PARAMS ((struct dwarf1_debug *));
a7b97311
AM
131static struct dwarf1_func *alloc_dwarf1_func
132 PARAMS ((struct dwarf1_debug *, struct dwarf1_unit *));
b34976b6
AM
133static bfd_boolean parse_die
134 PARAMS ((bfd *, struct die_info *, char *, char *));
135static bfd_boolean parse_line_table
a7b97311 136 PARAMS ((struct dwarf1_debug *, struct dwarf1_unit *));
b34976b6 137static bfd_boolean parse_functions_in_unit
a7b97311 138 PARAMS ((struct dwarf1_debug *, struct dwarf1_unit *));
b34976b6 139static bfd_boolean dwarf1_unit_find_nearest_line
a7b97311
AM
140 PARAMS ((struct dwarf1_debug *, struct dwarf1_unit *, unsigned long,
141 const char **, const char **, unsigned int *));
142
252b5132 143/* Return a newly allocated dwarf1_unit. It should be cleared and
98591c73 144 then attached into the 'stash' at 'stash->lastUnit'. */
252b5132
RH
145
146static struct dwarf1_unit*
147alloc_dwarf1_unit (stash)
148 struct dwarf1_debug* stash;
149{
dc810e39
AM
150 bfd_size_type amt = sizeof (struct dwarf1_unit);
151
152 struct dwarf1_unit* x = (struct dwarf1_unit*) bfd_zalloc (stash->abfd, amt);
252b5132
RH
153 x->prev = stash->lastUnit;
154 stash->lastUnit = x;
155
156 return x;
157}
158
159/* Return a newly allocated dwarf1_func. It must be cleared and
98591c73 160 attached into 'aUnit' at 'aUnit->func_list'. */
252b5132
RH
161
162static struct dwarf1_func*
163alloc_dwarf1_func (stash, aUnit)
164 struct dwarf1_debug* stash;
165 struct dwarf1_unit* aUnit;
166{
dc810e39
AM
167 bfd_size_type amt = sizeof (struct dwarf1_func);
168
169 struct dwarf1_func* x = (struct dwarf1_func*) bfd_zalloc (stash->abfd, amt);
252b5132
RH
170 x->prev = aUnit->func_list;
171 aUnit->func_list = x;
98591c73 172
252b5132
RH
173 return x;
174}
175
176/* parse_die - parse a Dwarf1 die.
177 Parse the die starting at 'aDiePtr' into 'aDieInfo'.
178 'abfd' must be the bfd from which the section that 'aDiePtr'
98591c73 179 points to was pulled from.
252b5132 180
b34976b6 181 Return FALSE if the die is invalidly formatted; TRUE otherwise. */
252b5132 182
b34976b6 183static bfd_boolean
bb731fb6 184parse_die (abfd, aDieInfo, aDiePtr, aDiePtrEnd)
252b5132
RH
185 bfd* abfd;
186 struct die_info* aDieInfo;
187 char* aDiePtr;
bb731fb6 188 char* aDiePtrEnd;
252b5132
RH
189{
190 char* this_die = aDiePtr;
191 char* xptr = this_die;
192
98591c73 193 memset (aDieInfo,0,sizeof (*aDieInfo));
252b5132 194
98591c73 195 /* First comes the length. */
b7af50e3 196 aDieInfo->length = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132 197 xptr += 4;
bb731fb6
L
198 if (aDieInfo->length == 0
199 || (this_die + aDieInfo->length) >= aDiePtrEnd)
b34976b6 200 return FALSE;
252b5132
RH
201 if (aDieInfo->length < 6)
202 {
98591c73 203 /* Just padding bytes. */
252b5132 204 aDieInfo->tag = TAG_padding;
b34976b6 205 return TRUE;
252b5132
RH
206 }
207
98591c73 208 /* Then the tag. */
b7af50e3 209 aDieInfo->tag = bfd_get_16 (abfd, (bfd_byte *) xptr);
252b5132 210 xptr += 2;
98591c73
KH
211
212 /* Then the attributes. */
252b5132
RH
213 while (xptr < (this_die + aDieInfo->length))
214 {
215 unsigned short attr;
98591c73
KH
216
217 /* Parse the attribute based on its form. This section
252b5132 218 must handle all dwarf1 forms, but need only handle the
98591c73 219 actual attributes that we care about. */
252b5132 220
b7af50e3 221 attr = bfd_get_16 (abfd, (bfd_byte *) xptr);
252b5132 222 xptr += 2;
98591c73 223
252b5132
RH
224 switch (FORM_FROM_ATTR (attr))
225 {
226 case FORM_DATA2:
227 xptr += 2;
228 break;
229 case FORM_DATA4:
230 case FORM_REF:
231 if (attr == AT_sibling)
b7af50e3 232 aDieInfo->sibling = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132
RH
233 else if (attr == AT_stmt_list)
234 {
b7af50e3 235 aDieInfo->stmt_list_offset = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132
RH
236 aDieInfo->has_stmt_list = 1;
237 }
238 xptr += 4;
239 break;
240 case FORM_DATA8:
241 xptr += 8;
242 break;
243 case FORM_ADDR:
244 if (attr == AT_low_pc)
b7af50e3 245 aDieInfo->low_pc = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132 246 else if (attr == AT_high_pc)
b7af50e3 247 aDieInfo->high_pc = bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132
RH
248 xptr += 4;
249 break;
250 case FORM_BLOCK2:
b7af50e3 251 xptr += 2 + bfd_get_16 (abfd, (bfd_byte *) xptr);
252b5132
RH
252 break;
253 case FORM_BLOCK4:
b7af50e3 254 xptr += 4 + bfd_get_32 (abfd, (bfd_byte *) xptr);
252b5132
RH
255 break;
256 case FORM_STRING:
257 if (attr == AT_name)
258 aDieInfo->name = xptr;
259 xptr += strlen (xptr) + 1;
260 break;
261 }
262 }
263
b34976b6 264 return TRUE;
252b5132
RH
265}
266
267/* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
b34976b6
AM
268 into 'aUnit->linenumber_table'. Return FALSE if an error
269 occurs; TRUE otherwise. */
98591c73 270
b34976b6 271static bfd_boolean
252b5132
RH
272parse_line_table (stash, aUnit)
273 struct dwarf1_debug* stash;
274 struct dwarf1_unit* aUnit;
275{
276 char* xptr;
277
98591c73 278 /* Load the ".line" section from the bfd if we haven't already. */
252b5132
RH
279 if (stash->line_section == 0)
280 {
281 asection *msec;
dc810e39 282 bfd_size_type size;
98591c73 283
252b5132
RH
284 msec = bfd_get_section_by_name (stash->abfd, ".line");
285 if (! msec)
b34976b6 286 return FALSE;
98591c73 287
eea6121a 288 size = msec->rawsize ? msec->rawsize : msec->size;
b7af50e3 289 stash->line_section = (char *) bfd_alloc (stash->abfd, size);
98591c73 290
252b5132 291 if (! stash->line_section)
b34976b6 292 return FALSE;
252b5132 293
dc810e39 294 if (! bfd_get_section_contents (stash->abfd, msec, stash->line_section,
eea6121a 295 0, size))
252b5132
RH
296 {
297 stash->line_section = 0;
b34976b6 298 return FALSE;
252b5132
RH
299 }
300
301 stash->line_section_end = stash->line_section + size;
302 }
303
304 xptr = stash->line_section + aUnit->stmt_list_offset;
305 if (xptr < stash->line_section_end)
306 {
7442e600 307 unsigned long eachLine;
dc810e39 308 char *tblend;
252b5132 309 unsigned long base;
dc810e39 310 bfd_size_type amt;
252b5132 311
98591c73 312 /* First comes the length. */
b7af50e3 313 tblend = bfd_get_32 (stash->abfd, (bfd_byte *) xptr) + xptr;
252b5132
RH
314 xptr += 4;
315
98591c73 316 /* Then the base address for each address in the table. */
b7af50e3 317 base = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
318 xptr += 4;
319
320 /* How many line entrys?
321 10 = 4 (line number) + 2 (pos in line) + 4 (address in line) */
322 aUnit->line_count = (tblend - xptr) / 10;
323
98591c73 324 /* Allocate an array for the entries. */
dc810e39
AM
325 amt = sizeof (struct linenumber) * aUnit->line_count;
326 aUnit->linenumber_table = ((struct linenumber *)
327 bfd_alloc (stash->abfd, amt));
98591c73 328
252b5132
RH
329 for (eachLine = 0; eachLine < aUnit->line_count; eachLine++)
330 {
98591c73 331 /* A line number. */
252b5132 332 aUnit->linenumber_table[eachLine].linenumber
b7af50e3 333 = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
334 xptr += 4;
335
98591c73 336 /* Skip the position within the line. */
252b5132
RH
337 xptr += 2;
338
98591c73
KH
339 /* And finally the address. */
340 aUnit->linenumber_table[eachLine].addr
b7af50e3 341 = base + bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
252b5132
RH
342 xptr += 4;
343 }
344 }
345
b34976b6 346 return TRUE;
252b5132
RH
347}
348
349/* Parse each function die in a compilation unit 'aUnit'.
350 The first child die of 'aUnit' should be in 'aUnit->first_child',
351 the result is placed in 'aUnit->func_list'.
b34976b6 352 Return FALSE if error; TRUE otherwise. */
252b5132 353
b34976b6 354static bfd_boolean
252b5132
RH
355parse_functions_in_unit (stash, aUnit)
356 struct dwarf1_debug* stash;
357 struct dwarf1_unit* aUnit;
358{
359 char* eachDie;
360
361 if (aUnit->first_child)
362 for (eachDie = aUnit->first_child;
363 eachDie < stash->debug_section_end;
364 )
365 {
366 struct die_info eachDieInfo;
98591c73 367
bb731fb6
L
368 if (! parse_die (stash->abfd, &eachDieInfo, eachDie,
369 stash->debug_section_end))
b34976b6 370 return FALSE;
98591c73 371
252b5132
RH
372 if (eachDieInfo.tag == TAG_global_subroutine
373 || eachDieInfo.tag == TAG_subroutine
374 || eachDieInfo.tag == TAG_inlined_subroutine
375 || eachDieInfo.tag == TAG_entry_point)
376 {
377 struct dwarf1_func* aFunc = alloc_dwarf1_func (stash,aUnit);
98591c73 378
252b5132
RH
379 aFunc->name = eachDieInfo.name;
380 aFunc->low_pc = eachDieInfo.low_pc;
381 aFunc->high_pc = eachDieInfo.high_pc;
382 }
98591c73 383
252b5132
RH
384 /* Move to next sibling, if none, end loop */
385 if (eachDieInfo.sibling)
386 eachDie = stash->debug_section + eachDieInfo.sibling;
387 else
388 break;
389 }
98591c73 390
b34976b6 391 return TRUE;
252b5132
RH
392}
393
394/* Find the nearest line to 'addr' in 'aUnit'.
98591c73 395 Return whether we found the line (or a function) without error. */
252b5132 396
b34976b6 397static bfd_boolean
98591c73 398dwarf1_unit_find_nearest_line (stash, aUnit, addr,
252b5132
RH
399 filename_ptr, functionname_ptr,
400 linenumber_ptr)
401 struct dwarf1_debug* stash;
402 struct dwarf1_unit* aUnit;
403 unsigned long addr;
404 const char **filename_ptr;
405 const char **functionname_ptr;
406 unsigned int *linenumber_ptr;
407{
b34976b6
AM
408 int line_p = FALSE;
409 int func_p = FALSE;
252b5132
RH
410
411 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
412 {
413 if (aUnit->has_stmt_list)
414 {
7442e600 415 unsigned long i;
252b5132
RH
416 struct dwarf1_func* eachFunc;
417
418 if (! aUnit->linenumber_table)
419 {
420 if (! parse_line_table (stash, aUnit))
b34976b6 421 return FALSE;
252b5132
RH
422 }
423
424 if (! aUnit->func_list)
425 {
426 if (! parse_functions_in_unit (stash, aUnit))
b34976b6 427 return FALSE;
252b5132
RH
428 }
429
430 for (i = 0; i < aUnit->line_count; i++)
431 {
432 if (aUnit->linenumber_table[i].addr <= addr
433 && addr < aUnit->linenumber_table[i+1].addr)
434 {
435 *filename_ptr = aUnit->name;
436 *linenumber_ptr = aUnit->linenumber_table[i].linenumber;
b34976b6 437 line_p = TRUE;
252b5132
RH
438 break;
439 }
440 }
441
98591c73
KH
442 for (eachFunc = aUnit->func_list;
443 eachFunc;
252b5132
RH
444 eachFunc = eachFunc->prev)
445 {
446 if (eachFunc->low_pc <= addr
447 && addr < eachFunc->high_pc)
448 {
449 *functionname_ptr = eachFunc->name;
b34976b6 450 func_p = TRUE;
252b5132
RH
451 break;
452 }
453 }
454 }
455 }
456
457 return line_p || func_p;
458}
459
252b5132 460/* The DWARF 1 version of find_nearest line.
b34976b6 461 Return TRUE if the line is found without error. */
252b5132 462
b34976b6 463bfd_boolean
252b5132
RH
464_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
465 filename_ptr, functionname_ptr, linenumber_ptr)
466 bfd *abfd;
467 asection *section;
7442e600 468 asymbol **symbols ATTRIBUTE_UNUSED;
252b5132
RH
469 bfd_vma offset;
470 const char **filename_ptr;
471 const char **functionname_ptr;
472 unsigned int *linenumber_ptr;
473{
474 struct dwarf1_debug *stash = elf_tdata (abfd)->dwarf1_find_line_info;
475
476 struct dwarf1_unit* eachUnit;
477
478 /* What address are we looking for? */
1548c54f 479 unsigned long addr = (unsigned long)(offset + section->vma);
252b5132
RH
480
481 *filename_ptr = NULL;
482 *functionname_ptr = NULL;
483 *linenumber_ptr = 0;
252b5132
RH
484
485 if (! stash)
486 {
487 asection *msec;
dc810e39 488 bfd_size_type size = sizeof (struct dwarf1_debug);
98591c73 489
dc810e39
AM
490 stash = elf_tdata (abfd)->dwarf1_find_line_info
491 = (struct dwarf1_debug *) bfd_zalloc (abfd, size);
98591c73 492
252b5132 493 if (! stash)
b34976b6 494 return FALSE;
98591c73 495
252b5132
RH
496 msec = bfd_get_section_by_name (abfd, ".debug");
497 if (! msec)
498 {
499 /* No dwarf1 info. Note that at this point the stash
500 has been allocated, but contains zeros, this lets
98591c73 501 future calls to this function fail quicker. */
b34976b6 502 return FALSE;
252b5132
RH
503 }
504
eea6121a 505 size = msec->rawsize ? msec->rawsize : msec->size;
b7af50e3 506 stash->debug_section = (char *) bfd_alloc (abfd, size);
98591c73 507
252b5132 508 if (! stash->debug_section)
b34976b6 509 return FALSE;
252b5132 510
dc810e39 511 if (! bfd_get_section_contents (abfd, msec, stash->debug_section,
eea6121a 512 0, size))
252b5132
RH
513 {
514 stash->debug_section = 0;
b34976b6 515 return FALSE;
252b5132
RH
516 }
517
518 stash->debug_section_end = stash->debug_section + size;
519 stash->currentDie = stash->debug_section;
520 stash->abfd = abfd;
521 }
522
523 /* A null debug_section indicates that there was no dwarf1 info
98591c73 524 or that an error occured while setting up the stash. */
252b5132
RH
525
526 if (! stash->debug_section)
b34976b6 527 return FALSE;
252b5132
RH
528
529 /* Look at the previously parsed units to see if any contain
98591c73 530 the addr. */
252b5132
RH
531 for (eachUnit = stash->lastUnit; eachUnit; eachUnit = eachUnit->prev)
532 {
533 if (eachUnit->low_pc <= addr && addr < eachUnit->high_pc)
534 return dwarf1_unit_find_nearest_line (stash, eachUnit, addr,
98591c73
KH
535 filename_ptr,
536 functionname_ptr,
252b5132
RH
537 linenumber_ptr);
538 }
539
540 while (stash->currentDie < stash->debug_section_end)
541 {
542 struct die_info aDieInfo;
543
bb731fb6
L
544 if (! parse_die (stash->abfd, &aDieInfo, stash->currentDie,
545 stash->debug_section_end))
b34976b6 546 return FALSE;
98591c73 547
252b5132
RH
548 if (aDieInfo.tag == TAG_compile_unit)
549 {
550 struct dwarf1_unit* aUnit
551 = alloc_dwarf1_unit (stash);
98591c73 552
252b5132
RH
553 aUnit->name = aDieInfo.name;
554 aUnit->low_pc = aDieInfo.low_pc;
555 aUnit->high_pc = aDieInfo.high_pc;
556 aUnit->has_stmt_list = aDieInfo.has_stmt_list;
557 aUnit->stmt_list_offset = aDieInfo.stmt_list_offset;
98591c73 558
252b5132 559 /* A die has a child if it's followed by a die that is
98591c73
KH
560 not it's sibling. */
561 if (aDieInfo.sibling
562 && stash->currentDie + aDieInfo.length
252b5132 563 < stash->debug_section_end
98591c73 564 && stash->currentDie + aDieInfo.length
252b5132
RH
565 != stash->debug_section + aDieInfo.sibling)
566 aUnit->first_child = stash->currentDie + aDieInfo.length;
567 else
568 aUnit->first_child = 0;
569
570 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
98591c73
KH
571 return dwarf1_unit_find_nearest_line (stash, aUnit, addr,
572 filename_ptr,
573 functionname_ptr,
252b5132
RH
574 linenumber_ptr);
575 }
98591c73 576
252b5132
RH
577 if (aDieInfo.sibling != 0)
578 stash->currentDie = stash->debug_section + aDieInfo.sibling;
579 else
580 stash->currentDie += aDieInfo.length;
581 }
582
b34976b6 583 return FALSE;
252b5132
RH
584}
585
252b5132 586/* EOF */
This page took 0.339299 seconds and 4 git commands to generate.