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