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