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