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