PR 868
[deliverable/binutils-gdb.git] / bfd / dwarf2.c
CommitLineData
252b5132 1/* DWARF 2 support.
818a27ac 2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
709d67f1 3 2004, 2005, 2006, 2007, 2008 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
cd123cb7 20 the Free Software Foundation; either version 3 of the License, or (at
e2f6d277 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
cd123cb7
NC
30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31 MA 02110-1301, USA. */
252b5132 32
252b5132 33#include "sysdep.h"
3db64b00 34#include "bfd.h"
252b5132
RH
35#include "libiberty.h"
36#include "libbfd.h"
37#include "elf-bfd.h"
38#include "elf/dwarf2.h"
39
40/* The data in the .debug_line statement prologue looks like this. */
a092b084 41
252b5132 42struct line_head
a092b084 43{
d03ba2a1 44 bfd_vma total_length;
a092b084 45 unsigned short version;
f46c2da6 46 bfd_vma prologue_length;
a092b084
NC
47 unsigned char minimum_instruction_length;
48 unsigned char default_is_stmt;
49 int line_base;
50 unsigned char line_range;
51 unsigned char opcode_base;
52 unsigned char *standard_opcode_lengths;
53};
54
55/* Attributes have a name and a value. */
56
252b5132 57struct attribute
a092b084
NC
58{
59 enum dwarf_attribute name;
60 enum dwarf_form form;
61 union
252b5132 62 {
a092b084
NC
63 char *str;
64 struct dwarf_block *blk;
8ce8c090
AM
65 bfd_uint64_t val;
66 bfd_int64_t sval;
a092b084
NC
67 }
68 u;
69};
70
98591c73 71/* Blocks are a bunch of untyped bytes. */
252b5132 72struct dwarf_block
a092b084
NC
73{
74 unsigned int size;
f075ee0c 75 bfd_byte *data;
a092b084 76};
252b5132 77
d4c32a81
L
78struct loadable_section
79{
80 asection *section;
81 bfd_vma adj_vma;
82};
83
a092b084
NC
84struct dwarf2_debug
85{
86 /* A list of all previously read comp_units. */
f075ee0c 87 struct comp_unit *all_comp_units;
252b5132 88
bd210d54
NC
89 /* Last comp unit in list above. */
90 struct comp_unit *last_comp_unit;
91
252b5132
RH
92 /* The next unread compilation unit within the .debug_info section.
93 Zero indicates that the .debug_info section has not been loaded
a092b084 94 into a buffer yet. */
f075ee0c 95 bfd_byte *info_ptr;
252b5132 96
a092b084 97 /* Pointer to the end of the .debug_info section memory buffer. */
f075ee0c 98 bfd_byte *info_ptr_end;
252b5132 99
0d161102
NC
100 /* Pointer to the bfd, section and address of the beginning of the
101 section. The bfd might be different than expected because of
102 gnu_debuglink sections. */
103 bfd * bfd;
f075ee0c
AM
104 asection *sec;
105 bfd_byte *sec_info_ptr;
f2363ce5
AO
106
107 /* Pointer to the symbol table. */
f075ee0c 108 asymbol **syms;
f2363ce5 109
a092b084 110 /* Pointer to the .debug_abbrev section loaded into memory. */
f075ee0c 111 bfd_byte *dwarf_abbrev_buffer;
252b5132 112
a092b084 113 /* Length of the loaded .debug_abbrev section. */
252b5132 114 unsigned long dwarf_abbrev_size;
69dd2e2d
RH
115
116 /* Buffer for decode_line_info. */
f075ee0c 117 bfd_byte *dwarf_line_buffer;
ccdb16fc
JW
118
119 /* Length of the loaded .debug_line section. */
120 unsigned long dwarf_line_size;
d03ba2a1
JJ
121
122 /* Pointer to the .debug_str section loaded into memory. */
f075ee0c 123 bfd_byte *dwarf_str_buffer;
d03ba2a1
JJ
124
125 /* Length of the loaded .debug_str section. */
126 unsigned long dwarf_str_size;
a13afe8e
FF
127
128 /* Pointer to the .debug_ranges section loaded into memory. */
129 bfd_byte *dwarf_ranges_buffer;
130
131 /* Length of the loaded .debug_ranges section. */
132 unsigned long dwarf_ranges_size;
4ab527b0
FF
133
134 /* If the most recent call to bfd_find_nearest_line was given an
135 address in an inlined function, preserve a pointer into the
136 calling chain for subsequent calls to bfd_find_inliner_info to
137 use. */
138 struct funcinfo *inliner_chain;
d4c32a81
L
139
140 /* Number of loadable sections. */
141 unsigned int loadable_section_count;
142
143 /* Array of loadable sections. */
144 struct loadable_section *loadable_sections;
bd210d54
NC
145
146 /* Number of times find_line is called. This is used in
147 the heuristic for enabling the info hash tables. */
148 int info_hash_count;
149
150#define STASH_INFO_HASH_TRIGGER 100
151
152 /* Hash table mapping symbol names to function infos. */
153 struct info_hash_table *funcinfo_hash_table;
154
155 /* Hash table mapping symbol names to variable infos. */
156 struct info_hash_table *varinfo_hash_table;
157
158 /* Head of comp_unit list in the last hash table update. */
159 struct comp_unit *hash_units_head;
160
161 /* Status of info hash. */
162 int info_hash_status;
163#define STASH_INFO_HASH_OFF 0
164#define STASH_INFO_HASH_ON 1
165#define STASH_INFO_HASH_DISABLED 2
252b5132
RH
166};
167
a092b084
NC
168struct arange
169{
f623be2b
RH
170 struct arange *next;
171 bfd_vma low;
172 bfd_vma high;
173};
252b5132 174
252b5132 175/* A minimal decoding of DWARF2 compilation units. We only decode
a092b084 176 what's needed to get to the line number information. */
252b5132 177
a092b084
NC
178struct comp_unit
179{
180 /* Chain the previously read compilation units. */
f075ee0c 181 struct comp_unit *next_unit;
252b5132 182
bd210d54
NC
183 /* Likewise, chain the compilation unit read after this one.
184 The comp units are stored in reversed reading order. */
185 struct comp_unit *prev_unit;
186
2ae727ad 187 /* Keep the bfd convenient (for memory allocation). */
f075ee0c 188 bfd *abfd;
252b5132 189
709d67f1
AM
190 /* The lowest and highest addresses contained in this compilation
191 unit as specified in the compilation unit header. */
192 struct arange arange;
252b5132 193
a092b084 194 /* The DW_AT_name attribute (for error messages). */
f075ee0c 195 char *name;
252b5132 196
a092b084 197 /* The abbrev hash table. */
f075ee0c 198 struct abbrev_info **abbrevs;
252b5132 199
a092b084 200 /* Note that an error was found by comp_unit_find_nearest_line. */
252b5132
RH
201 int error;
202
a092b084 203 /* The DW_AT_comp_dir attribute. */
f075ee0c 204 char *comp_dir;
252b5132 205
b34976b6 206 /* TRUE if there is a line number table associated with this comp. unit. */
252b5132 207 int stmtlist;
98591c73 208
c0c28ab8
L
209 /* Pointer to the current comp_unit so that we can find a given entry
210 by its reference. */
f075ee0c 211 bfd_byte *info_ptr_unit;
c0c28ab8 212
a092b084 213 /* The offset into .debug_line of the line number table. */
252b5132
RH
214 unsigned long line_offset;
215
a092b084 216 /* Pointer to the first child die for the comp unit. */
f075ee0c 217 bfd_byte *first_child_die_ptr;
252b5132 218
a092b084 219 /* The end of the comp unit. */
f075ee0c 220 bfd_byte *end_ptr;
252b5132 221
a092b084 222 /* The decoded line number, NULL if not yet decoded. */
f075ee0c 223 struct line_info_table *line_table;
252b5132 224
a092b084 225 /* A list of the functions found in this comp. unit. */
f075ee0c 226 struct funcinfo *function_table;
252b5132 227
5420f73d
L
228 /* A list of the variables found in this comp. unit. */
229 struct varinfo *variable_table;
230
d03ba2a1
JJ
231 /* Pointer to dwarf2_debug structure. */
232 struct dwarf2_debug *stash;
233
a092b084 234 /* Address size for this unit - from unit header. */
252b5132 235 unsigned char addr_size;
d03ba2a1
JJ
236
237 /* Offset size for this unit - from unit header. */
238 unsigned char offset_size;
a13afe8e
FF
239
240 /* Base address for this unit - from DW_AT_low_pc attribute of
241 DW_TAG_compile_unit DIE */
242 bfd_vma base_address;
bd210d54
NC
243
244 /* TRUE if symbols are cached in hash table for faster lookup by name. */
245 bfd_boolean cached;
252b5132
RH
246};
247
a7b97311
AM
248/* This data structure holds the information of an abbrev. */
249struct abbrev_info
250{
251 unsigned int number; /* Number identifying abbrev. */
252 enum dwarf_tag tag; /* DWARF tag. */
253 int has_children; /* Boolean. */
254 unsigned int num_attrs; /* Number of attributes. */
255 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
256 struct abbrev_info *next; /* Next in chain. */
257};
258
259struct attr_abbrev
260{
261 enum dwarf_attribute name;
262 enum dwarf_form form;
263};
264
265#ifndef ABBREV_HASH_SIZE
266#define ABBREV_HASH_SIZE 121
267#endif
268#ifndef ATTR_ALLOC_CHUNK
269#define ATTR_ALLOC_CHUNK 4
270#endif
271
bd210d54
NC
272/* Variable and function hash tables. This is used to speed up look-up
273 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
274 In order to share code between variable and function infos, we use
275 a list of untyped pointer for all variable/function info associated with
276 a symbol. We waste a bit of memory for list with one node but that
277 simplifies the code. */
278
279struct info_list_node
280{
281 struct info_list_node *next;
282 void *info;
283};
284
285/* Info hash entry. */
286struct info_hash_entry
287{
288 struct bfd_hash_entry root;
289 struct info_list_node *head;
290};
291
292struct info_hash_table
293{
294 struct bfd_hash_table base;
295};
296
297/* Function to create a new entry in info hash table. */
298
299static struct bfd_hash_entry *
300info_hash_table_newfunc (struct bfd_hash_entry *entry,
301 struct bfd_hash_table *table,
302 const char *string)
303{
304 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
305
306 /* Allocate the structure if it has not already been allocated by a
307 derived class. */
308 if (ret == NULL)
309 {
310 ret = bfd_hash_allocate (table, sizeof (* ret));
311 if (ret == NULL)
312 return NULL;
313 }
314
315 /* Call the allocation method of the base class. */
316 ret = ((struct info_hash_entry *)
317 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
318
319 /* Initialize the local fields here. */
320 if (ret)
321 ret->head = NULL;
322
323 return (struct bfd_hash_entry *) ret;
324}
325
326/* Function to create a new info hash table. It returns a pointer to the
327 newly created table or NULL if there is any error. We need abfd
328 solely for memory allocation. */
329
330static struct info_hash_table *
331create_info_hash_table (bfd *abfd)
332{
333 struct info_hash_table *hash_table;
334
335 hash_table = bfd_alloc (abfd, sizeof (struct info_hash_table));
336 if (!hash_table)
337 return hash_table;
338
339 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
340 sizeof (struct info_hash_entry)))
341 {
342 bfd_release (abfd, hash_table);
343 return NULL;
344 }
345
346 return hash_table;
347}
348
349/* Insert an info entry into an info hash table. We do not check of
350 duplicate entries. Also, the caller need to guarantee that the
351 right type of info in inserted as info is passed as a void* pointer.
352 This function returns true if there is no error. */
353
354static bfd_boolean
355insert_info_hash_table (struct info_hash_table *hash_table,
356 const char *key,
357 void *info,
358 bfd_boolean copy_p)
359{
360 struct info_hash_entry *entry;
361 struct info_list_node *node;
362
363 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
364 key, TRUE, copy_p);
365 if (!entry)
366 return FALSE;
367
368 node = bfd_hash_allocate (&hash_table->base, sizeof (*node));
369 if (!node)
370 return FALSE;
371
372 node->info = info;
373 node->next = entry->head;
374 entry->head = node;
375
376 return TRUE;
377}
378
379/* Look up an info entry list from an info hash table. Return NULL
380 if there is none. */
381
382static struct info_list_node *
383lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
384{
385 struct info_hash_entry *entry;
386
387 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
388 FALSE, FALSE);
389 return entry ? entry->head : NULL;
390}
391
98591c73
KH
392/* VERBATIM
393 The following function up to the END VERBATIM mark are
a092b084 394 copied directly from dwarf2read.c. */
252b5132 395
a092b084 396/* Read dwarf information from a buffer. */
252b5132
RH
397
398static unsigned int
f075ee0c 399read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 400{
818a27ac 401 return bfd_get_8 (abfd, buf);
252b5132
RH
402}
403
404static int
f075ee0c 405read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 406{
818a27ac 407 return bfd_get_signed_8 (abfd, buf);
252b5132
RH
408}
409
410static unsigned int
f075ee0c 411read_2_bytes (bfd *abfd, bfd_byte *buf)
252b5132 412{
818a27ac 413 return bfd_get_16 (abfd, buf);
252b5132
RH
414}
415
252b5132 416static unsigned int
f075ee0c 417read_4_bytes (bfd *abfd, bfd_byte *buf)
252b5132 418{
818a27ac 419 return bfd_get_32 (abfd, buf);
252b5132
RH
420}
421
8ce8c090 422static bfd_uint64_t
f075ee0c 423read_8_bytes (bfd *abfd, bfd_byte *buf)
252b5132 424{
818a27ac 425 return bfd_get_64 (abfd, buf);
252b5132
RH
426}
427
f075ee0c 428static bfd_byte *
818a27ac 429read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 430 bfd_byte *buf,
818a27ac 431 unsigned int size ATTRIBUTE_UNUSED)
252b5132
RH
432{
433 /* If the size of a host char is 8 bits, we can return a pointer
434 to the buffer, otherwise we have to copy the data to a buffer
435 allocated on the temporary obstack. */
436 return buf;
437}
438
439static char *
818a27ac 440read_string (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 441 bfd_byte *buf,
818a27ac 442 unsigned int *bytes_read_ptr)
252b5132 443{
d03ba2a1 444 /* Return a pointer to the embedded string. */
f075ee0c
AM
445 char *str = (char *) buf;
446 if (*str == '\0')
252b5132
RH
447 {
448 *bytes_read_ptr = 1;
449 return NULL;
450 }
98591c73 451
f075ee0c
AM
452 *bytes_read_ptr = strlen (str) + 1;
453 return str;
252b5132
RH
454}
455
d03ba2a1 456static char *
818a27ac 457read_indirect_string (struct comp_unit* unit,
f075ee0c 458 bfd_byte *buf,
818a27ac 459 unsigned int *bytes_read_ptr)
d03ba2a1 460{
8ce8c090 461 bfd_uint64_t offset;
d03ba2a1 462 struct dwarf2_debug *stash = unit->stash;
f075ee0c 463 char *str;
d03ba2a1
JJ
464
465 if (unit->offset_size == 4)
466 offset = read_4_bytes (unit->abfd, buf);
467 else
468 offset = read_8_bytes (unit->abfd, buf);
469 *bytes_read_ptr = unit->offset_size;
470
471 if (! stash->dwarf_str_buffer)
472 {
473 asection *msec;
474 bfd *abfd = unit->abfd;
eea6121a 475 bfd_size_type sz;
d03ba2a1
JJ
476
477 msec = bfd_get_section_by_name (abfd, ".debug_str");
478 if (! msec)
479 {
480 (*_bfd_error_handler)
481 (_("Dwarf Error: Can't find .debug_str section."));
482 bfd_set_error (bfd_error_bad_value);
483 return NULL;
484 }
485
eea6121a
AM
486 sz = msec->rawsize ? msec->rawsize : msec->size;
487 stash->dwarf_str_size = sz;
488 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
36868d45 489 if (! stash->dwarf_str_buffer)
d03ba2a1
JJ
490 return NULL;
491
9f632188 492 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
eea6121a 493 0, sz))
d03ba2a1
JJ
494 return NULL;
495 }
496
497 if (offset >= stash->dwarf_str_size)
498 {
f46c2da6
AM
499 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
500 (unsigned long) offset, stash->dwarf_str_size);
d03ba2a1
JJ
501 bfd_set_error (bfd_error_bad_value);
502 return NULL;
503 }
504
f075ee0c
AM
505 str = (char *) stash->dwarf_str_buffer + offset;
506 if (*str == '\0')
d03ba2a1 507 return NULL;
f075ee0c 508 return str;
d03ba2a1
JJ
509}
510
252b5132
RH
511/* END VERBATIM */
512
8ce8c090 513static bfd_uint64_t
f075ee0c 514read_address (struct comp_unit *unit, bfd_byte *buf)
252b5132 515{
0af4cd7c
PK
516 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
517
518 if (signed_vma)
519 {
520 switch (unit->addr_size)
521 {
522 case 8:
523 return bfd_get_signed_64 (unit->abfd, buf);
524 case 4:
525 return bfd_get_signed_32 (unit->abfd, buf);
526 case 2:
527 return bfd_get_signed_16 (unit->abfd, buf);
528 default:
529 abort ();
530 }
531 }
532 else
252b5132 533 {
0af4cd7c
PK
534 switch (unit->addr_size)
535 {
536 case 8:
537 return bfd_get_64 (unit->abfd, buf);
538 case 4:
539 return bfd_get_32 (unit->abfd, buf);
540 case 2:
541 return bfd_get_16 (unit->abfd, buf);
542 default:
543 abort ();
544 }
252b5132 545 }
252b5132
RH
546}
547
252b5132
RH
548/* Lookup an abbrev_info structure in the abbrev hash table. */
549
550static struct abbrev_info *
818a27ac 551lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
252b5132
RH
552{
553 unsigned int hash_number;
554 struct abbrev_info *abbrev;
555
556 hash_number = number % ABBREV_HASH_SIZE;
557 abbrev = abbrevs[hash_number];
558
559 while (abbrev)
560 {
561 if (abbrev->number == number)
562 return abbrev;
563 else
564 abbrev = abbrev->next;
565 }
98591c73 566
252b5132
RH
567 return NULL;
568}
569
570/* In DWARF version 2, the description of the debugging information is
571 stored in a separate .debug_abbrev section. Before we read any
572 dies from a section we read in all abbreviations and install them
573 in a hash table. */
574
575static struct abbrev_info**
8ce8c090 576read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
252b5132
RH
577{
578 struct abbrev_info **abbrevs;
f075ee0c 579 bfd_byte *abbrev_ptr;
252b5132
RH
580 struct abbrev_info *cur_abbrev;
581 unsigned int abbrev_number, bytes_read, abbrev_name;
582 unsigned int abbrev_form, hash_number;
dc810e39 583 bfd_size_type amt;
252b5132
RH
584
585 if (! stash->dwarf_abbrev_buffer)
586 {
587 asection *msec;
588
589 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
590 if (! msec)
591 {
592 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
593 bfd_set_error (bfd_error_bad_value);
594 return 0;
595 }
98591c73 596
eea6121a 597 stash->dwarf_abbrev_size = msec->size;
6e84a906
DJ
598 stash->dwarf_abbrev_buffer
599 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
600 stash->syms);
252b5132
RH
601 if (! stash->dwarf_abbrev_buffer)
602 return 0;
252b5132
RH
603 }
604
f5198f61 605 if (offset >= stash->dwarf_abbrev_size)
252b5132 606 {
f46c2da6
AM
607 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
608 (unsigned long) offset, stash->dwarf_abbrev_size);
252b5132
RH
609 bfd_set_error (bfd_error_bad_value);
610 return 0;
611 }
612
dc810e39 613 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
818a27ac 614 abbrevs = bfd_zalloc (abfd, amt);
252b5132
RH
615
616 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
617 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
618 abbrev_ptr += bytes_read;
619
a092b084 620 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
621 while (abbrev_number)
622 {
dc810e39 623 amt = sizeof (struct abbrev_info);
818a27ac 624 cur_abbrev = bfd_zalloc (abfd, amt);
252b5132 625
a092b084 626 /* Read in abbrev header. */
252b5132 627 cur_abbrev->number = abbrev_number;
d45913a0
DA
628 cur_abbrev->tag = (enum dwarf_tag)
629 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
252b5132
RH
630 abbrev_ptr += bytes_read;
631 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
632 abbrev_ptr += 1;
633
a092b084 634 /* Now read in declarations. */
252b5132
RH
635 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
636 abbrev_ptr += bytes_read;
637 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
638 abbrev_ptr += bytes_read;
98591c73 639
252b5132
RH
640 while (abbrev_name)
641 {
642 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
643 {
35330cce
NC
644 struct attr_abbrev *tmp;
645
dc810e39
AM
646 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
647 amt *= sizeof (struct attr_abbrev);
35330cce
NC
648 tmp = bfd_realloc (cur_abbrev->attrs, amt);
649 if (tmp == NULL)
d8d1c398
AM
650 {
651 size_t i;
652
653 for (i = 0; i < ABBREV_HASH_SIZE; i++)
654 {
655 struct abbrev_info *abbrev = abbrevs[i];
656
657 while (abbrev)
658 {
b42bf1e0
NC
659 struct abbrev_info * a = abbrev;
660
661 abbrev = a->next;
662 if (a->attrs)
663 free (abbrev->attrs);
664 free (a);
d8d1c398
AM
665 }
666 }
b42bf1e0
NC
667
668 free (abbrevs);
d8d1c398
AM
669 return NULL;
670 }
35330cce 671 cur_abbrev->attrs = tmp;
252b5132 672 }
98591c73 673
d45913a0
DA
674 cur_abbrev->attrs[cur_abbrev->num_attrs].name
675 = (enum dwarf_attribute) abbrev_name;
676 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
677 = (enum dwarf_form) abbrev_form;
252b5132
RH
678 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
679 abbrev_ptr += bytes_read;
680 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
681 abbrev_ptr += bytes_read;
682 }
683
684 hash_number = abbrev_number % ABBREV_HASH_SIZE;
685 cur_abbrev->next = abbrevs[hash_number];
686 abbrevs[hash_number] = cur_abbrev;
687
688 /* Get next abbreviation.
e82ce529 689 Under Irix6 the abbreviations for a compilation unit are not
252b5132
RH
690 always properly terminated with an abbrev number of 0.
691 Exit loop if we encounter an abbreviation which we have
692 already read (which means we are about to read the abbreviations
693 for the next compile unit) or if the end of the abbreviation
694 table is reached. */
695 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
d8d1c398 696 >= stash->dwarf_abbrev_size)
252b5132
RH
697 break;
698 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
699 abbrev_ptr += bytes_read;
700 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
701 break;
702 }
703
704 return abbrevs;
705}
706
cf716c56 707/* Read an attribute value described by an attribute form. */
252b5132 708
f075ee0c 709static bfd_byte *
818a27ac
AM
710read_attribute_value (struct attribute *attr,
711 unsigned form,
712 struct comp_unit *unit,
f075ee0c 713 bfd_byte *info_ptr)
252b5132
RH
714{
715 bfd *abfd = unit->abfd;
716 unsigned int bytes_read;
717 struct dwarf_block *blk;
dc810e39 718 bfd_size_type amt;
252b5132 719
d45913a0 720 attr->form = (enum dwarf_form) form;
98591c73 721
cf716c56 722 switch (form)
252b5132
RH
723 {
724 case DW_FORM_addr:
0cc1cf99 725 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
252b5132 726 case DW_FORM_ref_addr:
482e2e37 727 attr->u.val = read_address (unit, info_ptr);
252b5132
RH
728 info_ptr += unit->addr_size;
729 break;
730 case DW_FORM_block2:
dc810e39 731 amt = sizeof (struct dwarf_block);
818a27ac 732 blk = bfd_alloc (abfd, amt);
252b5132
RH
733 blk->size = read_2_bytes (abfd, info_ptr);
734 info_ptr += 2;
735 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
736 info_ptr += blk->size;
482e2e37 737 attr->u.blk = blk;
252b5132
RH
738 break;
739 case DW_FORM_block4:
dc810e39 740 amt = sizeof (struct dwarf_block);
818a27ac 741 blk = bfd_alloc (abfd, amt);
252b5132
RH
742 blk->size = read_4_bytes (abfd, info_ptr);
743 info_ptr += 4;
744 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
745 info_ptr += blk->size;
482e2e37 746 attr->u.blk = blk;
252b5132
RH
747 break;
748 case DW_FORM_data2:
482e2e37 749 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
750 info_ptr += 2;
751 break;
752 case DW_FORM_data4:
482e2e37 753 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
754 info_ptr += 4;
755 break;
756 case DW_FORM_data8:
482e2e37 757 attr->u.val = read_8_bytes (abfd, info_ptr);
252b5132
RH
758 info_ptr += 8;
759 break;
760 case DW_FORM_string:
482e2e37 761 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
252b5132
RH
762 info_ptr += bytes_read;
763 break;
d03ba2a1 764 case DW_FORM_strp:
482e2e37 765 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
d03ba2a1
JJ
766 info_ptr += bytes_read;
767 break;
252b5132 768 case DW_FORM_block:
dc810e39 769 amt = sizeof (struct dwarf_block);
818a27ac 770 blk = bfd_alloc (abfd, amt);
252b5132
RH
771 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
772 info_ptr += bytes_read;
773 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
774 info_ptr += blk->size;
482e2e37 775 attr->u.blk = blk;
252b5132
RH
776 break;
777 case DW_FORM_block1:
dc810e39 778 amt = sizeof (struct dwarf_block);
818a27ac 779 blk = bfd_alloc (abfd, amt);
252b5132
RH
780 blk->size = read_1_byte (abfd, info_ptr);
781 info_ptr += 1;
782 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
783 info_ptr += blk->size;
482e2e37 784 attr->u.blk = blk;
252b5132
RH
785 break;
786 case DW_FORM_data1:
482e2e37 787 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
788 info_ptr += 1;
789 break;
790 case DW_FORM_flag:
482e2e37 791 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
792 info_ptr += 1;
793 break;
794 case DW_FORM_sdata:
482e2e37 795 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
796 info_ptr += bytes_read;
797 break;
798 case DW_FORM_udata:
482e2e37 799 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
800 info_ptr += bytes_read;
801 break;
802 case DW_FORM_ref1:
482e2e37 803 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
804 info_ptr += 1;
805 break;
806 case DW_FORM_ref2:
482e2e37 807 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
808 info_ptr += 2;
809 break;
810 case DW_FORM_ref4:
482e2e37 811 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
812 info_ptr += 4;
813 break;
81edd86d 814 case DW_FORM_ref8:
482e2e37 815 attr->u.val = read_8_bytes (abfd, info_ptr);
81edd86d
MM
816 info_ptr += 8;
817 break;
252b5132 818 case DW_FORM_ref_udata:
482e2e37 819 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
820 info_ptr += bytes_read;
821 break;
252b5132 822 case DW_FORM_indirect:
cf716c56
RH
823 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
824 info_ptr += bytes_read;
825 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
826 break;
252b5132 827 default:
f46c2da6 828 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
cf716c56 829 form);
252b5132
RH
830 bfd_set_error (bfd_error_bad_value);
831 }
832 return info_ptr;
833}
834
cf716c56
RH
835/* Read an attribute described by an abbreviated attribute. */
836
f075ee0c 837static bfd_byte *
818a27ac
AM
838read_attribute (struct attribute *attr,
839 struct attr_abbrev *abbrev,
840 struct comp_unit *unit,
f075ee0c 841 bfd_byte *info_ptr)
cf716c56
RH
842{
843 attr->name = abbrev->name;
844 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
845 return info_ptr;
846}
847
a092b084 848/* Source line information table routines. */
252b5132
RH
849
850#define FILE_ALLOC_CHUNK 5
851#define DIR_ALLOC_CHUNK 5
852
a092b084
NC
853struct line_info
854{
252b5132 855 struct line_info* prev_line;
252b5132 856 bfd_vma address;
f075ee0c 857 char *filename;
252b5132
RH
858 unsigned int line;
859 unsigned int column;
a092b084 860 int end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
861};
862
a092b084
NC
863struct fileinfo
864{
252b5132
RH
865 char *name;
866 unsigned int dir;
867 unsigned int time;
868 unsigned int size;
869};
870
a092b084
NC
871struct line_info_table
872{
252b5132 873 bfd* abfd;
252b5132
RH
874 unsigned int num_files;
875 unsigned int num_dirs;
f075ee0c
AM
876 char *comp_dir;
877 char **dirs;
252b5132 878 struct fileinfo* files;
709d67f1
AM
879 struct line_info* last_line; /* largest VMA */
880 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
252b5132
RH
881};
882
4ab527b0
FF
883/* Remember some information about each function. If the function is
884 inlined (DW_TAG_inlined_subroutine) it may have two additional
885 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
886 source code location where this function was inlined. */
887
1ee24f27
DJ
888struct funcinfo
889{
709d67f1
AM
890 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
891 struct funcinfo *caller_func; /* Pointer to function one scope higher */
892 char *caller_file; /* Source location file name where caller_func inlines this func */
893 int caller_line; /* Source location line number where caller_func inlines this func */
894 char *file; /* Source location file name */
895 int line; /* Source location line number */
4ab527b0 896 int tag;
f075ee0c 897 char *name;
a13afe8e 898 struct arange arange;
709d67f1 899 asection *sec; /* Where the symbol is defined */
5420f73d
L
900};
901
902struct varinfo
903{
709d67f1 904 /* Pointer to previous variable in list of all variables */
5420f73d 905 struct varinfo *prev_var;
709d67f1 906 /* Source location file name */
5420f73d 907 char *file;
709d67f1 908 /* Source location line number */
5420f73d
L
909 int line;
910 int tag;
911 char *name;
5cf2e3f0 912 bfd_vma addr;
709d67f1 913 /* Where the symbol is defined */
5420f73d 914 asection *sec;
709d67f1 915 /* Is this a stack variable? */
5420f73d 916 unsigned int stack: 1;
1ee24f27
DJ
917};
918
d4c32a81
L
919/* Return TRUE if NEW_LINE should sort after LINE. */
920
921static inline bfd_boolean
922new_line_sorts_after (struct line_info *new_line, struct line_info *line)
923{
924 return (new_line->address > line->address
925 || (new_line->address == line->address
aff90a5f 926 && new_line->end_sequence < line->end_sequence));
d4c32a81
L
927}
928
929
af3ef9fe
NC
930/* Adds a new entry to the line_info list in the line_info_table, ensuring
931 that the list is sorted. Note that the line_info list is sorted from
932 highest to lowest VMA (with possible duplicates); that is,
933 line_info->prev_line always accesses an equal or smaller VMA. */
934
98591c73 935static void
818a27ac
AM
936add_line_info (struct line_info_table *table,
937 bfd_vma address,
938 char *filename,
939 unsigned int line,
940 unsigned int column,
941 int end_sequence)
252b5132 942{
dc810e39 943 bfd_size_type amt = sizeof (struct line_info);
709d67f1 944 struct line_info* info = bfd_alloc (table->abfd, amt);
252b5132 945
d4c32a81
L
946 /* Set member data of 'info'. */
947 info->address = address;
948 info->line = line;
949 info->column = column;
950 info->end_sequence = end_sequence;
951
952 if (filename && filename[0])
953 {
954 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
955 if (info->filename)
956 strcpy (info->filename, filename);
957 }
958 else
959 info->filename = NULL;
960
e82ce529
AM
961 /* Find the correct location for 'info'. Normally we will receive
962 new line_info data 1) in order and 2) with increasing VMAs.
963 However some compilers break the rules (cf. decode_line_info) and
964 so we include some heuristics for quickly finding the correct
965 location for 'info'. In particular, these heuristics optimize for
966 the common case in which the VMA sequence that we receive is a
967 list of locally sorted VMAs such as
968 p...z a...j (where a < j < p < z)
252b5132 969
e82ce529
AM
970 Note: table->lcl_head is used to head an *actual* or *possible*
971 sequence within the list (such as a...j) that is not directly
972 headed by table->last_line
973
974 Note: we may receive duplicate entries from 'decode_line_info'. */
975
aff90a5f
L
976 if (table->last_line
977 && table->last_line->address == address
978 && table->last_line->end_sequence == end_sequence)
979 {
980 /* We only keep the last entry with the same address and end
981 sequence. See PR ld/4986. */
982 if (table->lcl_head == table->last_line)
983 table->lcl_head = info;
984 info->prev_line = table->last_line->prev_line;
985 table->last_line = info;
986 }
987 else if (!table->last_line
709d67f1 988 || new_line_sorts_after (info, table->last_line))
d8d1c398 989 {
709d67f1 990 /* Normal case: add 'info' to the beginning of the list */
d8d1c398
AM
991 info->prev_line = table->last_line;
992 table->last_line = info;
e82ce529 993
d8d1c398
AM
994 /* lcl_head: initialize to head a *possible* sequence at the end. */
995 if (!table->lcl_head)
996 table->lcl_head = info;
997 }
998 else if (!new_line_sorts_after (info, table->lcl_head)
999 && (!table->lcl_head->prev_line
1000 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1001 {
1002 /* Abnormal but easy: lcl_head is the head of 'info'. */
1003 info->prev_line = table->lcl_head->prev_line;
1004 table->lcl_head->prev_line = info;
1005 }
1006 else
1007 {
1008 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1009 heads for 'info'. Reset 'lcl_head'. */
709d67f1 1010 struct line_info* li2 = table->last_line; /* always non-NULL */
d8d1c398 1011 struct line_info* li1 = li2->prev_line;
e82ce529 1012
d8d1c398
AM
1013 while (li1)
1014 {
1015 if (!new_line_sorts_after (info, li2)
1016 && new_line_sorts_after (info, li1))
1017 break;
e82ce529 1018
709d67f1 1019 li2 = li1; /* always non-NULL */
d8d1c398
AM
1020 li1 = li1->prev_line;
1021 }
1022 table->lcl_head = li2;
1023 info->prev_line = table->lcl_head->prev_line;
1024 table->lcl_head->prev_line = info;
1025 }
252b5132
RH
1026}
1027
5ed6aba4 1028/* Extract a fully qualified filename from a line info table.
af3ef9fe
NC
1029 The returned string has been malloc'ed and it is the caller's
1030 responsibility to free it. */
5ed6aba4 1031
a092b084 1032static char *
818a27ac 1033concat_filename (struct line_info_table *table, unsigned int file)
252b5132 1034{
f075ee0c 1035 char *filename;
159002ff
RH
1036
1037 if (file - 1 >= table->num_files)
1038 {
75a657ba
L
1039 /* FILE == 0 means unknown. */
1040 if (file)
1041 (*_bfd_error_handler)
1042 (_("Dwarf Error: mangled line number section (bad file number)."));
af3ef9fe 1043 return strdup ("<unknown>");
159002ff
RH
1044 }
1045
1046 filename = table->files[file - 1].name;
5ed6aba4 1047
7421a730 1048 if (!IS_ABSOLUTE_PATH (filename))
252b5132 1049 {
7421a730
AM
1050 char *dirname = NULL;
1051 char *subdirname = NULL;
1052 char *name;
1053 size_t len;
0dafd5f6 1054
7421a730
AM
1055 if (table->files[file - 1].dir)
1056 subdirname = table->dirs[table->files[file - 1].dir - 1];
1057
1058 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1059 dirname = table->comp_dir;
1060
1061 if (!dirname)
af3ef9fe 1062 {
7421a730
AM
1063 dirname = subdirname;
1064 subdirname = NULL;
1065 }
af3ef9fe 1066
7421a730
AM
1067 if (!dirname)
1068 return strdup (filename);
1069
1070 len = strlen (dirname) + strlen (filename) + 2;
1071
1072 if (subdirname)
1073 {
1074 len += strlen (subdirname) + 1;
1075 name = bfd_malloc (len);
1076 if (name)
1077 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1078 }
1079 else
1080 {
af3ef9fe
NC
1081 name = bfd_malloc (len);
1082 if (name)
1083 sprintf (name, "%s/%s", dirname, filename);
af3ef9fe 1084 }
7421a730
AM
1085
1086 return name;
252b5132 1087 }
af3ef9fe
NC
1088
1089 return strdup (filename);
252b5132
RH
1090}
1091
f623be2b 1092static void
709d67f1 1093arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
f623be2b
RH
1094{
1095 struct arange *arange;
1096
709d67f1 1097 /* If the first arange is empty, use it. */
a13afe8e
FF
1098 if (first_arange->high == 0)
1099 {
1100 first_arange->low = low_pc;
1101 first_arange->high = high_pc;
1102 return;
1103 }
98591c73 1104
a13afe8e
FF
1105 /* Next see if we can cheaply extend an existing range. */
1106 arange = first_arange;
f623be2b
RH
1107 do
1108 {
1109 if (low_pc == arange->high)
1110 {
1111 arange->high = high_pc;
1112 return;
1113 }
1114 if (high_pc == arange->low)
1115 {
1116 arange->low = low_pc;
1117 return;
1118 }
1119 arange = arange->next;
1120 }
1121 while (arange);
1122
a13afe8e 1123 /* Need to allocate a new arange and insert it into the arange list.
709d67f1 1124 Order isn't significant, so just insert after the first arange. */
a13afe8e 1125 arange = bfd_zalloc (abfd, sizeof (*arange));
f623be2b
RH
1126 arange->low = low_pc;
1127 arange->high = high_pc;
a13afe8e
FF
1128 arange->next = first_arange->next;
1129 first_arange->next = arange;
f623be2b
RH
1130}
1131
b42bf1e0
NC
1132/* Decode the line number information for UNIT.
1133 Note: this function allocates memory. It is the caller's
1134 responsibility to free it. */
252b5132 1135
b42bf1e0 1136static struct line_info_table *
818a27ac 1137decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
252b5132
RH
1138{
1139 bfd *abfd = unit->abfd;
252b5132 1140 struct line_info_table* table;
f075ee0c
AM
1141 bfd_byte *line_ptr;
1142 bfd_byte *line_end;
252b5132 1143 struct line_head lh;
d03ba2a1 1144 unsigned int i, bytes_read, offset_size;
252b5132
RH
1145 char *cur_file, *cur_dir;
1146 unsigned char op_code, extended_op, adj_opcode;
dc810e39 1147 bfd_size_type amt;
252b5132 1148
69dd2e2d 1149 if (! stash->dwarf_line_buffer)
252b5132
RH
1150 {
1151 asection *msec;
252b5132
RH
1152
1153 msec = bfd_get_section_by_name (abfd, ".debug_line");
1154 if (! msec)
1155 {
1156 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
1157 bfd_set_error (bfd_error_bad_value);
1158 return 0;
1159 }
98591c73 1160
eea6121a 1161 stash->dwarf_line_size = msec->size;
6e84a906
DJ
1162 stash->dwarf_line_buffer
1163 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1164 stash->syms);
f623be2b 1165 if (! stash->dwarf_line_buffer)
252b5132 1166 return 0;
252b5132
RH
1167 }
1168
6e84a906
DJ
1169 /* It is possible to get a bad value for the line_offset. Validate
1170 it here so that we won't get a segfault below. */
ccdb16fc
JW
1171 if (unit->line_offset >= stash->dwarf_line_size)
1172 {
f46c2da6 1173 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
ccdb16fc
JW
1174 unit->line_offset, stash->dwarf_line_size);
1175 bfd_set_error (bfd_error_bad_value);
1176 return 0;
1177 }
1178
dc810e39 1179 amt = sizeof (struct line_info_table);
818a27ac 1180 table = bfd_alloc (abfd, amt);
252b5132
RH
1181 table->abfd = abfd;
1182 table->comp_dir = unit->comp_dir;
1183
1184 table->num_files = 0;
1185 table->files = NULL;
1186
1187 table->num_dirs = 0;
1188 table->dirs = NULL;
1189
159002ff
RH
1190 table->files = NULL;
1191 table->last_line = NULL;
e82ce529 1192 table->lcl_head = NULL;
159002ff 1193
69dd2e2d 1194 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 1195
a092b084 1196 /* Read in the prologue. */
91a4d569
AM
1197 lh.total_length = read_4_bytes (abfd, line_ptr);
1198 line_ptr += 4;
1199 offset_size = 4;
1200 if (lh.total_length == 0xffffffff)
dae2dd0d 1201 {
dae2dd0d
NC
1202 lh.total_length = read_8_bytes (abfd, line_ptr);
1203 line_ptr += 8;
1204 offset_size = 8;
1205 }
91a4d569 1206 else if (lh.total_length == 0 && unit->addr_size == 8)
d03ba2a1 1207 {
91a4d569
AM
1208 /* Handle (non-standard) 64-bit DWARF2 formats. */
1209 lh.total_length = read_4_bytes (abfd, line_ptr);
1210 line_ptr += 4;
d03ba2a1
JJ
1211 offset_size = 8;
1212 }
252b5132
RH
1213 line_end = line_ptr + lh.total_length;
1214 lh.version = read_2_bytes (abfd, line_ptr);
1215 line_ptr += 2;
d03ba2a1
JJ
1216 if (offset_size == 4)
1217 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1218 else
1219 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1220 line_ptr += offset_size;
252b5132
RH
1221 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1222 line_ptr += 1;
1223 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1224 line_ptr += 1;
1225 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1226 line_ptr += 1;
1227 lh.line_range = read_1_byte (abfd, line_ptr);
1228 line_ptr += 1;
1229 lh.opcode_base = read_1_byte (abfd, line_ptr);
1230 line_ptr += 1;
dc810e39 1231 amt = lh.opcode_base * sizeof (unsigned char);
818a27ac 1232 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
252b5132
RH
1233
1234 lh.standard_opcode_lengths[0] = 1;
98591c73 1235
252b5132
RH
1236 for (i = 1; i < lh.opcode_base; ++i)
1237 {
1238 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1239 line_ptr += 1;
1240 }
1241
a092b084 1242 /* Read directory table. */
252b5132
RH
1243 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1244 {
1245 line_ptr += bytes_read;
98591c73 1246
252b5132
RH
1247 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1248 {
35330cce
NC
1249 char **tmp;
1250
dc810e39
AM
1251 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1252 amt *= sizeof (char *);
35330cce
NC
1253
1254 tmp = bfd_realloc (table->dirs, amt);
1255 if (tmp == NULL)
1256 {
1257 free (table->dirs);
515ef31d 1258 free (table);
35330cce
NC
1259 return NULL;
1260 }
1261 table->dirs = tmp;
252b5132 1262 }
98591c73 1263
252b5132
RH
1264 table->dirs[table->num_dirs++] = cur_dir;
1265 }
98591c73 1266
252b5132
RH
1267 line_ptr += bytes_read;
1268
a092b084 1269 /* Read file name table. */
252b5132
RH
1270 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1271 {
1272 line_ptr += bytes_read;
98591c73 1273
252b5132
RH
1274 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1275 {
35330cce
NC
1276 struct fileinfo *tmp;
1277
dc810e39
AM
1278 amt = table->num_files + FILE_ALLOC_CHUNK;
1279 amt *= sizeof (struct fileinfo);
35330cce
NC
1280
1281 tmp = bfd_realloc (table->files, amt);
1282 if (tmp == NULL)
1283 {
1284 free (table->files);
1285 free (table->dirs);
b42bf1e0 1286 free (table);
35330cce
NC
1287 return NULL;
1288 }
1289 table->files = tmp;
252b5132 1290 }
98591c73 1291
252b5132
RH
1292 table->files[table->num_files].name = cur_file;
1293 table->files[table->num_files].dir =
1294 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1295 line_ptr += bytes_read;
1296 table->files[table->num_files].time =
1297 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1298 line_ptr += bytes_read;
1299 table->files[table->num_files].size =
1300 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1301 line_ptr += bytes_read;
1302 table->num_files++;
1303 }
98591c73 1304
252b5132
RH
1305 line_ptr += bytes_read;
1306
1307 /* Read the statement sequences until there's nothing left. */
1308 while (line_ptr < line_end)
1309 {
a092b084 1310 /* State machine registers. */
252b5132 1311 bfd_vma address = 0;
8bfd78b3 1312 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
252b5132
RH
1313 unsigned int line = 1;
1314 unsigned int column = 0;
1315 int is_stmt = lh.default_is_stmt;
e2f6d277
NC
1316 int end_sequence = 0;
1317 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
e82ce529
AM
1318 compilers generate address sequences that are wildly out of
1319 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1320 for ia64-Linux). Thus, to determine the low and high
1321 address, we must compare on every DW_LNS_copy, etc. */
75758e9d 1322 bfd_vma low_pc = (bfd_vma) -1;
e2f6d277 1323 bfd_vma high_pc = 0;
252b5132 1324
a092b084 1325 /* Decode the table. */
252b5132
RH
1326 while (! end_sequence)
1327 {
1328 op_code = read_1_byte (abfd, line_ptr);
1329 line_ptr += 1;
98591c73 1330
1a509dcc 1331 if (op_code >= lh.opcode_base)
e2f6d277
NC
1332 {
1333 /* Special operand. */
1a509dcc
GK
1334 adj_opcode = op_code - lh.opcode_base;
1335 address += (adj_opcode / lh.line_range)
1336 * lh.minimum_instruction_length;
1337 line += lh.line_base + (adj_opcode % lh.line_range);
1338 /* Append row to matrix using current values. */
1339 add_line_info (table, address, filename, line, column, 0);
75758e9d
AM
1340 if (address < low_pc)
1341 low_pc = address;
e2f6d277
NC
1342 if (address > high_pc)
1343 high_pc = address;
1a509dcc
GK
1344 }
1345 else switch (op_code)
252b5132
RH
1346 {
1347 case DW_LNS_extended_op:
e2f6d277
NC
1348 /* Ignore length. */
1349 line_ptr += 1;
252b5132
RH
1350 extended_op = read_1_byte (abfd, line_ptr);
1351 line_ptr += 1;
e2f6d277 1352
252b5132
RH
1353 switch (extended_op)
1354 {
1355 case DW_LNE_end_sequence:
1356 end_sequence = 1;
f623be2b
RH
1357 add_line_info (table, address, filename, line, column,
1358 end_sequence);
75758e9d
AM
1359 if (address < low_pc)
1360 low_pc = address;
e2f6d277
NC
1361 if (address > high_pc)
1362 high_pc = address;
709d67f1 1363 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
252b5132
RH
1364 break;
1365 case DW_LNE_set_address:
1366 address = read_address (unit, line_ptr);
1367 line_ptr += unit->addr_size;
1368 break;
1369 case DW_LNE_define_file:
1370 cur_file = read_string (abfd, line_ptr, &bytes_read);
1371 line_ptr += bytes_read;
1372 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1373 {
35330cce
NC
1374 struct fileinfo *tmp;
1375
dc810e39
AM
1376 amt = table->num_files + FILE_ALLOC_CHUNK;
1377 amt *= sizeof (struct fileinfo);
35330cce
NC
1378 tmp = bfd_realloc (table->files, amt);
1379 if (tmp == NULL)
d8d1c398 1380 {
35330cce
NC
1381 free (table->files);
1382 free (table->dirs);
b42bf1e0 1383 free (table);
35330cce
NC
1384 free (filename);
1385 return NULL;
d8d1c398 1386 }
35330cce 1387 table->files = tmp;
252b5132
RH
1388 }
1389 table->files[table->num_files].name = cur_file;
1390 table->files[table->num_files].dir =
1391 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1392 line_ptr += bytes_read;
1393 table->files[table->num_files].time =
1394 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1395 line_ptr += bytes_read;
1396 table->files[table->num_files].size =
1397 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1398 line_ptr += bytes_read;
1399 table->num_files++;
1400 break;
1401 default:
1402 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1403 bfd_set_error (bfd_error_bad_value);
35330cce
NC
1404 free (filename);
1405 free (table->files);
1406 free (table->dirs);
b42bf1e0 1407 free (table);
35330cce 1408 return NULL;
252b5132
RH
1409 }
1410 break;
1411 case DW_LNS_copy:
159002ff 1412 add_line_info (table, address, filename, line, column, 0);
75758e9d
AM
1413 if (address < low_pc)
1414 low_pc = address;
e2f6d277
NC
1415 if (address > high_pc)
1416 high_pc = address;
252b5132
RH
1417 break;
1418 case DW_LNS_advance_pc:
1419 address += lh.minimum_instruction_length
1420 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1421 line_ptr += bytes_read;
1422 break;
1423 case DW_LNS_advance_line:
1424 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1425 line_ptr += bytes_read;
1426 break;
1427 case DW_LNS_set_file:
1428 {
1429 unsigned int file;
1430
e2f6d277
NC
1431 /* The file and directory tables are 0
1432 based, the references are 1 based. */
252b5132
RH
1433 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1434 line_ptr += bytes_read;
af3ef9fe
NC
1435 if (filename)
1436 free (filename);
252b5132
RH
1437 filename = concat_filename (table, file);
1438 break;
1439 }
1440 case DW_LNS_set_column:
1441 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1442 line_ptr += bytes_read;
1443 break;
1444 case DW_LNS_negate_stmt:
1445 is_stmt = (!is_stmt);
1446 break;
1447 case DW_LNS_set_basic_block:
252b5132
RH
1448 break;
1449 case DW_LNS_const_add_pc:
159002ff
RH
1450 address += lh.minimum_instruction_length
1451 * ((255 - lh.opcode_base) / lh.line_range);
252b5132
RH
1452 break;
1453 case DW_LNS_fixed_advance_pc:
1454 address += read_2_bytes (abfd, line_ptr);
1455 line_ptr += 2;
1456 break;
1a509dcc 1457 default:
e2f6d277 1458 {
1a509dcc 1459 int i;
5ed6aba4 1460
e2f6d277 1461 /* Unknown standard opcode, ignore it. */
1a509dcc
GK
1462 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1463 {
1464 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1465 line_ptr += bytes_read;
1466 }
1467 }
252b5132
RH
1468 }
1469 }
5ed6aba4 1470
af3ef9fe
NC
1471 if (filename)
1472 free (filename);
252b5132
RH
1473 }
1474
1475 return table;
1476}
1477
b34976b6
AM
1478/* If ADDR is within TABLE set the output parameters and return TRUE,
1479 otherwise return FALSE. The output parameters, FILENAME_PTR and
a092b084 1480 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132 1481
b34976b6 1482static bfd_boolean
818a27ac
AM
1483lookup_address_in_line_info_table (struct line_info_table *table,
1484 bfd_vma addr,
1485 struct funcinfo *function,
1486 const char **filename_ptr,
1487 unsigned int *linenumber_ptr)
252b5132 1488{
e82ce529 1489 /* Note: table->last_line should be a descendingly sorted list. */
159002ff 1490 struct line_info* next_line = table->last_line;
e82ce529
AM
1491 struct line_info* each_line = NULL;
1492 *filename_ptr = NULL;
98591c73 1493
159002ff 1494 if (!next_line)
b34976b6 1495 return FALSE;
159002ff
RH
1496
1497 each_line = next_line->prev_line;
1498
e82ce529
AM
1499 /* Check for large addresses */
1500 if (addr > next_line->address)
1501 each_line = NULL; /* ensure we skip over the normal case */
1502
1503 /* Normal case: search the list; save */
159002ff 1504 while (each_line && next_line)
252b5132 1505 {
e82ce529
AM
1506 /* If we have an address match, save this info. This allows us
1507 to return as good as results as possible for strange debugging
1508 info. */
b34976b6 1509 bfd_boolean addr_match = FALSE;
a13afe8e 1510 if (each_line->address <= addr && addr < next_line->address)
252b5132 1511 {
b34976b6 1512 addr_match = TRUE;
e82ce529 1513
1ee24f27
DJ
1514 /* If this line appears to span functions, and addr is in the
1515 later function, return the first line of that function instead
1516 of the last line of the earlier one. This check is for GCC
1517 2.95, which emits the first line number for a function late. */
a13afe8e
FF
1518
1519 if (function != NULL)
1ee24f27 1520 {
a13afe8e
FF
1521 bfd_vma lowest_pc;
1522 struct arange *arange;
1523
1524 /* Find the lowest address in the function's range list */
1525 lowest_pc = function->arange.low;
1526 for (arange = &function->arange;
1527 arange;
1528 arange = arange->next)
1529 {
1530 if (function->arange.low < lowest_pc)
1531 lowest_pc = function->arange.low;
1532 }
1533 /* Check for spanning function and set outgoing line info */
1534 if (addr >= lowest_pc
1535 && each_line->address < lowest_pc
1536 && next_line->address > lowest_pc)
1537 {
1538 *filename_ptr = next_line->filename;
1539 *linenumber_ptr = next_line->line;
1540 }
1541 else
1542 {
1543 *filename_ptr = each_line->filename;
1544 *linenumber_ptr = each_line->line;
1545 }
1ee24f27 1546 }
6bd00c5d
L
1547 else
1548 {
1549 *filename_ptr = each_line->filename;
1550 *linenumber_ptr = each_line->line;
1551 }
252b5132 1552 }
e82ce529
AM
1553
1554 if (addr_match && !each_line->end_sequence)
b34976b6 1555 return TRUE; /* we have definitely found what we want */
e82ce529 1556
159002ff
RH
1557 next_line = each_line;
1558 each_line = each_line->prev_line;
252b5132 1559 }
98591c73 1560
e82ce529
AM
1561 /* At this point each_line is NULL but next_line is not. If we found
1562 a candidate end-of-sequence point in the loop above, we can return
1563 that (compatibility with a bug in the Intel compiler); otherwise,
1564 assuming that we found the containing function for this address in
1565 this compilation unit, return the first line we have a number for
1566 (compatibility with GCC 2.95). */
1567 if (*filename_ptr == NULL && function != NULL)
1ee24f27
DJ
1568 {
1569 *filename_ptr = next_line->filename;
1570 *linenumber_ptr = next_line->line;
b34976b6 1571 return TRUE;
1ee24f27
DJ
1572 }
1573
b34976b6 1574 return FALSE;
252b5132 1575}
98591c73 1576
a13afe8e
FF
1577/* Read in the .debug_ranges section for future reference */
1578
1579static bfd_boolean
1580read_debug_ranges (struct comp_unit *unit)
1581{
1582 struct dwarf2_debug *stash = unit->stash;
1583 if (! stash->dwarf_ranges_buffer)
1584 {
1585 bfd *abfd = unit->abfd;
1586 asection *msec;
1587
1588 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1589 if (! msec)
1590 {
1591 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1592 bfd_set_error (bfd_error_bad_value);
1593 return FALSE;
1594 }
1595
1596 stash->dwarf_ranges_size = msec->size;
1597 stash->dwarf_ranges_buffer
1598 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1599 stash->syms);
1600 if (! stash->dwarf_ranges_buffer)
1601 return FALSE;
1602 }
1603 return TRUE;
1604}
1605
a092b084 1606/* Function table functions. */
252b5132 1607
a13afe8e
FF
1608/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1609 Note that we need to find the function that has the smallest
1610 range that contains ADDR, to handle inlined functions without
1611 depending upon them being ordered in TABLE by increasing range. */
252b5132 1612
b34976b6 1613static bfd_boolean
4ab527b0 1614lookup_address_in_function_table (struct comp_unit *unit,
818a27ac
AM
1615 bfd_vma addr,
1616 struct funcinfo **function_ptr,
1617 const char **functionname_ptr)
252b5132
RH
1618{
1619 struct funcinfo* each_func;
a13afe8e
FF
1620 struct funcinfo* best_fit = NULL;
1621 struct arange *arange;
252b5132 1622
4ab527b0 1623 for (each_func = unit->function_table;
252b5132
RH
1624 each_func;
1625 each_func = each_func->prev_func)
1626 {
a13afe8e
FF
1627 for (arange = &each_func->arange;
1628 arange;
1629 arange = arange->next)
252b5132 1630 {
a13afe8e
FF
1631 if (addr >= arange->low && addr < arange->high)
1632 {
1633 if (!best_fit ||
1634 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1635 best_fit = each_func;
1636 }
252b5132
RH
1637 }
1638 }
98591c73 1639
a13afe8e
FF
1640 if (best_fit)
1641 {
1642 *functionname_ptr = best_fit->name;
1643 *function_ptr = best_fit;
1644 return TRUE;
1645 }
1646 else
1647 {
1648 return FALSE;
1649 }
252b5132
RH
1650}
1651
5420f73d
L
1652/* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1653 and LINENUMBER_PTR, and return TRUE. */
1654
1655static bfd_boolean
1656lookup_symbol_in_function_table (struct comp_unit *unit,
1657 asymbol *sym,
1658 bfd_vma addr,
1659 const char **filename_ptr,
1660 unsigned int *linenumber_ptr)
1661{
1662 struct funcinfo* each_func;
1663 struct funcinfo* best_fit = NULL;
1664 struct arange *arange;
1665 const char *name = bfd_asymbol_name (sym);
1666 asection *sec = bfd_get_section (sym);
1667
1668 for (each_func = unit->function_table;
1669 each_func;
1670 each_func = each_func->prev_func)
1671 {
1672 for (arange = &each_func->arange;
1673 arange;
1674 arange = arange->next)
1675 {
1676 if ((!each_func->sec || each_func->sec == sec)
1677 && addr >= arange->low
1678 && addr < arange->high
650f284e 1679 && each_func->name
5420f73d
L
1680 && strcmp (name, each_func->name) == 0
1681 && (!best_fit
1682 || ((arange->high - arange->low)
1683 < (best_fit->arange.high - best_fit->arange.low))))
1684 best_fit = each_func;
1685 }
1686 }
1687
1688 if (best_fit)
1689 {
1690 best_fit->sec = sec;
1691 *filename_ptr = best_fit->file;
1692 *linenumber_ptr = best_fit->line;
1693 return TRUE;
1694 }
1695 else
1696 return FALSE;
1697}
1698
1699/* Variable table functions. */
1700
1701/* If SYM is within variable table of UNIT, set FILENAME_PTR and
1702 LINENUMBER_PTR, and return TRUE. */
1703
1704static bfd_boolean
1705lookup_symbol_in_variable_table (struct comp_unit *unit,
1706 asymbol *sym,
5cf2e3f0 1707 bfd_vma addr,
5420f73d
L
1708 const char **filename_ptr,
1709 unsigned int *linenumber_ptr)
1710{
1711 const char *name = bfd_asymbol_name (sym);
1712 asection *sec = bfd_get_section (sym);
1713 struct varinfo* each;
1714
1715 for (each = unit->variable_table; each; each = each->prev_var)
1716 if (each->stack == 0
5cf2e3f0
L
1717 && each->file != NULL
1718 && each->name != NULL
1719 && each->addr == addr
5420f73d
L
1720 && (!each->sec || each->sec == sec)
1721 && strcmp (name, each->name) == 0)
1722 break;
1723
1724 if (each)
1725 {
1726 each->sec = sec;
1727 *filename_ptr = each->file;
1728 *linenumber_ptr = each->line;
1729 return TRUE;
1730 }
1731 else
1732 return FALSE;
1733}
1734
06f22d7e
FF
1735static char *
1736find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1737{
1738 bfd *abfd = unit->abfd;
f075ee0c 1739 bfd_byte *info_ptr;
06f22d7e
FF
1740 unsigned int abbrev_number, bytes_read, i;
1741 struct abbrev_info *abbrev;
1742 struct attribute attr;
1743 char *name = 0;
1744
c0c28ab8 1745 info_ptr = unit->info_ptr_unit + die_ref;
06f22d7e
FF
1746 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1747 info_ptr += bytes_read;
1748
1749 if (abbrev_number)
1750 {
1751 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1752 if (! abbrev)
1753 {
1754 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1755 abbrev_number);
1756 bfd_set_error (bfd_error_bad_value);
1757 }
1758 else
1759 {
d5cbaa15 1760 for (i = 0; i < abbrev->num_attrs; ++i)
06f22d7e
FF
1761 {
1762 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
26bf4e33
FF
1763 switch (attr.name)
1764 {
1765 case DW_AT_name:
d5cbaa15
JW
1766 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1767 if (name == NULL)
1768 name = attr.u.str;
26bf4e33
FF
1769 break;
1770 case DW_AT_specification:
1771 name = find_abstract_instance_name (unit, attr.u.val);
1772 break;
d5cbaa15
JW
1773 case DW_AT_MIPS_linkage_name:
1774 name = attr.u.str;
1775 break;
26bf4e33
FF
1776 default:
1777 break;
1778 }
06f22d7e
FF
1779 }
1780 }
1781 }
709d67f1 1782 return (name);
06f22d7e
FF
1783}
1784
a13afe8e 1785static void
709d67f1 1786read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
a13afe8e
FF
1787{
1788 bfd_byte *ranges_ptr;
1789 bfd_vma base_address = unit->base_address;
1790
1791 if (! unit->stash->dwarf_ranges_buffer)
1792 {
1793 if (! read_debug_ranges (unit))
1794 return;
1795 }
1796 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
d8d1c398 1797
a13afe8e
FF
1798 for (;;)
1799 {
1800 bfd_vma low_pc;
1801 bfd_vma high_pc;
1802
a29b7e88 1803 if (unit->addr_size == 4)
a13afe8e
FF
1804 {
1805 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1806 ranges_ptr += 4;
1807 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1808 ranges_ptr += 4;
1809 }
1810 else
1811 {
1812 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1813 ranges_ptr += 8;
1814 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1815 ranges_ptr += 8;
1816 }
1817 if (low_pc == 0 && high_pc == 0)
1818 break;
1819 if (low_pc == -1UL && high_pc != -1UL)
1820 base_address = high_pc;
1821 else
709d67f1 1822 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
a13afe8e
FF
1823 }
1824}
1825
a092b084 1826/* DWARF2 Compilation unit functions. */
252b5132
RH
1827
1828/* Scan over each die in a comp. unit looking for functions to add
b42bf1e0
NC
1829 to the function table and variables to the variable table.
1830 Returns TRUE upon success, FALSE otherwise. Allocates memory
1831 blocks to the unit->function_table and unit->variable_table fields.
1832 It is the caller's responsibility to free this memory. */
252b5132 1833
b34976b6 1834static bfd_boolean
5420f73d 1835scan_unit_for_symbols (struct comp_unit *unit)
252b5132
RH
1836{
1837 bfd *abfd = unit->abfd;
f075ee0c 1838 bfd_byte *info_ptr = unit->first_child_die_ptr;
252b5132 1839 int nesting_level = 1;
c955f9cd
JW
1840 struct funcinfo **nested_funcs;
1841 int nested_funcs_size;
1842
1843 /* Maintain a stack of in-scope functions and inlined functions, which we
1844 can use to set the caller_func field. */
1845 nested_funcs_size = 32;
1846 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1847 if (nested_funcs == NULL)
1848 return FALSE;
b42bf1e0 1849 nested_funcs[nesting_level] = NULL;
252b5132
RH
1850
1851 while (nesting_level)
1852 {
1853 unsigned int abbrev_number, bytes_read, i;
1854 struct abbrev_info *abbrev;
1855 struct attribute attr;
1856 struct funcinfo *func;
5420f73d 1857 struct varinfo *var;
a13afe8e
FF
1858 bfd_vma low_pc = 0;
1859 bfd_vma high_pc = 0;
252b5132
RH
1860
1861 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1862 info_ptr += bytes_read;
1863
1864 if (! abbrev_number)
1865 {
1866 nesting_level--;
1867 continue;
1868 }
98591c73 1869
252b5132
RH
1870 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1871 if (! abbrev)
1872 {
f46c2da6 1873 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1874 abbrev_number);
1875 bfd_set_error (bfd_error_bad_value);
c955f9cd 1876 free (nested_funcs);
b34976b6 1877 return FALSE;
252b5132 1878 }
98591c73 1879
5420f73d 1880 var = NULL;
06f22d7e 1881 if (abbrev->tag == DW_TAG_subprogram
5420f73d 1882 || abbrev->tag == DW_TAG_entry_point
06f22d7e 1883 || abbrev->tag == DW_TAG_inlined_subroutine)
252b5132 1884 {
dc810e39 1885 bfd_size_type amt = sizeof (struct funcinfo);
b42bf1e0 1886
818a27ac 1887 func = bfd_zalloc (abfd, amt);
4ab527b0 1888 func->tag = abbrev->tag;
252b5132
RH
1889 func->prev_func = unit->function_table;
1890 unit->function_table = func;
bd210d54 1891 BFD_ASSERT (!unit->cached);
c955f9cd
JW
1892
1893 if (func->tag == DW_TAG_inlined_subroutine)
1894 for (i = nesting_level - 1; i >= 1; i--)
1895 if (nested_funcs[i])
1896 {
1897 func->caller_func = nested_funcs[i];
1898 break;
1899 }
1900 nested_funcs[nesting_level] = func;
252b5132
RH
1901 }
1902 else
5420f73d
L
1903 {
1904 func = NULL;
1905 if (abbrev->tag == DW_TAG_variable)
1906 {
1907 bfd_size_type amt = sizeof (struct varinfo);
b42bf1e0 1908
5420f73d
L
1909 var = bfd_zalloc (abfd, amt);
1910 var->tag = abbrev->tag;
1911 var->stack = 1;
1912 var->prev_var = unit->variable_table;
1913 unit->variable_table = var;
bd210d54 1914 BFD_ASSERT (!unit->cached);
5420f73d 1915 }
c955f9cd
JW
1916
1917 /* No inline function in scope at this nesting level. */
1918 nested_funcs[nesting_level] = 0;
5420f73d 1919 }
98591c73 1920
252b5132
RH
1921 for (i = 0; i < abbrev->num_attrs; ++i)
1922 {
1923 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
98591c73 1924
252b5132
RH
1925 if (func)
1926 {
1927 switch (attr.name)
1928 {
4ab527b0
FF
1929 case DW_AT_call_file:
1930 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1931 break;
1932
1933 case DW_AT_call_line:
1934 func->caller_line = attr.u.val;
1935 break;
1936
06f22d7e
FF
1937 case DW_AT_abstract_origin:
1938 func->name = find_abstract_instance_name (unit, attr.u.val);
1939 break;
1940
252b5132 1941 case DW_AT_name:
252b5132
RH
1942 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1943 if (func->name == NULL)
482e2e37 1944 func->name = attr.u.str;
252b5132 1945 break;
98591c73 1946
252b5132 1947 case DW_AT_MIPS_linkage_name:
482e2e37 1948 func->name = attr.u.str;
252b5132
RH
1949 break;
1950
1951 case DW_AT_low_pc:
a13afe8e 1952 low_pc = attr.u.val;
252b5132
RH
1953 break;
1954
1955 case DW_AT_high_pc:
a13afe8e
FF
1956 high_pc = attr.u.val;
1957 break;
1958
1959 case DW_AT_ranges:
709d67f1 1960 read_rangelist (unit, &func->arange, attr.u.val);
252b5132
RH
1961 break;
1962
5420f73d
L
1963 case DW_AT_decl_file:
1964 func->file = concat_filename (unit->line_table,
1965 attr.u.val);
1966 break;
1967
1968 case DW_AT_decl_line:
1969 func->line = attr.u.val;
1970 break;
1971
1972 default:
1973 break;
1974 }
1975 }
1976 else if (var)
1977 {
1978 switch (attr.name)
1979 {
1980 case DW_AT_name:
1981 var->name = attr.u.str;
1982 break;
1983
1984 case DW_AT_decl_file:
1985 var->file = concat_filename (unit->line_table,
1986 attr.u.val);
1987 break;
1988
1989 case DW_AT_decl_line:
1990 var->line = attr.u.val;
1991 break;
1992
1993 case DW_AT_external:
1994 if (attr.u.val != 0)
1995 var->stack = 0;
1996 break;
1997
1998 case DW_AT_location:
5cf2e3f0 1999 switch (attr.form)
5420f73d 2000 {
5cf2e3f0
L
2001 case DW_FORM_block:
2002 case DW_FORM_block1:
2003 case DW_FORM_block2:
2004 case DW_FORM_block4:
2005 if (*attr.u.blk->data == DW_OP_addr)
5420f73d 2006 {
5cf2e3f0 2007 var->stack = 0;
98b880f4
JW
2008
2009 /* Verify that DW_OP_addr is the only opcode in the
2010 location, in which case the block size will be 1
2011 plus the address size. */
2012 /* ??? For TLS variables, gcc can emit
2013 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2014 which we don't handle here yet. */
2015 if (attr.u.blk->size == unit->addr_size + 1U)
2016 var->addr = bfd_get (unit->addr_size * 8,
2017 unit->abfd,
2018 attr.u.blk->data + 1);
5420f73d 2019 }
5cf2e3f0 2020 break;
d8d1c398 2021
5cf2e3f0
L
2022 default:
2023 break;
5420f73d
L
2024 }
2025 break;
2026
252b5132
RH
2027 default:
2028 break;
2029 }
2030 }
2031 }
2032
a13afe8e
FF
2033 if (func && high_pc != 0)
2034 {
2035 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
2036 }
2037
252b5132 2038 if (abbrev->has_children)
c955f9cd
JW
2039 {
2040 nesting_level++;
2041
2042 if (nesting_level >= nested_funcs_size)
2043 {
2044 struct funcinfo **tmp;
2045
2046 nested_funcs_size *= 2;
2047 tmp = bfd_realloc (nested_funcs,
2048 (nested_funcs_size
2049 * sizeof (struct funcinfo *)));
2050 if (tmp == NULL)
2051 {
2052 free (nested_funcs);
2053 return FALSE;
2054 }
2055 nested_funcs = tmp;
2056 }
2057 nested_funcs[nesting_level] = 0;
2058 }
252b5132
RH
2059 }
2060
c955f9cd 2061 free (nested_funcs);
b34976b6 2062 return TRUE;
252b5132
RH
2063}
2064
5e38c3b8
MM
2065/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2066 includes the compilation unit header that proceeds the DIE's, but
5c4491d3 2067 does not include the length field that precedes each compilation
5e38c3b8 2068 unit header. END_PTR points one past the end of this comp unit.
d03ba2a1 2069 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
252b5132
RH
2070
2071 This routine does not read the whole compilation unit; only enough
2072 to get to the line number information for the compilation unit. */
2073
2074static struct comp_unit *
0d161102 2075parse_comp_unit (struct dwarf2_debug *stash,
818a27ac 2076 bfd_vma unit_length,
f075ee0c 2077 bfd_byte *info_ptr_unit,
818a27ac 2078 unsigned int offset_size)
252b5132
RH
2079{
2080 struct comp_unit* unit;
f46c2da6 2081 unsigned int version;
8ce8c090 2082 bfd_uint64_t abbrev_offset = 0;
f46c2da6 2083 unsigned int addr_size;
252b5132 2084 struct abbrev_info** abbrevs;
252b5132
RH
2085 unsigned int abbrev_number, bytes_read, i;
2086 struct abbrev_info *abbrev;
2087 struct attribute attr;
f075ee0c
AM
2088 bfd_byte *info_ptr = stash->info_ptr;
2089 bfd_byte *end_ptr = info_ptr + unit_length;
dc810e39 2090 bfd_size_type amt;
a13afe8e
FF
2091 bfd_vma low_pc = 0;
2092 bfd_vma high_pc = 0;
0d161102 2093 bfd *abfd = stash->bfd;
3fde5a36 2094
252b5132
RH
2095 version = read_2_bytes (abfd, info_ptr);
2096 info_ptr += 2;
d03ba2a1
JJ
2097 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2098 if (offset_size == 4)
5e38c3b8 2099 abbrev_offset = read_4_bytes (abfd, info_ptr);
d03ba2a1 2100 else
5e38c3b8 2101 abbrev_offset = read_8_bytes (abfd, info_ptr);
d03ba2a1 2102 info_ptr += offset_size;
252b5132
RH
2103 addr_size = read_1_byte (abfd, info_ptr);
2104 info_ptr += 1;
2105
2106 if (version != 2)
2107 {
f46c2da6 2108 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
252b5132
RH
2109 bfd_set_error (bfd_error_bad_value);
2110 return 0;
2111 }
2112
2113 if (addr_size > sizeof (bfd_vma))
2114 {
2115 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2116 addr_size,
f46c2da6 2117 (unsigned int) sizeof (bfd_vma));
252b5132
RH
2118 bfd_set_error (bfd_error_bad_value);
2119 return 0;
2120 }
2121
ecb651f0 2122 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 2123 {
f5a3e38a 2124 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
252b5132
RH
2125 bfd_set_error (bfd_error_bad_value);
2126 return 0;
2127 }
2128
a092b084 2129 /* Read the abbrevs for this compilation unit into a table. */
51db3708 2130 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
2131 if (! abbrevs)
2132 return 0;
2133
2134 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2135 info_ptr += bytes_read;
2136 if (! abbrev_number)
2137 {
f46c2da6 2138 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
252b5132
RH
2139 abbrev_number);
2140 bfd_set_error (bfd_error_bad_value);
2141 return 0;
2142 }
2143
2144 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2145 if (! abbrev)
2146 {
f46c2da6 2147 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
2148 abbrev_number);
2149 bfd_set_error (bfd_error_bad_value);
2150 return 0;
2151 }
98591c73 2152
dc810e39 2153 amt = sizeof (struct comp_unit);
818a27ac 2154 unit = bfd_zalloc (abfd, amt);
252b5132 2155 unit->abfd = abfd;
98591c73 2156 unit->addr_size = addr_size;
d03ba2a1 2157 unit->offset_size = offset_size;
252b5132
RH
2158 unit->abbrevs = abbrevs;
2159 unit->end_ptr = end_ptr;
d03ba2a1 2160 unit->stash = stash;
c0c28ab8 2161 unit->info_ptr_unit = info_ptr_unit;
252b5132
RH
2162
2163 for (i = 0; i < abbrev->num_attrs; ++i)
2164 {
2165 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2166
2167 /* Store the data if it is of an attribute we want to keep in a
2168 partial symbol table. */
2169 switch (attr.name)
2170 {
2171 case DW_AT_stmt_list:
2172 unit->stmtlist = 1;
482e2e37 2173 unit->line_offset = attr.u.val;
252b5132
RH
2174 break;
2175
2176 case DW_AT_name:
482e2e37 2177 unit->name = attr.u.str;
252b5132
RH
2178 break;
2179
2180 case DW_AT_low_pc:
a13afe8e
FF
2181 low_pc = attr.u.val;
2182 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2183 this is the base address to use when reading location
2184 lists or range lists. */
2185 unit->base_address = low_pc;
252b5132
RH
2186 break;
2187
2188 case DW_AT_high_pc:
a13afe8e
FF
2189 high_pc = attr.u.val;
2190 break;
2191
2192 case DW_AT_ranges:
709d67f1 2193 read_rangelist (unit, &unit->arange, attr.u.val);
252b5132
RH
2194 break;
2195
2196 case DW_AT_comp_dir:
2197 {
f075ee0c 2198 char *comp_dir = attr.u.str;
252b5132
RH
2199 if (comp_dir)
2200 {
2201 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2202 directory, get rid of it. */
818a27ac 2203 char *cp = strchr (comp_dir, ':');
252b5132
RH
2204
2205 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2206 comp_dir = cp + 1;
2207 }
2208 unit->comp_dir = comp_dir;
2209 break;
2210 }
2211
2212 default:
2213 break;
2214 }
2215 }
a13afe8e 2216 if (high_pc != 0)
709d67f1
AM
2217 {
2218 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2219 }
252b5132
RH
2220
2221 unit->first_child_die_ptr = info_ptr;
2222 return unit;
2223}
2224
6dd55cb7
L
2225/* Return TRUE if UNIT may contain the address given by ADDR. When
2226 there are functions written entirely with inline asm statements, the
2227 range info in the compilation unit header may not be correct. We
2228 need to consult the line info table to see if a compilation unit
2229 really contains the given address. */
252b5132 2230
b34976b6 2231static bfd_boolean
818a27ac 2232comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
252b5132 2233{
709d67f1
AM
2234 struct arange *arange;
2235
2236 if (unit->error)
2237 return FALSE;
2238
2239 arange = &unit->arange;
2240 do
2241 {
2242 if (addr >= arange->low && addr < arange->high)
2243 return TRUE;
2244 arange = arange->next;
2245 }
2246 while (arange);
2247
2248 return FALSE;
252b5132
RH
2249}
2250
252b5132
RH
2251/* If UNIT contains ADDR, set the output parameters to the values for
2252 the line containing ADDR. The output parameters, FILENAME_PTR,
2253 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 2254 to be filled in.
252b5132 2255
ab3acfbe 2256 Return TRUE if UNIT contains ADDR, and no errors were encountered;
b34976b6 2257 FALSE otherwise. */
252b5132 2258
b34976b6 2259static bfd_boolean
818a27ac
AM
2260comp_unit_find_nearest_line (struct comp_unit *unit,
2261 bfd_vma addr,
2262 const char **filename_ptr,
2263 const char **functionname_ptr,
2264 unsigned int *linenumber_ptr,
2265 struct dwarf2_debug *stash)
252b5132 2266{
b34976b6
AM
2267 bfd_boolean line_p;
2268 bfd_boolean func_p;
1ee24f27 2269 struct funcinfo *function;
98591c73 2270
252b5132 2271 if (unit->error)
b34976b6 2272 return FALSE;
252b5132
RH
2273
2274 if (! unit->line_table)
2275 {
2276 if (! unit->stmtlist)
2277 {
2278 unit->error = 1;
b34976b6 2279 return FALSE;
252b5132 2280 }
98591c73 2281
51db3708 2282 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
2283
2284 if (! unit->line_table)
2285 {
2286 unit->error = 1;
b34976b6 2287 return FALSE;
252b5132 2288 }
98591c73 2289
3f5864e1 2290 if (unit->first_child_die_ptr < unit->end_ptr
5420f73d 2291 && ! scan_unit_for_symbols (unit))
252b5132
RH
2292 {
2293 unit->error = 1;
b34976b6 2294 return FALSE;
252b5132
RH
2295 }
2296 }
2297
1ee24f27 2298 function = NULL;
4ab527b0 2299 func_p = lookup_address_in_function_table (unit, addr,
e2f6d277 2300 &function, functionname_ptr);
4ab527b0
FF
2301 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2302 stash->inliner_chain = function;
e2f6d277
NC
2303 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2304 function, filename_ptr,
252b5132 2305 linenumber_ptr);
b34976b6 2306 return line_p || func_p;
252b5132
RH
2307}
2308
bd210d54
NC
2309/* Check to see if line info is already decoded in a comp_unit.
2310 If not, decode it. Returns TRUE if no errors were encountered;
5420f73d
L
2311 FALSE otherwise. */
2312
2313static bfd_boolean
bd210d54
NC
2314comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2315 struct dwarf2_debug *stash)
5420f73d
L
2316{
2317 if (unit->error)
2318 return FALSE;
2319
2320 if (! unit->line_table)
2321 {
2322 if (! unit->stmtlist)
2323 {
2324 unit->error = 1;
2325 return FALSE;
2326 }
2327
2328 unit->line_table = decode_line_info (unit, stash);
2329
2330 if (! unit->line_table)
2331 {
2332 unit->error = 1;
2333 return FALSE;
2334 }
2335
2336 if (unit->first_child_die_ptr < unit->end_ptr
2337 && ! scan_unit_for_symbols (unit))
2338 {
2339 unit->error = 1;
2340 return FALSE;
2341 }
2342 }
2343
bd210d54
NC
2344 return TRUE;
2345}
2346
2347/* If UNIT contains SYM at ADDR, set the output parameters to the
2348 values for the line containing SYM. The output parameters,
2349 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2350 filled in.
2351
2352 Return TRUE if UNIT contains SYM, and no errors were encountered;
2353 FALSE otherwise. */
2354
2355static bfd_boolean
2356comp_unit_find_line (struct comp_unit *unit,
2357 asymbol *sym,
2358 bfd_vma addr,
2359 const char **filename_ptr,
2360 unsigned int *linenumber_ptr,
2361 struct dwarf2_debug *stash)
2362{
2363 if (!comp_unit_maybe_decode_line_info (unit, stash))
2364 return FALSE;
2365
5420f73d
L
2366 if (sym->flags & BSF_FUNCTION)
2367 return lookup_symbol_in_function_table (unit, sym, addr,
2368 filename_ptr,
2369 linenumber_ptr);
bd210d54
NC
2370
2371 return lookup_symbol_in_variable_table (unit, sym, addr,
2372 filename_ptr,
2373 linenumber_ptr);
2374}
2375
2376static struct funcinfo *
2377reverse_funcinfo_list (struct funcinfo *head)
2378{
2379 struct funcinfo *rhead;
2380 struct funcinfo *temp;
2381
2382 for (rhead = NULL; head; head = temp)
2383 {
2384 temp = head->prev_func;
2385 head->prev_func = rhead;
2386 rhead = head;
2387 }
2388 return rhead;
2389}
2390
2391static struct varinfo *
2392reverse_varinfo_list (struct varinfo *head)
2393{
2394 struct varinfo *rhead;
2395 struct varinfo *temp;
2396
2397 for (rhead = NULL; head; head = temp)
2398 {
2399 temp = head->prev_var;
2400 head->prev_var = rhead;
2401 rhead = head;
2402 }
2403 return rhead;
2404}
2405
2406/* Extract all interesting funcinfos and varinfos of a compilation
2407 unit into hash tables for faster lookup. Returns TRUE if no
2408 errors were enountered; FALSE otherwise. */
2409
2410static bfd_boolean
2411comp_unit_hash_info (struct dwarf2_debug *stash,
2412 struct comp_unit *unit,
2413 struct info_hash_table *funcinfo_hash_table,
2414 struct info_hash_table *varinfo_hash_table)
2415{
2416 struct funcinfo* each_func;
2417 struct varinfo* each_var;
2418 bfd_boolean okay = TRUE;
2419
2420 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2421
2422 if (!comp_unit_maybe_decode_line_info (unit, stash))
2423 return FALSE;
2424
2425 BFD_ASSERT (!unit->cached);
2426
2427 /* To preserve the original search order, we went to visit the function
2428 infos in the reversed order of the list. However, making the list
2429 bi-directional use quite a bit of extra memory. So we reverse
2430 the list first, traverse the list in the now reversed order and
2431 finally reverse the list again to get back the original order. */
2432 unit->function_table = reverse_funcinfo_list (unit->function_table);
2433 for (each_func = unit->function_table;
2434 each_func && okay;
2435 each_func = each_func->prev_func)
2436 {
2437 /* Skip nameless functions. */
2438 if (each_func->name)
2439 /* There is no need to copy name string into hash table as
2440 name string is either in the dwarf string buffer or
2441 info in the stash. */
2442 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2443 (void*) each_func, FALSE);
2444 }
2445 unit->function_table = reverse_funcinfo_list (unit->function_table);
2446 if (!okay)
2447 return FALSE;
2448
2449 /* We do the same for variable infos. */
2450 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2451 for (each_var = unit->variable_table;
2452 each_var && okay;
2453 each_var = each_var->prev_var)
2454 {
2455 /* Skip stack vars and vars with no files or names. */
2456 if (each_var->stack == 0
2457 && each_var->file != NULL
2458 && each_var->name != NULL)
2459 /* There is no need to copy name string into hash table as
2460 name string is either in the dwarf string buffer or
2461 info in the stash. */
2462 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2463 (void*) each_var, FALSE);
2464 }
2465
2466 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2467 unit->cached = TRUE;
2468 return okay;
5420f73d
L
2469}
2470
e2f6d277
NC
2471/* Locate a section in a BFD containing debugging info. The search starts
2472 from the section after AFTER_SEC, or from the first section in the BFD if
2473 AFTER_SEC is NULL. The search works by examining the names of the
2474 sections. There are two permissiable names. The first is .debug_info.
2475 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2476 This is a variation on the .debug_info section which has a checksum
2477 describing the contents appended onto the name. This allows the linker to
2478 identify and discard duplicate debugging sections for different
2479 compilation units. */
a092b084
NC
2480#define DWARF2_DEBUG_INFO ".debug_info"
2481#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2482
2483static asection *
818a27ac 2484find_debug_info (bfd *abfd, asection *after_sec)
a092b084
NC
2485{
2486 asection * msec;
2487
0d161102 2488 msec = after_sec != NULL ? after_sec->next : abfd->sections;
a092b084
NC
2489
2490 while (msec)
2491 {
2492 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2493 return msec;
2494
0112cd26 2495 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
a092b084
NC
2496 return msec;
2497
2498 msec = msec->next;
2499 }
2500
2501 return NULL;
2502}
2503
d4c32a81
L
2504/* Unset vmas for loadable sections in STASH. */
2505
2506static void
2507unset_sections (struct dwarf2_debug *stash)
2508{
2509 unsigned int i;
2510 struct loadable_section *p;
2511
2512 i = stash->loadable_section_count;
2513 p = stash->loadable_sections;
2514 for (; i > 0; i--, p++)
2515 p->section->vma = 0;
2516}
2517
2518/* Set unique vmas for loadable sections in ABFD and save vmas in
2519 STASH for unset_sections. */
35ccda9e
L
2520
2521static bfd_boolean
d4c32a81 2522place_sections (bfd *abfd, struct dwarf2_debug *stash)
35ccda9e 2523{
d4c32a81
L
2524 struct loadable_section *p;
2525 unsigned int i;
2526
2527 if (stash->loadable_section_count != 0)
35ccda9e 2528 {
d4c32a81
L
2529 i = stash->loadable_section_count;
2530 p = stash->loadable_sections;
2531 for (; i > 0; i--, p++)
2532 p->section->vma = p->adj_vma;
2533 }
2534 else
2535 {
2536 asection *sect;
2537 bfd_vma last_vma = 0;
2538 bfd_size_type amt;
2539 struct loadable_section *p;
35ccda9e 2540
d4c32a81
L
2541 i = 0;
2542 for (sect = abfd->sections; sect != NULL; sect = sect->next)
35ccda9e 2543 {
d4c32a81
L
2544 bfd_size_type sz;
2545
2546 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2547 continue;
2548
2549 sz = sect->rawsize ? sect->rawsize : sect->size;
2550 if (sz == 0)
2551 continue;
2552
2553 i++;
2554 }
2555
2556 amt = i * sizeof (struct loadable_section);
2557 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2558 if (! p)
2559 return FALSE;
2560
2561 stash->loadable_sections = p;
2562 stash->loadable_section_count = i;
2563
2564 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2565 {
2566 bfd_size_type sz;
2567
2568 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2569 continue;
2570
2571 sz = sect->rawsize ? sect->rawsize : sect->size;
2572 if (sz == 0)
2573 continue;
2574
2575 p->section = sect;
2576 if (last_vma != 0)
2577 {
2578 /* Align the new address to the current section
2579 alignment. */
2580 last_vma = ((last_vma
2581 + ~((bfd_vma) -1 << sect->alignment_power))
2582 & ((bfd_vma) -1 << sect->alignment_power));
2583 sect->vma = last_vma;
2584 }
2585 p->adj_vma = sect->vma;
2586 last_vma += sect->vma + sz;
2587
2588 p++;
35ccda9e
L
2589 }
2590 }
2591
2592 return TRUE;
2593}
2594
bd210d54
NC
2595/* Look up a funcinfo by name using the given info hash table. If found,
2596 also update the locations pointed to by filename_ptr and linenumber_ptr.
2597
2598 This function returns TRUE if a funcinfo that matches the given symbol
2599 and address is found with any error; otherwise it returns FALSE. */
2600
2601static bfd_boolean
2602info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2603 asymbol *sym,
2604 bfd_vma addr,
2605 const char **filename_ptr,
2606 unsigned int *linenumber_ptr)
2607{
2608 struct funcinfo* each_func;
2609 struct funcinfo* best_fit = NULL;
2610 struct info_list_node *node;
2611 struct arange *arange;
2612 const char *name = bfd_asymbol_name (sym);
2613 asection *sec = bfd_get_section (sym);
2614
2615 for (node = lookup_info_hash_table (hash_table, name);
2616 node;
2617 node = node->next)
2618 {
2619 each_func = node->info;
2620 for (arange = &each_func->arange;
2621 arange;
2622 arange = arange->next)
2623 {
2624 if ((!each_func->sec || each_func->sec == sec)
2625 && addr >= arange->low
2626 && addr < arange->high
2627 && (!best_fit
2628 || ((arange->high - arange->low)
2629 < (best_fit->arange.high - best_fit->arange.low))))
2630 best_fit = each_func;
2631 }
2632 }
2633
2634 if (best_fit)
2635 {
2636 best_fit->sec = sec;
2637 *filename_ptr = best_fit->file;
2638 *linenumber_ptr = best_fit->line;
2639 return TRUE;
2640 }
2641
2642 return FALSE;
2643}
2644
2645/* Look up a varinfo by name using the given info hash table. If found,
2646 also update the locations pointed to by filename_ptr and linenumber_ptr.
2647
2648 This function returns TRUE if a varinfo that matches the given symbol
2649 and address is found with any error; otherwise it returns FALSE. */
2650
2651static bfd_boolean
2652info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2653 asymbol *sym,
2654 bfd_vma addr,
2655 const char **filename_ptr,
2656 unsigned int *linenumber_ptr)
2657{
2658 const char *name = bfd_asymbol_name (sym);
2659 asection *sec = bfd_get_section (sym);
2660 struct varinfo* each;
2661 struct info_list_node *node;
2662
2663 for (node = lookup_info_hash_table (hash_table, name);
2664 node;
2665 node = node->next)
2666 {
2667 each = node->info;
2668 if (each->addr == addr
2669 && (!each->sec || each->sec == sec))
2670 {
2671 each->sec = sec;
2672 *filename_ptr = each->file;
2673 *linenumber_ptr = each->line;
2674 return TRUE;
2675 }
2676 }
2677
2678 return FALSE;
2679}
2680
2681/* Update the funcinfo and varinfo info hash tables if they are
2682 not up to date. Returns TRUE if there is no error; otherwise
2683 returns FALSE and disable the info hash tables. */
2684
2685static bfd_boolean
2686stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2687{
2688 struct comp_unit *each;
2689
2690 /* Exit if hash tables are up-to-date. */
2691 if (stash->all_comp_units == stash->hash_units_head)
2692 return TRUE;
2693
2694 if (stash->hash_units_head)
2695 each = stash->hash_units_head->prev_unit;
2696 else
2697 each = stash->last_comp_unit;
2698
2699 while (each)
2700 {
2701 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2702 stash->varinfo_hash_table))
2703 {
2704 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2705 return FALSE;
2706 }
2707 each = each->prev_unit;
2708 }
2709
2710 stash->hash_units_head = stash->all_comp_units;
2711 return TRUE;
2712}
2713
2714/* Check consistency of info hash tables. This is for debugging only. */
2715
2716static void ATTRIBUTE_UNUSED
2717stash_verify_info_hash_table (struct dwarf2_debug *stash)
2718{
2719 struct comp_unit *each_unit;
2720 struct funcinfo *each_func;
2721 struct varinfo *each_var;
2722 struct info_list_node *node;
2723 bfd_boolean found;
2724
2725 for (each_unit = stash->all_comp_units;
2726 each_unit;
2727 each_unit = each_unit->next_unit)
2728 {
2729 for (each_func = each_unit->function_table;
2730 each_func;
2731 each_func = each_func->prev_func)
2732 {
2733 if (!each_func->name)
2734 continue;
2735 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2736 each_func->name);
2737 BFD_ASSERT (node);
2738 found = FALSE;
2739 while (node && !found)
2740 {
2741 found = node->info == each_func;
2742 node = node->next;
2743 }
2744 BFD_ASSERT (found);
2745 }
2746
2747 for (each_var = each_unit->variable_table;
2748 each_var;
2749 each_var = each_var->prev_var)
2750 {
2751 if (!each_var->name || !each_var->file || each_var->stack)
2752 continue;
2753 node = lookup_info_hash_table (stash->varinfo_hash_table,
2754 each_var->name);
2755 BFD_ASSERT (node);
2756 found = FALSE;
2757 while (node && !found)
2758 {
2759 found = node->info == each_var;
2760 node = node->next;
2761 }
2762 BFD_ASSERT (found);
2763 }
2764 }
2765}
2766
2767/* Check to see if we want to enable the info hash tables, which consume
2768 quite a bit of memory. Currently we only check the number times
2769 bfd_dwarf2_find_line is called. In the future, we may also want to
2770 take the number of symbols into account. */
2771
2772static void
2773stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2774{
2775 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2776
2777 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2778 return;
2779
2780 /* FIXME: Maybe we should check the reduce_memory_overheads
2781 and optimize fields in the bfd_link_info structure ? */
2782
2783 /* Create hash tables. */
2784 stash->funcinfo_hash_table = create_info_hash_table (abfd);
2785 stash->varinfo_hash_table = create_info_hash_table (abfd);
2786 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2787 {
2788 /* Turn off info hashes if any allocation above fails. */
2789 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2790 return;
2791 }
2792 /* We need a forced update so that the info hash tables will
2793 be created even though there is no compilation unit. That
2794 happens if STASH_INFO_HASH_TRIGGER is 0. */
2795 stash_maybe_update_info_hash_tables (stash);
2796 stash->info_hash_status = STASH_INFO_HASH_ON;
2797}
2798
2799/* Find the file and line associated with a symbol and address using the
2800 info hash tables of a stash. If there is a match, the function returns
2801 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2802 otherwise it returns FALSE. */
2803
2804static bfd_boolean
2805stash_find_line_fast (struct dwarf2_debug *stash,
2806 asymbol *sym,
2807 bfd_vma addr,
2808 const char **filename_ptr,
2809 unsigned int *linenumber_ptr)
2810{
2811 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2812
2813 if (sym->flags & BSF_FUNCTION)
2814 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2815 filename_ptr, linenumber_ptr);
2816 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2817 filename_ptr, linenumber_ptr);
2818}
2819
bec42b15
NC
2820/* Find the source code location of SYMBOL. If SYMBOL is NULL
2821 then find the nearest source code location corresponding to
2822 the address SECTION + OFFSET.
2823 Returns TRUE if the line is found without error and fills in
2824 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2825 NULL the FUNCTIONNAME_PTR is also filled in.
2826 SYMBOLS contains the symbol table for ABFD.
2827 ADDR_SIZE is the number of bytes in the initial .debug_info length
2828 field and in the abbreviation offset, or zero to indicate that the
2829 default value should be used. */
252b5132 2830
bec42b15
NC
2831static bfd_boolean
2832find_line (bfd *abfd,
2833 asection *section,
2834 bfd_vma offset,
2835 asymbol *symbol,
2836 asymbol **symbols,
2837 const char **filename_ptr,
2838 const char **functionname_ptr,
2839 unsigned int *linenumber_ptr,
2840 unsigned int addr_size,
2841 void **pinfo)
252b5132
RH
2842{
2843 /* Read each compilation unit from the section .debug_info, and check
2844 to see if it contains the address we are searching for. If yes,
2845 lookup the address, and return the line number info. If no, go
98591c73 2846 on to the next compilation unit.
252b5132
RH
2847
2848 We keep a list of all the previously read compilation units, and
98591c73 2849 a pointer to the next un-read compilation unit. Check the
a092b084 2850 previously read units before reading more. */
1ba54ee0 2851 struct dwarf2_debug *stash;
a092b084 2852 /* What address are we looking for? */
1ba54ee0 2853 bfd_vma addr;
252b5132 2854 struct comp_unit* each;
d4c32a81 2855 bfd_vma found = FALSE;
bec42b15 2856 bfd_boolean do_line;
d4c32a81 2857
1ba54ee0 2858 stash = *pinfo;
d4c32a81
L
2859
2860 if (! stash)
2861 {
2862 bfd_size_type amt = sizeof (struct dwarf2_debug);
2863
2864 stash = bfd_zalloc (abfd, amt);
2865 if (! stash)
2866 return FALSE;
2867 }
2868
2869 /* In a relocatable file, 2 functions may have the same address.
2870 We change the section vma so that they won't overlap. */
2871 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2872 {
2873 if (! place_sections (abfd, stash))
2874 return FALSE;
2875 }
2876
bec42b15
NC
2877 do_line = (section == NULL
2878 && offset == 0
2879 && functionname_ptr == NULL
2880 && symbol != NULL);
2881 if (do_line)
2882 {
2883 addr = symbol->value;
2884 section = bfd_get_section (symbol);
2885 }
2886 else if (section != NULL
2887 && functionname_ptr != NULL
2888 && symbol == NULL)
2889 addr = offset;
2890 else
2891 abort ();
2892
1ba54ee0 2893 if (section->output_section)
6dd55cb7 2894 addr += section->output_section->vma + section->output_offset;
1ba54ee0 2895 else
6dd55cb7 2896 addr += section->vma;
252b5132 2897 *filename_ptr = NULL;
6b33789f
NC
2898 if (! do_line)
2899 *functionname_ptr = NULL;
252b5132
RH
2900 *linenumber_ptr = 0;
2901
d4c32a81 2902 if (! *pinfo)
252b5132 2903 {
0d161102 2904 bfd *debug_bfd;
dc810e39 2905 bfd_size_type total_size;
252b5132 2906 asection *msec;
252b5132 2907
818a27ac 2908 *pinfo = stash;
3fde5a36 2909
a092b084 2910 msec = find_debug_info (abfd, NULL);
0d161102
NC
2911 if (msec == NULL)
2912 {
0d4a1476 2913 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
0d161102
NC
2914
2915 if (debug_filename == NULL)
2916 /* No dwarf2 info, and no gnu_debuglink to follow.
2917 Note that at this point the stash has been allocated, but
2918 contains zeros. This lets future calls to this function
2919 fail more quickly. */
2920 goto done;
2921
2922 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2923 || ! bfd_check_format (debug_bfd, bfd_object)
2924 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2925 {
2926 if (debug_bfd)
2927 bfd_close (debug_bfd);
2928 /* FIXME: Should we report our failure to follow the debuglink ? */
2929 free (debug_filename);
2930 goto done;
2931 }
2932 }
2933 else
2934 debug_bfd = abfd;
a092b084
NC
2935
2936 /* There can be more than one DWARF2 info section in a BFD these days.
e82ce529 2937 Read them all in and produce one large stash. We do this in two
a092b084
NC
2938 passes - in the first pass we just accumulate the section sizes.
2939 In the second pass we read in the section's contents. The allows
2940 us to avoid reallocing the data as we add sections to the stash. */
0d161102 2941 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
eea6121a 2942 total_size += msec->size;
98591c73 2943
0d161102 2944 stash->info_ptr = bfd_alloc (debug_bfd, total_size);
a092b084 2945 if (stash->info_ptr == NULL)
d4c32a81 2946 goto done;
252b5132 2947
a092b084
NC
2948 stash->info_ptr_end = stash->info_ptr;
2949
0d161102 2950 for (msec = find_debug_info (debug_bfd, NULL);
a092b084 2951 msec;
0d161102 2952 msec = find_debug_info (debug_bfd, msec))
252b5132 2953 {
dc810e39
AM
2954 bfd_size_type size;
2955 bfd_size_type start;
a092b084 2956
eea6121a 2957 size = msec->size;
a092b084
NC
2958 if (size == 0)
2959 continue;
2960
2961 start = stash->info_ptr_end - stash->info_ptr;
2962
6e84a906 2963 if ((bfd_simple_get_relocated_section_contents
0d161102 2964 (debug_bfd, msec, stash->info_ptr + start, symbols)) == NULL)
a092b084
NC
2965 continue;
2966
2967 stash->info_ptr_end = stash->info_ptr + start + size;
252b5132
RH
2968 }
2969
f2363ce5
AO
2970 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2971
0d161102 2972 stash->sec = find_debug_info (debug_bfd, NULL);
f2363ce5
AO
2973 stash->sec_info_ptr = stash->info_ptr;
2974 stash->syms = symbols;
0d161102 2975 stash->bfd = debug_bfd;
252b5132 2976 }
a092b084 2977
98591c73 2978 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 2979 (or that an error occured while setting up the stash). */
252b5132 2980 if (! stash->info_ptr)
d4c32a81 2981 goto done;
252b5132 2982
4ab527b0
FF
2983 stash->inliner_chain = NULL;
2984
a092b084 2985 /* Check the previously read comp. units first. */
bd210d54
NC
2986 if (do_line)
2987 {
2988 /* The info hash tables use quite a bit of memory. We may not want to
2989 always use them. We use some heuristics to decide if and when to
2990 turn it on. */
2991 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
2992 stash_maybe_enable_info_hash_tables (abfd, stash);
2993
2994 /* Keep info hash table up to date if they are available. Note that we
2995 may disable the hash tables if there is any error duing update. */
2996 if (stash->info_hash_status == STASH_INFO_HASH_ON)
2997 stash_maybe_update_info_hash_tables (stash);
2998
2999 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3000 {
3001 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3002 linenumber_ptr);
3003 if (found)
3004 goto done;
3005 }
0d161102 3006 else
bd210d54
NC
3007 {
3008 /* Check the previously read comp. units first. */
3009 for (each = stash->all_comp_units; each; each = each->next_unit)
3010 if ((symbol->flags & BSF_FUNCTION) == 0
3011 || comp_unit_contains_address (each, addr))
3012 {
3013 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3014 linenumber_ptr, stash);
3015 if (found)
3016 goto done;
3017 }
3018 }
3019 }
3020 else
3021 {
709d67f1
AM
3022 for (each = stash->all_comp_units; each; each = each->next_unit)
3023 {
3024 found = (comp_unit_contains_address (each, addr)
3025 && comp_unit_find_nearest_line (each, addr,
3026 filename_ptr,
3027 functionname_ptr,
3028 linenumber_ptr,
3029 stash));
3030 if (found)
3031 goto done;
3032 }
5420f73d
L
3033 }
3034
5420f73d
L
3035 /* The DWARF2 spec says that the initial length field, and the
3036 offset of the abbreviation table, should both be 4-byte values.
3037 However, some compilers do things differently. */
3038 if (addr_size == 0)
3039 addr_size = 4;
3040 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3041
3042 /* Read each remaining comp. units checking each as they are read. */
3043 while (stash->info_ptr < stash->info_ptr_end)
3044 {
3045 bfd_vma length;
3046 unsigned int offset_size = addr_size;
3047 bfd_byte *info_ptr_unit = stash->info_ptr;
3048
0d161102 3049 length = read_4_bytes (stash->bfd, stash->info_ptr);
bec42b15
NC
3050 /* A 0xffffff length is the DWARF3 way of indicating
3051 we use 64-bit offsets, instead of 32-bit offsets. */
5420f73d
L
3052 if (length == 0xffffffff)
3053 {
3054 offset_size = 8;
0d161102 3055 length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
5420f73d
L
3056 stash->info_ptr += 12;
3057 }
3058 /* A zero length is the IRIX way of indicating 64-bit offsets,
3059 mostly because the 64-bit length will generally fit in 32
3060 bits, and the endianness helps. */
3061 else if (length == 0)
3062 {
3063 offset_size = 8;
0d161102 3064 length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
5420f73d
L
3065 stash->info_ptr += 8;
3066 }
024b2372
CD
3067 /* In the absence of the hints above, we assume 32-bit DWARF2
3068 offsets even for targets with 64-bit addresses, because:
3069 a) most of the time these targets will not have generated
3070 more than 2Gb of debug info and so will not need 64-bit
3071 offsets,
3072 and
3073 b) if they do use 64-bit offsets but they are not using
3074 the size hints that are tested for above then they are
3075 not conforming to the DWARF3 standard anyway. */
5420f73d
L
3076 else if (addr_size == 8)
3077 {
024b2372
CD
3078 offset_size = 4;
3079 stash->info_ptr += 4;
5420f73d
L
3080 }
3081 else
3082 stash->info_ptr += 4;
3083
3084 if (length > 0)
3085 {
0d161102 3086 each = parse_comp_unit (stash, length, info_ptr_unit,
5420f73d 3087 offset_size);
d74e4b29
NS
3088 if (!each)
3089 /* The dwarf information is damaged, don't trust it any
3090 more. */
3091 break;
5420f73d
L
3092 stash->info_ptr += length;
3093
3094 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3095 == stash->sec->size)
3096 {
0d161102 3097 stash->sec = find_debug_info (stash->bfd, stash->sec);
5420f73d
L
3098 stash->sec_info_ptr = stash->info_ptr;
3099 }
3100
d74e4b29
NS
3101 if (stash->all_comp_units)
3102 stash->all_comp_units->prev_unit = each;
3103 else
3104 stash->last_comp_unit = each;
3105
3106 each->next_unit = stash->all_comp_units;
3107 stash->all_comp_units = each;
3108
3109 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3110 compilation units. If we don't have them (i.e.,
3111 unit->high == 0), we need to consult the line info table
3112 to see if a compilation unit contains the given
3113 address. */
3114 if (do_line)
3115 found = (((symbol->flags & BSF_FUNCTION) == 0
3116 || each->arange.high == 0
3117 || comp_unit_contains_address (each, addr))
3118 && comp_unit_find_line (each, symbol, addr,
3119 filename_ptr,
3120 linenumber_ptr,
3121 stash));
3122 else
3123 found = ((each->arange.high == 0
3124 || comp_unit_contains_address (each, addr))
3125 && comp_unit_find_nearest_line (each, addr,
3126 filename_ptr,
3127 functionname_ptr,
3128 linenumber_ptr,
3129 stash));
3130 if (found)
3131 goto done;
5420f73d
L
3132 }
3133 }
3134
d4c32a81
L
3135done:
3136 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3137 unset_sections (stash);
3138
3139 return found;
5420f73d
L
3140}
3141
bec42b15
NC
3142/* The DWARF2 version of find_nearest_line.
3143 Return TRUE if the line is found without error. */
3144
3145bfd_boolean
3146_bfd_dwarf2_find_nearest_line (bfd *abfd,
3147 asection *section,
3148 asymbol **symbols,
3149 bfd_vma offset,
3150 const char **filename_ptr,
3151 const char **functionname_ptr,
3152 unsigned int *linenumber_ptr,
3153 unsigned int addr_size,
3154 void **pinfo)
3155{
3156 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3157 functionname_ptr, linenumber_ptr, addr_size,
3158 pinfo);
3159}
3160
3161/* The DWARF2 version of find_line.
3162 Return TRUE if the line is found without error. */
3163
3164bfd_boolean
3165_bfd_dwarf2_find_line (bfd *abfd,
3166 asymbol **symbols,
3167 asymbol *symbol,
3168 const char **filename_ptr,
3169 unsigned int *linenumber_ptr,
3170 unsigned int addr_size,
3171 void **pinfo)
3172{
3173 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3174 NULL, linenumber_ptr, addr_size,
3175 pinfo);
3176}
3177
4ab527b0
FF
3178bfd_boolean
3179_bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3180 const char **filename_ptr,
3181 const char **functionname_ptr,
3182 unsigned int *linenumber_ptr,
3183 void **pinfo)
3184{
3185 struct dwarf2_debug *stash;
3186
3187 stash = *pinfo;
3188 if (stash)
3189 {
3190 struct funcinfo *func = stash->inliner_chain;
bec42b15 3191
4ab527b0
FF
3192 if (func && func->caller_func)
3193 {
3194 *filename_ptr = func->caller_file;
3195 *functionname_ptr = func->caller_func->name;
3196 *linenumber_ptr = func->caller_line;
3197 stash->inliner_chain = func->caller_func;
bec42b15 3198 return TRUE;
4ab527b0
FF
3199 }
3200 }
3201
bec42b15 3202 return FALSE;
4ab527b0
FF
3203}
3204
35330cce
NC
3205void
3206_bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3207{
3208 struct comp_unit *each;
3209 struct dwarf2_debug *stash;
3210
3211 if (abfd == NULL || elf_tdata (abfd) == NULL)
3212 return;
3213
3214 stash = elf_tdata (abfd)->dwarf2_find_line_info;
3215
3216 if (stash == NULL)
3217 return;
3218
3219 for (each = stash->all_comp_units; each; each = each->next_unit)
3220 {
b42bf1e0
NC
3221 struct funcinfo * function_table = each->function_table;
3222 struct varinfo * variable_table = each->variable_table;
35330cce 3223
b42bf1e0 3224 if (each->abbrevs != NULL)
d8d1c398 3225 {
b42bf1e0 3226 size_t i;
35330cce 3227
b42bf1e0 3228 for (i = 0; i < ABBREV_HASH_SIZE; i++)
d8d1c398 3229 {
b42bf1e0
NC
3230 struct abbrev_info *abbrev = each->abbrevs[i];
3231
3232 while (abbrev)
3233 {
3234 struct abbrev_info * a = abbrev;
3235
3236 abbrev = a->next;
3237 if (a->attrs)
3238 free (a->attrs);
3239 free (a);
3240 }
d8d1c398 3241 }
b42bf1e0
NC
3242
3243 free (each->abbrevs);
3244 each->abbrevs = NULL;
d8d1c398 3245 }
35330cce
NC
3246
3247 if (each->line_table)
d8d1c398 3248 {
b42bf1e0
NC
3249 /* FIXME: We should free the line_info structures as well. */
3250 if (each->line_table->dirs)
3251 free (each->line_table->dirs);
3252
3253 if (each->line_table->files)
3254 free (each->line_table->files);
3255
3256 free (each->line_table);
3257 each->line_table = NULL;
d8d1c398 3258 }
b42bf1e0
NC
3259
3260 while (function_table)
3261 {
3262 struct funcinfo * f = function_table;
3263
3264 function_table = f->prev_func;
3265
3266 if (f->file)
3267 free (f->file);
3268
3269 if (f->caller_file)
3270 free (f->caller_file);
3271
3272 free (f);
3273 }
3274
3275 each->function_table = NULL;
3276
3277 while (variable_table)
3278 {
3279 struct varinfo * v = variable_table;
3280
3281 variable_table = variable_table->prev_var;
3282
3283 if (v->file)
3284 free (v->file);
3285
3286 free (v);
3287 }
3288
3289 each->variable_table = NULL;
35330cce
NC
3290 }
3291
3292 free (stash->dwarf_abbrev_buffer);
3293 free (stash->dwarf_line_buffer);
3294 free (stash->dwarf_ranges_buffer);
3295}
This page took 0.609131 seconds and 4 git commands to generate.