* dwarf2read.c: Whitespace cleanup, plus comment additions.
[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;
3876f04e
DE
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
9291a0cd
TT
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 206
ae2de4f8
DE
207 /* True if we are using the mapped index,
208 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
209 unsigned char using_index;
210
ae2de4f8 211 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 212 struct mapped_index *index_table;
98bfdba5
PA
213
214 /* Set during partial symbol reading, to prevent queueing of full
215 symbols. */
216 int reading_partial_symbols;
673bfd45
DE
217
218 /* Table mapping type .debug_info DIE offsets to types.
219 This is NULL if not allocated yet.
220 It (currently) makes sense to allocate debug_types_type_hash lazily.
221 To keep things simple we allocate both lazily. */
222 htab_t debug_info_type_hash;
223
224 /* Table mapping type .debug_types DIE offsets to types.
225 This is NULL if not allocated yet. */
226 htab_t debug_types_type_hash;
6502dd73
DJ
227};
228
229static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
230
231/* names of the debugging sections */
232
233a11ab
CS
233/* Note that if the debugging section has been compressed, it might
234 have a name like .zdebug_info. */
235
236#define INFO_SECTION "debug_info"
237#define ABBREV_SECTION "debug_abbrev"
238#define LINE_SECTION "debug_line"
233a11ab
CS
239#define LOC_SECTION "debug_loc"
240#define MACINFO_SECTION "debug_macinfo"
241#define STR_SECTION "debug_str"
242#define RANGES_SECTION "debug_ranges"
348e048f 243#define TYPES_SECTION "debug_types"
233a11ab
CS
244#define FRAME_SECTION "debug_frame"
245#define EH_FRAME_SECTION "eh_frame"
9291a0cd 246#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
247
248/* local data types */
249
57349743
JB
250/* We hold several abbreviation tables in memory at the same time. */
251#ifndef ABBREV_HASH_SIZE
252#define ABBREV_HASH_SIZE 121
253#endif
254
107d2387
AC
255/* The data in a compilation unit header, after target2host
256 translation, looks like this. */
c906108c 257struct comp_unit_head
a738430d 258{
c764a876 259 unsigned int length;
a738430d 260 short version;
a738430d
MK
261 unsigned char addr_size;
262 unsigned char signed_addr_p;
9cbfa09e 263 unsigned int abbrev_offset;
57349743 264
a738430d
MK
265 /* Size of file offsets; either 4 or 8. */
266 unsigned int offset_size;
57349743 267
a738430d
MK
268 /* Size of the length field; either 4 or 12. */
269 unsigned int initial_length_size;
57349743 270
a738430d
MK
271 /* Offset to the first byte of this compilation unit header in the
272 .debug_info section, for resolving relative reference dies. */
273 unsigned int offset;
57349743 274
d00adf39
DE
275 /* Offset to first die in this cu from the start of the cu.
276 This will be the first byte following the compilation unit header. */
277 unsigned int first_die_offset;
a738430d 278};
c906108c 279
3da10d80
KS
280/* Type used for delaying computation of method physnames.
281 See comments for compute_delayed_physnames. */
282struct delayed_method_info
283{
284 /* The type to which the method is attached, i.e., its parent class. */
285 struct type *type;
286
287 /* The index of the method in the type's function fieldlists. */
288 int fnfield_index;
289
290 /* The index of the method in the fieldlist. */
291 int index;
292
293 /* The name of the DIE. */
294 const char *name;
295
296 /* The DIE associated with this method. */
297 struct die_info *die;
298};
299
300typedef struct delayed_method_info delayed_method_info;
301DEF_VEC_O (delayed_method_info);
302
e7c27a73
DJ
303/* Internal state when decoding a particular compilation unit. */
304struct dwarf2_cu
305{
306 /* The objfile containing this compilation unit. */
307 struct objfile *objfile;
308
d00adf39 309 /* The header of the compilation unit. */
e7c27a73 310 struct comp_unit_head header;
e142c38c 311
d00adf39
DE
312 /* Base address of this compilation unit. */
313 CORE_ADDR base_address;
314
315 /* Non-zero if base_address has been set. */
316 int base_known;
317
e142c38c
DJ
318 struct function_range *first_fn, *last_fn, *cached_fn;
319
320 /* The language we are debugging. */
321 enum language language;
322 const struct language_defn *language_defn;
323
b0f35d58
DL
324 const char *producer;
325
e142c38c
DJ
326 /* The generic symbol table building routines have separate lists for
327 file scope symbols and all all other scopes (local scopes). So
328 we need to select the right one to pass to add_symbol_to_list().
329 We do it by keeping a pointer to the correct list in list_in_scope.
330
331 FIXME: The original dwarf code just treated the file scope as the
332 first local scope, and all other local scopes as nested local
333 scopes, and worked fine. Check to see if we really need to
334 distinguish these in buildsym.c. */
335 struct pending **list_in_scope;
336
f3dd6933
DJ
337 /* DWARF abbreviation table associated with this compilation unit. */
338 struct abbrev_info **dwarf2_abbrevs;
339
340 /* Storage for the abbrev table. */
341 struct obstack abbrev_obstack;
72bf9492
DJ
342
343 /* Hash table holding all the loaded partial DIEs. */
344 htab_t partial_dies;
345
346 /* Storage for things with the same lifetime as this read-in compilation
347 unit, including partial DIEs. */
348 struct obstack comp_unit_obstack;
349
ae038cb0
DJ
350 /* When multiple dwarf2_cu structures are living in memory, this field
351 chains them all together, so that they can be released efficiently.
352 We will probably also want a generation counter so that most-recently-used
353 compilation units are cached... */
354 struct dwarf2_per_cu_data *read_in_chain;
355
356 /* Backchain to our per_cu entry if the tree has been built. */
357 struct dwarf2_per_cu_data *per_cu;
358
359 /* How many compilation units ago was this CU last referenced? */
360 int last_used;
361
10b3939b 362 /* A hash table of die offsets for following references. */
51545339 363 htab_t die_hash;
10b3939b
DJ
364
365 /* Full DIEs if read in. */
366 struct die_info *dies;
367
368 /* A set of pointers to dwarf2_per_cu_data objects for compilation
369 units referenced by this one. Only set during full symbol processing;
370 partial symbol tables do not have dependencies. */
371 htab_t dependencies;
372
cb1df416
DJ
373 /* Header data from the line table, during full symbol processing. */
374 struct line_header *line_header;
375
3da10d80
KS
376 /* A list of methods which need to have physnames computed
377 after all type information has been read. */
378 VEC (delayed_method_info) *method_list;
379
ae038cb0
DJ
380 /* Mark used when releasing cached dies. */
381 unsigned int mark : 1;
382
383 /* This flag will be set if this compilation unit might include
384 inter-compilation-unit references. */
385 unsigned int has_form_ref_addr : 1;
386
72bf9492
DJ
387 /* This flag will be set if this compilation unit includes any
388 DW_TAG_namespace DIEs. If we know that there are explicit
389 DIEs for namespaces, we don't need to try to infer them
390 from mangled names. */
391 unsigned int has_namespace_info : 1;
e7c27a73
DJ
392};
393
9291a0cd
TT
394/* When using the index (and thus not using psymtabs), each CU has an
395 object of this type. This is used to hold information needed by
396 the various "quick" methods. */
397struct dwarf2_per_cu_quick_data
398{
399 /* The line table. This can be NULL if there was no line table. */
400 struct line_header *lines;
401
402 /* The file names from the line table. */
403 const char **file_names;
404 /* The file names from the line table after being run through
405 gdb_realpath. */
406 const char **full_names;
407
408 /* The corresponding symbol table. This is NULL if symbols for this
409 CU have not yet been read. */
410 struct symtab *symtab;
411
412 /* A temporary mark bit used when iterating over all CUs in
413 expand_symtabs_matching. */
414 unsigned int mark : 1;
415
416 /* True if we've tried to read the line table. */
417 unsigned int read_lines : 1;
418};
419
10b3939b
DJ
420/* Persistent data held for a compilation unit, even when not
421 processing it. We put a pointer to this structure in the
422 read_symtab_private field of the psymtab. If we encounter
423 inter-compilation-unit references, we also maintain a sorted
424 list of all compilation units. */
425
ae038cb0
DJ
426struct dwarf2_per_cu_data
427{
348e048f 428 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 429 bytes should suffice to store the length of any compilation unit
45452591
DE
430 - if it doesn't, GDB will fall over anyway.
431 NOTE: Unlike comp_unit_head.length, this length includes
432 initial_length_size. */
c764a876 433 unsigned int offset;
348e048f 434 unsigned int length : 29;
ae038cb0
DJ
435
436 /* Flag indicating this compilation unit will be read in before
437 any of the current compilation units are processed. */
c764a876 438 unsigned int queued : 1;
ae038cb0 439
5afb4e99
DJ
440 /* This flag will be set if we need to load absolutely all DIEs
441 for this compilation unit, instead of just the ones we think
442 are interesting. It gets set if we look for a DIE in the
443 hash table and don't find it. */
444 unsigned int load_all_dies : 1;
445
348e048f
DE
446 /* Non-zero if this CU is from .debug_types.
447 Otherwise it's from .debug_info. */
448 unsigned int from_debug_types : 1;
449
17ea53c3
JK
450 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
451 of the CU cache it gets reset to NULL again. */
ae038cb0 452 struct dwarf2_cu *cu;
1c379e20 453
9291a0cd
TT
454 /* The corresponding objfile. */
455 struct objfile *objfile;
456
457 /* When using partial symbol tables, the 'psymtab' field is active.
458 Otherwise the 'quick' field is active. */
459 union
460 {
461 /* The partial symbol table associated with this compilation unit,
462 or NULL for partial units (which do not have an associated
463 symtab). */
464 struct partial_symtab *psymtab;
465
466 /* Data needed by the "quick" functions. */
467 struct dwarf2_per_cu_quick_data *quick;
468 } v;
ae038cb0
DJ
469};
470
348e048f
DE
471/* Entry in the signatured_types hash table. */
472
473struct signatured_type
474{
475 ULONGEST signature;
476
477 /* Offset in .debug_types of the TU (type_unit) for this type. */
478 unsigned int offset;
479
480 /* Offset in .debug_types of the type defined by this TU. */
481 unsigned int type_offset;
482
483 /* The CU(/TU) of this type. */
484 struct dwarf2_per_cu_data per_cu;
485};
486
93311388
DE
487/* Struct used to pass misc. parameters to read_die_and_children, et. al.
488 which are used for both .debug_info and .debug_types dies.
489 All parameters here are unchanging for the life of the call.
490 This struct exists to abstract away the constant parameters of
491 die reading. */
492
493struct die_reader_specs
494{
495 /* The bfd of this objfile. */
496 bfd* abfd;
497
498 /* The CU of the DIE we are parsing. */
499 struct dwarf2_cu *cu;
500
501 /* Pointer to start of section buffer.
502 This is either the start of .debug_info or .debug_types. */
503 const gdb_byte *buffer;
504};
505
debd256d
JB
506/* The line number information for a compilation unit (found in the
507 .debug_line section) begins with a "statement program header",
508 which contains the following information. */
509struct line_header
510{
511 unsigned int total_length;
512 unsigned short version;
513 unsigned int header_length;
514 unsigned char minimum_instruction_length;
2dc7f7b3 515 unsigned char maximum_ops_per_instruction;
debd256d
JB
516 unsigned char default_is_stmt;
517 int line_base;
518 unsigned char line_range;
519 unsigned char opcode_base;
520
521 /* standard_opcode_lengths[i] is the number of operands for the
522 standard opcode whose value is i. This means that
523 standard_opcode_lengths[0] is unused, and the last meaningful
524 element is standard_opcode_lengths[opcode_base - 1]. */
525 unsigned char *standard_opcode_lengths;
526
527 /* The include_directories table. NOTE! These strings are not
528 allocated with xmalloc; instead, they are pointers into
529 debug_line_buffer. If you try to free them, `free' will get
530 indigestion. */
531 unsigned int num_include_dirs, include_dirs_size;
532 char **include_dirs;
533
534 /* The file_names table. NOTE! These strings are not allocated
535 with xmalloc; instead, they are pointers into debug_line_buffer.
536 Don't try to free them directly. */
537 unsigned int num_file_names, file_names_size;
538 struct file_entry
c906108c 539 {
debd256d
JB
540 char *name;
541 unsigned int dir_index;
542 unsigned int mod_time;
543 unsigned int length;
aaa75496 544 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 545 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
546 } *file_names;
547
548 /* The start and end of the statement program following this
6502dd73 549 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 550 gdb_byte *statement_program_start, *statement_program_end;
debd256d 551};
c906108c
SS
552
553/* When we construct a partial symbol table entry we only
554 need this much information. */
555struct partial_die_info
556 {
72bf9492 557 /* Offset of this DIE. */
c906108c 558 unsigned int offset;
72bf9492
DJ
559
560 /* DWARF-2 tag for this DIE. */
561 ENUM_BITFIELD(dwarf_tag) tag : 16;
562
72bf9492
DJ
563 /* Assorted flags describing the data found in this DIE. */
564 unsigned int has_children : 1;
565 unsigned int is_external : 1;
566 unsigned int is_declaration : 1;
567 unsigned int has_type : 1;
568 unsigned int has_specification : 1;
569 unsigned int has_pc_info : 1;
570
571 /* Flag set if the SCOPE field of this structure has been
572 computed. */
573 unsigned int scope_set : 1;
574
fa4028e9
JB
575 /* Flag set if the DIE has a byte_size attribute. */
576 unsigned int has_byte_size : 1;
577
98bfdba5
PA
578 /* Flag set if any of the DIE's children are template arguments. */
579 unsigned int has_template_arguments : 1;
580
abc72ce4
DE
581 /* Flag set if fixup_partial_die has been called on this die. */
582 unsigned int fixup_called : 1;
583
72bf9492 584 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 585 sometimes a default name for unnamed DIEs. */
c906108c 586 char *name;
72bf9492 587
abc72ce4
DE
588 /* The linkage name, if present. */
589 const char *linkage_name;
590
72bf9492
DJ
591 /* The scope to prepend to our children. This is generally
592 allocated on the comp_unit_obstack, so will disappear
593 when this compilation unit leaves the cache. */
594 char *scope;
595
596 /* The location description associated with this DIE, if any. */
597 struct dwarf_block *locdesc;
598
599 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
600 CORE_ADDR lowpc;
601 CORE_ADDR highpc;
72bf9492 602
93311388 603 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 604 DW_AT_sibling, if any. */
abc72ce4
DE
605 /* NOTE: This member isn't strictly necessary, read_partial_die could
606 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 607 gdb_byte *sibling;
72bf9492
DJ
608
609 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
610 DW_AT_specification (or DW_AT_abstract_origin or
611 DW_AT_extension). */
612 unsigned int spec_offset;
613
614 /* Pointers to this DIE's parent, first child, and next sibling,
615 if any. */
616 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
617 };
618
619/* This data structure holds the information of an abbrev. */
620struct abbrev_info
621 {
622 unsigned int number; /* number identifying abbrev */
623 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
624 unsigned short has_children; /* boolean */
625 unsigned short num_attrs; /* number of attributes */
c906108c
SS
626 struct attr_abbrev *attrs; /* an array of attribute descriptions */
627 struct abbrev_info *next; /* next in chain */
628 };
629
630struct attr_abbrev
631 {
9d25dd43
DE
632 ENUM_BITFIELD(dwarf_attribute) name : 16;
633 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
634 };
635
b60c80d6
DJ
636/* Attributes have a name and a value */
637struct attribute
638 {
9d25dd43 639 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
640 ENUM_BITFIELD(dwarf_form) form : 15;
641
642 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
643 field should be in u.str (existing only for DW_STRING) but it is kept
644 here for better struct attribute alignment. */
645 unsigned int string_is_canonical : 1;
646
b60c80d6
DJ
647 union
648 {
649 char *str;
650 struct dwarf_block *blk;
43bbcdc2
PH
651 ULONGEST unsnd;
652 LONGEST snd;
b60c80d6 653 CORE_ADDR addr;
348e048f 654 struct signatured_type *signatured_type;
b60c80d6
DJ
655 }
656 u;
657 };
658
c906108c
SS
659/* This data structure holds a complete die structure. */
660struct die_info
661 {
76815b17
DE
662 /* DWARF-2 tag for this DIE. */
663 ENUM_BITFIELD(dwarf_tag) tag : 16;
664
665 /* Number of attributes */
98bfdba5
PA
666 unsigned char num_attrs;
667
668 /* True if we're presently building the full type name for the
669 type derived from this DIE. */
670 unsigned char building_fullname : 1;
76815b17
DE
671
672 /* Abbrev number */
673 unsigned int abbrev;
674
93311388 675 /* Offset in .debug_info or .debug_types section. */
76815b17 676 unsigned int offset;
78ba4af6
JB
677
678 /* The dies in a compilation unit form an n-ary tree. PARENT
679 points to this die's parent; CHILD points to the first child of
680 this node; and all the children of a given node are chained
4950bc1c 681 together via their SIBLING fields. */
639d11d3
DC
682 struct die_info *child; /* Its first child, if any. */
683 struct die_info *sibling; /* Its next sibling, if any. */
684 struct die_info *parent; /* Its parent, if any. */
c906108c 685
b60c80d6
DJ
686 /* An array of attributes, with NUM_ATTRS elements. There may be
687 zero, but it's not common and zero-sized arrays are not
688 sufficiently portable C. */
689 struct attribute attrs[1];
c906108c
SS
690 };
691
5fb290d7
DJ
692struct function_range
693{
694 const char *name;
695 CORE_ADDR lowpc, highpc;
696 int seen_line;
697 struct function_range *next;
698};
699
c906108c
SS
700/* Get at parts of an attribute structure */
701
702#define DW_STRING(attr) ((attr)->u.str)
8285870a 703#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
704#define DW_UNSND(attr) ((attr)->u.unsnd)
705#define DW_BLOCK(attr) ((attr)->u.blk)
706#define DW_SND(attr) ((attr)->u.snd)
707#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 708#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
709
710/* Blocks are a bunch of untyped bytes. */
711struct dwarf_block
712 {
713 unsigned int size;
fe1b8b76 714 gdb_byte *data;
c906108c
SS
715 };
716
c906108c
SS
717#ifndef ATTR_ALLOC_CHUNK
718#define ATTR_ALLOC_CHUNK 4
719#endif
720
c906108c
SS
721/* Allocate fields for structs, unions and enums in this size. */
722#ifndef DW_FIELD_ALLOC_CHUNK
723#define DW_FIELD_ALLOC_CHUNK 4
724#endif
725
c906108c
SS
726/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
727 but this would require a corresponding change in unpack_field_as_long
728 and friends. */
729static int bits_per_byte = 8;
730
731/* The routines that read and process dies for a C struct or C++ class
732 pass lists of data member fields and lists of member function fields
733 in an instance of a field_info structure, as defined below. */
734struct field_info
c5aa993b
JM
735 {
736 /* List of data member and baseclasses fields. */
737 struct nextfield
738 {
739 struct nextfield *next;
740 int accessibility;
741 int virtuality;
742 struct field field;
743 }
7d0ccb61 744 *fields, *baseclasses;
c906108c 745
7d0ccb61 746 /* Number of fields (including baseclasses). */
c5aa993b 747 int nfields;
c906108c 748
c5aa993b
JM
749 /* Number of baseclasses. */
750 int nbaseclasses;
c906108c 751
c5aa993b
JM
752 /* Set if the accesibility of one of the fields is not public. */
753 int non_public_fields;
c906108c 754
c5aa993b
JM
755 /* Member function fields array, entries are allocated in the order they
756 are encountered in the object file. */
757 struct nextfnfield
758 {
759 struct nextfnfield *next;
760 struct fn_field fnfield;
761 }
762 *fnfields;
c906108c 763
c5aa993b
JM
764 /* Member function fieldlist array, contains name of possibly overloaded
765 member function, number of overloaded member functions and a pointer
766 to the head of the member function field chain. */
767 struct fnfieldlist
768 {
769 char *name;
770 int length;
771 struct nextfnfield *head;
772 }
773 *fnfieldlists;
c906108c 774
c5aa993b
JM
775 /* Number of entries in the fnfieldlists array. */
776 int nfnfields;
98751a41
JK
777
778 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
779 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
780 struct typedef_field_list
781 {
782 struct typedef_field field;
783 struct typedef_field_list *next;
784 }
785 *typedef_field_list;
786 unsigned typedef_field_list_count;
c5aa993b 787 };
c906108c 788
10b3939b
DJ
789/* One item on the queue of compilation units to read in full symbols
790 for. */
791struct dwarf2_queue_item
792{
793 struct dwarf2_per_cu_data *per_cu;
794 struct dwarf2_queue_item *next;
795};
796
797/* The current queue. */
798static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
799
ae038cb0
DJ
800/* Loaded secondary compilation units are kept in memory until they
801 have not been referenced for the processing of this many
802 compilation units. Set this to zero to disable caching. Cache
803 sizes of up to at least twenty will improve startup time for
804 typical inter-CU-reference binaries, at an obvious memory cost. */
805static int dwarf2_max_cache_age = 5;
920d2a44
AC
806static void
807show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
808 struct cmd_list_element *c, const char *value)
809{
810 fprintf_filtered (file, _("\
811The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
812 value);
813}
814
ae038cb0 815
c906108c
SS
816/* Various complaints about symbol reading that don't abort the process */
817
4d3c2250
KB
818static void
819dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 820{
4d3c2250 821 complaint (&symfile_complaints,
e2e0b3e5 822 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
823}
824
25e43795
DJ
825static void
826dwarf2_debug_line_missing_file_complaint (void)
827{
828 complaint (&symfile_complaints,
829 _(".debug_line section has line data without a file"));
830}
831
59205f5a
JB
832static void
833dwarf2_debug_line_missing_end_sequence_complaint (void)
834{
835 complaint (&symfile_complaints,
836 _(".debug_line section has line program sequence without an end"));
837}
838
4d3c2250
KB
839static void
840dwarf2_complex_location_expr_complaint (void)
2e276125 841{
e2e0b3e5 842 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
843}
844
4d3c2250
KB
845static void
846dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
847 int arg3)
2e276125 848{
4d3c2250 849 complaint (&symfile_complaints,
e2e0b3e5 850 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
851 arg2, arg3);
852}
853
854static void
855dwarf2_macros_too_long_complaint (void)
2e276125 856{
4d3c2250 857 complaint (&symfile_complaints,
e2e0b3e5 858 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
859}
860
861static void
862dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 863{
4d3c2250 864 complaint (&symfile_complaints,
e2e0b3e5 865 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
866 arg1);
867}
868
869static void
870dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 871{
4d3c2250 872 complaint (&symfile_complaints,
e2e0b3e5 873 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 874}
c906108c 875
c906108c
SS
876/* local function prototypes */
877
4efb68b1 878static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 879
aaa75496
JB
880static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
881 struct objfile *);
882
c67a9c90 883static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 884
72bf9492
DJ
885static void scan_partial_symbols (struct partial_die_info *,
886 CORE_ADDR *, CORE_ADDR *,
5734ee8b 887 int, struct dwarf2_cu *);
c906108c 888
72bf9492
DJ
889static void add_partial_symbol (struct partial_die_info *,
890 struct dwarf2_cu *);
63d06c5c 891
72bf9492
DJ
892static void add_partial_namespace (struct partial_die_info *pdi,
893 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 894 int need_pc, struct dwarf2_cu *cu);
63d06c5c 895
5d7cb8df
JK
896static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
897 CORE_ADDR *highpc, int need_pc,
898 struct dwarf2_cu *cu);
899
72bf9492
DJ
900static void add_partial_enumeration (struct partial_die_info *enum_pdi,
901 struct dwarf2_cu *cu);
91c24f0a 902
bc30ff58
JB
903static void add_partial_subprogram (struct partial_die_info *pdi,
904 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 905 int need_pc, struct dwarf2_cu *cu);
bc30ff58 906
fe1b8b76 907static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
908 gdb_byte *buffer, gdb_byte *info_ptr,
909 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 910
a14ed312 911static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 912
a14ed312 913static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 914
e7c27a73 915static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 916
f3dd6933 917static void dwarf2_free_abbrev_table (void *);
c906108c 918
fe1b8b76 919static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 920 struct dwarf2_cu *);
72bf9492 921
57349743 922static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 923 struct dwarf2_cu *);
c906108c 924
93311388
DE
925static struct partial_die_info *load_partial_dies (bfd *,
926 gdb_byte *, gdb_byte *,
927 int, struct dwarf2_cu *);
72bf9492 928
fe1b8b76 929static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
930 struct abbrev_info *abbrev,
931 unsigned int, bfd *,
932 gdb_byte *, gdb_byte *,
933 struct dwarf2_cu *);
c906108c 934
c764a876 935static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 936 struct dwarf2_cu *);
72bf9492
DJ
937
938static void fixup_partial_die (struct partial_die_info *,
939 struct dwarf2_cu *);
940
fe1b8b76
JB
941static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
942 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 943
fe1b8b76
JB
944static gdb_byte *read_attribute_value (struct attribute *, unsigned,
945 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 946
fe1b8b76 947static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 948
fe1b8b76 949static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 950
fe1b8b76 951static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 952
fe1b8b76 953static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 954
93311388 955static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 956
fe1b8b76 957static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 958 unsigned int *);
c906108c 959
c764a876
DE
960static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
961
962static LONGEST read_checked_initial_length_and_offset
963 (bfd *, gdb_byte *, const struct comp_unit_head *,
964 unsigned int *, unsigned int *);
613e1657 965
fe1b8b76 966static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
967 unsigned int *);
968
969static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 970
fe1b8b76 971static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 972
9b1c24c8 973static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 974
fe1b8b76
JB
975static char *read_indirect_string (bfd *, gdb_byte *,
976 const struct comp_unit_head *,
977 unsigned int *);
4bdf3d34 978
fe1b8b76 979static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 980
fe1b8b76 981static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 982
fe1b8b76 983static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 984
e142c38c 985static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 986
e142c38c
DJ
987static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
988 struct dwarf2_cu *);
c906108c 989
348e048f
DE
990static struct attribute *dwarf2_attr_no_follow (struct die_info *,
991 unsigned int,
992 struct dwarf2_cu *);
993
05cf31d1
JB
994static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
995 struct dwarf2_cu *cu);
996
e142c38c 997static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 998
e142c38c 999static struct die_info *die_specification (struct die_info *die,
f2f0e013 1000 struct dwarf2_cu **);
63d06c5c 1001
debd256d
JB
1002static void free_line_header (struct line_header *lh);
1003
aaa75496
JB
1004static void add_file_name (struct line_header *, char *, unsigned int,
1005 unsigned int, unsigned int);
1006
debd256d
JB
1007static struct line_header *(dwarf_decode_line_header
1008 (unsigned int offset,
e7c27a73 1009 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1010
72b9f47f 1011static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1012 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1013
72b9f47f 1014static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1015
a14ed312 1016static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1017 struct dwarf2_cu *);
c906108c 1018
34eaf542
TT
1019static struct symbol *new_symbol_full (struct die_info *, struct type *,
1020 struct dwarf2_cu *, struct symbol *);
1021
a14ed312 1022static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1023 struct dwarf2_cu *);
c906108c 1024
98bfdba5
PA
1025static void dwarf2_const_value_attr (struct attribute *attr,
1026 struct type *type,
1027 const char *name,
1028 struct obstack *obstack,
1029 struct dwarf2_cu *cu, long *value,
1030 gdb_byte **bytes,
1031 struct dwarf2_locexpr_baton **baton);
2df3850c 1032
e7c27a73 1033static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1034
b4ba55a1
JB
1035static int need_gnat_info (struct dwarf2_cu *);
1036
1037static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1038
1039static void set_descriptive_type (struct type *, struct die_info *,
1040 struct dwarf2_cu *);
1041
e7c27a73
DJ
1042static struct type *die_containing_type (struct die_info *,
1043 struct dwarf2_cu *);
c906108c 1044
673bfd45
DE
1045static struct type *lookup_die_type (struct die_info *, struct attribute *,
1046 struct dwarf2_cu *);
c906108c 1047
f792889a 1048static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1049
673bfd45
DE
1050static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1051
086ed43d 1052static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1053
6e70227d 1054static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1055 const char *suffix, int physname,
1056 struct dwarf2_cu *cu);
63d06c5c 1057
e7c27a73 1058static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1059
348e048f
DE
1060static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1061
e7c27a73 1062static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1063
e7c27a73 1064static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1065
ff013f42
JK
1066static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1067 struct dwarf2_cu *, struct partial_symtab *);
1068
a14ed312 1069static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1070 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1071 struct partial_symtab *);
c906108c 1072
fae299cd
DC
1073static void get_scope_pc_bounds (struct die_info *,
1074 CORE_ADDR *, CORE_ADDR *,
1075 struct dwarf2_cu *);
1076
801e3a5b
JB
1077static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1078 CORE_ADDR, struct dwarf2_cu *);
1079
a14ed312 1080static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1081 struct dwarf2_cu *);
c906108c 1082
a14ed312 1083static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1084 struct type *, struct dwarf2_cu *);
c906108c 1085
a14ed312 1086static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1087 struct die_info *, struct type *,
e7c27a73 1088 struct dwarf2_cu *);
c906108c 1089
a14ed312 1090static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1091 struct type *, struct dwarf2_cu *);
c906108c 1092
134d01f1 1093static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1094
e7c27a73 1095static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1096
e7c27a73 1097static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1098
5d7cb8df
JK
1099static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1100
27aa8d6a
SW
1101static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1102
f55ee35c
JK
1103static struct type *read_module_type (struct die_info *die,
1104 struct dwarf2_cu *cu);
1105
38d518c9 1106static const char *namespace_name (struct die_info *die,
e142c38c 1107 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1108
134d01f1 1109static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1110
e7c27a73 1111static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1112
6e70227d 1113static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1114 struct dwarf2_cu *);
1115
93311388 1116static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1117
93311388
DE
1118static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1119 gdb_byte *info_ptr,
d97bc12b
DE
1120 gdb_byte **new_info_ptr,
1121 struct die_info *parent);
1122
93311388
DE
1123static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1124 gdb_byte *info_ptr,
fe1b8b76 1125 gdb_byte **new_info_ptr,
639d11d3
DC
1126 struct die_info *parent);
1127
93311388
DE
1128static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1129 gdb_byte *info_ptr,
fe1b8b76 1130 gdb_byte **new_info_ptr,
639d11d3
DC
1131 struct die_info *parent);
1132
93311388
DE
1133static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1134 struct die_info **, gdb_byte *,
1135 int *);
1136
e7c27a73 1137static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1138
71c25dea
TT
1139static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1140 struct obstack *);
1141
e142c38c 1142static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1143
98bfdba5
PA
1144static const char *dwarf2_full_name (char *name,
1145 struct die_info *die,
1146 struct dwarf2_cu *cu);
1147
e142c38c 1148static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1149 struct dwarf2_cu **);
9219021c 1150
a14ed312 1151static char *dwarf_tag_name (unsigned int);
c906108c 1152
a14ed312 1153static char *dwarf_attr_name (unsigned int);
c906108c 1154
a14ed312 1155static char *dwarf_form_name (unsigned int);
c906108c 1156
a14ed312 1157static char *dwarf_bool_name (unsigned int);
c906108c 1158
a14ed312 1159static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1160
1161#if 0
a14ed312 1162static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1163#endif
1164
f9aca02d 1165static struct die_info *sibling_die (struct die_info *);
c906108c 1166
d97bc12b
DE
1167static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1168
1169static void dump_die_for_error (struct die_info *);
1170
1171static void dump_die_1 (struct ui_file *, int level, int max_level,
1172 struct die_info *);
c906108c 1173
d97bc12b 1174/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1175
51545339 1176static void store_in_ref_table (struct die_info *,
10b3939b 1177 struct dwarf2_cu *);
c906108c 1178
93311388
DE
1179static int is_ref_attr (struct attribute *);
1180
c764a876 1181static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1182
43bbcdc2 1183static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1184
348e048f
DE
1185static struct die_info *follow_die_ref_or_sig (struct die_info *,
1186 struct attribute *,
1187 struct dwarf2_cu **);
1188
10b3939b
DJ
1189static struct die_info *follow_die_ref (struct die_info *,
1190 struct attribute *,
f2f0e013 1191 struct dwarf2_cu **);
c906108c 1192
348e048f
DE
1193static struct die_info *follow_die_sig (struct die_info *,
1194 struct attribute *,
1195 struct dwarf2_cu **);
1196
1197static void read_signatured_type_at_offset (struct objfile *objfile,
1198 unsigned int offset);
1199
1200static void read_signatured_type (struct objfile *,
1201 struct signatured_type *type_sig);
1202
c906108c
SS
1203/* memory allocation interface */
1204
7b5a2f43 1205static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1206
f3dd6933 1207static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1208
b60c80d6 1209static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1210
e142c38c 1211static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1212
e142c38c
DJ
1213static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1214 struct dwarf2_cu *);
5fb290d7 1215
2e276125 1216static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1217 char *, bfd *, struct dwarf2_cu *);
2e276125 1218
8e19ed76
PS
1219static int attr_form_is_block (struct attribute *);
1220
3690dd37
JB
1221static int attr_form_is_section_offset (struct attribute *);
1222
1223static int attr_form_is_constant (struct attribute *);
1224
8cf6f0b1
TT
1225static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1226 struct dwarf2_loclist_baton *baton,
1227 struct attribute *attr);
1228
93e7bd98
DJ
1229static void dwarf2_symbol_mark_computed (struct attribute *attr,
1230 struct symbol *sym,
1231 struct dwarf2_cu *cu);
4c2df51b 1232
93311388
DE
1233static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1234 struct abbrev_info *abbrev,
1235 struct dwarf2_cu *cu);
4bb7a0a7 1236
72bf9492
DJ
1237static void free_stack_comp_unit (void *);
1238
72bf9492
DJ
1239static hashval_t partial_die_hash (const void *item);
1240
1241static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1242
ae038cb0 1243static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1244 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1245
1246static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1247 (unsigned int offset, struct objfile *objfile);
ae038cb0 1248
9816fde3
JK
1249static void init_one_comp_unit (struct dwarf2_cu *cu,
1250 struct objfile *objfile);
1251
1252static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1253 struct die_info *comp_unit_die);
93311388 1254
ae038cb0
DJ
1255static void free_one_comp_unit (void *);
1256
1257static void free_cached_comp_units (void *);
1258
1259static void age_cached_comp_units (void);
1260
1261static void free_one_cached_comp_unit (void *);
1262
f792889a
DJ
1263static struct type *set_die_type (struct die_info *, struct type *,
1264 struct dwarf2_cu *);
1c379e20 1265
ae038cb0
DJ
1266static void create_all_comp_units (struct objfile *);
1267
1fd400ff
TT
1268static int create_debug_types_hash_table (struct objfile *objfile);
1269
93311388
DE
1270static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1271 struct objfile *);
10b3939b
DJ
1272
1273static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1274
1275static void dwarf2_add_dependence (struct dwarf2_cu *,
1276 struct dwarf2_per_cu_data *);
1277
ae038cb0
DJ
1278static void dwarf2_mark (struct dwarf2_cu *);
1279
1280static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1281
673bfd45
DE
1282static struct type *get_die_type_at_offset (unsigned int,
1283 struct dwarf2_per_cu_data *per_cu);
1284
f792889a 1285static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1286
9291a0cd
TT
1287static void dwarf2_release_queue (void *dummy);
1288
1289static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1290 struct objfile *objfile);
1291
1292static void process_queue (struct objfile *objfile);
1293
1294static void find_file_and_directory (struct die_info *die,
1295 struct dwarf2_cu *cu,
1296 char **name, char **comp_dir);
1297
1298static char *file_full_name (int file, struct line_header *lh,
1299 const char *comp_dir);
1300
1301static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1302 gdb_byte *info_ptr,
1303 gdb_byte *buffer,
1304 unsigned int buffer_size,
1305 bfd *abfd);
1306
1307static void init_cu_die_reader (struct die_reader_specs *reader,
1308 struct dwarf2_cu *cu);
1309
673bfd45 1310static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1311
9291a0cd
TT
1312#if WORDS_BIGENDIAN
1313
1314/* Convert VALUE between big- and little-endian. */
1315static offset_type
1316byte_swap (offset_type value)
1317{
1318 offset_type result;
1319
1320 result = (value & 0xff) << 24;
1321 result |= (value & 0xff00) << 8;
1322 result |= (value & 0xff0000) >> 8;
1323 result |= (value & 0xff000000) >> 24;
1324 return result;
1325}
1326
1327#define MAYBE_SWAP(V) byte_swap (V)
1328
1329#else
1330#define MAYBE_SWAP(V) (V)
1331#endif /* WORDS_BIGENDIAN */
1332
1333/* The suffix for an index file. */
1334#define INDEX_SUFFIX ".gdb-index"
1335
3da10d80
KS
1336static const char *dwarf2_physname (char *name, struct die_info *die,
1337 struct dwarf2_cu *cu);
1338
c906108c
SS
1339/* Try to locate the sections we need for DWARF 2 debugging
1340 information and return true if we have enough to do something. */
1341
1342int
6502dd73 1343dwarf2_has_info (struct objfile *objfile)
c906108c 1344{
be391dca
TT
1345 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1346 if (!dwarf2_per_objfile)
1347 {
1348 /* Initialize per-objfile state. */
1349 struct dwarf2_per_objfile *data
1350 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1351
be391dca
TT
1352 memset (data, 0, sizeof (*data));
1353 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1354 dwarf2_per_objfile = data;
6502dd73 1355
be391dca
TT
1356 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1357 dwarf2_per_objfile->objfile = objfile;
1358 }
1359 return (dwarf2_per_objfile->info.asection != NULL
1360 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1361}
1362
233a11ab
CS
1363/* When loading sections, we can either look for ".<name>", or for
1364 * ".z<name>", which indicates a compressed section. */
1365
1366static int
dce234bc 1367section_is_p (const char *section_name, const char *name)
233a11ab 1368{
dce234bc
PP
1369 return (section_name[0] == '.'
1370 && (strcmp (section_name + 1, name) == 0
1371 || (section_name[1] == 'z'
1372 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1373}
1374
c906108c
SS
1375/* This function is mapped across the sections and remembers the
1376 offset and size of each of the debugging sections we are interested
1377 in. */
1378
1379static void
72dca2f5 1380dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1381{
dce234bc 1382 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1383 {
dce234bc
PP
1384 dwarf2_per_objfile->info.asection = sectp;
1385 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1386 }
dce234bc 1387 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1388 {
dce234bc
PP
1389 dwarf2_per_objfile->abbrev.asection = sectp;
1390 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1391 }
dce234bc 1392 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1393 {
dce234bc
PP
1394 dwarf2_per_objfile->line.asection = sectp;
1395 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1396 }
dce234bc 1397 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->loc.asection = sectp;
1400 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1401 }
dce234bc 1402 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1403 {
dce234bc
PP
1404 dwarf2_per_objfile->macinfo.asection = sectp;
1405 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1406 }
dce234bc 1407 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1408 {
dce234bc
PP
1409 dwarf2_per_objfile->str.asection = sectp;
1410 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1411 }
dce234bc 1412 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1413 {
dce234bc
PP
1414 dwarf2_per_objfile->frame.asection = sectp;
1415 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1416 }
dce234bc 1417 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1418 {
3799ccc6 1419 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1420
3799ccc6
EZ
1421 if (aflag & SEC_HAS_CONTENTS)
1422 {
dce234bc
PP
1423 dwarf2_per_objfile->eh_frame.asection = sectp;
1424 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1425 }
b6af0555 1426 }
dce234bc 1427 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1428 {
dce234bc
PP
1429 dwarf2_per_objfile->ranges.asection = sectp;
1430 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1431 }
348e048f
DE
1432 else if (section_is_p (sectp->name, TYPES_SECTION))
1433 {
1434 dwarf2_per_objfile->types.asection = sectp;
1435 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1436 }
9291a0cd
TT
1437 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1438 {
1439 dwarf2_per_objfile->gdb_index.asection = sectp;
1440 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1441 }
dce234bc 1442
72dca2f5
FR
1443 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1444 && bfd_section_vma (abfd, sectp) == 0)
1445 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1446}
1447
dce234bc
PP
1448/* Decompress a section that was compressed using zlib. Store the
1449 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1450
1451static void
dce234bc
PP
1452zlib_decompress_section (struct objfile *objfile, asection *sectp,
1453 gdb_byte **outbuf, bfd_size_type *outsize)
1454{
1455 bfd *abfd = objfile->obfd;
1456#ifndef HAVE_ZLIB_H
1457 error (_("Support for zlib-compressed DWARF data (from '%s') "
1458 "is disabled in this copy of GDB"),
1459 bfd_get_filename (abfd));
1460#else
1461 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1462 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1463 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1464 bfd_size_type uncompressed_size;
1465 gdb_byte *uncompressed_buffer;
1466 z_stream strm;
1467 int rc;
1468 int header_size = 12;
1469
1470 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1471 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1472 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1473 bfd_get_filename (abfd));
1474
1475 /* Read the zlib header. In this case, it should be "ZLIB" followed
1476 by the uncompressed section size, 8 bytes in big-endian order. */
1477 if (compressed_size < header_size
1478 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1479 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1480 bfd_get_filename (abfd));
1481 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[11];
1489
1490 /* It is possible the section consists of several compressed
1491 buffers concatenated together, so we uncompress in a loop. */
1492 strm.zalloc = NULL;
1493 strm.zfree = NULL;
1494 strm.opaque = NULL;
1495 strm.avail_in = compressed_size - header_size;
1496 strm.next_in = (Bytef*) compressed_buffer + header_size;
1497 strm.avail_out = uncompressed_size;
1498 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1499 uncompressed_size);
1500 rc = inflateInit (&strm);
1501 while (strm.avail_in > 0)
1502 {
1503 if (rc != Z_OK)
1504 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1505 bfd_get_filename (abfd), rc);
1506 strm.next_out = ((Bytef*) uncompressed_buffer
1507 + (uncompressed_size - strm.avail_out));
1508 rc = inflate (&strm, Z_FINISH);
1509 if (rc != Z_STREAM_END)
1510 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1511 bfd_get_filename (abfd), rc);
1512 rc = inflateReset (&strm);
1513 }
1514 rc = inflateEnd (&strm);
1515 if (rc != Z_OK
1516 || strm.avail_out != 0)
1517 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1518 bfd_get_filename (abfd), rc);
1519
affddf13 1520 do_cleanups (cleanup);
dce234bc
PP
1521 *outbuf = uncompressed_buffer;
1522 *outsize = uncompressed_size;
1523#endif
233a11ab
CS
1524}
1525
dce234bc
PP
1526/* Read the contents of the section SECTP from object file specified by
1527 OBJFILE, store info about the section into INFO.
1528 If the section is compressed, uncompress it before returning. */
c906108c 1529
dce234bc
PP
1530static void
1531dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1532{
dce234bc
PP
1533 bfd *abfd = objfile->obfd;
1534 asection *sectp = info->asection;
1535 gdb_byte *buf, *retbuf;
1536 unsigned char header[4];
c906108c 1537
be391dca
TT
1538 if (info->readin)
1539 return;
dce234bc
PP
1540 info->buffer = NULL;
1541 info->was_mmapped = 0;
be391dca 1542 info->readin = 1;
188dd5d6 1543
dce234bc
PP
1544 if (info->asection == NULL || info->size == 0)
1545 return;
c906108c 1546
dce234bc
PP
1547 /* Check if the file has a 4-byte header indicating compression. */
1548 if (info->size > sizeof (header)
1549 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1550 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1551 {
1552 /* Upon decompression, update the buffer and its size. */
1553 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1554 {
1555 zlib_decompress_section (objfile, sectp, &info->buffer,
1556 &info->size);
1557 return;
1558 }
1559 }
4bdf3d34 1560
dce234bc
PP
1561#ifdef HAVE_MMAP
1562 if (pagesize == 0)
1563 pagesize = getpagesize ();
2e276125 1564
dce234bc
PP
1565 /* Only try to mmap sections which are large enough: we don't want to
1566 waste space due to fragmentation. Also, only try mmap for sections
1567 without relocations. */
1568
1569 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1570 {
1571 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1572 size_t map_length = info->size + sectp->filepos - pg_offset;
1573 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1574 MAP_PRIVATE, pg_offset);
1575
1576 if (retbuf != MAP_FAILED)
1577 {
1578 info->was_mmapped = 1;
1579 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1580#if HAVE_POSIX_MADVISE
1581 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1582#endif
dce234bc
PP
1583 return;
1584 }
1585 }
1586#endif
1587
1588 /* If we get here, we are a normal, not-compressed section. */
1589 info->buffer = buf
1590 = obstack_alloc (&objfile->objfile_obstack, info->size);
1591
1592 /* When debugging .o files, we may need to apply relocations; see
1593 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1594 We never compress sections in .o files, so we only need to
1595 try this when the section is not compressed. */
ac8035ab 1596 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1597 if (retbuf != NULL)
1598 {
1599 info->buffer = retbuf;
1600 return;
1601 }
1602
1603 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1604 || bfd_bread (buf, info->size, abfd) != info->size)
1605 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1606 bfd_get_filename (abfd));
1607}
1608
1609/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1610 SECTION_NAME. */
af34e669 1611
dce234bc
PP
1612void
1613dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1614 asection **sectp, gdb_byte **bufp,
1615 bfd_size_type *sizep)
1616{
1617 struct dwarf2_per_objfile *data
1618 = objfile_data (objfile, dwarf2_objfile_data_key);
1619 struct dwarf2_section_info *info;
a3b2a86b
TT
1620
1621 /* We may see an objfile without any DWARF, in which case we just
1622 return nothing. */
1623 if (data == NULL)
1624 {
1625 *sectp = NULL;
1626 *bufp = NULL;
1627 *sizep = 0;
1628 return;
1629 }
dce234bc
PP
1630 if (section_is_p (section_name, EH_FRAME_SECTION))
1631 info = &data->eh_frame;
1632 else if (section_is_p (section_name, FRAME_SECTION))
1633 info = &data->frame;
0d53c4c4 1634 else
f3574227 1635 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1636
1637 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1638 /* We haven't read this section in yet. Do it now. */
1639 dwarf2_read_section (objfile, info);
1640
1641 *sectp = info->asection;
1642 *bufp = info->buffer;
1643 *sizep = info->size;
1644}
1645
9291a0cd
TT
1646\f
1647
1648/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1649 this CU came. */
2fdf6df6 1650
9291a0cd
TT
1651static void
1652dw2_do_instantiate_symtab (struct objfile *objfile,
1653 struct dwarf2_per_cu_data *per_cu)
1654{
1655 struct cleanup *back_to;
1656
1657 back_to = make_cleanup (dwarf2_release_queue, NULL);
1658
1659 queue_comp_unit (per_cu, objfile);
1660
1661 if (per_cu->from_debug_types)
1662 read_signatured_type_at_offset (objfile, per_cu->offset);
1663 else
1664 load_full_comp_unit (per_cu, objfile);
1665
1666 process_queue (objfile);
1667
1668 /* Age the cache, releasing compilation units that have not
1669 been used recently. */
1670 age_cached_comp_units ();
1671
1672 do_cleanups (back_to);
1673}
1674
1675/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1676 the objfile from which this CU came. Returns the resulting symbol
1677 table. */
2fdf6df6 1678
9291a0cd
TT
1679static struct symtab *
1680dw2_instantiate_symtab (struct objfile *objfile,
1681 struct dwarf2_per_cu_data *per_cu)
1682{
1683 if (!per_cu->v.quick->symtab)
1684 {
1685 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1686 increment_reading_symtab ();
1687 dw2_do_instantiate_symtab (objfile, per_cu);
1688 do_cleanups (back_to);
1689 }
1690 return per_cu->v.quick->symtab;
1691}
1692
1fd400ff 1693/* Return the CU given its index. */
2fdf6df6 1694
1fd400ff
TT
1695static struct dwarf2_per_cu_data *
1696dw2_get_cu (int index)
1697{
1698 if (index >= dwarf2_per_objfile->n_comp_units)
1699 {
1700 index -= dwarf2_per_objfile->n_comp_units;
1701 return dwarf2_per_objfile->type_comp_units[index];
1702 }
1703 return dwarf2_per_objfile->all_comp_units[index];
1704}
1705
9291a0cd
TT
1706/* A helper function that knows how to read a 64-bit value in a way
1707 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1708 otherwise. */
2fdf6df6 1709
9291a0cd
TT
1710static int
1711extract_cu_value (const char *bytes, ULONGEST *result)
1712{
1713 if (sizeof (ULONGEST) < 8)
1714 {
1715 int i;
1716
1717 /* Ignore the upper 4 bytes if they are all zero. */
1718 for (i = 0; i < 4; ++i)
1719 if (bytes[i + 4] != 0)
1720 return 0;
1721
1722 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1723 }
1724 else
1725 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1726 return 1;
1727}
1728
1729/* Read the CU list from the mapped index, and use it to create all
1730 the CU objects for this objfile. Return 0 if something went wrong,
1731 1 if everything went ok. */
2fdf6df6 1732
9291a0cd 1733static int
1fd400ff
TT
1734create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1735 offset_type cu_list_elements)
9291a0cd
TT
1736{
1737 offset_type i;
9291a0cd
TT
1738
1739 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1740 dwarf2_per_objfile->all_comp_units
1741 = obstack_alloc (&objfile->objfile_obstack,
1742 dwarf2_per_objfile->n_comp_units
1743 * sizeof (struct dwarf2_per_cu_data *));
1744
1745 for (i = 0; i < cu_list_elements; i += 2)
1746 {
1747 struct dwarf2_per_cu_data *the_cu;
1748 ULONGEST offset, length;
1749
1750 if (!extract_cu_value (cu_list, &offset)
1751 || !extract_cu_value (cu_list + 8, &length))
1752 return 0;
1753 cu_list += 2 * 8;
1754
1755 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1756 struct dwarf2_per_cu_data);
1757 the_cu->offset = offset;
1758 the_cu->length = length;
1759 the_cu->objfile = objfile;
1760 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1761 struct dwarf2_per_cu_quick_data);
1762 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1763 }
1764
1765 return 1;
1766}
1767
1fd400ff 1768/* Create the signatured type hash table from the index. */
673bfd45 1769
1fd400ff 1770static int
673bfd45
DE
1771create_signatured_type_table_from_index (struct objfile *objfile,
1772 const gdb_byte *bytes,
1773 offset_type elements)
1fd400ff
TT
1774{
1775 offset_type i;
673bfd45 1776 htab_t sig_types_hash;
1fd400ff
TT
1777
1778 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1779 dwarf2_per_objfile->type_comp_units
1780 = obstack_alloc (&objfile->objfile_obstack,
1781 dwarf2_per_objfile->n_type_comp_units
1782 * sizeof (struct dwarf2_per_cu_data *));
1783
673bfd45 1784 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1785
1786 for (i = 0; i < elements; i += 3)
1787 {
1788 struct signatured_type *type_sig;
1789 ULONGEST offset, type_offset, signature;
1790 void **slot;
1791
1792 if (!extract_cu_value (bytes, &offset)
1793 || !extract_cu_value (bytes + 8, &type_offset))
1794 return 0;
1795 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1796 bytes += 3 * 8;
1797
1798 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1799 struct signatured_type);
1800 type_sig->signature = signature;
1801 type_sig->offset = offset;
1802 type_sig->type_offset = type_offset;
1803 type_sig->per_cu.from_debug_types = 1;
1804 type_sig->per_cu.offset = offset;
1805 type_sig->per_cu.objfile = objfile;
1806 type_sig->per_cu.v.quick
1807 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1808 struct dwarf2_per_cu_quick_data);
1809
673bfd45 1810 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1811 *slot = type_sig;
1812
1813 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1814 }
1815
673bfd45 1816 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1817
1818 return 1;
1819}
1820
9291a0cd
TT
1821/* Read the address map data from the mapped index, and use it to
1822 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1823
9291a0cd
TT
1824static void
1825create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1826{
1827 const gdb_byte *iter, *end;
1828 struct obstack temp_obstack;
1829 struct addrmap *mutable_map;
1830 struct cleanup *cleanup;
1831 CORE_ADDR baseaddr;
1832
1833 obstack_init (&temp_obstack);
1834 cleanup = make_cleanup_obstack_free (&temp_obstack);
1835 mutable_map = addrmap_create_mutable (&temp_obstack);
1836
1837 iter = index->address_table;
1838 end = iter + index->address_table_size;
1839
1840 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1841
1842 while (iter < end)
1843 {
1844 ULONGEST hi, lo, cu_index;
1845 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1846 iter += 8;
1847 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1848 iter += 8;
1849 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1850 iter += 4;
1851
1852 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1853 dw2_get_cu (cu_index));
9291a0cd
TT
1854 }
1855
1856 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1857 &objfile->objfile_obstack);
1858 do_cleanups (cleanup);
1859}
1860
1861/* The hash function for strings in the mapped index. This is the
1862 same as the hashtab.c hash function, but we keep a separate copy to
1863 maintain control over the implementation. This is necessary
1864 because the hash function is tied to the format of the mapped index
1865 file. */
2fdf6df6 1866
9291a0cd
TT
1867static hashval_t
1868mapped_index_string_hash (const void *p)
1869{
1870 const unsigned char *str = (const unsigned char *) p;
1871 hashval_t r = 0;
1872 unsigned char c;
1873
1874 while ((c = *str++) != 0)
1875 r = r * 67 + c - 113;
1876
1877 return r;
1878}
1879
1880/* Find a slot in the mapped index INDEX for the object named NAME.
1881 If NAME is found, set *VEC_OUT to point to the CU vector in the
1882 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1883
9291a0cd
TT
1884static int
1885find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1886 offset_type **vec_out)
1887{
1888 offset_type hash = mapped_index_string_hash (name);
1889 offset_type slot, step;
1890
3876f04e
DE
1891 slot = hash & (index->symbol_table_slots - 1);
1892 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1893
1894 for (;;)
1895 {
1896 /* Convert a slot number to an offset into the table. */
1897 offset_type i = 2 * slot;
1898 const char *str;
3876f04e 1899 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1900 return 0;
1901
3876f04e 1902 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1903 if (!strcmp (name, str))
1904 {
1905 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1906 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1907 return 1;
1908 }
1909
3876f04e 1910 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1911 }
1912}
1913
1914/* Read the index file. If everything went ok, initialize the "quick"
1915 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1916
9291a0cd
TT
1917static int
1918dwarf2_read_index (struct objfile *objfile)
1919{
9291a0cd
TT
1920 char *addr;
1921 struct mapped_index *map;
b3b272e1 1922 offset_type *metadata;
ac0b195c
KW
1923 const gdb_byte *cu_list;
1924 const gdb_byte *types_list = NULL;
1925 offset_type version, cu_list_elements;
1926 offset_type types_list_elements = 0;
1fd400ff 1927 int i;
9291a0cd
TT
1928
1929 if (dwarf2_per_objfile->gdb_index.asection == NULL
1930 || dwarf2_per_objfile->gdb_index.size == 0)
1931 return 0;
82430852
JK
1932
1933 /* Older elfutils strip versions could keep the section in the main
1934 executable while splitting it for the separate debug info file. */
1935 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1936 & SEC_HAS_CONTENTS) == 0)
1937 return 0;
1938
9291a0cd
TT
1939 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1940
1941 addr = dwarf2_per_objfile->gdb_index.buffer;
1942 /* Version check. */
1fd400ff 1943 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
1944 /* Versions earlier than 3 emitted every copy of a psymbol. This
1945 causes the index to behave very poorly for certain requests. So,
1946 it seems better to just ignore such indices. */
1947 if (version < 3)
9291a0cd 1948 return 0;
594e8718
JK
1949 /* Indexes with higher version than the one supported by GDB may be no
1950 longer backward compatible. */
1951 if (version > 3)
1952 return 0;
9291a0cd
TT
1953
1954 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1955 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1956
1957 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1958
1959 i = 0;
1960 cu_list = addr + MAYBE_SWAP (metadata[i]);
1961 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1962 / 8);
1fd400ff
TT
1963 ++i;
1964
987d643c
TT
1965 types_list = addr + MAYBE_SWAP (metadata[i]);
1966 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1967 - MAYBE_SWAP (metadata[i]))
1968 / 8);
1969 ++i;
1fd400ff
TT
1970
1971 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1972 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1973 - MAYBE_SWAP (metadata[i]));
1974 ++i;
1975
3876f04e
DE
1976 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1977 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1978 - MAYBE_SWAP (metadata[i]))
1979 / (2 * sizeof (offset_type)));
1fd400ff 1980 ++i;
9291a0cd 1981
1fd400ff
TT
1982 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1983
1984 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1985 return 0;
1986
987d643c 1987 if (types_list_elements
673bfd45
DE
1988 && !create_signatured_type_table_from_index (objfile, types_list,
1989 types_list_elements))
9291a0cd
TT
1990 return 0;
1991
1992 create_addrmap_from_index (objfile, map);
1993
1994 dwarf2_per_objfile->index_table = map;
1995 dwarf2_per_objfile->using_index = 1;
1996
1997 return 1;
1998}
1999
2000/* A helper for the "quick" functions which sets the global
2001 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2002
9291a0cd
TT
2003static void
2004dw2_setup (struct objfile *objfile)
2005{
2006 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2007 gdb_assert (dwarf2_per_objfile);
2008}
2009
2010/* A helper for the "quick" functions which attempts to read the line
2011 table for THIS_CU. */
2fdf6df6 2012
9291a0cd
TT
2013static void
2014dw2_require_line_header (struct objfile *objfile,
2015 struct dwarf2_per_cu_data *this_cu)
2016{
2017 bfd *abfd = objfile->obfd;
2018 struct line_header *lh = NULL;
2019 struct attribute *attr;
2020 struct cleanup *cleanups;
2021 struct die_info *comp_unit_die;
36374493 2022 struct dwarf2_section_info* sec;
9291a0cd
TT
2023 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2024 int has_children, i;
2025 struct dwarf2_cu cu;
2026 unsigned int bytes_read, buffer_size;
2027 struct die_reader_specs reader_specs;
2028 char *name, *comp_dir;
2029
2030 if (this_cu->v.quick->read_lines)
2031 return;
2032 this_cu->v.quick->read_lines = 1;
2033
9816fde3 2034 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2035 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2036
36374493
DE
2037 if (this_cu->from_debug_types)
2038 sec = &dwarf2_per_objfile->types;
2039 else
2040 sec = &dwarf2_per_objfile->info;
2041 dwarf2_read_section (objfile, sec);
2042 buffer_size = sec->size;
2043 buffer = sec->buffer;
9291a0cd
TT
2044 info_ptr = buffer + this_cu->offset;
2045 beg_of_comp_unit = info_ptr;
2046
2047 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2048 buffer, buffer_size,
2049 abfd);
2050
2051 /* Complete the cu_header. */
2052 cu.header.offset = beg_of_comp_unit - buffer;
2053 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2054
2055 this_cu->cu = &cu;
2056 cu.per_cu = this_cu;
2057
2058 dwarf2_read_abbrevs (abfd, &cu);
2059 make_cleanup (dwarf2_free_abbrev_table, &cu);
2060
2061 if (this_cu->from_debug_types)
2062 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2063 init_cu_die_reader (&reader_specs, &cu);
2064 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2065 &has_children);
2066
2067 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2068 if (attr)
2069 {
2070 unsigned int line_offset = DW_UNSND (attr);
2071 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2072 }
2073 if (lh == NULL)
2074 {
2075 do_cleanups (cleanups);
2076 return;
2077 }
2078
2079 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2080
2081 this_cu->v.quick->lines = lh;
2082
2083 this_cu->v.quick->file_names
2084 = obstack_alloc (&objfile->objfile_obstack,
2085 lh->num_file_names * sizeof (char *));
2086 for (i = 0; i < lh->num_file_names; ++i)
2087 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2088
2089 do_cleanups (cleanups);
2090}
2091
2092/* A helper for the "quick" functions which computes and caches the
2093 real path for a given file name from the line table.
2094 dw2_require_line_header must have been called before this is
2095 invoked. */
2fdf6df6 2096
9291a0cd
TT
2097static const char *
2098dw2_require_full_path (struct objfile *objfile,
e254ef6a 2099 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2100 int index)
2101{
e254ef6a
DE
2102 if (!per_cu->v.quick->full_names)
2103 per_cu->v.quick->full_names
9291a0cd 2104 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2105 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2106 sizeof (char *));
2107
e254ef6a
DE
2108 if (!per_cu->v.quick->full_names[index])
2109 per_cu->v.quick->full_names[index]
2110 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2111
e254ef6a 2112 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2113}
2114
2115static struct symtab *
2116dw2_find_last_source_symtab (struct objfile *objfile)
2117{
2118 int index;
ae2de4f8 2119
9291a0cd
TT
2120 dw2_setup (objfile);
2121 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2122 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2123}
2124
2125static void
2126dw2_forget_cached_source_info (struct objfile *objfile)
2127{
2128 int i;
2129
2130 dw2_setup (objfile);
1fd400ff
TT
2131 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2132 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2133 {
e254ef6a 2134 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2135
e254ef6a 2136 if (per_cu->v.quick->full_names)
9291a0cd
TT
2137 {
2138 int j;
2139
e254ef6a 2140 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
87df528e
JK
2141 {
2142 xfree ((void *) per_cu->v.quick->full_names[j]);
2143 per_cu->v.quick->full_names[j] = NULL;
2144 }
9291a0cd
TT
2145 }
2146 }
2147}
2148
2149static int
2150dw2_lookup_symtab (struct objfile *objfile, const char *name,
2151 const char *full_path, const char *real_path,
2152 struct symtab **result)
2153{
2154 int i;
2155 int check_basename = lbasename (name) == name;
2156 struct dwarf2_per_cu_data *base_cu = NULL;
2157
2158 dw2_setup (objfile);
ae2de4f8 2159
1fd400ff
TT
2160 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2161 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2162 {
2163 int j;
e254ef6a 2164 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2165
e254ef6a 2166 if (per_cu->v.quick->symtab)
9291a0cd
TT
2167 continue;
2168
e254ef6a
DE
2169 dw2_require_line_header (objfile, per_cu);
2170 if (!per_cu->v.quick->lines)
9291a0cd
TT
2171 continue;
2172
e254ef6a 2173 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2174 {
e254ef6a 2175 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2176
2177 if (FILENAME_CMP (name, this_name) == 0)
2178 {
e254ef6a 2179 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2180 return 1;
2181 }
2182
2183 if (check_basename && ! base_cu
2184 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2185 base_cu = per_cu;
9291a0cd
TT
2186
2187 if (full_path != NULL)
2188 {
2189 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2190 per_cu, j);
9291a0cd
TT
2191
2192 if (this_full_name
2193 && FILENAME_CMP (full_path, this_full_name) == 0)
2194 {
e254ef6a 2195 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2196 return 1;
2197 }
2198 }
2199
2200 if (real_path != NULL)
2201 {
2202 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2203 per_cu, j);
9291a0cd 2204
74dd2ca6
DE
2205 if (this_full_name != NULL
2206 && FILENAME_CMP (real_path, this_full_name) == 0)
9291a0cd 2207 {
74dd2ca6
DE
2208 *result = dw2_instantiate_symtab (objfile, per_cu);
2209 return 1;
9291a0cd
TT
2210 }
2211 }
2212 }
2213 }
2214
2215 if (base_cu)
2216 {
2217 *result = dw2_instantiate_symtab (objfile, base_cu);
2218 return 1;
2219 }
2220
2221 return 0;
2222}
2223
2224static struct symtab *
2225dw2_lookup_symbol (struct objfile *objfile, int block_index,
2226 const char *name, domain_enum domain)
2227{
774b6a14 2228 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2229 instead. */
2230 return NULL;
2231}
2232
2233/* A helper function that expands all symtabs that hold an object
2234 named NAME. */
2fdf6df6 2235
9291a0cd
TT
2236static void
2237dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2238{
2239 dw2_setup (objfile);
2240
ae2de4f8 2241 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2242 if (dwarf2_per_objfile->index_table)
2243 {
2244 offset_type *vec;
2245
2246 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2247 name, &vec))
2248 {
2249 offset_type i, len = MAYBE_SWAP (*vec);
2250 for (i = 0; i < len; ++i)
2251 {
2252 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2253 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2254
e254ef6a 2255 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2256 }
2257 }
2258 }
2259}
2260
774b6a14
TT
2261static void
2262dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2263 int kind, const char *name,
2264 domain_enum domain)
9291a0cd 2265{
774b6a14 2266 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2267}
2268
2269static void
2270dw2_print_stats (struct objfile *objfile)
2271{
2272 int i, count;
2273
2274 dw2_setup (objfile);
2275 count = 0;
1fd400ff
TT
2276 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2277 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2278 {
e254ef6a 2279 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2280
e254ef6a 2281 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2282 ++count;
2283 }
2284 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2285}
2286
2287static void
2288dw2_dump (struct objfile *objfile)
2289{
2290 /* Nothing worth printing. */
2291}
2292
2293static void
2294dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2295 struct section_offsets *delta)
2296{
2297 /* There's nothing to relocate here. */
2298}
2299
2300static void
2301dw2_expand_symtabs_for_function (struct objfile *objfile,
2302 const char *func_name)
2303{
2304 dw2_do_expand_symtabs_matching (objfile, func_name);
2305}
2306
2307static void
2308dw2_expand_all_symtabs (struct objfile *objfile)
2309{
2310 int i;
2311
2312 dw2_setup (objfile);
1fd400ff
TT
2313
2314 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2315 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2316 {
e254ef6a 2317 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2318
e254ef6a 2319 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2320 }
2321}
2322
2323static void
2324dw2_expand_symtabs_with_filename (struct objfile *objfile,
2325 const char *filename)
2326{
2327 int i;
2328
2329 dw2_setup (objfile);
d4637a04
DE
2330
2331 /* We don't need to consider type units here.
2332 This is only called for examining code, e.g. expand_line_sal.
2333 There can be an order of magnitude (or more) more type units
2334 than comp units, and we avoid them if we can. */
2335
2336 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2337 {
2338 int j;
e254ef6a 2339 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2340
e254ef6a 2341 if (per_cu->v.quick->symtab)
9291a0cd
TT
2342 continue;
2343
e254ef6a
DE
2344 dw2_require_line_header (objfile, per_cu);
2345 if (!per_cu->v.quick->lines)
9291a0cd
TT
2346 continue;
2347
e254ef6a 2348 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2349 {
e254ef6a 2350 const char *this_name = per_cu->v.quick->file_names[j];
1ef75ecc 2351 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2352 {
e254ef6a 2353 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2354 break;
2355 }
2356 }
2357 }
2358}
2359
dd786858 2360static const char *
9291a0cd
TT
2361dw2_find_symbol_file (struct objfile *objfile, const char *name)
2362{
e254ef6a 2363 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2364 offset_type *vec;
2365
2366 dw2_setup (objfile);
2367
ae2de4f8 2368 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2369 if (!dwarf2_per_objfile->index_table)
2370 return NULL;
2371
2372 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2373 name, &vec))
2374 return NULL;
2375
2376 /* Note that this just looks at the very first one named NAME -- but
2377 actually we are looking for a function. find_main_filename
2378 should be rewritten so that it doesn't require a custom hook. It
2379 could just use the ordinary symbol tables. */
2380 /* vec[0] is the length, which must always be >0. */
e254ef6a 2381 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2382
e254ef6a
DE
2383 dw2_require_line_header (objfile, per_cu);
2384 if (!per_cu->v.quick->lines)
9291a0cd
TT
2385 return NULL;
2386
e254ef6a 2387 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2388}
2389
2390static void
40658b94
PH
2391dw2_map_matching_symbols (const char * name, domain_enum namespace,
2392 struct objfile *objfile, int global,
2393 int (*callback) (struct block *,
2394 struct symbol *, void *),
2edb89d3
JK
2395 void *data, symbol_compare_ftype *match,
2396 symbol_compare_ftype *ordered_compare)
9291a0cd 2397{
40658b94 2398 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2399 current language is Ada for a non-Ada objfile using GNU index. As Ada
2400 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2401}
2402
2403static void
2404dw2_expand_symtabs_matching (struct objfile *objfile,
2405 int (*file_matcher) (const char *, void *),
2406 int (*name_matcher) (const char *, void *),
2407 domain_enum kind,
2408 void *data)
2409{
2410 int i;
2411 offset_type iter;
4b5246aa 2412 struct mapped_index *index;
9291a0cd
TT
2413
2414 dw2_setup (objfile);
ae2de4f8
DE
2415
2416 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2417 if (!dwarf2_per_objfile->index_table)
2418 return;
4b5246aa 2419 index = dwarf2_per_objfile->index_table;
9291a0cd 2420
1fd400ff
TT
2421 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2422 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2423 {
2424 int j;
e254ef6a 2425 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2426
e254ef6a
DE
2427 per_cu->v.quick->mark = 0;
2428 if (per_cu->v.quick->symtab)
9291a0cd
TT
2429 continue;
2430
e254ef6a
DE
2431 dw2_require_line_header (objfile, per_cu);
2432 if (!per_cu->v.quick->lines)
9291a0cd
TT
2433 continue;
2434
e254ef6a 2435 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2436 {
e254ef6a 2437 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2438 {
e254ef6a 2439 per_cu->v.quick->mark = 1;
9291a0cd
TT
2440 break;
2441 }
2442 }
2443 }
2444
3876f04e 2445 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2446 {
2447 offset_type idx = 2 * iter;
2448 const char *name;
2449 offset_type *vec, vec_len, vec_idx;
2450
3876f04e 2451 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2452 continue;
2453
3876f04e 2454 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2455
2456 if (! (*name_matcher) (name, data))
2457 continue;
2458
2459 /* The name was matched, now expand corresponding CUs that were
2460 marked. */
4b5246aa 2461 vec = (offset_type *) (index->constant_pool
3876f04e 2462 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2463 vec_len = MAYBE_SWAP (vec[0]);
2464 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2465 {
e254ef6a 2466 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2467
e254ef6a
DE
2468 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2469 if (per_cu->v.quick->mark)
2470 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2471 }
2472 }
2473}
2474
2475static struct symtab *
2476dw2_find_pc_sect_symtab (struct objfile *objfile,
2477 struct minimal_symbol *msymbol,
2478 CORE_ADDR pc,
2479 struct obj_section *section,
2480 int warn_if_readin)
2481{
2482 struct dwarf2_per_cu_data *data;
2483
2484 dw2_setup (objfile);
2485
2486 if (!objfile->psymtabs_addrmap)
2487 return NULL;
2488
2489 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2490 if (!data)
2491 return NULL;
2492
2493 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2494 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2495 paddress (get_objfile_arch (objfile), pc));
2496
2497 return dw2_instantiate_symtab (objfile, data);
2498}
2499
2500static void
2501dw2_map_symbol_names (struct objfile *objfile,
2502 void (*fun) (const char *, void *),
2503 void *data)
2504{
2505 offset_type iter;
4b5246aa
TT
2506 struct mapped_index *index;
2507
9291a0cd
TT
2508 dw2_setup (objfile);
2509
ae2de4f8 2510 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2511 if (!dwarf2_per_objfile->index_table)
2512 return;
4b5246aa 2513 index = dwarf2_per_objfile->index_table;
9291a0cd 2514
3876f04e 2515 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2516 {
2517 offset_type idx = 2 * iter;
2518 const char *name;
2519 offset_type *vec, vec_len, vec_idx;
2520
3876f04e 2521 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2522 continue;
2523
3876f04e 2524 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2525
2526 (*fun) (name, data);
2527 }
2528}
2529
2530static void
2531dw2_map_symbol_filenames (struct objfile *objfile,
2532 void (*fun) (const char *, const char *, void *),
2533 void *data)
2534{
2535 int i;
2536
2537 dw2_setup (objfile);
ae2de4f8 2538
1fd400ff
TT
2539 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2540 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2541 {
2542 int j;
e254ef6a 2543 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2544
e254ef6a 2545 if (per_cu->v.quick->symtab)
9291a0cd
TT
2546 continue;
2547
e254ef6a
DE
2548 dw2_require_line_header (objfile, per_cu);
2549 if (!per_cu->v.quick->lines)
9291a0cd
TT
2550 continue;
2551
e254ef6a 2552 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2553 {
e254ef6a
DE
2554 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2555 j);
2556 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2557 }
2558 }
2559}
2560
2561static int
2562dw2_has_symbols (struct objfile *objfile)
2563{
2564 return 1;
2565}
2566
2567const struct quick_symbol_functions dwarf2_gdb_index_functions =
2568{
2569 dw2_has_symbols,
2570 dw2_find_last_source_symtab,
2571 dw2_forget_cached_source_info,
2572 dw2_lookup_symtab,
2573 dw2_lookup_symbol,
774b6a14 2574 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2575 dw2_print_stats,
2576 dw2_dump,
2577 dw2_relocate,
2578 dw2_expand_symtabs_for_function,
2579 dw2_expand_all_symtabs,
2580 dw2_expand_symtabs_with_filename,
2581 dw2_find_symbol_file,
40658b94 2582 dw2_map_matching_symbols,
9291a0cd
TT
2583 dw2_expand_symtabs_matching,
2584 dw2_find_pc_sect_symtab,
2585 dw2_map_symbol_names,
2586 dw2_map_symbol_filenames
2587};
2588
2589/* Initialize for reading DWARF for this objfile. Return 0 if this
2590 file will use psymtabs, or 1 if using the GNU index. */
2591
2592int
2593dwarf2_initialize_objfile (struct objfile *objfile)
2594{
2595 /* If we're about to read full symbols, don't bother with the
2596 indices. In this case we also don't care if some other debug
2597 format is making psymtabs, because they are all about to be
2598 expanded anyway. */
2599 if ((objfile->flags & OBJF_READNOW))
2600 {
2601 int i;
2602
2603 dwarf2_per_objfile->using_index = 1;
2604 create_all_comp_units (objfile);
1fd400ff 2605 create_debug_types_hash_table (objfile);
9291a0cd 2606
1fd400ff
TT
2607 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2608 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2609 {
e254ef6a 2610 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2611
e254ef6a
DE
2612 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2613 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2614 }
2615
2616 /* Return 1 so that gdb sees the "quick" functions. However,
2617 these functions will be no-ops because we will have expanded
2618 all symtabs. */
2619 return 1;
2620 }
2621
2622 if (dwarf2_read_index (objfile))
2623 return 1;
2624
2625 dwarf2_build_psymtabs (objfile);
2626 return 0;
2627}
2628
2629\f
2630
dce234bc
PP
2631/* Build a partial symbol table. */
2632
2633void
f29dff0a 2634dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2635{
f29dff0a 2636 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2637 {
2638 init_psymbol_list (objfile, 1024);
2639 }
2640
d146bf1e 2641 dwarf2_build_psymtabs_hard (objfile);
c906108c 2642}
c906108c 2643
45452591
DE
2644/* Return TRUE if OFFSET is within CU_HEADER. */
2645
2646static inline int
2647offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2648{
2649 unsigned int bottom = cu_header->offset;
2650 unsigned int top = (cu_header->offset
2651 + cu_header->length
2652 + cu_header->initial_length_size);
9a619af0 2653
45452591
DE
2654 return (offset >= bottom && offset < top);
2655}
2656
93311388
DE
2657/* Read in the comp unit header information from the debug_info at info_ptr.
2658 NOTE: This leaves members offset, first_die_offset to be filled in
2659 by the caller. */
107d2387 2660
fe1b8b76 2661static gdb_byte *
107d2387 2662read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2663 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2664{
2665 int signed_addr;
891d2f0b 2666 unsigned int bytes_read;
c764a876
DE
2667
2668 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2669 cu_header->initial_length_size = bytes_read;
2670 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2671 info_ptr += bytes_read;
107d2387
AC
2672 cu_header->version = read_2_bytes (abfd, info_ptr);
2673 info_ptr += 2;
613e1657 2674 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2675 &bytes_read);
613e1657 2676 info_ptr += bytes_read;
107d2387
AC
2677 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2678 info_ptr += 1;
2679 signed_addr = bfd_get_sign_extend_vma (abfd);
2680 if (signed_addr < 0)
8e65ff28 2681 internal_error (__FILE__, __LINE__,
e2e0b3e5 2682 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2683 cu_header->signed_addr_p = signed_addr;
c764a876 2684
107d2387
AC
2685 return info_ptr;
2686}
2687
fe1b8b76
JB
2688static gdb_byte *
2689partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2690 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2691 bfd *abfd)
2692{
fe1b8b76 2693 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2694
2695 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2696
2dc7f7b3 2697 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2698 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2699 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2700 bfd_get_filename (abfd));
72bf9492 2701
dce234bc 2702 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2703 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2704 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2705 (long) header->abbrev_offset,
93311388 2706 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2707 bfd_get_filename (abfd));
2708
2709 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2710 > buffer + buffer_size)
8a3fe4f8
AC
2711 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2712 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2713 (long) header->length,
93311388 2714 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2715 bfd_get_filename (abfd));
2716
2717 return info_ptr;
2718}
2719
348e048f
DE
2720/* Read in the types comp unit header information from .debug_types entry at
2721 types_ptr. The result is a pointer to one past the end of the header. */
2722
2723static gdb_byte *
2724read_type_comp_unit_head (struct comp_unit_head *cu_header,
2725 ULONGEST *signature,
2726 gdb_byte *types_ptr, bfd *abfd)
2727{
348e048f
DE
2728 gdb_byte *initial_types_ptr = types_ptr;
2729
6e70227d 2730 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2731 &dwarf2_per_objfile->types);
348e048f
DE
2732 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2733
2734 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2735
2736 *signature = read_8_bytes (abfd, types_ptr);
2737 types_ptr += 8;
2738 types_ptr += cu_header->offset_size;
2739 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2740
2741 return types_ptr;
2742}
2743
aaa75496
JB
2744/* Allocate a new partial symtab for file named NAME and mark this new
2745 partial symtab as being an include of PST. */
2746
2747static void
2748dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2749 struct objfile *objfile)
2750{
2751 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2752
2753 subpst->section_offsets = pst->section_offsets;
2754 subpst->textlow = 0;
2755 subpst->texthigh = 0;
2756
2757 subpst->dependencies = (struct partial_symtab **)
2758 obstack_alloc (&objfile->objfile_obstack,
2759 sizeof (struct partial_symtab *));
2760 subpst->dependencies[0] = pst;
2761 subpst->number_of_dependencies = 1;
2762
2763 subpst->globals_offset = 0;
2764 subpst->n_global_syms = 0;
2765 subpst->statics_offset = 0;
2766 subpst->n_static_syms = 0;
2767 subpst->symtab = NULL;
2768 subpst->read_symtab = pst->read_symtab;
2769 subpst->readin = 0;
2770
2771 /* No private part is necessary for include psymtabs. This property
2772 can be used to differentiate between such include psymtabs and
10b3939b 2773 the regular ones. */
58a9656e 2774 subpst->read_symtab_private = NULL;
aaa75496
JB
2775}
2776
2777/* Read the Line Number Program data and extract the list of files
2778 included by the source file represented by PST. Build an include
d85a05f0 2779 partial symtab for each of these included files. */
aaa75496
JB
2780
2781static void
2782dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2783 struct die_info *die,
aaa75496
JB
2784 struct partial_symtab *pst)
2785{
2786 struct objfile *objfile = cu->objfile;
2787 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2788 struct line_header *lh = NULL;
2789 struct attribute *attr;
aaa75496 2790
d85a05f0
DJ
2791 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2792 if (attr)
2793 {
2794 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2795
d85a05f0
DJ
2796 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2797 }
aaa75496
JB
2798 if (lh == NULL)
2799 return; /* No linetable, so no includes. */
2800
c6da4cef
DE
2801 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2802 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2803
2804 free_line_header (lh);
2805}
2806
348e048f
DE
2807static hashval_t
2808hash_type_signature (const void *item)
2809{
2810 const struct signatured_type *type_sig = item;
9a619af0 2811
348e048f
DE
2812 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2813 return type_sig->signature;
2814}
2815
2816static int
2817eq_type_signature (const void *item_lhs, const void *item_rhs)
2818{
2819 const struct signatured_type *lhs = item_lhs;
2820 const struct signatured_type *rhs = item_rhs;
9a619af0 2821
348e048f
DE
2822 return lhs->signature == rhs->signature;
2823}
2824
1fd400ff
TT
2825/* Allocate a hash table for signatured types. */
2826
2827static htab_t
673bfd45 2828allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2829{
2830 return htab_create_alloc_ex (41,
2831 hash_type_signature,
2832 eq_type_signature,
2833 NULL,
2834 &objfile->objfile_obstack,
2835 hashtab_obstack_allocate,
2836 dummy_obstack_deallocate);
2837}
2838
2839/* A helper function to add a signatured type CU to a list. */
2840
2841static int
2842add_signatured_type_cu_to_list (void **slot, void *datum)
2843{
2844 struct signatured_type *sigt = *slot;
2845 struct dwarf2_per_cu_data ***datap = datum;
2846
2847 **datap = &sigt->per_cu;
2848 ++*datap;
2849
2850 return 1;
2851}
2852
348e048f
DE
2853/* Create the hash table of all entries in the .debug_types section.
2854 The result is zero if there is an error (e.g. missing .debug_types section),
2855 otherwise non-zero. */
2856
2857static int
2858create_debug_types_hash_table (struct objfile *objfile)
2859{
be391dca 2860 gdb_byte *info_ptr;
348e048f 2861 htab_t types_htab;
1fd400ff 2862 struct dwarf2_per_cu_data **iter;
348e048f 2863
be391dca
TT
2864 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2865 info_ptr = dwarf2_per_objfile->types.buffer;
2866
348e048f
DE
2867 if (info_ptr == NULL)
2868 {
2869 dwarf2_per_objfile->signatured_types = NULL;
2870 return 0;
2871 }
2872
673bfd45 2873 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2874
2875 if (dwarf2_die_debug)
2876 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2877
2878 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2879 {
2880 unsigned int offset;
2881 unsigned int offset_size;
2882 unsigned int type_offset;
2883 unsigned int length, initial_length_size;
2884 unsigned short version;
2885 ULONGEST signature;
2886 struct signatured_type *type_sig;
2887 void **slot;
2888 gdb_byte *ptr = info_ptr;
2889
2890 offset = ptr - dwarf2_per_objfile->types.buffer;
2891
2892 /* We need to read the type's signature in order to build the hash
2893 table, but we don't need to read anything else just yet. */
2894
2895 /* Sanity check to ensure entire cu is present. */
2896 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2897 if (ptr + length + initial_length_size
2898 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2899 {
2900 complaint (&symfile_complaints,
2901 _("debug type entry runs off end of `.debug_types' section, ignored"));
2902 break;
2903 }
2904
2905 offset_size = initial_length_size == 4 ? 4 : 8;
2906 ptr += initial_length_size;
2907 version = bfd_get_16 (objfile->obfd, ptr);
2908 ptr += 2;
2909 ptr += offset_size; /* abbrev offset */
2910 ptr += 1; /* address size */
2911 signature = bfd_get_64 (objfile->obfd, ptr);
2912 ptr += 8;
2913 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2914
2915 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2916 memset (type_sig, 0, sizeof (*type_sig));
2917 type_sig->signature = signature;
2918 type_sig->offset = offset;
2919 type_sig->type_offset = type_offset;
ca1f3406 2920 type_sig->per_cu.objfile = objfile;
1fd400ff 2921 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2922
2923 slot = htab_find_slot (types_htab, type_sig, INSERT);
2924 gdb_assert (slot != NULL);
2925 *slot = type_sig;
2926
2927 if (dwarf2_die_debug)
2928 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2929 offset, phex (signature, sizeof (signature)));
2930
2931 info_ptr = info_ptr + initial_length_size + length;
2932 }
2933
2934 dwarf2_per_objfile->signatured_types = types_htab;
2935
1fd400ff
TT
2936 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2937 dwarf2_per_objfile->type_comp_units
2938 = obstack_alloc (&objfile->objfile_obstack,
2939 dwarf2_per_objfile->n_type_comp_units
2940 * sizeof (struct dwarf2_per_cu_data *));
2941 iter = &dwarf2_per_objfile->type_comp_units[0];
2942 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2943 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2944 == dwarf2_per_objfile->n_type_comp_units);
2945
348e048f
DE
2946 return 1;
2947}
2948
2949/* Lookup a signature based type.
2950 Returns NULL if SIG is not present in the table. */
2951
2952static struct signatured_type *
2953lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2954{
2955 struct signatured_type find_entry, *entry;
2956
2957 if (dwarf2_per_objfile->signatured_types == NULL)
2958 {
2959 complaint (&symfile_complaints,
2960 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2961 return 0;
2962 }
2963
2964 find_entry.signature = sig;
2965 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2966 return entry;
2967}
2968
d85a05f0
DJ
2969/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2970
2971static void
2972init_cu_die_reader (struct die_reader_specs *reader,
2973 struct dwarf2_cu *cu)
2974{
2975 reader->abfd = cu->objfile->obfd;
2976 reader->cu = cu;
2977 if (cu->per_cu->from_debug_types)
be391dca
TT
2978 {
2979 gdb_assert (dwarf2_per_objfile->types.readin);
2980 reader->buffer = dwarf2_per_objfile->types.buffer;
2981 }
d85a05f0 2982 else
be391dca
TT
2983 {
2984 gdb_assert (dwarf2_per_objfile->info.readin);
2985 reader->buffer = dwarf2_per_objfile->info.buffer;
2986 }
d85a05f0
DJ
2987}
2988
2989/* Find the base address of the compilation unit for range lists and
2990 location lists. It will normally be specified by DW_AT_low_pc.
2991 In DWARF-3 draft 4, the base address could be overridden by
2992 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2993 compilation units with discontinuous ranges. */
2994
2995static void
2996dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2997{
2998 struct attribute *attr;
2999
3000 cu->base_known = 0;
3001 cu->base_address = 0;
3002
3003 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3004 if (attr)
3005 {
3006 cu->base_address = DW_ADDR (attr);
3007 cu->base_known = 1;
3008 }
3009 else
3010 {
3011 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3012 if (attr)
3013 {
3014 cu->base_address = DW_ADDR (attr);
3015 cu->base_known = 1;
3016 }
3017 }
3018}
3019
348e048f
DE
3020/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3021 to combine the common parts.
93311388 3022 Process a compilation unit for a psymtab.
348e048f
DE
3023 BUFFER is a pointer to the beginning of the dwarf section buffer,
3024 either .debug_info or debug_types.
93311388
DE
3025 INFO_PTR is a pointer to the start of the CU.
3026 Returns a pointer to the next CU. */
aaa75496 3027
93311388
DE
3028static gdb_byte *
3029process_psymtab_comp_unit (struct objfile *objfile,
3030 struct dwarf2_per_cu_data *this_cu,
3031 gdb_byte *buffer, gdb_byte *info_ptr,
3032 unsigned int buffer_size)
c906108c 3033{
c906108c 3034 bfd *abfd = objfile->obfd;
93311388 3035 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3036 struct die_info *comp_unit_die;
c906108c 3037 struct partial_symtab *pst;
5734ee8b 3038 CORE_ADDR baseaddr;
93311388
DE
3039 struct cleanup *back_to_inner;
3040 struct dwarf2_cu cu;
d85a05f0
DJ
3041 int has_children, has_pc_info;
3042 struct attribute *attr;
d85a05f0
DJ
3043 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3044 struct die_reader_specs reader_specs;
c906108c 3045
9816fde3 3046 init_one_comp_unit (&cu, objfile);
93311388 3047 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3048
93311388
DE
3049 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3050 buffer, buffer_size,
3051 abfd);
10b3939b 3052
93311388
DE
3053 /* Complete the cu_header. */
3054 cu.header.offset = beg_of_comp_unit - buffer;
3055 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3056
93311388 3057 cu.list_in_scope = &file_symbols;
af703f96 3058
328c9494
DJ
3059 /* If this compilation unit was already read in, free the
3060 cached copy in order to read it in again. This is
3061 necessary because we skipped some symbols when we first
3062 read in the compilation unit (see load_partial_dies).
3063 This problem could be avoided, but the benefit is
3064 unclear. */
3065 if (this_cu->cu != NULL)
3066 free_one_cached_comp_unit (this_cu->cu);
3067
3068 /* Note that this is a pointer to our stack frame, being
3069 added to a global data structure. It will be cleaned up
3070 in free_stack_comp_unit when we finish with this
3071 compilation unit. */
3072 this_cu->cu = &cu;
d85a05f0
DJ
3073 cu.per_cu = this_cu;
3074
93311388
DE
3075 /* Read the abbrevs for this compilation unit into a table. */
3076 dwarf2_read_abbrevs (abfd, &cu);
3077 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3078
93311388 3079 /* Read the compilation unit die. */
348e048f
DE
3080 if (this_cu->from_debug_types)
3081 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3082 init_cu_die_reader (&reader_specs, &cu);
3083 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3084 &has_children);
93311388 3085
348e048f
DE
3086 if (this_cu->from_debug_types)
3087 {
3088 /* offset,length haven't been set yet for type units. */
3089 this_cu->offset = cu.header.offset;
3090 this_cu->length = cu.header.length + cu.header.initial_length_size;
3091 }
d85a05f0 3092 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3093 {
93311388
DE
3094 info_ptr = (beg_of_comp_unit + cu.header.length
3095 + cu.header.initial_length_size);
3096 do_cleanups (back_to_inner);
3097 return info_ptr;
3098 }
72bf9492 3099
9816fde3 3100 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3101
93311388 3102 /* Allocate a new partial symbol table structure. */
d85a05f0 3103 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3104 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3105 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3106 /* TEXTLOW and TEXTHIGH are set below. */
3107 0,
3108 objfile->global_psymbols.next,
3109 objfile->static_psymbols.next);
72bf9492 3110
d85a05f0
DJ
3111 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3112 if (attr != NULL)
3113 pst->dirname = DW_STRING (attr);
72bf9492 3114
e38df1d0 3115 pst->read_symtab_private = this_cu;
72bf9492 3116
93311388 3117 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3118
93311388
DE
3119 /* Store the function that reads in the rest of the symbol table */
3120 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3121
9291a0cd 3122 this_cu->v.psymtab = pst;
c906108c 3123
d85a05f0
DJ
3124 dwarf2_find_base_address (comp_unit_die, &cu);
3125
93311388
DE
3126 /* Possibly set the default values of LOWPC and HIGHPC from
3127 `DW_AT_ranges'. */
d85a05f0
DJ
3128 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3129 &best_highpc, &cu, pst);
3130 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3131 /* Store the contiguous range if it is not empty; it can be empty for
3132 CUs with no code. */
3133 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3134 best_lowpc + baseaddr,
3135 best_highpc + baseaddr - 1, pst);
93311388
DE
3136
3137 /* Check if comp unit has_children.
3138 If so, read the rest of the partial symbols from this comp unit.
3139 If not, there's no more debug_info for this comp unit. */
d85a05f0 3140 if (has_children)
93311388
DE
3141 {
3142 struct partial_die_info *first_die;
3143 CORE_ADDR lowpc, highpc;
31ffec48 3144
93311388
DE
3145 lowpc = ((CORE_ADDR) -1);
3146 highpc = ((CORE_ADDR) 0);
c906108c 3147
93311388 3148 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3149
93311388 3150 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3151 ! has_pc_info, &cu);
57c22c6c 3152
93311388
DE
3153 /* If we didn't find a lowpc, set it to highpc to avoid
3154 complaints from `maint check'. */
3155 if (lowpc == ((CORE_ADDR) -1))
3156 lowpc = highpc;
10b3939b 3157
93311388
DE
3158 /* If the compilation unit didn't have an explicit address range,
3159 then use the information extracted from its child dies. */
d85a05f0 3160 if (! has_pc_info)
93311388 3161 {
d85a05f0
DJ
3162 best_lowpc = lowpc;
3163 best_highpc = highpc;
93311388
DE
3164 }
3165 }
d85a05f0
DJ
3166 pst->textlow = best_lowpc + baseaddr;
3167 pst->texthigh = best_highpc + baseaddr;
c906108c 3168
93311388
DE
3169 pst->n_global_syms = objfile->global_psymbols.next -
3170 (objfile->global_psymbols.list + pst->globals_offset);
3171 pst->n_static_syms = objfile->static_psymbols.next -
3172 (objfile->static_psymbols.list + pst->statics_offset);
3173 sort_pst_symbols (pst);
c906108c 3174
93311388
DE
3175 info_ptr = (beg_of_comp_unit + cu.header.length
3176 + cu.header.initial_length_size);
ae038cb0 3177
348e048f
DE
3178 if (this_cu->from_debug_types)
3179 {
3180 /* It's not clear we want to do anything with stmt lists here.
3181 Waiting to see what gcc ultimately does. */
3182 }
d85a05f0 3183 else
93311388
DE
3184 {
3185 /* Get the list of files included in the current compilation unit,
3186 and build a psymtab for each of them. */
d85a05f0 3187 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3188 }
ae038cb0 3189
93311388 3190 do_cleanups (back_to_inner);
ae038cb0 3191
93311388
DE
3192 return info_ptr;
3193}
ff013f42 3194
348e048f
DE
3195/* Traversal function for htab_traverse_noresize.
3196 Process one .debug_types comp-unit. */
3197
3198static int
3199process_type_comp_unit (void **slot, void *info)
3200{
3201 struct signatured_type *entry = (struct signatured_type *) *slot;
3202 struct objfile *objfile = (struct objfile *) info;
3203 struct dwarf2_per_cu_data *this_cu;
3204
3205 this_cu = &entry->per_cu;
348e048f 3206
be391dca 3207 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3208 process_psymtab_comp_unit (objfile, this_cu,
3209 dwarf2_per_objfile->types.buffer,
3210 dwarf2_per_objfile->types.buffer + entry->offset,
3211 dwarf2_per_objfile->types.size);
3212
3213 return 1;
3214}
3215
3216/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3217 Build partial symbol tables for the .debug_types comp-units. */
3218
3219static void
3220build_type_psymtabs (struct objfile *objfile)
3221{
3222 if (! create_debug_types_hash_table (objfile))
3223 return;
3224
3225 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3226 process_type_comp_unit, objfile);
3227}
3228
60606b2c
TT
3229/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3230
3231static void
3232psymtabs_addrmap_cleanup (void *o)
3233{
3234 struct objfile *objfile = o;
ec61707d 3235
60606b2c
TT
3236 objfile->psymtabs_addrmap = NULL;
3237}
3238
93311388
DE
3239/* Build the partial symbol table by doing a quick pass through the
3240 .debug_info and .debug_abbrev sections. */
72bf9492 3241
93311388 3242static void
c67a9c90 3243dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3244{
93311388 3245 gdb_byte *info_ptr;
60606b2c
TT
3246 struct cleanup *back_to, *addrmap_cleanup;
3247 struct obstack temp_obstack;
93311388 3248
98bfdba5
PA
3249 dwarf2_per_objfile->reading_partial_symbols = 1;
3250
be391dca 3251 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3252 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3253
93311388
DE
3254 /* Any cached compilation units will be linked by the per-objfile
3255 read_in_chain. Make sure to free them when we're done. */
3256 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3257
348e048f
DE
3258 build_type_psymtabs (objfile);
3259
93311388 3260 create_all_comp_units (objfile);
c906108c 3261
60606b2c
TT
3262 /* Create a temporary address map on a temporary obstack. We later
3263 copy this to the final obstack. */
3264 obstack_init (&temp_obstack);
3265 make_cleanup_obstack_free (&temp_obstack);
3266 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3267 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3268
93311388
DE
3269 /* Since the objects we're extracting from .debug_info vary in
3270 length, only the individual functions to extract them (like
3271 read_comp_unit_head and load_partial_die) can really know whether
3272 the buffer is large enough to hold another complete object.
c906108c 3273
93311388
DE
3274 At the moment, they don't actually check that. If .debug_info
3275 holds just one extra byte after the last compilation unit's dies,
3276 then read_comp_unit_head will happily read off the end of the
3277 buffer. read_partial_die is similarly casual. Those functions
3278 should be fixed.
c906108c 3279
93311388
DE
3280 For this loop condition, simply checking whether there's any data
3281 left at all should be sufficient. */
c906108c 3282
93311388
DE
3283 while (info_ptr < (dwarf2_per_objfile->info.buffer
3284 + dwarf2_per_objfile->info.size))
3285 {
3286 struct dwarf2_per_cu_data *this_cu;
dd373385 3287
93311388
DE
3288 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3289 objfile);
aaa75496 3290
93311388
DE
3291 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3292 dwarf2_per_objfile->info.buffer,
3293 info_ptr,
3294 dwarf2_per_objfile->info.size);
c906108c 3295 }
ff013f42
JK
3296
3297 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3298 &objfile->objfile_obstack);
60606b2c 3299 discard_cleanups (addrmap_cleanup);
ff013f42 3300
ae038cb0
DJ
3301 do_cleanups (back_to);
3302}
3303
93311388 3304/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3305
3306static void
93311388
DE
3307load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3308 struct objfile *objfile)
ae038cb0
DJ
3309{
3310 bfd *abfd = objfile->obfd;
fe1b8b76 3311 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3312 struct die_info *comp_unit_die;
ae038cb0 3313 struct dwarf2_cu *cu;
1d9ec526 3314 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3315 int has_children;
3316 struct die_reader_specs reader_specs;
98bfdba5 3317 int read_cu = 0;
ae038cb0 3318
348e048f
DE
3319 gdb_assert (! this_cu->from_debug_types);
3320
be391dca 3321 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3322 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3323 beg_of_comp_unit = info_ptr;
3324
98bfdba5
PA
3325 if (this_cu->cu == NULL)
3326 {
9816fde3
JK
3327 cu = xmalloc (sizeof (*cu));
3328 init_one_comp_unit (cu, objfile);
ae038cb0 3329
98bfdba5 3330 read_cu = 1;
ae038cb0 3331
98bfdba5
PA
3332 /* If an error occurs while loading, release our storage. */
3333 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3334
98bfdba5
PA
3335 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3336 dwarf2_per_objfile->info.buffer,
3337 dwarf2_per_objfile->info.size,
3338 abfd);
ae038cb0 3339
98bfdba5
PA
3340 /* Complete the cu_header. */
3341 cu->header.offset = this_cu->offset;
3342 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3343
3344 /* Link this compilation unit into the compilation unit tree. */
3345 this_cu->cu = cu;
3346 cu->per_cu = this_cu;
98bfdba5
PA
3347
3348 /* Link this CU into read_in_chain. */
3349 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3350 dwarf2_per_objfile->read_in_chain = this_cu;
3351 }
3352 else
3353 {
3354 cu = this_cu->cu;
3355 info_ptr += cu->header.first_die_offset;
3356 }
ae038cb0
DJ
3357
3358 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3359 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3360 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3361 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3362
3363 /* Read the compilation unit die. */
d85a05f0
DJ
3364 init_cu_die_reader (&reader_specs, cu);
3365 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3366 &has_children);
ae038cb0 3367
9816fde3 3368 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3369
ae038cb0
DJ
3370 /* Check if comp unit has_children.
3371 If so, read the rest of the partial symbols from this comp unit.
3372 If not, there's no more debug_info for this comp unit. */
d85a05f0 3373 if (has_children)
93311388 3374 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3375
98bfdba5
PA
3376 do_cleanups (free_abbrevs_cleanup);
3377
3378 if (read_cu)
3379 {
3380 /* We've successfully allocated this compilation unit. Let our
3381 caller clean it up when finished with it. */
3382 discard_cleanups (free_cu_cleanup);
3383 }
ae038cb0
DJ
3384}
3385
3386/* Create a list of all compilation units in OBJFILE. We do this only
3387 if an inter-comp-unit reference is found; presumably if there is one,
3388 there will be many, and one will occur early in the .debug_info section.
3389 So there's no point in building this list incrementally. */
3390
3391static void
3392create_all_comp_units (struct objfile *objfile)
3393{
3394 int n_allocated;
3395 int n_comp_units;
3396 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3397 gdb_byte *info_ptr;
3398
3399 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3400 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3401
3402 n_comp_units = 0;
3403 n_allocated = 10;
3404 all_comp_units = xmalloc (n_allocated
3405 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3406
dce234bc 3407 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3408 {
c764a876 3409 unsigned int length, initial_length_size;
ae038cb0 3410 struct dwarf2_per_cu_data *this_cu;
c764a876 3411 unsigned int offset;
ae038cb0 3412
dce234bc 3413 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3414
3415 /* Read just enough information to find out where the next
3416 compilation unit is. */
c764a876
DE
3417 length = read_initial_length (objfile->obfd, info_ptr,
3418 &initial_length_size);
ae038cb0
DJ
3419
3420 /* Save the compilation unit for later lookup. */
3421 this_cu = obstack_alloc (&objfile->objfile_obstack,
3422 sizeof (struct dwarf2_per_cu_data));
3423 memset (this_cu, 0, sizeof (*this_cu));
3424 this_cu->offset = offset;
c764a876 3425 this_cu->length = length + initial_length_size;
9291a0cd 3426 this_cu->objfile = objfile;
ae038cb0
DJ
3427
3428 if (n_comp_units == n_allocated)
3429 {
3430 n_allocated *= 2;
3431 all_comp_units = xrealloc (all_comp_units,
3432 n_allocated
3433 * sizeof (struct dwarf2_per_cu_data *));
3434 }
3435 all_comp_units[n_comp_units++] = this_cu;
3436
3437 info_ptr = info_ptr + this_cu->length;
3438 }
3439
3440 dwarf2_per_objfile->all_comp_units
3441 = obstack_alloc (&objfile->objfile_obstack,
3442 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3443 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3444 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3445 xfree (all_comp_units);
3446 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3447}
3448
5734ee8b
DJ
3449/* Process all loaded DIEs for compilation unit CU, starting at
3450 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3451 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3452 DW_AT_ranges). If NEED_PC is set, then this function will set
3453 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3454 and record the covered ranges in the addrmap. */
c906108c 3455
72bf9492
DJ
3456static void
3457scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3458 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3459{
72bf9492 3460 struct partial_die_info *pdi;
c906108c 3461
91c24f0a
DC
3462 /* Now, march along the PDI's, descending into ones which have
3463 interesting children but skipping the children of the other ones,
3464 until we reach the end of the compilation unit. */
c906108c 3465
72bf9492 3466 pdi = first_die;
91c24f0a 3467
72bf9492
DJ
3468 while (pdi != NULL)
3469 {
3470 fixup_partial_die (pdi, cu);
c906108c 3471
f55ee35c 3472 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3473 children, so we need to look at them. Ditto for anonymous
3474 enums. */
933c6fe4 3475
72bf9492 3476 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3477 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3478 {
72bf9492 3479 switch (pdi->tag)
c906108c
SS
3480 {
3481 case DW_TAG_subprogram:
5734ee8b 3482 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3483 break;
72929c62 3484 case DW_TAG_constant:
c906108c
SS
3485 case DW_TAG_variable:
3486 case DW_TAG_typedef:
91c24f0a 3487 case DW_TAG_union_type:
72bf9492 3488 if (!pdi->is_declaration)
63d06c5c 3489 {
72bf9492 3490 add_partial_symbol (pdi, cu);
63d06c5c
DC
3491 }
3492 break;
c906108c 3493 case DW_TAG_class_type:
680b30c7 3494 case DW_TAG_interface_type:
c906108c 3495 case DW_TAG_structure_type:
72bf9492 3496 if (!pdi->is_declaration)
c906108c 3497 {
72bf9492 3498 add_partial_symbol (pdi, cu);
c906108c
SS
3499 }
3500 break;
91c24f0a 3501 case DW_TAG_enumeration_type:
72bf9492
DJ
3502 if (!pdi->is_declaration)
3503 add_partial_enumeration (pdi, cu);
c906108c
SS
3504 break;
3505 case DW_TAG_base_type:
a02abb62 3506 case DW_TAG_subrange_type:
c906108c 3507 /* File scope base type definitions are added to the partial
c5aa993b 3508 symbol table. */
72bf9492 3509 add_partial_symbol (pdi, cu);
c906108c 3510 break;
d9fa45fe 3511 case DW_TAG_namespace:
5734ee8b 3512 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3513 break;
5d7cb8df
JK
3514 case DW_TAG_module:
3515 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3516 break;
c906108c
SS
3517 default:
3518 break;
3519 }
3520 }
3521
72bf9492
DJ
3522 /* If the die has a sibling, skip to the sibling. */
3523
3524 pdi = pdi->die_sibling;
3525 }
3526}
3527
3528/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3529
72bf9492 3530 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3531 name is concatenated with "::" and the partial DIE's name. For
3532 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3533 Enumerators are an exception; they use the scope of their parent
3534 enumeration type, i.e. the name of the enumeration type is not
3535 prepended to the enumerator.
91c24f0a 3536
72bf9492
DJ
3537 There are two complexities. One is DW_AT_specification; in this
3538 case "parent" means the parent of the target of the specification,
3539 instead of the direct parent of the DIE. The other is compilers
3540 which do not emit DW_TAG_namespace; in this case we try to guess
3541 the fully qualified name of structure types from their members'
3542 linkage names. This must be done using the DIE's children rather
3543 than the children of any DW_AT_specification target. We only need
3544 to do this for structures at the top level, i.e. if the target of
3545 any DW_AT_specification (if any; otherwise the DIE itself) does not
3546 have a parent. */
3547
3548/* Compute the scope prefix associated with PDI's parent, in
3549 compilation unit CU. The result will be allocated on CU's
3550 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3551 field. NULL is returned if no prefix is necessary. */
3552static char *
3553partial_die_parent_scope (struct partial_die_info *pdi,
3554 struct dwarf2_cu *cu)
3555{
3556 char *grandparent_scope;
3557 struct partial_die_info *parent, *real_pdi;
91c24f0a 3558
72bf9492
DJ
3559 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3560 then this means the parent of the specification DIE. */
3561
3562 real_pdi = pdi;
72bf9492 3563 while (real_pdi->has_specification)
10b3939b 3564 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3565
3566 parent = real_pdi->die_parent;
3567 if (parent == NULL)
3568 return NULL;
3569
3570 if (parent->scope_set)
3571 return parent->scope;
3572
3573 fixup_partial_die (parent, cu);
3574
10b3939b 3575 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3576
acebe513
UW
3577 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3578 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3579 Work around this problem here. */
3580 if (cu->language == language_cplus
6e70227d 3581 && parent->tag == DW_TAG_namespace
acebe513
UW
3582 && strcmp (parent->name, "::") == 0
3583 && grandparent_scope == NULL)
3584 {
3585 parent->scope = NULL;
3586 parent->scope_set = 1;
3587 return NULL;
3588 }
3589
72bf9492 3590 if (parent->tag == DW_TAG_namespace
f55ee35c 3591 || parent->tag == DW_TAG_module
72bf9492
DJ
3592 || parent->tag == DW_TAG_structure_type
3593 || parent->tag == DW_TAG_class_type
680b30c7 3594 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3595 || parent->tag == DW_TAG_union_type
3596 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3597 {
3598 if (grandparent_scope == NULL)
3599 parent->scope = parent->name;
3600 else
987504bb 3601 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3602 parent->name, 0, cu);
72bf9492 3603 }
ceeb3d5a 3604 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3605 /* Enumerators should not get the name of the enumeration as a prefix. */
3606 parent->scope = grandparent_scope;
3607 else
3608 {
3609 /* FIXME drow/2004-04-01: What should we be doing with
3610 function-local names? For partial symbols, we should probably be
3611 ignoring them. */
3612 complaint (&symfile_complaints,
e2e0b3e5 3613 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3614 parent->tag, pdi->offset);
3615 parent->scope = grandparent_scope;
c906108c
SS
3616 }
3617
72bf9492
DJ
3618 parent->scope_set = 1;
3619 return parent->scope;
3620}
3621
3622/* Return the fully scoped name associated with PDI, from compilation unit
3623 CU. The result will be allocated with malloc. */
3624static char *
3625partial_die_full_name (struct partial_die_info *pdi,
3626 struct dwarf2_cu *cu)
3627{
3628 char *parent_scope;
3629
98bfdba5
PA
3630 /* If this is a template instantiation, we can not work out the
3631 template arguments from partial DIEs. So, unfortunately, we have
3632 to go through the full DIEs. At least any work we do building
3633 types here will be reused if full symbols are loaded later. */
3634 if (pdi->has_template_arguments)
3635 {
3636 fixup_partial_die (pdi, cu);
3637
3638 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3639 {
3640 struct die_info *die;
3641 struct attribute attr;
3642 struct dwarf2_cu *ref_cu = cu;
3643
3644 attr.name = 0;
3645 attr.form = DW_FORM_ref_addr;
3646 attr.u.addr = pdi->offset;
3647 die = follow_die_ref (NULL, &attr, &ref_cu);
3648
3649 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3650 }
3651 }
3652
72bf9492
DJ
3653 parent_scope = partial_die_parent_scope (pdi, cu);
3654 if (parent_scope == NULL)
3655 return NULL;
3656 else
f55ee35c 3657 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3658}
3659
3660static void
72bf9492 3661add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3662{
e7c27a73 3663 struct objfile *objfile = cu->objfile;
c906108c 3664 CORE_ADDR addr = 0;
decbce07 3665 char *actual_name = NULL;
5c4e30ca 3666 const struct partial_symbol *psym = NULL;
e142c38c 3667 CORE_ADDR baseaddr;
72bf9492 3668 int built_actual_name = 0;
e142c38c
DJ
3669
3670 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3671
94af9270
KS
3672 actual_name = partial_die_full_name (pdi, cu);
3673 if (actual_name)
3674 built_actual_name = 1;
63d06c5c 3675
72bf9492
DJ
3676 if (actual_name == NULL)
3677 actual_name = pdi->name;
3678
c906108c
SS
3679 switch (pdi->tag)
3680 {
3681 case DW_TAG_subprogram:
2cfa0c8d 3682 if (pdi->is_external || cu->language == language_ada)
c906108c 3683 {
2cfa0c8d
JB
3684 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3685 of the global scope. But in Ada, we want to be able to access
3686 nested procedures globally. So all Ada subprograms are stored
3687 in the global scope. */
38d518c9 3688 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3689 mst_text, objfile); */
38d518c9 3690 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3691 built_actual_name,
5c4e30ca
DC
3692 VAR_DOMAIN, LOC_BLOCK,
3693 &objfile->global_psymbols,
3694 0, pdi->lowpc + baseaddr,
e142c38c 3695 cu->language, objfile);
c906108c
SS
3696 }
3697 else
3698 {
38d518c9 3699 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3700 mst_file_text, objfile); */
38d518c9 3701 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3702 built_actual_name,
5c4e30ca
DC
3703 VAR_DOMAIN, LOC_BLOCK,
3704 &objfile->static_psymbols,
3705 0, pdi->lowpc + baseaddr,
e142c38c 3706 cu->language, objfile);
c906108c
SS
3707 }
3708 break;
72929c62
JB
3709 case DW_TAG_constant:
3710 {
3711 struct psymbol_allocation_list *list;
3712
3713 if (pdi->is_external)
3714 list = &objfile->global_psymbols;
3715 else
3716 list = &objfile->static_psymbols;
3717 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3718 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3719 list, 0, 0, cu->language, objfile);
3720
3721 }
3722 break;
c906108c 3723 case DW_TAG_variable:
caac4577
JG
3724 if (pdi->locdesc)
3725 addr = decode_locdesc (pdi->locdesc, cu);
3726
3727 if (pdi->locdesc
3728 && addr == 0
3729 && !dwarf2_per_objfile->has_section_at_zero)
3730 {
3731 /* A global or static variable may also have been stripped
3732 out by the linker if unused, in which case its address
3733 will be nullified; do not add such variables into partial
3734 symbol table then. */
3735 }
3736 else if (pdi->is_external)
c906108c
SS
3737 {
3738 /* Global Variable.
3739 Don't enter into the minimal symbol tables as there is
3740 a minimal symbol table entry from the ELF symbols already.
3741 Enter into partial symbol table if it has a location
3742 descriptor or a type.
3743 If the location descriptor is missing, new_symbol will create
3744 a LOC_UNRESOLVED symbol, the address of the variable will then
3745 be determined from the minimal symbol table whenever the variable
3746 is referenced.
3747 The address for the partial symbol table entry is not
3748 used by GDB, but it comes in handy for debugging partial symbol
3749 table building. */
3750
c906108c 3751 if (pdi->locdesc || pdi->has_type)
38d518c9 3752 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3753 built_actual_name,
5c4e30ca
DC
3754 VAR_DOMAIN, LOC_STATIC,
3755 &objfile->global_psymbols,
3756 0, addr + baseaddr,
e142c38c 3757 cu->language, objfile);
c906108c
SS
3758 }
3759 else
3760 {
3761 /* Static Variable. Skip symbols without location descriptors. */
3762 if (pdi->locdesc == NULL)
decbce07
MS
3763 {
3764 if (built_actual_name)
3765 xfree (actual_name);
3766 return;
3767 }
38d518c9 3768 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3769 mst_file_data, objfile); */
38d518c9 3770 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3771 built_actual_name,
5c4e30ca
DC
3772 VAR_DOMAIN, LOC_STATIC,
3773 &objfile->static_psymbols,
3774 0, addr + baseaddr,
e142c38c 3775 cu->language, objfile);
c906108c
SS
3776 }
3777 break;
3778 case DW_TAG_typedef:
3779 case DW_TAG_base_type:
a02abb62 3780 case DW_TAG_subrange_type:
38d518c9 3781 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3782 built_actual_name,
176620f1 3783 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3784 &objfile->static_psymbols,
e142c38c 3785 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3786 break;
72bf9492
DJ
3787 case DW_TAG_namespace:
3788 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3789 built_actual_name,
72bf9492
DJ
3790 VAR_DOMAIN, LOC_TYPEDEF,
3791 &objfile->global_psymbols,
3792 0, (CORE_ADDR) 0, cu->language, objfile);
3793 break;
c906108c 3794 case DW_TAG_class_type:
680b30c7 3795 case DW_TAG_interface_type:
c906108c
SS
3796 case DW_TAG_structure_type:
3797 case DW_TAG_union_type:
3798 case DW_TAG_enumeration_type:
fa4028e9
JB
3799 /* Skip external references. The DWARF standard says in the section
3800 about "Structure, Union, and Class Type Entries": "An incomplete
3801 structure, union or class type is represented by a structure,
3802 union or class entry that does not have a byte size attribute
3803 and that has a DW_AT_declaration attribute." */
3804 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3805 {
3806 if (built_actual_name)
3807 xfree (actual_name);
3808 return;
3809 }
fa4028e9 3810
63d06c5c
DC
3811 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3812 static vs. global. */
38d518c9 3813 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3814 built_actual_name,
176620f1 3815 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3816 (cu->language == language_cplus
3817 || cu->language == language_java)
63d06c5c
DC
3818 ? &objfile->global_psymbols
3819 : &objfile->static_psymbols,
e142c38c 3820 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3821
c906108c
SS
3822 break;
3823 case DW_TAG_enumerator:
38d518c9 3824 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3825 built_actual_name,
176620f1 3826 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3827 (cu->language == language_cplus
3828 || cu->language == language_java)
f6fe98ef
DJ
3829 ? &objfile->global_psymbols
3830 : &objfile->static_psymbols,
e142c38c 3831 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3832 break;
3833 default:
3834 break;
3835 }
5c4e30ca 3836
72bf9492
DJ
3837 if (built_actual_name)
3838 xfree (actual_name);
c906108c
SS
3839}
3840
5c4e30ca
DC
3841/* Read a partial die corresponding to a namespace; also, add a symbol
3842 corresponding to that namespace to the symbol table. NAMESPACE is
3843 the name of the enclosing namespace. */
91c24f0a 3844
72bf9492
DJ
3845static void
3846add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3847 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3848 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3849{
72bf9492 3850 /* Add a symbol for the namespace. */
e7c27a73 3851
72bf9492 3852 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3853
3854 /* Now scan partial symbols in that namespace. */
3855
91c24f0a 3856 if (pdi->has_children)
5734ee8b 3857 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3858}
3859
5d7cb8df
JK
3860/* Read a partial die corresponding to a Fortran module. */
3861
3862static void
3863add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3864 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3865{
f55ee35c 3866 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3867
3868 if (pdi->has_children)
3869 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3870}
3871
bc30ff58
JB
3872/* Read a partial die corresponding to a subprogram and create a partial
3873 symbol for that subprogram. When the CU language allows it, this
3874 routine also defines a partial symbol for each nested subprogram
3875 that this subprogram contains.
6e70227d 3876
bc30ff58
JB
3877 DIE my also be a lexical block, in which case we simply search
3878 recursively for suprograms defined inside that lexical block.
3879 Again, this is only performed when the CU language allows this
3880 type of definitions. */
3881
3882static void
3883add_partial_subprogram (struct partial_die_info *pdi,
3884 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3885 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3886{
3887 if (pdi->tag == DW_TAG_subprogram)
3888 {
3889 if (pdi->has_pc_info)
3890 {
3891 if (pdi->lowpc < *lowpc)
3892 *lowpc = pdi->lowpc;
3893 if (pdi->highpc > *highpc)
3894 *highpc = pdi->highpc;
5734ee8b
DJ
3895 if (need_pc)
3896 {
3897 CORE_ADDR baseaddr;
3898 struct objfile *objfile = cu->objfile;
3899
3900 baseaddr = ANOFFSET (objfile->section_offsets,
3901 SECT_OFF_TEXT (objfile));
3902 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3903 pdi->lowpc + baseaddr,
3904 pdi->highpc - 1 + baseaddr,
9291a0cd 3905 cu->per_cu->v.psymtab);
5734ee8b 3906 }
bc30ff58 3907 if (!pdi->is_declaration)
e8d05480
JB
3908 /* Ignore subprogram DIEs that do not have a name, they are
3909 illegal. Do not emit a complaint at this point, we will
3910 do so when we convert this psymtab into a symtab. */
3911 if (pdi->name)
3912 add_partial_symbol (pdi, cu);
bc30ff58
JB
3913 }
3914 }
6e70227d 3915
bc30ff58
JB
3916 if (! pdi->has_children)
3917 return;
3918
3919 if (cu->language == language_ada)
3920 {
3921 pdi = pdi->die_child;
3922 while (pdi != NULL)
3923 {
3924 fixup_partial_die (pdi, cu);
3925 if (pdi->tag == DW_TAG_subprogram
3926 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3927 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3928 pdi = pdi->die_sibling;
3929 }
3930 }
3931}
3932
91c24f0a
DC
3933/* Read a partial die corresponding to an enumeration type. */
3934
72bf9492
DJ
3935static void
3936add_partial_enumeration (struct partial_die_info *enum_pdi,
3937 struct dwarf2_cu *cu)
91c24f0a 3938{
72bf9492 3939 struct partial_die_info *pdi;
91c24f0a
DC
3940
3941 if (enum_pdi->name != NULL)
72bf9492
DJ
3942 add_partial_symbol (enum_pdi, cu);
3943
3944 pdi = enum_pdi->die_child;
3945 while (pdi)
91c24f0a 3946 {
72bf9492 3947 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3948 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3949 else
72bf9492
DJ
3950 add_partial_symbol (pdi, cu);
3951 pdi = pdi->die_sibling;
91c24f0a 3952 }
91c24f0a
DC
3953}
3954
4bb7a0a7
DJ
3955/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3956 Return the corresponding abbrev, or NULL if the number is zero (indicating
3957 an empty DIE). In either case *BYTES_READ will be set to the length of
3958 the initial number. */
3959
3960static struct abbrev_info *
fe1b8b76 3961peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3962 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3963{
3964 bfd *abfd = cu->objfile->obfd;
3965 unsigned int abbrev_number;
3966 struct abbrev_info *abbrev;
3967
3968 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3969
3970 if (abbrev_number == 0)
3971 return NULL;
3972
3973 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3974 if (!abbrev)
3975 {
8a3fe4f8 3976 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3977 bfd_get_filename (abfd));
3978 }
3979
3980 return abbrev;
3981}
3982
93311388
DE
3983/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3984 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3985 DIE. Any children of the skipped DIEs will also be skipped. */
3986
fe1b8b76 3987static gdb_byte *
93311388 3988skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3989{
3990 struct abbrev_info *abbrev;
3991 unsigned int bytes_read;
3992
3993 while (1)
3994 {
3995 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3996 if (abbrev == NULL)
3997 return info_ptr + bytes_read;
3998 else
93311388 3999 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4000 }
4001}
4002
93311388
DE
4003/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4004 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4005 abbrev corresponding to that skipped uleb128 should be passed in
4006 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4007 children. */
4008
fe1b8b76 4009static gdb_byte *
93311388
DE
4010skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4011 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4012{
4013 unsigned int bytes_read;
4014 struct attribute attr;
4015 bfd *abfd = cu->objfile->obfd;
4016 unsigned int form, i;
4017
4018 for (i = 0; i < abbrev->num_attrs; i++)
4019 {
4020 /* The only abbrev we care about is DW_AT_sibling. */
4021 if (abbrev->attrs[i].name == DW_AT_sibling)
4022 {
4023 read_attribute (&attr, &abbrev->attrs[i],
4024 abfd, info_ptr, cu);
4025 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4026 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4027 else
93311388 4028 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4029 }
4030
4031 /* If it isn't DW_AT_sibling, skip this attribute. */
4032 form = abbrev->attrs[i].form;
4033 skip_attribute:
4034 switch (form)
4035 {
4bb7a0a7 4036 case DW_FORM_ref_addr:
ae411497
TT
4037 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4038 and later it is offset sized. */
4039 if (cu->header.version == 2)
4040 info_ptr += cu->header.addr_size;
4041 else
4042 info_ptr += cu->header.offset_size;
4043 break;
4044 case DW_FORM_addr:
4bb7a0a7
DJ
4045 info_ptr += cu->header.addr_size;
4046 break;
4047 case DW_FORM_data1:
4048 case DW_FORM_ref1:
4049 case DW_FORM_flag:
4050 info_ptr += 1;
4051 break;
2dc7f7b3
TT
4052 case DW_FORM_flag_present:
4053 break;
4bb7a0a7
DJ
4054 case DW_FORM_data2:
4055 case DW_FORM_ref2:
4056 info_ptr += 2;
4057 break;
4058 case DW_FORM_data4:
4059 case DW_FORM_ref4:
4060 info_ptr += 4;
4061 break;
4062 case DW_FORM_data8:
4063 case DW_FORM_ref8:
348e048f 4064 case DW_FORM_sig8:
4bb7a0a7
DJ
4065 info_ptr += 8;
4066 break;
4067 case DW_FORM_string:
9b1c24c8 4068 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4069 info_ptr += bytes_read;
4070 break;
2dc7f7b3 4071 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4072 case DW_FORM_strp:
4073 info_ptr += cu->header.offset_size;
4074 break;
2dc7f7b3 4075 case DW_FORM_exprloc:
4bb7a0a7
DJ
4076 case DW_FORM_block:
4077 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4078 info_ptr += bytes_read;
4079 break;
4080 case DW_FORM_block1:
4081 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4082 break;
4083 case DW_FORM_block2:
4084 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4085 break;
4086 case DW_FORM_block4:
4087 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4088 break;
4089 case DW_FORM_sdata:
4090 case DW_FORM_udata:
4091 case DW_FORM_ref_udata:
4092 info_ptr = skip_leb128 (abfd, info_ptr);
4093 break;
4094 case DW_FORM_indirect:
4095 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4096 info_ptr += bytes_read;
4097 /* We need to continue parsing from here, so just go back to
4098 the top. */
4099 goto skip_attribute;
4100
4101 default:
8a3fe4f8 4102 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4103 dwarf_form_name (form),
4104 bfd_get_filename (abfd));
4105 }
4106 }
4107
4108 if (abbrev->has_children)
93311388 4109 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4110 else
4111 return info_ptr;
4112}
4113
93311388
DE
4114/* Locate ORIG_PDI's sibling.
4115 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4116 in BUFFER. */
91c24f0a 4117
fe1b8b76 4118static gdb_byte *
93311388
DE
4119locate_pdi_sibling (struct partial_die_info *orig_pdi,
4120 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4121 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4122{
4123 /* Do we know the sibling already? */
72bf9492 4124
91c24f0a
DC
4125 if (orig_pdi->sibling)
4126 return orig_pdi->sibling;
4127
4128 /* Are there any children to deal with? */
4129
4130 if (!orig_pdi->has_children)
4131 return info_ptr;
4132
4bb7a0a7 4133 /* Skip the children the long way. */
91c24f0a 4134
93311388 4135 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4136}
4137
c906108c
SS
4138/* Expand this partial symbol table into a full symbol table. */
4139
4140static void
fba45db2 4141dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4142{
c906108c
SS
4143 if (pst != NULL)
4144 {
4145 if (pst->readin)
4146 {
8a3fe4f8 4147 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4148 }
4149 else
4150 {
4151 if (info_verbose)
4152 {
a3f17187 4153 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4154 gdb_flush (gdb_stdout);
4155 }
4156
10b3939b
DJ
4157 /* Restore our global data. */
4158 dwarf2_per_objfile = objfile_data (pst->objfile,
4159 dwarf2_objfile_data_key);
4160
b2ab525c
KB
4161 /* If this psymtab is constructed from a debug-only objfile, the
4162 has_section_at_zero flag will not necessarily be correct. We
4163 can get the correct value for this flag by looking at the data
4164 associated with the (presumably stripped) associated objfile. */
4165 if (pst->objfile->separate_debug_objfile_backlink)
4166 {
4167 struct dwarf2_per_objfile *dpo_backlink
4168 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4169 dwarf2_objfile_data_key);
9a619af0 4170
b2ab525c
KB
4171 dwarf2_per_objfile->has_section_at_zero
4172 = dpo_backlink->has_section_at_zero;
4173 }
4174
98bfdba5
PA
4175 dwarf2_per_objfile->reading_partial_symbols = 0;
4176
c906108c
SS
4177 psymtab_to_symtab_1 (pst);
4178
4179 /* Finish up the debug error message. */
4180 if (info_verbose)
a3f17187 4181 printf_filtered (_("done.\n"));
c906108c
SS
4182 }
4183 }
4184}
4185
10b3939b
DJ
4186/* Add PER_CU to the queue. */
4187
4188static void
03dd20cc 4189queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4190{
4191 struct dwarf2_queue_item *item;
4192
4193 per_cu->queued = 1;
4194 item = xmalloc (sizeof (*item));
4195 item->per_cu = per_cu;
4196 item->next = NULL;
4197
4198 if (dwarf2_queue == NULL)
4199 dwarf2_queue = item;
4200 else
4201 dwarf2_queue_tail->next = item;
4202
4203 dwarf2_queue_tail = item;
4204}
4205
4206/* Process the queue. */
4207
4208static void
4209process_queue (struct objfile *objfile)
4210{
4211 struct dwarf2_queue_item *item, *next_item;
4212
03dd20cc
DJ
4213 /* The queue starts out with one item, but following a DIE reference
4214 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4215 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4216 {
9291a0cd
TT
4217 if (dwarf2_per_objfile->using_index
4218 ? !item->per_cu->v.quick->symtab
4219 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4220 process_full_comp_unit (item->per_cu);
4221
4222 item->per_cu->queued = 0;
4223 next_item = item->next;
4224 xfree (item);
4225 }
4226
4227 dwarf2_queue_tail = NULL;
4228}
4229
4230/* Free all allocated queue entries. This function only releases anything if
4231 an error was thrown; if the queue was processed then it would have been
4232 freed as we went along. */
4233
4234static void
4235dwarf2_release_queue (void *dummy)
4236{
4237 struct dwarf2_queue_item *item, *last;
4238
4239 item = dwarf2_queue;
4240 while (item)
4241 {
4242 /* Anything still marked queued is likely to be in an
4243 inconsistent state, so discard it. */
4244 if (item->per_cu->queued)
4245 {
4246 if (item->per_cu->cu != NULL)
4247 free_one_cached_comp_unit (item->per_cu->cu);
4248 item->per_cu->queued = 0;
4249 }
4250
4251 last = item;
4252 item = item->next;
4253 xfree (last);
4254 }
4255
4256 dwarf2_queue = dwarf2_queue_tail = NULL;
4257}
4258
4259/* Read in full symbols for PST, and anything it depends on. */
4260
c906108c 4261static void
fba45db2 4262psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4263{
10b3939b 4264 struct dwarf2_per_cu_data *per_cu;
c906108c 4265 struct cleanup *back_to;
aaa75496
JB
4266 int i;
4267
4268 for (i = 0; i < pst->number_of_dependencies; i++)
4269 if (!pst->dependencies[i]->readin)
4270 {
4271 /* Inform about additional files that need to be read in. */
4272 if (info_verbose)
4273 {
a3f17187 4274 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4275 fputs_filtered (" ", gdb_stdout);
4276 wrap_here ("");
4277 fputs_filtered ("and ", gdb_stdout);
4278 wrap_here ("");
4279 printf_filtered ("%s...", pst->dependencies[i]->filename);
4280 wrap_here (""); /* Flush output */
4281 gdb_flush (gdb_stdout);
4282 }
4283 psymtab_to_symtab_1 (pst->dependencies[i]);
4284 }
4285
e38df1d0 4286 per_cu = pst->read_symtab_private;
10b3939b
DJ
4287
4288 if (per_cu == NULL)
aaa75496
JB
4289 {
4290 /* It's an include file, no symbols to read for it.
4291 Everything is in the parent symtab. */
4292 pst->readin = 1;
4293 return;
4294 }
c906108c 4295
9291a0cd 4296 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4297}
4298
93311388 4299/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4300
93311388 4301static void
31ffec48 4302load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4303{
31ffec48 4304 bfd *abfd = objfile->obfd;
10b3939b 4305 struct dwarf2_cu *cu;
c764a876 4306 unsigned int offset;
93311388 4307 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4308 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4309 struct attribute *attr;
98bfdba5 4310 int read_cu = 0;
6502dd73 4311
348e048f
DE
4312 gdb_assert (! per_cu->from_debug_types);
4313
c906108c 4314 /* Set local variables from the partial symbol table info. */
10b3939b 4315 offset = per_cu->offset;
6502dd73 4316
be391dca 4317 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4318 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4319 beg_of_comp_unit = info_ptr;
63d06c5c 4320
98bfdba5
PA
4321 if (per_cu->cu == NULL)
4322 {
9816fde3
JK
4323 cu = xmalloc (sizeof (*cu));
4324 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4325
4326 read_cu = 1;
c906108c 4327
98bfdba5
PA
4328 /* If an error occurs while loading, release our storage. */
4329 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4330
98bfdba5
PA
4331 /* Read in the comp_unit header. */
4332 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4333
98bfdba5
PA
4334 /* Complete the cu_header. */
4335 cu->header.offset = offset;
4336 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4337
98bfdba5
PA
4338 /* Read the abbrevs for this compilation unit. */
4339 dwarf2_read_abbrevs (abfd, cu);
4340 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4341
98bfdba5
PA
4342 /* Link this compilation unit into the compilation unit tree. */
4343 per_cu->cu = cu;
4344 cu->per_cu = per_cu;
98bfdba5
PA
4345
4346 /* Link this CU into read_in_chain. */
4347 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4348 dwarf2_per_objfile->read_in_chain = per_cu;
4349 }
4350 else
4351 {
4352 cu = per_cu->cu;
4353 info_ptr += cu->header.first_die_offset;
4354 }
e142c38c 4355
93311388 4356 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4357
4358 /* We try not to read any attributes in this function, because not
4359 all objfiles needed for references have been loaded yet, and symbol
4360 table processing isn't initialized. But we have to set the CU language,
4361 or we won't be able to build types correctly. */
9816fde3 4362 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4363
a6c727b2
DJ
4364 /* Similarly, if we do not read the producer, we can not apply
4365 producer-specific interpretation. */
4366 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4367 if (attr)
4368 cu->producer = DW_STRING (attr);
4369
98bfdba5
PA
4370 if (read_cu)
4371 {
4372 do_cleanups (free_abbrevs_cleanup);
e142c38c 4373
98bfdba5
PA
4374 /* We've successfully allocated this compilation unit. Let our
4375 caller clean it up when finished with it. */
4376 discard_cleanups (free_cu_cleanup);
4377 }
10b3939b
DJ
4378}
4379
3da10d80
KS
4380/* Add a DIE to the delayed physname list. */
4381
4382static void
4383add_to_method_list (struct type *type, int fnfield_index, int index,
4384 const char *name, struct die_info *die,
4385 struct dwarf2_cu *cu)
4386{
4387 struct delayed_method_info mi;
4388 mi.type = type;
4389 mi.fnfield_index = fnfield_index;
4390 mi.index = index;
4391 mi.name = name;
4392 mi.die = die;
4393 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4394}
4395
4396/* A cleanup for freeing the delayed method list. */
4397
4398static void
4399free_delayed_list (void *ptr)
4400{
4401 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4402 if (cu->method_list != NULL)
4403 {
4404 VEC_free (delayed_method_info, cu->method_list);
4405 cu->method_list = NULL;
4406 }
4407}
4408
4409/* Compute the physnames of any methods on the CU's method list.
4410
4411 The computation of method physnames is delayed in order to avoid the
4412 (bad) condition that one of the method's formal parameters is of an as yet
4413 incomplete type. */
4414
4415static void
4416compute_delayed_physnames (struct dwarf2_cu *cu)
4417{
4418 int i;
4419 struct delayed_method_info *mi;
4420 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4421 {
4422 char *physname;
4423 struct fn_fieldlist *fn_flp
4424 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4425 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4426 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4427 }
4428}
4429
10b3939b
DJ
4430/* Generate full symbol information for PST and CU, whose DIEs have
4431 already been loaded into memory. */
4432
4433static void
4434process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4435{
10b3939b 4436 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4437 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4438 CORE_ADDR lowpc, highpc;
4439 struct symtab *symtab;
3da10d80 4440 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4441 CORE_ADDR baseaddr;
4442
4443 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4444
10b3939b
DJ
4445 buildsym_init ();
4446 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4447 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4448
4449 cu->list_in_scope = &file_symbols;
c906108c 4450
d85a05f0 4451 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4452
c906108c 4453 /* Do line number decoding in read_file_scope () */
10b3939b 4454 process_die (cu->dies, cu);
c906108c 4455
3da10d80
KS
4456 /* Now that we have processed all the DIEs in the CU, all the types
4457 should be complete, and it should now be safe to compute all of the
4458 physnames. */
4459 compute_delayed_physnames (cu);
4460 do_cleanups (delayed_list_cleanup);
4461
fae299cd
DC
4462 /* Some compilers don't define a DW_AT_high_pc attribute for the
4463 compilation unit. If the DW_AT_high_pc is missing, synthesize
4464 it, by scanning the DIE's below the compilation unit. */
10b3939b 4465 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4466
613e1657 4467 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4468
4469 /* Set symtab language to language from DW_AT_language.
4470 If the compilation is from a C file generated by language preprocessors,
4471 do not set the language if it was already deduced by start_subfile. */
4472 if (symtab != NULL
10b3939b 4473 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4474 {
10b3939b 4475 symtab->language = cu->language;
c906108c 4476 }
9291a0cd
TT
4477
4478 if (dwarf2_per_objfile->using_index)
4479 per_cu->v.quick->symtab = symtab;
4480 else
4481 {
4482 struct partial_symtab *pst = per_cu->v.psymtab;
4483 pst->symtab = symtab;
4484 pst->readin = 1;
4485 }
c906108c
SS
4486
4487 do_cleanups (back_to);
4488}
4489
4490/* Process a die and its children. */
4491
4492static void
e7c27a73 4493process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4494{
4495 switch (die->tag)
4496 {
4497 case DW_TAG_padding:
4498 break;
4499 case DW_TAG_compile_unit:
e7c27a73 4500 read_file_scope (die, cu);
c906108c 4501 break;
348e048f
DE
4502 case DW_TAG_type_unit:
4503 read_type_unit_scope (die, cu);
4504 break;
c906108c 4505 case DW_TAG_subprogram:
c906108c 4506 case DW_TAG_inlined_subroutine:
edb3359d 4507 read_func_scope (die, cu);
c906108c
SS
4508 break;
4509 case DW_TAG_lexical_block:
14898363
L
4510 case DW_TAG_try_block:
4511 case DW_TAG_catch_block:
e7c27a73 4512 read_lexical_block_scope (die, cu);
c906108c
SS
4513 break;
4514 case DW_TAG_class_type:
680b30c7 4515 case DW_TAG_interface_type:
c906108c
SS
4516 case DW_TAG_structure_type:
4517 case DW_TAG_union_type:
134d01f1 4518 process_structure_scope (die, cu);
c906108c
SS
4519 break;
4520 case DW_TAG_enumeration_type:
134d01f1 4521 process_enumeration_scope (die, cu);
c906108c 4522 break;
134d01f1 4523
f792889a
DJ
4524 /* These dies have a type, but processing them does not create
4525 a symbol or recurse to process the children. Therefore we can
4526 read them on-demand through read_type_die. */
c906108c 4527 case DW_TAG_subroutine_type:
72019c9c 4528 case DW_TAG_set_type:
c906108c 4529 case DW_TAG_array_type:
c906108c 4530 case DW_TAG_pointer_type:
c906108c 4531 case DW_TAG_ptr_to_member_type:
c906108c 4532 case DW_TAG_reference_type:
c906108c 4533 case DW_TAG_string_type:
c906108c 4534 break;
134d01f1 4535
c906108c 4536 case DW_TAG_base_type:
a02abb62 4537 case DW_TAG_subrange_type:
cb249c71 4538 case DW_TAG_typedef:
134d01f1
DJ
4539 /* Add a typedef symbol for the type definition, if it has a
4540 DW_AT_name. */
f792889a 4541 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4542 break;
c906108c 4543 case DW_TAG_common_block:
e7c27a73 4544 read_common_block (die, cu);
c906108c
SS
4545 break;
4546 case DW_TAG_common_inclusion:
4547 break;
d9fa45fe 4548 case DW_TAG_namespace:
63d06c5c 4549 processing_has_namespace_info = 1;
e7c27a73 4550 read_namespace (die, cu);
d9fa45fe 4551 break;
5d7cb8df 4552 case DW_TAG_module:
f55ee35c 4553 processing_has_namespace_info = 1;
5d7cb8df
JK
4554 read_module (die, cu);
4555 break;
d9fa45fe
DC
4556 case DW_TAG_imported_declaration:
4557 case DW_TAG_imported_module:
63d06c5c 4558 processing_has_namespace_info = 1;
27aa8d6a
SW
4559 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4560 || cu->language != language_fortran))
4561 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4562 dwarf_tag_name (die->tag));
4563 read_import_statement (die, cu);
d9fa45fe 4564 break;
c906108c 4565 default:
e7c27a73 4566 new_symbol (die, NULL, cu);
c906108c
SS
4567 break;
4568 }
4569}
4570
94af9270
KS
4571/* A helper function for dwarf2_compute_name which determines whether DIE
4572 needs to have the name of the scope prepended to the name listed in the
4573 die. */
4574
4575static int
4576die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4577{
1c809c68
TT
4578 struct attribute *attr;
4579
94af9270
KS
4580 switch (die->tag)
4581 {
4582 case DW_TAG_namespace:
4583 case DW_TAG_typedef:
4584 case DW_TAG_class_type:
4585 case DW_TAG_interface_type:
4586 case DW_TAG_structure_type:
4587 case DW_TAG_union_type:
4588 case DW_TAG_enumeration_type:
4589 case DW_TAG_enumerator:
4590 case DW_TAG_subprogram:
4591 case DW_TAG_member:
4592 return 1;
4593
4594 case DW_TAG_variable:
c2b0a229 4595 case DW_TAG_constant:
94af9270
KS
4596 /* We only need to prefix "globally" visible variables. These include
4597 any variable marked with DW_AT_external or any variable that
4598 lives in a namespace. [Variables in anonymous namespaces
4599 require prefixing, but they are not DW_AT_external.] */
4600
4601 if (dwarf2_attr (die, DW_AT_specification, cu))
4602 {
4603 struct dwarf2_cu *spec_cu = cu;
9a619af0 4604
94af9270
KS
4605 return die_needs_namespace (die_specification (die, &spec_cu),
4606 spec_cu);
4607 }
4608
1c809c68 4609 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4610 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4611 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4612 return 0;
4613 /* A variable in a lexical block of some kind does not need a
4614 namespace, even though in C++ such variables may be external
4615 and have a mangled name. */
4616 if (die->parent->tag == DW_TAG_lexical_block
4617 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4618 || die->parent->tag == DW_TAG_catch_block
4619 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4620 return 0;
4621 return 1;
94af9270
KS
4622
4623 default:
4624 return 0;
4625 }
4626}
4627
98bfdba5
PA
4628/* Retrieve the last character from a mem_file. */
4629
4630static void
4631do_ui_file_peek_last (void *object, const char *buffer, long length)
4632{
4633 char *last_char_p = (char *) object;
4634
4635 if (length > 0)
4636 *last_char_p = buffer[length - 1];
4637}
4638
94af9270
KS
4639/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4640 compute the physname for the object, which include a method's
4641 formal parameters (C++/Java) and return type (Java).
4642
af6b7be1
JB
4643 For Ada, return the DIE's linkage name rather than the fully qualified
4644 name. PHYSNAME is ignored..
4645
94af9270
KS
4646 The result is allocated on the objfile_obstack and canonicalized. */
4647
4648static const char *
4649dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4650 int physname)
4651{
4652 if (name == NULL)
4653 name = dwarf2_name (die, cu);
4654
f55ee35c
JK
4655 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4656 compute it by typename_concat inside GDB. */
4657 if (cu->language == language_ada
4658 || (cu->language == language_fortran && physname))
4659 {
4660 /* For Ada unit, we prefer the linkage name over the name, as
4661 the former contains the exported name, which the user expects
4662 to be able to reference. Ideally, we want the user to be able
4663 to reference this entity using either natural or linkage name,
4664 but we haven't started looking at this enhancement yet. */
4665 struct attribute *attr;
4666
4667 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4668 if (attr == NULL)
4669 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4670 if (attr && DW_STRING (attr))
4671 return DW_STRING (attr);
4672 }
4673
94af9270
KS
4674 /* These are the only languages we know how to qualify names in. */
4675 if (name != NULL
f55ee35c
JK
4676 && (cu->language == language_cplus || cu->language == language_java
4677 || cu->language == language_fortran))
94af9270
KS
4678 {
4679 if (die_needs_namespace (die, cu))
4680 {
4681 long length;
4682 char *prefix;
4683 struct ui_file *buf;
4684
4685 prefix = determine_prefix (die, cu);
4686 buf = mem_fileopen ();
4687 if (*prefix != '\0')
4688 {
f55ee35c
JK
4689 char *prefixed_name = typename_concat (NULL, prefix, name,
4690 physname, cu);
9a619af0 4691
94af9270
KS
4692 fputs_unfiltered (prefixed_name, buf);
4693 xfree (prefixed_name);
4694 }
4695 else
4696 fputs_unfiltered (name ? name : "", buf);
4697
98bfdba5
PA
4698 /* Template parameters may be specified in the DIE's DW_AT_name, or
4699 as children with DW_TAG_template_type_param or
4700 DW_TAG_value_type_param. If the latter, add them to the name
4701 here. If the name already has template parameters, then
4702 skip this step; some versions of GCC emit both, and
4703 it is more efficient to use the pre-computed name.
4704
4705 Something to keep in mind about this process: it is very
4706 unlikely, or in some cases downright impossible, to produce
4707 something that will match the mangled name of a function.
4708 If the definition of the function has the same debug info,
4709 we should be able to match up with it anyway. But fallbacks
4710 using the minimal symbol, for instance to find a method
4711 implemented in a stripped copy of libstdc++, will not work.
4712 If we do not have debug info for the definition, we will have to
4713 match them up some other way.
4714
4715 When we do name matching there is a related problem with function
4716 templates; two instantiated function templates are allowed to
4717 differ only by their return types, which we do not add here. */
4718
4719 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4720 {
4721 struct attribute *attr;
4722 struct die_info *child;
4723 int first = 1;
4724
4725 die->building_fullname = 1;
4726
4727 for (child = die->child; child != NULL; child = child->sibling)
4728 {
4729 struct type *type;
4730 long value;
4731 gdb_byte *bytes;
4732 struct dwarf2_locexpr_baton *baton;
4733 struct value *v;
4734
4735 if (child->tag != DW_TAG_template_type_param
4736 && child->tag != DW_TAG_template_value_param)
4737 continue;
4738
4739 if (first)
4740 {
4741 fputs_unfiltered ("<", buf);
4742 first = 0;
4743 }
4744 else
4745 fputs_unfiltered (", ", buf);
4746
4747 attr = dwarf2_attr (child, DW_AT_type, cu);
4748 if (attr == NULL)
4749 {
4750 complaint (&symfile_complaints,
4751 _("template parameter missing DW_AT_type"));
4752 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4753 continue;
4754 }
4755 type = die_type (child, cu);
4756
4757 if (child->tag == DW_TAG_template_type_param)
4758 {
4759 c_print_type (type, "", buf, -1, 0);
4760 continue;
4761 }
4762
4763 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4764 if (attr == NULL)
4765 {
4766 complaint (&symfile_complaints,
4767 _("template parameter missing DW_AT_const_value"));
4768 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4769 continue;
4770 }
4771
4772 dwarf2_const_value_attr (attr, type, name,
4773 &cu->comp_unit_obstack, cu,
4774 &value, &bytes, &baton);
4775
4776 if (TYPE_NOSIGN (type))
4777 /* GDB prints characters as NUMBER 'CHAR'. If that's
4778 changed, this can use value_print instead. */
4779 c_printchar (value, type, buf);
4780 else
4781 {
4782 struct value_print_options opts;
4783
4784 if (baton != NULL)
4785 v = dwarf2_evaluate_loc_desc (type, NULL,
4786 baton->data,
4787 baton->size,
4788 baton->per_cu);
4789 else if (bytes != NULL)
4790 {
4791 v = allocate_value (type);
4792 memcpy (value_contents_writeable (v), bytes,
4793 TYPE_LENGTH (type));
4794 }
4795 else
4796 v = value_from_longest (type, value);
4797
4798 /* Specify decimal so that we do not depend on the radix. */
4799 get_formatted_print_options (&opts, 'd');
4800 opts.raw = 1;
4801 value_print (v, buf, &opts);
4802 release_value (v);
4803 value_free (v);
4804 }
4805 }
4806
4807 die->building_fullname = 0;
4808
4809 if (!first)
4810 {
4811 /* Close the argument list, with a space if necessary
4812 (nested templates). */
4813 char last_char = '\0';
4814 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4815 if (last_char == '>')
4816 fputs_unfiltered (" >", buf);
4817 else
4818 fputs_unfiltered (">", buf);
4819 }
4820 }
4821
94af9270
KS
4822 /* For Java and C++ methods, append formal parameter type
4823 information, if PHYSNAME. */
6e70227d 4824
94af9270
KS
4825 if (physname && die->tag == DW_TAG_subprogram
4826 && (cu->language == language_cplus
4827 || cu->language == language_java))
4828 {
4829 struct type *type = read_type_die (die, cu);
4830
4831 c_type_print_args (type, buf, 0, cu->language);
4832
4833 if (cu->language == language_java)
4834 {
4835 /* For java, we must append the return type to method
4836 names. */
4837 if (die->tag == DW_TAG_subprogram)
4838 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4839 0, 0);
4840 }
4841 else if (cu->language == language_cplus)
4842 {
60430eff
DJ
4843 /* Assume that an artificial first parameter is
4844 "this", but do not crash if it is not. RealView
4845 marks unnamed (and thus unused) parameters as
4846 artificial; there is no way to differentiate
4847 the two cases. */
94af9270
KS
4848 if (TYPE_NFIELDS (type) > 0
4849 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4850 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4851 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4852 fputs_unfiltered (" const", buf);
4853 }
4854 }
4855
4856 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4857 &length);
4858 ui_file_delete (buf);
4859
4860 if (cu->language == language_cplus)
4861 {
4862 char *cname
4863 = dwarf2_canonicalize_name (name, cu,
4864 &cu->objfile->objfile_obstack);
9a619af0 4865
94af9270
KS
4866 if (cname != NULL)
4867 name = cname;
4868 }
4869 }
4870 }
4871
4872 return name;
4873}
4874
0114d602
DJ
4875/* Return the fully qualified name of DIE, based on its DW_AT_name.
4876 If scope qualifiers are appropriate they will be added. The result
4877 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4878 not have a name. NAME may either be from a previous call to
4879 dwarf2_name or NULL.
4880
4881 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4882
4883static const char *
94af9270 4884dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4885{
94af9270
KS
4886 return dwarf2_compute_name (name, die, cu, 0);
4887}
0114d602 4888
94af9270
KS
4889/* Construct a physname for the given DIE in CU. NAME may either be
4890 from a previous call to dwarf2_name or NULL. The result will be
4891 allocated on the objfile_objstack or NULL if the DIE does not have a
4892 name.
0114d602 4893
94af9270 4894 The output string will be canonicalized (if C++/Java). */
0114d602 4895
94af9270
KS
4896static const char *
4897dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4898{
4899 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4900}
4901
27aa8d6a
SW
4902/* Read the import statement specified by the given die and record it. */
4903
4904static void
4905read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4906{
4907 struct attribute *import_attr;
4908 struct die_info *imported_die;
de4affc9 4909 struct dwarf2_cu *imported_cu;
27aa8d6a 4910 const char *imported_name;
794684b6 4911 const char *imported_name_prefix;
13387711
SW
4912 const char *canonical_name;
4913 const char *import_alias;
4914 const char *imported_declaration = NULL;
794684b6 4915 const char *import_prefix;
13387711
SW
4916
4917 char *temp;
27aa8d6a
SW
4918
4919 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4920 if (import_attr == NULL)
4921 {
4922 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4923 dwarf_tag_name (die->tag));
4924 return;
4925 }
4926
de4affc9
CC
4927 imported_cu = cu;
4928 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4929 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4930 if (imported_name == NULL)
4931 {
4932 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4933
4934 The import in the following code:
4935 namespace A
4936 {
4937 typedef int B;
4938 }
4939
4940 int main ()
4941 {
4942 using A::B;
4943 B b;
4944 return b;
4945 }
4946
4947 ...
4948 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4949 <52> DW_AT_decl_file : 1
4950 <53> DW_AT_decl_line : 6
4951 <54> DW_AT_import : <0x75>
4952 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4953 <59> DW_AT_name : B
4954 <5b> DW_AT_decl_file : 1
4955 <5c> DW_AT_decl_line : 2
4956 <5d> DW_AT_type : <0x6e>
4957 ...
4958 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4959 <76> DW_AT_byte_size : 4
4960 <77> DW_AT_encoding : 5 (signed)
4961
4962 imports the wrong die ( 0x75 instead of 0x58 ).
4963 This case will be ignored until the gcc bug is fixed. */
4964 return;
4965 }
4966
82856980
SW
4967 /* Figure out the local name after import. */
4968 import_alias = dwarf2_name (die, cu);
27aa8d6a 4969
794684b6
SW
4970 /* Figure out where the statement is being imported to. */
4971 import_prefix = determine_prefix (die, cu);
4972
4973 /* Figure out what the scope of the imported die is and prepend it
4974 to the name of the imported die. */
de4affc9 4975 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4976
f55ee35c
JK
4977 if (imported_die->tag != DW_TAG_namespace
4978 && imported_die->tag != DW_TAG_module)
794684b6 4979 {
13387711
SW
4980 imported_declaration = imported_name;
4981 canonical_name = imported_name_prefix;
794684b6 4982 }
13387711 4983 else if (strlen (imported_name_prefix) > 0)
794684b6 4984 {
13387711
SW
4985 temp = alloca (strlen (imported_name_prefix)
4986 + 2 + strlen (imported_name) + 1);
4987 strcpy (temp, imported_name_prefix);
4988 strcat (temp, "::");
4989 strcat (temp, imported_name);
4990 canonical_name = temp;
794684b6 4991 }
13387711
SW
4992 else
4993 canonical_name = imported_name;
794684b6 4994
c0cc3a76
SW
4995 cp_add_using_directive (import_prefix,
4996 canonical_name,
4997 import_alias,
13387711 4998 imported_declaration,
c0cc3a76 4999 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5000}
5001
5fb290d7 5002static void
e142c38c 5003initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5004{
e142c38c 5005 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5006}
5007
ae2de4f8
DE
5008/* Cleanup function for read_file_scope. */
5009
cb1df416
DJ
5010static void
5011free_cu_line_header (void *arg)
5012{
5013 struct dwarf2_cu *cu = arg;
5014
5015 free_line_header (cu->line_header);
5016 cu->line_header = NULL;
5017}
5018
9291a0cd
TT
5019static void
5020find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5021 char **name, char **comp_dir)
5022{
5023 struct attribute *attr;
5024
5025 *name = NULL;
5026 *comp_dir = NULL;
5027
5028 /* Find the filename. Do not use dwarf2_name here, since the filename
5029 is not a source language identifier. */
5030 attr = dwarf2_attr (die, DW_AT_name, cu);
5031 if (attr)
5032 {
5033 *name = DW_STRING (attr);
5034 }
5035
5036 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5037 if (attr)
5038 *comp_dir = DW_STRING (attr);
5039 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5040 {
5041 *comp_dir = ldirname (*name);
5042 if (*comp_dir != NULL)
5043 make_cleanup (xfree, *comp_dir);
5044 }
5045 if (*comp_dir != NULL)
5046 {
5047 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5048 directory, get rid of it. */
5049 char *cp = strchr (*comp_dir, ':');
5050
5051 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5052 *comp_dir = cp + 1;
5053 }
5054
5055 if (*name == NULL)
5056 *name = "<unknown>";
5057}
5058
ae2de4f8
DE
5059/* Process DW_TAG_compile_unit. */
5060
c906108c 5061static void
e7c27a73 5062read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5063{
e7c27a73 5064 struct objfile *objfile = cu->objfile;
debd256d 5065 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5066 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5067 CORE_ADDR highpc = ((CORE_ADDR) 0);
5068 struct attribute *attr;
e1024ff1 5069 char *name = NULL;
c906108c
SS
5070 char *comp_dir = NULL;
5071 struct die_info *child_die;
5072 bfd *abfd = objfile->obfd;
debd256d 5073 struct line_header *line_header = 0;
e142c38c 5074 CORE_ADDR baseaddr;
6e70227d 5075
e142c38c 5076 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5077
fae299cd 5078 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5079
5080 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5081 from finish_block. */
2acceee2 5082 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5083 lowpc = highpc;
5084 lowpc += baseaddr;
5085 highpc += baseaddr;
5086
9291a0cd 5087 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5088
e142c38c 5089 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5090 if (attr)
5091 {
e142c38c 5092 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5093 }
5094
b0f35d58 5095 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5096 if (attr)
b0f35d58 5097 cu->producer = DW_STRING (attr);
303b6f5d 5098
f4b8a18d
KW
5099 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5100 standardised yet. As a workaround for the language detection we fall
5101 back to the DW_AT_producer string. */
5102 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5103 cu->language = language_opencl;
5104
c906108c
SS
5105 /* We assume that we're processing GCC output. */
5106 processing_gcc_compilation = 2;
c906108c 5107
df8a16a1
DJ
5108 processing_has_namespace_info = 0;
5109
c906108c
SS
5110 start_symtab (name, comp_dir, lowpc);
5111 record_debugformat ("DWARF 2");
303b6f5d 5112 record_producer (cu->producer);
c906108c 5113
e142c38c 5114 initialize_cu_func_list (cu);
c906108c 5115
cb1df416
DJ
5116 /* Decode line number information if present. We do this before
5117 processing child DIEs, so that the line header table is available
5118 for DW_AT_decl_file. */
e142c38c 5119 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5120 if (attr)
5121 {
debd256d 5122 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5123 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5124 if (line_header)
5125 {
cb1df416
DJ
5126 cu->line_header = line_header;
5127 make_cleanup (free_cu_line_header, cu);
aaa75496 5128 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5129 }
5fb290d7 5130 }
debd256d 5131
cb1df416
DJ
5132 /* Process all dies in compilation unit. */
5133 if (die->child != NULL)
5134 {
5135 child_die = die->child;
5136 while (child_die && child_die->tag)
5137 {
5138 process_die (child_die, cu);
5139 child_die = sibling_die (child_die);
5140 }
5141 }
5142
2e276125
JB
5143 /* Decode macro information, if present. Dwarf 2 macro information
5144 refers to information in the line number info statement program
5145 header, so we can only read it if we've read the header
5146 successfully. */
e142c38c 5147 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5148 if (attr && line_header)
2e276125
JB
5149 {
5150 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5151
2e276125 5152 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5153 comp_dir, abfd, cu);
2e276125 5154 }
debd256d 5155 do_cleanups (back_to);
5fb290d7
DJ
5156}
5157
ae2de4f8
DE
5158/* Process DW_TAG_type_unit.
5159 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5160 actual type being defined by this TU. In this case the first top
5161 level sibling is there to provide context only. */
5162
5163static void
5164read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5165{
5166 struct objfile *objfile = cu->objfile;
5167 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5168 CORE_ADDR lowpc;
5169 struct attribute *attr;
5170 char *name = NULL;
5171 char *comp_dir = NULL;
5172 struct die_info *child_die;
5173 bfd *abfd = objfile->obfd;
348e048f
DE
5174
5175 /* start_symtab needs a low pc, but we don't really have one.
5176 Do what read_file_scope would do in the absence of such info. */
5177 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5178
5179 /* Find the filename. Do not use dwarf2_name here, since the filename
5180 is not a source language identifier. */
5181 attr = dwarf2_attr (die, DW_AT_name, cu);
5182 if (attr)
5183 name = DW_STRING (attr);
5184
5185 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5186 if (attr)
5187 comp_dir = DW_STRING (attr);
5188 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5189 {
5190 comp_dir = ldirname (name);
5191 if (comp_dir != NULL)
5192 make_cleanup (xfree, comp_dir);
5193 }
5194
5195 if (name == NULL)
5196 name = "<unknown>";
5197
5198 attr = dwarf2_attr (die, DW_AT_language, cu);
5199 if (attr)
5200 set_cu_language (DW_UNSND (attr), cu);
5201
5202 /* This isn't technically needed today. It is done for symmetry
5203 with read_file_scope. */
5204 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5205 if (attr)
348e048f
DE
5206 cu->producer = DW_STRING (attr);
5207
5208 /* We assume that we're processing GCC output. */
5209 processing_gcc_compilation = 2;
5210
5211 processing_has_namespace_info = 0;
5212
5213 start_symtab (name, comp_dir, lowpc);
5214 record_debugformat ("DWARF 2");
5215 record_producer (cu->producer);
5216
5217 /* Process the dies in the type unit. */
5218 if (die->child == NULL)
5219 {
5220 dump_die_for_error (die);
5221 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5222 bfd_get_filename (abfd));
5223 }
5224
5225 child_die = die->child;
5226
5227 while (child_die && child_die->tag)
5228 {
5229 process_die (child_die, cu);
5230
5231 child_die = sibling_die (child_die);
5232 }
5233
5234 do_cleanups (back_to);
5235}
5236
5fb290d7 5237static void
e142c38c
DJ
5238add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5239 struct dwarf2_cu *cu)
5fb290d7
DJ
5240{
5241 struct function_range *thisfn;
5242
5243 thisfn = (struct function_range *)
7b5a2f43 5244 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5245 thisfn->name = name;
5246 thisfn->lowpc = lowpc;
5247 thisfn->highpc = highpc;
5248 thisfn->seen_line = 0;
5249 thisfn->next = NULL;
5250
e142c38c
DJ
5251 if (cu->last_fn == NULL)
5252 cu->first_fn = thisfn;
5fb290d7 5253 else
e142c38c 5254 cu->last_fn->next = thisfn;
5fb290d7 5255
e142c38c 5256 cu->last_fn = thisfn;
c906108c
SS
5257}
5258
d389af10
JK
5259/* qsort helper for inherit_abstract_dies. */
5260
5261static int
5262unsigned_int_compar (const void *ap, const void *bp)
5263{
5264 unsigned int a = *(unsigned int *) ap;
5265 unsigned int b = *(unsigned int *) bp;
5266
5267 return (a > b) - (b > a);
5268}
5269
5270/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5271 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5272 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5273
5274static void
5275inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5276{
5277 struct die_info *child_die;
5278 unsigned die_children_count;
5279 /* CU offsets which were referenced by children of the current DIE. */
5280 unsigned *offsets;
5281 unsigned *offsets_end, *offsetp;
5282 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5283 struct die_info *origin_die;
5284 /* Iterator of the ORIGIN_DIE children. */
5285 struct die_info *origin_child_die;
5286 struct cleanup *cleanups;
5287 struct attribute *attr;
cd02d79d
PA
5288 struct dwarf2_cu *origin_cu;
5289 struct pending **origin_previous_list_in_scope;
d389af10
JK
5290
5291 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5292 if (!attr)
5293 return;
5294
cd02d79d
PA
5295 /* Note that following die references may follow to a die in a
5296 different cu. */
5297
5298 origin_cu = cu;
5299 origin_die = follow_die_ref (die, attr, &origin_cu);
5300
5301 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5302 symbols in. */
5303 origin_previous_list_in_scope = origin_cu->list_in_scope;
5304 origin_cu->list_in_scope = cu->list_in_scope;
5305
edb3359d
DJ
5306 if (die->tag != origin_die->tag
5307 && !(die->tag == DW_TAG_inlined_subroutine
5308 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5309 complaint (&symfile_complaints,
5310 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5311 die->offset, origin_die->offset);
5312
5313 child_die = die->child;
5314 die_children_count = 0;
5315 while (child_die && child_die->tag)
5316 {
5317 child_die = sibling_die (child_die);
5318 die_children_count++;
5319 }
5320 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5321 cleanups = make_cleanup (xfree, offsets);
5322
5323 offsets_end = offsets;
5324 child_die = die->child;
5325 while (child_die && child_die->tag)
5326 {
c38f313d
DJ
5327 /* For each CHILD_DIE, find the corresponding child of
5328 ORIGIN_DIE. If there is more than one layer of
5329 DW_AT_abstract_origin, follow them all; there shouldn't be,
5330 but GCC versions at least through 4.4 generate this (GCC PR
5331 40573). */
5332 struct die_info *child_origin_die = child_die;
cd02d79d 5333 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5334
c38f313d
DJ
5335 while (1)
5336 {
cd02d79d
PA
5337 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5338 child_origin_cu);
c38f313d
DJ
5339 if (attr == NULL)
5340 break;
cd02d79d
PA
5341 child_origin_die = follow_die_ref (child_origin_die, attr,
5342 &child_origin_cu);
c38f313d
DJ
5343 }
5344
d389af10
JK
5345 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5346 counterpart may exist. */
c38f313d 5347 if (child_origin_die != child_die)
d389af10 5348 {
edb3359d
DJ
5349 if (child_die->tag != child_origin_die->tag
5350 && !(child_die->tag == DW_TAG_inlined_subroutine
5351 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5352 complaint (&symfile_complaints,
5353 _("Child DIE 0x%x and its abstract origin 0x%x have "
5354 "different tags"), child_die->offset,
5355 child_origin_die->offset);
c38f313d
DJ
5356 if (child_origin_die->parent != origin_die)
5357 complaint (&symfile_complaints,
5358 _("Child DIE 0x%x and its abstract origin 0x%x have "
5359 "different parents"), child_die->offset,
5360 child_origin_die->offset);
5361 else
5362 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5363 }
5364 child_die = sibling_die (child_die);
5365 }
5366 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5367 unsigned_int_compar);
5368 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5369 if (offsetp[-1] == *offsetp)
5370 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5371 "to DIE 0x%x as their abstract origin"),
5372 die->offset, *offsetp);
5373
5374 offsetp = offsets;
5375 origin_child_die = origin_die->child;
5376 while (origin_child_die && origin_child_die->tag)
5377 {
5378 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5379 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5380 offsetp++;
5381 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5382 {
5383 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5384 process_die (origin_child_die, origin_cu);
d389af10
JK
5385 }
5386 origin_child_die = sibling_die (origin_child_die);
5387 }
cd02d79d 5388 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5389
5390 do_cleanups (cleanups);
5391}
5392
c906108c 5393static void
e7c27a73 5394read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5395{
e7c27a73 5396 struct objfile *objfile = cu->objfile;
52f0bd74 5397 struct context_stack *new;
c906108c
SS
5398 CORE_ADDR lowpc;
5399 CORE_ADDR highpc;
5400 struct die_info *child_die;
edb3359d 5401 struct attribute *attr, *call_line, *call_file;
c906108c 5402 char *name;
e142c38c 5403 CORE_ADDR baseaddr;
801e3a5b 5404 struct block *block;
edb3359d 5405 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5406 VEC (symbolp) *template_args = NULL;
5407 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5408
5409 if (inlined_func)
5410 {
5411 /* If we do not have call site information, we can't show the
5412 caller of this inlined function. That's too confusing, so
5413 only use the scope for local variables. */
5414 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5415 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5416 if (call_line == NULL || call_file == NULL)
5417 {
5418 read_lexical_block_scope (die, cu);
5419 return;
5420 }
5421 }
c906108c 5422
e142c38c
DJ
5423 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5424
94af9270 5425 name = dwarf2_name (die, cu);
c906108c 5426
e8d05480
JB
5427 /* Ignore functions with missing or empty names. These are actually
5428 illegal according to the DWARF standard. */
5429 if (name == NULL)
5430 {
5431 complaint (&symfile_complaints,
5432 _("missing name for subprogram DIE at %d"), die->offset);
5433 return;
5434 }
5435
5436 /* Ignore functions with missing or invalid low and high pc attributes. */
5437 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5438 {
ae4d0c03
PM
5439 attr = dwarf2_attr (die, DW_AT_external, cu);
5440 if (!attr || !DW_UNSND (attr))
5441 complaint (&symfile_complaints,
5442 _("cannot get low and high bounds for subprogram DIE at %d"),
5443 die->offset);
e8d05480
JB
5444 return;
5445 }
c906108c
SS
5446
5447 lowpc += baseaddr;
5448 highpc += baseaddr;
5449
5fb290d7 5450 /* Record the function range for dwarf_decode_lines. */
e142c38c 5451 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5452
34eaf542
TT
5453 /* If we have any template arguments, then we must allocate a
5454 different sort of symbol. */
5455 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5456 {
5457 if (child_die->tag == DW_TAG_template_type_param
5458 || child_die->tag == DW_TAG_template_value_param)
5459 {
5460 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5461 struct template_symbol);
5462 templ_func->base.is_cplus_template_function = 1;
5463 break;
5464 }
5465 }
5466
c906108c 5467 new = push_context (0, lowpc);
34eaf542
TT
5468 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5469 (struct symbol *) templ_func);
4c2df51b 5470
4cecd739
DJ
5471 /* If there is a location expression for DW_AT_frame_base, record
5472 it. */
e142c38c 5473 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5474 if (attr)
c034e007
AC
5475 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5476 expression is being recorded directly in the function's symbol
5477 and not in a separate frame-base object. I guess this hack is
5478 to avoid adding some sort of frame-base adjunct/annex to the
5479 function's symbol :-(. The problem with doing this is that it
5480 results in a function symbol with a location expression that
5481 has nothing to do with the location of the function, ouch! The
5482 relationship should be: a function's symbol has-a frame base; a
5483 frame-base has-a location expression. */
e7c27a73 5484 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5485
e142c38c 5486 cu->list_in_scope = &local_symbols;
c906108c 5487
639d11d3 5488 if (die->child != NULL)
c906108c 5489 {
639d11d3 5490 child_die = die->child;
c906108c
SS
5491 while (child_die && child_die->tag)
5492 {
34eaf542
TT
5493 if (child_die->tag == DW_TAG_template_type_param
5494 || child_die->tag == DW_TAG_template_value_param)
5495 {
5496 struct symbol *arg = new_symbol (child_die, NULL, cu);
5497
f1078f66
DJ
5498 if (arg != NULL)
5499 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5500 }
5501 else
5502 process_die (child_die, cu);
c906108c
SS
5503 child_die = sibling_die (child_die);
5504 }
5505 }
5506
d389af10
JK
5507 inherit_abstract_dies (die, cu);
5508
4a811a97
UW
5509 /* If we have a DW_AT_specification, we might need to import using
5510 directives from the context of the specification DIE. See the
5511 comment in determine_prefix. */
5512 if (cu->language == language_cplus
5513 && dwarf2_attr (die, DW_AT_specification, cu))
5514 {
5515 struct dwarf2_cu *spec_cu = cu;
5516 struct die_info *spec_die = die_specification (die, &spec_cu);
5517
5518 while (spec_die)
5519 {
5520 child_die = spec_die->child;
5521 while (child_die && child_die->tag)
5522 {
5523 if (child_die->tag == DW_TAG_imported_module)
5524 process_die (child_die, spec_cu);
5525 child_die = sibling_die (child_die);
5526 }
5527
5528 /* In some cases, GCC generates specification DIEs that
5529 themselves contain DW_AT_specification attributes. */
5530 spec_die = die_specification (spec_die, &spec_cu);
5531 }
5532 }
5533
c906108c
SS
5534 new = pop_context ();
5535 /* Make a block for the local symbols within. */
801e3a5b
JB
5536 block = finish_block (new->name, &local_symbols, new->old_blocks,
5537 lowpc, highpc, objfile);
5538
df8a16a1 5539 /* For C++, set the block's scope. */
f55ee35c 5540 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5541 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5542 determine_prefix (die, cu),
df8a16a1
DJ
5543 processing_has_namespace_info);
5544
801e3a5b
JB
5545 /* If we have address ranges, record them. */
5546 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5547
34eaf542
TT
5548 /* Attach template arguments to function. */
5549 if (! VEC_empty (symbolp, template_args))
5550 {
5551 gdb_assert (templ_func != NULL);
5552
5553 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5554 templ_func->template_arguments
5555 = obstack_alloc (&objfile->objfile_obstack,
5556 (templ_func->n_template_arguments
5557 * sizeof (struct symbol *)));
5558 memcpy (templ_func->template_arguments,
5559 VEC_address (symbolp, template_args),
5560 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5561 VEC_free (symbolp, template_args);
5562 }
5563
208d8187
JB
5564 /* In C++, we can have functions nested inside functions (e.g., when
5565 a function declares a class that has methods). This means that
5566 when we finish processing a function scope, we may need to go
5567 back to building a containing block's symbol lists. */
5568 local_symbols = new->locals;
5569 param_symbols = new->params;
27aa8d6a 5570 using_directives = new->using_directives;
208d8187 5571
921e78cf
JB
5572 /* If we've finished processing a top-level function, subsequent
5573 symbols go in the file symbol list. */
5574 if (outermost_context_p ())
e142c38c 5575 cu->list_in_scope = &file_symbols;
c906108c
SS
5576}
5577
5578/* Process all the DIES contained within a lexical block scope. Start
5579 a new scope, process the dies, and then close the scope. */
5580
5581static void
e7c27a73 5582read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5583{
e7c27a73 5584 struct objfile *objfile = cu->objfile;
52f0bd74 5585 struct context_stack *new;
c906108c
SS
5586 CORE_ADDR lowpc, highpc;
5587 struct die_info *child_die;
e142c38c
DJ
5588 CORE_ADDR baseaddr;
5589
5590 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5591
5592 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5593 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5594 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5595 be nasty. Might be easier to properly extend generic blocks to
af34e669 5596 describe ranges. */
d85a05f0 5597 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5598 return;
5599 lowpc += baseaddr;
5600 highpc += baseaddr;
5601
5602 push_context (0, lowpc);
639d11d3 5603 if (die->child != NULL)
c906108c 5604 {
639d11d3 5605 child_die = die->child;
c906108c
SS
5606 while (child_die && child_die->tag)
5607 {
e7c27a73 5608 process_die (child_die, cu);
c906108c
SS
5609 child_die = sibling_die (child_die);
5610 }
5611 }
5612 new = pop_context ();
5613
8540c487 5614 if (local_symbols != NULL || using_directives != NULL)
c906108c 5615 {
801e3a5b
JB
5616 struct block *block
5617 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5618 highpc, objfile);
5619
5620 /* Note that recording ranges after traversing children, as we
5621 do here, means that recording a parent's ranges entails
5622 walking across all its children's ranges as they appear in
5623 the address map, which is quadratic behavior.
5624
5625 It would be nicer to record the parent's ranges before
5626 traversing its children, simply overriding whatever you find
5627 there. But since we don't even decide whether to create a
5628 block until after we've traversed its children, that's hard
5629 to do. */
5630 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5631 }
5632 local_symbols = new->locals;
27aa8d6a 5633 using_directives = new->using_directives;
c906108c
SS
5634}
5635
43039443 5636/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5637 Return 1 if the attributes are present and valid, otherwise, return 0.
5638 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5639
5640static int
5641dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5642 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5643 struct partial_symtab *ranges_pst)
43039443
JK
5644{
5645 struct objfile *objfile = cu->objfile;
5646 struct comp_unit_head *cu_header = &cu->header;
5647 bfd *obfd = objfile->obfd;
5648 unsigned int addr_size = cu_header->addr_size;
5649 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5650 /* Base address selection entry. */
5651 CORE_ADDR base;
5652 int found_base;
5653 unsigned int dummy;
5654 gdb_byte *buffer;
5655 CORE_ADDR marker;
5656 int low_set;
5657 CORE_ADDR low = 0;
5658 CORE_ADDR high = 0;
ff013f42 5659 CORE_ADDR baseaddr;
43039443 5660
d00adf39
DE
5661 found_base = cu->base_known;
5662 base = cu->base_address;
43039443 5663
be391dca 5664 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5665 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5666 {
5667 complaint (&symfile_complaints,
5668 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5669 offset);
5670 return 0;
5671 }
dce234bc 5672 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5673
5674 /* Read in the largest possible address. */
5675 marker = read_address (obfd, buffer, cu, &dummy);
5676 if ((marker & mask) == mask)
5677 {
5678 /* If we found the largest possible address, then
5679 read the base address. */
5680 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5681 buffer += 2 * addr_size;
5682 offset += 2 * addr_size;
5683 found_base = 1;
5684 }
5685
5686 low_set = 0;
5687
e7030f15 5688 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5689
43039443
JK
5690 while (1)
5691 {
5692 CORE_ADDR range_beginning, range_end;
5693
5694 range_beginning = read_address (obfd, buffer, cu, &dummy);
5695 buffer += addr_size;
5696 range_end = read_address (obfd, buffer, cu, &dummy);
5697 buffer += addr_size;
5698 offset += 2 * addr_size;
5699
5700 /* An end of list marker is a pair of zero addresses. */
5701 if (range_beginning == 0 && range_end == 0)
5702 /* Found the end of list entry. */
5703 break;
5704
5705 /* Each base address selection entry is a pair of 2 values.
5706 The first is the largest possible address, the second is
5707 the base address. Check for a base address here. */
5708 if ((range_beginning & mask) == mask)
5709 {
5710 /* If we found the largest possible address, then
5711 read the base address. */
5712 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5713 found_base = 1;
5714 continue;
5715 }
5716
5717 if (!found_base)
5718 {
5719 /* We have no valid base address for the ranges
5720 data. */
5721 complaint (&symfile_complaints,
5722 _("Invalid .debug_ranges data (no base address)"));
5723 return 0;
5724 }
5725
5726 range_beginning += base;
5727 range_end += base;
5728
ff013f42
JK
5729 if (ranges_pst != NULL && range_beginning < range_end)
5730 addrmap_set_empty (objfile->psymtabs_addrmap,
5731 range_beginning + baseaddr, range_end - 1 + baseaddr,
5732 ranges_pst);
5733
43039443
JK
5734 /* FIXME: This is recording everything as a low-high
5735 segment of consecutive addresses. We should have a
5736 data structure for discontiguous block ranges
5737 instead. */
5738 if (! low_set)
5739 {
5740 low = range_beginning;
5741 high = range_end;
5742 low_set = 1;
5743 }
5744 else
5745 {
5746 if (range_beginning < low)
5747 low = range_beginning;
5748 if (range_end > high)
5749 high = range_end;
5750 }
5751 }
5752
5753 if (! low_set)
5754 /* If the first entry is an end-of-list marker, the range
5755 describes an empty scope, i.e. no instructions. */
5756 return 0;
5757
5758 if (low_return)
5759 *low_return = low;
5760 if (high_return)
5761 *high_return = high;
5762 return 1;
5763}
5764
af34e669
DJ
5765/* Get low and high pc attributes from a die. Return 1 if the attributes
5766 are present and valid, otherwise, return 0. Return -1 if the range is
5767 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5768static int
af34e669 5769dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5770 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5771 struct partial_symtab *pst)
c906108c
SS
5772{
5773 struct attribute *attr;
af34e669
DJ
5774 CORE_ADDR low = 0;
5775 CORE_ADDR high = 0;
5776 int ret = 0;
c906108c 5777
e142c38c 5778 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5779 if (attr)
af34e669
DJ
5780 {
5781 high = DW_ADDR (attr);
e142c38c 5782 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5783 if (attr)
5784 low = DW_ADDR (attr);
5785 else
5786 /* Found high w/o low attribute. */
5787 return 0;
5788
5789 /* Found consecutive range of addresses. */
5790 ret = 1;
5791 }
c906108c 5792 else
af34e669 5793 {
e142c38c 5794 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5795 if (attr != NULL)
5796 {
af34e669 5797 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5798 .debug_ranges section. */
d85a05f0 5799 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5800 return 0;
43039443 5801 /* Found discontinuous range of addresses. */
af34e669
DJ
5802 ret = -1;
5803 }
5804 }
c906108c
SS
5805
5806 if (high < low)
5807 return 0;
5808
5809 /* When using the GNU linker, .gnu.linkonce. sections are used to
5810 eliminate duplicate copies of functions and vtables and such.
5811 The linker will arbitrarily choose one and discard the others.
5812 The AT_*_pc values for such functions refer to local labels in
5813 these sections. If the section from that file was discarded, the
5814 labels are not in the output, so the relocs get a value of 0.
5815 If this is a discarded function, mark the pc bounds as invalid,
5816 so that GDB will ignore it. */
72dca2f5 5817 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5818 return 0;
5819
5820 *lowpc = low;
5821 *highpc = high;
af34e669 5822 return ret;
c906108c
SS
5823}
5824
b084d499
JB
5825/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5826 its low and high PC addresses. Do nothing if these addresses could not
5827 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5828 and HIGHPC to the high address if greater than HIGHPC. */
5829
5830static void
5831dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5832 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5833 struct dwarf2_cu *cu)
5834{
5835 CORE_ADDR low, high;
5836 struct die_info *child = die->child;
5837
d85a05f0 5838 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5839 {
5840 *lowpc = min (*lowpc, low);
5841 *highpc = max (*highpc, high);
5842 }
5843
5844 /* If the language does not allow nested subprograms (either inside
5845 subprograms or lexical blocks), we're done. */
5846 if (cu->language != language_ada)
5847 return;
6e70227d 5848
b084d499
JB
5849 /* Check all the children of the given DIE. If it contains nested
5850 subprograms, then check their pc bounds. Likewise, we need to
5851 check lexical blocks as well, as they may also contain subprogram
5852 definitions. */
5853 while (child && child->tag)
5854 {
5855 if (child->tag == DW_TAG_subprogram
5856 || child->tag == DW_TAG_lexical_block)
5857 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5858 child = sibling_die (child);
5859 }
5860}
5861
fae299cd
DC
5862/* Get the low and high pc's represented by the scope DIE, and store
5863 them in *LOWPC and *HIGHPC. If the correct values can't be
5864 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5865
5866static void
5867get_scope_pc_bounds (struct die_info *die,
5868 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5869 struct dwarf2_cu *cu)
5870{
5871 CORE_ADDR best_low = (CORE_ADDR) -1;
5872 CORE_ADDR best_high = (CORE_ADDR) 0;
5873 CORE_ADDR current_low, current_high;
5874
d85a05f0 5875 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5876 {
5877 best_low = current_low;
5878 best_high = current_high;
5879 }
5880 else
5881 {
5882 struct die_info *child = die->child;
5883
5884 while (child && child->tag)
5885 {
5886 switch (child->tag) {
5887 case DW_TAG_subprogram:
b084d499 5888 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5889 break;
5890 case DW_TAG_namespace:
f55ee35c 5891 case DW_TAG_module:
fae299cd
DC
5892 /* FIXME: carlton/2004-01-16: Should we do this for
5893 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5894 that current GCC's always emit the DIEs corresponding
5895 to definitions of methods of classes as children of a
5896 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5897 the DIEs giving the declarations, which could be
5898 anywhere). But I don't see any reason why the
5899 standards says that they have to be there. */
5900 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5901
5902 if (current_low != ((CORE_ADDR) -1))
5903 {
5904 best_low = min (best_low, current_low);
5905 best_high = max (best_high, current_high);
5906 }
5907 break;
5908 default:
5909 /* Ignore. */
5910 break;
5911 }
5912
5913 child = sibling_die (child);
5914 }
5915 }
5916
5917 *lowpc = best_low;
5918 *highpc = best_high;
5919}
5920
801e3a5b
JB
5921/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5922 in DIE. */
5923static void
5924dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5925 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5926{
5927 struct attribute *attr;
5928
5929 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5930 if (attr)
5931 {
5932 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5933
801e3a5b
JB
5934 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5935 if (attr)
5936 {
5937 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5938
801e3a5b
JB
5939 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5940 }
5941 }
5942
5943 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5944 if (attr)
5945 {
5946 bfd *obfd = cu->objfile->obfd;
5947
5948 /* The value of the DW_AT_ranges attribute is the offset of the
5949 address range list in the .debug_ranges section. */
5950 unsigned long offset = DW_UNSND (attr);
dce234bc 5951 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5952
5953 /* For some target architectures, but not others, the
5954 read_address function sign-extends the addresses it returns.
5955 To recognize base address selection entries, we need a
5956 mask. */
5957 unsigned int addr_size = cu->header.addr_size;
5958 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5959
5960 /* The base address, to which the next pair is relative. Note
5961 that this 'base' is a DWARF concept: most entries in a range
5962 list are relative, to reduce the number of relocs against the
5963 debugging information. This is separate from this function's
5964 'baseaddr' argument, which GDB uses to relocate debugging
5965 information from a shared library based on the address at
5966 which the library was loaded. */
d00adf39
DE
5967 CORE_ADDR base = cu->base_address;
5968 int base_known = cu->base_known;
801e3a5b 5969
be391dca 5970 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5971 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5972 {
5973 complaint (&symfile_complaints,
5974 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5975 offset);
5976 return;
5977 }
5978
5979 for (;;)
5980 {
5981 unsigned int bytes_read;
5982 CORE_ADDR start, end;
5983
5984 start = read_address (obfd, buffer, cu, &bytes_read);
5985 buffer += bytes_read;
5986 end = read_address (obfd, buffer, cu, &bytes_read);
5987 buffer += bytes_read;
5988
5989 /* Did we find the end of the range list? */
5990 if (start == 0 && end == 0)
5991 break;
5992
5993 /* Did we find a base address selection entry? */
5994 else if ((start & base_select_mask) == base_select_mask)
5995 {
5996 base = end;
5997 base_known = 1;
5998 }
5999
6000 /* We found an ordinary address range. */
6001 else
6002 {
6003 if (!base_known)
6004 {
6005 complaint (&symfile_complaints,
6006 _("Invalid .debug_ranges data (no base address)"));
6007 return;
6008 }
6009
6e70227d
DE
6010 record_block_range (block,
6011 baseaddr + base + start,
801e3a5b
JB
6012 baseaddr + base + end - 1);
6013 }
6014 }
6015 }
6016}
6017
c906108c
SS
6018/* Add an aggregate field to the field list. */
6019
6020static void
107d2387 6021dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6022 struct dwarf2_cu *cu)
6e70227d 6023{
e7c27a73 6024 struct objfile *objfile = cu->objfile;
5e2b427d 6025 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6026 struct nextfield *new_field;
6027 struct attribute *attr;
6028 struct field *fp;
6029 char *fieldname = "";
6030
6031 /* Allocate a new field list entry and link it in. */
6032 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6033 make_cleanup (xfree, new_field);
c906108c 6034 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6035
6036 if (die->tag == DW_TAG_inheritance)
6037 {
6038 new_field->next = fip->baseclasses;
6039 fip->baseclasses = new_field;
6040 }
6041 else
6042 {
6043 new_field->next = fip->fields;
6044 fip->fields = new_field;
6045 }
c906108c
SS
6046 fip->nfields++;
6047
6048 /* Handle accessibility and virtuality of field.
6049 The default accessibility for members is public, the default
6050 accessibility for inheritance is private. */
6051 if (die->tag != DW_TAG_inheritance)
6052 new_field->accessibility = DW_ACCESS_public;
6053 else
6054 new_field->accessibility = DW_ACCESS_private;
6055 new_field->virtuality = DW_VIRTUALITY_none;
6056
e142c38c 6057 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6058 if (attr)
6059 new_field->accessibility = DW_UNSND (attr);
6060 if (new_field->accessibility != DW_ACCESS_public)
6061 fip->non_public_fields = 1;
e142c38c 6062 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6063 if (attr)
6064 new_field->virtuality = DW_UNSND (attr);
6065
6066 fp = &new_field->field;
a9a9bd0f 6067
e142c38c 6068 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6069 {
a9a9bd0f 6070 /* Data member other than a C++ static data member. */
6e70227d 6071
c906108c 6072 /* Get type of field. */
e7c27a73 6073 fp->type = die_type (die, cu);
c906108c 6074
d6a843b5 6075 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6076
c906108c 6077 /* Get bit size of field (zero if none). */
e142c38c 6078 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6079 if (attr)
6080 {
6081 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6082 }
6083 else
6084 {
6085 FIELD_BITSIZE (*fp) = 0;
6086 }
6087
6088 /* Get bit offset of field. */
e142c38c 6089 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6090 if (attr)
6091 {
d4b96c9a 6092 int byte_offset = 0;
c6a0999f 6093
3690dd37 6094 if (attr_form_is_section_offset (attr))
d4b96c9a 6095 dwarf2_complex_location_expr_complaint ();
3690dd37 6096 else if (attr_form_is_constant (attr))
c6a0999f 6097 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6098 else if (attr_form_is_block (attr))
c6a0999f 6099 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6100 else
6101 dwarf2_complex_location_expr_complaint ();
c6a0999f 6102
d6a843b5 6103 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6104 }
e142c38c 6105 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6106 if (attr)
6107 {
5e2b427d 6108 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6109 {
6110 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6111 additional bit offset from the MSB of the containing
6112 anonymous object to the MSB of the field. We don't
6113 have to do anything special since we don't need to
6114 know the size of the anonymous object. */
c906108c
SS
6115 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6116 }
6117 else
6118 {
6119 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6120 MSB of the anonymous object, subtract off the number of
6121 bits from the MSB of the field to the MSB of the
6122 object, and then subtract off the number of bits of
6123 the field itself. The result is the bit offset of
6124 the LSB of the field. */
c906108c
SS
6125 int anonymous_size;
6126 int bit_offset = DW_UNSND (attr);
6127
e142c38c 6128 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6129 if (attr)
6130 {
6131 /* The size of the anonymous object containing
6132 the bit field is explicit, so use the
6133 indicated size (in bytes). */
6134 anonymous_size = DW_UNSND (attr);
6135 }
6136 else
6137 {
6138 /* The size of the anonymous object containing
6139 the bit field must be inferred from the type
6140 attribute of the data member containing the
6141 bit field. */
6142 anonymous_size = TYPE_LENGTH (fp->type);
6143 }
6144 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6145 - bit_offset - FIELD_BITSIZE (*fp);
6146 }
6147 }
6148
6149 /* Get name of field. */
39cbfefa
DJ
6150 fieldname = dwarf2_name (die, cu);
6151 if (fieldname == NULL)
6152 fieldname = "";
d8151005
DJ
6153
6154 /* The name is already allocated along with this objfile, so we don't
6155 need to duplicate it for the type. */
6156 fp->name = fieldname;
c906108c
SS
6157
6158 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6159 pointer or virtual base class pointer) to private. */
e142c38c 6160 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6161 {
d48cc9dd 6162 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6163 new_field->accessibility = DW_ACCESS_private;
6164 fip->non_public_fields = 1;
6165 }
6166 }
a9a9bd0f 6167 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6168 {
a9a9bd0f
DC
6169 /* C++ static member. */
6170
6171 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6172 is a declaration, but all versions of G++ as of this writing
6173 (so through at least 3.2.1) incorrectly generate
6174 DW_TAG_variable tags. */
6e70227d 6175
c906108c 6176 char *physname;
c906108c 6177
a9a9bd0f 6178 /* Get name of field. */
39cbfefa
DJ
6179 fieldname = dwarf2_name (die, cu);
6180 if (fieldname == NULL)
c906108c
SS
6181 return;
6182
254e6b9e 6183 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6184 if (attr
6185 /* Only create a symbol if this is an external value.
6186 new_symbol checks this and puts the value in the global symbol
6187 table, which we want. If it is not external, new_symbol
6188 will try to put the value in cu->list_in_scope which is wrong. */
6189 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6190 {
6191 /* A static const member, not much different than an enum as far as
6192 we're concerned, except that we can support more types. */
6193 new_symbol (die, NULL, cu);
6194 }
6195
2df3850c 6196 /* Get physical name. */
94af9270 6197 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6198
d8151005
DJ
6199 /* The name is already allocated along with this objfile, so we don't
6200 need to duplicate it for the type. */
6201 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6202 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6203 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6204 }
6205 else if (die->tag == DW_TAG_inheritance)
6206 {
6207 /* C++ base class field. */
e142c38c 6208 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6209 if (attr)
d4b96c9a
JK
6210 {
6211 int byte_offset = 0;
6212
6213 if (attr_form_is_section_offset (attr))
6214 dwarf2_complex_location_expr_complaint ();
6215 else if (attr_form_is_constant (attr))
6216 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6217 else if (attr_form_is_block (attr))
6218 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6219 else
6220 dwarf2_complex_location_expr_complaint ();
6221
6222 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6223 }
c906108c 6224 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6225 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6226 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6227 fip->nbaseclasses++;
6228 }
6229}
6230
98751a41
JK
6231/* Add a typedef defined in the scope of the FIP's class. */
6232
6233static void
6234dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6235 struct dwarf2_cu *cu)
6e70227d 6236{
98751a41
JK
6237 struct objfile *objfile = cu->objfile;
6238 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6239 struct typedef_field_list *new_field;
6240 struct attribute *attr;
6241 struct typedef_field *fp;
6242 char *fieldname = "";
6243
6244 /* Allocate a new field list entry and link it in. */
6245 new_field = xzalloc (sizeof (*new_field));
6246 make_cleanup (xfree, new_field);
6247
6248 gdb_assert (die->tag == DW_TAG_typedef);
6249
6250 fp = &new_field->field;
6251
6252 /* Get name of field. */
6253 fp->name = dwarf2_name (die, cu);
6254 if (fp->name == NULL)
6255 return;
6256
6257 fp->type = read_type_die (die, cu);
6258
6259 new_field->next = fip->typedef_field_list;
6260 fip->typedef_field_list = new_field;
6261 fip->typedef_field_list_count++;
6262}
6263
c906108c
SS
6264/* Create the vector of fields, and attach it to the type. */
6265
6266static void
fba45db2 6267dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6268 struct dwarf2_cu *cu)
c906108c
SS
6269{
6270 int nfields = fip->nfields;
6271
6272 /* Record the field count, allocate space for the array of fields,
6273 and create blank accessibility bitfields if necessary. */
6274 TYPE_NFIELDS (type) = nfields;
6275 TYPE_FIELDS (type) = (struct field *)
6276 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6277 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6278
b4ba55a1 6279 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6280 {
6281 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6282
6283 TYPE_FIELD_PRIVATE_BITS (type) =
6284 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6285 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6286
6287 TYPE_FIELD_PROTECTED_BITS (type) =
6288 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6289 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6290
774b6a14
TT
6291 TYPE_FIELD_IGNORE_BITS (type) =
6292 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6293 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6294 }
6295
6296 /* If the type has baseclasses, allocate and clear a bit vector for
6297 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6298 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6299 {
6300 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6301 unsigned char *pointer;
c906108c
SS
6302
6303 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6304 pointer = TYPE_ALLOC (type, num_bytes);
6305 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6306 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6307 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6308 }
6309
6310 /* Copy the saved-up fields into the field vector. Start from the head
6311 of the list, adding to the tail of the field array, so that they end
6312 up in the same order in the array in which they were added to the list. */
6313 while (nfields-- > 0)
6314 {
7d0ccb61
DJ
6315 struct nextfield *fieldp;
6316
6317 if (fip->fields)
6318 {
6319 fieldp = fip->fields;
6320 fip->fields = fieldp->next;
6321 }
6322 else
6323 {
6324 fieldp = fip->baseclasses;
6325 fip->baseclasses = fieldp->next;
6326 }
6327
6328 TYPE_FIELD (type, nfields) = fieldp->field;
6329 switch (fieldp->accessibility)
c906108c 6330 {
c5aa993b 6331 case DW_ACCESS_private:
b4ba55a1
JB
6332 if (cu->language != language_ada)
6333 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6334 break;
c906108c 6335
c5aa993b 6336 case DW_ACCESS_protected:
b4ba55a1
JB
6337 if (cu->language != language_ada)
6338 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6339 break;
c906108c 6340
c5aa993b
JM
6341 case DW_ACCESS_public:
6342 break;
c906108c 6343
c5aa993b
JM
6344 default:
6345 /* Unknown accessibility. Complain and treat it as public. */
6346 {
e2e0b3e5 6347 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6348 fieldp->accessibility);
c5aa993b
JM
6349 }
6350 break;
c906108c
SS
6351 }
6352 if (nfields < fip->nbaseclasses)
6353 {
7d0ccb61 6354 switch (fieldp->virtuality)
c906108c 6355 {
c5aa993b
JM
6356 case DW_VIRTUALITY_virtual:
6357 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6358 if (cu->language == language_ada)
6359 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6360 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6361 break;
c906108c
SS
6362 }
6363 }
c906108c
SS
6364 }
6365}
6366
c906108c
SS
6367/* Add a member function to the proper fieldlist. */
6368
6369static void
107d2387 6370dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6371 struct type *type, struct dwarf2_cu *cu)
c906108c 6372{
e7c27a73 6373 struct objfile *objfile = cu->objfile;
c906108c
SS
6374 struct attribute *attr;
6375 struct fnfieldlist *flp;
6376 int i;
6377 struct fn_field *fnp;
6378 char *fieldname;
c906108c 6379 struct nextfnfield *new_fnfield;
f792889a 6380 struct type *this_type;
c906108c 6381
b4ba55a1
JB
6382 if (cu->language == language_ada)
6383 error ("unexpected member function in Ada type");
6384
2df3850c 6385 /* Get name of member function. */
39cbfefa
DJ
6386 fieldname = dwarf2_name (die, cu);
6387 if (fieldname == NULL)
2df3850c 6388 return;
c906108c 6389
c906108c
SS
6390 /* Look up member function name in fieldlist. */
6391 for (i = 0; i < fip->nfnfields; i++)
6392 {
27bfe10e 6393 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6394 break;
6395 }
6396
6397 /* Create new list element if necessary. */
6398 if (i < fip->nfnfields)
6399 flp = &fip->fnfieldlists[i];
6400 else
6401 {
6402 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6403 {
6404 fip->fnfieldlists = (struct fnfieldlist *)
6405 xrealloc (fip->fnfieldlists,
6406 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6407 * sizeof (struct fnfieldlist));
c906108c 6408 if (fip->nfnfields == 0)
c13c43fd 6409 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6410 }
6411 flp = &fip->fnfieldlists[fip->nfnfields];
6412 flp->name = fieldname;
6413 flp->length = 0;
6414 flp->head = NULL;
3da10d80 6415 i = fip->nfnfields++;
c906108c
SS
6416 }
6417
6418 /* Create a new member function field and chain it to the field list
6419 entry. */
6420 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6421 make_cleanup (xfree, new_fnfield);
c906108c
SS
6422 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6423 new_fnfield->next = flp->head;
6424 flp->head = new_fnfield;
6425 flp->length++;
6426
6427 /* Fill in the member function field info. */
6428 fnp = &new_fnfield->fnfield;
3da10d80
KS
6429
6430 /* Delay processing of the physname until later. */
6431 if (cu->language == language_cplus || cu->language == language_java)
6432 {
6433 add_to_method_list (type, i, flp->length - 1, fieldname,
6434 die, cu);
6435 }
6436 else
6437 {
6438 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6439 fnp->physname = physname ? physname : "";
6440 }
6441
c906108c 6442 fnp->type = alloc_type (objfile);
f792889a
DJ
6443 this_type = read_type_die (die, cu);
6444 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6445 {
f792889a 6446 int nparams = TYPE_NFIELDS (this_type);
c906108c 6447
f792889a 6448 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6449 of the method itself (TYPE_CODE_METHOD). */
6450 smash_to_method_type (fnp->type, type,
f792889a
DJ
6451 TYPE_TARGET_TYPE (this_type),
6452 TYPE_FIELDS (this_type),
6453 TYPE_NFIELDS (this_type),
6454 TYPE_VARARGS (this_type));
c906108c
SS
6455
6456 /* Handle static member functions.
c5aa993b
JM
6457 Dwarf2 has no clean way to discern C++ static and non-static
6458 member functions. G++ helps GDB by marking the first
6459 parameter for non-static member functions (which is the
6460 this pointer) as artificial. We obtain this information
6461 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6462 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6463 fnp->voffset = VOFFSET_STATIC;
6464 }
6465 else
e2e0b3e5 6466 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6467 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6468
6469 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6470 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6471 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6472
6473 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6474 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6475
6476 /* Get accessibility. */
e142c38c 6477 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6478 if (attr)
6479 {
6480 switch (DW_UNSND (attr))
6481 {
c5aa993b
JM
6482 case DW_ACCESS_private:
6483 fnp->is_private = 1;
6484 break;
6485 case DW_ACCESS_protected:
6486 fnp->is_protected = 1;
6487 break;
c906108c
SS
6488 }
6489 }
6490
b02dede2 6491 /* Check for artificial methods. */
e142c38c 6492 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6493 if (attr && DW_UNSND (attr) != 0)
6494 fnp->is_artificial = 1;
6495
0d564a31 6496 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6497 function. For older versions of GCC, this is an offset in the
6498 appropriate virtual table, as specified by DW_AT_containing_type.
6499 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6500 to the object address. */
6501
e142c38c 6502 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6503 if (attr)
8e19ed76 6504 {
aec5aa8b 6505 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6506 {
aec5aa8b
TT
6507 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6508 {
6509 /* Old-style GCC. */
6510 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6511 }
6512 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6513 || (DW_BLOCK (attr)->size > 1
6514 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6515 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6516 {
6517 struct dwarf_block blk;
6518 int offset;
6519
6520 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6521 ? 1 : 2);
6522 blk.size = DW_BLOCK (attr)->size - offset;
6523 blk.data = DW_BLOCK (attr)->data + offset;
6524 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6525 if ((fnp->voffset % cu->header.addr_size) != 0)
6526 dwarf2_complex_location_expr_complaint ();
6527 else
6528 fnp->voffset /= cu->header.addr_size;
6529 fnp->voffset += 2;
6530 }
6531 else
6532 dwarf2_complex_location_expr_complaint ();
6533
6534 if (!fnp->fcontext)
6535 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6536 }
3690dd37 6537 else if (attr_form_is_section_offset (attr))
8e19ed76 6538 {
4d3c2250 6539 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6540 }
6541 else
6542 {
4d3c2250
KB
6543 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6544 fieldname);
8e19ed76 6545 }
0d564a31 6546 }
d48cc9dd
DJ
6547 else
6548 {
6549 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6550 if (attr && DW_UNSND (attr))
6551 {
6552 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6553 complaint (&symfile_complaints,
6554 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6555 fieldname, die->offset);
9655fd1a 6556 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6557 TYPE_CPLUS_DYNAMIC (type) = 1;
6558 }
6559 }
c906108c
SS
6560}
6561
6562/* Create the vector of member function fields, and attach it to the type. */
6563
6564static void
fba45db2 6565dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6566 struct dwarf2_cu *cu)
c906108c
SS
6567{
6568 struct fnfieldlist *flp;
6569 int total_length = 0;
6570 int i;
6571
b4ba55a1
JB
6572 if (cu->language == language_ada)
6573 error ("unexpected member functions in Ada type");
6574
c906108c
SS
6575 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6576 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6577 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6578
6579 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6580 {
6581 struct nextfnfield *nfp = flp->head;
6582 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6583 int k;
6584
6585 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6586 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6587 fn_flp->fn_fields = (struct fn_field *)
6588 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6589 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6590 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6591
6592 total_length += flp->length;
6593 }
6594
6595 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6596 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6597}
6598
1168df01
JB
6599/* Returns non-zero if NAME is the name of a vtable member in CU's
6600 language, zero otherwise. */
6601static int
6602is_vtable_name (const char *name, struct dwarf2_cu *cu)
6603{
6604 static const char vptr[] = "_vptr";
987504bb 6605 static const char vtable[] = "vtable";
1168df01 6606
987504bb
JJ
6607 /* Look for the C++ and Java forms of the vtable. */
6608 if ((cu->language == language_java
6609 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6610 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6611 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6612 return 1;
6613
6614 return 0;
6615}
6616
c0dd20ea 6617/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6618 functions, with the ABI-specified layout. If TYPE describes
6619 such a structure, smash it into a member function type.
61049d3b
DJ
6620
6621 GCC shouldn't do this; it should just output pointer to member DIEs.
6622 This is GCC PR debug/28767. */
c0dd20ea 6623
0b92b5bb
TT
6624static void
6625quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6626{
0b92b5bb 6627 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6628
6629 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6630 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6631 return;
c0dd20ea
DJ
6632
6633 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6634 if (TYPE_FIELD_NAME (type, 0) == NULL
6635 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6636 || TYPE_FIELD_NAME (type, 1) == NULL
6637 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6638 return;
c0dd20ea
DJ
6639
6640 /* Find the type of the method. */
0b92b5bb 6641 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6642 if (pfn_type == NULL
6643 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6644 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6645 return;
c0dd20ea
DJ
6646
6647 /* Look for the "this" argument. */
6648 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6649 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6650 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6651 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6652 return;
c0dd20ea
DJ
6653
6654 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6655 new_type = alloc_type (objfile);
6656 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6657 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6658 TYPE_VARARGS (pfn_type));
0b92b5bb 6659 smash_to_methodptr_type (type, new_type);
c0dd20ea 6660}
1168df01 6661
c906108c 6662/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6663 (definition) to create a type for the structure or union. Fill in
6664 the type's name and general properties; the members will not be
6665 processed until process_structure_type.
c906108c 6666
c767944b
DJ
6667 NOTE: we need to call these functions regardless of whether or not the
6668 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6669 structure or union. This gets the type entered into our set of
6670 user defined types.
6671
6672 However, if the structure is incomplete (an opaque struct/union)
6673 then suppress creating a symbol table entry for it since gdb only
6674 wants to find the one with the complete definition. Note that if
6675 it is complete, we just call new_symbol, which does it's own
6676 checking about whether the struct/union is anonymous or not (and
6677 suppresses creating a symbol table entry itself). */
6678
f792889a 6679static struct type *
134d01f1 6680read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6681{
e7c27a73 6682 struct objfile *objfile = cu->objfile;
c906108c
SS
6683 struct type *type;
6684 struct attribute *attr;
39cbfefa 6685 char *name;
c906108c 6686
348e048f
DE
6687 /* If the definition of this type lives in .debug_types, read that type.
6688 Don't follow DW_AT_specification though, that will take us back up
6689 the chain and we want to go down. */
6690 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6691 if (attr)
6692 {
6693 struct dwarf2_cu *type_cu = cu;
6694 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6695
348e048f
DE
6696 /* We could just recurse on read_structure_type, but we need to call
6697 get_die_type to ensure only one type for this DIE is created.
6698 This is important, for example, because for c++ classes we need
6699 TYPE_NAME set which is only done by new_symbol. Blech. */
6700 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6701
6702 /* TYPE_CU may not be the same as CU.
6703 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6704 return set_die_type (die, type, cu);
6705 }
6706
c0dd20ea 6707 type = alloc_type (objfile);
c906108c 6708 INIT_CPLUS_SPECIFIC (type);
93311388 6709
39cbfefa
DJ
6710 name = dwarf2_name (die, cu);
6711 if (name != NULL)
c906108c 6712 {
987504bb
JJ
6713 if (cu->language == language_cplus
6714 || cu->language == language_java)
63d06c5c 6715 {
3da10d80
KS
6716 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6717
6718 /* dwarf2_full_name might have already finished building the DIE's
6719 type. If so, there is no need to continue. */
6720 if (get_die_type (die, cu) != NULL)
6721 return get_die_type (die, cu);
6722
6723 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6724 if (die->tag == DW_TAG_structure_type
6725 || die->tag == DW_TAG_class_type)
6726 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6727 }
6728 else
6729 {
d8151005
DJ
6730 /* The name is already allocated along with this objfile, so
6731 we don't need to duplicate it for the type. */
94af9270
KS
6732 TYPE_TAG_NAME (type) = (char *) name;
6733 if (die->tag == DW_TAG_class_type)
6734 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6735 }
c906108c
SS
6736 }
6737
6738 if (die->tag == DW_TAG_structure_type)
6739 {
6740 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6741 }
6742 else if (die->tag == DW_TAG_union_type)
6743 {
6744 TYPE_CODE (type) = TYPE_CODE_UNION;
6745 }
6746 else
6747 {
c906108c
SS
6748 TYPE_CODE (type) = TYPE_CODE_CLASS;
6749 }
6750
0cc2414c
TT
6751 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6752 TYPE_DECLARED_CLASS (type) = 1;
6753
e142c38c 6754 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6755 if (attr)
6756 {
6757 TYPE_LENGTH (type) = DW_UNSND (attr);
6758 }
6759 else
6760 {
6761 TYPE_LENGTH (type) = 0;
6762 }
6763
876cecd0 6764 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6765 if (die_is_declaration (die, cu))
876cecd0 6766 TYPE_STUB (type) = 1;
a6c727b2
DJ
6767 else if (attr == NULL && die->child == NULL
6768 && producer_is_realview (cu->producer))
6769 /* RealView does not output the required DW_AT_declaration
6770 on incomplete types. */
6771 TYPE_STUB (type) = 1;
dc718098 6772
c906108c
SS
6773 /* We need to add the type field to the die immediately so we don't
6774 infinitely recurse when dealing with pointers to the structure
6775 type within the structure itself. */
1c379e20 6776 set_die_type (die, type, cu);
c906108c 6777
7e314c57
JK
6778 /* set_die_type should be already done. */
6779 set_descriptive_type (type, die, cu);
6780
c767944b
DJ
6781 return type;
6782}
6783
6784/* Finish creating a structure or union type, including filling in
6785 its members and creating a symbol for it. */
6786
6787static void
6788process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6789{
6790 struct objfile *objfile = cu->objfile;
6791 struct die_info *child_die = die->child;
6792 struct type *type;
6793
6794 type = get_die_type (die, cu);
6795 if (type == NULL)
6796 type = read_structure_type (die, cu);
6797
e142c38c 6798 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6799 {
6800 struct field_info fi;
6801 struct die_info *child_die;
34eaf542 6802 VEC (symbolp) *template_args = NULL;
c767944b 6803 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6804
6805 memset (&fi, 0, sizeof (struct field_info));
6806
639d11d3 6807 child_die = die->child;
c906108c
SS
6808
6809 while (child_die && child_die->tag)
6810 {
a9a9bd0f
DC
6811 if (child_die->tag == DW_TAG_member
6812 || child_die->tag == DW_TAG_variable)
c906108c 6813 {
a9a9bd0f
DC
6814 /* NOTE: carlton/2002-11-05: A C++ static data member
6815 should be a DW_TAG_member that is a declaration, but
6816 all versions of G++ as of this writing (so through at
6817 least 3.2.1) incorrectly generate DW_TAG_variable
6818 tags for them instead. */
e7c27a73 6819 dwarf2_add_field (&fi, child_die, cu);
c906108c 6820 }
8713b1b1 6821 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6822 {
6823 /* C++ member function. */
e7c27a73 6824 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6825 }
6826 else if (child_die->tag == DW_TAG_inheritance)
6827 {
6828 /* C++ base class field. */
e7c27a73 6829 dwarf2_add_field (&fi, child_die, cu);
c906108c 6830 }
98751a41
JK
6831 else if (child_die->tag == DW_TAG_typedef)
6832 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6833 else if (child_die->tag == DW_TAG_template_type_param
6834 || child_die->tag == DW_TAG_template_value_param)
6835 {
6836 struct symbol *arg = new_symbol (child_die, NULL, cu);
6837
f1078f66
DJ
6838 if (arg != NULL)
6839 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6840 }
6841
c906108c
SS
6842 child_die = sibling_die (child_die);
6843 }
6844
34eaf542
TT
6845 /* Attach template arguments to type. */
6846 if (! VEC_empty (symbolp, template_args))
6847 {
6848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6849 TYPE_N_TEMPLATE_ARGUMENTS (type)
6850 = VEC_length (symbolp, template_args);
6851 TYPE_TEMPLATE_ARGUMENTS (type)
6852 = obstack_alloc (&objfile->objfile_obstack,
6853 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6854 * sizeof (struct symbol *)));
6855 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6856 VEC_address (symbolp, template_args),
6857 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6858 * sizeof (struct symbol *)));
6859 VEC_free (symbolp, template_args);
6860 }
6861
c906108c
SS
6862 /* Attach fields and member functions to the type. */
6863 if (fi.nfields)
e7c27a73 6864 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6865 if (fi.nfnfields)
6866 {
e7c27a73 6867 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6868
c5aa993b 6869 /* Get the type which refers to the base class (possibly this
c906108c 6870 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6871 class from the DW_AT_containing_type attribute. This use of
6872 DW_AT_containing_type is a GNU extension. */
c906108c 6873
e142c38c 6874 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6875 {
e7c27a73 6876 struct type *t = die_containing_type (die, cu);
c906108c
SS
6877
6878 TYPE_VPTR_BASETYPE (type) = t;
6879 if (type == t)
6880 {
c906108c
SS
6881 int i;
6882
6883 /* Our own class provides vtbl ptr. */
6884 for (i = TYPE_NFIELDS (t) - 1;
6885 i >= TYPE_N_BASECLASSES (t);
6886 --i)
6887 {
6888 char *fieldname = TYPE_FIELD_NAME (t, i);
6889
1168df01 6890 if (is_vtable_name (fieldname, cu))
c906108c
SS
6891 {
6892 TYPE_VPTR_FIELDNO (type) = i;
6893 break;
6894 }
6895 }
6896
6897 /* Complain if virtual function table field not found. */
6898 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6899 complaint (&symfile_complaints,
e2e0b3e5 6900 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6901 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6902 "");
c906108c
SS
6903 }
6904 else
6905 {
6906 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6907 }
6908 }
f6235d4c
EZ
6909 else if (cu->producer
6910 && strncmp (cu->producer,
6911 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6912 {
6913 /* The IBM XLC compiler does not provide direct indication
6914 of the containing type, but the vtable pointer is
6915 always named __vfp. */
6916
6917 int i;
6918
6919 for (i = TYPE_NFIELDS (type) - 1;
6920 i >= TYPE_N_BASECLASSES (type);
6921 --i)
6922 {
6923 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6924 {
6925 TYPE_VPTR_FIELDNO (type) = i;
6926 TYPE_VPTR_BASETYPE (type) = type;
6927 break;
6928 }
6929 }
6930 }
c906108c 6931 }
98751a41
JK
6932
6933 /* Copy fi.typedef_field_list linked list elements content into the
6934 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6935 if (fi.typedef_field_list)
6936 {
6937 int i = fi.typedef_field_list_count;
6938
a0d7a4ff 6939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6940 TYPE_TYPEDEF_FIELD_ARRAY (type)
6941 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6942 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6943
6944 /* Reverse the list order to keep the debug info elements order. */
6945 while (--i >= 0)
6946 {
6947 struct typedef_field *dest, *src;
6e70227d 6948
98751a41
JK
6949 dest = &TYPE_TYPEDEF_FIELD (type, i);
6950 src = &fi.typedef_field_list->field;
6951 fi.typedef_field_list = fi.typedef_field_list->next;
6952 *dest = *src;
6953 }
6954 }
c767944b
DJ
6955
6956 do_cleanups (back_to);
c906108c 6957 }
63d06c5c 6958
0b92b5bb
TT
6959 quirk_gcc_member_function_pointer (type, cu->objfile);
6960
90aeadfc
DC
6961 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6962 snapshots) has been known to create a die giving a declaration
6963 for a class that has, as a child, a die giving a definition for a
6964 nested class. So we have to process our children even if the
6965 current die is a declaration. Normally, of course, a declaration
6966 won't have any children at all. */
134d01f1 6967
90aeadfc
DC
6968 while (child_die != NULL && child_die->tag)
6969 {
6970 if (child_die->tag == DW_TAG_member
6971 || child_die->tag == DW_TAG_variable
34eaf542
TT
6972 || child_die->tag == DW_TAG_inheritance
6973 || child_die->tag == DW_TAG_template_value_param
6974 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6975 {
90aeadfc 6976 /* Do nothing. */
134d01f1 6977 }
90aeadfc
DC
6978 else
6979 process_die (child_die, cu);
134d01f1 6980
90aeadfc 6981 child_die = sibling_die (child_die);
134d01f1
DJ
6982 }
6983
fa4028e9
JB
6984 /* Do not consider external references. According to the DWARF standard,
6985 these DIEs are identified by the fact that they have no byte_size
6986 attribute, and a declaration attribute. */
6987 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6988 || !die_is_declaration (die, cu))
c767944b 6989 new_symbol (die, type, cu);
134d01f1
DJ
6990}
6991
6992/* Given a DW_AT_enumeration_type die, set its type. We do not
6993 complete the type's fields yet, or create any symbols. */
c906108c 6994
f792889a 6995static struct type *
134d01f1 6996read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6997{
e7c27a73 6998 struct objfile *objfile = cu->objfile;
c906108c 6999 struct type *type;
c906108c 7000 struct attribute *attr;
0114d602 7001 const char *name;
134d01f1 7002
348e048f
DE
7003 /* If the definition of this type lives in .debug_types, read that type.
7004 Don't follow DW_AT_specification though, that will take us back up
7005 the chain and we want to go down. */
7006 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7007 if (attr)
7008 {
7009 struct dwarf2_cu *type_cu = cu;
7010 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7011
348e048f 7012 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7013
7014 /* TYPE_CU may not be the same as CU.
7015 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7016 return set_die_type (die, type, cu);
7017 }
7018
c906108c
SS
7019 type = alloc_type (objfile);
7020
7021 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7022 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7023 if (name != NULL)
0114d602 7024 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7025
e142c38c 7026 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7027 if (attr)
7028 {
7029 TYPE_LENGTH (type) = DW_UNSND (attr);
7030 }
7031 else
7032 {
7033 TYPE_LENGTH (type) = 0;
7034 }
7035
137033e9
JB
7036 /* The enumeration DIE can be incomplete. In Ada, any type can be
7037 declared as private in the package spec, and then defined only
7038 inside the package body. Such types are known as Taft Amendment
7039 Types. When another package uses such a type, an incomplete DIE
7040 may be generated by the compiler. */
02eb380e 7041 if (die_is_declaration (die, cu))
876cecd0 7042 TYPE_STUB (type) = 1;
02eb380e 7043
f792889a 7044 return set_die_type (die, type, cu);
134d01f1
DJ
7045}
7046
7047/* Given a pointer to a die which begins an enumeration, process all
7048 the dies that define the members of the enumeration, and create the
7049 symbol for the enumeration type.
7050
7051 NOTE: We reverse the order of the element list. */
7052
7053static void
7054process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7055{
f792889a 7056 struct type *this_type;
134d01f1 7057
f792889a
DJ
7058 this_type = get_die_type (die, cu);
7059 if (this_type == NULL)
7060 this_type = read_enumeration_type (die, cu);
9dc481d3 7061
639d11d3 7062 if (die->child != NULL)
c906108c 7063 {
9dc481d3
DE
7064 struct die_info *child_die;
7065 struct symbol *sym;
7066 struct field *fields = NULL;
7067 int num_fields = 0;
7068 int unsigned_enum = 1;
7069 char *name;
7070
639d11d3 7071 child_die = die->child;
c906108c
SS
7072 while (child_die && child_die->tag)
7073 {
7074 if (child_die->tag != DW_TAG_enumerator)
7075 {
e7c27a73 7076 process_die (child_die, cu);
c906108c
SS
7077 }
7078 else
7079 {
39cbfefa
DJ
7080 name = dwarf2_name (child_die, cu);
7081 if (name)
c906108c 7082 {
f792889a 7083 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7084 if (SYMBOL_VALUE (sym) < 0)
7085 unsigned_enum = 0;
7086
7087 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7088 {
7089 fields = (struct field *)
7090 xrealloc (fields,
7091 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7092 * sizeof (struct field));
c906108c
SS
7093 }
7094
3567439c 7095 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7096 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7097 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7098 FIELD_BITSIZE (fields[num_fields]) = 0;
7099
7100 num_fields++;
7101 }
7102 }
7103
7104 child_die = sibling_die (child_die);
7105 }
7106
7107 if (num_fields)
7108 {
f792889a
DJ
7109 TYPE_NFIELDS (this_type) = num_fields;
7110 TYPE_FIELDS (this_type) = (struct field *)
7111 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7112 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7113 sizeof (struct field) * num_fields);
b8c9b27d 7114 xfree (fields);
c906108c
SS
7115 }
7116 if (unsigned_enum)
876cecd0 7117 TYPE_UNSIGNED (this_type) = 1;
c906108c 7118 }
134d01f1 7119
f792889a 7120 new_symbol (die, this_type, cu);
c906108c
SS
7121}
7122
7123/* Extract all information from a DW_TAG_array_type DIE and put it in
7124 the DIE's type field. For now, this only handles one dimensional
7125 arrays. */
7126
f792889a 7127static struct type *
e7c27a73 7128read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7129{
e7c27a73 7130 struct objfile *objfile = cu->objfile;
c906108c 7131 struct die_info *child_die;
7e314c57 7132 struct type *type;
c906108c
SS
7133 struct type *element_type, *range_type, *index_type;
7134 struct type **range_types = NULL;
7135 struct attribute *attr;
7136 int ndim = 0;
7137 struct cleanup *back_to;
39cbfefa 7138 char *name;
c906108c 7139
e7c27a73 7140 element_type = die_type (die, cu);
c906108c 7141
7e314c57
JK
7142 /* The die_type call above may have already set the type for this DIE. */
7143 type = get_die_type (die, cu);
7144 if (type)
7145 return type;
7146
c906108c
SS
7147 /* Irix 6.2 native cc creates array types without children for
7148 arrays with unspecified length. */
639d11d3 7149 if (die->child == NULL)
c906108c 7150 {
46bf5051 7151 index_type = objfile_type (objfile)->builtin_int;
c906108c 7152 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7153 type = create_array_type (NULL, element_type, range_type);
7154 return set_die_type (die, type, cu);
c906108c
SS
7155 }
7156
7157 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7158 child_die = die->child;
c906108c
SS
7159 while (child_die && child_die->tag)
7160 {
7161 if (child_die->tag == DW_TAG_subrange_type)
7162 {
f792889a 7163 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7164
f792889a 7165 if (child_type != NULL)
a02abb62
JB
7166 {
7167 /* The range type was succesfully read. Save it for
7168 the array type creation. */
7169 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7170 {
7171 range_types = (struct type **)
7172 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7173 * sizeof (struct type *));
7174 if (ndim == 0)
7175 make_cleanup (free_current_contents, &range_types);
7176 }
f792889a 7177 range_types[ndim++] = child_type;
a02abb62 7178 }
c906108c
SS
7179 }
7180 child_die = sibling_die (child_die);
7181 }
7182
7183 /* Dwarf2 dimensions are output from left to right, create the
7184 necessary array types in backwards order. */
7ca2d3a3 7185
c906108c 7186 type = element_type;
7ca2d3a3
DL
7187
7188 if (read_array_order (die, cu) == DW_ORD_col_major)
7189 {
7190 int i = 0;
9a619af0 7191
7ca2d3a3
DL
7192 while (i < ndim)
7193 type = create_array_type (NULL, type, range_types[i++]);
7194 }
7195 else
7196 {
7197 while (ndim-- > 0)
7198 type = create_array_type (NULL, type, range_types[ndim]);
7199 }
c906108c 7200
f5f8a009
EZ
7201 /* Understand Dwarf2 support for vector types (like they occur on
7202 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7203 array type. This is not part of the Dwarf2/3 standard yet, but a
7204 custom vendor extension. The main difference between a regular
7205 array and the vector variant is that vectors are passed by value
7206 to functions. */
e142c38c 7207 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7208 if (attr)
ea37ba09 7209 make_vector_type (type);
f5f8a009 7210
dbc98a8b
KW
7211 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7212 implementation may choose to implement triple vectors using this
7213 attribute. */
7214 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7215 if (attr)
7216 {
7217 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7218 TYPE_LENGTH (type) = DW_UNSND (attr);
7219 else
7220 complaint (&symfile_complaints, _("\
7221DW_AT_byte_size for array type smaller than the total size of elements"));
7222 }
7223
39cbfefa
DJ
7224 name = dwarf2_name (die, cu);
7225 if (name)
7226 TYPE_NAME (type) = name;
6e70227d 7227
7e314c57
JK
7228 /* Install the type in the die. */
7229 set_die_type (die, type, cu);
7230
7231 /* set_die_type should be already done. */
b4ba55a1
JB
7232 set_descriptive_type (type, die, cu);
7233
c906108c
SS
7234 do_cleanups (back_to);
7235
7e314c57 7236 return type;
c906108c
SS
7237}
7238
7ca2d3a3 7239static enum dwarf_array_dim_ordering
6e70227d 7240read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7241{
7242 struct attribute *attr;
7243
7244 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7245
7246 if (attr) return DW_SND (attr);
7247
7248 /*
7249 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7250 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7251 laid out as per normal fortran.
7252
6e70227d 7253 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7254 version checking.
7255 */
7256
905e0470
PM
7257 if (cu->language == language_fortran
7258 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7259 {
7260 return DW_ORD_row_major;
7261 }
7262
6e70227d 7263 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7264 {
7265 case array_column_major:
7266 return DW_ORD_col_major;
7267 case array_row_major:
7268 default:
7269 return DW_ORD_row_major;
7270 };
7271}
7272
72019c9c
GM
7273/* Extract all information from a DW_TAG_set_type DIE and put it in
7274 the DIE's type field. */
7275
f792889a 7276static struct type *
72019c9c
GM
7277read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7278{
7e314c57
JK
7279 struct type *domain_type, *set_type;
7280 struct attribute *attr;
f792889a 7281
7e314c57
JK
7282 domain_type = die_type (die, cu);
7283
7284 /* The die_type call above may have already set the type for this DIE. */
7285 set_type = get_die_type (die, cu);
7286 if (set_type)
7287 return set_type;
7288
7289 set_type = create_set_type (NULL, domain_type);
7290
7291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7292 if (attr)
7293 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7294
f792889a 7295 return set_die_type (die, set_type, cu);
72019c9c 7296}
7ca2d3a3 7297
c906108c
SS
7298/* First cut: install each common block member as a global variable. */
7299
7300static void
e7c27a73 7301read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7302{
7303 struct die_info *child_die;
7304 struct attribute *attr;
7305 struct symbol *sym;
7306 CORE_ADDR base = (CORE_ADDR) 0;
7307
e142c38c 7308 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7309 if (attr)
7310 {
8e19ed76
PS
7311 /* Support the .debug_loc offsets */
7312 if (attr_form_is_block (attr))
7313 {
e7c27a73 7314 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7315 }
3690dd37 7316 else if (attr_form_is_section_offset (attr))
8e19ed76 7317 {
4d3c2250 7318 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7319 }
7320 else
7321 {
4d3c2250
KB
7322 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7323 "common block member");
8e19ed76 7324 }
c906108c 7325 }
639d11d3 7326 if (die->child != NULL)
c906108c 7327 {
639d11d3 7328 child_die = die->child;
c906108c
SS
7329 while (child_die && child_die->tag)
7330 {
e7c27a73 7331 sym = new_symbol (child_die, NULL, cu);
e142c38c 7332 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7333 if (sym != NULL && attr != NULL)
c906108c 7334 {
d4b96c9a
JK
7335 CORE_ADDR byte_offset = 0;
7336
7337 if (attr_form_is_section_offset (attr))
7338 dwarf2_complex_location_expr_complaint ();
7339 else if (attr_form_is_constant (attr))
7340 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7341 else if (attr_form_is_block (attr))
7342 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7343 else
7344 dwarf2_complex_location_expr_complaint ();
7345
7346 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7347 add_symbol_to_list (sym, &global_symbols);
7348 }
7349 child_die = sibling_die (child_die);
7350 }
7351 }
7352}
7353
0114d602 7354/* Create a type for a C++ namespace. */
d9fa45fe 7355
0114d602
DJ
7356static struct type *
7357read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7358{
e7c27a73 7359 struct objfile *objfile = cu->objfile;
0114d602 7360 const char *previous_prefix, *name;
9219021c 7361 int is_anonymous;
0114d602
DJ
7362 struct type *type;
7363
7364 /* For extensions, reuse the type of the original namespace. */
7365 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7366 {
7367 struct die_info *ext_die;
7368 struct dwarf2_cu *ext_cu = cu;
9a619af0 7369
0114d602
DJ
7370 ext_die = dwarf2_extension (die, &ext_cu);
7371 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7372
7373 /* EXT_CU may not be the same as CU.
7374 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7375 return set_die_type (die, type, cu);
7376 }
9219021c 7377
e142c38c 7378 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7379
7380 /* Now build the name of the current namespace. */
7381
0114d602
DJ
7382 previous_prefix = determine_prefix (die, cu);
7383 if (previous_prefix[0] != '\0')
7384 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7385 previous_prefix, name, 0, cu);
0114d602
DJ
7386
7387 /* Create the type. */
7388 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7389 objfile);
7390 TYPE_NAME (type) = (char *) name;
7391 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7392
60531b24 7393 return set_die_type (die, type, cu);
0114d602
DJ
7394}
7395
7396/* Read a C++ namespace. */
7397
7398static void
7399read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7400{
7401 struct objfile *objfile = cu->objfile;
7402 const char *name;
7403 int is_anonymous;
9219021c 7404
5c4e30ca
DC
7405 /* Add a symbol associated to this if we haven't seen the namespace
7406 before. Also, add a using directive if it's an anonymous
7407 namespace. */
9219021c 7408
f2f0e013 7409 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7410 {
7411 struct type *type;
7412
0114d602 7413 type = read_type_die (die, cu);
e7c27a73 7414 new_symbol (die, type, cu);
5c4e30ca 7415
0114d602 7416 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7417 if (is_anonymous)
0114d602
DJ
7418 {
7419 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7420
c0cc3a76 7421 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7422 NULL, &objfile->objfile_obstack);
0114d602 7423 }
5c4e30ca 7424 }
9219021c 7425
639d11d3 7426 if (die->child != NULL)
d9fa45fe 7427 {
639d11d3 7428 struct die_info *child_die = die->child;
6e70227d 7429
d9fa45fe
DC
7430 while (child_die && child_die->tag)
7431 {
e7c27a73 7432 process_die (child_die, cu);
d9fa45fe
DC
7433 child_die = sibling_die (child_die);
7434 }
7435 }
38d518c9
EZ
7436}
7437
f55ee35c
JK
7438/* Read a Fortran module as type. This DIE can be only a declaration used for
7439 imported module. Still we need that type as local Fortran "use ... only"
7440 declaration imports depend on the created type in determine_prefix. */
7441
7442static struct type *
7443read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7444{
7445 struct objfile *objfile = cu->objfile;
7446 char *module_name;
7447 struct type *type;
7448
7449 module_name = dwarf2_name (die, cu);
7450 if (!module_name)
7451 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7452 die->offset);
7453 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7454
7455 /* determine_prefix uses TYPE_TAG_NAME. */
7456 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7457
7458 return set_die_type (die, type, cu);
7459}
7460
5d7cb8df
JK
7461/* Read a Fortran module. */
7462
7463static void
7464read_module (struct die_info *die, struct dwarf2_cu *cu)
7465{
7466 struct die_info *child_die = die->child;
7467
5d7cb8df
JK
7468 while (child_die && child_die->tag)
7469 {
7470 process_die (child_die, cu);
7471 child_die = sibling_die (child_die);
7472 }
7473}
7474
38d518c9
EZ
7475/* Return the name of the namespace represented by DIE. Set
7476 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7477 namespace. */
7478
7479static const char *
e142c38c 7480namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7481{
7482 struct die_info *current_die;
7483 const char *name = NULL;
7484
7485 /* Loop through the extensions until we find a name. */
7486
7487 for (current_die = die;
7488 current_die != NULL;
f2f0e013 7489 current_die = dwarf2_extension (die, &cu))
38d518c9 7490 {
e142c38c 7491 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7492 if (name != NULL)
7493 break;
7494 }
7495
7496 /* Is it an anonymous namespace? */
7497
7498 *is_anonymous = (name == NULL);
7499 if (*is_anonymous)
7500 name = "(anonymous namespace)";
7501
7502 return name;
d9fa45fe
DC
7503}
7504
c906108c
SS
7505/* Extract all information from a DW_TAG_pointer_type DIE and add to
7506 the user defined type vector. */
7507
f792889a 7508static struct type *
e7c27a73 7509read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7510{
5e2b427d 7511 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7512 struct comp_unit_head *cu_header = &cu->header;
c906108c 7513 struct type *type;
8b2dbe47
KB
7514 struct attribute *attr_byte_size;
7515 struct attribute *attr_address_class;
7516 int byte_size, addr_class;
7e314c57
JK
7517 struct type *target_type;
7518
7519 target_type = die_type (die, cu);
c906108c 7520
7e314c57
JK
7521 /* The die_type call above may have already set the type for this DIE. */
7522 type = get_die_type (die, cu);
7523 if (type)
7524 return type;
7525
7526 type = lookup_pointer_type (target_type);
8b2dbe47 7527
e142c38c 7528 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7529 if (attr_byte_size)
7530 byte_size = DW_UNSND (attr_byte_size);
c906108c 7531 else
8b2dbe47
KB
7532 byte_size = cu_header->addr_size;
7533
e142c38c 7534 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7535 if (attr_address_class)
7536 addr_class = DW_UNSND (attr_address_class);
7537 else
7538 addr_class = DW_ADDR_none;
7539
7540 /* If the pointer size or address class is different than the
7541 default, create a type variant marked as such and set the
7542 length accordingly. */
7543 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7544 {
5e2b427d 7545 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7546 {
7547 int type_flags;
7548
849957d9 7549 type_flags = gdbarch_address_class_type_flags
5e2b427d 7550 (gdbarch, byte_size, addr_class);
876cecd0
TT
7551 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7552 == 0);
8b2dbe47
KB
7553 type = make_type_with_address_space (type, type_flags);
7554 }
7555 else if (TYPE_LENGTH (type) != byte_size)
7556 {
e2e0b3e5 7557 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7558 }
6e70227d 7559 else
9a619af0
MS
7560 {
7561 /* Should we also complain about unhandled address classes? */
7562 }
c906108c 7563 }
8b2dbe47
KB
7564
7565 TYPE_LENGTH (type) = byte_size;
f792889a 7566 return set_die_type (die, type, cu);
c906108c
SS
7567}
7568
7569/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7570 the user defined type vector. */
7571
f792889a 7572static struct type *
e7c27a73 7573read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7574{
7575 struct type *type;
7576 struct type *to_type;
7577 struct type *domain;
7578
e7c27a73
DJ
7579 to_type = die_type (die, cu);
7580 domain = die_containing_type (die, cu);
0d5de010 7581
7e314c57
JK
7582 /* The calls above may have already set the type for this DIE. */
7583 type = get_die_type (die, cu);
7584 if (type)
7585 return type;
7586
0d5de010
DJ
7587 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7588 type = lookup_methodptr_type (to_type);
7589 else
7590 type = lookup_memberptr_type (to_type, domain);
c906108c 7591
f792889a 7592 return set_die_type (die, type, cu);
c906108c
SS
7593}
7594
7595/* Extract all information from a DW_TAG_reference_type DIE and add to
7596 the user defined type vector. */
7597
f792889a 7598static struct type *
e7c27a73 7599read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7600{
e7c27a73 7601 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7602 struct type *type, *target_type;
c906108c
SS
7603 struct attribute *attr;
7604
7e314c57
JK
7605 target_type = die_type (die, cu);
7606
7607 /* The die_type call above may have already set the type for this DIE. */
7608 type = get_die_type (die, cu);
7609 if (type)
7610 return type;
7611
7612 type = lookup_reference_type (target_type);
e142c38c 7613 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7614 if (attr)
7615 {
7616 TYPE_LENGTH (type) = DW_UNSND (attr);
7617 }
7618 else
7619 {
107d2387 7620 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7621 }
f792889a 7622 return set_die_type (die, type, cu);
c906108c
SS
7623}
7624
f792889a 7625static struct type *
e7c27a73 7626read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7627{
f792889a 7628 struct type *base_type, *cv_type;
c906108c 7629
e7c27a73 7630 base_type = die_type (die, cu);
7e314c57
JK
7631
7632 /* The die_type call above may have already set the type for this DIE. */
7633 cv_type = get_die_type (die, cu);
7634 if (cv_type)
7635 return cv_type;
7636
2f608a3a
KW
7637 /* In case the const qualifier is applied to an array type, the element type
7638 is so qualified, not the array type (section 6.7.3 of C99). */
7639 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7640 {
7641 struct type *el_type, *inner_array;
7642
7643 base_type = copy_type (base_type);
7644 inner_array = base_type;
7645
7646 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7647 {
7648 TYPE_TARGET_TYPE (inner_array) =
7649 copy_type (TYPE_TARGET_TYPE (inner_array));
7650 inner_array = TYPE_TARGET_TYPE (inner_array);
7651 }
7652
7653 el_type = TYPE_TARGET_TYPE (inner_array);
7654 TYPE_TARGET_TYPE (inner_array) =
7655 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7656
7657 return set_die_type (die, base_type, cu);
7658 }
7659
f792889a
DJ
7660 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7661 return set_die_type (die, cv_type, cu);
c906108c
SS
7662}
7663
f792889a 7664static struct type *
e7c27a73 7665read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7666{
f792889a 7667 struct type *base_type, *cv_type;
c906108c 7668
e7c27a73 7669 base_type = die_type (die, cu);
7e314c57
JK
7670
7671 /* The die_type call above may have already set the type for this DIE. */
7672 cv_type = get_die_type (die, cu);
7673 if (cv_type)
7674 return cv_type;
7675
f792889a
DJ
7676 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7677 return set_die_type (die, cv_type, cu);
c906108c
SS
7678}
7679
7680/* Extract all information from a DW_TAG_string_type DIE and add to
7681 the user defined type vector. It isn't really a user defined type,
7682 but it behaves like one, with other DIE's using an AT_user_def_type
7683 attribute to reference it. */
7684
f792889a 7685static struct type *
e7c27a73 7686read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7687{
e7c27a73 7688 struct objfile *objfile = cu->objfile;
3b7538c0 7689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7690 struct type *type, *range_type, *index_type, *char_type;
7691 struct attribute *attr;
7692 unsigned int length;
7693
e142c38c 7694 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7695 if (attr)
7696 {
7697 length = DW_UNSND (attr);
7698 }
7699 else
7700 {
b21b22e0 7701 /* check for the DW_AT_byte_size attribute */
e142c38c 7702 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7703 if (attr)
7704 {
7705 length = DW_UNSND (attr);
7706 }
7707 else
7708 {
7709 length = 1;
7710 }
c906108c 7711 }
6ccb9162 7712
46bf5051 7713 index_type = objfile_type (objfile)->builtin_int;
c906108c 7714 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7715 char_type = language_string_char_type (cu->language_defn, gdbarch);
7716 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7717
f792889a 7718 return set_die_type (die, type, cu);
c906108c
SS
7719}
7720
7721/* Handle DIES due to C code like:
7722
7723 struct foo
c5aa993b
JM
7724 {
7725 int (*funcp)(int a, long l);
7726 int b;
7727 };
c906108c
SS
7728
7729 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7730 */
c906108c 7731
f792889a 7732static struct type *
e7c27a73 7733read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7734{
7735 struct type *type; /* Type that this function returns */
7736 struct type *ftype; /* Function that returns above type */
7737 struct attribute *attr;
7738
e7c27a73 7739 type = die_type (die, cu);
7e314c57
JK
7740
7741 /* The die_type call above may have already set the type for this DIE. */
7742 ftype = get_die_type (die, cu);
7743 if (ftype)
7744 return ftype;
7745
0c8b41f1 7746 ftype = lookup_function_type (type);
c906108c 7747
5b8101ae 7748 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7749 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7750 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7751 || cu->language == language_cplus
5b8101ae
PM
7752 || cu->language == language_java
7753 || cu->language == language_pascal)
876cecd0 7754 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7755 else if (producer_is_realview (cu->producer))
7756 /* RealView does not emit DW_AT_prototyped. We can not
7757 distinguish prototyped and unprototyped functions; default to
7758 prototyped, since that is more common in modern code (and
7759 RealView warns about unprototyped functions). */
7760 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7761
c055b101
CV
7762 /* Store the calling convention in the type if it's available in
7763 the subroutine die. Otherwise set the calling convention to
7764 the default value DW_CC_normal. */
7765 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7766 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7767
7768 /* We need to add the subroutine type to the die immediately so
7769 we don't infinitely recurse when dealing with parameters
7770 declared as the same subroutine type. */
7771 set_die_type (die, ftype, cu);
6e70227d 7772
639d11d3 7773 if (die->child != NULL)
c906108c 7774 {
8072405b 7775 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7776 struct die_info *child_die;
8072405b 7777 int nparams, iparams;
c906108c
SS
7778
7779 /* Count the number of parameters.
7780 FIXME: GDB currently ignores vararg functions, but knows about
7781 vararg member functions. */
8072405b 7782 nparams = 0;
639d11d3 7783 child_die = die->child;
c906108c
SS
7784 while (child_die && child_die->tag)
7785 {
7786 if (child_die->tag == DW_TAG_formal_parameter)
7787 nparams++;
7788 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7789 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7790 child_die = sibling_die (child_die);
7791 }
7792
7793 /* Allocate storage for parameters and fill them in. */
7794 TYPE_NFIELDS (ftype) = nparams;
7795 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7796 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7797
8072405b
JK
7798 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7799 even if we error out during the parameters reading below. */
7800 for (iparams = 0; iparams < nparams; iparams++)
7801 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7802
7803 iparams = 0;
639d11d3 7804 child_die = die->child;
c906108c
SS
7805 while (child_die && child_die->tag)
7806 {
7807 if (child_die->tag == DW_TAG_formal_parameter)
7808 {
3ce3b1ba
PA
7809 struct type *arg_type;
7810
7811 /* DWARF version 2 has no clean way to discern C++
7812 static and non-static member functions. G++ helps
7813 GDB by marking the first parameter for non-static
7814 member functions (which is the this pointer) as
7815 artificial. We pass this information to
7816 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7817
7818 DWARF version 3 added DW_AT_object_pointer, which GCC
7819 4.5 does not yet generate. */
e142c38c 7820 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7821 if (attr)
7822 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7823 else
418835cc
KS
7824 {
7825 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7826
7827 /* GCC/43521: In java, the formal parameter
7828 "this" is sometimes not marked with DW_AT_artificial. */
7829 if (cu->language == language_java)
7830 {
7831 const char *name = dwarf2_name (child_die, cu);
9a619af0 7832
418835cc
KS
7833 if (name && !strcmp (name, "this"))
7834 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7835 }
7836 }
3ce3b1ba
PA
7837 arg_type = die_type (child_die, cu);
7838
7839 /* RealView does not mark THIS as const, which the testsuite
7840 expects. GCC marks THIS as const in method definitions,
7841 but not in the class specifications (GCC PR 43053). */
7842 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7843 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7844 {
7845 int is_this = 0;
7846 struct dwarf2_cu *arg_cu = cu;
7847 const char *name = dwarf2_name (child_die, cu);
7848
7849 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7850 if (attr)
7851 {
7852 /* If the compiler emits this, use it. */
7853 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7854 is_this = 1;
7855 }
7856 else if (name && strcmp (name, "this") == 0)
7857 /* Function definitions will have the argument names. */
7858 is_this = 1;
7859 else if (name == NULL && iparams == 0)
7860 /* Declarations may not have the names, so like
7861 elsewhere in GDB, assume an artificial first
7862 argument is "this". */
7863 is_this = 1;
7864
7865 if (is_this)
7866 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7867 arg_type, 0);
7868 }
7869
7870 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7871 iparams++;
7872 }
7873 child_die = sibling_die (child_die);
7874 }
7875 }
7876
76c10ea2 7877 return ftype;
c906108c
SS
7878}
7879
f792889a 7880static struct type *
e7c27a73 7881read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7882{
e7c27a73 7883 struct objfile *objfile = cu->objfile;
0114d602 7884 const char *name = NULL;
f792889a 7885 struct type *this_type;
c906108c 7886
94af9270 7887 name = dwarf2_full_name (NULL, die, cu);
f792889a 7888 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7889 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7890 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7891 set_die_type (die, this_type, cu);
7892 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7893 return this_type;
c906108c
SS
7894}
7895
7896/* Find a representation of a given base type and install
7897 it in the TYPE field of the die. */
7898
f792889a 7899static struct type *
e7c27a73 7900read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7901{
e7c27a73 7902 struct objfile *objfile = cu->objfile;
c906108c
SS
7903 struct type *type;
7904 struct attribute *attr;
7905 int encoding = 0, size = 0;
39cbfefa 7906 char *name;
6ccb9162
UW
7907 enum type_code code = TYPE_CODE_INT;
7908 int type_flags = 0;
7909 struct type *target_type = NULL;
c906108c 7910
e142c38c 7911 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7912 if (attr)
7913 {
7914 encoding = DW_UNSND (attr);
7915 }
e142c38c 7916 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7917 if (attr)
7918 {
7919 size = DW_UNSND (attr);
7920 }
39cbfefa 7921 name = dwarf2_name (die, cu);
6ccb9162 7922 if (!name)
c906108c 7923 {
6ccb9162
UW
7924 complaint (&symfile_complaints,
7925 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7926 }
6ccb9162
UW
7927
7928 switch (encoding)
c906108c 7929 {
6ccb9162
UW
7930 case DW_ATE_address:
7931 /* Turn DW_ATE_address into a void * pointer. */
7932 code = TYPE_CODE_PTR;
7933 type_flags |= TYPE_FLAG_UNSIGNED;
7934 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7935 break;
7936 case DW_ATE_boolean:
7937 code = TYPE_CODE_BOOL;
7938 type_flags |= TYPE_FLAG_UNSIGNED;
7939 break;
7940 case DW_ATE_complex_float:
7941 code = TYPE_CODE_COMPLEX;
7942 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7943 break;
7944 case DW_ATE_decimal_float:
7945 code = TYPE_CODE_DECFLOAT;
7946 break;
7947 case DW_ATE_float:
7948 code = TYPE_CODE_FLT;
7949 break;
7950 case DW_ATE_signed:
7951 break;
7952 case DW_ATE_unsigned:
7953 type_flags |= TYPE_FLAG_UNSIGNED;
7954 break;
7955 case DW_ATE_signed_char:
6e70227d 7956 if (cu->language == language_ada || cu->language == language_m2
868a0084 7957 || cu->language == language_pascal)
6ccb9162
UW
7958 code = TYPE_CODE_CHAR;
7959 break;
7960 case DW_ATE_unsigned_char:
868a0084
PM
7961 if (cu->language == language_ada || cu->language == language_m2
7962 || cu->language == language_pascal)
6ccb9162
UW
7963 code = TYPE_CODE_CHAR;
7964 type_flags |= TYPE_FLAG_UNSIGNED;
7965 break;
75079b2b
TT
7966 case DW_ATE_UTF:
7967 /* We just treat this as an integer and then recognize the
7968 type by name elsewhere. */
7969 break;
7970
6ccb9162
UW
7971 default:
7972 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7973 dwarf_type_encoding_name (encoding));
7974 break;
c906108c 7975 }
6ccb9162 7976
0114d602
DJ
7977 type = init_type (code, size, type_flags, NULL, objfile);
7978 TYPE_NAME (type) = name;
6ccb9162
UW
7979 TYPE_TARGET_TYPE (type) = target_type;
7980
0114d602 7981 if (name && strcmp (name, "char") == 0)
876cecd0 7982 TYPE_NOSIGN (type) = 1;
0114d602 7983
f792889a 7984 return set_die_type (die, type, cu);
c906108c
SS
7985}
7986
a02abb62
JB
7987/* Read the given DW_AT_subrange DIE. */
7988
f792889a 7989static struct type *
a02abb62
JB
7990read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7991{
5e2b427d 7992 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7993 struct type *base_type;
7994 struct type *range_type;
7995 struct attribute *attr;
43bbcdc2
PH
7996 LONGEST low = 0;
7997 LONGEST high = -1;
39cbfefa 7998 char *name;
43bbcdc2 7999 LONGEST negative_mask;
e77813c8 8000
a02abb62 8001 base_type = die_type (die, cu);
953ac07e
JK
8002 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8003 check_typedef (base_type);
a02abb62 8004
7e314c57
JK
8005 /* The die_type call above may have already set the type for this DIE. */
8006 range_type = get_die_type (die, cu);
8007 if (range_type)
8008 return range_type;
8009
e142c38c 8010 if (cu->language == language_fortran)
6e70227d 8011 {
a02abb62
JB
8012 /* FORTRAN implies a lower bound of 1, if not given. */
8013 low = 1;
8014 }
8015
dd5e6932
DJ
8016 /* FIXME: For variable sized arrays either of these could be
8017 a variable rather than a constant value. We'll allow it,
8018 but we don't know how to handle it. */
e142c38c 8019 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8020 if (attr)
8021 low = dwarf2_get_attr_constant_value (attr, 0);
8022
e142c38c 8023 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8024 if (attr)
6e70227d 8025 {
e77813c8 8026 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8027 {
8028 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8029 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8030 FIXME: GDB does not yet know how to handle dynamic
8031 arrays properly, treat them as arrays with unspecified
8032 length for now.
8033
8034 FIXME: jimb/2003-09-22: GDB does not really know
8035 how to handle arrays of unspecified length
8036 either; we just represent them as zero-length
8037 arrays. Choose an appropriate upper bound given
8038 the lower bound we've computed above. */
8039 high = low - 1;
8040 }
8041 else
8042 high = dwarf2_get_attr_constant_value (attr, 1);
8043 }
e77813c8
PM
8044 else
8045 {
8046 attr = dwarf2_attr (die, DW_AT_count, cu);
8047 if (attr)
8048 {
8049 int count = dwarf2_get_attr_constant_value (attr, 1);
8050 high = low + count - 1;
8051 }
8052 }
8053
8054 /* Dwarf-2 specifications explicitly allows to create subrange types
8055 without specifying a base type.
8056 In that case, the base type must be set to the type of
8057 the lower bound, upper bound or count, in that order, if any of these
8058 three attributes references an object that has a type.
8059 If no base type is found, the Dwarf-2 specifications say that
8060 a signed integer type of size equal to the size of an address should
8061 be used.
8062 For the following C code: `extern char gdb_int [];'
8063 GCC produces an empty range DIE.
8064 FIXME: muller/2010-05-28: Possible references to object for low bound,
8065 high bound or count are not yet handled by this code.
8066 */
8067 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8068 {
8069 struct objfile *objfile = cu->objfile;
8070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8071 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8072 struct type *int_type = objfile_type (objfile)->builtin_int;
8073
8074 /* Test "int", "long int", and "long long int" objfile types,
8075 and select the first one having a size above or equal to the
8076 architecture address size. */
8077 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8078 base_type = int_type;
8079 else
8080 {
8081 int_type = objfile_type (objfile)->builtin_long;
8082 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8083 base_type = int_type;
8084 else
8085 {
8086 int_type = objfile_type (objfile)->builtin_long_long;
8087 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8088 base_type = int_type;
8089 }
8090 }
8091 }
a02abb62 8092
6e70227d 8093 negative_mask =
43bbcdc2
PH
8094 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8095 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8096 low |= negative_mask;
8097 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8098 high |= negative_mask;
8099
a02abb62
JB
8100 range_type = create_range_type (NULL, base_type, low, high);
8101
bbb0eef6
JK
8102 /* Mark arrays with dynamic length at least as an array of unspecified
8103 length. GDB could check the boundary but before it gets implemented at
8104 least allow accessing the array elements. */
8105 if (attr && attr->form == DW_FORM_block1)
8106 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8107
39cbfefa
DJ
8108 name = dwarf2_name (die, cu);
8109 if (name)
8110 TYPE_NAME (range_type) = name;
6e70227d 8111
e142c38c 8112 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8113 if (attr)
8114 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8115
7e314c57
JK
8116 set_die_type (die, range_type, cu);
8117
8118 /* set_die_type should be already done. */
b4ba55a1
JB
8119 set_descriptive_type (range_type, die, cu);
8120
7e314c57 8121 return range_type;
a02abb62 8122}
6e70227d 8123
f792889a 8124static struct type *
81a17f79
JB
8125read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8126{
8127 struct type *type;
81a17f79 8128
81a17f79
JB
8129 /* For now, we only support the C meaning of an unspecified type: void. */
8130
0114d602
DJ
8131 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8132 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8133
f792889a 8134 return set_die_type (die, type, cu);
81a17f79 8135}
a02abb62 8136
51545339
DJ
8137/* Trivial hash function for die_info: the hash value of a DIE
8138 is its offset in .debug_info for this objfile. */
8139
8140static hashval_t
8141die_hash (const void *item)
8142{
8143 const struct die_info *die = item;
9a619af0 8144
51545339
DJ
8145 return die->offset;
8146}
8147
8148/* Trivial comparison function for die_info structures: two DIEs
8149 are equal if they have the same offset. */
8150
8151static int
8152die_eq (const void *item_lhs, const void *item_rhs)
8153{
8154 const struct die_info *die_lhs = item_lhs;
8155 const struct die_info *die_rhs = item_rhs;
9a619af0 8156
51545339
DJ
8157 return die_lhs->offset == die_rhs->offset;
8158}
8159
c906108c
SS
8160/* Read a whole compilation unit into a linked list of dies. */
8161
f9aca02d 8162static struct die_info *
93311388 8163read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8164{
93311388 8165 struct die_reader_specs reader_specs;
98bfdba5 8166 int read_abbrevs = 0;
1d9ec526 8167 struct cleanup *back_to = NULL;
98bfdba5
PA
8168 struct die_info *die;
8169
8170 if (cu->dwarf2_abbrevs == NULL)
8171 {
8172 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8173 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8174 read_abbrevs = 1;
8175 }
93311388 8176
348e048f 8177 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8178 cu->die_hash
8179 = htab_create_alloc_ex (cu->header.length / 12,
8180 die_hash,
8181 die_eq,
8182 NULL,
8183 &cu->comp_unit_obstack,
8184 hashtab_obstack_allocate,
8185 dummy_obstack_deallocate);
8186
93311388
DE
8187 init_cu_die_reader (&reader_specs, cu);
8188
98bfdba5
PA
8189 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8190
8191 if (read_abbrevs)
8192 do_cleanups (back_to);
8193
8194 return die;
639d11d3
DC
8195}
8196
d97bc12b
DE
8197/* Main entry point for reading a DIE and all children.
8198 Read the DIE and dump it if requested. */
8199
8200static struct die_info *
93311388
DE
8201read_die_and_children (const struct die_reader_specs *reader,
8202 gdb_byte *info_ptr,
d97bc12b
DE
8203 gdb_byte **new_info_ptr,
8204 struct die_info *parent)
8205{
93311388 8206 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8207 new_info_ptr, parent);
8208
8209 if (dwarf2_die_debug)
8210 {
348e048f
DE
8211 fprintf_unfiltered (gdb_stdlog,
8212 "\nRead die from %s of %s:\n",
8213 reader->buffer == dwarf2_per_objfile->info.buffer
8214 ? ".debug_info"
8215 : reader->buffer == dwarf2_per_objfile->types.buffer
8216 ? ".debug_types"
8217 : "unknown section",
8218 reader->abfd->filename);
d97bc12b
DE
8219 dump_die (result, dwarf2_die_debug);
8220 }
8221
8222 return result;
8223}
8224
639d11d3
DC
8225/* Read a single die and all its descendents. Set the die's sibling
8226 field to NULL; set other fields in the die correctly, and set all
8227 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8228 location of the info_ptr after reading all of those dies. PARENT
8229 is the parent of the die in question. */
8230
8231static struct die_info *
93311388
DE
8232read_die_and_children_1 (const struct die_reader_specs *reader,
8233 gdb_byte *info_ptr,
d97bc12b
DE
8234 gdb_byte **new_info_ptr,
8235 struct die_info *parent)
639d11d3
DC
8236{
8237 struct die_info *die;
fe1b8b76 8238 gdb_byte *cur_ptr;
639d11d3
DC
8239 int has_children;
8240
93311388 8241 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8242 if (die == NULL)
8243 {
8244 *new_info_ptr = cur_ptr;
8245 return NULL;
8246 }
93311388 8247 store_in_ref_table (die, reader->cu);
639d11d3
DC
8248
8249 if (has_children)
348e048f 8250 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8251 else
8252 {
8253 die->child = NULL;
8254 *new_info_ptr = cur_ptr;
8255 }
8256
8257 die->sibling = NULL;
8258 die->parent = parent;
8259 return die;
8260}
8261
8262/* Read a die, all of its descendents, and all of its siblings; set
8263 all of the fields of all of the dies correctly. Arguments are as
8264 in read_die_and_children. */
8265
8266static struct die_info *
93311388
DE
8267read_die_and_siblings (const struct die_reader_specs *reader,
8268 gdb_byte *info_ptr,
fe1b8b76 8269 gdb_byte **new_info_ptr,
639d11d3
DC
8270 struct die_info *parent)
8271{
8272 struct die_info *first_die, *last_sibling;
fe1b8b76 8273 gdb_byte *cur_ptr;
639d11d3 8274
c906108c 8275 cur_ptr = info_ptr;
639d11d3
DC
8276 first_die = last_sibling = NULL;
8277
8278 while (1)
c906108c 8279 {
639d11d3 8280 struct die_info *die
93311388 8281 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8282
1d325ec1 8283 if (die == NULL)
c906108c 8284 {
639d11d3
DC
8285 *new_info_ptr = cur_ptr;
8286 return first_die;
c906108c 8287 }
1d325ec1
DJ
8288
8289 if (!first_die)
8290 first_die = die;
c906108c 8291 else
1d325ec1
DJ
8292 last_sibling->sibling = die;
8293
8294 last_sibling = die;
c906108c 8295 }
c906108c
SS
8296}
8297
93311388
DE
8298/* Read the die from the .debug_info section buffer. Set DIEP to
8299 point to a newly allocated die with its information, except for its
8300 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8301 whether the die has children or not. */
8302
8303static gdb_byte *
8304read_full_die (const struct die_reader_specs *reader,
8305 struct die_info **diep, gdb_byte *info_ptr,
8306 int *has_children)
8307{
8308 unsigned int abbrev_number, bytes_read, i, offset;
8309 struct abbrev_info *abbrev;
8310 struct die_info *die;
8311 struct dwarf2_cu *cu = reader->cu;
8312 bfd *abfd = reader->abfd;
8313
8314 offset = info_ptr - reader->buffer;
8315 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8316 info_ptr += bytes_read;
8317 if (!abbrev_number)
8318 {
8319 *diep = NULL;
8320 *has_children = 0;
8321 return info_ptr;
8322 }
8323
8324 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8325 if (!abbrev)
348e048f
DE
8326 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8327 abbrev_number,
8328 bfd_get_filename (abfd));
8329
93311388
DE
8330 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8331 die->offset = offset;
8332 die->tag = abbrev->tag;
8333 die->abbrev = abbrev_number;
8334
8335 die->num_attrs = abbrev->num_attrs;
8336
8337 for (i = 0; i < abbrev->num_attrs; ++i)
8338 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8339 abfd, info_ptr, cu);
8340
8341 *diep = die;
8342 *has_children = abbrev->has_children;
8343 return info_ptr;
8344}
8345
c906108c
SS
8346/* In DWARF version 2, the description of the debugging information is
8347 stored in a separate .debug_abbrev section. Before we read any
8348 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8349 in a hash table. This function also sets flags in CU describing
8350 the data found in the abbrev table. */
c906108c
SS
8351
8352static void
e7c27a73 8353dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8354{
e7c27a73 8355 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8356 gdb_byte *abbrev_ptr;
c906108c
SS
8357 struct abbrev_info *cur_abbrev;
8358 unsigned int abbrev_number, bytes_read, abbrev_name;
8359 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8360 struct attr_abbrev *cur_attrs;
8361 unsigned int allocated_attrs;
c906108c 8362
57349743 8363 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8364 obstack_init (&cu->abbrev_obstack);
8365 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8366 (ABBREV_HASH_SIZE
8367 * sizeof (struct abbrev_info *)));
8368 memset (cu->dwarf2_abbrevs, 0,
8369 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8370
be391dca
TT
8371 dwarf2_read_section (dwarf2_per_objfile->objfile,
8372 &dwarf2_per_objfile->abbrev);
dce234bc 8373 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8374 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8375 abbrev_ptr += bytes_read;
8376
f3dd6933
DJ
8377 allocated_attrs = ATTR_ALLOC_CHUNK;
8378 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8379
c906108c
SS
8380 /* loop until we reach an abbrev number of 0 */
8381 while (abbrev_number)
8382 {
f3dd6933 8383 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8384
8385 /* read in abbrev header */
8386 cur_abbrev->number = abbrev_number;
8387 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8388 abbrev_ptr += bytes_read;
8389 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8390 abbrev_ptr += 1;
8391
72bf9492
DJ
8392 if (cur_abbrev->tag == DW_TAG_namespace)
8393 cu->has_namespace_info = 1;
8394
c906108c
SS
8395 /* now read in declarations */
8396 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8397 abbrev_ptr += bytes_read;
8398 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8399 abbrev_ptr += bytes_read;
8400 while (abbrev_name)
8401 {
f3dd6933 8402 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8403 {
f3dd6933
DJ
8404 allocated_attrs += ATTR_ALLOC_CHUNK;
8405 cur_attrs
8406 = xrealloc (cur_attrs, (allocated_attrs
8407 * sizeof (struct attr_abbrev)));
c906108c 8408 }
ae038cb0
DJ
8409
8410 /* Record whether this compilation unit might have
8411 inter-compilation-unit references. If we don't know what form
8412 this attribute will have, then it might potentially be a
8413 DW_FORM_ref_addr, so we conservatively expect inter-CU
8414 references. */
8415
8416 if (abbrev_form == DW_FORM_ref_addr
8417 || abbrev_form == DW_FORM_indirect)
8418 cu->has_form_ref_addr = 1;
8419
f3dd6933
DJ
8420 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8421 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8422 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8423 abbrev_ptr += bytes_read;
8424 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8425 abbrev_ptr += bytes_read;
8426 }
8427
f3dd6933
DJ
8428 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8429 (cur_abbrev->num_attrs
8430 * sizeof (struct attr_abbrev)));
8431 memcpy (cur_abbrev->attrs, cur_attrs,
8432 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8433
c906108c 8434 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8435 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8436 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8437
8438 /* Get next abbreviation.
8439 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8440 always properly terminated with an abbrev number of 0.
8441 Exit loop if we encounter an abbreviation which we have
8442 already read (which means we are about to read the abbreviations
8443 for the next compile unit) or if the end of the abbreviation
8444 table is reached. */
dce234bc
PP
8445 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8446 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8447 break;
8448 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8449 abbrev_ptr += bytes_read;
e7c27a73 8450 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8451 break;
8452 }
f3dd6933
DJ
8453
8454 xfree (cur_attrs);
c906108c
SS
8455}
8456
f3dd6933 8457/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8458
c906108c 8459static void
f3dd6933 8460dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8461{
f3dd6933 8462 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8463
f3dd6933
DJ
8464 obstack_free (&cu->abbrev_obstack, NULL);
8465 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8466}
8467
8468/* Lookup an abbrev_info structure in the abbrev hash table. */
8469
8470static struct abbrev_info *
e7c27a73 8471dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8472{
8473 unsigned int hash_number;
8474 struct abbrev_info *abbrev;
8475
8476 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8477 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8478
8479 while (abbrev)
8480 {
8481 if (abbrev->number == number)
8482 return abbrev;
8483 else
8484 abbrev = abbrev->next;
8485 }
8486 return NULL;
8487}
8488
72bf9492
DJ
8489/* Returns nonzero if TAG represents a type that we might generate a partial
8490 symbol for. */
8491
8492static int
8493is_type_tag_for_partial (int tag)
8494{
8495 switch (tag)
8496 {
8497#if 0
8498 /* Some types that would be reasonable to generate partial symbols for,
8499 that we don't at present. */
8500 case DW_TAG_array_type:
8501 case DW_TAG_file_type:
8502 case DW_TAG_ptr_to_member_type:
8503 case DW_TAG_set_type:
8504 case DW_TAG_string_type:
8505 case DW_TAG_subroutine_type:
8506#endif
8507 case DW_TAG_base_type:
8508 case DW_TAG_class_type:
680b30c7 8509 case DW_TAG_interface_type:
72bf9492
DJ
8510 case DW_TAG_enumeration_type:
8511 case DW_TAG_structure_type:
8512 case DW_TAG_subrange_type:
8513 case DW_TAG_typedef:
8514 case DW_TAG_union_type:
8515 return 1;
8516 default:
8517 return 0;
8518 }
8519}
8520
8521/* Load all DIEs that are interesting for partial symbols into memory. */
8522
8523static struct partial_die_info *
93311388
DE
8524load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8525 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8526{
8527 struct partial_die_info *part_die;
8528 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8529 struct abbrev_info *abbrev;
8530 unsigned int bytes_read;
5afb4e99 8531 unsigned int load_all = 0;
72bf9492
DJ
8532
8533 int nesting_level = 1;
8534
8535 parent_die = NULL;
8536 last_die = NULL;
8537
5afb4e99
DJ
8538 if (cu->per_cu && cu->per_cu->load_all_dies)
8539 load_all = 1;
8540
72bf9492
DJ
8541 cu->partial_dies
8542 = htab_create_alloc_ex (cu->header.length / 12,
8543 partial_die_hash,
8544 partial_die_eq,
8545 NULL,
8546 &cu->comp_unit_obstack,
8547 hashtab_obstack_allocate,
8548 dummy_obstack_deallocate);
8549
8550 part_die = obstack_alloc (&cu->comp_unit_obstack,
8551 sizeof (struct partial_die_info));
8552
8553 while (1)
8554 {
8555 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8556
8557 /* A NULL abbrev means the end of a series of children. */
8558 if (abbrev == NULL)
8559 {
8560 if (--nesting_level == 0)
8561 {
8562 /* PART_DIE was probably the last thing allocated on the
8563 comp_unit_obstack, so we could call obstack_free
8564 here. We don't do that because the waste is small,
8565 and will be cleaned up when we're done with this
8566 compilation unit. This way, we're also more robust
8567 against other users of the comp_unit_obstack. */
8568 return first_die;
8569 }
8570 info_ptr += bytes_read;
8571 last_die = parent_die;
8572 parent_die = parent_die->die_parent;
8573 continue;
8574 }
8575
98bfdba5
PA
8576 /* Check for template arguments. We never save these; if
8577 they're seen, we just mark the parent, and go on our way. */
8578 if (parent_die != NULL
8579 && cu->language == language_cplus
8580 && (abbrev->tag == DW_TAG_template_type_param
8581 || abbrev->tag == DW_TAG_template_value_param))
8582 {
8583 parent_die->has_template_arguments = 1;
8584
8585 if (!load_all)
8586 {
8587 /* We don't need a partial DIE for the template argument. */
8588 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8589 cu);
8590 continue;
8591 }
8592 }
8593
8594 /* We only recurse into subprograms looking for template arguments.
8595 Skip their other children. */
8596 if (!load_all
8597 && cu->language == language_cplus
8598 && parent_die != NULL
8599 && parent_die->tag == DW_TAG_subprogram)
8600 {
8601 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8602 continue;
8603 }
8604
5afb4e99
DJ
8605 /* Check whether this DIE is interesting enough to save. Normally
8606 we would not be interested in members here, but there may be
8607 later variables referencing them via DW_AT_specification (for
8608 static members). */
8609 if (!load_all
8610 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8611 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8612 && abbrev->tag != DW_TAG_enumerator
8613 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8614 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8615 && abbrev->tag != DW_TAG_variable
5afb4e99 8616 && abbrev->tag != DW_TAG_namespace
f55ee35c 8617 && abbrev->tag != DW_TAG_module
5afb4e99 8618 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8619 {
8620 /* Otherwise we skip to the next sibling, if any. */
93311388 8621 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8622 continue;
8623 }
8624
93311388
DE
8625 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8626 buffer, info_ptr, cu);
72bf9492
DJ
8627
8628 /* This two-pass algorithm for processing partial symbols has a
8629 high cost in cache pressure. Thus, handle some simple cases
8630 here which cover the majority of C partial symbols. DIEs
8631 which neither have specification tags in them, nor could have
8632 specification tags elsewhere pointing at them, can simply be
8633 processed and discarded.
8634
8635 This segment is also optional; scan_partial_symbols and
8636 add_partial_symbol will handle these DIEs if we chain
8637 them in normally. When compilers which do not emit large
8638 quantities of duplicate debug information are more common,
8639 this code can probably be removed. */
8640
8641 /* Any complete simple types at the top level (pretty much all
8642 of them, for a language without namespaces), can be processed
8643 directly. */
8644 if (parent_die == NULL
8645 && part_die->has_specification == 0
8646 && part_die->is_declaration == 0
8647 && (part_die->tag == DW_TAG_typedef
8648 || part_die->tag == DW_TAG_base_type
8649 || part_die->tag == DW_TAG_subrange_type))
8650 {
8651 if (building_psymtab && part_die->name != NULL)
04a679b8 8652 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8653 VAR_DOMAIN, LOC_TYPEDEF,
8654 &cu->objfile->static_psymbols,
8655 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8656 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8657 continue;
8658 }
8659
8660 /* If we're at the second level, and we're an enumerator, and
8661 our parent has no specification (meaning possibly lives in a
8662 namespace elsewhere), then we can add the partial symbol now
8663 instead of queueing it. */
8664 if (part_die->tag == DW_TAG_enumerator
8665 && parent_die != NULL
8666 && parent_die->die_parent == NULL
8667 && parent_die->tag == DW_TAG_enumeration_type
8668 && parent_die->has_specification == 0)
8669 {
8670 if (part_die->name == NULL)
e2e0b3e5 8671 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8672 else if (building_psymtab)
04a679b8 8673 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8674 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8675 (cu->language == language_cplus
8676 || cu->language == language_java)
72bf9492
DJ
8677 ? &cu->objfile->global_psymbols
8678 : &cu->objfile->static_psymbols,
8679 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8680
93311388 8681 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8682 continue;
8683 }
8684
8685 /* We'll save this DIE so link it in. */
8686 part_die->die_parent = parent_die;
8687 part_die->die_sibling = NULL;
8688 part_die->die_child = NULL;
8689
8690 if (last_die && last_die == parent_die)
8691 last_die->die_child = part_die;
8692 else if (last_die)
8693 last_die->die_sibling = part_die;
8694
8695 last_die = part_die;
8696
8697 if (first_die == NULL)
8698 first_die = part_die;
8699
8700 /* Maybe add the DIE to the hash table. Not all DIEs that we
8701 find interesting need to be in the hash table, because we
8702 also have the parent/sibling/child chains; only those that we
8703 might refer to by offset later during partial symbol reading.
8704
8705 For now this means things that might have be the target of a
8706 DW_AT_specification, DW_AT_abstract_origin, or
8707 DW_AT_extension. DW_AT_extension will refer only to
8708 namespaces; DW_AT_abstract_origin refers to functions (and
8709 many things under the function DIE, but we do not recurse
8710 into function DIEs during partial symbol reading) and
8711 possibly variables as well; DW_AT_specification refers to
8712 declarations. Declarations ought to have the DW_AT_declaration
8713 flag. It happens that GCC forgets to put it in sometimes, but
8714 only for functions, not for types.
8715
8716 Adding more things than necessary to the hash table is harmless
8717 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8718 wasted time in find_partial_die, when we reread the compilation
8719 unit with load_all_dies set. */
72bf9492 8720
5afb4e99 8721 if (load_all
72929c62 8722 || abbrev->tag == DW_TAG_constant
5afb4e99 8723 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8724 || abbrev->tag == DW_TAG_variable
8725 || abbrev->tag == DW_TAG_namespace
8726 || part_die->is_declaration)
8727 {
8728 void **slot;
8729
8730 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8731 part_die->offset, INSERT);
8732 *slot = part_die;
8733 }
8734
8735 part_die = obstack_alloc (&cu->comp_unit_obstack,
8736 sizeof (struct partial_die_info));
8737
8738 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8739 we have no reason to follow the children of structures; for other
98bfdba5
PA
8740 languages we have to, so that we can get at method physnames
8741 to infer fully qualified class names, for DW_AT_specification,
8742 and for C++ template arguments. For C++, we also look one level
8743 inside functions to find template arguments (if the name of the
8744 function does not already contain the template arguments).
bc30ff58
JB
8745
8746 For Ada, we need to scan the children of subprograms and lexical
8747 blocks as well because Ada allows the definition of nested
8748 entities that could be interesting for the debugger, such as
8749 nested subprograms for instance. */
72bf9492 8750 if (last_die->has_children
5afb4e99
DJ
8751 && (load_all
8752 || last_die->tag == DW_TAG_namespace
f55ee35c 8753 || last_die->tag == DW_TAG_module
72bf9492 8754 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8755 || (cu->language == language_cplus
8756 && last_die->tag == DW_TAG_subprogram
8757 && (last_die->name == NULL
8758 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8759 || (cu->language != language_c
8760 && (last_die->tag == DW_TAG_class_type
680b30c7 8761 || last_die->tag == DW_TAG_interface_type
72bf9492 8762 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8763 || last_die->tag == DW_TAG_union_type))
8764 || (cu->language == language_ada
8765 && (last_die->tag == DW_TAG_subprogram
8766 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8767 {
8768 nesting_level++;
8769 parent_die = last_die;
8770 continue;
8771 }
8772
8773 /* Otherwise we skip to the next sibling, if any. */
93311388 8774 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8775
8776 /* Back to the top, do it again. */
8777 }
8778}
8779
c906108c
SS
8780/* Read a minimal amount of information into the minimal die structure. */
8781
fe1b8b76 8782static gdb_byte *
72bf9492
DJ
8783read_partial_die (struct partial_die_info *part_die,
8784 struct abbrev_info *abbrev,
8785 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8786 gdb_byte *buffer, gdb_byte *info_ptr,
8787 struct dwarf2_cu *cu)
c906108c 8788{
fa238c03 8789 unsigned int i;
c906108c 8790 struct attribute attr;
c5aa993b 8791 int has_low_pc_attr = 0;
c906108c
SS
8792 int has_high_pc_attr = 0;
8793
72bf9492 8794 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8795
93311388 8796 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8797
8798 info_ptr += abbrev_len;
8799
8800 if (abbrev == NULL)
8801 return info_ptr;
8802
c906108c
SS
8803 part_die->tag = abbrev->tag;
8804 part_die->has_children = abbrev->has_children;
c906108c
SS
8805
8806 for (i = 0; i < abbrev->num_attrs; ++i)
8807 {
e7c27a73 8808 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8809
8810 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8811 partial symbol table. */
c906108c
SS
8812 switch (attr.name)
8813 {
8814 case DW_AT_name:
71c25dea
TT
8815 switch (part_die->tag)
8816 {
8817 case DW_TAG_compile_unit:
348e048f 8818 case DW_TAG_type_unit:
71c25dea
TT
8819 /* Compilation units have a DW_AT_name that is a filename, not
8820 a source language identifier. */
8821 case DW_TAG_enumeration_type:
8822 case DW_TAG_enumerator:
8823 /* These tags always have simple identifiers already; no need
8824 to canonicalize them. */
8825 part_die->name = DW_STRING (&attr);
8826 break;
8827 default:
8828 part_die->name
8829 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8830 &cu->objfile->objfile_obstack);
71c25dea
TT
8831 break;
8832 }
c906108c 8833 break;
31ef98ae 8834 case DW_AT_linkage_name:
c906108c 8835 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8836 /* Note that both forms of linkage name might appear. We
8837 assume they will be the same, and we only store the last
8838 one we see. */
94af9270
KS
8839 if (cu->language == language_ada)
8840 part_die->name = DW_STRING (&attr);
abc72ce4 8841 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8842 break;
8843 case DW_AT_low_pc:
8844 has_low_pc_attr = 1;
8845 part_die->lowpc = DW_ADDR (&attr);
8846 break;
8847 case DW_AT_high_pc:
8848 has_high_pc_attr = 1;
8849 part_die->highpc = DW_ADDR (&attr);
8850 break;
8851 case DW_AT_location:
8e19ed76
PS
8852 /* Support the .debug_loc offsets */
8853 if (attr_form_is_block (&attr))
8854 {
8855 part_die->locdesc = DW_BLOCK (&attr);
8856 }
3690dd37 8857 else if (attr_form_is_section_offset (&attr))
8e19ed76 8858 {
4d3c2250 8859 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8860 }
8861 else
8862 {
4d3c2250
KB
8863 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8864 "partial symbol information");
8e19ed76 8865 }
c906108c 8866 break;
c906108c
SS
8867 case DW_AT_external:
8868 part_die->is_external = DW_UNSND (&attr);
8869 break;
8870 case DW_AT_declaration:
8871 part_die->is_declaration = DW_UNSND (&attr);
8872 break;
8873 case DW_AT_type:
8874 part_die->has_type = 1;
8875 break;
8876 case DW_AT_abstract_origin:
8877 case DW_AT_specification:
72bf9492
DJ
8878 case DW_AT_extension:
8879 part_die->has_specification = 1;
c764a876 8880 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8881 break;
8882 case DW_AT_sibling:
8883 /* Ignore absolute siblings, they might point outside of
8884 the current compile unit. */
8885 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8886 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8887 else
93311388 8888 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8889 break;
fa4028e9
JB
8890 case DW_AT_byte_size:
8891 part_die->has_byte_size = 1;
8892 break;
68511cec
CES
8893 case DW_AT_calling_convention:
8894 /* DWARF doesn't provide a way to identify a program's source-level
8895 entry point. DW_AT_calling_convention attributes are only meant
8896 to describe functions' calling conventions.
8897
8898 However, because it's a necessary piece of information in
8899 Fortran, and because DW_CC_program is the only piece of debugging
8900 information whose definition refers to a 'main program' at all,
8901 several compilers have begun marking Fortran main programs with
8902 DW_CC_program --- even when those functions use the standard
8903 calling conventions.
8904
8905 So until DWARF specifies a way to provide this information and
8906 compilers pick up the new representation, we'll support this
8907 practice. */
8908 if (DW_UNSND (&attr) == DW_CC_program
8909 && cu->language == language_fortran)
01f8c46d
JK
8910 {
8911 set_main_name (part_die->name);
8912
8913 /* As this DIE has a static linkage the name would be difficult
8914 to look up later. */
8915 language_of_main = language_fortran;
8916 }
68511cec 8917 break;
c906108c
SS
8918 default:
8919 break;
8920 }
8921 }
8922
c906108c
SS
8923 /* When using the GNU linker, .gnu.linkonce. sections are used to
8924 eliminate duplicate copies of functions and vtables and such.
8925 The linker will arbitrarily choose one and discard the others.
8926 The AT_*_pc values for such functions refer to local labels in
8927 these sections. If the section from that file was discarded, the
8928 labels are not in the output, so the relocs get a value of 0.
8929 If this is a discarded function, mark the pc bounds as invalid,
8930 so that GDB will ignore it. */
8931 if (has_low_pc_attr && has_high_pc_attr
8932 && part_die->lowpc < part_die->highpc
8933 && (part_die->lowpc != 0
72dca2f5 8934 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8935 part_die->has_pc_info = 1;
85cbf3d3 8936
c906108c
SS
8937 return info_ptr;
8938}
8939
72bf9492
DJ
8940/* Find a cached partial DIE at OFFSET in CU. */
8941
8942static struct partial_die_info *
c764a876 8943find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8944{
8945 struct partial_die_info *lookup_die = NULL;
8946 struct partial_die_info part_die;
8947
8948 part_die.offset = offset;
8949 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8950
72bf9492
DJ
8951 return lookup_die;
8952}
8953
348e048f
DE
8954/* Find a partial DIE at OFFSET, which may or may not be in CU,
8955 except in the case of .debug_types DIEs which do not reference
8956 outside their CU (they do however referencing other types via
8957 DW_FORM_sig8). */
72bf9492
DJ
8958
8959static struct partial_die_info *
c764a876 8960find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8961{
5afb4e99
DJ
8962 struct dwarf2_per_cu_data *per_cu = NULL;
8963 struct partial_die_info *pd = NULL;
72bf9492 8964
348e048f
DE
8965 if (cu->per_cu->from_debug_types)
8966 {
8967 pd = find_partial_die_in_comp_unit (offset, cu);
8968 if (pd != NULL)
8969 return pd;
8970 goto not_found;
8971 }
8972
45452591 8973 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8974 {
8975 pd = find_partial_die_in_comp_unit (offset, cu);
8976 if (pd != NULL)
8977 return pd;
8978 }
72bf9492 8979
ae038cb0
DJ
8980 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8981
98bfdba5
PA
8982 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8983 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8984
8985 per_cu->cu->last_used = 0;
5afb4e99
DJ
8986 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8987
8988 if (pd == NULL && per_cu->load_all_dies == 0)
8989 {
8990 struct cleanup *back_to;
8991 struct partial_die_info comp_unit_die;
8992 struct abbrev_info *abbrev;
8993 unsigned int bytes_read;
8994 char *info_ptr;
8995
8996 per_cu->load_all_dies = 1;
8997
8998 /* Re-read the DIEs. */
8999 back_to = make_cleanup (null_cleanup, 0);
9000 if (per_cu->cu->dwarf2_abbrevs == NULL)
9001 {
9002 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9003 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9004 }
dce234bc 9005 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9006 + per_cu->cu->header.offset
9007 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9008 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9009 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9010 per_cu->cu->objfile->obfd,
9011 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9012 per_cu->cu);
9013 if (comp_unit_die.has_children)
93311388
DE
9014 load_partial_dies (per_cu->cu->objfile->obfd,
9015 dwarf2_per_objfile->info.buffer, info_ptr,
9016 0, per_cu->cu);
5afb4e99
DJ
9017 do_cleanups (back_to);
9018
9019 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9020 }
9021
348e048f
DE
9022 not_found:
9023
5afb4e99
DJ
9024 if (pd == NULL)
9025 internal_error (__FILE__, __LINE__,
c764a876 9026 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
9027 offset, bfd_get_filename (cu->objfile->obfd));
9028 return pd;
72bf9492
DJ
9029}
9030
abc72ce4
DE
9031/* See if we can figure out if the class lives in a namespace. We do
9032 this by looking for a member function; its demangled name will
9033 contain namespace info, if there is any. */
9034
9035static void
9036guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9037 struct dwarf2_cu *cu)
9038{
9039 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9040 what template types look like, because the demangler
9041 frequently doesn't give the same name as the debug info. We
9042 could fix this by only using the demangled name to get the
9043 prefix (but see comment in read_structure_type). */
9044
9045 struct partial_die_info *real_pdi;
9046 struct partial_die_info *child_pdi;
9047
9048 /* If this DIE (this DIE's specification, if any) has a parent, then
9049 we should not do this. We'll prepend the parent's fully qualified
9050 name when we create the partial symbol. */
9051
9052 real_pdi = struct_pdi;
9053 while (real_pdi->has_specification)
9054 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9055
9056 if (real_pdi->die_parent != NULL)
9057 return;
9058
9059 for (child_pdi = struct_pdi->die_child;
9060 child_pdi != NULL;
9061 child_pdi = child_pdi->die_sibling)
9062 {
9063 if (child_pdi->tag == DW_TAG_subprogram
9064 && child_pdi->linkage_name != NULL)
9065 {
9066 char *actual_class_name
9067 = language_class_name_from_physname (cu->language_defn,
9068 child_pdi->linkage_name);
9069 if (actual_class_name != NULL)
9070 {
9071 struct_pdi->name
9072 = obsavestring (actual_class_name,
9073 strlen (actual_class_name),
9074 &cu->objfile->objfile_obstack);
9075 xfree (actual_class_name);
9076 }
9077 break;
9078 }
9079 }
9080}
9081
72bf9492
DJ
9082/* Adjust PART_DIE before generating a symbol for it. This function
9083 may set the is_external flag or change the DIE's name. */
9084
9085static void
9086fixup_partial_die (struct partial_die_info *part_die,
9087 struct dwarf2_cu *cu)
9088{
abc72ce4
DE
9089 /* Once we've fixed up a die, there's no point in doing so again.
9090 This also avoids a memory leak if we were to call
9091 guess_partial_die_structure_name multiple times. */
9092 if (part_die->fixup_called)
9093 return;
9094
72bf9492
DJ
9095 /* If we found a reference attribute and the DIE has no name, try
9096 to find a name in the referred to DIE. */
9097
9098 if (part_die->name == NULL && part_die->has_specification)
9099 {
9100 struct partial_die_info *spec_die;
72bf9492 9101
10b3939b 9102 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9103
10b3939b 9104 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9105
9106 if (spec_die->name)
9107 {
9108 part_die->name = spec_die->name;
9109
9110 /* Copy DW_AT_external attribute if it is set. */
9111 if (spec_die->is_external)
9112 part_die->is_external = spec_die->is_external;
9113 }
9114 }
9115
9116 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9117
9118 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9119 part_die->name = "(anonymous namespace)";
9120
abc72ce4
DE
9121 /* If there is no parent die to provide a namespace, and there are
9122 children, see if we can determine the namespace from their linkage
9123 name.
9124 NOTE: We need to do this even if cu->has_namespace_info != 0.
9125 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9126 if (cu->language == language_cplus
9127 && dwarf2_per_objfile->types.asection != NULL
9128 && part_die->die_parent == NULL
9129 && part_die->has_children
9130 && (part_die->tag == DW_TAG_class_type
9131 || part_die->tag == DW_TAG_structure_type
9132 || part_die->tag == DW_TAG_union_type))
9133 guess_partial_die_structure_name (part_die, cu);
9134
9135 part_die->fixup_called = 1;
72bf9492
DJ
9136}
9137
a8329558 9138/* Read an attribute value described by an attribute form. */
c906108c 9139
fe1b8b76 9140static gdb_byte *
a8329558 9141read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9142 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9143 struct dwarf2_cu *cu)
c906108c 9144{
e7c27a73 9145 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9146 unsigned int bytes_read;
9147 struct dwarf_block *blk;
9148
a8329558
KW
9149 attr->form = form;
9150 switch (form)
c906108c 9151 {
c906108c 9152 case DW_FORM_ref_addr:
ae411497
TT
9153 if (cu->header.version == 2)
9154 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9155 else
9156 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9157 info_ptr += bytes_read;
9158 break;
9159 case DW_FORM_addr:
e7c27a73 9160 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9161 info_ptr += bytes_read;
c906108c
SS
9162 break;
9163 case DW_FORM_block2:
7b5a2f43 9164 blk = dwarf_alloc_block (cu);
c906108c
SS
9165 blk->size = read_2_bytes (abfd, info_ptr);
9166 info_ptr += 2;
9167 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9168 info_ptr += blk->size;
9169 DW_BLOCK (attr) = blk;
9170 break;
9171 case DW_FORM_block4:
7b5a2f43 9172 blk = dwarf_alloc_block (cu);
c906108c
SS
9173 blk->size = read_4_bytes (abfd, info_ptr);
9174 info_ptr += 4;
9175 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9176 info_ptr += blk->size;
9177 DW_BLOCK (attr) = blk;
9178 break;
9179 case DW_FORM_data2:
9180 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9181 info_ptr += 2;
9182 break;
9183 case DW_FORM_data4:
9184 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9185 info_ptr += 4;
9186 break;
9187 case DW_FORM_data8:
9188 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9189 info_ptr += 8;
9190 break;
2dc7f7b3
TT
9191 case DW_FORM_sec_offset:
9192 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9193 info_ptr += bytes_read;
9194 break;
c906108c 9195 case DW_FORM_string:
9b1c24c8 9196 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9197 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9198 info_ptr += bytes_read;
9199 break;
4bdf3d34
JJ
9200 case DW_FORM_strp:
9201 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9202 &bytes_read);
8285870a 9203 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9204 info_ptr += bytes_read;
9205 break;
2dc7f7b3 9206 case DW_FORM_exprloc:
c906108c 9207 case DW_FORM_block:
7b5a2f43 9208 blk = dwarf_alloc_block (cu);
c906108c
SS
9209 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9210 info_ptr += bytes_read;
9211 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9212 info_ptr += blk->size;
9213 DW_BLOCK (attr) = blk;
9214 break;
9215 case DW_FORM_block1:
7b5a2f43 9216 blk = dwarf_alloc_block (cu);
c906108c
SS
9217 blk->size = read_1_byte (abfd, info_ptr);
9218 info_ptr += 1;
9219 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9220 info_ptr += blk->size;
9221 DW_BLOCK (attr) = blk;
9222 break;
9223 case DW_FORM_data1:
9224 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9225 info_ptr += 1;
9226 break;
9227 case DW_FORM_flag:
9228 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9229 info_ptr += 1;
9230 break;
2dc7f7b3
TT
9231 case DW_FORM_flag_present:
9232 DW_UNSND (attr) = 1;
9233 break;
c906108c
SS
9234 case DW_FORM_sdata:
9235 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9236 info_ptr += bytes_read;
9237 break;
9238 case DW_FORM_udata:
9239 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9240 info_ptr += bytes_read;
9241 break;
9242 case DW_FORM_ref1:
10b3939b 9243 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9244 info_ptr += 1;
9245 break;
9246 case DW_FORM_ref2:
10b3939b 9247 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9248 info_ptr += 2;
9249 break;
9250 case DW_FORM_ref4:
10b3939b 9251 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9252 info_ptr += 4;
9253 break;
613e1657 9254 case DW_FORM_ref8:
10b3939b 9255 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9256 info_ptr += 8;
9257 break;
348e048f
DE
9258 case DW_FORM_sig8:
9259 /* Convert the signature to something we can record in DW_UNSND
9260 for later lookup.
9261 NOTE: This is NULL if the type wasn't found. */
9262 DW_SIGNATURED_TYPE (attr) =
9263 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9264 info_ptr += 8;
9265 break;
c906108c 9266 case DW_FORM_ref_udata:
10b3939b
DJ
9267 DW_ADDR (attr) = (cu->header.offset
9268 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9269 info_ptr += bytes_read;
9270 break;
c906108c 9271 case DW_FORM_indirect:
a8329558
KW
9272 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9273 info_ptr += bytes_read;
e7c27a73 9274 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9275 break;
c906108c 9276 default:
8a3fe4f8 9277 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9278 dwarf_form_name (form),
9279 bfd_get_filename (abfd));
c906108c 9280 }
28e94949
JB
9281
9282 /* We have seen instances where the compiler tried to emit a byte
9283 size attribute of -1 which ended up being encoded as an unsigned
9284 0xffffffff. Although 0xffffffff is technically a valid size value,
9285 an object of this size seems pretty unlikely so we can relatively
9286 safely treat these cases as if the size attribute was invalid and
9287 treat them as zero by default. */
9288 if (attr->name == DW_AT_byte_size
9289 && form == DW_FORM_data4
9290 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9291 {
9292 complaint
9293 (&symfile_complaints,
43bbcdc2
PH
9294 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9295 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9296 DW_UNSND (attr) = 0;
9297 }
28e94949 9298
c906108c
SS
9299 return info_ptr;
9300}
9301
a8329558
KW
9302/* Read an attribute described by an abbreviated attribute. */
9303
fe1b8b76 9304static gdb_byte *
a8329558 9305read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9306 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9307{
9308 attr->name = abbrev->name;
e7c27a73 9309 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9310}
9311
c906108c
SS
9312/* read dwarf information from a buffer */
9313
9314static unsigned int
fe1b8b76 9315read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9316{
fe1b8b76 9317 return bfd_get_8 (abfd, buf);
c906108c
SS
9318}
9319
9320static int
fe1b8b76 9321read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9322{
fe1b8b76 9323 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9324}
9325
9326static unsigned int
fe1b8b76 9327read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9328{
fe1b8b76 9329 return bfd_get_16 (abfd, buf);
c906108c
SS
9330}
9331
9332static int
fe1b8b76 9333read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9334{
fe1b8b76 9335 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9336}
9337
9338static unsigned int
fe1b8b76 9339read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9340{
fe1b8b76 9341 return bfd_get_32 (abfd, buf);
c906108c
SS
9342}
9343
9344static int
fe1b8b76 9345read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9346{
fe1b8b76 9347 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9348}
9349
93311388 9350static ULONGEST
fe1b8b76 9351read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9352{
fe1b8b76 9353 return bfd_get_64 (abfd, buf);
c906108c
SS
9354}
9355
9356static CORE_ADDR
fe1b8b76 9357read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9358 unsigned int *bytes_read)
c906108c 9359{
e7c27a73 9360 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9361 CORE_ADDR retval = 0;
9362
107d2387 9363 if (cu_header->signed_addr_p)
c906108c 9364 {
107d2387
AC
9365 switch (cu_header->addr_size)
9366 {
9367 case 2:
fe1b8b76 9368 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9369 break;
9370 case 4:
fe1b8b76 9371 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9372 break;
9373 case 8:
fe1b8b76 9374 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9375 break;
9376 default:
8e65ff28 9377 internal_error (__FILE__, __LINE__,
e2e0b3e5 9378 _("read_address: bad switch, signed [in module %s]"),
659b0389 9379 bfd_get_filename (abfd));
107d2387
AC
9380 }
9381 }
9382 else
9383 {
9384 switch (cu_header->addr_size)
9385 {
9386 case 2:
fe1b8b76 9387 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9388 break;
9389 case 4:
fe1b8b76 9390 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9391 break;
9392 case 8:
fe1b8b76 9393 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9394 break;
9395 default:
8e65ff28 9396 internal_error (__FILE__, __LINE__,
e2e0b3e5 9397 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9398 bfd_get_filename (abfd));
107d2387 9399 }
c906108c 9400 }
64367e0a 9401
107d2387
AC
9402 *bytes_read = cu_header->addr_size;
9403 return retval;
c906108c
SS
9404}
9405
f7ef9339 9406/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9407 specification allows the initial length to take up either 4 bytes
9408 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9409 bytes describe the length and all offsets will be 8 bytes in length
9410 instead of 4.
9411
f7ef9339
KB
9412 An older, non-standard 64-bit format is also handled by this
9413 function. The older format in question stores the initial length
9414 as an 8-byte quantity without an escape value. Lengths greater
9415 than 2^32 aren't very common which means that the initial 4 bytes
9416 is almost always zero. Since a length value of zero doesn't make
9417 sense for the 32-bit format, this initial zero can be considered to
9418 be an escape value which indicates the presence of the older 64-bit
9419 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9420 greater than 4GB. If it becomes necessary to handle lengths
9421 somewhat larger than 4GB, we could allow other small values (such
9422 as the non-sensical values of 1, 2, and 3) to also be used as
9423 escape values indicating the presence of the old format.
f7ef9339 9424
917c78fc
MK
9425 The value returned via bytes_read should be used to increment the
9426 relevant pointer after calling read_initial_length().
c764a876 9427
613e1657
KB
9428 [ Note: read_initial_length() and read_offset() are based on the
9429 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9430 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9431 from:
9432
f7ef9339 9433 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9434
613e1657
KB
9435 This document is only a draft and is subject to change. (So beware.)
9436
f7ef9339 9437 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9438 determined empirically by examining 64-bit ELF files produced by
9439 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9440
9441 - Kevin, July 16, 2002
613e1657
KB
9442 ] */
9443
9444static LONGEST
c764a876 9445read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9446{
fe1b8b76 9447 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9448
dd373385 9449 if (length == 0xffffffff)
613e1657 9450 {
fe1b8b76 9451 length = bfd_get_64 (abfd, buf + 4);
613e1657 9452 *bytes_read = 12;
613e1657 9453 }
dd373385 9454 else if (length == 0)
f7ef9339 9455 {
dd373385 9456 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9457 length = bfd_get_64 (abfd, buf);
f7ef9339 9458 *bytes_read = 8;
f7ef9339 9459 }
613e1657
KB
9460 else
9461 {
9462 *bytes_read = 4;
613e1657
KB
9463 }
9464
c764a876
DE
9465 return length;
9466}
dd373385 9467
c764a876
DE
9468/* Cover function for read_initial_length.
9469 Returns the length of the object at BUF, and stores the size of the
9470 initial length in *BYTES_READ and stores the size that offsets will be in
9471 *OFFSET_SIZE.
9472 If the initial length size is not equivalent to that specified in
9473 CU_HEADER then issue a complaint.
9474 This is useful when reading non-comp-unit headers. */
dd373385 9475
c764a876
DE
9476static LONGEST
9477read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9478 const struct comp_unit_head *cu_header,
9479 unsigned int *bytes_read,
9480 unsigned int *offset_size)
9481{
9482 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9483
9484 gdb_assert (cu_header->initial_length_size == 4
9485 || cu_header->initial_length_size == 8
9486 || cu_header->initial_length_size == 12);
9487
9488 if (cu_header->initial_length_size != *bytes_read)
9489 complaint (&symfile_complaints,
9490 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9491
c764a876 9492 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9493 return length;
613e1657
KB
9494}
9495
9496/* Read an offset from the data stream. The size of the offset is
917c78fc 9497 given by cu_header->offset_size. */
613e1657
KB
9498
9499static LONGEST
fe1b8b76 9500read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9501 unsigned int *bytes_read)
c764a876
DE
9502{
9503 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9504
c764a876
DE
9505 *bytes_read = cu_header->offset_size;
9506 return offset;
9507}
9508
9509/* Read an offset from the data stream. */
9510
9511static LONGEST
9512read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9513{
9514 LONGEST retval = 0;
9515
c764a876 9516 switch (offset_size)
613e1657
KB
9517 {
9518 case 4:
fe1b8b76 9519 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9520 break;
9521 case 8:
fe1b8b76 9522 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9523 break;
9524 default:
8e65ff28 9525 internal_error (__FILE__, __LINE__,
c764a876 9526 _("read_offset_1: bad switch [in module %s]"),
659b0389 9527 bfd_get_filename (abfd));
613e1657
KB
9528 }
9529
917c78fc 9530 return retval;
613e1657
KB
9531}
9532
fe1b8b76
JB
9533static gdb_byte *
9534read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9535{
9536 /* If the size of a host char is 8 bits, we can return a pointer
9537 to the buffer, otherwise we have to copy the data to a buffer
9538 allocated on the temporary obstack. */
4bdf3d34 9539 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9540 return buf;
c906108c
SS
9541}
9542
9543static char *
9b1c24c8 9544read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9545{
9546 /* If the size of a host char is 8 bits, we can return a pointer
9547 to the string, otherwise we have to copy the string to a buffer
9548 allocated on the temporary obstack. */
4bdf3d34 9549 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9550 if (*buf == '\0')
9551 {
9552 *bytes_read_ptr = 1;
9553 return NULL;
9554 }
fe1b8b76
JB
9555 *bytes_read_ptr = strlen ((char *) buf) + 1;
9556 return (char *) buf;
4bdf3d34
JJ
9557}
9558
9559static char *
fe1b8b76 9560read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9561 const struct comp_unit_head *cu_header,
9562 unsigned int *bytes_read_ptr)
9563{
c764a876 9564 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9565
be391dca 9566 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9567 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9568 {
8a3fe4f8 9569 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9570 bfd_get_filename (abfd));
4bdf3d34 9571 return NULL;
c906108c 9572 }
dce234bc 9573 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9574 {
8a3fe4f8 9575 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9576 bfd_get_filename (abfd));
c906108c
SS
9577 return NULL;
9578 }
4bdf3d34 9579 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9580 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9581 return NULL;
dce234bc 9582 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9583}
9584
ce5d95e1 9585static unsigned long
fe1b8b76 9586read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9587{
ce5d95e1
JB
9588 unsigned long result;
9589 unsigned int num_read;
c906108c
SS
9590 int i, shift;
9591 unsigned char byte;
9592
9593 result = 0;
9594 shift = 0;
9595 num_read = 0;
9596 i = 0;
9597 while (1)
9598 {
fe1b8b76 9599 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9600 buf++;
9601 num_read++;
ce5d95e1 9602 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9603 if ((byte & 128) == 0)
9604 {
9605 break;
9606 }
9607 shift += 7;
9608 }
9609 *bytes_read_ptr = num_read;
9610 return result;
9611}
9612
ce5d95e1 9613static long
fe1b8b76 9614read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9615{
ce5d95e1 9616 long result;
77e0b926 9617 int i, shift, num_read;
c906108c
SS
9618 unsigned char byte;
9619
9620 result = 0;
9621 shift = 0;
c906108c
SS
9622 num_read = 0;
9623 i = 0;
9624 while (1)
9625 {
fe1b8b76 9626 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9627 buf++;
9628 num_read++;
ce5d95e1 9629 result |= ((long)(byte & 127) << shift);
c906108c
SS
9630 shift += 7;
9631 if ((byte & 128) == 0)
9632 {
9633 break;
9634 }
9635 }
77e0b926
DJ
9636 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9637 result |= -(((long)1) << shift);
c906108c
SS
9638 *bytes_read_ptr = num_read;
9639 return result;
9640}
9641
4bb7a0a7
DJ
9642/* Return a pointer to just past the end of an LEB128 number in BUF. */
9643
fe1b8b76
JB
9644static gdb_byte *
9645skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9646{
9647 int byte;
9648
9649 while (1)
9650 {
fe1b8b76 9651 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9652 buf++;
9653 if ((byte & 128) == 0)
9654 return buf;
9655 }
9656}
9657
c906108c 9658static void
e142c38c 9659set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9660{
9661 switch (lang)
9662 {
9663 case DW_LANG_C89:
76bee0cc 9664 case DW_LANG_C99:
c906108c 9665 case DW_LANG_C:
e142c38c 9666 cu->language = language_c;
c906108c
SS
9667 break;
9668 case DW_LANG_C_plus_plus:
e142c38c 9669 cu->language = language_cplus;
c906108c 9670 break;
6aecb9c2
JB
9671 case DW_LANG_D:
9672 cu->language = language_d;
9673 break;
c906108c
SS
9674 case DW_LANG_Fortran77:
9675 case DW_LANG_Fortran90:
b21b22e0 9676 case DW_LANG_Fortran95:
e142c38c 9677 cu->language = language_fortran;
c906108c
SS
9678 break;
9679 case DW_LANG_Mips_Assembler:
e142c38c 9680 cu->language = language_asm;
c906108c 9681 break;
bebd888e 9682 case DW_LANG_Java:
e142c38c 9683 cu->language = language_java;
bebd888e 9684 break;
c906108c 9685 case DW_LANG_Ada83:
8aaf0b47 9686 case DW_LANG_Ada95:
bc5f45f8
JB
9687 cu->language = language_ada;
9688 break;
72019c9c
GM
9689 case DW_LANG_Modula2:
9690 cu->language = language_m2;
9691 break;
fe8e67fd
PM
9692 case DW_LANG_Pascal83:
9693 cu->language = language_pascal;
9694 break;
22566fbd
DJ
9695 case DW_LANG_ObjC:
9696 cu->language = language_objc;
9697 break;
c906108c
SS
9698 case DW_LANG_Cobol74:
9699 case DW_LANG_Cobol85:
c906108c 9700 default:
e142c38c 9701 cu->language = language_minimal;
c906108c
SS
9702 break;
9703 }
e142c38c 9704 cu->language_defn = language_def (cu->language);
c906108c
SS
9705}
9706
9707/* Return the named attribute or NULL if not there. */
9708
9709static struct attribute *
e142c38c 9710dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9711{
9712 unsigned int i;
9713 struct attribute *spec = NULL;
9714
9715 for (i = 0; i < die->num_attrs; ++i)
9716 {
9717 if (die->attrs[i].name == name)
10b3939b 9718 return &die->attrs[i];
c906108c
SS
9719 if (die->attrs[i].name == DW_AT_specification
9720 || die->attrs[i].name == DW_AT_abstract_origin)
9721 spec = &die->attrs[i];
9722 }
c906108c 9723
10b3939b 9724 if (spec)
f2f0e013
DJ
9725 {
9726 die = follow_die_ref (die, spec, &cu);
9727 return dwarf2_attr (die, name, cu);
9728 }
c5aa993b 9729
c906108c
SS
9730 return NULL;
9731}
9732
348e048f
DE
9733/* Return the named attribute or NULL if not there,
9734 but do not follow DW_AT_specification, etc.
9735 This is for use in contexts where we're reading .debug_types dies.
9736 Following DW_AT_specification, DW_AT_abstract_origin will take us
9737 back up the chain, and we want to go down. */
9738
9739static struct attribute *
9740dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9741 struct dwarf2_cu *cu)
9742{
9743 unsigned int i;
9744
9745 for (i = 0; i < die->num_attrs; ++i)
9746 if (die->attrs[i].name == name)
9747 return &die->attrs[i];
9748
9749 return NULL;
9750}
9751
05cf31d1
JB
9752/* Return non-zero iff the attribute NAME is defined for the given DIE,
9753 and holds a non-zero value. This function should only be used for
2dc7f7b3 9754 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9755
9756static int
9757dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9758{
9759 struct attribute *attr = dwarf2_attr (die, name, cu);
9760
9761 return (attr && DW_UNSND (attr));
9762}
9763
3ca72b44 9764static int
e142c38c 9765die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9766{
05cf31d1
JB
9767 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9768 which value is non-zero. However, we have to be careful with
9769 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9770 (via dwarf2_flag_true_p) follows this attribute. So we may
9771 end up accidently finding a declaration attribute that belongs
9772 to a different DIE referenced by the specification attribute,
9773 even though the given DIE does not have a declaration attribute. */
9774 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9775 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9776}
9777
63d06c5c 9778/* Return the die giving the specification for DIE, if there is
f2f0e013 9779 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9780 containing the return value on output. If there is no
9781 specification, but there is an abstract origin, that is
9782 returned. */
63d06c5c
DC
9783
9784static struct die_info *
f2f0e013 9785die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9786{
f2f0e013
DJ
9787 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9788 *spec_cu);
63d06c5c 9789
edb3359d
DJ
9790 if (spec_attr == NULL)
9791 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9792
63d06c5c
DC
9793 if (spec_attr == NULL)
9794 return NULL;
9795 else
f2f0e013 9796 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9797}
c906108c 9798
debd256d 9799/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
9800 refers to.
9801 NOTE: This is also used as a "cleanup" function. */
9802
debd256d
JB
9803static void
9804free_line_header (struct line_header *lh)
9805{
9806 if (lh->standard_opcode_lengths)
a8bc7b56 9807 xfree (lh->standard_opcode_lengths);
debd256d
JB
9808
9809 /* Remember that all the lh->file_names[i].name pointers are
9810 pointers into debug_line_buffer, and don't need to be freed. */
9811 if (lh->file_names)
a8bc7b56 9812 xfree (lh->file_names);
debd256d
JB
9813
9814 /* Similarly for the include directory names. */
9815 if (lh->include_dirs)
a8bc7b56 9816 xfree (lh->include_dirs);
debd256d 9817
a8bc7b56 9818 xfree (lh);
debd256d
JB
9819}
9820
debd256d 9821/* Add an entry to LH's include directory table. */
ae2de4f8 9822
debd256d
JB
9823static void
9824add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9825{
debd256d
JB
9826 /* Grow the array if necessary. */
9827 if (lh->include_dirs_size == 0)
c5aa993b 9828 {
debd256d
JB
9829 lh->include_dirs_size = 1; /* for testing */
9830 lh->include_dirs = xmalloc (lh->include_dirs_size
9831 * sizeof (*lh->include_dirs));
9832 }
9833 else if (lh->num_include_dirs >= lh->include_dirs_size)
9834 {
9835 lh->include_dirs_size *= 2;
9836 lh->include_dirs = xrealloc (lh->include_dirs,
9837 (lh->include_dirs_size
9838 * sizeof (*lh->include_dirs)));
c5aa993b 9839 }
c906108c 9840
debd256d
JB
9841 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9842}
6e70227d 9843
debd256d 9844/* Add an entry to LH's file name table. */
ae2de4f8 9845
debd256d
JB
9846static void
9847add_file_name (struct line_header *lh,
9848 char *name,
9849 unsigned int dir_index,
9850 unsigned int mod_time,
9851 unsigned int length)
9852{
9853 struct file_entry *fe;
9854
9855 /* Grow the array if necessary. */
9856 if (lh->file_names_size == 0)
9857 {
9858 lh->file_names_size = 1; /* for testing */
9859 lh->file_names = xmalloc (lh->file_names_size
9860 * sizeof (*lh->file_names));
9861 }
9862 else if (lh->num_file_names >= lh->file_names_size)
9863 {
9864 lh->file_names_size *= 2;
9865 lh->file_names = xrealloc (lh->file_names,
9866 (lh->file_names_size
9867 * sizeof (*lh->file_names)));
9868 }
9869
9870 fe = &lh->file_names[lh->num_file_names++];
9871 fe->name = name;
9872 fe->dir_index = dir_index;
9873 fe->mod_time = mod_time;
9874 fe->length = length;
aaa75496 9875 fe->included_p = 0;
cb1df416 9876 fe->symtab = NULL;
debd256d 9877}
6e70227d 9878
debd256d 9879/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9880 .debug_line, according to the endianness of ABFD. Return a pointer
9881 to a struct line_header, allocated using xmalloc.
debd256d
JB
9882
9883 NOTE: the strings in the include directory and file name tables of
9884 the returned object point into debug_line_buffer, and must not be
9885 freed. */
ae2de4f8 9886
debd256d
JB
9887static struct line_header *
9888dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9889 struct dwarf2_cu *cu)
debd256d
JB
9890{
9891 struct cleanup *back_to;
9892 struct line_header *lh;
fe1b8b76 9893 gdb_byte *line_ptr;
c764a876 9894 unsigned int bytes_read, offset_size;
debd256d
JB
9895 int i;
9896 char *cur_dir, *cur_file;
9897
be391dca 9898 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9899 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9900 {
e2e0b3e5 9901 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9902 return 0;
9903 }
9904
a738430d
MK
9905 /* Make sure that at least there's room for the total_length field.
9906 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9907 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9908 {
4d3c2250 9909 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9910 return 0;
9911 }
9912
9913 lh = xmalloc (sizeof (*lh));
9914 memset (lh, 0, sizeof (*lh));
9915 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9916 (void *) lh);
9917
dce234bc 9918 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9919
a738430d 9920 /* Read in the header. */
6e70227d 9921 lh->total_length =
c764a876
DE
9922 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9923 &bytes_read, &offset_size);
debd256d 9924 line_ptr += bytes_read;
dce234bc
PP
9925 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9926 + dwarf2_per_objfile->line.size))
debd256d 9927 {
4d3c2250 9928 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9929 return 0;
9930 }
9931 lh->statement_program_end = line_ptr + lh->total_length;
9932 lh->version = read_2_bytes (abfd, line_ptr);
9933 line_ptr += 2;
c764a876
DE
9934 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9935 line_ptr += offset_size;
debd256d
JB
9936 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9937 line_ptr += 1;
2dc7f7b3
TT
9938 if (lh->version >= 4)
9939 {
9940 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9941 line_ptr += 1;
9942 }
9943 else
9944 lh->maximum_ops_per_instruction = 1;
9945
9946 if (lh->maximum_ops_per_instruction == 0)
9947 {
9948 lh->maximum_ops_per_instruction = 1;
9949 complaint (&symfile_complaints,
9950 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9951 }
9952
debd256d
JB
9953 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9954 line_ptr += 1;
9955 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9956 line_ptr += 1;
9957 lh->line_range = read_1_byte (abfd, line_ptr);
9958 line_ptr += 1;
9959 lh->opcode_base = read_1_byte (abfd, line_ptr);
9960 line_ptr += 1;
9961 lh->standard_opcode_lengths
fe1b8b76 9962 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9963
9964 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9965 for (i = 1; i < lh->opcode_base; ++i)
9966 {
9967 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9968 line_ptr += 1;
9969 }
9970
a738430d 9971 /* Read directory table. */
9b1c24c8 9972 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9973 {
9974 line_ptr += bytes_read;
9975 add_include_dir (lh, cur_dir);
9976 }
9977 line_ptr += bytes_read;
9978
a738430d 9979 /* Read file name table. */
9b1c24c8 9980 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9981 {
9982 unsigned int dir_index, mod_time, length;
9983
9984 line_ptr += bytes_read;
9985 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9986 line_ptr += bytes_read;
9987 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9988 line_ptr += bytes_read;
9989 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9990 line_ptr += bytes_read;
9991
9992 add_file_name (lh, cur_file, dir_index, mod_time, length);
9993 }
9994 line_ptr += bytes_read;
6e70227d 9995 lh->statement_program_start = line_ptr;
debd256d 9996
dce234bc
PP
9997 if (line_ptr > (dwarf2_per_objfile->line.buffer
9998 + dwarf2_per_objfile->line.size))
4d3c2250 9999 complaint (&symfile_complaints,
e2e0b3e5 10000 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
10001
10002 discard_cleanups (back_to);
10003 return lh;
10004}
c906108c 10005
5fb290d7
DJ
10006/* This function exists to work around a bug in certain compilers
10007 (particularly GCC 2.95), in which the first line number marker of a
10008 function does not show up until after the prologue, right before
10009 the second line number marker. This function shifts ADDRESS down
10010 to the beginning of the function if necessary, and is called on
10011 addresses passed to record_line. */
10012
10013static CORE_ADDR
e142c38c 10014check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10015{
10016 struct function_range *fn;
10017
10018 /* Find the function_range containing address. */
e142c38c 10019 if (!cu->first_fn)
5fb290d7
DJ
10020 return address;
10021
e142c38c
DJ
10022 if (!cu->cached_fn)
10023 cu->cached_fn = cu->first_fn;
5fb290d7 10024
e142c38c 10025 fn = cu->cached_fn;
5fb290d7
DJ
10026 while (fn)
10027 if (fn->lowpc <= address && fn->highpc > address)
10028 goto found;
10029 else
10030 fn = fn->next;
10031
e142c38c
DJ
10032 fn = cu->first_fn;
10033 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10034 if (fn->lowpc <= address && fn->highpc > address)
10035 goto found;
10036 else
10037 fn = fn->next;
10038
10039 return address;
10040
10041 found:
10042 if (fn->seen_line)
10043 return address;
10044 if (address != fn->lowpc)
4d3c2250 10045 complaint (&symfile_complaints,
e2e0b3e5 10046 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10047 (unsigned long) address, fn->name);
5fb290d7
DJ
10048 fn->seen_line = 1;
10049 return fn->lowpc;
10050}
10051
c6da4cef
DE
10052/* Subroutine of dwarf_decode_lines to simplify it.
10053 Return the file name of the psymtab for included file FILE_INDEX
10054 in line header LH of PST.
10055 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10056 If space for the result is malloc'd, it will be freed by a cleanup.
10057 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10058
10059static char *
10060psymtab_include_file_name (const struct line_header *lh, int file_index,
10061 const struct partial_symtab *pst,
10062 const char *comp_dir)
10063{
10064 const struct file_entry fe = lh->file_names [file_index];
10065 char *include_name = fe.name;
10066 char *include_name_to_compare = include_name;
10067 char *dir_name = NULL;
72b9f47f
TT
10068 const char *pst_filename;
10069 char *copied_name = NULL;
c6da4cef
DE
10070 int file_is_pst;
10071
10072 if (fe.dir_index)
10073 dir_name = lh->include_dirs[fe.dir_index - 1];
10074
10075 if (!IS_ABSOLUTE_PATH (include_name)
10076 && (dir_name != NULL || comp_dir != NULL))
10077 {
10078 /* Avoid creating a duplicate psymtab for PST.
10079 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10080 Before we do the comparison, however, we need to account
10081 for DIR_NAME and COMP_DIR.
10082 First prepend dir_name (if non-NULL). If we still don't
10083 have an absolute path prepend comp_dir (if non-NULL).
10084 However, the directory we record in the include-file's
10085 psymtab does not contain COMP_DIR (to match the
10086 corresponding symtab(s)).
10087
10088 Example:
10089
10090 bash$ cd /tmp
10091 bash$ gcc -g ./hello.c
10092 include_name = "hello.c"
10093 dir_name = "."
10094 DW_AT_comp_dir = comp_dir = "/tmp"
10095 DW_AT_name = "./hello.c" */
10096
10097 if (dir_name != NULL)
10098 {
10099 include_name = concat (dir_name, SLASH_STRING,
10100 include_name, (char *)NULL);
10101 include_name_to_compare = include_name;
10102 make_cleanup (xfree, include_name);
10103 }
10104 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10105 {
10106 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10107 include_name, (char *)NULL);
10108 }
10109 }
10110
10111 pst_filename = pst->filename;
10112 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10113 {
72b9f47f
TT
10114 copied_name = concat (pst->dirname, SLASH_STRING,
10115 pst_filename, (char *)NULL);
10116 pst_filename = copied_name;
c6da4cef
DE
10117 }
10118
1e3fad37 10119 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10120
10121 if (include_name_to_compare != include_name)
10122 xfree (include_name_to_compare);
72b9f47f
TT
10123 if (copied_name != NULL)
10124 xfree (copied_name);
c6da4cef
DE
10125
10126 if (file_is_pst)
10127 return NULL;
10128 return include_name;
10129}
10130
aaa75496
JB
10131/* Decode the Line Number Program (LNP) for the given line_header
10132 structure and CU. The actual information extracted and the type
10133 of structures created from the LNP depends on the value of PST.
10134
10135 1. If PST is NULL, then this procedure uses the data from the program
10136 to create all necessary symbol tables, and their linetables.
6e70227d 10137
aaa75496
JB
10138 2. If PST is not NULL, this procedure reads the program to determine
10139 the list of files included by the unit represented by PST, and
c6da4cef
DE
10140 builds all the associated partial symbol tables.
10141
10142 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10143 It is used for relative paths in the line table.
10144 NOTE: When processing partial symtabs (pst != NULL),
10145 comp_dir == pst->dirname.
10146
10147 NOTE: It is important that psymtabs have the same file name (via strcmp)
10148 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10149 symtab we don't use it in the name of the psymtabs we create.
10150 E.g. expand_line_sal requires this when finding psymtabs to expand.
10151 A good testcase for this is mb-inline.exp. */
debd256d 10152
c906108c 10153static void
72b9f47f 10154dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10155 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10156{
a8c50c1f 10157 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10158 gdb_byte *line_end;
a8c50c1f 10159 unsigned int bytes_read, extended_len;
c906108c 10160 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10161 CORE_ADDR baseaddr;
10162 struct objfile *objfile = cu->objfile;
fbf65064 10163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10164 const int decode_for_pst_p = (pst != NULL);
cb1df416 10165 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10166
10167 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10168
debd256d
JB
10169 line_ptr = lh->statement_program_start;
10170 line_end = lh->statement_program_end;
c906108c
SS
10171
10172 /* Read the statement sequences until there's nothing left. */
10173 while (line_ptr < line_end)
10174 {
10175 /* state machine registers */
10176 CORE_ADDR address = 0;
10177 unsigned int file = 1;
10178 unsigned int line = 1;
10179 unsigned int column = 0;
debd256d 10180 int is_stmt = lh->default_is_stmt;
c906108c
SS
10181 int basic_block = 0;
10182 int end_sequence = 0;
fbf65064 10183 CORE_ADDR addr;
2dc7f7b3 10184 unsigned char op_index = 0;
c906108c 10185
aaa75496 10186 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10187 {
aaa75496 10188 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10189 /* lh->include_dirs and lh->file_names are 0-based, but the
10190 directory and file name numbers in the statement program
10191 are 1-based. */
10192 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10193 char *dir = NULL;
a738430d 10194
debd256d
JB
10195 if (fe->dir_index)
10196 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10197
10198 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10199 }
10200
a738430d 10201 /* Decode the table. */
c5aa993b 10202 while (!end_sequence)
c906108c
SS
10203 {
10204 op_code = read_1_byte (abfd, line_ptr);
10205 line_ptr += 1;
59205f5a
JB
10206 if (line_ptr > line_end)
10207 {
10208 dwarf2_debug_line_missing_end_sequence_complaint ();
10209 break;
10210 }
9aa1fe7e 10211
debd256d 10212 if (op_code >= lh->opcode_base)
6e70227d 10213 {
a738430d 10214 /* Special operand. */
debd256d 10215 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10216 address += (((op_index + (adj_opcode / lh->line_range))
10217 / lh->maximum_ops_per_instruction)
10218 * lh->minimum_instruction_length);
10219 op_index = ((op_index + (adj_opcode / lh->line_range))
10220 % lh->maximum_ops_per_instruction);
debd256d 10221 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10222 if (lh->num_file_names < file || file == 0)
25e43795 10223 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10224 /* For now we ignore lines not starting on an
10225 instruction boundary. */
10226 else if (op_index == 0)
25e43795
DJ
10227 {
10228 lh->file_names[file - 1].included_p = 1;
ca5f395d 10229 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10230 {
10231 if (last_subfile != current_subfile)
10232 {
10233 addr = gdbarch_addr_bits_remove (gdbarch, address);
10234 if (last_subfile)
10235 record_line (last_subfile, 0, addr);
10236 last_subfile = current_subfile;
10237 }
25e43795 10238 /* Append row to matrix using current values. */
fbf65064
UW
10239 addr = check_cu_functions (address, cu);
10240 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10241 record_line (current_subfile, line, addr);
366da635 10242 }
25e43795 10243 }
ca5f395d 10244 basic_block = 0;
9aa1fe7e
GK
10245 }
10246 else switch (op_code)
c906108c
SS
10247 {
10248 case DW_LNS_extended_op:
a8c50c1f 10249 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10250 line_ptr += bytes_read;
a8c50c1f 10251 extended_end = line_ptr + extended_len;
c906108c
SS
10252 extended_op = read_1_byte (abfd, line_ptr);
10253 line_ptr += 1;
10254 switch (extended_op)
10255 {
10256 case DW_LNE_end_sequence:
10257 end_sequence = 1;
c906108c
SS
10258 break;
10259 case DW_LNE_set_address:
e7c27a73 10260 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10261 op_index = 0;
107d2387
AC
10262 line_ptr += bytes_read;
10263 address += baseaddr;
c906108c
SS
10264 break;
10265 case DW_LNE_define_file:
debd256d
JB
10266 {
10267 char *cur_file;
10268 unsigned int dir_index, mod_time, length;
6e70227d 10269
9b1c24c8 10270 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10271 line_ptr += bytes_read;
10272 dir_index =
10273 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10274 line_ptr += bytes_read;
10275 mod_time =
10276 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10277 line_ptr += bytes_read;
10278 length =
10279 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10280 line_ptr += bytes_read;
10281 add_file_name (lh, cur_file, dir_index, mod_time, length);
10282 }
c906108c 10283 break;
d0c6ba3d
CC
10284 case DW_LNE_set_discriminator:
10285 /* The discriminator is not interesting to the debugger;
10286 just ignore it. */
10287 line_ptr = extended_end;
10288 break;
c906108c 10289 default:
4d3c2250 10290 complaint (&symfile_complaints,
e2e0b3e5 10291 _("mangled .debug_line section"));
debd256d 10292 return;
c906108c 10293 }
a8c50c1f
DJ
10294 /* Make sure that we parsed the extended op correctly. If e.g.
10295 we expected a different address size than the producer used,
10296 we may have read the wrong number of bytes. */
10297 if (line_ptr != extended_end)
10298 {
10299 complaint (&symfile_complaints,
10300 _("mangled .debug_line section"));
10301 return;
10302 }
c906108c
SS
10303 break;
10304 case DW_LNS_copy:
59205f5a 10305 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10306 dwarf2_debug_line_missing_file_complaint ();
10307 else
366da635 10308 {
25e43795 10309 lh->file_names[file - 1].included_p = 1;
ca5f395d 10310 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10311 {
10312 if (last_subfile != current_subfile)
10313 {
10314 addr = gdbarch_addr_bits_remove (gdbarch, address);
10315 if (last_subfile)
10316 record_line (last_subfile, 0, addr);
10317 last_subfile = current_subfile;
10318 }
10319 addr = check_cu_functions (address, cu);
10320 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10321 record_line (current_subfile, line, addr);
10322 }
366da635 10323 }
c906108c
SS
10324 basic_block = 0;
10325 break;
10326 case DW_LNS_advance_pc:
2dc7f7b3
TT
10327 {
10328 CORE_ADDR adjust
10329 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10330
10331 address += (((op_index + adjust)
10332 / lh->maximum_ops_per_instruction)
10333 * lh->minimum_instruction_length);
10334 op_index = ((op_index + adjust)
10335 % lh->maximum_ops_per_instruction);
10336 line_ptr += bytes_read;
10337 }
c906108c
SS
10338 break;
10339 case DW_LNS_advance_line:
10340 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10341 line_ptr += bytes_read;
10342 break;
10343 case DW_LNS_set_file:
debd256d 10344 {
a738430d
MK
10345 /* The arrays lh->include_dirs and lh->file_names are
10346 0-based, but the directory and file name numbers in
10347 the statement program are 1-based. */
debd256d 10348 struct file_entry *fe;
4f1520fb 10349 char *dir = NULL;
a738430d 10350
debd256d
JB
10351 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10352 line_ptr += bytes_read;
59205f5a 10353 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10354 dwarf2_debug_line_missing_file_complaint ();
10355 else
10356 {
10357 fe = &lh->file_names[file - 1];
10358 if (fe->dir_index)
10359 dir = lh->include_dirs[fe->dir_index - 1];
10360 if (!decode_for_pst_p)
10361 {
10362 last_subfile = current_subfile;
10363 dwarf2_start_subfile (fe->name, dir, comp_dir);
10364 }
10365 }
debd256d 10366 }
c906108c
SS
10367 break;
10368 case DW_LNS_set_column:
10369 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10370 line_ptr += bytes_read;
10371 break;
10372 case DW_LNS_negate_stmt:
10373 is_stmt = (!is_stmt);
10374 break;
10375 case DW_LNS_set_basic_block:
10376 basic_block = 1;
10377 break;
c2c6d25f
JM
10378 /* Add to the address register of the state machine the
10379 address increment value corresponding to special opcode
a738430d
MK
10380 255. I.e., this value is scaled by the minimum
10381 instruction length since special opcode 255 would have
10382 scaled the the increment. */
c906108c 10383 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10384 {
10385 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10386
10387 address += (((op_index + adjust)
10388 / lh->maximum_ops_per_instruction)
10389 * lh->minimum_instruction_length);
10390 op_index = ((op_index + adjust)
10391 % lh->maximum_ops_per_instruction);
10392 }
c906108c
SS
10393 break;
10394 case DW_LNS_fixed_advance_pc:
10395 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10396 op_index = 0;
c906108c
SS
10397 line_ptr += 2;
10398 break;
9aa1fe7e 10399 default:
a738430d
MK
10400 {
10401 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10402 int i;
a738430d 10403
debd256d 10404 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10405 {
10406 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10407 line_ptr += bytes_read;
10408 }
10409 }
c906108c
SS
10410 }
10411 }
59205f5a
JB
10412 if (lh->num_file_names < file || file == 0)
10413 dwarf2_debug_line_missing_file_complaint ();
10414 else
10415 {
10416 lh->file_names[file - 1].included_p = 1;
10417 if (!decode_for_pst_p)
fbf65064
UW
10418 {
10419 addr = gdbarch_addr_bits_remove (gdbarch, address);
10420 record_line (current_subfile, 0, addr);
10421 }
59205f5a 10422 }
c906108c 10423 }
aaa75496
JB
10424
10425 if (decode_for_pst_p)
10426 {
10427 int file_index;
10428
10429 /* Now that we're done scanning the Line Header Program, we can
10430 create the psymtab of each included file. */
10431 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10432 if (lh->file_names[file_index].included_p == 1)
10433 {
c6da4cef
DE
10434 char *include_name =
10435 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10436 if (include_name != NULL)
aaa75496
JB
10437 dwarf2_create_include_psymtab (include_name, pst, objfile);
10438 }
10439 }
cb1df416
DJ
10440 else
10441 {
10442 /* Make sure a symtab is created for every file, even files
10443 which contain only variables (i.e. no code with associated
10444 line numbers). */
10445
10446 int i;
10447 struct file_entry *fe;
10448
10449 for (i = 0; i < lh->num_file_names; i++)
10450 {
10451 char *dir = NULL;
9a619af0 10452
cb1df416
DJ
10453 fe = &lh->file_names[i];
10454 if (fe->dir_index)
10455 dir = lh->include_dirs[fe->dir_index - 1];
10456 dwarf2_start_subfile (fe->name, dir, comp_dir);
10457
10458 /* Skip the main file; we don't need it, and it must be
10459 allocated last, so that it will show up before the
10460 non-primary symtabs in the objfile's symtab list. */
10461 if (current_subfile == first_subfile)
10462 continue;
10463
10464 if (current_subfile->symtab == NULL)
10465 current_subfile->symtab = allocate_symtab (current_subfile->name,
10466 cu->objfile);
10467 fe->symtab = current_subfile->symtab;
10468 }
10469 }
c906108c
SS
10470}
10471
10472/* Start a subfile for DWARF. FILENAME is the name of the file and
10473 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10474 or NULL if not known. COMP_DIR is the compilation directory for the
10475 linetable's compilation unit or NULL if not known.
c906108c
SS
10476 This routine tries to keep line numbers from identical absolute and
10477 relative file names in a common subfile.
10478
10479 Using the `list' example from the GDB testsuite, which resides in
10480 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10481 of /srcdir/list0.c yields the following debugging information for list0.c:
10482
c5aa993b
JM
10483 DW_AT_name: /srcdir/list0.c
10484 DW_AT_comp_dir: /compdir
357e46e7 10485 files.files[0].name: list0.h
c5aa993b 10486 files.files[0].dir: /srcdir
357e46e7 10487 files.files[1].name: list0.c
c5aa993b 10488 files.files[1].dir: /srcdir
c906108c
SS
10489
10490 The line number information for list0.c has to end up in a single
4f1520fb
FR
10491 subfile, so that `break /srcdir/list0.c:1' works as expected.
10492 start_subfile will ensure that this happens provided that we pass the
10493 concatenation of files.files[1].dir and files.files[1].name as the
10494 subfile's name. */
c906108c
SS
10495
10496static void
72b9f47f 10497dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
c906108c 10498{
4f1520fb
FR
10499 char *fullname;
10500
10501 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10502 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10503 second argument to start_subfile. To be consistent, we do the
10504 same here. In order not to lose the line information directory,
10505 we concatenate it to the filename when it makes sense.
10506 Note that the Dwarf3 standard says (speaking of filenames in line
10507 information): ``The directory index is ignored for file names
10508 that represent full path names''. Thus ignoring dirname in the
10509 `else' branch below isn't an issue. */
c906108c 10510
d5166ae1 10511 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10512 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10513 else
10514 fullname = filename;
c906108c 10515
4f1520fb
FR
10516 start_subfile (fullname, comp_dir);
10517
10518 if (fullname != filename)
10519 xfree (fullname);
c906108c
SS
10520}
10521
4c2df51b
DJ
10522static void
10523var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10524 struct dwarf2_cu *cu)
4c2df51b 10525{
e7c27a73
DJ
10526 struct objfile *objfile = cu->objfile;
10527 struct comp_unit_head *cu_header = &cu->header;
10528
4c2df51b
DJ
10529 /* NOTE drow/2003-01-30: There used to be a comment and some special
10530 code here to turn a symbol with DW_AT_external and a
10531 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10532 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10533 with some versions of binutils) where shared libraries could have
10534 relocations against symbols in their debug information - the
10535 minimal symbol would have the right address, but the debug info
10536 would not. It's no longer necessary, because we will explicitly
10537 apply relocations when we read in the debug information now. */
10538
10539 /* A DW_AT_location attribute with no contents indicates that a
10540 variable has been optimized away. */
10541 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10542 {
10543 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10544 return;
10545 }
10546
10547 /* Handle one degenerate form of location expression specially, to
10548 preserve GDB's previous behavior when section offsets are
10549 specified. If this is just a DW_OP_addr then mark this symbol
10550 as LOC_STATIC. */
10551
10552 if (attr_form_is_block (attr)
10553 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10554 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10555 {
891d2f0b 10556 unsigned int dummy;
4c2df51b
DJ
10557
10558 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10559 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10560 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10561 fixup_symbol_section (sym, objfile);
10562 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10563 SYMBOL_SECTION (sym));
4c2df51b
DJ
10564 return;
10565 }
10566
10567 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10568 expression evaluator, and use LOC_COMPUTED only when necessary
10569 (i.e. when the value of a register or memory location is
10570 referenced, or a thread-local block, etc.). Then again, it might
10571 not be worthwhile. I'm assuming that it isn't unless performance
10572 or memory numbers show me otherwise. */
10573
e7c27a73 10574 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10575 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10576}
10577
c906108c
SS
10578/* Given a pointer to a DWARF information entry, figure out if we need
10579 to make a symbol table entry for it, and if so, create a new entry
10580 and return a pointer to it.
10581 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10582 used the passed type.
10583 If SPACE is not NULL, use it to hold the new symbol. If it is
10584 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10585
10586static struct symbol *
34eaf542
TT
10587new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10588 struct symbol *space)
c906108c 10589{
e7c27a73 10590 struct objfile *objfile = cu->objfile;
c906108c
SS
10591 struct symbol *sym = NULL;
10592 char *name;
10593 struct attribute *attr = NULL;
10594 struct attribute *attr2 = NULL;
e142c38c 10595 CORE_ADDR baseaddr;
e37fd15a
SW
10596 struct pending **list_to_add = NULL;
10597
edb3359d 10598 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10599
10600 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10601
94af9270 10602 name = dwarf2_name (die, cu);
c906108c
SS
10603 if (name)
10604 {
94af9270 10605 const char *linkagename;
34eaf542 10606 int suppress_add = 0;
94af9270 10607
34eaf542
TT
10608 if (space)
10609 sym = space;
10610 else
10611 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10612 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10613
10614 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10615 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10616 linkagename = dwarf2_physname (name, die, cu);
10617 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10618
f55ee35c
JK
10619 /* Fortran does not have mangling standard and the mangling does differ
10620 between gfortran, iFort etc. */
10621 if (cu->language == language_fortran
b250c185 10622 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10623 symbol_set_demangled_name (&(sym->ginfo),
10624 (char *) dwarf2_full_name (name, die, cu),
10625 NULL);
f55ee35c 10626
c906108c 10627 /* Default assumptions.
c5aa993b 10628 Use the passed type or decode it from the die. */
176620f1 10629 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10630 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10631 if (type != NULL)
10632 SYMBOL_TYPE (sym) = type;
10633 else
e7c27a73 10634 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10635 attr = dwarf2_attr (die,
10636 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10637 cu);
c906108c
SS
10638 if (attr)
10639 {
10640 SYMBOL_LINE (sym) = DW_UNSND (attr);
10641 }
cb1df416 10642
edb3359d
DJ
10643 attr = dwarf2_attr (die,
10644 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10645 cu);
cb1df416
DJ
10646 if (attr)
10647 {
10648 int file_index = DW_UNSND (attr);
9a619af0 10649
cb1df416
DJ
10650 if (cu->line_header == NULL
10651 || file_index > cu->line_header->num_file_names)
10652 complaint (&symfile_complaints,
10653 _("file index out of range"));
1c3d648d 10654 else if (file_index > 0)
cb1df416
DJ
10655 {
10656 struct file_entry *fe;
9a619af0 10657
cb1df416
DJ
10658 fe = &cu->line_header->file_names[file_index - 1];
10659 SYMBOL_SYMTAB (sym) = fe->symtab;
10660 }
10661 }
10662
c906108c
SS
10663 switch (die->tag)
10664 {
10665 case DW_TAG_label:
e142c38c 10666 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10667 if (attr)
10668 {
10669 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10670 }
0f5238ed
TT
10671 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10672 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10673 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10674 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10675 break;
10676 case DW_TAG_subprogram:
10677 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10678 finish_block. */
10679 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10680 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10681 if ((attr2 && (DW_UNSND (attr2) != 0))
10682 || cu->language == language_ada)
c906108c 10683 {
2cfa0c8d
JB
10684 /* Subprograms marked external are stored as a global symbol.
10685 Ada subprograms, whether marked external or not, are always
10686 stored as a global symbol, because we want to be able to
10687 access them globally. For instance, we want to be able
10688 to break on a nested subprogram without having to
10689 specify the context. */
e37fd15a 10690 list_to_add = &global_symbols;
c906108c
SS
10691 }
10692 else
10693 {
e37fd15a 10694 list_to_add = cu->list_in_scope;
c906108c
SS
10695 }
10696 break;
edb3359d
DJ
10697 case DW_TAG_inlined_subroutine:
10698 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10699 finish_block. */
10700 SYMBOL_CLASS (sym) = LOC_BLOCK;
10701 SYMBOL_INLINED (sym) = 1;
10702 /* Do not add the symbol to any lists. It will be found via
10703 BLOCK_FUNCTION from the blockvector. */
10704 break;
34eaf542
TT
10705 case DW_TAG_template_value_param:
10706 suppress_add = 1;
10707 /* Fall through. */
72929c62 10708 case DW_TAG_constant:
c906108c 10709 case DW_TAG_variable:
254e6b9e 10710 case DW_TAG_member:
c906108c
SS
10711 /* Compilation with minimal debug info may result in variables
10712 with missing type entries. Change the misleading `void' type
10713 to something sensible. */
10714 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10715 SYMBOL_TYPE (sym)
46bf5051 10716 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10717
e142c38c 10718 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10719 /* In the case of DW_TAG_member, we should only be called for
10720 static const members. */
10721 if (die->tag == DW_TAG_member)
10722 {
3863f96c
DE
10723 /* dwarf2_add_field uses die_is_declaration,
10724 so we do the same. */
254e6b9e
DE
10725 gdb_assert (die_is_declaration (die, cu));
10726 gdb_assert (attr);
10727 }
c906108c
SS
10728 if (attr)
10729 {
e7c27a73 10730 dwarf2_const_value (attr, sym, cu);
e142c38c 10731 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10732 if (!suppress_add)
34eaf542
TT
10733 {
10734 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10735 list_to_add = &global_symbols;
34eaf542 10736 else
e37fd15a 10737 list_to_add = cu->list_in_scope;
34eaf542 10738 }
c906108c
SS
10739 break;
10740 }
e142c38c 10741 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10742 if (attr)
10743 {
e7c27a73 10744 var_decode_location (attr, sym, cu);
e142c38c 10745 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10746 if (SYMBOL_CLASS (sym) == LOC_STATIC
10747 && SYMBOL_VALUE_ADDRESS (sym) == 0
10748 && !dwarf2_per_objfile->has_section_at_zero)
10749 {
10750 /* When a static variable is eliminated by the linker,
10751 the corresponding debug information is not stripped
10752 out, but the variable address is set to null;
10753 do not add such variables into symbol table. */
10754 }
10755 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10756 {
f55ee35c
JK
10757 /* Workaround gfortran PR debug/40040 - it uses
10758 DW_AT_location for variables in -fPIC libraries which may
10759 get overriden by other libraries/executable and get
10760 a different address. Resolve it by the minimal symbol
10761 which may come from inferior's executable using copy
10762 relocation. Make this workaround only for gfortran as for
10763 other compilers GDB cannot guess the minimal symbol
10764 Fortran mangling kind. */
10765 if (cu->language == language_fortran && die->parent
10766 && die->parent->tag == DW_TAG_module
10767 && cu->producer
10768 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10769 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10770
1c809c68
TT
10771 /* A variable with DW_AT_external is never static,
10772 but it may be block-scoped. */
10773 list_to_add = (cu->list_in_scope == &file_symbols
10774 ? &global_symbols : cu->list_in_scope);
1c809c68 10775 }
c906108c 10776 else
e37fd15a 10777 list_to_add = cu->list_in_scope;
c906108c
SS
10778 }
10779 else
10780 {
10781 /* We do not know the address of this symbol.
c5aa993b
JM
10782 If it is an external symbol and we have type information
10783 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10784 The address of the variable will then be determined from
10785 the minimal symbol table whenever the variable is
10786 referenced. */
e142c38c 10787 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10788 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10789 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10790 {
0fe7935b
DJ
10791 /* A variable with DW_AT_external is never static, but it
10792 may be block-scoped. */
10793 list_to_add = (cu->list_in_scope == &file_symbols
10794 ? &global_symbols : cu->list_in_scope);
10795
c906108c 10796 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10797 }
442ddf59
JK
10798 else if (!die_is_declaration (die, cu))
10799 {
10800 /* Use the default LOC_OPTIMIZED_OUT class. */
10801 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10802 if (!suppress_add)
10803 list_to_add = cu->list_in_scope;
442ddf59 10804 }
c906108c
SS
10805 }
10806 break;
10807 case DW_TAG_formal_parameter:
edb3359d
DJ
10808 /* If we are inside a function, mark this as an argument. If
10809 not, we might be looking at an argument to an inlined function
10810 when we do not have enough information to show inlined frames;
10811 pretend it's a local variable in that case so that the user can
10812 still see it. */
10813 if (context_stack_depth > 0
10814 && context_stack[context_stack_depth - 1].name != NULL)
10815 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10816 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10817 if (attr)
10818 {
e7c27a73 10819 var_decode_location (attr, sym, cu);
c906108c 10820 }
e142c38c 10821 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10822 if (attr)
10823 {
e7c27a73 10824 dwarf2_const_value (attr, sym, cu);
c906108c 10825 }
f346a30d
PM
10826 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10827 if (attr && DW_UNSND (attr))
10828 {
10829 struct type *ref_type;
10830
10831 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10832 SYMBOL_TYPE (sym) = ref_type;
10833 }
10834
e37fd15a 10835 list_to_add = cu->list_in_scope;
c906108c
SS
10836 break;
10837 case DW_TAG_unspecified_parameters:
10838 /* From varargs functions; gdb doesn't seem to have any
10839 interest in this information, so just ignore it for now.
10840 (FIXME?) */
10841 break;
34eaf542
TT
10842 case DW_TAG_template_type_param:
10843 suppress_add = 1;
10844 /* Fall through. */
c906108c 10845 case DW_TAG_class_type:
680b30c7 10846 case DW_TAG_interface_type:
c906108c
SS
10847 case DW_TAG_structure_type:
10848 case DW_TAG_union_type:
72019c9c 10849 case DW_TAG_set_type:
c906108c
SS
10850 case DW_TAG_enumeration_type:
10851 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10852 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10853
63d06c5c 10854 {
987504bb 10855 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10856 really ever be static objects: otherwise, if you try
10857 to, say, break of a class's method and you're in a file
10858 which doesn't mention that class, it won't work unless
10859 the check for all static symbols in lookup_symbol_aux
10860 saves you. See the OtherFileClass tests in
10861 gdb.c++/namespace.exp. */
10862
e37fd15a 10863 if (!suppress_add)
34eaf542 10864 {
34eaf542
TT
10865 list_to_add = (cu->list_in_scope == &file_symbols
10866 && (cu->language == language_cplus
10867 || cu->language == language_java)
10868 ? &global_symbols : cu->list_in_scope);
63d06c5c 10869
64382290
TT
10870 /* The semantics of C++ state that "struct foo {
10871 ... }" also defines a typedef for "foo". A Java
10872 class declaration also defines a typedef for the
10873 class. */
10874 if (cu->language == language_cplus
10875 || cu->language == language_java
10876 || cu->language == language_ada)
10877 {
10878 /* The symbol's name is already allocated along
10879 with this objfile, so we don't need to
10880 duplicate it for the type. */
10881 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10882 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10883 }
63d06c5c
DC
10884 }
10885 }
c906108c
SS
10886 break;
10887 case DW_TAG_typedef:
63d06c5c
DC
10888 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10889 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10890 list_to_add = cu->list_in_scope;
63d06c5c 10891 break;
c906108c 10892 case DW_TAG_base_type:
a02abb62 10893 case DW_TAG_subrange_type:
c906108c 10894 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10895 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10896 list_to_add = cu->list_in_scope;
c906108c
SS
10897 break;
10898 case DW_TAG_enumerator:
e142c38c 10899 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10900 if (attr)
10901 {
e7c27a73 10902 dwarf2_const_value (attr, sym, cu);
c906108c 10903 }
63d06c5c
DC
10904 {
10905 /* NOTE: carlton/2003-11-10: See comment above in the
10906 DW_TAG_class_type, etc. block. */
10907
e142c38c 10908 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10909 && (cu->language == language_cplus
10910 || cu->language == language_java)
e142c38c 10911 ? &global_symbols : cu->list_in_scope);
63d06c5c 10912 }
c906108c 10913 break;
5c4e30ca
DC
10914 case DW_TAG_namespace:
10915 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10916 list_to_add = &global_symbols;
5c4e30ca 10917 break;
c906108c
SS
10918 default:
10919 /* Not a tag we recognize. Hopefully we aren't processing
10920 trash data, but since we must specifically ignore things
10921 we don't recognize, there is nothing else we should do at
10922 this point. */
e2e0b3e5 10923 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10924 dwarf_tag_name (die->tag));
c906108c
SS
10925 break;
10926 }
df8a16a1 10927
e37fd15a
SW
10928 if (suppress_add)
10929 {
10930 sym->hash_next = objfile->template_symbols;
10931 objfile->template_symbols = sym;
10932 list_to_add = NULL;
10933 }
10934
10935 if (list_to_add != NULL)
10936 add_symbol_to_list (sym, list_to_add);
10937
df8a16a1
DJ
10938 /* For the benefit of old versions of GCC, check for anonymous
10939 namespaces based on the demangled name. */
10940 if (!processing_has_namespace_info
94af9270 10941 && cu->language == language_cplus)
df8a16a1 10942 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10943 }
10944 return (sym);
10945}
10946
34eaf542
TT
10947/* A wrapper for new_symbol_full that always allocates a new symbol. */
10948
10949static struct symbol *
10950new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10951{
10952 return new_symbol_full (die, type, cu, NULL);
10953}
10954
98bfdba5
PA
10955/* Given an attr with a DW_FORM_dataN value in host byte order,
10956 zero-extend it as appropriate for the symbol's type. The DWARF
10957 standard (v4) is not entirely clear about the meaning of using
10958 DW_FORM_dataN for a constant with a signed type, where the type is
10959 wider than the data. The conclusion of a discussion on the DWARF
10960 list was that this is unspecified. We choose to always zero-extend
10961 because that is the interpretation long in use by GCC. */
c906108c 10962
98bfdba5
PA
10963static gdb_byte *
10964dwarf2_const_value_data (struct attribute *attr, struct type *type,
10965 const char *name, struct obstack *obstack,
10966 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10967{
e7c27a73 10968 struct objfile *objfile = cu->objfile;
e17a4113
UW
10969 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10970 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10971 LONGEST l = DW_UNSND (attr);
10972
10973 if (bits < sizeof (*value) * 8)
10974 {
10975 l &= ((LONGEST) 1 << bits) - 1;
10976 *value = l;
10977 }
10978 else if (bits == sizeof (*value) * 8)
10979 *value = l;
10980 else
10981 {
10982 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10983 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10984 return bytes;
10985 }
10986
10987 return NULL;
10988}
10989
10990/* Read a constant value from an attribute. Either set *VALUE, or if
10991 the value does not fit in *VALUE, set *BYTES - either already
10992 allocated on the objfile obstack, or newly allocated on OBSTACK,
10993 or, set *BATON, if we translated the constant to a location
10994 expression. */
10995
10996static void
10997dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10998 const char *name, struct obstack *obstack,
10999 struct dwarf2_cu *cu,
11000 long *value, gdb_byte **bytes,
11001 struct dwarf2_locexpr_baton **baton)
11002{
11003 struct objfile *objfile = cu->objfile;
11004 struct comp_unit_head *cu_header = &cu->header;
c906108c 11005 struct dwarf_block *blk;
98bfdba5
PA
11006 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11007 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11008
11009 *value = 0;
11010 *bytes = NULL;
11011 *baton = NULL;
c906108c
SS
11012
11013 switch (attr->form)
11014 {
11015 case DW_FORM_addr:
ac56253d 11016 {
ac56253d
TT
11017 gdb_byte *data;
11018
98bfdba5
PA
11019 if (TYPE_LENGTH (type) != cu_header->addr_size)
11020 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11021 cu_header->addr_size,
98bfdba5 11022 TYPE_LENGTH (type));
ac56253d
TT
11023 /* Symbols of this form are reasonably rare, so we just
11024 piggyback on the existing location code rather than writing
11025 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11026 *baton = obstack_alloc (&objfile->objfile_obstack,
11027 sizeof (struct dwarf2_locexpr_baton));
11028 (*baton)->per_cu = cu->per_cu;
11029 gdb_assert ((*baton)->per_cu);
ac56253d 11030
98bfdba5
PA
11031 (*baton)->size = 2 + cu_header->addr_size;
11032 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11033 (*baton)->data = data;
ac56253d
TT
11034
11035 data[0] = DW_OP_addr;
11036 store_unsigned_integer (&data[1], cu_header->addr_size,
11037 byte_order, DW_ADDR (attr));
11038 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11039 }
c906108c 11040 break;
4ac36638 11041 case DW_FORM_string:
93b5768b 11042 case DW_FORM_strp:
98bfdba5
PA
11043 /* DW_STRING is already allocated on the objfile obstack, point
11044 directly to it. */
11045 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11046 break;
c906108c
SS
11047 case DW_FORM_block1:
11048 case DW_FORM_block2:
11049 case DW_FORM_block4:
11050 case DW_FORM_block:
2dc7f7b3 11051 case DW_FORM_exprloc:
c906108c 11052 blk = DW_BLOCK (attr);
98bfdba5
PA
11053 if (TYPE_LENGTH (type) != blk->size)
11054 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11055 TYPE_LENGTH (type));
11056 *bytes = blk->data;
c906108c 11057 break;
2df3850c
JM
11058
11059 /* The DW_AT_const_value attributes are supposed to carry the
11060 symbol's value "represented as it would be on the target
11061 architecture." By the time we get here, it's already been
11062 converted to host endianness, so we just need to sign- or
11063 zero-extend it as appropriate. */
11064 case DW_FORM_data1:
98bfdba5 11065 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 11066 break;
c906108c 11067 case DW_FORM_data2:
98bfdba5 11068 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 11069 break;
c906108c 11070 case DW_FORM_data4:
98bfdba5 11071 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 11072 break;
c906108c 11073 case DW_FORM_data8:
98bfdba5 11074 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11075 break;
11076
c906108c 11077 case DW_FORM_sdata:
98bfdba5 11078 *value = DW_SND (attr);
2df3850c
JM
11079 break;
11080
c906108c 11081 case DW_FORM_udata:
98bfdba5 11082 *value = DW_UNSND (attr);
c906108c 11083 break;
2df3850c 11084
c906108c 11085 default:
4d3c2250 11086 complaint (&symfile_complaints,
e2e0b3e5 11087 _("unsupported const value attribute form: '%s'"),
4d3c2250 11088 dwarf_form_name (attr->form));
98bfdba5 11089 *value = 0;
c906108c
SS
11090 break;
11091 }
11092}
11093
2df3850c 11094
98bfdba5
PA
11095/* Copy constant value from an attribute to a symbol. */
11096
2df3850c 11097static void
98bfdba5
PA
11098dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11099 struct dwarf2_cu *cu)
2df3850c 11100{
98bfdba5
PA
11101 struct objfile *objfile = cu->objfile;
11102 struct comp_unit_head *cu_header = &cu->header;
11103 long value;
11104 gdb_byte *bytes;
11105 struct dwarf2_locexpr_baton *baton;
2df3850c 11106
98bfdba5
PA
11107 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11108 SYMBOL_PRINT_NAME (sym),
11109 &objfile->objfile_obstack, cu,
11110 &value, &bytes, &baton);
2df3850c 11111
98bfdba5
PA
11112 if (baton != NULL)
11113 {
11114 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11115 SYMBOL_LOCATION_BATON (sym) = baton;
11116 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11117 }
11118 else if (bytes != NULL)
11119 {
11120 SYMBOL_VALUE_BYTES (sym) = bytes;
11121 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11122 }
11123 else
11124 {
11125 SYMBOL_VALUE (sym) = value;
11126 SYMBOL_CLASS (sym) = LOC_CONST;
11127 }
2df3850c
JM
11128}
11129
c906108c
SS
11130/* Return the type of the die in question using its DW_AT_type attribute. */
11131
11132static struct type *
e7c27a73 11133die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11134{
c906108c 11135 struct attribute *type_attr;
c906108c 11136
e142c38c 11137 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11138 if (!type_attr)
11139 {
11140 /* A missing DW_AT_type represents a void type. */
46bf5051 11141 return objfile_type (cu->objfile)->builtin_void;
c906108c 11142 }
348e048f 11143
673bfd45 11144 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11145}
11146
b4ba55a1
JB
11147/* True iff CU's producer generates GNAT Ada auxiliary information
11148 that allows to find parallel types through that information instead
11149 of having to do expensive parallel lookups by type name. */
11150
11151static int
11152need_gnat_info (struct dwarf2_cu *cu)
11153{
11154 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11155 of GNAT produces this auxiliary information, without any indication
11156 that it is produced. Part of enhancing the FSF version of GNAT
11157 to produce that information will be to put in place an indicator
11158 that we can use in order to determine whether the descriptive type
11159 info is available or not. One suggestion that has been made is
11160 to use a new attribute, attached to the CU die. For now, assume
11161 that the descriptive type info is not available. */
11162 return 0;
11163}
11164
b4ba55a1
JB
11165/* Return the auxiliary type of the die in question using its
11166 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11167 attribute is not present. */
11168
11169static struct type *
11170die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11171{
b4ba55a1 11172 struct attribute *type_attr;
b4ba55a1
JB
11173
11174 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11175 if (!type_attr)
11176 return NULL;
11177
673bfd45 11178 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11179}
11180
11181/* If DIE has a descriptive_type attribute, then set the TYPE's
11182 descriptive type accordingly. */
11183
11184static void
11185set_descriptive_type (struct type *type, struct die_info *die,
11186 struct dwarf2_cu *cu)
11187{
11188 struct type *descriptive_type = die_descriptive_type (die, cu);
11189
11190 if (descriptive_type)
11191 {
11192 ALLOCATE_GNAT_AUX_TYPE (type);
11193 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11194 }
11195}
11196
c906108c
SS
11197/* Return the containing type of the die in question using its
11198 DW_AT_containing_type attribute. */
11199
11200static struct type *
e7c27a73 11201die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11202{
c906108c 11203 struct attribute *type_attr;
c906108c 11204
e142c38c 11205 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11206 if (!type_attr)
11207 error (_("Dwarf Error: Problem turning containing type into gdb type "
11208 "[in module %s]"), cu->objfile->name);
11209
673bfd45 11210 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11211}
11212
673bfd45
DE
11213/* Look up the type of DIE in CU using its type attribute ATTR.
11214 If there is no type substitute an error marker. */
11215
c906108c 11216static struct type *
673bfd45
DE
11217lookup_die_type (struct die_info *die, struct attribute *attr,
11218 struct dwarf2_cu *cu)
c906108c 11219{
f792889a
DJ
11220 struct type *this_type;
11221
673bfd45
DE
11222 /* First see if we have it cached. */
11223
11224 if (is_ref_attr (attr))
11225 {
11226 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11227
11228 this_type = get_die_type_at_offset (offset, cu->per_cu);
11229 }
11230 else if (attr->form == DW_FORM_sig8)
11231 {
11232 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11233 struct dwarf2_cu *sig_cu;
11234 unsigned int offset;
11235
11236 /* sig_type will be NULL if the signatured type is missing from
11237 the debug info. */
11238 if (sig_type == NULL)
11239 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11240 "at 0x%x [in module %s]"),
11241 die->offset, cu->objfile->name);
11242
11243 gdb_assert (sig_type->per_cu.from_debug_types);
11244 offset = sig_type->offset + sig_type->type_offset;
11245 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11246 }
11247 else
11248 {
11249 dump_die_for_error (die);
11250 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11251 dwarf_attr_name (attr->name), cu->objfile->name);
11252 }
11253
11254 /* If not cached we need to read it in. */
11255
11256 if (this_type == NULL)
11257 {
11258 struct die_info *type_die;
11259 struct dwarf2_cu *type_cu = cu;
11260
11261 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11262 /* If the type is cached, we should have found it above. */
11263 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11264 this_type = read_type_die_1 (type_die, type_cu);
11265 }
11266
11267 /* If we still don't have a type use an error marker. */
11268
11269 if (this_type == NULL)
c906108c 11270 {
b00fdb78
TT
11271 char *message, *saved;
11272
11273 /* read_type_die already issued a complaint. */
11274 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11275 cu->objfile->name,
11276 cu->header.offset,
11277 die->offset);
11278 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11279 message, strlen (message));
11280 xfree (message);
11281
11282 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11283 }
673bfd45 11284
f792889a 11285 return this_type;
c906108c
SS
11286}
11287
673bfd45
DE
11288/* Return the type in DIE, CU.
11289 Returns NULL for invalid types.
11290
11291 This first does a lookup in the appropriate type_hash table,
11292 and only reads the die in if necessary.
11293
11294 NOTE: This can be called when reading in partial or full symbols. */
11295
f792889a 11296static struct type *
e7c27a73 11297read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11298{
f792889a
DJ
11299 struct type *this_type;
11300
11301 this_type = get_die_type (die, cu);
11302 if (this_type)
11303 return this_type;
11304
673bfd45
DE
11305 return read_type_die_1 (die, cu);
11306}
11307
11308/* Read the type in DIE, CU.
11309 Returns NULL for invalid types. */
11310
11311static struct type *
11312read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11313{
11314 struct type *this_type = NULL;
11315
c906108c
SS
11316 switch (die->tag)
11317 {
11318 case DW_TAG_class_type:
680b30c7 11319 case DW_TAG_interface_type:
c906108c
SS
11320 case DW_TAG_structure_type:
11321 case DW_TAG_union_type:
f792889a 11322 this_type = read_structure_type (die, cu);
c906108c
SS
11323 break;
11324 case DW_TAG_enumeration_type:
f792889a 11325 this_type = read_enumeration_type (die, cu);
c906108c
SS
11326 break;
11327 case DW_TAG_subprogram:
11328 case DW_TAG_subroutine_type:
edb3359d 11329 case DW_TAG_inlined_subroutine:
f792889a 11330 this_type = read_subroutine_type (die, cu);
c906108c
SS
11331 break;
11332 case DW_TAG_array_type:
f792889a 11333 this_type = read_array_type (die, cu);
c906108c 11334 break;
72019c9c 11335 case DW_TAG_set_type:
f792889a 11336 this_type = read_set_type (die, cu);
72019c9c 11337 break;
c906108c 11338 case DW_TAG_pointer_type:
f792889a 11339 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11340 break;
11341 case DW_TAG_ptr_to_member_type:
f792889a 11342 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11343 break;
11344 case DW_TAG_reference_type:
f792889a 11345 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11346 break;
11347 case DW_TAG_const_type:
f792889a 11348 this_type = read_tag_const_type (die, cu);
c906108c
SS
11349 break;
11350 case DW_TAG_volatile_type:
f792889a 11351 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11352 break;
11353 case DW_TAG_string_type:
f792889a 11354 this_type = read_tag_string_type (die, cu);
c906108c
SS
11355 break;
11356 case DW_TAG_typedef:
f792889a 11357 this_type = read_typedef (die, cu);
c906108c 11358 break;
a02abb62 11359 case DW_TAG_subrange_type:
f792889a 11360 this_type = read_subrange_type (die, cu);
a02abb62 11361 break;
c906108c 11362 case DW_TAG_base_type:
f792889a 11363 this_type = read_base_type (die, cu);
c906108c 11364 break;
81a17f79 11365 case DW_TAG_unspecified_type:
f792889a 11366 this_type = read_unspecified_type (die, cu);
81a17f79 11367 break;
0114d602
DJ
11368 case DW_TAG_namespace:
11369 this_type = read_namespace_type (die, cu);
11370 break;
f55ee35c
JK
11371 case DW_TAG_module:
11372 this_type = read_module_type (die, cu);
11373 break;
c906108c 11374 default:
a1f5b845 11375 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11376 dwarf_tag_name (die->tag));
c906108c
SS
11377 break;
11378 }
63d06c5c 11379
f792889a 11380 return this_type;
63d06c5c
DC
11381}
11382
abc72ce4
DE
11383/* See if we can figure out if the class lives in a namespace. We do
11384 this by looking for a member function; its demangled name will
11385 contain namespace info, if there is any.
11386 Return the computed name or NULL.
11387 Space for the result is allocated on the objfile's obstack.
11388 This is the full-die version of guess_partial_die_structure_name.
11389 In this case we know DIE has no useful parent. */
11390
11391static char *
11392guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11393{
11394 struct die_info *spec_die;
11395 struct dwarf2_cu *spec_cu;
11396 struct die_info *child;
11397
11398 spec_cu = cu;
11399 spec_die = die_specification (die, &spec_cu);
11400 if (spec_die != NULL)
11401 {
11402 die = spec_die;
11403 cu = spec_cu;
11404 }
11405
11406 for (child = die->child;
11407 child != NULL;
11408 child = child->sibling)
11409 {
11410 if (child->tag == DW_TAG_subprogram)
11411 {
11412 struct attribute *attr;
11413
11414 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11415 if (attr == NULL)
11416 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11417 if (attr != NULL)
11418 {
11419 char *actual_name
11420 = language_class_name_from_physname (cu->language_defn,
11421 DW_STRING (attr));
11422 char *name = NULL;
11423
11424 if (actual_name != NULL)
11425 {
11426 char *die_name = dwarf2_name (die, cu);
11427
11428 if (die_name != NULL
11429 && strcmp (die_name, actual_name) != 0)
11430 {
11431 /* Strip off the class name from the full name.
11432 We want the prefix. */
11433 int die_name_len = strlen (die_name);
11434 int actual_name_len = strlen (actual_name);
11435
11436 /* Test for '::' as a sanity check. */
11437 if (actual_name_len > die_name_len + 2
11438 && actual_name[actual_name_len - die_name_len - 1] == ':')
11439 name =
11440 obsavestring (actual_name,
11441 actual_name_len - die_name_len - 2,
11442 &cu->objfile->objfile_obstack);
11443 }
11444 }
11445 xfree (actual_name);
11446 return name;
11447 }
11448 }
11449 }
11450
11451 return NULL;
11452}
11453
fdde2d81 11454/* Return the name of the namespace/class that DIE is defined within,
0114d602 11455 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11456
0114d602
DJ
11457 For example, if we're within the method foo() in the following
11458 code:
11459
11460 namespace N {
11461 class C {
11462 void foo () {
11463 }
11464 };
11465 }
11466
11467 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11468
11469static char *
e142c38c 11470determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11471{
0114d602
DJ
11472 struct die_info *parent, *spec_die;
11473 struct dwarf2_cu *spec_cu;
11474 struct type *parent_type;
63d06c5c 11475
f55ee35c
JK
11476 if (cu->language != language_cplus && cu->language != language_java
11477 && cu->language != language_fortran)
0114d602
DJ
11478 return "";
11479
11480 /* We have to be careful in the presence of DW_AT_specification.
11481 For example, with GCC 3.4, given the code
11482
11483 namespace N {
11484 void foo() {
11485 // Definition of N::foo.
11486 }
11487 }
11488
11489 then we'll have a tree of DIEs like this:
11490
11491 1: DW_TAG_compile_unit
11492 2: DW_TAG_namespace // N
11493 3: DW_TAG_subprogram // declaration of N::foo
11494 4: DW_TAG_subprogram // definition of N::foo
11495 DW_AT_specification // refers to die #3
11496
11497 Thus, when processing die #4, we have to pretend that we're in
11498 the context of its DW_AT_specification, namely the contex of die
11499 #3. */
11500 spec_cu = cu;
11501 spec_die = die_specification (die, &spec_cu);
11502 if (spec_die == NULL)
11503 parent = die->parent;
11504 else
63d06c5c 11505 {
0114d602
DJ
11506 parent = spec_die->parent;
11507 cu = spec_cu;
63d06c5c 11508 }
0114d602
DJ
11509
11510 if (parent == NULL)
11511 return "";
98bfdba5
PA
11512 else if (parent->building_fullname)
11513 {
11514 const char *name;
11515 const char *parent_name;
11516
11517 /* It has been seen on RealView 2.2 built binaries,
11518 DW_TAG_template_type_param types actually _defined_ as
11519 children of the parent class:
11520
11521 enum E {};
11522 template class <class Enum> Class{};
11523 Class<enum E> class_e;
11524
11525 1: DW_TAG_class_type (Class)
11526 2: DW_TAG_enumeration_type (E)
11527 3: DW_TAG_enumerator (enum1:0)
11528 3: DW_TAG_enumerator (enum2:1)
11529 ...
11530 2: DW_TAG_template_type_param
11531 DW_AT_type DW_FORM_ref_udata (E)
11532
11533 Besides being broken debug info, it can put GDB into an
11534 infinite loop. Consider:
11535
11536 When we're building the full name for Class<E>, we'll start
11537 at Class, and go look over its template type parameters,
11538 finding E. We'll then try to build the full name of E, and
11539 reach here. We're now trying to build the full name of E,
11540 and look over the parent DIE for containing scope. In the
11541 broken case, if we followed the parent DIE of E, we'd again
11542 find Class, and once again go look at its template type
11543 arguments, etc., etc. Simply don't consider such parent die
11544 as source-level parent of this die (it can't be, the language
11545 doesn't allow it), and break the loop here. */
11546 name = dwarf2_name (die, cu);
11547 parent_name = dwarf2_name (parent, cu);
11548 complaint (&symfile_complaints,
11549 _("template param type '%s' defined within parent '%s'"),
11550 name ? name : "<unknown>",
11551 parent_name ? parent_name : "<unknown>");
11552 return "";
11553 }
63d06c5c 11554 else
0114d602
DJ
11555 switch (parent->tag)
11556 {
63d06c5c 11557 case DW_TAG_namespace:
0114d602 11558 parent_type = read_type_die (parent, cu);
acebe513
UW
11559 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11560 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11561 Work around this problem here. */
11562 if (cu->language == language_cplus
11563 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11564 return "";
0114d602
DJ
11565 /* We give a name to even anonymous namespaces. */
11566 return TYPE_TAG_NAME (parent_type);
63d06c5c 11567 case DW_TAG_class_type:
680b30c7 11568 case DW_TAG_interface_type:
63d06c5c 11569 case DW_TAG_structure_type:
0114d602 11570 case DW_TAG_union_type:
f55ee35c 11571 case DW_TAG_module:
0114d602
DJ
11572 parent_type = read_type_die (parent, cu);
11573 if (TYPE_TAG_NAME (parent_type) != NULL)
11574 return TYPE_TAG_NAME (parent_type);
11575 else
11576 /* An anonymous structure is only allowed non-static data
11577 members; no typedefs, no member functions, et cetera.
11578 So it does not need a prefix. */
11579 return "";
abc72ce4
DE
11580 case DW_TAG_compile_unit:
11581 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11582 if (cu->language == language_cplus
11583 && dwarf2_per_objfile->types.asection != NULL
11584 && die->child != NULL
11585 && (die->tag == DW_TAG_class_type
11586 || die->tag == DW_TAG_structure_type
11587 || die->tag == DW_TAG_union_type))
11588 {
11589 char *name = guess_full_die_structure_name (die, cu);
11590 if (name != NULL)
11591 return name;
11592 }
11593 return "";
63d06c5c 11594 default:
8176b9b8 11595 return determine_prefix (parent, cu);
63d06c5c 11596 }
63d06c5c
DC
11597}
11598
987504bb
JJ
11599/* Return a newly-allocated string formed by concatenating PREFIX and
11600 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11601 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11602 perform an obconcat, otherwise allocate storage for the result. The CU argument
11603 is used to determine the language and hence, the appropriate separator. */
11604
f55ee35c 11605#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11606
11607static char *
f55ee35c
JK
11608typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11609 int physname, struct dwarf2_cu *cu)
63d06c5c 11610{
f55ee35c 11611 const char *lead = "";
5c315b68 11612 const char *sep;
63d06c5c 11613
987504bb
JJ
11614 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11615 sep = "";
11616 else if (cu->language == language_java)
11617 sep = ".";
f55ee35c
JK
11618 else if (cu->language == language_fortran && physname)
11619 {
11620 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11621 DW_AT_MIPS_linkage_name is preferred and used instead. */
11622
11623 lead = "__";
11624 sep = "_MOD_";
11625 }
987504bb
JJ
11626 else
11627 sep = "::";
63d06c5c 11628
6dd47d34
DE
11629 if (prefix == NULL)
11630 prefix = "";
11631 if (suffix == NULL)
11632 suffix = "";
11633
987504bb
JJ
11634 if (obs == NULL)
11635 {
11636 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11637
f55ee35c
JK
11638 strcpy (retval, lead);
11639 strcat (retval, prefix);
6dd47d34
DE
11640 strcat (retval, sep);
11641 strcat (retval, suffix);
63d06c5c
DC
11642 return retval;
11643 }
987504bb
JJ
11644 else
11645 {
11646 /* We have an obstack. */
f55ee35c 11647 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11648 }
63d06c5c
DC
11649}
11650
c906108c
SS
11651/* Return sibling of die, NULL if no sibling. */
11652
f9aca02d 11653static struct die_info *
fba45db2 11654sibling_die (struct die_info *die)
c906108c 11655{
639d11d3 11656 return die->sibling;
c906108c
SS
11657}
11658
71c25dea
TT
11659/* Get name of a die, return NULL if not found. */
11660
11661static char *
11662dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11663 struct obstack *obstack)
11664{
11665 if (name && cu->language == language_cplus)
11666 {
11667 char *canon_name = cp_canonicalize_string (name);
11668
11669 if (canon_name != NULL)
11670 {
11671 if (strcmp (canon_name, name) != 0)
11672 name = obsavestring (canon_name, strlen (canon_name),
11673 obstack);
11674 xfree (canon_name);
11675 }
11676 }
11677
11678 return name;
c906108c
SS
11679}
11680
9219021c
DC
11681/* Get name of a die, return NULL if not found. */
11682
11683static char *
e142c38c 11684dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11685{
11686 struct attribute *attr;
11687
e142c38c 11688 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11689 if (!attr || !DW_STRING (attr))
11690 return NULL;
11691
11692 switch (die->tag)
11693 {
11694 case DW_TAG_compile_unit:
11695 /* Compilation units have a DW_AT_name that is a filename, not
11696 a source language identifier. */
11697 case DW_TAG_enumeration_type:
11698 case DW_TAG_enumerator:
11699 /* These tags always have simple identifiers already; no need
11700 to canonicalize them. */
11701 return DW_STRING (attr);
907af001 11702
418835cc
KS
11703 case DW_TAG_subprogram:
11704 /* Java constructors will all be named "<init>", so return
11705 the class name when we see this special case. */
11706 if (cu->language == language_java
11707 && DW_STRING (attr) != NULL
11708 && strcmp (DW_STRING (attr), "<init>") == 0)
11709 {
11710 struct dwarf2_cu *spec_cu = cu;
11711 struct die_info *spec_die;
11712
11713 /* GCJ will output '<init>' for Java constructor names.
11714 For this special case, return the name of the parent class. */
11715
11716 /* GCJ may output suprogram DIEs with AT_specification set.
11717 If so, use the name of the specified DIE. */
11718 spec_die = die_specification (die, &spec_cu);
11719 if (spec_die != NULL)
11720 return dwarf2_name (spec_die, spec_cu);
11721
11722 do
11723 {
11724 die = die->parent;
11725 if (die->tag == DW_TAG_class_type)
11726 return dwarf2_name (die, cu);
11727 }
11728 while (die->tag != DW_TAG_compile_unit);
11729 }
907af001
UW
11730 break;
11731
11732 case DW_TAG_class_type:
11733 case DW_TAG_interface_type:
11734 case DW_TAG_structure_type:
11735 case DW_TAG_union_type:
11736 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11737 structures or unions. These were of the form "._%d" in GCC 4.1,
11738 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11739 and GCC 4.4. We work around this problem by ignoring these. */
11740 if (strncmp (DW_STRING (attr), "._", 2) == 0
11741 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11742 return NULL;
11743 break;
11744
71c25dea 11745 default:
907af001
UW
11746 break;
11747 }
11748
11749 if (!DW_STRING_IS_CANONICAL (attr))
11750 {
11751 DW_STRING (attr)
11752 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11753 &cu->objfile->objfile_obstack);
11754 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11755 }
907af001 11756 return DW_STRING (attr);
9219021c
DC
11757}
11758
11759/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11760 is none. *EXT_CU is the CU containing DIE on input, and the CU
11761 containing the return value on output. */
9219021c
DC
11762
11763static struct die_info *
f2f0e013 11764dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11765{
11766 struct attribute *attr;
9219021c 11767
f2f0e013 11768 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11769 if (attr == NULL)
11770 return NULL;
11771
f2f0e013 11772 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11773}
11774
c906108c
SS
11775/* Convert a DIE tag into its string name. */
11776
11777static char *
aa1ee363 11778dwarf_tag_name (unsigned tag)
c906108c
SS
11779{
11780 switch (tag)
11781 {
11782 case DW_TAG_padding:
11783 return "DW_TAG_padding";
11784 case DW_TAG_array_type:
11785 return "DW_TAG_array_type";
11786 case DW_TAG_class_type:
11787 return "DW_TAG_class_type";
11788 case DW_TAG_entry_point:
11789 return "DW_TAG_entry_point";
11790 case DW_TAG_enumeration_type:
11791 return "DW_TAG_enumeration_type";
11792 case DW_TAG_formal_parameter:
11793 return "DW_TAG_formal_parameter";
11794 case DW_TAG_imported_declaration:
11795 return "DW_TAG_imported_declaration";
11796 case DW_TAG_label:
11797 return "DW_TAG_label";
11798 case DW_TAG_lexical_block:
11799 return "DW_TAG_lexical_block";
11800 case DW_TAG_member:
11801 return "DW_TAG_member";
11802 case DW_TAG_pointer_type:
11803 return "DW_TAG_pointer_type";
11804 case DW_TAG_reference_type:
11805 return "DW_TAG_reference_type";
11806 case DW_TAG_compile_unit:
11807 return "DW_TAG_compile_unit";
11808 case DW_TAG_string_type:
11809 return "DW_TAG_string_type";
11810 case DW_TAG_structure_type:
11811 return "DW_TAG_structure_type";
11812 case DW_TAG_subroutine_type:
11813 return "DW_TAG_subroutine_type";
11814 case DW_TAG_typedef:
11815 return "DW_TAG_typedef";
11816 case DW_TAG_union_type:
11817 return "DW_TAG_union_type";
11818 case DW_TAG_unspecified_parameters:
11819 return "DW_TAG_unspecified_parameters";
11820 case DW_TAG_variant:
11821 return "DW_TAG_variant";
11822 case DW_TAG_common_block:
11823 return "DW_TAG_common_block";
11824 case DW_TAG_common_inclusion:
11825 return "DW_TAG_common_inclusion";
11826 case DW_TAG_inheritance:
11827 return "DW_TAG_inheritance";
11828 case DW_TAG_inlined_subroutine:
11829 return "DW_TAG_inlined_subroutine";
11830 case DW_TAG_module:
11831 return "DW_TAG_module";
11832 case DW_TAG_ptr_to_member_type:
11833 return "DW_TAG_ptr_to_member_type";
11834 case DW_TAG_set_type:
11835 return "DW_TAG_set_type";
11836 case DW_TAG_subrange_type:
11837 return "DW_TAG_subrange_type";
11838 case DW_TAG_with_stmt:
11839 return "DW_TAG_with_stmt";
11840 case DW_TAG_access_declaration:
11841 return "DW_TAG_access_declaration";
11842 case DW_TAG_base_type:
11843 return "DW_TAG_base_type";
11844 case DW_TAG_catch_block:
11845 return "DW_TAG_catch_block";
11846 case DW_TAG_const_type:
11847 return "DW_TAG_const_type";
11848 case DW_TAG_constant:
11849 return "DW_TAG_constant";
11850 case DW_TAG_enumerator:
11851 return "DW_TAG_enumerator";
11852 case DW_TAG_file_type:
11853 return "DW_TAG_file_type";
11854 case DW_TAG_friend:
11855 return "DW_TAG_friend";
11856 case DW_TAG_namelist:
11857 return "DW_TAG_namelist";
11858 case DW_TAG_namelist_item:
11859 return "DW_TAG_namelist_item";
11860 case DW_TAG_packed_type:
11861 return "DW_TAG_packed_type";
11862 case DW_TAG_subprogram:
11863 return "DW_TAG_subprogram";
11864 case DW_TAG_template_type_param:
11865 return "DW_TAG_template_type_param";
11866 case DW_TAG_template_value_param:
11867 return "DW_TAG_template_value_param";
11868 case DW_TAG_thrown_type:
11869 return "DW_TAG_thrown_type";
11870 case DW_TAG_try_block:
11871 return "DW_TAG_try_block";
11872 case DW_TAG_variant_part:
11873 return "DW_TAG_variant_part";
11874 case DW_TAG_variable:
11875 return "DW_TAG_variable";
11876 case DW_TAG_volatile_type:
11877 return "DW_TAG_volatile_type";
d9fa45fe
DC
11878 case DW_TAG_dwarf_procedure:
11879 return "DW_TAG_dwarf_procedure";
11880 case DW_TAG_restrict_type:
11881 return "DW_TAG_restrict_type";
11882 case DW_TAG_interface_type:
11883 return "DW_TAG_interface_type";
11884 case DW_TAG_namespace:
11885 return "DW_TAG_namespace";
11886 case DW_TAG_imported_module:
11887 return "DW_TAG_imported_module";
11888 case DW_TAG_unspecified_type:
11889 return "DW_TAG_unspecified_type";
11890 case DW_TAG_partial_unit:
11891 return "DW_TAG_partial_unit";
11892 case DW_TAG_imported_unit:
11893 return "DW_TAG_imported_unit";
b7619582
GF
11894 case DW_TAG_condition:
11895 return "DW_TAG_condition";
11896 case DW_TAG_shared_type:
11897 return "DW_TAG_shared_type";
348e048f
DE
11898 case DW_TAG_type_unit:
11899 return "DW_TAG_type_unit";
c906108c
SS
11900 case DW_TAG_MIPS_loop:
11901 return "DW_TAG_MIPS_loop";
b7619582
GF
11902 case DW_TAG_HP_array_descriptor:
11903 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11904 case DW_TAG_format_label:
11905 return "DW_TAG_format_label";
11906 case DW_TAG_function_template:
11907 return "DW_TAG_function_template";
11908 case DW_TAG_class_template:
11909 return "DW_TAG_class_template";
b7619582
GF
11910 case DW_TAG_GNU_BINCL:
11911 return "DW_TAG_GNU_BINCL";
11912 case DW_TAG_GNU_EINCL:
11913 return "DW_TAG_GNU_EINCL";
11914 case DW_TAG_upc_shared_type:
11915 return "DW_TAG_upc_shared_type";
11916 case DW_TAG_upc_strict_type:
11917 return "DW_TAG_upc_strict_type";
11918 case DW_TAG_upc_relaxed_type:
11919 return "DW_TAG_upc_relaxed_type";
11920 case DW_TAG_PGI_kanji_type:
11921 return "DW_TAG_PGI_kanji_type";
11922 case DW_TAG_PGI_interface_block:
11923 return "DW_TAG_PGI_interface_block";
c906108c
SS
11924 default:
11925 return "DW_TAG_<unknown>";
11926 }
11927}
11928
11929/* Convert a DWARF attribute code into its string name. */
11930
11931static char *
aa1ee363 11932dwarf_attr_name (unsigned attr)
c906108c
SS
11933{
11934 switch (attr)
11935 {
11936 case DW_AT_sibling:
11937 return "DW_AT_sibling";
11938 case DW_AT_location:
11939 return "DW_AT_location";
11940 case DW_AT_name:
11941 return "DW_AT_name";
11942 case DW_AT_ordering:
11943 return "DW_AT_ordering";
11944 case DW_AT_subscr_data:
11945 return "DW_AT_subscr_data";
11946 case DW_AT_byte_size:
11947 return "DW_AT_byte_size";
11948 case DW_AT_bit_offset:
11949 return "DW_AT_bit_offset";
11950 case DW_AT_bit_size:
11951 return "DW_AT_bit_size";
11952 case DW_AT_element_list:
11953 return "DW_AT_element_list";
11954 case DW_AT_stmt_list:
11955 return "DW_AT_stmt_list";
11956 case DW_AT_low_pc:
11957 return "DW_AT_low_pc";
11958 case DW_AT_high_pc:
11959 return "DW_AT_high_pc";
11960 case DW_AT_language:
11961 return "DW_AT_language";
11962 case DW_AT_member:
11963 return "DW_AT_member";
11964 case DW_AT_discr:
11965 return "DW_AT_discr";
11966 case DW_AT_discr_value:
11967 return "DW_AT_discr_value";
11968 case DW_AT_visibility:
11969 return "DW_AT_visibility";
11970 case DW_AT_import:
11971 return "DW_AT_import";
11972 case DW_AT_string_length:
11973 return "DW_AT_string_length";
11974 case DW_AT_common_reference:
11975 return "DW_AT_common_reference";
11976 case DW_AT_comp_dir:
11977 return "DW_AT_comp_dir";
11978 case DW_AT_const_value:
11979 return "DW_AT_const_value";
11980 case DW_AT_containing_type:
11981 return "DW_AT_containing_type";
11982 case DW_AT_default_value:
11983 return "DW_AT_default_value";
11984 case DW_AT_inline:
11985 return "DW_AT_inline";
11986 case DW_AT_is_optional:
11987 return "DW_AT_is_optional";
11988 case DW_AT_lower_bound:
11989 return "DW_AT_lower_bound";
11990 case DW_AT_producer:
11991 return "DW_AT_producer";
11992 case DW_AT_prototyped:
11993 return "DW_AT_prototyped";
11994 case DW_AT_return_addr:
11995 return "DW_AT_return_addr";
11996 case DW_AT_start_scope:
11997 return "DW_AT_start_scope";
09fa0d7c
JK
11998 case DW_AT_bit_stride:
11999 return "DW_AT_bit_stride";
c906108c
SS
12000 case DW_AT_upper_bound:
12001 return "DW_AT_upper_bound";
12002 case DW_AT_abstract_origin:
12003 return "DW_AT_abstract_origin";
12004 case DW_AT_accessibility:
12005 return "DW_AT_accessibility";
12006 case DW_AT_address_class:
12007 return "DW_AT_address_class";
12008 case DW_AT_artificial:
12009 return "DW_AT_artificial";
12010 case DW_AT_base_types:
12011 return "DW_AT_base_types";
12012 case DW_AT_calling_convention:
12013 return "DW_AT_calling_convention";
12014 case DW_AT_count:
12015 return "DW_AT_count";
12016 case DW_AT_data_member_location:
12017 return "DW_AT_data_member_location";
12018 case DW_AT_decl_column:
12019 return "DW_AT_decl_column";
12020 case DW_AT_decl_file:
12021 return "DW_AT_decl_file";
12022 case DW_AT_decl_line:
12023 return "DW_AT_decl_line";
12024 case DW_AT_declaration:
12025 return "DW_AT_declaration";
12026 case DW_AT_discr_list:
12027 return "DW_AT_discr_list";
12028 case DW_AT_encoding:
12029 return "DW_AT_encoding";
12030 case DW_AT_external:
12031 return "DW_AT_external";
12032 case DW_AT_frame_base:
12033 return "DW_AT_frame_base";
12034 case DW_AT_friend:
12035 return "DW_AT_friend";
12036 case DW_AT_identifier_case:
12037 return "DW_AT_identifier_case";
12038 case DW_AT_macro_info:
12039 return "DW_AT_macro_info";
12040 case DW_AT_namelist_items:
12041 return "DW_AT_namelist_items";
12042 case DW_AT_priority:
12043 return "DW_AT_priority";
12044 case DW_AT_segment:
12045 return "DW_AT_segment";
12046 case DW_AT_specification:
12047 return "DW_AT_specification";
12048 case DW_AT_static_link:
12049 return "DW_AT_static_link";
12050 case DW_AT_type:
12051 return "DW_AT_type";
12052 case DW_AT_use_location:
12053 return "DW_AT_use_location";
12054 case DW_AT_variable_parameter:
12055 return "DW_AT_variable_parameter";
12056 case DW_AT_virtuality:
12057 return "DW_AT_virtuality";
12058 case DW_AT_vtable_elem_location:
12059 return "DW_AT_vtable_elem_location";
b7619582 12060 /* DWARF 3 values. */
d9fa45fe
DC
12061 case DW_AT_allocated:
12062 return "DW_AT_allocated";
12063 case DW_AT_associated:
12064 return "DW_AT_associated";
12065 case DW_AT_data_location:
12066 return "DW_AT_data_location";
09fa0d7c
JK
12067 case DW_AT_byte_stride:
12068 return "DW_AT_byte_stride";
d9fa45fe
DC
12069 case DW_AT_entry_pc:
12070 return "DW_AT_entry_pc";
12071 case DW_AT_use_UTF8:
12072 return "DW_AT_use_UTF8";
12073 case DW_AT_extension:
12074 return "DW_AT_extension";
12075 case DW_AT_ranges:
12076 return "DW_AT_ranges";
12077 case DW_AT_trampoline:
12078 return "DW_AT_trampoline";
12079 case DW_AT_call_column:
12080 return "DW_AT_call_column";
12081 case DW_AT_call_file:
12082 return "DW_AT_call_file";
12083 case DW_AT_call_line:
12084 return "DW_AT_call_line";
b7619582
GF
12085 case DW_AT_description:
12086 return "DW_AT_description";
12087 case DW_AT_binary_scale:
12088 return "DW_AT_binary_scale";
12089 case DW_AT_decimal_scale:
12090 return "DW_AT_decimal_scale";
12091 case DW_AT_small:
12092 return "DW_AT_small";
12093 case DW_AT_decimal_sign:
12094 return "DW_AT_decimal_sign";
12095 case DW_AT_digit_count:
12096 return "DW_AT_digit_count";
12097 case DW_AT_picture_string:
12098 return "DW_AT_picture_string";
12099 case DW_AT_mutable:
12100 return "DW_AT_mutable";
12101 case DW_AT_threads_scaled:
12102 return "DW_AT_threads_scaled";
12103 case DW_AT_explicit:
12104 return "DW_AT_explicit";
12105 case DW_AT_object_pointer:
12106 return "DW_AT_object_pointer";
12107 case DW_AT_endianity:
12108 return "DW_AT_endianity";
12109 case DW_AT_elemental:
12110 return "DW_AT_elemental";
12111 case DW_AT_pure:
12112 return "DW_AT_pure";
12113 case DW_AT_recursive:
12114 return "DW_AT_recursive";
348e048f
DE
12115 /* DWARF 4 values. */
12116 case DW_AT_signature:
12117 return "DW_AT_signature";
31ef98ae
TT
12118 case DW_AT_linkage_name:
12119 return "DW_AT_linkage_name";
b7619582 12120 /* SGI/MIPS extensions. */
c764a876 12121#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12122 case DW_AT_MIPS_fde:
12123 return "DW_AT_MIPS_fde";
c764a876 12124#endif
c906108c
SS
12125 case DW_AT_MIPS_loop_begin:
12126 return "DW_AT_MIPS_loop_begin";
12127 case DW_AT_MIPS_tail_loop_begin:
12128 return "DW_AT_MIPS_tail_loop_begin";
12129 case DW_AT_MIPS_epilog_begin:
12130 return "DW_AT_MIPS_epilog_begin";
12131 case DW_AT_MIPS_loop_unroll_factor:
12132 return "DW_AT_MIPS_loop_unroll_factor";
12133 case DW_AT_MIPS_software_pipeline_depth:
12134 return "DW_AT_MIPS_software_pipeline_depth";
12135 case DW_AT_MIPS_linkage_name:
12136 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12137 case DW_AT_MIPS_stride:
12138 return "DW_AT_MIPS_stride";
12139 case DW_AT_MIPS_abstract_name:
12140 return "DW_AT_MIPS_abstract_name";
12141 case DW_AT_MIPS_clone_origin:
12142 return "DW_AT_MIPS_clone_origin";
12143 case DW_AT_MIPS_has_inlines:
12144 return "DW_AT_MIPS_has_inlines";
b7619582 12145 /* HP extensions. */
c764a876 12146#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12147 case DW_AT_HP_block_index:
12148 return "DW_AT_HP_block_index";
c764a876 12149#endif
b7619582
GF
12150 case DW_AT_HP_unmodifiable:
12151 return "DW_AT_HP_unmodifiable";
12152 case DW_AT_HP_actuals_stmt_list:
12153 return "DW_AT_HP_actuals_stmt_list";
12154 case DW_AT_HP_proc_per_section:
12155 return "DW_AT_HP_proc_per_section";
12156 case DW_AT_HP_raw_data_ptr:
12157 return "DW_AT_HP_raw_data_ptr";
12158 case DW_AT_HP_pass_by_reference:
12159 return "DW_AT_HP_pass_by_reference";
12160 case DW_AT_HP_opt_level:
12161 return "DW_AT_HP_opt_level";
12162 case DW_AT_HP_prof_version_id:
12163 return "DW_AT_HP_prof_version_id";
12164 case DW_AT_HP_opt_flags:
12165 return "DW_AT_HP_opt_flags";
12166 case DW_AT_HP_cold_region_low_pc:
12167 return "DW_AT_HP_cold_region_low_pc";
12168 case DW_AT_HP_cold_region_high_pc:
12169 return "DW_AT_HP_cold_region_high_pc";
12170 case DW_AT_HP_all_variables_modifiable:
12171 return "DW_AT_HP_all_variables_modifiable";
12172 case DW_AT_HP_linkage_name:
12173 return "DW_AT_HP_linkage_name";
12174 case DW_AT_HP_prof_flags:
12175 return "DW_AT_HP_prof_flags";
12176 /* GNU extensions. */
c906108c
SS
12177 case DW_AT_sf_names:
12178 return "DW_AT_sf_names";
12179 case DW_AT_src_info:
12180 return "DW_AT_src_info";
12181 case DW_AT_mac_info:
12182 return "DW_AT_mac_info";
12183 case DW_AT_src_coords:
12184 return "DW_AT_src_coords";
12185 case DW_AT_body_begin:
12186 return "DW_AT_body_begin";
12187 case DW_AT_body_end:
12188 return "DW_AT_body_end";
f5f8a009
EZ
12189 case DW_AT_GNU_vector:
12190 return "DW_AT_GNU_vector";
2de00c64
DE
12191 case DW_AT_GNU_odr_signature:
12192 return "DW_AT_GNU_odr_signature";
b7619582
GF
12193 /* VMS extensions. */
12194 case DW_AT_VMS_rtnbeg_pd_address:
12195 return "DW_AT_VMS_rtnbeg_pd_address";
12196 /* UPC extension. */
12197 case DW_AT_upc_threads_scaled:
12198 return "DW_AT_upc_threads_scaled";
12199 /* PGI (STMicroelectronics) extensions. */
12200 case DW_AT_PGI_lbase:
12201 return "DW_AT_PGI_lbase";
12202 case DW_AT_PGI_soffset:
12203 return "DW_AT_PGI_soffset";
12204 case DW_AT_PGI_lstride:
12205 return "DW_AT_PGI_lstride";
c906108c
SS
12206 default:
12207 return "DW_AT_<unknown>";
12208 }
12209}
12210
12211/* Convert a DWARF value form code into its string name. */
12212
12213static char *
aa1ee363 12214dwarf_form_name (unsigned form)
c906108c
SS
12215{
12216 switch (form)
12217 {
12218 case DW_FORM_addr:
12219 return "DW_FORM_addr";
12220 case DW_FORM_block2:
12221 return "DW_FORM_block2";
12222 case DW_FORM_block4:
12223 return "DW_FORM_block4";
12224 case DW_FORM_data2:
12225 return "DW_FORM_data2";
12226 case DW_FORM_data4:
12227 return "DW_FORM_data4";
12228 case DW_FORM_data8:
12229 return "DW_FORM_data8";
12230 case DW_FORM_string:
12231 return "DW_FORM_string";
12232 case DW_FORM_block:
12233 return "DW_FORM_block";
12234 case DW_FORM_block1:
12235 return "DW_FORM_block1";
12236 case DW_FORM_data1:
12237 return "DW_FORM_data1";
12238 case DW_FORM_flag:
12239 return "DW_FORM_flag";
12240 case DW_FORM_sdata:
12241 return "DW_FORM_sdata";
12242 case DW_FORM_strp:
12243 return "DW_FORM_strp";
12244 case DW_FORM_udata:
12245 return "DW_FORM_udata";
12246 case DW_FORM_ref_addr:
12247 return "DW_FORM_ref_addr";
12248 case DW_FORM_ref1:
12249 return "DW_FORM_ref1";
12250 case DW_FORM_ref2:
12251 return "DW_FORM_ref2";
12252 case DW_FORM_ref4:
12253 return "DW_FORM_ref4";
12254 case DW_FORM_ref8:
12255 return "DW_FORM_ref8";
12256 case DW_FORM_ref_udata:
12257 return "DW_FORM_ref_udata";
12258 case DW_FORM_indirect:
12259 return "DW_FORM_indirect";
348e048f
DE
12260 case DW_FORM_sec_offset:
12261 return "DW_FORM_sec_offset";
12262 case DW_FORM_exprloc:
12263 return "DW_FORM_exprloc";
12264 case DW_FORM_flag_present:
12265 return "DW_FORM_flag_present";
12266 case DW_FORM_sig8:
12267 return "DW_FORM_sig8";
c906108c
SS
12268 default:
12269 return "DW_FORM_<unknown>";
12270 }
12271}
12272
12273/* Convert a DWARF stack opcode into its string name. */
12274
9eae7c52
TT
12275const char *
12276dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12277{
12278 switch (op)
12279 {
12280 case DW_OP_addr:
12281 return "DW_OP_addr";
12282 case DW_OP_deref:
12283 return "DW_OP_deref";
12284 case DW_OP_const1u:
12285 return "DW_OP_const1u";
12286 case DW_OP_const1s:
12287 return "DW_OP_const1s";
12288 case DW_OP_const2u:
12289 return "DW_OP_const2u";
12290 case DW_OP_const2s:
12291 return "DW_OP_const2s";
12292 case DW_OP_const4u:
12293 return "DW_OP_const4u";
12294 case DW_OP_const4s:
12295 return "DW_OP_const4s";
12296 case DW_OP_const8u:
12297 return "DW_OP_const8u";
12298 case DW_OP_const8s:
12299 return "DW_OP_const8s";
12300 case DW_OP_constu:
12301 return "DW_OP_constu";
12302 case DW_OP_consts:
12303 return "DW_OP_consts";
12304 case DW_OP_dup:
12305 return "DW_OP_dup";
12306 case DW_OP_drop:
12307 return "DW_OP_drop";
12308 case DW_OP_over:
12309 return "DW_OP_over";
12310 case DW_OP_pick:
12311 return "DW_OP_pick";
12312 case DW_OP_swap:
12313 return "DW_OP_swap";
12314 case DW_OP_rot:
12315 return "DW_OP_rot";
12316 case DW_OP_xderef:
12317 return "DW_OP_xderef";
12318 case DW_OP_abs:
12319 return "DW_OP_abs";
12320 case DW_OP_and:
12321 return "DW_OP_and";
12322 case DW_OP_div:
12323 return "DW_OP_div";
12324 case DW_OP_minus:
12325 return "DW_OP_minus";
12326 case DW_OP_mod:
12327 return "DW_OP_mod";
12328 case DW_OP_mul:
12329 return "DW_OP_mul";
12330 case DW_OP_neg:
12331 return "DW_OP_neg";
12332 case DW_OP_not:
12333 return "DW_OP_not";
12334 case DW_OP_or:
12335 return "DW_OP_or";
12336 case DW_OP_plus:
12337 return "DW_OP_plus";
12338 case DW_OP_plus_uconst:
12339 return "DW_OP_plus_uconst";
12340 case DW_OP_shl:
12341 return "DW_OP_shl";
12342 case DW_OP_shr:
12343 return "DW_OP_shr";
12344 case DW_OP_shra:
12345 return "DW_OP_shra";
12346 case DW_OP_xor:
12347 return "DW_OP_xor";
12348 case DW_OP_bra:
12349 return "DW_OP_bra";
12350 case DW_OP_eq:
12351 return "DW_OP_eq";
12352 case DW_OP_ge:
12353 return "DW_OP_ge";
12354 case DW_OP_gt:
12355 return "DW_OP_gt";
12356 case DW_OP_le:
12357 return "DW_OP_le";
12358 case DW_OP_lt:
12359 return "DW_OP_lt";
12360 case DW_OP_ne:
12361 return "DW_OP_ne";
12362 case DW_OP_skip:
12363 return "DW_OP_skip";
12364 case DW_OP_lit0:
12365 return "DW_OP_lit0";
12366 case DW_OP_lit1:
12367 return "DW_OP_lit1";
12368 case DW_OP_lit2:
12369 return "DW_OP_lit2";
12370 case DW_OP_lit3:
12371 return "DW_OP_lit3";
12372 case DW_OP_lit4:
12373 return "DW_OP_lit4";
12374 case DW_OP_lit5:
12375 return "DW_OP_lit5";
12376 case DW_OP_lit6:
12377 return "DW_OP_lit6";
12378 case DW_OP_lit7:
12379 return "DW_OP_lit7";
12380 case DW_OP_lit8:
12381 return "DW_OP_lit8";
12382 case DW_OP_lit9:
12383 return "DW_OP_lit9";
12384 case DW_OP_lit10:
12385 return "DW_OP_lit10";
12386 case DW_OP_lit11:
12387 return "DW_OP_lit11";
12388 case DW_OP_lit12:
12389 return "DW_OP_lit12";
12390 case DW_OP_lit13:
12391 return "DW_OP_lit13";
12392 case DW_OP_lit14:
12393 return "DW_OP_lit14";
12394 case DW_OP_lit15:
12395 return "DW_OP_lit15";
12396 case DW_OP_lit16:
12397 return "DW_OP_lit16";
12398 case DW_OP_lit17:
12399 return "DW_OP_lit17";
12400 case DW_OP_lit18:
12401 return "DW_OP_lit18";
12402 case DW_OP_lit19:
12403 return "DW_OP_lit19";
12404 case DW_OP_lit20:
12405 return "DW_OP_lit20";
12406 case DW_OP_lit21:
12407 return "DW_OP_lit21";
12408 case DW_OP_lit22:
12409 return "DW_OP_lit22";
12410 case DW_OP_lit23:
12411 return "DW_OP_lit23";
12412 case DW_OP_lit24:
12413 return "DW_OP_lit24";
12414 case DW_OP_lit25:
12415 return "DW_OP_lit25";
12416 case DW_OP_lit26:
12417 return "DW_OP_lit26";
12418 case DW_OP_lit27:
12419 return "DW_OP_lit27";
12420 case DW_OP_lit28:
12421 return "DW_OP_lit28";
12422 case DW_OP_lit29:
12423 return "DW_OP_lit29";
12424 case DW_OP_lit30:
12425 return "DW_OP_lit30";
12426 case DW_OP_lit31:
12427 return "DW_OP_lit31";
12428 case DW_OP_reg0:
12429 return "DW_OP_reg0";
12430 case DW_OP_reg1:
12431 return "DW_OP_reg1";
12432 case DW_OP_reg2:
12433 return "DW_OP_reg2";
12434 case DW_OP_reg3:
12435 return "DW_OP_reg3";
12436 case DW_OP_reg4:
12437 return "DW_OP_reg4";
12438 case DW_OP_reg5:
12439 return "DW_OP_reg5";
12440 case DW_OP_reg6:
12441 return "DW_OP_reg6";
12442 case DW_OP_reg7:
12443 return "DW_OP_reg7";
12444 case DW_OP_reg8:
12445 return "DW_OP_reg8";
12446 case DW_OP_reg9:
12447 return "DW_OP_reg9";
12448 case DW_OP_reg10:
12449 return "DW_OP_reg10";
12450 case DW_OP_reg11:
12451 return "DW_OP_reg11";
12452 case DW_OP_reg12:
12453 return "DW_OP_reg12";
12454 case DW_OP_reg13:
12455 return "DW_OP_reg13";
12456 case DW_OP_reg14:
12457 return "DW_OP_reg14";
12458 case DW_OP_reg15:
12459 return "DW_OP_reg15";
12460 case DW_OP_reg16:
12461 return "DW_OP_reg16";
12462 case DW_OP_reg17:
12463 return "DW_OP_reg17";
12464 case DW_OP_reg18:
12465 return "DW_OP_reg18";
12466 case DW_OP_reg19:
12467 return "DW_OP_reg19";
12468 case DW_OP_reg20:
12469 return "DW_OP_reg20";
12470 case DW_OP_reg21:
12471 return "DW_OP_reg21";
12472 case DW_OP_reg22:
12473 return "DW_OP_reg22";
12474 case DW_OP_reg23:
12475 return "DW_OP_reg23";
12476 case DW_OP_reg24:
12477 return "DW_OP_reg24";
12478 case DW_OP_reg25:
12479 return "DW_OP_reg25";
12480 case DW_OP_reg26:
12481 return "DW_OP_reg26";
12482 case DW_OP_reg27:
12483 return "DW_OP_reg27";
12484 case DW_OP_reg28:
12485 return "DW_OP_reg28";
12486 case DW_OP_reg29:
12487 return "DW_OP_reg29";
12488 case DW_OP_reg30:
12489 return "DW_OP_reg30";
12490 case DW_OP_reg31:
12491 return "DW_OP_reg31";
12492 case DW_OP_breg0:
12493 return "DW_OP_breg0";
12494 case DW_OP_breg1:
12495 return "DW_OP_breg1";
12496 case DW_OP_breg2:
12497 return "DW_OP_breg2";
12498 case DW_OP_breg3:
12499 return "DW_OP_breg3";
12500 case DW_OP_breg4:
12501 return "DW_OP_breg4";
12502 case DW_OP_breg5:
12503 return "DW_OP_breg5";
12504 case DW_OP_breg6:
12505 return "DW_OP_breg6";
12506 case DW_OP_breg7:
12507 return "DW_OP_breg7";
12508 case DW_OP_breg8:
12509 return "DW_OP_breg8";
12510 case DW_OP_breg9:
12511 return "DW_OP_breg9";
12512 case DW_OP_breg10:
12513 return "DW_OP_breg10";
12514 case DW_OP_breg11:
12515 return "DW_OP_breg11";
12516 case DW_OP_breg12:
12517 return "DW_OP_breg12";
12518 case DW_OP_breg13:
12519 return "DW_OP_breg13";
12520 case DW_OP_breg14:
12521 return "DW_OP_breg14";
12522 case DW_OP_breg15:
12523 return "DW_OP_breg15";
12524 case DW_OP_breg16:
12525 return "DW_OP_breg16";
12526 case DW_OP_breg17:
12527 return "DW_OP_breg17";
12528 case DW_OP_breg18:
12529 return "DW_OP_breg18";
12530 case DW_OP_breg19:
12531 return "DW_OP_breg19";
12532 case DW_OP_breg20:
12533 return "DW_OP_breg20";
12534 case DW_OP_breg21:
12535 return "DW_OP_breg21";
12536 case DW_OP_breg22:
12537 return "DW_OP_breg22";
12538 case DW_OP_breg23:
12539 return "DW_OP_breg23";
12540 case DW_OP_breg24:
12541 return "DW_OP_breg24";
12542 case DW_OP_breg25:
12543 return "DW_OP_breg25";
12544 case DW_OP_breg26:
12545 return "DW_OP_breg26";
12546 case DW_OP_breg27:
12547 return "DW_OP_breg27";
12548 case DW_OP_breg28:
12549 return "DW_OP_breg28";
12550 case DW_OP_breg29:
12551 return "DW_OP_breg29";
12552 case DW_OP_breg30:
12553 return "DW_OP_breg30";
12554 case DW_OP_breg31:
12555 return "DW_OP_breg31";
12556 case DW_OP_regx:
12557 return "DW_OP_regx";
12558 case DW_OP_fbreg:
12559 return "DW_OP_fbreg";
12560 case DW_OP_bregx:
12561 return "DW_OP_bregx";
12562 case DW_OP_piece:
12563 return "DW_OP_piece";
12564 case DW_OP_deref_size:
12565 return "DW_OP_deref_size";
12566 case DW_OP_xderef_size:
12567 return "DW_OP_xderef_size";
12568 case DW_OP_nop:
12569 return "DW_OP_nop";
b7619582 12570 /* DWARF 3 extensions. */
ed348acc
EZ
12571 case DW_OP_push_object_address:
12572 return "DW_OP_push_object_address";
12573 case DW_OP_call2:
12574 return "DW_OP_call2";
12575 case DW_OP_call4:
12576 return "DW_OP_call4";
12577 case DW_OP_call_ref:
12578 return "DW_OP_call_ref";
b7619582
GF
12579 case DW_OP_form_tls_address:
12580 return "DW_OP_form_tls_address";
12581 case DW_OP_call_frame_cfa:
12582 return "DW_OP_call_frame_cfa";
12583 case DW_OP_bit_piece:
12584 return "DW_OP_bit_piece";
9eae7c52
TT
12585 /* DWARF 4 extensions. */
12586 case DW_OP_implicit_value:
12587 return "DW_OP_implicit_value";
12588 case DW_OP_stack_value:
12589 return "DW_OP_stack_value";
12590 /* GNU extensions. */
ed348acc
EZ
12591 case DW_OP_GNU_push_tls_address:
12592 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12593 case DW_OP_GNU_uninit:
12594 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12595 case DW_OP_GNU_implicit_pointer:
12596 return "DW_OP_GNU_implicit_pointer";
c906108c 12597 default:
9eae7c52 12598 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12599 }
12600}
12601
12602static char *
fba45db2 12603dwarf_bool_name (unsigned mybool)
c906108c
SS
12604{
12605 if (mybool)
12606 return "TRUE";
12607 else
12608 return "FALSE";
12609}
12610
12611/* Convert a DWARF type code into its string name. */
12612
12613static char *
aa1ee363 12614dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12615{
12616 switch (enc)
12617 {
b7619582
GF
12618 case DW_ATE_void:
12619 return "DW_ATE_void";
c906108c
SS
12620 case DW_ATE_address:
12621 return "DW_ATE_address";
12622 case DW_ATE_boolean:
12623 return "DW_ATE_boolean";
12624 case DW_ATE_complex_float:
12625 return "DW_ATE_complex_float";
12626 case DW_ATE_float:
12627 return "DW_ATE_float";
12628 case DW_ATE_signed:
12629 return "DW_ATE_signed";
12630 case DW_ATE_signed_char:
12631 return "DW_ATE_signed_char";
12632 case DW_ATE_unsigned:
12633 return "DW_ATE_unsigned";
12634 case DW_ATE_unsigned_char:
12635 return "DW_ATE_unsigned_char";
b7619582 12636 /* DWARF 3. */
d9fa45fe
DC
12637 case DW_ATE_imaginary_float:
12638 return "DW_ATE_imaginary_float";
b7619582
GF
12639 case DW_ATE_packed_decimal:
12640 return "DW_ATE_packed_decimal";
12641 case DW_ATE_numeric_string:
12642 return "DW_ATE_numeric_string";
12643 case DW_ATE_edited:
12644 return "DW_ATE_edited";
12645 case DW_ATE_signed_fixed:
12646 return "DW_ATE_signed_fixed";
12647 case DW_ATE_unsigned_fixed:
12648 return "DW_ATE_unsigned_fixed";
12649 case DW_ATE_decimal_float:
12650 return "DW_ATE_decimal_float";
75079b2b
TT
12651 /* DWARF 4. */
12652 case DW_ATE_UTF:
12653 return "DW_ATE_UTF";
b7619582
GF
12654 /* HP extensions. */
12655 case DW_ATE_HP_float80:
12656 return "DW_ATE_HP_float80";
12657 case DW_ATE_HP_complex_float80:
12658 return "DW_ATE_HP_complex_float80";
12659 case DW_ATE_HP_float128:
12660 return "DW_ATE_HP_float128";
12661 case DW_ATE_HP_complex_float128:
12662 return "DW_ATE_HP_complex_float128";
12663 case DW_ATE_HP_floathpintel:
12664 return "DW_ATE_HP_floathpintel";
12665 case DW_ATE_HP_imaginary_float80:
12666 return "DW_ATE_HP_imaginary_float80";
12667 case DW_ATE_HP_imaginary_float128:
12668 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12669 default:
12670 return "DW_ATE_<unknown>";
12671 }
12672}
12673
12674/* Convert a DWARF call frame info operation to its string name. */
12675
12676#if 0
12677static char *
aa1ee363 12678dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12679{
12680 switch (cfi_opc)
12681 {
12682 case DW_CFA_advance_loc:
12683 return "DW_CFA_advance_loc";
12684 case DW_CFA_offset:
12685 return "DW_CFA_offset";
12686 case DW_CFA_restore:
12687 return "DW_CFA_restore";
12688 case DW_CFA_nop:
12689 return "DW_CFA_nop";
12690 case DW_CFA_set_loc:
12691 return "DW_CFA_set_loc";
12692 case DW_CFA_advance_loc1:
12693 return "DW_CFA_advance_loc1";
12694 case DW_CFA_advance_loc2:
12695 return "DW_CFA_advance_loc2";
12696 case DW_CFA_advance_loc4:
12697 return "DW_CFA_advance_loc4";
12698 case DW_CFA_offset_extended:
12699 return "DW_CFA_offset_extended";
12700 case DW_CFA_restore_extended:
12701 return "DW_CFA_restore_extended";
12702 case DW_CFA_undefined:
12703 return "DW_CFA_undefined";
12704 case DW_CFA_same_value:
12705 return "DW_CFA_same_value";
12706 case DW_CFA_register:
12707 return "DW_CFA_register";
12708 case DW_CFA_remember_state:
12709 return "DW_CFA_remember_state";
12710 case DW_CFA_restore_state:
12711 return "DW_CFA_restore_state";
12712 case DW_CFA_def_cfa:
12713 return "DW_CFA_def_cfa";
12714 case DW_CFA_def_cfa_register:
12715 return "DW_CFA_def_cfa_register";
12716 case DW_CFA_def_cfa_offset:
12717 return "DW_CFA_def_cfa_offset";
b7619582 12718 /* DWARF 3. */
985cb1a3
JM
12719 case DW_CFA_def_cfa_expression:
12720 return "DW_CFA_def_cfa_expression";
12721 case DW_CFA_expression:
12722 return "DW_CFA_expression";
12723 case DW_CFA_offset_extended_sf:
12724 return "DW_CFA_offset_extended_sf";
12725 case DW_CFA_def_cfa_sf:
12726 return "DW_CFA_def_cfa_sf";
12727 case DW_CFA_def_cfa_offset_sf:
12728 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12729 case DW_CFA_val_offset:
12730 return "DW_CFA_val_offset";
12731 case DW_CFA_val_offset_sf:
12732 return "DW_CFA_val_offset_sf";
12733 case DW_CFA_val_expression:
12734 return "DW_CFA_val_expression";
12735 /* SGI/MIPS specific. */
c906108c
SS
12736 case DW_CFA_MIPS_advance_loc8:
12737 return "DW_CFA_MIPS_advance_loc8";
b7619582 12738 /* GNU extensions. */
985cb1a3
JM
12739 case DW_CFA_GNU_window_save:
12740 return "DW_CFA_GNU_window_save";
12741 case DW_CFA_GNU_args_size:
12742 return "DW_CFA_GNU_args_size";
12743 case DW_CFA_GNU_negative_offset_extended:
12744 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12745 default:
12746 return "DW_CFA_<unknown>";
12747 }
12748}
12749#endif
12750
f9aca02d 12751static void
d97bc12b 12752dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12753{
12754 unsigned int i;
12755
d97bc12b
DE
12756 print_spaces (indent, f);
12757 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12758 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12759
12760 if (die->parent != NULL)
12761 {
12762 print_spaces (indent, f);
12763 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12764 die->parent->offset);
12765 }
12766
12767 print_spaces (indent, f);
12768 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12769 dwarf_bool_name (die->child != NULL));
c906108c 12770
d97bc12b
DE
12771 print_spaces (indent, f);
12772 fprintf_unfiltered (f, " attributes:\n");
12773
c906108c
SS
12774 for (i = 0; i < die->num_attrs; ++i)
12775 {
d97bc12b
DE
12776 print_spaces (indent, f);
12777 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12778 dwarf_attr_name (die->attrs[i].name),
12779 dwarf_form_name (die->attrs[i].form));
d97bc12b 12780
c906108c
SS
12781 switch (die->attrs[i].form)
12782 {
12783 case DW_FORM_ref_addr:
12784 case DW_FORM_addr:
d97bc12b 12785 fprintf_unfiltered (f, "address: ");
5af949e3 12786 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12787 break;
12788 case DW_FORM_block2:
12789 case DW_FORM_block4:
12790 case DW_FORM_block:
12791 case DW_FORM_block1:
d97bc12b 12792 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12793 break;
2dc7f7b3
TT
12794 case DW_FORM_exprloc:
12795 fprintf_unfiltered (f, "expression: size %u",
12796 DW_BLOCK (&die->attrs[i])->size);
12797 break;
10b3939b
DJ
12798 case DW_FORM_ref1:
12799 case DW_FORM_ref2:
12800 case DW_FORM_ref4:
d97bc12b 12801 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12802 (long) (DW_ADDR (&die->attrs[i])));
12803 break;
c906108c
SS
12804 case DW_FORM_data1:
12805 case DW_FORM_data2:
12806 case DW_FORM_data4:
ce5d95e1 12807 case DW_FORM_data8:
c906108c
SS
12808 case DW_FORM_udata:
12809 case DW_FORM_sdata:
43bbcdc2
PH
12810 fprintf_unfiltered (f, "constant: %s",
12811 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12812 break;
2dc7f7b3
TT
12813 case DW_FORM_sec_offset:
12814 fprintf_unfiltered (f, "section offset: %s",
12815 pulongest (DW_UNSND (&die->attrs[i])));
12816 break;
348e048f
DE
12817 case DW_FORM_sig8:
12818 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12819 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12820 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12821 else
12822 fprintf_unfiltered (f, "signatured type, offset: unknown");
12823 break;
c906108c 12824 case DW_FORM_string:
4bdf3d34 12825 case DW_FORM_strp:
8285870a 12826 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12827 DW_STRING (&die->attrs[i])
8285870a
JK
12828 ? DW_STRING (&die->attrs[i]) : "",
12829 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12830 break;
12831 case DW_FORM_flag:
12832 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12833 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12834 else
d97bc12b 12835 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12836 break;
2dc7f7b3
TT
12837 case DW_FORM_flag_present:
12838 fprintf_unfiltered (f, "flag: TRUE");
12839 break;
a8329558
KW
12840 case DW_FORM_indirect:
12841 /* the reader will have reduced the indirect form to
12842 the "base form" so this form should not occur */
d97bc12b 12843 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12844 break;
c906108c 12845 default:
d97bc12b 12846 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12847 die->attrs[i].form);
d97bc12b 12848 break;
c906108c 12849 }
d97bc12b 12850 fprintf_unfiltered (f, "\n");
c906108c
SS
12851 }
12852}
12853
f9aca02d 12854static void
d97bc12b 12855dump_die_for_error (struct die_info *die)
c906108c 12856{
d97bc12b
DE
12857 dump_die_shallow (gdb_stderr, 0, die);
12858}
12859
12860static void
12861dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12862{
12863 int indent = level * 4;
12864
12865 gdb_assert (die != NULL);
12866
12867 if (level >= max_level)
12868 return;
12869
12870 dump_die_shallow (f, indent, die);
12871
12872 if (die->child != NULL)
c906108c 12873 {
d97bc12b
DE
12874 print_spaces (indent, f);
12875 fprintf_unfiltered (f, " Children:");
12876 if (level + 1 < max_level)
12877 {
12878 fprintf_unfiltered (f, "\n");
12879 dump_die_1 (f, level + 1, max_level, die->child);
12880 }
12881 else
12882 {
12883 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12884 }
12885 }
12886
12887 if (die->sibling != NULL && level > 0)
12888 {
12889 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12890 }
12891}
12892
d97bc12b
DE
12893/* This is called from the pdie macro in gdbinit.in.
12894 It's not static so gcc will keep a copy callable from gdb. */
12895
12896void
12897dump_die (struct die_info *die, int max_level)
12898{
12899 dump_die_1 (gdb_stdlog, 0, max_level, die);
12900}
12901
f9aca02d 12902static void
51545339 12903store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12904{
51545339 12905 void **slot;
c906108c 12906
51545339
DJ
12907 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12908
12909 *slot = die;
c906108c
SS
12910}
12911
93311388
DE
12912static int
12913is_ref_attr (struct attribute *attr)
c906108c 12914{
c906108c
SS
12915 switch (attr->form)
12916 {
12917 case DW_FORM_ref_addr:
c906108c
SS
12918 case DW_FORM_ref1:
12919 case DW_FORM_ref2:
12920 case DW_FORM_ref4:
613e1657 12921 case DW_FORM_ref8:
c906108c 12922 case DW_FORM_ref_udata:
93311388 12923 return 1;
c906108c 12924 default:
93311388 12925 return 0;
c906108c 12926 }
93311388
DE
12927}
12928
12929static unsigned int
12930dwarf2_get_ref_die_offset (struct attribute *attr)
12931{
12932 if (is_ref_attr (attr))
12933 return DW_ADDR (attr);
12934
12935 complaint (&symfile_complaints,
12936 _("unsupported die ref attribute form: '%s'"),
12937 dwarf_form_name (attr->form));
12938 return 0;
c906108c
SS
12939}
12940
43bbcdc2
PH
12941/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12942 * the value held by the attribute is not constant. */
a02abb62 12943
43bbcdc2 12944static LONGEST
a02abb62
JB
12945dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12946{
12947 if (attr->form == DW_FORM_sdata)
12948 return DW_SND (attr);
12949 else if (attr->form == DW_FORM_udata
12950 || attr->form == DW_FORM_data1
12951 || attr->form == DW_FORM_data2
12952 || attr->form == DW_FORM_data4
12953 || attr->form == DW_FORM_data8)
12954 return DW_UNSND (attr);
12955 else
12956 {
e2e0b3e5 12957 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12958 dwarf_form_name (attr->form));
12959 return default_value;
12960 }
12961}
12962
03dd20cc 12963/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12964 unit and add it to our queue.
12965 The result is non-zero if PER_CU was queued, otherwise the result is zero
12966 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12967
348e048f 12968static int
03dd20cc
DJ
12969maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12970 struct dwarf2_per_cu_data *per_cu)
12971{
98bfdba5
PA
12972 /* We may arrive here during partial symbol reading, if we need full
12973 DIEs to process an unusual case (e.g. template arguments). Do
12974 not queue PER_CU, just tell our caller to load its DIEs. */
12975 if (dwarf2_per_objfile->reading_partial_symbols)
12976 {
12977 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12978 return 1;
12979 return 0;
12980 }
12981
03dd20cc
DJ
12982 /* Mark the dependence relation so that we don't flush PER_CU
12983 too early. */
12984 dwarf2_add_dependence (this_cu, per_cu);
12985
12986 /* If it's already on the queue, we have nothing to do. */
12987 if (per_cu->queued)
348e048f 12988 return 0;
03dd20cc
DJ
12989
12990 /* If the compilation unit is already loaded, just mark it as
12991 used. */
12992 if (per_cu->cu != NULL)
12993 {
12994 per_cu->cu->last_used = 0;
348e048f 12995 return 0;
03dd20cc
DJ
12996 }
12997
12998 /* Add it to the queue. */
12999 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13000
13001 return 1;
13002}
13003
13004/* Follow reference or signature attribute ATTR of SRC_DIE.
13005 On entry *REF_CU is the CU of SRC_DIE.
13006 On exit *REF_CU is the CU of the result. */
13007
13008static struct die_info *
13009follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13010 struct dwarf2_cu **ref_cu)
13011{
13012 struct die_info *die;
13013
13014 if (is_ref_attr (attr))
13015 die = follow_die_ref (src_die, attr, ref_cu);
13016 else if (attr->form == DW_FORM_sig8)
13017 die = follow_die_sig (src_die, attr, ref_cu);
13018 else
13019 {
13020 dump_die_for_error (src_die);
13021 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13022 (*ref_cu)->objfile->name);
13023 }
13024
13025 return die;
03dd20cc
DJ
13026}
13027
5c631832 13028/* Follow reference OFFSET.
673bfd45
DE
13029 On entry *REF_CU is the CU of the source die referencing OFFSET.
13030 On exit *REF_CU is the CU of the result.
13031 Returns NULL if OFFSET is invalid. */
f504f079 13032
f9aca02d 13033static struct die_info *
5c631832 13034follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13035{
10b3939b 13036 struct die_info temp_die;
f2f0e013 13037 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13038
348e048f
DE
13039 gdb_assert (cu->per_cu != NULL);
13040
98bfdba5
PA
13041 target_cu = cu;
13042
348e048f
DE
13043 if (cu->per_cu->from_debug_types)
13044 {
13045 /* .debug_types CUs cannot reference anything outside their CU.
13046 If they need to, they have to reference a signatured type via
13047 DW_FORM_sig8. */
13048 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13049 return NULL;
348e048f
DE
13050 }
13051 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13052 {
13053 struct dwarf2_per_cu_data *per_cu;
9a619af0 13054
45452591 13055 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13056
13057 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13058 if (maybe_queue_comp_unit (cu, per_cu))
13059 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13060
10b3939b
DJ
13061 target_cu = per_cu->cu;
13062 }
98bfdba5
PA
13063 else if (cu->dies == NULL)
13064 {
13065 /* We're loading full DIEs during partial symbol reading. */
13066 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13067 load_full_comp_unit (cu->per_cu, cu->objfile);
13068 }
c906108c 13069
f2f0e013 13070 *ref_cu = target_cu;
51545339 13071 temp_die.offset = offset;
5c631832
JK
13072 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13073}
10b3939b 13074
5c631832
JK
13075/* Follow reference attribute ATTR of SRC_DIE.
13076 On entry *REF_CU is the CU of SRC_DIE.
13077 On exit *REF_CU is the CU of the result. */
13078
13079static struct die_info *
13080follow_die_ref (struct die_info *src_die, struct attribute *attr,
13081 struct dwarf2_cu **ref_cu)
13082{
13083 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13084 struct dwarf2_cu *cu = *ref_cu;
13085 struct die_info *die;
13086
13087 die = follow_die_offset (offset, ref_cu);
13088 if (!die)
13089 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13090 "at 0x%x [in module %s]"),
13091 offset, src_die->offset, cu->objfile->name);
348e048f 13092
5c631832
JK
13093 return die;
13094}
13095
13096/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13097 value is intended for DW_OP_call*. */
13098
13099struct dwarf2_locexpr_baton
13100dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13101 struct dwarf2_per_cu_data *per_cu,
13102 CORE_ADDR (*get_frame_pc) (void *baton),
13103 void *baton)
5c631832
JK
13104{
13105 struct dwarf2_cu *cu = per_cu->cu;
13106 struct die_info *die;
13107 struct attribute *attr;
13108 struct dwarf2_locexpr_baton retval;
13109
8cf6f0b1
TT
13110 dw2_setup (per_cu->objfile);
13111
5c631832
JK
13112 die = follow_die_offset (offset, &cu);
13113 if (!die)
13114 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13115 offset, per_cu->cu->objfile->name);
13116
13117 attr = dwarf2_attr (die, DW_AT_location, cu);
13118 if (!attr)
13119 {
13120 /* DWARF: "If there is no such attribute, then there is no effect.". */
13121
13122 retval.data = NULL;
13123 retval.size = 0;
13124 }
8cf6f0b1
TT
13125 else if (attr_form_is_section_offset (attr))
13126 {
13127 struct dwarf2_loclist_baton loclist_baton;
13128 CORE_ADDR pc = (*get_frame_pc) (baton);
13129 size_t size;
13130
13131 fill_in_loclist_baton (cu, &loclist_baton, attr);
13132
13133 retval.data = dwarf2_find_location_expression (&loclist_baton,
13134 &size, pc);
13135 retval.size = size;
13136 }
5c631832
JK
13137 else
13138 {
13139 if (!attr_form_is_block (attr))
13140 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13141 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13142 offset, per_cu->cu->objfile->name);
13143
13144 retval.data = DW_BLOCK (attr)->data;
13145 retval.size = DW_BLOCK (attr)->size;
13146 }
13147 retval.per_cu = cu->per_cu;
13148 return retval;
348e048f
DE
13149}
13150
13151/* Follow the signature attribute ATTR in SRC_DIE.
13152 On entry *REF_CU is the CU of SRC_DIE.
13153 On exit *REF_CU is the CU of the result. */
13154
13155static struct die_info *
13156follow_die_sig (struct die_info *src_die, struct attribute *attr,
13157 struct dwarf2_cu **ref_cu)
13158{
13159 struct objfile *objfile = (*ref_cu)->objfile;
13160 struct die_info temp_die;
13161 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13162 struct dwarf2_cu *sig_cu;
13163 struct die_info *die;
13164
13165 /* sig_type will be NULL if the signatured type is missing from
13166 the debug info. */
13167 if (sig_type == NULL)
13168 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13169 "at 0x%x [in module %s]"),
13170 src_die->offset, objfile->name);
13171
13172 /* If necessary, add it to the queue and load its DIEs. */
13173
13174 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13175 read_signatured_type (objfile, sig_type);
13176
13177 gdb_assert (sig_type->per_cu.cu != NULL);
13178
13179 sig_cu = sig_type->per_cu.cu;
13180 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13181 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13182 if (die)
13183 {
13184 *ref_cu = sig_cu;
13185 return die;
13186 }
13187
13188 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13189 "at 0x%x [in module %s]"),
13190 sig_type->type_offset, src_die->offset, objfile->name);
13191}
13192
13193/* Given an offset of a signatured type, return its signatured_type. */
13194
13195static struct signatured_type *
13196lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13197{
13198 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13199 unsigned int length, initial_length_size;
13200 unsigned int sig_offset;
13201 struct signatured_type find_entry, *type_sig;
13202
13203 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13204 sig_offset = (initial_length_size
13205 + 2 /*version*/
13206 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13207 + 1 /*address_size*/);
13208 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13209 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13210
13211 /* This is only used to lookup previously recorded types.
13212 If we didn't find it, it's our bug. */
13213 gdb_assert (type_sig != NULL);
13214 gdb_assert (offset == type_sig->offset);
13215
13216 return type_sig;
13217}
13218
13219/* Read in signatured type at OFFSET and build its CU and die(s). */
13220
13221static void
13222read_signatured_type_at_offset (struct objfile *objfile,
13223 unsigned int offset)
13224{
13225 struct signatured_type *type_sig;
13226
be391dca
TT
13227 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13228
348e048f
DE
13229 /* We have the section offset, but we need the signature to do the
13230 hash table lookup. */
13231 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13232
13233 gdb_assert (type_sig->per_cu.cu == NULL);
13234
13235 read_signatured_type (objfile, type_sig);
13236
13237 gdb_assert (type_sig->per_cu.cu != NULL);
13238}
13239
13240/* Read in a signatured type and build its CU and DIEs. */
13241
13242static void
13243read_signatured_type (struct objfile *objfile,
13244 struct signatured_type *type_sig)
13245{
1fd400ff 13246 gdb_byte *types_ptr;
348e048f
DE
13247 struct die_reader_specs reader_specs;
13248 struct dwarf2_cu *cu;
13249 ULONGEST signature;
13250 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13251
1fd400ff
TT
13252 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13253 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13254
348e048f
DE
13255 gdb_assert (type_sig->per_cu.cu == NULL);
13256
9816fde3
JK
13257 cu = xmalloc (sizeof (*cu));
13258 init_one_comp_unit (cu, objfile);
13259
348e048f
DE
13260 type_sig->per_cu.cu = cu;
13261 cu->per_cu = &type_sig->per_cu;
13262
13263 /* If an error occurs while loading, release our storage. */
13264 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13265
13266 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13267 types_ptr, objfile->obfd);
13268 gdb_assert (signature == type_sig->signature);
13269
13270 cu->die_hash
13271 = htab_create_alloc_ex (cu->header.length / 12,
13272 die_hash,
13273 die_eq,
13274 NULL,
13275 &cu->comp_unit_obstack,
13276 hashtab_obstack_allocate,
13277 dummy_obstack_deallocate);
13278
13279 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13280 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13281
13282 init_cu_die_reader (&reader_specs, cu);
13283
13284 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13285 NULL /*parent*/);
13286
13287 /* We try not to read any attributes in this function, because not
13288 all objfiles needed for references have been loaded yet, and symbol
13289 table processing isn't initialized. But we have to set the CU language,
13290 or we won't be able to build types correctly. */
9816fde3 13291 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13292
13293 do_cleanups (back_to);
13294
13295 /* We've successfully allocated this compilation unit. Let our caller
13296 clean it up when finished with it. */
13297 discard_cleanups (free_cu_cleanup);
13298
13299 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13300 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13301}
13302
c906108c
SS
13303/* Decode simple location descriptions.
13304 Given a pointer to a dwarf block that defines a location, compute
13305 the location and return the value.
13306
4cecd739
DJ
13307 NOTE drow/2003-11-18: This function is called in two situations
13308 now: for the address of static or global variables (partial symbols
13309 only) and for offsets into structures which are expected to be
13310 (more or less) constant. The partial symbol case should go away,
13311 and only the constant case should remain. That will let this
13312 function complain more accurately. A few special modes are allowed
13313 without complaint for global variables (for instance, global
13314 register values and thread-local values).
c906108c
SS
13315
13316 A location description containing no operations indicates that the
4cecd739 13317 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13318 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13319 callers will only want a very basic result and this can become a
13320 complaint.
c906108c 13321
d53d4ac5 13322 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13323
13324static CORE_ADDR
e7c27a73 13325decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13326{
e7c27a73 13327 struct objfile *objfile = cu->objfile;
c906108c
SS
13328 int i;
13329 int size = blk->size;
fe1b8b76 13330 gdb_byte *data = blk->data;
c906108c
SS
13331 CORE_ADDR stack[64];
13332 int stacki;
13333 unsigned int bytes_read, unsnd;
fe1b8b76 13334 gdb_byte op;
c906108c
SS
13335
13336 i = 0;
13337 stacki = 0;
13338 stack[stacki] = 0;
d53d4ac5 13339 stack[++stacki] = 0;
c906108c
SS
13340
13341 while (i < size)
13342 {
c906108c
SS
13343 op = data[i++];
13344 switch (op)
13345 {
f1bea926
JM
13346 case DW_OP_lit0:
13347 case DW_OP_lit1:
13348 case DW_OP_lit2:
13349 case DW_OP_lit3:
13350 case DW_OP_lit4:
13351 case DW_OP_lit5:
13352 case DW_OP_lit6:
13353 case DW_OP_lit7:
13354 case DW_OP_lit8:
13355 case DW_OP_lit9:
13356 case DW_OP_lit10:
13357 case DW_OP_lit11:
13358 case DW_OP_lit12:
13359 case DW_OP_lit13:
13360 case DW_OP_lit14:
13361 case DW_OP_lit15:
13362 case DW_OP_lit16:
13363 case DW_OP_lit17:
13364 case DW_OP_lit18:
13365 case DW_OP_lit19:
13366 case DW_OP_lit20:
13367 case DW_OP_lit21:
13368 case DW_OP_lit22:
13369 case DW_OP_lit23:
13370 case DW_OP_lit24:
13371 case DW_OP_lit25:
13372 case DW_OP_lit26:
13373 case DW_OP_lit27:
13374 case DW_OP_lit28:
13375 case DW_OP_lit29:
13376 case DW_OP_lit30:
13377 case DW_OP_lit31:
13378 stack[++stacki] = op - DW_OP_lit0;
13379 break;
13380
c906108c
SS
13381 case DW_OP_reg0:
13382 case DW_OP_reg1:
13383 case DW_OP_reg2:
13384 case DW_OP_reg3:
13385 case DW_OP_reg4:
13386 case DW_OP_reg5:
13387 case DW_OP_reg6:
13388 case DW_OP_reg7:
13389 case DW_OP_reg8:
13390 case DW_OP_reg9:
13391 case DW_OP_reg10:
13392 case DW_OP_reg11:
13393 case DW_OP_reg12:
13394 case DW_OP_reg13:
13395 case DW_OP_reg14:
13396 case DW_OP_reg15:
13397 case DW_OP_reg16:
13398 case DW_OP_reg17:
13399 case DW_OP_reg18:
13400 case DW_OP_reg19:
13401 case DW_OP_reg20:
13402 case DW_OP_reg21:
13403 case DW_OP_reg22:
13404 case DW_OP_reg23:
13405 case DW_OP_reg24:
13406 case DW_OP_reg25:
13407 case DW_OP_reg26:
13408 case DW_OP_reg27:
13409 case DW_OP_reg28:
13410 case DW_OP_reg29:
13411 case DW_OP_reg30:
13412 case DW_OP_reg31:
c906108c 13413 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13414 if (i < size)
13415 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13416 break;
13417
13418 case DW_OP_regx:
c906108c
SS
13419 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13420 i += bytes_read;
c906108c 13421 stack[++stacki] = unsnd;
4cecd739
DJ
13422 if (i < size)
13423 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13424 break;
13425
13426 case DW_OP_addr:
107d2387 13427 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13428 cu, &bytes_read);
107d2387 13429 i += bytes_read;
c906108c
SS
13430 break;
13431
13432 case DW_OP_const1u:
13433 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13434 i += 1;
13435 break;
13436
13437 case DW_OP_const1s:
13438 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13439 i += 1;
13440 break;
13441
13442 case DW_OP_const2u:
13443 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13444 i += 2;
13445 break;
13446
13447 case DW_OP_const2s:
13448 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13449 i += 2;
13450 break;
13451
13452 case DW_OP_const4u:
13453 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13454 i += 4;
13455 break;
13456
13457 case DW_OP_const4s:
13458 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13459 i += 4;
13460 break;
13461
13462 case DW_OP_constu:
13463 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13464 &bytes_read);
c906108c
SS
13465 i += bytes_read;
13466 break;
13467
13468 case DW_OP_consts:
13469 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13470 i += bytes_read;
13471 break;
13472
f1bea926
JM
13473 case DW_OP_dup:
13474 stack[stacki + 1] = stack[stacki];
13475 stacki++;
13476 break;
13477
c906108c
SS
13478 case DW_OP_plus:
13479 stack[stacki - 1] += stack[stacki];
13480 stacki--;
13481 break;
13482
13483 case DW_OP_plus_uconst:
13484 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13485 i += bytes_read;
13486 break;
13487
13488 case DW_OP_minus:
f1bea926 13489 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13490 stacki--;
13491 break;
13492
7a292a7a 13493 case DW_OP_deref:
7a292a7a 13494 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13495 this using GDB's address_class enum. This is valid for partial
13496 global symbols, although the variable's address will be bogus
13497 in the psymtab. */
7a292a7a 13498 if (i < size)
4d3c2250 13499 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13500 break;
13501
9d774e44 13502 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13503 /* The top of the stack has the offset from the beginning
13504 of the thread control block at which the variable is located. */
13505 /* Nothing should follow this operator, so the top of stack would
13506 be returned. */
4cecd739
DJ
13507 /* This is valid for partial global symbols, but the variable's
13508 address will be bogus in the psymtab. */
9d774e44 13509 if (i < size)
4d3c2250 13510 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13511 break;
13512
42be36b3
CT
13513 case DW_OP_GNU_uninit:
13514 break;
13515
c906108c 13516 default:
e2e0b3e5 13517 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13518 dwarf_stack_op_name (op, 1));
c906108c
SS
13519 return (stack[stacki]);
13520 }
d53d4ac5
TT
13521
13522 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13523 outside of the allocated space. Also enforce minimum>0. */
13524 if (stacki >= ARRAY_SIZE (stack) - 1)
13525 {
13526 complaint (&symfile_complaints,
13527 _("location description stack overflow"));
13528 return 0;
13529 }
13530
13531 if (stacki <= 0)
13532 {
13533 complaint (&symfile_complaints,
13534 _("location description stack underflow"));
13535 return 0;
13536 }
c906108c
SS
13537 }
13538 return (stack[stacki]);
13539}
13540
13541/* memory allocation interface */
13542
c906108c 13543static struct dwarf_block *
7b5a2f43 13544dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13545{
13546 struct dwarf_block *blk;
13547
13548 blk = (struct dwarf_block *)
7b5a2f43 13549 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13550 return (blk);
13551}
13552
13553static struct abbrev_info *
f3dd6933 13554dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13555{
13556 struct abbrev_info *abbrev;
13557
f3dd6933
DJ
13558 abbrev = (struct abbrev_info *)
13559 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13560 memset (abbrev, 0, sizeof (struct abbrev_info));
13561 return (abbrev);
13562}
13563
13564static struct die_info *
b60c80d6 13565dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13566{
13567 struct die_info *die;
b60c80d6
DJ
13568 size_t size = sizeof (struct die_info);
13569
13570 if (num_attrs > 1)
13571 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13572
b60c80d6 13573 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13574 memset (die, 0, sizeof (struct die_info));
13575 return (die);
13576}
2e276125
JB
13577
13578\f
13579/* Macro support. */
13580
2e276125
JB
13581/* Return the full name of file number I in *LH's file name table.
13582 Use COMP_DIR as the name of the current directory of the
13583 compilation. The result is allocated using xmalloc; the caller is
13584 responsible for freeing it. */
13585static char *
13586file_full_name (int file, struct line_header *lh, const char *comp_dir)
13587{
6a83a1e6
EZ
13588 /* Is the file number a valid index into the line header's file name
13589 table? Remember that file numbers start with one, not zero. */
13590 if (1 <= file && file <= lh->num_file_names)
13591 {
13592 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13593
6a83a1e6
EZ
13594 if (IS_ABSOLUTE_PATH (fe->name))
13595 return xstrdup (fe->name);
13596 else
13597 {
13598 const char *dir;
13599 int dir_len;
13600 char *full_name;
13601
13602 if (fe->dir_index)
13603 dir = lh->include_dirs[fe->dir_index - 1];
13604 else
13605 dir = comp_dir;
13606
13607 if (dir)
13608 {
13609 dir_len = strlen (dir);
13610 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13611 strcpy (full_name, dir);
13612 full_name[dir_len] = '/';
13613 strcpy (full_name + dir_len + 1, fe->name);
13614 return full_name;
13615 }
13616 else
13617 return xstrdup (fe->name);
13618 }
13619 }
2e276125
JB
13620 else
13621 {
6a83a1e6
EZ
13622 /* The compiler produced a bogus file number. We can at least
13623 record the macro definitions made in the file, even if we
13624 won't be able to find the file by name. */
13625 char fake_name[80];
9a619af0 13626
6a83a1e6 13627 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13628
6e70227d 13629 complaint (&symfile_complaints,
6a83a1e6
EZ
13630 _("bad file number in macro information (%d)"),
13631 file);
2e276125 13632
6a83a1e6 13633 return xstrdup (fake_name);
2e276125
JB
13634 }
13635}
13636
13637
13638static struct macro_source_file *
13639macro_start_file (int file, int line,
13640 struct macro_source_file *current_file,
13641 const char *comp_dir,
13642 struct line_header *lh, struct objfile *objfile)
13643{
13644 /* The full name of this source file. */
13645 char *full_name = file_full_name (file, lh, comp_dir);
13646
13647 /* We don't create a macro table for this compilation unit
13648 at all until we actually get a filename. */
13649 if (! pending_macros)
4a146b47 13650 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13651 objfile->macro_cache);
2e276125
JB
13652
13653 if (! current_file)
13654 /* If we have no current file, then this must be the start_file
13655 directive for the compilation unit's main source file. */
13656 current_file = macro_set_main (pending_macros, full_name);
13657 else
13658 current_file = macro_include (current_file, line, full_name);
13659
13660 xfree (full_name);
6e70227d 13661
2e276125
JB
13662 return current_file;
13663}
13664
13665
13666/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13667 followed by a null byte. */
13668static char *
13669copy_string (const char *buf, int len)
13670{
13671 char *s = xmalloc (len + 1);
9a619af0 13672
2e276125
JB
13673 memcpy (s, buf, len);
13674 s[len] = '\0';
2e276125
JB
13675 return s;
13676}
13677
13678
13679static const char *
13680consume_improper_spaces (const char *p, const char *body)
13681{
13682 if (*p == ' ')
13683 {
4d3c2250 13684 complaint (&symfile_complaints,
e2e0b3e5 13685 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13686 body);
2e276125
JB
13687
13688 while (*p == ' ')
13689 p++;
13690 }
13691
13692 return p;
13693}
13694
13695
13696static void
13697parse_macro_definition (struct macro_source_file *file, int line,
13698 const char *body)
13699{
13700 const char *p;
13701
13702 /* The body string takes one of two forms. For object-like macro
13703 definitions, it should be:
13704
13705 <macro name> " " <definition>
13706
13707 For function-like macro definitions, it should be:
13708
13709 <macro name> "() " <definition>
13710 or
13711 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13712
13713 Spaces may appear only where explicitly indicated, and in the
13714 <definition>.
13715
13716 The Dwarf 2 spec says that an object-like macro's name is always
13717 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13718 the space when the macro's definition is the empty string.
2e276125
JB
13719
13720 The Dwarf 2 spec says that there should be no spaces between the
13721 formal arguments in a function-like macro's formal argument list,
13722 but versions of GCC around March 2002 include spaces after the
13723 commas. */
13724
13725
13726 /* Find the extent of the macro name. The macro name is terminated
13727 by either a space or null character (for an object-like macro) or
13728 an opening paren (for a function-like macro). */
13729 for (p = body; *p; p++)
13730 if (*p == ' ' || *p == '(')
13731 break;
13732
13733 if (*p == ' ' || *p == '\0')
13734 {
13735 /* It's an object-like macro. */
13736 int name_len = p - body;
13737 char *name = copy_string (body, name_len);
13738 const char *replacement;
13739
13740 if (*p == ' ')
13741 replacement = body + name_len + 1;
13742 else
13743 {
4d3c2250 13744 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13745 replacement = body + name_len;
13746 }
6e70227d 13747
2e276125
JB
13748 macro_define_object (file, line, name, replacement);
13749
13750 xfree (name);
13751 }
13752 else if (*p == '(')
13753 {
13754 /* It's a function-like macro. */
13755 char *name = copy_string (body, p - body);
13756 int argc = 0;
13757 int argv_size = 1;
13758 char **argv = xmalloc (argv_size * sizeof (*argv));
13759
13760 p++;
13761
13762 p = consume_improper_spaces (p, body);
13763
13764 /* Parse the formal argument list. */
13765 while (*p && *p != ')')
13766 {
13767 /* Find the extent of the current argument name. */
13768 const char *arg_start = p;
13769
13770 while (*p && *p != ',' && *p != ')' && *p != ' ')
13771 p++;
13772
13773 if (! *p || p == arg_start)
4d3c2250 13774 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13775 else
13776 {
13777 /* Make sure argv has room for the new argument. */
13778 if (argc >= argv_size)
13779 {
13780 argv_size *= 2;
13781 argv = xrealloc (argv, argv_size * sizeof (*argv));
13782 }
13783
13784 argv[argc++] = copy_string (arg_start, p - arg_start);
13785 }
13786
13787 p = consume_improper_spaces (p, body);
13788
13789 /* Consume the comma, if present. */
13790 if (*p == ',')
13791 {
13792 p++;
13793
13794 p = consume_improper_spaces (p, body);
13795 }
13796 }
13797
13798 if (*p == ')')
13799 {
13800 p++;
13801
13802 if (*p == ' ')
13803 /* Perfectly formed definition, no complaints. */
13804 macro_define_function (file, line, name,
6e70227d 13805 argc, (const char **) argv,
2e276125
JB
13806 p + 1);
13807 else if (*p == '\0')
13808 {
13809 /* Complain, but do define it. */
4d3c2250 13810 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13811 macro_define_function (file, line, name,
6e70227d 13812 argc, (const char **) argv,
2e276125
JB
13813 p);
13814 }
13815 else
13816 /* Just complain. */
4d3c2250 13817 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13818 }
13819 else
13820 /* Just complain. */
4d3c2250 13821 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13822
13823 xfree (name);
13824 {
13825 int i;
13826
13827 for (i = 0; i < argc; i++)
13828 xfree (argv[i]);
13829 }
13830 xfree (argv);
13831 }
13832 else
4d3c2250 13833 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13834}
13835
13836
13837static void
13838dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13839 char *comp_dir, bfd *abfd,
e7c27a73 13840 struct dwarf2_cu *cu)
2e276125 13841{
fe1b8b76 13842 gdb_byte *mac_ptr, *mac_end;
2e276125 13843 struct macro_source_file *current_file = 0;
757a13d0
JK
13844 enum dwarf_macinfo_record_type macinfo_type;
13845 int at_commandline;
2e276125 13846
be391dca
TT
13847 dwarf2_read_section (dwarf2_per_objfile->objfile,
13848 &dwarf2_per_objfile->macinfo);
dce234bc 13849 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13850 {
e2e0b3e5 13851 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13852 return;
13853 }
13854
757a13d0
JK
13855 /* First pass: Find the name of the base filename.
13856 This filename is needed in order to process all macros whose definition
13857 (or undefinition) comes from the command line. These macros are defined
13858 before the first DW_MACINFO_start_file entry, and yet still need to be
13859 associated to the base file.
13860
13861 To determine the base file name, we scan the macro definitions until we
13862 reach the first DW_MACINFO_start_file entry. We then initialize
13863 CURRENT_FILE accordingly so that any macro definition found before the
13864 first DW_MACINFO_start_file can still be associated to the base file. */
13865
dce234bc
PP
13866 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13867 mac_end = dwarf2_per_objfile->macinfo.buffer
13868 + dwarf2_per_objfile->macinfo.size;
2e276125 13869
757a13d0 13870 do
2e276125 13871 {
2e276125
JB
13872 /* Do we at least have room for a macinfo type byte? */
13873 if (mac_ptr >= mac_end)
13874 {
757a13d0
JK
13875 /* Complaint is printed during the second pass as GDB will probably
13876 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13877 break;
2e276125
JB
13878 }
13879
13880 macinfo_type = read_1_byte (abfd, mac_ptr);
13881 mac_ptr++;
13882
13883 switch (macinfo_type)
13884 {
13885 /* A zero macinfo type indicates the end of the macro
13886 information. */
13887 case 0:
757a13d0
JK
13888 break;
13889
13890 case DW_MACINFO_define:
13891 case DW_MACINFO_undef:
13892 /* Only skip the data by MAC_PTR. */
13893 {
13894 unsigned int bytes_read;
13895
13896 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13897 mac_ptr += bytes_read;
9b1c24c8 13898 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13899 mac_ptr += bytes_read;
13900 }
13901 break;
13902
13903 case DW_MACINFO_start_file:
13904 {
13905 unsigned int bytes_read;
13906 int line, file;
13907
13908 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13909 mac_ptr += bytes_read;
13910 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13911 mac_ptr += bytes_read;
13912
13913 current_file = macro_start_file (file, line, current_file, comp_dir,
13914 lh, cu->objfile);
13915 }
13916 break;
13917
13918 case DW_MACINFO_end_file:
13919 /* No data to skip by MAC_PTR. */
13920 break;
13921
13922 case DW_MACINFO_vendor_ext:
13923 /* Only skip the data by MAC_PTR. */
13924 {
13925 unsigned int bytes_read;
13926
13927 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13928 mac_ptr += bytes_read;
9b1c24c8 13929 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13930 mac_ptr += bytes_read;
13931 }
13932 break;
13933
13934 default:
13935 break;
13936 }
13937 } while (macinfo_type != 0 && current_file == NULL);
13938
13939 /* Second pass: Process all entries.
13940
13941 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13942 command-line macro definitions/undefinitions. This flag is unset when we
13943 reach the first DW_MACINFO_start_file entry. */
13944
dce234bc 13945 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13946
13947 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13948 GDB is still reading the definitions from command line. First
13949 DW_MACINFO_start_file will need to be ignored as it was already executed
13950 to create CURRENT_FILE for the main source holding also the command line
13951 definitions. On first met DW_MACINFO_start_file this flag is reset to
13952 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13953
13954 at_commandline = 1;
13955
13956 do
13957 {
13958 /* Do we at least have room for a macinfo type byte? */
13959 if (mac_ptr >= mac_end)
13960 {
13961 dwarf2_macros_too_long_complaint ();
13962 break;
13963 }
13964
13965 macinfo_type = read_1_byte (abfd, mac_ptr);
13966 mac_ptr++;
13967
13968 switch (macinfo_type)
13969 {
13970 /* A zero macinfo type indicates the end of the macro
13971 information. */
13972 case 0:
13973 break;
2e276125
JB
13974
13975 case DW_MACINFO_define:
13976 case DW_MACINFO_undef:
13977 {
891d2f0b 13978 unsigned int bytes_read;
2e276125
JB
13979 int line;
13980 char *body;
13981
13982 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13983 mac_ptr += bytes_read;
9b1c24c8 13984 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13985 mac_ptr += bytes_read;
13986
13987 if (! current_file)
757a13d0
JK
13988 {
13989 /* DWARF violation as no main source is present. */
13990 complaint (&symfile_complaints,
13991 _("debug info with no main source gives macro %s "
13992 "on line %d: %s"),
6e70227d
DE
13993 macinfo_type == DW_MACINFO_define ?
13994 _("definition") :
905e0470
PM
13995 macinfo_type == DW_MACINFO_undef ?
13996 _("undefinition") :
13997 _("something-or-other"), line, body);
757a13d0
JK
13998 break;
13999 }
14000 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 14001 complaint (&symfile_complaints,
757a13d0
JK
14002 _("debug info gives %s macro %s with %s line %d: %s"),
14003 at_commandline ? _("command-line") : _("in-file"),
905e0470 14004 macinfo_type == DW_MACINFO_define ?
6e70227d 14005 _("definition") :
905e0470
PM
14006 macinfo_type == DW_MACINFO_undef ?
14007 _("undefinition") :
14008 _("something-or-other"),
757a13d0
JK
14009 line == 0 ? _("zero") : _("non-zero"), line, body);
14010
14011 if (macinfo_type == DW_MACINFO_define)
14012 parse_macro_definition (current_file, line, body);
14013 else if (macinfo_type == DW_MACINFO_undef)
14014 macro_undef (current_file, line, body);
2e276125
JB
14015 }
14016 break;
14017
14018 case DW_MACINFO_start_file:
14019 {
891d2f0b 14020 unsigned int bytes_read;
2e276125
JB
14021 int line, file;
14022
14023 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14024 mac_ptr += bytes_read;
14025 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14026 mac_ptr += bytes_read;
14027
757a13d0
JK
14028 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14029 complaint (&symfile_complaints,
14030 _("debug info gives source %d included "
14031 "from %s at %s line %d"),
14032 file, at_commandline ? _("command-line") : _("file"),
14033 line == 0 ? _("zero") : _("non-zero"), line);
14034
14035 if (at_commandline)
14036 {
14037 /* This DW_MACINFO_start_file was executed in the pass one. */
14038 at_commandline = 0;
14039 }
14040 else
14041 current_file = macro_start_file (file, line,
14042 current_file, comp_dir,
14043 lh, cu->objfile);
2e276125
JB
14044 }
14045 break;
14046
14047 case DW_MACINFO_end_file:
14048 if (! current_file)
4d3c2250 14049 complaint (&symfile_complaints,
e2e0b3e5 14050 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
14051 else
14052 {
14053 current_file = current_file->included_by;
14054 if (! current_file)
14055 {
14056 enum dwarf_macinfo_record_type next_type;
14057
14058 /* GCC circa March 2002 doesn't produce the zero
14059 type byte marking the end of the compilation
14060 unit. Complain if it's not there, but exit no
14061 matter what. */
14062
14063 /* Do we at least have room for a macinfo type byte? */
14064 if (mac_ptr >= mac_end)
14065 {
4d3c2250 14066 dwarf2_macros_too_long_complaint ();
2e276125
JB
14067 return;
14068 }
14069
14070 /* We don't increment mac_ptr here, so this is just
14071 a look-ahead. */
14072 next_type = read_1_byte (abfd, mac_ptr);
14073 if (next_type != 0)
4d3c2250 14074 complaint (&symfile_complaints,
e2e0b3e5 14075 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
14076
14077 return;
14078 }
14079 }
14080 break;
14081
14082 case DW_MACINFO_vendor_ext:
14083 {
891d2f0b 14084 unsigned int bytes_read;
2e276125
JB
14085 int constant;
14086 char *string;
14087
14088 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14089 mac_ptr += bytes_read;
9b1c24c8 14090 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14091 mac_ptr += bytes_read;
14092
14093 /* We don't recognize any vendor extensions. */
14094 }
14095 break;
14096 }
757a13d0 14097 } while (macinfo_type != 0);
2e276125 14098}
8e19ed76
PS
14099
14100/* Check if the attribute's form is a DW_FORM_block*
14101 if so return true else false. */
14102static int
14103attr_form_is_block (struct attribute *attr)
14104{
14105 return (attr == NULL ? 0 :
14106 attr->form == DW_FORM_block1
14107 || attr->form == DW_FORM_block2
14108 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14109 || attr->form == DW_FORM_block
14110 || attr->form == DW_FORM_exprloc);
8e19ed76 14111}
4c2df51b 14112
c6a0999f
JB
14113/* Return non-zero if ATTR's value is a section offset --- classes
14114 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14115 You may use DW_UNSND (attr) to retrieve such offsets.
14116
14117 Section 7.5.4, "Attribute Encodings", explains that no attribute
14118 may have a value that belongs to more than one of these classes; it
14119 would be ambiguous if we did, because we use the same forms for all
14120 of them. */
3690dd37
JB
14121static int
14122attr_form_is_section_offset (struct attribute *attr)
14123{
14124 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14125 || attr->form == DW_FORM_data8
14126 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14127}
14128
14129
14130/* Return non-zero if ATTR's value falls in the 'constant' class, or
14131 zero otherwise. When this function returns true, you can apply
14132 dwarf2_get_attr_constant_value to it.
14133
14134 However, note that for some attributes you must check
14135 attr_form_is_section_offset before using this test. DW_FORM_data4
14136 and DW_FORM_data8 are members of both the constant class, and of
14137 the classes that contain offsets into other debug sections
14138 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14139 that, if an attribute's can be either a constant or one of the
14140 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14141 taken as section offsets, not constants. */
14142static int
14143attr_form_is_constant (struct attribute *attr)
14144{
14145 switch (attr->form)
14146 {
14147 case DW_FORM_sdata:
14148 case DW_FORM_udata:
14149 case DW_FORM_data1:
14150 case DW_FORM_data2:
14151 case DW_FORM_data4:
14152 case DW_FORM_data8:
14153 return 1;
14154 default:
14155 return 0;
14156 }
14157}
14158
8cf6f0b1
TT
14159/* A helper function that fills in a dwarf2_loclist_baton. */
14160
14161static void
14162fill_in_loclist_baton (struct dwarf2_cu *cu,
14163 struct dwarf2_loclist_baton *baton,
14164 struct attribute *attr)
14165{
14166 dwarf2_read_section (dwarf2_per_objfile->objfile,
14167 &dwarf2_per_objfile->loc);
14168
14169 baton->per_cu = cu->per_cu;
14170 gdb_assert (baton->per_cu);
14171 /* We don't know how long the location list is, but make sure we
14172 don't run off the edge of the section. */
14173 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14174 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14175 baton->base_address = cu->base_address;
14176}
14177
4c2df51b
DJ
14178static void
14179dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14180 struct dwarf2_cu *cu)
4c2df51b 14181{
3690dd37 14182 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14183 /* ".debug_loc" may not exist at all, or the offset may be outside
14184 the section. If so, fall through to the complaint in the
14185 other branch. */
dce234bc 14186 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14187 {
0d53c4c4 14188 struct dwarf2_loclist_baton *baton;
4c2df51b 14189
4a146b47 14190 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14191 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14192
8cf6f0b1 14193 fill_in_loclist_baton (cu, baton, attr);
be391dca 14194
d00adf39 14195 if (cu->base_known == 0)
0d53c4c4 14196 complaint (&symfile_complaints,
e2e0b3e5 14197 _("Location list used without specifying the CU base address."));
4c2df51b 14198
768a979c 14199 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14200 SYMBOL_LOCATION_BATON (sym) = baton;
14201 }
14202 else
14203 {
14204 struct dwarf2_locexpr_baton *baton;
14205
4a146b47 14206 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14207 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14208 baton->per_cu = cu->per_cu;
14209 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14210
14211 if (attr_form_is_block (attr))
14212 {
14213 /* Note that we're just copying the block's data pointer
14214 here, not the actual data. We're still pointing into the
6502dd73
DJ
14215 info_buffer for SYM's objfile; right now we never release
14216 that buffer, but when we do clean up properly this may
14217 need to change. */
0d53c4c4
DJ
14218 baton->size = DW_BLOCK (attr)->size;
14219 baton->data = DW_BLOCK (attr)->data;
14220 }
14221 else
14222 {
14223 dwarf2_invalid_attrib_class_complaint ("location description",
14224 SYMBOL_NATURAL_NAME (sym));
14225 baton->size = 0;
14226 baton->data = NULL;
14227 }
6e70227d 14228
768a979c 14229 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14230 SYMBOL_LOCATION_BATON (sym) = baton;
14231 }
4c2df51b 14232}
6502dd73 14233
9aa1f1e3
TT
14234/* Return the OBJFILE associated with the compilation unit CU. If CU
14235 came from a separate debuginfo file, then the master objfile is
14236 returned. */
ae0d2f24
UW
14237
14238struct objfile *
14239dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14240{
9291a0cd 14241 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14242
14243 /* Return the master objfile, so that we can report and look up the
14244 correct file containing this variable. */
14245 if (objfile->separate_debug_objfile_backlink)
14246 objfile = objfile->separate_debug_objfile_backlink;
14247
14248 return objfile;
14249}
14250
14251/* Return the address size given in the compilation unit header for CU. */
14252
14253CORE_ADDR
14254dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14255{
14256 if (per_cu->cu)
14257 return per_cu->cu->header.addr_size;
14258 else
14259 {
14260 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14261 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14262 struct dwarf2_per_objfile *per_objfile
14263 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14264 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14265 struct comp_unit_head cu_header;
9a619af0 14266
ae0d2f24
UW
14267 memset (&cu_header, 0, sizeof cu_header);
14268 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14269 return cu_header.addr_size;
14270 }
14271}
14272
9eae7c52
TT
14273/* Return the offset size given in the compilation unit header for CU. */
14274
14275int
14276dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14277{
14278 if (per_cu->cu)
14279 return per_cu->cu->header.offset_size;
14280 else
14281 {
14282 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14283 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14284 struct dwarf2_per_objfile *per_objfile
14285 = objfile_data (objfile, dwarf2_objfile_data_key);
14286 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14287 struct comp_unit_head cu_header;
14288
14289 memset (&cu_header, 0, sizeof cu_header);
14290 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14291 return cu_header.offset_size;
14292 }
14293}
14294
9aa1f1e3
TT
14295/* Return the text offset of the CU. The returned offset comes from
14296 this CU's objfile. If this objfile came from a separate debuginfo
14297 file, then the offset may be different from the corresponding
14298 offset in the parent objfile. */
14299
14300CORE_ADDR
14301dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14302{
bb3fa9d0 14303 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14304
14305 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14306}
14307
348e048f
DE
14308/* Locate the .debug_info compilation unit from CU's objfile which contains
14309 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14310
14311static struct dwarf2_per_cu_data *
c764a876 14312dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14313 struct objfile *objfile)
14314{
14315 struct dwarf2_per_cu_data *this_cu;
14316 int low, high;
14317
ae038cb0
DJ
14318 low = 0;
14319 high = dwarf2_per_objfile->n_comp_units - 1;
14320 while (high > low)
14321 {
14322 int mid = low + (high - low) / 2;
9a619af0 14323
ae038cb0
DJ
14324 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14325 high = mid;
14326 else
14327 low = mid + 1;
14328 }
14329 gdb_assert (low == high);
14330 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14331 {
10b3939b 14332 if (low == 0)
8a3fe4f8
AC
14333 error (_("Dwarf Error: could not find partial DIE containing "
14334 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14335 (long) offset, bfd_get_filename (objfile->obfd));
14336
ae038cb0
DJ
14337 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14338 return dwarf2_per_objfile->all_comp_units[low-1];
14339 }
14340 else
14341 {
14342 this_cu = dwarf2_per_objfile->all_comp_units[low];
14343 if (low == dwarf2_per_objfile->n_comp_units - 1
14344 && offset >= this_cu->offset + this_cu->length)
c764a876 14345 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14346 gdb_assert (offset < this_cu->offset + this_cu->length);
14347 return this_cu;
14348 }
14349}
14350
10b3939b
DJ
14351/* Locate the compilation unit from OBJFILE which is located at exactly
14352 OFFSET. Raises an error on failure. */
14353
ae038cb0 14354static struct dwarf2_per_cu_data *
c764a876 14355dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14356{
14357 struct dwarf2_per_cu_data *this_cu;
9a619af0 14358
ae038cb0
DJ
14359 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14360 if (this_cu->offset != offset)
c764a876 14361 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14362 return this_cu;
14363}
14364
9816fde3 14365/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14366
9816fde3
JK
14367static void
14368init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14369{
9816fde3 14370 memset (cu, 0, sizeof (*cu));
93311388
DE
14371 cu->objfile = objfile;
14372 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14373}
14374
14375/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14376
14377static void
14378prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14379{
14380 struct attribute *attr;
14381
14382 /* Set the language we're debugging. */
14383 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14384 if (attr)
14385 set_cu_language (DW_UNSND (attr), cu);
14386 else
14387 set_cu_language (language_minimal, cu);
93311388
DE
14388}
14389
ae038cb0
DJ
14390/* Release one cached compilation unit, CU. We unlink it from the tree
14391 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14392 the caller is responsible for that.
14393 NOTE: DATA is a void * because this function is also used as a
14394 cleanup routine. */
ae038cb0
DJ
14395
14396static void
14397free_one_comp_unit (void *data)
14398{
14399 struct dwarf2_cu *cu = data;
14400
14401 if (cu->per_cu != NULL)
14402 cu->per_cu->cu = NULL;
14403 cu->per_cu = NULL;
14404
14405 obstack_free (&cu->comp_unit_obstack, NULL);
14406
14407 xfree (cu);
14408}
14409
72bf9492 14410/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14411 when we're finished with it. We can't free the pointer itself, but be
14412 sure to unlink it from the cache. Also release any associated storage
14413 and perform cache maintenance.
72bf9492
DJ
14414
14415 Only used during partial symbol parsing. */
14416
14417static void
14418free_stack_comp_unit (void *data)
14419{
14420 struct dwarf2_cu *cu = data;
14421
14422 obstack_free (&cu->comp_unit_obstack, NULL);
14423 cu->partial_dies = NULL;
ae038cb0
DJ
14424
14425 if (cu->per_cu != NULL)
14426 {
14427 /* This compilation unit is on the stack in our caller, so we
14428 should not xfree it. Just unlink it. */
14429 cu->per_cu->cu = NULL;
14430 cu->per_cu = NULL;
14431
14432 /* If we had a per-cu pointer, then we may have other compilation
14433 units loaded, so age them now. */
14434 age_cached_comp_units ();
14435 }
14436}
14437
14438/* Free all cached compilation units. */
14439
14440static void
14441free_cached_comp_units (void *data)
14442{
14443 struct dwarf2_per_cu_data *per_cu, **last_chain;
14444
14445 per_cu = dwarf2_per_objfile->read_in_chain;
14446 last_chain = &dwarf2_per_objfile->read_in_chain;
14447 while (per_cu != NULL)
14448 {
14449 struct dwarf2_per_cu_data *next_cu;
14450
14451 next_cu = per_cu->cu->read_in_chain;
14452
14453 free_one_comp_unit (per_cu->cu);
14454 *last_chain = next_cu;
14455
14456 per_cu = next_cu;
14457 }
14458}
14459
14460/* Increase the age counter on each cached compilation unit, and free
14461 any that are too old. */
14462
14463static void
14464age_cached_comp_units (void)
14465{
14466 struct dwarf2_per_cu_data *per_cu, **last_chain;
14467
14468 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14469 per_cu = dwarf2_per_objfile->read_in_chain;
14470 while (per_cu != NULL)
14471 {
14472 per_cu->cu->last_used ++;
14473 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14474 dwarf2_mark (per_cu->cu);
14475 per_cu = per_cu->cu->read_in_chain;
14476 }
14477
14478 per_cu = dwarf2_per_objfile->read_in_chain;
14479 last_chain = &dwarf2_per_objfile->read_in_chain;
14480 while (per_cu != NULL)
14481 {
14482 struct dwarf2_per_cu_data *next_cu;
14483
14484 next_cu = per_cu->cu->read_in_chain;
14485
14486 if (!per_cu->cu->mark)
14487 {
14488 free_one_comp_unit (per_cu->cu);
14489 *last_chain = next_cu;
14490 }
14491 else
14492 last_chain = &per_cu->cu->read_in_chain;
14493
14494 per_cu = next_cu;
14495 }
14496}
14497
14498/* Remove a single compilation unit from the cache. */
14499
14500static void
14501free_one_cached_comp_unit (void *target_cu)
14502{
14503 struct dwarf2_per_cu_data *per_cu, **last_chain;
14504
14505 per_cu = dwarf2_per_objfile->read_in_chain;
14506 last_chain = &dwarf2_per_objfile->read_in_chain;
14507 while (per_cu != NULL)
14508 {
14509 struct dwarf2_per_cu_data *next_cu;
14510
14511 next_cu = per_cu->cu->read_in_chain;
14512
14513 if (per_cu->cu == target_cu)
14514 {
14515 free_one_comp_unit (per_cu->cu);
14516 *last_chain = next_cu;
14517 break;
14518 }
14519 else
14520 last_chain = &per_cu->cu->read_in_chain;
14521
14522 per_cu = next_cu;
14523 }
14524}
14525
fe3e1990
DJ
14526/* Release all extra memory associated with OBJFILE. */
14527
14528void
14529dwarf2_free_objfile (struct objfile *objfile)
14530{
14531 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14532
14533 if (dwarf2_per_objfile == NULL)
14534 return;
14535
14536 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14537 free_cached_comp_units (NULL);
14538
9291a0cd
TT
14539 if (dwarf2_per_objfile->using_index)
14540 {
14541 int i;
14542
14543 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14544 {
14545 int j;
e254ef6a
DE
14546 struct dwarf2_per_cu_data *per_cu =
14547 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14548
e254ef6a 14549 if (!per_cu->v.quick->lines)
9291a0cd
TT
14550 continue;
14551
e254ef6a 14552 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14553 {
e254ef6a
DE
14554 if (per_cu->v.quick->file_names)
14555 xfree ((void *) per_cu->v.quick->file_names[j]);
14556 if (per_cu->v.quick->full_names)
14557 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14558 }
14559
e254ef6a 14560 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14561 }
14562 }
14563
fe3e1990
DJ
14564 /* Everything else should be on the objfile obstack. */
14565}
14566
1c379e20
DJ
14567/* A pair of DIE offset and GDB type pointer. We store these
14568 in a hash table separate from the DIEs, and preserve them
14569 when the DIEs are flushed out of cache. */
14570
14571struct dwarf2_offset_and_type
14572{
14573 unsigned int offset;
14574 struct type *type;
14575};
14576
14577/* Hash function for a dwarf2_offset_and_type. */
14578
14579static hashval_t
14580offset_and_type_hash (const void *item)
14581{
14582 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14583
1c379e20
DJ
14584 return ofs->offset;
14585}
14586
14587/* Equality function for a dwarf2_offset_and_type. */
14588
14589static int
14590offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14591{
14592 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14593 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14594
1c379e20
DJ
14595 return ofs_lhs->offset == ofs_rhs->offset;
14596}
14597
14598/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14599 table if necessary. For convenience, return TYPE.
14600
14601 The DIEs reading must have careful ordering to:
14602 * Not cause infite loops trying to read in DIEs as a prerequisite for
14603 reading current DIE.
14604 * Not trying to dereference contents of still incompletely read in types
14605 while reading in other DIEs.
14606 * Enable referencing still incompletely read in types just by a pointer to
14607 the type without accessing its fields.
14608
14609 Therefore caller should follow these rules:
14610 * Try to fetch any prerequisite types we may need to build this DIE type
14611 before building the type and calling set_die_type.
e71ec853 14612 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14613 possible before fetching more types to complete the current type.
14614 * Make the type as complete as possible before fetching more types. */
1c379e20 14615
f792889a 14616static struct type *
1c379e20
DJ
14617set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14618{
14619 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14620 struct objfile *objfile = cu->objfile;
14621 htab_t *type_hash_ptr;
1c379e20 14622
b4ba55a1
JB
14623 /* For Ada types, make sure that the gnat-specific data is always
14624 initialized (if not already set). There are a few types where
14625 we should not be doing so, because the type-specific area is
14626 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14627 where the type-specific area is used to store the floatformat).
14628 But this is not a problem, because the gnat-specific information
14629 is actually not needed for these types. */
14630 if (need_gnat_info (cu)
14631 && TYPE_CODE (type) != TYPE_CODE_FUNC
14632 && TYPE_CODE (type) != TYPE_CODE_FLT
14633 && !HAVE_GNAT_AUX_INFO (type))
14634 INIT_GNAT_SPECIFIC (type);
14635
673bfd45
DE
14636 if (cu->per_cu->from_debug_types)
14637 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14638 else
14639 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14640
14641 if (*type_hash_ptr == NULL)
f792889a 14642 {
673bfd45
DE
14643 *type_hash_ptr
14644 = htab_create_alloc_ex (127,
f792889a
DJ
14645 offset_and_type_hash,
14646 offset_and_type_eq,
14647 NULL,
673bfd45 14648 &objfile->objfile_obstack,
f792889a
DJ
14649 hashtab_obstack_allocate,
14650 dummy_obstack_deallocate);
f792889a 14651 }
1c379e20
DJ
14652
14653 ofs.offset = die->offset;
14654 ofs.type = type;
14655 slot = (struct dwarf2_offset_and_type **)
673bfd45 14656 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14657 if (*slot)
14658 complaint (&symfile_complaints,
14659 _("A problem internal to GDB: DIE 0x%x has type already set"),
14660 die->offset);
673bfd45 14661 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14662 **slot = ofs;
f792889a 14663 return type;
1c379e20
DJ
14664}
14665
673bfd45
DE
14666/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14667 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14668
14669static struct type *
673bfd45
DE
14670get_die_type_at_offset (unsigned int offset,
14671 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14672{
14673 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14674 htab_t type_hash;
f792889a 14675
673bfd45
DE
14676 if (per_cu->from_debug_types)
14677 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14678 else
14679 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14680 if (type_hash == NULL)
14681 return NULL;
1c379e20 14682
673bfd45 14683 ofs.offset = offset;
1c379e20
DJ
14684 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14685 if (slot)
14686 return slot->type;
14687 else
14688 return NULL;
14689}
14690
673bfd45
DE
14691/* Look up the type for DIE in the appropriate type_hash table,
14692 or return NULL if DIE does not have a saved type. */
14693
14694static struct type *
14695get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14696{
14697 return get_die_type_at_offset (die->offset, cu->per_cu);
14698}
14699
10b3939b
DJ
14700/* Add a dependence relationship from CU to REF_PER_CU. */
14701
14702static void
14703dwarf2_add_dependence (struct dwarf2_cu *cu,
14704 struct dwarf2_per_cu_data *ref_per_cu)
14705{
14706 void **slot;
14707
14708 if (cu->dependencies == NULL)
14709 cu->dependencies
14710 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14711 NULL, &cu->comp_unit_obstack,
14712 hashtab_obstack_allocate,
14713 dummy_obstack_deallocate);
14714
14715 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14716 if (*slot == NULL)
14717 *slot = ref_per_cu;
14718}
1c379e20 14719
f504f079
DE
14720/* Subroutine of dwarf2_mark to pass to htab_traverse.
14721 Set the mark field in every compilation unit in the
ae038cb0
DJ
14722 cache that we must keep because we are keeping CU. */
14723
10b3939b
DJ
14724static int
14725dwarf2_mark_helper (void **slot, void *data)
14726{
14727 struct dwarf2_per_cu_data *per_cu;
14728
14729 per_cu = (struct dwarf2_per_cu_data *) *slot;
14730 if (per_cu->cu->mark)
14731 return 1;
14732 per_cu->cu->mark = 1;
14733
14734 if (per_cu->cu->dependencies != NULL)
14735 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14736
14737 return 1;
14738}
14739
f504f079
DE
14740/* Set the mark field in CU and in every other compilation unit in the
14741 cache that we must keep because we are keeping CU. */
14742
ae038cb0
DJ
14743static void
14744dwarf2_mark (struct dwarf2_cu *cu)
14745{
14746 if (cu->mark)
14747 return;
14748 cu->mark = 1;
10b3939b
DJ
14749 if (cu->dependencies != NULL)
14750 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14751}
14752
14753static void
14754dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14755{
14756 while (per_cu)
14757 {
14758 per_cu->cu->mark = 0;
14759 per_cu = per_cu->cu->read_in_chain;
14760 }
72bf9492
DJ
14761}
14762
72bf9492
DJ
14763/* Trivial hash function for partial_die_info: the hash value of a DIE
14764 is its offset in .debug_info for this objfile. */
14765
14766static hashval_t
14767partial_die_hash (const void *item)
14768{
14769 const struct partial_die_info *part_die = item;
9a619af0 14770
72bf9492
DJ
14771 return part_die->offset;
14772}
14773
14774/* Trivial comparison function for partial_die_info structures: two DIEs
14775 are equal if they have the same offset. */
14776
14777static int
14778partial_die_eq (const void *item_lhs, const void *item_rhs)
14779{
14780 const struct partial_die_info *part_die_lhs = item_lhs;
14781 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14782
72bf9492
DJ
14783 return part_die_lhs->offset == part_die_rhs->offset;
14784}
14785
ae038cb0
DJ
14786static struct cmd_list_element *set_dwarf2_cmdlist;
14787static struct cmd_list_element *show_dwarf2_cmdlist;
14788
14789static void
14790set_dwarf2_cmd (char *args, int from_tty)
14791{
14792 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14793}
14794
14795static void
14796show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14797{
ae038cb0
DJ
14798 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14799}
14800
dce234bc
PP
14801/* If section described by INFO was mmapped, munmap it now. */
14802
14803static void
14804munmap_section_buffer (struct dwarf2_section_info *info)
14805{
14806 if (info->was_mmapped)
14807 {
14808#ifdef HAVE_MMAP
14809 intptr_t begin = (intptr_t) info->buffer;
14810 intptr_t map_begin = begin & ~(pagesize - 1);
14811 size_t map_length = info->size + begin - map_begin;
9a619af0 14812
dce234bc
PP
14813 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14814#else
14815 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14816 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14817#endif
14818 }
14819}
14820
14821/* munmap debug sections for OBJFILE, if necessary. */
14822
14823static void
c1bd65d0 14824dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14825{
14826 struct dwarf2_per_objfile *data = d;
9a619af0 14827
16be1145
DE
14828 /* This is sorted according to the order they're defined in to make it easier
14829 to keep in sync. */
dce234bc
PP
14830 munmap_section_buffer (&data->info);
14831 munmap_section_buffer (&data->abbrev);
14832 munmap_section_buffer (&data->line);
16be1145 14833 munmap_section_buffer (&data->loc);
dce234bc 14834 munmap_section_buffer (&data->macinfo);
16be1145 14835 munmap_section_buffer (&data->str);
dce234bc 14836 munmap_section_buffer (&data->ranges);
16be1145 14837 munmap_section_buffer (&data->types);
dce234bc
PP
14838 munmap_section_buffer (&data->frame);
14839 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14840 munmap_section_buffer (&data->gdb_index);
14841}
14842
14843\f
ae2de4f8 14844/* The "save gdb-index" command. */
9291a0cd
TT
14845
14846/* The contents of the hash table we create when building the string
14847 table. */
14848struct strtab_entry
14849{
14850 offset_type offset;
14851 const char *str;
14852};
14853
14854/* Hash function for a strtab_entry. */
b89be57b 14855
9291a0cd
TT
14856static hashval_t
14857hash_strtab_entry (const void *e)
14858{
14859 const struct strtab_entry *entry = e;
14860 return mapped_index_string_hash (entry->str);
14861}
14862
14863/* Equality function for a strtab_entry. */
b89be57b 14864
9291a0cd
TT
14865static int
14866eq_strtab_entry (const void *a, const void *b)
14867{
14868 const struct strtab_entry *ea = a;
14869 const struct strtab_entry *eb = b;
14870 return !strcmp (ea->str, eb->str);
14871}
14872
14873/* Create a strtab_entry hash table. */
b89be57b 14874
9291a0cd
TT
14875static htab_t
14876create_strtab (void)
14877{
14878 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14879 xfree, xcalloc, xfree);
14880}
14881
14882/* Add a string to the constant pool. Return the string's offset in
14883 host order. */
b89be57b 14884
9291a0cd
TT
14885static offset_type
14886add_string (htab_t table, struct obstack *cpool, const char *str)
14887{
14888 void **slot;
14889 struct strtab_entry entry;
14890 struct strtab_entry *result;
14891
14892 entry.str = str;
14893 slot = htab_find_slot (table, &entry, INSERT);
14894 if (*slot)
14895 result = *slot;
14896 else
14897 {
14898 result = XNEW (struct strtab_entry);
14899 result->offset = obstack_object_size (cpool);
14900 result->str = str;
14901 obstack_grow_str0 (cpool, str);
14902 *slot = result;
14903 }
14904 return result->offset;
14905}
14906
14907/* An entry in the symbol table. */
14908struct symtab_index_entry
14909{
14910 /* The name of the symbol. */
14911 const char *name;
14912 /* The offset of the name in the constant pool. */
14913 offset_type index_offset;
14914 /* A sorted vector of the indices of all the CUs that hold an object
14915 of this name. */
14916 VEC (offset_type) *cu_indices;
14917};
14918
14919/* The symbol table. This is a power-of-2-sized hash table. */
14920struct mapped_symtab
14921{
14922 offset_type n_elements;
14923 offset_type size;
14924 struct symtab_index_entry **data;
14925};
14926
14927/* Hash function for a symtab_index_entry. */
b89be57b 14928
9291a0cd
TT
14929static hashval_t
14930hash_symtab_entry (const void *e)
14931{
14932 const struct symtab_index_entry *entry = e;
14933 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14934 sizeof (offset_type) * VEC_length (offset_type,
14935 entry->cu_indices),
14936 0);
14937}
14938
14939/* Equality function for a symtab_index_entry. */
b89be57b 14940
9291a0cd
TT
14941static int
14942eq_symtab_entry (const void *a, const void *b)
14943{
14944 const struct symtab_index_entry *ea = a;
14945 const struct symtab_index_entry *eb = b;
14946 int len = VEC_length (offset_type, ea->cu_indices);
14947 if (len != VEC_length (offset_type, eb->cu_indices))
14948 return 0;
14949 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14950 VEC_address (offset_type, eb->cu_indices),
14951 sizeof (offset_type) * len);
14952}
14953
14954/* Destroy a symtab_index_entry. */
b89be57b 14955
9291a0cd
TT
14956static void
14957delete_symtab_entry (void *p)
14958{
14959 struct symtab_index_entry *entry = p;
14960 VEC_free (offset_type, entry->cu_indices);
14961 xfree (entry);
14962}
14963
14964/* Create a hash table holding symtab_index_entry objects. */
b89be57b 14965
9291a0cd 14966static htab_t
3876f04e 14967create_symbol_hash_table (void)
9291a0cd
TT
14968{
14969 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14970 delete_symtab_entry, xcalloc, xfree);
14971}
14972
14973/* Create a new mapped symtab object. */
b89be57b 14974
9291a0cd
TT
14975static struct mapped_symtab *
14976create_mapped_symtab (void)
14977{
14978 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14979 symtab->n_elements = 0;
14980 symtab->size = 1024;
14981 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14982 return symtab;
14983}
14984
14985/* Destroy a mapped_symtab. */
b89be57b 14986
9291a0cd
TT
14987static void
14988cleanup_mapped_symtab (void *p)
14989{
14990 struct mapped_symtab *symtab = p;
14991 /* The contents of the array are freed when the other hash table is
14992 destroyed. */
14993 xfree (symtab->data);
14994 xfree (symtab);
14995}
14996
14997/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14998 the slot. */
b89be57b 14999
9291a0cd
TT
15000static struct symtab_index_entry **
15001find_slot (struct mapped_symtab *symtab, const char *name)
15002{
15003 offset_type index, step, hash = mapped_index_string_hash (name);
15004
15005 index = hash & (symtab->size - 1);
15006 step = ((hash * 17) & (symtab->size - 1)) | 1;
15007
15008 for (;;)
15009 {
15010 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15011 return &symtab->data[index];
15012 index = (index + step) & (symtab->size - 1);
15013 }
15014}
15015
15016/* Expand SYMTAB's hash table. */
b89be57b 15017
9291a0cd
TT
15018static void
15019hash_expand (struct mapped_symtab *symtab)
15020{
15021 offset_type old_size = symtab->size;
15022 offset_type i;
15023 struct symtab_index_entry **old_entries = symtab->data;
15024
15025 symtab->size *= 2;
15026 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15027
15028 for (i = 0; i < old_size; ++i)
15029 {
15030 if (old_entries[i])
15031 {
15032 struct symtab_index_entry **slot = find_slot (symtab,
15033 old_entries[i]->name);
15034 *slot = old_entries[i];
15035 }
15036 }
15037
15038 xfree (old_entries);
15039}
15040
15041/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15042 is the index of the CU in which the symbol appears. */
b89be57b 15043
9291a0cd
TT
15044static void
15045add_index_entry (struct mapped_symtab *symtab, const char *name,
15046 offset_type cu_index)
15047{
15048 struct symtab_index_entry **slot;
15049
15050 ++symtab->n_elements;
15051 if (4 * symtab->n_elements / 3 >= symtab->size)
15052 hash_expand (symtab);
15053
15054 slot = find_slot (symtab, name);
15055 if (!*slot)
15056 {
15057 *slot = XNEW (struct symtab_index_entry);
15058 (*slot)->name = name;
15059 (*slot)->cu_indices = NULL;
15060 }
15061 /* Don't push an index twice. Due to how we add entries we only
15062 have to check the last one. */
15063 if (VEC_empty (offset_type, (*slot)->cu_indices)
15064 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15065 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15066}
15067
15068/* Add a vector of indices to the constant pool. */
b89be57b 15069
9291a0cd 15070static offset_type
3876f04e 15071add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15072 struct symtab_index_entry *entry)
15073{
15074 void **slot;
15075
3876f04e 15076 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15077 if (!*slot)
15078 {
15079 offset_type len = VEC_length (offset_type, entry->cu_indices);
15080 offset_type val = MAYBE_SWAP (len);
15081 offset_type iter;
15082 int i;
15083
15084 *slot = entry;
15085 entry->index_offset = obstack_object_size (cpool);
15086
15087 obstack_grow (cpool, &val, sizeof (val));
15088 for (i = 0;
15089 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15090 ++i)
15091 {
15092 val = MAYBE_SWAP (iter);
15093 obstack_grow (cpool, &val, sizeof (val));
15094 }
15095 }
15096 else
15097 {
15098 struct symtab_index_entry *old_entry = *slot;
15099 entry->index_offset = old_entry->index_offset;
15100 entry = old_entry;
15101 }
15102 return entry->index_offset;
15103}
15104
15105/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15106 constant pool entries going into the obstack CPOOL. */
b89be57b 15107
9291a0cd
TT
15108static void
15109write_hash_table (struct mapped_symtab *symtab,
15110 struct obstack *output, struct obstack *cpool)
15111{
15112 offset_type i;
3876f04e 15113 htab_t symbol_hash_table;
9291a0cd
TT
15114 htab_t str_table;
15115
3876f04e 15116 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15117 str_table = create_strtab ();
3876f04e 15118
9291a0cd
TT
15119 /* We add all the index vectors to the constant pool first, to
15120 ensure alignment is ok. */
15121 for (i = 0; i < symtab->size; ++i)
15122 {
15123 if (symtab->data[i])
3876f04e 15124 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15125 }
15126
15127 /* Now write out the hash table. */
15128 for (i = 0; i < symtab->size; ++i)
15129 {
15130 offset_type str_off, vec_off;
15131
15132 if (symtab->data[i])
15133 {
15134 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15135 vec_off = symtab->data[i]->index_offset;
15136 }
15137 else
15138 {
15139 /* While 0 is a valid constant pool index, it is not valid
15140 to have 0 for both offsets. */
15141 str_off = 0;
15142 vec_off = 0;
15143 }
15144
15145 str_off = MAYBE_SWAP (str_off);
15146 vec_off = MAYBE_SWAP (vec_off);
15147
15148 obstack_grow (output, &str_off, sizeof (str_off));
15149 obstack_grow (output, &vec_off, sizeof (vec_off));
15150 }
15151
15152 htab_delete (str_table);
3876f04e 15153 htab_delete (symbol_hash_table);
9291a0cd
TT
15154}
15155
0a5429f6
DE
15156/* Struct to map psymtab to CU index in the index file. */
15157struct psymtab_cu_index_map
15158{
15159 struct partial_symtab *psymtab;
15160 unsigned int cu_index;
15161};
15162
15163static hashval_t
15164hash_psymtab_cu_index (const void *item)
15165{
15166 const struct psymtab_cu_index_map *map = item;
15167
15168 return htab_hash_pointer (map->psymtab);
15169}
15170
15171static int
15172eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15173{
15174 const struct psymtab_cu_index_map *lhs = item_lhs;
15175 const struct psymtab_cu_index_map *rhs = item_rhs;
15176
15177 return lhs->psymtab == rhs->psymtab;
15178}
15179
15180/* Helper struct for building the address table. */
15181struct addrmap_index_data
15182{
15183 struct objfile *objfile;
15184 struct obstack *addr_obstack;
15185 htab_t cu_index_htab;
15186
15187 /* Non-zero if the previous_* fields are valid.
15188 We can't write an entry until we see the next entry (since it is only then
15189 that we know the end of the entry). */
15190 int previous_valid;
15191 /* Index of the CU in the table of all CUs in the index file. */
15192 unsigned int previous_cu_index;
15193 /* Start address of the CU. */
15194 CORE_ADDR previous_cu_start;
15195};
15196
15197/* Write an address entry to OBSTACK. */
b89be57b 15198
9291a0cd 15199static void
0a5429f6
DE
15200add_address_entry (struct objfile *objfile, struct obstack *obstack,
15201 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15202{
0a5429f6 15203 offset_type cu_index_to_write;
9291a0cd
TT
15204 char addr[8];
15205 CORE_ADDR baseaddr;
15206
15207 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15208
0a5429f6
DE
15209 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15210 obstack_grow (obstack, addr, 8);
15211 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15212 obstack_grow (obstack, addr, 8);
15213 cu_index_to_write = MAYBE_SWAP (cu_index);
15214 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15215}
15216
15217/* Worker function for traversing an addrmap to build the address table. */
15218
15219static int
15220add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15221{
15222 struct addrmap_index_data *data = datap;
15223 struct partial_symtab *pst = obj;
15224 offset_type cu_index;
15225 void **slot;
15226
15227 if (data->previous_valid)
15228 add_address_entry (data->objfile, data->addr_obstack,
15229 data->previous_cu_start, start_addr,
15230 data->previous_cu_index);
15231
15232 data->previous_cu_start = start_addr;
15233 if (pst != NULL)
15234 {
15235 struct psymtab_cu_index_map find_map, *map;
15236 find_map.psymtab = pst;
15237 map = htab_find (data->cu_index_htab, &find_map);
15238 gdb_assert (map != NULL);
15239 data->previous_cu_index = map->cu_index;
15240 data->previous_valid = 1;
15241 }
15242 else
15243 data->previous_valid = 0;
15244
15245 return 0;
15246}
15247
15248/* Write OBJFILE's address map to OBSTACK.
15249 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15250 in the index file. */
15251
15252static void
15253write_address_map (struct objfile *objfile, struct obstack *obstack,
15254 htab_t cu_index_htab)
15255{
15256 struct addrmap_index_data addrmap_index_data;
15257
15258 /* When writing the address table, we have to cope with the fact that
15259 the addrmap iterator only provides the start of a region; we have to
15260 wait until the next invocation to get the start of the next region. */
15261
15262 addrmap_index_data.objfile = objfile;
15263 addrmap_index_data.addr_obstack = obstack;
15264 addrmap_index_data.cu_index_htab = cu_index_htab;
15265 addrmap_index_data.previous_valid = 0;
15266
15267 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15268 &addrmap_index_data);
15269
15270 /* It's highly unlikely the last entry (end address = 0xff...ff)
15271 is valid, but we should still handle it.
15272 The end address is recorded as the start of the next region, but that
15273 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15274 anyway. */
15275 if (addrmap_index_data.previous_valid)
15276 add_address_entry (objfile, obstack,
15277 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15278 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15279}
15280
15281/* Add a list of partial symbols to SYMTAB. */
b89be57b 15282
9291a0cd
TT
15283static void
15284write_psymbols (struct mapped_symtab *symtab,
987d643c 15285 htab_t psyms_seen,
9291a0cd
TT
15286 struct partial_symbol **psymp,
15287 int count,
987d643c
TT
15288 offset_type cu_index,
15289 int is_static)
9291a0cd
TT
15290{
15291 for (; count-- > 0; ++psymp)
15292 {
987d643c
TT
15293 void **slot, *lookup;
15294
9291a0cd
TT
15295 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15296 error (_("Ada is not currently supported by the index"));
987d643c
TT
15297
15298 /* We only want to add a given psymbol once. However, we also
15299 want to account for whether it is global or static. So, we
15300 may add it twice, using slightly different values. */
15301 if (is_static)
15302 {
15303 uintptr_t val = 1 | (uintptr_t) *psymp;
15304
15305 lookup = (void *) val;
15306 }
15307 else
15308 lookup = *psymp;
15309
15310 /* Only add a given psymbol once. */
15311 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15312 if (!*slot)
15313 {
15314 *slot = lookup;
15315 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15316 }
9291a0cd
TT
15317 }
15318}
15319
15320/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15321 exception if there is an error. */
b89be57b 15322
9291a0cd
TT
15323static void
15324write_obstack (FILE *file, struct obstack *obstack)
15325{
15326 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15327 file)
15328 != obstack_object_size (obstack))
15329 error (_("couldn't data write to file"));
15330}
15331
15332/* Unlink a file if the argument is not NULL. */
b89be57b 15333
9291a0cd
TT
15334static void
15335unlink_if_set (void *p)
15336{
15337 char **filename = p;
15338 if (*filename)
15339 unlink (*filename);
15340}
15341
1fd400ff
TT
15342/* A helper struct used when iterating over debug_types. */
15343struct signatured_type_index_data
15344{
15345 struct objfile *objfile;
15346 struct mapped_symtab *symtab;
15347 struct obstack *types_list;
987d643c 15348 htab_t psyms_seen;
1fd400ff
TT
15349 int cu_index;
15350};
15351
15352/* A helper function that writes a single signatured_type to an
15353 obstack. */
b89be57b 15354
1fd400ff
TT
15355static int
15356write_one_signatured_type (void **slot, void *d)
15357{
15358 struct signatured_type_index_data *info = d;
15359 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15360 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15361 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15362 gdb_byte val[8];
15363
15364 write_psymbols (info->symtab,
987d643c 15365 info->psyms_seen,
1fd400ff 15366 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15367 psymtab->n_global_syms, info->cu_index,
15368 0);
1fd400ff 15369 write_psymbols (info->symtab,
987d643c 15370 info->psyms_seen,
1fd400ff 15371 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15372 psymtab->n_static_syms, info->cu_index,
15373 1);
1fd400ff
TT
15374
15375 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15376 obstack_grow (info->types_list, val, 8);
15377 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15378 obstack_grow (info->types_list, val, 8);
15379 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15380 obstack_grow (info->types_list, val, 8);
15381
15382 ++info->cu_index;
15383
15384 return 1;
15385}
15386
987d643c
TT
15387/* A cleanup function for an htab_t. */
15388
15389static void
15390cleanup_htab (void *arg)
15391{
15392 htab_delete (arg);
15393}
15394
9291a0cd 15395/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15396
9291a0cd
TT
15397static void
15398write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15399{
15400 struct cleanup *cleanup;
15401 char *filename, *cleanup_filename;
1fd400ff
TT
15402 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15403 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15404 int i;
15405 FILE *out_file;
15406 struct mapped_symtab *symtab;
15407 offset_type val, size_of_contents, total_len;
15408 struct stat st;
15409 char buf[8];
987d643c 15410 htab_t psyms_seen;
0a5429f6
DE
15411 htab_t cu_index_htab;
15412 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd
TT
15413
15414 if (!objfile->psymtabs)
15415 return;
15416 if (dwarf2_per_objfile->using_index)
15417 error (_("Cannot use an index to create the index"));
15418
15419 if (stat (objfile->name, &st) < 0)
7e17e088 15420 perror_with_name (objfile->name);
9291a0cd
TT
15421
15422 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15423 INDEX_SUFFIX, (char *) NULL);
15424 cleanup = make_cleanup (xfree, filename);
15425
15426 out_file = fopen (filename, "wb");
15427 if (!out_file)
15428 error (_("Can't open `%s' for writing"), filename);
15429
15430 cleanup_filename = filename;
15431 make_cleanup (unlink_if_set, &cleanup_filename);
15432
15433 symtab = create_mapped_symtab ();
15434 make_cleanup (cleanup_mapped_symtab, symtab);
15435
15436 obstack_init (&addr_obstack);
15437 make_cleanup_obstack_free (&addr_obstack);
15438
15439 obstack_init (&cu_list);
15440 make_cleanup_obstack_free (&cu_list);
15441
1fd400ff
TT
15442 obstack_init (&types_cu_list);
15443 make_cleanup_obstack_free (&types_cu_list);
15444
987d643c
TT
15445 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15446 NULL, xcalloc, xfree);
15447 make_cleanup (cleanup_htab, psyms_seen);
15448
0a5429f6
DE
15449 /* While we're scanning CU's create a table that maps a psymtab pointer
15450 (which is what addrmap records) to its index (which is what is recorded
15451 in the index file). This will later be needed to write the address
15452 table. */
15453 cu_index_htab = htab_create_alloc (100,
15454 hash_psymtab_cu_index,
15455 eq_psymtab_cu_index,
15456 NULL, xcalloc, xfree);
15457 make_cleanup (cleanup_htab, cu_index_htab);
15458 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15459 xmalloc (sizeof (struct psymtab_cu_index_map)
15460 * dwarf2_per_objfile->n_comp_units);
15461 make_cleanup (xfree, psymtab_cu_index_map);
15462
15463 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15464 work here. Also, the debug_types entries do not appear in
15465 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15466 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15467 {
e254ef6a
DE
15468 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15469 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15470 gdb_byte val[8];
0a5429f6
DE
15471 struct psymtab_cu_index_map *map;
15472 void **slot;
9291a0cd
TT
15473
15474 write_psymbols (symtab,
987d643c 15475 psyms_seen,
9291a0cd 15476 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15477 psymtab->n_global_syms, i,
15478 0);
9291a0cd 15479 write_psymbols (symtab,
987d643c 15480 psyms_seen,
9291a0cd 15481 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15482 psymtab->n_static_syms, i,
15483 1);
9291a0cd 15484
0a5429f6
DE
15485 map = &psymtab_cu_index_map[i];
15486 map->psymtab = psymtab;
15487 map->cu_index = i;
15488 slot = htab_find_slot (cu_index_htab, map, INSERT);
15489 gdb_assert (slot != NULL);
15490 gdb_assert (*slot == NULL);
15491 *slot = map;
9291a0cd 15492
e254ef6a 15493 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15494 obstack_grow (&cu_list, val, 8);
e254ef6a 15495 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15496 obstack_grow (&cu_list, val, 8);
15497 }
15498
0a5429f6
DE
15499 /* Dump the address map. */
15500 write_address_map (objfile, &addr_obstack, cu_index_htab);
15501
1fd400ff
TT
15502 /* Write out the .debug_type entries, if any. */
15503 if (dwarf2_per_objfile->signatured_types)
15504 {
15505 struct signatured_type_index_data sig_data;
15506
15507 sig_data.objfile = objfile;
15508 sig_data.symtab = symtab;
15509 sig_data.types_list = &types_cu_list;
987d643c 15510 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15511 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15512 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15513 write_one_signatured_type, &sig_data);
15514 }
15515
9291a0cd
TT
15516 obstack_init (&constant_pool);
15517 make_cleanup_obstack_free (&constant_pool);
15518 obstack_init (&symtab_obstack);
15519 make_cleanup_obstack_free (&symtab_obstack);
15520 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15521
15522 obstack_init (&contents);
15523 make_cleanup_obstack_free (&contents);
1fd400ff 15524 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15525 total_len = size_of_contents;
15526
15527 /* The version number. */
987d643c 15528 val = MAYBE_SWAP (3);
9291a0cd
TT
15529 obstack_grow (&contents, &val, sizeof (val));
15530
15531 /* The offset of the CU list from the start of the file. */
15532 val = MAYBE_SWAP (total_len);
15533 obstack_grow (&contents, &val, sizeof (val));
15534 total_len += obstack_object_size (&cu_list);
15535
1fd400ff
TT
15536 /* The offset of the types CU list from the start of the file. */
15537 val = MAYBE_SWAP (total_len);
15538 obstack_grow (&contents, &val, sizeof (val));
15539 total_len += obstack_object_size (&types_cu_list);
15540
9291a0cd
TT
15541 /* The offset of the address table from the start of the file. */
15542 val = MAYBE_SWAP (total_len);
15543 obstack_grow (&contents, &val, sizeof (val));
15544 total_len += obstack_object_size (&addr_obstack);
15545
15546 /* The offset of the symbol table from the start of the file. */
15547 val = MAYBE_SWAP (total_len);
15548 obstack_grow (&contents, &val, sizeof (val));
15549 total_len += obstack_object_size (&symtab_obstack);
15550
15551 /* The offset of the constant pool from the start of the file. */
15552 val = MAYBE_SWAP (total_len);
15553 obstack_grow (&contents, &val, sizeof (val));
15554 total_len += obstack_object_size (&constant_pool);
15555
15556 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15557
15558 write_obstack (out_file, &contents);
15559 write_obstack (out_file, &cu_list);
1fd400ff 15560 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15561 write_obstack (out_file, &addr_obstack);
15562 write_obstack (out_file, &symtab_obstack);
15563 write_obstack (out_file, &constant_pool);
15564
15565 fclose (out_file);
15566
15567 /* We want to keep the file, so we set cleanup_filename to NULL
15568 here. See unlink_if_set. */
15569 cleanup_filename = NULL;
15570
15571 do_cleanups (cleanup);
15572}
15573
15574/* The mapped index file format is designed to be directly mmap()able
15575 on any architecture. In most cases, a datum is represented using a
15576 little-endian 32-bit integer value, called an offset_type. Big
15577 endian machines must byte-swap the values before using them.
15578 Exceptions to this rule are noted. The data is laid out such that
15579 alignment is always respected.
15580
15581 A mapped index consists of several sections.
15582
15583 1. The file header. This is a sequence of values, of offset_type
15584 unless otherwise noted:
987d643c
TT
15585
15586 [0] The version number, currently 3. Versions 1 and 2 are
15587 obsolete.
9291a0cd 15588 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15589 [2] The offset, from the start of the file, of the types CU list.
15590 Note that this section can be empty, in which case this offset will
15591 be equal to the next offset.
15592 [3] The offset, from the start of the file, of the address section.
15593 [4] The offset, from the start of the file, of the symbol table.
15594 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15595
15596 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15597 little-endian values, sorted by the CU offset. The first element
15598 in each pair is the offset of a CU in the .debug_info section. The
15599 second element in each pair is the length of that CU. References
15600 to a CU elsewhere in the map are done using a CU index, which is
15601 just the 0-based index into this table. Note that if there are
15602 type CUs, then conceptually CUs and type CUs form a single list for
15603 the purposes of CU indices.
15604
987d643c
TT
15605 3. The types CU list. This is a sequence of triplets of 64-bit
15606 little-endian values. In a triplet, the first value is the CU
15607 offset, the second value is the type offset in the CU, and the
15608 third value is the type signature. The types CU list is not
15609 sorted.
9291a0cd 15610
987d643c 15611 4. The address section. The address section consists of a sequence
9291a0cd
TT
15612 of address entries. Each address entry has three elements.
15613 [0] The low address. This is a 64-bit little-endian value.
15614 [1] The high address. This is a 64-bit little-endian value.
148c11bf 15615 Like DW_AT_high_pc, the value is one byte beyond the end.
9291a0cd
TT
15616 [2] The CU index. This is an offset_type value.
15617
987d643c 15618 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15619 table is always a power of 2. The initial hash and the step are
15620 currently defined by the `find_slot' function.
15621
15622 Each slot in the hash table consists of a pair of offset_type
15623 values. The first value is the offset of the symbol's name in the
15624 constant pool. The second value is the offset of the CU vector in
15625 the constant pool.
15626
15627 If both values are 0, then this slot in the hash table is empty.
15628 This is ok because while 0 is a valid constant pool index, it
15629 cannot be a valid index for both a string and a CU vector.
15630
15631 A string in the constant pool is stored as a \0-terminated string,
15632 as you'd expect.
15633
15634 A CU vector in the constant pool is a sequence of offset_type
15635 values. The first value is the number of CU indices in the vector.
15636 Each subsequent value is the index of a CU in the CU list. This
15637 element in the hash table is used to indicate which CUs define the
15638 symbol.
15639
987d643c 15640 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15641 organized so that alignment is correct: CU vectors are stored
15642 first, followed by strings. */
11570e71 15643
9291a0cd
TT
15644static void
15645save_gdb_index_command (char *arg, int from_tty)
15646{
15647 struct objfile *objfile;
15648
15649 if (!arg || !*arg)
96d19272 15650 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15651
15652 ALL_OBJFILES (objfile)
15653 {
15654 struct stat st;
15655
15656 /* If the objfile does not correspond to an actual file, skip it. */
15657 if (stat (objfile->name, &st) < 0)
15658 continue;
15659
15660 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15661 if (dwarf2_per_objfile)
15662 {
15663 volatile struct gdb_exception except;
15664
15665 TRY_CATCH (except, RETURN_MASK_ERROR)
15666 {
15667 write_psymtabs_to_index (objfile, arg);
15668 }
15669 if (except.reason < 0)
15670 exception_fprintf (gdb_stderr, except,
15671 _("Error while writing index for `%s': "),
15672 objfile->name);
15673 }
15674 }
dce234bc
PP
15675}
15676
9291a0cd
TT
15677\f
15678
9eae7c52
TT
15679int dwarf2_always_disassemble;
15680
15681static void
15682show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15683 struct cmd_list_element *c, const char *value)
15684{
15685 fprintf_filtered (file, _("\
15686Whether to always disassemble DWARF expressions is %s.\n"),
15687 value);
15688}
15689
6502dd73
DJ
15690void _initialize_dwarf2_read (void);
15691
15692void
15693_initialize_dwarf2_read (void)
15694{
96d19272
JK
15695 struct cmd_list_element *c;
15696
dce234bc 15697 dwarf2_objfile_data_key
c1bd65d0 15698 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15699
1bedd215
AC
15700 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15701Set DWARF 2 specific variables.\n\
15702Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15703 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15704 0/*allow-unknown*/, &maintenance_set_cmdlist);
15705
1bedd215
AC
15706 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15707Show DWARF 2 specific variables\n\
15708Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15709 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15710 0/*allow-unknown*/, &maintenance_show_cmdlist);
15711
15712 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15713 &dwarf2_max_cache_age, _("\
15714Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15715Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15716A higher limit means that cached compilation units will be stored\n\
15717in memory longer, and more total memory will be used. Zero disables\n\
15718caching, which can slow down startup."),
2c5b56ce 15719 NULL,
920d2a44 15720 show_dwarf2_max_cache_age,
2c5b56ce 15721 &set_dwarf2_cmdlist,
ae038cb0 15722 &show_dwarf2_cmdlist);
d97bc12b 15723
9eae7c52
TT
15724 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15725 &dwarf2_always_disassemble, _("\
15726Set whether `info address' always disassembles DWARF expressions."), _("\
15727Show whether `info address' always disassembles DWARF expressions."), _("\
15728When enabled, DWARF expressions are always printed in an assembly-like\n\
15729syntax. When disabled, expressions will be printed in a more\n\
15730conversational style, when possible."),
15731 NULL,
15732 show_dwarf2_always_disassemble,
15733 &set_dwarf2_cmdlist,
15734 &show_dwarf2_cmdlist);
15735
d97bc12b
DE
15736 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15737Set debugging of the dwarf2 DIE reader."), _("\
15738Show debugging of the dwarf2 DIE reader."), _("\
15739When enabled (non-zero), DIEs are dumped after they are read in.\n\
15740The value is the maximum depth to print."),
15741 NULL,
15742 NULL,
15743 &setdebuglist, &showdebuglist);
9291a0cd 15744
96d19272 15745 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71
DE
15746 _("\
15747Save a .gdb-index file.\n\
15748Usage: save gdb-index DIRECTORY"),
96d19272
JK
15749 &save_cmdlist);
15750 set_cmd_completer (c, filename_completer);
6502dd73 15751}
This page took 2.109052 seconds and 4 git commands to generate.