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