*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
0fb0cc75
JB
4 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
4c2df51b 51
c906108c
SS
52#include <fcntl.h>
53#include "gdb_string.h"
4bdf3d34 54#include "gdb_assert.h"
c906108c 55#include <sys/types.h>
233a11ab
CS
56#ifdef HAVE_ZLIB_H
57#include <zlib.h>
58#endif
dce234bc
PP
59#ifdef HAVE_MMAP
60#include <sys/mman.h>
61#endif
d8151005 62
107d2387 63#if 0
357e46e7 64/* .debug_info header for a compilation unit
c906108c
SS
65 Because of alignment constraints, this structure has padding and cannot
66 be mapped directly onto the beginning of the .debug_info section. */
67typedef struct comp_unit_header
68 {
69 unsigned int length; /* length of the .debug_info
70 contribution */
71 unsigned short version; /* version number -- 2 for DWARF
72 version 2 */
73 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
74 unsigned char addr_size; /* byte size of an address -- 4 */
75 }
76_COMP_UNIT_HEADER;
77#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 78#endif
c906108c
SS
79
80/* .debug_pubnames header
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct pubnames_header
84 {
85 unsigned int length; /* length of the .debug_pubnames
86 contribution */
87 unsigned char version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int info_offset; /* offset into .debug_info section */
90 unsigned int info_size; /* byte size of .debug_info section
91 portion */
92 }
93_PUBNAMES_HEADER;
94#define _ACTUAL_PUBNAMES_HEADER_SIZE 13
95
96/* .debug_pubnames header
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct aranges_header
100 {
101 unsigned int length; /* byte len of the .debug_aranges
102 contribution */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int info_offset; /* offset into .debug_info section */
106 unsigned char addr_size; /* byte size of an address */
107 unsigned char seg_size; /* byte size of segment descriptor */
108 }
109_ARANGES_HEADER;
110#define _ACTUAL_ARANGES_HEADER_SIZE 12
111
112/* .debug_line statement program prologue
113 Because of alignment constraints, this structure has padding and cannot
114 be mapped directly onto the beginning of the .debug_info section. */
115typedef struct statement_prologue
116 {
117 unsigned int total_length; /* byte length of the statement
118 information */
119 unsigned short version; /* version number -- 2 for DWARF
120 version 2 */
121 unsigned int prologue_length; /* # bytes between prologue &
122 stmt program */
123 unsigned char minimum_instruction_length; /* byte size of
124 smallest instr */
125 unsigned char default_is_stmt; /* initial value of is_stmt
126 register */
127 char line_base;
128 unsigned char line_range;
129 unsigned char opcode_base; /* number assigned to first special
130 opcode */
131 unsigned char *standard_opcode_lengths;
132 }
133_STATEMENT_PROLOGUE;
134
d97bc12b
DE
135/* When non-zero, dump DIEs after they are read in. */
136static int dwarf2_die_debug = 0;
137
dce234bc
PP
138static int pagesize;
139
df8a16a1
DJ
140/* When set, the file that we're processing is known to have debugging
141 info for C++ namespaces. GCC 3.3.x did not produce this information,
142 but later versions do. */
143
144static int processing_has_namespace_info;
145
6502dd73
DJ
146static const struct objfile_data *dwarf2_objfile_data_key;
147
dce234bc
PP
148struct dwarf2_section_info
149{
150 asection *asection;
151 gdb_byte *buffer;
152 bfd_size_type size;
153 int was_mmapped;
154};
155
6502dd73
DJ
156struct dwarf2_per_objfile
157{
dce234bc
PP
158 struct dwarf2_section_info info;
159 struct dwarf2_section_info abbrev;
160 struct dwarf2_section_info line;
161 struct dwarf2_section_info pubnames;
162 struct dwarf2_section_info aranges;
163 struct dwarf2_section_info loc;
164 struct dwarf2_section_info macinfo;
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
348e048f 167 struct dwarf2_section_info types;
dce234bc
PP
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
ae038cb0 170
10b3939b
DJ
171 /* A list of all the compilation units. This is used to locate
172 the target compilation unit of a particular reference. */
ae038cb0
DJ
173 struct dwarf2_per_cu_data **all_comp_units;
174
175 /* The number of compilation units in ALL_COMP_UNITS. */
176 int n_comp_units;
177
178 /* A chain of compilation units that are currently read in, so that
179 they can be freed later. */
180 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 181
348e048f
DE
182 /* A table mapping .debug_types signatures to its signatured_type entry.
183 This is NULL if the .debug_types section hasn't been read in yet. */
184 htab_t signatured_types;
185
72dca2f5
FR
186 /* A flag indicating wether this objfile has a section loaded at a
187 VMA of 0. */
188 int has_section_at_zero;
6502dd73
DJ
189};
190
191static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
192
193/* names of the debugging sections */
194
233a11ab
CS
195/* Note that if the debugging section has been compressed, it might
196 have a name like .zdebug_info. */
197
198#define INFO_SECTION "debug_info"
199#define ABBREV_SECTION "debug_abbrev"
200#define LINE_SECTION "debug_line"
201#define PUBNAMES_SECTION "debug_pubnames"
202#define ARANGES_SECTION "debug_aranges"
203#define LOC_SECTION "debug_loc"
204#define MACINFO_SECTION "debug_macinfo"
205#define STR_SECTION "debug_str"
206#define RANGES_SECTION "debug_ranges"
348e048f 207#define TYPES_SECTION "debug_types"
233a11ab
CS
208#define FRAME_SECTION "debug_frame"
209#define EH_FRAME_SECTION "eh_frame"
c906108c
SS
210
211/* local data types */
212
57349743
JB
213/* We hold several abbreviation tables in memory at the same time. */
214#ifndef ABBREV_HASH_SIZE
215#define ABBREV_HASH_SIZE 121
216#endif
217
107d2387
AC
218/* The data in a compilation unit header, after target2host
219 translation, looks like this. */
c906108c 220struct comp_unit_head
a738430d 221{
c764a876 222 unsigned int length;
a738430d 223 short version;
a738430d
MK
224 unsigned char addr_size;
225 unsigned char signed_addr_p;
9cbfa09e 226 unsigned int abbrev_offset;
57349743 227
a738430d
MK
228 /* Size of file offsets; either 4 or 8. */
229 unsigned int offset_size;
57349743 230
a738430d
MK
231 /* Size of the length field; either 4 or 12. */
232 unsigned int initial_length_size;
57349743 233
a738430d
MK
234 /* Offset to the first byte of this compilation unit header in the
235 .debug_info section, for resolving relative reference dies. */
236 unsigned int offset;
57349743 237
d00adf39
DE
238 /* Offset to first die in this cu from the start of the cu.
239 This will be the first byte following the compilation unit header. */
240 unsigned int first_die_offset;
a738430d 241};
c906108c 242
e7c27a73
DJ
243/* Internal state when decoding a particular compilation unit. */
244struct dwarf2_cu
245{
246 /* The objfile containing this compilation unit. */
247 struct objfile *objfile;
248
d00adf39 249 /* The header of the compilation unit. */
e7c27a73 250 struct comp_unit_head header;
e142c38c 251
d00adf39
DE
252 /* Base address of this compilation unit. */
253 CORE_ADDR base_address;
254
255 /* Non-zero if base_address has been set. */
256 int base_known;
257
e142c38c
DJ
258 struct function_range *first_fn, *last_fn, *cached_fn;
259
260 /* The language we are debugging. */
261 enum language language;
262 const struct language_defn *language_defn;
263
b0f35d58
DL
264 const char *producer;
265
e142c38c
DJ
266 /* The generic symbol table building routines have separate lists for
267 file scope symbols and all all other scopes (local scopes). So
268 we need to select the right one to pass to add_symbol_to_list().
269 We do it by keeping a pointer to the correct list in list_in_scope.
270
271 FIXME: The original dwarf code just treated the file scope as the
272 first local scope, and all other local scopes as nested local
273 scopes, and worked fine. Check to see if we really need to
274 distinguish these in buildsym.c. */
275 struct pending **list_in_scope;
276
f3dd6933
DJ
277 /* DWARF abbreviation table associated with this compilation unit. */
278 struct abbrev_info **dwarf2_abbrevs;
279
280 /* Storage for the abbrev table. */
281 struct obstack abbrev_obstack;
72bf9492
DJ
282
283 /* Hash table holding all the loaded partial DIEs. */
284 htab_t partial_dies;
285
ff013f42 286 /* `.debug_ranges' offset for this `DW_TAG_compile_unit' DIE. */
c764a876 287 unsigned int ranges_offset;
ff013f42 288
72bf9492
DJ
289 /* Storage for things with the same lifetime as this read-in compilation
290 unit, including partial DIEs. */
291 struct obstack comp_unit_obstack;
292
ae038cb0
DJ
293 /* When multiple dwarf2_cu structures are living in memory, this field
294 chains them all together, so that they can be released efficiently.
295 We will probably also want a generation counter so that most-recently-used
296 compilation units are cached... */
297 struct dwarf2_per_cu_data *read_in_chain;
298
299 /* Backchain to our per_cu entry if the tree has been built. */
300 struct dwarf2_per_cu_data *per_cu;
301
f792889a
DJ
302 /* Pointer to the die -> type map. Although it is stored
303 permanently in per_cu, we copy it here to avoid double
304 indirection. */
305 htab_t type_hash;
306
ae038cb0
DJ
307 /* How many compilation units ago was this CU last referenced? */
308 int last_used;
309
10b3939b 310 /* A hash table of die offsets for following references. */
51545339 311 htab_t die_hash;
10b3939b
DJ
312
313 /* Full DIEs if read in. */
314 struct die_info *dies;
315
316 /* A set of pointers to dwarf2_per_cu_data objects for compilation
317 units referenced by this one. Only set during full symbol processing;
318 partial symbol tables do not have dependencies. */
319 htab_t dependencies;
320
cb1df416
DJ
321 /* Header data from the line table, during full symbol processing. */
322 struct line_header *line_header;
323
ae038cb0
DJ
324 /* Mark used when releasing cached dies. */
325 unsigned int mark : 1;
326
327 /* This flag will be set if this compilation unit might include
328 inter-compilation-unit references. */
329 unsigned int has_form_ref_addr : 1;
330
72bf9492
DJ
331 /* This flag will be set if this compilation unit includes any
332 DW_TAG_namespace DIEs. If we know that there are explicit
333 DIEs for namespaces, we don't need to try to infer them
334 from mangled names. */
335 unsigned int has_namespace_info : 1;
ff013f42
JK
336
337 /* Field `ranges_offset' is filled in; flag as the value may be zero. */
338 unsigned int has_ranges_offset : 1;
e7c27a73
DJ
339};
340
10b3939b
DJ
341/* Persistent data held for a compilation unit, even when not
342 processing it. We put a pointer to this structure in the
343 read_symtab_private field of the psymtab. If we encounter
344 inter-compilation-unit references, we also maintain a sorted
345 list of all compilation units. */
346
ae038cb0
DJ
347struct dwarf2_per_cu_data
348{
348e048f 349 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 350 bytes should suffice to store the length of any compilation unit
45452591
DE
351 - if it doesn't, GDB will fall over anyway.
352 NOTE: Unlike comp_unit_head.length, this length includes
353 initial_length_size. */
c764a876 354 unsigned int offset;
348e048f 355 unsigned int length : 29;
ae038cb0
DJ
356
357 /* Flag indicating this compilation unit will be read in before
358 any of the current compilation units are processed. */
c764a876 359 unsigned int queued : 1;
ae038cb0 360
5afb4e99
DJ
361 /* This flag will be set if we need to load absolutely all DIEs
362 for this compilation unit, instead of just the ones we think
363 are interesting. It gets set if we look for a DIE in the
364 hash table and don't find it. */
365 unsigned int load_all_dies : 1;
366
348e048f
DE
367 /* Non-zero if this CU is from .debug_types.
368 Otherwise it's from .debug_info. */
369 unsigned int from_debug_types : 1;
370
ae038cb0
DJ
371 /* Set iff currently read in. */
372 struct dwarf2_cu *cu;
1c379e20
DJ
373
374 /* If full symbols for this CU have been read in, then this field
375 holds a map of DIE offsets to types. It isn't always possible
376 to reconstruct this information later, so we have to preserve
377 it. */
1c379e20 378 htab_t type_hash;
10b3939b 379
31ffec48
DJ
380 /* The partial symbol table associated with this compilation unit,
381 or NULL for partial units (which do not have an associated
382 symtab). */
10b3939b 383 struct partial_symtab *psymtab;
ae038cb0
DJ
384};
385
348e048f
DE
386/* Entry in the signatured_types hash table. */
387
388struct signatured_type
389{
390 ULONGEST signature;
391
392 /* Offset in .debug_types of the TU (type_unit) for this type. */
393 unsigned int offset;
394
395 /* Offset in .debug_types of the type defined by this TU. */
396 unsigned int type_offset;
397
398 /* The CU(/TU) of this type. */
399 struct dwarf2_per_cu_data per_cu;
400};
401
93311388
DE
402/* Struct used to pass misc. parameters to read_die_and_children, et. al.
403 which are used for both .debug_info and .debug_types dies.
404 All parameters here are unchanging for the life of the call.
405 This struct exists to abstract away the constant parameters of
406 die reading. */
407
408struct die_reader_specs
409{
410 /* The bfd of this objfile. */
411 bfd* abfd;
412
413 /* The CU of the DIE we are parsing. */
414 struct dwarf2_cu *cu;
415
416 /* Pointer to start of section buffer.
417 This is either the start of .debug_info or .debug_types. */
418 const gdb_byte *buffer;
419};
420
debd256d
JB
421/* The line number information for a compilation unit (found in the
422 .debug_line section) begins with a "statement program header",
423 which contains the following information. */
424struct line_header
425{
426 unsigned int total_length;
427 unsigned short version;
428 unsigned int header_length;
429 unsigned char minimum_instruction_length;
430 unsigned char default_is_stmt;
431 int line_base;
432 unsigned char line_range;
433 unsigned char opcode_base;
434
435 /* standard_opcode_lengths[i] is the number of operands for the
436 standard opcode whose value is i. This means that
437 standard_opcode_lengths[0] is unused, and the last meaningful
438 element is standard_opcode_lengths[opcode_base - 1]. */
439 unsigned char *standard_opcode_lengths;
440
441 /* The include_directories table. NOTE! These strings are not
442 allocated with xmalloc; instead, they are pointers into
443 debug_line_buffer. If you try to free them, `free' will get
444 indigestion. */
445 unsigned int num_include_dirs, include_dirs_size;
446 char **include_dirs;
447
448 /* The file_names table. NOTE! These strings are not allocated
449 with xmalloc; instead, they are pointers into debug_line_buffer.
450 Don't try to free them directly. */
451 unsigned int num_file_names, file_names_size;
452 struct file_entry
c906108c 453 {
debd256d
JB
454 char *name;
455 unsigned int dir_index;
456 unsigned int mod_time;
457 unsigned int length;
aaa75496 458 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 459 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
460 } *file_names;
461
462 /* The start and end of the statement program following this
6502dd73 463 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 464 gdb_byte *statement_program_start, *statement_program_end;
debd256d 465};
c906108c
SS
466
467/* When we construct a partial symbol table entry we only
468 need this much information. */
469struct partial_die_info
470 {
72bf9492 471 /* Offset of this DIE. */
c906108c 472 unsigned int offset;
72bf9492
DJ
473
474 /* DWARF-2 tag for this DIE. */
475 ENUM_BITFIELD(dwarf_tag) tag : 16;
476
477 /* Language code associated with this DIE. This is only used
478 for the compilation unit DIE. */
479 unsigned int language : 8;
480
481 /* Assorted flags describing the data found in this DIE. */
482 unsigned int has_children : 1;
483 unsigned int is_external : 1;
484 unsigned int is_declaration : 1;
485 unsigned int has_type : 1;
486 unsigned int has_specification : 1;
aaa75496 487 unsigned int has_stmt_list : 1;
72bf9492
DJ
488 unsigned int has_pc_info : 1;
489
490 /* Flag set if the SCOPE field of this structure has been
491 computed. */
492 unsigned int scope_set : 1;
493
fa4028e9
JB
494 /* Flag set if the DIE has a byte_size attribute. */
495 unsigned int has_byte_size : 1;
496
72bf9492
DJ
497 /* The name of this DIE. Normally the value of DW_AT_name, but
498 sometimes DW_TAG_MIPS_linkage_name or a string computed in some
499 other fashion. */
c906108c 500 char *name;
57c22c6c 501 char *dirname;
72bf9492
DJ
502
503 /* The scope to prepend to our children. This is generally
504 allocated on the comp_unit_obstack, so will disappear
505 when this compilation unit leaves the cache. */
506 char *scope;
507
508 /* The location description associated with this DIE, if any. */
509 struct dwarf_block *locdesc;
510
511 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
512 CORE_ADDR lowpc;
513 CORE_ADDR highpc;
72bf9492 514
93311388 515 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 516 DW_AT_sibling, if any. */
fe1b8b76 517 gdb_byte *sibling;
72bf9492
DJ
518
519 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
520 DW_AT_specification (or DW_AT_abstract_origin or
521 DW_AT_extension). */
522 unsigned int spec_offset;
523
aaa75496
JB
524 /* If HAS_STMT_LIST, the offset of the Line Number Information data. */
525 unsigned int line_offset;
526
72bf9492
DJ
527 /* Pointers to this DIE's parent, first child, and next sibling,
528 if any. */
529 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
530 };
531
532/* This data structure holds the information of an abbrev. */
533struct abbrev_info
534 {
535 unsigned int number; /* number identifying abbrev */
536 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
537 unsigned short has_children; /* boolean */
538 unsigned short num_attrs; /* number of attributes */
c906108c
SS
539 struct attr_abbrev *attrs; /* an array of attribute descriptions */
540 struct abbrev_info *next; /* next in chain */
541 };
542
543struct attr_abbrev
544 {
9d25dd43
DE
545 ENUM_BITFIELD(dwarf_attribute) name : 16;
546 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
547 };
548
71c25dea
TT
549/* Additional GDB-specific attribute forms. */
550enum
551 {
552 /* A string which has been updated to GDB's internal
553 representation (e.g. converted to canonical form) and does not
554 need to be updated again. */
555 GDB_FORM_cached_string = 0xff
556 };
557
b60c80d6
DJ
558/* Attributes have a name and a value */
559struct attribute
560 {
9d25dd43
DE
561 ENUM_BITFIELD(dwarf_attribute) name : 16;
562 ENUM_BITFIELD(dwarf_form) form : 16;
b60c80d6
DJ
563 union
564 {
565 char *str;
566 struct dwarf_block *blk;
567 unsigned long unsnd;
568 long int snd;
569 CORE_ADDR addr;
348e048f 570 struct signatured_type *signatured_type;
b60c80d6
DJ
571 }
572 u;
573 };
574
c906108c
SS
575/* This data structure holds a complete die structure. */
576struct die_info
577 {
76815b17
DE
578 /* DWARF-2 tag for this DIE. */
579 ENUM_BITFIELD(dwarf_tag) tag : 16;
580
581 /* Number of attributes */
582 unsigned short num_attrs;
583
584 /* Abbrev number */
585 unsigned int abbrev;
586
93311388 587 /* Offset in .debug_info or .debug_types section. */
76815b17 588 unsigned int offset;
78ba4af6
JB
589
590 /* The dies in a compilation unit form an n-ary tree. PARENT
591 points to this die's parent; CHILD points to the first child of
592 this node; and all the children of a given node are chained
593 together via their SIBLING fields, terminated by a die whose
594 tag is zero. */
639d11d3
DC
595 struct die_info *child; /* Its first child, if any. */
596 struct die_info *sibling; /* Its next sibling, if any. */
597 struct die_info *parent; /* Its parent, if any. */
c906108c 598
b60c80d6
DJ
599 /* An array of attributes, with NUM_ATTRS elements. There may be
600 zero, but it's not common and zero-sized arrays are not
601 sufficiently portable C. */
602 struct attribute attrs[1];
c906108c
SS
603 };
604
5fb290d7
DJ
605struct function_range
606{
607 const char *name;
608 CORE_ADDR lowpc, highpc;
609 int seen_line;
610 struct function_range *next;
611};
612
c906108c
SS
613/* Get at parts of an attribute structure */
614
615#define DW_STRING(attr) ((attr)->u.str)
616#define DW_UNSND(attr) ((attr)->u.unsnd)
617#define DW_BLOCK(attr) ((attr)->u.blk)
618#define DW_SND(attr) ((attr)->u.snd)
619#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 620#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
621
622/* Blocks are a bunch of untyped bytes. */
623struct dwarf_block
624 {
625 unsigned int size;
fe1b8b76 626 gdb_byte *data;
c906108c
SS
627 };
628
c906108c
SS
629#ifndef ATTR_ALLOC_CHUNK
630#define ATTR_ALLOC_CHUNK 4
631#endif
632
c906108c
SS
633/* Allocate fields for structs, unions and enums in this size. */
634#ifndef DW_FIELD_ALLOC_CHUNK
635#define DW_FIELD_ALLOC_CHUNK 4
636#endif
637
c906108c
SS
638/* A zeroed version of a partial die for initialization purposes. */
639static struct partial_die_info zeroed_partial_die;
640
c906108c
SS
641/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
642 but this would require a corresponding change in unpack_field_as_long
643 and friends. */
644static int bits_per_byte = 8;
645
646/* The routines that read and process dies for a C struct or C++ class
647 pass lists of data member fields and lists of member function fields
648 in an instance of a field_info structure, as defined below. */
649struct field_info
c5aa993b
JM
650 {
651 /* List of data member and baseclasses fields. */
652 struct nextfield
653 {
654 struct nextfield *next;
655 int accessibility;
656 int virtuality;
657 struct field field;
658 }
659 *fields;
c906108c 660
c5aa993b
JM
661 /* Number of fields. */
662 int nfields;
c906108c 663
c5aa993b
JM
664 /* Number of baseclasses. */
665 int nbaseclasses;
c906108c 666
c5aa993b
JM
667 /* Set if the accesibility of one of the fields is not public. */
668 int non_public_fields;
c906108c 669
c5aa993b
JM
670 /* Member function fields array, entries are allocated in the order they
671 are encountered in the object file. */
672 struct nextfnfield
673 {
674 struct nextfnfield *next;
675 struct fn_field fnfield;
676 }
677 *fnfields;
c906108c 678
c5aa993b
JM
679 /* Member function fieldlist array, contains name of possibly overloaded
680 member function, number of overloaded member functions and a pointer
681 to the head of the member function field chain. */
682 struct fnfieldlist
683 {
684 char *name;
685 int length;
686 struct nextfnfield *head;
687 }
688 *fnfieldlists;
c906108c 689
c5aa993b
JM
690 /* Number of entries in the fnfieldlists array. */
691 int nfnfields;
692 };
c906108c 693
10b3939b
DJ
694/* One item on the queue of compilation units to read in full symbols
695 for. */
696struct dwarf2_queue_item
697{
698 struct dwarf2_per_cu_data *per_cu;
699 struct dwarf2_queue_item *next;
700};
701
702/* The current queue. */
703static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
704
ae038cb0
DJ
705/* Loaded secondary compilation units are kept in memory until they
706 have not been referenced for the processing of this many
707 compilation units. Set this to zero to disable caching. Cache
708 sizes of up to at least twenty will improve startup time for
709 typical inter-CU-reference binaries, at an obvious memory cost. */
710static int dwarf2_max_cache_age = 5;
920d2a44
AC
711static void
712show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
713 struct cmd_list_element *c, const char *value)
714{
715 fprintf_filtered (file, _("\
716The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
717 value);
718}
719
ae038cb0 720
c906108c
SS
721/* Various complaints about symbol reading that don't abort the process */
722
4d3c2250
KB
723static void
724dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 725{
4d3c2250 726 complaint (&symfile_complaints,
e2e0b3e5 727 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
728}
729
25e43795
DJ
730static void
731dwarf2_debug_line_missing_file_complaint (void)
732{
733 complaint (&symfile_complaints,
734 _(".debug_line section has line data without a file"));
735}
736
59205f5a
JB
737static void
738dwarf2_debug_line_missing_end_sequence_complaint (void)
739{
740 complaint (&symfile_complaints,
741 _(".debug_line section has line program sequence without an end"));
742}
743
4d3c2250
KB
744static void
745dwarf2_complex_location_expr_complaint (void)
2e276125 746{
e2e0b3e5 747 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
748}
749
4d3c2250
KB
750static void
751dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
752 int arg3)
2e276125 753{
4d3c2250 754 complaint (&symfile_complaints,
e2e0b3e5 755 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
756 arg2, arg3);
757}
758
759static void
760dwarf2_macros_too_long_complaint (void)
2e276125 761{
4d3c2250 762 complaint (&symfile_complaints,
e2e0b3e5 763 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
764}
765
766static void
767dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 768{
4d3c2250 769 complaint (&symfile_complaints,
e2e0b3e5 770 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
771 arg1);
772}
773
774static void
775dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 776{
4d3c2250 777 complaint (&symfile_complaints,
e2e0b3e5 778 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 779}
c906108c 780
c906108c
SS
781/* local function prototypes */
782
4efb68b1 783static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c
SS
784
785#if 0
a14ed312 786static void dwarf2_build_psymtabs_easy (struct objfile *, int);
c906108c
SS
787#endif
788
aaa75496
JB
789static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
790 struct objfile *);
791
792static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
793 struct partial_die_info *,
794 struct partial_symtab *);
795
a14ed312 796static void dwarf2_build_psymtabs_hard (struct objfile *, int);
c906108c 797
72bf9492
DJ
798static void scan_partial_symbols (struct partial_die_info *,
799 CORE_ADDR *, CORE_ADDR *,
5734ee8b 800 int, struct dwarf2_cu *);
c906108c 801
72bf9492
DJ
802static void add_partial_symbol (struct partial_die_info *,
803 struct dwarf2_cu *);
63d06c5c 804
72bf9492 805static int pdi_needs_namespace (enum dwarf_tag tag);
91c24f0a 806
72bf9492
DJ
807static void add_partial_namespace (struct partial_die_info *pdi,
808 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 809 int need_pc, struct dwarf2_cu *cu);
63d06c5c 810
5d7cb8df
JK
811static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
812 CORE_ADDR *highpc, int need_pc,
813 struct dwarf2_cu *cu);
814
72bf9492
DJ
815static void add_partial_enumeration (struct partial_die_info *enum_pdi,
816 struct dwarf2_cu *cu);
91c24f0a 817
bc30ff58
JB
818static void add_partial_subprogram (struct partial_die_info *pdi,
819 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 820 int need_pc, struct dwarf2_cu *cu);
bc30ff58 821
fe1b8b76 822static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
823 gdb_byte *buffer, gdb_byte *info_ptr,
824 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 825
a14ed312 826static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 827
a14ed312 828static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 829
e7c27a73 830static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 831
f3dd6933 832static void dwarf2_free_abbrev_table (void *);
c906108c 833
fe1b8b76 834static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 835 struct dwarf2_cu *);
72bf9492 836
57349743 837static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 838 struct dwarf2_cu *);
c906108c 839
93311388
DE
840static struct partial_die_info *load_partial_dies (bfd *,
841 gdb_byte *, gdb_byte *,
842 int, struct dwarf2_cu *);
72bf9492 843
fe1b8b76 844static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
845 struct abbrev_info *abbrev,
846 unsigned int, bfd *,
847 gdb_byte *, gdb_byte *,
848 struct dwarf2_cu *);
c906108c 849
c764a876 850static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 851 struct dwarf2_cu *);
72bf9492
DJ
852
853static void fixup_partial_die (struct partial_die_info *,
854 struct dwarf2_cu *);
855
fe1b8b76
JB
856static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
857 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 858
fe1b8b76
JB
859static gdb_byte *read_attribute_value (struct attribute *, unsigned,
860 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 861
fe1b8b76 862static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 863
fe1b8b76 864static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 865
fe1b8b76 866static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 867
fe1b8b76 868static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 869
93311388 870static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 871
fe1b8b76 872static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 873 unsigned int *);
c906108c 874
c764a876
DE
875static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
876
877static LONGEST read_checked_initial_length_and_offset
878 (bfd *, gdb_byte *, const struct comp_unit_head *,
879 unsigned int *, unsigned int *);
613e1657 880
fe1b8b76 881static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
882 unsigned int *);
883
884static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 885
fe1b8b76 886static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 887
fe1b8b76 888static char *read_string (bfd *, gdb_byte *, unsigned int *);
c906108c 889
fe1b8b76
JB
890static char *read_indirect_string (bfd *, gdb_byte *,
891 const struct comp_unit_head *,
892 unsigned int *);
4bdf3d34 893
fe1b8b76 894static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 895
fe1b8b76 896static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 897
fe1b8b76 898static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 899
e142c38c 900static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 901
e142c38c
DJ
902static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
903 struct dwarf2_cu *);
c906108c 904
348e048f
DE
905static struct attribute *dwarf2_attr_no_follow (struct die_info *,
906 unsigned int,
907 struct dwarf2_cu *);
908
05cf31d1
JB
909static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
910 struct dwarf2_cu *cu);
911
e142c38c 912static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 913
e142c38c 914static struct die_info *die_specification (struct die_info *die,
f2f0e013 915 struct dwarf2_cu **);
63d06c5c 916
debd256d
JB
917static void free_line_header (struct line_header *lh);
918
aaa75496
JB
919static void add_file_name (struct line_header *, char *, unsigned int,
920 unsigned int, unsigned int);
921
debd256d
JB
922static struct line_header *(dwarf_decode_line_header
923 (unsigned int offset,
e7c27a73 924 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
925
926static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 927 struct dwarf2_cu *, struct partial_symtab *);
c906108c 928
4f1520fb 929static void dwarf2_start_subfile (char *, char *, char *);
c906108c 930
a14ed312 931static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 932 struct dwarf2_cu *);
c906108c 933
a14ed312 934static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 935 struct dwarf2_cu *);
c906108c 936
2df3850c
JM
937static void dwarf2_const_value_data (struct attribute *attr,
938 struct symbol *sym,
939 int bits);
940
e7c27a73 941static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 942
e7c27a73
DJ
943static struct type *die_containing_type (struct die_info *,
944 struct dwarf2_cu *);
c906108c 945
e7c27a73 946static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
c906108c 947
f792889a 948static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 949
086ed43d 950static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 951
fe1b8b76
JB
952static char *typename_concat (struct obstack *,
953 const char *prefix,
954 const char *suffix,
987504bb 955 struct dwarf2_cu *);
63d06c5c 956
e7c27a73 957static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 958
348e048f
DE
959static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
960
e7c27a73 961static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 962
e7c27a73 963static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 964
ff013f42
JK
965static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
966 struct dwarf2_cu *, struct partial_symtab *);
967
a14ed312 968static int dwarf2_get_pc_bounds (struct die_info *,
e7c27a73 969 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *);
c906108c 970
fae299cd
DC
971static void get_scope_pc_bounds (struct die_info *,
972 CORE_ADDR *, CORE_ADDR *,
973 struct dwarf2_cu *);
974
801e3a5b
JB
975static void dwarf2_record_block_ranges (struct die_info *, struct block *,
976 CORE_ADDR, struct dwarf2_cu *);
977
a14ed312 978static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 979 struct dwarf2_cu *);
c906108c 980
a14ed312 981static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 982 struct type *, struct dwarf2_cu *);
c906108c 983
a14ed312 984static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 985 struct die_info *, struct type *,
e7c27a73 986 struct dwarf2_cu *);
c906108c 987
a14ed312 988static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 989 struct type *, struct dwarf2_cu *);
c906108c 990
134d01f1 991static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 992
0114d602
DJ
993static const char *determine_class_name (struct die_info *die,
994 struct dwarf2_cu *cu);
8176b9b8 995
e7c27a73 996static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 997
e7c27a73 998static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 999
5d7cb8df
JK
1000static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1001
27aa8d6a
SW
1002static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1003
38d518c9 1004static const char *namespace_name (struct die_info *die,
e142c38c 1005 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1006
134d01f1 1007static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1008
e7c27a73 1009static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1010
7ca2d3a3
DL
1011static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1012 struct dwarf2_cu *);
1013
93311388 1014static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1015
93311388
DE
1016static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1017 gdb_byte *info_ptr,
d97bc12b
DE
1018 gdb_byte **new_info_ptr,
1019 struct die_info *parent);
1020
93311388
DE
1021static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1022 gdb_byte *info_ptr,
fe1b8b76 1023 gdb_byte **new_info_ptr,
639d11d3
DC
1024 struct die_info *parent);
1025
93311388
DE
1026static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1027 gdb_byte *info_ptr,
fe1b8b76 1028 gdb_byte **new_info_ptr,
639d11d3
DC
1029 struct die_info *parent);
1030
93311388
DE
1031static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1032 struct die_info **, gdb_byte *,
1033 int *);
1034
e7c27a73 1035static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1036
e142c38c 1037static char *dwarf2_linkage_name (struct die_info *, struct dwarf2_cu *);
c906108c 1038
71c25dea
TT
1039static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1040 struct obstack *);
1041
e142c38c 1042static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1043
e142c38c 1044static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1045 struct dwarf2_cu **);
9219021c 1046
a14ed312 1047static char *dwarf_tag_name (unsigned int);
c906108c 1048
a14ed312 1049static char *dwarf_attr_name (unsigned int);
c906108c 1050
a14ed312 1051static char *dwarf_form_name (unsigned int);
c906108c 1052
a14ed312 1053static char *dwarf_stack_op_name (unsigned int);
c906108c 1054
a14ed312 1055static char *dwarf_bool_name (unsigned int);
c906108c 1056
a14ed312 1057static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1058
1059#if 0
a14ed312 1060static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1061#endif
1062
f9aca02d 1063static struct die_info *sibling_die (struct die_info *);
c906108c 1064
d97bc12b
DE
1065static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1066
1067static void dump_die_for_error (struct die_info *);
1068
1069static void dump_die_1 (struct ui_file *, int level, int max_level,
1070 struct die_info *);
c906108c 1071
d97bc12b 1072/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1073
51545339 1074static void store_in_ref_table (struct die_info *,
10b3939b 1075 struct dwarf2_cu *);
c906108c 1076
93311388
DE
1077static int is_ref_attr (struct attribute *);
1078
c764a876 1079static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1080
a02abb62
JB
1081static int dwarf2_get_attr_constant_value (struct attribute *, int);
1082
348e048f
DE
1083static struct die_info *follow_die_ref_or_sig (struct die_info *,
1084 struct attribute *,
1085 struct dwarf2_cu **);
1086
10b3939b
DJ
1087static struct die_info *follow_die_ref (struct die_info *,
1088 struct attribute *,
f2f0e013 1089 struct dwarf2_cu **);
c906108c 1090
348e048f
DE
1091static struct die_info *follow_die_sig (struct die_info *,
1092 struct attribute *,
1093 struct dwarf2_cu **);
1094
1095static void read_signatured_type_at_offset (struct objfile *objfile,
1096 unsigned int offset);
1097
1098static void read_signatured_type (struct objfile *,
1099 struct signatured_type *type_sig);
1100
c906108c
SS
1101/* memory allocation interface */
1102
7b5a2f43 1103static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1104
f3dd6933 1105static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1106
b60c80d6 1107static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1108
e142c38c 1109static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1110
e142c38c
DJ
1111static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1112 struct dwarf2_cu *);
5fb290d7 1113
2e276125 1114static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1115 char *, bfd *, struct dwarf2_cu *);
2e276125 1116
8e19ed76
PS
1117static int attr_form_is_block (struct attribute *);
1118
3690dd37
JB
1119static int attr_form_is_section_offset (struct attribute *);
1120
1121static int attr_form_is_constant (struct attribute *);
1122
93e7bd98
DJ
1123static void dwarf2_symbol_mark_computed (struct attribute *attr,
1124 struct symbol *sym,
1125 struct dwarf2_cu *cu);
4c2df51b 1126
93311388
DE
1127static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1128 struct abbrev_info *abbrev,
1129 struct dwarf2_cu *cu);
4bb7a0a7 1130
72bf9492
DJ
1131static void free_stack_comp_unit (void *);
1132
72bf9492
DJ
1133static hashval_t partial_die_hash (const void *item);
1134
1135static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1136
ae038cb0 1137static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1138 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1139
1140static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1141 (unsigned int offset, struct objfile *objfile);
ae038cb0 1142
93311388
DE
1143static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1144
ae038cb0
DJ
1145static void free_one_comp_unit (void *);
1146
1147static void free_cached_comp_units (void *);
1148
1149static void age_cached_comp_units (void);
1150
1151static void free_one_cached_comp_unit (void *);
1152
f792889a
DJ
1153static struct type *set_die_type (struct die_info *, struct type *,
1154 struct dwarf2_cu *);
1c379e20 1155
ae038cb0
DJ
1156static void create_all_comp_units (struct objfile *);
1157
93311388
DE
1158static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1159 struct objfile *);
10b3939b
DJ
1160
1161static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1162
1163static void dwarf2_add_dependence (struct dwarf2_cu *,
1164 struct dwarf2_per_cu_data *);
1165
ae038cb0
DJ
1166static void dwarf2_mark (struct dwarf2_cu *);
1167
1168static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1169
f792889a 1170static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1171
c906108c
SS
1172/* Try to locate the sections we need for DWARF 2 debugging
1173 information and return true if we have enough to do something. */
1174
1175int
6502dd73 1176dwarf2_has_info (struct objfile *objfile)
c906108c 1177{
6502dd73
DJ
1178 struct dwarf2_per_objfile *data;
1179
1180 /* Initialize per-objfile state. */
1181 data = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1182 memset (data, 0, sizeof (*data));
1183 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1184 dwarf2_per_objfile = data;
1185
6502dd73 1186 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
dce234bc 1187 return (data->info.asection != NULL && data->abbrev.asection != NULL);
c906108c
SS
1188}
1189
233a11ab
CS
1190/* When loading sections, we can either look for ".<name>", or for
1191 * ".z<name>", which indicates a compressed section. */
1192
1193static int
dce234bc 1194section_is_p (const char *section_name, const char *name)
233a11ab 1195{
dce234bc
PP
1196 return (section_name[0] == '.'
1197 && (strcmp (section_name + 1, name) == 0
1198 || (section_name[1] == 'z'
1199 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1200}
1201
c906108c
SS
1202/* This function is mapped across the sections and remembers the
1203 offset and size of each of the debugging sections we are interested
1204 in. */
1205
1206static void
72dca2f5 1207dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1208{
dce234bc 1209 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1210 {
dce234bc
PP
1211 dwarf2_per_objfile->info.asection = sectp;
1212 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1213 }
dce234bc 1214 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1215 {
dce234bc
PP
1216 dwarf2_per_objfile->abbrev.asection = sectp;
1217 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1218 }
dce234bc 1219 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1220 {
dce234bc
PP
1221 dwarf2_per_objfile->line.asection = sectp;
1222 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1223 }
dce234bc 1224 else if (section_is_p (sectp->name, PUBNAMES_SECTION))
c906108c 1225 {
dce234bc
PP
1226 dwarf2_per_objfile->pubnames.asection = sectp;
1227 dwarf2_per_objfile->pubnames.size = bfd_get_section_size (sectp);
c906108c 1228 }
dce234bc 1229 else if (section_is_p (sectp->name, ARANGES_SECTION))
c906108c 1230 {
dce234bc
PP
1231 dwarf2_per_objfile->aranges.asection = sectp;
1232 dwarf2_per_objfile->aranges.size = bfd_get_section_size (sectp);
c906108c 1233 }
dce234bc 1234 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1235 {
dce234bc
PP
1236 dwarf2_per_objfile->loc.asection = sectp;
1237 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1238 }
dce234bc 1239 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1240 {
dce234bc
PP
1241 dwarf2_per_objfile->macinfo.asection = sectp;
1242 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1243 }
dce234bc 1244 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1245 {
dce234bc
PP
1246 dwarf2_per_objfile->str.asection = sectp;
1247 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1248 }
dce234bc 1249 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1250 {
dce234bc
PP
1251 dwarf2_per_objfile->frame.asection = sectp;
1252 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1253 }
dce234bc 1254 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1255 {
3799ccc6
EZ
1256 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1257 if (aflag & SEC_HAS_CONTENTS)
1258 {
dce234bc
PP
1259 dwarf2_per_objfile->eh_frame.asection = sectp;
1260 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1261 }
b6af0555 1262 }
dce234bc 1263 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1264 {
dce234bc
PP
1265 dwarf2_per_objfile->ranges.asection = sectp;
1266 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1267 }
348e048f
DE
1268 else if (section_is_p (sectp->name, TYPES_SECTION))
1269 {
1270 dwarf2_per_objfile->types.asection = sectp;
1271 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1272 }
dce234bc 1273
72dca2f5
FR
1274 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1275 && bfd_section_vma (abfd, sectp) == 0)
1276 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1277}
1278
dce234bc
PP
1279/* Decompress a section that was compressed using zlib. Store the
1280 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1281
1282static void
dce234bc
PP
1283zlib_decompress_section (struct objfile *objfile, asection *sectp,
1284 gdb_byte **outbuf, bfd_size_type *outsize)
1285{
1286 bfd *abfd = objfile->obfd;
1287#ifndef HAVE_ZLIB_H
1288 error (_("Support for zlib-compressed DWARF data (from '%s') "
1289 "is disabled in this copy of GDB"),
1290 bfd_get_filename (abfd));
1291#else
1292 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1293 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1294 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1295 bfd_size_type uncompressed_size;
1296 gdb_byte *uncompressed_buffer;
1297 z_stream strm;
1298 int rc;
1299 int header_size = 12;
1300
1301 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1302 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1303 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1304 bfd_get_filename (abfd));
1305
1306 /* Read the zlib header. In this case, it should be "ZLIB" followed
1307 by the uncompressed section size, 8 bytes in big-endian order. */
1308 if (compressed_size < header_size
1309 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1310 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1311 bfd_get_filename (abfd));
1312 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1313 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1314 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1315 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1316 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1317 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1318 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1319 uncompressed_size += compressed_buffer[11];
1320
1321 /* It is possible the section consists of several compressed
1322 buffers concatenated together, so we uncompress in a loop. */
1323 strm.zalloc = NULL;
1324 strm.zfree = NULL;
1325 strm.opaque = NULL;
1326 strm.avail_in = compressed_size - header_size;
1327 strm.next_in = (Bytef*) compressed_buffer + header_size;
1328 strm.avail_out = uncompressed_size;
1329 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1330 uncompressed_size);
1331 rc = inflateInit (&strm);
1332 while (strm.avail_in > 0)
1333 {
1334 if (rc != Z_OK)
1335 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1336 bfd_get_filename (abfd), rc);
1337 strm.next_out = ((Bytef*) uncompressed_buffer
1338 + (uncompressed_size - strm.avail_out));
1339 rc = inflate (&strm, Z_FINISH);
1340 if (rc != Z_STREAM_END)
1341 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1342 bfd_get_filename (abfd), rc);
1343 rc = inflateReset (&strm);
1344 }
1345 rc = inflateEnd (&strm);
1346 if (rc != Z_OK
1347 || strm.avail_out != 0)
1348 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1349 bfd_get_filename (abfd), rc);
1350
affddf13 1351 do_cleanups (cleanup);
dce234bc
PP
1352 *outbuf = uncompressed_buffer;
1353 *outsize = uncompressed_size;
1354#endif
233a11ab
CS
1355}
1356
dce234bc
PP
1357/* Read the contents of the section SECTP from object file specified by
1358 OBJFILE, store info about the section into INFO.
1359 If the section is compressed, uncompress it before returning. */
c906108c 1360
dce234bc
PP
1361static void
1362dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1363{
dce234bc
PP
1364 bfd *abfd = objfile->obfd;
1365 asection *sectp = info->asection;
1366 gdb_byte *buf, *retbuf;
1367 unsigned char header[4];
c906108c 1368
dce234bc
PP
1369 info->buffer = NULL;
1370 info->was_mmapped = 0;
188dd5d6 1371
dce234bc
PP
1372 if (info->asection == NULL || info->size == 0)
1373 return;
c906108c 1374
dce234bc
PP
1375 /* Check if the file has a 4-byte header indicating compression. */
1376 if (info->size > sizeof (header)
1377 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1378 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1379 {
1380 /* Upon decompression, update the buffer and its size. */
1381 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1382 {
1383 zlib_decompress_section (objfile, sectp, &info->buffer,
1384 &info->size);
1385 return;
1386 }
1387 }
4bdf3d34 1388
dce234bc
PP
1389#ifdef HAVE_MMAP
1390 if (pagesize == 0)
1391 pagesize = getpagesize ();
2e276125 1392
dce234bc
PP
1393 /* Only try to mmap sections which are large enough: we don't want to
1394 waste space due to fragmentation. Also, only try mmap for sections
1395 without relocations. */
1396
1397 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1398 {
1399 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1400 size_t map_length = info->size + sectp->filepos - pg_offset;
1401 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1402 MAP_PRIVATE, pg_offset);
1403
1404 if (retbuf != MAP_FAILED)
1405 {
1406 info->was_mmapped = 1;
1407 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1408 return;
1409 }
1410 }
1411#endif
1412
1413 /* If we get here, we are a normal, not-compressed section. */
1414 info->buffer = buf
1415 = obstack_alloc (&objfile->objfile_obstack, info->size);
1416
1417 /* When debugging .o files, we may need to apply relocations; see
1418 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1419 We never compress sections in .o files, so we only need to
1420 try this when the section is not compressed. */
1421 retbuf = symfile_relocate_debug_section (abfd, sectp, buf);
1422 if (retbuf != NULL)
1423 {
1424 info->buffer = retbuf;
1425 return;
1426 }
1427
1428 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1429 || bfd_bread (buf, info->size, abfd) != info->size)
1430 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1431 bfd_get_filename (abfd));
1432}
1433
1434/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1435 SECTION_NAME. */
af34e669 1436
dce234bc
PP
1437void
1438dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1439 asection **sectp, gdb_byte **bufp,
1440 bfd_size_type *sizep)
1441{
1442 struct dwarf2_per_objfile *data
1443 = objfile_data (objfile, dwarf2_objfile_data_key);
1444 struct dwarf2_section_info *info;
1445 if (section_is_p (section_name, EH_FRAME_SECTION))
1446 info = &data->eh_frame;
1447 else if (section_is_p (section_name, FRAME_SECTION))
1448 info = &data->frame;
0d53c4c4 1449 else
dce234bc
PP
1450 gdb_assert (0);
1451
1452 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1453 /* We haven't read this section in yet. Do it now. */
1454 dwarf2_read_section (objfile, info);
1455
1456 *sectp = info->asection;
1457 *bufp = info->buffer;
1458 *sizep = info->size;
1459}
1460
1461/* Build a partial symbol table. */
1462
1463void
1464dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
1465{
1466 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1467 dwarf2_read_section (objfile, &dwarf2_per_objfile->abbrev);
1468 dwarf2_read_section (objfile, &dwarf2_per_objfile->line);
1469 dwarf2_read_section (objfile, &dwarf2_per_objfile->str);
1470 dwarf2_read_section (objfile, &dwarf2_per_objfile->macinfo);
1471 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
348e048f 1472 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
dce234bc
PP
1473 dwarf2_read_section (objfile, &dwarf2_per_objfile->loc);
1474 dwarf2_read_section (objfile, &dwarf2_per_objfile->eh_frame);
1475 dwarf2_read_section (objfile, &dwarf2_per_objfile->frame);
0d53c4c4 1476
ef96bde8
EZ
1477 if (mainline
1478 || (objfile->global_psymbols.size == 0
1479 && objfile->static_psymbols.size == 0))
c906108c
SS
1480 {
1481 init_psymbol_list (objfile, 1024);
1482 }
1483
1484#if 0
1485 if (dwarf_aranges_offset && dwarf_pubnames_offset)
1486 {
d4f3574e 1487 /* Things are significantly easier if we have .debug_aranges and
c906108c
SS
1488 .debug_pubnames sections */
1489
d4f3574e 1490 dwarf2_build_psymtabs_easy (objfile, mainline);
c906108c
SS
1491 }
1492 else
1493#endif
1494 /* only test this case for now */
c5aa993b 1495 {
c906108c 1496 /* In this case we have to work a bit harder */
d4f3574e 1497 dwarf2_build_psymtabs_hard (objfile, mainline);
c906108c
SS
1498 }
1499}
1500
1501#if 0
1502/* Build the partial symbol table from the information in the
1503 .debug_pubnames and .debug_aranges sections. */
1504
1505static void
fba45db2 1506dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
c906108c
SS
1507{
1508 bfd *abfd = objfile->obfd;
1509 char *aranges_buffer, *pubnames_buffer;
1510 char *aranges_ptr, *pubnames_ptr;
1511 unsigned int entry_length, version, info_offset, info_size;
1512
1513 pubnames_buffer = dwarf2_read_section (objfile,
086df311 1514 dwarf_pubnames_section);
c906108c 1515 pubnames_ptr = pubnames_buffer;
dce234bc 1516 while ((pubnames_ptr - pubnames_buffer) < dwarf2_per_objfile->pubnames.size)
c906108c 1517 {
891d2f0b 1518 unsigned int bytes_read;
613e1657 1519
c764a876 1520 entry_length = read_initial_length (abfd, pubnames_ptr, &bytes_read);
613e1657 1521 pubnames_ptr += bytes_read;
c906108c
SS
1522 version = read_1_byte (abfd, pubnames_ptr);
1523 pubnames_ptr += 1;
1524 info_offset = read_4_bytes (abfd, pubnames_ptr);
1525 pubnames_ptr += 4;
1526 info_size = read_4_bytes (abfd, pubnames_ptr);
1527 pubnames_ptr += 4;
1528 }
1529
1530 aranges_buffer = dwarf2_read_section (objfile,
086df311 1531 dwarf_aranges_section);
c906108c
SS
1532
1533}
1534#endif
1535
45452591
DE
1536/* Return TRUE if OFFSET is within CU_HEADER. */
1537
1538static inline int
1539offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
1540{
1541 unsigned int bottom = cu_header->offset;
1542 unsigned int top = (cu_header->offset
1543 + cu_header->length
1544 + cu_header->initial_length_size);
1545 return (offset >= bottom && offset < top);
1546}
1547
93311388
DE
1548/* Read in the comp unit header information from the debug_info at info_ptr.
1549 NOTE: This leaves members offset, first_die_offset to be filled in
1550 by the caller. */
107d2387 1551
fe1b8b76 1552static gdb_byte *
107d2387 1553read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 1554 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
1555{
1556 int signed_addr;
891d2f0b 1557 unsigned int bytes_read;
c764a876
DE
1558
1559 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
1560 cu_header->initial_length_size = bytes_read;
1561 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 1562 info_ptr += bytes_read;
107d2387
AC
1563 cu_header->version = read_2_bytes (abfd, info_ptr);
1564 info_ptr += 2;
613e1657 1565 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 1566 &bytes_read);
613e1657 1567 info_ptr += bytes_read;
107d2387
AC
1568 cu_header->addr_size = read_1_byte (abfd, info_ptr);
1569 info_ptr += 1;
1570 signed_addr = bfd_get_sign_extend_vma (abfd);
1571 if (signed_addr < 0)
8e65ff28 1572 internal_error (__FILE__, __LINE__,
e2e0b3e5 1573 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 1574 cu_header->signed_addr_p = signed_addr;
c764a876 1575
107d2387
AC
1576 return info_ptr;
1577}
1578
fe1b8b76
JB
1579static gdb_byte *
1580partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 1581 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
1582 bfd *abfd)
1583{
fe1b8b76 1584 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
1585
1586 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
1587
2b949cb6 1588 if (header->version != 2 && header->version != 3)
8a3fe4f8
AC
1589 error (_("Dwarf Error: wrong version in compilation unit header "
1590 "(is %d, should be %d) [in module %s]"), header->version,
72bf9492
DJ
1591 2, bfd_get_filename (abfd));
1592
dce234bc 1593 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
1594 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
1595 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 1596 (long) header->abbrev_offset,
93311388 1597 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
1598 bfd_get_filename (abfd));
1599
1600 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 1601 > buffer + buffer_size)
8a3fe4f8
AC
1602 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
1603 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 1604 (long) header->length,
93311388 1605 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
1606 bfd_get_filename (abfd));
1607
1608 return info_ptr;
1609}
1610
348e048f
DE
1611/* Read in the types comp unit header information from .debug_types entry at
1612 types_ptr. The result is a pointer to one past the end of the header. */
1613
1614static gdb_byte *
1615read_type_comp_unit_head (struct comp_unit_head *cu_header,
1616 ULONGEST *signature,
1617 gdb_byte *types_ptr, bfd *abfd)
1618{
1619 unsigned int bytes_read;
1620 gdb_byte *initial_types_ptr = types_ptr;
1621
1622 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
1623
1624 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
1625
1626 *signature = read_8_bytes (abfd, types_ptr);
1627 types_ptr += 8;
1628 types_ptr += cu_header->offset_size;
1629 cu_header->first_die_offset = types_ptr - initial_types_ptr;
1630
1631 return types_ptr;
1632}
1633
aaa75496
JB
1634/* Allocate a new partial symtab for file named NAME and mark this new
1635 partial symtab as being an include of PST. */
1636
1637static void
1638dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
1639 struct objfile *objfile)
1640{
1641 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
1642
1643 subpst->section_offsets = pst->section_offsets;
1644 subpst->textlow = 0;
1645 subpst->texthigh = 0;
1646
1647 subpst->dependencies = (struct partial_symtab **)
1648 obstack_alloc (&objfile->objfile_obstack,
1649 sizeof (struct partial_symtab *));
1650 subpst->dependencies[0] = pst;
1651 subpst->number_of_dependencies = 1;
1652
1653 subpst->globals_offset = 0;
1654 subpst->n_global_syms = 0;
1655 subpst->statics_offset = 0;
1656 subpst->n_static_syms = 0;
1657 subpst->symtab = NULL;
1658 subpst->read_symtab = pst->read_symtab;
1659 subpst->readin = 0;
1660
1661 /* No private part is necessary for include psymtabs. This property
1662 can be used to differentiate between such include psymtabs and
10b3939b 1663 the regular ones. */
58a9656e 1664 subpst->read_symtab_private = NULL;
aaa75496
JB
1665}
1666
1667/* Read the Line Number Program data and extract the list of files
1668 included by the source file represented by PST. Build an include
1669 partial symtab for each of these included files.
1670
1671 This procedure assumes that there *is* a Line Number Program in
1672 the given CU. Callers should check that PDI->HAS_STMT_LIST is set
1673 before calling this procedure. */
1674
1675static void
1676dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
1677 struct partial_die_info *pdi,
1678 struct partial_symtab *pst)
1679{
1680 struct objfile *objfile = cu->objfile;
1681 bfd *abfd = objfile->obfd;
1682 struct line_header *lh;
1683
1684 lh = dwarf_decode_line_header (pdi->line_offset, abfd, cu);
1685 if (lh == NULL)
1686 return; /* No linetable, so no includes. */
1687
1688 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
1689
1690 free_line_header (lh);
1691}
1692
348e048f
DE
1693static hashval_t
1694hash_type_signature (const void *item)
1695{
1696 const struct signatured_type *type_sig = item;
1697 /* This drops the top 32 bits of the signature, but is ok for a hash. */
1698 return type_sig->signature;
1699}
1700
1701static int
1702eq_type_signature (const void *item_lhs, const void *item_rhs)
1703{
1704 const struct signatured_type *lhs = item_lhs;
1705 const struct signatured_type *rhs = item_rhs;
1706 return lhs->signature == rhs->signature;
1707}
1708
1709/* Create the hash table of all entries in the .debug_types section.
1710 The result is zero if there is an error (e.g. missing .debug_types section),
1711 otherwise non-zero. */
1712
1713static int
1714create_debug_types_hash_table (struct objfile *objfile)
1715{
1716 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer;
1717 htab_t types_htab;
1718
1719 if (info_ptr == NULL)
1720 {
1721 dwarf2_per_objfile->signatured_types = NULL;
1722 return 0;
1723 }
1724
1725 types_htab = htab_create_alloc_ex (41,
1726 hash_type_signature,
1727 eq_type_signature,
1728 NULL,
1729 &objfile->objfile_obstack,
1730 hashtab_obstack_allocate,
1731 dummy_obstack_deallocate);
1732
1733 if (dwarf2_die_debug)
1734 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
1735
1736 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
1737 {
1738 unsigned int offset;
1739 unsigned int offset_size;
1740 unsigned int type_offset;
1741 unsigned int length, initial_length_size;
1742 unsigned short version;
1743 ULONGEST signature;
1744 struct signatured_type *type_sig;
1745 void **slot;
1746 gdb_byte *ptr = info_ptr;
1747
1748 offset = ptr - dwarf2_per_objfile->types.buffer;
1749
1750 /* We need to read the type's signature in order to build the hash
1751 table, but we don't need to read anything else just yet. */
1752
1753 /* Sanity check to ensure entire cu is present. */
1754 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
1755 if (ptr + length + initial_length_size
1756 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
1757 {
1758 complaint (&symfile_complaints,
1759 _("debug type entry runs off end of `.debug_types' section, ignored"));
1760 break;
1761 }
1762
1763 offset_size = initial_length_size == 4 ? 4 : 8;
1764 ptr += initial_length_size;
1765 version = bfd_get_16 (objfile->obfd, ptr);
1766 ptr += 2;
1767 ptr += offset_size; /* abbrev offset */
1768 ptr += 1; /* address size */
1769 signature = bfd_get_64 (objfile->obfd, ptr);
1770 ptr += 8;
1771 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
1772
1773 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
1774 memset (type_sig, 0, sizeof (*type_sig));
1775 type_sig->signature = signature;
1776 type_sig->offset = offset;
1777 type_sig->type_offset = type_offset;
1778
1779 slot = htab_find_slot (types_htab, type_sig, INSERT);
1780 gdb_assert (slot != NULL);
1781 *slot = type_sig;
1782
1783 if (dwarf2_die_debug)
1784 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
1785 offset, phex (signature, sizeof (signature)));
1786
1787 info_ptr = info_ptr + initial_length_size + length;
1788 }
1789
1790 dwarf2_per_objfile->signatured_types = types_htab;
1791
1792 return 1;
1793}
1794
1795/* Lookup a signature based type.
1796 Returns NULL if SIG is not present in the table. */
1797
1798static struct signatured_type *
1799lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
1800{
1801 struct signatured_type find_entry, *entry;
1802
1803 if (dwarf2_per_objfile->signatured_types == NULL)
1804 {
1805 complaint (&symfile_complaints,
1806 _("missing `.debug_types' section for DW_FORM_sig8 die"));
1807 return 0;
1808 }
1809
1810 find_entry.signature = sig;
1811 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
1812 return entry;
1813}
1814
1815/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
1816 to combine the common parts.
93311388 1817 Process a compilation unit for a psymtab.
348e048f
DE
1818 BUFFER is a pointer to the beginning of the dwarf section buffer,
1819 either .debug_info or debug_types.
93311388
DE
1820 INFO_PTR is a pointer to the start of the CU.
1821 Returns a pointer to the next CU. */
aaa75496 1822
93311388
DE
1823static gdb_byte *
1824process_psymtab_comp_unit (struct objfile *objfile,
1825 struct dwarf2_per_cu_data *this_cu,
1826 gdb_byte *buffer, gdb_byte *info_ptr,
1827 unsigned int buffer_size)
c906108c 1828{
c906108c 1829 bfd *abfd = objfile->obfd;
93311388 1830 gdb_byte *beg_of_comp_unit = info_ptr;
c906108c
SS
1831 struct partial_die_info comp_unit_die;
1832 struct partial_symtab *pst;
5734ee8b 1833 CORE_ADDR baseaddr;
93311388
DE
1834 struct cleanup *back_to_inner;
1835 struct dwarf2_cu cu;
1836 struct abbrev_info *abbrev;
1837 unsigned int bytes_read;
c906108c 1838
93311388
DE
1839 memset (&cu, 0, sizeof (cu));
1840 cu.objfile = objfile;
1841 obstack_init (&cu.comp_unit_obstack);
c906108c 1842
93311388 1843 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 1844
93311388
DE
1845 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1846 buffer, buffer_size,
1847 abfd);
10b3939b 1848
93311388
DE
1849 /* Complete the cu_header. */
1850 cu.header.offset = beg_of_comp_unit - buffer;
1851 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 1852
93311388 1853 cu.list_in_scope = &file_symbols;
af703f96 1854
93311388
DE
1855 /* Read the abbrevs for this compilation unit into a table. */
1856 dwarf2_read_abbrevs (abfd, &cu);
1857 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 1858
93311388 1859 /* Read the compilation unit die. */
348e048f
DE
1860 if (this_cu->from_debug_types)
1861 info_ptr += 8 /*signature*/ + cu.header.offset_size;
93311388
DE
1862 abbrev = peek_die_abbrev (info_ptr, &bytes_read, &cu);
1863 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read, abfd,
1864 buffer, info_ptr, &cu);
1865
348e048f
DE
1866 if (this_cu->from_debug_types)
1867 {
1868 /* offset,length haven't been set yet for type units. */
1869 this_cu->offset = cu.header.offset;
1870 this_cu->length = cu.header.length + cu.header.initial_length_size;
1871 }
1872 else if (comp_unit_die.tag == DW_TAG_partial_unit)
c906108c 1873 {
93311388
DE
1874 info_ptr = (beg_of_comp_unit + cu.header.length
1875 + cu.header.initial_length_size);
1876 do_cleanups (back_to_inner);
1877 return info_ptr;
1878 }
72bf9492 1879
93311388
DE
1880 /* Set the language we're debugging. */
1881 set_cu_language (comp_unit_die.language, &cu);
c906108c 1882
93311388
DE
1883 /* Allocate a new partial symbol table structure. */
1884 pst = start_psymtab_common (objfile, objfile->section_offsets,
1885 comp_unit_die.name ? comp_unit_die.name : "",
1886 /* TEXTLOW and TEXTHIGH are set below. */
1887 0,
1888 objfile->global_psymbols.next,
1889 objfile->static_psymbols.next);
72bf9492 1890
93311388
DE
1891 if (comp_unit_die.dirname)
1892 pst->dirname = obsavestring (comp_unit_die.dirname,
1893 strlen (comp_unit_die.dirname),
1894 &objfile->objfile_obstack);
72bf9492 1895
93311388 1896 pst->read_symtab_private = (char *) this_cu;
72bf9492 1897
93311388 1898 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 1899
93311388
DE
1900 /* Store the function that reads in the rest of the symbol table */
1901 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 1902
93311388
DE
1903 /* If this compilation unit was already read in, free the
1904 cached copy in order to read it in again. This is
1905 necessary because we skipped some symbols when we first
1906 read in the compilation unit (see load_partial_dies).
1907 This problem could be avoided, but the benefit is
1908 unclear. */
1909 if (this_cu->cu != NULL)
1910 free_one_cached_comp_unit (this_cu->cu);
e142c38c 1911
93311388 1912 cu.per_cu = this_cu;
c906108c 1913
93311388
DE
1914 /* Note that this is a pointer to our stack frame, being
1915 added to a global data structure. It will be cleaned up
1916 in free_stack_comp_unit when we finish with this
1917 compilation unit. */
1918 this_cu->cu = &cu;
ae038cb0 1919
93311388 1920 this_cu->psymtab = pst;
c906108c 1921
93311388
DE
1922 /* Possibly set the default values of LOWPC and HIGHPC from
1923 `DW_AT_ranges'. */
1924 if (cu.has_ranges_offset)
1925 {
1926 if (dwarf2_ranges_read (cu.ranges_offset, &comp_unit_die.lowpc,
1927 &comp_unit_die.highpc, &cu, pst))
1928 comp_unit_die.has_pc_info = 1;
1929 }
1930 else if (comp_unit_die.has_pc_info
1931 && comp_unit_die.lowpc < comp_unit_die.highpc)
1932 /* Store the contiguous range if it is not empty; it can be empty for
1933 CUs with no code. */
1934 addrmap_set_empty (objfile->psymtabs_addrmap,
1935 comp_unit_die.lowpc + baseaddr,
1936 comp_unit_die.highpc + baseaddr - 1, pst);
1937
1938 /* Check if comp unit has_children.
1939 If so, read the rest of the partial symbols from this comp unit.
1940 If not, there's no more debug_info for this comp unit. */
1941 if (comp_unit_die.has_children)
1942 {
1943 struct partial_die_info *first_die;
1944 CORE_ADDR lowpc, highpc;
31ffec48 1945
93311388
DE
1946 lowpc = ((CORE_ADDR) -1);
1947 highpc = ((CORE_ADDR) 0);
c906108c 1948
93311388 1949 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 1950
93311388
DE
1951 scan_partial_symbols (first_die, &lowpc, &highpc,
1952 ! comp_unit_die.has_pc_info, &cu);
57c22c6c 1953
93311388
DE
1954 /* If we didn't find a lowpc, set it to highpc to avoid
1955 complaints from `maint check'. */
1956 if (lowpc == ((CORE_ADDR) -1))
1957 lowpc = highpc;
10b3939b 1958
93311388
DE
1959 /* If the compilation unit didn't have an explicit address range,
1960 then use the information extracted from its child dies. */
1961 if (! comp_unit_die.has_pc_info)
1962 {
1963 comp_unit_die.lowpc = lowpc;
1964 comp_unit_die.highpc = highpc;
1965 }
1966 }
1967 pst->textlow = comp_unit_die.lowpc + baseaddr;
1968 pst->texthigh = comp_unit_die.highpc + baseaddr;
c906108c 1969
93311388
DE
1970 pst->n_global_syms = objfile->global_psymbols.next -
1971 (objfile->global_psymbols.list + pst->globals_offset);
1972 pst->n_static_syms = objfile->static_psymbols.next -
1973 (objfile->static_psymbols.list + pst->statics_offset);
1974 sort_pst_symbols (pst);
c906108c 1975
93311388
DE
1976 /* If there is already a psymtab or symtab for a file of this
1977 name, remove it. (If there is a symtab, more drastic things
1978 also happen.) This happens in VxWorks. */
348e048f
DE
1979 if (! this_cu->from_debug_types)
1980 free_named_symtabs (pst->filename);
ae038cb0 1981
93311388
DE
1982 info_ptr = (beg_of_comp_unit + cu.header.length
1983 + cu.header.initial_length_size);
ae038cb0 1984
348e048f
DE
1985 if (this_cu->from_debug_types)
1986 {
1987 /* It's not clear we want to do anything with stmt lists here.
1988 Waiting to see what gcc ultimately does. */
1989 }
1990 else if (comp_unit_die.has_stmt_list)
93311388
DE
1991 {
1992 /* Get the list of files included in the current compilation unit,
1993 and build a psymtab for each of them. */
1994 dwarf2_build_include_psymtabs (&cu, &comp_unit_die, pst);
1995 }
ae038cb0 1996
93311388 1997 do_cleanups (back_to_inner);
ae038cb0 1998
93311388
DE
1999 return info_ptr;
2000}
ff013f42 2001
348e048f
DE
2002/* Traversal function for htab_traverse_noresize.
2003 Process one .debug_types comp-unit. */
2004
2005static int
2006process_type_comp_unit (void **slot, void *info)
2007{
2008 struct signatured_type *entry = (struct signatured_type *) *slot;
2009 struct objfile *objfile = (struct objfile *) info;
2010 struct dwarf2_per_cu_data *this_cu;
2011
2012 this_cu = &entry->per_cu;
2013 this_cu->from_debug_types = 1;
2014
2015 process_psymtab_comp_unit (objfile, this_cu,
2016 dwarf2_per_objfile->types.buffer,
2017 dwarf2_per_objfile->types.buffer + entry->offset,
2018 dwarf2_per_objfile->types.size);
2019
2020 return 1;
2021}
2022
2023/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
2024 Build partial symbol tables for the .debug_types comp-units. */
2025
2026static void
2027build_type_psymtabs (struct objfile *objfile)
2028{
2029 if (! create_debug_types_hash_table (objfile))
2030 return;
2031
2032 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
2033 process_type_comp_unit, objfile);
2034}
2035
93311388
DE
2036/* Build the partial symbol table by doing a quick pass through the
2037 .debug_info and .debug_abbrev sections. */
72bf9492 2038
93311388
DE
2039static void
2040dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
2041{
2042 /* Instead of reading this into a big buffer, we should probably use
2043 mmap() on architectures that support it. (FIXME) */
2044 bfd *abfd = objfile->obfd;
2045 gdb_byte *info_ptr;
2046 struct cleanup *back_to;
2047
2048 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 2049
93311388
DE
2050 /* Any cached compilation units will be linked by the per-objfile
2051 read_in_chain. Make sure to free them when we're done. */
2052 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 2053
348e048f
DE
2054 build_type_psymtabs (objfile);
2055
93311388 2056 create_all_comp_units (objfile);
c906108c 2057
93311388
DE
2058 objfile->psymtabs_addrmap =
2059 addrmap_create_mutable (&objfile->objfile_obstack);
72bf9492 2060
93311388
DE
2061 /* Since the objects we're extracting from .debug_info vary in
2062 length, only the individual functions to extract them (like
2063 read_comp_unit_head and load_partial_die) can really know whether
2064 the buffer is large enough to hold another complete object.
c906108c 2065
93311388
DE
2066 At the moment, they don't actually check that. If .debug_info
2067 holds just one extra byte after the last compilation unit's dies,
2068 then read_comp_unit_head will happily read off the end of the
2069 buffer. read_partial_die is similarly casual. Those functions
2070 should be fixed.
c906108c 2071
93311388
DE
2072 For this loop condition, simply checking whether there's any data
2073 left at all should be sufficient. */
c906108c 2074
93311388
DE
2075 while (info_ptr < (dwarf2_per_objfile->info.buffer
2076 + dwarf2_per_objfile->info.size))
2077 {
2078 struct dwarf2_per_cu_data *this_cu;
dd373385 2079
93311388
DE
2080 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
2081 objfile);
aaa75496 2082
93311388
DE
2083 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
2084 dwarf2_per_objfile->info.buffer,
2085 info_ptr,
2086 dwarf2_per_objfile->info.size);
c906108c 2087 }
ff013f42
JK
2088
2089 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
2090 &objfile->objfile_obstack);
2091
ae038cb0
DJ
2092 do_cleanups (back_to);
2093}
2094
93311388 2095/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
2096
2097static void
93311388
DE
2098load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
2099 struct objfile *objfile)
ae038cb0
DJ
2100{
2101 bfd *abfd = objfile->obfd;
fe1b8b76 2102 gdb_byte *info_ptr, *beg_of_comp_unit;
ae038cb0
DJ
2103 struct partial_die_info comp_unit_die;
2104 struct dwarf2_cu *cu;
2105 struct abbrev_info *abbrev;
2106 unsigned int bytes_read;
2107 struct cleanup *back_to;
2108
348e048f
DE
2109 gdb_assert (! this_cu->from_debug_types);
2110
dce234bc 2111 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
2112 beg_of_comp_unit = info_ptr;
2113
93311388 2114 cu = alloc_one_comp_unit (objfile);
ae038cb0 2115
93311388 2116 /* ??? Missing cleanup for CU? */
ae038cb0 2117
93311388
DE
2118 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
2119 dwarf2_per_objfile->info.buffer,
2120 dwarf2_per_objfile->info.size,
2121 abfd);
ae038cb0
DJ
2122
2123 /* Complete the cu_header. */
93311388 2124 cu->header.offset = this_cu->offset;
d00adf39 2125 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
ae038cb0
DJ
2126
2127 /* Read the abbrevs for this compilation unit into a table. */
2128 dwarf2_read_abbrevs (abfd, cu);
2129 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
2130
2131 /* Read the compilation unit die. */
2132 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
93311388
DE
2133 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read, abfd,
2134 dwarf2_per_objfile->info.buffer, info_ptr, cu);
ae038cb0
DJ
2135
2136 /* Set the language we're debugging. */
2137 set_cu_language (comp_unit_die.language, cu);
2138
2139 /* Link this compilation unit into the compilation unit tree. */
2140 this_cu->cu = cu;
2141 cu->per_cu = this_cu;
93311388 2142 cu->type_hash = this_cu->type_hash;
ae038cb0
DJ
2143
2144 /* Check if comp unit has_children.
2145 If so, read the rest of the partial symbols from this comp unit.
2146 If not, there's no more debug_info for this comp unit. */
2147 if (comp_unit_die.has_children)
93311388 2148 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0
DJ
2149
2150 do_cleanups (back_to);
2151}
2152
2153/* Create a list of all compilation units in OBJFILE. We do this only
2154 if an inter-comp-unit reference is found; presumably if there is one,
2155 there will be many, and one will occur early in the .debug_info section.
2156 So there's no point in building this list incrementally. */
2157
2158static void
2159create_all_comp_units (struct objfile *objfile)
2160{
2161 int n_allocated;
2162 int n_comp_units;
2163 struct dwarf2_per_cu_data **all_comp_units;
dce234bc 2164 gdb_byte *info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
2165
2166 n_comp_units = 0;
2167 n_allocated = 10;
2168 all_comp_units = xmalloc (n_allocated
2169 * sizeof (struct dwarf2_per_cu_data *));
2170
dce234bc 2171 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 2172 {
c764a876 2173 unsigned int length, initial_length_size;
fe1b8b76 2174 gdb_byte *beg_of_comp_unit;
ae038cb0 2175 struct dwarf2_per_cu_data *this_cu;
c764a876 2176 unsigned int offset;
ae038cb0 2177
dce234bc 2178 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
2179
2180 /* Read just enough information to find out where the next
2181 compilation unit is. */
c764a876
DE
2182 length = read_initial_length (objfile->obfd, info_ptr,
2183 &initial_length_size);
ae038cb0
DJ
2184
2185 /* Save the compilation unit for later lookup. */
2186 this_cu = obstack_alloc (&objfile->objfile_obstack,
2187 sizeof (struct dwarf2_per_cu_data));
2188 memset (this_cu, 0, sizeof (*this_cu));
2189 this_cu->offset = offset;
c764a876 2190 this_cu->length = length + initial_length_size;
ae038cb0
DJ
2191
2192 if (n_comp_units == n_allocated)
2193 {
2194 n_allocated *= 2;
2195 all_comp_units = xrealloc (all_comp_units,
2196 n_allocated
2197 * sizeof (struct dwarf2_per_cu_data *));
2198 }
2199 all_comp_units[n_comp_units++] = this_cu;
2200
2201 info_ptr = info_ptr + this_cu->length;
2202 }
2203
2204 dwarf2_per_objfile->all_comp_units
2205 = obstack_alloc (&objfile->objfile_obstack,
2206 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
2207 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
2208 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
2209 xfree (all_comp_units);
2210 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
2211}
2212
5734ee8b
DJ
2213/* Process all loaded DIEs for compilation unit CU, starting at
2214 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
2215 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
2216 DW_AT_ranges). If NEED_PC is set, then this function will set
2217 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
2218 and record the covered ranges in the addrmap. */
c906108c 2219
72bf9492
DJ
2220static void
2221scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 2222 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 2223{
e7c27a73 2224 struct objfile *objfile = cu->objfile;
c906108c 2225 bfd *abfd = objfile->obfd;
72bf9492 2226 struct partial_die_info *pdi;
c906108c 2227
91c24f0a
DC
2228 /* Now, march along the PDI's, descending into ones which have
2229 interesting children but skipping the children of the other ones,
2230 until we reach the end of the compilation unit. */
c906108c 2231
72bf9492 2232 pdi = first_die;
91c24f0a 2233
72bf9492
DJ
2234 while (pdi != NULL)
2235 {
2236 fixup_partial_die (pdi, cu);
c906108c 2237
91c24f0a
DC
2238 /* Anonymous namespaces have no name but have interesting
2239 children, so we need to look at them. Ditto for anonymous
2240 enums. */
933c6fe4 2241
72bf9492
DJ
2242 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
2243 || pdi->tag == DW_TAG_enumeration_type)
c906108c 2244 {
72bf9492 2245 switch (pdi->tag)
c906108c
SS
2246 {
2247 case DW_TAG_subprogram:
5734ee8b 2248 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
2249 break;
2250 case DW_TAG_variable:
2251 case DW_TAG_typedef:
91c24f0a 2252 case DW_TAG_union_type:
72bf9492 2253 if (!pdi->is_declaration)
63d06c5c 2254 {
72bf9492 2255 add_partial_symbol (pdi, cu);
63d06c5c
DC
2256 }
2257 break;
c906108c 2258 case DW_TAG_class_type:
680b30c7 2259 case DW_TAG_interface_type:
c906108c 2260 case DW_TAG_structure_type:
72bf9492 2261 if (!pdi->is_declaration)
c906108c 2262 {
72bf9492 2263 add_partial_symbol (pdi, cu);
c906108c
SS
2264 }
2265 break;
91c24f0a 2266 case DW_TAG_enumeration_type:
72bf9492
DJ
2267 if (!pdi->is_declaration)
2268 add_partial_enumeration (pdi, cu);
c906108c
SS
2269 break;
2270 case DW_TAG_base_type:
a02abb62 2271 case DW_TAG_subrange_type:
c906108c 2272 /* File scope base type definitions are added to the partial
c5aa993b 2273 symbol table. */
72bf9492 2274 add_partial_symbol (pdi, cu);
c906108c 2275 break;
d9fa45fe 2276 case DW_TAG_namespace:
5734ee8b 2277 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 2278 break;
5d7cb8df
JK
2279 case DW_TAG_module:
2280 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
2281 break;
c906108c
SS
2282 default:
2283 break;
2284 }
2285 }
2286
72bf9492
DJ
2287 /* If the die has a sibling, skip to the sibling. */
2288
2289 pdi = pdi->die_sibling;
2290 }
2291}
2292
2293/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 2294
72bf9492 2295 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
2296 name is concatenated with "::" and the partial DIE's name. For
2297 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
2298 Enumerators are an exception; they use the scope of their parent
2299 enumeration type, i.e. the name of the enumeration type is not
2300 prepended to the enumerator.
91c24f0a 2301
72bf9492
DJ
2302 There are two complexities. One is DW_AT_specification; in this
2303 case "parent" means the parent of the target of the specification,
2304 instead of the direct parent of the DIE. The other is compilers
2305 which do not emit DW_TAG_namespace; in this case we try to guess
2306 the fully qualified name of structure types from their members'
2307 linkage names. This must be done using the DIE's children rather
2308 than the children of any DW_AT_specification target. We only need
2309 to do this for structures at the top level, i.e. if the target of
2310 any DW_AT_specification (if any; otherwise the DIE itself) does not
2311 have a parent. */
2312
2313/* Compute the scope prefix associated with PDI's parent, in
2314 compilation unit CU. The result will be allocated on CU's
2315 comp_unit_obstack, or a copy of the already allocated PDI->NAME
2316 field. NULL is returned if no prefix is necessary. */
2317static char *
2318partial_die_parent_scope (struct partial_die_info *pdi,
2319 struct dwarf2_cu *cu)
2320{
2321 char *grandparent_scope;
2322 struct partial_die_info *parent, *real_pdi;
91c24f0a 2323
72bf9492
DJ
2324 /* We need to look at our parent DIE; if we have a DW_AT_specification,
2325 then this means the parent of the specification DIE. */
2326
2327 real_pdi = pdi;
72bf9492 2328 while (real_pdi->has_specification)
10b3939b 2329 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
2330
2331 parent = real_pdi->die_parent;
2332 if (parent == NULL)
2333 return NULL;
2334
2335 if (parent->scope_set)
2336 return parent->scope;
2337
2338 fixup_partial_die (parent, cu);
2339
10b3939b 2340 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492
DJ
2341
2342 if (parent->tag == DW_TAG_namespace
2343 || parent->tag == DW_TAG_structure_type
2344 || parent->tag == DW_TAG_class_type
680b30c7 2345 || parent->tag == DW_TAG_interface_type
72bf9492
DJ
2346 || parent->tag == DW_TAG_union_type)
2347 {
2348 if (grandparent_scope == NULL)
2349 parent->scope = parent->name;
2350 else
987504bb
JJ
2351 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
2352 parent->name, cu);
72bf9492
DJ
2353 }
2354 else if (parent->tag == DW_TAG_enumeration_type)
2355 /* Enumerators should not get the name of the enumeration as a prefix. */
2356 parent->scope = grandparent_scope;
2357 else
2358 {
2359 /* FIXME drow/2004-04-01: What should we be doing with
2360 function-local names? For partial symbols, we should probably be
2361 ignoring them. */
2362 complaint (&symfile_complaints,
e2e0b3e5 2363 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
2364 parent->tag, pdi->offset);
2365 parent->scope = grandparent_scope;
c906108c
SS
2366 }
2367
72bf9492
DJ
2368 parent->scope_set = 1;
2369 return parent->scope;
2370}
2371
2372/* Return the fully scoped name associated with PDI, from compilation unit
2373 CU. The result will be allocated with malloc. */
2374static char *
2375partial_die_full_name (struct partial_die_info *pdi,
2376 struct dwarf2_cu *cu)
2377{
2378 char *parent_scope;
2379
2380 parent_scope = partial_die_parent_scope (pdi, cu);
2381 if (parent_scope == NULL)
2382 return NULL;
2383 else
987504bb 2384 return typename_concat (NULL, parent_scope, pdi->name, cu);
c906108c
SS
2385}
2386
2387static void
72bf9492 2388add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 2389{
e7c27a73 2390 struct objfile *objfile = cu->objfile;
c906108c 2391 CORE_ADDR addr = 0;
decbce07 2392 char *actual_name = NULL;
72bf9492 2393 const char *my_prefix;
5c4e30ca 2394 const struct partial_symbol *psym = NULL;
e142c38c 2395 CORE_ADDR baseaddr;
72bf9492 2396 int built_actual_name = 0;
e142c38c
DJ
2397
2398 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 2399
72bf9492 2400 if (pdi_needs_namespace (pdi->tag))
63d06c5c 2401 {
72bf9492
DJ
2402 actual_name = partial_die_full_name (pdi, cu);
2403 if (actual_name)
2404 built_actual_name = 1;
63d06c5c
DC
2405 }
2406
72bf9492
DJ
2407 if (actual_name == NULL)
2408 actual_name = pdi->name;
2409
c906108c
SS
2410 switch (pdi->tag)
2411 {
2412 case DW_TAG_subprogram:
2cfa0c8d 2413 if (pdi->is_external || cu->language == language_ada)
c906108c 2414 {
2cfa0c8d
JB
2415 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
2416 of the global scope. But in Ada, we want to be able to access
2417 nested procedures globally. So all Ada subprograms are stored
2418 in the global scope. */
38d518c9 2419 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 2420 mst_text, objfile); */
38d518c9 2421 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2422 VAR_DOMAIN, LOC_BLOCK,
2423 &objfile->global_psymbols,
2424 0, pdi->lowpc + baseaddr,
e142c38c 2425 cu->language, objfile);
c906108c
SS
2426 }
2427 else
2428 {
38d518c9 2429 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 2430 mst_file_text, objfile); */
38d518c9 2431 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2432 VAR_DOMAIN, LOC_BLOCK,
2433 &objfile->static_psymbols,
2434 0, pdi->lowpc + baseaddr,
e142c38c 2435 cu->language, objfile);
c906108c
SS
2436 }
2437 break;
2438 case DW_TAG_variable:
2439 if (pdi->is_external)
2440 {
2441 /* Global Variable.
2442 Don't enter into the minimal symbol tables as there is
2443 a minimal symbol table entry from the ELF symbols already.
2444 Enter into partial symbol table if it has a location
2445 descriptor or a type.
2446 If the location descriptor is missing, new_symbol will create
2447 a LOC_UNRESOLVED symbol, the address of the variable will then
2448 be determined from the minimal symbol table whenever the variable
2449 is referenced.
2450 The address for the partial symbol table entry is not
2451 used by GDB, but it comes in handy for debugging partial symbol
2452 table building. */
2453
2454 if (pdi->locdesc)
e7c27a73 2455 addr = decode_locdesc (pdi->locdesc, cu);
c906108c 2456 if (pdi->locdesc || pdi->has_type)
38d518c9 2457 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2458 VAR_DOMAIN, LOC_STATIC,
2459 &objfile->global_psymbols,
2460 0, addr + baseaddr,
e142c38c 2461 cu->language, objfile);
c906108c
SS
2462 }
2463 else
2464 {
2465 /* Static Variable. Skip symbols without location descriptors. */
2466 if (pdi->locdesc == NULL)
decbce07
MS
2467 {
2468 if (built_actual_name)
2469 xfree (actual_name);
2470 return;
2471 }
e7c27a73 2472 addr = decode_locdesc (pdi->locdesc, cu);
38d518c9 2473 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 2474 mst_file_data, objfile); */
38d518c9 2475 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2476 VAR_DOMAIN, LOC_STATIC,
2477 &objfile->static_psymbols,
2478 0, addr + baseaddr,
e142c38c 2479 cu->language, objfile);
c906108c
SS
2480 }
2481 break;
2482 case DW_TAG_typedef:
2483 case DW_TAG_base_type:
a02abb62 2484 case DW_TAG_subrange_type:
38d518c9 2485 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2486 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 2487 &objfile->static_psymbols,
e142c38c 2488 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 2489 break;
72bf9492
DJ
2490 case DW_TAG_namespace:
2491 add_psymbol_to_list (actual_name, strlen (actual_name),
2492 VAR_DOMAIN, LOC_TYPEDEF,
2493 &objfile->global_psymbols,
2494 0, (CORE_ADDR) 0, cu->language, objfile);
2495 break;
c906108c 2496 case DW_TAG_class_type:
680b30c7 2497 case DW_TAG_interface_type:
c906108c
SS
2498 case DW_TAG_structure_type:
2499 case DW_TAG_union_type:
2500 case DW_TAG_enumeration_type:
fa4028e9
JB
2501 /* Skip external references. The DWARF standard says in the section
2502 about "Structure, Union, and Class Type Entries": "An incomplete
2503 structure, union or class type is represented by a structure,
2504 union or class entry that does not have a byte size attribute
2505 and that has a DW_AT_declaration attribute." */
2506 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
2507 {
2508 if (built_actual_name)
2509 xfree (actual_name);
2510 return;
2511 }
fa4028e9 2512
63d06c5c
DC
2513 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
2514 static vs. global. */
38d518c9 2515 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2516 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
2517 (cu->language == language_cplus
2518 || cu->language == language_java)
63d06c5c
DC
2519 ? &objfile->global_psymbols
2520 : &objfile->static_psymbols,
e142c38c 2521 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 2522
c906108c
SS
2523 break;
2524 case DW_TAG_enumerator:
38d518c9 2525 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2526 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
2527 (cu->language == language_cplus
2528 || cu->language == language_java)
f6fe98ef
DJ
2529 ? &objfile->global_psymbols
2530 : &objfile->static_psymbols,
e142c38c 2531 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
2532 break;
2533 default:
2534 break;
2535 }
5c4e30ca
DC
2536
2537 /* Check to see if we should scan the name for possible namespace
2538 info. Only do this if this is C++, if we don't have namespace
2539 debugging info in the file, if the psym is of an appropriate type
2540 (otherwise we'll have psym == NULL), and if we actually had a
2541 mangled name to begin with. */
2542
72bf9492
DJ
2543 /* FIXME drow/2004-02-22: Why don't we do this for classes, i.e. the
2544 cases which do not set PSYM above? */
2545
e142c38c 2546 if (cu->language == language_cplus
72bf9492 2547 && cu->has_namespace_info == 0
5c4e30ca
DC
2548 && psym != NULL
2549 && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
2550 cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
2551 objfile);
72bf9492
DJ
2552
2553 if (built_actual_name)
2554 xfree (actual_name);
c906108c
SS
2555}
2556
72bf9492
DJ
2557/* Determine whether a die of type TAG living in a C++ class or
2558 namespace needs to have the name of the scope prepended to the
63d06c5c
DC
2559 name listed in the die. */
2560
2561static int
72bf9492 2562pdi_needs_namespace (enum dwarf_tag tag)
63d06c5c 2563{
63d06c5c
DC
2564 switch (tag)
2565 {
72bf9492 2566 case DW_TAG_namespace:
63d06c5c
DC
2567 case DW_TAG_typedef:
2568 case DW_TAG_class_type:
680b30c7 2569 case DW_TAG_interface_type:
63d06c5c
DC
2570 case DW_TAG_structure_type:
2571 case DW_TAG_union_type:
2572 case DW_TAG_enumeration_type:
2573 case DW_TAG_enumerator:
2574 return 1;
2575 default:
2576 return 0;
2577 }
2578}
2579
5c4e30ca
DC
2580/* Read a partial die corresponding to a namespace; also, add a symbol
2581 corresponding to that namespace to the symbol table. NAMESPACE is
2582 the name of the enclosing namespace. */
91c24f0a 2583
72bf9492
DJ
2584static void
2585add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 2586 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 2587 int need_pc, struct dwarf2_cu *cu)
91c24f0a 2588{
e7c27a73 2589 struct objfile *objfile = cu->objfile;
5c4e30ca 2590
72bf9492 2591 /* Add a symbol for the namespace. */
e7c27a73 2592
72bf9492 2593 add_partial_symbol (pdi, cu);
5c4e30ca
DC
2594
2595 /* Now scan partial symbols in that namespace. */
2596
91c24f0a 2597 if (pdi->has_children)
5734ee8b 2598 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
2599}
2600
5d7cb8df
JK
2601/* Read a partial die corresponding to a Fortran module. */
2602
2603static void
2604add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
2605 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
2606{
2607 /* Now scan partial symbols in that module.
2608
2609 FIXME: Support the separate Fortran module namespaces. */
2610
2611 if (pdi->has_children)
2612 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
2613}
2614
bc30ff58
JB
2615/* Read a partial die corresponding to a subprogram and create a partial
2616 symbol for that subprogram. When the CU language allows it, this
2617 routine also defines a partial symbol for each nested subprogram
2618 that this subprogram contains.
2619
2620 DIE my also be a lexical block, in which case we simply search
2621 recursively for suprograms defined inside that lexical block.
2622 Again, this is only performed when the CU language allows this
2623 type of definitions. */
2624
2625static void
2626add_partial_subprogram (struct partial_die_info *pdi,
2627 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 2628 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
2629{
2630 if (pdi->tag == DW_TAG_subprogram)
2631 {
2632 if (pdi->has_pc_info)
2633 {
2634 if (pdi->lowpc < *lowpc)
2635 *lowpc = pdi->lowpc;
2636 if (pdi->highpc > *highpc)
2637 *highpc = pdi->highpc;
5734ee8b
DJ
2638 if (need_pc)
2639 {
2640 CORE_ADDR baseaddr;
2641 struct objfile *objfile = cu->objfile;
2642
2643 baseaddr = ANOFFSET (objfile->section_offsets,
2644 SECT_OFF_TEXT (objfile));
2645 addrmap_set_empty (objfile->psymtabs_addrmap,
2646 pdi->lowpc, pdi->highpc - 1,
2647 cu->per_cu->psymtab);
2648 }
bc30ff58
JB
2649 if (!pdi->is_declaration)
2650 add_partial_symbol (pdi, cu);
2651 }
2652 }
2653
2654 if (! pdi->has_children)
2655 return;
2656
2657 if (cu->language == language_ada)
2658 {
2659 pdi = pdi->die_child;
2660 while (pdi != NULL)
2661 {
2662 fixup_partial_die (pdi, cu);
2663 if (pdi->tag == DW_TAG_subprogram
2664 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 2665 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
2666 pdi = pdi->die_sibling;
2667 }
2668 }
2669}
2670
72bf9492
DJ
2671/* See if we can figure out if the class lives in a namespace. We do
2672 this by looking for a member function; its demangled name will
2673 contain namespace info, if there is any. */
63d06c5c 2674
72bf9492
DJ
2675static void
2676guess_structure_name (struct partial_die_info *struct_pdi,
2677 struct dwarf2_cu *cu)
63d06c5c 2678{
987504bb
JJ
2679 if ((cu->language == language_cplus
2680 || cu->language == language_java)
72bf9492 2681 && cu->has_namespace_info == 0
63d06c5c
DC
2682 && struct_pdi->has_children)
2683 {
63d06c5c
DC
2684 /* NOTE: carlton/2003-10-07: Getting the info this way changes
2685 what template types look like, because the demangler
2686 frequently doesn't give the same name as the debug info. We
2687 could fix this by only using the demangled name to get the
134d01f1 2688 prefix (but see comment in read_structure_type). */
63d06c5c 2689
72bf9492
DJ
2690 struct partial_die_info *child_pdi = struct_pdi->die_child;
2691 struct partial_die_info *real_pdi;
5d51ca54 2692
72bf9492
DJ
2693 /* If this DIE (this DIE's specification, if any) has a parent, then
2694 we should not do this. We'll prepend the parent's fully qualified
2695 name when we create the partial symbol. */
5d51ca54 2696
72bf9492 2697 real_pdi = struct_pdi;
72bf9492 2698 while (real_pdi->has_specification)
10b3939b 2699 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 2700
72bf9492
DJ
2701 if (real_pdi->die_parent != NULL)
2702 return;
63d06c5c 2703
72bf9492
DJ
2704 while (child_pdi != NULL)
2705 {
2706 if (child_pdi->tag == DW_TAG_subprogram)
63d06c5c 2707 {
72bf9492 2708 char *actual_class_name
31c27f77
JJ
2709 = language_class_name_from_physname (cu->language_defn,
2710 child_pdi->name);
63d06c5c 2711 if (actual_class_name != NULL)
72bf9492
DJ
2712 {
2713 struct_pdi->name
2714 = obsavestring (actual_class_name,
2715 strlen (actual_class_name),
2716 &cu->comp_unit_obstack);
2717 xfree (actual_class_name);
2718 }
63d06c5c
DC
2719 break;
2720 }
72bf9492
DJ
2721
2722 child_pdi = child_pdi->die_sibling;
63d06c5c
DC
2723 }
2724 }
63d06c5c
DC
2725}
2726
91c24f0a
DC
2727/* Read a partial die corresponding to an enumeration type. */
2728
72bf9492
DJ
2729static void
2730add_partial_enumeration (struct partial_die_info *enum_pdi,
2731 struct dwarf2_cu *cu)
91c24f0a 2732{
e7c27a73 2733 struct objfile *objfile = cu->objfile;
91c24f0a 2734 bfd *abfd = objfile->obfd;
72bf9492 2735 struct partial_die_info *pdi;
91c24f0a
DC
2736
2737 if (enum_pdi->name != NULL)
72bf9492
DJ
2738 add_partial_symbol (enum_pdi, cu);
2739
2740 pdi = enum_pdi->die_child;
2741 while (pdi)
91c24f0a 2742 {
72bf9492 2743 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 2744 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 2745 else
72bf9492
DJ
2746 add_partial_symbol (pdi, cu);
2747 pdi = pdi->die_sibling;
91c24f0a 2748 }
91c24f0a
DC
2749}
2750
4bb7a0a7
DJ
2751/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
2752 Return the corresponding abbrev, or NULL if the number is zero (indicating
2753 an empty DIE). In either case *BYTES_READ will be set to the length of
2754 the initial number. */
2755
2756static struct abbrev_info *
fe1b8b76 2757peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 2758 struct dwarf2_cu *cu)
4bb7a0a7
DJ
2759{
2760 bfd *abfd = cu->objfile->obfd;
2761 unsigned int abbrev_number;
2762 struct abbrev_info *abbrev;
2763
2764 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
2765
2766 if (abbrev_number == 0)
2767 return NULL;
2768
2769 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
2770 if (!abbrev)
2771 {
8a3fe4f8 2772 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
2773 bfd_get_filename (abfd));
2774 }
2775
2776 return abbrev;
2777}
2778
93311388
DE
2779/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
2780 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
2781 DIE. Any children of the skipped DIEs will also be skipped. */
2782
fe1b8b76 2783static gdb_byte *
93311388 2784skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
2785{
2786 struct abbrev_info *abbrev;
2787 unsigned int bytes_read;
2788
2789 while (1)
2790 {
2791 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
2792 if (abbrev == NULL)
2793 return info_ptr + bytes_read;
2794 else
93311388 2795 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
2796 }
2797}
2798
93311388
DE
2799/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
2800 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
2801 abbrev corresponding to that skipped uleb128 should be passed in
2802 ABBREV. Returns a pointer to this DIE's sibling, skipping any
2803 children. */
2804
fe1b8b76 2805static gdb_byte *
93311388
DE
2806skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
2807 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
2808{
2809 unsigned int bytes_read;
2810 struct attribute attr;
2811 bfd *abfd = cu->objfile->obfd;
2812 unsigned int form, i;
2813
2814 for (i = 0; i < abbrev->num_attrs; i++)
2815 {
2816 /* The only abbrev we care about is DW_AT_sibling. */
2817 if (abbrev->attrs[i].name == DW_AT_sibling)
2818 {
2819 read_attribute (&attr, &abbrev->attrs[i],
2820 abfd, info_ptr, cu);
2821 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 2822 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 2823 else
93311388 2824 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
2825 }
2826
2827 /* If it isn't DW_AT_sibling, skip this attribute. */
2828 form = abbrev->attrs[i].form;
2829 skip_attribute:
2830 switch (form)
2831 {
2832 case DW_FORM_addr:
2833 case DW_FORM_ref_addr:
2834 info_ptr += cu->header.addr_size;
2835 break;
2836 case DW_FORM_data1:
2837 case DW_FORM_ref1:
2838 case DW_FORM_flag:
2839 info_ptr += 1;
2840 break;
2841 case DW_FORM_data2:
2842 case DW_FORM_ref2:
2843 info_ptr += 2;
2844 break;
2845 case DW_FORM_data4:
2846 case DW_FORM_ref4:
2847 info_ptr += 4;
2848 break;
2849 case DW_FORM_data8:
2850 case DW_FORM_ref8:
348e048f 2851 case DW_FORM_sig8:
4bb7a0a7
DJ
2852 info_ptr += 8;
2853 break;
2854 case DW_FORM_string:
2855 read_string (abfd, info_ptr, &bytes_read);
2856 info_ptr += bytes_read;
2857 break;
2858 case DW_FORM_strp:
2859 info_ptr += cu->header.offset_size;
2860 break;
2861 case DW_FORM_block:
2862 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2863 info_ptr += bytes_read;
2864 break;
2865 case DW_FORM_block1:
2866 info_ptr += 1 + read_1_byte (abfd, info_ptr);
2867 break;
2868 case DW_FORM_block2:
2869 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
2870 break;
2871 case DW_FORM_block4:
2872 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
2873 break;
2874 case DW_FORM_sdata:
2875 case DW_FORM_udata:
2876 case DW_FORM_ref_udata:
2877 info_ptr = skip_leb128 (abfd, info_ptr);
2878 break;
2879 case DW_FORM_indirect:
2880 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2881 info_ptr += bytes_read;
2882 /* We need to continue parsing from here, so just go back to
2883 the top. */
2884 goto skip_attribute;
2885
2886 default:
8a3fe4f8 2887 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
2888 dwarf_form_name (form),
2889 bfd_get_filename (abfd));
2890 }
2891 }
2892
2893 if (abbrev->has_children)
93311388 2894 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
2895 else
2896 return info_ptr;
2897}
2898
93311388
DE
2899/* Locate ORIG_PDI's sibling.
2900 INFO_PTR should point to the start of the next DIE after ORIG_PDI
2901 in BUFFER. */
91c24f0a 2902
fe1b8b76 2903static gdb_byte *
93311388
DE
2904locate_pdi_sibling (struct partial_die_info *orig_pdi,
2905 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 2906 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
2907{
2908 /* Do we know the sibling already? */
72bf9492 2909
91c24f0a
DC
2910 if (orig_pdi->sibling)
2911 return orig_pdi->sibling;
2912
2913 /* Are there any children to deal with? */
2914
2915 if (!orig_pdi->has_children)
2916 return info_ptr;
2917
4bb7a0a7 2918 /* Skip the children the long way. */
91c24f0a 2919
93311388 2920 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
2921}
2922
c906108c
SS
2923/* Expand this partial symbol table into a full symbol table. */
2924
2925static void
fba45db2 2926dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c
SS
2927{
2928 /* FIXME: This is barely more than a stub. */
2929 if (pst != NULL)
2930 {
2931 if (pst->readin)
2932 {
8a3fe4f8 2933 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
2934 }
2935 else
2936 {
2937 if (info_verbose)
2938 {
a3f17187 2939 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
2940 gdb_flush (gdb_stdout);
2941 }
2942
10b3939b
DJ
2943 /* Restore our global data. */
2944 dwarf2_per_objfile = objfile_data (pst->objfile,
2945 dwarf2_objfile_data_key);
2946
b2ab525c
KB
2947 /* If this psymtab is constructed from a debug-only objfile, the
2948 has_section_at_zero flag will not necessarily be correct. We
2949 can get the correct value for this flag by looking at the data
2950 associated with the (presumably stripped) associated objfile. */
2951 if (pst->objfile->separate_debug_objfile_backlink)
2952 {
2953 struct dwarf2_per_objfile *dpo_backlink
2954 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
2955 dwarf2_objfile_data_key);
2956 dwarf2_per_objfile->has_section_at_zero
2957 = dpo_backlink->has_section_at_zero;
2958 }
2959
c906108c
SS
2960 psymtab_to_symtab_1 (pst);
2961
2962 /* Finish up the debug error message. */
2963 if (info_verbose)
a3f17187 2964 printf_filtered (_("done.\n"));
c906108c
SS
2965 }
2966 }
2967}
2968
10b3939b
DJ
2969/* Add PER_CU to the queue. */
2970
2971static void
03dd20cc 2972queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
2973{
2974 struct dwarf2_queue_item *item;
2975
2976 per_cu->queued = 1;
2977 item = xmalloc (sizeof (*item));
2978 item->per_cu = per_cu;
2979 item->next = NULL;
2980
2981 if (dwarf2_queue == NULL)
2982 dwarf2_queue = item;
2983 else
2984 dwarf2_queue_tail->next = item;
2985
2986 dwarf2_queue_tail = item;
2987}
2988
2989/* Process the queue. */
2990
2991static void
2992process_queue (struct objfile *objfile)
2993{
2994 struct dwarf2_queue_item *item, *next_item;
2995
03dd20cc
DJ
2996 /* The queue starts out with one item, but following a DIE reference
2997 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
2998 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
2999 {
31ffec48 3000 if (item->per_cu->psymtab && !item->per_cu->psymtab->readin)
10b3939b
DJ
3001 process_full_comp_unit (item->per_cu);
3002
3003 item->per_cu->queued = 0;
3004 next_item = item->next;
3005 xfree (item);
3006 }
3007
3008 dwarf2_queue_tail = NULL;
3009}
3010
3011/* Free all allocated queue entries. This function only releases anything if
3012 an error was thrown; if the queue was processed then it would have been
3013 freed as we went along. */
3014
3015static void
3016dwarf2_release_queue (void *dummy)
3017{
3018 struct dwarf2_queue_item *item, *last;
3019
3020 item = dwarf2_queue;
3021 while (item)
3022 {
3023 /* Anything still marked queued is likely to be in an
3024 inconsistent state, so discard it. */
3025 if (item->per_cu->queued)
3026 {
3027 if (item->per_cu->cu != NULL)
3028 free_one_cached_comp_unit (item->per_cu->cu);
3029 item->per_cu->queued = 0;
3030 }
3031
3032 last = item;
3033 item = item->next;
3034 xfree (last);
3035 }
3036
3037 dwarf2_queue = dwarf2_queue_tail = NULL;
3038}
3039
3040/* Read in full symbols for PST, and anything it depends on. */
3041
c906108c 3042static void
fba45db2 3043psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 3044{
10b3939b 3045 struct dwarf2_per_cu_data *per_cu;
c906108c 3046 struct cleanup *back_to;
aaa75496
JB
3047 int i;
3048
3049 for (i = 0; i < pst->number_of_dependencies; i++)
3050 if (!pst->dependencies[i]->readin)
3051 {
3052 /* Inform about additional files that need to be read in. */
3053 if (info_verbose)
3054 {
a3f17187 3055 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
3056 fputs_filtered (" ", gdb_stdout);
3057 wrap_here ("");
3058 fputs_filtered ("and ", gdb_stdout);
3059 wrap_here ("");
3060 printf_filtered ("%s...", pst->dependencies[i]->filename);
3061 wrap_here (""); /* Flush output */
3062 gdb_flush (gdb_stdout);
3063 }
3064 psymtab_to_symtab_1 (pst->dependencies[i]);
3065 }
3066
10b3939b
DJ
3067 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
3068
3069 if (per_cu == NULL)
aaa75496
JB
3070 {
3071 /* It's an include file, no symbols to read for it.
3072 Everything is in the parent symtab. */
3073 pst->readin = 1;
3074 return;
3075 }
c906108c 3076
10b3939b
DJ
3077 back_to = make_cleanup (dwarf2_release_queue, NULL);
3078
03dd20cc 3079 queue_comp_unit (per_cu, pst->objfile);
10b3939b 3080
348e048f
DE
3081 if (per_cu->from_debug_types)
3082 read_signatured_type_at_offset (pst->objfile, per_cu->offset);
3083 else
3084 load_full_comp_unit (per_cu, pst->objfile);
3085
10b3939b
DJ
3086 process_queue (pst->objfile);
3087
3088 /* Age the cache, releasing compilation units that have not
3089 been used recently. */
3090 age_cached_comp_units ();
3091
3092 do_cleanups (back_to);
3093}
3094
93311388 3095/* Load the DIEs associated with PER_CU into memory. */
10b3939b 3096
93311388 3097static void
31ffec48 3098load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 3099{
31ffec48 3100 bfd *abfd = objfile->obfd;
10b3939b 3101 struct dwarf2_cu *cu;
c764a876 3102 unsigned int offset;
93311388 3103 gdb_byte *info_ptr, *beg_of_comp_unit;
10b3939b
DJ
3104 struct cleanup *back_to, *free_cu_cleanup;
3105 struct attribute *attr;
3106 CORE_ADDR baseaddr;
6502dd73 3107
348e048f
DE
3108 gdb_assert (! per_cu->from_debug_types);
3109
c906108c 3110 /* Set local variables from the partial symbol table info. */
10b3939b 3111 offset = per_cu->offset;
6502dd73 3112
dce234bc 3113 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 3114 beg_of_comp_unit = info_ptr;
63d06c5c 3115
93311388 3116 cu = alloc_one_comp_unit (objfile);
c906108c 3117
10b3939b
DJ
3118 /* If an error occurs while loading, release our storage. */
3119 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 3120
93311388 3121 /* Read in the comp_unit header. */
10b3939b 3122 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 3123
93311388
DE
3124 /* Complete the cu_header. */
3125 cu->header.offset = offset;
3126 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3127
3128 /* Read the abbrevs for this compilation unit. */
10b3939b
DJ
3129 dwarf2_read_abbrevs (abfd, cu);
3130 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3131
93311388 3132 /* Link this compilation unit into the compilation unit tree. */
10b3939b 3133 per_cu->cu = cu;
93311388 3134 cu->per_cu = per_cu;
f792889a 3135 cu->type_hash = per_cu->type_hash;
e142c38c 3136
93311388 3137 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
3138
3139 /* We try not to read any attributes in this function, because not
3140 all objfiles needed for references have been loaded yet, and symbol
3141 table processing isn't initialized. But we have to set the CU language,
3142 or we won't be able to build types correctly. */
3143 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
3144 if (attr)
3145 set_cu_language (DW_UNSND (attr), cu);
3146 else
3147 set_cu_language (language_minimal, cu);
3148
348e048f
DE
3149 /* Link this CU into read_in_chain. */
3150 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3151 dwarf2_per_objfile->read_in_chain = per_cu;
3152
10b3939b 3153 do_cleanups (back_to);
e142c38c 3154
10b3939b
DJ
3155 /* We've successfully allocated this compilation unit. Let our caller
3156 clean it up when finished with it. */
3157 discard_cleanups (free_cu_cleanup);
10b3939b
DJ
3158}
3159
3160/* Generate full symbol information for PST and CU, whose DIEs have
3161 already been loaded into memory. */
3162
3163static void
3164process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
3165{
3166 struct partial_symtab *pst = per_cu->psymtab;
3167 struct dwarf2_cu *cu = per_cu->cu;
3168 struct objfile *objfile = pst->objfile;
3169 bfd *abfd = objfile->obfd;
3170 CORE_ADDR lowpc, highpc;
3171 struct symtab *symtab;
3172 struct cleanup *back_to;
3173 struct attribute *attr;
3174 CORE_ADDR baseaddr;
3175
3176 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3177
10b3939b
DJ
3178 buildsym_init ();
3179 back_to = make_cleanup (really_free_pendings, NULL);
3180
3181 cu->list_in_scope = &file_symbols;
c906108c 3182
0d53c4c4
DJ
3183 /* Find the base address of the compilation unit for range lists and
3184 location lists. It will normally be specified by DW_AT_low_pc.
3185 In DWARF-3 draft 4, the base address could be overridden by
3186 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3187 compilation units with discontinuous ranges. */
3188
d00adf39
DE
3189 cu->base_known = 0;
3190 cu->base_address = 0;
0d53c4c4 3191
10b3939b 3192 attr = dwarf2_attr (cu->dies, DW_AT_entry_pc, cu);
0d53c4c4
DJ
3193 if (attr)
3194 {
d00adf39
DE
3195 cu->base_address = DW_ADDR (attr);
3196 cu->base_known = 1;
0d53c4c4
DJ
3197 }
3198 else
3199 {
10b3939b 3200 attr = dwarf2_attr (cu->dies, DW_AT_low_pc, cu);
0d53c4c4
DJ
3201 if (attr)
3202 {
d00adf39
DE
3203 cu->base_address = DW_ADDR (attr);
3204 cu->base_known = 1;
0d53c4c4
DJ
3205 }
3206 }
3207
c906108c 3208 /* Do line number decoding in read_file_scope () */
10b3939b 3209 process_die (cu->dies, cu);
c906108c 3210
fae299cd
DC
3211 /* Some compilers don't define a DW_AT_high_pc attribute for the
3212 compilation unit. If the DW_AT_high_pc is missing, synthesize
3213 it, by scanning the DIE's below the compilation unit. */
10b3939b 3214 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 3215
613e1657 3216 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
3217
3218 /* Set symtab language to language from DW_AT_language.
3219 If the compilation is from a C file generated by language preprocessors,
3220 do not set the language if it was already deduced by start_subfile. */
3221 if (symtab != NULL
10b3939b 3222 && !(cu->language == language_c && symtab->language != language_c))
c906108c 3223 {
10b3939b 3224 symtab->language = cu->language;
c906108c
SS
3225 }
3226 pst->symtab = symtab;
3227 pst->readin = 1;
c906108c
SS
3228
3229 do_cleanups (back_to);
3230}
3231
3232/* Process a die and its children. */
3233
3234static void
e7c27a73 3235process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
3236{
3237 switch (die->tag)
3238 {
3239 case DW_TAG_padding:
3240 break;
3241 case DW_TAG_compile_unit:
e7c27a73 3242 read_file_scope (die, cu);
c906108c 3243 break;
348e048f
DE
3244 case DW_TAG_type_unit:
3245 read_type_unit_scope (die, cu);
3246 break;
c906108c 3247 case DW_TAG_subprogram:
c906108c 3248 case DW_TAG_inlined_subroutine:
edb3359d 3249 read_func_scope (die, cu);
c906108c
SS
3250 break;
3251 case DW_TAG_lexical_block:
14898363
L
3252 case DW_TAG_try_block:
3253 case DW_TAG_catch_block:
e7c27a73 3254 read_lexical_block_scope (die, cu);
c906108c
SS
3255 break;
3256 case DW_TAG_class_type:
680b30c7 3257 case DW_TAG_interface_type:
c906108c
SS
3258 case DW_TAG_structure_type:
3259 case DW_TAG_union_type:
134d01f1 3260 process_structure_scope (die, cu);
c906108c
SS
3261 break;
3262 case DW_TAG_enumeration_type:
134d01f1 3263 process_enumeration_scope (die, cu);
c906108c 3264 break;
134d01f1 3265
f792889a
DJ
3266 /* These dies have a type, but processing them does not create
3267 a symbol or recurse to process the children. Therefore we can
3268 read them on-demand through read_type_die. */
c906108c 3269 case DW_TAG_subroutine_type:
72019c9c 3270 case DW_TAG_set_type:
c906108c 3271 case DW_TAG_array_type:
c906108c 3272 case DW_TAG_pointer_type:
c906108c 3273 case DW_TAG_ptr_to_member_type:
c906108c 3274 case DW_TAG_reference_type:
c906108c 3275 case DW_TAG_string_type:
c906108c 3276 break;
134d01f1 3277
c906108c 3278 case DW_TAG_base_type:
a02abb62 3279 case DW_TAG_subrange_type:
cb249c71 3280 case DW_TAG_typedef:
134d01f1
DJ
3281 /* Add a typedef symbol for the type definition, if it has a
3282 DW_AT_name. */
f792889a 3283 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 3284 break;
c906108c 3285 case DW_TAG_common_block:
e7c27a73 3286 read_common_block (die, cu);
c906108c
SS
3287 break;
3288 case DW_TAG_common_inclusion:
3289 break;
d9fa45fe 3290 case DW_TAG_namespace:
63d06c5c 3291 processing_has_namespace_info = 1;
e7c27a73 3292 read_namespace (die, cu);
d9fa45fe 3293 break;
5d7cb8df
JK
3294 case DW_TAG_module:
3295 read_module (die, cu);
3296 break;
d9fa45fe
DC
3297 case DW_TAG_imported_declaration:
3298 case DW_TAG_imported_module:
63d06c5c 3299 processing_has_namespace_info = 1;
27aa8d6a
SW
3300 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
3301 || cu->language != language_fortran))
3302 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
3303 dwarf_tag_name (die->tag));
3304 read_import_statement (die, cu);
d9fa45fe 3305 break;
c906108c 3306 default:
e7c27a73 3307 new_symbol (die, NULL, cu);
c906108c
SS
3308 break;
3309 }
3310}
3311
0114d602
DJ
3312/* Return the fully qualified name of DIE, based on its DW_AT_name.
3313 If scope qualifiers are appropriate they will be added. The result
3314 will be allocated on the objfile_obstack, or NULL if the DIE does
3315 not have a name. */
3316
3317static const char *
3318dwarf2_full_name (struct die_info *die, struct dwarf2_cu *cu)
3319{
3320 struct attribute *attr;
3321 char *prefix, *name;
3322 struct ui_file *buf = NULL;
3323
3324 name = dwarf2_name (die, cu);
3325 if (!name)
3326 return NULL;
3327
3328 /* These are the only languages we know how to qualify names in. */
3329 if (cu->language != language_cplus
3330 && cu->language != language_java)
3331 return name;
3332
3333 /* If no prefix is necessary for this type of DIE, return the
3334 unqualified name. The other three tags listed could be handled
3335 in pdi_needs_namespace, but that requires broader changes. */
3336 if (!pdi_needs_namespace (die->tag)
3337 && die->tag != DW_TAG_subprogram
3338 && die->tag != DW_TAG_variable
3339 && die->tag != DW_TAG_member)
3340 return name;
3341
3342 prefix = determine_prefix (die, cu);
3343 if (*prefix != '\0')
3344 name = typename_concat (&cu->objfile->objfile_obstack, prefix,
3345 name, cu);
3346
3347 return name;
3348}
3349
27aa8d6a
SW
3350/* Read the import statement specified by the given die and record it. */
3351
3352static void
3353read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
3354{
3355 struct attribute *import_attr;
3356 struct die_info *imported_die;
3357 const char *imported_name;
794684b6
SW
3358 const char *imported_name_prefix;
3359 const char *import_prefix;
3360 char *canonical_name;
27aa8d6a
SW
3361
3362 import_attr = dwarf2_attr (die, DW_AT_import, cu);
3363 if (import_attr == NULL)
3364 {
3365 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
3366 dwarf_tag_name (die->tag));
3367 return;
3368 }
3369
3370 imported_die = follow_die_ref (die, import_attr, &cu);
3371 imported_name = dwarf2_name (imported_die, cu);
3372 if (imported_name == NULL)
3373 {
3374 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
3375
3376 The import in the following code:
3377 namespace A
3378 {
3379 typedef int B;
3380 }
3381
3382 int main ()
3383 {
3384 using A::B;
3385 B b;
3386 return b;
3387 }
3388
3389 ...
3390 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
3391 <52> DW_AT_decl_file : 1
3392 <53> DW_AT_decl_line : 6
3393 <54> DW_AT_import : <0x75>
3394 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
3395 <59> DW_AT_name : B
3396 <5b> DW_AT_decl_file : 1
3397 <5c> DW_AT_decl_line : 2
3398 <5d> DW_AT_type : <0x6e>
3399 ...
3400 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
3401 <76> DW_AT_byte_size : 4
3402 <77> DW_AT_encoding : 5 (signed)
3403
3404 imports the wrong die ( 0x75 instead of 0x58 ).
3405 This case will be ignored until the gcc bug is fixed. */
3406 return;
3407 }
3408
3409 /* FIXME: dwarf2_name (die); for the local name after import. */
3410
794684b6
SW
3411 /* Figure out where the statement is being imported to. */
3412 import_prefix = determine_prefix (die, cu);
3413
3414 /* Figure out what the scope of the imported die is and prepend it
3415 to the name of the imported die. */
3416 imported_name_prefix = determine_prefix (imported_die, cu);
3417
3418 if (strlen (imported_name_prefix) > 0)
3419 {
3420 canonical_name = alloca (strlen (imported_name_prefix) + 2 + strlen (imported_name) + 1);
3421 strcpy (canonical_name, imported_name_prefix);
3422 strcat (canonical_name, "::");
3423 strcat (canonical_name, imported_name);
3424 }
3425 else
3426 {
3427 canonical_name = alloca (strlen (imported_name) + 1);
3428 strcpy (canonical_name, imported_name);
3429 }
3430
3431 using_directives = cp_add_using (import_prefix,canonical_name, using_directives);
27aa8d6a
SW
3432}
3433
5fb290d7 3434static void
e142c38c 3435initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 3436{
e142c38c 3437 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
3438}
3439
cb1df416
DJ
3440static void
3441free_cu_line_header (void *arg)
3442{
3443 struct dwarf2_cu *cu = arg;
3444
3445 free_line_header (cu->line_header);
3446 cu->line_header = NULL;
3447}
3448
c906108c 3449static void
e7c27a73 3450read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3451{
e7c27a73
DJ
3452 struct objfile *objfile = cu->objfile;
3453 struct comp_unit_head *cu_header = &cu->header;
debd256d 3454 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 3455 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
3456 CORE_ADDR highpc = ((CORE_ADDR) 0);
3457 struct attribute *attr;
e1024ff1 3458 char *name = NULL;
c906108c
SS
3459 char *comp_dir = NULL;
3460 struct die_info *child_die;
3461 bfd *abfd = objfile->obfd;
debd256d 3462 struct line_header *line_header = 0;
e142c38c
DJ
3463 CORE_ADDR baseaddr;
3464
3465 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3466
fae299cd 3467 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
3468
3469 /* If we didn't find a lowpc, set it to highpc to avoid complaints
3470 from finish_block. */
2acceee2 3471 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
3472 lowpc = highpc;
3473 lowpc += baseaddr;
3474 highpc += baseaddr;
3475
39cbfefa
DJ
3476 /* Find the filename. Do not use dwarf2_name here, since the filename
3477 is not a source language identifier. */
e142c38c 3478 attr = dwarf2_attr (die, DW_AT_name, cu);
c906108c
SS
3479 if (attr)
3480 {
3481 name = DW_STRING (attr);
3482 }
e1024ff1 3483
e142c38c 3484 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
c906108c 3485 if (attr)
e1024ff1
DJ
3486 comp_dir = DW_STRING (attr);
3487 else if (name != NULL && IS_ABSOLUTE_PATH (name))
c906108c 3488 {
e1024ff1
DJ
3489 comp_dir = ldirname (name);
3490 if (comp_dir != NULL)
3491 make_cleanup (xfree, comp_dir);
3492 }
3493 if (comp_dir != NULL)
3494 {
3495 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3496 directory, get rid of it. */
3497 char *cp = strchr (comp_dir, ':');
c906108c 3498
e1024ff1
DJ
3499 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3500 comp_dir = cp + 1;
c906108c
SS
3501 }
3502
e1024ff1
DJ
3503 if (name == NULL)
3504 name = "<unknown>";
3505
e142c38c 3506 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
3507 if (attr)
3508 {
e142c38c 3509 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
3510 }
3511
b0f35d58
DL
3512 attr = dwarf2_attr (die, DW_AT_producer, cu);
3513 if (attr)
3514 cu->producer = DW_STRING (attr);
303b6f5d 3515
c906108c
SS
3516 /* We assume that we're processing GCC output. */
3517 processing_gcc_compilation = 2;
c906108c 3518
df8a16a1
DJ
3519 processing_has_namespace_info = 0;
3520
c906108c
SS
3521 start_symtab (name, comp_dir, lowpc);
3522 record_debugformat ("DWARF 2");
303b6f5d 3523 record_producer (cu->producer);
c906108c 3524
e142c38c 3525 initialize_cu_func_list (cu);
c906108c 3526
cb1df416
DJ
3527 /* Decode line number information if present. We do this before
3528 processing child DIEs, so that the line header table is available
3529 for DW_AT_decl_file. */
e142c38c 3530 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
3531 if (attr)
3532 {
debd256d 3533 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 3534 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
3535 if (line_header)
3536 {
cb1df416
DJ
3537 cu->line_header = line_header;
3538 make_cleanup (free_cu_line_header, cu);
aaa75496 3539 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 3540 }
5fb290d7 3541 }
debd256d 3542
cb1df416
DJ
3543 /* Process all dies in compilation unit. */
3544 if (die->child != NULL)
3545 {
3546 child_die = die->child;
3547 while (child_die && child_die->tag)
3548 {
3549 process_die (child_die, cu);
3550 child_die = sibling_die (child_die);
3551 }
3552 }
3553
2e276125
JB
3554 /* Decode macro information, if present. Dwarf 2 macro information
3555 refers to information in the line number info statement program
3556 header, so we can only read it if we've read the header
3557 successfully. */
e142c38c 3558 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 3559 if (attr && line_header)
2e276125
JB
3560 {
3561 unsigned int macro_offset = DW_UNSND (attr);
3562 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 3563 comp_dir, abfd, cu);
2e276125 3564 }
debd256d 3565 do_cleanups (back_to);
5fb290d7
DJ
3566}
3567
348e048f
DE
3568/* For TUs we want to skip the first top level sibling if it's not the
3569 actual type being defined by this TU. In this case the first top
3570 level sibling is there to provide context only. */
3571
3572static void
3573read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
3574{
3575 struct objfile *objfile = cu->objfile;
3576 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3577 CORE_ADDR lowpc;
3578 struct attribute *attr;
3579 char *name = NULL;
3580 char *comp_dir = NULL;
3581 struct die_info *child_die;
3582 bfd *abfd = objfile->obfd;
3583 struct line_header *line_header = 0;
3584
3585 /* start_symtab needs a low pc, but we don't really have one.
3586 Do what read_file_scope would do in the absence of such info. */
3587 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3588
3589 /* Find the filename. Do not use dwarf2_name here, since the filename
3590 is not a source language identifier. */
3591 attr = dwarf2_attr (die, DW_AT_name, cu);
3592 if (attr)
3593 name = DW_STRING (attr);
3594
3595 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
3596 if (attr)
3597 comp_dir = DW_STRING (attr);
3598 else if (name != NULL && IS_ABSOLUTE_PATH (name))
3599 {
3600 comp_dir = ldirname (name);
3601 if (comp_dir != NULL)
3602 make_cleanup (xfree, comp_dir);
3603 }
3604
3605 if (name == NULL)
3606 name = "<unknown>";
3607
3608 attr = dwarf2_attr (die, DW_AT_language, cu);
3609 if (attr)
3610 set_cu_language (DW_UNSND (attr), cu);
3611
3612 /* This isn't technically needed today. It is done for symmetry
3613 with read_file_scope. */
3614 attr = dwarf2_attr (die, DW_AT_producer, cu);
3615 if (attr)
3616 cu->producer = DW_STRING (attr);
3617
3618 /* We assume that we're processing GCC output. */
3619 processing_gcc_compilation = 2;
3620
3621 processing_has_namespace_info = 0;
3622
3623 start_symtab (name, comp_dir, lowpc);
3624 record_debugformat ("DWARF 2");
3625 record_producer (cu->producer);
3626
3627 /* Process the dies in the type unit. */
3628 if (die->child == NULL)
3629 {
3630 dump_die_for_error (die);
3631 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
3632 bfd_get_filename (abfd));
3633 }
3634
3635 child_die = die->child;
3636
3637 while (child_die && child_die->tag)
3638 {
3639 process_die (child_die, cu);
3640
3641 child_die = sibling_die (child_die);
3642 }
3643
3644 do_cleanups (back_to);
3645}
3646
5fb290d7 3647static void
e142c38c
DJ
3648add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
3649 struct dwarf2_cu *cu)
5fb290d7
DJ
3650{
3651 struct function_range *thisfn;
3652
3653 thisfn = (struct function_range *)
7b5a2f43 3654 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
3655 thisfn->name = name;
3656 thisfn->lowpc = lowpc;
3657 thisfn->highpc = highpc;
3658 thisfn->seen_line = 0;
3659 thisfn->next = NULL;
3660
e142c38c
DJ
3661 if (cu->last_fn == NULL)
3662 cu->first_fn = thisfn;
5fb290d7 3663 else
e142c38c 3664 cu->last_fn->next = thisfn;
5fb290d7 3665
e142c38c 3666 cu->last_fn = thisfn;
c906108c
SS
3667}
3668
d389af10
JK
3669/* qsort helper for inherit_abstract_dies. */
3670
3671static int
3672unsigned_int_compar (const void *ap, const void *bp)
3673{
3674 unsigned int a = *(unsigned int *) ap;
3675 unsigned int b = *(unsigned int *) bp;
3676
3677 return (a > b) - (b > a);
3678}
3679
3680/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3681 Inherit only the children of the DW_AT_abstract_origin DIE not being already
3682 referenced by DW_AT_abstract_origin from the children of the current DIE. */
3683
3684static void
3685inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
3686{
3687 struct die_info *child_die;
3688 unsigned die_children_count;
3689 /* CU offsets which were referenced by children of the current DIE. */
3690 unsigned *offsets;
3691 unsigned *offsets_end, *offsetp;
3692 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
3693 struct die_info *origin_die;
3694 /* Iterator of the ORIGIN_DIE children. */
3695 struct die_info *origin_child_die;
3696 struct cleanup *cleanups;
3697 struct attribute *attr;
3698
3699 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
3700 if (!attr)
3701 return;
3702
3703 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
3704 if (die->tag != origin_die->tag
3705 && !(die->tag == DW_TAG_inlined_subroutine
3706 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
3707 complaint (&symfile_complaints,
3708 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
3709 die->offset, origin_die->offset);
3710
3711 child_die = die->child;
3712 die_children_count = 0;
3713 while (child_die && child_die->tag)
3714 {
3715 child_die = sibling_die (child_die);
3716 die_children_count++;
3717 }
3718 offsets = xmalloc (sizeof (*offsets) * die_children_count);
3719 cleanups = make_cleanup (xfree, offsets);
3720
3721 offsets_end = offsets;
3722 child_die = die->child;
3723 while (child_die && child_die->tag)
3724 {
c38f313d
DJ
3725 /* For each CHILD_DIE, find the corresponding child of
3726 ORIGIN_DIE. If there is more than one layer of
3727 DW_AT_abstract_origin, follow them all; there shouldn't be,
3728 but GCC versions at least through 4.4 generate this (GCC PR
3729 40573). */
3730 struct die_info *child_origin_die = child_die;
3731 while (1)
3732 {
3733 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
3734 if (attr == NULL)
3735 break;
3736 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
3737 }
3738
d389af10
JK
3739 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
3740 counterpart may exist. */
c38f313d 3741 if (child_origin_die != child_die)
d389af10 3742 {
edb3359d
DJ
3743 if (child_die->tag != child_origin_die->tag
3744 && !(child_die->tag == DW_TAG_inlined_subroutine
3745 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
3746 complaint (&symfile_complaints,
3747 _("Child DIE 0x%x and its abstract origin 0x%x have "
3748 "different tags"), child_die->offset,
3749 child_origin_die->offset);
c38f313d
DJ
3750 if (child_origin_die->parent != origin_die)
3751 complaint (&symfile_complaints,
3752 _("Child DIE 0x%x and its abstract origin 0x%x have "
3753 "different parents"), child_die->offset,
3754 child_origin_die->offset);
3755 else
3756 *offsets_end++ = child_origin_die->offset;
d389af10
JK
3757 }
3758 child_die = sibling_die (child_die);
3759 }
3760 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
3761 unsigned_int_compar);
3762 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
3763 if (offsetp[-1] == *offsetp)
3764 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
3765 "to DIE 0x%x as their abstract origin"),
3766 die->offset, *offsetp);
3767
3768 offsetp = offsets;
3769 origin_child_die = origin_die->child;
3770 while (origin_child_die && origin_child_die->tag)
3771 {
3772 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
3773 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
3774 offsetp++;
3775 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
3776 {
3777 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
3778 process_die (origin_child_die, cu);
3779 }
3780 origin_child_die = sibling_die (origin_child_die);
3781 }
3782
3783 do_cleanups (cleanups);
3784}
3785
c906108c 3786static void
e7c27a73 3787read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3788{
e7c27a73 3789 struct objfile *objfile = cu->objfile;
52f0bd74 3790 struct context_stack *new;
c906108c
SS
3791 CORE_ADDR lowpc;
3792 CORE_ADDR highpc;
3793 struct die_info *child_die;
edb3359d 3794 struct attribute *attr, *call_line, *call_file;
c906108c 3795 char *name;
e142c38c 3796 CORE_ADDR baseaddr;
801e3a5b 3797 struct block *block;
edb3359d
DJ
3798 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
3799
3800 if (inlined_func)
3801 {
3802 /* If we do not have call site information, we can't show the
3803 caller of this inlined function. That's too confusing, so
3804 only use the scope for local variables. */
3805 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
3806 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
3807 if (call_line == NULL || call_file == NULL)
3808 {
3809 read_lexical_block_scope (die, cu);
3810 return;
3811 }
3812 }
c906108c 3813
e142c38c
DJ
3814 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3815
3816 name = dwarf2_linkage_name (die, cu);
c906108c
SS
3817
3818 /* Ignore functions with missing or empty names and functions with
3819 missing or invalid low and high pc attributes. */
e7c27a73 3820 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3821 return;
3822
3823 lowpc += baseaddr;
3824 highpc += baseaddr;
3825
5fb290d7 3826 /* Record the function range for dwarf_decode_lines. */
e142c38c 3827 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 3828
c906108c 3829 new = push_context (0, lowpc);
f792889a 3830 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 3831
4cecd739
DJ
3832 /* If there is a location expression for DW_AT_frame_base, record
3833 it. */
e142c38c 3834 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 3835 if (attr)
c034e007
AC
3836 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3837 expression is being recorded directly in the function's symbol
3838 and not in a separate frame-base object. I guess this hack is
3839 to avoid adding some sort of frame-base adjunct/annex to the
3840 function's symbol :-(. The problem with doing this is that it
3841 results in a function symbol with a location expression that
3842 has nothing to do with the location of the function, ouch! The
3843 relationship should be: a function's symbol has-a frame base; a
3844 frame-base has-a location expression. */
e7c27a73 3845 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 3846
e142c38c 3847 cu->list_in_scope = &local_symbols;
c906108c 3848
639d11d3 3849 if (die->child != NULL)
c906108c 3850 {
639d11d3 3851 child_die = die->child;
c906108c
SS
3852 while (child_die && child_die->tag)
3853 {
e7c27a73 3854 process_die (child_die, cu);
c906108c
SS
3855 child_die = sibling_die (child_die);
3856 }
3857 }
3858
d389af10
JK
3859 inherit_abstract_dies (die, cu);
3860
c906108c
SS
3861 new = pop_context ();
3862 /* Make a block for the local symbols within. */
801e3a5b
JB
3863 block = finish_block (new->name, &local_symbols, new->old_blocks,
3864 lowpc, highpc, objfile);
3865
df8a16a1
DJ
3866 /* For C++, set the block's scope. */
3867 if (cu->language == language_cplus)
3868 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 3869 determine_prefix (die, cu),
df8a16a1
DJ
3870 processing_has_namespace_info);
3871
801e3a5b
JB
3872 /* If we have address ranges, record them. */
3873 dwarf2_record_block_ranges (die, block, baseaddr, cu);
208d8187
JB
3874
3875 /* In C++, we can have functions nested inside functions (e.g., when
3876 a function declares a class that has methods). This means that
3877 when we finish processing a function scope, we may need to go
3878 back to building a containing block's symbol lists. */
3879 local_symbols = new->locals;
3880 param_symbols = new->params;
27aa8d6a 3881 using_directives = new->using_directives;
208d8187 3882
921e78cf
JB
3883 /* If we've finished processing a top-level function, subsequent
3884 symbols go in the file symbol list. */
3885 if (outermost_context_p ())
e142c38c 3886 cu->list_in_scope = &file_symbols;
c906108c
SS
3887}
3888
3889/* Process all the DIES contained within a lexical block scope. Start
3890 a new scope, process the dies, and then close the scope. */
3891
3892static void
e7c27a73 3893read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3894{
e7c27a73 3895 struct objfile *objfile = cu->objfile;
52f0bd74 3896 struct context_stack *new;
c906108c
SS
3897 CORE_ADDR lowpc, highpc;
3898 struct die_info *child_die;
e142c38c
DJ
3899 CORE_ADDR baseaddr;
3900
3901 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
3902
3903 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
3904 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
3905 as multiple lexical blocks? Handling children in a sane way would
3906 be nasty. Might be easier to properly extend generic blocks to
3907 describe ranges. */
e7c27a73 3908 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3909 return;
3910 lowpc += baseaddr;
3911 highpc += baseaddr;
3912
3913 push_context (0, lowpc);
639d11d3 3914 if (die->child != NULL)
c906108c 3915 {
639d11d3 3916 child_die = die->child;
c906108c
SS
3917 while (child_die && child_die->tag)
3918 {
e7c27a73 3919 process_die (child_die, cu);
c906108c
SS
3920 child_die = sibling_die (child_die);
3921 }
3922 }
3923 new = pop_context ();
3924
3925 if (local_symbols != NULL)
3926 {
801e3a5b
JB
3927 struct block *block
3928 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
3929 highpc, objfile);
3930
3931 /* Note that recording ranges after traversing children, as we
3932 do here, means that recording a parent's ranges entails
3933 walking across all its children's ranges as they appear in
3934 the address map, which is quadratic behavior.
3935
3936 It would be nicer to record the parent's ranges before
3937 traversing its children, simply overriding whatever you find
3938 there. But since we don't even decide whether to create a
3939 block until after we've traversed its children, that's hard
3940 to do. */
3941 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
3942 }
3943 local_symbols = new->locals;
27aa8d6a 3944 using_directives = new->using_directives;
c906108c
SS
3945}
3946
43039443 3947/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
3948 Return 1 if the attributes are present and valid, otherwise, return 0.
3949 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
3950
3951static int
3952dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
3953 CORE_ADDR *high_return, struct dwarf2_cu *cu,
3954 struct partial_symtab *ranges_pst)
43039443
JK
3955{
3956 struct objfile *objfile = cu->objfile;
3957 struct comp_unit_head *cu_header = &cu->header;
3958 bfd *obfd = objfile->obfd;
3959 unsigned int addr_size = cu_header->addr_size;
3960 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3961 /* Base address selection entry. */
3962 CORE_ADDR base;
3963 int found_base;
3964 unsigned int dummy;
3965 gdb_byte *buffer;
3966 CORE_ADDR marker;
3967 int low_set;
3968 CORE_ADDR low = 0;
3969 CORE_ADDR high = 0;
ff013f42 3970 CORE_ADDR baseaddr;
43039443 3971
d00adf39
DE
3972 found_base = cu->base_known;
3973 base = cu->base_address;
43039443 3974
dce234bc 3975 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
3976 {
3977 complaint (&symfile_complaints,
3978 _("Offset %d out of bounds for DW_AT_ranges attribute"),
3979 offset);
3980 return 0;
3981 }
dce234bc 3982 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
3983
3984 /* Read in the largest possible address. */
3985 marker = read_address (obfd, buffer, cu, &dummy);
3986 if ((marker & mask) == mask)
3987 {
3988 /* If we found the largest possible address, then
3989 read the base address. */
3990 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3991 buffer += 2 * addr_size;
3992 offset += 2 * addr_size;
3993 found_base = 1;
3994 }
3995
3996 low_set = 0;
3997
e7030f15 3998 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 3999
43039443
JK
4000 while (1)
4001 {
4002 CORE_ADDR range_beginning, range_end;
4003
4004 range_beginning = read_address (obfd, buffer, cu, &dummy);
4005 buffer += addr_size;
4006 range_end = read_address (obfd, buffer, cu, &dummy);
4007 buffer += addr_size;
4008 offset += 2 * addr_size;
4009
4010 /* An end of list marker is a pair of zero addresses. */
4011 if (range_beginning == 0 && range_end == 0)
4012 /* Found the end of list entry. */
4013 break;
4014
4015 /* Each base address selection entry is a pair of 2 values.
4016 The first is the largest possible address, the second is
4017 the base address. Check for a base address here. */
4018 if ((range_beginning & mask) == mask)
4019 {
4020 /* If we found the largest possible address, then
4021 read the base address. */
4022 base = read_address (obfd, buffer + addr_size, cu, &dummy);
4023 found_base = 1;
4024 continue;
4025 }
4026
4027 if (!found_base)
4028 {
4029 /* We have no valid base address for the ranges
4030 data. */
4031 complaint (&symfile_complaints,
4032 _("Invalid .debug_ranges data (no base address)"));
4033 return 0;
4034 }
4035
4036 range_beginning += base;
4037 range_end += base;
4038
ff013f42
JK
4039 if (ranges_pst != NULL && range_beginning < range_end)
4040 addrmap_set_empty (objfile->psymtabs_addrmap,
4041 range_beginning + baseaddr, range_end - 1 + baseaddr,
4042 ranges_pst);
4043
43039443
JK
4044 /* FIXME: This is recording everything as a low-high
4045 segment of consecutive addresses. We should have a
4046 data structure for discontiguous block ranges
4047 instead. */
4048 if (! low_set)
4049 {
4050 low = range_beginning;
4051 high = range_end;
4052 low_set = 1;
4053 }
4054 else
4055 {
4056 if (range_beginning < low)
4057 low = range_beginning;
4058 if (range_end > high)
4059 high = range_end;
4060 }
4061 }
4062
4063 if (! low_set)
4064 /* If the first entry is an end-of-list marker, the range
4065 describes an empty scope, i.e. no instructions. */
4066 return 0;
4067
4068 if (low_return)
4069 *low_return = low;
4070 if (high_return)
4071 *high_return = high;
4072 return 1;
4073}
4074
af34e669
DJ
4075/* Get low and high pc attributes from a die. Return 1 if the attributes
4076 are present and valid, otherwise, return 0. Return -1 if the range is
4077 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 4078static int
af34e669 4079dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
e7c27a73 4080 CORE_ADDR *highpc, struct dwarf2_cu *cu)
c906108c
SS
4081{
4082 struct attribute *attr;
af34e669
DJ
4083 CORE_ADDR low = 0;
4084 CORE_ADDR high = 0;
4085 int ret = 0;
c906108c 4086
e142c38c 4087 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 4088 if (attr)
af34e669
DJ
4089 {
4090 high = DW_ADDR (attr);
e142c38c 4091 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
4092 if (attr)
4093 low = DW_ADDR (attr);
4094 else
4095 /* Found high w/o low attribute. */
4096 return 0;
4097
4098 /* Found consecutive range of addresses. */
4099 ret = 1;
4100 }
c906108c 4101 else
af34e669 4102 {
e142c38c 4103 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
4104 if (attr != NULL)
4105 {
af34e669 4106 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 4107 .debug_ranges section. */
ff013f42 4108 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, NULL))
af34e669 4109 return 0;
43039443 4110 /* Found discontinuous range of addresses. */
af34e669
DJ
4111 ret = -1;
4112 }
4113 }
c906108c
SS
4114
4115 if (high < low)
4116 return 0;
4117
4118 /* When using the GNU linker, .gnu.linkonce. sections are used to
4119 eliminate duplicate copies of functions and vtables and such.
4120 The linker will arbitrarily choose one and discard the others.
4121 The AT_*_pc values for such functions refer to local labels in
4122 these sections. If the section from that file was discarded, the
4123 labels are not in the output, so the relocs get a value of 0.
4124 If this is a discarded function, mark the pc bounds as invalid,
4125 so that GDB will ignore it. */
72dca2f5 4126 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
4127 return 0;
4128
4129 *lowpc = low;
4130 *highpc = high;
af34e669 4131 return ret;
c906108c
SS
4132}
4133
b084d499
JB
4134/* Assuming that DIE represents a subprogram DIE or a lexical block, get
4135 its low and high PC addresses. Do nothing if these addresses could not
4136 be determined. Otherwise, set LOWPC to the low address if it is smaller,
4137 and HIGHPC to the high address if greater than HIGHPC. */
4138
4139static void
4140dwarf2_get_subprogram_pc_bounds (struct die_info *die,
4141 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4142 struct dwarf2_cu *cu)
4143{
4144 CORE_ADDR low, high;
4145 struct die_info *child = die->child;
4146
4147 if (dwarf2_get_pc_bounds (die, &low, &high, cu))
4148 {
4149 *lowpc = min (*lowpc, low);
4150 *highpc = max (*highpc, high);
4151 }
4152
4153 /* If the language does not allow nested subprograms (either inside
4154 subprograms or lexical blocks), we're done. */
4155 if (cu->language != language_ada)
4156 return;
4157
4158 /* Check all the children of the given DIE. If it contains nested
4159 subprograms, then check their pc bounds. Likewise, we need to
4160 check lexical blocks as well, as they may also contain subprogram
4161 definitions. */
4162 while (child && child->tag)
4163 {
4164 if (child->tag == DW_TAG_subprogram
4165 || child->tag == DW_TAG_lexical_block)
4166 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
4167 child = sibling_die (child);
4168 }
4169}
4170
fae299cd
DC
4171/* Get the low and high pc's represented by the scope DIE, and store
4172 them in *LOWPC and *HIGHPC. If the correct values can't be
4173 determined, set *LOWPC to -1 and *HIGHPC to 0. */
4174
4175static void
4176get_scope_pc_bounds (struct die_info *die,
4177 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4178 struct dwarf2_cu *cu)
4179{
4180 CORE_ADDR best_low = (CORE_ADDR) -1;
4181 CORE_ADDR best_high = (CORE_ADDR) 0;
4182 CORE_ADDR current_low, current_high;
4183
4184 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu))
4185 {
4186 best_low = current_low;
4187 best_high = current_high;
4188 }
4189 else
4190 {
4191 struct die_info *child = die->child;
4192
4193 while (child && child->tag)
4194 {
4195 switch (child->tag) {
4196 case DW_TAG_subprogram:
b084d499 4197 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
4198 break;
4199 case DW_TAG_namespace:
4200 /* FIXME: carlton/2004-01-16: Should we do this for
4201 DW_TAG_class_type/DW_TAG_structure_type, too? I think
4202 that current GCC's always emit the DIEs corresponding
4203 to definitions of methods of classes as children of a
4204 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
4205 the DIEs giving the declarations, which could be
4206 anywhere). But I don't see any reason why the
4207 standards says that they have to be there. */
4208 get_scope_pc_bounds (child, &current_low, &current_high, cu);
4209
4210 if (current_low != ((CORE_ADDR) -1))
4211 {
4212 best_low = min (best_low, current_low);
4213 best_high = max (best_high, current_high);
4214 }
4215 break;
4216 default:
4217 /* Ignore. */
4218 break;
4219 }
4220
4221 child = sibling_die (child);
4222 }
4223 }
4224
4225 *lowpc = best_low;
4226 *highpc = best_high;
4227}
4228
801e3a5b
JB
4229/* Record the address ranges for BLOCK, offset by BASEADDR, as given
4230 in DIE. */
4231static void
4232dwarf2_record_block_ranges (struct die_info *die, struct block *block,
4233 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
4234{
4235 struct attribute *attr;
4236
4237 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
4238 if (attr)
4239 {
4240 CORE_ADDR high = DW_ADDR (attr);
4241 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4242 if (attr)
4243 {
4244 CORE_ADDR low = DW_ADDR (attr);
4245 record_block_range (block, baseaddr + low, baseaddr + high - 1);
4246 }
4247 }
4248
4249 attr = dwarf2_attr (die, DW_AT_ranges, cu);
4250 if (attr)
4251 {
4252 bfd *obfd = cu->objfile->obfd;
4253
4254 /* The value of the DW_AT_ranges attribute is the offset of the
4255 address range list in the .debug_ranges section. */
4256 unsigned long offset = DW_UNSND (attr);
dce234bc 4257 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
4258
4259 /* For some target architectures, but not others, the
4260 read_address function sign-extends the addresses it returns.
4261 To recognize base address selection entries, we need a
4262 mask. */
4263 unsigned int addr_size = cu->header.addr_size;
4264 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4265
4266 /* The base address, to which the next pair is relative. Note
4267 that this 'base' is a DWARF concept: most entries in a range
4268 list are relative, to reduce the number of relocs against the
4269 debugging information. This is separate from this function's
4270 'baseaddr' argument, which GDB uses to relocate debugging
4271 information from a shared library based on the address at
4272 which the library was loaded. */
d00adf39
DE
4273 CORE_ADDR base = cu->base_address;
4274 int base_known = cu->base_known;
801e3a5b 4275
dce234bc 4276 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
4277 {
4278 complaint (&symfile_complaints,
4279 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
4280 offset);
4281 return;
4282 }
4283
4284 for (;;)
4285 {
4286 unsigned int bytes_read;
4287 CORE_ADDR start, end;
4288
4289 start = read_address (obfd, buffer, cu, &bytes_read);
4290 buffer += bytes_read;
4291 end = read_address (obfd, buffer, cu, &bytes_read);
4292 buffer += bytes_read;
4293
4294 /* Did we find the end of the range list? */
4295 if (start == 0 && end == 0)
4296 break;
4297
4298 /* Did we find a base address selection entry? */
4299 else if ((start & base_select_mask) == base_select_mask)
4300 {
4301 base = end;
4302 base_known = 1;
4303 }
4304
4305 /* We found an ordinary address range. */
4306 else
4307 {
4308 if (!base_known)
4309 {
4310 complaint (&symfile_complaints,
4311 _("Invalid .debug_ranges data (no base address)"));
4312 return;
4313 }
4314
4315 record_block_range (block,
4316 baseaddr + base + start,
4317 baseaddr + base + end - 1);
4318 }
4319 }
4320 }
4321}
4322
c906108c
SS
4323/* Add an aggregate field to the field list. */
4324
4325static void
107d2387 4326dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73
DJ
4327 struct dwarf2_cu *cu)
4328{
4329 struct objfile *objfile = cu->objfile;
5e2b427d 4330 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
4331 struct nextfield *new_field;
4332 struct attribute *attr;
4333 struct field *fp;
4334 char *fieldname = "";
4335
4336 /* Allocate a new field list entry and link it in. */
4337 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 4338 make_cleanup (xfree, new_field);
c906108c
SS
4339 memset (new_field, 0, sizeof (struct nextfield));
4340 new_field->next = fip->fields;
4341 fip->fields = new_field;
4342 fip->nfields++;
4343
4344 /* Handle accessibility and virtuality of field.
4345 The default accessibility for members is public, the default
4346 accessibility for inheritance is private. */
4347 if (die->tag != DW_TAG_inheritance)
4348 new_field->accessibility = DW_ACCESS_public;
4349 else
4350 new_field->accessibility = DW_ACCESS_private;
4351 new_field->virtuality = DW_VIRTUALITY_none;
4352
e142c38c 4353 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
4354 if (attr)
4355 new_field->accessibility = DW_UNSND (attr);
4356 if (new_field->accessibility != DW_ACCESS_public)
4357 fip->non_public_fields = 1;
e142c38c 4358 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
4359 if (attr)
4360 new_field->virtuality = DW_UNSND (attr);
4361
4362 fp = &new_field->field;
a9a9bd0f 4363
e142c38c 4364 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 4365 {
a9a9bd0f
DC
4366 /* Data member other than a C++ static data member. */
4367
c906108c 4368 /* Get type of field. */
e7c27a73 4369 fp->type = die_type (die, cu);
c906108c 4370
d6a843b5 4371 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 4372
c906108c 4373 /* Get bit size of field (zero if none). */
e142c38c 4374 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
4375 if (attr)
4376 {
4377 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
4378 }
4379 else
4380 {
4381 FIELD_BITSIZE (*fp) = 0;
4382 }
4383
4384 /* Get bit offset of field. */
e142c38c 4385 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
4386 if (attr)
4387 {
c6a0999f
JB
4388 int byte_offset;
4389
3690dd37
JB
4390 if (attr_form_is_section_offset (attr))
4391 {
4392 dwarf2_complex_location_expr_complaint ();
c6a0999f 4393 byte_offset = 0;
3690dd37
JB
4394 }
4395 else if (attr_form_is_constant (attr))
c6a0999f 4396 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
3690dd37 4397 else
c6a0999f
JB
4398 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
4399
d6a843b5 4400 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 4401 }
e142c38c 4402 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
4403 if (attr)
4404 {
5e2b427d 4405 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
4406 {
4407 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
4408 additional bit offset from the MSB of the containing
4409 anonymous object to the MSB of the field. We don't
4410 have to do anything special since we don't need to
4411 know the size of the anonymous object. */
c906108c
SS
4412 FIELD_BITPOS (*fp) += DW_UNSND (attr);
4413 }
4414 else
4415 {
4416 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
4417 MSB of the anonymous object, subtract off the number of
4418 bits from the MSB of the field to the MSB of the
4419 object, and then subtract off the number of bits of
4420 the field itself. The result is the bit offset of
4421 the LSB of the field. */
c906108c
SS
4422 int anonymous_size;
4423 int bit_offset = DW_UNSND (attr);
4424
e142c38c 4425 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4426 if (attr)
4427 {
4428 /* The size of the anonymous object containing
4429 the bit field is explicit, so use the
4430 indicated size (in bytes). */
4431 anonymous_size = DW_UNSND (attr);
4432 }
4433 else
4434 {
4435 /* The size of the anonymous object containing
4436 the bit field must be inferred from the type
4437 attribute of the data member containing the
4438 bit field. */
4439 anonymous_size = TYPE_LENGTH (fp->type);
4440 }
4441 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
4442 - bit_offset - FIELD_BITSIZE (*fp);
4443 }
4444 }
4445
4446 /* Get name of field. */
39cbfefa
DJ
4447 fieldname = dwarf2_name (die, cu);
4448 if (fieldname == NULL)
4449 fieldname = "";
d8151005
DJ
4450
4451 /* The name is already allocated along with this objfile, so we don't
4452 need to duplicate it for the type. */
4453 fp->name = fieldname;
c906108c
SS
4454
4455 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 4456 pointer or virtual base class pointer) to private. */
e142c38c 4457 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c
SS
4458 {
4459 new_field->accessibility = DW_ACCESS_private;
4460 fip->non_public_fields = 1;
4461 }
4462 }
a9a9bd0f 4463 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 4464 {
a9a9bd0f
DC
4465 /* C++ static member. */
4466
4467 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
4468 is a declaration, but all versions of G++ as of this writing
4469 (so through at least 3.2.1) incorrectly generate
4470 DW_TAG_variable tags. */
4471
c906108c 4472 char *physname;
c906108c 4473
a9a9bd0f 4474 /* Get name of field. */
39cbfefa
DJ
4475 fieldname = dwarf2_name (die, cu);
4476 if (fieldname == NULL)
c906108c
SS
4477 return;
4478
2df3850c 4479 /* Get physical name. */
e142c38c 4480 physname = dwarf2_linkage_name (die, cu);
c906108c 4481
d8151005
DJ
4482 /* The name is already allocated along with this objfile, so we don't
4483 need to duplicate it for the type. */
4484 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 4485 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 4486 FIELD_NAME (*fp) = fieldname;
c906108c
SS
4487 }
4488 else if (die->tag == DW_TAG_inheritance)
4489 {
4490 /* C++ base class field. */
e142c38c 4491 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 4492 if (attr)
d6a843b5
JK
4493 SET_FIELD_BITPOS (*fp, decode_locdesc (DW_BLOCK (attr), cu)
4494 * bits_per_byte);
c906108c 4495 FIELD_BITSIZE (*fp) = 0;
e7c27a73 4496 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
4497 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
4498 fip->nbaseclasses++;
4499 }
4500}
4501
4502/* Create the vector of fields, and attach it to the type. */
4503
4504static void
fba45db2 4505dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 4506 struct dwarf2_cu *cu)
c906108c
SS
4507{
4508 int nfields = fip->nfields;
4509
4510 /* Record the field count, allocate space for the array of fields,
4511 and create blank accessibility bitfields if necessary. */
4512 TYPE_NFIELDS (type) = nfields;
4513 TYPE_FIELDS (type) = (struct field *)
4514 TYPE_ALLOC (type, sizeof (struct field) * nfields);
4515 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
4516
4517 if (fip->non_public_fields)
4518 {
4519 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4520
4521 TYPE_FIELD_PRIVATE_BITS (type) =
4522 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4523 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
4524
4525 TYPE_FIELD_PROTECTED_BITS (type) =
4526 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4527 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
4528
4529 TYPE_FIELD_IGNORE_BITS (type) =
4530 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4531 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
4532 }
4533
4534 /* If the type has baseclasses, allocate and clear a bit vector for
4535 TYPE_FIELD_VIRTUAL_BITS. */
4536 if (fip->nbaseclasses)
4537 {
4538 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 4539 unsigned char *pointer;
c906108c
SS
4540
4541 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
4542 pointer = TYPE_ALLOC (type, num_bytes);
4543 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
4544 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
4545 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
4546 }
4547
4548 /* Copy the saved-up fields into the field vector. Start from the head
4549 of the list, adding to the tail of the field array, so that they end
4550 up in the same order in the array in which they were added to the list. */
4551 while (nfields-- > 0)
4552 {
4553 TYPE_FIELD (type, nfields) = fip->fields->field;
4554 switch (fip->fields->accessibility)
4555 {
c5aa993b
JM
4556 case DW_ACCESS_private:
4557 SET_TYPE_FIELD_PRIVATE (type, nfields);
4558 break;
c906108c 4559
c5aa993b
JM
4560 case DW_ACCESS_protected:
4561 SET_TYPE_FIELD_PROTECTED (type, nfields);
4562 break;
c906108c 4563
c5aa993b
JM
4564 case DW_ACCESS_public:
4565 break;
c906108c 4566
c5aa993b
JM
4567 default:
4568 /* Unknown accessibility. Complain and treat it as public. */
4569 {
e2e0b3e5 4570 complaint (&symfile_complaints, _("unsupported accessibility %d"),
4d3c2250 4571 fip->fields->accessibility);
c5aa993b
JM
4572 }
4573 break;
c906108c
SS
4574 }
4575 if (nfields < fip->nbaseclasses)
4576 {
4577 switch (fip->fields->virtuality)
4578 {
c5aa993b
JM
4579 case DW_VIRTUALITY_virtual:
4580 case DW_VIRTUALITY_pure_virtual:
4581 SET_TYPE_FIELD_VIRTUAL (type, nfields);
4582 break;
c906108c
SS
4583 }
4584 }
4585 fip->fields = fip->fields->next;
4586 }
4587}
4588
c906108c
SS
4589/* Add a member function to the proper fieldlist. */
4590
4591static void
107d2387 4592dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 4593 struct type *type, struct dwarf2_cu *cu)
c906108c 4594{
e7c27a73 4595 struct objfile *objfile = cu->objfile;
c906108c
SS
4596 struct attribute *attr;
4597 struct fnfieldlist *flp;
4598 int i;
4599 struct fn_field *fnp;
4600 char *fieldname;
4601 char *physname;
4602 struct nextfnfield *new_fnfield;
f792889a 4603 struct type *this_type;
c906108c 4604
2df3850c 4605 /* Get name of member function. */
39cbfefa
DJ
4606 fieldname = dwarf2_name (die, cu);
4607 if (fieldname == NULL)
2df3850c 4608 return;
c906108c 4609
2df3850c 4610 /* Get the mangled name. */
e142c38c 4611 physname = dwarf2_linkage_name (die, cu);
c906108c
SS
4612
4613 /* Look up member function name in fieldlist. */
4614 for (i = 0; i < fip->nfnfields; i++)
4615 {
27bfe10e 4616 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
4617 break;
4618 }
4619
4620 /* Create new list element if necessary. */
4621 if (i < fip->nfnfields)
4622 flp = &fip->fnfieldlists[i];
4623 else
4624 {
4625 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
4626 {
4627 fip->fnfieldlists = (struct fnfieldlist *)
4628 xrealloc (fip->fnfieldlists,
4629 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 4630 * sizeof (struct fnfieldlist));
c906108c 4631 if (fip->nfnfields == 0)
c13c43fd 4632 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
4633 }
4634 flp = &fip->fnfieldlists[fip->nfnfields];
4635 flp->name = fieldname;
4636 flp->length = 0;
4637 flp->head = NULL;
4638 fip->nfnfields++;
4639 }
4640
4641 /* Create a new member function field and chain it to the field list
4642 entry. */
4643 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 4644 make_cleanup (xfree, new_fnfield);
c906108c
SS
4645 memset (new_fnfield, 0, sizeof (struct nextfnfield));
4646 new_fnfield->next = flp->head;
4647 flp->head = new_fnfield;
4648 flp->length++;
4649
4650 /* Fill in the member function field info. */
4651 fnp = &new_fnfield->fnfield;
d8151005
DJ
4652 /* The name is already allocated along with this objfile, so we don't
4653 need to duplicate it for the type. */
4654 fnp->physname = physname ? physname : "";
c906108c 4655 fnp->type = alloc_type (objfile);
f792889a
DJ
4656 this_type = read_type_die (die, cu);
4657 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 4658 {
f792889a 4659 int nparams = TYPE_NFIELDS (this_type);
c906108c 4660
f792889a 4661 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
4662 of the method itself (TYPE_CODE_METHOD). */
4663 smash_to_method_type (fnp->type, type,
f792889a
DJ
4664 TYPE_TARGET_TYPE (this_type),
4665 TYPE_FIELDS (this_type),
4666 TYPE_NFIELDS (this_type),
4667 TYPE_VARARGS (this_type));
c906108c
SS
4668
4669 /* Handle static member functions.
c5aa993b
JM
4670 Dwarf2 has no clean way to discern C++ static and non-static
4671 member functions. G++ helps GDB by marking the first
4672 parameter for non-static member functions (which is the
4673 this pointer) as artificial. We obtain this information
4674 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 4675 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
4676 fnp->voffset = VOFFSET_STATIC;
4677 }
4678 else
e2e0b3e5 4679 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 4680 physname);
c906108c
SS
4681
4682 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 4683 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 4684 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
4685
4686 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
4687 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
4688
4689 /* Get accessibility. */
e142c38c 4690 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
4691 if (attr)
4692 {
4693 switch (DW_UNSND (attr))
4694 {
c5aa993b
JM
4695 case DW_ACCESS_private:
4696 fnp->is_private = 1;
4697 break;
4698 case DW_ACCESS_protected:
4699 fnp->is_protected = 1;
4700 break;
c906108c
SS
4701 }
4702 }
4703
b02dede2 4704 /* Check for artificial methods. */
e142c38c 4705 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
4706 if (attr && DW_UNSND (attr) != 0)
4707 fnp->is_artificial = 1;
4708
c906108c 4709 /* Get index in virtual function table if it is a virtual member function. */
e142c38c 4710 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
c906108c 4711 if (attr)
8e19ed76
PS
4712 {
4713 /* Support the .debug_loc offsets */
4714 if (attr_form_is_block (attr))
4715 {
e7c27a73 4716 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8e19ed76 4717 }
3690dd37 4718 else if (attr_form_is_section_offset (attr))
8e19ed76 4719 {
4d3c2250 4720 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4721 }
4722 else
4723 {
4d3c2250
KB
4724 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
4725 fieldname);
8e19ed76
PS
4726 }
4727 }
c906108c
SS
4728}
4729
4730/* Create the vector of member function fields, and attach it to the type. */
4731
4732static void
fba45db2 4733dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 4734 struct dwarf2_cu *cu)
c906108c
SS
4735{
4736 struct fnfieldlist *flp;
4737 int total_length = 0;
4738 int i;
4739
4740 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4741 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
4742 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
4743
4744 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
4745 {
4746 struct nextfnfield *nfp = flp->head;
4747 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
4748 int k;
4749
4750 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
4751 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
4752 fn_flp->fn_fields = (struct fn_field *)
4753 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
4754 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 4755 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
4756
4757 total_length += flp->length;
4758 }
4759
4760 TYPE_NFN_FIELDS (type) = fip->nfnfields;
4761 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
4762}
4763
1168df01
JB
4764/* Returns non-zero if NAME is the name of a vtable member in CU's
4765 language, zero otherwise. */
4766static int
4767is_vtable_name (const char *name, struct dwarf2_cu *cu)
4768{
4769 static const char vptr[] = "_vptr";
987504bb 4770 static const char vtable[] = "vtable";
1168df01 4771
987504bb
JJ
4772 /* Look for the C++ and Java forms of the vtable. */
4773 if ((cu->language == language_java
4774 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
4775 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
4776 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
4777 return 1;
4778
4779 return 0;
4780}
4781
c0dd20ea
DJ
4782/* GCC outputs unnamed structures that are really pointers to member
4783 functions, with the ABI-specified layout. If DIE (from CU) describes
4784 such a structure, set its type, and return nonzero. Otherwise return
61049d3b
DJ
4785 zero.
4786
4787 GCC shouldn't do this; it should just output pointer to member DIEs.
4788 This is GCC PR debug/28767. */
c0dd20ea 4789
f792889a 4790static struct type *
c0dd20ea
DJ
4791quirk_gcc_member_function_pointer (struct die_info *die, struct dwarf2_cu *cu)
4792{
4793 struct objfile *objfile = cu->objfile;
4794 struct type *type;
4795 struct die_info *pfn_die, *delta_die;
4796 struct attribute *pfn_name, *delta_name;
4797 struct type *pfn_type, *domain_type;
4798
4799 /* Check for a structure with no name and two children. */
4800 if (die->tag != DW_TAG_structure_type
4801 || dwarf2_attr (die, DW_AT_name, cu) != NULL
4802 || die->child == NULL
4803 || die->child->sibling == NULL
4804 || (die->child->sibling->sibling != NULL
4805 && die->child->sibling->sibling->tag != DW_TAG_padding))
f792889a 4806 return NULL;
c0dd20ea
DJ
4807
4808 /* Check for __pfn and __delta members. */
4809 pfn_die = die->child;
4810 pfn_name = dwarf2_attr (pfn_die, DW_AT_name, cu);
4811 if (pfn_die->tag != DW_TAG_member
4812 || pfn_name == NULL
4813 || DW_STRING (pfn_name) == NULL
4814 || strcmp ("__pfn", DW_STRING (pfn_name)) != 0)
f792889a 4815 return NULL;
c0dd20ea
DJ
4816
4817 delta_die = pfn_die->sibling;
4818 delta_name = dwarf2_attr (delta_die, DW_AT_name, cu);
4819 if (delta_die->tag != DW_TAG_member
4820 || delta_name == NULL
4821 || DW_STRING (delta_name) == NULL
4822 || strcmp ("__delta", DW_STRING (delta_name)) != 0)
f792889a 4823 return NULL;
c0dd20ea
DJ
4824
4825 /* Find the type of the method. */
4826 pfn_type = die_type (pfn_die, cu);
4827 if (pfn_type == NULL
4828 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
4829 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
f792889a 4830 return NULL;
c0dd20ea
DJ
4831
4832 /* Look for the "this" argument. */
4833 pfn_type = TYPE_TARGET_TYPE (pfn_type);
4834 if (TYPE_NFIELDS (pfn_type) == 0
4835 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
f792889a 4836 return NULL;
c0dd20ea
DJ
4837
4838 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
4839 type = alloc_type (objfile);
4840 smash_to_method_type (type, domain_type, TYPE_TARGET_TYPE (pfn_type),
4841 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
4842 TYPE_VARARGS (pfn_type));
0d5de010 4843 type = lookup_methodptr_type (type);
f792889a 4844 return set_die_type (die, type, cu);
c0dd20ea 4845}
1168df01 4846
c906108c
SS
4847/* Called when we find the DIE that starts a structure or union scope
4848 (definition) to process all dies that define the members of the
4849 structure or union.
4850
4851 NOTE: we need to call struct_type regardless of whether or not the
4852 DIE has an at_name attribute, since it might be an anonymous
4853 structure or union. This gets the type entered into our set of
4854 user defined types.
4855
4856 However, if the structure is incomplete (an opaque struct/union)
4857 then suppress creating a symbol table entry for it since gdb only
4858 wants to find the one with the complete definition. Note that if
4859 it is complete, we just call new_symbol, which does it's own
4860 checking about whether the struct/union is anonymous or not (and
4861 suppresses creating a symbol table entry itself). */
4862
f792889a 4863static struct type *
134d01f1 4864read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4865{
e7c27a73 4866 struct objfile *objfile = cu->objfile;
c906108c
SS
4867 struct type *type;
4868 struct attribute *attr;
39cbfefa 4869 char *name;
0114d602 4870 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c 4871
f792889a
DJ
4872 type = quirk_gcc_member_function_pointer (die, cu);
4873 if (type)
4874 return type;
c906108c 4875
348e048f
DE
4876 /* If the definition of this type lives in .debug_types, read that type.
4877 Don't follow DW_AT_specification though, that will take us back up
4878 the chain and we want to go down. */
4879 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
4880 if (attr)
4881 {
4882 struct dwarf2_cu *type_cu = cu;
4883 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
4884 /* We could just recurse on read_structure_type, but we need to call
4885 get_die_type to ensure only one type for this DIE is created.
4886 This is important, for example, because for c++ classes we need
4887 TYPE_NAME set which is only done by new_symbol. Blech. */
4888 type = read_type_die (type_die, type_cu);
4889 return set_die_type (die, type, cu);
4890 }
4891
c0dd20ea 4892 type = alloc_type (objfile);
c906108c 4893 INIT_CPLUS_SPECIFIC (type);
93311388 4894
39cbfefa
DJ
4895 name = dwarf2_name (die, cu);
4896 if (name != NULL)
c906108c 4897 {
987504bb
JJ
4898 if (cu->language == language_cplus
4899 || cu->language == language_java)
63d06c5c 4900 {
0114d602
DJ
4901 const char *new_prefix = determine_class_name (die, cu);
4902 TYPE_TAG_NAME (type) = (char *) new_prefix;
63d06c5c
DC
4903 }
4904 else
4905 {
d8151005
DJ
4906 /* The name is already allocated along with this objfile, so
4907 we don't need to duplicate it for the type. */
39cbfefa 4908 TYPE_TAG_NAME (type) = name;
63d06c5c 4909 }
c906108c
SS
4910 }
4911
4912 if (die->tag == DW_TAG_structure_type)
4913 {
4914 TYPE_CODE (type) = TYPE_CODE_STRUCT;
4915 }
4916 else if (die->tag == DW_TAG_union_type)
4917 {
4918 TYPE_CODE (type) = TYPE_CODE_UNION;
4919 }
4920 else
4921 {
4922 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
c5aa993b 4923 in gdbtypes.h. */
c906108c
SS
4924 TYPE_CODE (type) = TYPE_CODE_CLASS;
4925 }
4926
e142c38c 4927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4928 if (attr)
4929 {
4930 TYPE_LENGTH (type) = DW_UNSND (attr);
4931 }
4932 else
4933 {
4934 TYPE_LENGTH (type) = 0;
4935 }
4936
876cecd0 4937 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 4938 if (die_is_declaration (die, cu))
876cecd0 4939 TYPE_STUB (type) = 1;
dc718098 4940
c906108c
SS
4941 /* We need to add the type field to the die immediately so we don't
4942 infinitely recurse when dealing with pointers to the structure
4943 type within the structure itself. */
1c379e20 4944 set_die_type (die, type, cu);
c906108c 4945
e142c38c 4946 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
4947 {
4948 struct field_info fi;
4949 struct die_info *child_die;
c906108c
SS
4950
4951 memset (&fi, 0, sizeof (struct field_info));
4952
639d11d3 4953 child_die = die->child;
c906108c
SS
4954
4955 while (child_die && child_die->tag)
4956 {
a9a9bd0f
DC
4957 if (child_die->tag == DW_TAG_member
4958 || child_die->tag == DW_TAG_variable)
c906108c 4959 {
a9a9bd0f
DC
4960 /* NOTE: carlton/2002-11-05: A C++ static data member
4961 should be a DW_TAG_member that is a declaration, but
4962 all versions of G++ as of this writing (so through at
4963 least 3.2.1) incorrectly generate DW_TAG_variable
4964 tags for them instead. */
e7c27a73 4965 dwarf2_add_field (&fi, child_die, cu);
c906108c 4966 }
8713b1b1 4967 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
4968 {
4969 /* C++ member function. */
e7c27a73 4970 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
4971 }
4972 else if (child_die->tag == DW_TAG_inheritance)
4973 {
4974 /* C++ base class field. */
e7c27a73 4975 dwarf2_add_field (&fi, child_die, cu);
c906108c 4976 }
c906108c
SS
4977 child_die = sibling_die (child_die);
4978 }
4979
4980 /* Attach fields and member functions to the type. */
4981 if (fi.nfields)
e7c27a73 4982 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
4983 if (fi.nfnfields)
4984 {
e7c27a73 4985 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 4986
c5aa993b 4987 /* Get the type which refers to the base class (possibly this
c906108c
SS
4988 class itself) which contains the vtable pointer for the current
4989 class from the DW_AT_containing_type attribute. */
4990
e142c38c 4991 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 4992 {
e7c27a73 4993 struct type *t = die_containing_type (die, cu);
c906108c
SS
4994
4995 TYPE_VPTR_BASETYPE (type) = t;
4996 if (type == t)
4997 {
c906108c
SS
4998 int i;
4999
5000 /* Our own class provides vtbl ptr. */
5001 for (i = TYPE_NFIELDS (t) - 1;
5002 i >= TYPE_N_BASECLASSES (t);
5003 --i)
5004 {
5005 char *fieldname = TYPE_FIELD_NAME (t, i);
5006
1168df01 5007 if (is_vtable_name (fieldname, cu))
c906108c
SS
5008 {
5009 TYPE_VPTR_FIELDNO (type) = i;
5010 break;
5011 }
5012 }
5013
5014 /* Complain if virtual function table field not found. */
5015 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 5016 complaint (&symfile_complaints,
e2e0b3e5 5017 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
5018 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
5019 "");
c906108c
SS
5020 }
5021 else
5022 {
5023 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
5024 }
5025 }
f6235d4c
EZ
5026 else if (cu->producer
5027 && strncmp (cu->producer,
5028 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
5029 {
5030 /* The IBM XLC compiler does not provide direct indication
5031 of the containing type, but the vtable pointer is
5032 always named __vfp. */
5033
5034 int i;
5035
5036 for (i = TYPE_NFIELDS (type) - 1;
5037 i >= TYPE_N_BASECLASSES (type);
5038 --i)
5039 {
5040 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
5041 {
5042 TYPE_VPTR_FIELDNO (type) = i;
5043 TYPE_VPTR_BASETYPE (type) = type;
5044 break;
5045 }
5046 }
5047 }
c906108c 5048 }
c906108c 5049 }
63d06c5c 5050
0114d602 5051 do_cleanups (back_to);
f792889a 5052 return type;
c906108c
SS
5053}
5054
134d01f1
DJ
5055static void
5056process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
5057{
5058 struct objfile *objfile = cu->objfile;
90aeadfc 5059 struct die_info *child_die = die->child;
f792889a 5060 struct type *this_type;
c906108c 5061
f792889a
DJ
5062 this_type = get_die_type (die, cu);
5063 if (this_type == NULL)
5064 this_type = read_structure_type (die, cu);
c906108c 5065
90aeadfc
DC
5066 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
5067 snapshots) has been known to create a die giving a declaration
5068 for a class that has, as a child, a die giving a definition for a
5069 nested class. So we have to process our children even if the
5070 current die is a declaration. Normally, of course, a declaration
5071 won't have any children at all. */
134d01f1 5072
90aeadfc
DC
5073 while (child_die != NULL && child_die->tag)
5074 {
5075 if (child_die->tag == DW_TAG_member
5076 || child_die->tag == DW_TAG_variable
5077 || child_die->tag == DW_TAG_inheritance)
134d01f1 5078 {
90aeadfc 5079 /* Do nothing. */
134d01f1 5080 }
90aeadfc
DC
5081 else
5082 process_die (child_die, cu);
134d01f1 5083
90aeadfc 5084 child_die = sibling_die (child_die);
134d01f1
DJ
5085 }
5086
fa4028e9
JB
5087 /* Do not consider external references. According to the DWARF standard,
5088 these DIEs are identified by the fact that they have no byte_size
5089 attribute, and a declaration attribute. */
5090 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
5091 || !die_is_declaration (die, cu))
f792889a 5092 new_symbol (die, this_type, cu);
134d01f1
DJ
5093}
5094
5095/* Given a DW_AT_enumeration_type die, set its type. We do not
5096 complete the type's fields yet, or create any symbols. */
c906108c 5097
f792889a 5098static struct type *
134d01f1 5099read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5100{
e7c27a73 5101 struct objfile *objfile = cu->objfile;
c906108c 5102 struct type *type;
c906108c 5103 struct attribute *attr;
0114d602 5104 const char *name;
134d01f1 5105
348e048f
DE
5106 /* If the definition of this type lives in .debug_types, read that type.
5107 Don't follow DW_AT_specification though, that will take us back up
5108 the chain and we want to go down. */
5109 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
5110 if (attr)
5111 {
5112 struct dwarf2_cu *type_cu = cu;
5113 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
5114 type = read_type_die (type_die, type_cu);
5115 return set_die_type (die, type, cu);
5116 }
5117
c906108c
SS
5118 type = alloc_type (objfile);
5119
5120 TYPE_CODE (type) = TYPE_CODE_ENUM;
0114d602 5121 name = dwarf2_full_name (die, cu);
39cbfefa 5122 if (name != NULL)
0114d602 5123 TYPE_TAG_NAME (type) = (char *) name;
c906108c 5124
e142c38c 5125 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5126 if (attr)
5127 {
5128 TYPE_LENGTH (type) = DW_UNSND (attr);
5129 }
5130 else
5131 {
5132 TYPE_LENGTH (type) = 0;
5133 }
5134
137033e9
JB
5135 /* The enumeration DIE can be incomplete. In Ada, any type can be
5136 declared as private in the package spec, and then defined only
5137 inside the package body. Such types are known as Taft Amendment
5138 Types. When another package uses such a type, an incomplete DIE
5139 may be generated by the compiler. */
02eb380e 5140 if (die_is_declaration (die, cu))
876cecd0 5141 TYPE_STUB (type) = 1;
02eb380e 5142
f792889a 5143 return set_die_type (die, type, cu);
134d01f1
DJ
5144}
5145
8176b9b8 5146/* Determine the name of the type represented by DIE, which should be
0114d602
DJ
5147 a named C++ or Java compound type. Return the name in question,
5148 allocated on the objfile obstack. */
8176b9b8 5149
0114d602 5150static const char *
8176b9b8
DC
5151determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
5152{
0114d602 5153 const char *new_prefix = NULL;
8176b9b8
DC
5154
5155 /* If we don't have namespace debug info, guess the name by trying
5156 to demangle the names of members, just like we did in
72bf9492 5157 guess_structure_name. */
8176b9b8
DC
5158 if (!processing_has_namespace_info)
5159 {
5160 struct die_info *child;
5161
5162 for (child = die->child;
5163 child != NULL && child->tag != 0;
5164 child = sibling_die (child))
5165 {
5166 if (child->tag == DW_TAG_subprogram)
5167 {
0114d602 5168 char *phys_prefix
31c27f77
JJ
5169 = language_class_name_from_physname (cu->language_defn,
5170 dwarf2_linkage_name
8176b9b8
DC
5171 (child, cu));
5172
0114d602
DJ
5173 if (phys_prefix != NULL)
5174 {
5175 new_prefix
5176 = obsavestring (phys_prefix, strlen (phys_prefix),
5177 &cu->objfile->objfile_obstack);
5178 xfree (phys_prefix);
5179 break;
5180 }
8176b9b8
DC
5181 }
5182 }
5183 }
5184
5185 if (new_prefix == NULL)
0114d602 5186 new_prefix = dwarf2_full_name (die, cu);
8176b9b8
DC
5187
5188 return new_prefix;
5189}
5190
134d01f1
DJ
5191/* Given a pointer to a die which begins an enumeration, process all
5192 the dies that define the members of the enumeration, and create the
5193 symbol for the enumeration type.
5194
5195 NOTE: We reverse the order of the element list. */
5196
5197static void
5198process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
5199{
5200 struct objfile *objfile = cu->objfile;
5201 struct die_info *child_die;
5202 struct field *fields;
134d01f1
DJ
5203 struct symbol *sym;
5204 int num_fields;
5205 int unsigned_enum = 1;
39cbfefa 5206 char *name;
f792889a 5207 struct type *this_type;
134d01f1 5208
c906108c
SS
5209 num_fields = 0;
5210 fields = NULL;
f792889a
DJ
5211 this_type = get_die_type (die, cu);
5212 if (this_type == NULL)
5213 this_type = read_enumeration_type (die, cu);
639d11d3 5214 if (die->child != NULL)
c906108c 5215 {
639d11d3 5216 child_die = die->child;
c906108c
SS
5217 while (child_die && child_die->tag)
5218 {
5219 if (child_die->tag != DW_TAG_enumerator)
5220 {
e7c27a73 5221 process_die (child_die, cu);
c906108c
SS
5222 }
5223 else
5224 {
39cbfefa
DJ
5225 name = dwarf2_name (child_die, cu);
5226 if (name)
c906108c 5227 {
f792889a 5228 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
5229 if (SYMBOL_VALUE (sym) < 0)
5230 unsigned_enum = 0;
5231
5232 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
5233 {
5234 fields = (struct field *)
5235 xrealloc (fields,
5236 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 5237 * sizeof (struct field));
c906108c
SS
5238 }
5239
3567439c 5240 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 5241 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 5242 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
5243 FIELD_BITSIZE (fields[num_fields]) = 0;
5244
5245 num_fields++;
5246 }
5247 }
5248
5249 child_die = sibling_die (child_die);
5250 }
5251
5252 if (num_fields)
5253 {
f792889a
DJ
5254 TYPE_NFIELDS (this_type) = num_fields;
5255 TYPE_FIELDS (this_type) = (struct field *)
5256 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
5257 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 5258 sizeof (struct field) * num_fields);
b8c9b27d 5259 xfree (fields);
c906108c
SS
5260 }
5261 if (unsigned_enum)
876cecd0 5262 TYPE_UNSIGNED (this_type) = 1;
c906108c 5263 }
134d01f1 5264
f792889a 5265 new_symbol (die, this_type, cu);
c906108c
SS
5266}
5267
5268/* Extract all information from a DW_TAG_array_type DIE and put it in
5269 the DIE's type field. For now, this only handles one dimensional
5270 arrays. */
5271
f792889a 5272static struct type *
e7c27a73 5273read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5274{
e7c27a73 5275 struct objfile *objfile = cu->objfile;
c906108c
SS
5276 struct die_info *child_die;
5277 struct type *type = NULL;
5278 struct type *element_type, *range_type, *index_type;
5279 struct type **range_types = NULL;
5280 struct attribute *attr;
5281 int ndim = 0;
5282 struct cleanup *back_to;
39cbfefa 5283 char *name;
c906108c 5284
e7c27a73 5285 element_type = die_type (die, cu);
c906108c
SS
5286
5287 /* Irix 6.2 native cc creates array types without children for
5288 arrays with unspecified length. */
639d11d3 5289 if (die->child == NULL)
c906108c 5290 {
46bf5051 5291 index_type = objfile_type (objfile)->builtin_int;
c906108c 5292 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
5293 type = create_array_type (NULL, element_type, range_type);
5294 return set_die_type (die, type, cu);
c906108c
SS
5295 }
5296
5297 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 5298 child_die = die->child;
c906108c
SS
5299 while (child_die && child_die->tag)
5300 {
5301 if (child_die->tag == DW_TAG_subrange_type)
5302 {
f792889a
DJ
5303 struct type *child_type = read_type_die (child_die, cu);
5304 if (child_type != NULL)
a02abb62
JB
5305 {
5306 /* The range type was succesfully read. Save it for
5307 the array type creation. */
5308 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
5309 {
5310 range_types = (struct type **)
5311 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
5312 * sizeof (struct type *));
5313 if (ndim == 0)
5314 make_cleanup (free_current_contents, &range_types);
5315 }
f792889a 5316 range_types[ndim++] = child_type;
a02abb62 5317 }
c906108c
SS
5318 }
5319 child_die = sibling_die (child_die);
5320 }
5321
5322 /* Dwarf2 dimensions are output from left to right, create the
5323 necessary array types in backwards order. */
7ca2d3a3 5324
c906108c 5325 type = element_type;
7ca2d3a3
DL
5326
5327 if (read_array_order (die, cu) == DW_ORD_col_major)
5328 {
5329 int i = 0;
5330 while (i < ndim)
5331 type = create_array_type (NULL, type, range_types[i++]);
5332 }
5333 else
5334 {
5335 while (ndim-- > 0)
5336 type = create_array_type (NULL, type, range_types[ndim]);
5337 }
c906108c 5338
f5f8a009
EZ
5339 /* Understand Dwarf2 support for vector types (like they occur on
5340 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
5341 array type. This is not part of the Dwarf2/3 standard yet, but a
5342 custom vendor extension. The main difference between a regular
5343 array and the vector variant is that vectors are passed by value
5344 to functions. */
e142c38c 5345 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 5346 if (attr)
ea37ba09 5347 make_vector_type (type);
f5f8a009 5348
39cbfefa
DJ
5349 name = dwarf2_name (die, cu);
5350 if (name)
5351 TYPE_NAME (type) = name;
714e295e 5352
c906108c
SS
5353 do_cleanups (back_to);
5354
5355 /* Install the type in the die. */
f792889a 5356 return set_die_type (die, type, cu);
c906108c
SS
5357}
5358
7ca2d3a3
DL
5359static enum dwarf_array_dim_ordering
5360read_array_order (struct die_info *die, struct dwarf2_cu *cu)
5361{
5362 struct attribute *attr;
5363
5364 attr = dwarf2_attr (die, DW_AT_ordering, cu);
5365
5366 if (attr) return DW_SND (attr);
5367
5368 /*
5369 GNU F77 is a special case, as at 08/2004 array type info is the
5370 opposite order to the dwarf2 specification, but data is still
5371 laid out as per normal fortran.
5372
5373 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
5374 version checking.
5375 */
5376
5377 if (cu->language == language_fortran &&
5378 cu->producer && strstr (cu->producer, "GNU F77"))
5379 {
5380 return DW_ORD_row_major;
5381 }
5382
5383 switch (cu->language_defn->la_array_ordering)
5384 {
5385 case array_column_major:
5386 return DW_ORD_col_major;
5387 case array_row_major:
5388 default:
5389 return DW_ORD_row_major;
5390 };
5391}
5392
72019c9c
GM
5393/* Extract all information from a DW_TAG_set_type DIE and put it in
5394 the DIE's type field. */
5395
f792889a 5396static struct type *
72019c9c
GM
5397read_set_type (struct die_info *die, struct dwarf2_cu *cu)
5398{
f792889a
DJ
5399 struct type *set_type = create_set_type (NULL, die_type (die, cu));
5400
5401 return set_die_type (die, set_type, cu);
72019c9c 5402}
7ca2d3a3 5403
c906108c
SS
5404/* First cut: install each common block member as a global variable. */
5405
5406static void
e7c27a73 5407read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5408{
5409 struct die_info *child_die;
5410 struct attribute *attr;
5411 struct symbol *sym;
5412 CORE_ADDR base = (CORE_ADDR) 0;
5413
e142c38c 5414 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
5415 if (attr)
5416 {
8e19ed76
PS
5417 /* Support the .debug_loc offsets */
5418 if (attr_form_is_block (attr))
5419 {
e7c27a73 5420 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 5421 }
3690dd37 5422 else if (attr_form_is_section_offset (attr))
8e19ed76 5423 {
4d3c2250 5424 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
5425 }
5426 else
5427 {
4d3c2250
KB
5428 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5429 "common block member");
8e19ed76 5430 }
c906108c 5431 }
639d11d3 5432 if (die->child != NULL)
c906108c 5433 {
639d11d3 5434 child_die = die->child;
c906108c
SS
5435 while (child_die && child_die->tag)
5436 {
e7c27a73 5437 sym = new_symbol (child_die, NULL, cu);
e142c38c 5438 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
5439 if (attr)
5440 {
5441 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 5442 base + decode_locdesc (DW_BLOCK (attr), cu);
c906108c
SS
5443 add_symbol_to_list (sym, &global_symbols);
5444 }
5445 child_die = sibling_die (child_die);
5446 }
5447 }
5448}
5449
0114d602 5450/* Create a type for a C++ namespace. */
d9fa45fe 5451
0114d602
DJ
5452static struct type *
5453read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 5454{
e7c27a73 5455 struct objfile *objfile = cu->objfile;
0114d602 5456 const char *previous_prefix, *name;
9219021c 5457 int is_anonymous;
0114d602
DJ
5458 struct type *type;
5459
5460 /* For extensions, reuse the type of the original namespace. */
5461 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
5462 {
5463 struct die_info *ext_die;
5464 struct dwarf2_cu *ext_cu = cu;
5465 ext_die = dwarf2_extension (die, &ext_cu);
5466 type = read_type_die (ext_die, ext_cu);
5467 return set_die_type (die, type, cu);
5468 }
9219021c 5469
e142c38c 5470 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
5471
5472 /* Now build the name of the current namespace. */
5473
0114d602
DJ
5474 previous_prefix = determine_prefix (die, cu);
5475 if (previous_prefix[0] != '\0')
5476 name = typename_concat (&objfile->objfile_obstack,
5477 previous_prefix, name, cu);
5478
5479 /* Create the type. */
5480 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
5481 objfile);
5482 TYPE_NAME (type) = (char *) name;
5483 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5484
5485 set_die_type (die, type, cu);
5486
5487 return type;
5488}
5489
5490/* Read a C++ namespace. */
5491
5492static void
5493read_namespace (struct die_info *die, struct dwarf2_cu *cu)
5494{
5495 struct objfile *objfile = cu->objfile;
5496 const char *name;
5497 int is_anonymous;
9219021c 5498
5c4e30ca
DC
5499 /* Add a symbol associated to this if we haven't seen the namespace
5500 before. Also, add a using directive if it's an anonymous
5501 namespace. */
9219021c 5502
f2f0e013 5503 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
5504 {
5505 struct type *type;
5506
0114d602 5507 type = read_type_die (die, cu);
e7c27a73 5508 new_symbol (die, type, cu);
5c4e30ca 5509
0114d602 5510 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 5511 if (is_anonymous)
0114d602
DJ
5512 {
5513 const char *previous_prefix = determine_prefix (die, cu);
794684b6 5514 cp_add_using_directive (previous_prefix, TYPE_NAME (type));
0114d602 5515 }
5c4e30ca 5516 }
9219021c 5517
639d11d3 5518 if (die->child != NULL)
d9fa45fe 5519 {
639d11d3 5520 struct die_info *child_die = die->child;
d9fa45fe
DC
5521
5522 while (child_die && child_die->tag)
5523 {
e7c27a73 5524 process_die (child_die, cu);
d9fa45fe
DC
5525 child_die = sibling_die (child_die);
5526 }
5527 }
38d518c9
EZ
5528}
5529
5d7cb8df
JK
5530/* Read a Fortran module. */
5531
5532static void
5533read_module (struct die_info *die, struct dwarf2_cu *cu)
5534{
5535 struct die_info *child_die = die->child;
5536
5537 /* FIXME: Support the separate Fortran module namespaces. */
5538
5539 while (child_die && child_die->tag)
5540 {
5541 process_die (child_die, cu);
5542 child_die = sibling_die (child_die);
5543 }
5544}
5545
38d518c9
EZ
5546/* Return the name of the namespace represented by DIE. Set
5547 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
5548 namespace. */
5549
5550static const char *
e142c38c 5551namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
5552{
5553 struct die_info *current_die;
5554 const char *name = NULL;
5555
5556 /* Loop through the extensions until we find a name. */
5557
5558 for (current_die = die;
5559 current_die != NULL;
f2f0e013 5560 current_die = dwarf2_extension (die, &cu))
38d518c9 5561 {
e142c38c 5562 name = dwarf2_name (current_die, cu);
38d518c9
EZ
5563 if (name != NULL)
5564 break;
5565 }
5566
5567 /* Is it an anonymous namespace? */
5568
5569 *is_anonymous = (name == NULL);
5570 if (*is_anonymous)
5571 name = "(anonymous namespace)";
5572
5573 return name;
d9fa45fe
DC
5574}
5575
c906108c
SS
5576/* Extract all information from a DW_TAG_pointer_type DIE and add to
5577 the user defined type vector. */
5578
f792889a 5579static struct type *
e7c27a73 5580read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5581{
5e2b427d 5582 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 5583 struct comp_unit_head *cu_header = &cu->header;
c906108c 5584 struct type *type;
8b2dbe47
KB
5585 struct attribute *attr_byte_size;
5586 struct attribute *attr_address_class;
5587 int byte_size, addr_class;
c906108c 5588
e7c27a73 5589 type = lookup_pointer_type (die_type (die, cu));
8b2dbe47 5590
e142c38c 5591 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
5592 if (attr_byte_size)
5593 byte_size = DW_UNSND (attr_byte_size);
c906108c 5594 else
8b2dbe47
KB
5595 byte_size = cu_header->addr_size;
5596
e142c38c 5597 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
5598 if (attr_address_class)
5599 addr_class = DW_UNSND (attr_address_class);
5600 else
5601 addr_class = DW_ADDR_none;
5602
5603 /* If the pointer size or address class is different than the
5604 default, create a type variant marked as such and set the
5605 length accordingly. */
5606 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 5607 {
5e2b427d 5608 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
5609 {
5610 int type_flags;
5611
849957d9 5612 type_flags = gdbarch_address_class_type_flags
5e2b427d 5613 (gdbarch, byte_size, addr_class);
876cecd0
TT
5614 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5615 == 0);
8b2dbe47
KB
5616 type = make_type_with_address_space (type, type_flags);
5617 }
5618 else if (TYPE_LENGTH (type) != byte_size)
5619 {
e2e0b3e5 5620 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47
KB
5621 }
5622 else {
5623 /* Should we also complain about unhandled address classes? */
5624 }
c906108c 5625 }
8b2dbe47
KB
5626
5627 TYPE_LENGTH (type) = byte_size;
f792889a 5628 return set_die_type (die, type, cu);
c906108c
SS
5629}
5630
5631/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
5632 the user defined type vector. */
5633
f792889a 5634static struct type *
e7c27a73 5635read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5636{
e7c27a73 5637 struct objfile *objfile = cu->objfile;
c906108c
SS
5638 struct type *type;
5639 struct type *to_type;
5640 struct type *domain;
5641
e7c27a73
DJ
5642 to_type = die_type (die, cu);
5643 domain = die_containing_type (die, cu);
0d5de010
DJ
5644
5645 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
5646 type = lookup_methodptr_type (to_type);
5647 else
5648 type = lookup_memberptr_type (to_type, domain);
c906108c 5649
f792889a 5650 return set_die_type (die, type, cu);
c906108c
SS
5651}
5652
5653/* Extract all information from a DW_TAG_reference_type DIE and add to
5654 the user defined type vector. */
5655
f792889a 5656static struct type *
e7c27a73 5657read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5658{
e7c27a73 5659 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
5660 struct type *type;
5661 struct attribute *attr;
5662
e7c27a73 5663 type = lookup_reference_type (die_type (die, cu));
e142c38c 5664 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5665 if (attr)
5666 {
5667 TYPE_LENGTH (type) = DW_UNSND (attr);
5668 }
5669 else
5670 {
107d2387 5671 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 5672 }
f792889a 5673 return set_die_type (die, type, cu);
c906108c
SS
5674}
5675
f792889a 5676static struct type *
e7c27a73 5677read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5678{
f792889a 5679 struct type *base_type, *cv_type;
c906108c 5680
e7c27a73 5681 base_type = die_type (die, cu);
f792889a
DJ
5682 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
5683 return set_die_type (die, cv_type, cu);
c906108c
SS
5684}
5685
f792889a 5686static struct type *
e7c27a73 5687read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5688{
f792889a 5689 struct type *base_type, *cv_type;
c906108c 5690
e7c27a73 5691 base_type = die_type (die, cu);
f792889a
DJ
5692 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
5693 return set_die_type (die, cv_type, cu);
c906108c
SS
5694}
5695
5696/* Extract all information from a DW_TAG_string_type DIE and add to
5697 the user defined type vector. It isn't really a user defined type,
5698 but it behaves like one, with other DIE's using an AT_user_def_type
5699 attribute to reference it. */
5700
f792889a 5701static struct type *
e7c27a73 5702read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5703{
e7c27a73 5704 struct objfile *objfile = cu->objfile;
3b7538c0 5705 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5706 struct type *type, *range_type, *index_type, *char_type;
5707 struct attribute *attr;
5708 unsigned int length;
5709
e142c38c 5710 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
5711 if (attr)
5712 {
5713 length = DW_UNSND (attr);
5714 }
5715 else
5716 {
b21b22e0 5717 /* check for the DW_AT_byte_size attribute */
e142c38c 5718 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
5719 if (attr)
5720 {
5721 length = DW_UNSND (attr);
5722 }
5723 else
5724 {
5725 length = 1;
5726 }
c906108c 5727 }
6ccb9162 5728
46bf5051 5729 index_type = objfile_type (objfile)->builtin_int;
c906108c 5730 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
5731 char_type = language_string_char_type (cu->language_defn, gdbarch);
5732 type = create_string_type (NULL, char_type, range_type);
6ccb9162 5733
f792889a 5734 return set_die_type (die, type, cu);
c906108c
SS
5735}
5736
5737/* Handle DIES due to C code like:
5738
5739 struct foo
c5aa993b
JM
5740 {
5741 int (*funcp)(int a, long l);
5742 int b;
5743 };
c906108c
SS
5744
5745 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 5746 */
c906108c 5747
f792889a 5748static struct type *
e7c27a73 5749read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5750{
5751 struct type *type; /* Type that this function returns */
5752 struct type *ftype; /* Function that returns above type */
5753 struct attribute *attr;
5754
e7c27a73 5755 type = die_type (die, cu);
0c8b41f1 5756 ftype = lookup_function_type (type);
c906108c 5757
5b8101ae 5758 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 5759 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 5760 if ((attr && (DW_UNSND (attr) != 0))
987504bb 5761 || cu->language == language_cplus
5b8101ae
PM
5762 || cu->language == language_java
5763 || cu->language == language_pascal)
876cecd0 5764 TYPE_PROTOTYPED (ftype) = 1;
c906108c 5765
c055b101
CV
5766 /* Store the calling convention in the type if it's available in
5767 the subroutine die. Otherwise set the calling convention to
5768 the default value DW_CC_normal. */
5769 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
5770 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
5771
639d11d3 5772 if (die->child != NULL)
c906108c
SS
5773 {
5774 struct die_info *child_die;
5775 int nparams = 0;
5776 int iparams = 0;
5777
5778 /* Count the number of parameters.
5779 FIXME: GDB currently ignores vararg functions, but knows about
5780 vararg member functions. */
639d11d3 5781 child_die = die->child;
c906108c
SS
5782 while (child_die && child_die->tag)
5783 {
5784 if (child_die->tag == DW_TAG_formal_parameter)
5785 nparams++;
5786 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 5787 TYPE_VARARGS (ftype) = 1;
c906108c
SS
5788 child_die = sibling_die (child_die);
5789 }
5790
5791 /* Allocate storage for parameters and fill them in. */
5792 TYPE_NFIELDS (ftype) = nparams;
5793 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 5794 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 5795
639d11d3 5796 child_die = die->child;
c906108c
SS
5797 while (child_die && child_die->tag)
5798 {
5799 if (child_die->tag == DW_TAG_formal_parameter)
5800 {
5801 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
5802 member functions. G++ helps GDB by marking the first
5803 parameter for non-static member functions (which is the
5804 this pointer) as artificial. We pass this information
5805 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 5806 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
5807 if (attr)
5808 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
5809 else
5810 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
e7c27a73 5811 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
5812 iparams++;
5813 }
5814 child_die = sibling_die (child_die);
5815 }
5816 }
5817
f792889a 5818 return set_die_type (die, ftype, cu);
c906108c
SS
5819}
5820
f792889a 5821static struct type *
e7c27a73 5822read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5823{
e7c27a73 5824 struct objfile *objfile = cu->objfile;
2f038fcb 5825 struct attribute *attr;
0114d602 5826 const char *name = NULL;
f792889a 5827 struct type *this_type;
c906108c 5828
0114d602 5829 name = dwarf2_full_name (die, cu);
f792889a 5830 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
5831 TYPE_FLAG_TARGET_STUB, NULL, objfile);
5832 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
5833 set_die_type (die, this_type, cu);
5834 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
5835 return this_type;
c906108c
SS
5836}
5837
5838/* Find a representation of a given base type and install
5839 it in the TYPE field of the die. */
5840
f792889a 5841static struct type *
e7c27a73 5842read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5843{
e7c27a73 5844 struct objfile *objfile = cu->objfile;
c906108c
SS
5845 struct type *type;
5846 struct attribute *attr;
5847 int encoding = 0, size = 0;
39cbfefa 5848 char *name;
6ccb9162
UW
5849 enum type_code code = TYPE_CODE_INT;
5850 int type_flags = 0;
5851 struct type *target_type = NULL;
c906108c 5852
e142c38c 5853 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
5854 if (attr)
5855 {
5856 encoding = DW_UNSND (attr);
5857 }
e142c38c 5858 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5859 if (attr)
5860 {
5861 size = DW_UNSND (attr);
5862 }
39cbfefa 5863 name = dwarf2_name (die, cu);
6ccb9162 5864 if (!name)
c906108c 5865 {
6ccb9162
UW
5866 complaint (&symfile_complaints,
5867 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 5868 }
6ccb9162
UW
5869
5870 switch (encoding)
c906108c 5871 {
6ccb9162
UW
5872 case DW_ATE_address:
5873 /* Turn DW_ATE_address into a void * pointer. */
5874 code = TYPE_CODE_PTR;
5875 type_flags |= TYPE_FLAG_UNSIGNED;
5876 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
5877 break;
5878 case DW_ATE_boolean:
5879 code = TYPE_CODE_BOOL;
5880 type_flags |= TYPE_FLAG_UNSIGNED;
5881 break;
5882 case DW_ATE_complex_float:
5883 code = TYPE_CODE_COMPLEX;
5884 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
5885 break;
5886 case DW_ATE_decimal_float:
5887 code = TYPE_CODE_DECFLOAT;
5888 break;
5889 case DW_ATE_float:
5890 code = TYPE_CODE_FLT;
5891 break;
5892 case DW_ATE_signed:
5893 break;
5894 case DW_ATE_unsigned:
5895 type_flags |= TYPE_FLAG_UNSIGNED;
5896 break;
5897 case DW_ATE_signed_char:
868a0084
PM
5898 if (cu->language == language_ada || cu->language == language_m2
5899 || cu->language == language_pascal)
6ccb9162
UW
5900 code = TYPE_CODE_CHAR;
5901 break;
5902 case DW_ATE_unsigned_char:
868a0084
PM
5903 if (cu->language == language_ada || cu->language == language_m2
5904 || cu->language == language_pascal)
6ccb9162
UW
5905 code = TYPE_CODE_CHAR;
5906 type_flags |= TYPE_FLAG_UNSIGNED;
5907 break;
5908 default:
5909 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
5910 dwarf_type_encoding_name (encoding));
5911 break;
c906108c 5912 }
6ccb9162 5913
0114d602
DJ
5914 type = init_type (code, size, type_flags, NULL, objfile);
5915 TYPE_NAME (type) = name;
6ccb9162
UW
5916 TYPE_TARGET_TYPE (type) = target_type;
5917
0114d602 5918 if (name && strcmp (name, "char") == 0)
876cecd0 5919 TYPE_NOSIGN (type) = 1;
0114d602 5920
f792889a 5921 return set_die_type (die, type, cu);
c906108c
SS
5922}
5923
a02abb62
JB
5924/* Read the given DW_AT_subrange DIE. */
5925
f792889a 5926static struct type *
a02abb62
JB
5927read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
5928{
5e2b427d 5929 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
5930 struct type *base_type;
5931 struct type *range_type;
5932 struct attribute *attr;
5933 int low = 0;
5934 int high = -1;
39cbfefa 5935 char *name;
a02abb62 5936
a02abb62 5937 base_type = die_type (die, cu);
3d1f72c2 5938 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
a02abb62
JB
5939 {
5940 complaint (&symfile_complaints,
e2e0b3e5 5941 _("DW_AT_type missing from DW_TAG_subrange_type"));
17a912b6 5942 base_type
5e2b427d 5943 = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6ccb9162 5944 0, NULL, cu->objfile);
a02abb62
JB
5945 }
5946
e142c38c 5947 if (cu->language == language_fortran)
a02abb62
JB
5948 {
5949 /* FORTRAN implies a lower bound of 1, if not given. */
5950 low = 1;
5951 }
5952
dd5e6932
DJ
5953 /* FIXME: For variable sized arrays either of these could be
5954 a variable rather than a constant value. We'll allow it,
5955 but we don't know how to handle it. */
e142c38c 5956 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
5957 if (attr)
5958 low = dwarf2_get_attr_constant_value (attr, 0);
5959
e142c38c 5960 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62
JB
5961 if (attr)
5962 {
5963 if (attr->form == DW_FORM_block1)
5964 {
5965 /* GCC encodes arrays with unspecified or dynamic length
5966 with a DW_FORM_block1 attribute.
5967 FIXME: GDB does not yet know how to handle dynamic
5968 arrays properly, treat them as arrays with unspecified
5969 length for now.
5970
5971 FIXME: jimb/2003-09-22: GDB does not really know
5972 how to handle arrays of unspecified length
5973 either; we just represent them as zero-length
5974 arrays. Choose an appropriate upper bound given
5975 the lower bound we've computed above. */
5976 high = low - 1;
5977 }
5978 else
5979 high = dwarf2_get_attr_constant_value (attr, 1);
5980 }
5981
5982 range_type = create_range_type (NULL, base_type, low, high);
5983
39cbfefa
DJ
5984 name = dwarf2_name (die, cu);
5985 if (name)
5986 TYPE_NAME (range_type) = name;
a02abb62 5987
e142c38c 5988 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
5989 if (attr)
5990 TYPE_LENGTH (range_type) = DW_UNSND (attr);
5991
f792889a 5992 return set_die_type (die, range_type, cu);
a02abb62
JB
5993}
5994
f792889a 5995static struct type *
81a17f79
JB
5996read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
5997{
5998 struct type *type;
81a17f79 5999
81a17f79
JB
6000 /* For now, we only support the C meaning of an unspecified type: void. */
6001
0114d602
DJ
6002 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
6003 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 6004
f792889a 6005 return set_die_type (die, type, cu);
81a17f79 6006}
a02abb62 6007
51545339
DJ
6008/* Trivial hash function for die_info: the hash value of a DIE
6009 is its offset in .debug_info for this objfile. */
6010
6011static hashval_t
6012die_hash (const void *item)
6013{
6014 const struct die_info *die = item;
6015 return die->offset;
6016}
6017
6018/* Trivial comparison function for die_info structures: two DIEs
6019 are equal if they have the same offset. */
6020
6021static int
6022die_eq (const void *item_lhs, const void *item_rhs)
6023{
6024 const struct die_info *die_lhs = item_lhs;
6025 const struct die_info *die_rhs = item_rhs;
6026 return die_lhs->offset == die_rhs->offset;
6027}
6028
93311388
DE
6029/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6030
6031static void
6032init_cu_die_reader (struct die_reader_specs *reader,
6033 struct dwarf2_cu *cu)
6034{
6035 reader->abfd = cu->objfile->obfd;
6036 reader->cu = cu;
348e048f
DE
6037 if (cu->per_cu->from_debug_types)
6038 reader->buffer = dwarf2_per_objfile->types.buffer;
6039 else
6040 reader->buffer = dwarf2_per_objfile->info.buffer;
93311388
DE
6041}
6042
c906108c
SS
6043/* Read a whole compilation unit into a linked list of dies. */
6044
f9aca02d 6045static struct die_info *
93311388 6046read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 6047{
93311388
DE
6048 struct die_reader_specs reader_specs;
6049
348e048f 6050 gdb_assert (cu->die_hash == NULL);
51545339
DJ
6051 cu->die_hash
6052 = htab_create_alloc_ex (cu->header.length / 12,
6053 die_hash,
6054 die_eq,
6055 NULL,
6056 &cu->comp_unit_obstack,
6057 hashtab_obstack_allocate,
6058 dummy_obstack_deallocate);
6059
93311388
DE
6060 init_cu_die_reader (&reader_specs, cu);
6061
6062 return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
639d11d3
DC
6063}
6064
d97bc12b
DE
6065/* Main entry point for reading a DIE and all children.
6066 Read the DIE and dump it if requested. */
6067
6068static struct die_info *
93311388
DE
6069read_die_and_children (const struct die_reader_specs *reader,
6070 gdb_byte *info_ptr,
d97bc12b
DE
6071 gdb_byte **new_info_ptr,
6072 struct die_info *parent)
6073{
93311388 6074 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
6075 new_info_ptr, parent);
6076
6077 if (dwarf2_die_debug)
6078 {
348e048f
DE
6079 fprintf_unfiltered (gdb_stdlog,
6080 "\nRead die from %s of %s:\n",
6081 reader->buffer == dwarf2_per_objfile->info.buffer
6082 ? ".debug_info"
6083 : reader->buffer == dwarf2_per_objfile->types.buffer
6084 ? ".debug_types"
6085 : "unknown section",
6086 reader->abfd->filename);
d97bc12b
DE
6087 dump_die (result, dwarf2_die_debug);
6088 }
6089
6090 return result;
6091}
6092
639d11d3
DC
6093/* Read a single die and all its descendents. Set the die's sibling
6094 field to NULL; set other fields in the die correctly, and set all
6095 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
6096 location of the info_ptr after reading all of those dies. PARENT
6097 is the parent of the die in question. */
6098
6099static struct die_info *
93311388
DE
6100read_die_and_children_1 (const struct die_reader_specs *reader,
6101 gdb_byte *info_ptr,
d97bc12b
DE
6102 gdb_byte **new_info_ptr,
6103 struct die_info *parent)
639d11d3
DC
6104{
6105 struct die_info *die;
fe1b8b76 6106 gdb_byte *cur_ptr;
639d11d3
DC
6107 int has_children;
6108
93311388 6109 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
6110 if (die == NULL)
6111 {
6112 *new_info_ptr = cur_ptr;
6113 return NULL;
6114 }
93311388 6115 store_in_ref_table (die, reader->cu);
639d11d3
DC
6116
6117 if (has_children)
348e048f 6118 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
6119 else
6120 {
6121 die->child = NULL;
6122 *new_info_ptr = cur_ptr;
6123 }
6124
6125 die->sibling = NULL;
6126 die->parent = parent;
6127 return die;
6128}
6129
6130/* Read a die, all of its descendents, and all of its siblings; set
6131 all of the fields of all of the dies correctly. Arguments are as
6132 in read_die_and_children. */
6133
6134static struct die_info *
93311388
DE
6135read_die_and_siblings (const struct die_reader_specs *reader,
6136 gdb_byte *info_ptr,
fe1b8b76 6137 gdb_byte **new_info_ptr,
639d11d3
DC
6138 struct die_info *parent)
6139{
6140 struct die_info *first_die, *last_sibling;
fe1b8b76 6141 gdb_byte *cur_ptr;
639d11d3 6142
c906108c 6143 cur_ptr = info_ptr;
639d11d3
DC
6144 first_die = last_sibling = NULL;
6145
6146 while (1)
c906108c 6147 {
639d11d3 6148 struct die_info *die
93311388 6149 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 6150
1d325ec1 6151 if (die == NULL)
c906108c 6152 {
639d11d3
DC
6153 *new_info_ptr = cur_ptr;
6154 return first_die;
c906108c 6155 }
1d325ec1
DJ
6156
6157 if (!first_die)
6158 first_die = die;
c906108c 6159 else
1d325ec1
DJ
6160 last_sibling->sibling = die;
6161
6162 last_sibling = die;
c906108c 6163 }
c906108c
SS
6164}
6165
93311388
DE
6166/* Read the die from the .debug_info section buffer. Set DIEP to
6167 point to a newly allocated die with its information, except for its
6168 child, sibling, and parent fields. Set HAS_CHILDREN to tell
6169 whether the die has children or not. */
6170
6171static gdb_byte *
6172read_full_die (const struct die_reader_specs *reader,
6173 struct die_info **diep, gdb_byte *info_ptr,
6174 int *has_children)
6175{
6176 unsigned int abbrev_number, bytes_read, i, offset;
6177 struct abbrev_info *abbrev;
6178 struct die_info *die;
6179 struct dwarf2_cu *cu = reader->cu;
6180 bfd *abfd = reader->abfd;
6181
6182 offset = info_ptr - reader->buffer;
6183 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6184 info_ptr += bytes_read;
6185 if (!abbrev_number)
6186 {
6187 *diep = NULL;
6188 *has_children = 0;
6189 return info_ptr;
6190 }
6191
6192 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
6193 if (!abbrev)
348e048f
DE
6194 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
6195 abbrev_number,
6196 bfd_get_filename (abfd));
6197
93311388
DE
6198 die = dwarf_alloc_die (cu, abbrev->num_attrs);
6199 die->offset = offset;
6200 die->tag = abbrev->tag;
6201 die->abbrev = abbrev_number;
6202
6203 die->num_attrs = abbrev->num_attrs;
6204
6205 for (i = 0; i < abbrev->num_attrs; ++i)
6206 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6207 abfd, info_ptr, cu);
6208
6209 *diep = die;
6210 *has_children = abbrev->has_children;
6211 return info_ptr;
6212}
6213
c906108c
SS
6214/* In DWARF version 2, the description of the debugging information is
6215 stored in a separate .debug_abbrev section. Before we read any
6216 dies from a section we read in all abbreviations and install them
72bf9492
DJ
6217 in a hash table. This function also sets flags in CU describing
6218 the data found in the abbrev table. */
c906108c
SS
6219
6220static void
e7c27a73 6221dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 6222{
e7c27a73 6223 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 6224 gdb_byte *abbrev_ptr;
c906108c
SS
6225 struct abbrev_info *cur_abbrev;
6226 unsigned int abbrev_number, bytes_read, abbrev_name;
6227 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
6228 struct attr_abbrev *cur_attrs;
6229 unsigned int allocated_attrs;
c906108c 6230
57349743 6231 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
6232 obstack_init (&cu->abbrev_obstack);
6233 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
6234 (ABBREV_HASH_SIZE
6235 * sizeof (struct abbrev_info *)));
6236 memset (cu->dwarf2_abbrevs, 0,
6237 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 6238
dce234bc 6239 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
6240 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6241 abbrev_ptr += bytes_read;
6242
f3dd6933
DJ
6243 allocated_attrs = ATTR_ALLOC_CHUNK;
6244 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6245
c906108c
SS
6246 /* loop until we reach an abbrev number of 0 */
6247 while (abbrev_number)
6248 {
f3dd6933 6249 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
6250
6251 /* read in abbrev header */
6252 cur_abbrev->number = abbrev_number;
6253 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6254 abbrev_ptr += bytes_read;
6255 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
6256 abbrev_ptr += 1;
6257
72bf9492
DJ
6258 if (cur_abbrev->tag == DW_TAG_namespace)
6259 cu->has_namespace_info = 1;
6260
c906108c
SS
6261 /* now read in declarations */
6262 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6263 abbrev_ptr += bytes_read;
6264 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6265 abbrev_ptr += bytes_read;
6266 while (abbrev_name)
6267 {
f3dd6933 6268 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 6269 {
f3dd6933
DJ
6270 allocated_attrs += ATTR_ALLOC_CHUNK;
6271 cur_attrs
6272 = xrealloc (cur_attrs, (allocated_attrs
6273 * sizeof (struct attr_abbrev)));
c906108c 6274 }
ae038cb0
DJ
6275
6276 /* Record whether this compilation unit might have
6277 inter-compilation-unit references. If we don't know what form
6278 this attribute will have, then it might potentially be a
6279 DW_FORM_ref_addr, so we conservatively expect inter-CU
6280 references. */
6281
6282 if (abbrev_form == DW_FORM_ref_addr
6283 || abbrev_form == DW_FORM_indirect)
6284 cu->has_form_ref_addr = 1;
6285
f3dd6933
DJ
6286 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
6287 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
6288 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6289 abbrev_ptr += bytes_read;
6290 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6291 abbrev_ptr += bytes_read;
6292 }
6293
f3dd6933
DJ
6294 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
6295 (cur_abbrev->num_attrs
6296 * sizeof (struct attr_abbrev)));
6297 memcpy (cur_abbrev->attrs, cur_attrs,
6298 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
6299
c906108c 6300 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
6301 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
6302 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
6303
6304 /* Get next abbreviation.
6305 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
6306 always properly terminated with an abbrev number of 0.
6307 Exit loop if we encounter an abbreviation which we have
6308 already read (which means we are about to read the abbreviations
6309 for the next compile unit) or if the end of the abbreviation
6310 table is reached. */
dce234bc
PP
6311 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
6312 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
6313 break;
6314 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6315 abbrev_ptr += bytes_read;
e7c27a73 6316 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
6317 break;
6318 }
f3dd6933
DJ
6319
6320 xfree (cur_attrs);
c906108c
SS
6321}
6322
f3dd6933 6323/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 6324
c906108c 6325static void
f3dd6933 6326dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 6327{
f3dd6933 6328 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 6329
f3dd6933
DJ
6330 obstack_free (&cu->abbrev_obstack, NULL);
6331 cu->dwarf2_abbrevs = NULL;
c906108c
SS
6332}
6333
6334/* Lookup an abbrev_info structure in the abbrev hash table. */
6335
6336static struct abbrev_info *
e7c27a73 6337dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
6338{
6339 unsigned int hash_number;
6340 struct abbrev_info *abbrev;
6341
6342 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 6343 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
6344
6345 while (abbrev)
6346 {
6347 if (abbrev->number == number)
6348 return abbrev;
6349 else
6350 abbrev = abbrev->next;
6351 }
6352 return NULL;
6353}
6354
72bf9492
DJ
6355/* Returns nonzero if TAG represents a type that we might generate a partial
6356 symbol for. */
6357
6358static int
6359is_type_tag_for_partial (int tag)
6360{
6361 switch (tag)
6362 {
6363#if 0
6364 /* Some types that would be reasonable to generate partial symbols for,
6365 that we don't at present. */
6366 case DW_TAG_array_type:
6367 case DW_TAG_file_type:
6368 case DW_TAG_ptr_to_member_type:
6369 case DW_TAG_set_type:
6370 case DW_TAG_string_type:
6371 case DW_TAG_subroutine_type:
6372#endif
6373 case DW_TAG_base_type:
6374 case DW_TAG_class_type:
680b30c7 6375 case DW_TAG_interface_type:
72bf9492
DJ
6376 case DW_TAG_enumeration_type:
6377 case DW_TAG_structure_type:
6378 case DW_TAG_subrange_type:
6379 case DW_TAG_typedef:
6380 case DW_TAG_union_type:
6381 return 1;
6382 default:
6383 return 0;
6384 }
6385}
6386
6387/* Load all DIEs that are interesting for partial symbols into memory. */
6388
6389static struct partial_die_info *
93311388
DE
6390load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
6391 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
6392{
6393 struct partial_die_info *part_die;
6394 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
6395 struct abbrev_info *abbrev;
6396 unsigned int bytes_read;
5afb4e99 6397 unsigned int load_all = 0;
72bf9492
DJ
6398
6399 int nesting_level = 1;
6400
6401 parent_die = NULL;
6402 last_die = NULL;
6403
5afb4e99
DJ
6404 if (cu->per_cu && cu->per_cu->load_all_dies)
6405 load_all = 1;
6406
72bf9492
DJ
6407 cu->partial_dies
6408 = htab_create_alloc_ex (cu->header.length / 12,
6409 partial_die_hash,
6410 partial_die_eq,
6411 NULL,
6412 &cu->comp_unit_obstack,
6413 hashtab_obstack_allocate,
6414 dummy_obstack_deallocate);
6415
6416 part_die = obstack_alloc (&cu->comp_unit_obstack,
6417 sizeof (struct partial_die_info));
6418
6419 while (1)
6420 {
6421 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6422
6423 /* A NULL abbrev means the end of a series of children. */
6424 if (abbrev == NULL)
6425 {
6426 if (--nesting_level == 0)
6427 {
6428 /* PART_DIE was probably the last thing allocated on the
6429 comp_unit_obstack, so we could call obstack_free
6430 here. We don't do that because the waste is small,
6431 and will be cleaned up when we're done with this
6432 compilation unit. This way, we're also more robust
6433 against other users of the comp_unit_obstack. */
6434 return first_die;
6435 }
6436 info_ptr += bytes_read;
6437 last_die = parent_die;
6438 parent_die = parent_die->die_parent;
6439 continue;
6440 }
6441
5afb4e99
DJ
6442 /* Check whether this DIE is interesting enough to save. Normally
6443 we would not be interested in members here, but there may be
6444 later variables referencing them via DW_AT_specification (for
6445 static members). */
6446 if (!load_all
6447 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
6448 && abbrev->tag != DW_TAG_enumerator
6449 && abbrev->tag != DW_TAG_subprogram
bc30ff58 6450 && abbrev->tag != DW_TAG_lexical_block
72bf9492 6451 && abbrev->tag != DW_TAG_variable
5afb4e99
DJ
6452 && abbrev->tag != DW_TAG_namespace
6453 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
6454 {
6455 /* Otherwise we skip to the next sibling, if any. */
93311388 6456 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
6457 continue;
6458 }
6459
93311388
DE
6460 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
6461 buffer, info_ptr, cu);
72bf9492
DJ
6462
6463 /* This two-pass algorithm for processing partial symbols has a
6464 high cost in cache pressure. Thus, handle some simple cases
6465 here which cover the majority of C partial symbols. DIEs
6466 which neither have specification tags in them, nor could have
6467 specification tags elsewhere pointing at them, can simply be
6468 processed and discarded.
6469
6470 This segment is also optional; scan_partial_symbols and
6471 add_partial_symbol will handle these DIEs if we chain
6472 them in normally. When compilers which do not emit large
6473 quantities of duplicate debug information are more common,
6474 this code can probably be removed. */
6475
6476 /* Any complete simple types at the top level (pretty much all
6477 of them, for a language without namespaces), can be processed
6478 directly. */
6479 if (parent_die == NULL
6480 && part_die->has_specification == 0
6481 && part_die->is_declaration == 0
6482 && (part_die->tag == DW_TAG_typedef
6483 || part_die->tag == DW_TAG_base_type
6484 || part_die->tag == DW_TAG_subrange_type))
6485 {
6486 if (building_psymtab && part_die->name != NULL)
6487 add_psymbol_to_list (part_die->name, strlen (part_die->name),
6488 VAR_DOMAIN, LOC_TYPEDEF,
6489 &cu->objfile->static_psymbols,
6490 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 6491 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6492 continue;
6493 }
6494
6495 /* If we're at the second level, and we're an enumerator, and
6496 our parent has no specification (meaning possibly lives in a
6497 namespace elsewhere), then we can add the partial symbol now
6498 instead of queueing it. */
6499 if (part_die->tag == DW_TAG_enumerator
6500 && parent_die != NULL
6501 && parent_die->die_parent == NULL
6502 && parent_die->tag == DW_TAG_enumeration_type
6503 && parent_die->has_specification == 0)
6504 {
6505 if (part_die->name == NULL)
e2e0b3e5 6506 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492
DJ
6507 else if (building_psymtab)
6508 add_psymbol_to_list (part_die->name, strlen (part_die->name),
6509 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6510 (cu->language == language_cplus
6511 || cu->language == language_java)
72bf9492
DJ
6512 ? &cu->objfile->global_psymbols
6513 : &cu->objfile->static_psymbols,
6514 0, (CORE_ADDR) 0, cu->language, cu->objfile);
6515
93311388 6516 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6517 continue;
6518 }
6519
6520 /* We'll save this DIE so link it in. */
6521 part_die->die_parent = parent_die;
6522 part_die->die_sibling = NULL;
6523 part_die->die_child = NULL;
6524
6525 if (last_die && last_die == parent_die)
6526 last_die->die_child = part_die;
6527 else if (last_die)
6528 last_die->die_sibling = part_die;
6529
6530 last_die = part_die;
6531
6532 if (first_die == NULL)
6533 first_die = part_die;
6534
6535 /* Maybe add the DIE to the hash table. Not all DIEs that we
6536 find interesting need to be in the hash table, because we
6537 also have the parent/sibling/child chains; only those that we
6538 might refer to by offset later during partial symbol reading.
6539
6540 For now this means things that might have be the target of a
6541 DW_AT_specification, DW_AT_abstract_origin, or
6542 DW_AT_extension. DW_AT_extension will refer only to
6543 namespaces; DW_AT_abstract_origin refers to functions (and
6544 many things under the function DIE, but we do not recurse
6545 into function DIEs during partial symbol reading) and
6546 possibly variables as well; DW_AT_specification refers to
6547 declarations. Declarations ought to have the DW_AT_declaration
6548 flag. It happens that GCC forgets to put it in sometimes, but
6549 only for functions, not for types.
6550
6551 Adding more things than necessary to the hash table is harmless
6552 except for the performance cost. Adding too few will result in
5afb4e99
DJ
6553 wasted time in find_partial_die, when we reread the compilation
6554 unit with load_all_dies set. */
72bf9492 6555
5afb4e99
DJ
6556 if (load_all
6557 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
6558 || abbrev->tag == DW_TAG_variable
6559 || abbrev->tag == DW_TAG_namespace
6560 || part_die->is_declaration)
6561 {
6562 void **slot;
6563
6564 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
6565 part_die->offset, INSERT);
6566 *slot = part_die;
6567 }
6568
6569 part_die = obstack_alloc (&cu->comp_unit_obstack,
6570 sizeof (struct partial_die_info));
6571
6572 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 6573 we have no reason to follow the children of structures; for other
72bf9492 6574 languages we have to, both so that we can get at method physnames
bc30ff58
JB
6575 to infer fully qualified class names, and for DW_AT_specification.
6576
6577 For Ada, we need to scan the children of subprograms and lexical
6578 blocks as well because Ada allows the definition of nested
6579 entities that could be interesting for the debugger, such as
6580 nested subprograms for instance. */
72bf9492 6581 if (last_die->has_children
5afb4e99
DJ
6582 && (load_all
6583 || last_die->tag == DW_TAG_namespace
72bf9492
DJ
6584 || last_die->tag == DW_TAG_enumeration_type
6585 || (cu->language != language_c
6586 && (last_die->tag == DW_TAG_class_type
680b30c7 6587 || last_die->tag == DW_TAG_interface_type
72bf9492 6588 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
6589 || last_die->tag == DW_TAG_union_type))
6590 || (cu->language == language_ada
6591 && (last_die->tag == DW_TAG_subprogram
6592 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
6593 {
6594 nesting_level++;
6595 parent_die = last_die;
6596 continue;
6597 }
6598
6599 /* Otherwise we skip to the next sibling, if any. */
93311388 6600 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6601
6602 /* Back to the top, do it again. */
6603 }
6604}
6605
c906108c
SS
6606/* Read a minimal amount of information into the minimal die structure. */
6607
fe1b8b76 6608static gdb_byte *
72bf9492
DJ
6609read_partial_die (struct partial_die_info *part_die,
6610 struct abbrev_info *abbrev,
6611 unsigned int abbrev_len, bfd *abfd,
93311388
DE
6612 gdb_byte *buffer, gdb_byte *info_ptr,
6613 struct dwarf2_cu *cu)
c906108c 6614{
72bf9492 6615 unsigned int bytes_read, i;
c906108c 6616 struct attribute attr;
c5aa993b 6617 int has_low_pc_attr = 0;
c906108c 6618 int has_high_pc_attr = 0;
e7030f15 6619 CORE_ADDR base_address = 0;
85cbf3d3
JK
6620 enum
6621 {
6622 base_address_none,
6623 base_address_low_pc,
6624 /* Overrides BASE_ADDRESS_LOW_PC. */
6625 base_address_entry_pc
6626 }
6627 base_address_type = base_address_none;
c906108c 6628
72bf9492 6629 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 6630
93311388 6631 part_die->offset = info_ptr - buffer;
72bf9492
DJ
6632
6633 info_ptr += abbrev_len;
6634
6635 if (abbrev == NULL)
6636 return info_ptr;
6637
c906108c
SS
6638 part_die->tag = abbrev->tag;
6639 part_die->has_children = abbrev->has_children;
c906108c
SS
6640
6641 for (i = 0; i < abbrev->num_attrs; ++i)
6642 {
e7c27a73 6643 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
6644
6645 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 6646 partial symbol table. */
c906108c
SS
6647 switch (attr.name)
6648 {
6649 case DW_AT_name:
71c25dea
TT
6650 switch (part_die->tag)
6651 {
6652 case DW_TAG_compile_unit:
348e048f 6653 case DW_TAG_type_unit:
71c25dea
TT
6654 /* Compilation units have a DW_AT_name that is a filename, not
6655 a source language identifier. */
6656 case DW_TAG_enumeration_type:
6657 case DW_TAG_enumerator:
6658 /* These tags always have simple identifiers already; no need
6659 to canonicalize them. */
6660 part_die->name = DW_STRING (&attr);
6661 break;
6662 default:
6663 part_die->name
6664 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
6665 &cu->comp_unit_obstack);
6666 break;
6667 }
c906108c 6668 break;
57c22c6c
BR
6669 case DW_AT_comp_dir:
6670 if (part_die->dirname == NULL)
6671 part_die->dirname = DW_STRING (&attr);
6672 break;
c906108c
SS
6673 case DW_AT_MIPS_linkage_name:
6674 part_die->name = DW_STRING (&attr);
6675 break;
6676 case DW_AT_low_pc:
6677 has_low_pc_attr = 1;
6678 part_die->lowpc = DW_ADDR (&attr);
85cbf3d3
JK
6679 if (part_die->tag == DW_TAG_compile_unit
6680 && base_address_type < base_address_low_pc)
6681 {
6682 base_address = DW_ADDR (&attr);
6683 base_address_type = base_address_low_pc;
6684 }
c906108c
SS
6685 break;
6686 case DW_AT_high_pc:
6687 has_high_pc_attr = 1;
6688 part_die->highpc = DW_ADDR (&attr);
6689 break;
85cbf3d3
JK
6690 case DW_AT_entry_pc:
6691 if (part_die->tag == DW_TAG_compile_unit
6692 && base_address_type < base_address_entry_pc)
6693 {
6694 base_address = DW_ADDR (&attr);
6695 base_address_type = base_address_entry_pc;
6696 }
6697 break;
43039443 6698 case DW_AT_ranges:
ff013f42
JK
6699 if (part_die->tag == DW_TAG_compile_unit)
6700 {
6701 cu->ranges_offset = DW_UNSND (&attr);
6702 cu->has_ranges_offset = 1;
6703 }
43039443 6704 break;
c906108c 6705 case DW_AT_location:
8e19ed76
PS
6706 /* Support the .debug_loc offsets */
6707 if (attr_form_is_block (&attr))
6708 {
6709 part_die->locdesc = DW_BLOCK (&attr);
6710 }
3690dd37 6711 else if (attr_form_is_section_offset (&attr))
8e19ed76 6712 {
4d3c2250 6713 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6714 }
6715 else
6716 {
4d3c2250
KB
6717 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6718 "partial symbol information");
8e19ed76 6719 }
c906108c
SS
6720 break;
6721 case DW_AT_language:
6722 part_die->language = DW_UNSND (&attr);
6723 break;
6724 case DW_AT_external:
6725 part_die->is_external = DW_UNSND (&attr);
6726 break;
6727 case DW_AT_declaration:
6728 part_die->is_declaration = DW_UNSND (&attr);
6729 break;
6730 case DW_AT_type:
6731 part_die->has_type = 1;
6732 break;
6733 case DW_AT_abstract_origin:
6734 case DW_AT_specification:
72bf9492
DJ
6735 case DW_AT_extension:
6736 part_die->has_specification = 1;
c764a876 6737 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
6738 break;
6739 case DW_AT_sibling:
6740 /* Ignore absolute siblings, they might point outside of
6741 the current compile unit. */
6742 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 6743 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 6744 else
93311388 6745 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 6746 break;
aaa75496
JB
6747 case DW_AT_stmt_list:
6748 part_die->has_stmt_list = 1;
6749 part_die->line_offset = DW_UNSND (&attr);
6750 break;
fa4028e9
JB
6751 case DW_AT_byte_size:
6752 part_die->has_byte_size = 1;
6753 break;
68511cec
CES
6754 case DW_AT_calling_convention:
6755 /* DWARF doesn't provide a way to identify a program's source-level
6756 entry point. DW_AT_calling_convention attributes are only meant
6757 to describe functions' calling conventions.
6758
6759 However, because it's a necessary piece of information in
6760 Fortran, and because DW_CC_program is the only piece of debugging
6761 information whose definition refers to a 'main program' at all,
6762 several compilers have begun marking Fortran main programs with
6763 DW_CC_program --- even when those functions use the standard
6764 calling conventions.
6765
6766 So until DWARF specifies a way to provide this information and
6767 compilers pick up the new representation, we'll support this
6768 practice. */
6769 if (DW_UNSND (&attr) == DW_CC_program
6770 && cu->language == language_fortran)
6771 set_main_name (part_die->name);
6772 break;
c906108c
SS
6773 default:
6774 break;
6775 }
6776 }
6777
c906108c
SS
6778 /* When using the GNU linker, .gnu.linkonce. sections are used to
6779 eliminate duplicate copies of functions and vtables and such.
6780 The linker will arbitrarily choose one and discard the others.
6781 The AT_*_pc values for such functions refer to local labels in
6782 these sections. If the section from that file was discarded, the
6783 labels are not in the output, so the relocs get a value of 0.
6784 If this is a discarded function, mark the pc bounds as invalid,
6785 so that GDB will ignore it. */
6786 if (has_low_pc_attr && has_high_pc_attr
6787 && part_die->lowpc < part_die->highpc
6788 && (part_die->lowpc != 0
72dca2f5 6789 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 6790 part_die->has_pc_info = 1;
85cbf3d3 6791
d00adf39 6792 if (base_address_type != base_address_none && !cu->base_known)
85cbf3d3
JK
6793 {
6794 gdb_assert (part_die->tag == DW_TAG_compile_unit);
d00adf39
DE
6795 cu->base_known = 1;
6796 cu->base_address = base_address;
85cbf3d3
JK
6797 }
6798
c906108c
SS
6799 return info_ptr;
6800}
6801
72bf9492
DJ
6802/* Find a cached partial DIE at OFFSET in CU. */
6803
6804static struct partial_die_info *
c764a876 6805find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
6806{
6807 struct partial_die_info *lookup_die = NULL;
6808 struct partial_die_info part_die;
6809
6810 part_die.offset = offset;
6811 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6812
72bf9492
DJ
6813 return lookup_die;
6814}
6815
348e048f
DE
6816/* Find a partial DIE at OFFSET, which may or may not be in CU,
6817 except in the case of .debug_types DIEs which do not reference
6818 outside their CU (they do however referencing other types via
6819 DW_FORM_sig8). */
72bf9492
DJ
6820
6821static struct partial_die_info *
c764a876 6822find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 6823{
5afb4e99
DJ
6824 struct dwarf2_per_cu_data *per_cu = NULL;
6825 struct partial_die_info *pd = NULL;
72bf9492 6826
348e048f
DE
6827 if (cu->per_cu->from_debug_types)
6828 {
6829 pd = find_partial_die_in_comp_unit (offset, cu);
6830 if (pd != NULL)
6831 return pd;
6832 goto not_found;
6833 }
6834
45452591 6835 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
6836 {
6837 pd = find_partial_die_in_comp_unit (offset, cu);
6838 if (pd != NULL)
6839 return pd;
6840 }
72bf9492 6841
ae038cb0
DJ
6842 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6843
ae038cb0
DJ
6844 if (per_cu->cu == NULL)
6845 {
93311388 6846 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
6847 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6848 dwarf2_per_objfile->read_in_chain = per_cu;
6849 }
6850
6851 per_cu->cu->last_used = 0;
5afb4e99
DJ
6852 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6853
6854 if (pd == NULL && per_cu->load_all_dies == 0)
6855 {
6856 struct cleanup *back_to;
6857 struct partial_die_info comp_unit_die;
6858 struct abbrev_info *abbrev;
6859 unsigned int bytes_read;
6860 char *info_ptr;
6861
6862 per_cu->load_all_dies = 1;
6863
6864 /* Re-read the DIEs. */
6865 back_to = make_cleanup (null_cleanup, 0);
6866 if (per_cu->cu->dwarf2_abbrevs == NULL)
6867 {
6868 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
6869 back_to = make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
6870 }
dce234bc 6871 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
6872 + per_cu->cu->header.offset
6873 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
6874 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6875 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
6876 per_cu->cu->objfile->obfd,
6877 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
6878 per_cu->cu);
6879 if (comp_unit_die.has_children)
93311388
DE
6880 load_partial_dies (per_cu->cu->objfile->obfd,
6881 dwarf2_per_objfile->info.buffer, info_ptr,
6882 0, per_cu->cu);
5afb4e99
DJ
6883 do_cleanups (back_to);
6884
6885 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6886 }
6887
348e048f
DE
6888 not_found:
6889
5afb4e99
DJ
6890 if (pd == NULL)
6891 internal_error (__FILE__, __LINE__,
c764a876 6892 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
6893 offset, bfd_get_filename (cu->objfile->obfd));
6894 return pd;
72bf9492
DJ
6895}
6896
6897/* Adjust PART_DIE before generating a symbol for it. This function
6898 may set the is_external flag or change the DIE's name. */
6899
6900static void
6901fixup_partial_die (struct partial_die_info *part_die,
6902 struct dwarf2_cu *cu)
6903{
6904 /* If we found a reference attribute and the DIE has no name, try
6905 to find a name in the referred to DIE. */
6906
6907 if (part_die->name == NULL && part_die->has_specification)
6908 {
6909 struct partial_die_info *spec_die;
72bf9492 6910
10b3939b 6911 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 6912
10b3939b 6913 fixup_partial_die (spec_die, cu);
72bf9492
DJ
6914
6915 if (spec_die->name)
6916 {
6917 part_die->name = spec_die->name;
6918
6919 /* Copy DW_AT_external attribute if it is set. */
6920 if (spec_die->is_external)
6921 part_die->is_external = spec_die->is_external;
6922 }
6923 }
6924
6925 /* Set default names for some unnamed DIEs. */
6926 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
6927 || part_die->tag == DW_TAG_class_type))
6928 part_die->name = "(anonymous class)";
6929
6930 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
6931 part_die->name = "(anonymous namespace)";
6932
6933 if (part_die->tag == DW_TAG_structure_type
6934 || part_die->tag == DW_TAG_class_type
6935 || part_die->tag == DW_TAG_union_type)
6936 guess_structure_name (part_die, cu);
6937}
6938
a8329558 6939/* Read an attribute value described by an attribute form. */
c906108c 6940
fe1b8b76 6941static gdb_byte *
a8329558 6942read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 6943 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6944 struct dwarf2_cu *cu)
c906108c 6945{
e7c27a73 6946 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6947 unsigned int bytes_read;
6948 struct dwarf_block *blk;
6949
a8329558
KW
6950 attr->form = form;
6951 switch (form)
c906108c
SS
6952 {
6953 case DW_FORM_addr:
6954 case DW_FORM_ref_addr:
e7c27a73 6955 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 6956 info_ptr += bytes_read;
c906108c
SS
6957 break;
6958 case DW_FORM_block2:
7b5a2f43 6959 blk = dwarf_alloc_block (cu);
c906108c
SS
6960 blk->size = read_2_bytes (abfd, info_ptr);
6961 info_ptr += 2;
6962 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6963 info_ptr += blk->size;
6964 DW_BLOCK (attr) = blk;
6965 break;
6966 case DW_FORM_block4:
7b5a2f43 6967 blk = dwarf_alloc_block (cu);
c906108c
SS
6968 blk->size = read_4_bytes (abfd, info_ptr);
6969 info_ptr += 4;
6970 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6971 info_ptr += blk->size;
6972 DW_BLOCK (attr) = blk;
6973 break;
6974 case DW_FORM_data2:
6975 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
6976 info_ptr += 2;
6977 break;
6978 case DW_FORM_data4:
6979 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
6980 info_ptr += 4;
6981 break;
6982 case DW_FORM_data8:
6983 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
6984 info_ptr += 8;
6985 break;
6986 case DW_FORM_string:
6987 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
6988 info_ptr += bytes_read;
6989 break;
4bdf3d34
JJ
6990 case DW_FORM_strp:
6991 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
6992 &bytes_read);
6993 info_ptr += bytes_read;
6994 break;
c906108c 6995 case DW_FORM_block:
7b5a2f43 6996 blk = dwarf_alloc_block (cu);
c906108c
SS
6997 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6998 info_ptr += bytes_read;
6999 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7000 info_ptr += blk->size;
7001 DW_BLOCK (attr) = blk;
7002 break;
7003 case DW_FORM_block1:
7b5a2f43 7004 blk = dwarf_alloc_block (cu);
c906108c
SS
7005 blk->size = read_1_byte (abfd, info_ptr);
7006 info_ptr += 1;
7007 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7008 info_ptr += blk->size;
7009 DW_BLOCK (attr) = blk;
7010 break;
7011 case DW_FORM_data1:
7012 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7013 info_ptr += 1;
7014 break;
7015 case DW_FORM_flag:
7016 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7017 info_ptr += 1;
7018 break;
7019 case DW_FORM_sdata:
7020 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
7021 info_ptr += bytes_read;
7022 break;
7023 case DW_FORM_udata:
7024 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7025 info_ptr += bytes_read;
7026 break;
7027 case DW_FORM_ref1:
10b3939b 7028 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
7029 info_ptr += 1;
7030 break;
7031 case DW_FORM_ref2:
10b3939b 7032 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
7033 info_ptr += 2;
7034 break;
7035 case DW_FORM_ref4:
10b3939b 7036 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
7037 info_ptr += 4;
7038 break;
613e1657 7039 case DW_FORM_ref8:
10b3939b 7040 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
7041 info_ptr += 8;
7042 break;
348e048f
DE
7043 case DW_FORM_sig8:
7044 /* Convert the signature to something we can record in DW_UNSND
7045 for later lookup.
7046 NOTE: This is NULL if the type wasn't found. */
7047 DW_SIGNATURED_TYPE (attr) =
7048 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
7049 info_ptr += 8;
7050 break;
c906108c 7051 case DW_FORM_ref_udata:
10b3939b
DJ
7052 DW_ADDR (attr) = (cu->header.offset
7053 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
7054 info_ptr += bytes_read;
7055 break;
c906108c 7056 case DW_FORM_indirect:
a8329558
KW
7057 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7058 info_ptr += bytes_read;
e7c27a73 7059 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 7060 break;
c906108c 7061 default:
8a3fe4f8 7062 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
7063 dwarf_form_name (form),
7064 bfd_get_filename (abfd));
c906108c 7065 }
28e94949
JB
7066
7067 /* We have seen instances where the compiler tried to emit a byte
7068 size attribute of -1 which ended up being encoded as an unsigned
7069 0xffffffff. Although 0xffffffff is technically a valid size value,
7070 an object of this size seems pretty unlikely so we can relatively
7071 safely treat these cases as if the size attribute was invalid and
7072 treat them as zero by default. */
7073 if (attr->name == DW_AT_byte_size
7074 && form == DW_FORM_data4
7075 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
7076 {
7077 complaint
7078 (&symfile_complaints,
7079 _("Suspicious DW_AT_byte_size value treated as zero instead of 0x%lx"),
7080 DW_UNSND (attr));
7081 DW_UNSND (attr) = 0;
7082 }
28e94949 7083
c906108c
SS
7084 return info_ptr;
7085}
7086
a8329558
KW
7087/* Read an attribute described by an abbreviated attribute. */
7088
fe1b8b76 7089static gdb_byte *
a8329558 7090read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 7091 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
7092{
7093 attr->name = abbrev->name;
e7c27a73 7094 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
7095}
7096
c906108c
SS
7097/* read dwarf information from a buffer */
7098
7099static unsigned int
fe1b8b76 7100read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 7101{
fe1b8b76 7102 return bfd_get_8 (abfd, buf);
c906108c
SS
7103}
7104
7105static int
fe1b8b76 7106read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 7107{
fe1b8b76 7108 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
7109}
7110
7111static unsigned int
fe1b8b76 7112read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7113{
fe1b8b76 7114 return bfd_get_16 (abfd, buf);
c906108c
SS
7115}
7116
7117static int
fe1b8b76 7118read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7119{
fe1b8b76 7120 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
7121}
7122
7123static unsigned int
fe1b8b76 7124read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7125{
fe1b8b76 7126 return bfd_get_32 (abfd, buf);
c906108c
SS
7127}
7128
7129static int
fe1b8b76 7130read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7131{
fe1b8b76 7132 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
7133}
7134
93311388 7135static ULONGEST
fe1b8b76 7136read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7137{
fe1b8b76 7138 return bfd_get_64 (abfd, buf);
c906108c
SS
7139}
7140
7141static CORE_ADDR
fe1b8b76 7142read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 7143 unsigned int *bytes_read)
c906108c 7144{
e7c27a73 7145 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7146 CORE_ADDR retval = 0;
7147
107d2387 7148 if (cu_header->signed_addr_p)
c906108c 7149 {
107d2387
AC
7150 switch (cu_header->addr_size)
7151 {
7152 case 2:
fe1b8b76 7153 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
7154 break;
7155 case 4:
fe1b8b76 7156 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
7157 break;
7158 case 8:
fe1b8b76 7159 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
7160 break;
7161 default:
8e65ff28 7162 internal_error (__FILE__, __LINE__,
e2e0b3e5 7163 _("read_address: bad switch, signed [in module %s]"),
659b0389 7164 bfd_get_filename (abfd));
107d2387
AC
7165 }
7166 }
7167 else
7168 {
7169 switch (cu_header->addr_size)
7170 {
7171 case 2:
fe1b8b76 7172 retval = bfd_get_16 (abfd, buf);
107d2387
AC
7173 break;
7174 case 4:
fe1b8b76 7175 retval = bfd_get_32 (abfd, buf);
107d2387
AC
7176 break;
7177 case 8:
fe1b8b76 7178 retval = bfd_get_64 (abfd, buf);
107d2387
AC
7179 break;
7180 default:
8e65ff28 7181 internal_error (__FILE__, __LINE__,
e2e0b3e5 7182 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 7183 bfd_get_filename (abfd));
107d2387 7184 }
c906108c 7185 }
64367e0a 7186
107d2387
AC
7187 *bytes_read = cu_header->addr_size;
7188 return retval;
c906108c
SS
7189}
7190
f7ef9339 7191/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
7192 specification allows the initial length to take up either 4 bytes
7193 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
7194 bytes describe the length and all offsets will be 8 bytes in length
7195 instead of 4.
7196
f7ef9339
KB
7197 An older, non-standard 64-bit format is also handled by this
7198 function. The older format in question stores the initial length
7199 as an 8-byte quantity without an escape value. Lengths greater
7200 than 2^32 aren't very common which means that the initial 4 bytes
7201 is almost always zero. Since a length value of zero doesn't make
7202 sense for the 32-bit format, this initial zero can be considered to
7203 be an escape value which indicates the presence of the older 64-bit
7204 format. As written, the code can't detect (old format) lengths
917c78fc
MK
7205 greater than 4GB. If it becomes necessary to handle lengths
7206 somewhat larger than 4GB, we could allow other small values (such
7207 as the non-sensical values of 1, 2, and 3) to also be used as
7208 escape values indicating the presence of the old format.
f7ef9339 7209
917c78fc
MK
7210 The value returned via bytes_read should be used to increment the
7211 relevant pointer after calling read_initial_length().
c764a876 7212
613e1657
KB
7213 [ Note: read_initial_length() and read_offset() are based on the
7214 document entitled "DWARF Debugging Information Format", revision
f7ef9339 7215 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
7216 from:
7217
f7ef9339 7218 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
613e1657
KB
7219
7220 This document is only a draft and is subject to change. (So beware.)
7221
f7ef9339 7222 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
7223 determined empirically by examining 64-bit ELF files produced by
7224 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
7225
7226 - Kevin, July 16, 2002
613e1657
KB
7227 ] */
7228
7229static LONGEST
c764a876 7230read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 7231{
fe1b8b76 7232 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 7233
dd373385 7234 if (length == 0xffffffff)
613e1657 7235 {
fe1b8b76 7236 length = bfd_get_64 (abfd, buf + 4);
613e1657 7237 *bytes_read = 12;
613e1657 7238 }
dd373385 7239 else if (length == 0)
f7ef9339 7240 {
dd373385 7241 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 7242 length = bfd_get_64 (abfd, buf);
f7ef9339 7243 *bytes_read = 8;
f7ef9339 7244 }
613e1657
KB
7245 else
7246 {
7247 *bytes_read = 4;
613e1657
KB
7248 }
7249
c764a876
DE
7250 return length;
7251}
dd373385 7252
c764a876
DE
7253/* Cover function for read_initial_length.
7254 Returns the length of the object at BUF, and stores the size of the
7255 initial length in *BYTES_READ and stores the size that offsets will be in
7256 *OFFSET_SIZE.
7257 If the initial length size is not equivalent to that specified in
7258 CU_HEADER then issue a complaint.
7259 This is useful when reading non-comp-unit headers. */
dd373385 7260
c764a876
DE
7261static LONGEST
7262read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
7263 const struct comp_unit_head *cu_header,
7264 unsigned int *bytes_read,
7265 unsigned int *offset_size)
7266{
7267 LONGEST length = read_initial_length (abfd, buf, bytes_read);
7268
7269 gdb_assert (cu_header->initial_length_size == 4
7270 || cu_header->initial_length_size == 8
7271 || cu_header->initial_length_size == 12);
7272
7273 if (cu_header->initial_length_size != *bytes_read)
7274 complaint (&symfile_complaints,
7275 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 7276
c764a876 7277 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 7278 return length;
613e1657
KB
7279}
7280
7281/* Read an offset from the data stream. The size of the offset is
917c78fc 7282 given by cu_header->offset_size. */
613e1657
KB
7283
7284static LONGEST
fe1b8b76 7285read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 7286 unsigned int *bytes_read)
c764a876
DE
7287{
7288 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
7289 *bytes_read = cu_header->offset_size;
7290 return offset;
7291}
7292
7293/* Read an offset from the data stream. */
7294
7295static LONGEST
7296read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
7297{
7298 LONGEST retval = 0;
7299
c764a876 7300 switch (offset_size)
613e1657
KB
7301 {
7302 case 4:
fe1b8b76 7303 retval = bfd_get_32 (abfd, buf);
613e1657
KB
7304 break;
7305 case 8:
fe1b8b76 7306 retval = bfd_get_64 (abfd, buf);
613e1657
KB
7307 break;
7308 default:
8e65ff28 7309 internal_error (__FILE__, __LINE__,
c764a876 7310 _("read_offset_1: bad switch [in module %s]"),
659b0389 7311 bfd_get_filename (abfd));
613e1657
KB
7312 }
7313
917c78fc 7314 return retval;
613e1657
KB
7315}
7316
fe1b8b76
JB
7317static gdb_byte *
7318read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
7319{
7320 /* If the size of a host char is 8 bits, we can return a pointer
7321 to the buffer, otherwise we have to copy the data to a buffer
7322 allocated on the temporary obstack. */
4bdf3d34 7323 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 7324 return buf;
c906108c
SS
7325}
7326
7327static char *
fe1b8b76 7328read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
7329{
7330 /* If the size of a host char is 8 bits, we can return a pointer
7331 to the string, otherwise we have to copy the string to a buffer
7332 allocated on the temporary obstack. */
4bdf3d34 7333 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
7334 if (*buf == '\0')
7335 {
7336 *bytes_read_ptr = 1;
7337 return NULL;
7338 }
fe1b8b76
JB
7339 *bytes_read_ptr = strlen ((char *) buf) + 1;
7340 return (char *) buf;
4bdf3d34
JJ
7341}
7342
7343static char *
fe1b8b76 7344read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
7345 const struct comp_unit_head *cu_header,
7346 unsigned int *bytes_read_ptr)
7347{
c764a876 7348 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 7349
dce234bc 7350 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 7351 {
8a3fe4f8 7352 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 7353 bfd_get_filename (abfd));
4bdf3d34 7354 return NULL;
c906108c 7355 }
dce234bc 7356 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 7357 {
8a3fe4f8 7358 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 7359 bfd_get_filename (abfd));
c906108c
SS
7360 return NULL;
7361 }
4bdf3d34 7362 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 7363 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 7364 return NULL;
dce234bc 7365 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
7366}
7367
ce5d95e1 7368static unsigned long
fe1b8b76 7369read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 7370{
ce5d95e1
JB
7371 unsigned long result;
7372 unsigned int num_read;
c906108c
SS
7373 int i, shift;
7374 unsigned char byte;
7375
7376 result = 0;
7377 shift = 0;
7378 num_read = 0;
7379 i = 0;
7380 while (1)
7381 {
fe1b8b76 7382 byte = bfd_get_8 (abfd, buf);
c906108c
SS
7383 buf++;
7384 num_read++;
ce5d95e1 7385 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
7386 if ((byte & 128) == 0)
7387 {
7388 break;
7389 }
7390 shift += 7;
7391 }
7392 *bytes_read_ptr = num_read;
7393 return result;
7394}
7395
ce5d95e1 7396static long
fe1b8b76 7397read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 7398{
ce5d95e1 7399 long result;
77e0b926 7400 int i, shift, num_read;
c906108c
SS
7401 unsigned char byte;
7402
7403 result = 0;
7404 shift = 0;
c906108c
SS
7405 num_read = 0;
7406 i = 0;
7407 while (1)
7408 {
fe1b8b76 7409 byte = bfd_get_8 (abfd, buf);
c906108c
SS
7410 buf++;
7411 num_read++;
ce5d95e1 7412 result |= ((long)(byte & 127) << shift);
c906108c
SS
7413 shift += 7;
7414 if ((byte & 128) == 0)
7415 {
7416 break;
7417 }
7418 }
77e0b926
DJ
7419 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
7420 result |= -(((long)1) << shift);
c906108c
SS
7421 *bytes_read_ptr = num_read;
7422 return result;
7423}
7424
4bb7a0a7
DJ
7425/* Return a pointer to just past the end of an LEB128 number in BUF. */
7426
fe1b8b76
JB
7427static gdb_byte *
7428skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
7429{
7430 int byte;
7431
7432 while (1)
7433 {
fe1b8b76 7434 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
7435 buf++;
7436 if ((byte & 128) == 0)
7437 return buf;
7438 }
7439}
7440
c906108c 7441static void
e142c38c 7442set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
7443{
7444 switch (lang)
7445 {
7446 case DW_LANG_C89:
76bee0cc 7447 case DW_LANG_C99:
c906108c 7448 case DW_LANG_C:
e142c38c 7449 cu->language = language_c;
c906108c
SS
7450 break;
7451 case DW_LANG_C_plus_plus:
e142c38c 7452 cu->language = language_cplus;
c906108c
SS
7453 break;
7454 case DW_LANG_Fortran77:
7455 case DW_LANG_Fortran90:
b21b22e0 7456 case DW_LANG_Fortran95:
e142c38c 7457 cu->language = language_fortran;
c906108c
SS
7458 break;
7459 case DW_LANG_Mips_Assembler:
e142c38c 7460 cu->language = language_asm;
c906108c 7461 break;
bebd888e 7462 case DW_LANG_Java:
e142c38c 7463 cu->language = language_java;
bebd888e 7464 break;
c906108c 7465 case DW_LANG_Ada83:
8aaf0b47 7466 case DW_LANG_Ada95:
bc5f45f8
JB
7467 cu->language = language_ada;
7468 break;
72019c9c
GM
7469 case DW_LANG_Modula2:
7470 cu->language = language_m2;
7471 break;
fe8e67fd
PM
7472 case DW_LANG_Pascal83:
7473 cu->language = language_pascal;
7474 break;
22566fbd
DJ
7475 case DW_LANG_ObjC:
7476 cu->language = language_objc;
7477 break;
c906108c
SS
7478 case DW_LANG_Cobol74:
7479 case DW_LANG_Cobol85:
c906108c 7480 default:
e142c38c 7481 cu->language = language_minimal;
c906108c
SS
7482 break;
7483 }
e142c38c 7484 cu->language_defn = language_def (cu->language);
c906108c
SS
7485}
7486
7487/* Return the named attribute or NULL if not there. */
7488
7489static struct attribute *
e142c38c 7490dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
7491{
7492 unsigned int i;
7493 struct attribute *spec = NULL;
7494
7495 for (i = 0; i < die->num_attrs; ++i)
7496 {
7497 if (die->attrs[i].name == name)
10b3939b 7498 return &die->attrs[i];
c906108c
SS
7499 if (die->attrs[i].name == DW_AT_specification
7500 || die->attrs[i].name == DW_AT_abstract_origin)
7501 spec = &die->attrs[i];
7502 }
c906108c 7503
10b3939b 7504 if (spec)
f2f0e013
DJ
7505 {
7506 die = follow_die_ref (die, spec, &cu);
7507 return dwarf2_attr (die, name, cu);
7508 }
c5aa993b 7509
c906108c
SS
7510 return NULL;
7511}
7512
348e048f
DE
7513/* Return the named attribute or NULL if not there,
7514 but do not follow DW_AT_specification, etc.
7515 This is for use in contexts where we're reading .debug_types dies.
7516 Following DW_AT_specification, DW_AT_abstract_origin will take us
7517 back up the chain, and we want to go down. */
7518
7519static struct attribute *
7520dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
7521 struct dwarf2_cu *cu)
7522{
7523 unsigned int i;
7524
7525 for (i = 0; i < die->num_attrs; ++i)
7526 if (die->attrs[i].name == name)
7527 return &die->attrs[i];
7528
7529 return NULL;
7530}
7531
05cf31d1
JB
7532/* Return non-zero iff the attribute NAME is defined for the given DIE,
7533 and holds a non-zero value. This function should only be used for
7534 DW_FORM_flag attributes. */
7535
7536static int
7537dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
7538{
7539 struct attribute *attr = dwarf2_attr (die, name, cu);
7540
7541 return (attr && DW_UNSND (attr));
7542}
7543
3ca72b44 7544static int
e142c38c 7545die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 7546{
05cf31d1
JB
7547 /* A DIE is a declaration if it has a DW_AT_declaration attribute
7548 which value is non-zero. However, we have to be careful with
7549 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
7550 (via dwarf2_flag_true_p) follows this attribute. So we may
7551 end up accidently finding a declaration attribute that belongs
7552 to a different DIE referenced by the specification attribute,
7553 even though the given DIE does not have a declaration attribute. */
7554 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
7555 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
7556}
7557
63d06c5c 7558/* Return the die giving the specification for DIE, if there is
f2f0e013 7559 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
7560 containing the return value on output. If there is no
7561 specification, but there is an abstract origin, that is
7562 returned. */
63d06c5c
DC
7563
7564static struct die_info *
f2f0e013 7565die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 7566{
f2f0e013
DJ
7567 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
7568 *spec_cu);
63d06c5c 7569
edb3359d
DJ
7570 if (spec_attr == NULL)
7571 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
7572
63d06c5c
DC
7573 if (spec_attr == NULL)
7574 return NULL;
7575 else
f2f0e013 7576 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 7577}
c906108c 7578
debd256d
JB
7579/* Free the line_header structure *LH, and any arrays and strings it
7580 refers to. */
7581static void
7582free_line_header (struct line_header *lh)
7583{
7584 if (lh->standard_opcode_lengths)
a8bc7b56 7585 xfree (lh->standard_opcode_lengths);
debd256d
JB
7586
7587 /* Remember that all the lh->file_names[i].name pointers are
7588 pointers into debug_line_buffer, and don't need to be freed. */
7589 if (lh->file_names)
a8bc7b56 7590 xfree (lh->file_names);
debd256d
JB
7591
7592 /* Similarly for the include directory names. */
7593 if (lh->include_dirs)
a8bc7b56 7594 xfree (lh->include_dirs);
debd256d 7595
a8bc7b56 7596 xfree (lh);
debd256d
JB
7597}
7598
7599
7600/* Add an entry to LH's include directory table. */
7601static void
7602add_include_dir (struct line_header *lh, char *include_dir)
c906108c 7603{
debd256d
JB
7604 /* Grow the array if necessary. */
7605 if (lh->include_dirs_size == 0)
c5aa993b 7606 {
debd256d
JB
7607 lh->include_dirs_size = 1; /* for testing */
7608 lh->include_dirs = xmalloc (lh->include_dirs_size
7609 * sizeof (*lh->include_dirs));
7610 }
7611 else if (lh->num_include_dirs >= lh->include_dirs_size)
7612 {
7613 lh->include_dirs_size *= 2;
7614 lh->include_dirs = xrealloc (lh->include_dirs,
7615 (lh->include_dirs_size
7616 * sizeof (*lh->include_dirs)));
c5aa993b 7617 }
c906108c 7618
debd256d
JB
7619 lh->include_dirs[lh->num_include_dirs++] = include_dir;
7620}
7621
7622
7623/* Add an entry to LH's file name table. */
7624static void
7625add_file_name (struct line_header *lh,
7626 char *name,
7627 unsigned int dir_index,
7628 unsigned int mod_time,
7629 unsigned int length)
7630{
7631 struct file_entry *fe;
7632
7633 /* Grow the array if necessary. */
7634 if (lh->file_names_size == 0)
7635 {
7636 lh->file_names_size = 1; /* for testing */
7637 lh->file_names = xmalloc (lh->file_names_size
7638 * sizeof (*lh->file_names));
7639 }
7640 else if (lh->num_file_names >= lh->file_names_size)
7641 {
7642 lh->file_names_size *= 2;
7643 lh->file_names = xrealloc (lh->file_names,
7644 (lh->file_names_size
7645 * sizeof (*lh->file_names)));
7646 }
7647
7648 fe = &lh->file_names[lh->num_file_names++];
7649 fe->name = name;
7650 fe->dir_index = dir_index;
7651 fe->mod_time = mod_time;
7652 fe->length = length;
aaa75496 7653 fe->included_p = 0;
cb1df416 7654 fe->symtab = NULL;
debd256d
JB
7655}
7656
7657
7658/* Read the statement program header starting at OFFSET in
6502dd73
DJ
7659 .debug_line, according to the endianness of ABFD. Return a pointer
7660 to a struct line_header, allocated using xmalloc.
debd256d
JB
7661
7662 NOTE: the strings in the include directory and file name tables of
7663 the returned object point into debug_line_buffer, and must not be
7664 freed. */
7665static struct line_header *
7666dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 7667 struct dwarf2_cu *cu)
debd256d
JB
7668{
7669 struct cleanup *back_to;
7670 struct line_header *lh;
fe1b8b76 7671 gdb_byte *line_ptr;
c764a876 7672 unsigned int bytes_read, offset_size;
debd256d
JB
7673 int i;
7674 char *cur_dir, *cur_file;
7675
dce234bc 7676 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 7677 {
e2e0b3e5 7678 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
7679 return 0;
7680 }
7681
a738430d
MK
7682 /* Make sure that at least there's room for the total_length field.
7683 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 7684 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 7685 {
4d3c2250 7686 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7687 return 0;
7688 }
7689
7690 lh = xmalloc (sizeof (*lh));
7691 memset (lh, 0, sizeof (*lh));
7692 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
7693 (void *) lh);
7694
dce234bc 7695 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 7696
a738430d 7697 /* Read in the header. */
dd373385 7698 lh->total_length =
c764a876
DE
7699 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
7700 &bytes_read, &offset_size);
debd256d 7701 line_ptr += bytes_read;
dce234bc
PP
7702 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
7703 + dwarf2_per_objfile->line.size))
debd256d 7704 {
4d3c2250 7705 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7706 return 0;
7707 }
7708 lh->statement_program_end = line_ptr + lh->total_length;
7709 lh->version = read_2_bytes (abfd, line_ptr);
7710 line_ptr += 2;
c764a876
DE
7711 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
7712 line_ptr += offset_size;
debd256d
JB
7713 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
7714 line_ptr += 1;
7715 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
7716 line_ptr += 1;
7717 lh->line_base = read_1_signed_byte (abfd, line_ptr);
7718 line_ptr += 1;
7719 lh->line_range = read_1_byte (abfd, line_ptr);
7720 line_ptr += 1;
7721 lh->opcode_base = read_1_byte (abfd, line_ptr);
7722 line_ptr += 1;
7723 lh->standard_opcode_lengths
fe1b8b76 7724 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
7725
7726 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
7727 for (i = 1; i < lh->opcode_base; ++i)
7728 {
7729 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
7730 line_ptr += 1;
7731 }
7732
a738430d 7733 /* Read directory table. */
debd256d
JB
7734 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7735 {
7736 line_ptr += bytes_read;
7737 add_include_dir (lh, cur_dir);
7738 }
7739 line_ptr += bytes_read;
7740
a738430d 7741 /* Read file name table. */
debd256d
JB
7742 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7743 {
7744 unsigned int dir_index, mod_time, length;
7745
7746 line_ptr += bytes_read;
7747 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7748 line_ptr += bytes_read;
7749 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7750 line_ptr += bytes_read;
7751 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7752 line_ptr += bytes_read;
7753
7754 add_file_name (lh, cur_file, dir_index, mod_time, length);
7755 }
7756 line_ptr += bytes_read;
7757 lh->statement_program_start = line_ptr;
7758
dce234bc
PP
7759 if (line_ptr > (dwarf2_per_objfile->line.buffer
7760 + dwarf2_per_objfile->line.size))
4d3c2250 7761 complaint (&symfile_complaints,
e2e0b3e5 7762 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
7763
7764 discard_cleanups (back_to);
7765 return lh;
7766}
c906108c 7767
5fb290d7
DJ
7768/* This function exists to work around a bug in certain compilers
7769 (particularly GCC 2.95), in which the first line number marker of a
7770 function does not show up until after the prologue, right before
7771 the second line number marker. This function shifts ADDRESS down
7772 to the beginning of the function if necessary, and is called on
7773 addresses passed to record_line. */
7774
7775static CORE_ADDR
e142c38c 7776check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
7777{
7778 struct function_range *fn;
7779
7780 /* Find the function_range containing address. */
e142c38c 7781 if (!cu->first_fn)
5fb290d7
DJ
7782 return address;
7783
e142c38c
DJ
7784 if (!cu->cached_fn)
7785 cu->cached_fn = cu->first_fn;
5fb290d7 7786
e142c38c 7787 fn = cu->cached_fn;
5fb290d7
DJ
7788 while (fn)
7789 if (fn->lowpc <= address && fn->highpc > address)
7790 goto found;
7791 else
7792 fn = fn->next;
7793
e142c38c
DJ
7794 fn = cu->first_fn;
7795 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
7796 if (fn->lowpc <= address && fn->highpc > address)
7797 goto found;
7798 else
7799 fn = fn->next;
7800
7801 return address;
7802
7803 found:
7804 if (fn->seen_line)
7805 return address;
7806 if (address != fn->lowpc)
4d3c2250 7807 complaint (&symfile_complaints,
e2e0b3e5 7808 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 7809 (unsigned long) address, fn->name);
5fb290d7
DJ
7810 fn->seen_line = 1;
7811 return fn->lowpc;
7812}
7813
aaa75496
JB
7814/* Decode the Line Number Program (LNP) for the given line_header
7815 structure and CU. The actual information extracted and the type
7816 of structures created from the LNP depends on the value of PST.
7817
7818 1. If PST is NULL, then this procedure uses the data from the program
7819 to create all necessary symbol tables, and their linetables.
7820 The compilation directory of the file is passed in COMP_DIR,
7821 and must not be NULL.
7822
7823 2. If PST is not NULL, this procedure reads the program to determine
7824 the list of files included by the unit represented by PST, and
7825 builds all the associated partial symbol tables. In this case,
7826 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
7827 is not used to compute the full name of the symtab, and therefore
7828 omitting it when building the partial symtab does not introduce
7829 the potential for inconsistency - a partial symtab and its associated
7830 symbtab having a different fullname -). */
debd256d 7831
c906108c 7832static void
debd256d 7833dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 7834 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 7835{
a8c50c1f 7836 gdb_byte *line_ptr, *extended_end;
fe1b8b76 7837 gdb_byte *line_end;
a8c50c1f 7838 unsigned int bytes_read, extended_len;
c906108c 7839 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
7840 CORE_ADDR baseaddr;
7841 struct objfile *objfile = cu->objfile;
fbf65064 7842 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 7843 const int decode_for_pst_p = (pst != NULL);
cb1df416 7844 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
7845
7846 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7847
debd256d
JB
7848 line_ptr = lh->statement_program_start;
7849 line_end = lh->statement_program_end;
c906108c
SS
7850
7851 /* Read the statement sequences until there's nothing left. */
7852 while (line_ptr < line_end)
7853 {
7854 /* state machine registers */
7855 CORE_ADDR address = 0;
7856 unsigned int file = 1;
7857 unsigned int line = 1;
7858 unsigned int column = 0;
debd256d 7859 int is_stmt = lh->default_is_stmt;
c906108c
SS
7860 int basic_block = 0;
7861 int end_sequence = 0;
fbf65064 7862 CORE_ADDR addr;
c906108c 7863
aaa75496 7864 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 7865 {
aaa75496 7866 /* Start a subfile for the current file of the state machine. */
debd256d
JB
7867 /* lh->include_dirs and lh->file_names are 0-based, but the
7868 directory and file name numbers in the statement program
7869 are 1-based. */
7870 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 7871 char *dir = NULL;
a738430d 7872
debd256d
JB
7873 if (fe->dir_index)
7874 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
7875
7876 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
7877 }
7878
a738430d 7879 /* Decode the table. */
c5aa993b 7880 while (!end_sequence)
c906108c
SS
7881 {
7882 op_code = read_1_byte (abfd, line_ptr);
7883 line_ptr += 1;
59205f5a
JB
7884 if (line_ptr > line_end)
7885 {
7886 dwarf2_debug_line_missing_end_sequence_complaint ();
7887 break;
7888 }
9aa1fe7e 7889
debd256d 7890 if (op_code >= lh->opcode_base)
a738430d
MK
7891 {
7892 /* Special operand. */
debd256d
JB
7893 adj_opcode = op_code - lh->opcode_base;
7894 address += (adj_opcode / lh->line_range)
7895 * lh->minimum_instruction_length;
7896 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 7897 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7898 dwarf2_debug_line_missing_file_complaint ();
7899 else
7900 {
7901 lh->file_names[file - 1].included_p = 1;
ca5f395d 7902 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
7903 {
7904 if (last_subfile != current_subfile)
7905 {
7906 addr = gdbarch_addr_bits_remove (gdbarch, address);
7907 if (last_subfile)
7908 record_line (last_subfile, 0, addr);
7909 last_subfile = current_subfile;
7910 }
25e43795 7911 /* Append row to matrix using current values. */
fbf65064
UW
7912 addr = check_cu_functions (address, cu);
7913 addr = gdbarch_addr_bits_remove (gdbarch, addr);
7914 record_line (current_subfile, line, addr);
366da635 7915 }
25e43795 7916 }
ca5f395d 7917 basic_block = 0;
9aa1fe7e
GK
7918 }
7919 else switch (op_code)
c906108c
SS
7920 {
7921 case DW_LNS_extended_op:
a8c50c1f 7922 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 7923 line_ptr += bytes_read;
a8c50c1f 7924 extended_end = line_ptr + extended_len;
c906108c
SS
7925 extended_op = read_1_byte (abfd, line_ptr);
7926 line_ptr += 1;
7927 switch (extended_op)
7928 {
7929 case DW_LNE_end_sequence:
7930 end_sequence = 1;
c906108c
SS
7931 break;
7932 case DW_LNE_set_address:
e7c27a73 7933 address = read_address (abfd, line_ptr, cu, &bytes_read);
107d2387
AC
7934 line_ptr += bytes_read;
7935 address += baseaddr;
c906108c
SS
7936 break;
7937 case DW_LNE_define_file:
debd256d
JB
7938 {
7939 char *cur_file;
7940 unsigned int dir_index, mod_time, length;
7941
7942 cur_file = read_string (abfd, line_ptr, &bytes_read);
7943 line_ptr += bytes_read;
7944 dir_index =
7945 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7946 line_ptr += bytes_read;
7947 mod_time =
7948 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7949 line_ptr += bytes_read;
7950 length =
7951 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7952 line_ptr += bytes_read;
7953 add_file_name (lh, cur_file, dir_index, mod_time, length);
7954 }
c906108c 7955 break;
d0c6ba3d
CC
7956 case DW_LNE_set_discriminator:
7957 /* The discriminator is not interesting to the debugger;
7958 just ignore it. */
7959 line_ptr = extended_end;
7960 break;
c906108c 7961 default:
4d3c2250 7962 complaint (&symfile_complaints,
e2e0b3e5 7963 _("mangled .debug_line section"));
debd256d 7964 return;
c906108c 7965 }
a8c50c1f
DJ
7966 /* Make sure that we parsed the extended op correctly. If e.g.
7967 we expected a different address size than the producer used,
7968 we may have read the wrong number of bytes. */
7969 if (line_ptr != extended_end)
7970 {
7971 complaint (&symfile_complaints,
7972 _("mangled .debug_line section"));
7973 return;
7974 }
c906108c
SS
7975 break;
7976 case DW_LNS_copy:
59205f5a 7977 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7978 dwarf2_debug_line_missing_file_complaint ();
7979 else
366da635 7980 {
25e43795 7981 lh->file_names[file - 1].included_p = 1;
ca5f395d 7982 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
7983 {
7984 if (last_subfile != current_subfile)
7985 {
7986 addr = gdbarch_addr_bits_remove (gdbarch, address);
7987 if (last_subfile)
7988 record_line (last_subfile, 0, addr);
7989 last_subfile = current_subfile;
7990 }
7991 addr = check_cu_functions (address, cu);
7992 addr = gdbarch_addr_bits_remove (gdbarch, addr);
7993 record_line (current_subfile, line, addr);
7994 }
366da635 7995 }
c906108c
SS
7996 basic_block = 0;
7997 break;
7998 case DW_LNS_advance_pc:
debd256d 7999 address += lh->minimum_instruction_length
c906108c
SS
8000 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8001 line_ptr += bytes_read;
8002 break;
8003 case DW_LNS_advance_line:
8004 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
8005 line_ptr += bytes_read;
8006 break;
8007 case DW_LNS_set_file:
debd256d 8008 {
a738430d
MK
8009 /* The arrays lh->include_dirs and lh->file_names are
8010 0-based, but the directory and file name numbers in
8011 the statement program are 1-based. */
debd256d 8012 struct file_entry *fe;
4f1520fb 8013 char *dir = NULL;
a738430d 8014
debd256d
JB
8015 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8016 line_ptr += bytes_read;
59205f5a 8017 if (lh->num_file_names < file || file == 0)
25e43795
DJ
8018 dwarf2_debug_line_missing_file_complaint ();
8019 else
8020 {
8021 fe = &lh->file_names[file - 1];
8022 if (fe->dir_index)
8023 dir = lh->include_dirs[fe->dir_index - 1];
8024 if (!decode_for_pst_p)
8025 {
8026 last_subfile = current_subfile;
8027 dwarf2_start_subfile (fe->name, dir, comp_dir);
8028 }
8029 }
debd256d 8030 }
c906108c
SS
8031 break;
8032 case DW_LNS_set_column:
8033 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8034 line_ptr += bytes_read;
8035 break;
8036 case DW_LNS_negate_stmt:
8037 is_stmt = (!is_stmt);
8038 break;
8039 case DW_LNS_set_basic_block:
8040 basic_block = 1;
8041 break;
c2c6d25f
JM
8042 /* Add to the address register of the state machine the
8043 address increment value corresponding to special opcode
a738430d
MK
8044 255. I.e., this value is scaled by the minimum
8045 instruction length since special opcode 255 would have
8046 scaled the the increment. */
c906108c 8047 case DW_LNS_const_add_pc:
debd256d
JB
8048 address += (lh->minimum_instruction_length
8049 * ((255 - lh->opcode_base) / lh->line_range));
c906108c
SS
8050 break;
8051 case DW_LNS_fixed_advance_pc:
8052 address += read_2_bytes (abfd, line_ptr);
8053 line_ptr += 2;
8054 break;
9aa1fe7e 8055 default:
a738430d
MK
8056 {
8057 /* Unknown standard opcode, ignore it. */
9aa1fe7e 8058 int i;
a738430d 8059
debd256d 8060 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
8061 {
8062 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8063 line_ptr += bytes_read;
8064 }
8065 }
c906108c
SS
8066 }
8067 }
59205f5a
JB
8068 if (lh->num_file_names < file || file == 0)
8069 dwarf2_debug_line_missing_file_complaint ();
8070 else
8071 {
8072 lh->file_names[file - 1].included_p = 1;
8073 if (!decode_for_pst_p)
fbf65064
UW
8074 {
8075 addr = gdbarch_addr_bits_remove (gdbarch, address);
8076 record_line (current_subfile, 0, addr);
8077 }
59205f5a 8078 }
c906108c 8079 }
aaa75496
JB
8080
8081 if (decode_for_pst_p)
8082 {
8083 int file_index;
8084
8085 /* Now that we're done scanning the Line Header Program, we can
8086 create the psymtab of each included file. */
8087 for (file_index = 0; file_index < lh->num_file_names; file_index++)
8088 if (lh->file_names[file_index].included_p == 1)
8089 {
5b5464ad
JB
8090 const struct file_entry fe = lh->file_names [file_index];
8091 char *include_name = fe.name;
8092 char *dir_name = NULL;
8093 char *pst_filename = pst->filename;
8094
8095 if (fe.dir_index)
8096 dir_name = lh->include_dirs[fe.dir_index - 1];
8097
8098 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
8099 {
1754f103
MK
8100 include_name = concat (dir_name, SLASH_STRING,
8101 include_name, (char *)NULL);
5b5464ad
JB
8102 make_cleanup (xfree, include_name);
8103 }
8104
8105 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
8106 {
1754f103
MK
8107 pst_filename = concat (pst->dirname, SLASH_STRING,
8108 pst_filename, (char *)NULL);
5b5464ad
JB
8109 make_cleanup (xfree, pst_filename);
8110 }
8111
8112 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
8113 dwarf2_create_include_psymtab (include_name, pst, objfile);
8114 }
8115 }
cb1df416
DJ
8116 else
8117 {
8118 /* Make sure a symtab is created for every file, even files
8119 which contain only variables (i.e. no code with associated
8120 line numbers). */
8121
8122 int i;
8123 struct file_entry *fe;
8124
8125 for (i = 0; i < lh->num_file_names; i++)
8126 {
8127 char *dir = NULL;
8128 fe = &lh->file_names[i];
8129 if (fe->dir_index)
8130 dir = lh->include_dirs[fe->dir_index - 1];
8131 dwarf2_start_subfile (fe->name, dir, comp_dir);
8132
8133 /* Skip the main file; we don't need it, and it must be
8134 allocated last, so that it will show up before the
8135 non-primary symtabs in the objfile's symtab list. */
8136 if (current_subfile == first_subfile)
8137 continue;
8138
8139 if (current_subfile->symtab == NULL)
8140 current_subfile->symtab = allocate_symtab (current_subfile->name,
8141 cu->objfile);
8142 fe->symtab = current_subfile->symtab;
8143 }
8144 }
c906108c
SS
8145}
8146
8147/* Start a subfile for DWARF. FILENAME is the name of the file and
8148 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
8149 or NULL if not known. COMP_DIR is the compilation directory for the
8150 linetable's compilation unit or NULL if not known.
c906108c
SS
8151 This routine tries to keep line numbers from identical absolute and
8152 relative file names in a common subfile.
8153
8154 Using the `list' example from the GDB testsuite, which resides in
8155 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
8156 of /srcdir/list0.c yields the following debugging information for list0.c:
8157
c5aa993b
JM
8158 DW_AT_name: /srcdir/list0.c
8159 DW_AT_comp_dir: /compdir
357e46e7 8160 files.files[0].name: list0.h
c5aa993b 8161 files.files[0].dir: /srcdir
357e46e7 8162 files.files[1].name: list0.c
c5aa993b 8163 files.files[1].dir: /srcdir
c906108c
SS
8164
8165 The line number information for list0.c has to end up in a single
4f1520fb
FR
8166 subfile, so that `break /srcdir/list0.c:1' works as expected.
8167 start_subfile will ensure that this happens provided that we pass the
8168 concatenation of files.files[1].dir and files.files[1].name as the
8169 subfile's name. */
c906108c
SS
8170
8171static void
4f1520fb 8172dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 8173{
4f1520fb
FR
8174 char *fullname;
8175
8176 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
8177 `start_symtab' will always pass the contents of DW_AT_comp_dir as
8178 second argument to start_subfile. To be consistent, we do the
8179 same here. In order not to lose the line information directory,
8180 we concatenate it to the filename when it makes sense.
8181 Note that the Dwarf3 standard says (speaking of filenames in line
8182 information): ``The directory index is ignored for file names
8183 that represent full path names''. Thus ignoring dirname in the
8184 `else' branch below isn't an issue. */
c906108c 8185
d5166ae1 8186 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
8187 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
8188 else
8189 fullname = filename;
c906108c 8190
4f1520fb
FR
8191 start_subfile (fullname, comp_dir);
8192
8193 if (fullname != filename)
8194 xfree (fullname);
c906108c
SS
8195}
8196
4c2df51b
DJ
8197static void
8198var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 8199 struct dwarf2_cu *cu)
4c2df51b 8200{
e7c27a73
DJ
8201 struct objfile *objfile = cu->objfile;
8202 struct comp_unit_head *cu_header = &cu->header;
8203
4c2df51b
DJ
8204 /* NOTE drow/2003-01-30: There used to be a comment and some special
8205 code here to turn a symbol with DW_AT_external and a
8206 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
8207 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
8208 with some versions of binutils) where shared libraries could have
8209 relocations against symbols in their debug information - the
8210 minimal symbol would have the right address, but the debug info
8211 would not. It's no longer necessary, because we will explicitly
8212 apply relocations when we read in the debug information now. */
8213
8214 /* A DW_AT_location attribute with no contents indicates that a
8215 variable has been optimized away. */
8216 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
8217 {
8218 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8219 return;
8220 }
8221
8222 /* Handle one degenerate form of location expression specially, to
8223 preserve GDB's previous behavior when section offsets are
8224 specified. If this is just a DW_OP_addr then mark this symbol
8225 as LOC_STATIC. */
8226
8227 if (attr_form_is_block (attr)
8228 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
8229 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
8230 {
891d2f0b 8231 unsigned int dummy;
4c2df51b
DJ
8232
8233 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 8234 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 8235 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
8236 fixup_symbol_section (sym, objfile);
8237 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
8238 SYMBOL_SECTION (sym));
4c2df51b
DJ
8239 return;
8240 }
8241
8242 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
8243 expression evaluator, and use LOC_COMPUTED only when necessary
8244 (i.e. when the value of a register or memory location is
8245 referenced, or a thread-local block, etc.). Then again, it might
8246 not be worthwhile. I'm assuming that it isn't unless performance
8247 or memory numbers show me otherwise. */
8248
e7c27a73 8249 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
8250 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8251}
8252
c906108c
SS
8253/* Given a pointer to a DWARF information entry, figure out if we need
8254 to make a symbol table entry for it, and if so, create a new entry
8255 and return a pointer to it.
8256 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 8257 used the passed type. */
c906108c
SS
8258
8259static struct symbol *
e7c27a73 8260new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 8261{
e7c27a73 8262 struct objfile *objfile = cu->objfile;
c906108c
SS
8263 struct symbol *sym = NULL;
8264 char *name;
8265 struct attribute *attr = NULL;
8266 struct attribute *attr2 = NULL;
e142c38c 8267 CORE_ADDR baseaddr;
edb3359d 8268 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
8269
8270 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8271
5c4e30ca 8272 if (die->tag != DW_TAG_namespace)
e142c38c 8273 name = dwarf2_linkage_name (die, cu);
5c4e30ca
DC
8274 else
8275 name = TYPE_NAME (type);
8276
c906108c
SS
8277 if (name)
8278 {
4a146b47 8279 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
8280 sizeof (struct symbol));
8281 OBJSTAT (objfile, n_syms++);
8282 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
8283
8284 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 8285 SYMBOL_LANGUAGE (sym) = cu->language;
2de7ced7 8286 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
c906108c
SS
8287
8288 /* Default assumptions.
c5aa993b 8289 Use the passed type or decode it from the die. */
176620f1 8290 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 8291 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
8292 if (type != NULL)
8293 SYMBOL_TYPE (sym) = type;
8294 else
e7c27a73 8295 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
8296 attr = dwarf2_attr (die,
8297 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
8298 cu);
c906108c
SS
8299 if (attr)
8300 {
8301 SYMBOL_LINE (sym) = DW_UNSND (attr);
8302 }
cb1df416 8303
edb3359d
DJ
8304 attr = dwarf2_attr (die,
8305 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
8306 cu);
cb1df416
DJ
8307 if (attr)
8308 {
8309 int file_index = DW_UNSND (attr);
8310 if (cu->line_header == NULL
8311 || file_index > cu->line_header->num_file_names)
8312 complaint (&symfile_complaints,
8313 _("file index out of range"));
1c3d648d 8314 else if (file_index > 0)
cb1df416
DJ
8315 {
8316 struct file_entry *fe;
8317 fe = &cu->line_header->file_names[file_index - 1];
8318 SYMBOL_SYMTAB (sym) = fe->symtab;
8319 }
8320 }
8321
c906108c
SS
8322 switch (die->tag)
8323 {
8324 case DW_TAG_label:
e142c38c 8325 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
8326 if (attr)
8327 {
8328 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
8329 }
8330 SYMBOL_CLASS (sym) = LOC_LABEL;
8331 break;
8332 case DW_TAG_subprogram:
8333 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8334 finish_block. */
8335 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 8336 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
8337 if ((attr2 && (DW_UNSND (attr2) != 0))
8338 || cu->language == language_ada)
c906108c 8339 {
2cfa0c8d
JB
8340 /* Subprograms marked external are stored as a global symbol.
8341 Ada subprograms, whether marked external or not, are always
8342 stored as a global symbol, because we want to be able to
8343 access them globally. For instance, we want to be able
8344 to break on a nested subprogram without having to
8345 specify the context. */
c906108c
SS
8346 add_symbol_to_list (sym, &global_symbols);
8347 }
8348 else
8349 {
e142c38c 8350 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8351 }
8352 break;
edb3359d
DJ
8353 case DW_TAG_inlined_subroutine:
8354 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8355 finish_block. */
8356 SYMBOL_CLASS (sym) = LOC_BLOCK;
8357 SYMBOL_INLINED (sym) = 1;
8358 /* Do not add the symbol to any lists. It will be found via
8359 BLOCK_FUNCTION from the blockvector. */
8360 break;
c906108c
SS
8361 case DW_TAG_variable:
8362 /* Compilation with minimal debug info may result in variables
8363 with missing type entries. Change the misleading `void' type
8364 to something sensible. */
8365 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 8366 SYMBOL_TYPE (sym)
46bf5051 8367 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 8368
e142c38c 8369 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8370 if (attr)
8371 {
e7c27a73 8372 dwarf2_const_value (attr, sym, cu);
e142c38c 8373 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
8374 if (attr2 && (DW_UNSND (attr2) != 0))
8375 add_symbol_to_list (sym, &global_symbols);
8376 else
e142c38c 8377 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8378 break;
8379 }
e142c38c 8380 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8381 if (attr)
8382 {
e7c27a73 8383 var_decode_location (attr, sym, cu);
e142c38c 8384 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 8385 if (attr2 && (DW_UNSND (attr2) != 0))
4c2df51b 8386 add_symbol_to_list (sym, &global_symbols);
c906108c 8387 else
e142c38c 8388 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8389 }
8390 else
8391 {
8392 /* We do not know the address of this symbol.
c5aa993b
JM
8393 If it is an external symbol and we have type information
8394 for it, enter the symbol as a LOC_UNRESOLVED symbol.
8395 The address of the variable will then be determined from
8396 the minimal symbol table whenever the variable is
8397 referenced. */
e142c38c 8398 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 8399 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 8400 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c
SS
8401 {
8402 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
de40b933 8403 add_symbol_to_list (sym, cu->list_in_scope);
c906108c 8404 }
442ddf59
JK
8405 else if (!die_is_declaration (die, cu))
8406 {
8407 /* Use the default LOC_OPTIMIZED_OUT class. */
8408 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
8409 add_symbol_to_list (sym, cu->list_in_scope);
8410 }
c906108c
SS
8411 }
8412 break;
8413 case DW_TAG_formal_parameter:
edb3359d
DJ
8414 /* If we are inside a function, mark this as an argument. If
8415 not, we might be looking at an argument to an inlined function
8416 when we do not have enough information to show inlined frames;
8417 pretend it's a local variable in that case so that the user can
8418 still see it. */
8419 if (context_stack_depth > 0
8420 && context_stack[context_stack_depth - 1].name != NULL)
8421 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 8422 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8423 if (attr)
8424 {
e7c27a73 8425 var_decode_location (attr, sym, cu);
c906108c 8426 }
e142c38c 8427 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8428 if (attr)
8429 {
e7c27a73 8430 dwarf2_const_value (attr, sym, cu);
c906108c 8431 }
e142c38c 8432 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8433 break;
8434 case DW_TAG_unspecified_parameters:
8435 /* From varargs functions; gdb doesn't seem to have any
8436 interest in this information, so just ignore it for now.
8437 (FIXME?) */
8438 break;
8439 case DW_TAG_class_type:
680b30c7 8440 case DW_TAG_interface_type:
c906108c
SS
8441 case DW_TAG_structure_type:
8442 case DW_TAG_union_type:
72019c9c 8443 case DW_TAG_set_type:
c906108c
SS
8444 case DW_TAG_enumeration_type:
8445 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 8446 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 8447
63d06c5c
DC
8448 /* Make sure that the symbol includes appropriate enclosing
8449 classes/namespaces in its name. These are calculated in
134d01f1 8450 read_structure_type, and the correct name is saved in
63d06c5c
DC
8451 the type. */
8452
987504bb
JJ
8453 if (cu->language == language_cplus
8454 || cu->language == language_java)
c906108c 8455 {
63d06c5c
DC
8456 struct type *type = SYMBOL_TYPE (sym);
8457
8458 if (TYPE_TAG_NAME (type) != NULL)
8459 {
8460 /* FIXME: carlton/2003-11-10: Should this use
8461 SYMBOL_SET_NAMES instead? (The same problem also
d8151005
DJ
8462 arises further down in this function.) */
8463 /* The type's name is already allocated along with
8464 this objfile, so we don't need to duplicate it
8465 for the symbol. */
8466 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
63d06c5c 8467 }
c906108c 8468 }
63d06c5c
DC
8469
8470 {
987504bb 8471 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
8472 really ever be static objects: otherwise, if you try
8473 to, say, break of a class's method and you're in a file
8474 which doesn't mention that class, it won't work unless
8475 the check for all static symbols in lookup_symbol_aux
8476 saves you. See the OtherFileClass tests in
8477 gdb.c++/namespace.exp. */
8478
8479 struct pending **list_to_add;
8480
e142c38c 8481 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
8482 && (cu->language == language_cplus
8483 || cu->language == language_java)
e142c38c 8484 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
8485
8486 add_symbol_to_list (sym, list_to_add);
8487
8488 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 8489 defines a typedef for "foo". A Java class declaration also
5eeb2539 8490 defines a typedef for the class. */
987504bb 8491 if (cu->language == language_cplus
8c6860bb
JB
8492 || cu->language == language_java
8493 || cu->language == language_ada)
63d06c5c 8494 {
d8151005
DJ
8495 /* The symbol's name is already allocated along with
8496 this objfile, so we don't need to duplicate it for
8497 the type. */
63d06c5c 8498 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 8499 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
8500 }
8501 }
c906108c
SS
8502 break;
8503 case DW_TAG_typedef:
0114d602 8504 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
63d06c5c
DC
8505 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8506 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 8507 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 8508 break;
c906108c 8509 case DW_TAG_base_type:
a02abb62 8510 case DW_TAG_subrange_type:
c906108c 8511 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 8512 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 8513 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8514 break;
8515 case DW_TAG_enumerator:
0114d602 8516 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
e142c38c 8517 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8518 if (attr)
8519 {
e7c27a73 8520 dwarf2_const_value (attr, sym, cu);
c906108c 8521 }
63d06c5c
DC
8522 {
8523 /* NOTE: carlton/2003-11-10: See comment above in the
8524 DW_TAG_class_type, etc. block. */
8525
8526 struct pending **list_to_add;
8527
e142c38c 8528 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
8529 && (cu->language == language_cplus
8530 || cu->language == language_java)
e142c38c 8531 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
8532
8533 add_symbol_to_list (sym, list_to_add);
8534 }
c906108c 8535 break;
5c4e30ca
DC
8536 case DW_TAG_namespace:
8537 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8538 add_symbol_to_list (sym, &global_symbols);
8539 break;
c906108c
SS
8540 default:
8541 /* Not a tag we recognize. Hopefully we aren't processing
8542 trash data, but since we must specifically ignore things
8543 we don't recognize, there is nothing else we should do at
8544 this point. */
e2e0b3e5 8545 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 8546 dwarf_tag_name (die->tag));
c906108c
SS
8547 break;
8548 }
df8a16a1
DJ
8549
8550 /* For the benefit of old versions of GCC, check for anonymous
8551 namespaces based on the demangled name. */
8552 if (!processing_has_namespace_info
8553 && cu->language == language_cplus
8554 && dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu) != NULL)
8555 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
8556 }
8557 return (sym);
8558}
8559
8560/* Copy constant value from an attribute to a symbol. */
8561
8562static void
107d2387 8563dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 8564 struct dwarf2_cu *cu)
c906108c 8565{
e7c27a73
DJ
8566 struct objfile *objfile = cu->objfile;
8567 struct comp_unit_head *cu_header = &cu->header;
e17a4113
UW
8568 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
8569 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
c906108c
SS
8570 struct dwarf_block *blk;
8571
8572 switch (attr->form)
8573 {
8574 case DW_FORM_addr:
107d2387 8575 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
3567439c 8576 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
8577 cu_header->addr_size,
8578 TYPE_LENGTH (SYMBOL_TYPE
8579 (sym)));
4e38b386 8580 SYMBOL_VALUE_BYTES (sym) =
4a146b47 8581 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
fbd9dcd3
AC
8582 /* NOTE: cagney/2003-05-09: In-lined store_address call with
8583 it's body - store_unsigned_integer. */
8584 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
e17a4113 8585 DW_ADDR (attr), byte_order);
c906108c
SS
8586 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8587 break;
4ac36638 8588 case DW_FORM_string:
93b5768b
PA
8589 case DW_FORM_strp:
8590 /* DW_STRING is already allocated on the obstack, point directly
8591 to it. */
8592 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
8593 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8594 break;
c906108c
SS
8595 case DW_FORM_block1:
8596 case DW_FORM_block2:
8597 case DW_FORM_block4:
8598 case DW_FORM_block:
8599 blk = DW_BLOCK (attr);
8600 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 8601 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
8602 blk->size,
8603 TYPE_LENGTH (SYMBOL_TYPE
8604 (sym)));
4e38b386 8605 SYMBOL_VALUE_BYTES (sym) =
4a146b47 8606 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
8607 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
8608 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8609 break;
2df3850c
JM
8610
8611 /* The DW_AT_const_value attributes are supposed to carry the
8612 symbol's value "represented as it would be on the target
8613 architecture." By the time we get here, it's already been
8614 converted to host endianness, so we just need to sign- or
8615 zero-extend it as appropriate. */
8616 case DW_FORM_data1:
8617 dwarf2_const_value_data (attr, sym, 8);
8618 break;
c906108c 8619 case DW_FORM_data2:
2df3850c
JM
8620 dwarf2_const_value_data (attr, sym, 16);
8621 break;
c906108c 8622 case DW_FORM_data4:
2df3850c
JM
8623 dwarf2_const_value_data (attr, sym, 32);
8624 break;
c906108c 8625 case DW_FORM_data8:
2df3850c
JM
8626 dwarf2_const_value_data (attr, sym, 64);
8627 break;
8628
c906108c 8629 case DW_FORM_sdata:
2df3850c
JM
8630 SYMBOL_VALUE (sym) = DW_SND (attr);
8631 SYMBOL_CLASS (sym) = LOC_CONST;
8632 break;
8633
c906108c
SS
8634 case DW_FORM_udata:
8635 SYMBOL_VALUE (sym) = DW_UNSND (attr);
8636 SYMBOL_CLASS (sym) = LOC_CONST;
8637 break;
2df3850c 8638
c906108c 8639 default:
4d3c2250 8640 complaint (&symfile_complaints,
e2e0b3e5 8641 _("unsupported const value attribute form: '%s'"),
4d3c2250 8642 dwarf_form_name (attr->form));
c906108c
SS
8643 SYMBOL_VALUE (sym) = 0;
8644 SYMBOL_CLASS (sym) = LOC_CONST;
8645 break;
8646 }
8647}
8648
2df3850c
JM
8649
8650/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
8651 or zero-extend it as appropriate for the symbol's type. */
8652static void
8653dwarf2_const_value_data (struct attribute *attr,
8654 struct symbol *sym,
8655 int bits)
8656{
8657 LONGEST l = DW_UNSND (attr);
8658
8659 if (bits < sizeof (l) * 8)
8660 {
8661 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
8662 l &= ((LONGEST) 1 << bits) - 1;
8663 else
bf9198f1 8664 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
8665 }
8666
8667 SYMBOL_VALUE (sym) = l;
8668 SYMBOL_CLASS (sym) = LOC_CONST;
8669}
8670
8671
c906108c
SS
8672/* Return the type of the die in question using its DW_AT_type attribute. */
8673
8674static struct type *
e7c27a73 8675die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8676{
8677 struct type *type;
8678 struct attribute *type_attr;
8679 struct die_info *type_die;
c906108c 8680
e142c38c 8681 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
8682 if (!type_attr)
8683 {
8684 /* A missing DW_AT_type represents a void type. */
46bf5051 8685 return objfile_type (cu->objfile)->builtin_void;
c906108c 8686 }
348e048f
DE
8687
8688 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 8689
e7c27a73 8690 type = tag_type_to_type (type_die, cu);
c906108c
SS
8691 if (!type)
8692 {
d97bc12b 8693 dump_die_for_error (type_die);
8a3fe4f8 8694 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
e7c27a73 8695 cu->objfile->name);
c906108c
SS
8696 }
8697 return type;
8698}
8699
8700/* Return the containing type of the die in question using its
8701 DW_AT_containing_type attribute. */
8702
8703static struct type *
e7c27a73 8704die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8705{
8706 struct type *type = NULL;
8707 struct attribute *type_attr;
8708 struct die_info *type_die = NULL;
c906108c 8709
e142c38c 8710 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
c906108c
SS
8711 if (type_attr)
8712 {
348e048f 8713 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
e7c27a73 8714 type = tag_type_to_type (type_die, cu);
c906108c
SS
8715 }
8716 if (!type)
8717 {
8718 if (type_die)
d97bc12b 8719 dump_die_for_error (type_die);
8a3fe4f8 8720 error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
e7c27a73 8721 cu->objfile->name);
c906108c
SS
8722 }
8723 return type;
8724}
8725
c906108c 8726static struct type *
e7c27a73 8727tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8728{
f792889a
DJ
8729 struct type *this_type;
8730
8731 this_type = read_type_die (die, cu);
8732 if (!this_type)
c906108c 8733 {
d97bc12b 8734 dump_die_for_error (die);
f792889a
DJ
8735 error (_("Dwarf Error: Cannot find type of die [in module %s]"),
8736 cu->objfile->name);
c906108c 8737 }
f792889a 8738 return this_type;
c906108c
SS
8739}
8740
f792889a 8741static struct type *
e7c27a73 8742read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8743{
f792889a
DJ
8744 struct type *this_type;
8745
8746 this_type = get_die_type (die, cu);
8747 if (this_type)
8748 return this_type;
8749
c906108c
SS
8750 switch (die->tag)
8751 {
8752 case DW_TAG_class_type:
680b30c7 8753 case DW_TAG_interface_type:
c906108c
SS
8754 case DW_TAG_structure_type:
8755 case DW_TAG_union_type:
f792889a 8756 this_type = read_structure_type (die, cu);
c906108c
SS
8757 break;
8758 case DW_TAG_enumeration_type:
f792889a 8759 this_type = read_enumeration_type (die, cu);
c906108c
SS
8760 break;
8761 case DW_TAG_subprogram:
8762 case DW_TAG_subroutine_type:
edb3359d 8763 case DW_TAG_inlined_subroutine:
f792889a 8764 this_type = read_subroutine_type (die, cu);
c906108c
SS
8765 break;
8766 case DW_TAG_array_type:
f792889a 8767 this_type = read_array_type (die, cu);
c906108c 8768 break;
72019c9c 8769 case DW_TAG_set_type:
f792889a 8770 this_type = read_set_type (die, cu);
72019c9c 8771 break;
c906108c 8772 case DW_TAG_pointer_type:
f792889a 8773 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
8774 break;
8775 case DW_TAG_ptr_to_member_type:
f792889a 8776 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
8777 break;
8778 case DW_TAG_reference_type:
f792889a 8779 this_type = read_tag_reference_type (die, cu);
c906108c
SS
8780 break;
8781 case DW_TAG_const_type:
f792889a 8782 this_type = read_tag_const_type (die, cu);
c906108c
SS
8783 break;
8784 case DW_TAG_volatile_type:
f792889a 8785 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
8786 break;
8787 case DW_TAG_string_type:
f792889a 8788 this_type = read_tag_string_type (die, cu);
c906108c
SS
8789 break;
8790 case DW_TAG_typedef:
f792889a 8791 this_type = read_typedef (die, cu);
c906108c 8792 break;
a02abb62 8793 case DW_TAG_subrange_type:
f792889a 8794 this_type = read_subrange_type (die, cu);
a02abb62 8795 break;
c906108c 8796 case DW_TAG_base_type:
f792889a 8797 this_type = read_base_type (die, cu);
c906108c 8798 break;
81a17f79 8799 case DW_TAG_unspecified_type:
f792889a 8800 this_type = read_unspecified_type (die, cu);
81a17f79 8801 break;
0114d602
DJ
8802 case DW_TAG_namespace:
8803 this_type = read_namespace_type (die, cu);
8804 break;
c906108c 8805 default:
a1f5b845 8806 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 8807 dwarf_tag_name (die->tag));
c906108c
SS
8808 break;
8809 }
63d06c5c 8810
f792889a 8811 return this_type;
63d06c5c
DC
8812}
8813
fdde2d81 8814/* Return the name of the namespace/class that DIE is defined within,
0114d602 8815 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 8816
0114d602
DJ
8817 For example, if we're within the method foo() in the following
8818 code:
8819
8820 namespace N {
8821 class C {
8822 void foo () {
8823 }
8824 };
8825 }
8826
8827 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
8828
8829static char *
e142c38c 8830determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 8831{
0114d602
DJ
8832 struct die_info *parent, *spec_die;
8833 struct dwarf2_cu *spec_cu;
8834 struct type *parent_type;
63d06c5c 8835
987504bb
JJ
8836 if (cu->language != language_cplus
8837 && cu->language != language_java)
0114d602
DJ
8838 return "";
8839
8840 /* We have to be careful in the presence of DW_AT_specification.
8841 For example, with GCC 3.4, given the code
8842
8843 namespace N {
8844 void foo() {
8845 // Definition of N::foo.
8846 }
8847 }
8848
8849 then we'll have a tree of DIEs like this:
8850
8851 1: DW_TAG_compile_unit
8852 2: DW_TAG_namespace // N
8853 3: DW_TAG_subprogram // declaration of N::foo
8854 4: DW_TAG_subprogram // definition of N::foo
8855 DW_AT_specification // refers to die #3
8856
8857 Thus, when processing die #4, we have to pretend that we're in
8858 the context of its DW_AT_specification, namely the contex of die
8859 #3. */
8860 spec_cu = cu;
8861 spec_die = die_specification (die, &spec_cu);
8862 if (spec_die == NULL)
8863 parent = die->parent;
8864 else
63d06c5c 8865 {
0114d602
DJ
8866 parent = spec_die->parent;
8867 cu = spec_cu;
63d06c5c 8868 }
0114d602
DJ
8869
8870 if (parent == NULL)
8871 return "";
63d06c5c 8872 else
0114d602
DJ
8873 switch (parent->tag)
8874 {
63d06c5c 8875 case DW_TAG_namespace:
0114d602
DJ
8876 parent_type = read_type_die (parent, cu);
8877 /* We give a name to even anonymous namespaces. */
8878 return TYPE_TAG_NAME (parent_type);
63d06c5c 8879 case DW_TAG_class_type:
680b30c7 8880 case DW_TAG_interface_type:
63d06c5c 8881 case DW_TAG_structure_type:
0114d602
DJ
8882 case DW_TAG_union_type:
8883 parent_type = read_type_die (parent, cu);
8884 if (TYPE_TAG_NAME (parent_type) != NULL)
8885 return TYPE_TAG_NAME (parent_type);
8886 else
8887 /* An anonymous structure is only allowed non-static data
8888 members; no typedefs, no member functions, et cetera.
8889 So it does not need a prefix. */
8890 return "";
63d06c5c 8891 default:
8176b9b8 8892 return determine_prefix (parent, cu);
63d06c5c 8893 }
63d06c5c
DC
8894}
8895
987504bb
JJ
8896/* Return a newly-allocated string formed by concatenating PREFIX and
8897 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
8898 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
8899 perform an obconcat, otherwise allocate storage for the result. The CU argument
8900 is used to determine the language and hence, the appropriate separator. */
8901
8902#define MAX_SEP_LEN 2 /* sizeof ("::") */
63d06c5c
DC
8903
8904static char *
987504bb
JJ
8905typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
8906 struct dwarf2_cu *cu)
63d06c5c 8907{
987504bb 8908 char *sep;
63d06c5c 8909
987504bb
JJ
8910 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
8911 sep = "";
8912 else if (cu->language == language_java)
8913 sep = ".";
8914 else
8915 sep = "::";
63d06c5c 8916
6dd47d34
DE
8917 if (prefix == NULL)
8918 prefix = "";
8919 if (suffix == NULL)
8920 suffix = "";
8921
987504bb
JJ
8922 if (obs == NULL)
8923 {
8924 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
6dd47d34
DE
8925 strcpy (retval, prefix);
8926 strcat (retval, sep);
8927 strcat (retval, suffix);
63d06c5c
DC
8928 return retval;
8929 }
987504bb
JJ
8930 else
8931 {
8932 /* We have an obstack. */
8933 return obconcat (obs, prefix, sep, suffix);
8934 }
63d06c5c
DC
8935}
8936
c906108c
SS
8937/* Return sibling of die, NULL if no sibling. */
8938
f9aca02d 8939static struct die_info *
fba45db2 8940sibling_die (struct die_info *die)
c906108c 8941{
639d11d3 8942 return die->sibling;
c906108c
SS
8943}
8944
8945/* Get linkage name of a die, return NULL if not found. */
8946
8947static char *
e142c38c 8948dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8949{
8950 struct attribute *attr;
8951
e142c38c 8952 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
c906108c
SS
8953 if (attr && DW_STRING (attr))
8954 return DW_STRING (attr);
71c25dea
TT
8955 return dwarf2_name (die, cu);
8956}
8957
8958/* Get name of a die, return NULL if not found. */
8959
8960static char *
8961dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
8962 struct obstack *obstack)
8963{
8964 if (name && cu->language == language_cplus)
8965 {
8966 char *canon_name = cp_canonicalize_string (name);
8967
8968 if (canon_name != NULL)
8969 {
8970 if (strcmp (canon_name, name) != 0)
8971 name = obsavestring (canon_name, strlen (canon_name),
8972 obstack);
8973 xfree (canon_name);
8974 }
8975 }
8976
8977 return name;
c906108c
SS
8978}
8979
9219021c
DC
8980/* Get name of a die, return NULL if not found. */
8981
8982static char *
e142c38c 8983dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
8984{
8985 struct attribute *attr;
8986
e142c38c 8987 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
8988 if (!attr || !DW_STRING (attr))
8989 return NULL;
8990
8991 switch (die->tag)
8992 {
8993 case DW_TAG_compile_unit:
8994 /* Compilation units have a DW_AT_name that is a filename, not
8995 a source language identifier. */
8996 case DW_TAG_enumeration_type:
8997 case DW_TAG_enumerator:
8998 /* These tags always have simple identifiers already; no need
8999 to canonicalize them. */
9000 return DW_STRING (attr);
9001 default:
9002 if (attr->form != GDB_FORM_cached_string)
9003 {
9004 DW_STRING (attr)
9005 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
9006 &cu->objfile->objfile_obstack);
9007 attr->form = GDB_FORM_cached_string;
9008 }
9009 return DW_STRING (attr);
9010 }
9219021c
DC
9011}
9012
9013/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
9014 is none. *EXT_CU is the CU containing DIE on input, and the CU
9015 containing the return value on output. */
9219021c
DC
9016
9017static struct die_info *
f2f0e013 9018dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
9019{
9020 struct attribute *attr;
9219021c 9021
f2f0e013 9022 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
9023 if (attr == NULL)
9024 return NULL;
9025
f2f0e013 9026 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
9027}
9028
c906108c
SS
9029/* Convert a DIE tag into its string name. */
9030
9031static char *
aa1ee363 9032dwarf_tag_name (unsigned tag)
c906108c
SS
9033{
9034 switch (tag)
9035 {
9036 case DW_TAG_padding:
9037 return "DW_TAG_padding";
9038 case DW_TAG_array_type:
9039 return "DW_TAG_array_type";
9040 case DW_TAG_class_type:
9041 return "DW_TAG_class_type";
9042 case DW_TAG_entry_point:
9043 return "DW_TAG_entry_point";
9044 case DW_TAG_enumeration_type:
9045 return "DW_TAG_enumeration_type";
9046 case DW_TAG_formal_parameter:
9047 return "DW_TAG_formal_parameter";
9048 case DW_TAG_imported_declaration:
9049 return "DW_TAG_imported_declaration";
9050 case DW_TAG_label:
9051 return "DW_TAG_label";
9052 case DW_TAG_lexical_block:
9053 return "DW_TAG_lexical_block";
9054 case DW_TAG_member:
9055 return "DW_TAG_member";
9056 case DW_TAG_pointer_type:
9057 return "DW_TAG_pointer_type";
9058 case DW_TAG_reference_type:
9059 return "DW_TAG_reference_type";
9060 case DW_TAG_compile_unit:
9061 return "DW_TAG_compile_unit";
9062 case DW_TAG_string_type:
9063 return "DW_TAG_string_type";
9064 case DW_TAG_structure_type:
9065 return "DW_TAG_structure_type";
9066 case DW_TAG_subroutine_type:
9067 return "DW_TAG_subroutine_type";
9068 case DW_TAG_typedef:
9069 return "DW_TAG_typedef";
9070 case DW_TAG_union_type:
9071 return "DW_TAG_union_type";
9072 case DW_TAG_unspecified_parameters:
9073 return "DW_TAG_unspecified_parameters";
9074 case DW_TAG_variant:
9075 return "DW_TAG_variant";
9076 case DW_TAG_common_block:
9077 return "DW_TAG_common_block";
9078 case DW_TAG_common_inclusion:
9079 return "DW_TAG_common_inclusion";
9080 case DW_TAG_inheritance:
9081 return "DW_TAG_inheritance";
9082 case DW_TAG_inlined_subroutine:
9083 return "DW_TAG_inlined_subroutine";
9084 case DW_TAG_module:
9085 return "DW_TAG_module";
9086 case DW_TAG_ptr_to_member_type:
9087 return "DW_TAG_ptr_to_member_type";
9088 case DW_TAG_set_type:
9089 return "DW_TAG_set_type";
9090 case DW_TAG_subrange_type:
9091 return "DW_TAG_subrange_type";
9092 case DW_TAG_with_stmt:
9093 return "DW_TAG_with_stmt";
9094 case DW_TAG_access_declaration:
9095 return "DW_TAG_access_declaration";
9096 case DW_TAG_base_type:
9097 return "DW_TAG_base_type";
9098 case DW_TAG_catch_block:
9099 return "DW_TAG_catch_block";
9100 case DW_TAG_const_type:
9101 return "DW_TAG_const_type";
9102 case DW_TAG_constant:
9103 return "DW_TAG_constant";
9104 case DW_TAG_enumerator:
9105 return "DW_TAG_enumerator";
9106 case DW_TAG_file_type:
9107 return "DW_TAG_file_type";
9108 case DW_TAG_friend:
9109 return "DW_TAG_friend";
9110 case DW_TAG_namelist:
9111 return "DW_TAG_namelist";
9112 case DW_TAG_namelist_item:
9113 return "DW_TAG_namelist_item";
9114 case DW_TAG_packed_type:
9115 return "DW_TAG_packed_type";
9116 case DW_TAG_subprogram:
9117 return "DW_TAG_subprogram";
9118 case DW_TAG_template_type_param:
9119 return "DW_TAG_template_type_param";
9120 case DW_TAG_template_value_param:
9121 return "DW_TAG_template_value_param";
9122 case DW_TAG_thrown_type:
9123 return "DW_TAG_thrown_type";
9124 case DW_TAG_try_block:
9125 return "DW_TAG_try_block";
9126 case DW_TAG_variant_part:
9127 return "DW_TAG_variant_part";
9128 case DW_TAG_variable:
9129 return "DW_TAG_variable";
9130 case DW_TAG_volatile_type:
9131 return "DW_TAG_volatile_type";
d9fa45fe
DC
9132 case DW_TAG_dwarf_procedure:
9133 return "DW_TAG_dwarf_procedure";
9134 case DW_TAG_restrict_type:
9135 return "DW_TAG_restrict_type";
9136 case DW_TAG_interface_type:
9137 return "DW_TAG_interface_type";
9138 case DW_TAG_namespace:
9139 return "DW_TAG_namespace";
9140 case DW_TAG_imported_module:
9141 return "DW_TAG_imported_module";
9142 case DW_TAG_unspecified_type:
9143 return "DW_TAG_unspecified_type";
9144 case DW_TAG_partial_unit:
9145 return "DW_TAG_partial_unit";
9146 case DW_TAG_imported_unit:
9147 return "DW_TAG_imported_unit";
b7619582
GF
9148 case DW_TAG_condition:
9149 return "DW_TAG_condition";
9150 case DW_TAG_shared_type:
9151 return "DW_TAG_shared_type";
348e048f
DE
9152 case DW_TAG_type_unit:
9153 return "DW_TAG_type_unit";
c906108c
SS
9154 case DW_TAG_MIPS_loop:
9155 return "DW_TAG_MIPS_loop";
b7619582
GF
9156 case DW_TAG_HP_array_descriptor:
9157 return "DW_TAG_HP_array_descriptor";
c906108c
SS
9158 case DW_TAG_format_label:
9159 return "DW_TAG_format_label";
9160 case DW_TAG_function_template:
9161 return "DW_TAG_function_template";
9162 case DW_TAG_class_template:
9163 return "DW_TAG_class_template";
b7619582
GF
9164 case DW_TAG_GNU_BINCL:
9165 return "DW_TAG_GNU_BINCL";
9166 case DW_TAG_GNU_EINCL:
9167 return "DW_TAG_GNU_EINCL";
9168 case DW_TAG_upc_shared_type:
9169 return "DW_TAG_upc_shared_type";
9170 case DW_TAG_upc_strict_type:
9171 return "DW_TAG_upc_strict_type";
9172 case DW_TAG_upc_relaxed_type:
9173 return "DW_TAG_upc_relaxed_type";
9174 case DW_TAG_PGI_kanji_type:
9175 return "DW_TAG_PGI_kanji_type";
9176 case DW_TAG_PGI_interface_block:
9177 return "DW_TAG_PGI_interface_block";
c906108c
SS
9178 default:
9179 return "DW_TAG_<unknown>";
9180 }
9181}
9182
9183/* Convert a DWARF attribute code into its string name. */
9184
9185static char *
aa1ee363 9186dwarf_attr_name (unsigned attr)
c906108c
SS
9187{
9188 switch (attr)
9189 {
9190 case DW_AT_sibling:
9191 return "DW_AT_sibling";
9192 case DW_AT_location:
9193 return "DW_AT_location";
9194 case DW_AT_name:
9195 return "DW_AT_name";
9196 case DW_AT_ordering:
9197 return "DW_AT_ordering";
9198 case DW_AT_subscr_data:
9199 return "DW_AT_subscr_data";
9200 case DW_AT_byte_size:
9201 return "DW_AT_byte_size";
9202 case DW_AT_bit_offset:
9203 return "DW_AT_bit_offset";
9204 case DW_AT_bit_size:
9205 return "DW_AT_bit_size";
9206 case DW_AT_element_list:
9207 return "DW_AT_element_list";
9208 case DW_AT_stmt_list:
9209 return "DW_AT_stmt_list";
9210 case DW_AT_low_pc:
9211 return "DW_AT_low_pc";
9212 case DW_AT_high_pc:
9213 return "DW_AT_high_pc";
9214 case DW_AT_language:
9215 return "DW_AT_language";
9216 case DW_AT_member:
9217 return "DW_AT_member";
9218 case DW_AT_discr:
9219 return "DW_AT_discr";
9220 case DW_AT_discr_value:
9221 return "DW_AT_discr_value";
9222 case DW_AT_visibility:
9223 return "DW_AT_visibility";
9224 case DW_AT_import:
9225 return "DW_AT_import";
9226 case DW_AT_string_length:
9227 return "DW_AT_string_length";
9228 case DW_AT_common_reference:
9229 return "DW_AT_common_reference";
9230 case DW_AT_comp_dir:
9231 return "DW_AT_comp_dir";
9232 case DW_AT_const_value:
9233 return "DW_AT_const_value";
9234 case DW_AT_containing_type:
9235 return "DW_AT_containing_type";
9236 case DW_AT_default_value:
9237 return "DW_AT_default_value";
9238 case DW_AT_inline:
9239 return "DW_AT_inline";
9240 case DW_AT_is_optional:
9241 return "DW_AT_is_optional";
9242 case DW_AT_lower_bound:
9243 return "DW_AT_lower_bound";
9244 case DW_AT_producer:
9245 return "DW_AT_producer";
9246 case DW_AT_prototyped:
9247 return "DW_AT_prototyped";
9248 case DW_AT_return_addr:
9249 return "DW_AT_return_addr";
9250 case DW_AT_start_scope:
9251 return "DW_AT_start_scope";
09fa0d7c
JK
9252 case DW_AT_bit_stride:
9253 return "DW_AT_bit_stride";
c906108c
SS
9254 case DW_AT_upper_bound:
9255 return "DW_AT_upper_bound";
9256 case DW_AT_abstract_origin:
9257 return "DW_AT_abstract_origin";
9258 case DW_AT_accessibility:
9259 return "DW_AT_accessibility";
9260 case DW_AT_address_class:
9261 return "DW_AT_address_class";
9262 case DW_AT_artificial:
9263 return "DW_AT_artificial";
9264 case DW_AT_base_types:
9265 return "DW_AT_base_types";
9266 case DW_AT_calling_convention:
9267 return "DW_AT_calling_convention";
9268 case DW_AT_count:
9269 return "DW_AT_count";
9270 case DW_AT_data_member_location:
9271 return "DW_AT_data_member_location";
9272 case DW_AT_decl_column:
9273 return "DW_AT_decl_column";
9274 case DW_AT_decl_file:
9275 return "DW_AT_decl_file";
9276 case DW_AT_decl_line:
9277 return "DW_AT_decl_line";
9278 case DW_AT_declaration:
9279 return "DW_AT_declaration";
9280 case DW_AT_discr_list:
9281 return "DW_AT_discr_list";
9282 case DW_AT_encoding:
9283 return "DW_AT_encoding";
9284 case DW_AT_external:
9285 return "DW_AT_external";
9286 case DW_AT_frame_base:
9287 return "DW_AT_frame_base";
9288 case DW_AT_friend:
9289 return "DW_AT_friend";
9290 case DW_AT_identifier_case:
9291 return "DW_AT_identifier_case";
9292 case DW_AT_macro_info:
9293 return "DW_AT_macro_info";
9294 case DW_AT_namelist_items:
9295 return "DW_AT_namelist_items";
9296 case DW_AT_priority:
9297 return "DW_AT_priority";
9298 case DW_AT_segment:
9299 return "DW_AT_segment";
9300 case DW_AT_specification:
9301 return "DW_AT_specification";
9302 case DW_AT_static_link:
9303 return "DW_AT_static_link";
9304 case DW_AT_type:
9305 return "DW_AT_type";
9306 case DW_AT_use_location:
9307 return "DW_AT_use_location";
9308 case DW_AT_variable_parameter:
9309 return "DW_AT_variable_parameter";
9310 case DW_AT_virtuality:
9311 return "DW_AT_virtuality";
9312 case DW_AT_vtable_elem_location:
9313 return "DW_AT_vtable_elem_location";
b7619582 9314 /* DWARF 3 values. */
d9fa45fe
DC
9315 case DW_AT_allocated:
9316 return "DW_AT_allocated";
9317 case DW_AT_associated:
9318 return "DW_AT_associated";
9319 case DW_AT_data_location:
9320 return "DW_AT_data_location";
09fa0d7c
JK
9321 case DW_AT_byte_stride:
9322 return "DW_AT_byte_stride";
d9fa45fe
DC
9323 case DW_AT_entry_pc:
9324 return "DW_AT_entry_pc";
9325 case DW_AT_use_UTF8:
9326 return "DW_AT_use_UTF8";
9327 case DW_AT_extension:
9328 return "DW_AT_extension";
9329 case DW_AT_ranges:
9330 return "DW_AT_ranges";
9331 case DW_AT_trampoline:
9332 return "DW_AT_trampoline";
9333 case DW_AT_call_column:
9334 return "DW_AT_call_column";
9335 case DW_AT_call_file:
9336 return "DW_AT_call_file";
9337 case DW_AT_call_line:
9338 return "DW_AT_call_line";
b7619582
GF
9339 case DW_AT_description:
9340 return "DW_AT_description";
9341 case DW_AT_binary_scale:
9342 return "DW_AT_binary_scale";
9343 case DW_AT_decimal_scale:
9344 return "DW_AT_decimal_scale";
9345 case DW_AT_small:
9346 return "DW_AT_small";
9347 case DW_AT_decimal_sign:
9348 return "DW_AT_decimal_sign";
9349 case DW_AT_digit_count:
9350 return "DW_AT_digit_count";
9351 case DW_AT_picture_string:
9352 return "DW_AT_picture_string";
9353 case DW_AT_mutable:
9354 return "DW_AT_mutable";
9355 case DW_AT_threads_scaled:
9356 return "DW_AT_threads_scaled";
9357 case DW_AT_explicit:
9358 return "DW_AT_explicit";
9359 case DW_AT_object_pointer:
9360 return "DW_AT_object_pointer";
9361 case DW_AT_endianity:
9362 return "DW_AT_endianity";
9363 case DW_AT_elemental:
9364 return "DW_AT_elemental";
9365 case DW_AT_pure:
9366 return "DW_AT_pure";
9367 case DW_AT_recursive:
9368 return "DW_AT_recursive";
348e048f
DE
9369 /* DWARF 4 values. */
9370 case DW_AT_signature:
9371 return "DW_AT_signature";
b7619582 9372 /* SGI/MIPS extensions. */
c764a876 9373#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
9374 case DW_AT_MIPS_fde:
9375 return "DW_AT_MIPS_fde";
c764a876 9376#endif
c906108c
SS
9377 case DW_AT_MIPS_loop_begin:
9378 return "DW_AT_MIPS_loop_begin";
9379 case DW_AT_MIPS_tail_loop_begin:
9380 return "DW_AT_MIPS_tail_loop_begin";
9381 case DW_AT_MIPS_epilog_begin:
9382 return "DW_AT_MIPS_epilog_begin";
9383 case DW_AT_MIPS_loop_unroll_factor:
9384 return "DW_AT_MIPS_loop_unroll_factor";
9385 case DW_AT_MIPS_software_pipeline_depth:
9386 return "DW_AT_MIPS_software_pipeline_depth";
9387 case DW_AT_MIPS_linkage_name:
9388 return "DW_AT_MIPS_linkage_name";
b7619582
GF
9389 case DW_AT_MIPS_stride:
9390 return "DW_AT_MIPS_stride";
9391 case DW_AT_MIPS_abstract_name:
9392 return "DW_AT_MIPS_abstract_name";
9393 case DW_AT_MIPS_clone_origin:
9394 return "DW_AT_MIPS_clone_origin";
9395 case DW_AT_MIPS_has_inlines:
9396 return "DW_AT_MIPS_has_inlines";
b7619582 9397 /* HP extensions. */
c764a876 9398#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
9399 case DW_AT_HP_block_index:
9400 return "DW_AT_HP_block_index";
c764a876 9401#endif
b7619582
GF
9402 case DW_AT_HP_unmodifiable:
9403 return "DW_AT_HP_unmodifiable";
9404 case DW_AT_HP_actuals_stmt_list:
9405 return "DW_AT_HP_actuals_stmt_list";
9406 case DW_AT_HP_proc_per_section:
9407 return "DW_AT_HP_proc_per_section";
9408 case DW_AT_HP_raw_data_ptr:
9409 return "DW_AT_HP_raw_data_ptr";
9410 case DW_AT_HP_pass_by_reference:
9411 return "DW_AT_HP_pass_by_reference";
9412 case DW_AT_HP_opt_level:
9413 return "DW_AT_HP_opt_level";
9414 case DW_AT_HP_prof_version_id:
9415 return "DW_AT_HP_prof_version_id";
9416 case DW_AT_HP_opt_flags:
9417 return "DW_AT_HP_opt_flags";
9418 case DW_AT_HP_cold_region_low_pc:
9419 return "DW_AT_HP_cold_region_low_pc";
9420 case DW_AT_HP_cold_region_high_pc:
9421 return "DW_AT_HP_cold_region_high_pc";
9422 case DW_AT_HP_all_variables_modifiable:
9423 return "DW_AT_HP_all_variables_modifiable";
9424 case DW_AT_HP_linkage_name:
9425 return "DW_AT_HP_linkage_name";
9426 case DW_AT_HP_prof_flags:
9427 return "DW_AT_HP_prof_flags";
9428 /* GNU extensions. */
c906108c
SS
9429 case DW_AT_sf_names:
9430 return "DW_AT_sf_names";
9431 case DW_AT_src_info:
9432 return "DW_AT_src_info";
9433 case DW_AT_mac_info:
9434 return "DW_AT_mac_info";
9435 case DW_AT_src_coords:
9436 return "DW_AT_src_coords";
9437 case DW_AT_body_begin:
9438 return "DW_AT_body_begin";
9439 case DW_AT_body_end:
9440 return "DW_AT_body_end";
f5f8a009
EZ
9441 case DW_AT_GNU_vector:
9442 return "DW_AT_GNU_vector";
b7619582
GF
9443 /* VMS extensions. */
9444 case DW_AT_VMS_rtnbeg_pd_address:
9445 return "DW_AT_VMS_rtnbeg_pd_address";
9446 /* UPC extension. */
9447 case DW_AT_upc_threads_scaled:
9448 return "DW_AT_upc_threads_scaled";
9449 /* PGI (STMicroelectronics) extensions. */
9450 case DW_AT_PGI_lbase:
9451 return "DW_AT_PGI_lbase";
9452 case DW_AT_PGI_soffset:
9453 return "DW_AT_PGI_soffset";
9454 case DW_AT_PGI_lstride:
9455 return "DW_AT_PGI_lstride";
c906108c
SS
9456 default:
9457 return "DW_AT_<unknown>";
9458 }
9459}
9460
9461/* Convert a DWARF value form code into its string name. */
9462
9463static char *
aa1ee363 9464dwarf_form_name (unsigned form)
c906108c
SS
9465{
9466 switch (form)
9467 {
9468 case DW_FORM_addr:
9469 return "DW_FORM_addr";
9470 case DW_FORM_block2:
9471 return "DW_FORM_block2";
9472 case DW_FORM_block4:
9473 return "DW_FORM_block4";
9474 case DW_FORM_data2:
9475 return "DW_FORM_data2";
9476 case DW_FORM_data4:
9477 return "DW_FORM_data4";
9478 case DW_FORM_data8:
9479 return "DW_FORM_data8";
9480 case DW_FORM_string:
9481 return "DW_FORM_string";
9482 case DW_FORM_block:
9483 return "DW_FORM_block";
9484 case DW_FORM_block1:
9485 return "DW_FORM_block1";
9486 case DW_FORM_data1:
9487 return "DW_FORM_data1";
9488 case DW_FORM_flag:
9489 return "DW_FORM_flag";
9490 case DW_FORM_sdata:
9491 return "DW_FORM_sdata";
9492 case DW_FORM_strp:
9493 return "DW_FORM_strp";
9494 case DW_FORM_udata:
9495 return "DW_FORM_udata";
9496 case DW_FORM_ref_addr:
9497 return "DW_FORM_ref_addr";
9498 case DW_FORM_ref1:
9499 return "DW_FORM_ref1";
9500 case DW_FORM_ref2:
9501 return "DW_FORM_ref2";
9502 case DW_FORM_ref4:
9503 return "DW_FORM_ref4";
9504 case DW_FORM_ref8:
9505 return "DW_FORM_ref8";
9506 case DW_FORM_ref_udata:
9507 return "DW_FORM_ref_udata";
9508 case DW_FORM_indirect:
9509 return "DW_FORM_indirect";
348e048f
DE
9510 case DW_FORM_sec_offset:
9511 return "DW_FORM_sec_offset";
9512 case DW_FORM_exprloc:
9513 return "DW_FORM_exprloc";
9514 case DW_FORM_flag_present:
9515 return "DW_FORM_flag_present";
9516 case DW_FORM_sig8:
9517 return "DW_FORM_sig8";
71c25dea
TT
9518 case GDB_FORM_cached_string:
9519 return "GDB_FORM_cached_string";
c906108c
SS
9520 default:
9521 return "DW_FORM_<unknown>";
9522 }
9523}
9524
9525/* Convert a DWARF stack opcode into its string name. */
9526
9527static char *
aa1ee363 9528dwarf_stack_op_name (unsigned op)
c906108c
SS
9529{
9530 switch (op)
9531 {
9532 case DW_OP_addr:
9533 return "DW_OP_addr";
9534 case DW_OP_deref:
9535 return "DW_OP_deref";
9536 case DW_OP_const1u:
9537 return "DW_OP_const1u";
9538 case DW_OP_const1s:
9539 return "DW_OP_const1s";
9540 case DW_OP_const2u:
9541 return "DW_OP_const2u";
9542 case DW_OP_const2s:
9543 return "DW_OP_const2s";
9544 case DW_OP_const4u:
9545 return "DW_OP_const4u";
9546 case DW_OP_const4s:
9547 return "DW_OP_const4s";
9548 case DW_OP_const8u:
9549 return "DW_OP_const8u";
9550 case DW_OP_const8s:
9551 return "DW_OP_const8s";
9552 case DW_OP_constu:
9553 return "DW_OP_constu";
9554 case DW_OP_consts:
9555 return "DW_OP_consts";
9556 case DW_OP_dup:
9557 return "DW_OP_dup";
9558 case DW_OP_drop:
9559 return "DW_OP_drop";
9560 case DW_OP_over:
9561 return "DW_OP_over";
9562 case DW_OP_pick:
9563 return "DW_OP_pick";
9564 case DW_OP_swap:
9565 return "DW_OP_swap";
9566 case DW_OP_rot:
9567 return "DW_OP_rot";
9568 case DW_OP_xderef:
9569 return "DW_OP_xderef";
9570 case DW_OP_abs:
9571 return "DW_OP_abs";
9572 case DW_OP_and:
9573 return "DW_OP_and";
9574 case DW_OP_div:
9575 return "DW_OP_div";
9576 case DW_OP_minus:
9577 return "DW_OP_minus";
9578 case DW_OP_mod:
9579 return "DW_OP_mod";
9580 case DW_OP_mul:
9581 return "DW_OP_mul";
9582 case DW_OP_neg:
9583 return "DW_OP_neg";
9584 case DW_OP_not:
9585 return "DW_OP_not";
9586 case DW_OP_or:
9587 return "DW_OP_or";
9588 case DW_OP_plus:
9589 return "DW_OP_plus";
9590 case DW_OP_plus_uconst:
9591 return "DW_OP_plus_uconst";
9592 case DW_OP_shl:
9593 return "DW_OP_shl";
9594 case DW_OP_shr:
9595 return "DW_OP_shr";
9596 case DW_OP_shra:
9597 return "DW_OP_shra";
9598 case DW_OP_xor:
9599 return "DW_OP_xor";
9600 case DW_OP_bra:
9601 return "DW_OP_bra";
9602 case DW_OP_eq:
9603 return "DW_OP_eq";
9604 case DW_OP_ge:
9605 return "DW_OP_ge";
9606 case DW_OP_gt:
9607 return "DW_OP_gt";
9608 case DW_OP_le:
9609 return "DW_OP_le";
9610 case DW_OP_lt:
9611 return "DW_OP_lt";
9612 case DW_OP_ne:
9613 return "DW_OP_ne";
9614 case DW_OP_skip:
9615 return "DW_OP_skip";
9616 case DW_OP_lit0:
9617 return "DW_OP_lit0";
9618 case DW_OP_lit1:
9619 return "DW_OP_lit1";
9620 case DW_OP_lit2:
9621 return "DW_OP_lit2";
9622 case DW_OP_lit3:
9623 return "DW_OP_lit3";
9624 case DW_OP_lit4:
9625 return "DW_OP_lit4";
9626 case DW_OP_lit5:
9627 return "DW_OP_lit5";
9628 case DW_OP_lit6:
9629 return "DW_OP_lit6";
9630 case DW_OP_lit7:
9631 return "DW_OP_lit7";
9632 case DW_OP_lit8:
9633 return "DW_OP_lit8";
9634 case DW_OP_lit9:
9635 return "DW_OP_lit9";
9636 case DW_OP_lit10:
9637 return "DW_OP_lit10";
9638 case DW_OP_lit11:
9639 return "DW_OP_lit11";
9640 case DW_OP_lit12:
9641 return "DW_OP_lit12";
9642 case DW_OP_lit13:
9643 return "DW_OP_lit13";
9644 case DW_OP_lit14:
9645 return "DW_OP_lit14";
9646 case DW_OP_lit15:
9647 return "DW_OP_lit15";
9648 case DW_OP_lit16:
9649 return "DW_OP_lit16";
9650 case DW_OP_lit17:
9651 return "DW_OP_lit17";
9652 case DW_OP_lit18:
9653 return "DW_OP_lit18";
9654 case DW_OP_lit19:
9655 return "DW_OP_lit19";
9656 case DW_OP_lit20:
9657 return "DW_OP_lit20";
9658 case DW_OP_lit21:
9659 return "DW_OP_lit21";
9660 case DW_OP_lit22:
9661 return "DW_OP_lit22";
9662 case DW_OP_lit23:
9663 return "DW_OP_lit23";
9664 case DW_OP_lit24:
9665 return "DW_OP_lit24";
9666 case DW_OP_lit25:
9667 return "DW_OP_lit25";
9668 case DW_OP_lit26:
9669 return "DW_OP_lit26";
9670 case DW_OP_lit27:
9671 return "DW_OP_lit27";
9672 case DW_OP_lit28:
9673 return "DW_OP_lit28";
9674 case DW_OP_lit29:
9675 return "DW_OP_lit29";
9676 case DW_OP_lit30:
9677 return "DW_OP_lit30";
9678 case DW_OP_lit31:
9679 return "DW_OP_lit31";
9680 case DW_OP_reg0:
9681 return "DW_OP_reg0";
9682 case DW_OP_reg1:
9683 return "DW_OP_reg1";
9684 case DW_OP_reg2:
9685 return "DW_OP_reg2";
9686 case DW_OP_reg3:
9687 return "DW_OP_reg3";
9688 case DW_OP_reg4:
9689 return "DW_OP_reg4";
9690 case DW_OP_reg5:
9691 return "DW_OP_reg5";
9692 case DW_OP_reg6:
9693 return "DW_OP_reg6";
9694 case DW_OP_reg7:
9695 return "DW_OP_reg7";
9696 case DW_OP_reg8:
9697 return "DW_OP_reg8";
9698 case DW_OP_reg9:
9699 return "DW_OP_reg9";
9700 case DW_OP_reg10:
9701 return "DW_OP_reg10";
9702 case DW_OP_reg11:
9703 return "DW_OP_reg11";
9704 case DW_OP_reg12:
9705 return "DW_OP_reg12";
9706 case DW_OP_reg13:
9707 return "DW_OP_reg13";
9708 case DW_OP_reg14:
9709 return "DW_OP_reg14";
9710 case DW_OP_reg15:
9711 return "DW_OP_reg15";
9712 case DW_OP_reg16:
9713 return "DW_OP_reg16";
9714 case DW_OP_reg17:
9715 return "DW_OP_reg17";
9716 case DW_OP_reg18:
9717 return "DW_OP_reg18";
9718 case DW_OP_reg19:
9719 return "DW_OP_reg19";
9720 case DW_OP_reg20:
9721 return "DW_OP_reg20";
9722 case DW_OP_reg21:
9723 return "DW_OP_reg21";
9724 case DW_OP_reg22:
9725 return "DW_OP_reg22";
9726 case DW_OP_reg23:
9727 return "DW_OP_reg23";
9728 case DW_OP_reg24:
9729 return "DW_OP_reg24";
9730 case DW_OP_reg25:
9731 return "DW_OP_reg25";
9732 case DW_OP_reg26:
9733 return "DW_OP_reg26";
9734 case DW_OP_reg27:
9735 return "DW_OP_reg27";
9736 case DW_OP_reg28:
9737 return "DW_OP_reg28";
9738 case DW_OP_reg29:
9739 return "DW_OP_reg29";
9740 case DW_OP_reg30:
9741 return "DW_OP_reg30";
9742 case DW_OP_reg31:
9743 return "DW_OP_reg31";
9744 case DW_OP_breg0:
9745 return "DW_OP_breg0";
9746 case DW_OP_breg1:
9747 return "DW_OP_breg1";
9748 case DW_OP_breg2:
9749 return "DW_OP_breg2";
9750 case DW_OP_breg3:
9751 return "DW_OP_breg3";
9752 case DW_OP_breg4:
9753 return "DW_OP_breg4";
9754 case DW_OP_breg5:
9755 return "DW_OP_breg5";
9756 case DW_OP_breg6:
9757 return "DW_OP_breg6";
9758 case DW_OP_breg7:
9759 return "DW_OP_breg7";
9760 case DW_OP_breg8:
9761 return "DW_OP_breg8";
9762 case DW_OP_breg9:
9763 return "DW_OP_breg9";
9764 case DW_OP_breg10:
9765 return "DW_OP_breg10";
9766 case DW_OP_breg11:
9767 return "DW_OP_breg11";
9768 case DW_OP_breg12:
9769 return "DW_OP_breg12";
9770 case DW_OP_breg13:
9771 return "DW_OP_breg13";
9772 case DW_OP_breg14:
9773 return "DW_OP_breg14";
9774 case DW_OP_breg15:
9775 return "DW_OP_breg15";
9776 case DW_OP_breg16:
9777 return "DW_OP_breg16";
9778 case DW_OP_breg17:
9779 return "DW_OP_breg17";
9780 case DW_OP_breg18:
9781 return "DW_OP_breg18";
9782 case DW_OP_breg19:
9783 return "DW_OP_breg19";
9784 case DW_OP_breg20:
9785 return "DW_OP_breg20";
9786 case DW_OP_breg21:
9787 return "DW_OP_breg21";
9788 case DW_OP_breg22:
9789 return "DW_OP_breg22";
9790 case DW_OP_breg23:
9791 return "DW_OP_breg23";
9792 case DW_OP_breg24:
9793 return "DW_OP_breg24";
9794 case DW_OP_breg25:
9795 return "DW_OP_breg25";
9796 case DW_OP_breg26:
9797 return "DW_OP_breg26";
9798 case DW_OP_breg27:
9799 return "DW_OP_breg27";
9800 case DW_OP_breg28:
9801 return "DW_OP_breg28";
9802 case DW_OP_breg29:
9803 return "DW_OP_breg29";
9804 case DW_OP_breg30:
9805 return "DW_OP_breg30";
9806 case DW_OP_breg31:
9807 return "DW_OP_breg31";
9808 case DW_OP_regx:
9809 return "DW_OP_regx";
9810 case DW_OP_fbreg:
9811 return "DW_OP_fbreg";
9812 case DW_OP_bregx:
9813 return "DW_OP_bregx";
9814 case DW_OP_piece:
9815 return "DW_OP_piece";
9816 case DW_OP_deref_size:
9817 return "DW_OP_deref_size";
9818 case DW_OP_xderef_size:
9819 return "DW_OP_xderef_size";
9820 case DW_OP_nop:
9821 return "DW_OP_nop";
b7619582 9822 /* DWARF 3 extensions. */
ed348acc
EZ
9823 case DW_OP_push_object_address:
9824 return "DW_OP_push_object_address";
9825 case DW_OP_call2:
9826 return "DW_OP_call2";
9827 case DW_OP_call4:
9828 return "DW_OP_call4";
9829 case DW_OP_call_ref:
9830 return "DW_OP_call_ref";
b7619582
GF
9831 /* GNU extensions. */
9832 case DW_OP_form_tls_address:
9833 return "DW_OP_form_tls_address";
9834 case DW_OP_call_frame_cfa:
9835 return "DW_OP_call_frame_cfa";
9836 case DW_OP_bit_piece:
9837 return "DW_OP_bit_piece";
ed348acc
EZ
9838 case DW_OP_GNU_push_tls_address:
9839 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
9840 case DW_OP_GNU_uninit:
9841 return "DW_OP_GNU_uninit";
b7619582
GF
9842 /* HP extensions. */
9843 case DW_OP_HP_is_value:
9844 return "DW_OP_HP_is_value";
9845 case DW_OP_HP_fltconst4:
9846 return "DW_OP_HP_fltconst4";
9847 case DW_OP_HP_fltconst8:
9848 return "DW_OP_HP_fltconst8";
9849 case DW_OP_HP_mod_range:
9850 return "DW_OP_HP_mod_range";
9851 case DW_OP_HP_unmod_range:
9852 return "DW_OP_HP_unmod_range";
9853 case DW_OP_HP_tls:
9854 return "DW_OP_HP_tls";
c906108c
SS
9855 default:
9856 return "OP_<unknown>";
9857 }
9858}
9859
9860static char *
fba45db2 9861dwarf_bool_name (unsigned mybool)
c906108c
SS
9862{
9863 if (mybool)
9864 return "TRUE";
9865 else
9866 return "FALSE";
9867}
9868
9869/* Convert a DWARF type code into its string name. */
9870
9871static char *
aa1ee363 9872dwarf_type_encoding_name (unsigned enc)
c906108c
SS
9873{
9874 switch (enc)
9875 {
b7619582
GF
9876 case DW_ATE_void:
9877 return "DW_ATE_void";
c906108c
SS
9878 case DW_ATE_address:
9879 return "DW_ATE_address";
9880 case DW_ATE_boolean:
9881 return "DW_ATE_boolean";
9882 case DW_ATE_complex_float:
9883 return "DW_ATE_complex_float";
9884 case DW_ATE_float:
9885 return "DW_ATE_float";
9886 case DW_ATE_signed:
9887 return "DW_ATE_signed";
9888 case DW_ATE_signed_char:
9889 return "DW_ATE_signed_char";
9890 case DW_ATE_unsigned:
9891 return "DW_ATE_unsigned";
9892 case DW_ATE_unsigned_char:
9893 return "DW_ATE_unsigned_char";
b7619582 9894 /* DWARF 3. */
d9fa45fe
DC
9895 case DW_ATE_imaginary_float:
9896 return "DW_ATE_imaginary_float";
b7619582
GF
9897 case DW_ATE_packed_decimal:
9898 return "DW_ATE_packed_decimal";
9899 case DW_ATE_numeric_string:
9900 return "DW_ATE_numeric_string";
9901 case DW_ATE_edited:
9902 return "DW_ATE_edited";
9903 case DW_ATE_signed_fixed:
9904 return "DW_ATE_signed_fixed";
9905 case DW_ATE_unsigned_fixed:
9906 return "DW_ATE_unsigned_fixed";
9907 case DW_ATE_decimal_float:
9908 return "DW_ATE_decimal_float";
9909 /* HP extensions. */
9910 case DW_ATE_HP_float80:
9911 return "DW_ATE_HP_float80";
9912 case DW_ATE_HP_complex_float80:
9913 return "DW_ATE_HP_complex_float80";
9914 case DW_ATE_HP_float128:
9915 return "DW_ATE_HP_float128";
9916 case DW_ATE_HP_complex_float128:
9917 return "DW_ATE_HP_complex_float128";
9918 case DW_ATE_HP_floathpintel:
9919 return "DW_ATE_HP_floathpintel";
9920 case DW_ATE_HP_imaginary_float80:
9921 return "DW_ATE_HP_imaginary_float80";
9922 case DW_ATE_HP_imaginary_float128:
9923 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
9924 default:
9925 return "DW_ATE_<unknown>";
9926 }
9927}
9928
9929/* Convert a DWARF call frame info operation to its string name. */
9930
9931#if 0
9932static char *
aa1ee363 9933dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
9934{
9935 switch (cfi_opc)
9936 {
9937 case DW_CFA_advance_loc:
9938 return "DW_CFA_advance_loc";
9939 case DW_CFA_offset:
9940 return "DW_CFA_offset";
9941 case DW_CFA_restore:
9942 return "DW_CFA_restore";
9943 case DW_CFA_nop:
9944 return "DW_CFA_nop";
9945 case DW_CFA_set_loc:
9946 return "DW_CFA_set_loc";
9947 case DW_CFA_advance_loc1:
9948 return "DW_CFA_advance_loc1";
9949 case DW_CFA_advance_loc2:
9950 return "DW_CFA_advance_loc2";
9951 case DW_CFA_advance_loc4:
9952 return "DW_CFA_advance_loc4";
9953 case DW_CFA_offset_extended:
9954 return "DW_CFA_offset_extended";
9955 case DW_CFA_restore_extended:
9956 return "DW_CFA_restore_extended";
9957 case DW_CFA_undefined:
9958 return "DW_CFA_undefined";
9959 case DW_CFA_same_value:
9960 return "DW_CFA_same_value";
9961 case DW_CFA_register:
9962 return "DW_CFA_register";
9963 case DW_CFA_remember_state:
9964 return "DW_CFA_remember_state";
9965 case DW_CFA_restore_state:
9966 return "DW_CFA_restore_state";
9967 case DW_CFA_def_cfa:
9968 return "DW_CFA_def_cfa";
9969 case DW_CFA_def_cfa_register:
9970 return "DW_CFA_def_cfa_register";
9971 case DW_CFA_def_cfa_offset:
9972 return "DW_CFA_def_cfa_offset";
b7619582 9973 /* DWARF 3. */
985cb1a3
JM
9974 case DW_CFA_def_cfa_expression:
9975 return "DW_CFA_def_cfa_expression";
9976 case DW_CFA_expression:
9977 return "DW_CFA_expression";
9978 case DW_CFA_offset_extended_sf:
9979 return "DW_CFA_offset_extended_sf";
9980 case DW_CFA_def_cfa_sf:
9981 return "DW_CFA_def_cfa_sf";
9982 case DW_CFA_def_cfa_offset_sf:
9983 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
9984 case DW_CFA_val_offset:
9985 return "DW_CFA_val_offset";
9986 case DW_CFA_val_offset_sf:
9987 return "DW_CFA_val_offset_sf";
9988 case DW_CFA_val_expression:
9989 return "DW_CFA_val_expression";
9990 /* SGI/MIPS specific. */
c906108c
SS
9991 case DW_CFA_MIPS_advance_loc8:
9992 return "DW_CFA_MIPS_advance_loc8";
b7619582 9993 /* GNU extensions. */
985cb1a3
JM
9994 case DW_CFA_GNU_window_save:
9995 return "DW_CFA_GNU_window_save";
9996 case DW_CFA_GNU_args_size:
9997 return "DW_CFA_GNU_args_size";
9998 case DW_CFA_GNU_negative_offset_extended:
9999 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
10000 default:
10001 return "DW_CFA_<unknown>";
10002 }
10003}
10004#endif
10005
f9aca02d 10006static void
d97bc12b 10007dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
10008{
10009 unsigned int i;
10010
d97bc12b
DE
10011 print_spaces (indent, f);
10012 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 10013 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
10014
10015 if (die->parent != NULL)
10016 {
10017 print_spaces (indent, f);
10018 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
10019 die->parent->offset);
10020 }
10021
10022 print_spaces (indent, f);
10023 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 10024 dwarf_bool_name (die->child != NULL));
c906108c 10025
d97bc12b
DE
10026 print_spaces (indent, f);
10027 fprintf_unfiltered (f, " attributes:\n");
10028
c906108c
SS
10029 for (i = 0; i < die->num_attrs; ++i)
10030 {
d97bc12b
DE
10031 print_spaces (indent, f);
10032 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
10033 dwarf_attr_name (die->attrs[i].name),
10034 dwarf_form_name (die->attrs[i].form));
d97bc12b 10035
c906108c
SS
10036 switch (die->attrs[i].form)
10037 {
10038 case DW_FORM_ref_addr:
10039 case DW_FORM_addr:
d97bc12b 10040 fprintf_unfiltered (f, "address: ");
5af949e3 10041 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
10042 break;
10043 case DW_FORM_block2:
10044 case DW_FORM_block4:
10045 case DW_FORM_block:
10046 case DW_FORM_block1:
d97bc12b 10047 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 10048 break;
10b3939b
DJ
10049 case DW_FORM_ref1:
10050 case DW_FORM_ref2:
10051 case DW_FORM_ref4:
d97bc12b 10052 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
10053 (long) (DW_ADDR (&die->attrs[i])));
10054 break;
c906108c
SS
10055 case DW_FORM_data1:
10056 case DW_FORM_data2:
10057 case DW_FORM_data4:
ce5d95e1 10058 case DW_FORM_data8:
c906108c
SS
10059 case DW_FORM_udata:
10060 case DW_FORM_sdata:
d97bc12b 10061 fprintf_unfiltered (f, "constant: %ld", DW_UNSND (&die->attrs[i]));
c906108c 10062 break;
348e048f
DE
10063 case DW_FORM_sig8:
10064 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
10065 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
10066 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
10067 else
10068 fprintf_unfiltered (f, "signatured type, offset: unknown");
10069 break;
c906108c 10070 case DW_FORM_string:
4bdf3d34 10071 case DW_FORM_strp:
71c25dea 10072 case GDB_FORM_cached_string:
d97bc12b 10073 fprintf_unfiltered (f, "string: \"%s\"",
c906108c 10074 DW_STRING (&die->attrs[i])
c5aa993b 10075 ? DW_STRING (&die->attrs[i]) : "");
c906108c
SS
10076 break;
10077 case DW_FORM_flag:
10078 if (DW_UNSND (&die->attrs[i]))
d97bc12b 10079 fprintf_unfiltered (f, "flag: TRUE");
c906108c 10080 else
d97bc12b 10081 fprintf_unfiltered (f, "flag: FALSE");
c906108c 10082 break;
a8329558
KW
10083 case DW_FORM_indirect:
10084 /* the reader will have reduced the indirect form to
10085 the "base form" so this form should not occur */
d97bc12b 10086 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 10087 break;
c906108c 10088 default:
d97bc12b 10089 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 10090 die->attrs[i].form);
d97bc12b 10091 break;
c906108c 10092 }
d97bc12b 10093 fprintf_unfiltered (f, "\n");
c906108c
SS
10094 }
10095}
10096
f9aca02d 10097static void
d97bc12b 10098dump_die_for_error (struct die_info *die)
c906108c 10099{
d97bc12b
DE
10100 dump_die_shallow (gdb_stderr, 0, die);
10101}
10102
10103static void
10104dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
10105{
10106 int indent = level * 4;
10107
10108 gdb_assert (die != NULL);
10109
10110 if (level >= max_level)
10111 return;
10112
10113 dump_die_shallow (f, indent, die);
10114
10115 if (die->child != NULL)
c906108c 10116 {
d97bc12b
DE
10117 print_spaces (indent, f);
10118 fprintf_unfiltered (f, " Children:");
10119 if (level + 1 < max_level)
10120 {
10121 fprintf_unfiltered (f, "\n");
10122 dump_die_1 (f, level + 1, max_level, die->child);
10123 }
10124 else
10125 {
10126 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
10127 }
10128 }
10129
10130 if (die->sibling != NULL && level > 0)
10131 {
10132 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
10133 }
10134}
10135
d97bc12b
DE
10136/* This is called from the pdie macro in gdbinit.in.
10137 It's not static so gcc will keep a copy callable from gdb. */
10138
10139void
10140dump_die (struct die_info *die, int max_level)
10141{
10142 dump_die_1 (gdb_stdlog, 0, max_level, die);
10143}
10144
f9aca02d 10145static void
51545339 10146store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10147{
51545339 10148 void **slot;
c906108c 10149
51545339
DJ
10150 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
10151
10152 *slot = die;
c906108c
SS
10153}
10154
93311388
DE
10155static int
10156is_ref_attr (struct attribute *attr)
c906108c 10157{
c906108c
SS
10158 switch (attr->form)
10159 {
10160 case DW_FORM_ref_addr:
c906108c
SS
10161 case DW_FORM_ref1:
10162 case DW_FORM_ref2:
10163 case DW_FORM_ref4:
613e1657 10164 case DW_FORM_ref8:
c906108c 10165 case DW_FORM_ref_udata:
93311388 10166 return 1;
c906108c 10167 default:
93311388 10168 return 0;
c906108c 10169 }
93311388
DE
10170}
10171
10172static unsigned int
10173dwarf2_get_ref_die_offset (struct attribute *attr)
10174{
10175 if (is_ref_attr (attr))
10176 return DW_ADDR (attr);
10177
10178 complaint (&symfile_complaints,
10179 _("unsupported die ref attribute form: '%s'"),
10180 dwarf_form_name (attr->form));
10181 return 0;
c906108c
SS
10182}
10183
a02abb62
JB
10184/* Return the constant value held by the given attribute. Return -1
10185 if the value held by the attribute is not constant. */
10186
10187static int
10188dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
10189{
10190 if (attr->form == DW_FORM_sdata)
10191 return DW_SND (attr);
10192 else if (attr->form == DW_FORM_udata
10193 || attr->form == DW_FORM_data1
10194 || attr->form == DW_FORM_data2
10195 || attr->form == DW_FORM_data4
10196 || attr->form == DW_FORM_data8)
10197 return DW_UNSND (attr);
10198 else
10199 {
e2e0b3e5 10200 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
10201 dwarf_form_name (attr->form));
10202 return default_value;
10203 }
10204}
10205
03dd20cc 10206/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
10207 unit and add it to our queue.
10208 The result is non-zero if PER_CU was queued, otherwise the result is zero
10209 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 10210
348e048f 10211static int
03dd20cc
DJ
10212maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
10213 struct dwarf2_per_cu_data *per_cu)
10214{
10215 /* Mark the dependence relation so that we don't flush PER_CU
10216 too early. */
10217 dwarf2_add_dependence (this_cu, per_cu);
10218
10219 /* If it's already on the queue, we have nothing to do. */
10220 if (per_cu->queued)
348e048f 10221 return 0;
03dd20cc
DJ
10222
10223 /* If the compilation unit is already loaded, just mark it as
10224 used. */
10225 if (per_cu->cu != NULL)
10226 {
10227 per_cu->cu->last_used = 0;
348e048f 10228 return 0;
03dd20cc
DJ
10229 }
10230
10231 /* Add it to the queue. */
10232 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
10233
10234 return 1;
10235}
10236
10237/* Follow reference or signature attribute ATTR of SRC_DIE.
10238 On entry *REF_CU is the CU of SRC_DIE.
10239 On exit *REF_CU is the CU of the result. */
10240
10241static struct die_info *
10242follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
10243 struct dwarf2_cu **ref_cu)
10244{
10245 struct die_info *die;
10246
10247 if (is_ref_attr (attr))
10248 die = follow_die_ref (src_die, attr, ref_cu);
10249 else if (attr->form == DW_FORM_sig8)
10250 die = follow_die_sig (src_die, attr, ref_cu);
10251 else
10252 {
10253 dump_die_for_error (src_die);
10254 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
10255 (*ref_cu)->objfile->name);
10256 }
10257
10258 return die;
03dd20cc
DJ
10259}
10260
f504f079
DE
10261/* Follow reference attribute ATTR of SRC_DIE.
10262 On entry *REF_CU is the CU of SRC_DIE.
10263 On exit *REF_CU is the CU of the result. */
10264
f9aca02d 10265static struct die_info *
10b3939b 10266follow_die_ref (struct die_info *src_die, struct attribute *attr,
f2f0e013 10267 struct dwarf2_cu **ref_cu)
c906108c
SS
10268{
10269 struct die_info *die;
10b3939b 10270 unsigned int offset;
10b3939b 10271 struct die_info temp_die;
f2f0e013 10272 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 10273
348e048f
DE
10274 gdb_assert (cu->per_cu != NULL);
10275
c764a876 10276 offset = dwarf2_get_ref_die_offset (attr);
10b3939b 10277
348e048f
DE
10278 if (cu->per_cu->from_debug_types)
10279 {
10280 /* .debug_types CUs cannot reference anything outside their CU.
10281 If they need to, they have to reference a signatured type via
10282 DW_FORM_sig8. */
10283 if (! offset_in_cu_p (&cu->header, offset))
10284 goto not_found;
10285 target_cu = cu;
10286 }
10287 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
10288 {
10289 struct dwarf2_per_cu_data *per_cu;
45452591 10290 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
10291
10292 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
10293 if (maybe_queue_comp_unit (cu, per_cu))
10294 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 10295
10b3939b
DJ
10296 target_cu = per_cu->cu;
10297 }
10298 else
10299 target_cu = cu;
c906108c 10300
f2f0e013 10301 *ref_cu = target_cu;
51545339
DJ
10302 temp_die.offset = offset;
10303 die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
10304 if (die)
10305 return die;
10b3939b 10306
348e048f
DE
10307 not_found:
10308
10309 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
10310 "at 0x%x [in module %s]"),
10311 offset, src_die->offset, cu->objfile->name);
10312}
10313
10314/* Follow the signature attribute ATTR in SRC_DIE.
10315 On entry *REF_CU is the CU of SRC_DIE.
10316 On exit *REF_CU is the CU of the result. */
10317
10318static struct die_info *
10319follow_die_sig (struct die_info *src_die, struct attribute *attr,
10320 struct dwarf2_cu **ref_cu)
10321{
10322 struct objfile *objfile = (*ref_cu)->objfile;
10323 struct die_info temp_die;
10324 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
10325 struct dwarf2_cu *sig_cu;
10326 struct die_info *die;
10327
10328 /* sig_type will be NULL if the signatured type is missing from
10329 the debug info. */
10330 if (sig_type == NULL)
10331 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
10332 "at 0x%x [in module %s]"),
10333 src_die->offset, objfile->name);
10334
10335 /* If necessary, add it to the queue and load its DIEs. */
10336
10337 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
10338 read_signatured_type (objfile, sig_type);
10339
10340 gdb_assert (sig_type->per_cu.cu != NULL);
10341
10342 sig_cu = sig_type->per_cu.cu;
10343 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
10344 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
10345 if (die)
10346 {
10347 *ref_cu = sig_cu;
10348 return die;
10349 }
10350
10351 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
10352 "at 0x%x [in module %s]"),
10353 sig_type->type_offset, src_die->offset, objfile->name);
10354}
10355
10356/* Given an offset of a signatured type, return its signatured_type. */
10357
10358static struct signatured_type *
10359lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
10360{
10361 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
10362 unsigned int length, initial_length_size;
10363 unsigned int sig_offset;
10364 struct signatured_type find_entry, *type_sig;
10365
10366 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
10367 sig_offset = (initial_length_size
10368 + 2 /*version*/
10369 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
10370 + 1 /*address_size*/);
10371 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
10372 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
10373
10374 /* This is only used to lookup previously recorded types.
10375 If we didn't find it, it's our bug. */
10376 gdb_assert (type_sig != NULL);
10377 gdb_assert (offset == type_sig->offset);
10378
10379 return type_sig;
10380}
10381
10382/* Read in signatured type at OFFSET and build its CU and die(s). */
10383
10384static void
10385read_signatured_type_at_offset (struct objfile *objfile,
10386 unsigned int offset)
10387{
10388 struct signatured_type *type_sig;
10389
10390 /* We have the section offset, but we need the signature to do the
10391 hash table lookup. */
10392 type_sig = lookup_signatured_type_at_offset (objfile, offset);
10393
10394 gdb_assert (type_sig->per_cu.cu == NULL);
10395
10396 read_signatured_type (objfile, type_sig);
10397
10398 gdb_assert (type_sig->per_cu.cu != NULL);
10399}
10400
10401/* Read in a signatured type and build its CU and DIEs. */
10402
10403static void
10404read_signatured_type (struct objfile *objfile,
10405 struct signatured_type *type_sig)
10406{
10407 gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
10408 struct die_reader_specs reader_specs;
10409 struct dwarf2_cu *cu;
10410 ULONGEST signature;
10411 struct cleanup *back_to, *free_cu_cleanup;
10412 struct attribute *attr;
10413
10414 gdb_assert (type_sig->per_cu.cu == NULL);
10415
10416 cu = xmalloc (sizeof (struct dwarf2_cu));
10417 memset (cu, 0, sizeof (struct dwarf2_cu));
10418 obstack_init (&cu->comp_unit_obstack);
10419 cu->objfile = objfile;
10420 type_sig->per_cu.cu = cu;
10421 cu->per_cu = &type_sig->per_cu;
10422
10423 /* If an error occurs while loading, release our storage. */
10424 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
10425
10426 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
10427 types_ptr, objfile->obfd);
10428 gdb_assert (signature == type_sig->signature);
10429
10430 cu->die_hash
10431 = htab_create_alloc_ex (cu->header.length / 12,
10432 die_hash,
10433 die_eq,
10434 NULL,
10435 &cu->comp_unit_obstack,
10436 hashtab_obstack_allocate,
10437 dummy_obstack_deallocate);
10438
10439 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
10440 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
10441
10442 init_cu_die_reader (&reader_specs, cu);
10443
10444 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
10445 NULL /*parent*/);
10446
10447 /* We try not to read any attributes in this function, because not
10448 all objfiles needed for references have been loaded yet, and symbol
10449 table processing isn't initialized. But we have to set the CU language,
10450 or we won't be able to build types correctly. */
10451 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
10452 if (attr)
10453 set_cu_language (DW_UNSND (attr), cu);
10454 else
10455 set_cu_language (language_minimal, cu);
10456
10457 do_cleanups (back_to);
10458
10459 /* We've successfully allocated this compilation unit. Let our caller
10460 clean it up when finished with it. */
10461 discard_cleanups (free_cu_cleanup);
10462
10463 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
10464 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
10465}
10466
c906108c
SS
10467/* Decode simple location descriptions.
10468 Given a pointer to a dwarf block that defines a location, compute
10469 the location and return the value.
10470
4cecd739
DJ
10471 NOTE drow/2003-11-18: This function is called in two situations
10472 now: for the address of static or global variables (partial symbols
10473 only) and for offsets into structures which are expected to be
10474 (more or less) constant. The partial symbol case should go away,
10475 and only the constant case should remain. That will let this
10476 function complain more accurately. A few special modes are allowed
10477 without complaint for global variables (for instance, global
10478 register values and thread-local values).
c906108c
SS
10479
10480 A location description containing no operations indicates that the
4cecd739 10481 object is optimized out. The return value is 0 for that case.
6b992462
DJ
10482 FIXME drow/2003-11-16: No callers check for this case any more; soon all
10483 callers will only want a very basic result and this can become a
10484 complaint.
c906108c 10485
c906108c
SS
10486 Note that stack[0] is unused except as a default error return.
10487 Note that stack overflow is not yet handled. */
10488
10489static CORE_ADDR
e7c27a73 10490decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 10491{
e7c27a73
DJ
10492 struct objfile *objfile = cu->objfile;
10493 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10494 int i;
10495 int size = blk->size;
fe1b8b76 10496 gdb_byte *data = blk->data;
c906108c
SS
10497 CORE_ADDR stack[64];
10498 int stacki;
10499 unsigned int bytes_read, unsnd;
fe1b8b76 10500 gdb_byte op;
c906108c
SS
10501
10502 i = 0;
10503 stacki = 0;
10504 stack[stacki] = 0;
c906108c
SS
10505
10506 while (i < size)
10507 {
c906108c
SS
10508 op = data[i++];
10509 switch (op)
10510 {
f1bea926
JM
10511 case DW_OP_lit0:
10512 case DW_OP_lit1:
10513 case DW_OP_lit2:
10514 case DW_OP_lit3:
10515 case DW_OP_lit4:
10516 case DW_OP_lit5:
10517 case DW_OP_lit6:
10518 case DW_OP_lit7:
10519 case DW_OP_lit8:
10520 case DW_OP_lit9:
10521 case DW_OP_lit10:
10522 case DW_OP_lit11:
10523 case DW_OP_lit12:
10524 case DW_OP_lit13:
10525 case DW_OP_lit14:
10526 case DW_OP_lit15:
10527 case DW_OP_lit16:
10528 case DW_OP_lit17:
10529 case DW_OP_lit18:
10530 case DW_OP_lit19:
10531 case DW_OP_lit20:
10532 case DW_OP_lit21:
10533 case DW_OP_lit22:
10534 case DW_OP_lit23:
10535 case DW_OP_lit24:
10536 case DW_OP_lit25:
10537 case DW_OP_lit26:
10538 case DW_OP_lit27:
10539 case DW_OP_lit28:
10540 case DW_OP_lit29:
10541 case DW_OP_lit30:
10542 case DW_OP_lit31:
10543 stack[++stacki] = op - DW_OP_lit0;
10544 break;
10545
c906108c
SS
10546 case DW_OP_reg0:
10547 case DW_OP_reg1:
10548 case DW_OP_reg2:
10549 case DW_OP_reg3:
10550 case DW_OP_reg4:
10551 case DW_OP_reg5:
10552 case DW_OP_reg6:
10553 case DW_OP_reg7:
10554 case DW_OP_reg8:
10555 case DW_OP_reg9:
10556 case DW_OP_reg10:
10557 case DW_OP_reg11:
10558 case DW_OP_reg12:
10559 case DW_OP_reg13:
10560 case DW_OP_reg14:
10561 case DW_OP_reg15:
10562 case DW_OP_reg16:
10563 case DW_OP_reg17:
10564 case DW_OP_reg18:
10565 case DW_OP_reg19:
10566 case DW_OP_reg20:
10567 case DW_OP_reg21:
10568 case DW_OP_reg22:
10569 case DW_OP_reg23:
10570 case DW_OP_reg24:
10571 case DW_OP_reg25:
10572 case DW_OP_reg26:
10573 case DW_OP_reg27:
10574 case DW_OP_reg28:
10575 case DW_OP_reg29:
10576 case DW_OP_reg30:
10577 case DW_OP_reg31:
c906108c 10578 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
10579 if (i < size)
10580 dwarf2_complex_location_expr_complaint ();
c906108c
SS
10581 break;
10582
10583 case DW_OP_regx:
c906108c
SS
10584 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10585 i += bytes_read;
c906108c 10586 stack[++stacki] = unsnd;
4cecd739
DJ
10587 if (i < size)
10588 dwarf2_complex_location_expr_complaint ();
c906108c
SS
10589 break;
10590
10591 case DW_OP_addr:
107d2387 10592 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 10593 cu, &bytes_read);
107d2387 10594 i += bytes_read;
c906108c
SS
10595 break;
10596
10597 case DW_OP_const1u:
10598 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
10599 i += 1;
10600 break;
10601
10602 case DW_OP_const1s:
10603 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
10604 i += 1;
10605 break;
10606
10607 case DW_OP_const2u:
10608 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
10609 i += 2;
10610 break;
10611
10612 case DW_OP_const2s:
10613 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
10614 i += 2;
10615 break;
10616
10617 case DW_OP_const4u:
10618 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
10619 i += 4;
10620 break;
10621
10622 case DW_OP_const4s:
10623 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
10624 i += 4;
10625 break;
10626
10627 case DW_OP_constu:
10628 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 10629 &bytes_read);
c906108c
SS
10630 i += bytes_read;
10631 break;
10632
10633 case DW_OP_consts:
10634 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
10635 i += bytes_read;
10636 break;
10637
f1bea926
JM
10638 case DW_OP_dup:
10639 stack[stacki + 1] = stack[stacki];
10640 stacki++;
10641 break;
10642
c906108c
SS
10643 case DW_OP_plus:
10644 stack[stacki - 1] += stack[stacki];
10645 stacki--;
10646 break;
10647
10648 case DW_OP_plus_uconst:
10649 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10650 i += bytes_read;
10651 break;
10652
10653 case DW_OP_minus:
f1bea926 10654 stack[stacki - 1] -= stack[stacki];
c906108c
SS
10655 stacki--;
10656 break;
10657
7a292a7a 10658 case DW_OP_deref:
7a292a7a 10659 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
10660 this using GDB's address_class enum. This is valid for partial
10661 global symbols, although the variable's address will be bogus
10662 in the psymtab. */
7a292a7a 10663 if (i < size)
4d3c2250 10664 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
10665 break;
10666
9d774e44 10667 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
10668 /* The top of the stack has the offset from the beginning
10669 of the thread control block at which the variable is located. */
10670 /* Nothing should follow this operator, so the top of stack would
10671 be returned. */
4cecd739
DJ
10672 /* This is valid for partial global symbols, but the variable's
10673 address will be bogus in the psymtab. */
9d774e44 10674 if (i < size)
4d3c2250 10675 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
10676 break;
10677
42be36b3
CT
10678 case DW_OP_GNU_uninit:
10679 break;
10680
c906108c 10681 default:
e2e0b3e5 10682 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
4d3c2250 10683 dwarf_stack_op_name (op));
c906108c
SS
10684 return (stack[stacki]);
10685 }
10686 }
10687 return (stack[stacki]);
10688}
10689
10690/* memory allocation interface */
10691
c906108c 10692static struct dwarf_block *
7b5a2f43 10693dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
10694{
10695 struct dwarf_block *blk;
10696
10697 blk = (struct dwarf_block *)
7b5a2f43 10698 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
10699 return (blk);
10700}
10701
10702static struct abbrev_info *
f3dd6933 10703dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
10704{
10705 struct abbrev_info *abbrev;
10706
f3dd6933
DJ
10707 abbrev = (struct abbrev_info *)
10708 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
10709 memset (abbrev, 0, sizeof (struct abbrev_info));
10710 return (abbrev);
10711}
10712
10713static struct die_info *
b60c80d6 10714dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
10715{
10716 struct die_info *die;
b60c80d6
DJ
10717 size_t size = sizeof (struct die_info);
10718
10719 if (num_attrs > 1)
10720 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 10721
b60c80d6 10722 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
10723 memset (die, 0, sizeof (struct die_info));
10724 return (die);
10725}
2e276125
JB
10726
10727\f
10728/* Macro support. */
10729
10730
10731/* Return the full name of file number I in *LH's file name table.
10732 Use COMP_DIR as the name of the current directory of the
10733 compilation. The result is allocated using xmalloc; the caller is
10734 responsible for freeing it. */
10735static char *
10736file_full_name (int file, struct line_header *lh, const char *comp_dir)
10737{
6a83a1e6
EZ
10738 /* Is the file number a valid index into the line header's file name
10739 table? Remember that file numbers start with one, not zero. */
10740 if (1 <= file && file <= lh->num_file_names)
10741 {
10742 struct file_entry *fe = &lh->file_names[file - 1];
2e276125 10743
6a83a1e6
EZ
10744 if (IS_ABSOLUTE_PATH (fe->name))
10745 return xstrdup (fe->name);
10746 else
10747 {
10748 const char *dir;
10749 int dir_len;
10750 char *full_name;
10751
10752 if (fe->dir_index)
10753 dir = lh->include_dirs[fe->dir_index - 1];
10754 else
10755 dir = comp_dir;
10756
10757 if (dir)
10758 {
10759 dir_len = strlen (dir);
10760 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
10761 strcpy (full_name, dir);
10762 full_name[dir_len] = '/';
10763 strcpy (full_name + dir_len + 1, fe->name);
10764 return full_name;
10765 }
10766 else
10767 return xstrdup (fe->name);
10768 }
10769 }
2e276125
JB
10770 else
10771 {
6a83a1e6
EZ
10772 /* The compiler produced a bogus file number. We can at least
10773 record the macro definitions made in the file, even if we
10774 won't be able to find the file by name. */
10775 char fake_name[80];
10776 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 10777
6a83a1e6
EZ
10778 complaint (&symfile_complaints,
10779 _("bad file number in macro information (%d)"),
10780 file);
2e276125 10781
6a83a1e6 10782 return xstrdup (fake_name);
2e276125
JB
10783 }
10784}
10785
10786
10787static struct macro_source_file *
10788macro_start_file (int file, int line,
10789 struct macro_source_file *current_file,
10790 const char *comp_dir,
10791 struct line_header *lh, struct objfile *objfile)
10792{
10793 /* The full name of this source file. */
10794 char *full_name = file_full_name (file, lh, comp_dir);
10795
10796 /* We don't create a macro table for this compilation unit
10797 at all until we actually get a filename. */
10798 if (! pending_macros)
4a146b47 10799 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 10800 objfile->macro_cache);
2e276125
JB
10801
10802 if (! current_file)
10803 /* If we have no current file, then this must be the start_file
10804 directive for the compilation unit's main source file. */
10805 current_file = macro_set_main (pending_macros, full_name);
10806 else
10807 current_file = macro_include (current_file, line, full_name);
10808
10809 xfree (full_name);
10810
10811 return current_file;
10812}
10813
10814
10815/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
10816 followed by a null byte. */
10817static char *
10818copy_string (const char *buf, int len)
10819{
10820 char *s = xmalloc (len + 1);
10821 memcpy (s, buf, len);
10822 s[len] = '\0';
10823
10824 return s;
10825}
10826
10827
10828static const char *
10829consume_improper_spaces (const char *p, const char *body)
10830{
10831 if (*p == ' ')
10832 {
4d3c2250 10833 complaint (&symfile_complaints,
e2e0b3e5 10834 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 10835 body);
2e276125
JB
10836
10837 while (*p == ' ')
10838 p++;
10839 }
10840
10841 return p;
10842}
10843
10844
10845static void
10846parse_macro_definition (struct macro_source_file *file, int line,
10847 const char *body)
10848{
10849 const char *p;
10850
10851 /* The body string takes one of two forms. For object-like macro
10852 definitions, it should be:
10853
10854 <macro name> " " <definition>
10855
10856 For function-like macro definitions, it should be:
10857
10858 <macro name> "() " <definition>
10859 or
10860 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
10861
10862 Spaces may appear only where explicitly indicated, and in the
10863 <definition>.
10864
10865 The Dwarf 2 spec says that an object-like macro's name is always
10866 followed by a space, but versions of GCC around March 2002 omit
10867 the space when the macro's definition is the empty string.
10868
10869 The Dwarf 2 spec says that there should be no spaces between the
10870 formal arguments in a function-like macro's formal argument list,
10871 but versions of GCC around March 2002 include spaces after the
10872 commas. */
10873
10874
10875 /* Find the extent of the macro name. The macro name is terminated
10876 by either a space or null character (for an object-like macro) or
10877 an opening paren (for a function-like macro). */
10878 for (p = body; *p; p++)
10879 if (*p == ' ' || *p == '(')
10880 break;
10881
10882 if (*p == ' ' || *p == '\0')
10883 {
10884 /* It's an object-like macro. */
10885 int name_len = p - body;
10886 char *name = copy_string (body, name_len);
10887 const char *replacement;
10888
10889 if (*p == ' ')
10890 replacement = body + name_len + 1;
10891 else
10892 {
4d3c2250 10893 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10894 replacement = body + name_len;
10895 }
10896
10897 macro_define_object (file, line, name, replacement);
10898
10899 xfree (name);
10900 }
10901 else if (*p == '(')
10902 {
10903 /* It's a function-like macro. */
10904 char *name = copy_string (body, p - body);
10905 int argc = 0;
10906 int argv_size = 1;
10907 char **argv = xmalloc (argv_size * sizeof (*argv));
10908
10909 p++;
10910
10911 p = consume_improper_spaces (p, body);
10912
10913 /* Parse the formal argument list. */
10914 while (*p && *p != ')')
10915 {
10916 /* Find the extent of the current argument name. */
10917 const char *arg_start = p;
10918
10919 while (*p && *p != ',' && *p != ')' && *p != ' ')
10920 p++;
10921
10922 if (! *p || p == arg_start)
4d3c2250 10923 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10924 else
10925 {
10926 /* Make sure argv has room for the new argument. */
10927 if (argc >= argv_size)
10928 {
10929 argv_size *= 2;
10930 argv = xrealloc (argv, argv_size * sizeof (*argv));
10931 }
10932
10933 argv[argc++] = copy_string (arg_start, p - arg_start);
10934 }
10935
10936 p = consume_improper_spaces (p, body);
10937
10938 /* Consume the comma, if present. */
10939 if (*p == ',')
10940 {
10941 p++;
10942
10943 p = consume_improper_spaces (p, body);
10944 }
10945 }
10946
10947 if (*p == ')')
10948 {
10949 p++;
10950
10951 if (*p == ' ')
10952 /* Perfectly formed definition, no complaints. */
10953 macro_define_function (file, line, name,
10954 argc, (const char **) argv,
10955 p + 1);
10956 else if (*p == '\0')
10957 {
10958 /* Complain, but do define it. */
4d3c2250 10959 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10960 macro_define_function (file, line, name,
10961 argc, (const char **) argv,
10962 p);
10963 }
10964 else
10965 /* Just complain. */
4d3c2250 10966 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10967 }
10968 else
10969 /* Just complain. */
4d3c2250 10970 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10971
10972 xfree (name);
10973 {
10974 int i;
10975
10976 for (i = 0; i < argc; i++)
10977 xfree (argv[i]);
10978 }
10979 xfree (argv);
10980 }
10981 else
4d3c2250 10982 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10983}
10984
10985
10986static void
10987dwarf_decode_macros (struct line_header *lh, unsigned int offset,
10988 char *comp_dir, bfd *abfd,
e7c27a73 10989 struct dwarf2_cu *cu)
2e276125 10990{
fe1b8b76 10991 gdb_byte *mac_ptr, *mac_end;
2e276125 10992 struct macro_source_file *current_file = 0;
757a13d0
JK
10993 enum dwarf_macinfo_record_type macinfo_type;
10994 int at_commandline;
2e276125 10995
dce234bc 10996 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 10997 {
e2e0b3e5 10998 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
10999 return;
11000 }
11001
757a13d0
JK
11002 /* First pass: Find the name of the base filename.
11003 This filename is needed in order to process all macros whose definition
11004 (or undefinition) comes from the command line. These macros are defined
11005 before the first DW_MACINFO_start_file entry, and yet still need to be
11006 associated to the base file.
11007
11008 To determine the base file name, we scan the macro definitions until we
11009 reach the first DW_MACINFO_start_file entry. We then initialize
11010 CURRENT_FILE accordingly so that any macro definition found before the
11011 first DW_MACINFO_start_file can still be associated to the base file. */
11012
dce234bc
PP
11013 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11014 mac_end = dwarf2_per_objfile->macinfo.buffer
11015 + dwarf2_per_objfile->macinfo.size;
2e276125 11016
757a13d0 11017 do
2e276125 11018 {
2e276125
JB
11019 /* Do we at least have room for a macinfo type byte? */
11020 if (mac_ptr >= mac_end)
11021 {
757a13d0
JK
11022 /* Complaint is printed during the second pass as GDB will probably
11023 stop the first pass earlier upon finding DW_MACINFO_start_file. */
11024 break;
2e276125
JB
11025 }
11026
11027 macinfo_type = read_1_byte (abfd, mac_ptr);
11028 mac_ptr++;
11029
11030 switch (macinfo_type)
11031 {
11032 /* A zero macinfo type indicates the end of the macro
11033 information. */
11034 case 0:
757a13d0
JK
11035 break;
11036
11037 case DW_MACINFO_define:
11038 case DW_MACINFO_undef:
11039 /* Only skip the data by MAC_PTR. */
11040 {
11041 unsigned int bytes_read;
11042
11043 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11044 mac_ptr += bytes_read;
11045 read_string (abfd, mac_ptr, &bytes_read);
11046 mac_ptr += bytes_read;
11047 }
11048 break;
11049
11050 case DW_MACINFO_start_file:
11051 {
11052 unsigned int bytes_read;
11053 int line, file;
11054
11055 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11056 mac_ptr += bytes_read;
11057 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11058 mac_ptr += bytes_read;
11059
11060 current_file = macro_start_file (file, line, current_file, comp_dir,
11061 lh, cu->objfile);
11062 }
11063 break;
11064
11065 case DW_MACINFO_end_file:
11066 /* No data to skip by MAC_PTR. */
11067 break;
11068
11069 case DW_MACINFO_vendor_ext:
11070 /* Only skip the data by MAC_PTR. */
11071 {
11072 unsigned int bytes_read;
11073
11074 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11075 mac_ptr += bytes_read;
11076 read_string (abfd, mac_ptr, &bytes_read);
11077 mac_ptr += bytes_read;
11078 }
11079 break;
11080
11081 default:
11082 break;
11083 }
11084 } while (macinfo_type != 0 && current_file == NULL);
11085
11086 /* Second pass: Process all entries.
11087
11088 Use the AT_COMMAND_LINE flag to determine whether we are still processing
11089 command-line macro definitions/undefinitions. This flag is unset when we
11090 reach the first DW_MACINFO_start_file entry. */
11091
dce234bc 11092 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
11093
11094 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
11095 GDB is still reading the definitions from command line. First
11096 DW_MACINFO_start_file will need to be ignored as it was already executed
11097 to create CURRENT_FILE for the main source holding also the command line
11098 definitions. On first met DW_MACINFO_start_file this flag is reset to
11099 normally execute all the remaining DW_MACINFO_start_file macinfos. */
11100
11101 at_commandline = 1;
11102
11103 do
11104 {
11105 /* Do we at least have room for a macinfo type byte? */
11106 if (mac_ptr >= mac_end)
11107 {
11108 dwarf2_macros_too_long_complaint ();
11109 break;
11110 }
11111
11112 macinfo_type = read_1_byte (abfd, mac_ptr);
11113 mac_ptr++;
11114
11115 switch (macinfo_type)
11116 {
11117 /* A zero macinfo type indicates the end of the macro
11118 information. */
11119 case 0:
11120 break;
2e276125
JB
11121
11122 case DW_MACINFO_define:
11123 case DW_MACINFO_undef:
11124 {
891d2f0b 11125 unsigned int bytes_read;
2e276125
JB
11126 int line;
11127 char *body;
11128
11129 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11130 mac_ptr += bytes_read;
11131 body = read_string (abfd, mac_ptr, &bytes_read);
11132 mac_ptr += bytes_read;
11133
11134 if (! current_file)
757a13d0
JK
11135 {
11136 /* DWARF violation as no main source is present. */
11137 complaint (&symfile_complaints,
11138 _("debug info with no main source gives macro %s "
11139 "on line %d: %s"),
11140 macinfo_type ==
11141 DW_MACINFO_define ? _("definition") : macinfo_type ==
11142 DW_MACINFO_undef ? _("undefinition") :
11143 "something-or-other", line, body);
11144 break;
11145 }
11146 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 11147 complaint (&symfile_complaints,
757a13d0
JK
11148 _("debug info gives %s macro %s with %s line %d: %s"),
11149 at_commandline ? _("command-line") : _("in-file"),
4d3c2250 11150 macinfo_type ==
757a13d0
JK
11151 DW_MACINFO_define ? _("definition") : macinfo_type ==
11152 DW_MACINFO_undef ? _("undefinition") :
11153 "something-or-other",
11154 line == 0 ? _("zero") : _("non-zero"), line, body);
11155
11156 if (macinfo_type == DW_MACINFO_define)
11157 parse_macro_definition (current_file, line, body);
11158 else if (macinfo_type == DW_MACINFO_undef)
11159 macro_undef (current_file, line, body);
2e276125
JB
11160 }
11161 break;
11162
11163 case DW_MACINFO_start_file:
11164 {
891d2f0b 11165 unsigned int bytes_read;
2e276125
JB
11166 int line, file;
11167
11168 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11169 mac_ptr += bytes_read;
11170 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11171 mac_ptr += bytes_read;
11172
757a13d0
JK
11173 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11174 complaint (&symfile_complaints,
11175 _("debug info gives source %d included "
11176 "from %s at %s line %d"),
11177 file, at_commandline ? _("command-line") : _("file"),
11178 line == 0 ? _("zero") : _("non-zero"), line);
11179
11180 if (at_commandline)
11181 {
11182 /* This DW_MACINFO_start_file was executed in the pass one. */
11183 at_commandline = 0;
11184 }
11185 else
11186 current_file = macro_start_file (file, line,
11187 current_file, comp_dir,
11188 lh, cu->objfile);
2e276125
JB
11189 }
11190 break;
11191
11192 case DW_MACINFO_end_file:
11193 if (! current_file)
4d3c2250 11194 complaint (&symfile_complaints,
e2e0b3e5 11195 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
11196 else
11197 {
11198 current_file = current_file->included_by;
11199 if (! current_file)
11200 {
11201 enum dwarf_macinfo_record_type next_type;
11202
11203 /* GCC circa March 2002 doesn't produce the zero
11204 type byte marking the end of the compilation
11205 unit. Complain if it's not there, but exit no
11206 matter what. */
11207
11208 /* Do we at least have room for a macinfo type byte? */
11209 if (mac_ptr >= mac_end)
11210 {
4d3c2250 11211 dwarf2_macros_too_long_complaint ();
2e276125
JB
11212 return;
11213 }
11214
11215 /* We don't increment mac_ptr here, so this is just
11216 a look-ahead. */
11217 next_type = read_1_byte (abfd, mac_ptr);
11218 if (next_type != 0)
4d3c2250 11219 complaint (&symfile_complaints,
e2e0b3e5 11220 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
11221
11222 return;
11223 }
11224 }
11225 break;
11226
11227 case DW_MACINFO_vendor_ext:
11228 {
891d2f0b 11229 unsigned int bytes_read;
2e276125
JB
11230 int constant;
11231 char *string;
11232
11233 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11234 mac_ptr += bytes_read;
11235 string = read_string (abfd, mac_ptr, &bytes_read);
11236 mac_ptr += bytes_read;
11237
11238 /* We don't recognize any vendor extensions. */
11239 }
11240 break;
11241 }
757a13d0 11242 } while (macinfo_type != 0);
2e276125 11243}
8e19ed76
PS
11244
11245/* Check if the attribute's form is a DW_FORM_block*
11246 if so return true else false. */
11247static int
11248attr_form_is_block (struct attribute *attr)
11249{
11250 return (attr == NULL ? 0 :
11251 attr->form == DW_FORM_block1
11252 || attr->form == DW_FORM_block2
11253 || attr->form == DW_FORM_block4
11254 || attr->form == DW_FORM_block);
11255}
4c2df51b 11256
c6a0999f
JB
11257/* Return non-zero if ATTR's value is a section offset --- classes
11258 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
11259 You may use DW_UNSND (attr) to retrieve such offsets.
11260
11261 Section 7.5.4, "Attribute Encodings", explains that no attribute
11262 may have a value that belongs to more than one of these classes; it
11263 would be ambiguous if we did, because we use the same forms for all
11264 of them. */
3690dd37
JB
11265static int
11266attr_form_is_section_offset (struct attribute *attr)
11267{
11268 return (attr->form == DW_FORM_data4
11269 || attr->form == DW_FORM_data8);
11270}
11271
11272
11273/* Return non-zero if ATTR's value falls in the 'constant' class, or
11274 zero otherwise. When this function returns true, you can apply
11275 dwarf2_get_attr_constant_value to it.
11276
11277 However, note that for some attributes you must check
11278 attr_form_is_section_offset before using this test. DW_FORM_data4
11279 and DW_FORM_data8 are members of both the constant class, and of
11280 the classes that contain offsets into other debug sections
11281 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
11282 that, if an attribute's can be either a constant or one of the
11283 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
11284 taken as section offsets, not constants. */
11285static int
11286attr_form_is_constant (struct attribute *attr)
11287{
11288 switch (attr->form)
11289 {
11290 case DW_FORM_sdata:
11291 case DW_FORM_udata:
11292 case DW_FORM_data1:
11293 case DW_FORM_data2:
11294 case DW_FORM_data4:
11295 case DW_FORM_data8:
11296 return 1;
11297 default:
11298 return 0;
11299 }
11300}
11301
4c2df51b
DJ
11302static void
11303dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 11304 struct dwarf2_cu *cu)
4c2df51b 11305{
3690dd37 11306 if (attr_form_is_section_offset (attr)
99bcc461
DJ
11307 /* ".debug_loc" may not exist at all, or the offset may be outside
11308 the section. If so, fall through to the complaint in the
11309 other branch. */
dce234bc 11310 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 11311 {
0d53c4c4 11312 struct dwarf2_loclist_baton *baton;
4c2df51b 11313
4a146b47 11314 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 11315 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
11316 baton->per_cu = cu->per_cu;
11317 gdb_assert (baton->per_cu);
4c2df51b 11318
0d53c4c4
DJ
11319 /* We don't know how long the location list is, but make sure we
11320 don't run off the edge of the section. */
dce234bc
PP
11321 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
11322 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
11323 baton->base_address = cu->base_address;
11324 if (cu->base_known == 0)
0d53c4c4 11325 complaint (&symfile_complaints,
e2e0b3e5 11326 _("Location list used without specifying the CU base address."));
4c2df51b 11327
768a979c 11328 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
11329 SYMBOL_LOCATION_BATON (sym) = baton;
11330 }
11331 else
11332 {
11333 struct dwarf2_locexpr_baton *baton;
11334
4a146b47 11335 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 11336 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
11337 baton->per_cu = cu->per_cu;
11338 gdb_assert (baton->per_cu);
0d53c4c4
DJ
11339
11340 if (attr_form_is_block (attr))
11341 {
11342 /* Note that we're just copying the block's data pointer
11343 here, not the actual data. We're still pointing into the
6502dd73
DJ
11344 info_buffer for SYM's objfile; right now we never release
11345 that buffer, but when we do clean up properly this may
11346 need to change. */
0d53c4c4
DJ
11347 baton->size = DW_BLOCK (attr)->size;
11348 baton->data = DW_BLOCK (attr)->data;
11349 }
11350 else
11351 {
11352 dwarf2_invalid_attrib_class_complaint ("location description",
11353 SYMBOL_NATURAL_NAME (sym));
11354 baton->size = 0;
11355 baton->data = NULL;
11356 }
11357
768a979c 11358 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
11359 SYMBOL_LOCATION_BATON (sym) = baton;
11360 }
4c2df51b 11361}
6502dd73 11362
ae0d2f24
UW
11363/* Return the OBJFILE associated with the compilation unit CU. */
11364
11365struct objfile *
11366dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
11367{
11368 struct objfile *objfile = per_cu->psymtab->objfile;
11369
11370 /* Return the master objfile, so that we can report and look up the
11371 correct file containing this variable. */
11372 if (objfile->separate_debug_objfile_backlink)
11373 objfile = objfile->separate_debug_objfile_backlink;
11374
11375 return objfile;
11376}
11377
11378/* Return the address size given in the compilation unit header for CU. */
11379
11380CORE_ADDR
11381dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
11382{
11383 if (per_cu->cu)
11384 return per_cu->cu->header.addr_size;
11385 else
11386 {
11387 /* If the CU is not currently read in, we re-read its header. */
11388 struct objfile *objfile = per_cu->psymtab->objfile;
11389 struct dwarf2_per_objfile *per_objfile
11390 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 11391 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24
UW
11392
11393 struct comp_unit_head cu_header;
11394 memset (&cu_header, 0, sizeof cu_header);
11395 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
11396 return cu_header.addr_size;
11397 }
11398}
11399
348e048f
DE
11400/* Locate the .debug_info compilation unit from CU's objfile which contains
11401 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
11402
11403static struct dwarf2_per_cu_data *
c764a876 11404dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
11405 struct objfile *objfile)
11406{
11407 struct dwarf2_per_cu_data *this_cu;
11408 int low, high;
11409
ae038cb0
DJ
11410 low = 0;
11411 high = dwarf2_per_objfile->n_comp_units - 1;
11412 while (high > low)
11413 {
11414 int mid = low + (high - low) / 2;
11415 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
11416 high = mid;
11417 else
11418 low = mid + 1;
11419 }
11420 gdb_assert (low == high);
11421 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
11422 {
10b3939b 11423 if (low == 0)
8a3fe4f8
AC
11424 error (_("Dwarf Error: could not find partial DIE containing "
11425 "offset 0x%lx [in module %s]"),
10b3939b
DJ
11426 (long) offset, bfd_get_filename (objfile->obfd));
11427
ae038cb0
DJ
11428 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
11429 return dwarf2_per_objfile->all_comp_units[low-1];
11430 }
11431 else
11432 {
11433 this_cu = dwarf2_per_objfile->all_comp_units[low];
11434 if (low == dwarf2_per_objfile->n_comp_units - 1
11435 && offset >= this_cu->offset + this_cu->length)
c764a876 11436 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
11437 gdb_assert (offset < this_cu->offset + this_cu->length);
11438 return this_cu;
11439 }
11440}
11441
10b3939b
DJ
11442/* Locate the compilation unit from OBJFILE which is located at exactly
11443 OFFSET. Raises an error on failure. */
11444
ae038cb0 11445static struct dwarf2_per_cu_data *
c764a876 11446dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
11447{
11448 struct dwarf2_per_cu_data *this_cu;
11449 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11450 if (this_cu->offset != offset)
c764a876 11451 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
11452 return this_cu;
11453}
11454
93311388
DE
11455/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
11456
11457static struct dwarf2_cu *
11458alloc_one_comp_unit (struct objfile *objfile)
11459{
11460 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
11461 cu->objfile = objfile;
11462 obstack_init (&cu->comp_unit_obstack);
11463 return cu;
11464}
11465
ae038cb0
DJ
11466/* Release one cached compilation unit, CU. We unlink it from the tree
11467 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
11468 the caller is responsible for that.
11469 NOTE: DATA is a void * because this function is also used as a
11470 cleanup routine. */
ae038cb0
DJ
11471
11472static void
11473free_one_comp_unit (void *data)
11474{
11475 struct dwarf2_cu *cu = data;
11476
11477 if (cu->per_cu != NULL)
11478 cu->per_cu->cu = NULL;
11479 cu->per_cu = NULL;
11480
11481 obstack_free (&cu->comp_unit_obstack, NULL);
11482
11483 xfree (cu);
11484}
11485
72bf9492 11486/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
11487 when we're finished with it. We can't free the pointer itself, but be
11488 sure to unlink it from the cache. Also release any associated storage
11489 and perform cache maintenance.
72bf9492
DJ
11490
11491 Only used during partial symbol parsing. */
11492
11493static void
11494free_stack_comp_unit (void *data)
11495{
11496 struct dwarf2_cu *cu = data;
11497
11498 obstack_free (&cu->comp_unit_obstack, NULL);
11499 cu->partial_dies = NULL;
ae038cb0
DJ
11500
11501 if (cu->per_cu != NULL)
11502 {
11503 /* This compilation unit is on the stack in our caller, so we
11504 should not xfree it. Just unlink it. */
11505 cu->per_cu->cu = NULL;
11506 cu->per_cu = NULL;
11507
11508 /* If we had a per-cu pointer, then we may have other compilation
11509 units loaded, so age them now. */
11510 age_cached_comp_units ();
11511 }
11512}
11513
11514/* Free all cached compilation units. */
11515
11516static void
11517free_cached_comp_units (void *data)
11518{
11519 struct dwarf2_per_cu_data *per_cu, **last_chain;
11520
11521 per_cu = dwarf2_per_objfile->read_in_chain;
11522 last_chain = &dwarf2_per_objfile->read_in_chain;
11523 while (per_cu != NULL)
11524 {
11525 struct dwarf2_per_cu_data *next_cu;
11526
11527 next_cu = per_cu->cu->read_in_chain;
11528
11529 free_one_comp_unit (per_cu->cu);
11530 *last_chain = next_cu;
11531
11532 per_cu = next_cu;
11533 }
11534}
11535
11536/* Increase the age counter on each cached compilation unit, and free
11537 any that are too old. */
11538
11539static void
11540age_cached_comp_units (void)
11541{
11542 struct dwarf2_per_cu_data *per_cu, **last_chain;
11543
11544 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
11545 per_cu = dwarf2_per_objfile->read_in_chain;
11546 while (per_cu != NULL)
11547 {
11548 per_cu->cu->last_used ++;
11549 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
11550 dwarf2_mark (per_cu->cu);
11551 per_cu = per_cu->cu->read_in_chain;
11552 }
11553
11554 per_cu = dwarf2_per_objfile->read_in_chain;
11555 last_chain = &dwarf2_per_objfile->read_in_chain;
11556 while (per_cu != NULL)
11557 {
11558 struct dwarf2_per_cu_data *next_cu;
11559
11560 next_cu = per_cu->cu->read_in_chain;
11561
11562 if (!per_cu->cu->mark)
11563 {
11564 free_one_comp_unit (per_cu->cu);
11565 *last_chain = next_cu;
11566 }
11567 else
11568 last_chain = &per_cu->cu->read_in_chain;
11569
11570 per_cu = next_cu;
11571 }
11572}
11573
11574/* Remove a single compilation unit from the cache. */
11575
11576static void
11577free_one_cached_comp_unit (void *target_cu)
11578{
11579 struct dwarf2_per_cu_data *per_cu, **last_chain;
11580
11581 per_cu = dwarf2_per_objfile->read_in_chain;
11582 last_chain = &dwarf2_per_objfile->read_in_chain;
11583 while (per_cu != NULL)
11584 {
11585 struct dwarf2_per_cu_data *next_cu;
11586
11587 next_cu = per_cu->cu->read_in_chain;
11588
11589 if (per_cu->cu == target_cu)
11590 {
11591 free_one_comp_unit (per_cu->cu);
11592 *last_chain = next_cu;
11593 break;
11594 }
11595 else
11596 last_chain = &per_cu->cu->read_in_chain;
11597
11598 per_cu = next_cu;
11599 }
11600}
11601
fe3e1990
DJ
11602/* Release all extra memory associated with OBJFILE. */
11603
11604void
11605dwarf2_free_objfile (struct objfile *objfile)
11606{
11607 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
11608
11609 if (dwarf2_per_objfile == NULL)
11610 return;
11611
11612 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
11613 free_cached_comp_units (NULL);
11614
11615 /* Everything else should be on the objfile obstack. */
11616}
11617
1c379e20
DJ
11618/* A pair of DIE offset and GDB type pointer. We store these
11619 in a hash table separate from the DIEs, and preserve them
11620 when the DIEs are flushed out of cache. */
11621
11622struct dwarf2_offset_and_type
11623{
11624 unsigned int offset;
11625 struct type *type;
11626};
11627
11628/* Hash function for a dwarf2_offset_and_type. */
11629
11630static hashval_t
11631offset_and_type_hash (const void *item)
11632{
11633 const struct dwarf2_offset_and_type *ofs = item;
11634 return ofs->offset;
11635}
11636
11637/* Equality function for a dwarf2_offset_and_type. */
11638
11639static int
11640offset_and_type_eq (const void *item_lhs, const void *item_rhs)
11641{
11642 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
11643 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
11644 return ofs_lhs->offset == ofs_rhs->offset;
11645}
11646
11647/* Set the type associated with DIE to TYPE. Save it in CU's hash
f792889a 11648 table if necessary. For convenience, return TYPE. */
1c379e20 11649
f792889a 11650static struct type *
1c379e20
DJ
11651set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11652{
11653 struct dwarf2_offset_and_type **slot, ofs;
11654
f792889a
DJ
11655 if (cu->type_hash == NULL)
11656 {
11657 gdb_assert (cu->per_cu != NULL);
11658 cu->per_cu->type_hash
11659 = htab_create_alloc_ex (cu->header.length / 24,
11660 offset_and_type_hash,
11661 offset_and_type_eq,
11662 NULL,
11663 &cu->objfile->objfile_obstack,
11664 hashtab_obstack_allocate,
11665 dummy_obstack_deallocate);
11666 cu->type_hash = cu->per_cu->type_hash;
11667 }
1c379e20
DJ
11668
11669 ofs.offset = die->offset;
11670 ofs.type = type;
11671 slot = (struct dwarf2_offset_and_type **)
f792889a 11672 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
1c379e20
DJ
11673 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
11674 **slot = ofs;
f792889a 11675 return type;
1c379e20
DJ
11676}
11677
f792889a
DJ
11678/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
11679 not have a saved type. */
1c379e20
DJ
11680
11681static struct type *
f792889a 11682get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
11683{
11684 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
11685 htab_t type_hash = cu->type_hash;
11686
11687 if (type_hash == NULL)
11688 return NULL;
1c379e20
DJ
11689
11690 ofs.offset = die->offset;
11691 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
11692 if (slot)
11693 return slot->type;
11694 else
11695 return NULL;
11696}
11697
10b3939b
DJ
11698/* Add a dependence relationship from CU to REF_PER_CU. */
11699
11700static void
11701dwarf2_add_dependence (struct dwarf2_cu *cu,
11702 struct dwarf2_per_cu_data *ref_per_cu)
11703{
11704 void **slot;
11705
11706 if (cu->dependencies == NULL)
11707 cu->dependencies
11708 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
11709 NULL, &cu->comp_unit_obstack,
11710 hashtab_obstack_allocate,
11711 dummy_obstack_deallocate);
11712
11713 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
11714 if (*slot == NULL)
11715 *slot = ref_per_cu;
11716}
1c379e20 11717
f504f079
DE
11718/* Subroutine of dwarf2_mark to pass to htab_traverse.
11719 Set the mark field in every compilation unit in the
ae038cb0
DJ
11720 cache that we must keep because we are keeping CU. */
11721
10b3939b
DJ
11722static int
11723dwarf2_mark_helper (void **slot, void *data)
11724{
11725 struct dwarf2_per_cu_data *per_cu;
11726
11727 per_cu = (struct dwarf2_per_cu_data *) *slot;
11728 if (per_cu->cu->mark)
11729 return 1;
11730 per_cu->cu->mark = 1;
11731
11732 if (per_cu->cu->dependencies != NULL)
11733 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
11734
11735 return 1;
11736}
11737
f504f079
DE
11738/* Set the mark field in CU and in every other compilation unit in the
11739 cache that we must keep because we are keeping CU. */
11740
ae038cb0
DJ
11741static void
11742dwarf2_mark (struct dwarf2_cu *cu)
11743{
11744 if (cu->mark)
11745 return;
11746 cu->mark = 1;
10b3939b
DJ
11747 if (cu->dependencies != NULL)
11748 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
11749}
11750
11751static void
11752dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
11753{
11754 while (per_cu)
11755 {
11756 per_cu->cu->mark = 0;
11757 per_cu = per_cu->cu->read_in_chain;
11758 }
72bf9492
DJ
11759}
11760
72bf9492
DJ
11761/* Trivial hash function for partial_die_info: the hash value of a DIE
11762 is its offset in .debug_info for this objfile. */
11763
11764static hashval_t
11765partial_die_hash (const void *item)
11766{
11767 const struct partial_die_info *part_die = item;
11768 return part_die->offset;
11769}
11770
11771/* Trivial comparison function for partial_die_info structures: two DIEs
11772 are equal if they have the same offset. */
11773
11774static int
11775partial_die_eq (const void *item_lhs, const void *item_rhs)
11776{
11777 const struct partial_die_info *part_die_lhs = item_lhs;
11778 const struct partial_die_info *part_die_rhs = item_rhs;
11779 return part_die_lhs->offset == part_die_rhs->offset;
11780}
11781
ae038cb0
DJ
11782static struct cmd_list_element *set_dwarf2_cmdlist;
11783static struct cmd_list_element *show_dwarf2_cmdlist;
11784
11785static void
11786set_dwarf2_cmd (char *args, int from_tty)
11787{
11788 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
11789}
11790
11791static void
11792show_dwarf2_cmd (char *args, int from_tty)
11793{
11794 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
11795}
11796
dce234bc
PP
11797/* If section described by INFO was mmapped, munmap it now. */
11798
11799static void
11800munmap_section_buffer (struct dwarf2_section_info *info)
11801{
11802 if (info->was_mmapped)
11803 {
11804#ifdef HAVE_MMAP
11805 intptr_t begin = (intptr_t) info->buffer;
11806 intptr_t map_begin = begin & ~(pagesize - 1);
11807 size_t map_length = info->size + begin - map_begin;
11808 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
11809#else
11810 /* Without HAVE_MMAP, we should never be here to begin with. */
11811 gdb_assert (0);
11812#endif
11813 }
11814}
11815
11816/* munmap debug sections for OBJFILE, if necessary. */
11817
11818static void
11819dwarf2_per_objfile_cleanup (struct objfile *objfile, void *d)
11820{
11821 struct dwarf2_per_objfile *data = d;
11822 munmap_section_buffer (&data->info);
11823 munmap_section_buffer (&data->abbrev);
11824 munmap_section_buffer (&data->line);
11825 munmap_section_buffer (&data->str);
11826 munmap_section_buffer (&data->macinfo);
11827 munmap_section_buffer (&data->ranges);
11828 munmap_section_buffer (&data->loc);
11829 munmap_section_buffer (&data->frame);
11830 munmap_section_buffer (&data->eh_frame);
11831}
11832
6502dd73
DJ
11833void _initialize_dwarf2_read (void);
11834
11835void
11836_initialize_dwarf2_read (void)
11837{
dce234bc
PP
11838 dwarf2_objfile_data_key
11839 = register_objfile_data_with_cleanup (dwarf2_per_objfile_cleanup);
ae038cb0 11840
1bedd215
AC
11841 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
11842Set DWARF 2 specific variables.\n\
11843Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
11844 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
11845 0/*allow-unknown*/, &maintenance_set_cmdlist);
11846
1bedd215
AC
11847 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
11848Show DWARF 2 specific variables\n\
11849Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
11850 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
11851 0/*allow-unknown*/, &maintenance_show_cmdlist);
11852
11853 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
11854 &dwarf2_max_cache_age, _("\
11855Set the upper bound on the age of cached dwarf2 compilation units."), _("\
11856Show the upper bound on the age of cached dwarf2 compilation units."), _("\
11857A higher limit means that cached compilation units will be stored\n\
11858in memory longer, and more total memory will be used. Zero disables\n\
11859caching, which can slow down startup."),
2c5b56ce 11860 NULL,
920d2a44 11861 show_dwarf2_max_cache_age,
2c5b56ce 11862 &set_dwarf2_cmdlist,
ae038cb0 11863 &show_dwarf2_cmdlist);
d97bc12b
DE
11864
11865 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
11866Set debugging of the dwarf2 DIE reader."), _("\
11867Show debugging of the dwarf2 DIE reader."), _("\
11868When enabled (non-zero), DIEs are dumped after they are read in.\n\
11869The value is the maximum depth to print."),
11870 NULL,
11871 NULL,
11872 &setdebuglist, &showdebuglist);
6502dd73 11873}
This page took 1.506896 seconds and 4 git commands to generate.