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