]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/dwarf2.c
* readelf.c (read_and_display_attr_value): New function to
[thirdparty/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;
dc810e39 466 bfd_size_type amt;
252b5132
RH
467
468 if (! stash->dwarf_abbrev_buffer)
469 {
470 asection *msec;
471
472 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
473 if (! msec)
474 {
475 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
476 bfd_set_error (bfd_error_bad_value);
477 return 0;
478 }
98591c73 479
67d83c76 480 stash->dwarf_abbrev_size = msec->_raw_size;
dc810e39 481 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, msec->_raw_size);
252b5132
RH
482 if (! stash->dwarf_abbrev_buffer)
483 return 0;
98591c73 484
dc810e39
AM
485 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_abbrev_buffer,
486 (bfd_vma) 0, msec->_raw_size))
252b5132
RH
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
dc810e39
AM
498 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
499 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, amt);
252b5132
RH
500
501 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
502 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
503 abbrev_ptr += bytes_read;
504
a092b084 505 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
506 while (abbrev_number)
507 {
dc810e39
AM
508 amt = sizeof (struct abbrev_info);
509 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
252b5132 510
a092b084 511 /* Read in abbrev header. */
252b5132
RH
512 cur_abbrev->number = abbrev_number;
513 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
514 abbrev_ptr += bytes_read;
515 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
516 abbrev_ptr += 1;
517
a092b084 518 /* Now read in declarations. */
252b5132
RH
519 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
520 abbrev_ptr += bytes_read;
521 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
522 abbrev_ptr += bytes_read;
98591c73 523
252b5132
RH
524 while (abbrev_name)
525 {
526 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
527 {
dc810e39
AM
528 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
529 amt *= sizeof (struct attr_abbrev);
530 cur_abbrev->attrs = ((struct attr_abbrev *)
531 bfd_realloc (cur_abbrev->attrs, amt));
252b5132
RH
532 if (! cur_abbrev->attrs)
533 return 0;
534 }
98591c73 535
252b5132
RH
536 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
537 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
538 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
539 abbrev_ptr += bytes_read;
540 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
541 abbrev_ptr += bytes_read;
542 }
543
544 hash_number = abbrev_number % ABBREV_HASH_SIZE;
545 cur_abbrev->next = abbrevs[hash_number];
546 abbrevs[hash_number] = cur_abbrev;
547
548 /* Get next abbreviation.
549 Under Irix6 the abbreviations for a compilation unit are not
550 always properly terminated with an abbrev number of 0.
551 Exit loop if we encounter an abbreviation which we have
552 already read (which means we are about to read the abbreviations
553 for the next compile unit) or if the end of the abbreviation
554 table is reached. */
555 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
556 >= stash->dwarf_abbrev_size)
557 break;
558 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
559 abbrev_ptr += bytes_read;
560 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
561 break;
562 }
563
564 return abbrevs;
565}
566
567/* Read an attribute described by an abbreviated attribute. */
568
569static char *
570read_attribute (attr, abbrev, unit, info_ptr)
571 struct attribute *attr;
572 struct attr_abbrev *abbrev;
573 struct comp_unit *unit;
574 char *info_ptr;
575{
576 bfd *abfd = unit->abfd;
577 unsigned int bytes_read;
578 struct dwarf_block *blk;
dc810e39 579 bfd_size_type amt;
252b5132
RH
580
581 attr->name = abbrev->name;
582 attr->form = abbrev->form;
98591c73 583
252b5132
RH
584 switch (abbrev->form)
585 {
586 case DW_FORM_addr:
587 case DW_FORM_ref_addr:
588 DW_ADDR (attr) = read_address (unit, info_ptr);
589 info_ptr += unit->addr_size;
590 break;
591 case DW_FORM_block2:
dc810e39
AM
592 amt = sizeof (struct dwarf_block);
593 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
252b5132
RH
594 blk->size = read_2_bytes (abfd, info_ptr);
595 info_ptr += 2;
596 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
597 info_ptr += blk->size;
598 DW_BLOCK (attr) = blk;
599 break;
600 case DW_FORM_block4:
dc810e39
AM
601 amt = sizeof (struct dwarf_block);
602 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
252b5132
RH
603 blk->size = read_4_bytes (abfd, info_ptr);
604 info_ptr += 4;
605 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
606 info_ptr += blk->size;
607 DW_BLOCK (attr) = blk;
608 break;
609 case DW_FORM_data2:
610 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
611 info_ptr += 2;
612 break;
613 case DW_FORM_data4:
614 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
615 info_ptr += 4;
616 break;
617 case DW_FORM_data8:
618 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
619 info_ptr += 8;
620 break;
621 case DW_FORM_string:
622 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
623 info_ptr += bytes_read;
624 break;
625 case DW_FORM_block:
dc810e39
AM
626 amt = sizeof (struct dwarf_block);
627 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
252b5132
RH
628 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
629 info_ptr += bytes_read;
630 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
631 info_ptr += blk->size;
632 DW_BLOCK (attr) = blk;
633 break;
634 case DW_FORM_block1:
dc810e39
AM
635 amt = sizeof (struct dwarf_block);
636 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
252b5132
RH
637 blk->size = read_1_byte (abfd, info_ptr);
638 info_ptr += 1;
639 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
640 info_ptr += blk->size;
641 DW_BLOCK (attr) = blk;
642 break;
643 case DW_FORM_data1:
644 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
645 info_ptr += 1;
646 break;
647 case DW_FORM_flag:
648 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
649 info_ptr += 1;
650 break;
651 case DW_FORM_sdata:
652 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
653 info_ptr += bytes_read;
654 break;
655 case DW_FORM_udata:
656 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
657 info_ptr += bytes_read;
658 break;
659 case DW_FORM_ref1:
660 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
661 info_ptr += 1;
662 break;
663 case DW_FORM_ref2:
664 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
665 info_ptr += 2;
666 break;
667 case DW_FORM_ref4:
668 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
669 info_ptr += 4;
670 break;
81edd86d
MM
671 case DW_FORM_ref8:
672 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
673 info_ptr += 8;
674 break;
252b5132
RH
675 case DW_FORM_ref_udata:
676 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
677 info_ptr += bytes_read;
678 break;
679 case DW_FORM_strp:
680 case DW_FORM_indirect:
681 default:
682 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
683 abbrev->form);
684 bfd_set_error (bfd_error_bad_value);
685 }
686 return info_ptr;
687}
688
a092b084 689/* Source line information table routines. */
252b5132
RH
690
691#define FILE_ALLOC_CHUNK 5
692#define DIR_ALLOC_CHUNK 5
693
a092b084
NC
694struct line_info
695{
252b5132 696 struct line_info* prev_line;
252b5132
RH
697 bfd_vma address;
698 char* filename;
699 unsigned int line;
700 unsigned int column;
a092b084 701 int end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
702};
703
a092b084
NC
704struct fileinfo
705{
252b5132
RH
706 char *name;
707 unsigned int dir;
708 unsigned int time;
709 unsigned int size;
710};
711
a092b084
NC
712struct line_info_table
713{
252b5132 714 bfd* abfd;
252b5132
RH
715 unsigned int num_files;
716 unsigned int num_dirs;
252b5132
RH
717 char* comp_dir;
718 char** dirs;
719 struct fileinfo* files;
720 struct line_info* last_line;
721};
722
98591c73 723static void
159002ff 724add_line_info (table, address, filename, line, column, end_sequence)
252b5132
RH
725 struct line_info_table* table;
726 bfd_vma address;
727 char* filename;
728 unsigned int line;
729 unsigned int column;
159002ff 730 int end_sequence;
252b5132 731{
dc810e39
AM
732 bfd_size_type amt = sizeof (struct line_info);
733 struct line_info* info = (struct line_info*) bfd_alloc (table->abfd, amt);
252b5132
RH
734
735 info->prev_line = table->last_line;
736 table->last_line = info;
737
738 info->address = address;
739 info->filename = filename;
740 info->line = line;
741 info->column = column;
159002ff 742 info->end_sequence = end_sequence;
252b5132
RH
743}
744
a092b084 745static char *
252b5132
RH
746concat_filename (table, file)
747 struct line_info_table* table;
748 unsigned int file;
749{
159002ff
RH
750 char* filename;
751
752 if (file - 1 >= table->num_files)
753 {
dcdea4f4
AM
754 (*_bfd_error_handler)
755 (_("Dwarf Error: mangled line number section (bad file number)."));
159002ff
RH
756 return "<unknown>";
757 }
758
759 filename = table->files[file - 1].name;
51db3708 760 if (IS_ABSOLUTE_PATH(filename))
252b5132
RH
761 return filename;
762
763 else
764 {
765 char* dirname = (table->files[file - 1].dir
766 ? table->dirs[table->files[file - 1].dir - 1]
767 : table->comp_dir);
768 return (char*) concat (dirname, "/", filename, NULL);
769 }
770}
771
f623be2b
RH
772static void
773arange_add (unit, low_pc, high_pc)
774 struct comp_unit *unit;
775 bfd_vma low_pc;
776 bfd_vma high_pc;
777{
778 struct arange *arange;
779
a092b084 780 /* First see if we can cheaply extend an existing range. */
f623be2b 781 arange = &unit->arange;
98591c73 782
f623be2b
RH
783 do
784 {
785 if (low_pc == arange->high)
786 {
787 arange->high = high_pc;
788 return;
789 }
790 if (high_pc == arange->low)
791 {
792 arange->low = low_pc;
793 return;
794 }
795 arange = arange->next;
796 }
797 while (arange);
798
799 if (unit->arange.high == 0)
800 {
a092b084 801 /* This is the first address range: store it in unit->arange. */
f623be2b
RH
802 unit->arange.next = 0;
803 unit->arange.low = low_pc;
804 unit->arange.high = high_pc;
805 return;
806 }
807
a092b084 808 /* Need to allocate a new arange and insert it into the arange list. */
dc810e39 809 arange = bfd_zalloc (unit->abfd, (bfd_size_type) sizeof (*arange));
f623be2b
RH
810 arange->low = low_pc;
811 arange->high = high_pc;
812
813 arange->next = unit->arange.next;
814 unit->arange.next = arange;
815}
816
a092b084 817/* Decode the line number information for UNIT. */
252b5132
RH
818
819static struct line_info_table*
51db3708 820decode_line_info (unit, stash)
252b5132 821 struct comp_unit *unit;
51db3708 822 struct dwarf2_debug *stash;
252b5132
RH
823{
824 bfd *abfd = unit->abfd;
252b5132 825 struct line_info_table* table;
252b5132
RH
826 char *line_ptr;
827 char *line_end;
828 struct line_head lh;
829 unsigned int i, bytes_read;
830 char *cur_file, *cur_dir;
831 unsigned char op_code, extended_op, adj_opcode;
dc810e39 832 bfd_size_type amt;
252b5132 833
69dd2e2d 834 if (! stash->dwarf_line_buffer)
252b5132
RH
835 {
836 asection *msec;
252b5132
RH
837
838 msec = bfd_get_section_by_name (abfd, ".debug_line");
839 if (! msec)
840 {
841 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
842 bfd_set_error (bfd_error_bad_value);
843 return 0;
844 }
98591c73 845
ccdb16fc 846 stash->dwarf_line_size = msec->_raw_size;
dc810e39 847 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, msec->_raw_size);
f623be2b 848 if (! stash->dwarf_line_buffer)
252b5132
RH
849 return 0;
850
dc810e39
AM
851 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_line_buffer,
852 (bfd_vma) 0, msec->_raw_size))
252b5132
RH
853 return 0;
854
855 /* FIXME: We ought to apply the relocs against this section before
a092b084 856 we process it... */
252b5132
RH
857 }
858
ccdb16fc
JW
859 /* Since we are using un-relocated data, it is possible to get a bad value
860 for the line_offset. Validate it here so that we won't get a segfault
861 below. */
862 if (unit->line_offset >= stash->dwarf_line_size)
863 {
f5198f61 864 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%u) greater than or equal to line size (%u)."),
ccdb16fc
JW
865 unit->line_offset, stash->dwarf_line_size);
866 bfd_set_error (bfd_error_bad_value);
867 return 0;
868 }
869
dc810e39
AM
870 amt = sizeof (struct line_info_table);
871 table = (struct line_info_table*) bfd_alloc (abfd, amt);
252b5132
RH
872 table->abfd = abfd;
873 table->comp_dir = unit->comp_dir;
874
875 table->num_files = 0;
876 table->files = NULL;
877
878 table->num_dirs = 0;
879 table->dirs = NULL;
880
159002ff
RH
881 table->files = NULL;
882 table->last_line = NULL;
883
69dd2e2d 884 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 885
a092b084 886 /* Read in the prologue. */
252b5132
RH
887 lh.total_length = read_4_bytes (abfd, line_ptr);
888 line_ptr += 4;
889 line_end = line_ptr + lh.total_length;
890 lh.version = read_2_bytes (abfd, line_ptr);
891 line_ptr += 2;
892 lh.prologue_length = read_4_bytes (abfd, line_ptr);
893 line_ptr += 4;
894 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
895 line_ptr += 1;
896 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
897 line_ptr += 1;
898 lh.line_base = read_1_signed_byte (abfd, line_ptr);
899 line_ptr += 1;
900 lh.line_range = read_1_byte (abfd, line_ptr);
901 line_ptr += 1;
902 lh.opcode_base = read_1_byte (abfd, line_ptr);
903 line_ptr += 1;
dc810e39
AM
904 amt = lh.opcode_base * sizeof (unsigned char);
905 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
252b5132
RH
906
907 lh.standard_opcode_lengths[0] = 1;
98591c73 908
252b5132
RH
909 for (i = 1; i < lh.opcode_base; ++i)
910 {
911 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
912 line_ptr += 1;
913 }
914
a092b084 915 /* Read directory table. */
252b5132
RH
916 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
917 {
918 line_ptr += bytes_read;
98591c73 919
252b5132
RH
920 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
921 {
dc810e39
AM
922 amt = table->num_dirs + DIR_ALLOC_CHUNK;
923 amt *= sizeof (char *);
924 table->dirs = (char **) bfd_realloc (table->dirs, amt);
252b5132
RH
925 if (! table->dirs)
926 return 0;
927 }
98591c73 928
252b5132
RH
929 table->dirs[table->num_dirs++] = cur_dir;
930 }
98591c73 931
252b5132
RH
932 line_ptr += bytes_read;
933
a092b084 934 /* Read file name table. */
252b5132
RH
935 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
936 {
937 line_ptr += bytes_read;
98591c73 938
252b5132
RH
939 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
940 {
dc810e39
AM
941 amt = table->num_files + FILE_ALLOC_CHUNK;
942 amt *= sizeof (struct fileinfo);
943 table->files = (struct fileinfo *) bfd_realloc (table->files, amt);
252b5132
RH
944 if (! table->files)
945 return 0;
946 }
98591c73 947
252b5132
RH
948 table->files[table->num_files].name = cur_file;
949 table->files[table->num_files].dir =
950 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
951 line_ptr += bytes_read;
952 table->files[table->num_files].time =
953 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
954 line_ptr += bytes_read;
955 table->files[table->num_files].size =
956 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
957 line_ptr += bytes_read;
958 table->num_files++;
959 }
98591c73 960
252b5132
RH
961 line_ptr += bytes_read;
962
963 /* Read the statement sequences until there's nothing left. */
964 while (line_ptr < line_end)
965 {
a092b084 966 /* State machine registers. */
252b5132
RH
967 bfd_vma address = 0;
968 char* filename = concat_filename (table, 1);
969 unsigned int line = 1;
970 unsigned int column = 0;
971 int is_stmt = lh.default_is_stmt;
972 int basic_block = 0;
f623be2b
RH
973 int end_sequence = 0, need_low_pc = 1;
974 bfd_vma low_pc = 0;
252b5132 975
a092b084 976 /* Decode the table. */
252b5132
RH
977 while (! end_sequence)
978 {
979 op_code = read_1_byte (abfd, line_ptr);
980 line_ptr += 1;
98591c73 981
1a509dcc
GK
982 if (op_code >= lh.opcode_base)
983 { /* Special operand. */
984 adj_opcode = op_code - lh.opcode_base;
985 address += (adj_opcode / lh.line_range)
986 * lh.minimum_instruction_length;
987 line += lh.line_base + (adj_opcode % lh.line_range);
988 /* Append row to matrix using current values. */
989 add_line_info (table, address, filename, line, column, 0);
990 basic_block = 1;
991 if (need_low_pc)
992 {
993 need_low_pc = 0;
994 low_pc = address;
995 }
996 }
997 else switch (op_code)
252b5132
RH
998 {
999 case DW_LNS_extended_op:
a092b084 1000 line_ptr += 1; /* Ignore length. */
252b5132
RH
1001 extended_op = read_1_byte (abfd, line_ptr);
1002 line_ptr += 1;
1003 switch (extended_op)
1004 {
1005 case DW_LNE_end_sequence:
1006 end_sequence = 1;
f623be2b
RH
1007 add_line_info (table, address, filename, line, column,
1008 end_sequence);
1009 if (need_low_pc)
1010 {
1011 need_low_pc = 0;
1012 low_pc = address;
1013 }
1014 arange_add (unit, low_pc, address);
252b5132
RH
1015 break;
1016 case DW_LNE_set_address:
1017 address = read_address (unit, line_ptr);
1018 line_ptr += unit->addr_size;
1019 break;
1020 case DW_LNE_define_file:
1021 cur_file = read_string (abfd, line_ptr, &bytes_read);
1022 line_ptr += bytes_read;
1023 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1024 {
dc810e39
AM
1025 amt = table->num_files + FILE_ALLOC_CHUNK;
1026 amt *= sizeof (struct fileinfo);
1027 table->files =
1028 (struct fileinfo *) bfd_realloc (table->files, amt);
252b5132
RH
1029 if (! table->files)
1030 return 0;
1031 }
1032 table->files[table->num_files].name = cur_file;
1033 table->files[table->num_files].dir =
1034 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1035 line_ptr += bytes_read;
1036 table->files[table->num_files].time =
1037 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1038 line_ptr += bytes_read;
1039 table->files[table->num_files].size =
1040 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1041 line_ptr += bytes_read;
1042 table->num_files++;
1043 break;
1044 default:
1045 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1046 bfd_set_error (bfd_error_bad_value);
1047 return 0;
1048 }
1049 break;
1050 case DW_LNS_copy:
159002ff 1051 add_line_info (table, address, filename, line, column, 0);
252b5132 1052 basic_block = 0;
f623be2b
RH
1053 if (need_low_pc)
1054 {
1055 need_low_pc = 0;
1056 low_pc = address;
1057 }
252b5132
RH
1058 break;
1059 case DW_LNS_advance_pc:
1060 address += lh.minimum_instruction_length
1061 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1062 line_ptr += bytes_read;
1063 break;
1064 case DW_LNS_advance_line:
1065 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1066 line_ptr += bytes_read;
1067 break;
1068 case DW_LNS_set_file:
1069 {
1070 unsigned int file;
1071
1072 /* The file and directory tables are 0 based, the references
1073 are 1 based. */
1074 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1075 line_ptr += bytes_read;
1076 filename = concat_filename (table, file);
1077 break;
1078 }
1079 case DW_LNS_set_column:
1080 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1081 line_ptr += bytes_read;
1082 break;
1083 case DW_LNS_negate_stmt:
1084 is_stmt = (!is_stmt);
1085 break;
1086 case DW_LNS_set_basic_block:
1087 basic_block = 1;
1088 break;
1089 case DW_LNS_const_add_pc:
159002ff
RH
1090 address += lh.minimum_instruction_length
1091 * ((255 - lh.opcode_base) / lh.line_range);
252b5132
RH
1092 break;
1093 case DW_LNS_fixed_advance_pc:
1094 address += read_2_bytes (abfd, line_ptr);
1095 line_ptr += 2;
1096 break;
1a509dcc
GK
1097 default:
1098 { /* Unknown standard opcode, ignore it. */
1099 int i;
1100 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1101 {
1102 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1103 line_ptr += bytes_read;
1104 }
1105 }
252b5132
RH
1106 }
1107 }
1108 }
1109
1110 return table;
1111}
1112
252b5132
RH
1113/* If ADDR is within TABLE set the output parameters and return true,
1114 otherwise return false. The output parameters, FILENAME_PTR and
a092b084 1115 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132
RH
1116
1117static boolean
98591c73 1118lookup_address_in_line_info_table (table,
252b5132 1119 addr,
98591c73 1120 filename_ptr,
252b5132
RH
1121 linenumber_ptr)
1122 struct line_info_table* table;
1123 bfd_vma addr;
1124 const char **filename_ptr;
1125 unsigned int *linenumber_ptr;
1126{
159002ff 1127 struct line_info* next_line = table->last_line;
252b5132 1128 struct line_info* each_line;
98591c73 1129
159002ff
RH
1130 if (!next_line)
1131 return false;
1132
1133 each_line = next_line->prev_line;
1134
1135 while (each_line && next_line)
252b5132 1136 {
159002ff
RH
1137 if (!each_line->end_sequence
1138 && addr >= each_line->address && addr < next_line->address)
252b5132
RH
1139 {
1140 *filename_ptr = each_line->filename;
1141 *linenumber_ptr = each_line->line;
1142 return true;
1143 }
159002ff
RH
1144 next_line = each_line;
1145 each_line = each_line->prev_line;
252b5132 1146 }
98591c73 1147
252b5132
RH
1148 return false;
1149}
98591c73 1150
a092b084 1151/* Function table functions. */
252b5132 1152
a092b084
NC
1153struct funcinfo
1154{
252b5132 1155 struct funcinfo *prev_func;
252b5132
RH
1156 char* name;
1157 bfd_vma low;
1158 bfd_vma high;
1159};
1160
98591c73 1161/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
252b5132
RH
1162
1163static boolean
98591c73 1164lookup_address_in_function_table (table,
252b5132
RH
1165 addr,
1166 functionname_ptr)
1167 struct funcinfo* table;
1168 bfd_vma addr;
1169 const char **functionname_ptr;
1170{
1171 struct funcinfo* each_func;
1172
1173 for (each_func = table;
1174 each_func;
1175 each_func = each_func->prev_func)
1176 {
1177 if (addr >= each_func->low && addr < each_func->high)
1178 {
1179 *functionname_ptr = each_func->name;
1180 return true;
1181 }
1182 }
98591c73 1183
252b5132
RH
1184 return false;
1185}
1186
a092b084 1187/* DWARF2 Compilation unit functions. */
252b5132
RH
1188
1189/* Scan over each die in a comp. unit looking for functions to add
a092b084 1190 to the function table. */
252b5132
RH
1191
1192static boolean
1193scan_unit_for_functions (unit)
1194 struct comp_unit *unit;
1195{
1196 bfd *abfd = unit->abfd;
1197 char *info_ptr = unit->first_child_die_ptr;
1198 int nesting_level = 1;
1199
1200 while (nesting_level)
1201 {
1202 unsigned int abbrev_number, bytes_read, i;
1203 struct abbrev_info *abbrev;
1204 struct attribute attr;
1205 struct funcinfo *func;
1206 char* name = 0;
1207
1208 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1209 info_ptr += bytes_read;
1210
1211 if (! abbrev_number)
1212 {
1213 nesting_level--;
1214 continue;
1215 }
98591c73 1216
252b5132
RH
1217 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1218 if (! abbrev)
1219 {
98591c73 1220 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
252b5132
RH
1221 abbrev_number);
1222 bfd_set_error (bfd_error_bad_value);
1223 return false;
1224 }
98591c73 1225
252b5132
RH
1226 if (abbrev->tag == DW_TAG_subprogram)
1227 {
dc810e39
AM
1228 bfd_size_type amt = sizeof (struct funcinfo);
1229 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
252b5132
RH
1230 func->prev_func = unit->function_table;
1231 unit->function_table = func;
1232 }
1233 else
1234 func = NULL;
98591c73 1235
252b5132
RH
1236 for (i = 0; i < abbrev->num_attrs; ++i)
1237 {
1238 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
98591c73 1239
252b5132
RH
1240 if (func)
1241 {
1242 switch (attr.name)
1243 {
1244 case DW_AT_name:
98591c73 1245
252b5132
RH
1246 name = DW_STRING (&attr);
1247
1248 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1249 if (func->name == NULL)
1250 func->name = DW_STRING (&attr);
1251 break;
98591c73 1252
252b5132
RH
1253 case DW_AT_MIPS_linkage_name:
1254 func->name = DW_STRING (&attr);
1255 break;
1256
1257 case DW_AT_low_pc:
1258 func->low = DW_ADDR (&attr);
1259 break;
1260
1261 case DW_AT_high_pc:
1262 func->high = DW_ADDR (&attr);
1263 break;
1264
1265 default:
1266 break;
1267 }
1268 }
1269 else
1270 {
1271 switch (attr.name)
1272 {
1273 case DW_AT_name:
1274 name = DW_STRING (&attr);
1275 break;
98591c73 1276
252b5132
RH
1277 default:
1278 break;
1279 }
1280 }
1281 }
1282
1283 if (abbrev->has_children)
1284 nesting_level++;
1285 }
1286
1287 return true;
1288}
1289
f2363ce5
AO
1290/* Look for a RELA relocation to be applied on OFFSET of section SEC,
1291 and return the addend if such a relocation is found. Since this is
1292 only used to find relocations referring to the .debug_abbrev
1293 section, we make sure the relocation refers to this section, but
1294 this is not strictly necessary, and it can probably be safely
1295 removed if needed. However, it is important to note that this
1296 function only returns the addend, it doesn't serve the purpose of
1297 applying a generic relocation.
1298
1299 If no suitable relocation is found, or if it is not a real RELA
1300 relocation, this function returns 0. */
1301
1302static bfd_vma
1303find_rela_addend (abfd, sec, offset, syms)
1304 bfd* abfd;
1305 asection* sec;
1306 bfd_size_type offset;
1307 asymbol** syms;
1308{
1309 long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
1310 arelent **relocs = NULL;
1311 long reloc_count, relc;
1312
1313 if (reloc_size <= 0)
1314 return 0;
1315
dc810e39 1316 relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
f2363ce5
AO
1317 if (relocs == NULL)
1318 return 0;
1319
1320 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, syms);
1321
1322 if (reloc_count <= 0)
1323 {
1324 free (relocs);
1325 return 0;
1326 }
1327
1328 for (relc = 0; relc < reloc_count; relc++)
1329 if (relocs[relc]->address == offset
1330 && (*relocs[relc]->sym_ptr_ptr)->flags & BSF_SECTION_SYM
1331 && strcmp ((*relocs[relc]->sym_ptr_ptr)->name,
1332 ".debug_abbrev") == 0)
1333 {
1334 bfd_vma addend = (relocs[relc]->howto->partial_inplace
1335 ? 0 : relocs[relc]->addend);
1336 free (relocs);
1337 return addend;
1338 }
dc810e39 1339
f2363ce5
AO
1340 free (relocs);
1341 return 0;
1342}
1343
5e38c3b8
MM
1344/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1345 includes the compilation unit header that proceeds the DIE's, but
1346 does not include the length field that preceeds each compilation
1347 unit header. END_PTR points one past the end of this comp unit.
1348 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1349 is assumed to be four bytes. Otherwise, it it is the size given.
252b5132
RH
1350
1351 This routine does not read the whole compilation unit; only enough
1352 to get to the line number information for the compilation unit. */
1353
1354static struct comp_unit *
51db3708 1355parse_comp_unit (abfd, stash, unit_length, abbrev_length)
252b5132 1356 bfd* abfd;
51db3708
NC
1357 struct dwarf2_debug *stash;
1358 bfd_vma unit_length;
5e38c3b8 1359 unsigned int abbrev_length;
252b5132
RH
1360{
1361 struct comp_unit* unit;
1362
1363 unsigned short version;
7442e600 1364 unsigned int abbrev_offset = 0;
252b5132
RH
1365 unsigned char addr_size;
1366 struct abbrev_info** abbrevs;
1367
1368 unsigned int abbrev_number, bytes_read, i;
1369 struct abbrev_info *abbrev;
1370 struct attribute attr;
1371
51db3708
NC
1372 char *info_ptr = stash->info_ptr;
1373 char *end_ptr = info_ptr + unit_length;
dc810e39
AM
1374 bfd_size_type amt;
1375 bfd_size_type off;
3fde5a36 1376
252b5132
RH
1377 version = read_2_bytes (abfd, info_ptr);
1378 info_ptr += 2;
5e38c3b8
MM
1379 BFD_ASSERT (abbrev_length == 0
1380 || abbrev_length == 4
1381 || abbrev_length == 8);
1382 if (abbrev_length == 0 || abbrev_length == 4)
1383 abbrev_offset = read_4_bytes (abfd, info_ptr);
1384 else if (abbrev_length == 8)
1385 abbrev_offset = read_8_bytes (abfd, info_ptr);
f2363ce5
AO
1386 /* The abbrev offset is generally a relocation pointing to
1387 .debug_abbrev+offset. On RELA targets, we have to find the
1388 relocation and extract the addend to obtain the actual
1389 abbrev_offset, so do it here. */
dc810e39
AM
1390 off = info_ptr - stash->sec_info_ptr;
1391 abbrev_offset += find_rela_addend (abfd, stash->sec, off, stash->syms);
5e38c3b8 1392 info_ptr += abbrev_length;
252b5132
RH
1393 addr_size = read_1_byte (abfd, info_ptr);
1394 info_ptr += 1;
1395
1396 if (version != 2)
1397 {
1398 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1399 bfd_set_error (bfd_error_bad_value);
1400 return 0;
1401 }
1402
1403 if (addr_size > sizeof (bfd_vma))
1404 {
1405 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1406 addr_size,
1407 sizeof (bfd_vma));
1408 bfd_set_error (bfd_error_bad_value);
1409 return 0;
1410 }
1411
ecb651f0 1412 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 1413 {
ecb651f0 1414 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size );
252b5132
RH
1415 bfd_set_error (bfd_error_bad_value);
1416 return 0;
1417 }
1418
a092b084 1419 /* Read the abbrevs for this compilation unit into a table. */
51db3708 1420 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
1421 if (! abbrevs)
1422 return 0;
1423
1424 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1425 info_ptr += bytes_read;
1426 if (! abbrev_number)
1427 {
1428 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1429 abbrev_number);
1430 bfd_set_error (bfd_error_bad_value);
1431 return 0;
1432 }
1433
1434 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1435 if (! abbrev)
1436 {
1437 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1438 abbrev_number);
1439 bfd_set_error (bfd_error_bad_value);
1440 return 0;
1441 }
98591c73 1442
dc810e39
AM
1443 amt = sizeof (struct comp_unit);
1444 unit = (struct comp_unit*) bfd_zalloc (abfd, amt);
252b5132 1445 unit->abfd = abfd;
98591c73 1446 unit->addr_size = addr_size;
252b5132
RH
1447 unit->abbrevs = abbrevs;
1448 unit->end_ptr = end_ptr;
1449
1450 for (i = 0; i < abbrev->num_attrs; ++i)
1451 {
1452 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1453
1454 /* Store the data if it is of an attribute we want to keep in a
1455 partial symbol table. */
1456 switch (attr.name)
1457 {
1458 case DW_AT_stmt_list:
1459 unit->stmtlist = 1;
1460 unit->line_offset = DW_UNSND (&attr);
1461 break;
1462
1463 case DW_AT_name:
1464 unit->name = DW_STRING (&attr);
1465 break;
1466
1467 case DW_AT_low_pc:
f623be2b 1468 unit->arange.low = DW_ADDR (&attr);
252b5132
RH
1469 break;
1470
1471 case DW_AT_high_pc:
f623be2b 1472 unit->arange.high = DW_ADDR (&attr);
252b5132
RH
1473 break;
1474
1475 case DW_AT_comp_dir:
1476 {
1477 char* comp_dir = DW_STRING (&attr);
1478 if (comp_dir)
1479 {
1480 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1481 directory, get rid of it. */
1482 char *cp = (char*) strchr (comp_dir, ':');
1483
1484 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1485 comp_dir = cp + 1;
1486 }
1487 unit->comp_dir = comp_dir;
1488 break;
1489 }
1490
1491 default:
1492 break;
1493 }
1494 }
1495
1496 unit->first_child_die_ptr = info_ptr;
1497 return unit;
1498}
1499
a092b084 1500/* Return true if UNIT contains the address given by ADDR. */
252b5132
RH
1501
1502static boolean
1503comp_unit_contains_address (unit, addr)
1504 struct comp_unit* unit;
1505 bfd_vma addr;
1506{
f623be2b
RH
1507 struct arange *arange;
1508
1509 if (unit->error)
1510 return 0;
1511
1512 arange = &unit->arange;
1513 do
1514 {
1515 if (addr >= arange->low && addr < arange->high)
1516 return 1;
1517 arange = arange->next;
1518 }
1519 while (arange);
98591c73 1520
f623be2b 1521 return 0;
252b5132
RH
1522}
1523
252b5132
RH
1524/* If UNIT contains ADDR, set the output parameters to the values for
1525 the line containing ADDR. The output parameters, FILENAME_PTR,
1526 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 1527 to be filled in.
252b5132
RH
1528
1529 Return true of UNIT contains ADDR, and no errors were encountered;
1530 false otherwise. */
1531
1532static boolean
1533comp_unit_find_nearest_line (unit, addr,
51db3708
NC
1534 filename_ptr, functionname_ptr, linenumber_ptr,
1535 stash)
252b5132
RH
1536 struct comp_unit* unit;
1537 bfd_vma addr;
1538 const char **filename_ptr;
1539 const char **functionname_ptr;
1540 unsigned int *linenumber_ptr;
51db3708 1541 struct dwarf2_debug *stash;
252b5132
RH
1542{
1543 boolean line_p;
1544 boolean func_p;
98591c73 1545
252b5132
RH
1546 if (unit->error)
1547 return false;
1548
1549 if (! unit->line_table)
1550 {
1551 if (! unit->stmtlist)
1552 {
1553 unit->error = 1;
1554 return false;
1555 }
98591c73 1556
51db3708 1557 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
1558
1559 if (! unit->line_table)
1560 {
1561 unit->error = 1;
1562 return false;
1563 }
98591c73 1564
3f5864e1
SC
1565 if (unit->first_child_die_ptr < unit->end_ptr
1566 && ! scan_unit_for_functions (unit))
252b5132
RH
1567 {
1568 unit->error = 1;
1569 return false;
1570 }
1571 }
1572
1573 line_p = lookup_address_in_line_info_table (unit->line_table,
1574 addr,
98591c73 1575 filename_ptr,
252b5132 1576 linenumber_ptr);
98591c73 1577 func_p = lookup_address_in_function_table (unit->function_table,
252b5132
RH
1578 addr,
1579 functionname_ptr);
1580 return line_p || func_p;
1581}
1582
a092b084
NC
1583/* Locate a section in a BFD containing debugging info. The search starts from the
1584 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1585 NULL. The search works by examining the names of the sections. There are two
1586 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1587 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1588 section which has a checksum describing the contents appended onto the name. This
1589 allows the linker to identify and discard duplicate debugging sections for
1590 different compilation units. */
1591#define DWARF2_DEBUG_INFO ".debug_info"
1592#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1593
1594static asection *
1595find_debug_info (abfd, after_sec)
1596 bfd * abfd;
1597 asection * after_sec;
1598{
1599 asection * msec;
1600
1601 if (after_sec)
1602 msec = after_sec->next;
1603 else
1604 msec = abfd->sections;
1605
1606 while (msec)
1607 {
1608 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1609 return msec;
1610
1611 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1612 return msec;
1613
1614 msec = msec->next;
1615 }
1616
1617 return NULL;
1618}
1619
5e38c3b8
MM
1620/* The DWARF2 version of find_nearest line. Return true if the line
1621 is found without error. ADDR_SIZE is the number of bytes in the
1622 initial .debug_info length field and in the abbreviation offset.
1623 You may use zero to indicate that the default value should be
1624 used. */
252b5132
RH
1625
1626boolean
1627_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5e38c3b8
MM
1628 filename_ptr, functionname_ptr,
1629 linenumber_ptr,
51db3708 1630 addr_size, pinfo)
252b5132
RH
1631 bfd *abfd;
1632 asection *section;
f2363ce5 1633 asymbol **symbols;
252b5132
RH
1634 bfd_vma offset;
1635 const char **filename_ptr;
1636 const char **functionname_ptr;
1637 unsigned int *linenumber_ptr;
5e38c3b8 1638 unsigned int addr_size;
51db3708 1639 PTR *pinfo;
252b5132
RH
1640{
1641 /* Read each compilation unit from the section .debug_info, and check
1642 to see if it contains the address we are searching for. If yes,
1643 lookup the address, and return the line number info. If no, go
98591c73 1644 on to the next compilation unit.
252b5132
RH
1645
1646 We keep a list of all the previously read compilation units, and
98591c73 1647 a pointer to the next un-read compilation unit. Check the
a092b084 1648 previously read units before reading more. */
51db3708 1649 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
252b5132 1650
a092b084 1651 /* What address are we looking for? */
252b5132
RH
1652 bfd_vma addr = offset + section->vma;
1653
1654 struct comp_unit* each;
98591c73 1655
252b5132
RH
1656 *filename_ptr = NULL;
1657 *functionname_ptr = NULL;
1658 *linenumber_ptr = 0;
1659
5e38c3b8
MM
1660 /* The DWARF2 spec says that the initial length field, and the
1661 offset of the abbreviation table, should both be 4-byte values.
1662 However, some compilers do things differently. */
1663 if (addr_size == 0)
1664 addr_size = 4;
1efe4b10 1665 BFD_ASSERT (addr_size == 4 || addr_size == 8);
98591c73 1666
252b5132
RH
1667 if (! stash)
1668 {
dc810e39 1669 bfd_size_type total_size;
252b5132 1670 asection *msec;
dc810e39 1671 bfd_size_type amt = sizeof (struct dwarf2_debug);
a092b084 1672
dc810e39 1673 stash = (struct dwarf2_debug*) bfd_zalloc (abfd, amt);
252b5132
RH
1674 if (! stash)
1675 return false;
252b5132 1676
51db3708 1677 *pinfo = (PTR) stash;
3fde5a36 1678
a092b084
NC
1679 msec = find_debug_info (abfd, NULL);
1680 if (! msec)
1681 /* No dwarf2 info. Note that at this point the stash
1682 has been allocated, but contains zeros, this lets
1683 future calls to this function fail quicker. */
51db3708 1684 return false;
a092b084
NC
1685
1686 /* There can be more than one DWARF2 info section in a BFD these days.
1687 Read them all in and produce one large stash. We do this in two
1688 passes - in the first pass we just accumulate the section sizes.
1689 In the second pass we read in the section's contents. The allows
1690 us to avoid reallocing the data as we add sections to the stash. */
1691 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1692 total_size += msec->_raw_size;
98591c73 1693
a092b084
NC
1694 stash->info_ptr = (char *) bfd_alloc (abfd, total_size);
1695 if (stash->info_ptr == NULL)
252b5132
RH
1696 return false;
1697
a092b084
NC
1698 stash->info_ptr_end = stash->info_ptr;
1699
1700 for (msec = find_debug_info (abfd, NULL);
1701 msec;
1702 msec = find_debug_info (abfd, msec))
252b5132 1703 {
dc810e39
AM
1704 bfd_size_type size;
1705 bfd_size_type start;
a092b084
NC
1706
1707 size = msec->_raw_size;
1708 if (size == 0)
1709 continue;
1710
1711 start = stash->info_ptr_end - stash->info_ptr;
1712
dc810e39
AM
1713 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr + start,
1714 (bfd_vma) 0, size))
a092b084
NC
1715 continue;
1716
1717 stash->info_ptr_end = stash->info_ptr + start + size;
252b5132
RH
1718 }
1719
f2363ce5
AO
1720 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1721
1722 stash->sec = find_debug_info (abfd, NULL);
1723 stash->sec_info_ptr = stash->info_ptr;
1724 stash->syms = symbols;
252b5132 1725 }
a092b084
NC
1726
1727 /* FIXME: There is a problem with the contents of the
1728 .debug_info section. The 'low' and 'high' addresses of the
1729 comp_units are computed by relocs against symbols in the
1730 .text segment. We need these addresses in order to determine
1731 the nearest line number, and so we have to resolve the
1732 relocs. There is a similar problem when the .debug_line
1733 section is processed as well (e.g., there may be relocs
1734 against the operand of the DW_LNE_set_address operator).
98591c73 1735
a092b084 1736 Unfortunately getting hold of the reloc information is hard...
98591c73 1737
a092b084
NC
1738 For now, this means that disassembling object files (as
1739 opposed to fully executables) does not always work as well as
1740 we would like. */
98591c73
KH
1741
1742 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 1743 (or that an error occured while setting up the stash). */
252b5132
RH
1744 if (! stash->info_ptr)
1745 return false;
1746
a092b084 1747 /* Check the previously read comp. units first. */
252b5132 1748 for (each = stash->all_comp_units; each; each = each->next_unit)
f623be2b 1749 if (comp_unit_contains_address (each, addr))
98591c73 1750 return comp_unit_find_nearest_line (each, addr, filename_ptr,
51db3708
NC
1751 functionname_ptr, linenumber_ptr,
1752 stash);
252b5132 1753
a092b084 1754 /* Read each remaining comp. units checking each as they are read. */
252b5132
RH
1755 while (stash->info_ptr < stash->info_ptr_end)
1756 {
5e38c3b8 1757 bfd_vma length;
f623be2b 1758 boolean found;
252b5132 1759
5e38c3b8
MM
1760 if (addr_size == 4)
1761 length = read_4_bytes (abfd, stash->info_ptr);
1762 else
1763 length = read_8_bytes (abfd, stash->info_ptr);
1764 stash->info_ptr += addr_size;
252b5132
RH
1765
1766 if (length > 0)
1767 {
51db3708 1768 each = parse_comp_unit (abfd, stash, length, addr_size);
252b5132
RH
1769 stash->info_ptr += length;
1770
f2363ce5
AO
1771 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1772 == stash->sec->_raw_size)
1773 {
1774 stash->sec = find_debug_info (abfd, stash->sec);
1775 stash->sec_info_ptr = stash->info_ptr;
1776 }
1777
252b5132
RH
1778 if (each)
1779 {
1780 each->next_unit = stash->all_comp_units;
1781 stash->all_comp_units = each;
1782
159002ff
RH
1783 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1784 compilation units. If we don't have them (i.e.,
1785 unit->high == 0), we need to consult the line info
1786 table to see if a compilation unit contains the given
a092b084 1787 address. */
f623be2b 1788 if (each->arange.high > 0)
159002ff
RH
1789 {
1790 if (comp_unit_contains_address (each, addr))
1791 return comp_unit_find_nearest_line (each, addr,
1792 filename_ptr,
1793 functionname_ptr,
51db3708
NC
1794 linenumber_ptr,
1795 stash);
159002ff
RH
1796 }
1797 else
1798 {
1799 found = comp_unit_find_nearest_line (each, addr,
1800 filename_ptr,
1801 functionname_ptr,
51db3708
NC
1802 linenumber_ptr,
1803 stash);
159002ff
RH
1804 if (found)
1805 return true;
1806 }
252b5132
RH
1807 }
1808 }
1809 }
1810
1811 return false;
1812}