* mem-break.c [HAVE_MALLOC_H]: Include malloc.h.
[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,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 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"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
673bfd45
DE
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
6502dd73
DJ
226};
227
228static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
229
230/* names of the debugging sections */
231
233a11ab
CS
232/* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235#define INFO_SECTION "debug_info"
236#define ABBREV_SECTION "debug_abbrev"
237#define LINE_SECTION "debug_line"
233a11ab
CS
238#define LOC_SECTION "debug_loc"
239#define MACINFO_SECTION "debug_macinfo"
240#define STR_SECTION "debug_str"
241#define RANGES_SECTION "debug_ranges"
348e048f 242#define TYPES_SECTION "debug_types"
233a11ab
CS
243#define FRAME_SECTION "debug_frame"
244#define EH_FRAME_SECTION "eh_frame"
9291a0cd 245#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
246
247/* local data types */
248
57349743
JB
249/* We hold several abbreviation tables in memory at the same time. */
250#ifndef ABBREV_HASH_SIZE
251#define ABBREV_HASH_SIZE 121
252#endif
253
107d2387
AC
254/* The data in a compilation unit header, after target2host
255 translation, looks like this. */
c906108c 256struct comp_unit_head
a738430d 257{
c764a876 258 unsigned int length;
a738430d 259 short version;
a738430d
MK
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
9cbfa09e 262 unsigned int abbrev_offset;
57349743 263
a738430d
MK
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
57349743 266
a738430d
MK
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
57349743 269
a738430d
MK
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
57349743 273
d00adf39
DE
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
a738430d 277};
c906108c 278
3da10d80
KS
279/* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281struct delayed_method_info
282{
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297};
298
299typedef struct delayed_method_info delayed_method_info;
300DEF_VEC_O (delayed_method_info);
301
e7c27a73
DJ
302/* Internal state when decoding a particular compilation unit. */
303struct dwarf2_cu
304{
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
d00adf39 308 /* The header of the compilation unit. */
e7c27a73 309 struct comp_unit_head header;
e142c38c 310
d00adf39
DE
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
e142c38c
DJ
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492
DJ
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
10b3939b 361 /* A hash table of die offsets for following references. */
51545339 362 htab_t die_hash;
10b3939b
DJ
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
cb1df416
DJ
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
3da10d80
KS
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
ae038cb0
DJ
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
72bf9492
DJ
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
e7c27a73
DJ
391};
392
9291a0cd
TT
393/* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396struct dwarf2_per_cu_quick_data
397{
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417};
418
10b3939b
DJ
419/* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
ae038cb0
DJ
425struct dwarf2_per_cu_data
426{
348e048f 427 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 428 bytes should suffice to store the length of any compilation unit
45452591
DE
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
c764a876 432 unsigned int offset;
348e048f 433 unsigned int length : 29;
ae038cb0
DJ
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
c764a876 437 unsigned int queued : 1;
ae038cb0 438
5afb4e99
DJ
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
348e048f
DE
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
17ea53c3
JK
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
ae038cb0 451 struct dwarf2_cu *cu;
1c379e20 452
9291a0cd
TT
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
ae038cb0
DJ
468};
469
348e048f
DE
470/* Entry in the signatured_types hash table. */
471
472struct signatured_type
473{
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484};
485
93311388
DE
486/* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492struct die_reader_specs
493{
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503};
504
debd256d
JB
505/* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508struct line_header
509{
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
2dc7f7b3 514 unsigned char maximum_ops_per_instruction;
debd256d
JB
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
c906108c 538 {
debd256d
JB
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
aaa75496 543 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 544 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
545 } *file_names;
546
547 /* The start and end of the statement program following this
6502dd73 548 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 549 gdb_byte *statement_program_start, *statement_program_end;
debd256d 550};
c906108c
SS
551
552/* When we construct a partial symbol table entry we only
553 need this much information. */
554struct partial_die_info
555 {
72bf9492 556 /* Offset of this DIE. */
c906108c 557 unsigned int offset;
72bf9492
DJ
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
72bf9492
DJ
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
fa4028e9
JB
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
98bfdba5
PA
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
72bf9492 580 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 581 sometimes a default name for unnamed DIEs. */
c906108c 582 char *name;
72bf9492
DJ
583
584 /* The scope to prepend to our children. This is generally
585 allocated on the comp_unit_obstack, so will disappear
586 when this compilation unit leaves the cache. */
587 char *scope;
588
589 /* The location description associated with this DIE, if any. */
590 struct dwarf_block *locdesc;
591
592 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
593 CORE_ADDR lowpc;
594 CORE_ADDR highpc;
72bf9492 595
93311388 596 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 597 DW_AT_sibling, if any. */
fe1b8b76 598 gdb_byte *sibling;
72bf9492
DJ
599
600 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
601 DW_AT_specification (or DW_AT_abstract_origin or
602 DW_AT_extension). */
603 unsigned int spec_offset;
604
605 /* Pointers to this DIE's parent, first child, and next sibling,
606 if any. */
607 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
608 };
609
610/* This data structure holds the information of an abbrev. */
611struct abbrev_info
612 {
613 unsigned int number; /* number identifying abbrev */
614 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
615 unsigned short has_children; /* boolean */
616 unsigned short num_attrs; /* number of attributes */
c906108c
SS
617 struct attr_abbrev *attrs; /* an array of attribute descriptions */
618 struct abbrev_info *next; /* next in chain */
619 };
620
621struct attr_abbrev
622 {
9d25dd43
DE
623 ENUM_BITFIELD(dwarf_attribute) name : 16;
624 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
625 };
626
b60c80d6
DJ
627/* Attributes have a name and a value */
628struct attribute
629 {
9d25dd43 630 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
631 ENUM_BITFIELD(dwarf_form) form : 15;
632
633 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
634 field should be in u.str (existing only for DW_STRING) but it is kept
635 here for better struct attribute alignment. */
636 unsigned int string_is_canonical : 1;
637
b60c80d6
DJ
638 union
639 {
640 char *str;
641 struct dwarf_block *blk;
43bbcdc2
PH
642 ULONGEST unsnd;
643 LONGEST snd;
b60c80d6 644 CORE_ADDR addr;
348e048f 645 struct signatured_type *signatured_type;
b60c80d6
DJ
646 }
647 u;
648 };
649
c906108c
SS
650/* This data structure holds a complete die structure. */
651struct die_info
652 {
76815b17
DE
653 /* DWARF-2 tag for this DIE. */
654 ENUM_BITFIELD(dwarf_tag) tag : 16;
655
656 /* Number of attributes */
98bfdba5
PA
657 unsigned char num_attrs;
658
659 /* True if we're presently building the full type name for the
660 type derived from this DIE. */
661 unsigned char building_fullname : 1;
76815b17
DE
662
663 /* Abbrev number */
664 unsigned int abbrev;
665
93311388 666 /* Offset in .debug_info or .debug_types section. */
76815b17 667 unsigned int offset;
78ba4af6
JB
668
669 /* The dies in a compilation unit form an n-ary tree. PARENT
670 points to this die's parent; CHILD points to the first child of
671 this node; and all the children of a given node are chained
672 together via their SIBLING fields, terminated by a die whose
673 tag is zero. */
639d11d3
DC
674 struct die_info *child; /* Its first child, if any. */
675 struct die_info *sibling; /* Its next sibling, if any. */
676 struct die_info *parent; /* Its parent, if any. */
c906108c 677
b60c80d6
DJ
678 /* An array of attributes, with NUM_ATTRS elements. There may be
679 zero, but it's not common and zero-sized arrays are not
680 sufficiently portable C. */
681 struct attribute attrs[1];
c906108c
SS
682 };
683
5fb290d7
DJ
684struct function_range
685{
686 const char *name;
687 CORE_ADDR lowpc, highpc;
688 int seen_line;
689 struct function_range *next;
690};
691
c906108c
SS
692/* Get at parts of an attribute structure */
693
694#define DW_STRING(attr) ((attr)->u.str)
8285870a 695#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
696#define DW_UNSND(attr) ((attr)->u.unsnd)
697#define DW_BLOCK(attr) ((attr)->u.blk)
698#define DW_SND(attr) ((attr)->u.snd)
699#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 700#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
701
702/* Blocks are a bunch of untyped bytes. */
703struct dwarf_block
704 {
705 unsigned int size;
fe1b8b76 706 gdb_byte *data;
c906108c
SS
707 };
708
c906108c
SS
709#ifndef ATTR_ALLOC_CHUNK
710#define ATTR_ALLOC_CHUNK 4
711#endif
712
c906108c
SS
713/* Allocate fields for structs, unions and enums in this size. */
714#ifndef DW_FIELD_ALLOC_CHUNK
715#define DW_FIELD_ALLOC_CHUNK 4
716#endif
717
c906108c
SS
718/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
719 but this would require a corresponding change in unpack_field_as_long
720 and friends. */
721static int bits_per_byte = 8;
722
723/* The routines that read and process dies for a C struct or C++ class
724 pass lists of data member fields and lists of member function fields
725 in an instance of a field_info structure, as defined below. */
726struct field_info
c5aa993b
JM
727 {
728 /* List of data member and baseclasses fields. */
729 struct nextfield
730 {
731 struct nextfield *next;
732 int accessibility;
733 int virtuality;
734 struct field field;
735 }
7d0ccb61 736 *fields, *baseclasses;
c906108c 737
7d0ccb61 738 /* Number of fields (including baseclasses). */
c5aa993b 739 int nfields;
c906108c 740
c5aa993b
JM
741 /* Number of baseclasses. */
742 int nbaseclasses;
c906108c 743
c5aa993b
JM
744 /* Set if the accesibility of one of the fields is not public. */
745 int non_public_fields;
c906108c 746
c5aa993b
JM
747 /* Member function fields array, entries are allocated in the order they
748 are encountered in the object file. */
749 struct nextfnfield
750 {
751 struct nextfnfield *next;
752 struct fn_field fnfield;
753 }
754 *fnfields;
c906108c 755
c5aa993b
JM
756 /* Member function fieldlist array, contains name of possibly overloaded
757 member function, number of overloaded member functions and a pointer
758 to the head of the member function field chain. */
759 struct fnfieldlist
760 {
761 char *name;
762 int length;
763 struct nextfnfield *head;
764 }
765 *fnfieldlists;
c906108c 766
c5aa993b
JM
767 /* Number of entries in the fnfieldlists array. */
768 int nfnfields;
98751a41
JK
769
770 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
771 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
772 struct typedef_field_list
773 {
774 struct typedef_field field;
775 struct typedef_field_list *next;
776 }
777 *typedef_field_list;
778 unsigned typedef_field_list_count;
c5aa993b 779 };
c906108c 780
10b3939b
DJ
781/* One item on the queue of compilation units to read in full symbols
782 for. */
783struct dwarf2_queue_item
784{
785 struct dwarf2_per_cu_data *per_cu;
786 struct dwarf2_queue_item *next;
787};
788
789/* The current queue. */
790static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
791
ae038cb0
DJ
792/* Loaded secondary compilation units are kept in memory until they
793 have not been referenced for the processing of this many
794 compilation units. Set this to zero to disable caching. Cache
795 sizes of up to at least twenty will improve startup time for
796 typical inter-CU-reference binaries, at an obvious memory cost. */
797static int dwarf2_max_cache_age = 5;
920d2a44
AC
798static void
799show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
800 struct cmd_list_element *c, const char *value)
801{
802 fprintf_filtered (file, _("\
803The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
804 value);
805}
806
ae038cb0 807
c906108c
SS
808/* Various complaints about symbol reading that don't abort the process */
809
4d3c2250
KB
810static void
811dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 812{
4d3c2250 813 complaint (&symfile_complaints,
e2e0b3e5 814 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
815}
816
25e43795
DJ
817static void
818dwarf2_debug_line_missing_file_complaint (void)
819{
820 complaint (&symfile_complaints,
821 _(".debug_line section has line data without a file"));
822}
823
59205f5a
JB
824static void
825dwarf2_debug_line_missing_end_sequence_complaint (void)
826{
827 complaint (&symfile_complaints,
828 _(".debug_line section has line program sequence without an end"));
829}
830
4d3c2250
KB
831static void
832dwarf2_complex_location_expr_complaint (void)
2e276125 833{
e2e0b3e5 834 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
835}
836
4d3c2250
KB
837static void
838dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
839 int arg3)
2e276125 840{
4d3c2250 841 complaint (&symfile_complaints,
e2e0b3e5 842 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
843 arg2, arg3);
844}
845
846static void
847dwarf2_macros_too_long_complaint (void)
2e276125 848{
4d3c2250 849 complaint (&symfile_complaints,
e2e0b3e5 850 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
851}
852
853static void
854dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 855{
4d3c2250 856 complaint (&symfile_complaints,
e2e0b3e5 857 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
858 arg1);
859}
860
861static void
862dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 863{
4d3c2250 864 complaint (&symfile_complaints,
e2e0b3e5 865 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 866}
c906108c 867
c906108c
SS
868/* local function prototypes */
869
4efb68b1 870static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 871
aaa75496
JB
872static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873 struct objfile *);
874
875static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
d85a05f0 876 struct die_info *,
aaa75496
JB
877 struct partial_symtab *);
878
c67a9c90 879static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 880
72bf9492
DJ
881static void scan_partial_symbols (struct partial_die_info *,
882 CORE_ADDR *, CORE_ADDR *,
5734ee8b 883 int, struct dwarf2_cu *);
c906108c 884
72bf9492
DJ
885static void add_partial_symbol (struct partial_die_info *,
886 struct dwarf2_cu *);
63d06c5c 887
72bf9492
DJ
888static void add_partial_namespace (struct partial_die_info *pdi,
889 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 890 int need_pc, struct dwarf2_cu *cu);
63d06c5c 891
5d7cb8df
JK
892static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
893 CORE_ADDR *highpc, int need_pc,
894 struct dwarf2_cu *cu);
895
72bf9492
DJ
896static void add_partial_enumeration (struct partial_die_info *enum_pdi,
897 struct dwarf2_cu *cu);
91c24f0a 898
bc30ff58
JB
899static void add_partial_subprogram (struct partial_die_info *pdi,
900 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 901 int need_pc, struct dwarf2_cu *cu);
bc30ff58 902
fe1b8b76 903static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
904 gdb_byte *buffer, gdb_byte *info_ptr,
905 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 906
a14ed312 907static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 908
a14ed312 909static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 910
e7c27a73 911static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 912
f3dd6933 913static void dwarf2_free_abbrev_table (void *);
c906108c 914
fe1b8b76 915static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 916 struct dwarf2_cu *);
72bf9492 917
57349743 918static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 919 struct dwarf2_cu *);
c906108c 920
93311388
DE
921static struct partial_die_info *load_partial_dies (bfd *,
922 gdb_byte *, gdb_byte *,
923 int, struct dwarf2_cu *);
72bf9492 924
fe1b8b76 925static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
926 struct abbrev_info *abbrev,
927 unsigned int, bfd *,
928 gdb_byte *, gdb_byte *,
929 struct dwarf2_cu *);
c906108c 930
c764a876 931static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 932 struct dwarf2_cu *);
72bf9492
DJ
933
934static void fixup_partial_die (struct partial_die_info *,
935 struct dwarf2_cu *);
936
fe1b8b76
JB
937static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
938 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 939
fe1b8b76
JB
940static gdb_byte *read_attribute_value (struct attribute *, unsigned,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 942
fe1b8b76 943static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 944
fe1b8b76 945static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 946
fe1b8b76 947static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 948
fe1b8b76 949static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 950
93311388 951static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 952
fe1b8b76 953static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 954 unsigned int *);
c906108c 955
c764a876
DE
956static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
957
958static LONGEST read_checked_initial_length_and_offset
959 (bfd *, gdb_byte *, const struct comp_unit_head *,
960 unsigned int *, unsigned int *);
613e1657 961
fe1b8b76 962static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
963 unsigned int *);
964
965static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 966
fe1b8b76 967static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 968
9b1c24c8 969static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 970
fe1b8b76
JB
971static char *read_indirect_string (bfd *, gdb_byte *,
972 const struct comp_unit_head *,
973 unsigned int *);
4bdf3d34 974
fe1b8b76 975static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 976
fe1b8b76 977static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 978
fe1b8b76 979static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 980
e142c38c 981static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 982
e142c38c
DJ
983static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
984 struct dwarf2_cu *);
c906108c 985
348e048f
DE
986static struct attribute *dwarf2_attr_no_follow (struct die_info *,
987 unsigned int,
988 struct dwarf2_cu *);
989
05cf31d1
JB
990static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
991 struct dwarf2_cu *cu);
992
e142c38c 993static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 994
e142c38c 995static struct die_info *die_specification (struct die_info *die,
f2f0e013 996 struct dwarf2_cu **);
63d06c5c 997
debd256d
JB
998static void free_line_header (struct line_header *lh);
999
aaa75496
JB
1000static void add_file_name (struct line_header *, char *, unsigned int,
1001 unsigned int, unsigned int);
1002
debd256d
JB
1003static struct line_header *(dwarf_decode_line_header
1004 (unsigned int offset,
e7c27a73 1005 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
1006
1007static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 1008 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1009
4f1520fb 1010static void dwarf2_start_subfile (char *, char *, char *);
c906108c 1011
a14ed312 1012static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1013 struct dwarf2_cu *);
c906108c 1014
34eaf542
TT
1015static struct symbol *new_symbol_full (struct die_info *, struct type *,
1016 struct dwarf2_cu *, struct symbol *);
1017
a14ed312 1018static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1019 struct dwarf2_cu *);
c906108c 1020
98bfdba5
PA
1021static void dwarf2_const_value_attr (struct attribute *attr,
1022 struct type *type,
1023 const char *name,
1024 struct obstack *obstack,
1025 struct dwarf2_cu *cu, long *value,
1026 gdb_byte **bytes,
1027 struct dwarf2_locexpr_baton **baton);
2df3850c 1028
e7c27a73 1029static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1030
b4ba55a1
JB
1031static int need_gnat_info (struct dwarf2_cu *);
1032
1033static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1034
1035static void set_descriptive_type (struct type *, struct die_info *,
1036 struct dwarf2_cu *);
1037
e7c27a73
DJ
1038static struct type *die_containing_type (struct die_info *,
1039 struct dwarf2_cu *);
c906108c 1040
673bfd45
DE
1041static struct type *lookup_die_type (struct die_info *, struct attribute *,
1042 struct dwarf2_cu *);
c906108c 1043
f792889a 1044static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1045
673bfd45
DE
1046static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1047
086ed43d 1048static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1049
6e70227d 1050static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1051 const char *suffix, int physname,
1052 struct dwarf2_cu *cu);
63d06c5c 1053
e7c27a73 1054static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1055
348e048f
DE
1056static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1057
e7c27a73 1058static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1059
e7c27a73 1060static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1061
ff013f42
JK
1062static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1063 struct dwarf2_cu *, struct partial_symtab *);
1064
a14ed312 1065static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1066 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1067 struct partial_symtab *);
c906108c 1068
fae299cd
DC
1069static void get_scope_pc_bounds (struct die_info *,
1070 CORE_ADDR *, CORE_ADDR *,
1071 struct dwarf2_cu *);
1072
801e3a5b
JB
1073static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1074 CORE_ADDR, struct dwarf2_cu *);
1075
a14ed312 1076static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1077 struct dwarf2_cu *);
c906108c 1078
a14ed312 1079static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1080 struct type *, struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1083 struct die_info *, struct type *,
e7c27a73 1084 struct dwarf2_cu *);
c906108c 1085
a14ed312 1086static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1087 struct type *, struct dwarf2_cu *);
c906108c 1088
134d01f1 1089static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1090
e7c27a73 1091static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1092
e7c27a73 1093static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1094
5d7cb8df
JK
1095static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1096
27aa8d6a
SW
1097static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1098
f55ee35c
JK
1099static struct type *read_module_type (struct die_info *die,
1100 struct dwarf2_cu *cu);
1101
38d518c9 1102static const char *namespace_name (struct die_info *die,
e142c38c 1103 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1104
134d01f1 1105static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1106
e7c27a73 1107static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1108
6e70227d 1109static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1110 struct dwarf2_cu *);
1111
93311388 1112static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1113
93311388
DE
1114static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
d97bc12b
DE
1116 gdb_byte **new_info_ptr,
1117 struct die_info *parent);
1118
93311388
DE
1119static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1120 gdb_byte *info_ptr,
fe1b8b76 1121 gdb_byte **new_info_ptr,
639d11d3
DC
1122 struct die_info *parent);
1123
93311388
DE
1124static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1125 gdb_byte *info_ptr,
fe1b8b76 1126 gdb_byte **new_info_ptr,
639d11d3
DC
1127 struct die_info *parent);
1128
93311388
DE
1129static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1130 struct die_info **, gdb_byte *,
1131 int *);
1132
e7c27a73 1133static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1134
71c25dea
TT
1135static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1136 struct obstack *);
1137
e142c38c 1138static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1139
98bfdba5
PA
1140static const char *dwarf2_full_name (char *name,
1141 struct die_info *die,
1142 struct dwarf2_cu *cu);
1143
e142c38c 1144static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1145 struct dwarf2_cu **);
9219021c 1146
a14ed312 1147static char *dwarf_tag_name (unsigned int);
c906108c 1148
a14ed312 1149static char *dwarf_attr_name (unsigned int);
c906108c 1150
a14ed312 1151static char *dwarf_form_name (unsigned int);
c906108c 1152
a14ed312 1153static char *dwarf_bool_name (unsigned int);
c906108c 1154
a14ed312 1155static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1156
1157#if 0
a14ed312 1158static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1159#endif
1160
f9aca02d 1161static struct die_info *sibling_die (struct die_info *);
c906108c 1162
d97bc12b
DE
1163static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1164
1165static void dump_die_for_error (struct die_info *);
1166
1167static void dump_die_1 (struct ui_file *, int level, int max_level,
1168 struct die_info *);
c906108c 1169
d97bc12b 1170/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1171
51545339 1172static void store_in_ref_table (struct die_info *,
10b3939b 1173 struct dwarf2_cu *);
c906108c 1174
93311388
DE
1175static int is_ref_attr (struct attribute *);
1176
c764a876 1177static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1178
43bbcdc2 1179static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1180
348e048f
DE
1181static struct die_info *follow_die_ref_or_sig (struct die_info *,
1182 struct attribute *,
1183 struct dwarf2_cu **);
1184
10b3939b
DJ
1185static struct die_info *follow_die_ref (struct die_info *,
1186 struct attribute *,
f2f0e013 1187 struct dwarf2_cu **);
c906108c 1188
348e048f
DE
1189static struct die_info *follow_die_sig (struct die_info *,
1190 struct attribute *,
1191 struct dwarf2_cu **);
1192
1193static void read_signatured_type_at_offset (struct objfile *objfile,
1194 unsigned int offset);
1195
1196static void read_signatured_type (struct objfile *,
1197 struct signatured_type *type_sig);
1198
c906108c
SS
1199/* memory allocation interface */
1200
7b5a2f43 1201static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1202
f3dd6933 1203static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1204
b60c80d6 1205static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1206
e142c38c 1207static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1208
e142c38c
DJ
1209static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1210 struct dwarf2_cu *);
5fb290d7 1211
2e276125 1212static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1213 char *, bfd *, struct dwarf2_cu *);
2e276125 1214
8e19ed76
PS
1215static int attr_form_is_block (struct attribute *);
1216
3690dd37
JB
1217static int attr_form_is_section_offset (struct attribute *);
1218
1219static int attr_form_is_constant (struct attribute *);
1220
93e7bd98
DJ
1221static void dwarf2_symbol_mark_computed (struct attribute *attr,
1222 struct symbol *sym,
1223 struct dwarf2_cu *cu);
4c2df51b 1224
93311388
DE
1225static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1226 struct abbrev_info *abbrev,
1227 struct dwarf2_cu *cu);
4bb7a0a7 1228
72bf9492
DJ
1229static void free_stack_comp_unit (void *);
1230
72bf9492
DJ
1231static hashval_t partial_die_hash (const void *item);
1232
1233static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1234
ae038cb0 1235static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1236 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1237
1238static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1239 (unsigned int offset, struct objfile *objfile);
ae038cb0 1240
93311388
DE
1241static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1242
ae038cb0
DJ
1243static void free_one_comp_unit (void *);
1244
1245static void free_cached_comp_units (void *);
1246
1247static void age_cached_comp_units (void);
1248
1249static void free_one_cached_comp_unit (void *);
1250
f792889a
DJ
1251static struct type *set_die_type (struct die_info *, struct type *,
1252 struct dwarf2_cu *);
1c379e20 1253
ae038cb0
DJ
1254static void create_all_comp_units (struct objfile *);
1255
1fd400ff
TT
1256static int create_debug_types_hash_table (struct objfile *objfile);
1257
93311388
DE
1258static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1259 struct objfile *);
10b3939b
DJ
1260
1261static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1262
1263static void dwarf2_add_dependence (struct dwarf2_cu *,
1264 struct dwarf2_per_cu_data *);
1265
ae038cb0
DJ
1266static void dwarf2_mark (struct dwarf2_cu *);
1267
1268static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1269
673bfd45
DE
1270static struct type *get_die_type_at_offset (unsigned int,
1271 struct dwarf2_per_cu_data *per_cu);
1272
f792889a 1273static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1274
9291a0cd
TT
1275static void dwarf2_release_queue (void *dummy);
1276
1277static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1278 struct objfile *objfile);
1279
1280static void process_queue (struct objfile *objfile);
1281
1282static void find_file_and_directory (struct die_info *die,
1283 struct dwarf2_cu *cu,
1284 char **name, char **comp_dir);
1285
1286static char *file_full_name (int file, struct line_header *lh,
1287 const char *comp_dir);
1288
1289static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1290 gdb_byte *info_ptr,
1291 gdb_byte *buffer,
1292 unsigned int buffer_size,
1293 bfd *abfd);
1294
1295static void init_cu_die_reader (struct die_reader_specs *reader,
1296 struct dwarf2_cu *cu);
1297
673bfd45 1298static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1299
9291a0cd
TT
1300#if WORDS_BIGENDIAN
1301
1302/* Convert VALUE between big- and little-endian. */
1303static offset_type
1304byte_swap (offset_type value)
1305{
1306 offset_type result;
1307
1308 result = (value & 0xff) << 24;
1309 result |= (value & 0xff00) << 8;
1310 result |= (value & 0xff0000) >> 8;
1311 result |= (value & 0xff000000) >> 24;
1312 return result;
1313}
1314
1315#define MAYBE_SWAP(V) byte_swap (V)
1316
1317#else
1318#define MAYBE_SWAP(V) (V)
1319#endif /* WORDS_BIGENDIAN */
1320
1321/* The suffix for an index file. */
1322#define INDEX_SUFFIX ".gdb-index"
1323
3da10d80
KS
1324static const char *dwarf2_physname (char *name, struct die_info *die,
1325 struct dwarf2_cu *cu);
1326
c906108c
SS
1327/* Try to locate the sections we need for DWARF 2 debugging
1328 information and return true if we have enough to do something. */
1329
1330int
6502dd73 1331dwarf2_has_info (struct objfile *objfile)
c906108c 1332{
be391dca
TT
1333 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1334 if (!dwarf2_per_objfile)
1335 {
1336 /* Initialize per-objfile state. */
1337 struct dwarf2_per_objfile *data
1338 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1339
be391dca
TT
1340 memset (data, 0, sizeof (*data));
1341 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1342 dwarf2_per_objfile = data;
6502dd73 1343
be391dca
TT
1344 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1345 dwarf2_per_objfile->objfile = objfile;
1346 }
1347 return (dwarf2_per_objfile->info.asection != NULL
1348 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1349}
1350
233a11ab
CS
1351/* When loading sections, we can either look for ".<name>", or for
1352 * ".z<name>", which indicates a compressed section. */
1353
1354static int
dce234bc 1355section_is_p (const char *section_name, const char *name)
233a11ab 1356{
dce234bc
PP
1357 return (section_name[0] == '.'
1358 && (strcmp (section_name + 1, name) == 0
1359 || (section_name[1] == 'z'
1360 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1361}
1362
c906108c
SS
1363/* This function is mapped across the sections and remembers the
1364 offset and size of each of the debugging sections we are interested
1365 in. */
1366
1367static void
72dca2f5 1368dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1369{
dce234bc 1370 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1371 {
dce234bc
PP
1372 dwarf2_per_objfile->info.asection = sectp;
1373 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1374 }
dce234bc 1375 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1376 {
dce234bc
PP
1377 dwarf2_per_objfile->abbrev.asection = sectp;
1378 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1379 }
dce234bc 1380 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1381 {
dce234bc
PP
1382 dwarf2_per_objfile->line.asection = sectp;
1383 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1384 }
dce234bc 1385 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1386 {
dce234bc
PP
1387 dwarf2_per_objfile->loc.asection = sectp;
1388 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1389 }
dce234bc 1390 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1391 {
dce234bc
PP
1392 dwarf2_per_objfile->macinfo.asection = sectp;
1393 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1394 }
dce234bc 1395 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1396 {
dce234bc
PP
1397 dwarf2_per_objfile->str.asection = sectp;
1398 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1399 }
dce234bc 1400 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1401 {
dce234bc
PP
1402 dwarf2_per_objfile->frame.asection = sectp;
1403 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1404 }
dce234bc 1405 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1406 {
3799ccc6 1407 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1408
3799ccc6
EZ
1409 if (aflag & SEC_HAS_CONTENTS)
1410 {
dce234bc
PP
1411 dwarf2_per_objfile->eh_frame.asection = sectp;
1412 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1413 }
b6af0555 1414 }
dce234bc 1415 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1416 {
dce234bc
PP
1417 dwarf2_per_objfile->ranges.asection = sectp;
1418 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1419 }
348e048f
DE
1420 else if (section_is_p (sectp->name, TYPES_SECTION))
1421 {
1422 dwarf2_per_objfile->types.asection = sectp;
1423 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1424 }
9291a0cd
TT
1425 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1426 {
1427 dwarf2_per_objfile->gdb_index.asection = sectp;
1428 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1429 }
dce234bc 1430
72dca2f5
FR
1431 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1432 && bfd_section_vma (abfd, sectp) == 0)
1433 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1434}
1435
dce234bc
PP
1436/* Decompress a section that was compressed using zlib. Store the
1437 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1438
1439static void
dce234bc
PP
1440zlib_decompress_section (struct objfile *objfile, asection *sectp,
1441 gdb_byte **outbuf, bfd_size_type *outsize)
1442{
1443 bfd *abfd = objfile->obfd;
1444#ifndef HAVE_ZLIB_H
1445 error (_("Support for zlib-compressed DWARF data (from '%s') "
1446 "is disabled in this copy of GDB"),
1447 bfd_get_filename (abfd));
1448#else
1449 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1450 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1451 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1452 bfd_size_type uncompressed_size;
1453 gdb_byte *uncompressed_buffer;
1454 z_stream strm;
1455 int rc;
1456 int header_size = 12;
1457
1458 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1459 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1460 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1461 bfd_get_filename (abfd));
1462
1463 /* Read the zlib header. In this case, it should be "ZLIB" followed
1464 by the uncompressed section size, 8 bytes in big-endian order. */
1465 if (compressed_size < header_size
1466 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1467 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1468 bfd_get_filename (abfd));
1469 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1475 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1476 uncompressed_size += compressed_buffer[11];
1477
1478 /* It is possible the section consists of several compressed
1479 buffers concatenated together, so we uncompress in a loop. */
1480 strm.zalloc = NULL;
1481 strm.zfree = NULL;
1482 strm.opaque = NULL;
1483 strm.avail_in = compressed_size - header_size;
1484 strm.next_in = (Bytef*) compressed_buffer + header_size;
1485 strm.avail_out = uncompressed_size;
1486 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1487 uncompressed_size);
1488 rc = inflateInit (&strm);
1489 while (strm.avail_in > 0)
1490 {
1491 if (rc != Z_OK)
1492 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1493 bfd_get_filename (abfd), rc);
1494 strm.next_out = ((Bytef*) uncompressed_buffer
1495 + (uncompressed_size - strm.avail_out));
1496 rc = inflate (&strm, Z_FINISH);
1497 if (rc != Z_STREAM_END)
1498 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1499 bfd_get_filename (abfd), rc);
1500 rc = inflateReset (&strm);
1501 }
1502 rc = inflateEnd (&strm);
1503 if (rc != Z_OK
1504 || strm.avail_out != 0)
1505 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1506 bfd_get_filename (abfd), rc);
1507
affddf13 1508 do_cleanups (cleanup);
dce234bc
PP
1509 *outbuf = uncompressed_buffer;
1510 *outsize = uncompressed_size;
1511#endif
233a11ab
CS
1512}
1513
dce234bc
PP
1514/* Read the contents of the section SECTP from object file specified by
1515 OBJFILE, store info about the section into INFO.
1516 If the section is compressed, uncompress it before returning. */
c906108c 1517
dce234bc
PP
1518static void
1519dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1520{
dce234bc
PP
1521 bfd *abfd = objfile->obfd;
1522 asection *sectp = info->asection;
1523 gdb_byte *buf, *retbuf;
1524 unsigned char header[4];
c906108c 1525
be391dca
TT
1526 if (info->readin)
1527 return;
dce234bc
PP
1528 info->buffer = NULL;
1529 info->was_mmapped = 0;
be391dca 1530 info->readin = 1;
188dd5d6 1531
dce234bc
PP
1532 if (info->asection == NULL || info->size == 0)
1533 return;
c906108c 1534
dce234bc
PP
1535 /* Check if the file has a 4-byte header indicating compression. */
1536 if (info->size > sizeof (header)
1537 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1538 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1539 {
1540 /* Upon decompression, update the buffer and its size. */
1541 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1542 {
1543 zlib_decompress_section (objfile, sectp, &info->buffer,
1544 &info->size);
1545 return;
1546 }
1547 }
4bdf3d34 1548
dce234bc
PP
1549#ifdef HAVE_MMAP
1550 if (pagesize == 0)
1551 pagesize = getpagesize ();
2e276125 1552
dce234bc
PP
1553 /* Only try to mmap sections which are large enough: we don't want to
1554 waste space due to fragmentation. Also, only try mmap for sections
1555 without relocations. */
1556
1557 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1558 {
1559 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1560 size_t map_length = info->size + sectp->filepos - pg_offset;
1561 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1562 MAP_PRIVATE, pg_offset);
1563
1564 if (retbuf != MAP_FAILED)
1565 {
1566 info->was_mmapped = 1;
1567 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1568#if HAVE_POSIX_MADVISE
1569 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1570#endif
dce234bc
PP
1571 return;
1572 }
1573 }
1574#endif
1575
1576 /* If we get here, we are a normal, not-compressed section. */
1577 info->buffer = buf
1578 = obstack_alloc (&objfile->objfile_obstack, info->size);
1579
1580 /* When debugging .o files, we may need to apply relocations; see
1581 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1582 We never compress sections in .o files, so we only need to
1583 try this when the section is not compressed. */
ac8035ab 1584 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1585 if (retbuf != NULL)
1586 {
1587 info->buffer = retbuf;
1588 return;
1589 }
1590
1591 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1592 || bfd_bread (buf, info->size, abfd) != info->size)
1593 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1594 bfd_get_filename (abfd));
1595}
1596
1597/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1598 SECTION_NAME. */
af34e669 1599
dce234bc
PP
1600void
1601dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1602 asection **sectp, gdb_byte **bufp,
1603 bfd_size_type *sizep)
1604{
1605 struct dwarf2_per_objfile *data
1606 = objfile_data (objfile, dwarf2_objfile_data_key);
1607 struct dwarf2_section_info *info;
a3b2a86b
TT
1608
1609 /* We may see an objfile without any DWARF, in which case we just
1610 return nothing. */
1611 if (data == NULL)
1612 {
1613 *sectp = NULL;
1614 *bufp = NULL;
1615 *sizep = 0;
1616 return;
1617 }
dce234bc
PP
1618 if (section_is_p (section_name, EH_FRAME_SECTION))
1619 info = &data->eh_frame;
1620 else if (section_is_p (section_name, FRAME_SECTION))
1621 info = &data->frame;
0d53c4c4 1622 else
f3574227 1623 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1624
1625 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1626 /* We haven't read this section in yet. Do it now. */
1627 dwarf2_read_section (objfile, info);
1628
1629 *sectp = info->asection;
1630 *bufp = info->buffer;
1631 *sizep = info->size;
1632}
1633
9291a0cd
TT
1634\f
1635
1636/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1637 this CU came. */
1638static void
1639dw2_do_instantiate_symtab (struct objfile *objfile,
1640 struct dwarf2_per_cu_data *per_cu)
1641{
1642 struct cleanup *back_to;
1643
1644 back_to = make_cleanup (dwarf2_release_queue, NULL);
1645
1646 queue_comp_unit (per_cu, objfile);
1647
1648 if (per_cu->from_debug_types)
1649 read_signatured_type_at_offset (objfile, per_cu->offset);
1650 else
1651 load_full_comp_unit (per_cu, objfile);
1652
1653 process_queue (objfile);
1654
1655 /* Age the cache, releasing compilation units that have not
1656 been used recently. */
1657 age_cached_comp_units ();
1658
1659 do_cleanups (back_to);
1660}
1661
1662/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1663 the objfile from which this CU came. Returns the resulting symbol
1664 table. */
1665static struct symtab *
1666dw2_instantiate_symtab (struct objfile *objfile,
1667 struct dwarf2_per_cu_data *per_cu)
1668{
1669 if (!per_cu->v.quick->symtab)
1670 {
1671 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1672 increment_reading_symtab ();
1673 dw2_do_instantiate_symtab (objfile, per_cu);
1674 do_cleanups (back_to);
1675 }
1676 return per_cu->v.quick->symtab;
1677}
1678
1fd400ff
TT
1679/* Return the CU given its index. */
1680static struct dwarf2_per_cu_data *
1681dw2_get_cu (int index)
1682{
1683 if (index >= dwarf2_per_objfile->n_comp_units)
1684 {
1685 index -= dwarf2_per_objfile->n_comp_units;
1686 return dwarf2_per_objfile->type_comp_units[index];
1687 }
1688 return dwarf2_per_objfile->all_comp_units[index];
1689}
1690
9291a0cd
TT
1691/* A helper function that knows how to read a 64-bit value in a way
1692 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1693 otherwise. */
1694static int
1695extract_cu_value (const char *bytes, ULONGEST *result)
1696{
1697 if (sizeof (ULONGEST) < 8)
1698 {
1699 int i;
1700
1701 /* Ignore the upper 4 bytes if they are all zero. */
1702 for (i = 0; i < 4; ++i)
1703 if (bytes[i + 4] != 0)
1704 return 0;
1705
1706 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1707 }
1708 else
1709 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1710 return 1;
1711}
1712
1713/* Read the CU list from the mapped index, and use it to create all
1714 the CU objects for this objfile. Return 0 if something went wrong,
1715 1 if everything went ok. */
1716static int
1fd400ff
TT
1717create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1718 offset_type cu_list_elements)
9291a0cd
TT
1719{
1720 offset_type i;
9291a0cd
TT
1721
1722 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1723 dwarf2_per_objfile->all_comp_units
1724 = obstack_alloc (&objfile->objfile_obstack,
1725 dwarf2_per_objfile->n_comp_units
1726 * sizeof (struct dwarf2_per_cu_data *));
1727
1728 for (i = 0; i < cu_list_elements; i += 2)
1729 {
1730 struct dwarf2_per_cu_data *the_cu;
1731 ULONGEST offset, length;
1732
1733 if (!extract_cu_value (cu_list, &offset)
1734 || !extract_cu_value (cu_list + 8, &length))
1735 return 0;
1736 cu_list += 2 * 8;
1737
1738 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1739 struct dwarf2_per_cu_data);
1740 the_cu->offset = offset;
1741 the_cu->length = length;
1742 the_cu->objfile = objfile;
1743 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1744 struct dwarf2_per_cu_quick_data);
1745 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1746 }
1747
1748 return 1;
1749}
1750
1fd400ff 1751/* Create the signatured type hash table from the index. */
673bfd45 1752
1fd400ff 1753static int
673bfd45
DE
1754create_signatured_type_table_from_index (struct objfile *objfile,
1755 const gdb_byte *bytes,
1756 offset_type elements)
1fd400ff
TT
1757{
1758 offset_type i;
673bfd45 1759 htab_t sig_types_hash;
1fd400ff
TT
1760
1761 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1762 dwarf2_per_objfile->type_comp_units
1763 = obstack_alloc (&objfile->objfile_obstack,
1764 dwarf2_per_objfile->n_type_comp_units
1765 * sizeof (struct dwarf2_per_cu_data *));
1766
673bfd45 1767 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1768
1769 for (i = 0; i < elements; i += 3)
1770 {
1771 struct signatured_type *type_sig;
1772 ULONGEST offset, type_offset, signature;
1773 void **slot;
1774
1775 if (!extract_cu_value (bytes, &offset)
1776 || !extract_cu_value (bytes + 8, &type_offset))
1777 return 0;
1778 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1779 bytes += 3 * 8;
1780
1781 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1782 struct signatured_type);
1783 type_sig->signature = signature;
1784 type_sig->offset = offset;
1785 type_sig->type_offset = type_offset;
1786 type_sig->per_cu.from_debug_types = 1;
1787 type_sig->per_cu.offset = offset;
1788 type_sig->per_cu.objfile = objfile;
1789 type_sig->per_cu.v.quick
1790 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1791 struct dwarf2_per_cu_quick_data);
1792
673bfd45 1793 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1794 *slot = type_sig;
1795
1796 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1797 }
1798
673bfd45 1799 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1800
1801 return 1;
1802}
1803
9291a0cd
TT
1804/* Read the address map data from the mapped index, and use it to
1805 populate the objfile's psymtabs_addrmap. */
1806static void
1807create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1808{
1809 const gdb_byte *iter, *end;
1810 struct obstack temp_obstack;
1811 struct addrmap *mutable_map;
1812 struct cleanup *cleanup;
1813 CORE_ADDR baseaddr;
1814
1815 obstack_init (&temp_obstack);
1816 cleanup = make_cleanup_obstack_free (&temp_obstack);
1817 mutable_map = addrmap_create_mutable (&temp_obstack);
1818
1819 iter = index->address_table;
1820 end = iter + index->address_table_size;
1821
1822 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1823
1824 while (iter < end)
1825 {
1826 ULONGEST hi, lo, cu_index;
1827 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1828 iter += 8;
1829 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1830 iter += 8;
1831 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1832 iter += 4;
1833
1834 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1835 dw2_get_cu (cu_index));
9291a0cd
TT
1836 }
1837
1838 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1839 &objfile->objfile_obstack);
1840 do_cleanups (cleanup);
1841}
1842
1843/* The hash function for strings in the mapped index. This is the
1844 same as the hashtab.c hash function, but we keep a separate copy to
1845 maintain control over the implementation. This is necessary
1846 because the hash function is tied to the format of the mapped index
1847 file. */
1848static hashval_t
1849mapped_index_string_hash (const void *p)
1850{
1851 const unsigned char *str = (const unsigned char *) p;
1852 hashval_t r = 0;
1853 unsigned char c;
1854
1855 while ((c = *str++) != 0)
1856 r = r * 67 + c - 113;
1857
1858 return r;
1859}
1860
1861/* Find a slot in the mapped index INDEX for the object named NAME.
1862 If NAME is found, set *VEC_OUT to point to the CU vector in the
1863 constant pool and return 1. If NAME cannot be found, return 0. */
1864static int
1865find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1866 offset_type **vec_out)
1867{
1868 offset_type hash = mapped_index_string_hash (name);
1869 offset_type slot, step;
1870
1871 slot = hash & (index->index_table_slots - 1);
1872 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1873
1874 for (;;)
1875 {
1876 /* Convert a slot number to an offset into the table. */
1877 offset_type i = 2 * slot;
1878 const char *str;
1879 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1880 return 0;
1881
1882 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1883 if (!strcmp (name, str))
1884 {
1885 *vec_out = (offset_type *) (index->constant_pool
1886 + MAYBE_SWAP (index->index_table[i + 1]));
1887 return 1;
1888 }
1889
1890 slot = (slot + step) & (index->index_table_slots - 1);
1891 }
1892}
1893
1894/* Read the index file. If everything went ok, initialize the "quick"
1895 elements of all the CUs and return 1. Otherwise, return 0. */
1896static int
1897dwarf2_read_index (struct objfile *objfile)
1898{
9291a0cd
TT
1899 char *addr;
1900 struct mapped_index *map;
b3b272e1 1901 offset_type *metadata;
ac0b195c
KW
1902 const gdb_byte *cu_list;
1903 const gdb_byte *types_list = NULL;
1904 offset_type version, cu_list_elements;
1905 offset_type types_list_elements = 0;
1fd400ff 1906 int i;
9291a0cd
TT
1907
1908 if (dwarf2_per_objfile->gdb_index.asection == NULL
1909 || dwarf2_per_objfile->gdb_index.size == 0)
1910 return 0;
1911 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1912
1913 addr = dwarf2_per_objfile->gdb_index.buffer;
1914 /* Version check. */
1fd400ff
TT
1915 version = MAYBE_SWAP (*(offset_type *) addr);
1916 if (version == 1)
1917 {
1918 /* Index version 1 neglected to account for .debug_types. So,
1919 if we see .debug_types, we cannot use this index. */
1920 if (dwarf2_per_objfile->types.asection != NULL
1921 && dwarf2_per_objfile->types.size != 0)
1922 return 0;
1923 }
1924 else if (version != 2)
9291a0cd
TT
1925 return 0;
1926
1927 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1928 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1929
1930 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1931
1932 i = 0;
1933 cu_list = addr + MAYBE_SWAP (metadata[i]);
1934 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1935 / 8);
1fd400ff
TT
1936 ++i;
1937
1938 if (version == 2)
1939 {
1940 types_list = addr + MAYBE_SWAP (metadata[i]);
1941 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1942 - MAYBE_SWAP (metadata[i]))
1943 / 8);
1944 ++i;
1945 }
1946
1947 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1948 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1949 - MAYBE_SWAP (metadata[i]));
1950 ++i;
1951
1952 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1953 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1954 - MAYBE_SWAP (metadata[i]))
9291a0cd 1955 / (2 * sizeof (offset_type)));
1fd400ff 1956 ++i;
9291a0cd 1957
1fd400ff
TT
1958 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1959
1960 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1961 return 0;
1962
1963 if (version == 2
1964 && types_list_elements
673bfd45
DE
1965 && !create_signatured_type_table_from_index (objfile, types_list,
1966 types_list_elements))
9291a0cd
TT
1967 return 0;
1968
1969 create_addrmap_from_index (objfile, map);
1970
1971 dwarf2_per_objfile->index_table = map;
1972 dwarf2_per_objfile->using_index = 1;
1973
1974 return 1;
1975}
1976
1977/* A helper for the "quick" functions which sets the global
1978 dwarf2_per_objfile according to OBJFILE. */
1979static void
1980dw2_setup (struct objfile *objfile)
1981{
1982 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1983 gdb_assert (dwarf2_per_objfile);
1984}
1985
1986/* A helper for the "quick" functions which attempts to read the line
1987 table for THIS_CU. */
1988static void
1989dw2_require_line_header (struct objfile *objfile,
1990 struct dwarf2_per_cu_data *this_cu)
1991{
1992 bfd *abfd = objfile->obfd;
1993 struct line_header *lh = NULL;
1994 struct attribute *attr;
1995 struct cleanup *cleanups;
1996 struct die_info *comp_unit_die;
1997 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1998 int has_children, i;
1999 struct dwarf2_cu cu;
2000 unsigned int bytes_read, buffer_size;
2001 struct die_reader_specs reader_specs;
2002 char *name, *comp_dir;
2003
2004 if (this_cu->v.quick->read_lines)
2005 return;
2006 this_cu->v.quick->read_lines = 1;
2007
2008 memset (&cu, 0, sizeof (cu));
2009 cu.objfile = objfile;
2010 obstack_init (&cu.comp_unit_obstack);
2011
2012 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2013
2014 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2015 buffer_size = dwarf2_per_objfile->info.size;
2016 buffer = dwarf2_per_objfile->info.buffer;
2017 info_ptr = buffer + this_cu->offset;
2018 beg_of_comp_unit = info_ptr;
2019
2020 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2021 buffer, buffer_size,
2022 abfd);
2023
2024 /* Complete the cu_header. */
2025 cu.header.offset = beg_of_comp_unit - buffer;
2026 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2027
2028 this_cu->cu = &cu;
2029 cu.per_cu = this_cu;
2030
2031 dwarf2_read_abbrevs (abfd, &cu);
2032 make_cleanup (dwarf2_free_abbrev_table, &cu);
2033
2034 if (this_cu->from_debug_types)
2035 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2036 init_cu_die_reader (&reader_specs, &cu);
2037 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2038 &has_children);
2039
2040 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2041 if (attr)
2042 {
2043 unsigned int line_offset = DW_UNSND (attr);
2044 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2045 }
2046 if (lh == NULL)
2047 {
2048 do_cleanups (cleanups);
2049 return;
2050 }
2051
2052 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2053
2054 this_cu->v.quick->lines = lh;
2055
2056 this_cu->v.quick->file_names
2057 = obstack_alloc (&objfile->objfile_obstack,
2058 lh->num_file_names * sizeof (char *));
2059 for (i = 0; i < lh->num_file_names; ++i)
2060 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2061
2062 do_cleanups (cleanups);
2063}
2064
2065/* A helper for the "quick" functions which computes and caches the
2066 real path for a given file name from the line table.
2067 dw2_require_line_header must have been called before this is
2068 invoked. */
2069static const char *
2070dw2_require_full_path (struct objfile *objfile,
2071 struct dwarf2_per_cu_data *cu,
2072 int index)
2073{
2074 if (!cu->v.quick->full_names)
2075 cu->v.quick->full_names
2076 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2077 cu->v.quick->lines->num_file_names,
2078 sizeof (char *));
2079
2080 if (!cu->v.quick->full_names[index])
2081 cu->v.quick->full_names[index]
2082 = gdb_realpath (cu->v.quick->file_names[index]);
2083
2084 return cu->v.quick->full_names[index];
2085}
2086
2087static struct symtab *
2088dw2_find_last_source_symtab (struct objfile *objfile)
2089{
2090 int index;
2091 dw2_setup (objfile);
2092 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2093 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2094}
2095
2096static void
2097dw2_forget_cached_source_info (struct objfile *objfile)
2098{
2099 int i;
2100
2101 dw2_setup (objfile);
1fd400ff
TT
2102 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2103 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2104 {
1fd400ff 2105 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2106
2107 if (cu->v.quick->full_names)
2108 {
2109 int j;
2110
2111 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2112 xfree ((void *) cu->v.quick->full_names[j]);
2113 }
2114 }
2115}
2116
2117static int
2118dw2_lookup_symtab (struct objfile *objfile, const char *name,
2119 const char *full_path, const char *real_path,
2120 struct symtab **result)
2121{
2122 int i;
2123 int check_basename = lbasename (name) == name;
2124 struct dwarf2_per_cu_data *base_cu = NULL;
2125
2126 dw2_setup (objfile);
1fd400ff
TT
2127 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2128 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2129 {
2130 int j;
1fd400ff 2131 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2132
2133 if (cu->v.quick->symtab)
2134 continue;
2135
2136 dw2_require_line_header (objfile, cu);
2137 if (!cu->v.quick->lines)
2138 continue;
2139
2140 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2141 {
2142 const char *this_name = cu->v.quick->file_names[j];
2143
2144 if (FILENAME_CMP (name, this_name) == 0)
2145 {
2146 *result = dw2_instantiate_symtab (objfile, cu);
2147 return 1;
2148 }
2149
2150 if (check_basename && ! base_cu
2151 && FILENAME_CMP (lbasename (this_name), name) == 0)
2152 base_cu = cu;
2153
2154 if (full_path != NULL)
2155 {
2156 const char *this_full_name = dw2_require_full_path (objfile,
2157 cu, j);
2158
2159 if (this_full_name
2160 && FILENAME_CMP (full_path, this_full_name) == 0)
2161 {
2162 *result = dw2_instantiate_symtab (objfile, cu);
2163 return 1;
2164 }
2165 }
2166
2167 if (real_path != NULL)
2168 {
2169 const char *this_full_name = dw2_require_full_path (objfile,
2170 cu, j);
2171
2172 if (this_full_name != NULL)
2173 {
2174 char *rp = gdb_realpath (this_full_name);
2175 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2176 {
2177 xfree (rp);
2178 *result = dw2_instantiate_symtab (objfile, cu);
2179 return 1;
2180 }
2181 xfree (rp);
2182 }
2183 }
2184 }
2185 }
2186
2187 if (base_cu)
2188 {
2189 *result = dw2_instantiate_symtab (objfile, base_cu);
2190 return 1;
2191 }
2192
2193 return 0;
2194}
2195
2196static struct symtab *
2197dw2_lookup_symbol (struct objfile *objfile, int block_index,
2198 const char *name, domain_enum domain)
2199{
2200 /* We do all the work in the pre_expand_symtabs_matching hook
2201 instead. */
2202 return NULL;
2203}
2204
2205/* A helper function that expands all symtabs that hold an object
2206 named NAME. */
2207static void
2208dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2209{
2210 dw2_setup (objfile);
2211
2212 if (dwarf2_per_objfile->index_table)
2213 {
2214 offset_type *vec;
2215
2216 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2217 name, &vec))
2218 {
2219 offset_type i, len = MAYBE_SWAP (*vec);
2220 for (i = 0; i < len; ++i)
2221 {
2222 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
1fd400ff
TT
2223 struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2224
9291a0cd
TT
2225 dw2_instantiate_symtab (objfile, cu);
2226 }
2227 }
2228 }
2229}
2230
2231static void
2232dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2233 int kind, const char *name,
2234 domain_enum domain)
2235{
2236 dw2_do_expand_symtabs_matching (objfile, name);
2237}
2238
2239static void
2240dw2_print_stats (struct objfile *objfile)
2241{
2242 int i, count;
2243
2244 dw2_setup (objfile);
2245 count = 0;
1fd400ff
TT
2246 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2247 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2248 {
1fd400ff 2249 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2250
2251 if (!cu->v.quick->symtab)
2252 ++count;
2253 }
2254 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2255}
2256
2257static void
2258dw2_dump (struct objfile *objfile)
2259{
2260 /* Nothing worth printing. */
2261}
2262
2263static void
2264dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2265 struct section_offsets *delta)
2266{
2267 /* There's nothing to relocate here. */
2268}
2269
2270static void
2271dw2_expand_symtabs_for_function (struct objfile *objfile,
2272 const char *func_name)
2273{
2274 dw2_do_expand_symtabs_matching (objfile, func_name);
2275}
2276
2277static void
2278dw2_expand_all_symtabs (struct objfile *objfile)
2279{
2280 int i;
2281
2282 dw2_setup (objfile);
1fd400ff
TT
2283
2284 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2285 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2286 {
1fd400ff 2287 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2288
2289 dw2_instantiate_symtab (objfile, cu);
2290 }
2291}
2292
2293static void
2294dw2_expand_symtabs_with_filename (struct objfile *objfile,
2295 const char *filename)
2296{
2297 int i;
2298
2299 dw2_setup (objfile);
1fd400ff
TT
2300 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2301 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2302 {
2303 int j;
1fd400ff 2304 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2305
2306 if (cu->v.quick->symtab)
2307 continue;
2308
2309 dw2_require_line_header (objfile, cu);
2310 if (!cu->v.quick->lines)
2311 continue;
2312
2313 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2314 {
2315 const char *this_name = cu->v.quick->file_names[j];
2316 if (strcmp (this_name, filename) == 0)
2317 {
2318 dw2_instantiate_symtab (objfile, cu);
2319 break;
2320 }
2321 }
2322 }
2323}
2324
dd786858 2325static const char *
9291a0cd
TT
2326dw2_find_symbol_file (struct objfile *objfile, const char *name)
2327{
2328 struct dwarf2_per_cu_data *cu;
2329 offset_type *vec;
2330
2331 dw2_setup (objfile);
2332
2333 if (!dwarf2_per_objfile->index_table)
2334 return NULL;
2335
2336 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2337 name, &vec))
2338 return NULL;
2339
2340 /* Note that this just looks at the very first one named NAME -- but
2341 actually we are looking for a function. find_main_filename
2342 should be rewritten so that it doesn't require a custom hook. It
2343 could just use the ordinary symbol tables. */
2344 /* vec[0] is the length, which must always be >0. */
1fd400ff 2345 cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd
TT
2346
2347 dw2_require_line_header (objfile, cu);
2348 if (!cu->v.quick->lines)
2349 return NULL;
2350
dd786858 2351 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2352}
2353
2354static void
2355dw2_map_ada_symtabs (struct objfile *objfile,
2356 int (*wild_match) (const char *, int, const char *),
2357 int (*is_name_suffix) (const char *),
2358 void (*callback) (struct objfile *,
2359 struct symtab *, void *),
2360 const char *name, int global,
2361 domain_enum namespace, int wild,
2362 void *data)
2363{
2364 /* For now, we don't support Ada, so this function can't be
2365 reached. */
2366 internal_error (__FILE__, __LINE__,
2367 _("map_ada_symtabs called via index method"));
2368}
2369
2370static void
2371dw2_expand_symtabs_matching (struct objfile *objfile,
2372 int (*file_matcher) (const char *, void *),
2373 int (*name_matcher) (const char *, void *),
2374 domain_enum kind,
2375 void *data)
2376{
2377 int i;
2378 offset_type iter;
2379
2380 dw2_setup (objfile);
2381 if (!dwarf2_per_objfile->index_table)
2382 return;
2383
1fd400ff
TT
2384 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2385 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2386 {
2387 int j;
1fd400ff 2388 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2389
2390 cu->v.quick->mark = 0;
2391 if (cu->v.quick->symtab)
2392 continue;
2393
2394 dw2_require_line_header (objfile, cu);
2395 if (!cu->v.quick->lines)
2396 continue;
2397
2398 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2399 {
2400 if (file_matcher (cu->v.quick->file_names[j], data))
2401 {
2402 cu->v.quick->mark = 1;
2403 break;
2404 }
2405 }
2406 }
2407
2408 for (iter = 0;
2409 iter < dwarf2_per_objfile->index_table->index_table_slots;
2410 ++iter)
2411 {
2412 offset_type idx = 2 * iter;
2413 const char *name;
2414 offset_type *vec, vec_len, vec_idx;
2415
2416 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2417 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2418 continue;
2419
2420 name = (dwarf2_per_objfile->index_table->constant_pool
2421 + dwarf2_per_objfile->index_table->index_table[idx]);
2422
2423 if (! (*name_matcher) (name, data))
2424 continue;
2425
2426 /* The name was matched, now expand corresponding CUs that were
2427 marked. */
2428 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2429 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2430 vec_len = MAYBE_SWAP (vec[0]);
2431 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2432 {
1fd400ff
TT
2433 struct dwarf2_per_cu_data *cu;
2434
2435 cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
9291a0cd
TT
2436 if (cu->v.quick->mark)
2437 dw2_instantiate_symtab (objfile, cu);
2438 }
2439 }
2440}
2441
2442static struct symtab *
2443dw2_find_pc_sect_symtab (struct objfile *objfile,
2444 struct minimal_symbol *msymbol,
2445 CORE_ADDR pc,
2446 struct obj_section *section,
2447 int warn_if_readin)
2448{
2449 struct dwarf2_per_cu_data *data;
2450
2451 dw2_setup (objfile);
2452
2453 if (!objfile->psymtabs_addrmap)
2454 return NULL;
2455
2456 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2457 if (!data)
2458 return NULL;
2459
2460 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2461 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2462 paddress (get_objfile_arch (objfile), pc));
2463
2464 return dw2_instantiate_symtab (objfile, data);
2465}
2466
2467static void
2468dw2_map_symbol_names (struct objfile *objfile,
2469 void (*fun) (const char *, void *),
2470 void *data)
2471{
2472 offset_type iter;
2473 dw2_setup (objfile);
2474
2475 if (!dwarf2_per_objfile->index_table)
2476 return;
2477
2478 for (iter = 0;
2479 iter < dwarf2_per_objfile->index_table->index_table_slots;
2480 ++iter)
2481 {
2482 offset_type idx = 2 * iter;
2483 const char *name;
2484 offset_type *vec, vec_len, vec_idx;
2485
2486 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2487 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2488 continue;
2489
2490 name = (dwarf2_per_objfile->index_table->constant_pool
2491 + dwarf2_per_objfile->index_table->index_table[idx]);
2492
2493 (*fun) (name, data);
2494 }
2495}
2496
2497static void
2498dw2_map_symbol_filenames (struct objfile *objfile,
2499 void (*fun) (const char *, const char *, void *),
2500 void *data)
2501{
2502 int i;
2503
2504 dw2_setup (objfile);
1fd400ff
TT
2505 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2506 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2507 {
2508 int j;
1fd400ff 2509 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2510
2511 if (cu->v.quick->symtab)
2512 continue;
2513
2514 dw2_require_line_header (objfile, cu);
2515 if (!cu->v.quick->lines)
2516 continue;
2517
2518 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2519 {
2520 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2521 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2522 }
2523 }
2524}
2525
2526static int
2527dw2_has_symbols (struct objfile *objfile)
2528{
2529 return 1;
2530}
2531
2532const struct quick_symbol_functions dwarf2_gdb_index_functions =
2533{
2534 dw2_has_symbols,
2535 dw2_find_last_source_symtab,
2536 dw2_forget_cached_source_info,
2537 dw2_lookup_symtab,
2538 dw2_lookup_symbol,
2539 dw2_pre_expand_symtabs_matching,
2540 dw2_print_stats,
2541 dw2_dump,
2542 dw2_relocate,
2543 dw2_expand_symtabs_for_function,
2544 dw2_expand_all_symtabs,
2545 dw2_expand_symtabs_with_filename,
2546 dw2_find_symbol_file,
2547 dw2_map_ada_symtabs,
2548 dw2_expand_symtabs_matching,
2549 dw2_find_pc_sect_symtab,
2550 dw2_map_symbol_names,
2551 dw2_map_symbol_filenames
2552};
2553
2554/* Initialize for reading DWARF for this objfile. Return 0 if this
2555 file will use psymtabs, or 1 if using the GNU index. */
2556
2557int
2558dwarf2_initialize_objfile (struct objfile *objfile)
2559{
2560 /* If we're about to read full symbols, don't bother with the
2561 indices. In this case we also don't care if some other debug
2562 format is making psymtabs, because they are all about to be
2563 expanded anyway. */
2564 if ((objfile->flags & OBJF_READNOW))
2565 {
2566 int i;
2567
2568 dwarf2_per_objfile->using_index = 1;
2569 create_all_comp_units (objfile);
1fd400ff 2570 create_debug_types_hash_table (objfile);
9291a0cd 2571
1fd400ff
TT
2572 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2573 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2574 {
1fd400ff 2575 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2576
2577 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2578 struct dwarf2_per_cu_quick_data);
2579 }
2580
2581 /* Return 1 so that gdb sees the "quick" functions. However,
2582 these functions will be no-ops because we will have expanded
2583 all symtabs. */
2584 return 1;
2585 }
2586
2587 if (dwarf2_read_index (objfile))
2588 return 1;
2589
2590 dwarf2_build_psymtabs (objfile);
2591 return 0;
2592}
2593
2594\f
2595
dce234bc
PP
2596/* Build a partial symbol table. */
2597
2598void
f29dff0a 2599dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2600{
f29dff0a 2601 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2602 {
2603 init_psymbol_list (objfile, 1024);
2604 }
2605
d146bf1e 2606 dwarf2_build_psymtabs_hard (objfile);
c906108c 2607}
c906108c 2608
45452591
DE
2609/* Return TRUE if OFFSET is within CU_HEADER. */
2610
2611static inline int
2612offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2613{
2614 unsigned int bottom = cu_header->offset;
2615 unsigned int top = (cu_header->offset
2616 + cu_header->length
2617 + cu_header->initial_length_size);
9a619af0 2618
45452591
DE
2619 return (offset >= bottom && offset < top);
2620}
2621
93311388
DE
2622/* Read in the comp unit header information from the debug_info at info_ptr.
2623 NOTE: This leaves members offset, first_die_offset to be filled in
2624 by the caller. */
107d2387 2625
fe1b8b76 2626static gdb_byte *
107d2387 2627read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2628 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2629{
2630 int signed_addr;
891d2f0b 2631 unsigned int bytes_read;
c764a876
DE
2632
2633 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2634 cu_header->initial_length_size = bytes_read;
2635 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2636 info_ptr += bytes_read;
107d2387
AC
2637 cu_header->version = read_2_bytes (abfd, info_ptr);
2638 info_ptr += 2;
613e1657 2639 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2640 &bytes_read);
613e1657 2641 info_ptr += bytes_read;
107d2387
AC
2642 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2643 info_ptr += 1;
2644 signed_addr = bfd_get_sign_extend_vma (abfd);
2645 if (signed_addr < 0)
8e65ff28 2646 internal_error (__FILE__, __LINE__,
e2e0b3e5 2647 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2648 cu_header->signed_addr_p = signed_addr;
c764a876 2649
107d2387
AC
2650 return info_ptr;
2651}
2652
fe1b8b76
JB
2653static gdb_byte *
2654partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2655 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2656 bfd *abfd)
2657{
fe1b8b76 2658 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2659
2660 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2661
2dc7f7b3 2662 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2663 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2664 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2665 bfd_get_filename (abfd));
72bf9492 2666
dce234bc 2667 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2668 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2669 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2670 (long) header->abbrev_offset,
93311388 2671 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2672 bfd_get_filename (abfd));
2673
2674 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2675 > buffer + buffer_size)
8a3fe4f8
AC
2676 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2677 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2678 (long) header->length,
93311388 2679 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2680 bfd_get_filename (abfd));
2681
2682 return info_ptr;
2683}
2684
348e048f
DE
2685/* Read in the types comp unit header information from .debug_types entry at
2686 types_ptr. The result is a pointer to one past the end of the header. */
2687
2688static gdb_byte *
2689read_type_comp_unit_head (struct comp_unit_head *cu_header,
2690 ULONGEST *signature,
2691 gdb_byte *types_ptr, bfd *abfd)
2692{
348e048f
DE
2693 gdb_byte *initial_types_ptr = types_ptr;
2694
6e70227d 2695 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2696 &dwarf2_per_objfile->types);
348e048f
DE
2697 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2698
2699 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2700
2701 *signature = read_8_bytes (abfd, types_ptr);
2702 types_ptr += 8;
2703 types_ptr += cu_header->offset_size;
2704 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2705
2706 return types_ptr;
2707}
2708
aaa75496
JB
2709/* Allocate a new partial symtab for file named NAME and mark this new
2710 partial symtab as being an include of PST. */
2711
2712static void
2713dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2714 struct objfile *objfile)
2715{
2716 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2717
2718 subpst->section_offsets = pst->section_offsets;
2719 subpst->textlow = 0;
2720 subpst->texthigh = 0;
2721
2722 subpst->dependencies = (struct partial_symtab **)
2723 obstack_alloc (&objfile->objfile_obstack,
2724 sizeof (struct partial_symtab *));
2725 subpst->dependencies[0] = pst;
2726 subpst->number_of_dependencies = 1;
2727
2728 subpst->globals_offset = 0;
2729 subpst->n_global_syms = 0;
2730 subpst->statics_offset = 0;
2731 subpst->n_static_syms = 0;
2732 subpst->symtab = NULL;
2733 subpst->read_symtab = pst->read_symtab;
2734 subpst->readin = 0;
2735
2736 /* No private part is necessary for include psymtabs. This property
2737 can be used to differentiate between such include psymtabs and
10b3939b 2738 the regular ones. */
58a9656e 2739 subpst->read_symtab_private = NULL;
aaa75496
JB
2740}
2741
2742/* Read the Line Number Program data and extract the list of files
2743 included by the source file represented by PST. Build an include
d85a05f0 2744 partial symtab for each of these included files. */
aaa75496
JB
2745
2746static void
2747dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2748 struct die_info *die,
aaa75496
JB
2749 struct partial_symtab *pst)
2750{
2751 struct objfile *objfile = cu->objfile;
2752 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2753 struct line_header *lh = NULL;
2754 struct attribute *attr;
aaa75496 2755
d85a05f0
DJ
2756 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2757 if (attr)
2758 {
2759 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2760
d85a05f0
DJ
2761 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2762 }
aaa75496
JB
2763 if (lh == NULL)
2764 return; /* No linetable, so no includes. */
2765
2766 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2767
2768 free_line_header (lh);
2769}
2770
348e048f
DE
2771static hashval_t
2772hash_type_signature (const void *item)
2773{
2774 const struct signatured_type *type_sig = item;
9a619af0 2775
348e048f
DE
2776 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2777 return type_sig->signature;
2778}
2779
2780static int
2781eq_type_signature (const void *item_lhs, const void *item_rhs)
2782{
2783 const struct signatured_type *lhs = item_lhs;
2784 const struct signatured_type *rhs = item_rhs;
9a619af0 2785
348e048f
DE
2786 return lhs->signature == rhs->signature;
2787}
2788
1fd400ff
TT
2789/* Allocate a hash table for signatured types. */
2790
2791static htab_t
673bfd45 2792allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2793{
2794 return htab_create_alloc_ex (41,
2795 hash_type_signature,
2796 eq_type_signature,
2797 NULL,
2798 &objfile->objfile_obstack,
2799 hashtab_obstack_allocate,
2800 dummy_obstack_deallocate);
2801}
2802
2803/* A helper function to add a signatured type CU to a list. */
2804
2805static int
2806add_signatured_type_cu_to_list (void **slot, void *datum)
2807{
2808 struct signatured_type *sigt = *slot;
2809 struct dwarf2_per_cu_data ***datap = datum;
2810
2811 **datap = &sigt->per_cu;
2812 ++*datap;
2813
2814 return 1;
2815}
2816
348e048f
DE
2817/* Create the hash table of all entries in the .debug_types section.
2818 The result is zero if there is an error (e.g. missing .debug_types section),
2819 otherwise non-zero. */
2820
2821static int
2822create_debug_types_hash_table (struct objfile *objfile)
2823{
be391dca 2824 gdb_byte *info_ptr;
348e048f 2825 htab_t types_htab;
1fd400ff 2826 struct dwarf2_per_cu_data **iter;
348e048f 2827
be391dca
TT
2828 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2829 info_ptr = dwarf2_per_objfile->types.buffer;
2830
348e048f
DE
2831 if (info_ptr == NULL)
2832 {
2833 dwarf2_per_objfile->signatured_types = NULL;
2834 return 0;
2835 }
2836
673bfd45 2837 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2838
2839 if (dwarf2_die_debug)
2840 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2841
2842 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2843 {
2844 unsigned int offset;
2845 unsigned int offset_size;
2846 unsigned int type_offset;
2847 unsigned int length, initial_length_size;
2848 unsigned short version;
2849 ULONGEST signature;
2850 struct signatured_type *type_sig;
2851 void **slot;
2852 gdb_byte *ptr = info_ptr;
2853
2854 offset = ptr - dwarf2_per_objfile->types.buffer;
2855
2856 /* We need to read the type's signature in order to build the hash
2857 table, but we don't need to read anything else just yet. */
2858
2859 /* Sanity check to ensure entire cu is present. */
2860 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2861 if (ptr + length + initial_length_size
2862 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2863 {
2864 complaint (&symfile_complaints,
2865 _("debug type entry runs off end of `.debug_types' section, ignored"));
2866 break;
2867 }
2868
2869 offset_size = initial_length_size == 4 ? 4 : 8;
2870 ptr += initial_length_size;
2871 version = bfd_get_16 (objfile->obfd, ptr);
2872 ptr += 2;
2873 ptr += offset_size; /* abbrev offset */
2874 ptr += 1; /* address size */
2875 signature = bfd_get_64 (objfile->obfd, ptr);
2876 ptr += 8;
2877 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2878
2879 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2880 memset (type_sig, 0, sizeof (*type_sig));
2881 type_sig->signature = signature;
2882 type_sig->offset = offset;
2883 type_sig->type_offset = type_offset;
ca1f3406 2884 type_sig->per_cu.objfile = objfile;
1fd400ff 2885 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2886
2887 slot = htab_find_slot (types_htab, type_sig, INSERT);
2888 gdb_assert (slot != NULL);
2889 *slot = type_sig;
2890
2891 if (dwarf2_die_debug)
2892 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2893 offset, phex (signature, sizeof (signature)));
2894
2895 info_ptr = info_ptr + initial_length_size + length;
2896 }
2897
2898 dwarf2_per_objfile->signatured_types = types_htab;
2899
1fd400ff
TT
2900 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2901 dwarf2_per_objfile->type_comp_units
2902 = obstack_alloc (&objfile->objfile_obstack,
2903 dwarf2_per_objfile->n_type_comp_units
2904 * sizeof (struct dwarf2_per_cu_data *));
2905 iter = &dwarf2_per_objfile->type_comp_units[0];
2906 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2907 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2908 == dwarf2_per_objfile->n_type_comp_units);
2909
348e048f
DE
2910 return 1;
2911}
2912
2913/* Lookup a signature based type.
2914 Returns NULL if SIG is not present in the table. */
2915
2916static struct signatured_type *
2917lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2918{
2919 struct signatured_type find_entry, *entry;
2920
2921 if (dwarf2_per_objfile->signatured_types == NULL)
2922 {
2923 complaint (&symfile_complaints,
2924 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2925 return 0;
2926 }
2927
2928 find_entry.signature = sig;
2929 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2930 return entry;
2931}
2932
d85a05f0
DJ
2933/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2934
2935static void
2936init_cu_die_reader (struct die_reader_specs *reader,
2937 struct dwarf2_cu *cu)
2938{
2939 reader->abfd = cu->objfile->obfd;
2940 reader->cu = cu;
2941 if (cu->per_cu->from_debug_types)
be391dca
TT
2942 {
2943 gdb_assert (dwarf2_per_objfile->types.readin);
2944 reader->buffer = dwarf2_per_objfile->types.buffer;
2945 }
d85a05f0 2946 else
be391dca
TT
2947 {
2948 gdb_assert (dwarf2_per_objfile->info.readin);
2949 reader->buffer = dwarf2_per_objfile->info.buffer;
2950 }
d85a05f0
DJ
2951}
2952
2953/* Find the base address of the compilation unit for range lists and
2954 location lists. It will normally be specified by DW_AT_low_pc.
2955 In DWARF-3 draft 4, the base address could be overridden by
2956 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2957 compilation units with discontinuous ranges. */
2958
2959static void
2960dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2961{
2962 struct attribute *attr;
2963
2964 cu->base_known = 0;
2965 cu->base_address = 0;
2966
2967 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2968 if (attr)
2969 {
2970 cu->base_address = DW_ADDR (attr);
2971 cu->base_known = 1;
2972 }
2973 else
2974 {
2975 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2976 if (attr)
2977 {
2978 cu->base_address = DW_ADDR (attr);
2979 cu->base_known = 1;
2980 }
2981 }
2982}
2983
348e048f
DE
2984/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2985 to combine the common parts.
93311388 2986 Process a compilation unit for a psymtab.
348e048f
DE
2987 BUFFER is a pointer to the beginning of the dwarf section buffer,
2988 either .debug_info or debug_types.
93311388
DE
2989 INFO_PTR is a pointer to the start of the CU.
2990 Returns a pointer to the next CU. */
aaa75496 2991
93311388
DE
2992static gdb_byte *
2993process_psymtab_comp_unit (struct objfile *objfile,
2994 struct dwarf2_per_cu_data *this_cu,
2995 gdb_byte *buffer, gdb_byte *info_ptr,
2996 unsigned int buffer_size)
c906108c 2997{
c906108c 2998 bfd *abfd = objfile->obfd;
93311388 2999 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3000 struct die_info *comp_unit_die;
c906108c 3001 struct partial_symtab *pst;
5734ee8b 3002 CORE_ADDR baseaddr;
93311388
DE
3003 struct cleanup *back_to_inner;
3004 struct dwarf2_cu cu;
d85a05f0
DJ
3005 int has_children, has_pc_info;
3006 struct attribute *attr;
d85a05f0
DJ
3007 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3008 struct die_reader_specs reader_specs;
c906108c 3009
93311388
DE
3010 memset (&cu, 0, sizeof (cu));
3011 cu.objfile = objfile;
3012 obstack_init (&cu.comp_unit_obstack);
c906108c 3013
93311388 3014 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3015
93311388
DE
3016 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3017 buffer, buffer_size,
3018 abfd);
10b3939b 3019
93311388
DE
3020 /* Complete the cu_header. */
3021 cu.header.offset = beg_of_comp_unit - buffer;
3022 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3023
93311388 3024 cu.list_in_scope = &file_symbols;
af703f96 3025
328c9494
DJ
3026 /* If this compilation unit was already read in, free the
3027 cached copy in order to read it in again. This is
3028 necessary because we skipped some symbols when we first
3029 read in the compilation unit (see load_partial_dies).
3030 This problem could be avoided, but the benefit is
3031 unclear. */
3032 if (this_cu->cu != NULL)
3033 free_one_cached_comp_unit (this_cu->cu);
3034
3035 /* Note that this is a pointer to our stack frame, being
3036 added to a global data structure. It will be cleaned up
3037 in free_stack_comp_unit when we finish with this
3038 compilation unit. */
3039 this_cu->cu = &cu;
d85a05f0
DJ
3040 cu.per_cu = this_cu;
3041
93311388
DE
3042 /* Read the abbrevs for this compilation unit into a table. */
3043 dwarf2_read_abbrevs (abfd, &cu);
3044 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3045
93311388 3046 /* Read the compilation unit die. */
348e048f
DE
3047 if (this_cu->from_debug_types)
3048 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3049 init_cu_die_reader (&reader_specs, &cu);
3050 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3051 &has_children);
93311388 3052
348e048f
DE
3053 if (this_cu->from_debug_types)
3054 {
3055 /* offset,length haven't been set yet for type units. */
3056 this_cu->offset = cu.header.offset;
3057 this_cu->length = cu.header.length + cu.header.initial_length_size;
3058 }
d85a05f0 3059 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3060 {
93311388
DE
3061 info_ptr = (beg_of_comp_unit + cu.header.length
3062 + cu.header.initial_length_size);
3063 do_cleanups (back_to_inner);
3064 return info_ptr;
3065 }
72bf9492 3066
93311388 3067 /* Set the language we're debugging. */
d85a05f0
DJ
3068 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3069 if (attr)
3070 set_cu_language (DW_UNSND (attr), &cu);
3071 else
3072 set_cu_language (language_minimal, &cu);
c906108c 3073
93311388 3074 /* Allocate a new partial symbol table structure. */
d85a05f0 3075 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3076 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3077 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3078 /* TEXTLOW and TEXTHIGH are set below. */
3079 0,
3080 objfile->global_psymbols.next,
3081 objfile->static_psymbols.next);
72bf9492 3082
d85a05f0
DJ
3083 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3084 if (attr != NULL)
3085 pst->dirname = DW_STRING (attr);
72bf9492 3086
e38df1d0 3087 pst->read_symtab_private = this_cu;
72bf9492 3088
93311388 3089 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3090
93311388
DE
3091 /* Store the function that reads in the rest of the symbol table */
3092 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3093
9291a0cd 3094 this_cu->v.psymtab = pst;
c906108c 3095
d85a05f0
DJ
3096 dwarf2_find_base_address (comp_unit_die, &cu);
3097
93311388
DE
3098 /* Possibly set the default values of LOWPC and HIGHPC from
3099 `DW_AT_ranges'. */
d85a05f0
DJ
3100 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3101 &best_highpc, &cu, pst);
3102 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3103 /* Store the contiguous range if it is not empty; it can be empty for
3104 CUs with no code. */
3105 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3106 best_lowpc + baseaddr,
3107 best_highpc + baseaddr - 1, pst);
93311388
DE
3108
3109 /* Check if comp unit has_children.
3110 If so, read the rest of the partial symbols from this comp unit.
3111 If not, there's no more debug_info for this comp unit. */
d85a05f0 3112 if (has_children)
93311388
DE
3113 {
3114 struct partial_die_info *first_die;
3115 CORE_ADDR lowpc, highpc;
31ffec48 3116
93311388
DE
3117 lowpc = ((CORE_ADDR) -1);
3118 highpc = ((CORE_ADDR) 0);
c906108c 3119
93311388 3120 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3121
93311388 3122 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3123 ! has_pc_info, &cu);
57c22c6c 3124
93311388
DE
3125 /* If we didn't find a lowpc, set it to highpc to avoid
3126 complaints from `maint check'. */
3127 if (lowpc == ((CORE_ADDR) -1))
3128 lowpc = highpc;
10b3939b 3129
93311388
DE
3130 /* If the compilation unit didn't have an explicit address range,
3131 then use the information extracted from its child dies. */
d85a05f0 3132 if (! has_pc_info)
93311388 3133 {
d85a05f0
DJ
3134 best_lowpc = lowpc;
3135 best_highpc = highpc;
93311388
DE
3136 }
3137 }
d85a05f0
DJ
3138 pst->textlow = best_lowpc + baseaddr;
3139 pst->texthigh = best_highpc + baseaddr;
c906108c 3140
93311388
DE
3141 pst->n_global_syms = objfile->global_psymbols.next -
3142 (objfile->global_psymbols.list + pst->globals_offset);
3143 pst->n_static_syms = objfile->static_psymbols.next -
3144 (objfile->static_psymbols.list + pst->statics_offset);
3145 sort_pst_symbols (pst);
c906108c 3146
93311388
DE
3147 info_ptr = (beg_of_comp_unit + cu.header.length
3148 + cu.header.initial_length_size);
ae038cb0 3149
348e048f
DE
3150 if (this_cu->from_debug_types)
3151 {
3152 /* It's not clear we want to do anything with stmt lists here.
3153 Waiting to see what gcc ultimately does. */
3154 }
d85a05f0 3155 else
93311388
DE
3156 {
3157 /* Get the list of files included in the current compilation unit,
3158 and build a psymtab for each of them. */
d85a05f0 3159 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3160 }
ae038cb0 3161
93311388 3162 do_cleanups (back_to_inner);
ae038cb0 3163
93311388
DE
3164 return info_ptr;
3165}
ff013f42 3166
348e048f
DE
3167/* Traversal function for htab_traverse_noresize.
3168 Process one .debug_types comp-unit. */
3169
3170static int
3171process_type_comp_unit (void **slot, void *info)
3172{
3173 struct signatured_type *entry = (struct signatured_type *) *slot;
3174 struct objfile *objfile = (struct objfile *) info;
3175 struct dwarf2_per_cu_data *this_cu;
3176
3177 this_cu = &entry->per_cu;
348e048f 3178
be391dca 3179 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3180 process_psymtab_comp_unit (objfile, this_cu,
3181 dwarf2_per_objfile->types.buffer,
3182 dwarf2_per_objfile->types.buffer + entry->offset,
3183 dwarf2_per_objfile->types.size);
3184
3185 return 1;
3186}
3187
3188/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3189 Build partial symbol tables for the .debug_types comp-units. */
3190
3191static void
3192build_type_psymtabs (struct objfile *objfile)
3193{
3194 if (! create_debug_types_hash_table (objfile))
3195 return;
3196
3197 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3198 process_type_comp_unit, objfile);
3199}
3200
60606b2c
TT
3201/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3202
3203static void
3204psymtabs_addrmap_cleanup (void *o)
3205{
3206 struct objfile *objfile = o;
ec61707d 3207
60606b2c
TT
3208 objfile->psymtabs_addrmap = NULL;
3209}
3210
93311388
DE
3211/* Build the partial symbol table by doing a quick pass through the
3212 .debug_info and .debug_abbrev sections. */
72bf9492 3213
93311388 3214static void
c67a9c90 3215dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3216{
93311388 3217 gdb_byte *info_ptr;
60606b2c
TT
3218 struct cleanup *back_to, *addrmap_cleanup;
3219 struct obstack temp_obstack;
93311388 3220
98bfdba5
PA
3221 dwarf2_per_objfile->reading_partial_symbols = 1;
3222
be391dca 3223 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3224 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3225
93311388
DE
3226 /* Any cached compilation units will be linked by the per-objfile
3227 read_in_chain. Make sure to free them when we're done. */
3228 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3229
348e048f
DE
3230 build_type_psymtabs (objfile);
3231
93311388 3232 create_all_comp_units (objfile);
c906108c 3233
60606b2c
TT
3234 /* Create a temporary address map on a temporary obstack. We later
3235 copy this to the final obstack. */
3236 obstack_init (&temp_obstack);
3237 make_cleanup_obstack_free (&temp_obstack);
3238 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3239 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3240
93311388
DE
3241 /* Since the objects we're extracting from .debug_info vary in
3242 length, only the individual functions to extract them (like
3243 read_comp_unit_head and load_partial_die) can really know whether
3244 the buffer is large enough to hold another complete object.
c906108c 3245
93311388
DE
3246 At the moment, they don't actually check that. If .debug_info
3247 holds just one extra byte after the last compilation unit's dies,
3248 then read_comp_unit_head will happily read off the end of the
3249 buffer. read_partial_die is similarly casual. Those functions
3250 should be fixed.
c906108c 3251
93311388
DE
3252 For this loop condition, simply checking whether there's any data
3253 left at all should be sufficient. */
c906108c 3254
93311388
DE
3255 while (info_ptr < (dwarf2_per_objfile->info.buffer
3256 + dwarf2_per_objfile->info.size))
3257 {
3258 struct dwarf2_per_cu_data *this_cu;
dd373385 3259
93311388
DE
3260 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3261 objfile);
aaa75496 3262
93311388
DE
3263 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3264 dwarf2_per_objfile->info.buffer,
3265 info_ptr,
3266 dwarf2_per_objfile->info.size);
c906108c 3267 }
ff013f42
JK
3268
3269 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3270 &objfile->objfile_obstack);
60606b2c 3271 discard_cleanups (addrmap_cleanup);
ff013f42 3272
ae038cb0
DJ
3273 do_cleanups (back_to);
3274}
3275
93311388 3276/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3277
3278static void
93311388
DE
3279load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3280 struct objfile *objfile)
ae038cb0
DJ
3281{
3282 bfd *abfd = objfile->obfd;
fe1b8b76 3283 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3284 struct die_info *comp_unit_die;
ae038cb0 3285 struct dwarf2_cu *cu;
1d9ec526 3286 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3287 struct attribute *attr;
3288 int has_children;
3289 struct die_reader_specs reader_specs;
98bfdba5 3290 int read_cu = 0;
ae038cb0 3291
348e048f
DE
3292 gdb_assert (! this_cu->from_debug_types);
3293
be391dca 3294 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3295 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3296 beg_of_comp_unit = info_ptr;
3297
98bfdba5
PA
3298 if (this_cu->cu == NULL)
3299 {
3300 cu = alloc_one_comp_unit (objfile);
ae038cb0 3301
98bfdba5 3302 read_cu = 1;
ae038cb0 3303
98bfdba5
PA
3304 /* If an error occurs while loading, release our storage. */
3305 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3306
98bfdba5
PA
3307 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3308 dwarf2_per_objfile->info.buffer,
3309 dwarf2_per_objfile->info.size,
3310 abfd);
ae038cb0 3311
98bfdba5
PA
3312 /* Complete the cu_header. */
3313 cu->header.offset = this_cu->offset;
3314 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3315
3316 /* Link this compilation unit into the compilation unit tree. */
3317 this_cu->cu = cu;
3318 cu->per_cu = this_cu;
98bfdba5
PA
3319
3320 /* Link this CU into read_in_chain. */
3321 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3322 dwarf2_per_objfile->read_in_chain = this_cu;
3323 }
3324 else
3325 {
3326 cu = this_cu->cu;
3327 info_ptr += cu->header.first_die_offset;
3328 }
ae038cb0
DJ
3329
3330 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3331 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3332 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3333 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3334
3335 /* Read the compilation unit die. */
d85a05f0
DJ
3336 init_cu_die_reader (&reader_specs, cu);
3337 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3338 &has_children);
ae038cb0
DJ
3339
3340 /* Set the language we're debugging. */
d85a05f0
DJ
3341 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3342 if (attr)
3343 set_cu_language (DW_UNSND (attr), cu);
3344 else
3345 set_cu_language (language_minimal, cu);
ae038cb0 3346
ae038cb0
DJ
3347 /* Check if comp unit has_children.
3348 If so, read the rest of the partial symbols from this comp unit.
3349 If not, there's no more debug_info for this comp unit. */
d85a05f0 3350 if (has_children)
93311388 3351 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3352
98bfdba5
PA
3353 do_cleanups (free_abbrevs_cleanup);
3354
3355 if (read_cu)
3356 {
3357 /* We've successfully allocated this compilation unit. Let our
3358 caller clean it up when finished with it. */
3359 discard_cleanups (free_cu_cleanup);
3360 }
ae038cb0
DJ
3361}
3362
3363/* Create a list of all compilation units in OBJFILE. We do this only
3364 if an inter-comp-unit reference is found; presumably if there is one,
3365 there will be many, and one will occur early in the .debug_info section.
3366 So there's no point in building this list incrementally. */
3367
3368static void
3369create_all_comp_units (struct objfile *objfile)
3370{
3371 int n_allocated;
3372 int n_comp_units;
3373 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3374 gdb_byte *info_ptr;
3375
3376 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3377 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3378
3379 n_comp_units = 0;
3380 n_allocated = 10;
3381 all_comp_units = xmalloc (n_allocated
3382 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3383
dce234bc 3384 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3385 {
c764a876 3386 unsigned int length, initial_length_size;
ae038cb0 3387 struct dwarf2_per_cu_data *this_cu;
c764a876 3388 unsigned int offset;
ae038cb0 3389
dce234bc 3390 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3391
3392 /* Read just enough information to find out where the next
3393 compilation unit is. */
c764a876
DE
3394 length = read_initial_length (objfile->obfd, info_ptr,
3395 &initial_length_size);
ae038cb0
DJ
3396
3397 /* Save the compilation unit for later lookup. */
3398 this_cu = obstack_alloc (&objfile->objfile_obstack,
3399 sizeof (struct dwarf2_per_cu_data));
3400 memset (this_cu, 0, sizeof (*this_cu));
3401 this_cu->offset = offset;
c764a876 3402 this_cu->length = length + initial_length_size;
9291a0cd 3403 this_cu->objfile = objfile;
ae038cb0
DJ
3404
3405 if (n_comp_units == n_allocated)
3406 {
3407 n_allocated *= 2;
3408 all_comp_units = xrealloc (all_comp_units,
3409 n_allocated
3410 * sizeof (struct dwarf2_per_cu_data *));
3411 }
3412 all_comp_units[n_comp_units++] = this_cu;
3413
3414 info_ptr = info_ptr + this_cu->length;
3415 }
3416
3417 dwarf2_per_objfile->all_comp_units
3418 = obstack_alloc (&objfile->objfile_obstack,
3419 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3420 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3421 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3422 xfree (all_comp_units);
3423 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3424}
3425
5734ee8b
DJ
3426/* Process all loaded DIEs for compilation unit CU, starting at
3427 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3428 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3429 DW_AT_ranges). If NEED_PC is set, then this function will set
3430 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3431 and record the covered ranges in the addrmap. */
c906108c 3432
72bf9492
DJ
3433static void
3434scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3435 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3436{
72bf9492 3437 struct partial_die_info *pdi;
c906108c 3438
91c24f0a
DC
3439 /* Now, march along the PDI's, descending into ones which have
3440 interesting children but skipping the children of the other ones,
3441 until we reach the end of the compilation unit. */
c906108c 3442
72bf9492 3443 pdi = first_die;
91c24f0a 3444
72bf9492
DJ
3445 while (pdi != NULL)
3446 {
3447 fixup_partial_die (pdi, cu);
c906108c 3448
f55ee35c 3449 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3450 children, so we need to look at them. Ditto for anonymous
3451 enums. */
933c6fe4 3452
72bf9492 3453 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3454 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3455 {
72bf9492 3456 switch (pdi->tag)
c906108c
SS
3457 {
3458 case DW_TAG_subprogram:
5734ee8b 3459 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3460 break;
3461 case DW_TAG_variable:
3462 case DW_TAG_typedef:
91c24f0a 3463 case DW_TAG_union_type:
72bf9492 3464 if (!pdi->is_declaration)
63d06c5c 3465 {
72bf9492 3466 add_partial_symbol (pdi, cu);
63d06c5c
DC
3467 }
3468 break;
c906108c 3469 case DW_TAG_class_type:
680b30c7 3470 case DW_TAG_interface_type:
c906108c 3471 case DW_TAG_structure_type:
72bf9492 3472 if (!pdi->is_declaration)
c906108c 3473 {
72bf9492 3474 add_partial_symbol (pdi, cu);
c906108c
SS
3475 }
3476 break;
91c24f0a 3477 case DW_TAG_enumeration_type:
72bf9492
DJ
3478 if (!pdi->is_declaration)
3479 add_partial_enumeration (pdi, cu);
c906108c
SS
3480 break;
3481 case DW_TAG_base_type:
a02abb62 3482 case DW_TAG_subrange_type:
c906108c 3483 /* File scope base type definitions are added to the partial
c5aa993b 3484 symbol table. */
72bf9492 3485 add_partial_symbol (pdi, cu);
c906108c 3486 break;
d9fa45fe 3487 case DW_TAG_namespace:
5734ee8b 3488 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3489 break;
5d7cb8df
JK
3490 case DW_TAG_module:
3491 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3492 break;
c906108c
SS
3493 default:
3494 break;
3495 }
3496 }
3497
72bf9492
DJ
3498 /* If the die has a sibling, skip to the sibling. */
3499
3500 pdi = pdi->die_sibling;
3501 }
3502}
3503
3504/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3505
72bf9492 3506 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3507 name is concatenated with "::" and the partial DIE's name. For
3508 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3509 Enumerators are an exception; they use the scope of their parent
3510 enumeration type, i.e. the name of the enumeration type is not
3511 prepended to the enumerator.
91c24f0a 3512
72bf9492
DJ
3513 There are two complexities. One is DW_AT_specification; in this
3514 case "parent" means the parent of the target of the specification,
3515 instead of the direct parent of the DIE. The other is compilers
3516 which do not emit DW_TAG_namespace; in this case we try to guess
3517 the fully qualified name of structure types from their members'
3518 linkage names. This must be done using the DIE's children rather
3519 than the children of any DW_AT_specification target. We only need
3520 to do this for structures at the top level, i.e. if the target of
3521 any DW_AT_specification (if any; otherwise the DIE itself) does not
3522 have a parent. */
3523
3524/* Compute the scope prefix associated with PDI's parent, in
3525 compilation unit CU. The result will be allocated on CU's
3526 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3527 field. NULL is returned if no prefix is necessary. */
3528static char *
3529partial_die_parent_scope (struct partial_die_info *pdi,
3530 struct dwarf2_cu *cu)
3531{
3532 char *grandparent_scope;
3533 struct partial_die_info *parent, *real_pdi;
91c24f0a 3534
72bf9492
DJ
3535 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3536 then this means the parent of the specification DIE. */
3537
3538 real_pdi = pdi;
72bf9492 3539 while (real_pdi->has_specification)
10b3939b 3540 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3541
3542 parent = real_pdi->die_parent;
3543 if (parent == NULL)
3544 return NULL;
3545
3546 if (parent->scope_set)
3547 return parent->scope;
3548
3549 fixup_partial_die (parent, cu);
3550
10b3939b 3551 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3552
acebe513
UW
3553 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3554 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3555 Work around this problem here. */
3556 if (cu->language == language_cplus
6e70227d 3557 && parent->tag == DW_TAG_namespace
acebe513
UW
3558 && strcmp (parent->name, "::") == 0
3559 && grandparent_scope == NULL)
3560 {
3561 parent->scope = NULL;
3562 parent->scope_set = 1;
3563 return NULL;
3564 }
3565
72bf9492 3566 if (parent->tag == DW_TAG_namespace
f55ee35c 3567 || parent->tag == DW_TAG_module
72bf9492
DJ
3568 || parent->tag == DW_TAG_structure_type
3569 || parent->tag == DW_TAG_class_type
680b30c7 3570 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3571 || parent->tag == DW_TAG_union_type
3572 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3573 {
3574 if (grandparent_scope == NULL)
3575 parent->scope = parent->name;
3576 else
987504bb 3577 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3578 parent->name, 0, cu);
72bf9492 3579 }
ceeb3d5a 3580 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3581 /* Enumerators should not get the name of the enumeration as a prefix. */
3582 parent->scope = grandparent_scope;
3583 else
3584 {
3585 /* FIXME drow/2004-04-01: What should we be doing with
3586 function-local names? For partial symbols, we should probably be
3587 ignoring them. */
3588 complaint (&symfile_complaints,
e2e0b3e5 3589 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3590 parent->tag, pdi->offset);
3591 parent->scope = grandparent_scope;
c906108c
SS
3592 }
3593
72bf9492
DJ
3594 parent->scope_set = 1;
3595 return parent->scope;
3596}
3597
3598/* Return the fully scoped name associated with PDI, from compilation unit
3599 CU. The result will be allocated with malloc. */
3600static char *
3601partial_die_full_name (struct partial_die_info *pdi,
3602 struct dwarf2_cu *cu)
3603{
3604 char *parent_scope;
3605
98bfdba5
PA
3606 /* If this is a template instantiation, we can not work out the
3607 template arguments from partial DIEs. So, unfortunately, we have
3608 to go through the full DIEs. At least any work we do building
3609 types here will be reused if full symbols are loaded later. */
3610 if (pdi->has_template_arguments)
3611 {
3612 fixup_partial_die (pdi, cu);
3613
3614 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3615 {
3616 struct die_info *die;
3617 struct attribute attr;
3618 struct dwarf2_cu *ref_cu = cu;
3619
3620 attr.name = 0;
3621 attr.form = DW_FORM_ref_addr;
3622 attr.u.addr = pdi->offset;
3623 die = follow_die_ref (NULL, &attr, &ref_cu);
3624
3625 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3626 }
3627 }
3628
72bf9492
DJ
3629 parent_scope = partial_die_parent_scope (pdi, cu);
3630 if (parent_scope == NULL)
3631 return NULL;
3632 else
f55ee35c 3633 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3634}
3635
3636static void
72bf9492 3637add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3638{
e7c27a73 3639 struct objfile *objfile = cu->objfile;
c906108c 3640 CORE_ADDR addr = 0;
decbce07 3641 char *actual_name = NULL;
5c4e30ca 3642 const struct partial_symbol *psym = NULL;
e142c38c 3643 CORE_ADDR baseaddr;
72bf9492 3644 int built_actual_name = 0;
e142c38c
DJ
3645
3646 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3647
94af9270
KS
3648 actual_name = partial_die_full_name (pdi, cu);
3649 if (actual_name)
3650 built_actual_name = 1;
63d06c5c 3651
72bf9492
DJ
3652 if (actual_name == NULL)
3653 actual_name = pdi->name;
3654
c906108c
SS
3655 switch (pdi->tag)
3656 {
3657 case DW_TAG_subprogram:
2cfa0c8d 3658 if (pdi->is_external || cu->language == language_ada)
c906108c 3659 {
2cfa0c8d
JB
3660 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3661 of the global scope. But in Ada, we want to be able to access
3662 nested procedures globally. So all Ada subprograms are stored
3663 in the global scope. */
38d518c9 3664 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3665 mst_text, objfile); */
38d518c9 3666 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3667 built_actual_name,
5c4e30ca
DC
3668 VAR_DOMAIN, LOC_BLOCK,
3669 &objfile->global_psymbols,
3670 0, pdi->lowpc + baseaddr,
e142c38c 3671 cu->language, objfile);
c906108c
SS
3672 }
3673 else
3674 {
38d518c9 3675 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3676 mst_file_text, objfile); */
38d518c9 3677 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3678 built_actual_name,
5c4e30ca
DC
3679 VAR_DOMAIN, LOC_BLOCK,
3680 &objfile->static_psymbols,
3681 0, pdi->lowpc + baseaddr,
e142c38c 3682 cu->language, objfile);
c906108c
SS
3683 }
3684 break;
3685 case DW_TAG_variable:
caac4577
JG
3686 if (pdi->locdesc)
3687 addr = decode_locdesc (pdi->locdesc, cu);
3688
3689 if (pdi->locdesc
3690 && addr == 0
3691 && !dwarf2_per_objfile->has_section_at_zero)
3692 {
3693 /* A global or static variable may also have been stripped
3694 out by the linker if unused, in which case its address
3695 will be nullified; do not add such variables into partial
3696 symbol table then. */
3697 }
3698 else if (pdi->is_external)
c906108c
SS
3699 {
3700 /* Global Variable.
3701 Don't enter into the minimal symbol tables as there is
3702 a minimal symbol table entry from the ELF symbols already.
3703 Enter into partial symbol table if it has a location
3704 descriptor or a type.
3705 If the location descriptor is missing, new_symbol will create
3706 a LOC_UNRESOLVED symbol, the address of the variable will then
3707 be determined from the minimal symbol table whenever the variable
3708 is referenced.
3709 The address for the partial symbol table entry is not
3710 used by GDB, but it comes in handy for debugging partial symbol
3711 table building. */
3712
c906108c 3713 if (pdi->locdesc || pdi->has_type)
38d518c9 3714 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3715 built_actual_name,
5c4e30ca
DC
3716 VAR_DOMAIN, LOC_STATIC,
3717 &objfile->global_psymbols,
3718 0, addr + baseaddr,
e142c38c 3719 cu->language, objfile);
c906108c
SS
3720 }
3721 else
3722 {
3723 /* Static Variable. Skip symbols without location descriptors. */
3724 if (pdi->locdesc == NULL)
decbce07
MS
3725 {
3726 if (built_actual_name)
3727 xfree (actual_name);
3728 return;
3729 }
38d518c9 3730 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3731 mst_file_data, objfile); */
38d518c9 3732 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3733 built_actual_name,
5c4e30ca
DC
3734 VAR_DOMAIN, LOC_STATIC,
3735 &objfile->static_psymbols,
3736 0, addr + baseaddr,
e142c38c 3737 cu->language, objfile);
c906108c
SS
3738 }
3739 break;
3740 case DW_TAG_typedef:
3741 case DW_TAG_base_type:
a02abb62 3742 case DW_TAG_subrange_type:
38d518c9 3743 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3744 built_actual_name,
176620f1 3745 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3746 &objfile->static_psymbols,
e142c38c 3747 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3748 break;
72bf9492
DJ
3749 case DW_TAG_namespace:
3750 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3751 built_actual_name,
72bf9492
DJ
3752 VAR_DOMAIN, LOC_TYPEDEF,
3753 &objfile->global_psymbols,
3754 0, (CORE_ADDR) 0, cu->language, objfile);
3755 break;
c906108c 3756 case DW_TAG_class_type:
680b30c7 3757 case DW_TAG_interface_type:
c906108c
SS
3758 case DW_TAG_structure_type:
3759 case DW_TAG_union_type:
3760 case DW_TAG_enumeration_type:
fa4028e9
JB
3761 /* Skip external references. The DWARF standard says in the section
3762 about "Structure, Union, and Class Type Entries": "An incomplete
3763 structure, union or class type is represented by a structure,
3764 union or class entry that does not have a byte size attribute
3765 and that has a DW_AT_declaration attribute." */
3766 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3767 {
3768 if (built_actual_name)
3769 xfree (actual_name);
3770 return;
3771 }
fa4028e9 3772
63d06c5c
DC
3773 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3774 static vs. global. */
38d518c9 3775 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3776 built_actual_name,
176620f1 3777 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3778 (cu->language == language_cplus
3779 || cu->language == language_java)
63d06c5c
DC
3780 ? &objfile->global_psymbols
3781 : &objfile->static_psymbols,
e142c38c 3782 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3783
c906108c
SS
3784 break;
3785 case DW_TAG_enumerator:
38d518c9 3786 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3787 built_actual_name,
176620f1 3788 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3789 (cu->language == language_cplus
3790 || cu->language == language_java)
f6fe98ef
DJ
3791 ? &objfile->global_psymbols
3792 : &objfile->static_psymbols,
e142c38c 3793 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3794 break;
3795 default:
3796 break;
3797 }
5c4e30ca 3798
72bf9492
DJ
3799 if (built_actual_name)
3800 xfree (actual_name);
c906108c
SS
3801}
3802
5c4e30ca
DC
3803/* Read a partial die corresponding to a namespace; also, add a symbol
3804 corresponding to that namespace to the symbol table. NAMESPACE is
3805 the name of the enclosing namespace. */
91c24f0a 3806
72bf9492
DJ
3807static void
3808add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3809 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3810 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3811{
72bf9492 3812 /* Add a symbol for the namespace. */
e7c27a73 3813
72bf9492 3814 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3815
3816 /* Now scan partial symbols in that namespace. */
3817
91c24f0a 3818 if (pdi->has_children)
5734ee8b 3819 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3820}
3821
5d7cb8df
JK
3822/* Read a partial die corresponding to a Fortran module. */
3823
3824static void
3825add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3826 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3827{
f55ee35c 3828 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3829
3830 if (pdi->has_children)
3831 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3832}
3833
bc30ff58
JB
3834/* Read a partial die corresponding to a subprogram and create a partial
3835 symbol for that subprogram. When the CU language allows it, this
3836 routine also defines a partial symbol for each nested subprogram
3837 that this subprogram contains.
6e70227d 3838
bc30ff58
JB
3839 DIE my also be a lexical block, in which case we simply search
3840 recursively for suprograms defined inside that lexical block.
3841 Again, this is only performed when the CU language allows this
3842 type of definitions. */
3843
3844static void
3845add_partial_subprogram (struct partial_die_info *pdi,
3846 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3847 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3848{
3849 if (pdi->tag == DW_TAG_subprogram)
3850 {
3851 if (pdi->has_pc_info)
3852 {
3853 if (pdi->lowpc < *lowpc)
3854 *lowpc = pdi->lowpc;
3855 if (pdi->highpc > *highpc)
3856 *highpc = pdi->highpc;
5734ee8b
DJ
3857 if (need_pc)
3858 {
3859 CORE_ADDR baseaddr;
3860 struct objfile *objfile = cu->objfile;
3861
3862 baseaddr = ANOFFSET (objfile->section_offsets,
3863 SECT_OFF_TEXT (objfile));
3864 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3865 pdi->lowpc + baseaddr,
3866 pdi->highpc - 1 + baseaddr,
9291a0cd 3867 cu->per_cu->v.psymtab);
5734ee8b 3868 }
bc30ff58 3869 if (!pdi->is_declaration)
e8d05480
JB
3870 /* Ignore subprogram DIEs that do not have a name, they are
3871 illegal. Do not emit a complaint at this point, we will
3872 do so when we convert this psymtab into a symtab. */
3873 if (pdi->name)
3874 add_partial_symbol (pdi, cu);
bc30ff58
JB
3875 }
3876 }
6e70227d 3877
bc30ff58
JB
3878 if (! pdi->has_children)
3879 return;
3880
3881 if (cu->language == language_ada)
3882 {
3883 pdi = pdi->die_child;
3884 while (pdi != NULL)
3885 {
3886 fixup_partial_die (pdi, cu);
3887 if (pdi->tag == DW_TAG_subprogram
3888 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3889 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3890 pdi = pdi->die_sibling;
3891 }
3892 }
3893}
3894
72bf9492
DJ
3895/* See if we can figure out if the class lives in a namespace. We do
3896 this by looking for a member function; its demangled name will
3897 contain namespace info, if there is any. */
63d06c5c 3898
72bf9492
DJ
3899static void
3900guess_structure_name (struct partial_die_info *struct_pdi,
3901 struct dwarf2_cu *cu)
63d06c5c 3902{
987504bb
JJ
3903 if ((cu->language == language_cplus
3904 || cu->language == language_java)
72bf9492 3905 && cu->has_namespace_info == 0
63d06c5c
DC
3906 && struct_pdi->has_children)
3907 {
63d06c5c
DC
3908 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3909 what template types look like, because the demangler
3910 frequently doesn't give the same name as the debug info. We
3911 could fix this by only using the demangled name to get the
134d01f1 3912 prefix (but see comment in read_structure_type). */
63d06c5c 3913
72bf9492 3914 struct partial_die_info *real_pdi;
5d51ca54 3915
72bf9492
DJ
3916 /* If this DIE (this DIE's specification, if any) has a parent, then
3917 we should not do this. We'll prepend the parent's fully qualified
3918 name when we create the partial symbol. */
5d51ca54 3919
72bf9492 3920 real_pdi = struct_pdi;
72bf9492 3921 while (real_pdi->has_specification)
10b3939b 3922 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3923
72bf9492
DJ
3924 if (real_pdi->die_parent != NULL)
3925 return;
63d06c5c 3926 }
63d06c5c
DC
3927}
3928
91c24f0a
DC
3929/* Read a partial die corresponding to an enumeration type. */
3930
72bf9492
DJ
3931static void
3932add_partial_enumeration (struct partial_die_info *enum_pdi,
3933 struct dwarf2_cu *cu)
91c24f0a 3934{
72bf9492 3935 struct partial_die_info *pdi;
91c24f0a
DC
3936
3937 if (enum_pdi->name != NULL)
72bf9492
DJ
3938 add_partial_symbol (enum_pdi, cu);
3939
3940 pdi = enum_pdi->die_child;
3941 while (pdi)
91c24f0a 3942 {
72bf9492 3943 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3944 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3945 else
72bf9492
DJ
3946 add_partial_symbol (pdi, cu);
3947 pdi = pdi->die_sibling;
91c24f0a 3948 }
91c24f0a
DC
3949}
3950
4bb7a0a7
DJ
3951/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3952 Return the corresponding abbrev, or NULL if the number is zero (indicating
3953 an empty DIE). In either case *BYTES_READ will be set to the length of
3954 the initial number. */
3955
3956static struct abbrev_info *
fe1b8b76 3957peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3958 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3959{
3960 bfd *abfd = cu->objfile->obfd;
3961 unsigned int abbrev_number;
3962 struct abbrev_info *abbrev;
3963
3964 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3965
3966 if (abbrev_number == 0)
3967 return NULL;
3968
3969 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3970 if (!abbrev)
3971 {
8a3fe4f8 3972 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3973 bfd_get_filename (abfd));
3974 }
3975
3976 return abbrev;
3977}
3978
93311388
DE
3979/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3980 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3981 DIE. Any children of the skipped DIEs will also be skipped. */
3982
fe1b8b76 3983static gdb_byte *
93311388 3984skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3985{
3986 struct abbrev_info *abbrev;
3987 unsigned int bytes_read;
3988
3989 while (1)
3990 {
3991 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3992 if (abbrev == NULL)
3993 return info_ptr + bytes_read;
3994 else
93311388 3995 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3996 }
3997}
3998
93311388
DE
3999/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4000 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4001 abbrev corresponding to that skipped uleb128 should be passed in
4002 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4003 children. */
4004
fe1b8b76 4005static gdb_byte *
93311388
DE
4006skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4007 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4008{
4009 unsigned int bytes_read;
4010 struct attribute attr;
4011 bfd *abfd = cu->objfile->obfd;
4012 unsigned int form, i;
4013
4014 for (i = 0; i < abbrev->num_attrs; i++)
4015 {
4016 /* The only abbrev we care about is DW_AT_sibling. */
4017 if (abbrev->attrs[i].name == DW_AT_sibling)
4018 {
4019 read_attribute (&attr, &abbrev->attrs[i],
4020 abfd, info_ptr, cu);
4021 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4022 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4023 else
93311388 4024 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4025 }
4026
4027 /* If it isn't DW_AT_sibling, skip this attribute. */
4028 form = abbrev->attrs[i].form;
4029 skip_attribute:
4030 switch (form)
4031 {
4bb7a0a7 4032 case DW_FORM_ref_addr:
ae411497
TT
4033 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4034 and later it is offset sized. */
4035 if (cu->header.version == 2)
4036 info_ptr += cu->header.addr_size;
4037 else
4038 info_ptr += cu->header.offset_size;
4039 break;
4040 case DW_FORM_addr:
4bb7a0a7
DJ
4041 info_ptr += cu->header.addr_size;
4042 break;
4043 case DW_FORM_data1:
4044 case DW_FORM_ref1:
4045 case DW_FORM_flag:
4046 info_ptr += 1;
4047 break;
2dc7f7b3
TT
4048 case DW_FORM_flag_present:
4049 break;
4bb7a0a7
DJ
4050 case DW_FORM_data2:
4051 case DW_FORM_ref2:
4052 info_ptr += 2;
4053 break;
4054 case DW_FORM_data4:
4055 case DW_FORM_ref4:
4056 info_ptr += 4;
4057 break;
4058 case DW_FORM_data8:
4059 case DW_FORM_ref8:
348e048f 4060 case DW_FORM_sig8:
4bb7a0a7
DJ
4061 info_ptr += 8;
4062 break;
4063 case DW_FORM_string:
9b1c24c8 4064 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4065 info_ptr += bytes_read;
4066 break;
2dc7f7b3 4067 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4068 case DW_FORM_strp:
4069 info_ptr += cu->header.offset_size;
4070 break;
2dc7f7b3 4071 case DW_FORM_exprloc:
4bb7a0a7
DJ
4072 case DW_FORM_block:
4073 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4074 info_ptr += bytes_read;
4075 break;
4076 case DW_FORM_block1:
4077 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4078 break;
4079 case DW_FORM_block2:
4080 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4081 break;
4082 case DW_FORM_block4:
4083 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4084 break;
4085 case DW_FORM_sdata:
4086 case DW_FORM_udata:
4087 case DW_FORM_ref_udata:
4088 info_ptr = skip_leb128 (abfd, info_ptr);
4089 break;
4090 case DW_FORM_indirect:
4091 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4092 info_ptr += bytes_read;
4093 /* We need to continue parsing from here, so just go back to
4094 the top. */
4095 goto skip_attribute;
4096
4097 default:
8a3fe4f8 4098 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4099 dwarf_form_name (form),
4100 bfd_get_filename (abfd));
4101 }
4102 }
4103
4104 if (abbrev->has_children)
93311388 4105 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4106 else
4107 return info_ptr;
4108}
4109
93311388
DE
4110/* Locate ORIG_PDI's sibling.
4111 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4112 in BUFFER. */
91c24f0a 4113
fe1b8b76 4114static gdb_byte *
93311388
DE
4115locate_pdi_sibling (struct partial_die_info *orig_pdi,
4116 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4117 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4118{
4119 /* Do we know the sibling already? */
72bf9492 4120
91c24f0a
DC
4121 if (orig_pdi->sibling)
4122 return orig_pdi->sibling;
4123
4124 /* Are there any children to deal with? */
4125
4126 if (!orig_pdi->has_children)
4127 return info_ptr;
4128
4bb7a0a7 4129 /* Skip the children the long way. */
91c24f0a 4130
93311388 4131 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4132}
4133
c906108c
SS
4134/* Expand this partial symbol table into a full symbol table. */
4135
4136static void
fba45db2 4137dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4138{
c906108c
SS
4139 if (pst != NULL)
4140 {
4141 if (pst->readin)
4142 {
8a3fe4f8 4143 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4144 }
4145 else
4146 {
4147 if (info_verbose)
4148 {
a3f17187 4149 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4150 gdb_flush (gdb_stdout);
4151 }
4152
10b3939b
DJ
4153 /* Restore our global data. */
4154 dwarf2_per_objfile = objfile_data (pst->objfile,
4155 dwarf2_objfile_data_key);
4156
b2ab525c
KB
4157 /* If this psymtab is constructed from a debug-only objfile, the
4158 has_section_at_zero flag will not necessarily be correct. We
4159 can get the correct value for this flag by looking at the data
4160 associated with the (presumably stripped) associated objfile. */
4161 if (pst->objfile->separate_debug_objfile_backlink)
4162 {
4163 struct dwarf2_per_objfile *dpo_backlink
4164 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4165 dwarf2_objfile_data_key);
9a619af0 4166
b2ab525c
KB
4167 dwarf2_per_objfile->has_section_at_zero
4168 = dpo_backlink->has_section_at_zero;
4169 }
4170
98bfdba5
PA
4171 dwarf2_per_objfile->reading_partial_symbols = 0;
4172
c906108c
SS
4173 psymtab_to_symtab_1 (pst);
4174
4175 /* Finish up the debug error message. */
4176 if (info_verbose)
a3f17187 4177 printf_filtered (_("done.\n"));
c906108c
SS
4178 }
4179 }
4180}
4181
10b3939b
DJ
4182/* Add PER_CU to the queue. */
4183
4184static void
03dd20cc 4185queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4186{
4187 struct dwarf2_queue_item *item;
4188
4189 per_cu->queued = 1;
4190 item = xmalloc (sizeof (*item));
4191 item->per_cu = per_cu;
4192 item->next = NULL;
4193
4194 if (dwarf2_queue == NULL)
4195 dwarf2_queue = item;
4196 else
4197 dwarf2_queue_tail->next = item;
4198
4199 dwarf2_queue_tail = item;
4200}
4201
4202/* Process the queue. */
4203
4204static void
4205process_queue (struct objfile *objfile)
4206{
4207 struct dwarf2_queue_item *item, *next_item;
4208
03dd20cc
DJ
4209 /* The queue starts out with one item, but following a DIE reference
4210 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4211 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4212 {
9291a0cd
TT
4213 if (dwarf2_per_objfile->using_index
4214 ? !item->per_cu->v.quick->symtab
4215 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4216 process_full_comp_unit (item->per_cu);
4217
4218 item->per_cu->queued = 0;
4219 next_item = item->next;
4220 xfree (item);
4221 }
4222
4223 dwarf2_queue_tail = NULL;
4224}
4225
4226/* Free all allocated queue entries. This function only releases anything if
4227 an error was thrown; if the queue was processed then it would have been
4228 freed as we went along. */
4229
4230static void
4231dwarf2_release_queue (void *dummy)
4232{
4233 struct dwarf2_queue_item *item, *last;
4234
4235 item = dwarf2_queue;
4236 while (item)
4237 {
4238 /* Anything still marked queued is likely to be in an
4239 inconsistent state, so discard it. */
4240 if (item->per_cu->queued)
4241 {
4242 if (item->per_cu->cu != NULL)
4243 free_one_cached_comp_unit (item->per_cu->cu);
4244 item->per_cu->queued = 0;
4245 }
4246
4247 last = item;
4248 item = item->next;
4249 xfree (last);
4250 }
4251
4252 dwarf2_queue = dwarf2_queue_tail = NULL;
4253}
4254
4255/* Read in full symbols for PST, and anything it depends on. */
4256
c906108c 4257static void
fba45db2 4258psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4259{
10b3939b 4260 struct dwarf2_per_cu_data *per_cu;
c906108c 4261 struct cleanup *back_to;
aaa75496
JB
4262 int i;
4263
4264 for (i = 0; i < pst->number_of_dependencies; i++)
4265 if (!pst->dependencies[i]->readin)
4266 {
4267 /* Inform about additional files that need to be read in. */
4268 if (info_verbose)
4269 {
a3f17187 4270 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4271 fputs_filtered (" ", gdb_stdout);
4272 wrap_here ("");
4273 fputs_filtered ("and ", gdb_stdout);
4274 wrap_here ("");
4275 printf_filtered ("%s...", pst->dependencies[i]->filename);
4276 wrap_here (""); /* Flush output */
4277 gdb_flush (gdb_stdout);
4278 }
4279 psymtab_to_symtab_1 (pst->dependencies[i]);
4280 }
4281
e38df1d0 4282 per_cu = pst->read_symtab_private;
10b3939b
DJ
4283
4284 if (per_cu == NULL)
aaa75496
JB
4285 {
4286 /* It's an include file, no symbols to read for it.
4287 Everything is in the parent symtab. */
4288 pst->readin = 1;
4289 return;
4290 }
c906108c 4291
9291a0cd 4292 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4293}
4294
93311388 4295/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4296
93311388 4297static void
31ffec48 4298load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4299{
31ffec48 4300 bfd *abfd = objfile->obfd;
10b3939b 4301 struct dwarf2_cu *cu;
c764a876 4302 unsigned int offset;
93311388 4303 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4304 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4305 struct attribute *attr;
98bfdba5 4306 int read_cu = 0;
6502dd73 4307
348e048f
DE
4308 gdb_assert (! per_cu->from_debug_types);
4309
c906108c 4310 /* Set local variables from the partial symbol table info. */
10b3939b 4311 offset = per_cu->offset;
6502dd73 4312
be391dca 4313 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4314 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4315 beg_of_comp_unit = info_ptr;
63d06c5c 4316
98bfdba5
PA
4317 if (per_cu->cu == NULL)
4318 {
4319 cu = alloc_one_comp_unit (objfile);
4320
4321 read_cu = 1;
c906108c 4322
98bfdba5
PA
4323 /* If an error occurs while loading, release our storage. */
4324 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4325
98bfdba5
PA
4326 /* Read in the comp_unit header. */
4327 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4328
98bfdba5
PA
4329 /* Complete the cu_header. */
4330 cu->header.offset = offset;
4331 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4332
98bfdba5
PA
4333 /* Read the abbrevs for this compilation unit. */
4334 dwarf2_read_abbrevs (abfd, cu);
4335 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4336
98bfdba5
PA
4337 /* Link this compilation unit into the compilation unit tree. */
4338 per_cu->cu = cu;
4339 cu->per_cu = per_cu;
98bfdba5
PA
4340
4341 /* Link this CU into read_in_chain. */
4342 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4343 dwarf2_per_objfile->read_in_chain = per_cu;
4344 }
4345 else
4346 {
4347 cu = per_cu->cu;
4348 info_ptr += cu->header.first_die_offset;
4349 }
e142c38c 4350
93311388 4351 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4352
4353 /* We try not to read any attributes in this function, because not
4354 all objfiles needed for references have been loaded yet, and symbol
4355 table processing isn't initialized. But we have to set the CU language,
4356 or we won't be able to build types correctly. */
4357 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4358 if (attr)
4359 set_cu_language (DW_UNSND (attr), cu);
4360 else
4361 set_cu_language (language_minimal, cu);
4362
a6c727b2
DJ
4363 /* Similarly, if we do not read the producer, we can not apply
4364 producer-specific interpretation. */
4365 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4366 if (attr)
4367 cu->producer = DW_STRING (attr);
4368
98bfdba5
PA
4369 if (read_cu)
4370 {
4371 do_cleanups (free_abbrevs_cleanup);
e142c38c 4372
98bfdba5
PA
4373 /* We've successfully allocated this compilation unit. Let our
4374 caller clean it up when finished with it. */
4375 discard_cleanups (free_cu_cleanup);
4376 }
10b3939b
DJ
4377}
4378
3da10d80
KS
4379/* Add a DIE to the delayed physname list. */
4380
4381static void
4382add_to_method_list (struct type *type, int fnfield_index, int index,
4383 const char *name, struct die_info *die,
4384 struct dwarf2_cu *cu)
4385{
4386 struct delayed_method_info mi;
4387 mi.type = type;
4388 mi.fnfield_index = fnfield_index;
4389 mi.index = index;
4390 mi.name = name;
4391 mi.die = die;
4392 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4393}
4394
4395/* A cleanup for freeing the delayed method list. */
4396
4397static void
4398free_delayed_list (void *ptr)
4399{
4400 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4401 if (cu->method_list != NULL)
4402 {
4403 VEC_free (delayed_method_info, cu->method_list);
4404 cu->method_list = NULL;
4405 }
4406}
4407
4408/* Compute the physnames of any methods on the CU's method list.
4409
4410 The computation of method physnames is delayed in order to avoid the
4411 (bad) condition that one of the method's formal parameters is of an as yet
4412 incomplete type. */
4413
4414static void
4415compute_delayed_physnames (struct dwarf2_cu *cu)
4416{
4417 int i;
4418 struct delayed_method_info *mi;
4419 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4420 {
4421 char *physname;
4422 struct fn_fieldlist *fn_flp
4423 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4424 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4425 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4426 }
4427}
4428
10b3939b
DJ
4429/* Generate full symbol information for PST and CU, whose DIEs have
4430 already been loaded into memory. */
4431
4432static void
4433process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4434{
10b3939b 4435 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4436 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4437 CORE_ADDR lowpc, highpc;
4438 struct symtab *symtab;
3da10d80 4439 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4440 CORE_ADDR baseaddr;
4441
4442 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4443
10b3939b
DJ
4444 buildsym_init ();
4445 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4446 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4447
4448 cu->list_in_scope = &file_symbols;
c906108c 4449
d85a05f0 4450 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4451
c906108c 4452 /* Do line number decoding in read_file_scope () */
10b3939b 4453 process_die (cu->dies, cu);
c906108c 4454
3da10d80
KS
4455 /* Now that we have processed all the DIEs in the CU, all the types
4456 should be complete, and it should now be safe to compute all of the
4457 physnames. */
4458 compute_delayed_physnames (cu);
4459 do_cleanups (delayed_list_cleanup);
4460
fae299cd
DC
4461 /* Some compilers don't define a DW_AT_high_pc attribute for the
4462 compilation unit. If the DW_AT_high_pc is missing, synthesize
4463 it, by scanning the DIE's below the compilation unit. */
10b3939b 4464 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4465
613e1657 4466 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4467
4468 /* Set symtab language to language from DW_AT_language.
4469 If the compilation is from a C file generated by language preprocessors,
4470 do not set the language if it was already deduced by start_subfile. */
4471 if (symtab != NULL
10b3939b 4472 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4473 {
10b3939b 4474 symtab->language = cu->language;
c906108c 4475 }
9291a0cd
TT
4476
4477 if (dwarf2_per_objfile->using_index)
4478 per_cu->v.quick->symtab = symtab;
4479 else
4480 {
4481 struct partial_symtab *pst = per_cu->v.psymtab;
4482 pst->symtab = symtab;
4483 pst->readin = 1;
4484 }
c906108c
SS
4485
4486 do_cleanups (back_to);
4487}
4488
4489/* Process a die and its children. */
4490
4491static void
e7c27a73 4492process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4493{
4494 switch (die->tag)
4495 {
4496 case DW_TAG_padding:
4497 break;
4498 case DW_TAG_compile_unit:
e7c27a73 4499 read_file_scope (die, cu);
c906108c 4500 break;
348e048f
DE
4501 case DW_TAG_type_unit:
4502 read_type_unit_scope (die, cu);
4503 break;
c906108c 4504 case DW_TAG_subprogram:
c906108c 4505 case DW_TAG_inlined_subroutine:
edb3359d 4506 read_func_scope (die, cu);
c906108c
SS
4507 break;
4508 case DW_TAG_lexical_block:
14898363
L
4509 case DW_TAG_try_block:
4510 case DW_TAG_catch_block:
e7c27a73 4511 read_lexical_block_scope (die, cu);
c906108c
SS
4512 break;
4513 case DW_TAG_class_type:
680b30c7 4514 case DW_TAG_interface_type:
c906108c
SS
4515 case DW_TAG_structure_type:
4516 case DW_TAG_union_type:
134d01f1 4517 process_structure_scope (die, cu);
c906108c
SS
4518 break;
4519 case DW_TAG_enumeration_type:
134d01f1 4520 process_enumeration_scope (die, cu);
c906108c 4521 break;
134d01f1 4522
f792889a
DJ
4523 /* These dies have a type, but processing them does not create
4524 a symbol or recurse to process the children. Therefore we can
4525 read them on-demand through read_type_die. */
c906108c 4526 case DW_TAG_subroutine_type:
72019c9c 4527 case DW_TAG_set_type:
c906108c 4528 case DW_TAG_array_type:
c906108c 4529 case DW_TAG_pointer_type:
c906108c 4530 case DW_TAG_ptr_to_member_type:
c906108c 4531 case DW_TAG_reference_type:
c906108c 4532 case DW_TAG_string_type:
c906108c 4533 break;
134d01f1 4534
c906108c 4535 case DW_TAG_base_type:
a02abb62 4536 case DW_TAG_subrange_type:
cb249c71 4537 case DW_TAG_typedef:
134d01f1
DJ
4538 /* Add a typedef symbol for the type definition, if it has a
4539 DW_AT_name. */
f792889a 4540 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4541 break;
c906108c 4542 case DW_TAG_common_block:
e7c27a73 4543 read_common_block (die, cu);
c906108c
SS
4544 break;
4545 case DW_TAG_common_inclusion:
4546 break;
d9fa45fe 4547 case DW_TAG_namespace:
63d06c5c 4548 processing_has_namespace_info = 1;
e7c27a73 4549 read_namespace (die, cu);
d9fa45fe 4550 break;
5d7cb8df 4551 case DW_TAG_module:
f55ee35c 4552 processing_has_namespace_info = 1;
5d7cb8df
JK
4553 read_module (die, cu);
4554 break;
d9fa45fe
DC
4555 case DW_TAG_imported_declaration:
4556 case DW_TAG_imported_module:
63d06c5c 4557 processing_has_namespace_info = 1;
27aa8d6a
SW
4558 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4559 || cu->language != language_fortran))
4560 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4561 dwarf_tag_name (die->tag));
4562 read_import_statement (die, cu);
d9fa45fe 4563 break;
c906108c 4564 default:
e7c27a73 4565 new_symbol (die, NULL, cu);
c906108c
SS
4566 break;
4567 }
4568}
4569
94af9270
KS
4570/* A helper function for dwarf2_compute_name which determines whether DIE
4571 needs to have the name of the scope prepended to the name listed in the
4572 die. */
4573
4574static int
4575die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4576{
1c809c68
TT
4577 struct attribute *attr;
4578
94af9270
KS
4579 switch (die->tag)
4580 {
4581 case DW_TAG_namespace:
4582 case DW_TAG_typedef:
4583 case DW_TAG_class_type:
4584 case DW_TAG_interface_type:
4585 case DW_TAG_structure_type:
4586 case DW_TAG_union_type:
4587 case DW_TAG_enumeration_type:
4588 case DW_TAG_enumerator:
4589 case DW_TAG_subprogram:
4590 case DW_TAG_member:
4591 return 1;
4592
4593 case DW_TAG_variable:
4594 /* We only need to prefix "globally" visible variables. These include
4595 any variable marked with DW_AT_external or any variable that
4596 lives in a namespace. [Variables in anonymous namespaces
4597 require prefixing, but they are not DW_AT_external.] */
4598
4599 if (dwarf2_attr (die, DW_AT_specification, cu))
4600 {
4601 struct dwarf2_cu *spec_cu = cu;
9a619af0 4602
94af9270
KS
4603 return die_needs_namespace (die_specification (die, &spec_cu),
4604 spec_cu);
4605 }
4606
1c809c68 4607 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4608 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4609 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4610 return 0;
4611 /* A variable in a lexical block of some kind does not need a
4612 namespace, even though in C++ such variables may be external
4613 and have a mangled name. */
4614 if (die->parent->tag == DW_TAG_lexical_block
4615 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4616 || die->parent->tag == DW_TAG_catch_block
4617 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4618 return 0;
4619 return 1;
94af9270
KS
4620
4621 default:
4622 return 0;
4623 }
4624}
4625
98bfdba5
PA
4626/* Retrieve the last character from a mem_file. */
4627
4628static void
4629do_ui_file_peek_last (void *object, const char *buffer, long length)
4630{
4631 char *last_char_p = (char *) object;
4632
4633 if (length > 0)
4634 *last_char_p = buffer[length - 1];
4635}
4636
94af9270
KS
4637/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4638 compute the physname for the object, which include a method's
4639 formal parameters (C++/Java) and return type (Java).
4640
af6b7be1
JB
4641 For Ada, return the DIE's linkage name rather than the fully qualified
4642 name. PHYSNAME is ignored..
4643
94af9270
KS
4644 The result is allocated on the objfile_obstack and canonicalized. */
4645
4646static const char *
4647dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4648 int physname)
4649{
4650 if (name == NULL)
4651 name = dwarf2_name (die, cu);
4652
f55ee35c
JK
4653 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4654 compute it by typename_concat inside GDB. */
4655 if (cu->language == language_ada
4656 || (cu->language == language_fortran && physname))
4657 {
4658 /* For Ada unit, we prefer the linkage name over the name, as
4659 the former contains the exported name, which the user expects
4660 to be able to reference. Ideally, we want the user to be able
4661 to reference this entity using either natural or linkage name,
4662 but we haven't started looking at this enhancement yet. */
4663 struct attribute *attr;
4664
4665 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4666 if (attr == NULL)
4667 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4668 if (attr && DW_STRING (attr))
4669 return DW_STRING (attr);
4670 }
4671
94af9270
KS
4672 /* These are the only languages we know how to qualify names in. */
4673 if (name != NULL
f55ee35c
JK
4674 && (cu->language == language_cplus || cu->language == language_java
4675 || cu->language == language_fortran))
94af9270
KS
4676 {
4677 if (die_needs_namespace (die, cu))
4678 {
4679 long length;
4680 char *prefix;
4681 struct ui_file *buf;
4682
4683 prefix = determine_prefix (die, cu);
4684 buf = mem_fileopen ();
4685 if (*prefix != '\0')
4686 {
f55ee35c
JK
4687 char *prefixed_name = typename_concat (NULL, prefix, name,
4688 physname, cu);
9a619af0 4689
94af9270
KS
4690 fputs_unfiltered (prefixed_name, buf);
4691 xfree (prefixed_name);
4692 }
4693 else
4694 fputs_unfiltered (name ? name : "", buf);
4695
98bfdba5
PA
4696 /* Template parameters may be specified in the DIE's DW_AT_name, or
4697 as children with DW_TAG_template_type_param or
4698 DW_TAG_value_type_param. If the latter, add them to the name
4699 here. If the name already has template parameters, then
4700 skip this step; some versions of GCC emit both, and
4701 it is more efficient to use the pre-computed name.
4702
4703 Something to keep in mind about this process: it is very
4704 unlikely, or in some cases downright impossible, to produce
4705 something that will match the mangled name of a function.
4706 If the definition of the function has the same debug info,
4707 we should be able to match up with it anyway. But fallbacks
4708 using the minimal symbol, for instance to find a method
4709 implemented in a stripped copy of libstdc++, will not work.
4710 If we do not have debug info for the definition, we will have to
4711 match them up some other way.
4712
4713 When we do name matching there is a related problem with function
4714 templates; two instantiated function templates are allowed to
4715 differ only by their return types, which we do not add here. */
4716
4717 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4718 {
4719 struct attribute *attr;
4720 struct die_info *child;
4721 int first = 1;
4722
4723 die->building_fullname = 1;
4724
4725 for (child = die->child; child != NULL; child = child->sibling)
4726 {
4727 struct type *type;
4728 long value;
4729 gdb_byte *bytes;
4730 struct dwarf2_locexpr_baton *baton;
4731 struct value *v;
4732
4733 if (child->tag != DW_TAG_template_type_param
4734 && child->tag != DW_TAG_template_value_param)
4735 continue;
4736
4737 if (first)
4738 {
4739 fputs_unfiltered ("<", buf);
4740 first = 0;
4741 }
4742 else
4743 fputs_unfiltered (", ", buf);
4744
4745 attr = dwarf2_attr (child, DW_AT_type, cu);
4746 if (attr == NULL)
4747 {
4748 complaint (&symfile_complaints,
4749 _("template parameter missing DW_AT_type"));
4750 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4751 continue;
4752 }
4753 type = die_type (child, cu);
4754
4755 if (child->tag == DW_TAG_template_type_param)
4756 {
4757 c_print_type (type, "", buf, -1, 0);
4758 continue;
4759 }
4760
4761 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4762 if (attr == NULL)
4763 {
4764 complaint (&symfile_complaints,
4765 _("template parameter missing DW_AT_const_value"));
4766 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4767 continue;
4768 }
4769
4770 dwarf2_const_value_attr (attr, type, name,
4771 &cu->comp_unit_obstack, cu,
4772 &value, &bytes, &baton);
4773
4774 if (TYPE_NOSIGN (type))
4775 /* GDB prints characters as NUMBER 'CHAR'. If that's
4776 changed, this can use value_print instead. */
4777 c_printchar (value, type, buf);
4778 else
4779 {
4780 struct value_print_options opts;
4781
4782 if (baton != NULL)
4783 v = dwarf2_evaluate_loc_desc (type, NULL,
4784 baton->data,
4785 baton->size,
4786 baton->per_cu);
4787 else if (bytes != NULL)
4788 {
4789 v = allocate_value (type);
4790 memcpy (value_contents_writeable (v), bytes,
4791 TYPE_LENGTH (type));
4792 }
4793 else
4794 v = value_from_longest (type, value);
4795
4796 /* Specify decimal so that we do not depend on the radix. */
4797 get_formatted_print_options (&opts, 'd');
4798 opts.raw = 1;
4799 value_print (v, buf, &opts);
4800 release_value (v);
4801 value_free (v);
4802 }
4803 }
4804
4805 die->building_fullname = 0;
4806
4807 if (!first)
4808 {
4809 /* Close the argument list, with a space if necessary
4810 (nested templates). */
4811 char last_char = '\0';
4812 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4813 if (last_char == '>')
4814 fputs_unfiltered (" >", buf);
4815 else
4816 fputs_unfiltered (">", buf);
4817 }
4818 }
4819
94af9270
KS
4820 /* For Java and C++ methods, append formal parameter type
4821 information, if PHYSNAME. */
6e70227d 4822
94af9270
KS
4823 if (physname && die->tag == DW_TAG_subprogram
4824 && (cu->language == language_cplus
4825 || cu->language == language_java))
4826 {
4827 struct type *type = read_type_die (die, cu);
4828
4829 c_type_print_args (type, buf, 0, cu->language);
4830
4831 if (cu->language == language_java)
4832 {
4833 /* For java, we must append the return type to method
4834 names. */
4835 if (die->tag == DW_TAG_subprogram)
4836 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4837 0, 0);
4838 }
4839 else if (cu->language == language_cplus)
4840 {
4841 if (TYPE_NFIELDS (type) > 0
4842 && TYPE_FIELD_ARTIFICIAL (type, 0)
4843 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4844 fputs_unfiltered (" const", buf);
4845 }
4846 }
4847
4848 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4849 &length);
4850 ui_file_delete (buf);
4851
4852 if (cu->language == language_cplus)
4853 {
4854 char *cname
4855 = dwarf2_canonicalize_name (name, cu,
4856 &cu->objfile->objfile_obstack);
9a619af0 4857
94af9270
KS
4858 if (cname != NULL)
4859 name = cname;
4860 }
4861 }
4862 }
4863
4864 return name;
4865}
4866
0114d602
DJ
4867/* Return the fully qualified name of DIE, based on its DW_AT_name.
4868 If scope qualifiers are appropriate they will be added. The result
4869 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4870 not have a name. NAME may either be from a previous call to
4871 dwarf2_name or NULL.
4872
4873 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4874
4875static const char *
94af9270 4876dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4877{
94af9270
KS
4878 return dwarf2_compute_name (name, die, cu, 0);
4879}
0114d602 4880
94af9270
KS
4881/* Construct a physname for the given DIE in CU. NAME may either be
4882 from a previous call to dwarf2_name or NULL. The result will be
4883 allocated on the objfile_objstack or NULL if the DIE does not have a
4884 name.
0114d602 4885
94af9270 4886 The output string will be canonicalized (if C++/Java). */
0114d602 4887
94af9270
KS
4888static const char *
4889dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4890{
4891 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4892}
4893
27aa8d6a
SW
4894/* Read the import statement specified by the given die and record it. */
4895
4896static void
4897read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4898{
4899 struct attribute *import_attr;
4900 struct die_info *imported_die;
de4affc9 4901 struct dwarf2_cu *imported_cu;
27aa8d6a 4902 const char *imported_name;
794684b6 4903 const char *imported_name_prefix;
13387711
SW
4904 const char *canonical_name;
4905 const char *import_alias;
4906 const char *imported_declaration = NULL;
794684b6 4907 const char *import_prefix;
13387711
SW
4908
4909 char *temp;
27aa8d6a
SW
4910
4911 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4912 if (import_attr == NULL)
4913 {
4914 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4915 dwarf_tag_name (die->tag));
4916 return;
4917 }
4918
de4affc9
CC
4919 imported_cu = cu;
4920 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4921 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4922 if (imported_name == NULL)
4923 {
4924 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4925
4926 The import in the following code:
4927 namespace A
4928 {
4929 typedef int B;
4930 }
4931
4932 int main ()
4933 {
4934 using A::B;
4935 B b;
4936 return b;
4937 }
4938
4939 ...
4940 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4941 <52> DW_AT_decl_file : 1
4942 <53> DW_AT_decl_line : 6
4943 <54> DW_AT_import : <0x75>
4944 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4945 <59> DW_AT_name : B
4946 <5b> DW_AT_decl_file : 1
4947 <5c> DW_AT_decl_line : 2
4948 <5d> DW_AT_type : <0x6e>
4949 ...
4950 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4951 <76> DW_AT_byte_size : 4
4952 <77> DW_AT_encoding : 5 (signed)
4953
4954 imports the wrong die ( 0x75 instead of 0x58 ).
4955 This case will be ignored until the gcc bug is fixed. */
4956 return;
4957 }
4958
82856980
SW
4959 /* Figure out the local name after import. */
4960 import_alias = dwarf2_name (die, cu);
27aa8d6a 4961
794684b6
SW
4962 /* Figure out where the statement is being imported to. */
4963 import_prefix = determine_prefix (die, cu);
4964
4965 /* Figure out what the scope of the imported die is and prepend it
4966 to the name of the imported die. */
de4affc9 4967 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4968
f55ee35c
JK
4969 if (imported_die->tag != DW_TAG_namespace
4970 && imported_die->tag != DW_TAG_module)
794684b6 4971 {
13387711
SW
4972 imported_declaration = imported_name;
4973 canonical_name = imported_name_prefix;
794684b6 4974 }
13387711 4975 else if (strlen (imported_name_prefix) > 0)
794684b6 4976 {
13387711
SW
4977 temp = alloca (strlen (imported_name_prefix)
4978 + 2 + strlen (imported_name) + 1);
4979 strcpy (temp, imported_name_prefix);
4980 strcat (temp, "::");
4981 strcat (temp, imported_name);
4982 canonical_name = temp;
794684b6 4983 }
13387711
SW
4984 else
4985 canonical_name = imported_name;
794684b6 4986
c0cc3a76
SW
4987 cp_add_using_directive (import_prefix,
4988 canonical_name,
4989 import_alias,
13387711 4990 imported_declaration,
c0cc3a76 4991 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4992}
4993
5fb290d7 4994static void
e142c38c 4995initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4996{
e142c38c 4997 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4998}
4999
cb1df416
DJ
5000static void
5001free_cu_line_header (void *arg)
5002{
5003 struct dwarf2_cu *cu = arg;
5004
5005 free_line_header (cu->line_header);
5006 cu->line_header = NULL;
5007}
5008
9291a0cd
TT
5009static void
5010find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5011 char **name, char **comp_dir)
5012{
5013 struct attribute *attr;
5014
5015 *name = NULL;
5016 *comp_dir = NULL;
5017
5018 /* Find the filename. Do not use dwarf2_name here, since the filename
5019 is not a source language identifier. */
5020 attr = dwarf2_attr (die, DW_AT_name, cu);
5021 if (attr)
5022 {
5023 *name = DW_STRING (attr);
5024 }
5025
5026 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5027 if (attr)
5028 *comp_dir = DW_STRING (attr);
5029 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5030 {
5031 *comp_dir = ldirname (*name);
5032 if (*comp_dir != NULL)
5033 make_cleanup (xfree, *comp_dir);
5034 }
5035 if (*comp_dir != NULL)
5036 {
5037 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5038 directory, get rid of it. */
5039 char *cp = strchr (*comp_dir, ':');
5040
5041 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5042 *comp_dir = cp + 1;
5043 }
5044
5045 if (*name == NULL)
5046 *name = "<unknown>";
5047}
5048
c906108c 5049static void
e7c27a73 5050read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5051{
e7c27a73 5052 struct objfile *objfile = cu->objfile;
debd256d 5053 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5054 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5055 CORE_ADDR highpc = ((CORE_ADDR) 0);
5056 struct attribute *attr;
e1024ff1 5057 char *name = NULL;
c906108c
SS
5058 char *comp_dir = NULL;
5059 struct die_info *child_die;
5060 bfd *abfd = objfile->obfd;
debd256d 5061 struct line_header *line_header = 0;
e142c38c 5062 CORE_ADDR baseaddr;
6e70227d 5063
e142c38c 5064 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5065
fae299cd 5066 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5067
5068 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5069 from finish_block. */
2acceee2 5070 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5071 lowpc = highpc;
5072 lowpc += baseaddr;
5073 highpc += baseaddr;
5074
9291a0cd 5075 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5076
e142c38c 5077 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5078 if (attr)
5079 {
e142c38c 5080 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5081 }
5082
b0f35d58 5083 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5084 if (attr)
b0f35d58 5085 cu->producer = DW_STRING (attr);
303b6f5d 5086
c906108c
SS
5087 /* We assume that we're processing GCC output. */
5088 processing_gcc_compilation = 2;
c906108c 5089
df8a16a1
DJ
5090 processing_has_namespace_info = 0;
5091
c906108c
SS
5092 start_symtab (name, comp_dir, lowpc);
5093 record_debugformat ("DWARF 2");
303b6f5d 5094 record_producer (cu->producer);
c906108c 5095
e142c38c 5096 initialize_cu_func_list (cu);
c906108c 5097
cb1df416
DJ
5098 /* Decode line number information if present. We do this before
5099 processing child DIEs, so that the line header table is available
5100 for DW_AT_decl_file. */
e142c38c 5101 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5102 if (attr)
5103 {
debd256d 5104 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5105 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5106 if (line_header)
5107 {
cb1df416
DJ
5108 cu->line_header = line_header;
5109 make_cleanup (free_cu_line_header, cu);
aaa75496 5110 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5111 }
5fb290d7 5112 }
debd256d 5113
cb1df416
DJ
5114 /* Process all dies in compilation unit. */
5115 if (die->child != NULL)
5116 {
5117 child_die = die->child;
5118 while (child_die && child_die->tag)
5119 {
5120 process_die (child_die, cu);
5121 child_die = sibling_die (child_die);
5122 }
5123 }
5124
2e276125
JB
5125 /* Decode macro information, if present. Dwarf 2 macro information
5126 refers to information in the line number info statement program
5127 header, so we can only read it if we've read the header
5128 successfully. */
e142c38c 5129 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5130 if (attr && line_header)
2e276125
JB
5131 {
5132 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5133
2e276125 5134 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5135 comp_dir, abfd, cu);
2e276125 5136 }
debd256d 5137 do_cleanups (back_to);
5fb290d7
DJ
5138}
5139
348e048f
DE
5140/* For TUs we want to skip the first top level sibling if it's not the
5141 actual type being defined by this TU. In this case the first top
5142 level sibling is there to provide context only. */
5143
5144static void
5145read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5146{
5147 struct objfile *objfile = cu->objfile;
5148 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5149 CORE_ADDR lowpc;
5150 struct attribute *attr;
5151 char *name = NULL;
5152 char *comp_dir = NULL;
5153 struct die_info *child_die;
5154 bfd *abfd = objfile->obfd;
348e048f
DE
5155
5156 /* start_symtab needs a low pc, but we don't really have one.
5157 Do what read_file_scope would do in the absence of such info. */
5158 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5159
5160 /* Find the filename. Do not use dwarf2_name here, since the filename
5161 is not a source language identifier. */
5162 attr = dwarf2_attr (die, DW_AT_name, cu);
5163 if (attr)
5164 name = DW_STRING (attr);
5165
5166 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5167 if (attr)
5168 comp_dir = DW_STRING (attr);
5169 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5170 {
5171 comp_dir = ldirname (name);
5172 if (comp_dir != NULL)
5173 make_cleanup (xfree, comp_dir);
5174 }
5175
5176 if (name == NULL)
5177 name = "<unknown>";
5178
5179 attr = dwarf2_attr (die, DW_AT_language, cu);
5180 if (attr)
5181 set_cu_language (DW_UNSND (attr), cu);
5182
5183 /* This isn't technically needed today. It is done for symmetry
5184 with read_file_scope. */
5185 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5186 if (attr)
348e048f
DE
5187 cu->producer = DW_STRING (attr);
5188
5189 /* We assume that we're processing GCC output. */
5190 processing_gcc_compilation = 2;
5191
5192 processing_has_namespace_info = 0;
5193
5194 start_symtab (name, comp_dir, lowpc);
5195 record_debugformat ("DWARF 2");
5196 record_producer (cu->producer);
5197
5198 /* Process the dies in the type unit. */
5199 if (die->child == NULL)
5200 {
5201 dump_die_for_error (die);
5202 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5203 bfd_get_filename (abfd));
5204 }
5205
5206 child_die = die->child;
5207
5208 while (child_die && child_die->tag)
5209 {
5210 process_die (child_die, cu);
5211
5212 child_die = sibling_die (child_die);
5213 }
5214
5215 do_cleanups (back_to);
5216}
5217
5fb290d7 5218static void
e142c38c
DJ
5219add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5220 struct dwarf2_cu *cu)
5fb290d7
DJ
5221{
5222 struct function_range *thisfn;
5223
5224 thisfn = (struct function_range *)
7b5a2f43 5225 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5226 thisfn->name = name;
5227 thisfn->lowpc = lowpc;
5228 thisfn->highpc = highpc;
5229 thisfn->seen_line = 0;
5230 thisfn->next = NULL;
5231
e142c38c
DJ
5232 if (cu->last_fn == NULL)
5233 cu->first_fn = thisfn;
5fb290d7 5234 else
e142c38c 5235 cu->last_fn->next = thisfn;
5fb290d7 5236
e142c38c 5237 cu->last_fn = thisfn;
c906108c
SS
5238}
5239
d389af10
JK
5240/* qsort helper for inherit_abstract_dies. */
5241
5242static int
5243unsigned_int_compar (const void *ap, const void *bp)
5244{
5245 unsigned int a = *(unsigned int *) ap;
5246 unsigned int b = *(unsigned int *) bp;
5247
5248 return (a > b) - (b > a);
5249}
5250
5251/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5252 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5253 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5254
5255static void
5256inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5257{
5258 struct die_info *child_die;
5259 unsigned die_children_count;
5260 /* CU offsets which were referenced by children of the current DIE. */
5261 unsigned *offsets;
5262 unsigned *offsets_end, *offsetp;
5263 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5264 struct die_info *origin_die;
5265 /* Iterator of the ORIGIN_DIE children. */
5266 struct die_info *origin_child_die;
5267 struct cleanup *cleanups;
5268 struct attribute *attr;
cd02d79d
PA
5269 struct dwarf2_cu *origin_cu;
5270 struct pending **origin_previous_list_in_scope;
d389af10
JK
5271
5272 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5273 if (!attr)
5274 return;
5275
cd02d79d
PA
5276 /* Note that following die references may follow to a die in a
5277 different cu. */
5278
5279 origin_cu = cu;
5280 origin_die = follow_die_ref (die, attr, &origin_cu);
5281
5282 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5283 symbols in. */
5284 origin_previous_list_in_scope = origin_cu->list_in_scope;
5285 origin_cu->list_in_scope = cu->list_in_scope;
5286
edb3359d
DJ
5287 if (die->tag != origin_die->tag
5288 && !(die->tag == DW_TAG_inlined_subroutine
5289 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5290 complaint (&symfile_complaints,
5291 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5292 die->offset, origin_die->offset);
5293
5294 child_die = die->child;
5295 die_children_count = 0;
5296 while (child_die && child_die->tag)
5297 {
5298 child_die = sibling_die (child_die);
5299 die_children_count++;
5300 }
5301 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5302 cleanups = make_cleanup (xfree, offsets);
5303
5304 offsets_end = offsets;
5305 child_die = die->child;
5306 while (child_die && child_die->tag)
5307 {
c38f313d
DJ
5308 /* For each CHILD_DIE, find the corresponding child of
5309 ORIGIN_DIE. If there is more than one layer of
5310 DW_AT_abstract_origin, follow them all; there shouldn't be,
5311 but GCC versions at least through 4.4 generate this (GCC PR
5312 40573). */
5313 struct die_info *child_origin_die = child_die;
cd02d79d 5314 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5315
c38f313d
DJ
5316 while (1)
5317 {
cd02d79d
PA
5318 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5319 child_origin_cu);
c38f313d
DJ
5320 if (attr == NULL)
5321 break;
cd02d79d
PA
5322 child_origin_die = follow_die_ref (child_origin_die, attr,
5323 &child_origin_cu);
c38f313d
DJ
5324 }
5325
d389af10
JK
5326 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5327 counterpart may exist. */
c38f313d 5328 if (child_origin_die != child_die)
d389af10 5329 {
edb3359d
DJ
5330 if (child_die->tag != child_origin_die->tag
5331 && !(child_die->tag == DW_TAG_inlined_subroutine
5332 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5333 complaint (&symfile_complaints,
5334 _("Child DIE 0x%x and its abstract origin 0x%x have "
5335 "different tags"), child_die->offset,
5336 child_origin_die->offset);
c38f313d
DJ
5337 if (child_origin_die->parent != origin_die)
5338 complaint (&symfile_complaints,
5339 _("Child DIE 0x%x and its abstract origin 0x%x have "
5340 "different parents"), child_die->offset,
5341 child_origin_die->offset);
5342 else
5343 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5344 }
5345 child_die = sibling_die (child_die);
5346 }
5347 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5348 unsigned_int_compar);
5349 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5350 if (offsetp[-1] == *offsetp)
5351 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5352 "to DIE 0x%x as their abstract origin"),
5353 die->offset, *offsetp);
5354
5355 offsetp = offsets;
5356 origin_child_die = origin_die->child;
5357 while (origin_child_die && origin_child_die->tag)
5358 {
5359 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5360 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5361 offsetp++;
5362 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5363 {
5364 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5365 process_die (origin_child_die, origin_cu);
d389af10
JK
5366 }
5367 origin_child_die = sibling_die (origin_child_die);
5368 }
cd02d79d 5369 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5370
5371 do_cleanups (cleanups);
5372}
5373
c906108c 5374static void
e7c27a73 5375read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5376{
e7c27a73 5377 struct objfile *objfile = cu->objfile;
52f0bd74 5378 struct context_stack *new;
c906108c
SS
5379 CORE_ADDR lowpc;
5380 CORE_ADDR highpc;
5381 struct die_info *child_die;
edb3359d 5382 struct attribute *attr, *call_line, *call_file;
c906108c 5383 char *name;
e142c38c 5384 CORE_ADDR baseaddr;
801e3a5b 5385 struct block *block;
edb3359d 5386 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5387 VEC (symbolp) *template_args = NULL;
5388 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5389
5390 if (inlined_func)
5391 {
5392 /* If we do not have call site information, we can't show the
5393 caller of this inlined function. That's too confusing, so
5394 only use the scope for local variables. */
5395 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5396 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5397 if (call_line == NULL || call_file == NULL)
5398 {
5399 read_lexical_block_scope (die, cu);
5400 return;
5401 }
5402 }
c906108c 5403
e142c38c
DJ
5404 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5405
94af9270 5406 name = dwarf2_name (die, cu);
c906108c 5407
e8d05480
JB
5408 /* Ignore functions with missing or empty names. These are actually
5409 illegal according to the DWARF standard. */
5410 if (name == NULL)
5411 {
5412 complaint (&symfile_complaints,
5413 _("missing name for subprogram DIE at %d"), die->offset);
5414 return;
5415 }
5416
5417 /* Ignore functions with missing or invalid low and high pc attributes. */
5418 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5419 {
ae4d0c03
PM
5420 attr = dwarf2_attr (die, DW_AT_external, cu);
5421 if (!attr || !DW_UNSND (attr))
5422 complaint (&symfile_complaints,
5423 _("cannot get low and high bounds for subprogram DIE at %d"),
5424 die->offset);
e8d05480
JB
5425 return;
5426 }
c906108c
SS
5427
5428 lowpc += baseaddr;
5429 highpc += baseaddr;
5430
5fb290d7 5431 /* Record the function range for dwarf_decode_lines. */
e142c38c 5432 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5433
34eaf542
TT
5434 /* If we have any template arguments, then we must allocate a
5435 different sort of symbol. */
5436 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5437 {
5438 if (child_die->tag == DW_TAG_template_type_param
5439 || child_die->tag == DW_TAG_template_value_param)
5440 {
5441 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5442 struct template_symbol);
5443 templ_func->base.is_cplus_template_function = 1;
5444 break;
5445 }
5446 }
5447
c906108c 5448 new = push_context (0, lowpc);
34eaf542
TT
5449 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5450 (struct symbol *) templ_func);
4c2df51b 5451
4cecd739
DJ
5452 /* If there is a location expression for DW_AT_frame_base, record
5453 it. */
e142c38c 5454 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5455 if (attr)
c034e007
AC
5456 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5457 expression is being recorded directly in the function's symbol
5458 and not in a separate frame-base object. I guess this hack is
5459 to avoid adding some sort of frame-base adjunct/annex to the
5460 function's symbol :-(. The problem with doing this is that it
5461 results in a function symbol with a location expression that
5462 has nothing to do with the location of the function, ouch! The
5463 relationship should be: a function's symbol has-a frame base; a
5464 frame-base has-a location expression. */
e7c27a73 5465 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5466
e142c38c 5467 cu->list_in_scope = &local_symbols;
c906108c 5468
639d11d3 5469 if (die->child != NULL)
c906108c 5470 {
639d11d3 5471 child_die = die->child;
c906108c
SS
5472 while (child_die && child_die->tag)
5473 {
34eaf542
TT
5474 if (child_die->tag == DW_TAG_template_type_param
5475 || child_die->tag == DW_TAG_template_value_param)
5476 {
5477 struct symbol *arg = new_symbol (child_die, NULL, cu);
5478
5479 VEC_safe_push (symbolp, template_args, arg);
5480 }
5481 else
5482 process_die (child_die, cu);
c906108c
SS
5483 child_die = sibling_die (child_die);
5484 }
5485 }
5486
d389af10
JK
5487 inherit_abstract_dies (die, cu);
5488
4a811a97
UW
5489 /* If we have a DW_AT_specification, we might need to import using
5490 directives from the context of the specification DIE. See the
5491 comment in determine_prefix. */
5492 if (cu->language == language_cplus
5493 && dwarf2_attr (die, DW_AT_specification, cu))
5494 {
5495 struct dwarf2_cu *spec_cu = cu;
5496 struct die_info *spec_die = die_specification (die, &spec_cu);
5497
5498 while (spec_die)
5499 {
5500 child_die = spec_die->child;
5501 while (child_die && child_die->tag)
5502 {
5503 if (child_die->tag == DW_TAG_imported_module)
5504 process_die (child_die, spec_cu);
5505 child_die = sibling_die (child_die);
5506 }
5507
5508 /* In some cases, GCC generates specification DIEs that
5509 themselves contain DW_AT_specification attributes. */
5510 spec_die = die_specification (spec_die, &spec_cu);
5511 }
5512 }
5513
c906108c
SS
5514 new = pop_context ();
5515 /* Make a block for the local symbols within. */
801e3a5b
JB
5516 block = finish_block (new->name, &local_symbols, new->old_blocks,
5517 lowpc, highpc, objfile);
5518
df8a16a1 5519 /* For C++, set the block's scope. */
f55ee35c 5520 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5521 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5522 determine_prefix (die, cu),
df8a16a1
DJ
5523 processing_has_namespace_info);
5524
801e3a5b
JB
5525 /* If we have address ranges, record them. */
5526 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5527
34eaf542
TT
5528 /* Attach template arguments to function. */
5529 if (! VEC_empty (symbolp, template_args))
5530 {
5531 gdb_assert (templ_func != NULL);
5532
5533 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5534 templ_func->template_arguments
5535 = obstack_alloc (&objfile->objfile_obstack,
5536 (templ_func->n_template_arguments
5537 * sizeof (struct symbol *)));
5538 memcpy (templ_func->template_arguments,
5539 VEC_address (symbolp, template_args),
5540 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5541 VEC_free (symbolp, template_args);
5542 }
5543
208d8187
JB
5544 /* In C++, we can have functions nested inside functions (e.g., when
5545 a function declares a class that has methods). This means that
5546 when we finish processing a function scope, we may need to go
5547 back to building a containing block's symbol lists. */
5548 local_symbols = new->locals;
5549 param_symbols = new->params;
27aa8d6a 5550 using_directives = new->using_directives;
208d8187 5551
921e78cf
JB
5552 /* If we've finished processing a top-level function, subsequent
5553 symbols go in the file symbol list. */
5554 if (outermost_context_p ())
e142c38c 5555 cu->list_in_scope = &file_symbols;
c906108c
SS
5556}
5557
5558/* Process all the DIES contained within a lexical block scope. Start
5559 a new scope, process the dies, and then close the scope. */
5560
5561static void
e7c27a73 5562read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5563{
e7c27a73 5564 struct objfile *objfile = cu->objfile;
52f0bd74 5565 struct context_stack *new;
c906108c
SS
5566 CORE_ADDR lowpc, highpc;
5567 struct die_info *child_die;
e142c38c
DJ
5568 CORE_ADDR baseaddr;
5569
5570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5571
5572 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5573 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5574 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5575 be nasty. Might be easier to properly extend generic blocks to
af34e669 5576 describe ranges. */
d85a05f0 5577 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5578 return;
5579 lowpc += baseaddr;
5580 highpc += baseaddr;
5581
5582 push_context (0, lowpc);
639d11d3 5583 if (die->child != NULL)
c906108c 5584 {
639d11d3 5585 child_die = die->child;
c906108c
SS
5586 while (child_die && child_die->tag)
5587 {
e7c27a73 5588 process_die (child_die, cu);
c906108c
SS
5589 child_die = sibling_die (child_die);
5590 }
5591 }
5592 new = pop_context ();
5593
8540c487 5594 if (local_symbols != NULL || using_directives != NULL)
c906108c 5595 {
801e3a5b
JB
5596 struct block *block
5597 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5598 highpc, objfile);
5599
5600 /* Note that recording ranges after traversing children, as we
5601 do here, means that recording a parent's ranges entails
5602 walking across all its children's ranges as they appear in
5603 the address map, which is quadratic behavior.
5604
5605 It would be nicer to record the parent's ranges before
5606 traversing its children, simply overriding whatever you find
5607 there. But since we don't even decide whether to create a
5608 block until after we've traversed its children, that's hard
5609 to do. */
5610 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5611 }
5612 local_symbols = new->locals;
27aa8d6a 5613 using_directives = new->using_directives;
c906108c
SS
5614}
5615
43039443 5616/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5617 Return 1 if the attributes are present and valid, otherwise, return 0.
5618 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5619
5620static int
5621dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5622 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5623 struct partial_symtab *ranges_pst)
43039443
JK
5624{
5625 struct objfile *objfile = cu->objfile;
5626 struct comp_unit_head *cu_header = &cu->header;
5627 bfd *obfd = objfile->obfd;
5628 unsigned int addr_size = cu_header->addr_size;
5629 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5630 /* Base address selection entry. */
5631 CORE_ADDR base;
5632 int found_base;
5633 unsigned int dummy;
5634 gdb_byte *buffer;
5635 CORE_ADDR marker;
5636 int low_set;
5637 CORE_ADDR low = 0;
5638 CORE_ADDR high = 0;
ff013f42 5639 CORE_ADDR baseaddr;
43039443 5640
d00adf39
DE
5641 found_base = cu->base_known;
5642 base = cu->base_address;
43039443 5643
be391dca 5644 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5645 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5646 {
5647 complaint (&symfile_complaints,
5648 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5649 offset);
5650 return 0;
5651 }
dce234bc 5652 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5653
5654 /* Read in the largest possible address. */
5655 marker = read_address (obfd, buffer, cu, &dummy);
5656 if ((marker & mask) == mask)
5657 {
5658 /* If we found the largest possible address, then
5659 read the base address. */
5660 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5661 buffer += 2 * addr_size;
5662 offset += 2 * addr_size;
5663 found_base = 1;
5664 }
5665
5666 low_set = 0;
5667
e7030f15 5668 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5669
43039443
JK
5670 while (1)
5671 {
5672 CORE_ADDR range_beginning, range_end;
5673
5674 range_beginning = read_address (obfd, buffer, cu, &dummy);
5675 buffer += addr_size;
5676 range_end = read_address (obfd, buffer, cu, &dummy);
5677 buffer += addr_size;
5678 offset += 2 * addr_size;
5679
5680 /* An end of list marker is a pair of zero addresses. */
5681 if (range_beginning == 0 && range_end == 0)
5682 /* Found the end of list entry. */
5683 break;
5684
5685 /* Each base address selection entry is a pair of 2 values.
5686 The first is the largest possible address, the second is
5687 the base address. Check for a base address here. */
5688 if ((range_beginning & mask) == mask)
5689 {
5690 /* If we found the largest possible address, then
5691 read the base address. */
5692 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5693 found_base = 1;
5694 continue;
5695 }
5696
5697 if (!found_base)
5698 {
5699 /* We have no valid base address for the ranges
5700 data. */
5701 complaint (&symfile_complaints,
5702 _("Invalid .debug_ranges data (no base address)"));
5703 return 0;
5704 }
5705
5706 range_beginning += base;
5707 range_end += base;
5708
ff013f42
JK
5709 if (ranges_pst != NULL && range_beginning < range_end)
5710 addrmap_set_empty (objfile->psymtabs_addrmap,
5711 range_beginning + baseaddr, range_end - 1 + baseaddr,
5712 ranges_pst);
5713
43039443
JK
5714 /* FIXME: This is recording everything as a low-high
5715 segment of consecutive addresses. We should have a
5716 data structure for discontiguous block ranges
5717 instead. */
5718 if (! low_set)
5719 {
5720 low = range_beginning;
5721 high = range_end;
5722 low_set = 1;
5723 }
5724 else
5725 {
5726 if (range_beginning < low)
5727 low = range_beginning;
5728 if (range_end > high)
5729 high = range_end;
5730 }
5731 }
5732
5733 if (! low_set)
5734 /* If the first entry is an end-of-list marker, the range
5735 describes an empty scope, i.e. no instructions. */
5736 return 0;
5737
5738 if (low_return)
5739 *low_return = low;
5740 if (high_return)
5741 *high_return = high;
5742 return 1;
5743}
5744
af34e669
DJ
5745/* Get low and high pc attributes from a die. Return 1 if the attributes
5746 are present and valid, otherwise, return 0. Return -1 if the range is
5747 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5748static int
af34e669 5749dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5750 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5751 struct partial_symtab *pst)
c906108c
SS
5752{
5753 struct attribute *attr;
af34e669
DJ
5754 CORE_ADDR low = 0;
5755 CORE_ADDR high = 0;
5756 int ret = 0;
c906108c 5757
e142c38c 5758 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5759 if (attr)
af34e669
DJ
5760 {
5761 high = DW_ADDR (attr);
e142c38c 5762 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5763 if (attr)
5764 low = DW_ADDR (attr);
5765 else
5766 /* Found high w/o low attribute. */
5767 return 0;
5768
5769 /* Found consecutive range of addresses. */
5770 ret = 1;
5771 }
c906108c 5772 else
af34e669 5773 {
e142c38c 5774 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5775 if (attr != NULL)
5776 {
af34e669 5777 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5778 .debug_ranges section. */
d85a05f0 5779 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5780 return 0;
43039443 5781 /* Found discontinuous range of addresses. */
af34e669
DJ
5782 ret = -1;
5783 }
5784 }
c906108c
SS
5785
5786 if (high < low)
5787 return 0;
5788
5789 /* When using the GNU linker, .gnu.linkonce. sections are used to
5790 eliminate duplicate copies of functions and vtables and such.
5791 The linker will arbitrarily choose one and discard the others.
5792 The AT_*_pc values for such functions refer to local labels in
5793 these sections. If the section from that file was discarded, the
5794 labels are not in the output, so the relocs get a value of 0.
5795 If this is a discarded function, mark the pc bounds as invalid,
5796 so that GDB will ignore it. */
72dca2f5 5797 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5798 return 0;
5799
5800 *lowpc = low;
5801 *highpc = high;
af34e669 5802 return ret;
c906108c
SS
5803}
5804
b084d499
JB
5805/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5806 its low and high PC addresses. Do nothing if these addresses could not
5807 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5808 and HIGHPC to the high address if greater than HIGHPC. */
5809
5810static void
5811dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5812 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5813 struct dwarf2_cu *cu)
5814{
5815 CORE_ADDR low, high;
5816 struct die_info *child = die->child;
5817
d85a05f0 5818 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5819 {
5820 *lowpc = min (*lowpc, low);
5821 *highpc = max (*highpc, high);
5822 }
5823
5824 /* If the language does not allow nested subprograms (either inside
5825 subprograms or lexical blocks), we're done. */
5826 if (cu->language != language_ada)
5827 return;
6e70227d 5828
b084d499
JB
5829 /* Check all the children of the given DIE. If it contains nested
5830 subprograms, then check their pc bounds. Likewise, we need to
5831 check lexical blocks as well, as they may also contain subprogram
5832 definitions. */
5833 while (child && child->tag)
5834 {
5835 if (child->tag == DW_TAG_subprogram
5836 || child->tag == DW_TAG_lexical_block)
5837 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5838 child = sibling_die (child);
5839 }
5840}
5841
fae299cd
DC
5842/* Get the low and high pc's represented by the scope DIE, and store
5843 them in *LOWPC and *HIGHPC. If the correct values can't be
5844 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5845
5846static void
5847get_scope_pc_bounds (struct die_info *die,
5848 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5849 struct dwarf2_cu *cu)
5850{
5851 CORE_ADDR best_low = (CORE_ADDR) -1;
5852 CORE_ADDR best_high = (CORE_ADDR) 0;
5853 CORE_ADDR current_low, current_high;
5854
d85a05f0 5855 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5856 {
5857 best_low = current_low;
5858 best_high = current_high;
5859 }
5860 else
5861 {
5862 struct die_info *child = die->child;
5863
5864 while (child && child->tag)
5865 {
5866 switch (child->tag) {
5867 case DW_TAG_subprogram:
b084d499 5868 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5869 break;
5870 case DW_TAG_namespace:
f55ee35c 5871 case DW_TAG_module:
fae299cd
DC
5872 /* FIXME: carlton/2004-01-16: Should we do this for
5873 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5874 that current GCC's always emit the DIEs corresponding
5875 to definitions of methods of classes as children of a
5876 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5877 the DIEs giving the declarations, which could be
5878 anywhere). But I don't see any reason why the
5879 standards says that they have to be there. */
5880 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5881
5882 if (current_low != ((CORE_ADDR) -1))
5883 {
5884 best_low = min (best_low, current_low);
5885 best_high = max (best_high, current_high);
5886 }
5887 break;
5888 default:
5889 /* Ignore. */
5890 break;
5891 }
5892
5893 child = sibling_die (child);
5894 }
5895 }
5896
5897 *lowpc = best_low;
5898 *highpc = best_high;
5899}
5900
801e3a5b
JB
5901/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5902 in DIE. */
5903static void
5904dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5905 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5906{
5907 struct attribute *attr;
5908
5909 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5910 if (attr)
5911 {
5912 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5913
801e3a5b
JB
5914 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5915 if (attr)
5916 {
5917 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5918
801e3a5b
JB
5919 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5920 }
5921 }
5922
5923 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5924 if (attr)
5925 {
5926 bfd *obfd = cu->objfile->obfd;
5927
5928 /* The value of the DW_AT_ranges attribute is the offset of the
5929 address range list in the .debug_ranges section. */
5930 unsigned long offset = DW_UNSND (attr);
dce234bc 5931 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5932
5933 /* For some target architectures, but not others, the
5934 read_address function sign-extends the addresses it returns.
5935 To recognize base address selection entries, we need a
5936 mask. */
5937 unsigned int addr_size = cu->header.addr_size;
5938 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5939
5940 /* The base address, to which the next pair is relative. Note
5941 that this 'base' is a DWARF concept: most entries in a range
5942 list are relative, to reduce the number of relocs against the
5943 debugging information. This is separate from this function's
5944 'baseaddr' argument, which GDB uses to relocate debugging
5945 information from a shared library based on the address at
5946 which the library was loaded. */
d00adf39
DE
5947 CORE_ADDR base = cu->base_address;
5948 int base_known = cu->base_known;
801e3a5b 5949
be391dca 5950 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5951 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5952 {
5953 complaint (&symfile_complaints,
5954 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5955 offset);
5956 return;
5957 }
5958
5959 for (;;)
5960 {
5961 unsigned int bytes_read;
5962 CORE_ADDR start, end;
5963
5964 start = read_address (obfd, buffer, cu, &bytes_read);
5965 buffer += bytes_read;
5966 end = read_address (obfd, buffer, cu, &bytes_read);
5967 buffer += bytes_read;
5968
5969 /* Did we find the end of the range list? */
5970 if (start == 0 && end == 0)
5971 break;
5972
5973 /* Did we find a base address selection entry? */
5974 else if ((start & base_select_mask) == base_select_mask)
5975 {
5976 base = end;
5977 base_known = 1;
5978 }
5979
5980 /* We found an ordinary address range. */
5981 else
5982 {
5983 if (!base_known)
5984 {
5985 complaint (&symfile_complaints,
5986 _("Invalid .debug_ranges data (no base address)"));
5987 return;
5988 }
5989
6e70227d
DE
5990 record_block_range (block,
5991 baseaddr + base + start,
801e3a5b
JB
5992 baseaddr + base + end - 1);
5993 }
5994 }
5995 }
5996}
5997
c906108c
SS
5998/* Add an aggregate field to the field list. */
5999
6000static void
107d2387 6001dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6002 struct dwarf2_cu *cu)
6e70227d 6003{
e7c27a73 6004 struct objfile *objfile = cu->objfile;
5e2b427d 6005 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6006 struct nextfield *new_field;
6007 struct attribute *attr;
6008 struct field *fp;
6009 char *fieldname = "";
6010
6011 /* Allocate a new field list entry and link it in. */
6012 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6013 make_cleanup (xfree, new_field);
c906108c 6014 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6015
6016 if (die->tag == DW_TAG_inheritance)
6017 {
6018 new_field->next = fip->baseclasses;
6019 fip->baseclasses = new_field;
6020 }
6021 else
6022 {
6023 new_field->next = fip->fields;
6024 fip->fields = new_field;
6025 }
c906108c
SS
6026 fip->nfields++;
6027
6028 /* Handle accessibility and virtuality of field.
6029 The default accessibility for members is public, the default
6030 accessibility for inheritance is private. */
6031 if (die->tag != DW_TAG_inheritance)
6032 new_field->accessibility = DW_ACCESS_public;
6033 else
6034 new_field->accessibility = DW_ACCESS_private;
6035 new_field->virtuality = DW_VIRTUALITY_none;
6036
e142c38c 6037 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6038 if (attr)
6039 new_field->accessibility = DW_UNSND (attr);
6040 if (new_field->accessibility != DW_ACCESS_public)
6041 fip->non_public_fields = 1;
e142c38c 6042 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6043 if (attr)
6044 new_field->virtuality = DW_UNSND (attr);
6045
6046 fp = &new_field->field;
a9a9bd0f 6047
e142c38c 6048 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6049 {
a9a9bd0f 6050 /* Data member other than a C++ static data member. */
6e70227d 6051
c906108c 6052 /* Get type of field. */
e7c27a73 6053 fp->type = die_type (die, cu);
c906108c 6054
d6a843b5 6055 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6056
c906108c 6057 /* Get bit size of field (zero if none). */
e142c38c 6058 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6059 if (attr)
6060 {
6061 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6062 }
6063 else
6064 {
6065 FIELD_BITSIZE (*fp) = 0;
6066 }
6067
6068 /* Get bit offset of field. */
e142c38c 6069 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6070 if (attr)
6071 {
d4b96c9a 6072 int byte_offset = 0;
c6a0999f 6073
3690dd37 6074 if (attr_form_is_section_offset (attr))
d4b96c9a 6075 dwarf2_complex_location_expr_complaint ();
3690dd37 6076 else if (attr_form_is_constant (attr))
c6a0999f 6077 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6078 else if (attr_form_is_block (attr))
c6a0999f 6079 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6080 else
6081 dwarf2_complex_location_expr_complaint ();
c6a0999f 6082
d6a843b5 6083 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6084 }
e142c38c 6085 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6086 if (attr)
6087 {
5e2b427d 6088 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6089 {
6090 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6091 additional bit offset from the MSB of the containing
6092 anonymous object to the MSB of the field. We don't
6093 have to do anything special since we don't need to
6094 know the size of the anonymous object. */
c906108c
SS
6095 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6096 }
6097 else
6098 {
6099 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6100 MSB of the anonymous object, subtract off the number of
6101 bits from the MSB of the field to the MSB of the
6102 object, and then subtract off the number of bits of
6103 the field itself. The result is the bit offset of
6104 the LSB of the field. */
c906108c
SS
6105 int anonymous_size;
6106 int bit_offset = DW_UNSND (attr);
6107
e142c38c 6108 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6109 if (attr)
6110 {
6111 /* The size of the anonymous object containing
6112 the bit field is explicit, so use the
6113 indicated size (in bytes). */
6114 anonymous_size = DW_UNSND (attr);
6115 }
6116 else
6117 {
6118 /* The size of the anonymous object containing
6119 the bit field must be inferred from the type
6120 attribute of the data member containing the
6121 bit field. */
6122 anonymous_size = TYPE_LENGTH (fp->type);
6123 }
6124 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6125 - bit_offset - FIELD_BITSIZE (*fp);
6126 }
6127 }
6128
6129 /* Get name of field. */
39cbfefa
DJ
6130 fieldname = dwarf2_name (die, cu);
6131 if (fieldname == NULL)
6132 fieldname = "";
d8151005
DJ
6133
6134 /* The name is already allocated along with this objfile, so we don't
6135 need to duplicate it for the type. */
6136 fp->name = fieldname;
c906108c
SS
6137
6138 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6139 pointer or virtual base class pointer) to private. */
e142c38c 6140 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6141 {
d48cc9dd 6142 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6143 new_field->accessibility = DW_ACCESS_private;
6144 fip->non_public_fields = 1;
6145 }
6146 }
a9a9bd0f 6147 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6148 {
a9a9bd0f
DC
6149 /* C++ static member. */
6150
6151 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6152 is a declaration, but all versions of G++ as of this writing
6153 (so through at least 3.2.1) incorrectly generate
6154 DW_TAG_variable tags. */
6e70227d 6155
c906108c 6156 char *physname;
c906108c 6157
a9a9bd0f 6158 /* Get name of field. */
39cbfefa
DJ
6159 fieldname = dwarf2_name (die, cu);
6160 if (fieldname == NULL)
c906108c
SS
6161 return;
6162
254e6b9e 6163 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6164 if (attr
6165 /* Only create a symbol if this is an external value.
6166 new_symbol checks this and puts the value in the global symbol
6167 table, which we want. If it is not external, new_symbol
6168 will try to put the value in cu->list_in_scope which is wrong. */
6169 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6170 {
6171 /* A static const member, not much different than an enum as far as
6172 we're concerned, except that we can support more types. */
6173 new_symbol (die, NULL, cu);
6174 }
6175
2df3850c 6176 /* Get physical name. */
94af9270 6177 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6178
d8151005
DJ
6179 /* The name is already allocated along with this objfile, so we don't
6180 need to duplicate it for the type. */
6181 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6182 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6183 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6184 }
6185 else if (die->tag == DW_TAG_inheritance)
6186 {
6187 /* C++ base class field. */
e142c38c 6188 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6189 if (attr)
d4b96c9a
JK
6190 {
6191 int byte_offset = 0;
6192
6193 if (attr_form_is_section_offset (attr))
6194 dwarf2_complex_location_expr_complaint ();
6195 else if (attr_form_is_constant (attr))
6196 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6197 else if (attr_form_is_block (attr))
6198 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6199 else
6200 dwarf2_complex_location_expr_complaint ();
6201
6202 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6203 }
c906108c 6204 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6205 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6206 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6207 fip->nbaseclasses++;
6208 }
6209}
6210
98751a41
JK
6211/* Add a typedef defined in the scope of the FIP's class. */
6212
6213static void
6214dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6215 struct dwarf2_cu *cu)
6e70227d 6216{
98751a41
JK
6217 struct objfile *objfile = cu->objfile;
6218 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6219 struct typedef_field_list *new_field;
6220 struct attribute *attr;
6221 struct typedef_field *fp;
6222 char *fieldname = "";
6223
6224 /* Allocate a new field list entry and link it in. */
6225 new_field = xzalloc (sizeof (*new_field));
6226 make_cleanup (xfree, new_field);
6227
6228 gdb_assert (die->tag == DW_TAG_typedef);
6229
6230 fp = &new_field->field;
6231
6232 /* Get name of field. */
6233 fp->name = dwarf2_name (die, cu);
6234 if (fp->name == NULL)
6235 return;
6236
6237 fp->type = read_type_die (die, cu);
6238
6239 new_field->next = fip->typedef_field_list;
6240 fip->typedef_field_list = new_field;
6241 fip->typedef_field_list_count++;
6242}
6243
c906108c
SS
6244/* Create the vector of fields, and attach it to the type. */
6245
6246static void
fba45db2 6247dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6248 struct dwarf2_cu *cu)
c906108c
SS
6249{
6250 int nfields = fip->nfields;
6251
6252 /* Record the field count, allocate space for the array of fields,
6253 and create blank accessibility bitfields if necessary. */
6254 TYPE_NFIELDS (type) = nfields;
6255 TYPE_FIELDS (type) = (struct field *)
6256 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6257 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6258
b4ba55a1 6259 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6260 {
6261 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6262
6263 TYPE_FIELD_PRIVATE_BITS (type) =
6264 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6265 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6266
6267 TYPE_FIELD_PROTECTED_BITS (type) =
6268 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6269 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6270
6271 TYPE_FIELD_IGNORE_BITS (type) =
6272 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6273 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6274 }
6275
6276 /* If the type has baseclasses, allocate and clear a bit vector for
6277 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6278 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6279 {
6280 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6281 unsigned char *pointer;
c906108c
SS
6282
6283 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6284 pointer = TYPE_ALLOC (type, num_bytes);
6285 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6286 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6287 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6288 }
6289
6290 /* Copy the saved-up fields into the field vector. Start from the head
6291 of the list, adding to the tail of the field array, so that they end
6292 up in the same order in the array in which they were added to the list. */
6293 while (nfields-- > 0)
6294 {
7d0ccb61
DJ
6295 struct nextfield *fieldp;
6296
6297 if (fip->fields)
6298 {
6299 fieldp = fip->fields;
6300 fip->fields = fieldp->next;
6301 }
6302 else
6303 {
6304 fieldp = fip->baseclasses;
6305 fip->baseclasses = fieldp->next;
6306 }
6307
6308 TYPE_FIELD (type, nfields) = fieldp->field;
6309 switch (fieldp->accessibility)
c906108c 6310 {
c5aa993b 6311 case DW_ACCESS_private:
b4ba55a1
JB
6312 if (cu->language != language_ada)
6313 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6314 break;
c906108c 6315
c5aa993b 6316 case DW_ACCESS_protected:
b4ba55a1
JB
6317 if (cu->language != language_ada)
6318 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6319 break;
c906108c 6320
c5aa993b
JM
6321 case DW_ACCESS_public:
6322 break;
c906108c 6323
c5aa993b
JM
6324 default:
6325 /* Unknown accessibility. Complain and treat it as public. */
6326 {
e2e0b3e5 6327 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6328 fieldp->accessibility);
c5aa993b
JM
6329 }
6330 break;
c906108c
SS
6331 }
6332 if (nfields < fip->nbaseclasses)
6333 {
7d0ccb61 6334 switch (fieldp->virtuality)
c906108c 6335 {
c5aa993b
JM
6336 case DW_VIRTUALITY_virtual:
6337 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6338 if (cu->language == language_ada)
6339 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6340 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6341 break;
c906108c
SS
6342 }
6343 }
c906108c
SS
6344 }
6345}
6346
c906108c
SS
6347/* Add a member function to the proper fieldlist. */
6348
6349static void
107d2387 6350dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6351 struct type *type, struct dwarf2_cu *cu)
c906108c 6352{
e7c27a73 6353 struct objfile *objfile = cu->objfile;
c906108c
SS
6354 struct attribute *attr;
6355 struct fnfieldlist *flp;
6356 int i;
6357 struct fn_field *fnp;
6358 char *fieldname;
c906108c 6359 struct nextfnfield *new_fnfield;
f792889a 6360 struct type *this_type;
c906108c 6361
b4ba55a1
JB
6362 if (cu->language == language_ada)
6363 error ("unexpected member function in Ada type");
6364
2df3850c 6365 /* Get name of member function. */
39cbfefa
DJ
6366 fieldname = dwarf2_name (die, cu);
6367 if (fieldname == NULL)
2df3850c 6368 return;
c906108c 6369
c906108c
SS
6370 /* Look up member function name in fieldlist. */
6371 for (i = 0; i < fip->nfnfields; i++)
6372 {
27bfe10e 6373 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6374 break;
6375 }
6376
6377 /* Create new list element if necessary. */
6378 if (i < fip->nfnfields)
6379 flp = &fip->fnfieldlists[i];
6380 else
6381 {
6382 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6383 {
6384 fip->fnfieldlists = (struct fnfieldlist *)
6385 xrealloc (fip->fnfieldlists,
6386 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6387 * sizeof (struct fnfieldlist));
c906108c 6388 if (fip->nfnfields == 0)
c13c43fd 6389 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6390 }
6391 flp = &fip->fnfieldlists[fip->nfnfields];
6392 flp->name = fieldname;
6393 flp->length = 0;
6394 flp->head = NULL;
3da10d80 6395 i = fip->nfnfields++;
c906108c
SS
6396 }
6397
6398 /* Create a new member function field and chain it to the field list
6399 entry. */
6400 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6401 make_cleanup (xfree, new_fnfield);
c906108c
SS
6402 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6403 new_fnfield->next = flp->head;
6404 flp->head = new_fnfield;
6405 flp->length++;
6406
6407 /* Fill in the member function field info. */
6408 fnp = &new_fnfield->fnfield;
3da10d80
KS
6409
6410 /* Delay processing of the physname until later. */
6411 if (cu->language == language_cplus || cu->language == language_java)
6412 {
6413 add_to_method_list (type, i, flp->length - 1, fieldname,
6414 die, cu);
6415 }
6416 else
6417 {
6418 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6419 fnp->physname = physname ? physname : "";
6420 }
6421
c906108c 6422 fnp->type = alloc_type (objfile);
f792889a
DJ
6423 this_type = read_type_die (die, cu);
6424 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6425 {
f792889a 6426 int nparams = TYPE_NFIELDS (this_type);
c906108c 6427
f792889a 6428 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6429 of the method itself (TYPE_CODE_METHOD). */
6430 smash_to_method_type (fnp->type, type,
f792889a
DJ
6431 TYPE_TARGET_TYPE (this_type),
6432 TYPE_FIELDS (this_type),
6433 TYPE_NFIELDS (this_type),
6434 TYPE_VARARGS (this_type));
c906108c
SS
6435
6436 /* Handle static member functions.
c5aa993b
JM
6437 Dwarf2 has no clean way to discern C++ static and non-static
6438 member functions. G++ helps GDB by marking the first
6439 parameter for non-static member functions (which is the
6440 this pointer) as artificial. We obtain this information
6441 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6442 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6443 fnp->voffset = VOFFSET_STATIC;
6444 }
6445 else
e2e0b3e5 6446 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6447 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6448
6449 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6450 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6451 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6452
6453 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6454 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6455
6456 /* Get accessibility. */
e142c38c 6457 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6458 if (attr)
6459 {
6460 switch (DW_UNSND (attr))
6461 {
c5aa993b
JM
6462 case DW_ACCESS_private:
6463 fnp->is_private = 1;
6464 break;
6465 case DW_ACCESS_protected:
6466 fnp->is_protected = 1;
6467 break;
c906108c
SS
6468 }
6469 }
6470
b02dede2 6471 /* Check for artificial methods. */
e142c38c 6472 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6473 if (attr && DW_UNSND (attr) != 0)
6474 fnp->is_artificial = 1;
6475
0d564a31 6476 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6477 function. For older versions of GCC, this is an offset in the
6478 appropriate virtual table, as specified by DW_AT_containing_type.
6479 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6480 to the object address. */
6481
e142c38c 6482 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6483 if (attr)
8e19ed76 6484 {
aec5aa8b 6485 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6486 {
aec5aa8b
TT
6487 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6488 {
6489 /* Old-style GCC. */
6490 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6491 }
6492 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6493 || (DW_BLOCK (attr)->size > 1
6494 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6495 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6496 {
6497 struct dwarf_block blk;
6498 int offset;
6499
6500 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6501 ? 1 : 2);
6502 blk.size = DW_BLOCK (attr)->size - offset;
6503 blk.data = DW_BLOCK (attr)->data + offset;
6504 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6505 if ((fnp->voffset % cu->header.addr_size) != 0)
6506 dwarf2_complex_location_expr_complaint ();
6507 else
6508 fnp->voffset /= cu->header.addr_size;
6509 fnp->voffset += 2;
6510 }
6511 else
6512 dwarf2_complex_location_expr_complaint ();
6513
6514 if (!fnp->fcontext)
6515 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6516 }
3690dd37 6517 else if (attr_form_is_section_offset (attr))
8e19ed76 6518 {
4d3c2250 6519 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6520 }
6521 else
6522 {
4d3c2250
KB
6523 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6524 fieldname);
8e19ed76 6525 }
0d564a31 6526 }
d48cc9dd
DJ
6527 else
6528 {
6529 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6530 if (attr && DW_UNSND (attr))
6531 {
6532 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6533 complaint (&symfile_complaints,
6534 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6535 fieldname, die->offset);
9655fd1a 6536 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6537 TYPE_CPLUS_DYNAMIC (type) = 1;
6538 }
6539 }
c906108c
SS
6540}
6541
6542/* Create the vector of member function fields, and attach it to the type. */
6543
6544static void
fba45db2 6545dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6546 struct dwarf2_cu *cu)
c906108c
SS
6547{
6548 struct fnfieldlist *flp;
6549 int total_length = 0;
6550 int i;
6551
b4ba55a1
JB
6552 if (cu->language == language_ada)
6553 error ("unexpected member functions in Ada type");
6554
c906108c
SS
6555 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6556 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6557 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6558
6559 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6560 {
6561 struct nextfnfield *nfp = flp->head;
6562 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6563 int k;
6564
6565 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6566 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6567 fn_flp->fn_fields = (struct fn_field *)
6568 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6569 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6570 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6571
6572 total_length += flp->length;
6573 }
6574
6575 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6576 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6577}
6578
1168df01
JB
6579/* Returns non-zero if NAME is the name of a vtable member in CU's
6580 language, zero otherwise. */
6581static int
6582is_vtable_name (const char *name, struct dwarf2_cu *cu)
6583{
6584 static const char vptr[] = "_vptr";
987504bb 6585 static const char vtable[] = "vtable";
1168df01 6586
987504bb
JJ
6587 /* Look for the C++ and Java forms of the vtable. */
6588 if ((cu->language == language_java
6589 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6590 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6591 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6592 return 1;
6593
6594 return 0;
6595}
6596
c0dd20ea 6597/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6598 functions, with the ABI-specified layout. If TYPE describes
6599 such a structure, smash it into a member function type.
61049d3b
DJ
6600
6601 GCC shouldn't do this; it should just output pointer to member DIEs.
6602 This is GCC PR debug/28767. */
c0dd20ea 6603
0b92b5bb
TT
6604static void
6605quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6606{
0b92b5bb 6607 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6608
6609 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6610 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6611 return;
c0dd20ea
DJ
6612
6613 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6614 if (TYPE_FIELD_NAME (type, 0) == NULL
6615 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6616 || TYPE_FIELD_NAME (type, 1) == NULL
6617 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6618 return;
c0dd20ea
DJ
6619
6620 /* Find the type of the method. */
0b92b5bb 6621 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6622 if (pfn_type == NULL
6623 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6624 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6625 return;
c0dd20ea
DJ
6626
6627 /* Look for the "this" argument. */
6628 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6629 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6630 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6631 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6632 return;
c0dd20ea
DJ
6633
6634 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6635 new_type = alloc_type (objfile);
6636 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6637 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6638 TYPE_VARARGS (pfn_type));
0b92b5bb 6639 smash_to_methodptr_type (type, new_type);
c0dd20ea 6640}
1168df01 6641
c906108c
SS
6642/* Called when we find the DIE that starts a structure or union scope
6643 (definition) to process all dies that define the members of the
6644 structure or union.
6645
6646 NOTE: we need to call struct_type regardless of whether or not the
6647 DIE has an at_name attribute, since it might be an anonymous
6648 structure or union. This gets the type entered into our set of
6649 user defined types.
6650
6651 However, if the structure is incomplete (an opaque struct/union)
6652 then suppress creating a symbol table entry for it since gdb only
6653 wants to find the one with the complete definition. Note that if
6654 it is complete, we just call new_symbol, which does it's own
6655 checking about whether the struct/union is anonymous or not (and
6656 suppresses creating a symbol table entry itself). */
6657
f792889a 6658static struct type *
134d01f1 6659read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6660{
e7c27a73 6661 struct objfile *objfile = cu->objfile;
c906108c
SS
6662 struct type *type;
6663 struct attribute *attr;
39cbfefa 6664 char *name;
d3f41bb1 6665 struct cleanup *back_to;
c906108c 6666
348e048f
DE
6667 /* If the definition of this type lives in .debug_types, read that type.
6668 Don't follow DW_AT_specification though, that will take us back up
6669 the chain and we want to go down. */
6670 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6671 if (attr)
6672 {
6673 struct dwarf2_cu *type_cu = cu;
6674 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6675
348e048f
DE
6676 /* We could just recurse on read_structure_type, but we need to call
6677 get_die_type to ensure only one type for this DIE is created.
6678 This is important, for example, because for c++ classes we need
6679 TYPE_NAME set which is only done by new_symbol. Blech. */
6680 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6681
6682 /* TYPE_CU may not be the same as CU.
6683 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6684 return set_die_type (die, type, cu);
6685 }
6686
d3f41bb1
TT
6687 back_to = make_cleanup (null_cleanup, 0);
6688
c0dd20ea 6689 type = alloc_type (objfile);
c906108c 6690 INIT_CPLUS_SPECIFIC (type);
93311388 6691
39cbfefa
DJ
6692 name = dwarf2_name (die, cu);
6693 if (name != NULL)
c906108c 6694 {
987504bb
JJ
6695 if (cu->language == language_cplus
6696 || cu->language == language_java)
63d06c5c 6697 {
3da10d80
KS
6698 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6699
6700 /* dwarf2_full_name might have already finished building the DIE's
6701 type. If so, there is no need to continue. */
6702 if (get_die_type (die, cu) != NULL)
6703 return get_die_type (die, cu);
6704
6705 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6706 if (die->tag == DW_TAG_structure_type
6707 || die->tag == DW_TAG_class_type)
6708 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6709 }
6710 else
6711 {
d8151005
DJ
6712 /* The name is already allocated along with this objfile, so
6713 we don't need to duplicate it for the type. */
94af9270
KS
6714 TYPE_TAG_NAME (type) = (char *) name;
6715 if (die->tag == DW_TAG_class_type)
6716 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6717 }
c906108c
SS
6718 }
6719
6720 if (die->tag == DW_TAG_structure_type)
6721 {
6722 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6723 }
6724 else if (die->tag == DW_TAG_union_type)
6725 {
6726 TYPE_CODE (type) = TYPE_CODE_UNION;
6727 }
6728 else
6729 {
c906108c
SS
6730 TYPE_CODE (type) = TYPE_CODE_CLASS;
6731 }
6732
0cc2414c
TT
6733 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6734 TYPE_DECLARED_CLASS (type) = 1;
6735
e142c38c 6736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6737 if (attr)
6738 {
6739 TYPE_LENGTH (type) = DW_UNSND (attr);
6740 }
6741 else
6742 {
6743 TYPE_LENGTH (type) = 0;
6744 }
6745
876cecd0 6746 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6747 if (die_is_declaration (die, cu))
876cecd0 6748 TYPE_STUB (type) = 1;
a6c727b2
DJ
6749 else if (attr == NULL && die->child == NULL
6750 && producer_is_realview (cu->producer))
6751 /* RealView does not output the required DW_AT_declaration
6752 on incomplete types. */
6753 TYPE_STUB (type) = 1;
dc718098 6754
c906108c
SS
6755 /* We need to add the type field to the die immediately so we don't
6756 infinitely recurse when dealing with pointers to the structure
6757 type within the structure itself. */
1c379e20 6758 set_die_type (die, type, cu);
c906108c 6759
7e314c57
JK
6760 /* set_die_type should be already done. */
6761 set_descriptive_type (type, die, cu);
6762
e142c38c 6763 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6764 {
6765 struct field_info fi;
6766 struct die_info *child_die;
34eaf542 6767 VEC (symbolp) *template_args = NULL;
c906108c
SS
6768
6769 memset (&fi, 0, sizeof (struct field_info));
6770
639d11d3 6771 child_die = die->child;
c906108c
SS
6772
6773 while (child_die && child_die->tag)
6774 {
a9a9bd0f
DC
6775 if (child_die->tag == DW_TAG_member
6776 || child_die->tag == DW_TAG_variable)
c906108c 6777 {
a9a9bd0f
DC
6778 /* NOTE: carlton/2002-11-05: A C++ static data member
6779 should be a DW_TAG_member that is a declaration, but
6780 all versions of G++ as of this writing (so through at
6781 least 3.2.1) incorrectly generate DW_TAG_variable
6782 tags for them instead. */
e7c27a73 6783 dwarf2_add_field (&fi, child_die, cu);
c906108c 6784 }
8713b1b1 6785 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6786 {
6787 /* C++ member function. */
e7c27a73 6788 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6789 }
6790 else if (child_die->tag == DW_TAG_inheritance)
6791 {
6792 /* C++ base class field. */
e7c27a73 6793 dwarf2_add_field (&fi, child_die, cu);
c906108c 6794 }
98751a41
JK
6795 else if (child_die->tag == DW_TAG_typedef)
6796 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6797 else if (child_die->tag == DW_TAG_template_type_param
6798 || child_die->tag == DW_TAG_template_value_param)
6799 {
6800 struct symbol *arg = new_symbol (child_die, NULL, cu);
6801
6802 VEC_safe_push (symbolp, template_args, arg);
6803 }
6804
c906108c
SS
6805 child_die = sibling_die (child_die);
6806 }
6807
34eaf542
TT
6808 /* Attach template arguments to type. */
6809 if (! VEC_empty (symbolp, template_args))
6810 {
6811 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6812 TYPE_N_TEMPLATE_ARGUMENTS (type)
6813 = VEC_length (symbolp, template_args);
6814 TYPE_TEMPLATE_ARGUMENTS (type)
6815 = obstack_alloc (&objfile->objfile_obstack,
6816 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6817 * sizeof (struct symbol *)));
6818 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6819 VEC_address (symbolp, template_args),
6820 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6821 * sizeof (struct symbol *)));
6822 VEC_free (symbolp, template_args);
6823 }
6824
c906108c
SS
6825 /* Attach fields and member functions to the type. */
6826 if (fi.nfields)
e7c27a73 6827 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6828 if (fi.nfnfields)
6829 {
e7c27a73 6830 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6831
c5aa993b 6832 /* Get the type which refers to the base class (possibly this
c906108c 6833 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6834 class from the DW_AT_containing_type attribute. This use of
6835 DW_AT_containing_type is a GNU extension. */
c906108c 6836
e142c38c 6837 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6838 {
e7c27a73 6839 struct type *t = die_containing_type (die, cu);
c906108c
SS
6840
6841 TYPE_VPTR_BASETYPE (type) = t;
6842 if (type == t)
6843 {
c906108c
SS
6844 int i;
6845
6846 /* Our own class provides vtbl ptr. */
6847 for (i = TYPE_NFIELDS (t) - 1;
6848 i >= TYPE_N_BASECLASSES (t);
6849 --i)
6850 {
6851 char *fieldname = TYPE_FIELD_NAME (t, i);
6852
1168df01 6853 if (is_vtable_name (fieldname, cu))
c906108c
SS
6854 {
6855 TYPE_VPTR_FIELDNO (type) = i;
6856 break;
6857 }
6858 }
6859
6860 /* Complain if virtual function table field not found. */
6861 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6862 complaint (&symfile_complaints,
e2e0b3e5 6863 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6864 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6865 "");
c906108c
SS
6866 }
6867 else
6868 {
6869 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6870 }
6871 }
f6235d4c
EZ
6872 else if (cu->producer
6873 && strncmp (cu->producer,
6874 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6875 {
6876 /* The IBM XLC compiler does not provide direct indication
6877 of the containing type, but the vtable pointer is
6878 always named __vfp. */
6879
6880 int i;
6881
6882 for (i = TYPE_NFIELDS (type) - 1;
6883 i >= TYPE_N_BASECLASSES (type);
6884 --i)
6885 {
6886 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6887 {
6888 TYPE_VPTR_FIELDNO (type) = i;
6889 TYPE_VPTR_BASETYPE (type) = type;
6890 break;
6891 }
6892 }
6893 }
c906108c 6894 }
98751a41
JK
6895
6896 /* Copy fi.typedef_field_list linked list elements content into the
6897 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6898 if (fi.typedef_field_list)
6899 {
6900 int i = fi.typedef_field_list_count;
6901
a0d7a4ff 6902 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6903 TYPE_TYPEDEF_FIELD_ARRAY (type)
6904 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6905 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6906
6907 /* Reverse the list order to keep the debug info elements order. */
6908 while (--i >= 0)
6909 {
6910 struct typedef_field *dest, *src;
6e70227d 6911
98751a41
JK
6912 dest = &TYPE_TYPEDEF_FIELD (type, i);
6913 src = &fi.typedef_field_list->field;
6914 fi.typedef_field_list = fi.typedef_field_list->next;
6915 *dest = *src;
6916 }
6917 }
c906108c 6918 }
63d06c5c 6919
0b92b5bb
TT
6920 quirk_gcc_member_function_pointer (type, cu->objfile);
6921
0114d602 6922 do_cleanups (back_to);
f792889a 6923 return type;
c906108c
SS
6924}
6925
134d01f1
DJ
6926static void
6927process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6928{
90aeadfc 6929 struct die_info *child_die = die->child;
f792889a 6930 struct type *this_type;
c906108c 6931
f792889a
DJ
6932 this_type = get_die_type (die, cu);
6933 if (this_type == NULL)
6934 this_type = read_structure_type (die, cu);
c906108c 6935
90aeadfc
DC
6936 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6937 snapshots) has been known to create a die giving a declaration
6938 for a class that has, as a child, a die giving a definition for a
6939 nested class. So we have to process our children even if the
6940 current die is a declaration. Normally, of course, a declaration
6941 won't have any children at all. */
134d01f1 6942
90aeadfc
DC
6943 while (child_die != NULL && child_die->tag)
6944 {
6945 if (child_die->tag == DW_TAG_member
6946 || child_die->tag == DW_TAG_variable
34eaf542
TT
6947 || child_die->tag == DW_TAG_inheritance
6948 || child_die->tag == DW_TAG_template_value_param
6949 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6950 {
90aeadfc 6951 /* Do nothing. */
134d01f1 6952 }
90aeadfc
DC
6953 else
6954 process_die (child_die, cu);
134d01f1 6955
90aeadfc 6956 child_die = sibling_die (child_die);
134d01f1
DJ
6957 }
6958
fa4028e9
JB
6959 /* Do not consider external references. According to the DWARF standard,
6960 these DIEs are identified by the fact that they have no byte_size
6961 attribute, and a declaration attribute. */
6962 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6963 || !die_is_declaration (die, cu))
f792889a 6964 new_symbol (die, this_type, cu);
134d01f1
DJ
6965}
6966
6967/* Given a DW_AT_enumeration_type die, set its type. We do not
6968 complete the type's fields yet, or create any symbols. */
c906108c 6969
f792889a 6970static struct type *
134d01f1 6971read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6972{
e7c27a73 6973 struct objfile *objfile = cu->objfile;
c906108c 6974 struct type *type;
c906108c 6975 struct attribute *attr;
0114d602 6976 const char *name;
134d01f1 6977
348e048f
DE
6978 /* If the definition of this type lives in .debug_types, read that type.
6979 Don't follow DW_AT_specification though, that will take us back up
6980 the chain and we want to go down. */
6981 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6982 if (attr)
6983 {
6984 struct dwarf2_cu *type_cu = cu;
6985 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6986
348e048f 6987 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6988
6989 /* TYPE_CU may not be the same as CU.
6990 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6991 return set_die_type (die, type, cu);
6992 }
6993
c906108c
SS
6994 type = alloc_type (objfile);
6995
6996 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6997 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6998 if (name != NULL)
0114d602 6999 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7000
e142c38c 7001 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7002 if (attr)
7003 {
7004 TYPE_LENGTH (type) = DW_UNSND (attr);
7005 }
7006 else
7007 {
7008 TYPE_LENGTH (type) = 0;
7009 }
7010
137033e9
JB
7011 /* The enumeration DIE can be incomplete. In Ada, any type can be
7012 declared as private in the package spec, and then defined only
7013 inside the package body. Such types are known as Taft Amendment
7014 Types. When another package uses such a type, an incomplete DIE
7015 may be generated by the compiler. */
02eb380e 7016 if (die_is_declaration (die, cu))
876cecd0 7017 TYPE_STUB (type) = 1;
02eb380e 7018
f792889a 7019 return set_die_type (die, type, cu);
134d01f1
DJ
7020}
7021
7022/* Given a pointer to a die which begins an enumeration, process all
7023 the dies that define the members of the enumeration, and create the
7024 symbol for the enumeration type.
7025
7026 NOTE: We reverse the order of the element list. */
7027
7028static void
7029process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7030{
f792889a 7031 struct type *this_type;
134d01f1 7032
f792889a
DJ
7033 this_type = get_die_type (die, cu);
7034 if (this_type == NULL)
7035 this_type = read_enumeration_type (die, cu);
9dc481d3 7036
639d11d3 7037 if (die->child != NULL)
c906108c 7038 {
9dc481d3
DE
7039 struct die_info *child_die;
7040 struct symbol *sym;
7041 struct field *fields = NULL;
7042 int num_fields = 0;
7043 int unsigned_enum = 1;
7044 char *name;
7045
639d11d3 7046 child_die = die->child;
c906108c
SS
7047 while (child_die && child_die->tag)
7048 {
7049 if (child_die->tag != DW_TAG_enumerator)
7050 {
e7c27a73 7051 process_die (child_die, cu);
c906108c
SS
7052 }
7053 else
7054 {
39cbfefa
DJ
7055 name = dwarf2_name (child_die, cu);
7056 if (name)
c906108c 7057 {
f792889a 7058 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7059 if (SYMBOL_VALUE (sym) < 0)
7060 unsigned_enum = 0;
7061
7062 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7063 {
7064 fields = (struct field *)
7065 xrealloc (fields,
7066 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7067 * sizeof (struct field));
c906108c
SS
7068 }
7069
3567439c 7070 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7071 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7072 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7073 FIELD_BITSIZE (fields[num_fields]) = 0;
7074
7075 num_fields++;
7076 }
7077 }
7078
7079 child_die = sibling_die (child_die);
7080 }
7081
7082 if (num_fields)
7083 {
f792889a
DJ
7084 TYPE_NFIELDS (this_type) = num_fields;
7085 TYPE_FIELDS (this_type) = (struct field *)
7086 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7087 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7088 sizeof (struct field) * num_fields);
b8c9b27d 7089 xfree (fields);
c906108c
SS
7090 }
7091 if (unsigned_enum)
876cecd0 7092 TYPE_UNSIGNED (this_type) = 1;
c906108c 7093 }
134d01f1 7094
f792889a 7095 new_symbol (die, this_type, cu);
c906108c
SS
7096}
7097
7098/* Extract all information from a DW_TAG_array_type DIE and put it in
7099 the DIE's type field. For now, this only handles one dimensional
7100 arrays. */
7101
f792889a 7102static struct type *
e7c27a73 7103read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7104{
e7c27a73 7105 struct objfile *objfile = cu->objfile;
c906108c 7106 struct die_info *child_die;
7e314c57 7107 struct type *type;
c906108c
SS
7108 struct type *element_type, *range_type, *index_type;
7109 struct type **range_types = NULL;
7110 struct attribute *attr;
7111 int ndim = 0;
7112 struct cleanup *back_to;
39cbfefa 7113 char *name;
c906108c 7114
e7c27a73 7115 element_type = die_type (die, cu);
c906108c 7116
7e314c57
JK
7117 /* The die_type call above may have already set the type for this DIE. */
7118 type = get_die_type (die, cu);
7119 if (type)
7120 return type;
7121
c906108c
SS
7122 /* Irix 6.2 native cc creates array types without children for
7123 arrays with unspecified length. */
639d11d3 7124 if (die->child == NULL)
c906108c 7125 {
46bf5051 7126 index_type = objfile_type (objfile)->builtin_int;
c906108c 7127 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7128 type = create_array_type (NULL, element_type, range_type);
7129 return set_die_type (die, type, cu);
c906108c
SS
7130 }
7131
7132 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7133 child_die = die->child;
c906108c
SS
7134 while (child_die && child_die->tag)
7135 {
7136 if (child_die->tag == DW_TAG_subrange_type)
7137 {
f792889a 7138 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7139
f792889a 7140 if (child_type != NULL)
a02abb62
JB
7141 {
7142 /* The range type was succesfully read. Save it for
7143 the array type creation. */
7144 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7145 {
7146 range_types = (struct type **)
7147 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7148 * sizeof (struct type *));
7149 if (ndim == 0)
7150 make_cleanup (free_current_contents, &range_types);
7151 }
f792889a 7152 range_types[ndim++] = child_type;
a02abb62 7153 }
c906108c
SS
7154 }
7155 child_die = sibling_die (child_die);
7156 }
7157
7158 /* Dwarf2 dimensions are output from left to right, create the
7159 necessary array types in backwards order. */
7ca2d3a3 7160
c906108c 7161 type = element_type;
7ca2d3a3
DL
7162
7163 if (read_array_order (die, cu) == DW_ORD_col_major)
7164 {
7165 int i = 0;
9a619af0 7166
7ca2d3a3
DL
7167 while (i < ndim)
7168 type = create_array_type (NULL, type, range_types[i++]);
7169 }
7170 else
7171 {
7172 while (ndim-- > 0)
7173 type = create_array_type (NULL, type, range_types[ndim]);
7174 }
c906108c 7175
f5f8a009
EZ
7176 /* Understand Dwarf2 support for vector types (like they occur on
7177 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7178 array type. This is not part of the Dwarf2/3 standard yet, but a
7179 custom vendor extension. The main difference between a regular
7180 array and the vector variant is that vectors are passed by value
7181 to functions. */
e142c38c 7182 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7183 if (attr)
ea37ba09 7184 make_vector_type (type);
f5f8a009 7185
39cbfefa
DJ
7186 name = dwarf2_name (die, cu);
7187 if (name)
7188 TYPE_NAME (type) = name;
6e70227d 7189
7e314c57
JK
7190 /* Install the type in the die. */
7191 set_die_type (die, type, cu);
7192
7193 /* set_die_type should be already done. */
b4ba55a1
JB
7194 set_descriptive_type (type, die, cu);
7195
c906108c
SS
7196 do_cleanups (back_to);
7197
7e314c57 7198 return type;
c906108c
SS
7199}
7200
7ca2d3a3 7201static enum dwarf_array_dim_ordering
6e70227d 7202read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7203{
7204 struct attribute *attr;
7205
7206 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7207
7208 if (attr) return DW_SND (attr);
7209
7210 /*
7211 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7212 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7213 laid out as per normal fortran.
7214
6e70227d 7215 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7216 version checking.
7217 */
7218
905e0470
PM
7219 if (cu->language == language_fortran
7220 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7221 {
7222 return DW_ORD_row_major;
7223 }
7224
6e70227d 7225 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7226 {
7227 case array_column_major:
7228 return DW_ORD_col_major;
7229 case array_row_major:
7230 default:
7231 return DW_ORD_row_major;
7232 };
7233}
7234
72019c9c
GM
7235/* Extract all information from a DW_TAG_set_type DIE and put it in
7236 the DIE's type field. */
7237
f792889a 7238static struct type *
72019c9c
GM
7239read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7240{
7e314c57
JK
7241 struct type *domain_type, *set_type;
7242 struct attribute *attr;
f792889a 7243
7e314c57
JK
7244 domain_type = die_type (die, cu);
7245
7246 /* The die_type call above may have already set the type for this DIE. */
7247 set_type = get_die_type (die, cu);
7248 if (set_type)
7249 return set_type;
7250
7251 set_type = create_set_type (NULL, domain_type);
7252
7253 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7254 if (attr)
7255 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7256
f792889a 7257 return set_die_type (die, set_type, cu);
72019c9c 7258}
7ca2d3a3 7259
c906108c
SS
7260/* First cut: install each common block member as a global variable. */
7261
7262static void
e7c27a73 7263read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7264{
7265 struct die_info *child_die;
7266 struct attribute *attr;
7267 struct symbol *sym;
7268 CORE_ADDR base = (CORE_ADDR) 0;
7269
e142c38c 7270 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7271 if (attr)
7272 {
8e19ed76
PS
7273 /* Support the .debug_loc offsets */
7274 if (attr_form_is_block (attr))
7275 {
e7c27a73 7276 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7277 }
3690dd37 7278 else if (attr_form_is_section_offset (attr))
8e19ed76 7279 {
4d3c2250 7280 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7281 }
7282 else
7283 {
4d3c2250
KB
7284 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7285 "common block member");
8e19ed76 7286 }
c906108c 7287 }
639d11d3 7288 if (die->child != NULL)
c906108c 7289 {
639d11d3 7290 child_die = die->child;
c906108c
SS
7291 while (child_die && child_die->tag)
7292 {
e7c27a73 7293 sym = new_symbol (child_die, NULL, cu);
e142c38c 7294 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
7295 if (attr)
7296 {
d4b96c9a
JK
7297 CORE_ADDR byte_offset = 0;
7298
7299 if (attr_form_is_section_offset (attr))
7300 dwarf2_complex_location_expr_complaint ();
7301 else if (attr_form_is_constant (attr))
7302 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7303 else if (attr_form_is_block (attr))
7304 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7305 else
7306 dwarf2_complex_location_expr_complaint ();
7307
7308 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7309 add_symbol_to_list (sym, &global_symbols);
7310 }
7311 child_die = sibling_die (child_die);
7312 }
7313 }
7314}
7315
0114d602 7316/* Create a type for a C++ namespace. */
d9fa45fe 7317
0114d602
DJ
7318static struct type *
7319read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7320{
e7c27a73 7321 struct objfile *objfile = cu->objfile;
0114d602 7322 const char *previous_prefix, *name;
9219021c 7323 int is_anonymous;
0114d602
DJ
7324 struct type *type;
7325
7326 /* For extensions, reuse the type of the original namespace. */
7327 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7328 {
7329 struct die_info *ext_die;
7330 struct dwarf2_cu *ext_cu = cu;
9a619af0 7331
0114d602
DJ
7332 ext_die = dwarf2_extension (die, &ext_cu);
7333 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7334
7335 /* EXT_CU may not be the same as CU.
7336 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7337 return set_die_type (die, type, cu);
7338 }
9219021c 7339
e142c38c 7340 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7341
7342 /* Now build the name of the current namespace. */
7343
0114d602
DJ
7344 previous_prefix = determine_prefix (die, cu);
7345 if (previous_prefix[0] != '\0')
7346 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7347 previous_prefix, name, 0, cu);
0114d602
DJ
7348
7349 /* Create the type. */
7350 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7351 objfile);
7352 TYPE_NAME (type) = (char *) name;
7353 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7354
60531b24 7355 return set_die_type (die, type, cu);
0114d602
DJ
7356}
7357
7358/* Read a C++ namespace. */
7359
7360static void
7361read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7362{
7363 struct objfile *objfile = cu->objfile;
7364 const char *name;
7365 int is_anonymous;
9219021c 7366
5c4e30ca
DC
7367 /* Add a symbol associated to this if we haven't seen the namespace
7368 before. Also, add a using directive if it's an anonymous
7369 namespace. */
9219021c 7370
f2f0e013 7371 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7372 {
7373 struct type *type;
7374
0114d602 7375 type = read_type_die (die, cu);
e7c27a73 7376 new_symbol (die, type, cu);
5c4e30ca 7377
0114d602 7378 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7379 if (is_anonymous)
0114d602
DJ
7380 {
7381 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7382
c0cc3a76 7383 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7384 NULL, &objfile->objfile_obstack);
0114d602 7385 }
5c4e30ca 7386 }
9219021c 7387
639d11d3 7388 if (die->child != NULL)
d9fa45fe 7389 {
639d11d3 7390 struct die_info *child_die = die->child;
6e70227d 7391
d9fa45fe
DC
7392 while (child_die && child_die->tag)
7393 {
e7c27a73 7394 process_die (child_die, cu);
d9fa45fe
DC
7395 child_die = sibling_die (child_die);
7396 }
7397 }
38d518c9
EZ
7398}
7399
f55ee35c
JK
7400/* Read a Fortran module as type. This DIE can be only a declaration used for
7401 imported module. Still we need that type as local Fortran "use ... only"
7402 declaration imports depend on the created type in determine_prefix. */
7403
7404static struct type *
7405read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7406{
7407 struct objfile *objfile = cu->objfile;
7408 char *module_name;
7409 struct type *type;
7410
7411 module_name = dwarf2_name (die, cu);
7412 if (!module_name)
7413 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7414 die->offset);
7415 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7416
7417 /* determine_prefix uses TYPE_TAG_NAME. */
7418 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7419
7420 return set_die_type (die, type, cu);
7421}
7422
5d7cb8df
JK
7423/* Read a Fortran module. */
7424
7425static void
7426read_module (struct die_info *die, struct dwarf2_cu *cu)
7427{
7428 struct die_info *child_die = die->child;
7429
5d7cb8df
JK
7430 while (child_die && child_die->tag)
7431 {
7432 process_die (child_die, cu);
7433 child_die = sibling_die (child_die);
7434 }
7435}
7436
38d518c9
EZ
7437/* Return the name of the namespace represented by DIE. Set
7438 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7439 namespace. */
7440
7441static const char *
e142c38c 7442namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7443{
7444 struct die_info *current_die;
7445 const char *name = NULL;
7446
7447 /* Loop through the extensions until we find a name. */
7448
7449 for (current_die = die;
7450 current_die != NULL;
f2f0e013 7451 current_die = dwarf2_extension (die, &cu))
38d518c9 7452 {
e142c38c 7453 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7454 if (name != NULL)
7455 break;
7456 }
7457
7458 /* Is it an anonymous namespace? */
7459
7460 *is_anonymous = (name == NULL);
7461 if (*is_anonymous)
7462 name = "(anonymous namespace)";
7463
7464 return name;
d9fa45fe
DC
7465}
7466
c906108c
SS
7467/* Extract all information from a DW_TAG_pointer_type DIE and add to
7468 the user defined type vector. */
7469
f792889a 7470static struct type *
e7c27a73 7471read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7472{
5e2b427d 7473 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7474 struct comp_unit_head *cu_header = &cu->header;
c906108c 7475 struct type *type;
8b2dbe47
KB
7476 struct attribute *attr_byte_size;
7477 struct attribute *attr_address_class;
7478 int byte_size, addr_class;
7e314c57
JK
7479 struct type *target_type;
7480
7481 target_type = die_type (die, cu);
c906108c 7482
7e314c57
JK
7483 /* The die_type call above may have already set the type for this DIE. */
7484 type = get_die_type (die, cu);
7485 if (type)
7486 return type;
7487
7488 type = lookup_pointer_type (target_type);
8b2dbe47 7489
e142c38c 7490 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7491 if (attr_byte_size)
7492 byte_size = DW_UNSND (attr_byte_size);
c906108c 7493 else
8b2dbe47
KB
7494 byte_size = cu_header->addr_size;
7495
e142c38c 7496 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7497 if (attr_address_class)
7498 addr_class = DW_UNSND (attr_address_class);
7499 else
7500 addr_class = DW_ADDR_none;
7501
7502 /* If the pointer size or address class is different than the
7503 default, create a type variant marked as such and set the
7504 length accordingly. */
7505 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7506 {
5e2b427d 7507 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7508 {
7509 int type_flags;
7510
849957d9 7511 type_flags = gdbarch_address_class_type_flags
5e2b427d 7512 (gdbarch, byte_size, addr_class);
876cecd0
TT
7513 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7514 == 0);
8b2dbe47
KB
7515 type = make_type_with_address_space (type, type_flags);
7516 }
7517 else if (TYPE_LENGTH (type) != byte_size)
7518 {
e2e0b3e5 7519 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7520 }
6e70227d 7521 else
9a619af0
MS
7522 {
7523 /* Should we also complain about unhandled address classes? */
7524 }
c906108c 7525 }
8b2dbe47
KB
7526
7527 TYPE_LENGTH (type) = byte_size;
f792889a 7528 return set_die_type (die, type, cu);
c906108c
SS
7529}
7530
7531/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7532 the user defined type vector. */
7533
f792889a 7534static struct type *
e7c27a73 7535read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7536{
7537 struct type *type;
7538 struct type *to_type;
7539 struct type *domain;
7540
e7c27a73
DJ
7541 to_type = die_type (die, cu);
7542 domain = die_containing_type (die, cu);
0d5de010 7543
7e314c57
JK
7544 /* The calls above may have already set the type for this DIE. */
7545 type = get_die_type (die, cu);
7546 if (type)
7547 return type;
7548
0d5de010
DJ
7549 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7550 type = lookup_methodptr_type (to_type);
7551 else
7552 type = lookup_memberptr_type (to_type, domain);
c906108c 7553
f792889a 7554 return set_die_type (die, type, cu);
c906108c
SS
7555}
7556
7557/* Extract all information from a DW_TAG_reference_type DIE and add to
7558 the user defined type vector. */
7559
f792889a 7560static struct type *
e7c27a73 7561read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7562{
e7c27a73 7563 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7564 struct type *type, *target_type;
c906108c
SS
7565 struct attribute *attr;
7566
7e314c57
JK
7567 target_type = die_type (die, cu);
7568
7569 /* The die_type call above may have already set the type for this DIE. */
7570 type = get_die_type (die, cu);
7571 if (type)
7572 return type;
7573
7574 type = lookup_reference_type (target_type);
e142c38c 7575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7576 if (attr)
7577 {
7578 TYPE_LENGTH (type) = DW_UNSND (attr);
7579 }
7580 else
7581 {
107d2387 7582 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7583 }
f792889a 7584 return set_die_type (die, type, cu);
c906108c
SS
7585}
7586
f792889a 7587static struct type *
e7c27a73 7588read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7589{
f792889a 7590 struct type *base_type, *cv_type;
c906108c 7591
e7c27a73 7592 base_type = die_type (die, cu);
7e314c57
JK
7593
7594 /* The die_type call above may have already set the type for this DIE. */
7595 cv_type = get_die_type (die, cu);
7596 if (cv_type)
7597 return cv_type;
7598
f792889a
DJ
7599 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7600 return set_die_type (die, cv_type, cu);
c906108c
SS
7601}
7602
f792889a 7603static struct type *
e7c27a73 7604read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7605{
f792889a 7606 struct type *base_type, *cv_type;
c906108c 7607
e7c27a73 7608 base_type = die_type (die, cu);
7e314c57
JK
7609
7610 /* The die_type call above may have already set the type for this DIE. */
7611 cv_type = get_die_type (die, cu);
7612 if (cv_type)
7613 return cv_type;
7614
f792889a
DJ
7615 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7616 return set_die_type (die, cv_type, cu);
c906108c
SS
7617}
7618
7619/* Extract all information from a DW_TAG_string_type DIE and add to
7620 the user defined type vector. It isn't really a user defined type,
7621 but it behaves like one, with other DIE's using an AT_user_def_type
7622 attribute to reference it. */
7623
f792889a 7624static struct type *
e7c27a73 7625read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7626{
e7c27a73 7627 struct objfile *objfile = cu->objfile;
3b7538c0 7628 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7629 struct type *type, *range_type, *index_type, *char_type;
7630 struct attribute *attr;
7631 unsigned int length;
7632
e142c38c 7633 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7634 if (attr)
7635 {
7636 length = DW_UNSND (attr);
7637 }
7638 else
7639 {
b21b22e0 7640 /* check for the DW_AT_byte_size attribute */
e142c38c 7641 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7642 if (attr)
7643 {
7644 length = DW_UNSND (attr);
7645 }
7646 else
7647 {
7648 length = 1;
7649 }
c906108c 7650 }
6ccb9162 7651
46bf5051 7652 index_type = objfile_type (objfile)->builtin_int;
c906108c 7653 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7654 char_type = language_string_char_type (cu->language_defn, gdbarch);
7655 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7656
f792889a 7657 return set_die_type (die, type, cu);
c906108c
SS
7658}
7659
7660/* Handle DIES due to C code like:
7661
7662 struct foo
c5aa993b
JM
7663 {
7664 int (*funcp)(int a, long l);
7665 int b;
7666 };
c906108c
SS
7667
7668 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7669 */
c906108c 7670
f792889a 7671static struct type *
e7c27a73 7672read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7673{
7674 struct type *type; /* Type that this function returns */
7675 struct type *ftype; /* Function that returns above type */
7676 struct attribute *attr;
7677
e7c27a73 7678 type = die_type (die, cu);
7e314c57
JK
7679
7680 /* The die_type call above may have already set the type for this DIE. */
7681 ftype = get_die_type (die, cu);
7682 if (ftype)
7683 return ftype;
7684
0c8b41f1 7685 ftype = lookup_function_type (type);
c906108c 7686
5b8101ae 7687 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7688 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7689 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7690 || cu->language == language_cplus
5b8101ae
PM
7691 || cu->language == language_java
7692 || cu->language == language_pascal)
876cecd0 7693 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7694 else if (producer_is_realview (cu->producer))
7695 /* RealView does not emit DW_AT_prototyped. We can not
7696 distinguish prototyped and unprototyped functions; default to
7697 prototyped, since that is more common in modern code (and
7698 RealView warns about unprototyped functions). */
7699 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7700
c055b101
CV
7701 /* Store the calling convention in the type if it's available in
7702 the subroutine die. Otherwise set the calling convention to
7703 the default value DW_CC_normal. */
7704 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7705 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7706
7707 /* We need to add the subroutine type to the die immediately so
7708 we don't infinitely recurse when dealing with parameters
7709 declared as the same subroutine type. */
7710 set_die_type (die, ftype, cu);
6e70227d 7711
639d11d3 7712 if (die->child != NULL)
c906108c 7713 {
8072405b 7714 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7715 struct die_info *child_die;
8072405b 7716 int nparams, iparams;
c906108c
SS
7717
7718 /* Count the number of parameters.
7719 FIXME: GDB currently ignores vararg functions, but knows about
7720 vararg member functions. */
8072405b 7721 nparams = 0;
639d11d3 7722 child_die = die->child;
c906108c
SS
7723 while (child_die && child_die->tag)
7724 {
7725 if (child_die->tag == DW_TAG_formal_parameter)
7726 nparams++;
7727 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7728 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7729 child_die = sibling_die (child_die);
7730 }
7731
7732 /* Allocate storage for parameters and fill them in. */
7733 TYPE_NFIELDS (ftype) = nparams;
7734 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7735 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7736
8072405b
JK
7737 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7738 even if we error out during the parameters reading below. */
7739 for (iparams = 0; iparams < nparams; iparams++)
7740 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7741
7742 iparams = 0;
639d11d3 7743 child_die = die->child;
c906108c
SS
7744 while (child_die && child_die->tag)
7745 {
7746 if (child_die->tag == DW_TAG_formal_parameter)
7747 {
3ce3b1ba
PA
7748 struct type *arg_type;
7749
7750 /* DWARF version 2 has no clean way to discern C++
7751 static and non-static member functions. G++ helps
7752 GDB by marking the first parameter for non-static
7753 member functions (which is the this pointer) as
7754 artificial. We pass this information to
7755 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7756
7757 DWARF version 3 added DW_AT_object_pointer, which GCC
7758 4.5 does not yet generate. */
e142c38c 7759 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7760 if (attr)
7761 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7762 else
418835cc
KS
7763 {
7764 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7765
7766 /* GCC/43521: In java, the formal parameter
7767 "this" is sometimes not marked with DW_AT_artificial. */
7768 if (cu->language == language_java)
7769 {
7770 const char *name = dwarf2_name (child_die, cu);
9a619af0 7771
418835cc
KS
7772 if (name && !strcmp (name, "this"))
7773 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7774 }
7775 }
3ce3b1ba
PA
7776 arg_type = die_type (child_die, cu);
7777
7778 /* RealView does not mark THIS as const, which the testsuite
7779 expects. GCC marks THIS as const in method definitions,
7780 but not in the class specifications (GCC PR 43053). */
7781 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7782 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7783 {
7784 int is_this = 0;
7785 struct dwarf2_cu *arg_cu = cu;
7786 const char *name = dwarf2_name (child_die, cu);
7787
7788 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7789 if (attr)
7790 {
7791 /* If the compiler emits this, use it. */
7792 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7793 is_this = 1;
7794 }
7795 else if (name && strcmp (name, "this") == 0)
7796 /* Function definitions will have the argument names. */
7797 is_this = 1;
7798 else if (name == NULL && iparams == 0)
7799 /* Declarations may not have the names, so like
7800 elsewhere in GDB, assume an artificial first
7801 argument is "this". */
7802 is_this = 1;
7803
7804 if (is_this)
7805 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7806 arg_type, 0);
7807 }
7808
7809 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7810 iparams++;
7811 }
7812 child_die = sibling_die (child_die);
7813 }
7814 }
7815
76c10ea2 7816 return ftype;
c906108c
SS
7817}
7818
f792889a 7819static struct type *
e7c27a73 7820read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7821{
e7c27a73 7822 struct objfile *objfile = cu->objfile;
0114d602 7823 const char *name = NULL;
f792889a 7824 struct type *this_type;
c906108c 7825
94af9270 7826 name = dwarf2_full_name (NULL, die, cu);
f792889a 7827 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7828 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7829 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7830 set_die_type (die, this_type, cu);
7831 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7832 return this_type;
c906108c
SS
7833}
7834
7835/* Find a representation of a given base type and install
7836 it in the TYPE field of the die. */
7837
f792889a 7838static struct type *
e7c27a73 7839read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7840{
e7c27a73 7841 struct objfile *objfile = cu->objfile;
c906108c
SS
7842 struct type *type;
7843 struct attribute *attr;
7844 int encoding = 0, size = 0;
39cbfefa 7845 char *name;
6ccb9162
UW
7846 enum type_code code = TYPE_CODE_INT;
7847 int type_flags = 0;
7848 struct type *target_type = NULL;
c906108c 7849
e142c38c 7850 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7851 if (attr)
7852 {
7853 encoding = DW_UNSND (attr);
7854 }
e142c38c 7855 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7856 if (attr)
7857 {
7858 size = DW_UNSND (attr);
7859 }
39cbfefa 7860 name = dwarf2_name (die, cu);
6ccb9162 7861 if (!name)
c906108c 7862 {
6ccb9162
UW
7863 complaint (&symfile_complaints,
7864 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7865 }
6ccb9162
UW
7866
7867 switch (encoding)
c906108c 7868 {
6ccb9162
UW
7869 case DW_ATE_address:
7870 /* Turn DW_ATE_address into a void * pointer. */
7871 code = TYPE_CODE_PTR;
7872 type_flags |= TYPE_FLAG_UNSIGNED;
7873 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7874 break;
7875 case DW_ATE_boolean:
7876 code = TYPE_CODE_BOOL;
7877 type_flags |= TYPE_FLAG_UNSIGNED;
7878 break;
7879 case DW_ATE_complex_float:
7880 code = TYPE_CODE_COMPLEX;
7881 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7882 break;
7883 case DW_ATE_decimal_float:
7884 code = TYPE_CODE_DECFLOAT;
7885 break;
7886 case DW_ATE_float:
7887 code = TYPE_CODE_FLT;
7888 break;
7889 case DW_ATE_signed:
7890 break;
7891 case DW_ATE_unsigned:
7892 type_flags |= TYPE_FLAG_UNSIGNED;
7893 break;
7894 case DW_ATE_signed_char:
6e70227d 7895 if (cu->language == language_ada || cu->language == language_m2
868a0084 7896 || cu->language == language_pascal)
6ccb9162
UW
7897 code = TYPE_CODE_CHAR;
7898 break;
7899 case DW_ATE_unsigned_char:
868a0084
PM
7900 if (cu->language == language_ada || cu->language == language_m2
7901 || cu->language == language_pascal)
6ccb9162
UW
7902 code = TYPE_CODE_CHAR;
7903 type_flags |= TYPE_FLAG_UNSIGNED;
7904 break;
75079b2b
TT
7905 case DW_ATE_UTF:
7906 /* We just treat this as an integer and then recognize the
7907 type by name elsewhere. */
7908 break;
7909
6ccb9162
UW
7910 default:
7911 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7912 dwarf_type_encoding_name (encoding));
7913 break;
c906108c 7914 }
6ccb9162 7915
0114d602
DJ
7916 type = init_type (code, size, type_flags, NULL, objfile);
7917 TYPE_NAME (type) = name;
6ccb9162
UW
7918 TYPE_TARGET_TYPE (type) = target_type;
7919
0114d602 7920 if (name && strcmp (name, "char") == 0)
876cecd0 7921 TYPE_NOSIGN (type) = 1;
0114d602 7922
f792889a 7923 return set_die_type (die, type, cu);
c906108c
SS
7924}
7925
a02abb62
JB
7926/* Read the given DW_AT_subrange DIE. */
7927
f792889a 7928static struct type *
a02abb62
JB
7929read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7930{
5e2b427d 7931 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7932 struct type *base_type;
7933 struct type *range_type;
7934 struct attribute *attr;
43bbcdc2
PH
7935 LONGEST low = 0;
7936 LONGEST high = -1;
39cbfefa 7937 char *name;
43bbcdc2 7938 LONGEST negative_mask;
e77813c8 7939
a02abb62 7940 base_type = die_type (die, cu);
953ac07e
JK
7941 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7942 check_typedef (base_type);
a02abb62 7943
7e314c57
JK
7944 /* The die_type call above may have already set the type for this DIE. */
7945 range_type = get_die_type (die, cu);
7946 if (range_type)
7947 return range_type;
7948
e142c38c 7949 if (cu->language == language_fortran)
6e70227d 7950 {
a02abb62
JB
7951 /* FORTRAN implies a lower bound of 1, if not given. */
7952 low = 1;
7953 }
7954
dd5e6932
DJ
7955 /* FIXME: For variable sized arrays either of these could be
7956 a variable rather than a constant value. We'll allow it,
7957 but we don't know how to handle it. */
e142c38c 7958 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7959 if (attr)
7960 low = dwarf2_get_attr_constant_value (attr, 0);
7961
e142c38c 7962 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7963 if (attr)
6e70227d 7964 {
e77813c8 7965 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7966 {
7967 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7968 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7969 FIXME: GDB does not yet know how to handle dynamic
7970 arrays properly, treat them as arrays with unspecified
7971 length for now.
7972
7973 FIXME: jimb/2003-09-22: GDB does not really know
7974 how to handle arrays of unspecified length
7975 either; we just represent them as zero-length
7976 arrays. Choose an appropriate upper bound given
7977 the lower bound we've computed above. */
7978 high = low - 1;
7979 }
7980 else
7981 high = dwarf2_get_attr_constant_value (attr, 1);
7982 }
e77813c8
PM
7983 else
7984 {
7985 attr = dwarf2_attr (die, DW_AT_count, cu);
7986 if (attr)
7987 {
7988 int count = dwarf2_get_attr_constant_value (attr, 1);
7989 high = low + count - 1;
7990 }
7991 }
7992
7993 /* Dwarf-2 specifications explicitly allows to create subrange types
7994 without specifying a base type.
7995 In that case, the base type must be set to the type of
7996 the lower bound, upper bound or count, in that order, if any of these
7997 three attributes references an object that has a type.
7998 If no base type is found, the Dwarf-2 specifications say that
7999 a signed integer type of size equal to the size of an address should
8000 be used.
8001 For the following C code: `extern char gdb_int [];'
8002 GCC produces an empty range DIE.
8003 FIXME: muller/2010-05-28: Possible references to object for low bound,
8004 high bound or count are not yet handled by this code.
8005 */
8006 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8007 {
8008 struct objfile *objfile = cu->objfile;
8009 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8010 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8011 struct type *int_type = objfile_type (objfile)->builtin_int;
8012
8013 /* Test "int", "long int", and "long long int" objfile types,
8014 and select the first one having a size above or equal to the
8015 architecture address size. */
8016 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8017 base_type = int_type;
8018 else
8019 {
8020 int_type = objfile_type (objfile)->builtin_long;
8021 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8022 base_type = int_type;
8023 else
8024 {
8025 int_type = objfile_type (objfile)->builtin_long_long;
8026 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8027 base_type = int_type;
8028 }
8029 }
8030 }
a02abb62 8031
6e70227d 8032 negative_mask =
43bbcdc2
PH
8033 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8034 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8035 low |= negative_mask;
8036 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8037 high |= negative_mask;
8038
a02abb62
JB
8039 range_type = create_range_type (NULL, base_type, low, high);
8040
bbb0eef6
JK
8041 /* Mark arrays with dynamic length at least as an array of unspecified
8042 length. GDB could check the boundary but before it gets implemented at
8043 least allow accessing the array elements. */
8044 if (attr && attr->form == DW_FORM_block1)
8045 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8046
39cbfefa
DJ
8047 name = dwarf2_name (die, cu);
8048 if (name)
8049 TYPE_NAME (range_type) = name;
6e70227d 8050
e142c38c 8051 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8052 if (attr)
8053 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8054
7e314c57
JK
8055 set_die_type (die, range_type, cu);
8056
8057 /* set_die_type should be already done. */
b4ba55a1
JB
8058 set_descriptive_type (range_type, die, cu);
8059
7e314c57 8060 return range_type;
a02abb62 8061}
6e70227d 8062
f792889a 8063static struct type *
81a17f79
JB
8064read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8065{
8066 struct type *type;
81a17f79 8067
81a17f79
JB
8068 /* For now, we only support the C meaning of an unspecified type: void. */
8069
0114d602
DJ
8070 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8071 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8072
f792889a 8073 return set_die_type (die, type, cu);
81a17f79 8074}
a02abb62 8075
51545339
DJ
8076/* Trivial hash function for die_info: the hash value of a DIE
8077 is its offset in .debug_info for this objfile. */
8078
8079static hashval_t
8080die_hash (const void *item)
8081{
8082 const struct die_info *die = item;
9a619af0 8083
51545339
DJ
8084 return die->offset;
8085}
8086
8087/* Trivial comparison function for die_info structures: two DIEs
8088 are equal if they have the same offset. */
8089
8090static int
8091die_eq (const void *item_lhs, const void *item_rhs)
8092{
8093 const struct die_info *die_lhs = item_lhs;
8094 const struct die_info *die_rhs = item_rhs;
9a619af0 8095
51545339
DJ
8096 return die_lhs->offset == die_rhs->offset;
8097}
8098
c906108c
SS
8099/* Read a whole compilation unit into a linked list of dies. */
8100
f9aca02d 8101static struct die_info *
93311388 8102read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8103{
93311388 8104 struct die_reader_specs reader_specs;
98bfdba5 8105 int read_abbrevs = 0;
1d9ec526 8106 struct cleanup *back_to = NULL;
98bfdba5
PA
8107 struct die_info *die;
8108
8109 if (cu->dwarf2_abbrevs == NULL)
8110 {
8111 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8112 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8113 read_abbrevs = 1;
8114 }
93311388 8115
348e048f 8116 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8117 cu->die_hash
8118 = htab_create_alloc_ex (cu->header.length / 12,
8119 die_hash,
8120 die_eq,
8121 NULL,
8122 &cu->comp_unit_obstack,
8123 hashtab_obstack_allocate,
8124 dummy_obstack_deallocate);
8125
93311388
DE
8126 init_cu_die_reader (&reader_specs, cu);
8127
98bfdba5
PA
8128 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8129
8130 if (read_abbrevs)
8131 do_cleanups (back_to);
8132
8133 return die;
639d11d3
DC
8134}
8135
d97bc12b
DE
8136/* Main entry point for reading a DIE and all children.
8137 Read the DIE and dump it if requested. */
8138
8139static struct die_info *
93311388
DE
8140read_die_and_children (const struct die_reader_specs *reader,
8141 gdb_byte *info_ptr,
d97bc12b
DE
8142 gdb_byte **new_info_ptr,
8143 struct die_info *parent)
8144{
93311388 8145 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8146 new_info_ptr, parent);
8147
8148 if (dwarf2_die_debug)
8149 {
348e048f
DE
8150 fprintf_unfiltered (gdb_stdlog,
8151 "\nRead die from %s of %s:\n",
8152 reader->buffer == dwarf2_per_objfile->info.buffer
8153 ? ".debug_info"
8154 : reader->buffer == dwarf2_per_objfile->types.buffer
8155 ? ".debug_types"
8156 : "unknown section",
8157 reader->abfd->filename);
d97bc12b
DE
8158 dump_die (result, dwarf2_die_debug);
8159 }
8160
8161 return result;
8162}
8163
639d11d3
DC
8164/* Read a single die and all its descendents. Set the die's sibling
8165 field to NULL; set other fields in the die correctly, and set all
8166 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8167 location of the info_ptr after reading all of those dies. PARENT
8168 is the parent of the die in question. */
8169
8170static struct die_info *
93311388
DE
8171read_die_and_children_1 (const struct die_reader_specs *reader,
8172 gdb_byte *info_ptr,
d97bc12b
DE
8173 gdb_byte **new_info_ptr,
8174 struct die_info *parent)
639d11d3
DC
8175{
8176 struct die_info *die;
fe1b8b76 8177 gdb_byte *cur_ptr;
639d11d3
DC
8178 int has_children;
8179
93311388 8180 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8181 if (die == NULL)
8182 {
8183 *new_info_ptr = cur_ptr;
8184 return NULL;
8185 }
93311388 8186 store_in_ref_table (die, reader->cu);
639d11d3
DC
8187
8188 if (has_children)
348e048f 8189 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8190 else
8191 {
8192 die->child = NULL;
8193 *new_info_ptr = cur_ptr;
8194 }
8195
8196 die->sibling = NULL;
8197 die->parent = parent;
8198 return die;
8199}
8200
8201/* Read a die, all of its descendents, and all of its siblings; set
8202 all of the fields of all of the dies correctly. Arguments are as
8203 in read_die_and_children. */
8204
8205static struct die_info *
93311388
DE
8206read_die_and_siblings (const struct die_reader_specs *reader,
8207 gdb_byte *info_ptr,
fe1b8b76 8208 gdb_byte **new_info_ptr,
639d11d3
DC
8209 struct die_info *parent)
8210{
8211 struct die_info *first_die, *last_sibling;
fe1b8b76 8212 gdb_byte *cur_ptr;
639d11d3 8213
c906108c 8214 cur_ptr = info_ptr;
639d11d3
DC
8215 first_die = last_sibling = NULL;
8216
8217 while (1)
c906108c 8218 {
639d11d3 8219 struct die_info *die
93311388 8220 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8221
1d325ec1 8222 if (die == NULL)
c906108c 8223 {
639d11d3
DC
8224 *new_info_ptr = cur_ptr;
8225 return first_die;
c906108c 8226 }
1d325ec1
DJ
8227
8228 if (!first_die)
8229 first_die = die;
c906108c 8230 else
1d325ec1
DJ
8231 last_sibling->sibling = die;
8232
8233 last_sibling = die;
c906108c 8234 }
c906108c
SS
8235}
8236
93311388
DE
8237/* Read the die from the .debug_info section buffer. Set DIEP to
8238 point to a newly allocated die with its information, except for its
8239 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8240 whether the die has children or not. */
8241
8242static gdb_byte *
8243read_full_die (const struct die_reader_specs *reader,
8244 struct die_info **diep, gdb_byte *info_ptr,
8245 int *has_children)
8246{
8247 unsigned int abbrev_number, bytes_read, i, offset;
8248 struct abbrev_info *abbrev;
8249 struct die_info *die;
8250 struct dwarf2_cu *cu = reader->cu;
8251 bfd *abfd = reader->abfd;
8252
8253 offset = info_ptr - reader->buffer;
8254 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8255 info_ptr += bytes_read;
8256 if (!abbrev_number)
8257 {
8258 *diep = NULL;
8259 *has_children = 0;
8260 return info_ptr;
8261 }
8262
8263 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8264 if (!abbrev)
348e048f
DE
8265 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8266 abbrev_number,
8267 bfd_get_filename (abfd));
8268
93311388
DE
8269 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8270 die->offset = offset;
8271 die->tag = abbrev->tag;
8272 die->abbrev = abbrev_number;
8273
8274 die->num_attrs = abbrev->num_attrs;
8275
8276 for (i = 0; i < abbrev->num_attrs; ++i)
8277 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8278 abfd, info_ptr, cu);
8279
8280 *diep = die;
8281 *has_children = abbrev->has_children;
8282 return info_ptr;
8283}
8284
c906108c
SS
8285/* In DWARF version 2, the description of the debugging information is
8286 stored in a separate .debug_abbrev section. Before we read any
8287 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8288 in a hash table. This function also sets flags in CU describing
8289 the data found in the abbrev table. */
c906108c
SS
8290
8291static void
e7c27a73 8292dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8293{
e7c27a73 8294 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8295 gdb_byte *abbrev_ptr;
c906108c
SS
8296 struct abbrev_info *cur_abbrev;
8297 unsigned int abbrev_number, bytes_read, abbrev_name;
8298 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8299 struct attr_abbrev *cur_attrs;
8300 unsigned int allocated_attrs;
c906108c 8301
57349743 8302 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8303 obstack_init (&cu->abbrev_obstack);
8304 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8305 (ABBREV_HASH_SIZE
8306 * sizeof (struct abbrev_info *)));
8307 memset (cu->dwarf2_abbrevs, 0,
8308 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8309
be391dca
TT
8310 dwarf2_read_section (dwarf2_per_objfile->objfile,
8311 &dwarf2_per_objfile->abbrev);
dce234bc 8312 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8313 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8314 abbrev_ptr += bytes_read;
8315
f3dd6933
DJ
8316 allocated_attrs = ATTR_ALLOC_CHUNK;
8317 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8318
c906108c
SS
8319 /* loop until we reach an abbrev number of 0 */
8320 while (abbrev_number)
8321 {
f3dd6933 8322 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8323
8324 /* read in abbrev header */
8325 cur_abbrev->number = abbrev_number;
8326 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8327 abbrev_ptr += bytes_read;
8328 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8329 abbrev_ptr += 1;
8330
72bf9492
DJ
8331 if (cur_abbrev->tag == DW_TAG_namespace)
8332 cu->has_namespace_info = 1;
8333
c906108c
SS
8334 /* now read in declarations */
8335 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8336 abbrev_ptr += bytes_read;
8337 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8338 abbrev_ptr += bytes_read;
8339 while (abbrev_name)
8340 {
f3dd6933 8341 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8342 {
f3dd6933
DJ
8343 allocated_attrs += ATTR_ALLOC_CHUNK;
8344 cur_attrs
8345 = xrealloc (cur_attrs, (allocated_attrs
8346 * sizeof (struct attr_abbrev)));
c906108c 8347 }
ae038cb0
DJ
8348
8349 /* Record whether this compilation unit might have
8350 inter-compilation-unit references. If we don't know what form
8351 this attribute will have, then it might potentially be a
8352 DW_FORM_ref_addr, so we conservatively expect inter-CU
8353 references. */
8354
8355 if (abbrev_form == DW_FORM_ref_addr
8356 || abbrev_form == DW_FORM_indirect)
8357 cu->has_form_ref_addr = 1;
8358
f3dd6933
DJ
8359 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8360 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8361 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8362 abbrev_ptr += bytes_read;
8363 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8364 abbrev_ptr += bytes_read;
8365 }
8366
f3dd6933
DJ
8367 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8368 (cur_abbrev->num_attrs
8369 * sizeof (struct attr_abbrev)));
8370 memcpy (cur_abbrev->attrs, cur_attrs,
8371 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8372
c906108c 8373 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8374 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8375 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8376
8377 /* Get next abbreviation.
8378 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8379 always properly terminated with an abbrev number of 0.
8380 Exit loop if we encounter an abbreviation which we have
8381 already read (which means we are about to read the abbreviations
8382 for the next compile unit) or if the end of the abbreviation
8383 table is reached. */
dce234bc
PP
8384 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8385 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8386 break;
8387 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8388 abbrev_ptr += bytes_read;
e7c27a73 8389 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8390 break;
8391 }
f3dd6933
DJ
8392
8393 xfree (cur_attrs);
c906108c
SS
8394}
8395
f3dd6933 8396/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8397
c906108c 8398static void
f3dd6933 8399dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8400{
f3dd6933 8401 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8402
f3dd6933
DJ
8403 obstack_free (&cu->abbrev_obstack, NULL);
8404 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8405}
8406
8407/* Lookup an abbrev_info structure in the abbrev hash table. */
8408
8409static struct abbrev_info *
e7c27a73 8410dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8411{
8412 unsigned int hash_number;
8413 struct abbrev_info *abbrev;
8414
8415 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8416 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8417
8418 while (abbrev)
8419 {
8420 if (abbrev->number == number)
8421 return abbrev;
8422 else
8423 abbrev = abbrev->next;
8424 }
8425 return NULL;
8426}
8427
72bf9492
DJ
8428/* Returns nonzero if TAG represents a type that we might generate a partial
8429 symbol for. */
8430
8431static int
8432is_type_tag_for_partial (int tag)
8433{
8434 switch (tag)
8435 {
8436#if 0
8437 /* Some types that would be reasonable to generate partial symbols for,
8438 that we don't at present. */
8439 case DW_TAG_array_type:
8440 case DW_TAG_file_type:
8441 case DW_TAG_ptr_to_member_type:
8442 case DW_TAG_set_type:
8443 case DW_TAG_string_type:
8444 case DW_TAG_subroutine_type:
8445#endif
8446 case DW_TAG_base_type:
8447 case DW_TAG_class_type:
680b30c7 8448 case DW_TAG_interface_type:
72bf9492
DJ
8449 case DW_TAG_enumeration_type:
8450 case DW_TAG_structure_type:
8451 case DW_TAG_subrange_type:
8452 case DW_TAG_typedef:
8453 case DW_TAG_union_type:
8454 return 1;
8455 default:
8456 return 0;
8457 }
8458}
8459
8460/* Load all DIEs that are interesting for partial symbols into memory. */
8461
8462static struct partial_die_info *
93311388
DE
8463load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8464 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8465{
8466 struct partial_die_info *part_die;
8467 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8468 struct abbrev_info *abbrev;
8469 unsigned int bytes_read;
5afb4e99 8470 unsigned int load_all = 0;
72bf9492
DJ
8471
8472 int nesting_level = 1;
8473
8474 parent_die = NULL;
8475 last_die = NULL;
8476
5afb4e99
DJ
8477 if (cu->per_cu && cu->per_cu->load_all_dies)
8478 load_all = 1;
8479
72bf9492
DJ
8480 cu->partial_dies
8481 = htab_create_alloc_ex (cu->header.length / 12,
8482 partial_die_hash,
8483 partial_die_eq,
8484 NULL,
8485 &cu->comp_unit_obstack,
8486 hashtab_obstack_allocate,
8487 dummy_obstack_deallocate);
8488
8489 part_die = obstack_alloc (&cu->comp_unit_obstack,
8490 sizeof (struct partial_die_info));
8491
8492 while (1)
8493 {
8494 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8495
8496 /* A NULL abbrev means the end of a series of children. */
8497 if (abbrev == NULL)
8498 {
8499 if (--nesting_level == 0)
8500 {
8501 /* PART_DIE was probably the last thing allocated on the
8502 comp_unit_obstack, so we could call obstack_free
8503 here. We don't do that because the waste is small,
8504 and will be cleaned up when we're done with this
8505 compilation unit. This way, we're also more robust
8506 against other users of the comp_unit_obstack. */
8507 return first_die;
8508 }
8509 info_ptr += bytes_read;
8510 last_die = parent_die;
8511 parent_die = parent_die->die_parent;
8512 continue;
8513 }
8514
98bfdba5
PA
8515 /* Check for template arguments. We never save these; if
8516 they're seen, we just mark the parent, and go on our way. */
8517 if (parent_die != NULL
8518 && cu->language == language_cplus
8519 && (abbrev->tag == DW_TAG_template_type_param
8520 || abbrev->tag == DW_TAG_template_value_param))
8521 {
8522 parent_die->has_template_arguments = 1;
8523
8524 if (!load_all)
8525 {
8526 /* We don't need a partial DIE for the template argument. */
8527 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8528 cu);
8529 continue;
8530 }
8531 }
8532
8533 /* We only recurse into subprograms looking for template arguments.
8534 Skip their other children. */
8535 if (!load_all
8536 && cu->language == language_cplus
8537 && parent_die != NULL
8538 && parent_die->tag == DW_TAG_subprogram)
8539 {
8540 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8541 continue;
8542 }
8543
5afb4e99
DJ
8544 /* Check whether this DIE is interesting enough to save. Normally
8545 we would not be interested in members here, but there may be
8546 later variables referencing them via DW_AT_specification (for
8547 static members). */
8548 if (!load_all
8549 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8550 && abbrev->tag != DW_TAG_enumerator
8551 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8552 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8553 && abbrev->tag != DW_TAG_variable
5afb4e99 8554 && abbrev->tag != DW_TAG_namespace
f55ee35c 8555 && abbrev->tag != DW_TAG_module
5afb4e99 8556 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8557 {
8558 /* Otherwise we skip to the next sibling, if any. */
93311388 8559 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8560 continue;
8561 }
8562
93311388
DE
8563 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8564 buffer, info_ptr, cu);
72bf9492
DJ
8565
8566 /* This two-pass algorithm for processing partial symbols has a
8567 high cost in cache pressure. Thus, handle some simple cases
8568 here which cover the majority of C partial symbols. DIEs
8569 which neither have specification tags in them, nor could have
8570 specification tags elsewhere pointing at them, can simply be
8571 processed and discarded.
8572
8573 This segment is also optional; scan_partial_symbols and
8574 add_partial_symbol will handle these DIEs if we chain
8575 them in normally. When compilers which do not emit large
8576 quantities of duplicate debug information are more common,
8577 this code can probably be removed. */
8578
8579 /* Any complete simple types at the top level (pretty much all
8580 of them, for a language without namespaces), can be processed
8581 directly. */
8582 if (parent_die == NULL
8583 && part_die->has_specification == 0
8584 && part_die->is_declaration == 0
8585 && (part_die->tag == DW_TAG_typedef
8586 || part_die->tag == DW_TAG_base_type
8587 || part_die->tag == DW_TAG_subrange_type))
8588 {
8589 if (building_psymtab && part_die->name != NULL)
04a679b8 8590 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8591 VAR_DOMAIN, LOC_TYPEDEF,
8592 &cu->objfile->static_psymbols,
8593 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8594 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8595 continue;
8596 }
8597
8598 /* If we're at the second level, and we're an enumerator, and
8599 our parent has no specification (meaning possibly lives in a
8600 namespace elsewhere), then we can add the partial symbol now
8601 instead of queueing it. */
8602 if (part_die->tag == DW_TAG_enumerator
8603 && parent_die != NULL
8604 && parent_die->die_parent == NULL
8605 && parent_die->tag == DW_TAG_enumeration_type
8606 && parent_die->has_specification == 0)
8607 {
8608 if (part_die->name == NULL)
e2e0b3e5 8609 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8610 else if (building_psymtab)
04a679b8 8611 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8612 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8613 (cu->language == language_cplus
8614 || cu->language == language_java)
72bf9492
DJ
8615 ? &cu->objfile->global_psymbols
8616 : &cu->objfile->static_psymbols,
8617 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8618
93311388 8619 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8620 continue;
8621 }
8622
8623 /* We'll save this DIE so link it in. */
8624 part_die->die_parent = parent_die;
8625 part_die->die_sibling = NULL;
8626 part_die->die_child = NULL;
8627
8628 if (last_die && last_die == parent_die)
8629 last_die->die_child = part_die;
8630 else if (last_die)
8631 last_die->die_sibling = part_die;
8632
8633 last_die = part_die;
8634
8635 if (first_die == NULL)
8636 first_die = part_die;
8637
8638 /* Maybe add the DIE to the hash table. Not all DIEs that we
8639 find interesting need to be in the hash table, because we
8640 also have the parent/sibling/child chains; only those that we
8641 might refer to by offset later during partial symbol reading.
8642
8643 For now this means things that might have be the target of a
8644 DW_AT_specification, DW_AT_abstract_origin, or
8645 DW_AT_extension. DW_AT_extension will refer only to
8646 namespaces; DW_AT_abstract_origin refers to functions (and
8647 many things under the function DIE, but we do not recurse
8648 into function DIEs during partial symbol reading) and
8649 possibly variables as well; DW_AT_specification refers to
8650 declarations. Declarations ought to have the DW_AT_declaration
8651 flag. It happens that GCC forgets to put it in sometimes, but
8652 only for functions, not for types.
8653
8654 Adding more things than necessary to the hash table is harmless
8655 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8656 wasted time in find_partial_die, when we reread the compilation
8657 unit with load_all_dies set. */
72bf9492 8658
5afb4e99
DJ
8659 if (load_all
8660 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8661 || abbrev->tag == DW_TAG_variable
8662 || abbrev->tag == DW_TAG_namespace
8663 || part_die->is_declaration)
8664 {
8665 void **slot;
8666
8667 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8668 part_die->offset, INSERT);
8669 *slot = part_die;
8670 }
8671
8672 part_die = obstack_alloc (&cu->comp_unit_obstack,
8673 sizeof (struct partial_die_info));
8674
8675 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8676 we have no reason to follow the children of structures; for other
98bfdba5
PA
8677 languages we have to, so that we can get at method physnames
8678 to infer fully qualified class names, for DW_AT_specification,
8679 and for C++ template arguments. For C++, we also look one level
8680 inside functions to find template arguments (if the name of the
8681 function does not already contain the template arguments).
bc30ff58
JB
8682
8683 For Ada, we need to scan the children of subprograms and lexical
8684 blocks as well because Ada allows the definition of nested
8685 entities that could be interesting for the debugger, such as
8686 nested subprograms for instance. */
72bf9492 8687 if (last_die->has_children
5afb4e99
DJ
8688 && (load_all
8689 || last_die->tag == DW_TAG_namespace
f55ee35c 8690 || last_die->tag == DW_TAG_module
72bf9492 8691 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8692 || (cu->language == language_cplus
8693 && last_die->tag == DW_TAG_subprogram
8694 && (last_die->name == NULL
8695 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8696 || (cu->language != language_c
8697 && (last_die->tag == DW_TAG_class_type
680b30c7 8698 || last_die->tag == DW_TAG_interface_type
72bf9492 8699 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8700 || last_die->tag == DW_TAG_union_type))
8701 || (cu->language == language_ada
8702 && (last_die->tag == DW_TAG_subprogram
8703 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8704 {
8705 nesting_level++;
8706 parent_die = last_die;
8707 continue;
8708 }
8709
8710 /* Otherwise we skip to the next sibling, if any. */
93311388 8711 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8712
8713 /* Back to the top, do it again. */
8714 }
8715}
8716
c906108c
SS
8717/* Read a minimal amount of information into the minimal die structure. */
8718
fe1b8b76 8719static gdb_byte *
72bf9492
DJ
8720read_partial_die (struct partial_die_info *part_die,
8721 struct abbrev_info *abbrev,
8722 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8723 gdb_byte *buffer, gdb_byte *info_ptr,
8724 struct dwarf2_cu *cu)
c906108c 8725{
fa238c03 8726 unsigned int i;
c906108c 8727 struct attribute attr;
c5aa993b 8728 int has_low_pc_attr = 0;
c906108c
SS
8729 int has_high_pc_attr = 0;
8730
72bf9492 8731 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8732
93311388 8733 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8734
8735 info_ptr += abbrev_len;
8736
8737 if (abbrev == NULL)
8738 return info_ptr;
8739
c906108c
SS
8740 part_die->tag = abbrev->tag;
8741 part_die->has_children = abbrev->has_children;
c906108c
SS
8742
8743 for (i = 0; i < abbrev->num_attrs; ++i)
8744 {
e7c27a73 8745 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8746
8747 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8748 partial symbol table. */
c906108c
SS
8749 switch (attr.name)
8750 {
8751 case DW_AT_name:
71c25dea
TT
8752 switch (part_die->tag)
8753 {
8754 case DW_TAG_compile_unit:
348e048f 8755 case DW_TAG_type_unit:
71c25dea
TT
8756 /* Compilation units have a DW_AT_name that is a filename, not
8757 a source language identifier. */
8758 case DW_TAG_enumeration_type:
8759 case DW_TAG_enumerator:
8760 /* These tags always have simple identifiers already; no need
8761 to canonicalize them. */
8762 part_die->name = DW_STRING (&attr);
8763 break;
8764 default:
8765 part_die->name
8766 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8767 &cu->objfile->objfile_obstack);
71c25dea
TT
8768 break;
8769 }
c906108c 8770 break;
31ef98ae 8771 case DW_AT_linkage_name:
c906108c 8772 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8773 /* Note that both forms of linkage name might appear. We
8774 assume they will be the same, and we only store the last
8775 one we see. */
94af9270
KS
8776 if (cu->language == language_ada)
8777 part_die->name = DW_STRING (&attr);
c906108c
SS
8778 break;
8779 case DW_AT_low_pc:
8780 has_low_pc_attr = 1;
8781 part_die->lowpc = DW_ADDR (&attr);
8782 break;
8783 case DW_AT_high_pc:
8784 has_high_pc_attr = 1;
8785 part_die->highpc = DW_ADDR (&attr);
8786 break;
8787 case DW_AT_location:
8e19ed76
PS
8788 /* Support the .debug_loc offsets */
8789 if (attr_form_is_block (&attr))
8790 {
8791 part_die->locdesc = DW_BLOCK (&attr);
8792 }
3690dd37 8793 else if (attr_form_is_section_offset (&attr))
8e19ed76 8794 {
4d3c2250 8795 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8796 }
8797 else
8798 {
4d3c2250
KB
8799 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8800 "partial symbol information");
8e19ed76 8801 }
c906108c 8802 break;
c906108c
SS
8803 case DW_AT_external:
8804 part_die->is_external = DW_UNSND (&attr);
8805 break;
8806 case DW_AT_declaration:
8807 part_die->is_declaration = DW_UNSND (&attr);
8808 break;
8809 case DW_AT_type:
8810 part_die->has_type = 1;
8811 break;
8812 case DW_AT_abstract_origin:
8813 case DW_AT_specification:
72bf9492
DJ
8814 case DW_AT_extension:
8815 part_die->has_specification = 1;
c764a876 8816 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8817 break;
8818 case DW_AT_sibling:
8819 /* Ignore absolute siblings, they might point outside of
8820 the current compile unit. */
8821 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8822 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8823 else
93311388 8824 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8825 break;
fa4028e9
JB
8826 case DW_AT_byte_size:
8827 part_die->has_byte_size = 1;
8828 break;
68511cec
CES
8829 case DW_AT_calling_convention:
8830 /* DWARF doesn't provide a way to identify a program's source-level
8831 entry point. DW_AT_calling_convention attributes are only meant
8832 to describe functions' calling conventions.
8833
8834 However, because it's a necessary piece of information in
8835 Fortran, and because DW_CC_program is the only piece of debugging
8836 information whose definition refers to a 'main program' at all,
8837 several compilers have begun marking Fortran main programs with
8838 DW_CC_program --- even when those functions use the standard
8839 calling conventions.
8840
8841 So until DWARF specifies a way to provide this information and
8842 compilers pick up the new representation, we'll support this
8843 practice. */
8844 if (DW_UNSND (&attr) == DW_CC_program
8845 && cu->language == language_fortran)
8846 set_main_name (part_die->name);
8847 break;
c906108c
SS
8848 default:
8849 break;
8850 }
8851 }
8852
c906108c
SS
8853 /* When using the GNU linker, .gnu.linkonce. sections are used to
8854 eliminate duplicate copies of functions and vtables and such.
8855 The linker will arbitrarily choose one and discard the others.
8856 The AT_*_pc values for such functions refer to local labels in
8857 these sections. If the section from that file was discarded, the
8858 labels are not in the output, so the relocs get a value of 0.
8859 If this is a discarded function, mark the pc bounds as invalid,
8860 so that GDB will ignore it. */
8861 if (has_low_pc_attr && has_high_pc_attr
8862 && part_die->lowpc < part_die->highpc
8863 && (part_die->lowpc != 0
72dca2f5 8864 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8865 part_die->has_pc_info = 1;
85cbf3d3 8866
c906108c
SS
8867 return info_ptr;
8868}
8869
72bf9492
DJ
8870/* Find a cached partial DIE at OFFSET in CU. */
8871
8872static struct partial_die_info *
c764a876 8873find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8874{
8875 struct partial_die_info *lookup_die = NULL;
8876 struct partial_die_info part_die;
8877
8878 part_die.offset = offset;
8879 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8880
72bf9492
DJ
8881 return lookup_die;
8882}
8883
348e048f
DE
8884/* Find a partial DIE at OFFSET, which may or may not be in CU,
8885 except in the case of .debug_types DIEs which do not reference
8886 outside their CU (they do however referencing other types via
8887 DW_FORM_sig8). */
72bf9492
DJ
8888
8889static struct partial_die_info *
c764a876 8890find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8891{
5afb4e99
DJ
8892 struct dwarf2_per_cu_data *per_cu = NULL;
8893 struct partial_die_info *pd = NULL;
72bf9492 8894
348e048f
DE
8895 if (cu->per_cu->from_debug_types)
8896 {
8897 pd = find_partial_die_in_comp_unit (offset, cu);
8898 if (pd != NULL)
8899 return pd;
8900 goto not_found;
8901 }
8902
45452591 8903 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8904 {
8905 pd = find_partial_die_in_comp_unit (offset, cu);
8906 if (pd != NULL)
8907 return pd;
8908 }
72bf9492 8909
ae038cb0
DJ
8910 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8911
98bfdba5
PA
8912 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8913 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8914
8915 per_cu->cu->last_used = 0;
5afb4e99
DJ
8916 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8917
8918 if (pd == NULL && per_cu->load_all_dies == 0)
8919 {
8920 struct cleanup *back_to;
8921 struct partial_die_info comp_unit_die;
8922 struct abbrev_info *abbrev;
8923 unsigned int bytes_read;
8924 char *info_ptr;
8925
8926 per_cu->load_all_dies = 1;
8927
8928 /* Re-read the DIEs. */
8929 back_to = make_cleanup (null_cleanup, 0);
8930 if (per_cu->cu->dwarf2_abbrevs == NULL)
8931 {
8932 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8933 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8934 }
dce234bc 8935 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8936 + per_cu->cu->header.offset
8937 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8938 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8939 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8940 per_cu->cu->objfile->obfd,
8941 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8942 per_cu->cu);
8943 if (comp_unit_die.has_children)
93311388
DE
8944 load_partial_dies (per_cu->cu->objfile->obfd,
8945 dwarf2_per_objfile->info.buffer, info_ptr,
8946 0, per_cu->cu);
5afb4e99
DJ
8947 do_cleanups (back_to);
8948
8949 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8950 }
8951
348e048f
DE
8952 not_found:
8953
5afb4e99
DJ
8954 if (pd == NULL)
8955 internal_error (__FILE__, __LINE__,
c764a876 8956 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8957 offset, bfd_get_filename (cu->objfile->obfd));
8958 return pd;
72bf9492
DJ
8959}
8960
8961/* Adjust PART_DIE before generating a symbol for it. This function
8962 may set the is_external flag or change the DIE's name. */
8963
8964static void
8965fixup_partial_die (struct partial_die_info *part_die,
8966 struct dwarf2_cu *cu)
8967{
8968 /* If we found a reference attribute and the DIE has no name, try
8969 to find a name in the referred to DIE. */
8970
8971 if (part_die->name == NULL && part_die->has_specification)
8972 {
8973 struct partial_die_info *spec_die;
72bf9492 8974
10b3939b 8975 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8976
10b3939b 8977 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8978
8979 if (spec_die->name)
8980 {
8981 part_die->name = spec_die->name;
8982
8983 /* Copy DW_AT_external attribute if it is set. */
8984 if (spec_die->is_external)
8985 part_die->is_external = spec_die->is_external;
8986 }
8987 }
8988
8989 /* Set default names for some unnamed DIEs. */
8990 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8991 || part_die->tag == DW_TAG_class_type))
8992 part_die->name = "(anonymous class)";
8993
8994 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8995 part_die->name = "(anonymous namespace)";
8996
8997 if (part_die->tag == DW_TAG_structure_type
8998 || part_die->tag == DW_TAG_class_type
8999 || part_die->tag == DW_TAG_union_type)
9000 guess_structure_name (part_die, cu);
9001}
9002
a8329558 9003/* Read an attribute value described by an attribute form. */
c906108c 9004
fe1b8b76 9005static gdb_byte *
a8329558 9006read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9007 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9008 struct dwarf2_cu *cu)
c906108c 9009{
e7c27a73 9010 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9011 unsigned int bytes_read;
9012 struct dwarf_block *blk;
9013
a8329558
KW
9014 attr->form = form;
9015 switch (form)
c906108c 9016 {
c906108c 9017 case DW_FORM_ref_addr:
ae411497
TT
9018 if (cu->header.version == 2)
9019 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9020 else
9021 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9022 info_ptr += bytes_read;
9023 break;
9024 case DW_FORM_addr:
e7c27a73 9025 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9026 info_ptr += bytes_read;
c906108c
SS
9027 break;
9028 case DW_FORM_block2:
7b5a2f43 9029 blk = dwarf_alloc_block (cu);
c906108c
SS
9030 blk->size = read_2_bytes (abfd, info_ptr);
9031 info_ptr += 2;
9032 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9033 info_ptr += blk->size;
9034 DW_BLOCK (attr) = blk;
9035 break;
9036 case DW_FORM_block4:
7b5a2f43 9037 blk = dwarf_alloc_block (cu);
c906108c
SS
9038 blk->size = read_4_bytes (abfd, info_ptr);
9039 info_ptr += 4;
9040 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9041 info_ptr += blk->size;
9042 DW_BLOCK (attr) = blk;
9043 break;
9044 case DW_FORM_data2:
9045 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9046 info_ptr += 2;
9047 break;
9048 case DW_FORM_data4:
9049 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9050 info_ptr += 4;
9051 break;
9052 case DW_FORM_data8:
9053 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9054 info_ptr += 8;
9055 break;
2dc7f7b3
TT
9056 case DW_FORM_sec_offset:
9057 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9058 info_ptr += bytes_read;
9059 break;
c906108c 9060 case DW_FORM_string:
9b1c24c8 9061 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9062 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9063 info_ptr += bytes_read;
9064 break;
4bdf3d34
JJ
9065 case DW_FORM_strp:
9066 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9067 &bytes_read);
8285870a 9068 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9069 info_ptr += bytes_read;
9070 break;
2dc7f7b3 9071 case DW_FORM_exprloc:
c906108c 9072 case DW_FORM_block:
7b5a2f43 9073 blk = dwarf_alloc_block (cu);
c906108c
SS
9074 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9075 info_ptr += bytes_read;
9076 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9077 info_ptr += blk->size;
9078 DW_BLOCK (attr) = blk;
9079 break;
9080 case DW_FORM_block1:
7b5a2f43 9081 blk = dwarf_alloc_block (cu);
c906108c
SS
9082 blk->size = read_1_byte (abfd, info_ptr);
9083 info_ptr += 1;
9084 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9085 info_ptr += blk->size;
9086 DW_BLOCK (attr) = blk;
9087 break;
9088 case DW_FORM_data1:
9089 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9090 info_ptr += 1;
9091 break;
9092 case DW_FORM_flag:
9093 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9094 info_ptr += 1;
9095 break;
2dc7f7b3
TT
9096 case DW_FORM_flag_present:
9097 DW_UNSND (attr) = 1;
9098 break;
c906108c
SS
9099 case DW_FORM_sdata:
9100 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9101 info_ptr += bytes_read;
9102 break;
9103 case DW_FORM_udata:
9104 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9105 info_ptr += bytes_read;
9106 break;
9107 case DW_FORM_ref1:
10b3939b 9108 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9109 info_ptr += 1;
9110 break;
9111 case DW_FORM_ref2:
10b3939b 9112 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9113 info_ptr += 2;
9114 break;
9115 case DW_FORM_ref4:
10b3939b 9116 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9117 info_ptr += 4;
9118 break;
613e1657 9119 case DW_FORM_ref8:
10b3939b 9120 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9121 info_ptr += 8;
9122 break;
348e048f
DE
9123 case DW_FORM_sig8:
9124 /* Convert the signature to something we can record in DW_UNSND
9125 for later lookup.
9126 NOTE: This is NULL if the type wasn't found. */
9127 DW_SIGNATURED_TYPE (attr) =
9128 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9129 info_ptr += 8;
9130 break;
c906108c 9131 case DW_FORM_ref_udata:
10b3939b
DJ
9132 DW_ADDR (attr) = (cu->header.offset
9133 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9134 info_ptr += bytes_read;
9135 break;
c906108c 9136 case DW_FORM_indirect:
a8329558
KW
9137 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9138 info_ptr += bytes_read;
e7c27a73 9139 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9140 break;
c906108c 9141 default:
8a3fe4f8 9142 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9143 dwarf_form_name (form),
9144 bfd_get_filename (abfd));
c906108c 9145 }
28e94949
JB
9146
9147 /* We have seen instances where the compiler tried to emit a byte
9148 size attribute of -1 which ended up being encoded as an unsigned
9149 0xffffffff. Although 0xffffffff is technically a valid size value,
9150 an object of this size seems pretty unlikely so we can relatively
9151 safely treat these cases as if the size attribute was invalid and
9152 treat them as zero by default. */
9153 if (attr->name == DW_AT_byte_size
9154 && form == DW_FORM_data4
9155 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9156 {
9157 complaint
9158 (&symfile_complaints,
43bbcdc2
PH
9159 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9160 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9161 DW_UNSND (attr) = 0;
9162 }
28e94949 9163
c906108c
SS
9164 return info_ptr;
9165}
9166
a8329558
KW
9167/* Read an attribute described by an abbreviated attribute. */
9168
fe1b8b76 9169static gdb_byte *
a8329558 9170read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9171 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9172{
9173 attr->name = abbrev->name;
e7c27a73 9174 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9175}
9176
c906108c
SS
9177/* read dwarf information from a buffer */
9178
9179static unsigned int
fe1b8b76 9180read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9181{
fe1b8b76 9182 return bfd_get_8 (abfd, buf);
c906108c
SS
9183}
9184
9185static int
fe1b8b76 9186read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9187{
fe1b8b76 9188 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9189}
9190
9191static unsigned int
fe1b8b76 9192read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9193{
fe1b8b76 9194 return bfd_get_16 (abfd, buf);
c906108c
SS
9195}
9196
9197static int
fe1b8b76 9198read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9199{
fe1b8b76 9200 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9201}
9202
9203static unsigned int
fe1b8b76 9204read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9205{
fe1b8b76 9206 return bfd_get_32 (abfd, buf);
c906108c
SS
9207}
9208
9209static int
fe1b8b76 9210read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9211{
fe1b8b76 9212 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9213}
9214
93311388 9215static ULONGEST
fe1b8b76 9216read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9217{
fe1b8b76 9218 return bfd_get_64 (abfd, buf);
c906108c
SS
9219}
9220
9221static CORE_ADDR
fe1b8b76 9222read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9223 unsigned int *bytes_read)
c906108c 9224{
e7c27a73 9225 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9226 CORE_ADDR retval = 0;
9227
107d2387 9228 if (cu_header->signed_addr_p)
c906108c 9229 {
107d2387
AC
9230 switch (cu_header->addr_size)
9231 {
9232 case 2:
fe1b8b76 9233 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9234 break;
9235 case 4:
fe1b8b76 9236 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9237 break;
9238 case 8:
fe1b8b76 9239 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9240 break;
9241 default:
8e65ff28 9242 internal_error (__FILE__, __LINE__,
e2e0b3e5 9243 _("read_address: bad switch, signed [in module %s]"),
659b0389 9244 bfd_get_filename (abfd));
107d2387
AC
9245 }
9246 }
9247 else
9248 {
9249 switch (cu_header->addr_size)
9250 {
9251 case 2:
fe1b8b76 9252 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9253 break;
9254 case 4:
fe1b8b76 9255 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9256 break;
9257 case 8:
fe1b8b76 9258 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9259 break;
9260 default:
8e65ff28 9261 internal_error (__FILE__, __LINE__,
e2e0b3e5 9262 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9263 bfd_get_filename (abfd));
107d2387 9264 }
c906108c 9265 }
64367e0a 9266
107d2387
AC
9267 *bytes_read = cu_header->addr_size;
9268 return retval;
c906108c
SS
9269}
9270
f7ef9339 9271/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9272 specification allows the initial length to take up either 4 bytes
9273 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9274 bytes describe the length and all offsets will be 8 bytes in length
9275 instead of 4.
9276
f7ef9339
KB
9277 An older, non-standard 64-bit format is also handled by this
9278 function. The older format in question stores the initial length
9279 as an 8-byte quantity without an escape value. Lengths greater
9280 than 2^32 aren't very common which means that the initial 4 bytes
9281 is almost always zero. Since a length value of zero doesn't make
9282 sense for the 32-bit format, this initial zero can be considered to
9283 be an escape value which indicates the presence of the older 64-bit
9284 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9285 greater than 4GB. If it becomes necessary to handle lengths
9286 somewhat larger than 4GB, we could allow other small values (such
9287 as the non-sensical values of 1, 2, and 3) to also be used as
9288 escape values indicating the presence of the old format.
f7ef9339 9289
917c78fc
MK
9290 The value returned via bytes_read should be used to increment the
9291 relevant pointer after calling read_initial_length().
c764a876 9292
613e1657
KB
9293 [ Note: read_initial_length() and read_offset() are based on the
9294 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9295 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9296 from:
9297
f7ef9339 9298 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9299
613e1657
KB
9300 This document is only a draft and is subject to change. (So beware.)
9301
f7ef9339 9302 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9303 determined empirically by examining 64-bit ELF files produced by
9304 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9305
9306 - Kevin, July 16, 2002
613e1657
KB
9307 ] */
9308
9309static LONGEST
c764a876 9310read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9311{
fe1b8b76 9312 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9313
dd373385 9314 if (length == 0xffffffff)
613e1657 9315 {
fe1b8b76 9316 length = bfd_get_64 (abfd, buf + 4);
613e1657 9317 *bytes_read = 12;
613e1657 9318 }
dd373385 9319 else if (length == 0)
f7ef9339 9320 {
dd373385 9321 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9322 length = bfd_get_64 (abfd, buf);
f7ef9339 9323 *bytes_read = 8;
f7ef9339 9324 }
613e1657
KB
9325 else
9326 {
9327 *bytes_read = 4;
613e1657
KB
9328 }
9329
c764a876
DE
9330 return length;
9331}
dd373385 9332
c764a876
DE
9333/* Cover function for read_initial_length.
9334 Returns the length of the object at BUF, and stores the size of the
9335 initial length in *BYTES_READ and stores the size that offsets will be in
9336 *OFFSET_SIZE.
9337 If the initial length size is not equivalent to that specified in
9338 CU_HEADER then issue a complaint.
9339 This is useful when reading non-comp-unit headers. */
dd373385 9340
c764a876
DE
9341static LONGEST
9342read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9343 const struct comp_unit_head *cu_header,
9344 unsigned int *bytes_read,
9345 unsigned int *offset_size)
9346{
9347 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9348
9349 gdb_assert (cu_header->initial_length_size == 4
9350 || cu_header->initial_length_size == 8
9351 || cu_header->initial_length_size == 12);
9352
9353 if (cu_header->initial_length_size != *bytes_read)
9354 complaint (&symfile_complaints,
9355 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9356
c764a876 9357 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9358 return length;
613e1657
KB
9359}
9360
9361/* Read an offset from the data stream. The size of the offset is
917c78fc 9362 given by cu_header->offset_size. */
613e1657
KB
9363
9364static LONGEST
fe1b8b76 9365read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9366 unsigned int *bytes_read)
c764a876
DE
9367{
9368 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9369
c764a876
DE
9370 *bytes_read = cu_header->offset_size;
9371 return offset;
9372}
9373
9374/* Read an offset from the data stream. */
9375
9376static LONGEST
9377read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9378{
9379 LONGEST retval = 0;
9380
c764a876 9381 switch (offset_size)
613e1657
KB
9382 {
9383 case 4:
fe1b8b76 9384 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9385 break;
9386 case 8:
fe1b8b76 9387 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9388 break;
9389 default:
8e65ff28 9390 internal_error (__FILE__, __LINE__,
c764a876 9391 _("read_offset_1: bad switch [in module %s]"),
659b0389 9392 bfd_get_filename (abfd));
613e1657
KB
9393 }
9394
917c78fc 9395 return retval;
613e1657
KB
9396}
9397
fe1b8b76
JB
9398static gdb_byte *
9399read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9400{
9401 /* If the size of a host char is 8 bits, we can return a pointer
9402 to the buffer, otherwise we have to copy the data to a buffer
9403 allocated on the temporary obstack. */
4bdf3d34 9404 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9405 return buf;
c906108c
SS
9406}
9407
9408static char *
9b1c24c8 9409read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9410{
9411 /* If the size of a host char is 8 bits, we can return a pointer
9412 to the string, otherwise we have to copy the string to a buffer
9413 allocated on the temporary obstack. */
4bdf3d34 9414 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9415 if (*buf == '\0')
9416 {
9417 *bytes_read_ptr = 1;
9418 return NULL;
9419 }
fe1b8b76
JB
9420 *bytes_read_ptr = strlen ((char *) buf) + 1;
9421 return (char *) buf;
4bdf3d34
JJ
9422}
9423
9424static char *
fe1b8b76 9425read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9426 const struct comp_unit_head *cu_header,
9427 unsigned int *bytes_read_ptr)
9428{
c764a876 9429 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9430
be391dca 9431 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9432 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9433 {
8a3fe4f8 9434 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9435 bfd_get_filename (abfd));
4bdf3d34 9436 return NULL;
c906108c 9437 }
dce234bc 9438 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9439 {
8a3fe4f8 9440 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9441 bfd_get_filename (abfd));
c906108c
SS
9442 return NULL;
9443 }
4bdf3d34 9444 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9445 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9446 return NULL;
dce234bc 9447 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9448}
9449
ce5d95e1 9450static unsigned long
fe1b8b76 9451read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9452{
ce5d95e1
JB
9453 unsigned long result;
9454 unsigned int num_read;
c906108c
SS
9455 int i, shift;
9456 unsigned char byte;
9457
9458 result = 0;
9459 shift = 0;
9460 num_read = 0;
9461 i = 0;
9462 while (1)
9463 {
fe1b8b76 9464 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9465 buf++;
9466 num_read++;
ce5d95e1 9467 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9468 if ((byte & 128) == 0)
9469 {
9470 break;
9471 }
9472 shift += 7;
9473 }
9474 *bytes_read_ptr = num_read;
9475 return result;
9476}
9477
ce5d95e1 9478static long
fe1b8b76 9479read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9480{
ce5d95e1 9481 long result;
77e0b926 9482 int i, shift, num_read;
c906108c
SS
9483 unsigned char byte;
9484
9485 result = 0;
9486 shift = 0;
c906108c
SS
9487 num_read = 0;
9488 i = 0;
9489 while (1)
9490 {
fe1b8b76 9491 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9492 buf++;
9493 num_read++;
ce5d95e1 9494 result |= ((long)(byte & 127) << shift);
c906108c
SS
9495 shift += 7;
9496 if ((byte & 128) == 0)
9497 {
9498 break;
9499 }
9500 }
77e0b926
DJ
9501 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9502 result |= -(((long)1) << shift);
c906108c
SS
9503 *bytes_read_ptr = num_read;
9504 return result;
9505}
9506
4bb7a0a7
DJ
9507/* Return a pointer to just past the end of an LEB128 number in BUF. */
9508
fe1b8b76
JB
9509static gdb_byte *
9510skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9511{
9512 int byte;
9513
9514 while (1)
9515 {
fe1b8b76 9516 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9517 buf++;
9518 if ((byte & 128) == 0)
9519 return buf;
9520 }
9521}
9522
c906108c 9523static void
e142c38c 9524set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9525{
9526 switch (lang)
9527 {
9528 case DW_LANG_C89:
76bee0cc 9529 case DW_LANG_C99:
c906108c 9530 case DW_LANG_C:
e142c38c 9531 cu->language = language_c;
c906108c
SS
9532 break;
9533 case DW_LANG_C_plus_plus:
e142c38c 9534 cu->language = language_cplus;
c906108c 9535 break;
6aecb9c2
JB
9536 case DW_LANG_D:
9537 cu->language = language_d;
9538 break;
c906108c
SS
9539 case DW_LANG_Fortran77:
9540 case DW_LANG_Fortran90:
b21b22e0 9541 case DW_LANG_Fortran95:
e142c38c 9542 cu->language = language_fortran;
c906108c
SS
9543 break;
9544 case DW_LANG_Mips_Assembler:
e142c38c 9545 cu->language = language_asm;
c906108c 9546 break;
bebd888e 9547 case DW_LANG_Java:
e142c38c 9548 cu->language = language_java;
bebd888e 9549 break;
c906108c 9550 case DW_LANG_Ada83:
8aaf0b47 9551 case DW_LANG_Ada95:
bc5f45f8
JB
9552 cu->language = language_ada;
9553 break;
72019c9c
GM
9554 case DW_LANG_Modula2:
9555 cu->language = language_m2;
9556 break;
fe8e67fd
PM
9557 case DW_LANG_Pascal83:
9558 cu->language = language_pascal;
9559 break;
22566fbd
DJ
9560 case DW_LANG_ObjC:
9561 cu->language = language_objc;
9562 break;
c906108c
SS
9563 case DW_LANG_Cobol74:
9564 case DW_LANG_Cobol85:
c906108c 9565 default:
e142c38c 9566 cu->language = language_minimal;
c906108c
SS
9567 break;
9568 }
e142c38c 9569 cu->language_defn = language_def (cu->language);
c906108c
SS
9570}
9571
9572/* Return the named attribute or NULL if not there. */
9573
9574static struct attribute *
e142c38c 9575dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9576{
9577 unsigned int i;
9578 struct attribute *spec = NULL;
9579
9580 for (i = 0; i < die->num_attrs; ++i)
9581 {
9582 if (die->attrs[i].name == name)
10b3939b 9583 return &die->attrs[i];
c906108c
SS
9584 if (die->attrs[i].name == DW_AT_specification
9585 || die->attrs[i].name == DW_AT_abstract_origin)
9586 spec = &die->attrs[i];
9587 }
c906108c 9588
10b3939b 9589 if (spec)
f2f0e013
DJ
9590 {
9591 die = follow_die_ref (die, spec, &cu);
9592 return dwarf2_attr (die, name, cu);
9593 }
c5aa993b 9594
c906108c
SS
9595 return NULL;
9596}
9597
348e048f
DE
9598/* Return the named attribute or NULL if not there,
9599 but do not follow DW_AT_specification, etc.
9600 This is for use in contexts where we're reading .debug_types dies.
9601 Following DW_AT_specification, DW_AT_abstract_origin will take us
9602 back up the chain, and we want to go down. */
9603
9604static struct attribute *
9605dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9606 struct dwarf2_cu *cu)
9607{
9608 unsigned int i;
9609
9610 for (i = 0; i < die->num_attrs; ++i)
9611 if (die->attrs[i].name == name)
9612 return &die->attrs[i];
9613
9614 return NULL;
9615}
9616
05cf31d1
JB
9617/* Return non-zero iff the attribute NAME is defined for the given DIE,
9618 and holds a non-zero value. This function should only be used for
2dc7f7b3 9619 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9620
9621static int
9622dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9623{
9624 struct attribute *attr = dwarf2_attr (die, name, cu);
9625
9626 return (attr && DW_UNSND (attr));
9627}
9628
3ca72b44 9629static int
e142c38c 9630die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9631{
05cf31d1
JB
9632 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9633 which value is non-zero. However, we have to be careful with
9634 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9635 (via dwarf2_flag_true_p) follows this attribute. So we may
9636 end up accidently finding a declaration attribute that belongs
9637 to a different DIE referenced by the specification attribute,
9638 even though the given DIE does not have a declaration attribute. */
9639 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9640 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9641}
9642
63d06c5c 9643/* Return the die giving the specification for DIE, if there is
f2f0e013 9644 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9645 containing the return value on output. If there is no
9646 specification, but there is an abstract origin, that is
9647 returned. */
63d06c5c
DC
9648
9649static struct die_info *
f2f0e013 9650die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9651{
f2f0e013
DJ
9652 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9653 *spec_cu);
63d06c5c 9654
edb3359d
DJ
9655 if (spec_attr == NULL)
9656 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9657
63d06c5c
DC
9658 if (spec_attr == NULL)
9659 return NULL;
9660 else
f2f0e013 9661 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9662}
c906108c 9663
debd256d
JB
9664/* Free the line_header structure *LH, and any arrays and strings it
9665 refers to. */
9666static void
9667free_line_header (struct line_header *lh)
9668{
9669 if (lh->standard_opcode_lengths)
a8bc7b56 9670 xfree (lh->standard_opcode_lengths);
debd256d
JB
9671
9672 /* Remember that all the lh->file_names[i].name pointers are
9673 pointers into debug_line_buffer, and don't need to be freed. */
9674 if (lh->file_names)
a8bc7b56 9675 xfree (lh->file_names);
debd256d
JB
9676
9677 /* Similarly for the include directory names. */
9678 if (lh->include_dirs)
a8bc7b56 9679 xfree (lh->include_dirs);
debd256d 9680
a8bc7b56 9681 xfree (lh);
debd256d
JB
9682}
9683
9684
9685/* Add an entry to LH's include directory table. */
9686static void
9687add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9688{
debd256d
JB
9689 /* Grow the array if necessary. */
9690 if (lh->include_dirs_size == 0)
c5aa993b 9691 {
debd256d
JB
9692 lh->include_dirs_size = 1; /* for testing */
9693 lh->include_dirs = xmalloc (lh->include_dirs_size
9694 * sizeof (*lh->include_dirs));
9695 }
9696 else if (lh->num_include_dirs >= lh->include_dirs_size)
9697 {
9698 lh->include_dirs_size *= 2;
9699 lh->include_dirs = xrealloc (lh->include_dirs,
9700 (lh->include_dirs_size
9701 * sizeof (*lh->include_dirs)));
c5aa993b 9702 }
c906108c 9703
debd256d
JB
9704 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9705}
6e70227d 9706
debd256d
JB
9707
9708/* Add an entry to LH's file name table. */
9709static void
9710add_file_name (struct line_header *lh,
9711 char *name,
9712 unsigned int dir_index,
9713 unsigned int mod_time,
9714 unsigned int length)
9715{
9716 struct file_entry *fe;
9717
9718 /* Grow the array if necessary. */
9719 if (lh->file_names_size == 0)
9720 {
9721 lh->file_names_size = 1; /* for testing */
9722 lh->file_names = xmalloc (lh->file_names_size
9723 * sizeof (*lh->file_names));
9724 }
9725 else if (lh->num_file_names >= lh->file_names_size)
9726 {
9727 lh->file_names_size *= 2;
9728 lh->file_names = xrealloc (lh->file_names,
9729 (lh->file_names_size
9730 * sizeof (*lh->file_names)));
9731 }
9732
9733 fe = &lh->file_names[lh->num_file_names++];
9734 fe->name = name;
9735 fe->dir_index = dir_index;
9736 fe->mod_time = mod_time;
9737 fe->length = length;
aaa75496 9738 fe->included_p = 0;
cb1df416 9739 fe->symtab = NULL;
debd256d 9740}
6e70227d 9741
debd256d
JB
9742
9743/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9744 .debug_line, according to the endianness of ABFD. Return a pointer
9745 to a struct line_header, allocated using xmalloc.
debd256d
JB
9746
9747 NOTE: the strings in the include directory and file name tables of
9748 the returned object point into debug_line_buffer, and must not be
9749 freed. */
9750static struct line_header *
9751dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9752 struct dwarf2_cu *cu)
debd256d
JB
9753{
9754 struct cleanup *back_to;
9755 struct line_header *lh;
fe1b8b76 9756 gdb_byte *line_ptr;
c764a876 9757 unsigned int bytes_read, offset_size;
debd256d
JB
9758 int i;
9759 char *cur_dir, *cur_file;
9760
be391dca 9761 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9762 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9763 {
e2e0b3e5 9764 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9765 return 0;
9766 }
9767
a738430d
MK
9768 /* Make sure that at least there's room for the total_length field.
9769 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9770 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9771 {
4d3c2250 9772 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9773 return 0;
9774 }
9775
9776 lh = xmalloc (sizeof (*lh));
9777 memset (lh, 0, sizeof (*lh));
9778 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9779 (void *) lh);
9780
dce234bc 9781 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9782
a738430d 9783 /* Read in the header. */
6e70227d 9784 lh->total_length =
c764a876
DE
9785 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9786 &bytes_read, &offset_size);
debd256d 9787 line_ptr += bytes_read;
dce234bc
PP
9788 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9789 + dwarf2_per_objfile->line.size))
debd256d 9790 {
4d3c2250 9791 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9792 return 0;
9793 }
9794 lh->statement_program_end = line_ptr + lh->total_length;
9795 lh->version = read_2_bytes (abfd, line_ptr);
9796 line_ptr += 2;
c764a876
DE
9797 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9798 line_ptr += offset_size;
debd256d
JB
9799 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9800 line_ptr += 1;
2dc7f7b3
TT
9801 if (lh->version >= 4)
9802 {
9803 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9804 line_ptr += 1;
9805 }
9806 else
9807 lh->maximum_ops_per_instruction = 1;
9808
9809 if (lh->maximum_ops_per_instruction == 0)
9810 {
9811 lh->maximum_ops_per_instruction = 1;
9812 complaint (&symfile_complaints,
9813 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9814 }
9815
debd256d
JB
9816 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9817 line_ptr += 1;
9818 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9819 line_ptr += 1;
9820 lh->line_range = read_1_byte (abfd, line_ptr);
9821 line_ptr += 1;
9822 lh->opcode_base = read_1_byte (abfd, line_ptr);
9823 line_ptr += 1;
9824 lh->standard_opcode_lengths
fe1b8b76 9825 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9826
9827 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9828 for (i = 1; i < lh->opcode_base; ++i)
9829 {
9830 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9831 line_ptr += 1;
9832 }
9833
a738430d 9834 /* Read directory table. */
9b1c24c8 9835 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9836 {
9837 line_ptr += bytes_read;
9838 add_include_dir (lh, cur_dir);
9839 }
9840 line_ptr += bytes_read;
9841
a738430d 9842 /* Read file name table. */
9b1c24c8 9843 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9844 {
9845 unsigned int dir_index, mod_time, length;
9846
9847 line_ptr += bytes_read;
9848 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9849 line_ptr += bytes_read;
9850 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9851 line_ptr += bytes_read;
9852 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9853 line_ptr += bytes_read;
9854
9855 add_file_name (lh, cur_file, dir_index, mod_time, length);
9856 }
9857 line_ptr += bytes_read;
6e70227d 9858 lh->statement_program_start = line_ptr;
debd256d 9859
dce234bc
PP
9860 if (line_ptr > (dwarf2_per_objfile->line.buffer
9861 + dwarf2_per_objfile->line.size))
4d3c2250 9862 complaint (&symfile_complaints,
e2e0b3e5 9863 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9864
9865 discard_cleanups (back_to);
9866 return lh;
9867}
c906108c 9868
5fb290d7
DJ
9869/* This function exists to work around a bug in certain compilers
9870 (particularly GCC 2.95), in which the first line number marker of a
9871 function does not show up until after the prologue, right before
9872 the second line number marker. This function shifts ADDRESS down
9873 to the beginning of the function if necessary, and is called on
9874 addresses passed to record_line. */
9875
9876static CORE_ADDR
e142c38c 9877check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9878{
9879 struct function_range *fn;
9880
9881 /* Find the function_range containing address. */
e142c38c 9882 if (!cu->first_fn)
5fb290d7
DJ
9883 return address;
9884
e142c38c
DJ
9885 if (!cu->cached_fn)
9886 cu->cached_fn = cu->first_fn;
5fb290d7 9887
e142c38c 9888 fn = cu->cached_fn;
5fb290d7
DJ
9889 while (fn)
9890 if (fn->lowpc <= address && fn->highpc > address)
9891 goto found;
9892 else
9893 fn = fn->next;
9894
e142c38c
DJ
9895 fn = cu->first_fn;
9896 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9897 if (fn->lowpc <= address && fn->highpc > address)
9898 goto found;
9899 else
9900 fn = fn->next;
9901
9902 return address;
9903
9904 found:
9905 if (fn->seen_line)
9906 return address;
9907 if (address != fn->lowpc)
4d3c2250 9908 complaint (&symfile_complaints,
e2e0b3e5 9909 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9910 (unsigned long) address, fn->name);
5fb290d7
DJ
9911 fn->seen_line = 1;
9912 return fn->lowpc;
9913}
9914
aaa75496
JB
9915/* Decode the Line Number Program (LNP) for the given line_header
9916 structure and CU. The actual information extracted and the type
9917 of structures created from the LNP depends on the value of PST.
9918
9919 1. If PST is NULL, then this procedure uses the data from the program
9920 to create all necessary symbol tables, and their linetables.
9921 The compilation directory of the file is passed in COMP_DIR,
9922 and must not be NULL.
6e70227d 9923
aaa75496
JB
9924 2. If PST is not NULL, this procedure reads the program to determine
9925 the list of files included by the unit represented by PST, and
9926 builds all the associated partial symbol tables. In this case,
9927 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9928 is not used to compute the full name of the symtab, and therefore
9929 omitting it when building the partial symtab does not introduce
9930 the potential for inconsistency - a partial symtab and its associated
9931 symbtab having a different fullname -). */
debd256d 9932
c906108c 9933static void
debd256d 9934dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 9935 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 9936{
a8c50c1f 9937 gdb_byte *line_ptr, *extended_end;
fe1b8b76 9938 gdb_byte *line_end;
a8c50c1f 9939 unsigned int bytes_read, extended_len;
c906108c 9940 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
9941 CORE_ADDR baseaddr;
9942 struct objfile *objfile = cu->objfile;
fbf65064 9943 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 9944 const int decode_for_pst_p = (pst != NULL);
cb1df416 9945 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
9946
9947 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9948
debd256d
JB
9949 line_ptr = lh->statement_program_start;
9950 line_end = lh->statement_program_end;
c906108c
SS
9951
9952 /* Read the statement sequences until there's nothing left. */
9953 while (line_ptr < line_end)
9954 {
9955 /* state machine registers */
9956 CORE_ADDR address = 0;
9957 unsigned int file = 1;
9958 unsigned int line = 1;
9959 unsigned int column = 0;
debd256d 9960 int is_stmt = lh->default_is_stmt;
c906108c
SS
9961 int basic_block = 0;
9962 int end_sequence = 0;
fbf65064 9963 CORE_ADDR addr;
2dc7f7b3 9964 unsigned char op_index = 0;
c906108c 9965
aaa75496 9966 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 9967 {
aaa75496 9968 /* Start a subfile for the current file of the state machine. */
debd256d
JB
9969 /* lh->include_dirs and lh->file_names are 0-based, but the
9970 directory and file name numbers in the statement program
9971 are 1-based. */
9972 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 9973 char *dir = NULL;
a738430d 9974
debd256d
JB
9975 if (fe->dir_index)
9976 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
9977
9978 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
9979 }
9980
a738430d 9981 /* Decode the table. */
c5aa993b 9982 while (!end_sequence)
c906108c
SS
9983 {
9984 op_code = read_1_byte (abfd, line_ptr);
9985 line_ptr += 1;
59205f5a
JB
9986 if (line_ptr > line_end)
9987 {
9988 dwarf2_debug_line_missing_end_sequence_complaint ();
9989 break;
9990 }
9aa1fe7e 9991
debd256d 9992 if (op_code >= lh->opcode_base)
6e70227d 9993 {
a738430d 9994 /* Special operand. */
debd256d 9995 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
9996 address += (((op_index + (adj_opcode / lh->line_range))
9997 / lh->maximum_ops_per_instruction)
9998 * lh->minimum_instruction_length);
9999 op_index = ((op_index + (adj_opcode / lh->line_range))
10000 % lh->maximum_ops_per_instruction);
debd256d 10001 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10002 if (lh->num_file_names < file || file == 0)
25e43795 10003 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10004 /* For now we ignore lines not starting on an
10005 instruction boundary. */
10006 else if (op_index == 0)
25e43795
DJ
10007 {
10008 lh->file_names[file - 1].included_p = 1;
ca5f395d 10009 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10010 {
10011 if (last_subfile != current_subfile)
10012 {
10013 addr = gdbarch_addr_bits_remove (gdbarch, address);
10014 if (last_subfile)
10015 record_line (last_subfile, 0, addr);
10016 last_subfile = current_subfile;
10017 }
25e43795 10018 /* Append row to matrix using current values. */
fbf65064
UW
10019 addr = check_cu_functions (address, cu);
10020 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10021 record_line (current_subfile, line, addr);
366da635 10022 }
25e43795 10023 }
ca5f395d 10024 basic_block = 0;
9aa1fe7e
GK
10025 }
10026 else switch (op_code)
c906108c
SS
10027 {
10028 case DW_LNS_extended_op:
a8c50c1f 10029 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10030 line_ptr += bytes_read;
a8c50c1f 10031 extended_end = line_ptr + extended_len;
c906108c
SS
10032 extended_op = read_1_byte (abfd, line_ptr);
10033 line_ptr += 1;
10034 switch (extended_op)
10035 {
10036 case DW_LNE_end_sequence:
10037 end_sequence = 1;
c906108c
SS
10038 break;
10039 case DW_LNE_set_address:
e7c27a73 10040 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10041 op_index = 0;
107d2387
AC
10042 line_ptr += bytes_read;
10043 address += baseaddr;
c906108c
SS
10044 break;
10045 case DW_LNE_define_file:
debd256d
JB
10046 {
10047 char *cur_file;
10048 unsigned int dir_index, mod_time, length;
6e70227d 10049
9b1c24c8 10050 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10051 line_ptr += bytes_read;
10052 dir_index =
10053 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10054 line_ptr += bytes_read;
10055 mod_time =
10056 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10057 line_ptr += bytes_read;
10058 length =
10059 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10060 line_ptr += bytes_read;
10061 add_file_name (lh, cur_file, dir_index, mod_time, length);
10062 }
c906108c 10063 break;
d0c6ba3d
CC
10064 case DW_LNE_set_discriminator:
10065 /* The discriminator is not interesting to the debugger;
10066 just ignore it. */
10067 line_ptr = extended_end;
10068 break;
c906108c 10069 default:
4d3c2250 10070 complaint (&symfile_complaints,
e2e0b3e5 10071 _("mangled .debug_line section"));
debd256d 10072 return;
c906108c 10073 }
a8c50c1f
DJ
10074 /* Make sure that we parsed the extended op correctly. If e.g.
10075 we expected a different address size than the producer used,
10076 we may have read the wrong number of bytes. */
10077 if (line_ptr != extended_end)
10078 {
10079 complaint (&symfile_complaints,
10080 _("mangled .debug_line section"));
10081 return;
10082 }
c906108c
SS
10083 break;
10084 case DW_LNS_copy:
59205f5a 10085 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10086 dwarf2_debug_line_missing_file_complaint ();
10087 else
366da635 10088 {
25e43795 10089 lh->file_names[file - 1].included_p = 1;
ca5f395d 10090 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10091 {
10092 if (last_subfile != current_subfile)
10093 {
10094 addr = gdbarch_addr_bits_remove (gdbarch, address);
10095 if (last_subfile)
10096 record_line (last_subfile, 0, addr);
10097 last_subfile = current_subfile;
10098 }
10099 addr = check_cu_functions (address, cu);
10100 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10101 record_line (current_subfile, line, addr);
10102 }
366da635 10103 }
c906108c
SS
10104 basic_block = 0;
10105 break;
10106 case DW_LNS_advance_pc:
2dc7f7b3
TT
10107 {
10108 CORE_ADDR adjust
10109 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10110
10111 address += (((op_index + adjust)
10112 / lh->maximum_ops_per_instruction)
10113 * lh->minimum_instruction_length);
10114 op_index = ((op_index + adjust)
10115 % lh->maximum_ops_per_instruction);
10116 line_ptr += bytes_read;
10117 }
c906108c
SS
10118 break;
10119 case DW_LNS_advance_line:
10120 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10121 line_ptr += bytes_read;
10122 break;
10123 case DW_LNS_set_file:
debd256d 10124 {
a738430d
MK
10125 /* The arrays lh->include_dirs and lh->file_names are
10126 0-based, but the directory and file name numbers in
10127 the statement program are 1-based. */
debd256d 10128 struct file_entry *fe;
4f1520fb 10129 char *dir = NULL;
a738430d 10130
debd256d
JB
10131 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10132 line_ptr += bytes_read;
59205f5a 10133 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10134 dwarf2_debug_line_missing_file_complaint ();
10135 else
10136 {
10137 fe = &lh->file_names[file - 1];
10138 if (fe->dir_index)
10139 dir = lh->include_dirs[fe->dir_index - 1];
10140 if (!decode_for_pst_p)
10141 {
10142 last_subfile = current_subfile;
10143 dwarf2_start_subfile (fe->name, dir, comp_dir);
10144 }
10145 }
debd256d 10146 }
c906108c
SS
10147 break;
10148 case DW_LNS_set_column:
10149 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10150 line_ptr += bytes_read;
10151 break;
10152 case DW_LNS_negate_stmt:
10153 is_stmt = (!is_stmt);
10154 break;
10155 case DW_LNS_set_basic_block:
10156 basic_block = 1;
10157 break;
c2c6d25f
JM
10158 /* Add to the address register of the state machine the
10159 address increment value corresponding to special opcode
a738430d
MK
10160 255. I.e., this value is scaled by the minimum
10161 instruction length since special opcode 255 would have
10162 scaled the the increment. */
c906108c 10163 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10164 {
10165 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10166
10167 address += (((op_index + adjust)
10168 / lh->maximum_ops_per_instruction)
10169 * lh->minimum_instruction_length);
10170 op_index = ((op_index + adjust)
10171 % lh->maximum_ops_per_instruction);
10172 }
c906108c
SS
10173 break;
10174 case DW_LNS_fixed_advance_pc:
10175 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10176 op_index = 0;
c906108c
SS
10177 line_ptr += 2;
10178 break;
9aa1fe7e 10179 default:
a738430d
MK
10180 {
10181 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10182 int i;
a738430d 10183
debd256d 10184 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10185 {
10186 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10187 line_ptr += bytes_read;
10188 }
10189 }
c906108c
SS
10190 }
10191 }
59205f5a
JB
10192 if (lh->num_file_names < file || file == 0)
10193 dwarf2_debug_line_missing_file_complaint ();
10194 else
10195 {
10196 lh->file_names[file - 1].included_p = 1;
10197 if (!decode_for_pst_p)
fbf65064
UW
10198 {
10199 addr = gdbarch_addr_bits_remove (gdbarch, address);
10200 record_line (current_subfile, 0, addr);
10201 }
59205f5a 10202 }
c906108c 10203 }
aaa75496
JB
10204
10205 if (decode_for_pst_p)
10206 {
10207 int file_index;
10208
10209 /* Now that we're done scanning the Line Header Program, we can
10210 create the psymtab of each included file. */
10211 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10212 if (lh->file_names[file_index].included_p == 1)
10213 {
5b5464ad
JB
10214 const struct file_entry fe = lh->file_names [file_index];
10215 char *include_name = fe.name;
10216 char *dir_name = NULL;
10217 char *pst_filename = pst->filename;
10218
10219 if (fe.dir_index)
10220 dir_name = lh->include_dirs[fe.dir_index - 1];
10221
10222 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
10223 {
1754f103
MK
10224 include_name = concat (dir_name, SLASH_STRING,
10225 include_name, (char *)NULL);
5b5464ad
JB
10226 make_cleanup (xfree, include_name);
10227 }
10228
10229 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10230 {
1754f103
MK
10231 pst_filename = concat (pst->dirname, SLASH_STRING,
10232 pst_filename, (char *)NULL);
5b5464ad
JB
10233 make_cleanup (xfree, pst_filename);
10234 }
10235
10236 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
10237 dwarf2_create_include_psymtab (include_name, pst, objfile);
10238 }
10239 }
cb1df416
DJ
10240 else
10241 {
10242 /* Make sure a symtab is created for every file, even files
10243 which contain only variables (i.e. no code with associated
10244 line numbers). */
10245
10246 int i;
10247 struct file_entry *fe;
10248
10249 for (i = 0; i < lh->num_file_names; i++)
10250 {
10251 char *dir = NULL;
9a619af0 10252
cb1df416
DJ
10253 fe = &lh->file_names[i];
10254 if (fe->dir_index)
10255 dir = lh->include_dirs[fe->dir_index - 1];
10256 dwarf2_start_subfile (fe->name, dir, comp_dir);
10257
10258 /* Skip the main file; we don't need it, and it must be
10259 allocated last, so that it will show up before the
10260 non-primary symtabs in the objfile's symtab list. */
10261 if (current_subfile == first_subfile)
10262 continue;
10263
10264 if (current_subfile->symtab == NULL)
10265 current_subfile->symtab = allocate_symtab (current_subfile->name,
10266 cu->objfile);
10267 fe->symtab = current_subfile->symtab;
10268 }
10269 }
c906108c
SS
10270}
10271
10272/* Start a subfile for DWARF. FILENAME is the name of the file and
10273 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10274 or NULL if not known. COMP_DIR is the compilation directory for the
10275 linetable's compilation unit or NULL if not known.
c906108c
SS
10276 This routine tries to keep line numbers from identical absolute and
10277 relative file names in a common subfile.
10278
10279 Using the `list' example from the GDB testsuite, which resides in
10280 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10281 of /srcdir/list0.c yields the following debugging information for list0.c:
10282
c5aa993b
JM
10283 DW_AT_name: /srcdir/list0.c
10284 DW_AT_comp_dir: /compdir
357e46e7 10285 files.files[0].name: list0.h
c5aa993b 10286 files.files[0].dir: /srcdir
357e46e7 10287 files.files[1].name: list0.c
c5aa993b 10288 files.files[1].dir: /srcdir
c906108c
SS
10289
10290 The line number information for list0.c has to end up in a single
4f1520fb
FR
10291 subfile, so that `break /srcdir/list0.c:1' works as expected.
10292 start_subfile will ensure that this happens provided that we pass the
10293 concatenation of files.files[1].dir and files.files[1].name as the
10294 subfile's name. */
c906108c
SS
10295
10296static void
4f1520fb 10297dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10298{
4f1520fb
FR
10299 char *fullname;
10300
10301 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10302 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10303 second argument to start_subfile. To be consistent, we do the
10304 same here. In order not to lose the line information directory,
10305 we concatenate it to the filename when it makes sense.
10306 Note that the Dwarf3 standard says (speaking of filenames in line
10307 information): ``The directory index is ignored for file names
10308 that represent full path names''. Thus ignoring dirname in the
10309 `else' branch below isn't an issue. */
c906108c 10310
d5166ae1 10311 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10312 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10313 else
10314 fullname = filename;
c906108c 10315
4f1520fb
FR
10316 start_subfile (fullname, comp_dir);
10317
10318 if (fullname != filename)
10319 xfree (fullname);
c906108c
SS
10320}
10321
4c2df51b
DJ
10322static void
10323var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10324 struct dwarf2_cu *cu)
4c2df51b 10325{
e7c27a73
DJ
10326 struct objfile *objfile = cu->objfile;
10327 struct comp_unit_head *cu_header = &cu->header;
10328
4c2df51b
DJ
10329 /* NOTE drow/2003-01-30: There used to be a comment and some special
10330 code here to turn a symbol with DW_AT_external and a
10331 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10332 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10333 with some versions of binutils) where shared libraries could have
10334 relocations against symbols in their debug information - the
10335 minimal symbol would have the right address, but the debug info
10336 would not. It's no longer necessary, because we will explicitly
10337 apply relocations when we read in the debug information now. */
10338
10339 /* A DW_AT_location attribute with no contents indicates that a
10340 variable has been optimized away. */
10341 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10342 {
10343 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10344 return;
10345 }
10346
10347 /* Handle one degenerate form of location expression specially, to
10348 preserve GDB's previous behavior when section offsets are
10349 specified. If this is just a DW_OP_addr then mark this symbol
10350 as LOC_STATIC. */
10351
10352 if (attr_form_is_block (attr)
10353 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10354 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10355 {
891d2f0b 10356 unsigned int dummy;
4c2df51b
DJ
10357
10358 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10359 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10360 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10361 fixup_symbol_section (sym, objfile);
10362 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10363 SYMBOL_SECTION (sym));
4c2df51b
DJ
10364 return;
10365 }
10366
10367 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10368 expression evaluator, and use LOC_COMPUTED only when necessary
10369 (i.e. when the value of a register or memory location is
10370 referenced, or a thread-local block, etc.). Then again, it might
10371 not be worthwhile. I'm assuming that it isn't unless performance
10372 or memory numbers show me otherwise. */
10373
e7c27a73 10374 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10375 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10376}
10377
c906108c
SS
10378/* Given a pointer to a DWARF information entry, figure out if we need
10379 to make a symbol table entry for it, and if so, create a new entry
10380 and return a pointer to it.
10381 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10382 used the passed type.
10383 If SPACE is not NULL, use it to hold the new symbol. If it is
10384 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10385
10386static struct symbol *
34eaf542
TT
10387new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10388 struct symbol *space)
c906108c 10389{
e7c27a73 10390 struct objfile *objfile = cu->objfile;
c906108c
SS
10391 struct symbol *sym = NULL;
10392 char *name;
10393 struct attribute *attr = NULL;
10394 struct attribute *attr2 = NULL;
e142c38c 10395 CORE_ADDR baseaddr;
e37fd15a
SW
10396 struct pending **list_to_add = NULL;
10397
edb3359d 10398 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10399
10400 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10401
94af9270 10402 name = dwarf2_name (die, cu);
c906108c
SS
10403 if (name)
10404 {
94af9270 10405 const char *linkagename;
34eaf542 10406 int suppress_add = 0;
94af9270 10407
34eaf542
TT
10408 if (space)
10409 sym = space;
10410 else
10411 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10412 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10413
10414 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10415 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10416 linkagename = dwarf2_physname (name, die, cu);
10417 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10418
f55ee35c
JK
10419 /* Fortran does not have mangling standard and the mangling does differ
10420 between gfortran, iFort etc. */
10421 if (cu->language == language_fortran
b250c185 10422 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10423 symbol_set_demangled_name (&(sym->ginfo),
10424 (char *) dwarf2_full_name (name, die, cu),
10425 NULL);
f55ee35c 10426
c906108c 10427 /* Default assumptions.
c5aa993b 10428 Use the passed type or decode it from the die. */
176620f1 10429 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10430 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10431 if (type != NULL)
10432 SYMBOL_TYPE (sym) = type;
10433 else
e7c27a73 10434 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10435 attr = dwarf2_attr (die,
10436 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10437 cu);
c906108c
SS
10438 if (attr)
10439 {
10440 SYMBOL_LINE (sym) = DW_UNSND (attr);
10441 }
cb1df416 10442
edb3359d
DJ
10443 attr = dwarf2_attr (die,
10444 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10445 cu);
cb1df416
DJ
10446 if (attr)
10447 {
10448 int file_index = DW_UNSND (attr);
9a619af0 10449
cb1df416
DJ
10450 if (cu->line_header == NULL
10451 || file_index > cu->line_header->num_file_names)
10452 complaint (&symfile_complaints,
10453 _("file index out of range"));
1c3d648d 10454 else if (file_index > 0)
cb1df416
DJ
10455 {
10456 struct file_entry *fe;
9a619af0 10457
cb1df416
DJ
10458 fe = &cu->line_header->file_names[file_index - 1];
10459 SYMBOL_SYMTAB (sym) = fe->symtab;
10460 }
10461 }
10462
c906108c
SS
10463 switch (die->tag)
10464 {
10465 case DW_TAG_label:
e142c38c 10466 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10467 if (attr)
10468 {
10469 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10470 }
0f5238ed
TT
10471 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10472 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10473 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10474 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10475 break;
10476 case DW_TAG_subprogram:
10477 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10478 finish_block. */
10479 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10480 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10481 if ((attr2 && (DW_UNSND (attr2) != 0))
10482 || cu->language == language_ada)
c906108c 10483 {
2cfa0c8d
JB
10484 /* Subprograms marked external are stored as a global symbol.
10485 Ada subprograms, whether marked external or not, are always
10486 stored as a global symbol, because we want to be able to
10487 access them globally. For instance, we want to be able
10488 to break on a nested subprogram without having to
10489 specify the context. */
e37fd15a 10490 list_to_add = &global_symbols;
c906108c
SS
10491 }
10492 else
10493 {
e37fd15a 10494 list_to_add = cu->list_in_scope;
c906108c
SS
10495 }
10496 break;
edb3359d
DJ
10497 case DW_TAG_inlined_subroutine:
10498 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10499 finish_block. */
10500 SYMBOL_CLASS (sym) = LOC_BLOCK;
10501 SYMBOL_INLINED (sym) = 1;
10502 /* Do not add the symbol to any lists. It will be found via
10503 BLOCK_FUNCTION from the blockvector. */
10504 break;
34eaf542
TT
10505 case DW_TAG_template_value_param:
10506 suppress_add = 1;
10507 /* Fall through. */
c906108c 10508 case DW_TAG_variable:
254e6b9e 10509 case DW_TAG_member:
c906108c
SS
10510 /* Compilation with minimal debug info may result in variables
10511 with missing type entries. Change the misleading `void' type
10512 to something sensible. */
10513 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10514 SYMBOL_TYPE (sym)
46bf5051 10515 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10516
e142c38c 10517 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10518 /* In the case of DW_TAG_member, we should only be called for
10519 static const members. */
10520 if (die->tag == DW_TAG_member)
10521 {
3863f96c
DE
10522 /* dwarf2_add_field uses die_is_declaration,
10523 so we do the same. */
254e6b9e
DE
10524 gdb_assert (die_is_declaration (die, cu));
10525 gdb_assert (attr);
10526 }
c906108c
SS
10527 if (attr)
10528 {
e7c27a73 10529 dwarf2_const_value (attr, sym, cu);
e142c38c 10530 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10531 if (!suppress_add)
34eaf542
TT
10532 {
10533 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10534 list_to_add = &global_symbols;
34eaf542 10535 else
e37fd15a 10536 list_to_add = cu->list_in_scope;
34eaf542 10537 }
c906108c
SS
10538 break;
10539 }
e142c38c 10540 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10541 if (attr)
10542 {
e7c27a73 10543 var_decode_location (attr, sym, cu);
e142c38c 10544 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10545 if (SYMBOL_CLASS (sym) == LOC_STATIC
10546 && SYMBOL_VALUE_ADDRESS (sym) == 0
10547 && !dwarf2_per_objfile->has_section_at_zero)
10548 {
10549 /* When a static variable is eliminated by the linker,
10550 the corresponding debug information is not stripped
10551 out, but the variable address is set to null;
10552 do not add such variables into symbol table. */
10553 }
10554 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10555 {
f55ee35c
JK
10556 /* Workaround gfortran PR debug/40040 - it uses
10557 DW_AT_location for variables in -fPIC libraries which may
10558 get overriden by other libraries/executable and get
10559 a different address. Resolve it by the minimal symbol
10560 which may come from inferior's executable using copy
10561 relocation. Make this workaround only for gfortran as for
10562 other compilers GDB cannot guess the minimal symbol
10563 Fortran mangling kind. */
10564 if (cu->language == language_fortran && die->parent
10565 && die->parent->tag == DW_TAG_module
10566 && cu->producer
10567 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10568 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10569
1c809c68
TT
10570 /* A variable with DW_AT_external is never static,
10571 but it may be block-scoped. */
10572 list_to_add = (cu->list_in_scope == &file_symbols
10573 ? &global_symbols : cu->list_in_scope);
1c809c68 10574 }
c906108c 10575 else
e37fd15a 10576 list_to_add = cu->list_in_scope;
c906108c
SS
10577 }
10578 else
10579 {
10580 /* We do not know the address of this symbol.
c5aa993b
JM
10581 If it is an external symbol and we have type information
10582 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10583 The address of the variable will then be determined from
10584 the minimal symbol table whenever the variable is
10585 referenced. */
e142c38c 10586 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10587 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10588 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10589 {
0fe7935b
DJ
10590 /* A variable with DW_AT_external is never static, but it
10591 may be block-scoped. */
10592 list_to_add = (cu->list_in_scope == &file_symbols
10593 ? &global_symbols : cu->list_in_scope);
10594
c906108c 10595 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10596 }
442ddf59
JK
10597 else if (!die_is_declaration (die, cu))
10598 {
10599 /* Use the default LOC_OPTIMIZED_OUT class. */
10600 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10601 if (!suppress_add)
10602 list_to_add = cu->list_in_scope;
442ddf59 10603 }
c906108c
SS
10604 }
10605 break;
10606 case DW_TAG_formal_parameter:
edb3359d
DJ
10607 /* If we are inside a function, mark this as an argument. If
10608 not, we might be looking at an argument to an inlined function
10609 when we do not have enough information to show inlined frames;
10610 pretend it's a local variable in that case so that the user can
10611 still see it. */
10612 if (context_stack_depth > 0
10613 && context_stack[context_stack_depth - 1].name != NULL)
10614 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10615 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10616 if (attr)
10617 {
e7c27a73 10618 var_decode_location (attr, sym, cu);
c906108c 10619 }
e142c38c 10620 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10621 if (attr)
10622 {
e7c27a73 10623 dwarf2_const_value (attr, sym, cu);
c906108c 10624 }
f346a30d
PM
10625 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10626 if (attr && DW_UNSND (attr))
10627 {
10628 struct type *ref_type;
10629
10630 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10631 SYMBOL_TYPE (sym) = ref_type;
10632 }
10633
e37fd15a 10634 list_to_add = cu->list_in_scope;
c906108c
SS
10635 break;
10636 case DW_TAG_unspecified_parameters:
10637 /* From varargs functions; gdb doesn't seem to have any
10638 interest in this information, so just ignore it for now.
10639 (FIXME?) */
10640 break;
34eaf542
TT
10641 case DW_TAG_template_type_param:
10642 suppress_add = 1;
10643 /* Fall through. */
c906108c 10644 case DW_TAG_class_type:
680b30c7 10645 case DW_TAG_interface_type:
c906108c
SS
10646 case DW_TAG_structure_type:
10647 case DW_TAG_union_type:
72019c9c 10648 case DW_TAG_set_type:
c906108c
SS
10649 case DW_TAG_enumeration_type:
10650 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10651 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10652
63d06c5c 10653 {
987504bb 10654 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10655 really ever be static objects: otherwise, if you try
10656 to, say, break of a class's method and you're in a file
10657 which doesn't mention that class, it won't work unless
10658 the check for all static symbols in lookup_symbol_aux
10659 saves you. See the OtherFileClass tests in
10660 gdb.c++/namespace.exp. */
10661
e37fd15a 10662 if (!suppress_add)
34eaf542 10663 {
34eaf542
TT
10664 list_to_add = (cu->list_in_scope == &file_symbols
10665 && (cu->language == language_cplus
10666 || cu->language == language_java)
10667 ? &global_symbols : cu->list_in_scope);
34eaf542 10668 }
63d06c5c
DC
10669
10670 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 10671 defines a typedef for "foo". A Java class declaration also
5eeb2539 10672 defines a typedef for the class. */
987504bb 10673 if (cu->language == language_cplus
8c6860bb
JB
10674 || cu->language == language_java
10675 || cu->language == language_ada)
63d06c5c 10676 {
d8151005
DJ
10677 /* The symbol's name is already allocated along with
10678 this objfile, so we don't need to duplicate it for
10679 the type. */
63d06c5c 10680 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 10681 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
10682 }
10683 }
c906108c
SS
10684 break;
10685 case DW_TAG_typedef:
63d06c5c
DC
10686 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10687 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10688 list_to_add = cu->list_in_scope;
63d06c5c 10689 break;
c906108c 10690 case DW_TAG_base_type:
a02abb62 10691 case DW_TAG_subrange_type:
c906108c 10692 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10693 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10694 list_to_add = cu->list_in_scope;
c906108c
SS
10695 break;
10696 case DW_TAG_enumerator:
e142c38c 10697 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10698 if (attr)
10699 {
e7c27a73 10700 dwarf2_const_value (attr, sym, cu);
c906108c 10701 }
63d06c5c
DC
10702 {
10703 /* NOTE: carlton/2003-11-10: See comment above in the
10704 DW_TAG_class_type, etc. block. */
10705
e142c38c 10706 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10707 && (cu->language == language_cplus
10708 || cu->language == language_java)
e142c38c 10709 ? &global_symbols : cu->list_in_scope);
63d06c5c 10710 }
c906108c 10711 break;
5c4e30ca
DC
10712 case DW_TAG_namespace:
10713 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10714 list_to_add = &global_symbols;
5c4e30ca 10715 break;
c906108c
SS
10716 default:
10717 /* Not a tag we recognize. Hopefully we aren't processing
10718 trash data, but since we must specifically ignore things
10719 we don't recognize, there is nothing else we should do at
10720 this point. */
e2e0b3e5 10721 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10722 dwarf_tag_name (die->tag));
c906108c
SS
10723 break;
10724 }
df8a16a1 10725
e37fd15a
SW
10726 if (suppress_add)
10727 {
10728 sym->hash_next = objfile->template_symbols;
10729 objfile->template_symbols = sym;
10730 list_to_add = NULL;
10731 }
10732
10733 if (list_to_add != NULL)
10734 add_symbol_to_list (sym, list_to_add);
10735
df8a16a1
DJ
10736 /* For the benefit of old versions of GCC, check for anonymous
10737 namespaces based on the demangled name. */
10738 if (!processing_has_namespace_info
94af9270 10739 && cu->language == language_cplus)
df8a16a1 10740 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10741 }
10742 return (sym);
10743}
10744
34eaf542
TT
10745/* A wrapper for new_symbol_full that always allocates a new symbol. */
10746
10747static struct symbol *
10748new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10749{
10750 return new_symbol_full (die, type, cu, NULL);
10751}
10752
98bfdba5
PA
10753/* Given an attr with a DW_FORM_dataN value in host byte order,
10754 zero-extend it as appropriate for the symbol's type. The DWARF
10755 standard (v4) is not entirely clear about the meaning of using
10756 DW_FORM_dataN for a constant with a signed type, where the type is
10757 wider than the data. The conclusion of a discussion on the DWARF
10758 list was that this is unspecified. We choose to always zero-extend
10759 because that is the interpretation long in use by GCC. */
c906108c 10760
98bfdba5
PA
10761static gdb_byte *
10762dwarf2_const_value_data (struct attribute *attr, struct type *type,
10763 const char *name, struct obstack *obstack,
10764 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10765{
e7c27a73 10766 struct objfile *objfile = cu->objfile;
e17a4113
UW
10767 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10768 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10769 LONGEST l = DW_UNSND (attr);
10770
10771 if (bits < sizeof (*value) * 8)
10772 {
10773 l &= ((LONGEST) 1 << bits) - 1;
10774 *value = l;
10775 }
10776 else if (bits == sizeof (*value) * 8)
10777 *value = l;
10778 else
10779 {
10780 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10781 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10782 return bytes;
10783 }
10784
10785 return NULL;
10786}
10787
10788/* Read a constant value from an attribute. Either set *VALUE, or if
10789 the value does not fit in *VALUE, set *BYTES - either already
10790 allocated on the objfile obstack, or newly allocated on OBSTACK,
10791 or, set *BATON, if we translated the constant to a location
10792 expression. */
10793
10794static void
10795dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10796 const char *name, struct obstack *obstack,
10797 struct dwarf2_cu *cu,
10798 long *value, gdb_byte **bytes,
10799 struct dwarf2_locexpr_baton **baton)
10800{
10801 struct objfile *objfile = cu->objfile;
10802 struct comp_unit_head *cu_header = &cu->header;
c906108c 10803 struct dwarf_block *blk;
98bfdba5
PA
10804 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10805 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10806
10807 *value = 0;
10808 *bytes = NULL;
10809 *baton = NULL;
c906108c
SS
10810
10811 switch (attr->form)
10812 {
10813 case DW_FORM_addr:
ac56253d 10814 {
ac56253d
TT
10815 gdb_byte *data;
10816
98bfdba5
PA
10817 if (TYPE_LENGTH (type) != cu_header->addr_size)
10818 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10819 cu_header->addr_size,
98bfdba5 10820 TYPE_LENGTH (type));
ac56253d
TT
10821 /* Symbols of this form are reasonably rare, so we just
10822 piggyback on the existing location code rather than writing
10823 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10824 *baton = obstack_alloc (&objfile->objfile_obstack,
10825 sizeof (struct dwarf2_locexpr_baton));
10826 (*baton)->per_cu = cu->per_cu;
10827 gdb_assert ((*baton)->per_cu);
ac56253d 10828
98bfdba5
PA
10829 (*baton)->size = 2 + cu_header->addr_size;
10830 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10831 (*baton)->data = data;
ac56253d
TT
10832
10833 data[0] = DW_OP_addr;
10834 store_unsigned_integer (&data[1], cu_header->addr_size,
10835 byte_order, DW_ADDR (attr));
10836 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10837 }
c906108c 10838 break;
4ac36638 10839 case DW_FORM_string:
93b5768b 10840 case DW_FORM_strp:
98bfdba5
PA
10841 /* DW_STRING is already allocated on the objfile obstack, point
10842 directly to it. */
10843 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10844 break;
c906108c
SS
10845 case DW_FORM_block1:
10846 case DW_FORM_block2:
10847 case DW_FORM_block4:
10848 case DW_FORM_block:
2dc7f7b3 10849 case DW_FORM_exprloc:
c906108c 10850 blk = DW_BLOCK (attr);
98bfdba5
PA
10851 if (TYPE_LENGTH (type) != blk->size)
10852 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10853 TYPE_LENGTH (type));
10854 *bytes = blk->data;
c906108c 10855 break;
2df3850c
JM
10856
10857 /* The DW_AT_const_value attributes are supposed to carry the
10858 symbol's value "represented as it would be on the target
10859 architecture." By the time we get here, it's already been
10860 converted to host endianness, so we just need to sign- or
10861 zero-extend it as appropriate. */
10862 case DW_FORM_data1:
98bfdba5 10863 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10864 break;
c906108c 10865 case DW_FORM_data2:
98bfdba5 10866 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10867 break;
c906108c 10868 case DW_FORM_data4:
98bfdba5 10869 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10870 break;
c906108c 10871 case DW_FORM_data8:
98bfdba5 10872 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
10873 break;
10874
c906108c 10875 case DW_FORM_sdata:
98bfdba5 10876 *value = DW_SND (attr);
2df3850c
JM
10877 break;
10878
c906108c 10879 case DW_FORM_udata:
98bfdba5 10880 *value = DW_UNSND (attr);
c906108c 10881 break;
2df3850c 10882
c906108c 10883 default:
4d3c2250 10884 complaint (&symfile_complaints,
e2e0b3e5 10885 _("unsupported const value attribute form: '%s'"),
4d3c2250 10886 dwarf_form_name (attr->form));
98bfdba5 10887 *value = 0;
c906108c
SS
10888 break;
10889 }
10890}
10891
2df3850c 10892
98bfdba5
PA
10893/* Copy constant value from an attribute to a symbol. */
10894
2df3850c 10895static void
98bfdba5
PA
10896dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10897 struct dwarf2_cu *cu)
2df3850c 10898{
98bfdba5
PA
10899 struct objfile *objfile = cu->objfile;
10900 struct comp_unit_head *cu_header = &cu->header;
10901 long value;
10902 gdb_byte *bytes;
10903 struct dwarf2_locexpr_baton *baton;
2df3850c 10904
98bfdba5
PA
10905 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10906 SYMBOL_PRINT_NAME (sym),
10907 &objfile->objfile_obstack, cu,
10908 &value, &bytes, &baton);
2df3850c 10909
98bfdba5
PA
10910 if (baton != NULL)
10911 {
10912 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10913 SYMBOL_LOCATION_BATON (sym) = baton;
10914 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10915 }
10916 else if (bytes != NULL)
10917 {
10918 SYMBOL_VALUE_BYTES (sym) = bytes;
10919 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10920 }
10921 else
10922 {
10923 SYMBOL_VALUE (sym) = value;
10924 SYMBOL_CLASS (sym) = LOC_CONST;
10925 }
2df3850c
JM
10926}
10927
c906108c
SS
10928/* Return the type of the die in question using its DW_AT_type attribute. */
10929
10930static struct type *
e7c27a73 10931die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10932{
c906108c 10933 struct attribute *type_attr;
c906108c 10934
e142c38c 10935 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10936 if (!type_attr)
10937 {
10938 /* A missing DW_AT_type represents a void type. */
46bf5051 10939 return objfile_type (cu->objfile)->builtin_void;
c906108c 10940 }
348e048f 10941
673bfd45 10942 return lookup_die_type (die, type_attr, cu);
c906108c
SS
10943}
10944
b4ba55a1
JB
10945/* True iff CU's producer generates GNAT Ada auxiliary information
10946 that allows to find parallel types through that information instead
10947 of having to do expensive parallel lookups by type name. */
10948
10949static int
10950need_gnat_info (struct dwarf2_cu *cu)
10951{
10952 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10953 of GNAT produces this auxiliary information, without any indication
10954 that it is produced. Part of enhancing the FSF version of GNAT
10955 to produce that information will be to put in place an indicator
10956 that we can use in order to determine whether the descriptive type
10957 info is available or not. One suggestion that has been made is
10958 to use a new attribute, attached to the CU die. For now, assume
10959 that the descriptive type info is not available. */
10960 return 0;
10961}
10962
b4ba55a1
JB
10963/* Return the auxiliary type of the die in question using its
10964 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10965 attribute is not present. */
10966
10967static struct type *
10968die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10969{
b4ba55a1 10970 struct attribute *type_attr;
b4ba55a1
JB
10971
10972 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10973 if (!type_attr)
10974 return NULL;
10975
673bfd45 10976 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
10977}
10978
10979/* If DIE has a descriptive_type attribute, then set the TYPE's
10980 descriptive type accordingly. */
10981
10982static void
10983set_descriptive_type (struct type *type, struct die_info *die,
10984 struct dwarf2_cu *cu)
10985{
10986 struct type *descriptive_type = die_descriptive_type (die, cu);
10987
10988 if (descriptive_type)
10989 {
10990 ALLOCATE_GNAT_AUX_TYPE (type);
10991 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10992 }
10993}
10994
c906108c
SS
10995/* Return the containing type of the die in question using its
10996 DW_AT_containing_type attribute. */
10997
10998static struct type *
e7c27a73 10999die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11000{
c906108c 11001 struct attribute *type_attr;
c906108c 11002
e142c38c 11003 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11004 if (!type_attr)
11005 error (_("Dwarf Error: Problem turning containing type into gdb type "
11006 "[in module %s]"), cu->objfile->name);
11007
673bfd45 11008 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11009}
11010
673bfd45
DE
11011/* Look up the type of DIE in CU using its type attribute ATTR.
11012 If there is no type substitute an error marker. */
11013
c906108c 11014static struct type *
673bfd45
DE
11015lookup_die_type (struct die_info *die, struct attribute *attr,
11016 struct dwarf2_cu *cu)
c906108c 11017{
f792889a
DJ
11018 struct type *this_type;
11019
673bfd45
DE
11020 /* First see if we have it cached. */
11021
11022 if (is_ref_attr (attr))
11023 {
11024 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11025
11026 this_type = get_die_type_at_offset (offset, cu->per_cu);
11027 }
11028 else if (attr->form == DW_FORM_sig8)
11029 {
11030 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11031 struct dwarf2_cu *sig_cu;
11032 unsigned int offset;
11033
11034 /* sig_type will be NULL if the signatured type is missing from
11035 the debug info. */
11036 if (sig_type == NULL)
11037 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11038 "at 0x%x [in module %s]"),
11039 die->offset, cu->objfile->name);
11040
11041 gdb_assert (sig_type->per_cu.from_debug_types);
11042 offset = sig_type->offset + sig_type->type_offset;
11043 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11044 }
11045 else
11046 {
11047 dump_die_for_error (die);
11048 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11049 dwarf_attr_name (attr->name), cu->objfile->name);
11050 }
11051
11052 /* If not cached we need to read it in. */
11053
11054 if (this_type == NULL)
11055 {
11056 struct die_info *type_die;
11057 struct dwarf2_cu *type_cu = cu;
11058
11059 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11060 /* If the type is cached, we should have found it above. */
11061 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11062 this_type = read_type_die_1 (type_die, type_cu);
11063 }
11064
11065 /* If we still don't have a type use an error marker. */
11066
11067 if (this_type == NULL)
c906108c 11068 {
b00fdb78
TT
11069 char *message, *saved;
11070
11071 /* read_type_die already issued a complaint. */
11072 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11073 cu->objfile->name,
11074 cu->header.offset,
11075 die->offset);
11076 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11077 message, strlen (message));
11078 xfree (message);
11079
11080 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11081 }
673bfd45 11082
f792889a 11083 return this_type;
c906108c
SS
11084}
11085
673bfd45
DE
11086/* Return the type in DIE, CU.
11087 Returns NULL for invalid types.
11088
11089 This first does a lookup in the appropriate type_hash table,
11090 and only reads the die in if necessary.
11091
11092 NOTE: This can be called when reading in partial or full symbols. */
11093
f792889a 11094static struct type *
e7c27a73 11095read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11096{
f792889a
DJ
11097 struct type *this_type;
11098
11099 this_type = get_die_type (die, cu);
11100 if (this_type)
11101 return this_type;
11102
673bfd45
DE
11103 return read_type_die_1 (die, cu);
11104}
11105
11106/* Read the type in DIE, CU.
11107 Returns NULL for invalid types. */
11108
11109static struct type *
11110read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11111{
11112 struct type *this_type = NULL;
11113
c906108c
SS
11114 switch (die->tag)
11115 {
11116 case DW_TAG_class_type:
680b30c7 11117 case DW_TAG_interface_type:
c906108c
SS
11118 case DW_TAG_structure_type:
11119 case DW_TAG_union_type:
f792889a 11120 this_type = read_structure_type (die, cu);
c906108c
SS
11121 break;
11122 case DW_TAG_enumeration_type:
f792889a 11123 this_type = read_enumeration_type (die, cu);
c906108c
SS
11124 break;
11125 case DW_TAG_subprogram:
11126 case DW_TAG_subroutine_type:
edb3359d 11127 case DW_TAG_inlined_subroutine:
f792889a 11128 this_type = read_subroutine_type (die, cu);
c906108c
SS
11129 break;
11130 case DW_TAG_array_type:
f792889a 11131 this_type = read_array_type (die, cu);
c906108c 11132 break;
72019c9c 11133 case DW_TAG_set_type:
f792889a 11134 this_type = read_set_type (die, cu);
72019c9c 11135 break;
c906108c 11136 case DW_TAG_pointer_type:
f792889a 11137 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11138 break;
11139 case DW_TAG_ptr_to_member_type:
f792889a 11140 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11141 break;
11142 case DW_TAG_reference_type:
f792889a 11143 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11144 break;
11145 case DW_TAG_const_type:
f792889a 11146 this_type = read_tag_const_type (die, cu);
c906108c
SS
11147 break;
11148 case DW_TAG_volatile_type:
f792889a 11149 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11150 break;
11151 case DW_TAG_string_type:
f792889a 11152 this_type = read_tag_string_type (die, cu);
c906108c
SS
11153 break;
11154 case DW_TAG_typedef:
f792889a 11155 this_type = read_typedef (die, cu);
c906108c 11156 break;
a02abb62 11157 case DW_TAG_subrange_type:
f792889a 11158 this_type = read_subrange_type (die, cu);
a02abb62 11159 break;
c906108c 11160 case DW_TAG_base_type:
f792889a 11161 this_type = read_base_type (die, cu);
c906108c 11162 break;
81a17f79 11163 case DW_TAG_unspecified_type:
f792889a 11164 this_type = read_unspecified_type (die, cu);
81a17f79 11165 break;
0114d602
DJ
11166 case DW_TAG_namespace:
11167 this_type = read_namespace_type (die, cu);
11168 break;
f55ee35c
JK
11169 case DW_TAG_module:
11170 this_type = read_module_type (die, cu);
11171 break;
c906108c 11172 default:
a1f5b845 11173 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11174 dwarf_tag_name (die->tag));
c906108c
SS
11175 break;
11176 }
63d06c5c 11177
f792889a 11178 return this_type;
63d06c5c
DC
11179}
11180
fdde2d81 11181/* Return the name of the namespace/class that DIE is defined within,
0114d602 11182 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11183
0114d602
DJ
11184 For example, if we're within the method foo() in the following
11185 code:
11186
11187 namespace N {
11188 class C {
11189 void foo () {
11190 }
11191 };
11192 }
11193
11194 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11195
11196static char *
e142c38c 11197determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11198{
0114d602
DJ
11199 struct die_info *parent, *spec_die;
11200 struct dwarf2_cu *spec_cu;
11201 struct type *parent_type;
63d06c5c 11202
f55ee35c
JK
11203 if (cu->language != language_cplus && cu->language != language_java
11204 && cu->language != language_fortran)
0114d602
DJ
11205 return "";
11206
11207 /* We have to be careful in the presence of DW_AT_specification.
11208 For example, with GCC 3.4, given the code
11209
11210 namespace N {
11211 void foo() {
11212 // Definition of N::foo.
11213 }
11214 }
11215
11216 then we'll have a tree of DIEs like this:
11217
11218 1: DW_TAG_compile_unit
11219 2: DW_TAG_namespace // N
11220 3: DW_TAG_subprogram // declaration of N::foo
11221 4: DW_TAG_subprogram // definition of N::foo
11222 DW_AT_specification // refers to die #3
11223
11224 Thus, when processing die #4, we have to pretend that we're in
11225 the context of its DW_AT_specification, namely the contex of die
11226 #3. */
11227 spec_cu = cu;
11228 spec_die = die_specification (die, &spec_cu);
11229 if (spec_die == NULL)
11230 parent = die->parent;
11231 else
63d06c5c 11232 {
0114d602
DJ
11233 parent = spec_die->parent;
11234 cu = spec_cu;
63d06c5c 11235 }
0114d602
DJ
11236
11237 if (parent == NULL)
11238 return "";
98bfdba5
PA
11239 else if (parent->building_fullname)
11240 {
11241 const char *name;
11242 const char *parent_name;
11243
11244 /* It has been seen on RealView 2.2 built binaries,
11245 DW_TAG_template_type_param types actually _defined_ as
11246 children of the parent class:
11247
11248 enum E {};
11249 template class <class Enum> Class{};
11250 Class<enum E> class_e;
11251
11252 1: DW_TAG_class_type (Class)
11253 2: DW_TAG_enumeration_type (E)
11254 3: DW_TAG_enumerator (enum1:0)
11255 3: DW_TAG_enumerator (enum2:1)
11256 ...
11257 2: DW_TAG_template_type_param
11258 DW_AT_type DW_FORM_ref_udata (E)
11259
11260 Besides being broken debug info, it can put GDB into an
11261 infinite loop. Consider:
11262
11263 When we're building the full name for Class<E>, we'll start
11264 at Class, and go look over its template type parameters,
11265 finding E. We'll then try to build the full name of E, and
11266 reach here. We're now trying to build the full name of E,
11267 and look over the parent DIE for containing scope. In the
11268 broken case, if we followed the parent DIE of E, we'd again
11269 find Class, and once again go look at its template type
11270 arguments, etc., etc. Simply don't consider such parent die
11271 as source-level parent of this die (it can't be, the language
11272 doesn't allow it), and break the loop here. */
11273 name = dwarf2_name (die, cu);
11274 parent_name = dwarf2_name (parent, cu);
11275 complaint (&symfile_complaints,
11276 _("template param type '%s' defined within parent '%s'"),
11277 name ? name : "<unknown>",
11278 parent_name ? parent_name : "<unknown>");
11279 return "";
11280 }
63d06c5c 11281 else
0114d602
DJ
11282 switch (parent->tag)
11283 {
63d06c5c 11284 case DW_TAG_namespace:
0114d602 11285 parent_type = read_type_die (parent, cu);
acebe513
UW
11286 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11287 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11288 Work around this problem here. */
11289 if (cu->language == language_cplus
11290 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11291 return "";
0114d602
DJ
11292 /* We give a name to even anonymous namespaces. */
11293 return TYPE_TAG_NAME (parent_type);
63d06c5c 11294 case DW_TAG_class_type:
680b30c7 11295 case DW_TAG_interface_type:
63d06c5c 11296 case DW_TAG_structure_type:
0114d602 11297 case DW_TAG_union_type:
f55ee35c 11298 case DW_TAG_module:
0114d602
DJ
11299 parent_type = read_type_die (parent, cu);
11300 if (TYPE_TAG_NAME (parent_type) != NULL)
11301 return TYPE_TAG_NAME (parent_type);
11302 else
11303 /* An anonymous structure is only allowed non-static data
11304 members; no typedefs, no member functions, et cetera.
11305 So it does not need a prefix. */
11306 return "";
63d06c5c 11307 default:
8176b9b8 11308 return determine_prefix (parent, cu);
63d06c5c 11309 }
63d06c5c
DC
11310}
11311
987504bb
JJ
11312/* Return a newly-allocated string formed by concatenating PREFIX and
11313 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11314 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11315 perform an obconcat, otherwise allocate storage for the result. The CU argument
11316 is used to determine the language and hence, the appropriate separator. */
11317
f55ee35c 11318#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11319
11320static char *
f55ee35c
JK
11321typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11322 int physname, struct dwarf2_cu *cu)
63d06c5c 11323{
f55ee35c 11324 const char *lead = "";
5c315b68 11325 const char *sep;
63d06c5c 11326
987504bb
JJ
11327 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11328 sep = "";
11329 else if (cu->language == language_java)
11330 sep = ".";
f55ee35c
JK
11331 else if (cu->language == language_fortran && physname)
11332 {
11333 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11334 DW_AT_MIPS_linkage_name is preferred and used instead. */
11335
11336 lead = "__";
11337 sep = "_MOD_";
11338 }
987504bb
JJ
11339 else
11340 sep = "::";
63d06c5c 11341
6dd47d34
DE
11342 if (prefix == NULL)
11343 prefix = "";
11344 if (suffix == NULL)
11345 suffix = "";
11346
987504bb
JJ
11347 if (obs == NULL)
11348 {
11349 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11350
f55ee35c
JK
11351 strcpy (retval, lead);
11352 strcat (retval, prefix);
6dd47d34
DE
11353 strcat (retval, sep);
11354 strcat (retval, suffix);
63d06c5c
DC
11355 return retval;
11356 }
987504bb
JJ
11357 else
11358 {
11359 /* We have an obstack. */
f55ee35c 11360 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11361 }
63d06c5c
DC
11362}
11363
c906108c
SS
11364/* Return sibling of die, NULL if no sibling. */
11365
f9aca02d 11366static struct die_info *
fba45db2 11367sibling_die (struct die_info *die)
c906108c 11368{
639d11d3 11369 return die->sibling;
c906108c
SS
11370}
11371
71c25dea
TT
11372/* Get name of a die, return NULL if not found. */
11373
11374static char *
11375dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11376 struct obstack *obstack)
11377{
11378 if (name && cu->language == language_cplus)
11379 {
11380 char *canon_name = cp_canonicalize_string (name);
11381
11382 if (canon_name != NULL)
11383 {
11384 if (strcmp (canon_name, name) != 0)
11385 name = obsavestring (canon_name, strlen (canon_name),
11386 obstack);
11387 xfree (canon_name);
11388 }
11389 }
11390
11391 return name;
c906108c
SS
11392}
11393
9219021c
DC
11394/* Get name of a die, return NULL if not found. */
11395
11396static char *
e142c38c 11397dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11398{
11399 struct attribute *attr;
11400
e142c38c 11401 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11402 if (!attr || !DW_STRING (attr))
11403 return NULL;
11404
11405 switch (die->tag)
11406 {
11407 case DW_TAG_compile_unit:
11408 /* Compilation units have a DW_AT_name that is a filename, not
11409 a source language identifier. */
11410 case DW_TAG_enumeration_type:
11411 case DW_TAG_enumerator:
11412 /* These tags always have simple identifiers already; no need
11413 to canonicalize them. */
11414 return DW_STRING (attr);
907af001 11415
418835cc
KS
11416 case DW_TAG_subprogram:
11417 /* Java constructors will all be named "<init>", so return
11418 the class name when we see this special case. */
11419 if (cu->language == language_java
11420 && DW_STRING (attr) != NULL
11421 && strcmp (DW_STRING (attr), "<init>") == 0)
11422 {
11423 struct dwarf2_cu *spec_cu = cu;
11424 struct die_info *spec_die;
11425
11426 /* GCJ will output '<init>' for Java constructor names.
11427 For this special case, return the name of the parent class. */
11428
11429 /* GCJ may output suprogram DIEs with AT_specification set.
11430 If so, use the name of the specified DIE. */
11431 spec_die = die_specification (die, &spec_cu);
11432 if (spec_die != NULL)
11433 return dwarf2_name (spec_die, spec_cu);
11434
11435 do
11436 {
11437 die = die->parent;
11438 if (die->tag == DW_TAG_class_type)
11439 return dwarf2_name (die, cu);
11440 }
11441 while (die->tag != DW_TAG_compile_unit);
11442 }
907af001
UW
11443 break;
11444
11445 case DW_TAG_class_type:
11446 case DW_TAG_interface_type:
11447 case DW_TAG_structure_type:
11448 case DW_TAG_union_type:
11449 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11450 structures or unions. These were of the form "._%d" in GCC 4.1,
11451 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11452 and GCC 4.4. We work around this problem by ignoring these. */
11453 if (strncmp (DW_STRING (attr), "._", 2) == 0
11454 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11455 return NULL;
11456 break;
11457
71c25dea 11458 default:
907af001
UW
11459 break;
11460 }
11461
11462 if (!DW_STRING_IS_CANONICAL (attr))
11463 {
11464 DW_STRING (attr)
11465 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11466 &cu->objfile->objfile_obstack);
11467 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11468 }
907af001 11469 return DW_STRING (attr);
9219021c
DC
11470}
11471
11472/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11473 is none. *EXT_CU is the CU containing DIE on input, and the CU
11474 containing the return value on output. */
9219021c
DC
11475
11476static struct die_info *
f2f0e013 11477dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11478{
11479 struct attribute *attr;
9219021c 11480
f2f0e013 11481 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11482 if (attr == NULL)
11483 return NULL;
11484
f2f0e013 11485 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11486}
11487
c906108c
SS
11488/* Convert a DIE tag into its string name. */
11489
11490static char *
aa1ee363 11491dwarf_tag_name (unsigned tag)
c906108c
SS
11492{
11493 switch (tag)
11494 {
11495 case DW_TAG_padding:
11496 return "DW_TAG_padding";
11497 case DW_TAG_array_type:
11498 return "DW_TAG_array_type";
11499 case DW_TAG_class_type:
11500 return "DW_TAG_class_type";
11501 case DW_TAG_entry_point:
11502 return "DW_TAG_entry_point";
11503 case DW_TAG_enumeration_type:
11504 return "DW_TAG_enumeration_type";
11505 case DW_TAG_formal_parameter:
11506 return "DW_TAG_formal_parameter";
11507 case DW_TAG_imported_declaration:
11508 return "DW_TAG_imported_declaration";
11509 case DW_TAG_label:
11510 return "DW_TAG_label";
11511 case DW_TAG_lexical_block:
11512 return "DW_TAG_lexical_block";
11513 case DW_TAG_member:
11514 return "DW_TAG_member";
11515 case DW_TAG_pointer_type:
11516 return "DW_TAG_pointer_type";
11517 case DW_TAG_reference_type:
11518 return "DW_TAG_reference_type";
11519 case DW_TAG_compile_unit:
11520 return "DW_TAG_compile_unit";
11521 case DW_TAG_string_type:
11522 return "DW_TAG_string_type";
11523 case DW_TAG_structure_type:
11524 return "DW_TAG_structure_type";
11525 case DW_TAG_subroutine_type:
11526 return "DW_TAG_subroutine_type";
11527 case DW_TAG_typedef:
11528 return "DW_TAG_typedef";
11529 case DW_TAG_union_type:
11530 return "DW_TAG_union_type";
11531 case DW_TAG_unspecified_parameters:
11532 return "DW_TAG_unspecified_parameters";
11533 case DW_TAG_variant:
11534 return "DW_TAG_variant";
11535 case DW_TAG_common_block:
11536 return "DW_TAG_common_block";
11537 case DW_TAG_common_inclusion:
11538 return "DW_TAG_common_inclusion";
11539 case DW_TAG_inheritance:
11540 return "DW_TAG_inheritance";
11541 case DW_TAG_inlined_subroutine:
11542 return "DW_TAG_inlined_subroutine";
11543 case DW_TAG_module:
11544 return "DW_TAG_module";
11545 case DW_TAG_ptr_to_member_type:
11546 return "DW_TAG_ptr_to_member_type";
11547 case DW_TAG_set_type:
11548 return "DW_TAG_set_type";
11549 case DW_TAG_subrange_type:
11550 return "DW_TAG_subrange_type";
11551 case DW_TAG_with_stmt:
11552 return "DW_TAG_with_stmt";
11553 case DW_TAG_access_declaration:
11554 return "DW_TAG_access_declaration";
11555 case DW_TAG_base_type:
11556 return "DW_TAG_base_type";
11557 case DW_TAG_catch_block:
11558 return "DW_TAG_catch_block";
11559 case DW_TAG_const_type:
11560 return "DW_TAG_const_type";
11561 case DW_TAG_constant:
11562 return "DW_TAG_constant";
11563 case DW_TAG_enumerator:
11564 return "DW_TAG_enumerator";
11565 case DW_TAG_file_type:
11566 return "DW_TAG_file_type";
11567 case DW_TAG_friend:
11568 return "DW_TAG_friend";
11569 case DW_TAG_namelist:
11570 return "DW_TAG_namelist";
11571 case DW_TAG_namelist_item:
11572 return "DW_TAG_namelist_item";
11573 case DW_TAG_packed_type:
11574 return "DW_TAG_packed_type";
11575 case DW_TAG_subprogram:
11576 return "DW_TAG_subprogram";
11577 case DW_TAG_template_type_param:
11578 return "DW_TAG_template_type_param";
11579 case DW_TAG_template_value_param:
11580 return "DW_TAG_template_value_param";
11581 case DW_TAG_thrown_type:
11582 return "DW_TAG_thrown_type";
11583 case DW_TAG_try_block:
11584 return "DW_TAG_try_block";
11585 case DW_TAG_variant_part:
11586 return "DW_TAG_variant_part";
11587 case DW_TAG_variable:
11588 return "DW_TAG_variable";
11589 case DW_TAG_volatile_type:
11590 return "DW_TAG_volatile_type";
d9fa45fe
DC
11591 case DW_TAG_dwarf_procedure:
11592 return "DW_TAG_dwarf_procedure";
11593 case DW_TAG_restrict_type:
11594 return "DW_TAG_restrict_type";
11595 case DW_TAG_interface_type:
11596 return "DW_TAG_interface_type";
11597 case DW_TAG_namespace:
11598 return "DW_TAG_namespace";
11599 case DW_TAG_imported_module:
11600 return "DW_TAG_imported_module";
11601 case DW_TAG_unspecified_type:
11602 return "DW_TAG_unspecified_type";
11603 case DW_TAG_partial_unit:
11604 return "DW_TAG_partial_unit";
11605 case DW_TAG_imported_unit:
11606 return "DW_TAG_imported_unit";
b7619582
GF
11607 case DW_TAG_condition:
11608 return "DW_TAG_condition";
11609 case DW_TAG_shared_type:
11610 return "DW_TAG_shared_type";
348e048f
DE
11611 case DW_TAG_type_unit:
11612 return "DW_TAG_type_unit";
c906108c
SS
11613 case DW_TAG_MIPS_loop:
11614 return "DW_TAG_MIPS_loop";
b7619582
GF
11615 case DW_TAG_HP_array_descriptor:
11616 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11617 case DW_TAG_format_label:
11618 return "DW_TAG_format_label";
11619 case DW_TAG_function_template:
11620 return "DW_TAG_function_template";
11621 case DW_TAG_class_template:
11622 return "DW_TAG_class_template";
b7619582
GF
11623 case DW_TAG_GNU_BINCL:
11624 return "DW_TAG_GNU_BINCL";
11625 case DW_TAG_GNU_EINCL:
11626 return "DW_TAG_GNU_EINCL";
11627 case DW_TAG_upc_shared_type:
11628 return "DW_TAG_upc_shared_type";
11629 case DW_TAG_upc_strict_type:
11630 return "DW_TAG_upc_strict_type";
11631 case DW_TAG_upc_relaxed_type:
11632 return "DW_TAG_upc_relaxed_type";
11633 case DW_TAG_PGI_kanji_type:
11634 return "DW_TAG_PGI_kanji_type";
11635 case DW_TAG_PGI_interface_block:
11636 return "DW_TAG_PGI_interface_block";
c906108c
SS
11637 default:
11638 return "DW_TAG_<unknown>";
11639 }
11640}
11641
11642/* Convert a DWARF attribute code into its string name. */
11643
11644static char *
aa1ee363 11645dwarf_attr_name (unsigned attr)
c906108c
SS
11646{
11647 switch (attr)
11648 {
11649 case DW_AT_sibling:
11650 return "DW_AT_sibling";
11651 case DW_AT_location:
11652 return "DW_AT_location";
11653 case DW_AT_name:
11654 return "DW_AT_name";
11655 case DW_AT_ordering:
11656 return "DW_AT_ordering";
11657 case DW_AT_subscr_data:
11658 return "DW_AT_subscr_data";
11659 case DW_AT_byte_size:
11660 return "DW_AT_byte_size";
11661 case DW_AT_bit_offset:
11662 return "DW_AT_bit_offset";
11663 case DW_AT_bit_size:
11664 return "DW_AT_bit_size";
11665 case DW_AT_element_list:
11666 return "DW_AT_element_list";
11667 case DW_AT_stmt_list:
11668 return "DW_AT_stmt_list";
11669 case DW_AT_low_pc:
11670 return "DW_AT_low_pc";
11671 case DW_AT_high_pc:
11672 return "DW_AT_high_pc";
11673 case DW_AT_language:
11674 return "DW_AT_language";
11675 case DW_AT_member:
11676 return "DW_AT_member";
11677 case DW_AT_discr:
11678 return "DW_AT_discr";
11679 case DW_AT_discr_value:
11680 return "DW_AT_discr_value";
11681 case DW_AT_visibility:
11682 return "DW_AT_visibility";
11683 case DW_AT_import:
11684 return "DW_AT_import";
11685 case DW_AT_string_length:
11686 return "DW_AT_string_length";
11687 case DW_AT_common_reference:
11688 return "DW_AT_common_reference";
11689 case DW_AT_comp_dir:
11690 return "DW_AT_comp_dir";
11691 case DW_AT_const_value:
11692 return "DW_AT_const_value";
11693 case DW_AT_containing_type:
11694 return "DW_AT_containing_type";
11695 case DW_AT_default_value:
11696 return "DW_AT_default_value";
11697 case DW_AT_inline:
11698 return "DW_AT_inline";
11699 case DW_AT_is_optional:
11700 return "DW_AT_is_optional";
11701 case DW_AT_lower_bound:
11702 return "DW_AT_lower_bound";
11703 case DW_AT_producer:
11704 return "DW_AT_producer";
11705 case DW_AT_prototyped:
11706 return "DW_AT_prototyped";
11707 case DW_AT_return_addr:
11708 return "DW_AT_return_addr";
11709 case DW_AT_start_scope:
11710 return "DW_AT_start_scope";
09fa0d7c
JK
11711 case DW_AT_bit_stride:
11712 return "DW_AT_bit_stride";
c906108c
SS
11713 case DW_AT_upper_bound:
11714 return "DW_AT_upper_bound";
11715 case DW_AT_abstract_origin:
11716 return "DW_AT_abstract_origin";
11717 case DW_AT_accessibility:
11718 return "DW_AT_accessibility";
11719 case DW_AT_address_class:
11720 return "DW_AT_address_class";
11721 case DW_AT_artificial:
11722 return "DW_AT_artificial";
11723 case DW_AT_base_types:
11724 return "DW_AT_base_types";
11725 case DW_AT_calling_convention:
11726 return "DW_AT_calling_convention";
11727 case DW_AT_count:
11728 return "DW_AT_count";
11729 case DW_AT_data_member_location:
11730 return "DW_AT_data_member_location";
11731 case DW_AT_decl_column:
11732 return "DW_AT_decl_column";
11733 case DW_AT_decl_file:
11734 return "DW_AT_decl_file";
11735 case DW_AT_decl_line:
11736 return "DW_AT_decl_line";
11737 case DW_AT_declaration:
11738 return "DW_AT_declaration";
11739 case DW_AT_discr_list:
11740 return "DW_AT_discr_list";
11741 case DW_AT_encoding:
11742 return "DW_AT_encoding";
11743 case DW_AT_external:
11744 return "DW_AT_external";
11745 case DW_AT_frame_base:
11746 return "DW_AT_frame_base";
11747 case DW_AT_friend:
11748 return "DW_AT_friend";
11749 case DW_AT_identifier_case:
11750 return "DW_AT_identifier_case";
11751 case DW_AT_macro_info:
11752 return "DW_AT_macro_info";
11753 case DW_AT_namelist_items:
11754 return "DW_AT_namelist_items";
11755 case DW_AT_priority:
11756 return "DW_AT_priority";
11757 case DW_AT_segment:
11758 return "DW_AT_segment";
11759 case DW_AT_specification:
11760 return "DW_AT_specification";
11761 case DW_AT_static_link:
11762 return "DW_AT_static_link";
11763 case DW_AT_type:
11764 return "DW_AT_type";
11765 case DW_AT_use_location:
11766 return "DW_AT_use_location";
11767 case DW_AT_variable_parameter:
11768 return "DW_AT_variable_parameter";
11769 case DW_AT_virtuality:
11770 return "DW_AT_virtuality";
11771 case DW_AT_vtable_elem_location:
11772 return "DW_AT_vtable_elem_location";
b7619582 11773 /* DWARF 3 values. */
d9fa45fe
DC
11774 case DW_AT_allocated:
11775 return "DW_AT_allocated";
11776 case DW_AT_associated:
11777 return "DW_AT_associated";
11778 case DW_AT_data_location:
11779 return "DW_AT_data_location";
09fa0d7c
JK
11780 case DW_AT_byte_stride:
11781 return "DW_AT_byte_stride";
d9fa45fe
DC
11782 case DW_AT_entry_pc:
11783 return "DW_AT_entry_pc";
11784 case DW_AT_use_UTF8:
11785 return "DW_AT_use_UTF8";
11786 case DW_AT_extension:
11787 return "DW_AT_extension";
11788 case DW_AT_ranges:
11789 return "DW_AT_ranges";
11790 case DW_AT_trampoline:
11791 return "DW_AT_trampoline";
11792 case DW_AT_call_column:
11793 return "DW_AT_call_column";
11794 case DW_AT_call_file:
11795 return "DW_AT_call_file";
11796 case DW_AT_call_line:
11797 return "DW_AT_call_line";
b7619582
GF
11798 case DW_AT_description:
11799 return "DW_AT_description";
11800 case DW_AT_binary_scale:
11801 return "DW_AT_binary_scale";
11802 case DW_AT_decimal_scale:
11803 return "DW_AT_decimal_scale";
11804 case DW_AT_small:
11805 return "DW_AT_small";
11806 case DW_AT_decimal_sign:
11807 return "DW_AT_decimal_sign";
11808 case DW_AT_digit_count:
11809 return "DW_AT_digit_count";
11810 case DW_AT_picture_string:
11811 return "DW_AT_picture_string";
11812 case DW_AT_mutable:
11813 return "DW_AT_mutable";
11814 case DW_AT_threads_scaled:
11815 return "DW_AT_threads_scaled";
11816 case DW_AT_explicit:
11817 return "DW_AT_explicit";
11818 case DW_AT_object_pointer:
11819 return "DW_AT_object_pointer";
11820 case DW_AT_endianity:
11821 return "DW_AT_endianity";
11822 case DW_AT_elemental:
11823 return "DW_AT_elemental";
11824 case DW_AT_pure:
11825 return "DW_AT_pure";
11826 case DW_AT_recursive:
11827 return "DW_AT_recursive";
348e048f
DE
11828 /* DWARF 4 values. */
11829 case DW_AT_signature:
11830 return "DW_AT_signature";
31ef98ae
TT
11831 case DW_AT_linkage_name:
11832 return "DW_AT_linkage_name";
b7619582 11833 /* SGI/MIPS extensions. */
c764a876 11834#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11835 case DW_AT_MIPS_fde:
11836 return "DW_AT_MIPS_fde";
c764a876 11837#endif
c906108c
SS
11838 case DW_AT_MIPS_loop_begin:
11839 return "DW_AT_MIPS_loop_begin";
11840 case DW_AT_MIPS_tail_loop_begin:
11841 return "DW_AT_MIPS_tail_loop_begin";
11842 case DW_AT_MIPS_epilog_begin:
11843 return "DW_AT_MIPS_epilog_begin";
11844 case DW_AT_MIPS_loop_unroll_factor:
11845 return "DW_AT_MIPS_loop_unroll_factor";
11846 case DW_AT_MIPS_software_pipeline_depth:
11847 return "DW_AT_MIPS_software_pipeline_depth";
11848 case DW_AT_MIPS_linkage_name:
11849 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11850 case DW_AT_MIPS_stride:
11851 return "DW_AT_MIPS_stride";
11852 case DW_AT_MIPS_abstract_name:
11853 return "DW_AT_MIPS_abstract_name";
11854 case DW_AT_MIPS_clone_origin:
11855 return "DW_AT_MIPS_clone_origin";
11856 case DW_AT_MIPS_has_inlines:
11857 return "DW_AT_MIPS_has_inlines";
b7619582 11858 /* HP extensions. */
c764a876 11859#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11860 case DW_AT_HP_block_index:
11861 return "DW_AT_HP_block_index";
c764a876 11862#endif
b7619582
GF
11863 case DW_AT_HP_unmodifiable:
11864 return "DW_AT_HP_unmodifiable";
11865 case DW_AT_HP_actuals_stmt_list:
11866 return "DW_AT_HP_actuals_stmt_list";
11867 case DW_AT_HP_proc_per_section:
11868 return "DW_AT_HP_proc_per_section";
11869 case DW_AT_HP_raw_data_ptr:
11870 return "DW_AT_HP_raw_data_ptr";
11871 case DW_AT_HP_pass_by_reference:
11872 return "DW_AT_HP_pass_by_reference";
11873 case DW_AT_HP_opt_level:
11874 return "DW_AT_HP_opt_level";
11875 case DW_AT_HP_prof_version_id:
11876 return "DW_AT_HP_prof_version_id";
11877 case DW_AT_HP_opt_flags:
11878 return "DW_AT_HP_opt_flags";
11879 case DW_AT_HP_cold_region_low_pc:
11880 return "DW_AT_HP_cold_region_low_pc";
11881 case DW_AT_HP_cold_region_high_pc:
11882 return "DW_AT_HP_cold_region_high_pc";
11883 case DW_AT_HP_all_variables_modifiable:
11884 return "DW_AT_HP_all_variables_modifiable";
11885 case DW_AT_HP_linkage_name:
11886 return "DW_AT_HP_linkage_name";
11887 case DW_AT_HP_prof_flags:
11888 return "DW_AT_HP_prof_flags";
11889 /* GNU extensions. */
c906108c
SS
11890 case DW_AT_sf_names:
11891 return "DW_AT_sf_names";
11892 case DW_AT_src_info:
11893 return "DW_AT_src_info";
11894 case DW_AT_mac_info:
11895 return "DW_AT_mac_info";
11896 case DW_AT_src_coords:
11897 return "DW_AT_src_coords";
11898 case DW_AT_body_begin:
11899 return "DW_AT_body_begin";
11900 case DW_AT_body_end:
11901 return "DW_AT_body_end";
f5f8a009
EZ
11902 case DW_AT_GNU_vector:
11903 return "DW_AT_GNU_vector";
2de00c64
DE
11904 case DW_AT_GNU_odr_signature:
11905 return "DW_AT_GNU_odr_signature";
b7619582
GF
11906 /* VMS extensions. */
11907 case DW_AT_VMS_rtnbeg_pd_address:
11908 return "DW_AT_VMS_rtnbeg_pd_address";
11909 /* UPC extension. */
11910 case DW_AT_upc_threads_scaled:
11911 return "DW_AT_upc_threads_scaled";
11912 /* PGI (STMicroelectronics) extensions. */
11913 case DW_AT_PGI_lbase:
11914 return "DW_AT_PGI_lbase";
11915 case DW_AT_PGI_soffset:
11916 return "DW_AT_PGI_soffset";
11917 case DW_AT_PGI_lstride:
11918 return "DW_AT_PGI_lstride";
c906108c
SS
11919 default:
11920 return "DW_AT_<unknown>";
11921 }
11922}
11923
11924/* Convert a DWARF value form code into its string name. */
11925
11926static char *
aa1ee363 11927dwarf_form_name (unsigned form)
c906108c
SS
11928{
11929 switch (form)
11930 {
11931 case DW_FORM_addr:
11932 return "DW_FORM_addr";
11933 case DW_FORM_block2:
11934 return "DW_FORM_block2";
11935 case DW_FORM_block4:
11936 return "DW_FORM_block4";
11937 case DW_FORM_data2:
11938 return "DW_FORM_data2";
11939 case DW_FORM_data4:
11940 return "DW_FORM_data4";
11941 case DW_FORM_data8:
11942 return "DW_FORM_data8";
11943 case DW_FORM_string:
11944 return "DW_FORM_string";
11945 case DW_FORM_block:
11946 return "DW_FORM_block";
11947 case DW_FORM_block1:
11948 return "DW_FORM_block1";
11949 case DW_FORM_data1:
11950 return "DW_FORM_data1";
11951 case DW_FORM_flag:
11952 return "DW_FORM_flag";
11953 case DW_FORM_sdata:
11954 return "DW_FORM_sdata";
11955 case DW_FORM_strp:
11956 return "DW_FORM_strp";
11957 case DW_FORM_udata:
11958 return "DW_FORM_udata";
11959 case DW_FORM_ref_addr:
11960 return "DW_FORM_ref_addr";
11961 case DW_FORM_ref1:
11962 return "DW_FORM_ref1";
11963 case DW_FORM_ref2:
11964 return "DW_FORM_ref2";
11965 case DW_FORM_ref4:
11966 return "DW_FORM_ref4";
11967 case DW_FORM_ref8:
11968 return "DW_FORM_ref8";
11969 case DW_FORM_ref_udata:
11970 return "DW_FORM_ref_udata";
11971 case DW_FORM_indirect:
11972 return "DW_FORM_indirect";
348e048f
DE
11973 case DW_FORM_sec_offset:
11974 return "DW_FORM_sec_offset";
11975 case DW_FORM_exprloc:
11976 return "DW_FORM_exprloc";
11977 case DW_FORM_flag_present:
11978 return "DW_FORM_flag_present";
11979 case DW_FORM_sig8:
11980 return "DW_FORM_sig8";
c906108c
SS
11981 default:
11982 return "DW_FORM_<unknown>";
11983 }
11984}
11985
11986/* Convert a DWARF stack opcode into its string name. */
11987
9eae7c52
TT
11988const char *
11989dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
11990{
11991 switch (op)
11992 {
11993 case DW_OP_addr:
11994 return "DW_OP_addr";
11995 case DW_OP_deref:
11996 return "DW_OP_deref";
11997 case DW_OP_const1u:
11998 return "DW_OP_const1u";
11999 case DW_OP_const1s:
12000 return "DW_OP_const1s";
12001 case DW_OP_const2u:
12002 return "DW_OP_const2u";
12003 case DW_OP_const2s:
12004 return "DW_OP_const2s";
12005 case DW_OP_const4u:
12006 return "DW_OP_const4u";
12007 case DW_OP_const4s:
12008 return "DW_OP_const4s";
12009 case DW_OP_const8u:
12010 return "DW_OP_const8u";
12011 case DW_OP_const8s:
12012 return "DW_OP_const8s";
12013 case DW_OP_constu:
12014 return "DW_OP_constu";
12015 case DW_OP_consts:
12016 return "DW_OP_consts";
12017 case DW_OP_dup:
12018 return "DW_OP_dup";
12019 case DW_OP_drop:
12020 return "DW_OP_drop";
12021 case DW_OP_over:
12022 return "DW_OP_over";
12023 case DW_OP_pick:
12024 return "DW_OP_pick";
12025 case DW_OP_swap:
12026 return "DW_OP_swap";
12027 case DW_OP_rot:
12028 return "DW_OP_rot";
12029 case DW_OP_xderef:
12030 return "DW_OP_xderef";
12031 case DW_OP_abs:
12032 return "DW_OP_abs";
12033 case DW_OP_and:
12034 return "DW_OP_and";
12035 case DW_OP_div:
12036 return "DW_OP_div";
12037 case DW_OP_minus:
12038 return "DW_OP_minus";
12039 case DW_OP_mod:
12040 return "DW_OP_mod";
12041 case DW_OP_mul:
12042 return "DW_OP_mul";
12043 case DW_OP_neg:
12044 return "DW_OP_neg";
12045 case DW_OP_not:
12046 return "DW_OP_not";
12047 case DW_OP_or:
12048 return "DW_OP_or";
12049 case DW_OP_plus:
12050 return "DW_OP_plus";
12051 case DW_OP_plus_uconst:
12052 return "DW_OP_plus_uconst";
12053 case DW_OP_shl:
12054 return "DW_OP_shl";
12055 case DW_OP_shr:
12056 return "DW_OP_shr";
12057 case DW_OP_shra:
12058 return "DW_OP_shra";
12059 case DW_OP_xor:
12060 return "DW_OP_xor";
12061 case DW_OP_bra:
12062 return "DW_OP_bra";
12063 case DW_OP_eq:
12064 return "DW_OP_eq";
12065 case DW_OP_ge:
12066 return "DW_OP_ge";
12067 case DW_OP_gt:
12068 return "DW_OP_gt";
12069 case DW_OP_le:
12070 return "DW_OP_le";
12071 case DW_OP_lt:
12072 return "DW_OP_lt";
12073 case DW_OP_ne:
12074 return "DW_OP_ne";
12075 case DW_OP_skip:
12076 return "DW_OP_skip";
12077 case DW_OP_lit0:
12078 return "DW_OP_lit0";
12079 case DW_OP_lit1:
12080 return "DW_OP_lit1";
12081 case DW_OP_lit2:
12082 return "DW_OP_lit2";
12083 case DW_OP_lit3:
12084 return "DW_OP_lit3";
12085 case DW_OP_lit4:
12086 return "DW_OP_lit4";
12087 case DW_OP_lit5:
12088 return "DW_OP_lit5";
12089 case DW_OP_lit6:
12090 return "DW_OP_lit6";
12091 case DW_OP_lit7:
12092 return "DW_OP_lit7";
12093 case DW_OP_lit8:
12094 return "DW_OP_lit8";
12095 case DW_OP_lit9:
12096 return "DW_OP_lit9";
12097 case DW_OP_lit10:
12098 return "DW_OP_lit10";
12099 case DW_OP_lit11:
12100 return "DW_OP_lit11";
12101 case DW_OP_lit12:
12102 return "DW_OP_lit12";
12103 case DW_OP_lit13:
12104 return "DW_OP_lit13";
12105 case DW_OP_lit14:
12106 return "DW_OP_lit14";
12107 case DW_OP_lit15:
12108 return "DW_OP_lit15";
12109 case DW_OP_lit16:
12110 return "DW_OP_lit16";
12111 case DW_OP_lit17:
12112 return "DW_OP_lit17";
12113 case DW_OP_lit18:
12114 return "DW_OP_lit18";
12115 case DW_OP_lit19:
12116 return "DW_OP_lit19";
12117 case DW_OP_lit20:
12118 return "DW_OP_lit20";
12119 case DW_OP_lit21:
12120 return "DW_OP_lit21";
12121 case DW_OP_lit22:
12122 return "DW_OP_lit22";
12123 case DW_OP_lit23:
12124 return "DW_OP_lit23";
12125 case DW_OP_lit24:
12126 return "DW_OP_lit24";
12127 case DW_OP_lit25:
12128 return "DW_OP_lit25";
12129 case DW_OP_lit26:
12130 return "DW_OP_lit26";
12131 case DW_OP_lit27:
12132 return "DW_OP_lit27";
12133 case DW_OP_lit28:
12134 return "DW_OP_lit28";
12135 case DW_OP_lit29:
12136 return "DW_OP_lit29";
12137 case DW_OP_lit30:
12138 return "DW_OP_lit30";
12139 case DW_OP_lit31:
12140 return "DW_OP_lit31";
12141 case DW_OP_reg0:
12142 return "DW_OP_reg0";
12143 case DW_OP_reg1:
12144 return "DW_OP_reg1";
12145 case DW_OP_reg2:
12146 return "DW_OP_reg2";
12147 case DW_OP_reg3:
12148 return "DW_OP_reg3";
12149 case DW_OP_reg4:
12150 return "DW_OP_reg4";
12151 case DW_OP_reg5:
12152 return "DW_OP_reg5";
12153 case DW_OP_reg6:
12154 return "DW_OP_reg6";
12155 case DW_OP_reg7:
12156 return "DW_OP_reg7";
12157 case DW_OP_reg8:
12158 return "DW_OP_reg8";
12159 case DW_OP_reg9:
12160 return "DW_OP_reg9";
12161 case DW_OP_reg10:
12162 return "DW_OP_reg10";
12163 case DW_OP_reg11:
12164 return "DW_OP_reg11";
12165 case DW_OP_reg12:
12166 return "DW_OP_reg12";
12167 case DW_OP_reg13:
12168 return "DW_OP_reg13";
12169 case DW_OP_reg14:
12170 return "DW_OP_reg14";
12171 case DW_OP_reg15:
12172 return "DW_OP_reg15";
12173 case DW_OP_reg16:
12174 return "DW_OP_reg16";
12175 case DW_OP_reg17:
12176 return "DW_OP_reg17";
12177 case DW_OP_reg18:
12178 return "DW_OP_reg18";
12179 case DW_OP_reg19:
12180 return "DW_OP_reg19";
12181 case DW_OP_reg20:
12182 return "DW_OP_reg20";
12183 case DW_OP_reg21:
12184 return "DW_OP_reg21";
12185 case DW_OP_reg22:
12186 return "DW_OP_reg22";
12187 case DW_OP_reg23:
12188 return "DW_OP_reg23";
12189 case DW_OP_reg24:
12190 return "DW_OP_reg24";
12191 case DW_OP_reg25:
12192 return "DW_OP_reg25";
12193 case DW_OP_reg26:
12194 return "DW_OP_reg26";
12195 case DW_OP_reg27:
12196 return "DW_OP_reg27";
12197 case DW_OP_reg28:
12198 return "DW_OP_reg28";
12199 case DW_OP_reg29:
12200 return "DW_OP_reg29";
12201 case DW_OP_reg30:
12202 return "DW_OP_reg30";
12203 case DW_OP_reg31:
12204 return "DW_OP_reg31";
12205 case DW_OP_breg0:
12206 return "DW_OP_breg0";
12207 case DW_OP_breg1:
12208 return "DW_OP_breg1";
12209 case DW_OP_breg2:
12210 return "DW_OP_breg2";
12211 case DW_OP_breg3:
12212 return "DW_OP_breg3";
12213 case DW_OP_breg4:
12214 return "DW_OP_breg4";
12215 case DW_OP_breg5:
12216 return "DW_OP_breg5";
12217 case DW_OP_breg6:
12218 return "DW_OP_breg6";
12219 case DW_OP_breg7:
12220 return "DW_OP_breg7";
12221 case DW_OP_breg8:
12222 return "DW_OP_breg8";
12223 case DW_OP_breg9:
12224 return "DW_OP_breg9";
12225 case DW_OP_breg10:
12226 return "DW_OP_breg10";
12227 case DW_OP_breg11:
12228 return "DW_OP_breg11";
12229 case DW_OP_breg12:
12230 return "DW_OP_breg12";
12231 case DW_OP_breg13:
12232 return "DW_OP_breg13";
12233 case DW_OP_breg14:
12234 return "DW_OP_breg14";
12235 case DW_OP_breg15:
12236 return "DW_OP_breg15";
12237 case DW_OP_breg16:
12238 return "DW_OP_breg16";
12239 case DW_OP_breg17:
12240 return "DW_OP_breg17";
12241 case DW_OP_breg18:
12242 return "DW_OP_breg18";
12243 case DW_OP_breg19:
12244 return "DW_OP_breg19";
12245 case DW_OP_breg20:
12246 return "DW_OP_breg20";
12247 case DW_OP_breg21:
12248 return "DW_OP_breg21";
12249 case DW_OP_breg22:
12250 return "DW_OP_breg22";
12251 case DW_OP_breg23:
12252 return "DW_OP_breg23";
12253 case DW_OP_breg24:
12254 return "DW_OP_breg24";
12255 case DW_OP_breg25:
12256 return "DW_OP_breg25";
12257 case DW_OP_breg26:
12258 return "DW_OP_breg26";
12259 case DW_OP_breg27:
12260 return "DW_OP_breg27";
12261 case DW_OP_breg28:
12262 return "DW_OP_breg28";
12263 case DW_OP_breg29:
12264 return "DW_OP_breg29";
12265 case DW_OP_breg30:
12266 return "DW_OP_breg30";
12267 case DW_OP_breg31:
12268 return "DW_OP_breg31";
12269 case DW_OP_regx:
12270 return "DW_OP_regx";
12271 case DW_OP_fbreg:
12272 return "DW_OP_fbreg";
12273 case DW_OP_bregx:
12274 return "DW_OP_bregx";
12275 case DW_OP_piece:
12276 return "DW_OP_piece";
12277 case DW_OP_deref_size:
12278 return "DW_OP_deref_size";
12279 case DW_OP_xderef_size:
12280 return "DW_OP_xderef_size";
12281 case DW_OP_nop:
12282 return "DW_OP_nop";
b7619582 12283 /* DWARF 3 extensions. */
ed348acc
EZ
12284 case DW_OP_push_object_address:
12285 return "DW_OP_push_object_address";
12286 case DW_OP_call2:
12287 return "DW_OP_call2";
12288 case DW_OP_call4:
12289 return "DW_OP_call4";
12290 case DW_OP_call_ref:
12291 return "DW_OP_call_ref";
b7619582
GF
12292 case DW_OP_form_tls_address:
12293 return "DW_OP_form_tls_address";
12294 case DW_OP_call_frame_cfa:
12295 return "DW_OP_call_frame_cfa";
12296 case DW_OP_bit_piece:
12297 return "DW_OP_bit_piece";
9eae7c52
TT
12298 /* DWARF 4 extensions. */
12299 case DW_OP_implicit_value:
12300 return "DW_OP_implicit_value";
12301 case DW_OP_stack_value:
12302 return "DW_OP_stack_value";
12303 /* GNU extensions. */
ed348acc
EZ
12304 case DW_OP_GNU_push_tls_address:
12305 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12306 case DW_OP_GNU_uninit:
12307 return "DW_OP_GNU_uninit";
c906108c 12308 default:
9eae7c52 12309 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12310 }
12311}
12312
12313static char *
fba45db2 12314dwarf_bool_name (unsigned mybool)
c906108c
SS
12315{
12316 if (mybool)
12317 return "TRUE";
12318 else
12319 return "FALSE";
12320}
12321
12322/* Convert a DWARF type code into its string name. */
12323
12324static char *
aa1ee363 12325dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12326{
12327 switch (enc)
12328 {
b7619582
GF
12329 case DW_ATE_void:
12330 return "DW_ATE_void";
c906108c
SS
12331 case DW_ATE_address:
12332 return "DW_ATE_address";
12333 case DW_ATE_boolean:
12334 return "DW_ATE_boolean";
12335 case DW_ATE_complex_float:
12336 return "DW_ATE_complex_float";
12337 case DW_ATE_float:
12338 return "DW_ATE_float";
12339 case DW_ATE_signed:
12340 return "DW_ATE_signed";
12341 case DW_ATE_signed_char:
12342 return "DW_ATE_signed_char";
12343 case DW_ATE_unsigned:
12344 return "DW_ATE_unsigned";
12345 case DW_ATE_unsigned_char:
12346 return "DW_ATE_unsigned_char";
b7619582 12347 /* DWARF 3. */
d9fa45fe
DC
12348 case DW_ATE_imaginary_float:
12349 return "DW_ATE_imaginary_float";
b7619582
GF
12350 case DW_ATE_packed_decimal:
12351 return "DW_ATE_packed_decimal";
12352 case DW_ATE_numeric_string:
12353 return "DW_ATE_numeric_string";
12354 case DW_ATE_edited:
12355 return "DW_ATE_edited";
12356 case DW_ATE_signed_fixed:
12357 return "DW_ATE_signed_fixed";
12358 case DW_ATE_unsigned_fixed:
12359 return "DW_ATE_unsigned_fixed";
12360 case DW_ATE_decimal_float:
12361 return "DW_ATE_decimal_float";
75079b2b
TT
12362 /* DWARF 4. */
12363 case DW_ATE_UTF:
12364 return "DW_ATE_UTF";
b7619582
GF
12365 /* HP extensions. */
12366 case DW_ATE_HP_float80:
12367 return "DW_ATE_HP_float80";
12368 case DW_ATE_HP_complex_float80:
12369 return "DW_ATE_HP_complex_float80";
12370 case DW_ATE_HP_float128:
12371 return "DW_ATE_HP_float128";
12372 case DW_ATE_HP_complex_float128:
12373 return "DW_ATE_HP_complex_float128";
12374 case DW_ATE_HP_floathpintel:
12375 return "DW_ATE_HP_floathpintel";
12376 case DW_ATE_HP_imaginary_float80:
12377 return "DW_ATE_HP_imaginary_float80";
12378 case DW_ATE_HP_imaginary_float128:
12379 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12380 default:
12381 return "DW_ATE_<unknown>";
12382 }
12383}
12384
12385/* Convert a DWARF call frame info operation to its string name. */
12386
12387#if 0
12388static char *
aa1ee363 12389dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12390{
12391 switch (cfi_opc)
12392 {
12393 case DW_CFA_advance_loc:
12394 return "DW_CFA_advance_loc";
12395 case DW_CFA_offset:
12396 return "DW_CFA_offset";
12397 case DW_CFA_restore:
12398 return "DW_CFA_restore";
12399 case DW_CFA_nop:
12400 return "DW_CFA_nop";
12401 case DW_CFA_set_loc:
12402 return "DW_CFA_set_loc";
12403 case DW_CFA_advance_loc1:
12404 return "DW_CFA_advance_loc1";
12405 case DW_CFA_advance_loc2:
12406 return "DW_CFA_advance_loc2";
12407 case DW_CFA_advance_loc4:
12408 return "DW_CFA_advance_loc4";
12409 case DW_CFA_offset_extended:
12410 return "DW_CFA_offset_extended";
12411 case DW_CFA_restore_extended:
12412 return "DW_CFA_restore_extended";
12413 case DW_CFA_undefined:
12414 return "DW_CFA_undefined";
12415 case DW_CFA_same_value:
12416 return "DW_CFA_same_value";
12417 case DW_CFA_register:
12418 return "DW_CFA_register";
12419 case DW_CFA_remember_state:
12420 return "DW_CFA_remember_state";
12421 case DW_CFA_restore_state:
12422 return "DW_CFA_restore_state";
12423 case DW_CFA_def_cfa:
12424 return "DW_CFA_def_cfa";
12425 case DW_CFA_def_cfa_register:
12426 return "DW_CFA_def_cfa_register";
12427 case DW_CFA_def_cfa_offset:
12428 return "DW_CFA_def_cfa_offset";
b7619582 12429 /* DWARF 3. */
985cb1a3
JM
12430 case DW_CFA_def_cfa_expression:
12431 return "DW_CFA_def_cfa_expression";
12432 case DW_CFA_expression:
12433 return "DW_CFA_expression";
12434 case DW_CFA_offset_extended_sf:
12435 return "DW_CFA_offset_extended_sf";
12436 case DW_CFA_def_cfa_sf:
12437 return "DW_CFA_def_cfa_sf";
12438 case DW_CFA_def_cfa_offset_sf:
12439 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12440 case DW_CFA_val_offset:
12441 return "DW_CFA_val_offset";
12442 case DW_CFA_val_offset_sf:
12443 return "DW_CFA_val_offset_sf";
12444 case DW_CFA_val_expression:
12445 return "DW_CFA_val_expression";
12446 /* SGI/MIPS specific. */
c906108c
SS
12447 case DW_CFA_MIPS_advance_loc8:
12448 return "DW_CFA_MIPS_advance_loc8";
b7619582 12449 /* GNU extensions. */
985cb1a3
JM
12450 case DW_CFA_GNU_window_save:
12451 return "DW_CFA_GNU_window_save";
12452 case DW_CFA_GNU_args_size:
12453 return "DW_CFA_GNU_args_size";
12454 case DW_CFA_GNU_negative_offset_extended:
12455 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12456 default:
12457 return "DW_CFA_<unknown>";
12458 }
12459}
12460#endif
12461
f9aca02d 12462static void
d97bc12b 12463dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12464{
12465 unsigned int i;
12466
d97bc12b
DE
12467 print_spaces (indent, f);
12468 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12469 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12470
12471 if (die->parent != NULL)
12472 {
12473 print_spaces (indent, f);
12474 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12475 die->parent->offset);
12476 }
12477
12478 print_spaces (indent, f);
12479 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12480 dwarf_bool_name (die->child != NULL));
c906108c 12481
d97bc12b
DE
12482 print_spaces (indent, f);
12483 fprintf_unfiltered (f, " attributes:\n");
12484
c906108c
SS
12485 for (i = 0; i < die->num_attrs; ++i)
12486 {
d97bc12b
DE
12487 print_spaces (indent, f);
12488 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12489 dwarf_attr_name (die->attrs[i].name),
12490 dwarf_form_name (die->attrs[i].form));
d97bc12b 12491
c906108c
SS
12492 switch (die->attrs[i].form)
12493 {
12494 case DW_FORM_ref_addr:
12495 case DW_FORM_addr:
d97bc12b 12496 fprintf_unfiltered (f, "address: ");
5af949e3 12497 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12498 break;
12499 case DW_FORM_block2:
12500 case DW_FORM_block4:
12501 case DW_FORM_block:
12502 case DW_FORM_block1:
d97bc12b 12503 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12504 break;
2dc7f7b3
TT
12505 case DW_FORM_exprloc:
12506 fprintf_unfiltered (f, "expression: size %u",
12507 DW_BLOCK (&die->attrs[i])->size);
12508 break;
10b3939b
DJ
12509 case DW_FORM_ref1:
12510 case DW_FORM_ref2:
12511 case DW_FORM_ref4:
d97bc12b 12512 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12513 (long) (DW_ADDR (&die->attrs[i])));
12514 break;
c906108c
SS
12515 case DW_FORM_data1:
12516 case DW_FORM_data2:
12517 case DW_FORM_data4:
ce5d95e1 12518 case DW_FORM_data8:
c906108c
SS
12519 case DW_FORM_udata:
12520 case DW_FORM_sdata:
43bbcdc2
PH
12521 fprintf_unfiltered (f, "constant: %s",
12522 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12523 break;
2dc7f7b3
TT
12524 case DW_FORM_sec_offset:
12525 fprintf_unfiltered (f, "section offset: %s",
12526 pulongest (DW_UNSND (&die->attrs[i])));
12527 break;
348e048f
DE
12528 case DW_FORM_sig8:
12529 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12530 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12531 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12532 else
12533 fprintf_unfiltered (f, "signatured type, offset: unknown");
12534 break;
c906108c 12535 case DW_FORM_string:
4bdf3d34 12536 case DW_FORM_strp:
8285870a 12537 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12538 DW_STRING (&die->attrs[i])
8285870a
JK
12539 ? DW_STRING (&die->attrs[i]) : "",
12540 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12541 break;
12542 case DW_FORM_flag:
12543 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12544 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12545 else
d97bc12b 12546 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12547 break;
2dc7f7b3
TT
12548 case DW_FORM_flag_present:
12549 fprintf_unfiltered (f, "flag: TRUE");
12550 break;
a8329558
KW
12551 case DW_FORM_indirect:
12552 /* the reader will have reduced the indirect form to
12553 the "base form" so this form should not occur */
d97bc12b 12554 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12555 break;
c906108c 12556 default:
d97bc12b 12557 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12558 die->attrs[i].form);
d97bc12b 12559 break;
c906108c 12560 }
d97bc12b 12561 fprintf_unfiltered (f, "\n");
c906108c
SS
12562 }
12563}
12564
f9aca02d 12565static void
d97bc12b 12566dump_die_for_error (struct die_info *die)
c906108c 12567{
d97bc12b
DE
12568 dump_die_shallow (gdb_stderr, 0, die);
12569}
12570
12571static void
12572dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12573{
12574 int indent = level * 4;
12575
12576 gdb_assert (die != NULL);
12577
12578 if (level >= max_level)
12579 return;
12580
12581 dump_die_shallow (f, indent, die);
12582
12583 if (die->child != NULL)
c906108c 12584 {
d97bc12b
DE
12585 print_spaces (indent, f);
12586 fprintf_unfiltered (f, " Children:");
12587 if (level + 1 < max_level)
12588 {
12589 fprintf_unfiltered (f, "\n");
12590 dump_die_1 (f, level + 1, max_level, die->child);
12591 }
12592 else
12593 {
12594 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12595 }
12596 }
12597
12598 if (die->sibling != NULL && level > 0)
12599 {
12600 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12601 }
12602}
12603
d97bc12b
DE
12604/* This is called from the pdie macro in gdbinit.in.
12605 It's not static so gcc will keep a copy callable from gdb. */
12606
12607void
12608dump_die (struct die_info *die, int max_level)
12609{
12610 dump_die_1 (gdb_stdlog, 0, max_level, die);
12611}
12612
f9aca02d 12613static void
51545339 12614store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12615{
51545339 12616 void **slot;
c906108c 12617
51545339
DJ
12618 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12619
12620 *slot = die;
c906108c
SS
12621}
12622
93311388
DE
12623static int
12624is_ref_attr (struct attribute *attr)
c906108c 12625{
c906108c
SS
12626 switch (attr->form)
12627 {
12628 case DW_FORM_ref_addr:
c906108c
SS
12629 case DW_FORM_ref1:
12630 case DW_FORM_ref2:
12631 case DW_FORM_ref4:
613e1657 12632 case DW_FORM_ref8:
c906108c 12633 case DW_FORM_ref_udata:
93311388 12634 return 1;
c906108c 12635 default:
93311388 12636 return 0;
c906108c 12637 }
93311388
DE
12638}
12639
12640static unsigned int
12641dwarf2_get_ref_die_offset (struct attribute *attr)
12642{
12643 if (is_ref_attr (attr))
12644 return DW_ADDR (attr);
12645
12646 complaint (&symfile_complaints,
12647 _("unsupported die ref attribute form: '%s'"),
12648 dwarf_form_name (attr->form));
12649 return 0;
c906108c
SS
12650}
12651
43bbcdc2
PH
12652/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12653 * the value held by the attribute is not constant. */
a02abb62 12654
43bbcdc2 12655static LONGEST
a02abb62
JB
12656dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12657{
12658 if (attr->form == DW_FORM_sdata)
12659 return DW_SND (attr);
12660 else if (attr->form == DW_FORM_udata
12661 || attr->form == DW_FORM_data1
12662 || attr->form == DW_FORM_data2
12663 || attr->form == DW_FORM_data4
12664 || attr->form == DW_FORM_data8)
12665 return DW_UNSND (attr);
12666 else
12667 {
e2e0b3e5 12668 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12669 dwarf_form_name (attr->form));
12670 return default_value;
12671 }
12672}
12673
03dd20cc 12674/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12675 unit and add it to our queue.
12676 The result is non-zero if PER_CU was queued, otherwise the result is zero
12677 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12678
348e048f 12679static int
03dd20cc
DJ
12680maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12681 struct dwarf2_per_cu_data *per_cu)
12682{
98bfdba5
PA
12683 /* We may arrive here during partial symbol reading, if we need full
12684 DIEs to process an unusual case (e.g. template arguments). Do
12685 not queue PER_CU, just tell our caller to load its DIEs. */
12686 if (dwarf2_per_objfile->reading_partial_symbols)
12687 {
12688 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12689 return 1;
12690 return 0;
12691 }
12692
03dd20cc
DJ
12693 /* Mark the dependence relation so that we don't flush PER_CU
12694 too early. */
12695 dwarf2_add_dependence (this_cu, per_cu);
12696
12697 /* If it's already on the queue, we have nothing to do. */
12698 if (per_cu->queued)
348e048f 12699 return 0;
03dd20cc
DJ
12700
12701 /* If the compilation unit is already loaded, just mark it as
12702 used. */
12703 if (per_cu->cu != NULL)
12704 {
12705 per_cu->cu->last_used = 0;
348e048f 12706 return 0;
03dd20cc
DJ
12707 }
12708
12709 /* Add it to the queue. */
12710 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12711
12712 return 1;
12713}
12714
12715/* Follow reference or signature attribute ATTR of SRC_DIE.
12716 On entry *REF_CU is the CU of SRC_DIE.
12717 On exit *REF_CU is the CU of the result. */
12718
12719static struct die_info *
12720follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12721 struct dwarf2_cu **ref_cu)
12722{
12723 struct die_info *die;
12724
12725 if (is_ref_attr (attr))
12726 die = follow_die_ref (src_die, attr, ref_cu);
12727 else if (attr->form == DW_FORM_sig8)
12728 die = follow_die_sig (src_die, attr, ref_cu);
12729 else
12730 {
12731 dump_die_for_error (src_die);
12732 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12733 (*ref_cu)->objfile->name);
12734 }
12735
12736 return die;
03dd20cc
DJ
12737}
12738
5c631832 12739/* Follow reference OFFSET.
673bfd45
DE
12740 On entry *REF_CU is the CU of the source die referencing OFFSET.
12741 On exit *REF_CU is the CU of the result.
12742 Returns NULL if OFFSET is invalid. */
f504f079 12743
f9aca02d 12744static struct die_info *
5c631832 12745follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12746{
10b3939b 12747 struct die_info temp_die;
f2f0e013 12748 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12749
348e048f
DE
12750 gdb_assert (cu->per_cu != NULL);
12751
98bfdba5
PA
12752 target_cu = cu;
12753
348e048f
DE
12754 if (cu->per_cu->from_debug_types)
12755 {
12756 /* .debug_types CUs cannot reference anything outside their CU.
12757 If they need to, they have to reference a signatured type via
12758 DW_FORM_sig8. */
12759 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12760 return NULL;
348e048f
DE
12761 }
12762 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12763 {
12764 struct dwarf2_per_cu_data *per_cu;
9a619af0 12765
45452591 12766 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12767
12768 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12769 if (maybe_queue_comp_unit (cu, per_cu))
12770 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12771
10b3939b
DJ
12772 target_cu = per_cu->cu;
12773 }
98bfdba5
PA
12774 else if (cu->dies == NULL)
12775 {
12776 /* We're loading full DIEs during partial symbol reading. */
12777 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12778 load_full_comp_unit (cu->per_cu, cu->objfile);
12779 }
c906108c 12780
f2f0e013 12781 *ref_cu = target_cu;
51545339 12782 temp_die.offset = offset;
5c631832
JK
12783 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12784}
10b3939b 12785
5c631832
JK
12786/* Follow reference attribute ATTR of SRC_DIE.
12787 On entry *REF_CU is the CU of SRC_DIE.
12788 On exit *REF_CU is the CU of the result. */
12789
12790static struct die_info *
12791follow_die_ref (struct die_info *src_die, struct attribute *attr,
12792 struct dwarf2_cu **ref_cu)
12793{
12794 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12795 struct dwarf2_cu *cu = *ref_cu;
12796 struct die_info *die;
12797
12798 die = follow_die_offset (offset, ref_cu);
12799 if (!die)
12800 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12801 "at 0x%x [in module %s]"),
12802 offset, src_die->offset, cu->objfile->name);
348e048f 12803
5c631832
JK
12804 return die;
12805}
12806
12807/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12808 value is intended for DW_OP_call*. */
12809
12810struct dwarf2_locexpr_baton
12811dwarf2_fetch_die_location_block (unsigned int offset,
12812 struct dwarf2_per_cu_data *per_cu)
12813{
12814 struct dwarf2_cu *cu = per_cu->cu;
12815 struct die_info *die;
12816 struct attribute *attr;
12817 struct dwarf2_locexpr_baton retval;
12818
12819 die = follow_die_offset (offset, &cu);
12820 if (!die)
12821 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12822 offset, per_cu->cu->objfile->name);
12823
12824 attr = dwarf2_attr (die, DW_AT_location, cu);
12825 if (!attr)
12826 {
12827 /* DWARF: "If there is no such attribute, then there is no effect.". */
12828
12829 retval.data = NULL;
12830 retval.size = 0;
12831 }
12832 else
12833 {
12834 if (!attr_form_is_block (attr))
12835 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12836 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12837 offset, per_cu->cu->objfile->name);
12838
12839 retval.data = DW_BLOCK (attr)->data;
12840 retval.size = DW_BLOCK (attr)->size;
12841 }
12842 retval.per_cu = cu->per_cu;
12843 return retval;
348e048f
DE
12844}
12845
12846/* Follow the signature attribute ATTR in SRC_DIE.
12847 On entry *REF_CU is the CU of SRC_DIE.
12848 On exit *REF_CU is the CU of the result. */
12849
12850static struct die_info *
12851follow_die_sig (struct die_info *src_die, struct attribute *attr,
12852 struct dwarf2_cu **ref_cu)
12853{
12854 struct objfile *objfile = (*ref_cu)->objfile;
12855 struct die_info temp_die;
12856 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12857 struct dwarf2_cu *sig_cu;
12858 struct die_info *die;
12859
12860 /* sig_type will be NULL if the signatured type is missing from
12861 the debug info. */
12862 if (sig_type == NULL)
12863 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12864 "at 0x%x [in module %s]"),
12865 src_die->offset, objfile->name);
12866
12867 /* If necessary, add it to the queue and load its DIEs. */
12868
12869 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12870 read_signatured_type (objfile, sig_type);
12871
12872 gdb_assert (sig_type->per_cu.cu != NULL);
12873
12874 sig_cu = sig_type->per_cu.cu;
12875 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12876 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12877 if (die)
12878 {
12879 *ref_cu = sig_cu;
12880 return die;
12881 }
12882
12883 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12884 "at 0x%x [in module %s]"),
12885 sig_type->type_offset, src_die->offset, objfile->name);
12886}
12887
12888/* Given an offset of a signatured type, return its signatured_type. */
12889
12890static struct signatured_type *
12891lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12892{
12893 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12894 unsigned int length, initial_length_size;
12895 unsigned int sig_offset;
12896 struct signatured_type find_entry, *type_sig;
12897
12898 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12899 sig_offset = (initial_length_size
12900 + 2 /*version*/
12901 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12902 + 1 /*address_size*/);
12903 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12904 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12905
12906 /* This is only used to lookup previously recorded types.
12907 If we didn't find it, it's our bug. */
12908 gdb_assert (type_sig != NULL);
12909 gdb_assert (offset == type_sig->offset);
12910
12911 return type_sig;
12912}
12913
12914/* Read in signatured type at OFFSET and build its CU and die(s). */
12915
12916static void
12917read_signatured_type_at_offset (struct objfile *objfile,
12918 unsigned int offset)
12919{
12920 struct signatured_type *type_sig;
12921
be391dca
TT
12922 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12923
348e048f
DE
12924 /* We have the section offset, but we need the signature to do the
12925 hash table lookup. */
12926 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12927
12928 gdb_assert (type_sig->per_cu.cu == NULL);
12929
12930 read_signatured_type (objfile, type_sig);
12931
12932 gdb_assert (type_sig->per_cu.cu != NULL);
12933}
12934
12935/* Read in a signatured type and build its CU and DIEs. */
12936
12937static void
12938read_signatured_type (struct objfile *objfile,
12939 struct signatured_type *type_sig)
12940{
1fd400ff 12941 gdb_byte *types_ptr;
348e048f
DE
12942 struct die_reader_specs reader_specs;
12943 struct dwarf2_cu *cu;
12944 ULONGEST signature;
12945 struct cleanup *back_to, *free_cu_cleanup;
12946 struct attribute *attr;
12947
1fd400ff
TT
12948 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12949 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12950
348e048f
DE
12951 gdb_assert (type_sig->per_cu.cu == NULL);
12952
12953 cu = xmalloc (sizeof (struct dwarf2_cu));
12954 memset (cu, 0, sizeof (struct dwarf2_cu));
12955 obstack_init (&cu->comp_unit_obstack);
12956 cu->objfile = objfile;
12957 type_sig->per_cu.cu = cu;
12958 cu->per_cu = &type_sig->per_cu;
12959
12960 /* If an error occurs while loading, release our storage. */
12961 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12962
12963 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12964 types_ptr, objfile->obfd);
12965 gdb_assert (signature == type_sig->signature);
12966
12967 cu->die_hash
12968 = htab_create_alloc_ex (cu->header.length / 12,
12969 die_hash,
12970 die_eq,
12971 NULL,
12972 &cu->comp_unit_obstack,
12973 hashtab_obstack_allocate,
12974 dummy_obstack_deallocate);
12975
12976 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12977 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12978
12979 init_cu_die_reader (&reader_specs, cu);
12980
12981 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12982 NULL /*parent*/);
12983
12984 /* We try not to read any attributes in this function, because not
12985 all objfiles needed for references have been loaded yet, and symbol
12986 table processing isn't initialized. But we have to set the CU language,
12987 or we won't be able to build types correctly. */
12988 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12989 if (attr)
12990 set_cu_language (DW_UNSND (attr), cu);
12991 else
12992 set_cu_language (language_minimal, cu);
12993
12994 do_cleanups (back_to);
12995
12996 /* We've successfully allocated this compilation unit. Let our caller
12997 clean it up when finished with it. */
12998 discard_cleanups (free_cu_cleanup);
12999
13000 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13001 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13002}
13003
c906108c
SS
13004/* Decode simple location descriptions.
13005 Given a pointer to a dwarf block that defines a location, compute
13006 the location and return the value.
13007
4cecd739
DJ
13008 NOTE drow/2003-11-18: This function is called in two situations
13009 now: for the address of static or global variables (partial symbols
13010 only) and for offsets into structures which are expected to be
13011 (more or less) constant. The partial symbol case should go away,
13012 and only the constant case should remain. That will let this
13013 function complain more accurately. A few special modes are allowed
13014 without complaint for global variables (for instance, global
13015 register values and thread-local values).
c906108c
SS
13016
13017 A location description containing no operations indicates that the
4cecd739 13018 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13019 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13020 callers will only want a very basic result and this can become a
13021 complaint.
c906108c 13022
c906108c
SS
13023 Note that stack[0] is unused except as a default error return.
13024 Note that stack overflow is not yet handled. */
13025
13026static CORE_ADDR
e7c27a73 13027decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13028{
e7c27a73 13029 struct objfile *objfile = cu->objfile;
c906108c
SS
13030 int i;
13031 int size = blk->size;
fe1b8b76 13032 gdb_byte *data = blk->data;
c906108c
SS
13033 CORE_ADDR stack[64];
13034 int stacki;
13035 unsigned int bytes_read, unsnd;
fe1b8b76 13036 gdb_byte op;
c906108c
SS
13037
13038 i = 0;
13039 stacki = 0;
13040 stack[stacki] = 0;
c906108c
SS
13041
13042 while (i < size)
13043 {
c906108c
SS
13044 op = data[i++];
13045 switch (op)
13046 {
f1bea926
JM
13047 case DW_OP_lit0:
13048 case DW_OP_lit1:
13049 case DW_OP_lit2:
13050 case DW_OP_lit3:
13051 case DW_OP_lit4:
13052 case DW_OP_lit5:
13053 case DW_OP_lit6:
13054 case DW_OP_lit7:
13055 case DW_OP_lit8:
13056 case DW_OP_lit9:
13057 case DW_OP_lit10:
13058 case DW_OP_lit11:
13059 case DW_OP_lit12:
13060 case DW_OP_lit13:
13061 case DW_OP_lit14:
13062 case DW_OP_lit15:
13063 case DW_OP_lit16:
13064 case DW_OP_lit17:
13065 case DW_OP_lit18:
13066 case DW_OP_lit19:
13067 case DW_OP_lit20:
13068 case DW_OP_lit21:
13069 case DW_OP_lit22:
13070 case DW_OP_lit23:
13071 case DW_OP_lit24:
13072 case DW_OP_lit25:
13073 case DW_OP_lit26:
13074 case DW_OP_lit27:
13075 case DW_OP_lit28:
13076 case DW_OP_lit29:
13077 case DW_OP_lit30:
13078 case DW_OP_lit31:
13079 stack[++stacki] = op - DW_OP_lit0;
13080 break;
13081
c906108c
SS
13082 case DW_OP_reg0:
13083 case DW_OP_reg1:
13084 case DW_OP_reg2:
13085 case DW_OP_reg3:
13086 case DW_OP_reg4:
13087 case DW_OP_reg5:
13088 case DW_OP_reg6:
13089 case DW_OP_reg7:
13090 case DW_OP_reg8:
13091 case DW_OP_reg9:
13092 case DW_OP_reg10:
13093 case DW_OP_reg11:
13094 case DW_OP_reg12:
13095 case DW_OP_reg13:
13096 case DW_OP_reg14:
13097 case DW_OP_reg15:
13098 case DW_OP_reg16:
13099 case DW_OP_reg17:
13100 case DW_OP_reg18:
13101 case DW_OP_reg19:
13102 case DW_OP_reg20:
13103 case DW_OP_reg21:
13104 case DW_OP_reg22:
13105 case DW_OP_reg23:
13106 case DW_OP_reg24:
13107 case DW_OP_reg25:
13108 case DW_OP_reg26:
13109 case DW_OP_reg27:
13110 case DW_OP_reg28:
13111 case DW_OP_reg29:
13112 case DW_OP_reg30:
13113 case DW_OP_reg31:
c906108c 13114 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13115 if (i < size)
13116 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13117 break;
13118
13119 case DW_OP_regx:
c906108c
SS
13120 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13121 i += bytes_read;
c906108c 13122 stack[++stacki] = unsnd;
4cecd739
DJ
13123 if (i < size)
13124 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13125 break;
13126
13127 case DW_OP_addr:
107d2387 13128 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13129 cu, &bytes_read);
107d2387 13130 i += bytes_read;
c906108c
SS
13131 break;
13132
13133 case DW_OP_const1u:
13134 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13135 i += 1;
13136 break;
13137
13138 case DW_OP_const1s:
13139 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13140 i += 1;
13141 break;
13142
13143 case DW_OP_const2u:
13144 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13145 i += 2;
13146 break;
13147
13148 case DW_OP_const2s:
13149 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13150 i += 2;
13151 break;
13152
13153 case DW_OP_const4u:
13154 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13155 i += 4;
13156 break;
13157
13158 case DW_OP_const4s:
13159 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13160 i += 4;
13161 break;
13162
13163 case DW_OP_constu:
13164 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13165 &bytes_read);
c906108c
SS
13166 i += bytes_read;
13167 break;
13168
13169 case DW_OP_consts:
13170 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13171 i += bytes_read;
13172 break;
13173
f1bea926
JM
13174 case DW_OP_dup:
13175 stack[stacki + 1] = stack[stacki];
13176 stacki++;
13177 break;
13178
c906108c
SS
13179 case DW_OP_plus:
13180 stack[stacki - 1] += stack[stacki];
13181 stacki--;
13182 break;
13183
13184 case DW_OP_plus_uconst:
13185 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13186 i += bytes_read;
13187 break;
13188
13189 case DW_OP_minus:
f1bea926 13190 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13191 stacki--;
13192 break;
13193
7a292a7a 13194 case DW_OP_deref:
7a292a7a 13195 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13196 this using GDB's address_class enum. This is valid for partial
13197 global symbols, although the variable's address will be bogus
13198 in the psymtab. */
7a292a7a 13199 if (i < size)
4d3c2250 13200 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13201 break;
13202
9d774e44 13203 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13204 /* The top of the stack has the offset from the beginning
13205 of the thread control block at which the variable is located. */
13206 /* Nothing should follow this operator, so the top of stack would
13207 be returned. */
4cecd739
DJ
13208 /* This is valid for partial global symbols, but the variable's
13209 address will be bogus in the psymtab. */
9d774e44 13210 if (i < size)
4d3c2250 13211 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13212 break;
13213
42be36b3
CT
13214 case DW_OP_GNU_uninit:
13215 break;
13216
c906108c 13217 default:
e2e0b3e5 13218 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13219 dwarf_stack_op_name (op, 1));
c906108c
SS
13220 return (stack[stacki]);
13221 }
13222 }
13223 return (stack[stacki]);
13224}
13225
13226/* memory allocation interface */
13227
c906108c 13228static struct dwarf_block *
7b5a2f43 13229dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13230{
13231 struct dwarf_block *blk;
13232
13233 blk = (struct dwarf_block *)
7b5a2f43 13234 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13235 return (blk);
13236}
13237
13238static struct abbrev_info *
f3dd6933 13239dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13240{
13241 struct abbrev_info *abbrev;
13242
f3dd6933
DJ
13243 abbrev = (struct abbrev_info *)
13244 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13245 memset (abbrev, 0, sizeof (struct abbrev_info));
13246 return (abbrev);
13247}
13248
13249static struct die_info *
b60c80d6 13250dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13251{
13252 struct die_info *die;
b60c80d6
DJ
13253 size_t size = sizeof (struct die_info);
13254
13255 if (num_attrs > 1)
13256 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13257
b60c80d6 13258 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13259 memset (die, 0, sizeof (struct die_info));
13260 return (die);
13261}
2e276125
JB
13262
13263\f
13264/* Macro support. */
13265
13266
13267/* Return the full name of file number I in *LH's file name table.
13268 Use COMP_DIR as the name of the current directory of the
13269 compilation. The result is allocated using xmalloc; the caller is
13270 responsible for freeing it. */
13271static char *
13272file_full_name (int file, struct line_header *lh, const char *comp_dir)
13273{
6a83a1e6
EZ
13274 /* Is the file number a valid index into the line header's file name
13275 table? Remember that file numbers start with one, not zero. */
13276 if (1 <= file && file <= lh->num_file_names)
13277 {
13278 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13279
6a83a1e6
EZ
13280 if (IS_ABSOLUTE_PATH (fe->name))
13281 return xstrdup (fe->name);
13282 else
13283 {
13284 const char *dir;
13285 int dir_len;
13286 char *full_name;
13287
13288 if (fe->dir_index)
13289 dir = lh->include_dirs[fe->dir_index - 1];
13290 else
13291 dir = comp_dir;
13292
13293 if (dir)
13294 {
13295 dir_len = strlen (dir);
13296 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13297 strcpy (full_name, dir);
13298 full_name[dir_len] = '/';
13299 strcpy (full_name + dir_len + 1, fe->name);
13300 return full_name;
13301 }
13302 else
13303 return xstrdup (fe->name);
13304 }
13305 }
2e276125
JB
13306 else
13307 {
6a83a1e6
EZ
13308 /* The compiler produced a bogus file number. We can at least
13309 record the macro definitions made in the file, even if we
13310 won't be able to find the file by name. */
13311 char fake_name[80];
9a619af0 13312
6a83a1e6 13313 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13314
6e70227d 13315 complaint (&symfile_complaints,
6a83a1e6
EZ
13316 _("bad file number in macro information (%d)"),
13317 file);
2e276125 13318
6a83a1e6 13319 return xstrdup (fake_name);
2e276125
JB
13320 }
13321}
13322
13323
13324static struct macro_source_file *
13325macro_start_file (int file, int line,
13326 struct macro_source_file *current_file,
13327 const char *comp_dir,
13328 struct line_header *lh, struct objfile *objfile)
13329{
13330 /* The full name of this source file. */
13331 char *full_name = file_full_name (file, lh, comp_dir);
13332
13333 /* We don't create a macro table for this compilation unit
13334 at all until we actually get a filename. */
13335 if (! pending_macros)
4a146b47 13336 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13337 objfile->macro_cache);
2e276125
JB
13338
13339 if (! current_file)
13340 /* If we have no current file, then this must be the start_file
13341 directive for the compilation unit's main source file. */
13342 current_file = macro_set_main (pending_macros, full_name);
13343 else
13344 current_file = macro_include (current_file, line, full_name);
13345
13346 xfree (full_name);
6e70227d 13347
2e276125
JB
13348 return current_file;
13349}
13350
13351
13352/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13353 followed by a null byte. */
13354static char *
13355copy_string (const char *buf, int len)
13356{
13357 char *s = xmalloc (len + 1);
9a619af0 13358
2e276125
JB
13359 memcpy (s, buf, len);
13360 s[len] = '\0';
2e276125
JB
13361 return s;
13362}
13363
13364
13365static const char *
13366consume_improper_spaces (const char *p, const char *body)
13367{
13368 if (*p == ' ')
13369 {
4d3c2250 13370 complaint (&symfile_complaints,
e2e0b3e5 13371 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13372 body);
2e276125
JB
13373
13374 while (*p == ' ')
13375 p++;
13376 }
13377
13378 return p;
13379}
13380
13381
13382static void
13383parse_macro_definition (struct macro_source_file *file, int line,
13384 const char *body)
13385{
13386 const char *p;
13387
13388 /* The body string takes one of two forms. For object-like macro
13389 definitions, it should be:
13390
13391 <macro name> " " <definition>
13392
13393 For function-like macro definitions, it should be:
13394
13395 <macro name> "() " <definition>
13396 or
13397 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13398
13399 Spaces may appear only where explicitly indicated, and in the
13400 <definition>.
13401
13402 The Dwarf 2 spec says that an object-like macro's name is always
13403 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13404 the space when the macro's definition is the empty string.
2e276125
JB
13405
13406 The Dwarf 2 spec says that there should be no spaces between the
13407 formal arguments in a function-like macro's formal argument list,
13408 but versions of GCC around March 2002 include spaces after the
13409 commas. */
13410
13411
13412 /* Find the extent of the macro name. The macro name is terminated
13413 by either a space or null character (for an object-like macro) or
13414 an opening paren (for a function-like macro). */
13415 for (p = body; *p; p++)
13416 if (*p == ' ' || *p == '(')
13417 break;
13418
13419 if (*p == ' ' || *p == '\0')
13420 {
13421 /* It's an object-like macro. */
13422 int name_len = p - body;
13423 char *name = copy_string (body, name_len);
13424 const char *replacement;
13425
13426 if (*p == ' ')
13427 replacement = body + name_len + 1;
13428 else
13429 {
4d3c2250 13430 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13431 replacement = body + name_len;
13432 }
6e70227d 13433
2e276125
JB
13434 macro_define_object (file, line, name, replacement);
13435
13436 xfree (name);
13437 }
13438 else if (*p == '(')
13439 {
13440 /* It's a function-like macro. */
13441 char *name = copy_string (body, p - body);
13442 int argc = 0;
13443 int argv_size = 1;
13444 char **argv = xmalloc (argv_size * sizeof (*argv));
13445
13446 p++;
13447
13448 p = consume_improper_spaces (p, body);
13449
13450 /* Parse the formal argument list. */
13451 while (*p && *p != ')')
13452 {
13453 /* Find the extent of the current argument name. */
13454 const char *arg_start = p;
13455
13456 while (*p && *p != ',' && *p != ')' && *p != ' ')
13457 p++;
13458
13459 if (! *p || p == arg_start)
4d3c2250 13460 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13461 else
13462 {
13463 /* Make sure argv has room for the new argument. */
13464 if (argc >= argv_size)
13465 {
13466 argv_size *= 2;
13467 argv = xrealloc (argv, argv_size * sizeof (*argv));
13468 }
13469
13470 argv[argc++] = copy_string (arg_start, p - arg_start);
13471 }
13472
13473 p = consume_improper_spaces (p, body);
13474
13475 /* Consume the comma, if present. */
13476 if (*p == ',')
13477 {
13478 p++;
13479
13480 p = consume_improper_spaces (p, body);
13481 }
13482 }
13483
13484 if (*p == ')')
13485 {
13486 p++;
13487
13488 if (*p == ' ')
13489 /* Perfectly formed definition, no complaints. */
13490 macro_define_function (file, line, name,
6e70227d 13491 argc, (const char **) argv,
2e276125
JB
13492 p + 1);
13493 else if (*p == '\0')
13494 {
13495 /* Complain, but do define it. */
4d3c2250 13496 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13497 macro_define_function (file, line, name,
6e70227d 13498 argc, (const char **) argv,
2e276125
JB
13499 p);
13500 }
13501 else
13502 /* Just complain. */
4d3c2250 13503 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13504 }
13505 else
13506 /* Just complain. */
4d3c2250 13507 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13508
13509 xfree (name);
13510 {
13511 int i;
13512
13513 for (i = 0; i < argc; i++)
13514 xfree (argv[i]);
13515 }
13516 xfree (argv);
13517 }
13518 else
4d3c2250 13519 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13520}
13521
13522
13523static void
13524dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13525 char *comp_dir, bfd *abfd,
e7c27a73 13526 struct dwarf2_cu *cu)
2e276125 13527{
fe1b8b76 13528 gdb_byte *mac_ptr, *mac_end;
2e276125 13529 struct macro_source_file *current_file = 0;
757a13d0
JK
13530 enum dwarf_macinfo_record_type macinfo_type;
13531 int at_commandline;
2e276125 13532
be391dca
TT
13533 dwarf2_read_section (dwarf2_per_objfile->objfile,
13534 &dwarf2_per_objfile->macinfo);
dce234bc 13535 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13536 {
e2e0b3e5 13537 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13538 return;
13539 }
13540
757a13d0
JK
13541 /* First pass: Find the name of the base filename.
13542 This filename is needed in order to process all macros whose definition
13543 (or undefinition) comes from the command line. These macros are defined
13544 before the first DW_MACINFO_start_file entry, and yet still need to be
13545 associated to the base file.
13546
13547 To determine the base file name, we scan the macro definitions until we
13548 reach the first DW_MACINFO_start_file entry. We then initialize
13549 CURRENT_FILE accordingly so that any macro definition found before the
13550 first DW_MACINFO_start_file can still be associated to the base file. */
13551
dce234bc
PP
13552 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13553 mac_end = dwarf2_per_objfile->macinfo.buffer
13554 + dwarf2_per_objfile->macinfo.size;
2e276125 13555
757a13d0 13556 do
2e276125 13557 {
2e276125
JB
13558 /* Do we at least have room for a macinfo type byte? */
13559 if (mac_ptr >= mac_end)
13560 {
757a13d0
JK
13561 /* Complaint is printed during the second pass as GDB will probably
13562 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13563 break;
2e276125
JB
13564 }
13565
13566 macinfo_type = read_1_byte (abfd, mac_ptr);
13567 mac_ptr++;
13568
13569 switch (macinfo_type)
13570 {
13571 /* A zero macinfo type indicates the end of the macro
13572 information. */
13573 case 0:
757a13d0
JK
13574 break;
13575
13576 case DW_MACINFO_define:
13577 case DW_MACINFO_undef:
13578 /* Only skip the data by MAC_PTR. */
13579 {
13580 unsigned int bytes_read;
13581
13582 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13583 mac_ptr += bytes_read;
9b1c24c8 13584 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13585 mac_ptr += bytes_read;
13586 }
13587 break;
13588
13589 case DW_MACINFO_start_file:
13590 {
13591 unsigned int bytes_read;
13592 int line, file;
13593
13594 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13595 mac_ptr += bytes_read;
13596 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13597 mac_ptr += bytes_read;
13598
13599 current_file = macro_start_file (file, line, current_file, comp_dir,
13600 lh, cu->objfile);
13601 }
13602 break;
13603
13604 case DW_MACINFO_end_file:
13605 /* No data to skip by MAC_PTR. */
13606 break;
13607
13608 case DW_MACINFO_vendor_ext:
13609 /* Only skip the data by MAC_PTR. */
13610 {
13611 unsigned int bytes_read;
13612
13613 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13614 mac_ptr += bytes_read;
9b1c24c8 13615 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13616 mac_ptr += bytes_read;
13617 }
13618 break;
13619
13620 default:
13621 break;
13622 }
13623 } while (macinfo_type != 0 && current_file == NULL);
13624
13625 /* Second pass: Process all entries.
13626
13627 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13628 command-line macro definitions/undefinitions. This flag is unset when we
13629 reach the first DW_MACINFO_start_file entry. */
13630
dce234bc 13631 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13632
13633 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13634 GDB is still reading the definitions from command line. First
13635 DW_MACINFO_start_file will need to be ignored as it was already executed
13636 to create CURRENT_FILE for the main source holding also the command line
13637 definitions. On first met DW_MACINFO_start_file this flag is reset to
13638 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13639
13640 at_commandline = 1;
13641
13642 do
13643 {
13644 /* Do we at least have room for a macinfo type byte? */
13645 if (mac_ptr >= mac_end)
13646 {
13647 dwarf2_macros_too_long_complaint ();
13648 break;
13649 }
13650
13651 macinfo_type = read_1_byte (abfd, mac_ptr);
13652 mac_ptr++;
13653
13654 switch (macinfo_type)
13655 {
13656 /* A zero macinfo type indicates the end of the macro
13657 information. */
13658 case 0:
13659 break;
2e276125
JB
13660
13661 case DW_MACINFO_define:
13662 case DW_MACINFO_undef:
13663 {
891d2f0b 13664 unsigned int bytes_read;
2e276125
JB
13665 int line;
13666 char *body;
13667
13668 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13669 mac_ptr += bytes_read;
9b1c24c8 13670 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13671 mac_ptr += bytes_read;
13672
13673 if (! current_file)
757a13d0
JK
13674 {
13675 /* DWARF violation as no main source is present. */
13676 complaint (&symfile_complaints,
13677 _("debug info with no main source gives macro %s "
13678 "on line %d: %s"),
6e70227d
DE
13679 macinfo_type == DW_MACINFO_define ?
13680 _("definition") :
905e0470
PM
13681 macinfo_type == DW_MACINFO_undef ?
13682 _("undefinition") :
13683 _("something-or-other"), line, body);
757a13d0
JK
13684 break;
13685 }
13686 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13687 complaint (&symfile_complaints,
757a13d0
JK
13688 _("debug info gives %s macro %s with %s line %d: %s"),
13689 at_commandline ? _("command-line") : _("in-file"),
905e0470 13690 macinfo_type == DW_MACINFO_define ?
6e70227d 13691 _("definition") :
905e0470
PM
13692 macinfo_type == DW_MACINFO_undef ?
13693 _("undefinition") :
13694 _("something-or-other"),
757a13d0
JK
13695 line == 0 ? _("zero") : _("non-zero"), line, body);
13696
13697 if (macinfo_type == DW_MACINFO_define)
13698 parse_macro_definition (current_file, line, body);
13699 else if (macinfo_type == DW_MACINFO_undef)
13700 macro_undef (current_file, line, body);
2e276125
JB
13701 }
13702 break;
13703
13704 case DW_MACINFO_start_file:
13705 {
891d2f0b 13706 unsigned int bytes_read;
2e276125
JB
13707 int line, file;
13708
13709 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13710 mac_ptr += bytes_read;
13711 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13712 mac_ptr += bytes_read;
13713
757a13d0
JK
13714 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13715 complaint (&symfile_complaints,
13716 _("debug info gives source %d included "
13717 "from %s at %s line %d"),
13718 file, at_commandline ? _("command-line") : _("file"),
13719 line == 0 ? _("zero") : _("non-zero"), line);
13720
13721 if (at_commandline)
13722 {
13723 /* This DW_MACINFO_start_file was executed in the pass one. */
13724 at_commandline = 0;
13725 }
13726 else
13727 current_file = macro_start_file (file, line,
13728 current_file, comp_dir,
13729 lh, cu->objfile);
2e276125
JB
13730 }
13731 break;
13732
13733 case DW_MACINFO_end_file:
13734 if (! current_file)
4d3c2250 13735 complaint (&symfile_complaints,
e2e0b3e5 13736 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13737 else
13738 {
13739 current_file = current_file->included_by;
13740 if (! current_file)
13741 {
13742 enum dwarf_macinfo_record_type next_type;
13743
13744 /* GCC circa March 2002 doesn't produce the zero
13745 type byte marking the end of the compilation
13746 unit. Complain if it's not there, but exit no
13747 matter what. */
13748
13749 /* Do we at least have room for a macinfo type byte? */
13750 if (mac_ptr >= mac_end)
13751 {
4d3c2250 13752 dwarf2_macros_too_long_complaint ();
2e276125
JB
13753 return;
13754 }
13755
13756 /* We don't increment mac_ptr here, so this is just
13757 a look-ahead. */
13758 next_type = read_1_byte (abfd, mac_ptr);
13759 if (next_type != 0)
4d3c2250 13760 complaint (&symfile_complaints,
e2e0b3e5 13761 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13762
13763 return;
13764 }
13765 }
13766 break;
13767
13768 case DW_MACINFO_vendor_ext:
13769 {
891d2f0b 13770 unsigned int bytes_read;
2e276125
JB
13771 int constant;
13772 char *string;
13773
13774 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13775 mac_ptr += bytes_read;
9b1c24c8 13776 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13777 mac_ptr += bytes_read;
13778
13779 /* We don't recognize any vendor extensions. */
13780 }
13781 break;
13782 }
757a13d0 13783 } while (macinfo_type != 0);
2e276125 13784}
8e19ed76
PS
13785
13786/* Check if the attribute's form is a DW_FORM_block*
13787 if so return true else false. */
13788static int
13789attr_form_is_block (struct attribute *attr)
13790{
13791 return (attr == NULL ? 0 :
13792 attr->form == DW_FORM_block1
13793 || attr->form == DW_FORM_block2
13794 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13795 || attr->form == DW_FORM_block
13796 || attr->form == DW_FORM_exprloc);
8e19ed76 13797}
4c2df51b 13798
c6a0999f
JB
13799/* Return non-zero if ATTR's value is a section offset --- classes
13800 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13801 You may use DW_UNSND (attr) to retrieve such offsets.
13802
13803 Section 7.5.4, "Attribute Encodings", explains that no attribute
13804 may have a value that belongs to more than one of these classes; it
13805 would be ambiguous if we did, because we use the same forms for all
13806 of them. */
3690dd37
JB
13807static int
13808attr_form_is_section_offset (struct attribute *attr)
13809{
13810 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13811 || attr->form == DW_FORM_data8
13812 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13813}
13814
13815
13816/* Return non-zero if ATTR's value falls in the 'constant' class, or
13817 zero otherwise. When this function returns true, you can apply
13818 dwarf2_get_attr_constant_value to it.
13819
13820 However, note that for some attributes you must check
13821 attr_form_is_section_offset before using this test. DW_FORM_data4
13822 and DW_FORM_data8 are members of both the constant class, and of
13823 the classes that contain offsets into other debug sections
13824 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13825 that, if an attribute's can be either a constant or one of the
13826 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13827 taken as section offsets, not constants. */
13828static int
13829attr_form_is_constant (struct attribute *attr)
13830{
13831 switch (attr->form)
13832 {
13833 case DW_FORM_sdata:
13834 case DW_FORM_udata:
13835 case DW_FORM_data1:
13836 case DW_FORM_data2:
13837 case DW_FORM_data4:
13838 case DW_FORM_data8:
13839 return 1;
13840 default:
13841 return 0;
13842 }
13843}
13844
4c2df51b
DJ
13845static void
13846dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13847 struct dwarf2_cu *cu)
4c2df51b 13848{
3690dd37 13849 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13850 /* ".debug_loc" may not exist at all, or the offset may be outside
13851 the section. If so, fall through to the complaint in the
13852 other branch. */
dce234bc 13853 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13854 {
0d53c4c4 13855 struct dwarf2_loclist_baton *baton;
4c2df51b 13856
4a146b47 13857 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13858 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13859 baton->per_cu = cu->per_cu;
13860 gdb_assert (baton->per_cu);
4c2df51b 13861
be391dca
TT
13862 dwarf2_read_section (dwarf2_per_objfile->objfile,
13863 &dwarf2_per_objfile->loc);
13864
0d53c4c4
DJ
13865 /* We don't know how long the location list is, but make sure we
13866 don't run off the edge of the section. */
dce234bc
PP
13867 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13868 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13869 baton->base_address = cu->base_address;
13870 if (cu->base_known == 0)
0d53c4c4 13871 complaint (&symfile_complaints,
e2e0b3e5 13872 _("Location list used without specifying the CU base address."));
4c2df51b 13873
768a979c 13874 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13875 SYMBOL_LOCATION_BATON (sym) = baton;
13876 }
13877 else
13878 {
13879 struct dwarf2_locexpr_baton *baton;
13880
4a146b47 13881 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13882 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13883 baton->per_cu = cu->per_cu;
13884 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13885
13886 if (attr_form_is_block (attr))
13887 {
13888 /* Note that we're just copying the block's data pointer
13889 here, not the actual data. We're still pointing into the
6502dd73
DJ
13890 info_buffer for SYM's objfile; right now we never release
13891 that buffer, but when we do clean up properly this may
13892 need to change. */
0d53c4c4
DJ
13893 baton->size = DW_BLOCK (attr)->size;
13894 baton->data = DW_BLOCK (attr)->data;
13895 }
13896 else
13897 {
13898 dwarf2_invalid_attrib_class_complaint ("location description",
13899 SYMBOL_NATURAL_NAME (sym));
13900 baton->size = 0;
13901 baton->data = NULL;
13902 }
6e70227d 13903
768a979c 13904 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13905 SYMBOL_LOCATION_BATON (sym) = baton;
13906 }
4c2df51b 13907}
6502dd73 13908
9aa1f1e3
TT
13909/* Return the OBJFILE associated with the compilation unit CU. If CU
13910 came from a separate debuginfo file, then the master objfile is
13911 returned. */
ae0d2f24
UW
13912
13913struct objfile *
13914dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13915{
9291a0cd 13916 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13917
13918 /* Return the master objfile, so that we can report and look up the
13919 correct file containing this variable. */
13920 if (objfile->separate_debug_objfile_backlink)
13921 objfile = objfile->separate_debug_objfile_backlink;
13922
13923 return objfile;
13924}
13925
13926/* Return the address size given in the compilation unit header for CU. */
13927
13928CORE_ADDR
13929dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13930{
13931 if (per_cu->cu)
13932 return per_cu->cu->header.addr_size;
13933 else
13934 {
13935 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13936 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13937 struct dwarf2_per_objfile *per_objfile
13938 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13939 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13940 struct comp_unit_head cu_header;
9a619af0 13941
ae0d2f24
UW
13942 memset (&cu_header, 0, sizeof cu_header);
13943 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13944 return cu_header.addr_size;
13945 }
13946}
13947
9eae7c52
TT
13948/* Return the offset size given in the compilation unit header for CU. */
13949
13950int
13951dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13952{
13953 if (per_cu->cu)
13954 return per_cu->cu->header.offset_size;
13955 else
13956 {
13957 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13958 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
13959 struct dwarf2_per_objfile *per_objfile
13960 = objfile_data (objfile, dwarf2_objfile_data_key);
13961 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13962 struct comp_unit_head cu_header;
13963
13964 memset (&cu_header, 0, sizeof cu_header);
13965 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13966 return cu_header.offset_size;
13967 }
13968}
13969
9aa1f1e3
TT
13970/* Return the text offset of the CU. The returned offset comes from
13971 this CU's objfile. If this objfile came from a separate debuginfo
13972 file, then the offset may be different from the corresponding
13973 offset in the parent objfile. */
13974
13975CORE_ADDR
13976dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13977{
bb3fa9d0 13978 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
13979
13980 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13981}
13982
348e048f
DE
13983/* Locate the .debug_info compilation unit from CU's objfile which contains
13984 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
13985
13986static struct dwarf2_per_cu_data *
c764a876 13987dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
13988 struct objfile *objfile)
13989{
13990 struct dwarf2_per_cu_data *this_cu;
13991 int low, high;
13992
ae038cb0
DJ
13993 low = 0;
13994 high = dwarf2_per_objfile->n_comp_units - 1;
13995 while (high > low)
13996 {
13997 int mid = low + (high - low) / 2;
9a619af0 13998
ae038cb0
DJ
13999 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14000 high = mid;
14001 else
14002 low = mid + 1;
14003 }
14004 gdb_assert (low == high);
14005 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14006 {
10b3939b 14007 if (low == 0)
8a3fe4f8
AC
14008 error (_("Dwarf Error: could not find partial DIE containing "
14009 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14010 (long) offset, bfd_get_filename (objfile->obfd));
14011
ae038cb0
DJ
14012 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14013 return dwarf2_per_objfile->all_comp_units[low-1];
14014 }
14015 else
14016 {
14017 this_cu = dwarf2_per_objfile->all_comp_units[low];
14018 if (low == dwarf2_per_objfile->n_comp_units - 1
14019 && offset >= this_cu->offset + this_cu->length)
c764a876 14020 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14021 gdb_assert (offset < this_cu->offset + this_cu->length);
14022 return this_cu;
14023 }
14024}
14025
10b3939b
DJ
14026/* Locate the compilation unit from OBJFILE which is located at exactly
14027 OFFSET. Raises an error on failure. */
14028
ae038cb0 14029static struct dwarf2_per_cu_data *
c764a876 14030dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14031{
14032 struct dwarf2_per_cu_data *this_cu;
9a619af0 14033
ae038cb0
DJ
14034 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14035 if (this_cu->offset != offset)
c764a876 14036 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14037 return this_cu;
14038}
14039
93311388
DE
14040/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14041
14042static struct dwarf2_cu *
14043alloc_one_comp_unit (struct objfile *objfile)
14044{
14045 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14046 cu->objfile = objfile;
14047 obstack_init (&cu->comp_unit_obstack);
14048 return cu;
14049}
14050
ae038cb0
DJ
14051/* Release one cached compilation unit, CU. We unlink it from the tree
14052 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14053 the caller is responsible for that.
14054 NOTE: DATA is a void * because this function is also used as a
14055 cleanup routine. */
ae038cb0
DJ
14056
14057static void
14058free_one_comp_unit (void *data)
14059{
14060 struct dwarf2_cu *cu = data;
14061
14062 if (cu->per_cu != NULL)
14063 cu->per_cu->cu = NULL;
14064 cu->per_cu = NULL;
14065
14066 obstack_free (&cu->comp_unit_obstack, NULL);
14067
14068 xfree (cu);
14069}
14070
72bf9492 14071/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14072 when we're finished with it. We can't free the pointer itself, but be
14073 sure to unlink it from the cache. Also release any associated storage
14074 and perform cache maintenance.
72bf9492
DJ
14075
14076 Only used during partial symbol parsing. */
14077
14078static void
14079free_stack_comp_unit (void *data)
14080{
14081 struct dwarf2_cu *cu = data;
14082
14083 obstack_free (&cu->comp_unit_obstack, NULL);
14084 cu->partial_dies = NULL;
ae038cb0
DJ
14085
14086 if (cu->per_cu != NULL)
14087 {
14088 /* This compilation unit is on the stack in our caller, so we
14089 should not xfree it. Just unlink it. */
14090 cu->per_cu->cu = NULL;
14091 cu->per_cu = NULL;
14092
14093 /* If we had a per-cu pointer, then we may have other compilation
14094 units loaded, so age them now. */
14095 age_cached_comp_units ();
14096 }
14097}
14098
14099/* Free all cached compilation units. */
14100
14101static void
14102free_cached_comp_units (void *data)
14103{
14104 struct dwarf2_per_cu_data *per_cu, **last_chain;
14105
14106 per_cu = dwarf2_per_objfile->read_in_chain;
14107 last_chain = &dwarf2_per_objfile->read_in_chain;
14108 while (per_cu != NULL)
14109 {
14110 struct dwarf2_per_cu_data *next_cu;
14111
14112 next_cu = per_cu->cu->read_in_chain;
14113
14114 free_one_comp_unit (per_cu->cu);
14115 *last_chain = next_cu;
14116
14117 per_cu = next_cu;
14118 }
14119}
14120
14121/* Increase the age counter on each cached compilation unit, and free
14122 any that are too old. */
14123
14124static void
14125age_cached_comp_units (void)
14126{
14127 struct dwarf2_per_cu_data *per_cu, **last_chain;
14128
14129 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14130 per_cu = dwarf2_per_objfile->read_in_chain;
14131 while (per_cu != NULL)
14132 {
14133 per_cu->cu->last_used ++;
14134 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14135 dwarf2_mark (per_cu->cu);
14136 per_cu = per_cu->cu->read_in_chain;
14137 }
14138
14139 per_cu = dwarf2_per_objfile->read_in_chain;
14140 last_chain = &dwarf2_per_objfile->read_in_chain;
14141 while (per_cu != NULL)
14142 {
14143 struct dwarf2_per_cu_data *next_cu;
14144
14145 next_cu = per_cu->cu->read_in_chain;
14146
14147 if (!per_cu->cu->mark)
14148 {
14149 free_one_comp_unit (per_cu->cu);
14150 *last_chain = next_cu;
14151 }
14152 else
14153 last_chain = &per_cu->cu->read_in_chain;
14154
14155 per_cu = next_cu;
14156 }
14157}
14158
14159/* Remove a single compilation unit from the cache. */
14160
14161static void
14162free_one_cached_comp_unit (void *target_cu)
14163{
14164 struct dwarf2_per_cu_data *per_cu, **last_chain;
14165
14166 per_cu = dwarf2_per_objfile->read_in_chain;
14167 last_chain = &dwarf2_per_objfile->read_in_chain;
14168 while (per_cu != NULL)
14169 {
14170 struct dwarf2_per_cu_data *next_cu;
14171
14172 next_cu = per_cu->cu->read_in_chain;
14173
14174 if (per_cu->cu == target_cu)
14175 {
14176 free_one_comp_unit (per_cu->cu);
14177 *last_chain = next_cu;
14178 break;
14179 }
14180 else
14181 last_chain = &per_cu->cu->read_in_chain;
14182
14183 per_cu = next_cu;
14184 }
14185}
14186
fe3e1990
DJ
14187/* Release all extra memory associated with OBJFILE. */
14188
14189void
14190dwarf2_free_objfile (struct objfile *objfile)
14191{
14192 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14193
14194 if (dwarf2_per_objfile == NULL)
14195 return;
14196
14197 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14198 free_cached_comp_units (NULL);
14199
9291a0cd
TT
14200 if (dwarf2_per_objfile->using_index)
14201 {
14202 int i;
14203
14204 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14205 {
14206 int j;
14207 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14208
14209 if (!cu->v.quick->lines)
14210 continue;
14211
14212 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
14213 {
14214 if (cu->v.quick->file_names)
14215 xfree ((void *) cu->v.quick->file_names[j]);
14216 if (cu->v.quick->full_names)
14217 xfree ((void *) cu->v.quick->full_names[j]);
14218 }
14219
14220 free_line_header (cu->v.quick->lines);
14221 }
14222 }
14223
fe3e1990
DJ
14224 /* Everything else should be on the objfile obstack. */
14225}
14226
1c379e20
DJ
14227/* A pair of DIE offset and GDB type pointer. We store these
14228 in a hash table separate from the DIEs, and preserve them
14229 when the DIEs are flushed out of cache. */
14230
14231struct dwarf2_offset_and_type
14232{
14233 unsigned int offset;
14234 struct type *type;
14235};
14236
14237/* Hash function for a dwarf2_offset_and_type. */
14238
14239static hashval_t
14240offset_and_type_hash (const void *item)
14241{
14242 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14243
1c379e20
DJ
14244 return ofs->offset;
14245}
14246
14247/* Equality function for a dwarf2_offset_and_type. */
14248
14249static int
14250offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14251{
14252 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14253 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14254
1c379e20
DJ
14255 return ofs_lhs->offset == ofs_rhs->offset;
14256}
14257
14258/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14259 table if necessary. For convenience, return TYPE.
14260
14261 The DIEs reading must have careful ordering to:
14262 * Not cause infite loops trying to read in DIEs as a prerequisite for
14263 reading current DIE.
14264 * Not trying to dereference contents of still incompletely read in types
14265 while reading in other DIEs.
14266 * Enable referencing still incompletely read in types just by a pointer to
14267 the type without accessing its fields.
14268
14269 Therefore caller should follow these rules:
14270 * Try to fetch any prerequisite types we may need to build this DIE type
14271 before building the type and calling set_die_type.
e71ec853 14272 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14273 possible before fetching more types to complete the current type.
14274 * Make the type as complete as possible before fetching more types. */
1c379e20 14275
f792889a 14276static struct type *
1c379e20
DJ
14277set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14278{
14279 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14280 struct objfile *objfile = cu->objfile;
14281 htab_t *type_hash_ptr;
1c379e20 14282
b4ba55a1
JB
14283 /* For Ada types, make sure that the gnat-specific data is always
14284 initialized (if not already set). There are a few types where
14285 we should not be doing so, because the type-specific area is
14286 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14287 where the type-specific area is used to store the floatformat).
14288 But this is not a problem, because the gnat-specific information
14289 is actually not needed for these types. */
14290 if (need_gnat_info (cu)
14291 && TYPE_CODE (type) != TYPE_CODE_FUNC
14292 && TYPE_CODE (type) != TYPE_CODE_FLT
14293 && !HAVE_GNAT_AUX_INFO (type))
14294 INIT_GNAT_SPECIFIC (type);
14295
673bfd45
DE
14296 if (cu->per_cu->from_debug_types)
14297 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14298 else
14299 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14300
14301 if (*type_hash_ptr == NULL)
f792889a 14302 {
673bfd45
DE
14303 *type_hash_ptr
14304 = htab_create_alloc_ex (127,
f792889a
DJ
14305 offset_and_type_hash,
14306 offset_and_type_eq,
14307 NULL,
673bfd45 14308 &objfile->objfile_obstack,
f792889a
DJ
14309 hashtab_obstack_allocate,
14310 dummy_obstack_deallocate);
f792889a 14311 }
1c379e20
DJ
14312
14313 ofs.offset = die->offset;
14314 ofs.type = type;
14315 slot = (struct dwarf2_offset_and_type **)
673bfd45 14316 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14317 if (*slot)
14318 complaint (&symfile_complaints,
14319 _("A problem internal to GDB: DIE 0x%x has type already set"),
14320 die->offset);
673bfd45 14321 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14322 **slot = ofs;
f792889a 14323 return type;
1c379e20
DJ
14324}
14325
673bfd45
DE
14326/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14327 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14328
14329static struct type *
673bfd45
DE
14330get_die_type_at_offset (unsigned int offset,
14331 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14332{
14333 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14334 htab_t type_hash;
f792889a 14335
673bfd45
DE
14336 if (per_cu->from_debug_types)
14337 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14338 else
14339 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14340 if (type_hash == NULL)
14341 return NULL;
1c379e20 14342
673bfd45 14343 ofs.offset = offset;
1c379e20
DJ
14344 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14345 if (slot)
14346 return slot->type;
14347 else
14348 return NULL;
14349}
14350
673bfd45
DE
14351/* Look up the type for DIE in the appropriate type_hash table,
14352 or return NULL if DIE does not have a saved type. */
14353
14354static struct type *
14355get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14356{
14357 return get_die_type_at_offset (die->offset, cu->per_cu);
14358}
14359
10b3939b
DJ
14360/* Add a dependence relationship from CU to REF_PER_CU. */
14361
14362static void
14363dwarf2_add_dependence (struct dwarf2_cu *cu,
14364 struct dwarf2_per_cu_data *ref_per_cu)
14365{
14366 void **slot;
14367
14368 if (cu->dependencies == NULL)
14369 cu->dependencies
14370 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14371 NULL, &cu->comp_unit_obstack,
14372 hashtab_obstack_allocate,
14373 dummy_obstack_deallocate);
14374
14375 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14376 if (*slot == NULL)
14377 *slot = ref_per_cu;
14378}
1c379e20 14379
f504f079
DE
14380/* Subroutine of dwarf2_mark to pass to htab_traverse.
14381 Set the mark field in every compilation unit in the
ae038cb0
DJ
14382 cache that we must keep because we are keeping CU. */
14383
10b3939b
DJ
14384static int
14385dwarf2_mark_helper (void **slot, void *data)
14386{
14387 struct dwarf2_per_cu_data *per_cu;
14388
14389 per_cu = (struct dwarf2_per_cu_data *) *slot;
14390 if (per_cu->cu->mark)
14391 return 1;
14392 per_cu->cu->mark = 1;
14393
14394 if (per_cu->cu->dependencies != NULL)
14395 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14396
14397 return 1;
14398}
14399
f504f079
DE
14400/* Set the mark field in CU and in every other compilation unit in the
14401 cache that we must keep because we are keeping CU. */
14402
ae038cb0
DJ
14403static void
14404dwarf2_mark (struct dwarf2_cu *cu)
14405{
14406 if (cu->mark)
14407 return;
14408 cu->mark = 1;
10b3939b
DJ
14409 if (cu->dependencies != NULL)
14410 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14411}
14412
14413static void
14414dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14415{
14416 while (per_cu)
14417 {
14418 per_cu->cu->mark = 0;
14419 per_cu = per_cu->cu->read_in_chain;
14420 }
72bf9492
DJ
14421}
14422
72bf9492
DJ
14423/* Trivial hash function for partial_die_info: the hash value of a DIE
14424 is its offset in .debug_info for this objfile. */
14425
14426static hashval_t
14427partial_die_hash (const void *item)
14428{
14429 const struct partial_die_info *part_die = item;
9a619af0 14430
72bf9492
DJ
14431 return part_die->offset;
14432}
14433
14434/* Trivial comparison function for partial_die_info structures: two DIEs
14435 are equal if they have the same offset. */
14436
14437static int
14438partial_die_eq (const void *item_lhs, const void *item_rhs)
14439{
14440 const struct partial_die_info *part_die_lhs = item_lhs;
14441 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14442
72bf9492
DJ
14443 return part_die_lhs->offset == part_die_rhs->offset;
14444}
14445
ae038cb0
DJ
14446static struct cmd_list_element *set_dwarf2_cmdlist;
14447static struct cmd_list_element *show_dwarf2_cmdlist;
14448
14449static void
14450set_dwarf2_cmd (char *args, int from_tty)
14451{
14452 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14453}
14454
14455static void
14456show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14457{
ae038cb0
DJ
14458 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14459}
14460
dce234bc
PP
14461/* If section described by INFO was mmapped, munmap it now. */
14462
14463static void
14464munmap_section_buffer (struct dwarf2_section_info *info)
14465{
14466 if (info->was_mmapped)
14467 {
14468#ifdef HAVE_MMAP
14469 intptr_t begin = (intptr_t) info->buffer;
14470 intptr_t map_begin = begin & ~(pagesize - 1);
14471 size_t map_length = info->size + begin - map_begin;
9a619af0 14472
dce234bc
PP
14473 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14474#else
14475 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14476 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14477#endif
14478 }
14479}
14480
14481/* munmap debug sections for OBJFILE, if necessary. */
14482
14483static void
c1bd65d0 14484dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14485{
14486 struct dwarf2_per_objfile *data = d;
9a619af0 14487
dce234bc
PP
14488 munmap_section_buffer (&data->info);
14489 munmap_section_buffer (&data->abbrev);
14490 munmap_section_buffer (&data->line);
14491 munmap_section_buffer (&data->str);
14492 munmap_section_buffer (&data->macinfo);
14493 munmap_section_buffer (&data->ranges);
14494 munmap_section_buffer (&data->loc);
14495 munmap_section_buffer (&data->frame);
14496 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14497 munmap_section_buffer (&data->gdb_index);
14498}
14499
14500\f
14501
14502/* The contents of the hash table we create when building the string
14503 table. */
14504struct strtab_entry
14505{
14506 offset_type offset;
14507 const char *str;
14508};
14509
14510/* Hash function for a strtab_entry. */
14511static hashval_t
14512hash_strtab_entry (const void *e)
14513{
14514 const struct strtab_entry *entry = e;
14515 return mapped_index_string_hash (entry->str);
14516}
14517
14518/* Equality function for a strtab_entry. */
14519static int
14520eq_strtab_entry (const void *a, const void *b)
14521{
14522 const struct strtab_entry *ea = a;
14523 const struct strtab_entry *eb = b;
14524 return !strcmp (ea->str, eb->str);
14525}
14526
14527/* Create a strtab_entry hash table. */
14528static htab_t
14529create_strtab (void)
14530{
14531 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14532 xfree, xcalloc, xfree);
14533}
14534
14535/* Add a string to the constant pool. Return the string's offset in
14536 host order. */
14537static offset_type
14538add_string (htab_t table, struct obstack *cpool, const char *str)
14539{
14540 void **slot;
14541 struct strtab_entry entry;
14542 struct strtab_entry *result;
14543
14544 entry.str = str;
14545 slot = htab_find_slot (table, &entry, INSERT);
14546 if (*slot)
14547 result = *slot;
14548 else
14549 {
14550 result = XNEW (struct strtab_entry);
14551 result->offset = obstack_object_size (cpool);
14552 result->str = str;
14553 obstack_grow_str0 (cpool, str);
14554 *slot = result;
14555 }
14556 return result->offset;
14557}
14558
14559/* An entry in the symbol table. */
14560struct symtab_index_entry
14561{
14562 /* The name of the symbol. */
14563 const char *name;
14564 /* The offset of the name in the constant pool. */
14565 offset_type index_offset;
14566 /* A sorted vector of the indices of all the CUs that hold an object
14567 of this name. */
14568 VEC (offset_type) *cu_indices;
14569};
14570
14571/* The symbol table. This is a power-of-2-sized hash table. */
14572struct mapped_symtab
14573{
14574 offset_type n_elements;
14575 offset_type size;
14576 struct symtab_index_entry **data;
14577};
14578
14579/* Hash function for a symtab_index_entry. */
14580static hashval_t
14581hash_symtab_entry (const void *e)
14582{
14583 const struct symtab_index_entry *entry = e;
14584 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14585 sizeof (offset_type) * VEC_length (offset_type,
14586 entry->cu_indices),
14587 0);
14588}
14589
14590/* Equality function for a symtab_index_entry. */
14591static int
14592eq_symtab_entry (const void *a, const void *b)
14593{
14594 const struct symtab_index_entry *ea = a;
14595 const struct symtab_index_entry *eb = b;
14596 int len = VEC_length (offset_type, ea->cu_indices);
14597 if (len != VEC_length (offset_type, eb->cu_indices))
14598 return 0;
14599 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14600 VEC_address (offset_type, eb->cu_indices),
14601 sizeof (offset_type) * len);
14602}
14603
14604/* Destroy a symtab_index_entry. */
14605static void
14606delete_symtab_entry (void *p)
14607{
14608 struct symtab_index_entry *entry = p;
14609 VEC_free (offset_type, entry->cu_indices);
14610 xfree (entry);
14611}
14612
14613/* Create a hash table holding symtab_index_entry objects. */
14614static htab_t
14615create_index_table (void)
14616{
14617 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14618 delete_symtab_entry, xcalloc, xfree);
14619}
14620
14621/* Create a new mapped symtab object. */
14622static struct mapped_symtab *
14623create_mapped_symtab (void)
14624{
14625 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14626 symtab->n_elements = 0;
14627 symtab->size = 1024;
14628 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14629 return symtab;
14630}
14631
14632/* Destroy a mapped_symtab. */
14633static void
14634cleanup_mapped_symtab (void *p)
14635{
14636 struct mapped_symtab *symtab = p;
14637 /* The contents of the array are freed when the other hash table is
14638 destroyed. */
14639 xfree (symtab->data);
14640 xfree (symtab);
14641}
14642
14643/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14644 the slot. */
14645static struct symtab_index_entry **
14646find_slot (struct mapped_symtab *symtab, const char *name)
14647{
14648 offset_type index, step, hash = mapped_index_string_hash (name);
14649
14650 index = hash & (symtab->size - 1);
14651 step = ((hash * 17) & (symtab->size - 1)) | 1;
14652
14653 for (;;)
14654 {
14655 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14656 return &symtab->data[index];
14657 index = (index + step) & (symtab->size - 1);
14658 }
14659}
14660
14661/* Expand SYMTAB's hash table. */
14662static void
14663hash_expand (struct mapped_symtab *symtab)
14664{
14665 offset_type old_size = symtab->size;
14666 offset_type i;
14667 struct symtab_index_entry **old_entries = symtab->data;
14668
14669 symtab->size *= 2;
14670 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14671
14672 for (i = 0; i < old_size; ++i)
14673 {
14674 if (old_entries[i])
14675 {
14676 struct symtab_index_entry **slot = find_slot (symtab,
14677 old_entries[i]->name);
14678 *slot = old_entries[i];
14679 }
14680 }
14681
14682 xfree (old_entries);
14683}
14684
14685/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14686 is the index of the CU in which the symbol appears. */
14687static void
14688add_index_entry (struct mapped_symtab *symtab, const char *name,
14689 offset_type cu_index)
14690{
14691 struct symtab_index_entry **slot;
14692
14693 ++symtab->n_elements;
14694 if (4 * symtab->n_elements / 3 >= symtab->size)
14695 hash_expand (symtab);
14696
14697 slot = find_slot (symtab, name);
14698 if (!*slot)
14699 {
14700 *slot = XNEW (struct symtab_index_entry);
14701 (*slot)->name = name;
14702 (*slot)->cu_indices = NULL;
14703 }
14704 /* Don't push an index twice. Due to how we add entries we only
14705 have to check the last one. */
14706 if (VEC_empty (offset_type, (*slot)->cu_indices)
14707 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14708 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14709}
14710
14711/* Add a vector of indices to the constant pool. */
14712static offset_type
14713add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14714 struct symtab_index_entry *entry)
14715{
14716 void **slot;
14717
14718 slot = htab_find_slot (index_table, entry, INSERT);
14719 if (!*slot)
14720 {
14721 offset_type len = VEC_length (offset_type, entry->cu_indices);
14722 offset_type val = MAYBE_SWAP (len);
14723 offset_type iter;
14724 int i;
14725
14726 *slot = entry;
14727 entry->index_offset = obstack_object_size (cpool);
14728
14729 obstack_grow (cpool, &val, sizeof (val));
14730 for (i = 0;
14731 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14732 ++i)
14733 {
14734 val = MAYBE_SWAP (iter);
14735 obstack_grow (cpool, &val, sizeof (val));
14736 }
14737 }
14738 else
14739 {
14740 struct symtab_index_entry *old_entry = *slot;
14741 entry->index_offset = old_entry->index_offset;
14742 entry = old_entry;
14743 }
14744 return entry->index_offset;
14745}
14746
14747/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14748 constant pool entries going into the obstack CPOOL. */
14749static void
14750write_hash_table (struct mapped_symtab *symtab,
14751 struct obstack *output, struct obstack *cpool)
14752{
14753 offset_type i;
14754 htab_t index_table;
14755 htab_t str_table;
14756
14757 index_table = create_index_table ();
14758 str_table = create_strtab ();
14759 /* We add all the index vectors to the constant pool first, to
14760 ensure alignment is ok. */
14761 for (i = 0; i < symtab->size; ++i)
14762 {
14763 if (symtab->data[i])
14764 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14765 }
14766
14767 /* Now write out the hash table. */
14768 for (i = 0; i < symtab->size; ++i)
14769 {
14770 offset_type str_off, vec_off;
14771
14772 if (symtab->data[i])
14773 {
14774 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14775 vec_off = symtab->data[i]->index_offset;
14776 }
14777 else
14778 {
14779 /* While 0 is a valid constant pool index, it is not valid
14780 to have 0 for both offsets. */
14781 str_off = 0;
14782 vec_off = 0;
14783 }
14784
14785 str_off = MAYBE_SWAP (str_off);
14786 vec_off = MAYBE_SWAP (vec_off);
14787
14788 obstack_grow (output, &str_off, sizeof (str_off));
14789 obstack_grow (output, &vec_off, sizeof (vec_off));
14790 }
14791
14792 htab_delete (str_table);
14793 htab_delete (index_table);
14794}
14795
14796/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14797 from PST; CU_INDEX is the index of the CU in the vector of all
14798 CUs. */
14799static void
14800add_address_entry (struct objfile *objfile,
14801 struct obstack *addr_obstack, struct partial_symtab *pst,
14802 unsigned int cu_index)
14803{
14804 offset_type offset;
14805 char addr[8];
14806 CORE_ADDR baseaddr;
14807
1fd400ff
TT
14808 /* Don't bother recording empty ranges. */
14809 if (pst->textlow == pst->texthigh)
14810 return;
14811
9291a0cd
TT
14812 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14813
14814 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14815 obstack_grow (addr_obstack, addr, 8);
14816 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14817 obstack_grow (addr_obstack, addr, 8);
14818 offset = MAYBE_SWAP (cu_index);
14819 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14820}
14821
14822/* Add a list of partial symbols to SYMTAB. */
14823static void
14824write_psymbols (struct mapped_symtab *symtab,
14825 struct partial_symbol **psymp,
14826 int count,
14827 offset_type cu_index)
14828{
14829 for (; count-- > 0; ++psymp)
14830 {
14831 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14832 error (_("Ada is not currently supported by the index"));
14833 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14834 }
14835}
14836
14837/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14838 exception if there is an error. */
14839static void
14840write_obstack (FILE *file, struct obstack *obstack)
14841{
14842 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14843 file)
14844 != obstack_object_size (obstack))
14845 error (_("couldn't data write to file"));
14846}
14847
14848/* Unlink a file if the argument is not NULL. */
14849static void
14850unlink_if_set (void *p)
14851{
14852 char **filename = p;
14853 if (*filename)
14854 unlink (*filename);
14855}
14856
1fd400ff
TT
14857/* A helper struct used when iterating over debug_types. */
14858struct signatured_type_index_data
14859{
14860 struct objfile *objfile;
14861 struct mapped_symtab *symtab;
14862 struct obstack *types_list;
14863 int cu_index;
14864};
14865
14866/* A helper function that writes a single signatured_type to an
14867 obstack. */
14868static int
14869write_one_signatured_type (void **slot, void *d)
14870{
14871 struct signatured_type_index_data *info = d;
14872 struct signatured_type *entry = (struct signatured_type *) *slot;
14873 struct dwarf2_per_cu_data *cu = &entry->per_cu;
14874 struct partial_symtab *psymtab = cu->v.psymtab;
14875 gdb_byte val[8];
14876
14877 write_psymbols (info->symtab,
14878 info->objfile->global_psymbols.list + psymtab->globals_offset,
14879 psymtab->n_global_syms, info->cu_index);
14880 write_psymbols (info->symtab,
14881 info->objfile->static_psymbols.list + psymtab->statics_offset,
14882 psymtab->n_static_syms, info->cu_index);
14883
14884 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14885 obstack_grow (info->types_list, val, 8);
14886 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14887 obstack_grow (info->types_list, val, 8);
14888 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14889 obstack_grow (info->types_list, val, 8);
14890
14891 ++info->cu_index;
14892
14893 return 1;
14894}
14895
9291a0cd
TT
14896/* Create an index file for OBJFILE in the directory DIR. */
14897static void
14898write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14899{
14900 struct cleanup *cleanup;
14901 char *filename, *cleanup_filename;
1fd400ff
TT
14902 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14903 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14904 int i;
14905 FILE *out_file;
14906 struct mapped_symtab *symtab;
14907 offset_type val, size_of_contents, total_len;
14908 struct stat st;
14909 char buf[8];
14910
14911 if (!objfile->psymtabs)
14912 return;
14913 if (dwarf2_per_objfile->using_index)
14914 error (_("Cannot use an index to create the index"));
14915
14916 if (stat (objfile->name, &st) < 0)
14917 perror_with_name (_("Could not stat"));
14918
14919 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14920 INDEX_SUFFIX, (char *) NULL);
14921 cleanup = make_cleanup (xfree, filename);
14922
14923 out_file = fopen (filename, "wb");
14924 if (!out_file)
14925 error (_("Can't open `%s' for writing"), filename);
14926
14927 cleanup_filename = filename;
14928 make_cleanup (unlink_if_set, &cleanup_filename);
14929
14930 symtab = create_mapped_symtab ();
14931 make_cleanup (cleanup_mapped_symtab, symtab);
14932
14933 obstack_init (&addr_obstack);
14934 make_cleanup_obstack_free (&addr_obstack);
14935
14936 obstack_init (&cu_list);
14937 make_cleanup_obstack_free (&cu_list);
14938
1fd400ff
TT
14939 obstack_init (&types_cu_list);
14940 make_cleanup_obstack_free (&types_cu_list);
14941
14942 /* The list is already sorted, so we don't need to do additional
14943 work here. Also, the debug_types entries do not appear in
14944 all_comp_units, but only in their own hash table. */
9291a0cd
TT
14945 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14946 {
14947 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14948 struct partial_symtab *psymtab = cu->v.psymtab;
14949 gdb_byte val[8];
14950
14951 write_psymbols (symtab,
14952 objfile->global_psymbols.list + psymtab->globals_offset,
14953 psymtab->n_global_syms, i);
14954 write_psymbols (symtab,
14955 objfile->static_psymbols.list + psymtab->statics_offset,
14956 psymtab->n_static_syms, i);
14957
14958 add_address_entry (objfile, &addr_obstack, psymtab, i);
14959
14960 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14961 obstack_grow (&cu_list, val, 8);
14962 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14963 obstack_grow (&cu_list, val, 8);
14964 }
14965
1fd400ff
TT
14966 /* Write out the .debug_type entries, if any. */
14967 if (dwarf2_per_objfile->signatured_types)
14968 {
14969 struct signatured_type_index_data sig_data;
14970
14971 sig_data.objfile = objfile;
14972 sig_data.symtab = symtab;
14973 sig_data.types_list = &types_cu_list;
14974 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14975 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14976 write_one_signatured_type, &sig_data);
14977 }
14978
9291a0cd
TT
14979 obstack_init (&constant_pool);
14980 make_cleanup_obstack_free (&constant_pool);
14981 obstack_init (&symtab_obstack);
14982 make_cleanup_obstack_free (&symtab_obstack);
14983 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14984
14985 obstack_init (&contents);
14986 make_cleanup_obstack_free (&contents);
1fd400ff 14987 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
14988 total_len = size_of_contents;
14989
14990 /* The version number. */
1fd400ff 14991 val = MAYBE_SWAP (2);
9291a0cd
TT
14992 obstack_grow (&contents, &val, sizeof (val));
14993
14994 /* The offset of the CU list from the start of the file. */
14995 val = MAYBE_SWAP (total_len);
14996 obstack_grow (&contents, &val, sizeof (val));
14997 total_len += obstack_object_size (&cu_list);
14998
1fd400ff
TT
14999 /* The offset of the types CU list from the start of the file. */
15000 val = MAYBE_SWAP (total_len);
15001 obstack_grow (&contents, &val, sizeof (val));
15002 total_len += obstack_object_size (&types_cu_list);
15003
9291a0cd
TT
15004 /* The offset of the address table from the start of the file. */
15005 val = MAYBE_SWAP (total_len);
15006 obstack_grow (&contents, &val, sizeof (val));
15007 total_len += obstack_object_size (&addr_obstack);
15008
15009 /* The offset of the symbol table from the start of the file. */
15010 val = MAYBE_SWAP (total_len);
15011 obstack_grow (&contents, &val, sizeof (val));
15012 total_len += obstack_object_size (&symtab_obstack);
15013
15014 /* The offset of the constant pool from the start of the file. */
15015 val = MAYBE_SWAP (total_len);
15016 obstack_grow (&contents, &val, sizeof (val));
15017 total_len += obstack_object_size (&constant_pool);
15018
15019 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15020
15021 write_obstack (out_file, &contents);
15022 write_obstack (out_file, &cu_list);
1fd400ff 15023 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15024 write_obstack (out_file, &addr_obstack);
15025 write_obstack (out_file, &symtab_obstack);
15026 write_obstack (out_file, &constant_pool);
15027
15028 fclose (out_file);
15029
15030 /* We want to keep the file, so we set cleanup_filename to NULL
15031 here. See unlink_if_set. */
15032 cleanup_filename = NULL;
15033
15034 do_cleanups (cleanup);
15035}
15036
15037/* The mapped index file format is designed to be directly mmap()able
15038 on any architecture. In most cases, a datum is represented using a
15039 little-endian 32-bit integer value, called an offset_type. Big
15040 endian machines must byte-swap the values before using them.
15041 Exceptions to this rule are noted. The data is laid out such that
15042 alignment is always respected.
15043
15044 A mapped index consists of several sections.
15045
15046 1. The file header. This is a sequence of values, of offset_type
15047 unless otherwise noted:
1fd400ff
TT
15048 [0] The version number. Currently 1 or 2. The differences are
15049 noted below. Version 1 did not account for .debug_types sections;
15050 the presence of a .debug_types section invalidates any version 1
15051 index that may exist.
9291a0cd 15052 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
15053 [1.5] In version 2, the offset, from the start of the file, of the
15054 types CU list. This offset does not appear in version 1. Note
15055 that this can be empty, in which case this offset will be equal to
15056 the next offset.
9291a0cd
TT
15057 [2] The offset, from the start of the file, of the address section.
15058 [3] The offset, from the start of the file, of the symbol table.
15059 [4] The offset, from the start of the file, of the constant pool.
15060
15061 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15062 little-endian values, sorted by the CU offset. The first element
15063 in each pair is the offset of a CU in the .debug_info section. The
15064 second element in each pair is the length of that CU. References
15065 to a CU elsewhere in the map are done using a CU index, which is
15066 just the 0-based index into this table. Note that if there are
15067 type CUs, then conceptually CUs and type CUs form a single list for
15068 the purposes of CU indices.
15069
15070 2.5 The types CU list. This does not appear in a version 1 index.
15071 This is a sequence of triplets of 64-bit little-endian values. In
15072 a triplet, the first value is the CU offset, the second value is
15073 the type offset in the CU, and the third value is the type
15074 signature. The types CU list is not sorted.
9291a0cd
TT
15075
15076 3. The address section. The address section consists of a sequence
15077 of address entries. Each address entry has three elements.
15078 [0] The low address. This is a 64-bit little-endian value.
15079 [1] The high address. This is a 64-bit little-endian value.
15080 [2] The CU index. This is an offset_type value.
15081
15082 4. The symbol table. This is a hash table. The size of the hash
15083 table is always a power of 2. The initial hash and the step are
15084 currently defined by the `find_slot' function.
15085
15086 Each slot in the hash table consists of a pair of offset_type
15087 values. The first value is the offset of the symbol's name in the
15088 constant pool. The second value is the offset of the CU vector in
15089 the constant pool.
15090
15091 If both values are 0, then this slot in the hash table is empty.
15092 This is ok because while 0 is a valid constant pool index, it
15093 cannot be a valid index for both a string and a CU vector.
15094
15095 A string in the constant pool is stored as a \0-terminated string,
15096 as you'd expect.
15097
15098 A CU vector in the constant pool is a sequence of offset_type
15099 values. The first value is the number of CU indices in the vector.
15100 Each subsequent value is the index of a CU in the CU list. This
15101 element in the hash table is used to indicate which CUs define the
15102 symbol.
15103
15104 5. The constant pool. This is simply a bunch of bytes. It is
15105 organized so that alignment is correct: CU vectors are stored
15106 first, followed by strings. */
15107static void
15108save_gdb_index_command (char *arg, int from_tty)
15109{
15110 struct objfile *objfile;
15111
15112 if (!arg || !*arg)
96d19272 15113 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15114
15115 ALL_OBJFILES (objfile)
15116 {
15117 struct stat st;
15118
15119 /* If the objfile does not correspond to an actual file, skip it. */
15120 if (stat (objfile->name, &st) < 0)
15121 continue;
15122
15123 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15124 if (dwarf2_per_objfile)
15125 {
15126 volatile struct gdb_exception except;
15127
15128 TRY_CATCH (except, RETURN_MASK_ERROR)
15129 {
15130 write_psymtabs_to_index (objfile, arg);
15131 }
15132 if (except.reason < 0)
15133 exception_fprintf (gdb_stderr, except,
15134 _("Error while writing index for `%s': "),
15135 objfile->name);
15136 }
15137 }
dce234bc
PP
15138}
15139
9291a0cd
TT
15140\f
15141
9eae7c52
TT
15142int dwarf2_always_disassemble;
15143
15144static void
15145show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15146 struct cmd_list_element *c, const char *value)
15147{
15148 fprintf_filtered (file, _("\
15149Whether to always disassemble DWARF expressions is %s.\n"),
15150 value);
15151}
15152
6502dd73
DJ
15153void _initialize_dwarf2_read (void);
15154
15155void
15156_initialize_dwarf2_read (void)
15157{
96d19272
JK
15158 struct cmd_list_element *c;
15159
dce234bc 15160 dwarf2_objfile_data_key
c1bd65d0 15161 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15162
1bedd215
AC
15163 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15164Set DWARF 2 specific variables.\n\
15165Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15166 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15167 0/*allow-unknown*/, &maintenance_set_cmdlist);
15168
1bedd215
AC
15169 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15170Show DWARF 2 specific variables\n\
15171Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15172 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15173 0/*allow-unknown*/, &maintenance_show_cmdlist);
15174
15175 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15176 &dwarf2_max_cache_age, _("\
15177Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15178Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15179A higher limit means that cached compilation units will be stored\n\
15180in memory longer, and more total memory will be used. Zero disables\n\
15181caching, which can slow down startup."),
2c5b56ce 15182 NULL,
920d2a44 15183 show_dwarf2_max_cache_age,
2c5b56ce 15184 &set_dwarf2_cmdlist,
ae038cb0 15185 &show_dwarf2_cmdlist);
d97bc12b 15186
9eae7c52
TT
15187 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15188 &dwarf2_always_disassemble, _("\
15189Set whether `info address' always disassembles DWARF expressions."), _("\
15190Show whether `info address' always disassembles DWARF expressions."), _("\
15191When enabled, DWARF expressions are always printed in an assembly-like\n\
15192syntax. When disabled, expressions will be printed in a more\n\
15193conversational style, when possible."),
15194 NULL,
15195 show_dwarf2_always_disassemble,
15196 &set_dwarf2_cmdlist,
15197 &show_dwarf2_cmdlist);
15198
d97bc12b
DE
15199 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15200Set debugging of the dwarf2 DIE reader."), _("\
15201Show debugging of the dwarf2 DIE reader."), _("\
15202When enabled (non-zero), DIEs are dumped after they are read in.\n\
15203The value is the maximum depth to print."),
15204 NULL,
15205 NULL,
15206 &setdebuglist, &showdebuglist);
9291a0cd 15207
96d19272
JK
15208 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15209 _("Save a .gdb-index file"),
15210 &save_cmdlist);
15211 set_cmd_completer (c, filename_completer);
6502dd73 15212}
This page took 1.989804 seconds and 4 git commands to generate.