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