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