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