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