* ld.texinfo: Document that fill values now use the four least
[deliverable/binutils-gdb.git] / bfd / dwarf2.c
CommitLineData
252b5132 1/* DWARF 2 support.
7898deda
NC
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
252b5132
RH
4
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
7
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
15
16This file is part of BFD.
17
18This program is free software; you can redistribute it and/or modify
19it under the terms of the GNU General Public License as published by
20the Free Software Foundation; either version 2 of the License, or (at
21your option) any later version.
22
23This program is distributed in the hope that it will be useful, but
24WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with this program; if not, write to the Free Software
30Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31
32#include "bfd.h"
33#include "sysdep.h"
34#include "libiberty.h"
35#include "libbfd.h"
36#include "elf-bfd.h"
37#include "elf/dwarf2.h"
38
39/* The data in the .debug_line statement prologue looks like this. */
a092b084 40
252b5132 41struct line_head
a092b084
NC
42{
43 unsigned int total_length;
44 unsigned short version;
45 unsigned int prologue_length;
46 unsigned char minimum_instruction_length;
47 unsigned char default_is_stmt;
48 int line_base;
49 unsigned char line_range;
50 unsigned char opcode_base;
51 unsigned char *standard_opcode_lengths;
52};
53
54/* Attributes have a name and a value. */
55
252b5132 56struct attribute
a092b084
NC
57{
58 enum dwarf_attribute name;
59 enum dwarf_form form;
60 union
252b5132 61 {
a092b084
NC
62 char *str;
63 struct dwarf_block *blk;
64 unsigned int unsnd;
65 int snd;
66 bfd_vma addr;
67 }
68 u;
69};
70
71/* Get at parts of an attribute structure. */
252b5132
RH
72
73#define DW_STRING(attr) ((attr)->u.str)
74#define DW_UNSND(attr) ((attr)->u.unsnd)
75#define DW_BLOCK(attr) ((attr)->u.blk)
76#define DW_SND(attr) ((attr)->u.snd)
77#define DW_ADDR(attr) ((attr)->u.addr)
78
98591c73 79/* Blocks are a bunch of untyped bytes. */
252b5132 80struct dwarf_block
a092b084
NC
81{
82 unsigned int size;
83 char *data;
84};
252b5132 85
a092b084
NC
86struct dwarf2_debug
87{
88 /* A list of all previously read comp_units. */
252b5132
RH
89 struct comp_unit* all_comp_units;
90
91 /* The next unread compilation unit within the .debug_info section.
92 Zero indicates that the .debug_info section has not been loaded
a092b084 93 into a buffer yet. */
252b5132
RH
94 char* info_ptr;
95
a092b084 96 /* Pointer to the end of the .debug_info section memory buffer. */
252b5132
RH
97 char* info_ptr_end;
98
f2363ce5
AO
99 /* Pointer to the section and address of the beginning of the
100 section. */
101 asection* sec;
102 char* sec_info_ptr;
103
104 /* Pointer to the symbol table. */
105 asymbol** syms;
106
a092b084 107 /* Pointer to the .debug_abbrev section loaded into memory. */
252b5132
RH
108 char* dwarf_abbrev_buffer;
109
a092b084 110 /* Length of the loaded .debug_abbrev section. */
252b5132 111 unsigned long dwarf_abbrev_size;
69dd2e2d
RH
112
113 /* Buffer for decode_line_info. */
114 char *dwarf_line_buffer;
ccdb16fc
JW
115
116 /* Length of the loaded .debug_line section. */
117 unsigned long dwarf_line_size;
252b5132
RH
118};
119
a092b084
NC
120struct arange
121{
f623be2b
RH
122 struct arange *next;
123 bfd_vma low;
124 bfd_vma high;
125};
252b5132 126
252b5132 127/* A minimal decoding of DWARF2 compilation units. We only decode
a092b084 128 what's needed to get to the line number information. */
252b5132 129
a092b084
NC
130struct comp_unit
131{
132 /* Chain the previously read compilation units. */
252b5132
RH
133 struct comp_unit* next_unit;
134
a092b084 135 /* Keep the bdf convenient (for memory allocation). */
252b5132
RH
136 bfd* abfd;
137
138 /* The lowest and higest addresses contained in this compilation
a092b084 139 unit as specified in the compilation unit header. */
f623be2b 140 struct arange arange;
252b5132 141
a092b084 142 /* The DW_AT_name attribute (for error messages). */
252b5132
RH
143 char* name;
144
a092b084 145 /* The abbrev hash table. */
252b5132
RH
146 struct abbrev_info** abbrevs;
147
a092b084 148 /* Note that an error was found by comp_unit_find_nearest_line. */
252b5132
RH
149 int error;
150
a092b084 151 /* The DW_AT_comp_dir attribute. */
252b5132
RH
152 char* comp_dir;
153
a092b084 154 /* True if there is a line number table associated with this comp. unit. */
252b5132 155 int stmtlist;
98591c73 156
a092b084 157 /* The offset into .debug_line of the line number table. */
252b5132
RH
158 unsigned long line_offset;
159
a092b084 160 /* Pointer to the first child die for the comp unit. */
252b5132
RH
161 char *first_child_die_ptr;
162
a092b084 163 /* The end of the comp unit. */
252b5132
RH
164 char *end_ptr;
165
a092b084 166 /* The decoded line number, NULL if not yet decoded. */
252b5132
RH
167 struct line_info_table* line_table;
168
a092b084 169 /* A list of the functions found in this comp. unit. */
98591c73 170 struct funcinfo* function_table;
252b5132 171
a092b084 172 /* Address size for this unit - from unit header. */
252b5132
RH
173 unsigned char addr_size;
174};
175
a7b97311
AM
176/* This data structure holds the information of an abbrev. */
177struct abbrev_info
178{
179 unsigned int number; /* Number identifying abbrev. */
180 enum dwarf_tag tag; /* DWARF tag. */
181 int has_children; /* Boolean. */
182 unsigned int num_attrs; /* Number of attributes. */
183 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
184 struct abbrev_info *next; /* Next in chain. */
185};
186
187struct attr_abbrev
188{
189 enum dwarf_attribute name;
190 enum dwarf_form form;
191};
192
193#ifndef ABBREV_HASH_SIZE
194#define ABBREV_HASH_SIZE 121
195#endif
196#ifndef ATTR_ALLOC_CHUNK
197#define ATTR_ALLOC_CHUNK 4
198#endif
199
200static unsigned int read_1_byte PARAMS ((bfd *, char *));
201static int read_1_signed_byte PARAMS ((bfd *, char *));
202static unsigned int read_2_bytes PARAMS ((bfd *, char *));
203static unsigned int read_4_bytes PARAMS ((bfd *, char *));
204static unsigned int read_8_bytes PARAMS ((bfd *, char *));
205static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int));
206static char *read_string PARAMS ((bfd *, char *, unsigned int *));
207static unsigned int read_unsigned_leb128
208 PARAMS ((bfd *, char *, unsigned int *));
209static int read_signed_leb128
210 PARAMS ((bfd *, char *, unsigned int *));
211static bfd_vma read_address PARAMS ((struct comp_unit *, char *));
212static struct abbrev_info *lookup_abbrev
213 PARAMS ((unsigned int, struct abbrev_info **));
214static struct abbrev_info **read_abbrevs
215 PARAMS ((bfd *, unsigned int, struct dwarf2_debug *));
216static char *read_attribute
217 PARAMS ((struct attribute *, struct attr_abbrev *,
218 struct comp_unit *, char *));
219static void add_line_info
220 PARAMS ((struct line_info_table *, bfd_vma, char *,
221 unsigned int, unsigned int, int));
222static char *concat_filename PARAMS ((struct line_info_table *, unsigned int));
223static void arange_add PARAMS ((struct comp_unit *, bfd_vma, bfd_vma));
224static struct line_info_table *decode_line_info
225 PARAMS ((struct comp_unit *, struct dwarf2_debug *));
226static boolean lookup_address_in_line_info_table
227 PARAMS ((struct line_info_table *, bfd_vma, const char **, unsigned int *));
228static boolean lookup_address_in_function_table
229 PARAMS ((struct funcinfo *, bfd_vma, const char **));
230static boolean scan_unit_for_functions PARAMS ((struct comp_unit *));
231static bfd_vma find_rela_addend
232 PARAMS ((bfd *, asection *, bfd_size_type, asymbol**));
233static struct comp_unit *parse_comp_unit
234 PARAMS ((bfd *, struct dwarf2_debug *, bfd_vma, unsigned int));
235static boolean comp_unit_contains_address
236 PARAMS ((struct comp_unit *, bfd_vma));
237static boolean comp_unit_find_nearest_line
238 PARAMS ((struct comp_unit *, bfd_vma, const char **, const char **,
239 unsigned int *, struct dwarf2_debug *));
240static asection *find_debug_info PARAMS ((bfd *, asection *));
241
98591c73
KH
242/* VERBATIM
243 The following function up to the END VERBATIM mark are
a092b084 244 copied directly from dwarf2read.c. */
252b5132 245
a092b084 246/* Read dwarf information from a buffer. */
252b5132
RH
247
248static unsigned int
249read_1_byte (abfd, buf)
7442e600 250 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
251 char *buf;
252{
253 return bfd_get_8 (abfd, (bfd_byte *) buf);
254}
255
256static int
257read_1_signed_byte (abfd, buf)
7442e600 258 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
259 char *buf;
260{
261 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
262}
263
264static unsigned int
265read_2_bytes (abfd, buf)
266 bfd *abfd;
267 char *buf;
268{
269 return bfd_get_16 (abfd, (bfd_byte *) buf);
270}
271
a092b084 272#if 0 /* This is not used. */
252b5132
RH
273
274static int
275read_2_signed_bytes (abfd, buf)
276 bfd *abfd;
277 char *buf;
278{
279 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
280}
281
282#endif
283
284static unsigned int
285read_4_bytes (abfd, buf)
286 bfd *abfd;
287 char *buf;
288{
289 return bfd_get_32 (abfd, (bfd_byte *) buf);
290}
291
a092b084 292#if 0 /* This is not used. */
252b5132
RH
293
294static int
295read_4_signed_bytes (abfd, buf)
296 bfd *abfd;
297 char *buf;
298{
299 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
300}
301
302#endif
303
304static unsigned int
305read_8_bytes (abfd, buf)
306 bfd *abfd;
307 char *buf;
308{
309 return bfd_get_64 (abfd, (bfd_byte *) buf);
310}
311
312static char *
313read_n_bytes (abfd, buf, size)
7442e600 314 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 315 char *buf;
7442e600 316 unsigned int size ATTRIBUTE_UNUSED;
252b5132
RH
317{
318 /* If the size of a host char is 8 bits, we can return a pointer
319 to the buffer, otherwise we have to copy the data to a buffer
320 allocated on the temporary obstack. */
321 return buf;
322}
323
324static char *
325read_string (abfd, buf, bytes_read_ptr)
7442e600 326 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
327 char *buf;
328 unsigned int *bytes_read_ptr;
329{
330 /* If the size of a host char is 8 bits, we can return a pointer
331 to the string, otherwise we have to copy the string to a buffer
332 allocated on the temporary obstack. */
333 if (*buf == '\0')
334 {
335 *bytes_read_ptr = 1;
336 return NULL;
337 }
98591c73 338
252b5132
RH
339 *bytes_read_ptr = strlen (buf) + 1;
340 return buf;
341}
342
343static unsigned int
344read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
7442e600 345 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
346 char *buf;
347 unsigned int *bytes_read_ptr;
348{
349 unsigned int result;
350 unsigned int num_read;
351 int shift;
352 unsigned char byte;
353
354 result = 0;
355 shift = 0;
356 num_read = 0;
98591c73 357
252b5132
RH
358 do
359 {
360 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
361 buf ++;
362 num_read ++;
363 result |= ((byte & 0x7f) << shift);
364 shift += 7;
365 }
366 while (byte & 0x80);
98591c73 367
252b5132 368 * bytes_read_ptr = num_read;
98591c73 369
252b5132
RH
370 return result;
371}
372
373static int
374read_signed_leb128 (abfd, buf, bytes_read_ptr)
7442e600
ILT
375 bfd *abfd ATTRIBUTE_UNUSED;
376 char *buf;
252b5132
RH
377 unsigned int * bytes_read_ptr;
378{
379 int result;
380 int shift;
381 int num_read;
382 unsigned char byte;
383
384 result = 0;
385 shift = 0;
386 num_read = 0;
387
388 do
389 {
390 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
391 buf ++;
392 num_read ++;
393 result |= ((byte & 0x7f) << shift);
394 shift += 7;
395 }
396 while (byte & 0x80);
98591c73 397
252b5132
RH
398 if ((shift < 32) && (byte & 0x40))
399 result |= -(1 << shift);
400
401 * bytes_read_ptr = num_read;
98591c73 402
252b5132
RH
403 return result;
404}
405
406/* END VERBATIM */
407
408static bfd_vma
409read_address (unit, buf)
410 struct comp_unit* unit;
411 char *buf;
412{
ecb651f0 413 switch (unit->addr_size)
252b5132 414 {
ecb651f0
NC
415 case 8:
416 return bfd_get_64 (unit->abfd, (bfd_byte *) buf);
417 case 4:
418 return bfd_get_32 (unit->abfd, (bfd_byte *) buf);
419 case 2:
420 return bfd_get_16 (unit->abfd, (bfd_byte *) buf);
421 default:
422 abort ();
252b5132 423 }
252b5132
RH
424}
425
252b5132
RH
426/* Lookup an abbrev_info structure in the abbrev hash table. */
427
428static struct abbrev_info *
429lookup_abbrev (number,abbrevs)
430 unsigned int number;
431 struct abbrev_info **abbrevs;
432{
433 unsigned int hash_number;
434 struct abbrev_info *abbrev;
435
436 hash_number = number % ABBREV_HASH_SIZE;
437 abbrev = abbrevs[hash_number];
438
439 while (abbrev)
440 {
441 if (abbrev->number == number)
442 return abbrev;
443 else
444 abbrev = abbrev->next;
445 }
98591c73 446
252b5132
RH
447 return NULL;
448}
449
450/* In DWARF version 2, the description of the debugging information is
451 stored in a separate .debug_abbrev section. Before we read any
452 dies from a section we read in all abbreviations and install them
453 in a hash table. */
454
455static struct abbrev_info**
51db3708 456read_abbrevs (abfd, offset, stash)
252b5132
RH
457 bfd * abfd;
458 unsigned int offset;
51db3708 459 struct dwarf2_debug *stash;
252b5132
RH
460{
461 struct abbrev_info **abbrevs;
462 char *abbrev_ptr;
463 struct abbrev_info *cur_abbrev;
464 unsigned int abbrev_number, bytes_read, abbrev_name;
465 unsigned int abbrev_form, hash_number;
252b5132
RH
466
467 if (! stash->dwarf_abbrev_buffer)
468 {
469 asection *msec;
470
471 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
472 if (! msec)
473 {
474 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
475 bfd_set_error (bfd_error_bad_value);
476 return 0;
477 }
98591c73 478
67d83c76 479 stash->dwarf_abbrev_size = msec->_raw_size;
7fafc0fd 480 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
252b5132
RH
481 if (! stash->dwarf_abbrev_buffer)
482 return 0;
98591c73
KH
483
484 if (! bfd_get_section_contents (abfd, msec,
252b5132
RH
485 stash->dwarf_abbrev_buffer, 0,
486 stash->dwarf_abbrev_size))
487 return 0;
488 }
489
f5198f61 490 if (offset >= stash->dwarf_abbrev_size)
252b5132 491 {
f5198f61 492 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) greater than or equal to abbrev size (%u)."),
252b5132
RH
493 offset, stash->dwarf_abbrev_size );
494 bfd_set_error (bfd_error_bad_value);
495 return 0;
496 }
497
98591c73 498 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE);
252b5132
RH
499
500 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
501 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
502 abbrev_ptr += bytes_read;
503
a092b084 504 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
505 while (abbrev_number)
506 {
507 cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
508
a092b084 509 /* Read in abbrev header. */
252b5132
RH
510 cur_abbrev->number = abbrev_number;
511 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
512 abbrev_ptr += bytes_read;
513 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
514 abbrev_ptr += 1;
515
a092b084 516 /* Now read in declarations. */
252b5132
RH
517 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
518 abbrev_ptr += bytes_read;
519 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
520 abbrev_ptr += bytes_read;
98591c73 521
252b5132
RH
522 while (abbrev_name)
523 {
524 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
525 {
526 cur_abbrev->attrs = (struct attr_abbrev *)
527 bfd_realloc (cur_abbrev->attrs,
528 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
529 * sizeof (struct attr_abbrev));
530 if (! cur_abbrev->attrs)
531 return 0;
532 }
98591c73 533
252b5132
RH
534 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
535 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
536 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
537 abbrev_ptr += bytes_read;
538 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
539 abbrev_ptr += bytes_read;
540 }
541
542 hash_number = abbrev_number % ABBREV_HASH_SIZE;
543 cur_abbrev->next = abbrevs[hash_number];
544 abbrevs[hash_number] = cur_abbrev;
545
546 /* Get next abbreviation.
547 Under Irix6 the abbreviations for a compilation unit are not
548 always properly terminated with an abbrev number of 0.
549 Exit loop if we encounter an abbreviation which we have
550 already read (which means we are about to read the abbreviations
551 for the next compile unit) or if the end of the abbreviation
552 table is reached. */
553 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
554 >= stash->dwarf_abbrev_size)
555 break;
556 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
557 abbrev_ptr += bytes_read;
558 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
559 break;
560 }
561
562 return abbrevs;
563}
564
565/* Read an attribute described by an abbreviated attribute. */
566
567static char *
568read_attribute (attr, abbrev, unit, info_ptr)
569 struct attribute *attr;
570 struct attr_abbrev *abbrev;
571 struct comp_unit *unit;
572 char *info_ptr;
573{
574 bfd *abfd = unit->abfd;
575 unsigned int bytes_read;
576 struct dwarf_block *blk;
577
578 attr->name = abbrev->name;
579 attr->form = abbrev->form;
98591c73 580
252b5132
RH
581 switch (abbrev->form)
582 {
583 case DW_FORM_addr:
584 case DW_FORM_ref_addr:
585 DW_ADDR (attr) = read_address (unit, info_ptr);
586 info_ptr += unit->addr_size;
587 break;
588 case DW_FORM_block2:
589 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
590 blk->size = read_2_bytes (abfd, info_ptr);
591 info_ptr += 2;
592 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
593 info_ptr += blk->size;
594 DW_BLOCK (attr) = blk;
595 break;
596 case DW_FORM_block4:
597 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
598 blk->size = read_4_bytes (abfd, info_ptr);
599 info_ptr += 4;
600 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
601 info_ptr += blk->size;
602 DW_BLOCK (attr) = blk;
603 break;
604 case DW_FORM_data2:
605 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
606 info_ptr += 2;
607 break;
608 case DW_FORM_data4:
609 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
610 info_ptr += 4;
611 break;
612 case DW_FORM_data8:
613 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
614 info_ptr += 8;
615 break;
616 case DW_FORM_string:
617 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
618 info_ptr += bytes_read;
619 break;
620 case DW_FORM_block:
621 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
622 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
623 info_ptr += bytes_read;
624 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
625 info_ptr += blk->size;
626 DW_BLOCK (attr) = blk;
627 break;
628 case DW_FORM_block1:
629 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
630 blk->size = read_1_byte (abfd, info_ptr);
631 info_ptr += 1;
632 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
633 info_ptr += blk->size;
634 DW_BLOCK (attr) = blk;
635 break;
636 case DW_FORM_data1:
637 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
638 info_ptr += 1;
639 break;
640 case DW_FORM_flag:
641 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
642 info_ptr += 1;
643 break;
644 case DW_FORM_sdata:
645 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
646 info_ptr += bytes_read;
647 break;
648 case DW_FORM_udata:
649 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
650 info_ptr += bytes_read;
651 break;
652 case DW_FORM_ref1:
653 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
654 info_ptr += 1;
655 break;
656 case DW_FORM_ref2:
657 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
658 info_ptr += 2;
659 break;
660 case DW_FORM_ref4:
661 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
662 info_ptr += 4;
663 break;
81edd86d
MM
664 case DW_FORM_ref8:
665 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
666 info_ptr += 8;
667 break;
252b5132
RH
668 case DW_FORM_ref_udata:
669 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
670 info_ptr += bytes_read;
671 break;
672 case DW_FORM_strp:
673 case DW_FORM_indirect:
674 default:
675 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
676 abbrev->form);
677 bfd_set_error (bfd_error_bad_value);
678 }
679 return info_ptr;
680}
681
a092b084 682/* Source line information table routines. */
252b5132
RH
683
684#define FILE_ALLOC_CHUNK 5
685#define DIR_ALLOC_CHUNK 5
686
a092b084
NC
687struct line_info
688{
252b5132 689 struct line_info* prev_line;
252b5132
RH
690 bfd_vma address;
691 char* filename;
692 unsigned int line;
693 unsigned int column;
a092b084 694 int end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
695};
696
a092b084
NC
697struct fileinfo
698{
252b5132
RH
699 char *name;
700 unsigned int dir;
701 unsigned int time;
702 unsigned int size;
703};
704
a092b084
NC
705struct line_info_table
706{
252b5132 707 bfd* abfd;
252b5132
RH
708 unsigned int num_files;
709 unsigned int num_dirs;
252b5132
RH
710 char* comp_dir;
711 char** dirs;
712 struct fileinfo* files;
713 struct line_info* last_line;
714};
715
98591c73 716static void
159002ff 717add_line_info (table, address, filename, line, column, end_sequence)
252b5132
RH
718 struct line_info_table* table;
719 bfd_vma address;
720 char* filename;
721 unsigned int line;
722 unsigned int column;
159002ff 723 int end_sequence;
252b5132
RH
724{
725 struct line_info* info = (struct line_info*)
726 bfd_alloc (table->abfd, sizeof (struct line_info));
727
728 info->prev_line = table->last_line;
729 table->last_line = info;
730
731 info->address = address;
732 info->filename = filename;
733 info->line = line;
734 info->column = column;
159002ff 735 info->end_sequence = end_sequence;
252b5132
RH
736}
737
a092b084 738static char *
252b5132
RH
739concat_filename (table, file)
740 struct line_info_table* table;
741 unsigned int file;
742{
159002ff
RH
743 char* filename;
744
745 if (file - 1 >= table->num_files)
746 {
dcdea4f4
AM
747 (*_bfd_error_handler)
748 (_("Dwarf Error: mangled line number section (bad file number)."));
159002ff
RH
749 return "<unknown>";
750 }
751
752 filename = table->files[file - 1].name;
51db3708 753 if (IS_ABSOLUTE_PATH(filename))
252b5132
RH
754 return filename;
755
756 else
757 {
758 char* dirname = (table->files[file - 1].dir
759 ? table->dirs[table->files[file - 1].dir - 1]
760 : table->comp_dir);
761 return (char*) concat (dirname, "/", filename, NULL);
762 }
763}
764
f623be2b
RH
765static void
766arange_add (unit, low_pc, high_pc)
767 struct comp_unit *unit;
768 bfd_vma low_pc;
769 bfd_vma high_pc;
770{
771 struct arange *arange;
772
a092b084 773 /* First see if we can cheaply extend an existing range. */
f623be2b 774 arange = &unit->arange;
98591c73 775
f623be2b
RH
776 do
777 {
778 if (low_pc == arange->high)
779 {
780 arange->high = high_pc;
781 return;
782 }
783 if (high_pc == arange->low)
784 {
785 arange->low = low_pc;
786 return;
787 }
788 arange = arange->next;
789 }
790 while (arange);
791
792 if (unit->arange.high == 0)
793 {
a092b084 794 /* This is the first address range: store it in unit->arange. */
f623be2b
RH
795 unit->arange.next = 0;
796 unit->arange.low = low_pc;
797 unit->arange.high = high_pc;
798 return;
799 }
800
a092b084 801 /* Need to allocate a new arange and insert it into the arange list. */
f623be2b
RH
802 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
803 arange->low = low_pc;
804 arange->high = high_pc;
805
806 arange->next = unit->arange.next;
807 unit->arange.next = arange;
808}
809
a092b084 810/* Decode the line number information for UNIT. */
252b5132
RH
811
812static struct line_info_table*
51db3708 813decode_line_info (unit, stash)
252b5132 814 struct comp_unit *unit;
51db3708 815 struct dwarf2_debug *stash;
252b5132
RH
816{
817 bfd *abfd = unit->abfd;
252b5132 818 struct line_info_table* table;
252b5132
RH
819 char *line_ptr;
820 char *line_end;
821 struct line_head lh;
822 unsigned int i, bytes_read;
823 char *cur_file, *cur_dir;
824 unsigned char op_code, extended_op, adj_opcode;
825
69dd2e2d 826 if (! stash->dwarf_line_buffer)
252b5132
RH
827 {
828 asection *msec;
252b5132
RH
829
830 msec = bfd_get_section_by_name (abfd, ".debug_line");
831 if (! msec)
832 {
833 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
834 bfd_set_error (bfd_error_bad_value);
835 return 0;
836 }
98591c73 837
ccdb16fc
JW
838 stash->dwarf_line_size = msec->_raw_size;
839 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, stash->dwarf_line_size);
f623be2b 840 if (! stash->dwarf_line_buffer)
252b5132
RH
841 return 0;
842
98591c73 843 if (! bfd_get_section_contents (abfd, msec,
f623be2b 844 stash->dwarf_line_buffer, 0,
ccdb16fc 845 stash->dwarf_line_size))
252b5132
RH
846 return 0;
847
848 /* FIXME: We ought to apply the relocs against this section before
a092b084 849 we process it... */
252b5132
RH
850 }
851
ccdb16fc
JW
852 /* Since we are using un-relocated data, it is possible to get a bad value
853 for the line_offset. Validate it here so that we won't get a segfault
854 below. */
855 if (unit->line_offset >= stash->dwarf_line_size)
856 {
f5198f61 857 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%u) greater than or equal to line size (%u)."),
ccdb16fc
JW
858 unit->line_offset, stash->dwarf_line_size);
859 bfd_set_error (bfd_error_bad_value);
860 return 0;
861 }
862
98591c73 863 table = (struct line_info_table*) bfd_alloc (abfd,
252b5132
RH
864 sizeof (struct line_info_table));
865 table->abfd = abfd;
866 table->comp_dir = unit->comp_dir;
867
868 table->num_files = 0;
869 table->files = NULL;
870
871 table->num_dirs = 0;
872 table->dirs = NULL;
873
159002ff
RH
874 table->files = NULL;
875 table->last_line = NULL;
876
69dd2e2d 877 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 878
a092b084 879 /* Read in the prologue. */
252b5132
RH
880 lh.total_length = read_4_bytes (abfd, line_ptr);
881 line_ptr += 4;
882 line_end = line_ptr + lh.total_length;
883 lh.version = read_2_bytes (abfd, line_ptr);
884 line_ptr += 2;
885 lh.prologue_length = read_4_bytes (abfd, line_ptr);
886 line_ptr += 4;
887 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
888 line_ptr += 1;
889 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
890 line_ptr += 1;
891 lh.line_base = read_1_signed_byte (abfd, line_ptr);
892 line_ptr += 1;
893 lh.line_range = read_1_byte (abfd, line_ptr);
894 line_ptr += 1;
895 lh.opcode_base = read_1_byte (abfd, line_ptr);
896 line_ptr += 1;
897 lh.standard_opcode_lengths = (unsigned char *)
898 bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
899
900 lh.standard_opcode_lengths[0] = 1;
98591c73 901
252b5132
RH
902 for (i = 1; i < lh.opcode_base; ++i)
903 {
904 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
905 line_ptr += 1;
906 }
907
a092b084 908 /* Read directory table. */
252b5132
RH
909 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
910 {
911 line_ptr += bytes_read;
98591c73 912
252b5132
RH
913 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
914 {
915 table->dirs = (char **)
916 bfd_realloc (table->dirs,
917 (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
918 if (! table->dirs)
919 return 0;
920 }
98591c73 921
252b5132
RH
922 table->dirs[table->num_dirs++] = cur_dir;
923 }
98591c73 924
252b5132
RH
925 line_ptr += bytes_read;
926
a092b084 927 /* Read file name table. */
252b5132
RH
928 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
929 {
930 line_ptr += bytes_read;
98591c73 931
252b5132
RH
932 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
933 {
934 table->files = (struct fileinfo *)
935 bfd_realloc (table->files,
936 (table->num_files + FILE_ALLOC_CHUNK)
937 * sizeof (struct fileinfo));
938 if (! table->files)
939 return 0;
940 }
98591c73 941
252b5132
RH
942 table->files[table->num_files].name = cur_file;
943 table->files[table->num_files].dir =
944 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
945 line_ptr += bytes_read;
946 table->files[table->num_files].time =
947 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
948 line_ptr += bytes_read;
949 table->files[table->num_files].size =
950 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
951 line_ptr += bytes_read;
952 table->num_files++;
953 }
98591c73 954
252b5132
RH
955 line_ptr += bytes_read;
956
957 /* Read the statement sequences until there's nothing left. */
958 while (line_ptr < line_end)
959 {
a092b084 960 /* State machine registers. */
252b5132
RH
961 bfd_vma address = 0;
962 char* filename = concat_filename (table, 1);
963 unsigned int line = 1;
964 unsigned int column = 0;
965 int is_stmt = lh.default_is_stmt;
966 int basic_block = 0;
f623be2b
RH
967 int end_sequence = 0, need_low_pc = 1;
968 bfd_vma low_pc = 0;
252b5132 969
a092b084 970 /* Decode the table. */
252b5132
RH
971 while (! end_sequence)
972 {
973 op_code = read_1_byte (abfd, line_ptr);
974 line_ptr += 1;
98591c73 975
252b5132
RH
976 switch (op_code)
977 {
978 case DW_LNS_extended_op:
a092b084 979 line_ptr += 1; /* Ignore length. */
252b5132
RH
980 extended_op = read_1_byte (abfd, line_ptr);
981 line_ptr += 1;
982 switch (extended_op)
983 {
984 case DW_LNE_end_sequence:
985 end_sequence = 1;
f623be2b
RH
986 add_line_info (table, address, filename, line, column,
987 end_sequence);
988 if (need_low_pc)
989 {
990 need_low_pc = 0;
991 low_pc = address;
992 }
993 arange_add (unit, low_pc, address);
252b5132
RH
994 break;
995 case DW_LNE_set_address:
996 address = read_address (unit, line_ptr);
997 line_ptr += unit->addr_size;
998 break;
999 case DW_LNE_define_file:
1000 cur_file = read_string (abfd, line_ptr, &bytes_read);
1001 line_ptr += bytes_read;
1002 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1003 {
1004 table->files = (struct fileinfo *)
1005 bfd_realloc (table->files,
1006 (table->num_files + FILE_ALLOC_CHUNK)
1007 * sizeof (struct fileinfo));
1008 if (! table->files)
1009 return 0;
1010 }
1011 table->files[table->num_files].name = cur_file;
1012 table->files[table->num_files].dir =
1013 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1014 line_ptr += bytes_read;
1015 table->files[table->num_files].time =
1016 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1017 line_ptr += bytes_read;
1018 table->files[table->num_files].size =
1019 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1020 line_ptr += bytes_read;
1021 table->num_files++;
1022 break;
1023 default:
1024 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1025 bfd_set_error (bfd_error_bad_value);
1026 return 0;
1027 }
1028 break;
1029 case DW_LNS_copy:
159002ff 1030 add_line_info (table, address, filename, line, column, 0);
252b5132 1031 basic_block = 0;
f623be2b
RH
1032 if (need_low_pc)
1033 {
1034 need_low_pc = 0;
1035 low_pc = address;
1036 }
252b5132
RH
1037 break;
1038 case DW_LNS_advance_pc:
1039 address += lh.minimum_instruction_length
1040 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1041 line_ptr += bytes_read;
1042 break;
1043 case DW_LNS_advance_line:
1044 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1045 line_ptr += bytes_read;
1046 break;
1047 case DW_LNS_set_file:
1048 {
1049 unsigned int file;
1050
1051 /* The file and directory tables are 0 based, the references
1052 are 1 based. */
1053 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1054 line_ptr += bytes_read;
1055 filename = concat_filename (table, file);
1056 break;
1057 }
1058 case DW_LNS_set_column:
1059 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1060 line_ptr += bytes_read;
1061 break;
1062 case DW_LNS_negate_stmt:
1063 is_stmt = (!is_stmt);
1064 break;
1065 case DW_LNS_set_basic_block:
1066 basic_block = 1;
1067 break;
1068 case DW_LNS_const_add_pc:
159002ff
RH
1069 address += lh.minimum_instruction_length
1070 * ((255 - lh.opcode_base) / lh.line_range);
252b5132
RH
1071 break;
1072 case DW_LNS_fixed_advance_pc:
1073 address += read_2_bytes (abfd, line_ptr);
1074 line_ptr += 2;
1075 break;
a092b084 1076 default: /* Special operand. */
252b5132
RH
1077 adj_opcode = op_code - lh.opcode_base;
1078 address += (adj_opcode / lh.line_range)
1079 * lh.minimum_instruction_length;
1080 line += lh.line_base + (adj_opcode % lh.line_range);
a092b084 1081 /* Append row to matrix using current values. */
159002ff 1082 add_line_info (table, address, filename, line, column, 0);
252b5132 1083 basic_block = 1;
f623be2b
RH
1084 if (need_low_pc)
1085 {
1086 need_low_pc = 0;
1087 low_pc = address;
1088 }
252b5132
RH
1089 }
1090 }
1091 }
1092
1093 return table;
1094}
1095
252b5132
RH
1096/* If ADDR is within TABLE set the output parameters and return true,
1097 otherwise return false. The output parameters, FILENAME_PTR and
a092b084 1098 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132
RH
1099
1100static boolean
98591c73 1101lookup_address_in_line_info_table (table,
252b5132 1102 addr,
98591c73 1103 filename_ptr,
252b5132
RH
1104 linenumber_ptr)
1105 struct line_info_table* table;
1106 bfd_vma addr;
1107 const char **filename_ptr;
1108 unsigned int *linenumber_ptr;
1109{
159002ff 1110 struct line_info* next_line = table->last_line;
252b5132 1111 struct line_info* each_line;
98591c73 1112
159002ff
RH
1113 if (!next_line)
1114 return false;
1115
1116 each_line = next_line->prev_line;
1117
1118 while (each_line && next_line)
252b5132 1119 {
159002ff
RH
1120 if (!each_line->end_sequence
1121 && addr >= each_line->address && addr < next_line->address)
252b5132
RH
1122 {
1123 *filename_ptr = each_line->filename;
1124 *linenumber_ptr = each_line->line;
1125 return true;
1126 }
159002ff
RH
1127 next_line = each_line;
1128 each_line = each_line->prev_line;
252b5132 1129 }
98591c73 1130
252b5132
RH
1131 return false;
1132}
98591c73 1133
a092b084 1134/* Function table functions. */
252b5132 1135
a092b084
NC
1136struct funcinfo
1137{
252b5132 1138 struct funcinfo *prev_func;
252b5132
RH
1139 char* name;
1140 bfd_vma low;
1141 bfd_vma high;
1142};
1143
98591c73 1144/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
252b5132
RH
1145
1146static boolean
98591c73 1147lookup_address_in_function_table (table,
252b5132
RH
1148 addr,
1149 functionname_ptr)
1150 struct funcinfo* table;
1151 bfd_vma addr;
1152 const char **functionname_ptr;
1153{
1154 struct funcinfo* each_func;
1155
1156 for (each_func = table;
1157 each_func;
1158 each_func = each_func->prev_func)
1159 {
1160 if (addr >= each_func->low && addr < each_func->high)
1161 {
1162 *functionname_ptr = each_func->name;
1163 return true;
1164 }
1165 }
98591c73 1166
252b5132
RH
1167 return false;
1168}
1169
a092b084 1170/* DWARF2 Compilation unit functions. */
252b5132
RH
1171
1172/* Scan over each die in a comp. unit looking for functions to add
a092b084 1173 to the function table. */
252b5132
RH
1174
1175static boolean
1176scan_unit_for_functions (unit)
1177 struct comp_unit *unit;
1178{
1179 bfd *abfd = unit->abfd;
1180 char *info_ptr = unit->first_child_die_ptr;
1181 int nesting_level = 1;
1182
1183 while (nesting_level)
1184 {
1185 unsigned int abbrev_number, bytes_read, i;
1186 struct abbrev_info *abbrev;
1187 struct attribute attr;
1188 struct funcinfo *func;
1189 char* name = 0;
1190
1191 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1192 info_ptr += bytes_read;
1193
1194 if (! abbrev_number)
1195 {
1196 nesting_level--;
1197 continue;
1198 }
98591c73 1199
252b5132
RH
1200 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1201 if (! abbrev)
1202 {
98591c73 1203 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
252b5132
RH
1204 abbrev_number);
1205 bfd_set_error (bfd_error_bad_value);
1206 return false;
1207 }
98591c73 1208
252b5132
RH
1209 if (abbrev->tag == DW_TAG_subprogram)
1210 {
1211 func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1212 func->prev_func = unit->function_table;
1213 unit->function_table = func;
1214 }
1215 else
1216 func = NULL;
98591c73 1217
252b5132
RH
1218 for (i = 0; i < abbrev->num_attrs; ++i)
1219 {
1220 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
98591c73 1221
252b5132
RH
1222 if (func)
1223 {
1224 switch (attr.name)
1225 {
1226 case DW_AT_name:
98591c73 1227
252b5132
RH
1228 name = DW_STRING (&attr);
1229
1230 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1231 if (func->name == NULL)
1232 func->name = DW_STRING (&attr);
1233 break;
98591c73 1234
252b5132
RH
1235 case DW_AT_MIPS_linkage_name:
1236 func->name = DW_STRING (&attr);
1237 break;
1238
1239 case DW_AT_low_pc:
1240 func->low = DW_ADDR (&attr);
1241 break;
1242
1243 case DW_AT_high_pc:
1244 func->high = DW_ADDR (&attr);
1245 break;
1246
1247 default:
1248 break;
1249 }
1250 }
1251 else
1252 {
1253 switch (attr.name)
1254 {
1255 case DW_AT_name:
1256 name = DW_STRING (&attr);
1257 break;
98591c73 1258
252b5132
RH
1259 default:
1260 break;
1261 }
1262 }
1263 }
1264
1265 if (abbrev->has_children)
1266 nesting_level++;
1267 }
1268
1269 return true;
1270}
1271
f2363ce5
AO
1272/* Look for a RELA relocation to be applied on OFFSET of section SEC,
1273 and return the addend if such a relocation is found. Since this is
1274 only used to find relocations referring to the .debug_abbrev
1275 section, we make sure the relocation refers to this section, but
1276 this is not strictly necessary, and it can probably be safely
1277 removed if needed. However, it is important to note that this
1278 function only returns the addend, it doesn't serve the purpose of
1279 applying a generic relocation.
1280
1281 If no suitable relocation is found, or if it is not a real RELA
1282 relocation, this function returns 0. */
1283
1284static bfd_vma
1285find_rela_addend (abfd, sec, offset, syms)
1286 bfd* abfd;
1287 asection* sec;
1288 bfd_size_type offset;
1289 asymbol** syms;
1290{
1291 long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
1292 arelent **relocs = NULL;
1293 long reloc_count, relc;
1294
1295 if (reloc_size <= 0)
1296 return 0;
1297
1298 relocs = (arelent **) bfd_malloc ((size_t) reloc_size);
1299 if (relocs == NULL)
1300 return 0;
1301
1302 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, syms);
1303
1304 if (reloc_count <= 0)
1305 {
1306 free (relocs);
1307 return 0;
1308 }
1309
1310 for (relc = 0; relc < reloc_count; relc++)
1311 if (relocs[relc]->address == offset
1312 && (*relocs[relc]->sym_ptr_ptr)->flags & BSF_SECTION_SYM
1313 && strcmp ((*relocs[relc]->sym_ptr_ptr)->name,
1314 ".debug_abbrev") == 0)
1315 {
1316 bfd_vma addend = (relocs[relc]->howto->partial_inplace
1317 ? 0 : relocs[relc]->addend);
1318 free (relocs);
1319 return addend;
1320 }
1321
1322 free (relocs);
1323 return 0;
1324}
1325
5e38c3b8
MM
1326/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1327 includes the compilation unit header that proceeds the DIE's, but
1328 does not include the length field that preceeds each compilation
1329 unit header. END_PTR points one past the end of this comp unit.
1330 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1331 is assumed to be four bytes. Otherwise, it it is the size given.
252b5132
RH
1332
1333 This routine does not read the whole compilation unit; only enough
1334 to get to the line number information for the compilation unit. */
1335
1336static struct comp_unit *
51db3708 1337parse_comp_unit (abfd, stash, unit_length, abbrev_length)
252b5132 1338 bfd* abfd;
51db3708
NC
1339 struct dwarf2_debug *stash;
1340 bfd_vma unit_length;
5e38c3b8 1341 unsigned int abbrev_length;
252b5132
RH
1342{
1343 struct comp_unit* unit;
1344
1345 unsigned short version;
7442e600 1346 unsigned int abbrev_offset = 0;
252b5132
RH
1347 unsigned char addr_size;
1348 struct abbrev_info** abbrevs;
1349
1350 unsigned int abbrev_number, bytes_read, i;
1351 struct abbrev_info *abbrev;
1352 struct attribute attr;
1353
51db3708
NC
1354 char *info_ptr = stash->info_ptr;
1355 char *end_ptr = info_ptr + unit_length;
3fde5a36 1356
252b5132
RH
1357 version = read_2_bytes (abfd, info_ptr);
1358 info_ptr += 2;
5e38c3b8
MM
1359 BFD_ASSERT (abbrev_length == 0
1360 || abbrev_length == 4
1361 || abbrev_length == 8);
1362 if (abbrev_length == 0 || abbrev_length == 4)
1363 abbrev_offset = read_4_bytes (abfd, info_ptr);
1364 else if (abbrev_length == 8)
1365 abbrev_offset = read_8_bytes (abfd, info_ptr);
f2363ce5
AO
1366 /* The abbrev offset is generally a relocation pointing to
1367 .debug_abbrev+offset. On RELA targets, we have to find the
1368 relocation and extract the addend to obtain the actual
1369 abbrev_offset, so do it here. */
1370 abbrev_offset += find_rela_addend (abfd, stash->sec,
1371 info_ptr - stash->sec_info_ptr,
1372 stash->syms);
5e38c3b8 1373 info_ptr += abbrev_length;
252b5132
RH
1374 addr_size = read_1_byte (abfd, info_ptr);
1375 info_ptr += 1;
1376
1377 if (version != 2)
1378 {
1379 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1380 bfd_set_error (bfd_error_bad_value);
1381 return 0;
1382 }
1383
1384 if (addr_size > sizeof (bfd_vma))
1385 {
1386 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1387 addr_size,
1388 sizeof (bfd_vma));
1389 bfd_set_error (bfd_error_bad_value);
1390 return 0;
1391 }
1392
ecb651f0 1393 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 1394 {
ecb651f0 1395 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size );
252b5132
RH
1396 bfd_set_error (bfd_error_bad_value);
1397 return 0;
1398 }
1399
a092b084 1400 /* Read the abbrevs for this compilation unit into a table. */
51db3708 1401 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
1402 if (! abbrevs)
1403 return 0;
1404
1405 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1406 info_ptr += bytes_read;
1407 if (! abbrev_number)
1408 {
1409 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1410 abbrev_number);
1411 bfd_set_error (bfd_error_bad_value);
1412 return 0;
1413 }
1414
1415 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1416 if (! abbrev)
1417 {
1418 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1419 abbrev_number);
1420 bfd_set_error (bfd_error_bad_value);
1421 return 0;
1422 }
98591c73 1423
252b5132
RH
1424 unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1425 unit->abfd = abfd;
98591c73 1426 unit->addr_size = addr_size;
252b5132
RH
1427 unit->abbrevs = abbrevs;
1428 unit->end_ptr = end_ptr;
1429
1430 for (i = 0; i < abbrev->num_attrs; ++i)
1431 {
1432 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1433
1434 /* Store the data if it is of an attribute we want to keep in a
1435 partial symbol table. */
1436 switch (attr.name)
1437 {
1438 case DW_AT_stmt_list:
1439 unit->stmtlist = 1;
1440 unit->line_offset = DW_UNSND (&attr);
1441 break;
1442
1443 case DW_AT_name:
1444 unit->name = DW_STRING (&attr);
1445 break;
1446
1447 case DW_AT_low_pc:
f623be2b 1448 unit->arange.low = DW_ADDR (&attr);
252b5132
RH
1449 break;
1450
1451 case DW_AT_high_pc:
f623be2b 1452 unit->arange.high = DW_ADDR (&attr);
252b5132
RH
1453 break;
1454
1455 case DW_AT_comp_dir:
1456 {
1457 char* comp_dir = DW_STRING (&attr);
1458 if (comp_dir)
1459 {
1460 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1461 directory, get rid of it. */
1462 char *cp = (char*) strchr (comp_dir, ':');
1463
1464 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1465 comp_dir = cp + 1;
1466 }
1467 unit->comp_dir = comp_dir;
1468 break;
1469 }
1470
1471 default:
1472 break;
1473 }
1474 }
1475
1476 unit->first_child_die_ptr = info_ptr;
1477 return unit;
1478}
1479
a092b084 1480/* Return true if UNIT contains the address given by ADDR. */
252b5132
RH
1481
1482static boolean
1483comp_unit_contains_address (unit, addr)
1484 struct comp_unit* unit;
1485 bfd_vma addr;
1486{
f623be2b
RH
1487 struct arange *arange;
1488
1489 if (unit->error)
1490 return 0;
1491
1492 arange = &unit->arange;
1493 do
1494 {
1495 if (addr >= arange->low && addr < arange->high)
1496 return 1;
1497 arange = arange->next;
1498 }
1499 while (arange);
98591c73 1500
f623be2b 1501 return 0;
252b5132
RH
1502}
1503
252b5132
RH
1504/* If UNIT contains ADDR, set the output parameters to the values for
1505 the line containing ADDR. The output parameters, FILENAME_PTR,
1506 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 1507 to be filled in.
252b5132
RH
1508
1509 Return true of UNIT contains ADDR, and no errors were encountered;
1510 false otherwise. */
1511
1512static boolean
1513comp_unit_find_nearest_line (unit, addr,
51db3708
NC
1514 filename_ptr, functionname_ptr, linenumber_ptr,
1515 stash)
252b5132
RH
1516 struct comp_unit* unit;
1517 bfd_vma addr;
1518 const char **filename_ptr;
1519 const char **functionname_ptr;
1520 unsigned int *linenumber_ptr;
51db3708 1521 struct dwarf2_debug *stash;
252b5132
RH
1522{
1523 boolean line_p;
1524 boolean func_p;
98591c73 1525
252b5132
RH
1526 if (unit->error)
1527 return false;
1528
1529 if (! unit->line_table)
1530 {
1531 if (! unit->stmtlist)
1532 {
1533 unit->error = 1;
1534 return false;
1535 }
98591c73 1536
51db3708 1537 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
1538
1539 if (! unit->line_table)
1540 {
1541 unit->error = 1;
1542 return false;
1543 }
98591c73 1544
252b5132
RH
1545 if (! scan_unit_for_functions (unit))
1546 {
1547 unit->error = 1;
1548 return false;
1549 }
1550 }
1551
1552 line_p = lookup_address_in_line_info_table (unit->line_table,
1553 addr,
98591c73 1554 filename_ptr,
252b5132 1555 linenumber_ptr);
98591c73 1556 func_p = lookup_address_in_function_table (unit->function_table,
252b5132
RH
1557 addr,
1558 functionname_ptr);
1559 return line_p || func_p;
1560}
1561
a092b084
NC
1562/* Locate a section in a BFD containing debugging info. The search starts from the
1563 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1564 NULL. The search works by examining the names of the sections. There are two
1565 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1566 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1567 section which has a checksum describing the contents appended onto the name. This
1568 allows the linker to identify and discard duplicate debugging sections for
1569 different compilation units. */
1570#define DWARF2_DEBUG_INFO ".debug_info"
1571#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1572
1573static asection *
1574find_debug_info (abfd, after_sec)
1575 bfd * abfd;
1576 asection * after_sec;
1577{
1578 asection * msec;
1579
1580 if (after_sec)
1581 msec = after_sec->next;
1582 else
1583 msec = abfd->sections;
1584
1585 while (msec)
1586 {
1587 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1588 return msec;
1589
1590 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1591 return msec;
1592
1593 msec = msec->next;
1594 }
1595
1596 return NULL;
1597}
1598
5e38c3b8
MM
1599/* The DWARF2 version of find_nearest line. Return true if the line
1600 is found without error. ADDR_SIZE is the number of bytes in the
1601 initial .debug_info length field and in the abbreviation offset.
1602 You may use zero to indicate that the default value should be
1603 used. */
252b5132
RH
1604
1605boolean
1606_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5e38c3b8
MM
1607 filename_ptr, functionname_ptr,
1608 linenumber_ptr,
51db3708 1609 addr_size, pinfo)
252b5132
RH
1610 bfd *abfd;
1611 asection *section;
f2363ce5 1612 asymbol **symbols;
252b5132
RH
1613 bfd_vma offset;
1614 const char **filename_ptr;
1615 const char **functionname_ptr;
1616 unsigned int *linenumber_ptr;
5e38c3b8 1617 unsigned int addr_size;
51db3708 1618 PTR *pinfo;
252b5132
RH
1619{
1620 /* Read each compilation unit from the section .debug_info, and check
1621 to see if it contains the address we are searching for. If yes,
1622 lookup the address, and return the line number info. If no, go
98591c73 1623 on to the next compilation unit.
252b5132
RH
1624
1625 We keep a list of all the previously read compilation units, and
98591c73 1626 a pointer to the next un-read compilation unit. Check the
a092b084 1627 previously read units before reading more. */
51db3708 1628 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
252b5132 1629
a092b084 1630 /* What address are we looking for? */
252b5132
RH
1631 bfd_vma addr = offset + section->vma;
1632
1633 struct comp_unit* each;
98591c73 1634
252b5132
RH
1635 *filename_ptr = NULL;
1636 *functionname_ptr = NULL;
1637 *linenumber_ptr = 0;
1638
5e38c3b8
MM
1639 /* The DWARF2 spec says that the initial length field, and the
1640 offset of the abbreviation table, should both be 4-byte values.
1641 However, some compilers do things differently. */
1642 if (addr_size == 0)
1643 addr_size = 4;
1efe4b10 1644 BFD_ASSERT (addr_size == 4 || addr_size == 8);
98591c73 1645
252b5132
RH
1646 if (! stash)
1647 {
a092b084 1648 unsigned long total_size;
252b5132 1649 asection *msec;
a092b084 1650
51db3708 1651 stash =
252b5132 1652 (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
252b5132
RH
1653 if (! stash)
1654 return false;
252b5132 1655
51db3708 1656 *pinfo = (PTR) stash;
3fde5a36 1657
a092b084
NC
1658 msec = find_debug_info (abfd, NULL);
1659 if (! msec)
1660 /* No dwarf2 info. Note that at this point the stash
1661 has been allocated, but contains zeros, this lets
1662 future calls to this function fail quicker. */
51db3708 1663 return false;
a092b084
NC
1664
1665 /* There can be more than one DWARF2 info section in a BFD these days.
1666 Read them all in and produce one large stash. We do this in two
1667 passes - in the first pass we just accumulate the section sizes.
1668 In the second pass we read in the section's contents. The allows
1669 us to avoid reallocing the data as we add sections to the stash. */
1670 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1671 total_size += msec->_raw_size;
98591c73 1672
a092b084
NC
1673 stash->info_ptr = (char *) bfd_alloc (abfd, total_size);
1674 if (stash->info_ptr == NULL)
252b5132
RH
1675 return false;
1676
a092b084
NC
1677 stash->info_ptr_end = stash->info_ptr;
1678
1679 for (msec = find_debug_info (abfd, NULL);
1680 msec;
1681 msec = find_debug_info (abfd, msec))
252b5132 1682 {
a092b084
NC
1683 unsigned long size;
1684 unsigned long start;
1685
1686 size = msec->_raw_size;
1687 if (size == 0)
1688 continue;
1689
1690 start = stash->info_ptr_end - stash->info_ptr;
1691
1692 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr + start, 0, size))
1693 continue;
1694
1695 stash->info_ptr_end = stash->info_ptr + start + size;
252b5132
RH
1696 }
1697
f2363ce5
AO
1698 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1699
1700 stash->sec = find_debug_info (abfd, NULL);
1701 stash->sec_info_ptr = stash->info_ptr;
1702 stash->syms = symbols;
252b5132 1703 }
a092b084
NC
1704
1705 /* FIXME: There is a problem with the contents of the
1706 .debug_info section. The 'low' and 'high' addresses of the
1707 comp_units are computed by relocs against symbols in the
1708 .text segment. We need these addresses in order to determine
1709 the nearest line number, and so we have to resolve the
1710 relocs. There is a similar problem when the .debug_line
1711 section is processed as well (e.g., there may be relocs
1712 against the operand of the DW_LNE_set_address operator).
98591c73 1713
a092b084 1714 Unfortunately getting hold of the reloc information is hard...
98591c73 1715
a092b084
NC
1716 For now, this means that disassembling object files (as
1717 opposed to fully executables) does not always work as well as
1718 we would like. */
98591c73
KH
1719
1720 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 1721 (or that an error occured while setting up the stash). */
252b5132
RH
1722 if (! stash->info_ptr)
1723 return false;
1724
a092b084 1725 /* Check the previously read comp. units first. */
252b5132 1726 for (each = stash->all_comp_units; each; each = each->next_unit)
f623be2b 1727 if (comp_unit_contains_address (each, addr))
98591c73 1728 return comp_unit_find_nearest_line (each, addr, filename_ptr,
51db3708
NC
1729 functionname_ptr, linenumber_ptr,
1730 stash);
252b5132 1731
a092b084 1732 /* Read each remaining comp. units checking each as they are read. */
252b5132
RH
1733 while (stash->info_ptr < stash->info_ptr_end)
1734 {
1735 struct comp_unit* each;
5e38c3b8 1736 bfd_vma length;
f623be2b 1737 boolean found;
252b5132 1738
5e38c3b8
MM
1739 if (addr_size == 4)
1740 length = read_4_bytes (abfd, stash->info_ptr);
1741 else
1742 length = read_8_bytes (abfd, stash->info_ptr);
1743 stash->info_ptr += addr_size;
252b5132
RH
1744
1745 if (length > 0)
1746 {
51db3708 1747 each = parse_comp_unit (abfd, stash, length, addr_size);
252b5132
RH
1748 stash->info_ptr += length;
1749
f2363ce5
AO
1750 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1751 == stash->sec->_raw_size)
1752 {
1753 stash->sec = find_debug_info (abfd, stash->sec);
1754 stash->sec_info_ptr = stash->info_ptr;
1755 }
1756
252b5132
RH
1757 if (each)
1758 {
1759 each->next_unit = stash->all_comp_units;
1760 stash->all_comp_units = each;
1761
159002ff
RH
1762 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1763 compilation units. If we don't have them (i.e.,
1764 unit->high == 0), we need to consult the line info
1765 table to see if a compilation unit contains the given
a092b084 1766 address. */
f623be2b 1767 if (each->arange.high > 0)
159002ff
RH
1768 {
1769 if (comp_unit_contains_address (each, addr))
1770 return comp_unit_find_nearest_line (each, addr,
1771 filename_ptr,
1772 functionname_ptr,
51db3708
NC
1773 linenumber_ptr,
1774 stash);
159002ff
RH
1775 }
1776 else
1777 {
1778 found = comp_unit_find_nearest_line (each, addr,
1779 filename_ptr,
1780 functionname_ptr,
51db3708
NC
1781 linenumber_ptr,
1782 stash);
159002ff
RH
1783 if (found)
1784 return true;
1785 }
252b5132
RH
1786 }
1787 }
1788 }
1789
1790 return false;
1791}
This page took 0.16808 seconds and 4 git commands to generate.