* linux-low.c (linux_prepare_to_access_memory): New.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
673bfd45
DE
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
6502dd73
DJ
226};
227
228static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
229
230/* names of the debugging sections */
231
233a11ab
CS
232/* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235#define INFO_SECTION "debug_info"
236#define ABBREV_SECTION "debug_abbrev"
237#define LINE_SECTION "debug_line"
233a11ab
CS
238#define LOC_SECTION "debug_loc"
239#define MACINFO_SECTION "debug_macinfo"
240#define STR_SECTION "debug_str"
241#define RANGES_SECTION "debug_ranges"
348e048f 242#define TYPES_SECTION "debug_types"
233a11ab
CS
243#define FRAME_SECTION "debug_frame"
244#define EH_FRAME_SECTION "eh_frame"
9291a0cd 245#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
246
247/* local data types */
248
57349743
JB
249/* We hold several abbreviation tables in memory at the same time. */
250#ifndef ABBREV_HASH_SIZE
251#define ABBREV_HASH_SIZE 121
252#endif
253
107d2387
AC
254/* The data in a compilation unit header, after target2host
255 translation, looks like this. */
c906108c 256struct comp_unit_head
a738430d 257{
c764a876 258 unsigned int length;
a738430d 259 short version;
a738430d
MK
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
9cbfa09e 262 unsigned int abbrev_offset;
57349743 263
a738430d
MK
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
57349743 266
a738430d
MK
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
57349743 269
a738430d
MK
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
57349743 273
d00adf39
DE
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
a738430d 277};
c906108c 278
3da10d80
KS
279/* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281struct delayed_method_info
282{
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297};
298
299typedef struct delayed_method_info delayed_method_info;
300DEF_VEC_O (delayed_method_info);
301
e7c27a73
DJ
302/* Internal state when decoding a particular compilation unit. */
303struct dwarf2_cu
304{
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
d00adf39 308 /* The header of the compilation unit. */
e7c27a73 309 struct comp_unit_head header;
e142c38c 310
d00adf39
DE
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
e142c38c
DJ
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492
DJ
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
10b3939b 361 /* A hash table of die offsets for following references. */
51545339 362 htab_t die_hash;
10b3939b
DJ
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
cb1df416
DJ
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
3da10d80
KS
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
ae038cb0
DJ
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
72bf9492
DJ
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
e7c27a73
DJ
391};
392
9291a0cd
TT
393/* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396struct dwarf2_per_cu_quick_data
397{
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417};
418
10b3939b
DJ
419/* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
ae038cb0
DJ
425struct dwarf2_per_cu_data
426{
348e048f 427 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 428 bytes should suffice to store the length of any compilation unit
45452591
DE
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
c764a876 432 unsigned int offset;
348e048f 433 unsigned int length : 29;
ae038cb0
DJ
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
c764a876 437 unsigned int queued : 1;
ae038cb0 438
5afb4e99
DJ
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
348e048f
DE
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
17ea53c3
JK
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
ae038cb0 451 struct dwarf2_cu *cu;
1c379e20 452
9291a0cd
TT
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
ae038cb0
DJ
468};
469
348e048f
DE
470/* Entry in the signatured_types hash table. */
471
472struct signatured_type
473{
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484};
485
93311388
DE
486/* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492struct die_reader_specs
493{
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503};
504
debd256d
JB
505/* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508struct line_header
509{
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
2dc7f7b3 514 unsigned char maximum_ops_per_instruction;
debd256d
JB
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
c906108c 538 {
debd256d
JB
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
aaa75496 543 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 544 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
545 } *file_names;
546
547 /* The start and end of the statement program following this
6502dd73 548 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 549 gdb_byte *statement_program_start, *statement_program_end;
debd256d 550};
c906108c
SS
551
552/* When we construct a partial symbol table entry we only
553 need this much information. */
554struct partial_die_info
555 {
72bf9492 556 /* Offset of this DIE. */
c906108c 557 unsigned int offset;
72bf9492
DJ
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
72bf9492
DJ
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
fa4028e9
JB
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
98bfdba5
PA
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
72bf9492 580 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 581 sometimes a default name for unnamed DIEs. */
c906108c 582 char *name;
72bf9492
DJ
583
584 /* The scope to prepend to our children. This is generally
585 allocated on the comp_unit_obstack, so will disappear
586 when this compilation unit leaves the cache. */
587 char *scope;
588
589 /* The location description associated with this DIE, if any. */
590 struct dwarf_block *locdesc;
591
592 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
593 CORE_ADDR lowpc;
594 CORE_ADDR highpc;
72bf9492 595
93311388 596 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 597 DW_AT_sibling, if any. */
fe1b8b76 598 gdb_byte *sibling;
72bf9492
DJ
599
600 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
601 DW_AT_specification (or DW_AT_abstract_origin or
602 DW_AT_extension). */
603 unsigned int spec_offset;
604
605 /* Pointers to this DIE's parent, first child, and next sibling,
606 if any. */
607 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
608 };
609
610/* This data structure holds the information of an abbrev. */
611struct abbrev_info
612 {
613 unsigned int number; /* number identifying abbrev */
614 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
615 unsigned short has_children; /* boolean */
616 unsigned short num_attrs; /* number of attributes */
c906108c
SS
617 struct attr_abbrev *attrs; /* an array of attribute descriptions */
618 struct abbrev_info *next; /* next in chain */
619 };
620
621struct attr_abbrev
622 {
9d25dd43
DE
623 ENUM_BITFIELD(dwarf_attribute) name : 16;
624 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
625 };
626
b60c80d6
DJ
627/* Attributes have a name and a value */
628struct attribute
629 {
9d25dd43 630 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
631 ENUM_BITFIELD(dwarf_form) form : 15;
632
633 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
634 field should be in u.str (existing only for DW_STRING) but it is kept
635 here for better struct attribute alignment. */
636 unsigned int string_is_canonical : 1;
637
b60c80d6
DJ
638 union
639 {
640 char *str;
641 struct dwarf_block *blk;
43bbcdc2
PH
642 ULONGEST unsnd;
643 LONGEST snd;
b60c80d6 644 CORE_ADDR addr;
348e048f 645 struct signatured_type *signatured_type;
b60c80d6
DJ
646 }
647 u;
648 };
649
c906108c
SS
650/* This data structure holds a complete die structure. */
651struct die_info
652 {
76815b17
DE
653 /* DWARF-2 tag for this DIE. */
654 ENUM_BITFIELD(dwarf_tag) tag : 16;
655
656 /* Number of attributes */
98bfdba5
PA
657 unsigned char num_attrs;
658
659 /* True if we're presently building the full type name for the
660 type derived from this DIE. */
661 unsigned char building_fullname : 1;
76815b17
DE
662
663 /* Abbrev number */
664 unsigned int abbrev;
665
93311388 666 /* Offset in .debug_info or .debug_types section. */
76815b17 667 unsigned int offset;
78ba4af6
JB
668
669 /* The dies in a compilation unit form an n-ary tree. PARENT
670 points to this die's parent; CHILD points to the first child of
671 this node; and all the children of a given node are chained
672 together via their SIBLING fields, terminated by a die whose
673 tag is zero. */
639d11d3
DC
674 struct die_info *child; /* Its first child, if any. */
675 struct die_info *sibling; /* Its next sibling, if any. */
676 struct die_info *parent; /* Its parent, if any. */
c906108c 677
b60c80d6
DJ
678 /* An array of attributes, with NUM_ATTRS elements. There may be
679 zero, but it's not common and zero-sized arrays are not
680 sufficiently portable C. */
681 struct attribute attrs[1];
c906108c
SS
682 };
683
5fb290d7
DJ
684struct function_range
685{
686 const char *name;
687 CORE_ADDR lowpc, highpc;
688 int seen_line;
689 struct function_range *next;
690};
691
c906108c
SS
692/* Get at parts of an attribute structure */
693
694#define DW_STRING(attr) ((attr)->u.str)
8285870a 695#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
696#define DW_UNSND(attr) ((attr)->u.unsnd)
697#define DW_BLOCK(attr) ((attr)->u.blk)
698#define DW_SND(attr) ((attr)->u.snd)
699#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 700#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
701
702/* Blocks are a bunch of untyped bytes. */
703struct dwarf_block
704 {
705 unsigned int size;
fe1b8b76 706 gdb_byte *data;
c906108c
SS
707 };
708
c906108c
SS
709#ifndef ATTR_ALLOC_CHUNK
710#define ATTR_ALLOC_CHUNK 4
711#endif
712
c906108c
SS
713/* Allocate fields for structs, unions and enums in this size. */
714#ifndef DW_FIELD_ALLOC_CHUNK
715#define DW_FIELD_ALLOC_CHUNK 4
716#endif
717
c906108c
SS
718/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
719 but this would require a corresponding change in unpack_field_as_long
720 and friends. */
721static int bits_per_byte = 8;
722
723/* The routines that read and process dies for a C struct or C++ class
724 pass lists of data member fields and lists of member function fields
725 in an instance of a field_info structure, as defined below. */
726struct field_info
c5aa993b
JM
727 {
728 /* List of data member and baseclasses fields. */
729 struct nextfield
730 {
731 struct nextfield *next;
732 int accessibility;
733 int virtuality;
734 struct field field;
735 }
7d0ccb61 736 *fields, *baseclasses;
c906108c 737
7d0ccb61 738 /* Number of fields (including baseclasses). */
c5aa993b 739 int nfields;
c906108c 740
c5aa993b
JM
741 /* Number of baseclasses. */
742 int nbaseclasses;
c906108c 743
c5aa993b
JM
744 /* Set if the accesibility of one of the fields is not public. */
745 int non_public_fields;
c906108c 746
c5aa993b
JM
747 /* Member function fields array, entries are allocated in the order they
748 are encountered in the object file. */
749 struct nextfnfield
750 {
751 struct nextfnfield *next;
752 struct fn_field fnfield;
753 }
754 *fnfields;
c906108c 755
c5aa993b
JM
756 /* Member function fieldlist array, contains name of possibly overloaded
757 member function, number of overloaded member functions and a pointer
758 to the head of the member function field chain. */
759 struct fnfieldlist
760 {
761 char *name;
762 int length;
763 struct nextfnfield *head;
764 }
765 *fnfieldlists;
c906108c 766
c5aa993b
JM
767 /* Number of entries in the fnfieldlists array. */
768 int nfnfields;
98751a41
JK
769
770 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
771 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
772 struct typedef_field_list
773 {
774 struct typedef_field field;
775 struct typedef_field_list *next;
776 }
777 *typedef_field_list;
778 unsigned typedef_field_list_count;
c5aa993b 779 };
c906108c 780
10b3939b
DJ
781/* One item on the queue of compilation units to read in full symbols
782 for. */
783struct dwarf2_queue_item
784{
785 struct dwarf2_per_cu_data *per_cu;
786 struct dwarf2_queue_item *next;
787};
788
789/* The current queue. */
790static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
791
ae038cb0
DJ
792/* Loaded secondary compilation units are kept in memory until they
793 have not been referenced for the processing of this many
794 compilation units. Set this to zero to disable caching. Cache
795 sizes of up to at least twenty will improve startup time for
796 typical inter-CU-reference binaries, at an obvious memory cost. */
797static int dwarf2_max_cache_age = 5;
920d2a44
AC
798static void
799show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
800 struct cmd_list_element *c, const char *value)
801{
802 fprintf_filtered (file, _("\
803The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
804 value);
805}
806
ae038cb0 807
c906108c
SS
808/* Various complaints about symbol reading that don't abort the process */
809
4d3c2250
KB
810static void
811dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 812{
4d3c2250 813 complaint (&symfile_complaints,
e2e0b3e5 814 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
815}
816
25e43795
DJ
817static void
818dwarf2_debug_line_missing_file_complaint (void)
819{
820 complaint (&symfile_complaints,
821 _(".debug_line section has line data without a file"));
822}
823
59205f5a
JB
824static void
825dwarf2_debug_line_missing_end_sequence_complaint (void)
826{
827 complaint (&symfile_complaints,
828 _(".debug_line section has line program sequence without an end"));
829}
830
4d3c2250
KB
831static void
832dwarf2_complex_location_expr_complaint (void)
2e276125 833{
e2e0b3e5 834 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
835}
836
4d3c2250
KB
837static void
838dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
839 int arg3)
2e276125 840{
4d3c2250 841 complaint (&symfile_complaints,
e2e0b3e5 842 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
843 arg2, arg3);
844}
845
846static void
847dwarf2_macros_too_long_complaint (void)
2e276125 848{
4d3c2250 849 complaint (&symfile_complaints,
e2e0b3e5 850 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
851}
852
853static void
854dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 855{
4d3c2250 856 complaint (&symfile_complaints,
e2e0b3e5 857 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
858 arg1);
859}
860
861static void
862dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 863{
4d3c2250 864 complaint (&symfile_complaints,
e2e0b3e5 865 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 866}
c906108c 867
c906108c
SS
868/* local function prototypes */
869
4efb68b1 870static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 871
aaa75496
JB
872static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873 struct objfile *);
874
c67a9c90 875static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 876
72bf9492
DJ
877static void scan_partial_symbols (struct partial_die_info *,
878 CORE_ADDR *, CORE_ADDR *,
5734ee8b 879 int, struct dwarf2_cu *);
c906108c 880
72bf9492
DJ
881static void add_partial_symbol (struct partial_die_info *,
882 struct dwarf2_cu *);
63d06c5c 883
72bf9492
DJ
884static void add_partial_namespace (struct partial_die_info *pdi,
885 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 886 int need_pc, struct dwarf2_cu *cu);
63d06c5c 887
5d7cb8df
JK
888static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
889 CORE_ADDR *highpc, int need_pc,
890 struct dwarf2_cu *cu);
891
72bf9492
DJ
892static void add_partial_enumeration (struct partial_die_info *enum_pdi,
893 struct dwarf2_cu *cu);
91c24f0a 894
bc30ff58
JB
895static void add_partial_subprogram (struct partial_die_info *pdi,
896 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 897 int need_pc, struct dwarf2_cu *cu);
bc30ff58 898
fe1b8b76 899static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
900 gdb_byte *buffer, gdb_byte *info_ptr,
901 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 902
a14ed312 903static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 904
a14ed312 905static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 906
e7c27a73 907static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 908
f3dd6933 909static void dwarf2_free_abbrev_table (void *);
c906108c 910
fe1b8b76 911static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 912 struct dwarf2_cu *);
72bf9492 913
57349743 914static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 915 struct dwarf2_cu *);
c906108c 916
93311388
DE
917static struct partial_die_info *load_partial_dies (bfd *,
918 gdb_byte *, gdb_byte *,
919 int, struct dwarf2_cu *);
72bf9492 920
fe1b8b76 921static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
922 struct abbrev_info *abbrev,
923 unsigned int, bfd *,
924 gdb_byte *, gdb_byte *,
925 struct dwarf2_cu *);
c906108c 926
c764a876 927static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 928 struct dwarf2_cu *);
72bf9492
DJ
929
930static void fixup_partial_die (struct partial_die_info *,
931 struct dwarf2_cu *);
932
fe1b8b76
JB
933static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
934 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 935
fe1b8b76
JB
936static gdb_byte *read_attribute_value (struct attribute *, unsigned,
937 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 938
fe1b8b76 939static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 940
fe1b8b76 941static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 942
fe1b8b76 943static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 944
fe1b8b76 945static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 946
93311388 947static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 948
fe1b8b76 949static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 950 unsigned int *);
c906108c 951
c764a876
DE
952static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
953
954static LONGEST read_checked_initial_length_and_offset
955 (bfd *, gdb_byte *, const struct comp_unit_head *,
956 unsigned int *, unsigned int *);
613e1657 957
fe1b8b76 958static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
959 unsigned int *);
960
961static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 962
fe1b8b76 963static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 964
9b1c24c8 965static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 966
fe1b8b76
JB
967static char *read_indirect_string (bfd *, gdb_byte *,
968 const struct comp_unit_head *,
969 unsigned int *);
4bdf3d34 970
fe1b8b76 971static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 972
fe1b8b76 973static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 974
fe1b8b76 975static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 976
e142c38c 977static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 978
e142c38c
DJ
979static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
980 struct dwarf2_cu *);
c906108c 981
348e048f
DE
982static struct attribute *dwarf2_attr_no_follow (struct die_info *,
983 unsigned int,
984 struct dwarf2_cu *);
985
05cf31d1
JB
986static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
987 struct dwarf2_cu *cu);
988
e142c38c 989static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 990
e142c38c 991static struct die_info *die_specification (struct die_info *die,
f2f0e013 992 struct dwarf2_cu **);
63d06c5c 993
debd256d
JB
994static void free_line_header (struct line_header *lh);
995
aaa75496
JB
996static void add_file_name (struct line_header *, char *, unsigned int,
997 unsigned int, unsigned int);
998
debd256d
JB
999static struct line_header *(dwarf_decode_line_header
1000 (unsigned int offset,
e7c27a73 1001 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
1002
1003static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 1004 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1005
4f1520fb 1006static void dwarf2_start_subfile (char *, char *, char *);
c906108c 1007
a14ed312 1008static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1009 struct dwarf2_cu *);
c906108c 1010
34eaf542
TT
1011static struct symbol *new_symbol_full (struct die_info *, struct type *,
1012 struct dwarf2_cu *, struct symbol *);
1013
a14ed312 1014static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1015 struct dwarf2_cu *);
c906108c 1016
98bfdba5
PA
1017static void dwarf2_const_value_attr (struct attribute *attr,
1018 struct type *type,
1019 const char *name,
1020 struct obstack *obstack,
1021 struct dwarf2_cu *cu, long *value,
1022 gdb_byte **bytes,
1023 struct dwarf2_locexpr_baton **baton);
2df3850c 1024
e7c27a73 1025static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1026
b4ba55a1
JB
1027static int need_gnat_info (struct dwarf2_cu *);
1028
1029static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1030
1031static void set_descriptive_type (struct type *, struct die_info *,
1032 struct dwarf2_cu *);
1033
e7c27a73
DJ
1034static struct type *die_containing_type (struct die_info *,
1035 struct dwarf2_cu *);
c906108c 1036
673bfd45
DE
1037static struct type *lookup_die_type (struct die_info *, struct attribute *,
1038 struct dwarf2_cu *);
c906108c 1039
f792889a 1040static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1041
673bfd45
DE
1042static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1043
086ed43d 1044static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1045
6e70227d 1046static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1047 const char *suffix, int physname,
1048 struct dwarf2_cu *cu);
63d06c5c 1049
e7c27a73 1050static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1051
348e048f
DE
1052static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1053
e7c27a73 1054static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1055
e7c27a73 1056static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1057
ff013f42
JK
1058static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1059 struct dwarf2_cu *, struct partial_symtab *);
1060
a14ed312 1061static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1062 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1063 struct partial_symtab *);
c906108c 1064
fae299cd
DC
1065static void get_scope_pc_bounds (struct die_info *,
1066 CORE_ADDR *, CORE_ADDR *,
1067 struct dwarf2_cu *);
1068
801e3a5b
JB
1069static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1070 CORE_ADDR, struct dwarf2_cu *);
1071
a14ed312 1072static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1073 struct dwarf2_cu *);
c906108c 1074
a14ed312 1075static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1076 struct type *, struct dwarf2_cu *);
c906108c 1077
a14ed312 1078static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1079 struct die_info *, struct type *,
e7c27a73 1080 struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1083 struct type *, struct dwarf2_cu *);
c906108c 1084
134d01f1 1085static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1086
e7c27a73 1087static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1088
e7c27a73 1089static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1090
5d7cb8df
JK
1091static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1092
27aa8d6a
SW
1093static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1094
f55ee35c
JK
1095static struct type *read_module_type (struct die_info *die,
1096 struct dwarf2_cu *cu);
1097
38d518c9 1098static const char *namespace_name (struct die_info *die,
e142c38c 1099 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1100
134d01f1 1101static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1102
e7c27a73 1103static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1104
6e70227d 1105static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1106 struct dwarf2_cu *);
1107
93311388 1108static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1109
93311388
DE
1110static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1111 gdb_byte *info_ptr,
d97bc12b
DE
1112 gdb_byte **new_info_ptr,
1113 struct die_info *parent);
1114
93311388
DE
1115static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1116 gdb_byte *info_ptr,
fe1b8b76 1117 gdb_byte **new_info_ptr,
639d11d3
DC
1118 struct die_info *parent);
1119
93311388
DE
1120static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1121 gdb_byte *info_ptr,
fe1b8b76 1122 gdb_byte **new_info_ptr,
639d11d3
DC
1123 struct die_info *parent);
1124
93311388
DE
1125static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1126 struct die_info **, gdb_byte *,
1127 int *);
1128
e7c27a73 1129static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1130
71c25dea
TT
1131static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1132 struct obstack *);
1133
e142c38c 1134static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1135
98bfdba5
PA
1136static const char *dwarf2_full_name (char *name,
1137 struct die_info *die,
1138 struct dwarf2_cu *cu);
1139
e142c38c 1140static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1141 struct dwarf2_cu **);
9219021c 1142
a14ed312 1143static char *dwarf_tag_name (unsigned int);
c906108c 1144
a14ed312 1145static char *dwarf_attr_name (unsigned int);
c906108c 1146
a14ed312 1147static char *dwarf_form_name (unsigned int);
c906108c 1148
a14ed312 1149static char *dwarf_bool_name (unsigned int);
c906108c 1150
a14ed312 1151static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1152
1153#if 0
a14ed312 1154static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1155#endif
1156
f9aca02d 1157static struct die_info *sibling_die (struct die_info *);
c906108c 1158
d97bc12b
DE
1159static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1160
1161static void dump_die_for_error (struct die_info *);
1162
1163static void dump_die_1 (struct ui_file *, int level, int max_level,
1164 struct die_info *);
c906108c 1165
d97bc12b 1166/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1167
51545339 1168static void store_in_ref_table (struct die_info *,
10b3939b 1169 struct dwarf2_cu *);
c906108c 1170
93311388
DE
1171static int is_ref_attr (struct attribute *);
1172
c764a876 1173static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1174
43bbcdc2 1175static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1176
348e048f
DE
1177static struct die_info *follow_die_ref_or_sig (struct die_info *,
1178 struct attribute *,
1179 struct dwarf2_cu **);
1180
10b3939b
DJ
1181static struct die_info *follow_die_ref (struct die_info *,
1182 struct attribute *,
f2f0e013 1183 struct dwarf2_cu **);
c906108c 1184
348e048f
DE
1185static struct die_info *follow_die_sig (struct die_info *,
1186 struct attribute *,
1187 struct dwarf2_cu **);
1188
1189static void read_signatured_type_at_offset (struct objfile *objfile,
1190 unsigned int offset);
1191
1192static void read_signatured_type (struct objfile *,
1193 struct signatured_type *type_sig);
1194
c906108c
SS
1195/* memory allocation interface */
1196
7b5a2f43 1197static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1198
f3dd6933 1199static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1200
b60c80d6 1201static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1202
e142c38c 1203static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1204
e142c38c
DJ
1205static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1206 struct dwarf2_cu *);
5fb290d7 1207
2e276125 1208static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1209 char *, bfd *, struct dwarf2_cu *);
2e276125 1210
8e19ed76
PS
1211static int attr_form_is_block (struct attribute *);
1212
3690dd37
JB
1213static int attr_form_is_section_offset (struct attribute *);
1214
1215static int attr_form_is_constant (struct attribute *);
1216
93e7bd98
DJ
1217static void dwarf2_symbol_mark_computed (struct attribute *attr,
1218 struct symbol *sym,
1219 struct dwarf2_cu *cu);
4c2df51b 1220
93311388
DE
1221static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1222 struct abbrev_info *abbrev,
1223 struct dwarf2_cu *cu);
4bb7a0a7 1224
72bf9492
DJ
1225static void free_stack_comp_unit (void *);
1226
72bf9492
DJ
1227static hashval_t partial_die_hash (const void *item);
1228
1229static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1230
ae038cb0 1231static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1232 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1233
1234static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1235 (unsigned int offset, struct objfile *objfile);
ae038cb0 1236
93311388
DE
1237static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1238
ae038cb0
DJ
1239static void free_one_comp_unit (void *);
1240
1241static void free_cached_comp_units (void *);
1242
1243static void age_cached_comp_units (void);
1244
1245static void free_one_cached_comp_unit (void *);
1246
f792889a
DJ
1247static struct type *set_die_type (struct die_info *, struct type *,
1248 struct dwarf2_cu *);
1c379e20 1249
ae038cb0
DJ
1250static void create_all_comp_units (struct objfile *);
1251
1fd400ff
TT
1252static int create_debug_types_hash_table (struct objfile *objfile);
1253
93311388
DE
1254static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1255 struct objfile *);
10b3939b
DJ
1256
1257static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1258
1259static void dwarf2_add_dependence (struct dwarf2_cu *,
1260 struct dwarf2_per_cu_data *);
1261
ae038cb0
DJ
1262static void dwarf2_mark (struct dwarf2_cu *);
1263
1264static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1265
673bfd45
DE
1266static struct type *get_die_type_at_offset (unsigned int,
1267 struct dwarf2_per_cu_data *per_cu);
1268
f792889a 1269static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1270
9291a0cd
TT
1271static void dwarf2_release_queue (void *dummy);
1272
1273static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1274 struct objfile *objfile);
1275
1276static void process_queue (struct objfile *objfile);
1277
1278static void find_file_and_directory (struct die_info *die,
1279 struct dwarf2_cu *cu,
1280 char **name, char **comp_dir);
1281
1282static char *file_full_name (int file, struct line_header *lh,
1283 const char *comp_dir);
1284
1285static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1286 gdb_byte *info_ptr,
1287 gdb_byte *buffer,
1288 unsigned int buffer_size,
1289 bfd *abfd);
1290
1291static void init_cu_die_reader (struct die_reader_specs *reader,
1292 struct dwarf2_cu *cu);
1293
673bfd45 1294static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1295
9291a0cd
TT
1296#if WORDS_BIGENDIAN
1297
1298/* Convert VALUE between big- and little-endian. */
1299static offset_type
1300byte_swap (offset_type value)
1301{
1302 offset_type result;
1303
1304 result = (value & 0xff) << 24;
1305 result |= (value & 0xff00) << 8;
1306 result |= (value & 0xff0000) >> 8;
1307 result |= (value & 0xff000000) >> 24;
1308 return result;
1309}
1310
1311#define MAYBE_SWAP(V) byte_swap (V)
1312
1313#else
1314#define MAYBE_SWAP(V) (V)
1315#endif /* WORDS_BIGENDIAN */
1316
1317/* The suffix for an index file. */
1318#define INDEX_SUFFIX ".gdb-index"
1319
3da10d80
KS
1320static const char *dwarf2_physname (char *name, struct die_info *die,
1321 struct dwarf2_cu *cu);
1322
c906108c
SS
1323/* Try to locate the sections we need for DWARF 2 debugging
1324 information and return true if we have enough to do something. */
1325
1326int
6502dd73 1327dwarf2_has_info (struct objfile *objfile)
c906108c 1328{
be391dca
TT
1329 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1330 if (!dwarf2_per_objfile)
1331 {
1332 /* Initialize per-objfile state. */
1333 struct dwarf2_per_objfile *data
1334 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1335
be391dca
TT
1336 memset (data, 0, sizeof (*data));
1337 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1338 dwarf2_per_objfile = data;
6502dd73 1339
be391dca
TT
1340 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1341 dwarf2_per_objfile->objfile = objfile;
1342 }
1343 return (dwarf2_per_objfile->info.asection != NULL
1344 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1345}
1346
233a11ab
CS
1347/* When loading sections, we can either look for ".<name>", or for
1348 * ".z<name>", which indicates a compressed section. */
1349
1350static int
dce234bc 1351section_is_p (const char *section_name, const char *name)
233a11ab 1352{
dce234bc
PP
1353 return (section_name[0] == '.'
1354 && (strcmp (section_name + 1, name) == 0
1355 || (section_name[1] == 'z'
1356 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1357}
1358
c906108c
SS
1359/* This function is mapped across the sections and remembers the
1360 offset and size of each of the debugging sections we are interested
1361 in. */
1362
1363static void
72dca2f5 1364dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1365{
dce234bc 1366 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1367 {
dce234bc
PP
1368 dwarf2_per_objfile->info.asection = sectp;
1369 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1370 }
dce234bc 1371 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1372 {
dce234bc
PP
1373 dwarf2_per_objfile->abbrev.asection = sectp;
1374 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1375 }
dce234bc 1376 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1377 {
dce234bc
PP
1378 dwarf2_per_objfile->line.asection = sectp;
1379 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1380 }
dce234bc 1381 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1382 {
dce234bc
PP
1383 dwarf2_per_objfile->loc.asection = sectp;
1384 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1385 }
dce234bc 1386 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1387 {
dce234bc
PP
1388 dwarf2_per_objfile->macinfo.asection = sectp;
1389 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1390 }
dce234bc 1391 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1392 {
dce234bc
PP
1393 dwarf2_per_objfile->str.asection = sectp;
1394 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1395 }
dce234bc 1396 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1397 {
dce234bc
PP
1398 dwarf2_per_objfile->frame.asection = sectp;
1399 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1400 }
dce234bc 1401 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1402 {
3799ccc6 1403 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1404
3799ccc6
EZ
1405 if (aflag & SEC_HAS_CONTENTS)
1406 {
dce234bc
PP
1407 dwarf2_per_objfile->eh_frame.asection = sectp;
1408 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1409 }
b6af0555 1410 }
dce234bc 1411 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1412 {
dce234bc
PP
1413 dwarf2_per_objfile->ranges.asection = sectp;
1414 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1415 }
348e048f
DE
1416 else if (section_is_p (sectp->name, TYPES_SECTION))
1417 {
1418 dwarf2_per_objfile->types.asection = sectp;
1419 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1420 }
9291a0cd
TT
1421 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1422 {
1423 dwarf2_per_objfile->gdb_index.asection = sectp;
1424 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1425 }
dce234bc 1426
72dca2f5
FR
1427 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1428 && bfd_section_vma (abfd, sectp) == 0)
1429 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1430}
1431
dce234bc
PP
1432/* Decompress a section that was compressed using zlib. Store the
1433 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1434
1435static void
dce234bc
PP
1436zlib_decompress_section (struct objfile *objfile, asection *sectp,
1437 gdb_byte **outbuf, bfd_size_type *outsize)
1438{
1439 bfd *abfd = objfile->obfd;
1440#ifndef HAVE_ZLIB_H
1441 error (_("Support for zlib-compressed DWARF data (from '%s') "
1442 "is disabled in this copy of GDB"),
1443 bfd_get_filename (abfd));
1444#else
1445 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1446 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1447 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1448 bfd_size_type uncompressed_size;
1449 gdb_byte *uncompressed_buffer;
1450 z_stream strm;
1451 int rc;
1452 int header_size = 12;
1453
1454 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1455 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1456 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1457 bfd_get_filename (abfd));
1458
1459 /* Read the zlib header. In this case, it should be "ZLIB" followed
1460 by the uncompressed section size, 8 bytes in big-endian order. */
1461 if (compressed_size < header_size
1462 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1463 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1464 bfd_get_filename (abfd));
1465 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[11];
1473
1474 /* It is possible the section consists of several compressed
1475 buffers concatenated together, so we uncompress in a loop. */
1476 strm.zalloc = NULL;
1477 strm.zfree = NULL;
1478 strm.opaque = NULL;
1479 strm.avail_in = compressed_size - header_size;
1480 strm.next_in = (Bytef*) compressed_buffer + header_size;
1481 strm.avail_out = uncompressed_size;
1482 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1483 uncompressed_size);
1484 rc = inflateInit (&strm);
1485 while (strm.avail_in > 0)
1486 {
1487 if (rc != Z_OK)
1488 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1489 bfd_get_filename (abfd), rc);
1490 strm.next_out = ((Bytef*) uncompressed_buffer
1491 + (uncompressed_size - strm.avail_out));
1492 rc = inflate (&strm, Z_FINISH);
1493 if (rc != Z_STREAM_END)
1494 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1495 bfd_get_filename (abfd), rc);
1496 rc = inflateReset (&strm);
1497 }
1498 rc = inflateEnd (&strm);
1499 if (rc != Z_OK
1500 || strm.avail_out != 0)
1501 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1502 bfd_get_filename (abfd), rc);
1503
affddf13 1504 do_cleanups (cleanup);
dce234bc
PP
1505 *outbuf = uncompressed_buffer;
1506 *outsize = uncompressed_size;
1507#endif
233a11ab
CS
1508}
1509
dce234bc
PP
1510/* Read the contents of the section SECTP from object file specified by
1511 OBJFILE, store info about the section into INFO.
1512 If the section is compressed, uncompress it before returning. */
c906108c 1513
dce234bc
PP
1514static void
1515dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1516{
dce234bc
PP
1517 bfd *abfd = objfile->obfd;
1518 asection *sectp = info->asection;
1519 gdb_byte *buf, *retbuf;
1520 unsigned char header[4];
c906108c 1521
be391dca
TT
1522 if (info->readin)
1523 return;
dce234bc
PP
1524 info->buffer = NULL;
1525 info->was_mmapped = 0;
be391dca 1526 info->readin = 1;
188dd5d6 1527
dce234bc
PP
1528 if (info->asection == NULL || info->size == 0)
1529 return;
c906108c 1530
dce234bc
PP
1531 /* Check if the file has a 4-byte header indicating compression. */
1532 if (info->size > sizeof (header)
1533 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1534 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1535 {
1536 /* Upon decompression, update the buffer and its size. */
1537 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1538 {
1539 zlib_decompress_section (objfile, sectp, &info->buffer,
1540 &info->size);
1541 return;
1542 }
1543 }
4bdf3d34 1544
dce234bc
PP
1545#ifdef HAVE_MMAP
1546 if (pagesize == 0)
1547 pagesize = getpagesize ();
2e276125 1548
dce234bc
PP
1549 /* Only try to mmap sections which are large enough: we don't want to
1550 waste space due to fragmentation. Also, only try mmap for sections
1551 without relocations. */
1552
1553 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1554 {
1555 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1556 size_t map_length = info->size + sectp->filepos - pg_offset;
1557 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1558 MAP_PRIVATE, pg_offset);
1559
1560 if (retbuf != MAP_FAILED)
1561 {
1562 info->was_mmapped = 1;
1563 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1564#if HAVE_POSIX_MADVISE
1565 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1566#endif
dce234bc
PP
1567 return;
1568 }
1569 }
1570#endif
1571
1572 /* If we get here, we are a normal, not-compressed section. */
1573 info->buffer = buf
1574 = obstack_alloc (&objfile->objfile_obstack, info->size);
1575
1576 /* When debugging .o files, we may need to apply relocations; see
1577 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1578 We never compress sections in .o files, so we only need to
1579 try this when the section is not compressed. */
ac8035ab 1580 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1581 if (retbuf != NULL)
1582 {
1583 info->buffer = retbuf;
1584 return;
1585 }
1586
1587 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1588 || bfd_bread (buf, info->size, abfd) != info->size)
1589 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1590 bfd_get_filename (abfd));
1591}
1592
1593/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1594 SECTION_NAME. */
af34e669 1595
dce234bc
PP
1596void
1597dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1598 asection **sectp, gdb_byte **bufp,
1599 bfd_size_type *sizep)
1600{
1601 struct dwarf2_per_objfile *data
1602 = objfile_data (objfile, dwarf2_objfile_data_key);
1603 struct dwarf2_section_info *info;
a3b2a86b
TT
1604
1605 /* We may see an objfile without any DWARF, in which case we just
1606 return nothing. */
1607 if (data == NULL)
1608 {
1609 *sectp = NULL;
1610 *bufp = NULL;
1611 *sizep = 0;
1612 return;
1613 }
dce234bc
PP
1614 if (section_is_p (section_name, EH_FRAME_SECTION))
1615 info = &data->eh_frame;
1616 else if (section_is_p (section_name, FRAME_SECTION))
1617 info = &data->frame;
0d53c4c4 1618 else
f3574227 1619 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1620
1621 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1622 /* We haven't read this section in yet. Do it now. */
1623 dwarf2_read_section (objfile, info);
1624
1625 *sectp = info->asection;
1626 *bufp = info->buffer;
1627 *sizep = info->size;
1628}
1629
9291a0cd
TT
1630\f
1631
1632/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1633 this CU came. */
1634static void
1635dw2_do_instantiate_symtab (struct objfile *objfile,
1636 struct dwarf2_per_cu_data *per_cu)
1637{
1638 struct cleanup *back_to;
1639
1640 back_to = make_cleanup (dwarf2_release_queue, NULL);
1641
1642 queue_comp_unit (per_cu, objfile);
1643
1644 if (per_cu->from_debug_types)
1645 read_signatured_type_at_offset (objfile, per_cu->offset);
1646 else
1647 load_full_comp_unit (per_cu, objfile);
1648
1649 process_queue (objfile);
1650
1651 /* Age the cache, releasing compilation units that have not
1652 been used recently. */
1653 age_cached_comp_units ();
1654
1655 do_cleanups (back_to);
1656}
1657
1658/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1659 the objfile from which this CU came. Returns the resulting symbol
1660 table. */
1661static struct symtab *
1662dw2_instantiate_symtab (struct objfile *objfile,
1663 struct dwarf2_per_cu_data *per_cu)
1664{
1665 if (!per_cu->v.quick->symtab)
1666 {
1667 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1668 increment_reading_symtab ();
1669 dw2_do_instantiate_symtab (objfile, per_cu);
1670 do_cleanups (back_to);
1671 }
1672 return per_cu->v.quick->symtab;
1673}
1674
1fd400ff
TT
1675/* Return the CU given its index. */
1676static struct dwarf2_per_cu_data *
1677dw2_get_cu (int index)
1678{
1679 if (index >= dwarf2_per_objfile->n_comp_units)
1680 {
1681 index -= dwarf2_per_objfile->n_comp_units;
1682 return dwarf2_per_objfile->type_comp_units[index];
1683 }
1684 return dwarf2_per_objfile->all_comp_units[index];
1685}
1686
9291a0cd
TT
1687/* A helper function that knows how to read a 64-bit value in a way
1688 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1689 otherwise. */
1690static int
1691extract_cu_value (const char *bytes, ULONGEST *result)
1692{
1693 if (sizeof (ULONGEST) < 8)
1694 {
1695 int i;
1696
1697 /* Ignore the upper 4 bytes if they are all zero. */
1698 for (i = 0; i < 4; ++i)
1699 if (bytes[i + 4] != 0)
1700 return 0;
1701
1702 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1703 }
1704 else
1705 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1706 return 1;
1707}
1708
1709/* Read the CU list from the mapped index, and use it to create all
1710 the CU objects for this objfile. Return 0 if something went wrong,
1711 1 if everything went ok. */
1712static int
1fd400ff
TT
1713create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1714 offset_type cu_list_elements)
9291a0cd
TT
1715{
1716 offset_type i;
9291a0cd
TT
1717
1718 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1719 dwarf2_per_objfile->all_comp_units
1720 = obstack_alloc (&objfile->objfile_obstack,
1721 dwarf2_per_objfile->n_comp_units
1722 * sizeof (struct dwarf2_per_cu_data *));
1723
1724 for (i = 0; i < cu_list_elements; i += 2)
1725 {
1726 struct dwarf2_per_cu_data *the_cu;
1727 ULONGEST offset, length;
1728
1729 if (!extract_cu_value (cu_list, &offset)
1730 || !extract_cu_value (cu_list + 8, &length))
1731 return 0;
1732 cu_list += 2 * 8;
1733
1734 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1735 struct dwarf2_per_cu_data);
1736 the_cu->offset = offset;
1737 the_cu->length = length;
1738 the_cu->objfile = objfile;
1739 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1740 struct dwarf2_per_cu_quick_data);
1741 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1742 }
1743
1744 return 1;
1745}
1746
1fd400ff 1747/* Create the signatured type hash table from the index. */
673bfd45 1748
1fd400ff 1749static int
673bfd45
DE
1750create_signatured_type_table_from_index (struct objfile *objfile,
1751 const gdb_byte *bytes,
1752 offset_type elements)
1fd400ff
TT
1753{
1754 offset_type i;
673bfd45 1755 htab_t sig_types_hash;
1fd400ff
TT
1756
1757 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1758 dwarf2_per_objfile->type_comp_units
1759 = obstack_alloc (&objfile->objfile_obstack,
1760 dwarf2_per_objfile->n_type_comp_units
1761 * sizeof (struct dwarf2_per_cu_data *));
1762
673bfd45 1763 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1764
1765 for (i = 0; i < elements; i += 3)
1766 {
1767 struct signatured_type *type_sig;
1768 ULONGEST offset, type_offset, signature;
1769 void **slot;
1770
1771 if (!extract_cu_value (bytes, &offset)
1772 || !extract_cu_value (bytes + 8, &type_offset))
1773 return 0;
1774 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1775 bytes += 3 * 8;
1776
1777 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1778 struct signatured_type);
1779 type_sig->signature = signature;
1780 type_sig->offset = offset;
1781 type_sig->type_offset = type_offset;
1782 type_sig->per_cu.from_debug_types = 1;
1783 type_sig->per_cu.offset = offset;
1784 type_sig->per_cu.objfile = objfile;
1785 type_sig->per_cu.v.quick
1786 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1787 struct dwarf2_per_cu_quick_data);
1788
673bfd45 1789 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1790 *slot = type_sig;
1791
1792 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1793 }
1794
673bfd45 1795 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1796
1797 return 1;
1798}
1799
9291a0cd
TT
1800/* Read the address map data from the mapped index, and use it to
1801 populate the objfile's psymtabs_addrmap. */
1802static void
1803create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1804{
1805 const gdb_byte *iter, *end;
1806 struct obstack temp_obstack;
1807 struct addrmap *mutable_map;
1808 struct cleanup *cleanup;
1809 CORE_ADDR baseaddr;
1810
1811 obstack_init (&temp_obstack);
1812 cleanup = make_cleanup_obstack_free (&temp_obstack);
1813 mutable_map = addrmap_create_mutable (&temp_obstack);
1814
1815 iter = index->address_table;
1816 end = iter + index->address_table_size;
1817
1818 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1819
1820 while (iter < end)
1821 {
1822 ULONGEST hi, lo, cu_index;
1823 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1824 iter += 8;
1825 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1826 iter += 8;
1827 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1828 iter += 4;
1829
1830 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1831 dw2_get_cu (cu_index));
9291a0cd
TT
1832 }
1833
1834 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1835 &objfile->objfile_obstack);
1836 do_cleanups (cleanup);
1837}
1838
1839/* The hash function for strings in the mapped index. This is the
1840 same as the hashtab.c hash function, but we keep a separate copy to
1841 maintain control over the implementation. This is necessary
1842 because the hash function is tied to the format of the mapped index
1843 file. */
1844static hashval_t
1845mapped_index_string_hash (const void *p)
1846{
1847 const unsigned char *str = (const unsigned char *) p;
1848 hashval_t r = 0;
1849 unsigned char c;
1850
1851 while ((c = *str++) != 0)
1852 r = r * 67 + c - 113;
1853
1854 return r;
1855}
1856
1857/* Find a slot in the mapped index INDEX for the object named NAME.
1858 If NAME is found, set *VEC_OUT to point to the CU vector in the
1859 constant pool and return 1. If NAME cannot be found, return 0. */
1860static int
1861find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1862 offset_type **vec_out)
1863{
1864 offset_type hash = mapped_index_string_hash (name);
1865 offset_type slot, step;
1866
1867 slot = hash & (index->index_table_slots - 1);
1868 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1869
1870 for (;;)
1871 {
1872 /* Convert a slot number to an offset into the table. */
1873 offset_type i = 2 * slot;
1874 const char *str;
1875 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1876 return 0;
1877
1878 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1879 if (!strcmp (name, str))
1880 {
1881 *vec_out = (offset_type *) (index->constant_pool
1882 + MAYBE_SWAP (index->index_table[i + 1]));
1883 return 1;
1884 }
1885
1886 slot = (slot + step) & (index->index_table_slots - 1);
1887 }
1888}
1889
1890/* Read the index file. If everything went ok, initialize the "quick"
1891 elements of all the CUs and return 1. Otherwise, return 0. */
1892static int
1893dwarf2_read_index (struct objfile *objfile)
1894{
9291a0cd
TT
1895 char *addr;
1896 struct mapped_index *map;
b3b272e1 1897 offset_type *metadata;
ac0b195c
KW
1898 const gdb_byte *cu_list;
1899 const gdb_byte *types_list = NULL;
1900 offset_type version, cu_list_elements;
1901 offset_type types_list_elements = 0;
1fd400ff 1902 int i;
9291a0cd
TT
1903
1904 if (dwarf2_per_objfile->gdb_index.asection == NULL
1905 || dwarf2_per_objfile->gdb_index.size == 0)
1906 return 0;
1907 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1908
1909 addr = dwarf2_per_objfile->gdb_index.buffer;
1910 /* Version check. */
1fd400ff
TT
1911 version = MAYBE_SWAP (*(offset_type *) addr);
1912 if (version == 1)
1913 {
1914 /* Index version 1 neglected to account for .debug_types. So,
1915 if we see .debug_types, we cannot use this index. */
1916 if (dwarf2_per_objfile->types.asection != NULL
1917 && dwarf2_per_objfile->types.size != 0)
1918 return 0;
1919 }
1920 else if (version != 2)
9291a0cd
TT
1921 return 0;
1922
1923 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1924 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1925
1926 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1927
1928 i = 0;
1929 cu_list = addr + MAYBE_SWAP (metadata[i]);
1930 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1931 / 8);
1fd400ff
TT
1932 ++i;
1933
1934 if (version == 2)
1935 {
1936 types_list = addr + MAYBE_SWAP (metadata[i]);
1937 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1938 - MAYBE_SWAP (metadata[i]))
1939 / 8);
1940 ++i;
1941 }
1942
1943 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1944 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1945 - MAYBE_SWAP (metadata[i]));
1946 ++i;
1947
1948 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1949 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1950 - MAYBE_SWAP (metadata[i]))
9291a0cd 1951 / (2 * sizeof (offset_type)));
1fd400ff 1952 ++i;
9291a0cd 1953
1fd400ff
TT
1954 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1955
1956 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1957 return 0;
1958
1959 if (version == 2
1960 && types_list_elements
673bfd45
DE
1961 && !create_signatured_type_table_from_index (objfile, types_list,
1962 types_list_elements))
9291a0cd
TT
1963 return 0;
1964
1965 create_addrmap_from_index (objfile, map);
1966
1967 dwarf2_per_objfile->index_table = map;
1968 dwarf2_per_objfile->using_index = 1;
1969
1970 return 1;
1971}
1972
1973/* A helper for the "quick" functions which sets the global
1974 dwarf2_per_objfile according to OBJFILE. */
1975static void
1976dw2_setup (struct objfile *objfile)
1977{
1978 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1979 gdb_assert (dwarf2_per_objfile);
1980}
1981
1982/* A helper for the "quick" functions which attempts to read the line
1983 table for THIS_CU. */
1984static void
1985dw2_require_line_header (struct objfile *objfile,
1986 struct dwarf2_per_cu_data *this_cu)
1987{
1988 bfd *abfd = objfile->obfd;
1989 struct line_header *lh = NULL;
1990 struct attribute *attr;
1991 struct cleanup *cleanups;
1992 struct die_info *comp_unit_die;
1993 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1994 int has_children, i;
1995 struct dwarf2_cu cu;
1996 unsigned int bytes_read, buffer_size;
1997 struct die_reader_specs reader_specs;
1998 char *name, *comp_dir;
1999
2000 if (this_cu->v.quick->read_lines)
2001 return;
2002 this_cu->v.quick->read_lines = 1;
2003
2004 memset (&cu, 0, sizeof (cu));
2005 cu.objfile = objfile;
2006 obstack_init (&cu.comp_unit_obstack);
2007
2008 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2009
2010 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2011 buffer_size = dwarf2_per_objfile->info.size;
2012 buffer = dwarf2_per_objfile->info.buffer;
2013 info_ptr = buffer + this_cu->offset;
2014 beg_of_comp_unit = info_ptr;
2015
2016 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2017 buffer, buffer_size,
2018 abfd);
2019
2020 /* Complete the cu_header. */
2021 cu.header.offset = beg_of_comp_unit - buffer;
2022 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2023
2024 this_cu->cu = &cu;
2025 cu.per_cu = this_cu;
2026
2027 dwarf2_read_abbrevs (abfd, &cu);
2028 make_cleanup (dwarf2_free_abbrev_table, &cu);
2029
2030 if (this_cu->from_debug_types)
2031 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2032 init_cu_die_reader (&reader_specs, &cu);
2033 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2034 &has_children);
2035
2036 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2037 if (attr)
2038 {
2039 unsigned int line_offset = DW_UNSND (attr);
2040 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2041 }
2042 if (lh == NULL)
2043 {
2044 do_cleanups (cleanups);
2045 return;
2046 }
2047
2048 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2049
2050 this_cu->v.quick->lines = lh;
2051
2052 this_cu->v.quick->file_names
2053 = obstack_alloc (&objfile->objfile_obstack,
2054 lh->num_file_names * sizeof (char *));
2055 for (i = 0; i < lh->num_file_names; ++i)
2056 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2057
2058 do_cleanups (cleanups);
2059}
2060
2061/* A helper for the "quick" functions which computes and caches the
2062 real path for a given file name from the line table.
2063 dw2_require_line_header must have been called before this is
2064 invoked. */
2065static const char *
2066dw2_require_full_path (struct objfile *objfile,
2067 struct dwarf2_per_cu_data *cu,
2068 int index)
2069{
2070 if (!cu->v.quick->full_names)
2071 cu->v.quick->full_names
2072 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2073 cu->v.quick->lines->num_file_names,
2074 sizeof (char *));
2075
2076 if (!cu->v.quick->full_names[index])
2077 cu->v.quick->full_names[index]
2078 = gdb_realpath (cu->v.quick->file_names[index]);
2079
2080 return cu->v.quick->full_names[index];
2081}
2082
2083static struct symtab *
2084dw2_find_last_source_symtab (struct objfile *objfile)
2085{
2086 int index;
2087 dw2_setup (objfile);
2088 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2089 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2090}
2091
2092static void
2093dw2_forget_cached_source_info (struct objfile *objfile)
2094{
2095 int i;
2096
2097 dw2_setup (objfile);
1fd400ff
TT
2098 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2099 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2100 {
1fd400ff 2101 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2102
2103 if (cu->v.quick->full_names)
2104 {
2105 int j;
2106
2107 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2108 xfree ((void *) cu->v.quick->full_names[j]);
2109 }
2110 }
2111}
2112
2113static int
2114dw2_lookup_symtab (struct objfile *objfile, const char *name,
2115 const char *full_path, const char *real_path,
2116 struct symtab **result)
2117{
2118 int i;
2119 int check_basename = lbasename (name) == name;
2120 struct dwarf2_per_cu_data *base_cu = NULL;
2121
2122 dw2_setup (objfile);
1fd400ff
TT
2123 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2124 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2125 {
2126 int j;
1fd400ff 2127 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2128
2129 if (cu->v.quick->symtab)
2130 continue;
2131
2132 dw2_require_line_header (objfile, cu);
2133 if (!cu->v.quick->lines)
2134 continue;
2135
2136 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2137 {
2138 const char *this_name = cu->v.quick->file_names[j];
2139
2140 if (FILENAME_CMP (name, this_name) == 0)
2141 {
2142 *result = dw2_instantiate_symtab (objfile, cu);
2143 return 1;
2144 }
2145
2146 if (check_basename && ! base_cu
2147 && FILENAME_CMP (lbasename (this_name), name) == 0)
2148 base_cu = cu;
2149
2150 if (full_path != NULL)
2151 {
2152 const char *this_full_name = dw2_require_full_path (objfile,
2153 cu, j);
2154
2155 if (this_full_name
2156 && FILENAME_CMP (full_path, this_full_name) == 0)
2157 {
2158 *result = dw2_instantiate_symtab (objfile, cu);
2159 return 1;
2160 }
2161 }
2162
2163 if (real_path != NULL)
2164 {
2165 const char *this_full_name = dw2_require_full_path (objfile,
2166 cu, j);
2167
2168 if (this_full_name != NULL)
2169 {
2170 char *rp = gdb_realpath (this_full_name);
2171 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2172 {
2173 xfree (rp);
2174 *result = dw2_instantiate_symtab (objfile, cu);
2175 return 1;
2176 }
2177 xfree (rp);
2178 }
2179 }
2180 }
2181 }
2182
2183 if (base_cu)
2184 {
2185 *result = dw2_instantiate_symtab (objfile, base_cu);
2186 return 1;
2187 }
2188
2189 return 0;
2190}
2191
2192static struct symtab *
2193dw2_lookup_symbol (struct objfile *objfile, int block_index,
2194 const char *name, domain_enum domain)
2195{
2196 /* We do all the work in the pre_expand_symtabs_matching hook
2197 instead. */
2198 return NULL;
2199}
2200
2201/* A helper function that expands all symtabs that hold an object
2202 named NAME. */
2203static void
2204dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2205{
2206 dw2_setup (objfile);
2207
2208 if (dwarf2_per_objfile->index_table)
2209 {
2210 offset_type *vec;
2211
2212 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2213 name, &vec))
2214 {
2215 offset_type i, len = MAYBE_SWAP (*vec);
2216 for (i = 0; i < len; ++i)
2217 {
2218 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
1fd400ff
TT
2219 struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2220
9291a0cd
TT
2221 dw2_instantiate_symtab (objfile, cu);
2222 }
2223 }
2224 }
2225}
2226
2227static void
2228dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2229 int kind, const char *name,
2230 domain_enum domain)
2231{
2232 dw2_do_expand_symtabs_matching (objfile, name);
2233}
2234
2235static void
2236dw2_print_stats (struct objfile *objfile)
2237{
2238 int i, count;
2239
2240 dw2_setup (objfile);
2241 count = 0;
1fd400ff
TT
2242 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2243 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2244 {
1fd400ff 2245 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2246
2247 if (!cu->v.quick->symtab)
2248 ++count;
2249 }
2250 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2251}
2252
2253static void
2254dw2_dump (struct objfile *objfile)
2255{
2256 /* Nothing worth printing. */
2257}
2258
2259static void
2260dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2261 struct section_offsets *delta)
2262{
2263 /* There's nothing to relocate here. */
2264}
2265
2266static void
2267dw2_expand_symtabs_for_function (struct objfile *objfile,
2268 const char *func_name)
2269{
2270 dw2_do_expand_symtabs_matching (objfile, func_name);
2271}
2272
2273static void
2274dw2_expand_all_symtabs (struct objfile *objfile)
2275{
2276 int i;
2277
2278 dw2_setup (objfile);
1fd400ff
TT
2279
2280 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2281 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2282 {
1fd400ff 2283 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2284
2285 dw2_instantiate_symtab (objfile, cu);
2286 }
2287}
2288
2289static void
2290dw2_expand_symtabs_with_filename (struct objfile *objfile,
2291 const char *filename)
2292{
2293 int i;
2294
2295 dw2_setup (objfile);
1fd400ff
TT
2296 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2297 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2298 {
2299 int j;
1fd400ff 2300 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2301
2302 if (cu->v.quick->symtab)
2303 continue;
2304
2305 dw2_require_line_header (objfile, cu);
2306 if (!cu->v.quick->lines)
2307 continue;
2308
2309 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2310 {
2311 const char *this_name = cu->v.quick->file_names[j];
2312 if (strcmp (this_name, filename) == 0)
2313 {
2314 dw2_instantiate_symtab (objfile, cu);
2315 break;
2316 }
2317 }
2318 }
2319}
2320
dd786858 2321static const char *
9291a0cd
TT
2322dw2_find_symbol_file (struct objfile *objfile, const char *name)
2323{
2324 struct dwarf2_per_cu_data *cu;
2325 offset_type *vec;
2326
2327 dw2_setup (objfile);
2328
2329 if (!dwarf2_per_objfile->index_table)
2330 return NULL;
2331
2332 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2333 name, &vec))
2334 return NULL;
2335
2336 /* Note that this just looks at the very first one named NAME -- but
2337 actually we are looking for a function. find_main_filename
2338 should be rewritten so that it doesn't require a custom hook. It
2339 could just use the ordinary symbol tables. */
2340 /* vec[0] is the length, which must always be >0. */
1fd400ff 2341 cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd
TT
2342
2343 dw2_require_line_header (objfile, cu);
2344 if (!cu->v.quick->lines)
2345 return NULL;
2346
dd786858 2347 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2348}
2349
2350static void
2351dw2_map_ada_symtabs (struct objfile *objfile,
2352 int (*wild_match) (const char *, int, const char *),
2353 int (*is_name_suffix) (const char *),
2354 void (*callback) (struct objfile *,
2355 struct symtab *, void *),
2356 const char *name, int global,
2357 domain_enum namespace, int wild,
2358 void *data)
2359{
2360 /* For now, we don't support Ada, so this function can't be
2361 reached. */
2362 internal_error (__FILE__, __LINE__,
2363 _("map_ada_symtabs called via index method"));
2364}
2365
2366static void
2367dw2_expand_symtabs_matching (struct objfile *objfile,
2368 int (*file_matcher) (const char *, void *),
2369 int (*name_matcher) (const char *, void *),
2370 domain_enum kind,
2371 void *data)
2372{
2373 int i;
2374 offset_type iter;
2375
2376 dw2_setup (objfile);
2377 if (!dwarf2_per_objfile->index_table)
2378 return;
2379
1fd400ff
TT
2380 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2381 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2382 {
2383 int j;
1fd400ff 2384 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2385
2386 cu->v.quick->mark = 0;
2387 if (cu->v.quick->symtab)
2388 continue;
2389
2390 dw2_require_line_header (objfile, cu);
2391 if (!cu->v.quick->lines)
2392 continue;
2393
2394 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2395 {
2396 if (file_matcher (cu->v.quick->file_names[j], data))
2397 {
2398 cu->v.quick->mark = 1;
2399 break;
2400 }
2401 }
2402 }
2403
2404 for (iter = 0;
2405 iter < dwarf2_per_objfile->index_table->index_table_slots;
2406 ++iter)
2407 {
2408 offset_type idx = 2 * iter;
2409 const char *name;
2410 offset_type *vec, vec_len, vec_idx;
2411
2412 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2413 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2414 continue;
2415
2416 name = (dwarf2_per_objfile->index_table->constant_pool
2417 + dwarf2_per_objfile->index_table->index_table[idx]);
2418
2419 if (! (*name_matcher) (name, data))
2420 continue;
2421
2422 /* The name was matched, now expand corresponding CUs that were
2423 marked. */
2424 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2425 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2426 vec_len = MAYBE_SWAP (vec[0]);
2427 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2428 {
1fd400ff
TT
2429 struct dwarf2_per_cu_data *cu;
2430
2431 cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
9291a0cd
TT
2432 if (cu->v.quick->mark)
2433 dw2_instantiate_symtab (objfile, cu);
2434 }
2435 }
2436}
2437
2438static struct symtab *
2439dw2_find_pc_sect_symtab (struct objfile *objfile,
2440 struct minimal_symbol *msymbol,
2441 CORE_ADDR pc,
2442 struct obj_section *section,
2443 int warn_if_readin)
2444{
2445 struct dwarf2_per_cu_data *data;
2446
2447 dw2_setup (objfile);
2448
2449 if (!objfile->psymtabs_addrmap)
2450 return NULL;
2451
2452 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2453 if (!data)
2454 return NULL;
2455
2456 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2457 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2458 paddress (get_objfile_arch (objfile), pc));
2459
2460 return dw2_instantiate_symtab (objfile, data);
2461}
2462
2463static void
2464dw2_map_symbol_names (struct objfile *objfile,
2465 void (*fun) (const char *, void *),
2466 void *data)
2467{
2468 offset_type iter;
2469 dw2_setup (objfile);
2470
2471 if (!dwarf2_per_objfile->index_table)
2472 return;
2473
2474 for (iter = 0;
2475 iter < dwarf2_per_objfile->index_table->index_table_slots;
2476 ++iter)
2477 {
2478 offset_type idx = 2 * iter;
2479 const char *name;
2480 offset_type *vec, vec_len, vec_idx;
2481
2482 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2483 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2484 continue;
2485
2486 name = (dwarf2_per_objfile->index_table->constant_pool
2487 + dwarf2_per_objfile->index_table->index_table[idx]);
2488
2489 (*fun) (name, data);
2490 }
2491}
2492
2493static void
2494dw2_map_symbol_filenames (struct objfile *objfile,
2495 void (*fun) (const char *, const char *, void *),
2496 void *data)
2497{
2498 int i;
2499
2500 dw2_setup (objfile);
1fd400ff
TT
2501 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2502 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2503 {
2504 int j;
1fd400ff 2505 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2506
2507 if (cu->v.quick->symtab)
2508 continue;
2509
2510 dw2_require_line_header (objfile, cu);
2511 if (!cu->v.quick->lines)
2512 continue;
2513
2514 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2515 {
2516 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2517 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2518 }
2519 }
2520}
2521
2522static int
2523dw2_has_symbols (struct objfile *objfile)
2524{
2525 return 1;
2526}
2527
2528const struct quick_symbol_functions dwarf2_gdb_index_functions =
2529{
2530 dw2_has_symbols,
2531 dw2_find_last_source_symtab,
2532 dw2_forget_cached_source_info,
2533 dw2_lookup_symtab,
2534 dw2_lookup_symbol,
2535 dw2_pre_expand_symtabs_matching,
2536 dw2_print_stats,
2537 dw2_dump,
2538 dw2_relocate,
2539 dw2_expand_symtabs_for_function,
2540 dw2_expand_all_symtabs,
2541 dw2_expand_symtabs_with_filename,
2542 dw2_find_symbol_file,
2543 dw2_map_ada_symtabs,
2544 dw2_expand_symtabs_matching,
2545 dw2_find_pc_sect_symtab,
2546 dw2_map_symbol_names,
2547 dw2_map_symbol_filenames
2548};
2549
2550/* Initialize for reading DWARF for this objfile. Return 0 if this
2551 file will use psymtabs, or 1 if using the GNU index. */
2552
2553int
2554dwarf2_initialize_objfile (struct objfile *objfile)
2555{
2556 /* If we're about to read full symbols, don't bother with the
2557 indices. In this case we also don't care if some other debug
2558 format is making psymtabs, because they are all about to be
2559 expanded anyway. */
2560 if ((objfile->flags & OBJF_READNOW))
2561 {
2562 int i;
2563
2564 dwarf2_per_objfile->using_index = 1;
2565 create_all_comp_units (objfile);
1fd400ff 2566 create_debug_types_hash_table (objfile);
9291a0cd 2567
1fd400ff
TT
2568 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2569 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2570 {
1fd400ff 2571 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2572
2573 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2574 struct dwarf2_per_cu_quick_data);
2575 }
2576
2577 /* Return 1 so that gdb sees the "quick" functions. However,
2578 these functions will be no-ops because we will have expanded
2579 all symtabs. */
2580 return 1;
2581 }
2582
2583 if (dwarf2_read_index (objfile))
2584 return 1;
2585
2586 dwarf2_build_psymtabs (objfile);
2587 return 0;
2588}
2589
2590\f
2591
dce234bc
PP
2592/* Build a partial symbol table. */
2593
2594void
f29dff0a 2595dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2596{
f29dff0a 2597 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2598 {
2599 init_psymbol_list (objfile, 1024);
2600 }
2601
d146bf1e 2602 dwarf2_build_psymtabs_hard (objfile);
c906108c 2603}
c906108c 2604
45452591
DE
2605/* Return TRUE if OFFSET is within CU_HEADER. */
2606
2607static inline int
2608offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2609{
2610 unsigned int bottom = cu_header->offset;
2611 unsigned int top = (cu_header->offset
2612 + cu_header->length
2613 + cu_header->initial_length_size);
9a619af0 2614
45452591
DE
2615 return (offset >= bottom && offset < top);
2616}
2617
93311388
DE
2618/* Read in the comp unit header information from the debug_info at info_ptr.
2619 NOTE: This leaves members offset, first_die_offset to be filled in
2620 by the caller. */
107d2387 2621
fe1b8b76 2622static gdb_byte *
107d2387 2623read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2624 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2625{
2626 int signed_addr;
891d2f0b 2627 unsigned int bytes_read;
c764a876
DE
2628
2629 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2630 cu_header->initial_length_size = bytes_read;
2631 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2632 info_ptr += bytes_read;
107d2387
AC
2633 cu_header->version = read_2_bytes (abfd, info_ptr);
2634 info_ptr += 2;
613e1657 2635 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2636 &bytes_read);
613e1657 2637 info_ptr += bytes_read;
107d2387
AC
2638 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2639 info_ptr += 1;
2640 signed_addr = bfd_get_sign_extend_vma (abfd);
2641 if (signed_addr < 0)
8e65ff28 2642 internal_error (__FILE__, __LINE__,
e2e0b3e5 2643 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2644 cu_header->signed_addr_p = signed_addr;
c764a876 2645
107d2387
AC
2646 return info_ptr;
2647}
2648
fe1b8b76
JB
2649static gdb_byte *
2650partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2651 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2652 bfd *abfd)
2653{
fe1b8b76 2654 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2655
2656 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2657
2dc7f7b3 2658 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2659 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2660 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2661 bfd_get_filename (abfd));
72bf9492 2662
dce234bc 2663 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2664 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2665 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2666 (long) header->abbrev_offset,
93311388 2667 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2668 bfd_get_filename (abfd));
2669
2670 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2671 > buffer + buffer_size)
8a3fe4f8
AC
2672 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2673 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2674 (long) header->length,
93311388 2675 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2676 bfd_get_filename (abfd));
2677
2678 return info_ptr;
2679}
2680
348e048f
DE
2681/* Read in the types comp unit header information from .debug_types entry at
2682 types_ptr. The result is a pointer to one past the end of the header. */
2683
2684static gdb_byte *
2685read_type_comp_unit_head (struct comp_unit_head *cu_header,
2686 ULONGEST *signature,
2687 gdb_byte *types_ptr, bfd *abfd)
2688{
348e048f
DE
2689 gdb_byte *initial_types_ptr = types_ptr;
2690
6e70227d 2691 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2692 &dwarf2_per_objfile->types);
348e048f
DE
2693 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2694
2695 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2696
2697 *signature = read_8_bytes (abfd, types_ptr);
2698 types_ptr += 8;
2699 types_ptr += cu_header->offset_size;
2700 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2701
2702 return types_ptr;
2703}
2704
aaa75496
JB
2705/* Allocate a new partial symtab for file named NAME and mark this new
2706 partial symtab as being an include of PST. */
2707
2708static void
2709dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2710 struct objfile *objfile)
2711{
2712 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2713
2714 subpst->section_offsets = pst->section_offsets;
2715 subpst->textlow = 0;
2716 subpst->texthigh = 0;
2717
2718 subpst->dependencies = (struct partial_symtab **)
2719 obstack_alloc (&objfile->objfile_obstack,
2720 sizeof (struct partial_symtab *));
2721 subpst->dependencies[0] = pst;
2722 subpst->number_of_dependencies = 1;
2723
2724 subpst->globals_offset = 0;
2725 subpst->n_global_syms = 0;
2726 subpst->statics_offset = 0;
2727 subpst->n_static_syms = 0;
2728 subpst->symtab = NULL;
2729 subpst->read_symtab = pst->read_symtab;
2730 subpst->readin = 0;
2731
2732 /* No private part is necessary for include psymtabs. This property
2733 can be used to differentiate between such include psymtabs and
10b3939b 2734 the regular ones. */
58a9656e 2735 subpst->read_symtab_private = NULL;
aaa75496
JB
2736}
2737
2738/* Read the Line Number Program data and extract the list of files
2739 included by the source file represented by PST. Build an include
d85a05f0 2740 partial symtab for each of these included files. */
aaa75496
JB
2741
2742static void
2743dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2744 struct die_info *die,
aaa75496
JB
2745 struct partial_symtab *pst)
2746{
2747 struct objfile *objfile = cu->objfile;
2748 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2749 struct line_header *lh = NULL;
2750 struct attribute *attr;
aaa75496 2751
d85a05f0
DJ
2752 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2753 if (attr)
2754 {
2755 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2756
d85a05f0
DJ
2757 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2758 }
aaa75496
JB
2759 if (lh == NULL)
2760 return; /* No linetable, so no includes. */
2761
c6da4cef
DE
2762 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2763 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2764
2765 free_line_header (lh);
2766}
2767
348e048f
DE
2768static hashval_t
2769hash_type_signature (const void *item)
2770{
2771 const struct signatured_type *type_sig = item;
9a619af0 2772
348e048f
DE
2773 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2774 return type_sig->signature;
2775}
2776
2777static int
2778eq_type_signature (const void *item_lhs, const void *item_rhs)
2779{
2780 const struct signatured_type *lhs = item_lhs;
2781 const struct signatured_type *rhs = item_rhs;
9a619af0 2782
348e048f
DE
2783 return lhs->signature == rhs->signature;
2784}
2785
1fd400ff
TT
2786/* Allocate a hash table for signatured types. */
2787
2788static htab_t
673bfd45 2789allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2790{
2791 return htab_create_alloc_ex (41,
2792 hash_type_signature,
2793 eq_type_signature,
2794 NULL,
2795 &objfile->objfile_obstack,
2796 hashtab_obstack_allocate,
2797 dummy_obstack_deallocate);
2798}
2799
2800/* A helper function to add a signatured type CU to a list. */
2801
2802static int
2803add_signatured_type_cu_to_list (void **slot, void *datum)
2804{
2805 struct signatured_type *sigt = *slot;
2806 struct dwarf2_per_cu_data ***datap = datum;
2807
2808 **datap = &sigt->per_cu;
2809 ++*datap;
2810
2811 return 1;
2812}
2813
348e048f
DE
2814/* Create the hash table of all entries in the .debug_types section.
2815 The result is zero if there is an error (e.g. missing .debug_types section),
2816 otherwise non-zero. */
2817
2818static int
2819create_debug_types_hash_table (struct objfile *objfile)
2820{
be391dca 2821 gdb_byte *info_ptr;
348e048f 2822 htab_t types_htab;
1fd400ff 2823 struct dwarf2_per_cu_data **iter;
348e048f 2824
be391dca
TT
2825 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2826 info_ptr = dwarf2_per_objfile->types.buffer;
2827
348e048f
DE
2828 if (info_ptr == NULL)
2829 {
2830 dwarf2_per_objfile->signatured_types = NULL;
2831 return 0;
2832 }
2833
673bfd45 2834 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2835
2836 if (dwarf2_die_debug)
2837 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2838
2839 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2840 {
2841 unsigned int offset;
2842 unsigned int offset_size;
2843 unsigned int type_offset;
2844 unsigned int length, initial_length_size;
2845 unsigned short version;
2846 ULONGEST signature;
2847 struct signatured_type *type_sig;
2848 void **slot;
2849 gdb_byte *ptr = info_ptr;
2850
2851 offset = ptr - dwarf2_per_objfile->types.buffer;
2852
2853 /* We need to read the type's signature in order to build the hash
2854 table, but we don't need to read anything else just yet. */
2855
2856 /* Sanity check to ensure entire cu is present. */
2857 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2858 if (ptr + length + initial_length_size
2859 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2860 {
2861 complaint (&symfile_complaints,
2862 _("debug type entry runs off end of `.debug_types' section, ignored"));
2863 break;
2864 }
2865
2866 offset_size = initial_length_size == 4 ? 4 : 8;
2867 ptr += initial_length_size;
2868 version = bfd_get_16 (objfile->obfd, ptr);
2869 ptr += 2;
2870 ptr += offset_size; /* abbrev offset */
2871 ptr += 1; /* address size */
2872 signature = bfd_get_64 (objfile->obfd, ptr);
2873 ptr += 8;
2874 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2875
2876 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2877 memset (type_sig, 0, sizeof (*type_sig));
2878 type_sig->signature = signature;
2879 type_sig->offset = offset;
2880 type_sig->type_offset = type_offset;
ca1f3406 2881 type_sig->per_cu.objfile = objfile;
1fd400ff 2882 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2883
2884 slot = htab_find_slot (types_htab, type_sig, INSERT);
2885 gdb_assert (slot != NULL);
2886 *slot = type_sig;
2887
2888 if (dwarf2_die_debug)
2889 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2890 offset, phex (signature, sizeof (signature)));
2891
2892 info_ptr = info_ptr + initial_length_size + length;
2893 }
2894
2895 dwarf2_per_objfile->signatured_types = types_htab;
2896
1fd400ff
TT
2897 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2898 dwarf2_per_objfile->type_comp_units
2899 = obstack_alloc (&objfile->objfile_obstack,
2900 dwarf2_per_objfile->n_type_comp_units
2901 * sizeof (struct dwarf2_per_cu_data *));
2902 iter = &dwarf2_per_objfile->type_comp_units[0];
2903 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2904 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2905 == dwarf2_per_objfile->n_type_comp_units);
2906
348e048f
DE
2907 return 1;
2908}
2909
2910/* Lookup a signature based type.
2911 Returns NULL if SIG is not present in the table. */
2912
2913static struct signatured_type *
2914lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2915{
2916 struct signatured_type find_entry, *entry;
2917
2918 if (dwarf2_per_objfile->signatured_types == NULL)
2919 {
2920 complaint (&symfile_complaints,
2921 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2922 return 0;
2923 }
2924
2925 find_entry.signature = sig;
2926 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2927 return entry;
2928}
2929
d85a05f0
DJ
2930/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2931
2932static void
2933init_cu_die_reader (struct die_reader_specs *reader,
2934 struct dwarf2_cu *cu)
2935{
2936 reader->abfd = cu->objfile->obfd;
2937 reader->cu = cu;
2938 if (cu->per_cu->from_debug_types)
be391dca
TT
2939 {
2940 gdb_assert (dwarf2_per_objfile->types.readin);
2941 reader->buffer = dwarf2_per_objfile->types.buffer;
2942 }
d85a05f0 2943 else
be391dca
TT
2944 {
2945 gdb_assert (dwarf2_per_objfile->info.readin);
2946 reader->buffer = dwarf2_per_objfile->info.buffer;
2947 }
d85a05f0
DJ
2948}
2949
2950/* Find the base address of the compilation unit for range lists and
2951 location lists. It will normally be specified by DW_AT_low_pc.
2952 In DWARF-3 draft 4, the base address could be overridden by
2953 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2954 compilation units with discontinuous ranges. */
2955
2956static void
2957dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2958{
2959 struct attribute *attr;
2960
2961 cu->base_known = 0;
2962 cu->base_address = 0;
2963
2964 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2965 if (attr)
2966 {
2967 cu->base_address = DW_ADDR (attr);
2968 cu->base_known = 1;
2969 }
2970 else
2971 {
2972 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2973 if (attr)
2974 {
2975 cu->base_address = DW_ADDR (attr);
2976 cu->base_known = 1;
2977 }
2978 }
2979}
2980
348e048f
DE
2981/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2982 to combine the common parts.
93311388 2983 Process a compilation unit for a psymtab.
348e048f
DE
2984 BUFFER is a pointer to the beginning of the dwarf section buffer,
2985 either .debug_info or debug_types.
93311388
DE
2986 INFO_PTR is a pointer to the start of the CU.
2987 Returns a pointer to the next CU. */
aaa75496 2988
93311388
DE
2989static gdb_byte *
2990process_psymtab_comp_unit (struct objfile *objfile,
2991 struct dwarf2_per_cu_data *this_cu,
2992 gdb_byte *buffer, gdb_byte *info_ptr,
2993 unsigned int buffer_size)
c906108c 2994{
c906108c 2995 bfd *abfd = objfile->obfd;
93311388 2996 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 2997 struct die_info *comp_unit_die;
c906108c 2998 struct partial_symtab *pst;
5734ee8b 2999 CORE_ADDR baseaddr;
93311388
DE
3000 struct cleanup *back_to_inner;
3001 struct dwarf2_cu cu;
d85a05f0
DJ
3002 int has_children, has_pc_info;
3003 struct attribute *attr;
d85a05f0
DJ
3004 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3005 struct die_reader_specs reader_specs;
c906108c 3006
93311388
DE
3007 memset (&cu, 0, sizeof (cu));
3008 cu.objfile = objfile;
3009 obstack_init (&cu.comp_unit_obstack);
c906108c 3010
93311388 3011 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3012
93311388
DE
3013 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3014 buffer, buffer_size,
3015 abfd);
10b3939b 3016
93311388
DE
3017 /* Complete the cu_header. */
3018 cu.header.offset = beg_of_comp_unit - buffer;
3019 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3020
93311388 3021 cu.list_in_scope = &file_symbols;
af703f96 3022
328c9494
DJ
3023 /* If this compilation unit was already read in, free the
3024 cached copy in order to read it in again. This is
3025 necessary because we skipped some symbols when we first
3026 read in the compilation unit (see load_partial_dies).
3027 This problem could be avoided, but the benefit is
3028 unclear. */
3029 if (this_cu->cu != NULL)
3030 free_one_cached_comp_unit (this_cu->cu);
3031
3032 /* Note that this is a pointer to our stack frame, being
3033 added to a global data structure. It will be cleaned up
3034 in free_stack_comp_unit when we finish with this
3035 compilation unit. */
3036 this_cu->cu = &cu;
d85a05f0
DJ
3037 cu.per_cu = this_cu;
3038
93311388
DE
3039 /* Read the abbrevs for this compilation unit into a table. */
3040 dwarf2_read_abbrevs (abfd, &cu);
3041 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3042
93311388 3043 /* Read the compilation unit die. */
348e048f
DE
3044 if (this_cu->from_debug_types)
3045 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3046 init_cu_die_reader (&reader_specs, &cu);
3047 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3048 &has_children);
93311388 3049
348e048f
DE
3050 if (this_cu->from_debug_types)
3051 {
3052 /* offset,length haven't been set yet for type units. */
3053 this_cu->offset = cu.header.offset;
3054 this_cu->length = cu.header.length + cu.header.initial_length_size;
3055 }
d85a05f0 3056 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3057 {
93311388
DE
3058 info_ptr = (beg_of_comp_unit + cu.header.length
3059 + cu.header.initial_length_size);
3060 do_cleanups (back_to_inner);
3061 return info_ptr;
3062 }
72bf9492 3063
93311388 3064 /* Set the language we're debugging. */
d85a05f0
DJ
3065 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3066 if (attr)
3067 set_cu_language (DW_UNSND (attr), &cu);
3068 else
3069 set_cu_language (language_minimal, &cu);
c906108c 3070
93311388 3071 /* Allocate a new partial symbol table structure. */
d85a05f0 3072 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3073 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3074 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3075 /* TEXTLOW and TEXTHIGH are set below. */
3076 0,
3077 objfile->global_psymbols.next,
3078 objfile->static_psymbols.next);
72bf9492 3079
d85a05f0
DJ
3080 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3081 if (attr != NULL)
3082 pst->dirname = DW_STRING (attr);
72bf9492 3083
e38df1d0 3084 pst->read_symtab_private = this_cu;
72bf9492 3085
93311388 3086 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3087
93311388
DE
3088 /* Store the function that reads in the rest of the symbol table */
3089 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3090
9291a0cd 3091 this_cu->v.psymtab = pst;
c906108c 3092
d85a05f0
DJ
3093 dwarf2_find_base_address (comp_unit_die, &cu);
3094
93311388
DE
3095 /* Possibly set the default values of LOWPC and HIGHPC from
3096 `DW_AT_ranges'. */
d85a05f0
DJ
3097 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3098 &best_highpc, &cu, pst);
3099 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3100 /* Store the contiguous range if it is not empty; it can be empty for
3101 CUs with no code. */
3102 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3103 best_lowpc + baseaddr,
3104 best_highpc + baseaddr - 1, pst);
93311388
DE
3105
3106 /* Check if comp unit has_children.
3107 If so, read the rest of the partial symbols from this comp unit.
3108 If not, there's no more debug_info for this comp unit. */
d85a05f0 3109 if (has_children)
93311388
DE
3110 {
3111 struct partial_die_info *first_die;
3112 CORE_ADDR lowpc, highpc;
31ffec48 3113
93311388
DE
3114 lowpc = ((CORE_ADDR) -1);
3115 highpc = ((CORE_ADDR) 0);
c906108c 3116
93311388 3117 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3118
93311388 3119 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3120 ! has_pc_info, &cu);
57c22c6c 3121
93311388
DE
3122 /* If we didn't find a lowpc, set it to highpc to avoid
3123 complaints from `maint check'. */
3124 if (lowpc == ((CORE_ADDR) -1))
3125 lowpc = highpc;
10b3939b 3126
93311388
DE
3127 /* If the compilation unit didn't have an explicit address range,
3128 then use the information extracted from its child dies. */
d85a05f0 3129 if (! has_pc_info)
93311388 3130 {
d85a05f0
DJ
3131 best_lowpc = lowpc;
3132 best_highpc = highpc;
93311388
DE
3133 }
3134 }
d85a05f0
DJ
3135 pst->textlow = best_lowpc + baseaddr;
3136 pst->texthigh = best_highpc + baseaddr;
c906108c 3137
93311388
DE
3138 pst->n_global_syms = objfile->global_psymbols.next -
3139 (objfile->global_psymbols.list + pst->globals_offset);
3140 pst->n_static_syms = objfile->static_psymbols.next -
3141 (objfile->static_psymbols.list + pst->statics_offset);
3142 sort_pst_symbols (pst);
c906108c 3143
93311388
DE
3144 info_ptr = (beg_of_comp_unit + cu.header.length
3145 + cu.header.initial_length_size);
ae038cb0 3146
348e048f
DE
3147 if (this_cu->from_debug_types)
3148 {
3149 /* It's not clear we want to do anything with stmt lists here.
3150 Waiting to see what gcc ultimately does. */
3151 }
d85a05f0 3152 else
93311388
DE
3153 {
3154 /* Get the list of files included in the current compilation unit,
3155 and build a psymtab for each of them. */
d85a05f0 3156 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3157 }
ae038cb0 3158
93311388 3159 do_cleanups (back_to_inner);
ae038cb0 3160
93311388
DE
3161 return info_ptr;
3162}
ff013f42 3163
348e048f
DE
3164/* Traversal function for htab_traverse_noresize.
3165 Process one .debug_types comp-unit. */
3166
3167static int
3168process_type_comp_unit (void **slot, void *info)
3169{
3170 struct signatured_type *entry = (struct signatured_type *) *slot;
3171 struct objfile *objfile = (struct objfile *) info;
3172 struct dwarf2_per_cu_data *this_cu;
3173
3174 this_cu = &entry->per_cu;
348e048f 3175
be391dca 3176 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3177 process_psymtab_comp_unit (objfile, this_cu,
3178 dwarf2_per_objfile->types.buffer,
3179 dwarf2_per_objfile->types.buffer + entry->offset,
3180 dwarf2_per_objfile->types.size);
3181
3182 return 1;
3183}
3184
3185/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3186 Build partial symbol tables for the .debug_types comp-units. */
3187
3188static void
3189build_type_psymtabs (struct objfile *objfile)
3190{
3191 if (! create_debug_types_hash_table (objfile))
3192 return;
3193
3194 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3195 process_type_comp_unit, objfile);
3196}
3197
60606b2c
TT
3198/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3199
3200static void
3201psymtabs_addrmap_cleanup (void *o)
3202{
3203 struct objfile *objfile = o;
ec61707d 3204
60606b2c
TT
3205 objfile->psymtabs_addrmap = NULL;
3206}
3207
93311388
DE
3208/* Build the partial symbol table by doing a quick pass through the
3209 .debug_info and .debug_abbrev sections. */
72bf9492 3210
93311388 3211static void
c67a9c90 3212dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3213{
93311388 3214 gdb_byte *info_ptr;
60606b2c
TT
3215 struct cleanup *back_to, *addrmap_cleanup;
3216 struct obstack temp_obstack;
93311388 3217
98bfdba5
PA
3218 dwarf2_per_objfile->reading_partial_symbols = 1;
3219
be391dca 3220 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3221 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3222
93311388
DE
3223 /* Any cached compilation units will be linked by the per-objfile
3224 read_in_chain. Make sure to free them when we're done. */
3225 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3226
348e048f
DE
3227 build_type_psymtabs (objfile);
3228
93311388 3229 create_all_comp_units (objfile);
c906108c 3230
60606b2c
TT
3231 /* Create a temporary address map on a temporary obstack. We later
3232 copy this to the final obstack. */
3233 obstack_init (&temp_obstack);
3234 make_cleanup_obstack_free (&temp_obstack);
3235 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3236 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3237
93311388
DE
3238 /* Since the objects we're extracting from .debug_info vary in
3239 length, only the individual functions to extract them (like
3240 read_comp_unit_head and load_partial_die) can really know whether
3241 the buffer is large enough to hold another complete object.
c906108c 3242
93311388
DE
3243 At the moment, they don't actually check that. If .debug_info
3244 holds just one extra byte after the last compilation unit's dies,
3245 then read_comp_unit_head will happily read off the end of the
3246 buffer. read_partial_die is similarly casual. Those functions
3247 should be fixed.
c906108c 3248
93311388
DE
3249 For this loop condition, simply checking whether there's any data
3250 left at all should be sufficient. */
c906108c 3251
93311388
DE
3252 while (info_ptr < (dwarf2_per_objfile->info.buffer
3253 + dwarf2_per_objfile->info.size))
3254 {
3255 struct dwarf2_per_cu_data *this_cu;
dd373385 3256
93311388
DE
3257 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3258 objfile);
aaa75496 3259
93311388
DE
3260 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3261 dwarf2_per_objfile->info.buffer,
3262 info_ptr,
3263 dwarf2_per_objfile->info.size);
c906108c 3264 }
ff013f42
JK
3265
3266 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3267 &objfile->objfile_obstack);
60606b2c 3268 discard_cleanups (addrmap_cleanup);
ff013f42 3269
ae038cb0
DJ
3270 do_cleanups (back_to);
3271}
3272
93311388 3273/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3274
3275static void
93311388
DE
3276load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3277 struct objfile *objfile)
ae038cb0
DJ
3278{
3279 bfd *abfd = objfile->obfd;
fe1b8b76 3280 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3281 struct die_info *comp_unit_die;
ae038cb0 3282 struct dwarf2_cu *cu;
1d9ec526 3283 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3284 struct attribute *attr;
3285 int has_children;
3286 struct die_reader_specs reader_specs;
98bfdba5 3287 int read_cu = 0;
ae038cb0 3288
348e048f
DE
3289 gdb_assert (! this_cu->from_debug_types);
3290
be391dca 3291 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3292 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3293 beg_of_comp_unit = info_ptr;
3294
98bfdba5
PA
3295 if (this_cu->cu == NULL)
3296 {
3297 cu = alloc_one_comp_unit (objfile);
ae038cb0 3298
98bfdba5 3299 read_cu = 1;
ae038cb0 3300
98bfdba5
PA
3301 /* If an error occurs while loading, release our storage. */
3302 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3303
98bfdba5
PA
3304 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3305 dwarf2_per_objfile->info.buffer,
3306 dwarf2_per_objfile->info.size,
3307 abfd);
ae038cb0 3308
98bfdba5
PA
3309 /* Complete the cu_header. */
3310 cu->header.offset = this_cu->offset;
3311 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3312
3313 /* Link this compilation unit into the compilation unit tree. */
3314 this_cu->cu = cu;
3315 cu->per_cu = this_cu;
98bfdba5
PA
3316
3317 /* Link this CU into read_in_chain. */
3318 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3319 dwarf2_per_objfile->read_in_chain = this_cu;
3320 }
3321 else
3322 {
3323 cu = this_cu->cu;
3324 info_ptr += cu->header.first_die_offset;
3325 }
ae038cb0
DJ
3326
3327 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3328 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3329 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3330 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3331
3332 /* Read the compilation unit die. */
d85a05f0
DJ
3333 init_cu_die_reader (&reader_specs, cu);
3334 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3335 &has_children);
ae038cb0
DJ
3336
3337 /* Set the language we're debugging. */
d85a05f0
DJ
3338 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3339 if (attr)
3340 set_cu_language (DW_UNSND (attr), cu);
3341 else
3342 set_cu_language (language_minimal, cu);
ae038cb0 3343
ae038cb0
DJ
3344 /* Check if comp unit has_children.
3345 If so, read the rest of the partial symbols from this comp unit.
3346 If not, there's no more debug_info for this comp unit. */
d85a05f0 3347 if (has_children)
93311388 3348 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3349
98bfdba5
PA
3350 do_cleanups (free_abbrevs_cleanup);
3351
3352 if (read_cu)
3353 {
3354 /* We've successfully allocated this compilation unit. Let our
3355 caller clean it up when finished with it. */
3356 discard_cleanups (free_cu_cleanup);
3357 }
ae038cb0
DJ
3358}
3359
3360/* Create a list of all compilation units in OBJFILE. We do this only
3361 if an inter-comp-unit reference is found; presumably if there is one,
3362 there will be many, and one will occur early in the .debug_info section.
3363 So there's no point in building this list incrementally. */
3364
3365static void
3366create_all_comp_units (struct objfile *objfile)
3367{
3368 int n_allocated;
3369 int n_comp_units;
3370 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3371 gdb_byte *info_ptr;
3372
3373 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3374 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3375
3376 n_comp_units = 0;
3377 n_allocated = 10;
3378 all_comp_units = xmalloc (n_allocated
3379 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3380
dce234bc 3381 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3382 {
c764a876 3383 unsigned int length, initial_length_size;
ae038cb0 3384 struct dwarf2_per_cu_data *this_cu;
c764a876 3385 unsigned int offset;
ae038cb0 3386
dce234bc 3387 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3388
3389 /* Read just enough information to find out where the next
3390 compilation unit is. */
c764a876
DE
3391 length = read_initial_length (objfile->obfd, info_ptr,
3392 &initial_length_size);
ae038cb0
DJ
3393
3394 /* Save the compilation unit for later lookup. */
3395 this_cu = obstack_alloc (&objfile->objfile_obstack,
3396 sizeof (struct dwarf2_per_cu_data));
3397 memset (this_cu, 0, sizeof (*this_cu));
3398 this_cu->offset = offset;
c764a876 3399 this_cu->length = length + initial_length_size;
9291a0cd 3400 this_cu->objfile = objfile;
ae038cb0
DJ
3401
3402 if (n_comp_units == n_allocated)
3403 {
3404 n_allocated *= 2;
3405 all_comp_units = xrealloc (all_comp_units,
3406 n_allocated
3407 * sizeof (struct dwarf2_per_cu_data *));
3408 }
3409 all_comp_units[n_comp_units++] = this_cu;
3410
3411 info_ptr = info_ptr + this_cu->length;
3412 }
3413
3414 dwarf2_per_objfile->all_comp_units
3415 = obstack_alloc (&objfile->objfile_obstack,
3416 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3417 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3418 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3419 xfree (all_comp_units);
3420 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3421}
3422
5734ee8b
DJ
3423/* Process all loaded DIEs for compilation unit CU, starting at
3424 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3425 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3426 DW_AT_ranges). If NEED_PC is set, then this function will set
3427 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3428 and record the covered ranges in the addrmap. */
c906108c 3429
72bf9492
DJ
3430static void
3431scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3432 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3433{
72bf9492 3434 struct partial_die_info *pdi;
c906108c 3435
91c24f0a
DC
3436 /* Now, march along the PDI's, descending into ones which have
3437 interesting children but skipping the children of the other ones,
3438 until we reach the end of the compilation unit. */
c906108c 3439
72bf9492 3440 pdi = first_die;
91c24f0a 3441
72bf9492
DJ
3442 while (pdi != NULL)
3443 {
3444 fixup_partial_die (pdi, cu);
c906108c 3445
f55ee35c 3446 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3447 children, so we need to look at them. Ditto for anonymous
3448 enums. */
933c6fe4 3449
72bf9492 3450 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3451 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3452 {
72bf9492 3453 switch (pdi->tag)
c906108c
SS
3454 {
3455 case DW_TAG_subprogram:
5734ee8b 3456 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3457 break;
3458 case DW_TAG_variable:
3459 case DW_TAG_typedef:
91c24f0a 3460 case DW_TAG_union_type:
72bf9492 3461 if (!pdi->is_declaration)
63d06c5c 3462 {
72bf9492 3463 add_partial_symbol (pdi, cu);
63d06c5c
DC
3464 }
3465 break;
c906108c 3466 case DW_TAG_class_type:
680b30c7 3467 case DW_TAG_interface_type:
c906108c 3468 case DW_TAG_structure_type:
72bf9492 3469 if (!pdi->is_declaration)
c906108c 3470 {
72bf9492 3471 add_partial_symbol (pdi, cu);
c906108c
SS
3472 }
3473 break;
91c24f0a 3474 case DW_TAG_enumeration_type:
72bf9492
DJ
3475 if (!pdi->is_declaration)
3476 add_partial_enumeration (pdi, cu);
c906108c
SS
3477 break;
3478 case DW_TAG_base_type:
a02abb62 3479 case DW_TAG_subrange_type:
c906108c 3480 /* File scope base type definitions are added to the partial
c5aa993b 3481 symbol table. */
72bf9492 3482 add_partial_symbol (pdi, cu);
c906108c 3483 break;
d9fa45fe 3484 case DW_TAG_namespace:
5734ee8b 3485 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3486 break;
5d7cb8df
JK
3487 case DW_TAG_module:
3488 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3489 break;
c906108c
SS
3490 default:
3491 break;
3492 }
3493 }
3494
72bf9492
DJ
3495 /* If the die has a sibling, skip to the sibling. */
3496
3497 pdi = pdi->die_sibling;
3498 }
3499}
3500
3501/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3502
72bf9492 3503 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3504 name is concatenated with "::" and the partial DIE's name. For
3505 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3506 Enumerators are an exception; they use the scope of their parent
3507 enumeration type, i.e. the name of the enumeration type is not
3508 prepended to the enumerator.
91c24f0a 3509
72bf9492
DJ
3510 There are two complexities. One is DW_AT_specification; in this
3511 case "parent" means the parent of the target of the specification,
3512 instead of the direct parent of the DIE. The other is compilers
3513 which do not emit DW_TAG_namespace; in this case we try to guess
3514 the fully qualified name of structure types from their members'
3515 linkage names. This must be done using the DIE's children rather
3516 than the children of any DW_AT_specification target. We only need
3517 to do this for structures at the top level, i.e. if the target of
3518 any DW_AT_specification (if any; otherwise the DIE itself) does not
3519 have a parent. */
3520
3521/* Compute the scope prefix associated with PDI's parent, in
3522 compilation unit CU. The result will be allocated on CU's
3523 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3524 field. NULL is returned if no prefix is necessary. */
3525static char *
3526partial_die_parent_scope (struct partial_die_info *pdi,
3527 struct dwarf2_cu *cu)
3528{
3529 char *grandparent_scope;
3530 struct partial_die_info *parent, *real_pdi;
91c24f0a 3531
72bf9492
DJ
3532 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3533 then this means the parent of the specification DIE. */
3534
3535 real_pdi = pdi;
72bf9492 3536 while (real_pdi->has_specification)
10b3939b 3537 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3538
3539 parent = real_pdi->die_parent;
3540 if (parent == NULL)
3541 return NULL;
3542
3543 if (parent->scope_set)
3544 return parent->scope;
3545
3546 fixup_partial_die (parent, cu);
3547
10b3939b 3548 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3549
acebe513
UW
3550 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3551 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3552 Work around this problem here. */
3553 if (cu->language == language_cplus
6e70227d 3554 && parent->tag == DW_TAG_namespace
acebe513
UW
3555 && strcmp (parent->name, "::") == 0
3556 && grandparent_scope == NULL)
3557 {
3558 parent->scope = NULL;
3559 parent->scope_set = 1;
3560 return NULL;
3561 }
3562
72bf9492 3563 if (parent->tag == DW_TAG_namespace
f55ee35c 3564 || parent->tag == DW_TAG_module
72bf9492
DJ
3565 || parent->tag == DW_TAG_structure_type
3566 || parent->tag == DW_TAG_class_type
680b30c7 3567 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3568 || parent->tag == DW_TAG_union_type
3569 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3570 {
3571 if (grandparent_scope == NULL)
3572 parent->scope = parent->name;
3573 else
987504bb 3574 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3575 parent->name, 0, cu);
72bf9492 3576 }
ceeb3d5a 3577 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3578 /* Enumerators should not get the name of the enumeration as a prefix. */
3579 parent->scope = grandparent_scope;
3580 else
3581 {
3582 /* FIXME drow/2004-04-01: What should we be doing with
3583 function-local names? For partial symbols, we should probably be
3584 ignoring them. */
3585 complaint (&symfile_complaints,
e2e0b3e5 3586 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3587 parent->tag, pdi->offset);
3588 parent->scope = grandparent_scope;
c906108c
SS
3589 }
3590
72bf9492
DJ
3591 parent->scope_set = 1;
3592 return parent->scope;
3593}
3594
3595/* Return the fully scoped name associated with PDI, from compilation unit
3596 CU. The result will be allocated with malloc. */
3597static char *
3598partial_die_full_name (struct partial_die_info *pdi,
3599 struct dwarf2_cu *cu)
3600{
3601 char *parent_scope;
3602
98bfdba5
PA
3603 /* If this is a template instantiation, we can not work out the
3604 template arguments from partial DIEs. So, unfortunately, we have
3605 to go through the full DIEs. At least any work we do building
3606 types here will be reused if full symbols are loaded later. */
3607 if (pdi->has_template_arguments)
3608 {
3609 fixup_partial_die (pdi, cu);
3610
3611 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3612 {
3613 struct die_info *die;
3614 struct attribute attr;
3615 struct dwarf2_cu *ref_cu = cu;
3616
3617 attr.name = 0;
3618 attr.form = DW_FORM_ref_addr;
3619 attr.u.addr = pdi->offset;
3620 die = follow_die_ref (NULL, &attr, &ref_cu);
3621
3622 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3623 }
3624 }
3625
72bf9492
DJ
3626 parent_scope = partial_die_parent_scope (pdi, cu);
3627 if (parent_scope == NULL)
3628 return NULL;
3629 else
f55ee35c 3630 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3631}
3632
3633static void
72bf9492 3634add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3635{
e7c27a73 3636 struct objfile *objfile = cu->objfile;
c906108c 3637 CORE_ADDR addr = 0;
decbce07 3638 char *actual_name = NULL;
5c4e30ca 3639 const struct partial_symbol *psym = NULL;
e142c38c 3640 CORE_ADDR baseaddr;
72bf9492 3641 int built_actual_name = 0;
e142c38c
DJ
3642
3643 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3644
94af9270
KS
3645 actual_name = partial_die_full_name (pdi, cu);
3646 if (actual_name)
3647 built_actual_name = 1;
63d06c5c 3648
72bf9492
DJ
3649 if (actual_name == NULL)
3650 actual_name = pdi->name;
3651
c906108c
SS
3652 switch (pdi->tag)
3653 {
3654 case DW_TAG_subprogram:
2cfa0c8d 3655 if (pdi->is_external || cu->language == language_ada)
c906108c 3656 {
2cfa0c8d
JB
3657 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3658 of the global scope. But in Ada, we want to be able to access
3659 nested procedures globally. So all Ada subprograms are stored
3660 in the global scope. */
38d518c9 3661 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3662 mst_text, objfile); */
38d518c9 3663 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3664 built_actual_name,
5c4e30ca
DC
3665 VAR_DOMAIN, LOC_BLOCK,
3666 &objfile->global_psymbols,
3667 0, pdi->lowpc + baseaddr,
e142c38c 3668 cu->language, objfile);
c906108c
SS
3669 }
3670 else
3671 {
38d518c9 3672 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3673 mst_file_text, objfile); */
38d518c9 3674 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3675 built_actual_name,
5c4e30ca
DC
3676 VAR_DOMAIN, LOC_BLOCK,
3677 &objfile->static_psymbols,
3678 0, pdi->lowpc + baseaddr,
e142c38c 3679 cu->language, objfile);
c906108c
SS
3680 }
3681 break;
3682 case DW_TAG_variable:
caac4577
JG
3683 if (pdi->locdesc)
3684 addr = decode_locdesc (pdi->locdesc, cu);
3685
3686 if (pdi->locdesc
3687 && addr == 0
3688 && !dwarf2_per_objfile->has_section_at_zero)
3689 {
3690 /* A global or static variable may also have been stripped
3691 out by the linker if unused, in which case its address
3692 will be nullified; do not add such variables into partial
3693 symbol table then. */
3694 }
3695 else if (pdi->is_external)
c906108c
SS
3696 {
3697 /* Global Variable.
3698 Don't enter into the minimal symbol tables as there is
3699 a minimal symbol table entry from the ELF symbols already.
3700 Enter into partial symbol table if it has a location
3701 descriptor or a type.
3702 If the location descriptor is missing, new_symbol will create
3703 a LOC_UNRESOLVED symbol, the address of the variable will then
3704 be determined from the minimal symbol table whenever the variable
3705 is referenced.
3706 The address for the partial symbol table entry is not
3707 used by GDB, but it comes in handy for debugging partial symbol
3708 table building. */
3709
c906108c 3710 if (pdi->locdesc || pdi->has_type)
38d518c9 3711 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3712 built_actual_name,
5c4e30ca
DC
3713 VAR_DOMAIN, LOC_STATIC,
3714 &objfile->global_psymbols,
3715 0, addr + baseaddr,
e142c38c 3716 cu->language, objfile);
c906108c
SS
3717 }
3718 else
3719 {
3720 /* Static Variable. Skip symbols without location descriptors. */
3721 if (pdi->locdesc == NULL)
decbce07
MS
3722 {
3723 if (built_actual_name)
3724 xfree (actual_name);
3725 return;
3726 }
38d518c9 3727 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3728 mst_file_data, objfile); */
38d518c9 3729 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3730 built_actual_name,
5c4e30ca
DC
3731 VAR_DOMAIN, LOC_STATIC,
3732 &objfile->static_psymbols,
3733 0, addr + baseaddr,
e142c38c 3734 cu->language, objfile);
c906108c
SS
3735 }
3736 break;
3737 case DW_TAG_typedef:
3738 case DW_TAG_base_type:
a02abb62 3739 case DW_TAG_subrange_type:
38d518c9 3740 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3741 built_actual_name,
176620f1 3742 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3743 &objfile->static_psymbols,
e142c38c 3744 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3745 break;
72bf9492
DJ
3746 case DW_TAG_namespace:
3747 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3748 built_actual_name,
72bf9492
DJ
3749 VAR_DOMAIN, LOC_TYPEDEF,
3750 &objfile->global_psymbols,
3751 0, (CORE_ADDR) 0, cu->language, objfile);
3752 break;
c906108c 3753 case DW_TAG_class_type:
680b30c7 3754 case DW_TAG_interface_type:
c906108c
SS
3755 case DW_TAG_structure_type:
3756 case DW_TAG_union_type:
3757 case DW_TAG_enumeration_type:
fa4028e9
JB
3758 /* Skip external references. The DWARF standard says in the section
3759 about "Structure, Union, and Class Type Entries": "An incomplete
3760 structure, union or class type is represented by a structure,
3761 union or class entry that does not have a byte size attribute
3762 and that has a DW_AT_declaration attribute." */
3763 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3764 {
3765 if (built_actual_name)
3766 xfree (actual_name);
3767 return;
3768 }
fa4028e9 3769
63d06c5c
DC
3770 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3771 static vs. global. */
38d518c9 3772 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3773 built_actual_name,
176620f1 3774 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3775 (cu->language == language_cplus
3776 || cu->language == language_java)
63d06c5c
DC
3777 ? &objfile->global_psymbols
3778 : &objfile->static_psymbols,
e142c38c 3779 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3780
c906108c
SS
3781 break;
3782 case DW_TAG_enumerator:
38d518c9 3783 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3784 built_actual_name,
176620f1 3785 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3786 (cu->language == language_cplus
3787 || cu->language == language_java)
f6fe98ef
DJ
3788 ? &objfile->global_psymbols
3789 : &objfile->static_psymbols,
e142c38c 3790 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3791 break;
3792 default:
3793 break;
3794 }
5c4e30ca 3795
72bf9492
DJ
3796 if (built_actual_name)
3797 xfree (actual_name);
c906108c
SS
3798}
3799
5c4e30ca
DC
3800/* Read a partial die corresponding to a namespace; also, add a symbol
3801 corresponding to that namespace to the symbol table. NAMESPACE is
3802 the name of the enclosing namespace. */
91c24f0a 3803
72bf9492
DJ
3804static void
3805add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3806 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3807 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3808{
72bf9492 3809 /* Add a symbol for the namespace. */
e7c27a73 3810
72bf9492 3811 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3812
3813 /* Now scan partial symbols in that namespace. */
3814
91c24f0a 3815 if (pdi->has_children)
5734ee8b 3816 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3817}
3818
5d7cb8df
JK
3819/* Read a partial die corresponding to a Fortran module. */
3820
3821static void
3822add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3823 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3824{
f55ee35c 3825 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3826
3827 if (pdi->has_children)
3828 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3829}
3830
bc30ff58
JB
3831/* Read a partial die corresponding to a subprogram and create a partial
3832 symbol for that subprogram. When the CU language allows it, this
3833 routine also defines a partial symbol for each nested subprogram
3834 that this subprogram contains.
6e70227d 3835
bc30ff58
JB
3836 DIE my also be a lexical block, in which case we simply search
3837 recursively for suprograms defined inside that lexical block.
3838 Again, this is only performed when the CU language allows this
3839 type of definitions. */
3840
3841static void
3842add_partial_subprogram (struct partial_die_info *pdi,
3843 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3844 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3845{
3846 if (pdi->tag == DW_TAG_subprogram)
3847 {
3848 if (pdi->has_pc_info)
3849 {
3850 if (pdi->lowpc < *lowpc)
3851 *lowpc = pdi->lowpc;
3852 if (pdi->highpc > *highpc)
3853 *highpc = pdi->highpc;
5734ee8b
DJ
3854 if (need_pc)
3855 {
3856 CORE_ADDR baseaddr;
3857 struct objfile *objfile = cu->objfile;
3858
3859 baseaddr = ANOFFSET (objfile->section_offsets,
3860 SECT_OFF_TEXT (objfile));
3861 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3862 pdi->lowpc + baseaddr,
3863 pdi->highpc - 1 + baseaddr,
9291a0cd 3864 cu->per_cu->v.psymtab);
5734ee8b 3865 }
bc30ff58 3866 if (!pdi->is_declaration)
e8d05480
JB
3867 /* Ignore subprogram DIEs that do not have a name, they are
3868 illegal. Do not emit a complaint at this point, we will
3869 do so when we convert this psymtab into a symtab. */
3870 if (pdi->name)
3871 add_partial_symbol (pdi, cu);
bc30ff58
JB
3872 }
3873 }
6e70227d 3874
bc30ff58
JB
3875 if (! pdi->has_children)
3876 return;
3877
3878 if (cu->language == language_ada)
3879 {
3880 pdi = pdi->die_child;
3881 while (pdi != NULL)
3882 {
3883 fixup_partial_die (pdi, cu);
3884 if (pdi->tag == DW_TAG_subprogram
3885 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3886 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3887 pdi = pdi->die_sibling;
3888 }
3889 }
3890}
3891
72bf9492
DJ
3892/* See if we can figure out if the class lives in a namespace. We do
3893 this by looking for a member function; its demangled name will
3894 contain namespace info, if there is any. */
63d06c5c 3895
72bf9492
DJ
3896static void
3897guess_structure_name (struct partial_die_info *struct_pdi,
3898 struct dwarf2_cu *cu)
63d06c5c 3899{
987504bb
JJ
3900 if ((cu->language == language_cplus
3901 || cu->language == language_java)
72bf9492 3902 && cu->has_namespace_info == 0
63d06c5c
DC
3903 && struct_pdi->has_children)
3904 {
63d06c5c
DC
3905 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3906 what template types look like, because the demangler
3907 frequently doesn't give the same name as the debug info. We
3908 could fix this by only using the demangled name to get the
134d01f1 3909 prefix (but see comment in read_structure_type). */
63d06c5c 3910
72bf9492 3911 struct partial_die_info *real_pdi;
5d51ca54 3912
72bf9492
DJ
3913 /* If this DIE (this DIE's specification, if any) has a parent, then
3914 we should not do this. We'll prepend the parent's fully qualified
3915 name when we create the partial symbol. */
5d51ca54 3916
72bf9492 3917 real_pdi = struct_pdi;
72bf9492 3918 while (real_pdi->has_specification)
10b3939b 3919 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3920
72bf9492
DJ
3921 if (real_pdi->die_parent != NULL)
3922 return;
63d06c5c 3923 }
63d06c5c
DC
3924}
3925
91c24f0a
DC
3926/* Read a partial die corresponding to an enumeration type. */
3927
72bf9492
DJ
3928static void
3929add_partial_enumeration (struct partial_die_info *enum_pdi,
3930 struct dwarf2_cu *cu)
91c24f0a 3931{
72bf9492 3932 struct partial_die_info *pdi;
91c24f0a
DC
3933
3934 if (enum_pdi->name != NULL)
72bf9492
DJ
3935 add_partial_symbol (enum_pdi, cu);
3936
3937 pdi = enum_pdi->die_child;
3938 while (pdi)
91c24f0a 3939 {
72bf9492 3940 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3941 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3942 else
72bf9492
DJ
3943 add_partial_symbol (pdi, cu);
3944 pdi = pdi->die_sibling;
91c24f0a 3945 }
91c24f0a
DC
3946}
3947
4bb7a0a7
DJ
3948/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3949 Return the corresponding abbrev, or NULL if the number is zero (indicating
3950 an empty DIE). In either case *BYTES_READ will be set to the length of
3951 the initial number. */
3952
3953static struct abbrev_info *
fe1b8b76 3954peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3955 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3956{
3957 bfd *abfd = cu->objfile->obfd;
3958 unsigned int abbrev_number;
3959 struct abbrev_info *abbrev;
3960
3961 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3962
3963 if (abbrev_number == 0)
3964 return NULL;
3965
3966 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3967 if (!abbrev)
3968 {
8a3fe4f8 3969 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3970 bfd_get_filename (abfd));
3971 }
3972
3973 return abbrev;
3974}
3975
93311388
DE
3976/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3977 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3978 DIE. Any children of the skipped DIEs will also be skipped. */
3979
fe1b8b76 3980static gdb_byte *
93311388 3981skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3982{
3983 struct abbrev_info *abbrev;
3984 unsigned int bytes_read;
3985
3986 while (1)
3987 {
3988 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3989 if (abbrev == NULL)
3990 return info_ptr + bytes_read;
3991 else
93311388 3992 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3993 }
3994}
3995
93311388
DE
3996/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3997 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3998 abbrev corresponding to that skipped uleb128 should be passed in
3999 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4000 children. */
4001
fe1b8b76 4002static gdb_byte *
93311388
DE
4003skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4004 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4005{
4006 unsigned int bytes_read;
4007 struct attribute attr;
4008 bfd *abfd = cu->objfile->obfd;
4009 unsigned int form, i;
4010
4011 for (i = 0; i < abbrev->num_attrs; i++)
4012 {
4013 /* The only abbrev we care about is DW_AT_sibling. */
4014 if (abbrev->attrs[i].name == DW_AT_sibling)
4015 {
4016 read_attribute (&attr, &abbrev->attrs[i],
4017 abfd, info_ptr, cu);
4018 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4019 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4020 else
93311388 4021 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4022 }
4023
4024 /* If it isn't DW_AT_sibling, skip this attribute. */
4025 form = abbrev->attrs[i].form;
4026 skip_attribute:
4027 switch (form)
4028 {
4bb7a0a7 4029 case DW_FORM_ref_addr:
ae411497
TT
4030 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4031 and later it is offset sized. */
4032 if (cu->header.version == 2)
4033 info_ptr += cu->header.addr_size;
4034 else
4035 info_ptr += cu->header.offset_size;
4036 break;
4037 case DW_FORM_addr:
4bb7a0a7
DJ
4038 info_ptr += cu->header.addr_size;
4039 break;
4040 case DW_FORM_data1:
4041 case DW_FORM_ref1:
4042 case DW_FORM_flag:
4043 info_ptr += 1;
4044 break;
2dc7f7b3
TT
4045 case DW_FORM_flag_present:
4046 break;
4bb7a0a7
DJ
4047 case DW_FORM_data2:
4048 case DW_FORM_ref2:
4049 info_ptr += 2;
4050 break;
4051 case DW_FORM_data4:
4052 case DW_FORM_ref4:
4053 info_ptr += 4;
4054 break;
4055 case DW_FORM_data8:
4056 case DW_FORM_ref8:
348e048f 4057 case DW_FORM_sig8:
4bb7a0a7
DJ
4058 info_ptr += 8;
4059 break;
4060 case DW_FORM_string:
9b1c24c8 4061 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4062 info_ptr += bytes_read;
4063 break;
2dc7f7b3 4064 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4065 case DW_FORM_strp:
4066 info_ptr += cu->header.offset_size;
4067 break;
2dc7f7b3 4068 case DW_FORM_exprloc:
4bb7a0a7
DJ
4069 case DW_FORM_block:
4070 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4071 info_ptr += bytes_read;
4072 break;
4073 case DW_FORM_block1:
4074 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4075 break;
4076 case DW_FORM_block2:
4077 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4078 break;
4079 case DW_FORM_block4:
4080 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4081 break;
4082 case DW_FORM_sdata:
4083 case DW_FORM_udata:
4084 case DW_FORM_ref_udata:
4085 info_ptr = skip_leb128 (abfd, info_ptr);
4086 break;
4087 case DW_FORM_indirect:
4088 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4089 info_ptr += bytes_read;
4090 /* We need to continue parsing from here, so just go back to
4091 the top. */
4092 goto skip_attribute;
4093
4094 default:
8a3fe4f8 4095 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4096 dwarf_form_name (form),
4097 bfd_get_filename (abfd));
4098 }
4099 }
4100
4101 if (abbrev->has_children)
93311388 4102 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4103 else
4104 return info_ptr;
4105}
4106
93311388
DE
4107/* Locate ORIG_PDI's sibling.
4108 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4109 in BUFFER. */
91c24f0a 4110
fe1b8b76 4111static gdb_byte *
93311388
DE
4112locate_pdi_sibling (struct partial_die_info *orig_pdi,
4113 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4114 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4115{
4116 /* Do we know the sibling already? */
72bf9492 4117
91c24f0a
DC
4118 if (orig_pdi->sibling)
4119 return orig_pdi->sibling;
4120
4121 /* Are there any children to deal with? */
4122
4123 if (!orig_pdi->has_children)
4124 return info_ptr;
4125
4bb7a0a7 4126 /* Skip the children the long way. */
91c24f0a 4127
93311388 4128 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4129}
4130
c906108c
SS
4131/* Expand this partial symbol table into a full symbol table. */
4132
4133static void
fba45db2 4134dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4135{
c906108c
SS
4136 if (pst != NULL)
4137 {
4138 if (pst->readin)
4139 {
8a3fe4f8 4140 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4141 }
4142 else
4143 {
4144 if (info_verbose)
4145 {
a3f17187 4146 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4147 gdb_flush (gdb_stdout);
4148 }
4149
10b3939b
DJ
4150 /* Restore our global data. */
4151 dwarf2_per_objfile = objfile_data (pst->objfile,
4152 dwarf2_objfile_data_key);
4153
b2ab525c
KB
4154 /* If this psymtab is constructed from a debug-only objfile, the
4155 has_section_at_zero flag will not necessarily be correct. We
4156 can get the correct value for this flag by looking at the data
4157 associated with the (presumably stripped) associated objfile. */
4158 if (pst->objfile->separate_debug_objfile_backlink)
4159 {
4160 struct dwarf2_per_objfile *dpo_backlink
4161 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4162 dwarf2_objfile_data_key);
9a619af0 4163
b2ab525c
KB
4164 dwarf2_per_objfile->has_section_at_zero
4165 = dpo_backlink->has_section_at_zero;
4166 }
4167
98bfdba5
PA
4168 dwarf2_per_objfile->reading_partial_symbols = 0;
4169
c906108c
SS
4170 psymtab_to_symtab_1 (pst);
4171
4172 /* Finish up the debug error message. */
4173 if (info_verbose)
a3f17187 4174 printf_filtered (_("done.\n"));
c906108c
SS
4175 }
4176 }
4177}
4178
10b3939b
DJ
4179/* Add PER_CU to the queue. */
4180
4181static void
03dd20cc 4182queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4183{
4184 struct dwarf2_queue_item *item;
4185
4186 per_cu->queued = 1;
4187 item = xmalloc (sizeof (*item));
4188 item->per_cu = per_cu;
4189 item->next = NULL;
4190
4191 if (dwarf2_queue == NULL)
4192 dwarf2_queue = item;
4193 else
4194 dwarf2_queue_tail->next = item;
4195
4196 dwarf2_queue_tail = item;
4197}
4198
4199/* Process the queue. */
4200
4201static void
4202process_queue (struct objfile *objfile)
4203{
4204 struct dwarf2_queue_item *item, *next_item;
4205
03dd20cc
DJ
4206 /* The queue starts out with one item, but following a DIE reference
4207 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4208 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4209 {
9291a0cd
TT
4210 if (dwarf2_per_objfile->using_index
4211 ? !item->per_cu->v.quick->symtab
4212 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4213 process_full_comp_unit (item->per_cu);
4214
4215 item->per_cu->queued = 0;
4216 next_item = item->next;
4217 xfree (item);
4218 }
4219
4220 dwarf2_queue_tail = NULL;
4221}
4222
4223/* Free all allocated queue entries. This function only releases anything if
4224 an error was thrown; if the queue was processed then it would have been
4225 freed as we went along. */
4226
4227static void
4228dwarf2_release_queue (void *dummy)
4229{
4230 struct dwarf2_queue_item *item, *last;
4231
4232 item = dwarf2_queue;
4233 while (item)
4234 {
4235 /* Anything still marked queued is likely to be in an
4236 inconsistent state, so discard it. */
4237 if (item->per_cu->queued)
4238 {
4239 if (item->per_cu->cu != NULL)
4240 free_one_cached_comp_unit (item->per_cu->cu);
4241 item->per_cu->queued = 0;
4242 }
4243
4244 last = item;
4245 item = item->next;
4246 xfree (last);
4247 }
4248
4249 dwarf2_queue = dwarf2_queue_tail = NULL;
4250}
4251
4252/* Read in full symbols for PST, and anything it depends on. */
4253
c906108c 4254static void
fba45db2 4255psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4256{
10b3939b 4257 struct dwarf2_per_cu_data *per_cu;
c906108c 4258 struct cleanup *back_to;
aaa75496
JB
4259 int i;
4260
4261 for (i = 0; i < pst->number_of_dependencies; i++)
4262 if (!pst->dependencies[i]->readin)
4263 {
4264 /* Inform about additional files that need to be read in. */
4265 if (info_verbose)
4266 {
a3f17187 4267 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4268 fputs_filtered (" ", gdb_stdout);
4269 wrap_here ("");
4270 fputs_filtered ("and ", gdb_stdout);
4271 wrap_here ("");
4272 printf_filtered ("%s...", pst->dependencies[i]->filename);
4273 wrap_here (""); /* Flush output */
4274 gdb_flush (gdb_stdout);
4275 }
4276 psymtab_to_symtab_1 (pst->dependencies[i]);
4277 }
4278
e38df1d0 4279 per_cu = pst->read_symtab_private;
10b3939b
DJ
4280
4281 if (per_cu == NULL)
aaa75496
JB
4282 {
4283 /* It's an include file, no symbols to read for it.
4284 Everything is in the parent symtab. */
4285 pst->readin = 1;
4286 return;
4287 }
c906108c 4288
9291a0cd 4289 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4290}
4291
93311388 4292/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4293
93311388 4294static void
31ffec48 4295load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4296{
31ffec48 4297 bfd *abfd = objfile->obfd;
10b3939b 4298 struct dwarf2_cu *cu;
c764a876 4299 unsigned int offset;
93311388 4300 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4301 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4302 struct attribute *attr;
98bfdba5 4303 int read_cu = 0;
6502dd73 4304
348e048f
DE
4305 gdb_assert (! per_cu->from_debug_types);
4306
c906108c 4307 /* Set local variables from the partial symbol table info. */
10b3939b 4308 offset = per_cu->offset;
6502dd73 4309
be391dca 4310 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4311 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4312 beg_of_comp_unit = info_ptr;
63d06c5c 4313
98bfdba5
PA
4314 if (per_cu->cu == NULL)
4315 {
4316 cu = alloc_one_comp_unit (objfile);
4317
4318 read_cu = 1;
c906108c 4319
98bfdba5
PA
4320 /* If an error occurs while loading, release our storage. */
4321 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4322
98bfdba5
PA
4323 /* Read in the comp_unit header. */
4324 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4325
98bfdba5
PA
4326 /* Complete the cu_header. */
4327 cu->header.offset = offset;
4328 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4329
98bfdba5
PA
4330 /* Read the abbrevs for this compilation unit. */
4331 dwarf2_read_abbrevs (abfd, cu);
4332 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4333
98bfdba5
PA
4334 /* Link this compilation unit into the compilation unit tree. */
4335 per_cu->cu = cu;
4336 cu->per_cu = per_cu;
98bfdba5
PA
4337
4338 /* Link this CU into read_in_chain. */
4339 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4340 dwarf2_per_objfile->read_in_chain = per_cu;
4341 }
4342 else
4343 {
4344 cu = per_cu->cu;
4345 info_ptr += cu->header.first_die_offset;
4346 }
e142c38c 4347
93311388 4348 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4349
4350 /* We try not to read any attributes in this function, because not
4351 all objfiles needed for references have been loaded yet, and symbol
4352 table processing isn't initialized. But we have to set the CU language,
4353 or we won't be able to build types correctly. */
4354 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4355 if (attr)
4356 set_cu_language (DW_UNSND (attr), cu);
4357 else
4358 set_cu_language (language_minimal, cu);
4359
a6c727b2
DJ
4360 /* Similarly, if we do not read the producer, we can not apply
4361 producer-specific interpretation. */
4362 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4363 if (attr)
4364 cu->producer = DW_STRING (attr);
4365
98bfdba5
PA
4366 if (read_cu)
4367 {
4368 do_cleanups (free_abbrevs_cleanup);
e142c38c 4369
98bfdba5
PA
4370 /* We've successfully allocated this compilation unit. Let our
4371 caller clean it up when finished with it. */
4372 discard_cleanups (free_cu_cleanup);
4373 }
10b3939b
DJ
4374}
4375
3da10d80
KS
4376/* Add a DIE to the delayed physname list. */
4377
4378static void
4379add_to_method_list (struct type *type, int fnfield_index, int index,
4380 const char *name, struct die_info *die,
4381 struct dwarf2_cu *cu)
4382{
4383 struct delayed_method_info mi;
4384 mi.type = type;
4385 mi.fnfield_index = fnfield_index;
4386 mi.index = index;
4387 mi.name = name;
4388 mi.die = die;
4389 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4390}
4391
4392/* A cleanup for freeing the delayed method list. */
4393
4394static void
4395free_delayed_list (void *ptr)
4396{
4397 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4398 if (cu->method_list != NULL)
4399 {
4400 VEC_free (delayed_method_info, cu->method_list);
4401 cu->method_list = NULL;
4402 }
4403}
4404
4405/* Compute the physnames of any methods on the CU's method list.
4406
4407 The computation of method physnames is delayed in order to avoid the
4408 (bad) condition that one of the method's formal parameters is of an as yet
4409 incomplete type. */
4410
4411static void
4412compute_delayed_physnames (struct dwarf2_cu *cu)
4413{
4414 int i;
4415 struct delayed_method_info *mi;
4416 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4417 {
4418 char *physname;
4419 struct fn_fieldlist *fn_flp
4420 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4421 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4422 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4423 }
4424}
4425
10b3939b
DJ
4426/* Generate full symbol information for PST and CU, whose DIEs have
4427 already been loaded into memory. */
4428
4429static void
4430process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4431{
10b3939b 4432 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4433 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4434 CORE_ADDR lowpc, highpc;
4435 struct symtab *symtab;
3da10d80 4436 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4437 CORE_ADDR baseaddr;
4438
4439 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4440
10b3939b
DJ
4441 buildsym_init ();
4442 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4443 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4444
4445 cu->list_in_scope = &file_symbols;
c906108c 4446
d85a05f0 4447 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4448
c906108c 4449 /* Do line number decoding in read_file_scope () */
10b3939b 4450 process_die (cu->dies, cu);
c906108c 4451
3da10d80
KS
4452 /* Now that we have processed all the DIEs in the CU, all the types
4453 should be complete, and it should now be safe to compute all of the
4454 physnames. */
4455 compute_delayed_physnames (cu);
4456 do_cleanups (delayed_list_cleanup);
4457
fae299cd
DC
4458 /* Some compilers don't define a DW_AT_high_pc attribute for the
4459 compilation unit. If the DW_AT_high_pc is missing, synthesize
4460 it, by scanning the DIE's below the compilation unit. */
10b3939b 4461 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4462
613e1657 4463 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4464
4465 /* Set symtab language to language from DW_AT_language.
4466 If the compilation is from a C file generated by language preprocessors,
4467 do not set the language if it was already deduced by start_subfile. */
4468 if (symtab != NULL
10b3939b 4469 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4470 {
10b3939b 4471 symtab->language = cu->language;
c906108c 4472 }
9291a0cd
TT
4473
4474 if (dwarf2_per_objfile->using_index)
4475 per_cu->v.quick->symtab = symtab;
4476 else
4477 {
4478 struct partial_symtab *pst = per_cu->v.psymtab;
4479 pst->symtab = symtab;
4480 pst->readin = 1;
4481 }
c906108c
SS
4482
4483 do_cleanups (back_to);
4484}
4485
4486/* Process a die and its children. */
4487
4488static void
e7c27a73 4489process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4490{
4491 switch (die->tag)
4492 {
4493 case DW_TAG_padding:
4494 break;
4495 case DW_TAG_compile_unit:
e7c27a73 4496 read_file_scope (die, cu);
c906108c 4497 break;
348e048f
DE
4498 case DW_TAG_type_unit:
4499 read_type_unit_scope (die, cu);
4500 break;
c906108c 4501 case DW_TAG_subprogram:
c906108c 4502 case DW_TAG_inlined_subroutine:
edb3359d 4503 read_func_scope (die, cu);
c906108c
SS
4504 break;
4505 case DW_TAG_lexical_block:
14898363
L
4506 case DW_TAG_try_block:
4507 case DW_TAG_catch_block:
e7c27a73 4508 read_lexical_block_scope (die, cu);
c906108c
SS
4509 break;
4510 case DW_TAG_class_type:
680b30c7 4511 case DW_TAG_interface_type:
c906108c
SS
4512 case DW_TAG_structure_type:
4513 case DW_TAG_union_type:
134d01f1 4514 process_structure_scope (die, cu);
c906108c
SS
4515 break;
4516 case DW_TAG_enumeration_type:
134d01f1 4517 process_enumeration_scope (die, cu);
c906108c 4518 break;
134d01f1 4519
f792889a
DJ
4520 /* These dies have a type, but processing them does not create
4521 a symbol or recurse to process the children. Therefore we can
4522 read them on-demand through read_type_die. */
c906108c 4523 case DW_TAG_subroutine_type:
72019c9c 4524 case DW_TAG_set_type:
c906108c 4525 case DW_TAG_array_type:
c906108c 4526 case DW_TAG_pointer_type:
c906108c 4527 case DW_TAG_ptr_to_member_type:
c906108c 4528 case DW_TAG_reference_type:
c906108c 4529 case DW_TAG_string_type:
c906108c 4530 break;
134d01f1 4531
c906108c 4532 case DW_TAG_base_type:
a02abb62 4533 case DW_TAG_subrange_type:
cb249c71 4534 case DW_TAG_typedef:
134d01f1
DJ
4535 /* Add a typedef symbol for the type definition, if it has a
4536 DW_AT_name. */
f792889a 4537 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4538 break;
c906108c 4539 case DW_TAG_common_block:
e7c27a73 4540 read_common_block (die, cu);
c906108c
SS
4541 break;
4542 case DW_TAG_common_inclusion:
4543 break;
d9fa45fe 4544 case DW_TAG_namespace:
63d06c5c 4545 processing_has_namespace_info = 1;
e7c27a73 4546 read_namespace (die, cu);
d9fa45fe 4547 break;
5d7cb8df 4548 case DW_TAG_module:
f55ee35c 4549 processing_has_namespace_info = 1;
5d7cb8df
JK
4550 read_module (die, cu);
4551 break;
d9fa45fe
DC
4552 case DW_TAG_imported_declaration:
4553 case DW_TAG_imported_module:
63d06c5c 4554 processing_has_namespace_info = 1;
27aa8d6a
SW
4555 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4556 || cu->language != language_fortran))
4557 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4558 dwarf_tag_name (die->tag));
4559 read_import_statement (die, cu);
d9fa45fe 4560 break;
c906108c 4561 default:
e7c27a73 4562 new_symbol (die, NULL, cu);
c906108c
SS
4563 break;
4564 }
4565}
4566
94af9270
KS
4567/* A helper function for dwarf2_compute_name which determines whether DIE
4568 needs to have the name of the scope prepended to the name listed in the
4569 die. */
4570
4571static int
4572die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4573{
1c809c68
TT
4574 struct attribute *attr;
4575
94af9270
KS
4576 switch (die->tag)
4577 {
4578 case DW_TAG_namespace:
4579 case DW_TAG_typedef:
4580 case DW_TAG_class_type:
4581 case DW_TAG_interface_type:
4582 case DW_TAG_structure_type:
4583 case DW_TAG_union_type:
4584 case DW_TAG_enumeration_type:
4585 case DW_TAG_enumerator:
4586 case DW_TAG_subprogram:
4587 case DW_TAG_member:
4588 return 1;
4589
4590 case DW_TAG_variable:
4591 /* We only need to prefix "globally" visible variables. These include
4592 any variable marked with DW_AT_external or any variable that
4593 lives in a namespace. [Variables in anonymous namespaces
4594 require prefixing, but they are not DW_AT_external.] */
4595
4596 if (dwarf2_attr (die, DW_AT_specification, cu))
4597 {
4598 struct dwarf2_cu *spec_cu = cu;
9a619af0 4599
94af9270
KS
4600 return die_needs_namespace (die_specification (die, &spec_cu),
4601 spec_cu);
4602 }
4603
1c809c68 4604 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4605 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4606 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4607 return 0;
4608 /* A variable in a lexical block of some kind does not need a
4609 namespace, even though in C++ such variables may be external
4610 and have a mangled name. */
4611 if (die->parent->tag == DW_TAG_lexical_block
4612 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4613 || die->parent->tag == DW_TAG_catch_block
4614 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4615 return 0;
4616 return 1;
94af9270
KS
4617
4618 default:
4619 return 0;
4620 }
4621}
4622
98bfdba5
PA
4623/* Retrieve the last character from a mem_file. */
4624
4625static void
4626do_ui_file_peek_last (void *object, const char *buffer, long length)
4627{
4628 char *last_char_p = (char *) object;
4629
4630 if (length > 0)
4631 *last_char_p = buffer[length - 1];
4632}
4633
94af9270
KS
4634/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4635 compute the physname for the object, which include a method's
4636 formal parameters (C++/Java) and return type (Java).
4637
af6b7be1
JB
4638 For Ada, return the DIE's linkage name rather than the fully qualified
4639 name. PHYSNAME is ignored..
4640
94af9270
KS
4641 The result is allocated on the objfile_obstack and canonicalized. */
4642
4643static const char *
4644dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4645 int physname)
4646{
4647 if (name == NULL)
4648 name = dwarf2_name (die, cu);
4649
f55ee35c
JK
4650 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4651 compute it by typename_concat inside GDB. */
4652 if (cu->language == language_ada
4653 || (cu->language == language_fortran && physname))
4654 {
4655 /* For Ada unit, we prefer the linkage name over the name, as
4656 the former contains the exported name, which the user expects
4657 to be able to reference. Ideally, we want the user to be able
4658 to reference this entity using either natural or linkage name,
4659 but we haven't started looking at this enhancement yet. */
4660 struct attribute *attr;
4661
4662 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4663 if (attr == NULL)
4664 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4665 if (attr && DW_STRING (attr))
4666 return DW_STRING (attr);
4667 }
4668
94af9270
KS
4669 /* These are the only languages we know how to qualify names in. */
4670 if (name != NULL
f55ee35c
JK
4671 && (cu->language == language_cplus || cu->language == language_java
4672 || cu->language == language_fortran))
94af9270
KS
4673 {
4674 if (die_needs_namespace (die, cu))
4675 {
4676 long length;
4677 char *prefix;
4678 struct ui_file *buf;
4679
4680 prefix = determine_prefix (die, cu);
4681 buf = mem_fileopen ();
4682 if (*prefix != '\0')
4683 {
f55ee35c
JK
4684 char *prefixed_name = typename_concat (NULL, prefix, name,
4685 physname, cu);
9a619af0 4686
94af9270
KS
4687 fputs_unfiltered (prefixed_name, buf);
4688 xfree (prefixed_name);
4689 }
4690 else
4691 fputs_unfiltered (name ? name : "", buf);
4692
98bfdba5
PA
4693 /* Template parameters may be specified in the DIE's DW_AT_name, or
4694 as children with DW_TAG_template_type_param or
4695 DW_TAG_value_type_param. If the latter, add them to the name
4696 here. If the name already has template parameters, then
4697 skip this step; some versions of GCC emit both, and
4698 it is more efficient to use the pre-computed name.
4699
4700 Something to keep in mind about this process: it is very
4701 unlikely, or in some cases downright impossible, to produce
4702 something that will match the mangled name of a function.
4703 If the definition of the function has the same debug info,
4704 we should be able to match up with it anyway. But fallbacks
4705 using the minimal symbol, for instance to find a method
4706 implemented in a stripped copy of libstdc++, will not work.
4707 If we do not have debug info for the definition, we will have to
4708 match them up some other way.
4709
4710 When we do name matching there is a related problem with function
4711 templates; two instantiated function templates are allowed to
4712 differ only by their return types, which we do not add here. */
4713
4714 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4715 {
4716 struct attribute *attr;
4717 struct die_info *child;
4718 int first = 1;
4719
4720 die->building_fullname = 1;
4721
4722 for (child = die->child; child != NULL; child = child->sibling)
4723 {
4724 struct type *type;
4725 long value;
4726 gdb_byte *bytes;
4727 struct dwarf2_locexpr_baton *baton;
4728 struct value *v;
4729
4730 if (child->tag != DW_TAG_template_type_param
4731 && child->tag != DW_TAG_template_value_param)
4732 continue;
4733
4734 if (first)
4735 {
4736 fputs_unfiltered ("<", buf);
4737 first = 0;
4738 }
4739 else
4740 fputs_unfiltered (", ", buf);
4741
4742 attr = dwarf2_attr (child, DW_AT_type, cu);
4743 if (attr == NULL)
4744 {
4745 complaint (&symfile_complaints,
4746 _("template parameter missing DW_AT_type"));
4747 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4748 continue;
4749 }
4750 type = die_type (child, cu);
4751
4752 if (child->tag == DW_TAG_template_type_param)
4753 {
4754 c_print_type (type, "", buf, -1, 0);
4755 continue;
4756 }
4757
4758 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4759 if (attr == NULL)
4760 {
4761 complaint (&symfile_complaints,
4762 _("template parameter missing DW_AT_const_value"));
4763 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4764 continue;
4765 }
4766
4767 dwarf2_const_value_attr (attr, type, name,
4768 &cu->comp_unit_obstack, cu,
4769 &value, &bytes, &baton);
4770
4771 if (TYPE_NOSIGN (type))
4772 /* GDB prints characters as NUMBER 'CHAR'. If that's
4773 changed, this can use value_print instead. */
4774 c_printchar (value, type, buf);
4775 else
4776 {
4777 struct value_print_options opts;
4778
4779 if (baton != NULL)
4780 v = dwarf2_evaluate_loc_desc (type, NULL,
4781 baton->data,
4782 baton->size,
4783 baton->per_cu);
4784 else if (bytes != NULL)
4785 {
4786 v = allocate_value (type);
4787 memcpy (value_contents_writeable (v), bytes,
4788 TYPE_LENGTH (type));
4789 }
4790 else
4791 v = value_from_longest (type, value);
4792
4793 /* Specify decimal so that we do not depend on the radix. */
4794 get_formatted_print_options (&opts, 'd');
4795 opts.raw = 1;
4796 value_print (v, buf, &opts);
4797 release_value (v);
4798 value_free (v);
4799 }
4800 }
4801
4802 die->building_fullname = 0;
4803
4804 if (!first)
4805 {
4806 /* Close the argument list, with a space if necessary
4807 (nested templates). */
4808 char last_char = '\0';
4809 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4810 if (last_char == '>')
4811 fputs_unfiltered (" >", buf);
4812 else
4813 fputs_unfiltered (">", buf);
4814 }
4815 }
4816
94af9270
KS
4817 /* For Java and C++ methods, append formal parameter type
4818 information, if PHYSNAME. */
6e70227d 4819
94af9270
KS
4820 if (physname && die->tag == DW_TAG_subprogram
4821 && (cu->language == language_cplus
4822 || cu->language == language_java))
4823 {
4824 struct type *type = read_type_die (die, cu);
4825
4826 c_type_print_args (type, buf, 0, cu->language);
4827
4828 if (cu->language == language_java)
4829 {
4830 /* For java, we must append the return type to method
4831 names. */
4832 if (die->tag == DW_TAG_subprogram)
4833 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4834 0, 0);
4835 }
4836 else if (cu->language == language_cplus)
4837 {
4838 if (TYPE_NFIELDS (type) > 0
4839 && TYPE_FIELD_ARTIFICIAL (type, 0)
4840 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4841 fputs_unfiltered (" const", buf);
4842 }
4843 }
4844
4845 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4846 &length);
4847 ui_file_delete (buf);
4848
4849 if (cu->language == language_cplus)
4850 {
4851 char *cname
4852 = dwarf2_canonicalize_name (name, cu,
4853 &cu->objfile->objfile_obstack);
9a619af0 4854
94af9270
KS
4855 if (cname != NULL)
4856 name = cname;
4857 }
4858 }
4859 }
4860
4861 return name;
4862}
4863
0114d602
DJ
4864/* Return the fully qualified name of DIE, based on its DW_AT_name.
4865 If scope qualifiers are appropriate they will be added. The result
4866 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4867 not have a name. NAME may either be from a previous call to
4868 dwarf2_name or NULL.
4869
4870 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4871
4872static const char *
94af9270 4873dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4874{
94af9270
KS
4875 return dwarf2_compute_name (name, die, cu, 0);
4876}
0114d602 4877
94af9270
KS
4878/* Construct a physname for the given DIE in CU. NAME may either be
4879 from a previous call to dwarf2_name or NULL. The result will be
4880 allocated on the objfile_objstack or NULL if the DIE does not have a
4881 name.
0114d602 4882
94af9270 4883 The output string will be canonicalized (if C++/Java). */
0114d602 4884
94af9270
KS
4885static const char *
4886dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4887{
4888 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4889}
4890
27aa8d6a
SW
4891/* Read the import statement specified by the given die and record it. */
4892
4893static void
4894read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4895{
4896 struct attribute *import_attr;
4897 struct die_info *imported_die;
de4affc9 4898 struct dwarf2_cu *imported_cu;
27aa8d6a 4899 const char *imported_name;
794684b6 4900 const char *imported_name_prefix;
13387711
SW
4901 const char *canonical_name;
4902 const char *import_alias;
4903 const char *imported_declaration = NULL;
794684b6 4904 const char *import_prefix;
13387711
SW
4905
4906 char *temp;
27aa8d6a
SW
4907
4908 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4909 if (import_attr == NULL)
4910 {
4911 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4912 dwarf_tag_name (die->tag));
4913 return;
4914 }
4915
de4affc9
CC
4916 imported_cu = cu;
4917 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4918 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4919 if (imported_name == NULL)
4920 {
4921 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4922
4923 The import in the following code:
4924 namespace A
4925 {
4926 typedef int B;
4927 }
4928
4929 int main ()
4930 {
4931 using A::B;
4932 B b;
4933 return b;
4934 }
4935
4936 ...
4937 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4938 <52> DW_AT_decl_file : 1
4939 <53> DW_AT_decl_line : 6
4940 <54> DW_AT_import : <0x75>
4941 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4942 <59> DW_AT_name : B
4943 <5b> DW_AT_decl_file : 1
4944 <5c> DW_AT_decl_line : 2
4945 <5d> DW_AT_type : <0x6e>
4946 ...
4947 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4948 <76> DW_AT_byte_size : 4
4949 <77> DW_AT_encoding : 5 (signed)
4950
4951 imports the wrong die ( 0x75 instead of 0x58 ).
4952 This case will be ignored until the gcc bug is fixed. */
4953 return;
4954 }
4955
82856980
SW
4956 /* Figure out the local name after import. */
4957 import_alias = dwarf2_name (die, cu);
27aa8d6a 4958
794684b6
SW
4959 /* Figure out where the statement is being imported to. */
4960 import_prefix = determine_prefix (die, cu);
4961
4962 /* Figure out what the scope of the imported die is and prepend it
4963 to the name of the imported die. */
de4affc9 4964 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4965
f55ee35c
JK
4966 if (imported_die->tag != DW_TAG_namespace
4967 && imported_die->tag != DW_TAG_module)
794684b6 4968 {
13387711
SW
4969 imported_declaration = imported_name;
4970 canonical_name = imported_name_prefix;
794684b6 4971 }
13387711 4972 else if (strlen (imported_name_prefix) > 0)
794684b6 4973 {
13387711
SW
4974 temp = alloca (strlen (imported_name_prefix)
4975 + 2 + strlen (imported_name) + 1);
4976 strcpy (temp, imported_name_prefix);
4977 strcat (temp, "::");
4978 strcat (temp, imported_name);
4979 canonical_name = temp;
794684b6 4980 }
13387711
SW
4981 else
4982 canonical_name = imported_name;
794684b6 4983
c0cc3a76
SW
4984 cp_add_using_directive (import_prefix,
4985 canonical_name,
4986 import_alias,
13387711 4987 imported_declaration,
c0cc3a76 4988 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4989}
4990
5fb290d7 4991static void
e142c38c 4992initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4993{
e142c38c 4994 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4995}
4996
cb1df416
DJ
4997static void
4998free_cu_line_header (void *arg)
4999{
5000 struct dwarf2_cu *cu = arg;
5001
5002 free_line_header (cu->line_header);
5003 cu->line_header = NULL;
5004}
5005
9291a0cd
TT
5006static void
5007find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5008 char **name, char **comp_dir)
5009{
5010 struct attribute *attr;
5011
5012 *name = NULL;
5013 *comp_dir = NULL;
5014
5015 /* Find the filename. Do not use dwarf2_name here, since the filename
5016 is not a source language identifier. */
5017 attr = dwarf2_attr (die, DW_AT_name, cu);
5018 if (attr)
5019 {
5020 *name = DW_STRING (attr);
5021 }
5022
5023 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5024 if (attr)
5025 *comp_dir = DW_STRING (attr);
5026 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5027 {
5028 *comp_dir = ldirname (*name);
5029 if (*comp_dir != NULL)
5030 make_cleanup (xfree, *comp_dir);
5031 }
5032 if (*comp_dir != NULL)
5033 {
5034 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5035 directory, get rid of it. */
5036 char *cp = strchr (*comp_dir, ':');
5037
5038 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5039 *comp_dir = cp + 1;
5040 }
5041
5042 if (*name == NULL)
5043 *name = "<unknown>";
5044}
5045
c906108c 5046static void
e7c27a73 5047read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5048{
e7c27a73 5049 struct objfile *objfile = cu->objfile;
debd256d 5050 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5051 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5052 CORE_ADDR highpc = ((CORE_ADDR) 0);
5053 struct attribute *attr;
e1024ff1 5054 char *name = NULL;
c906108c
SS
5055 char *comp_dir = NULL;
5056 struct die_info *child_die;
5057 bfd *abfd = objfile->obfd;
debd256d 5058 struct line_header *line_header = 0;
e142c38c 5059 CORE_ADDR baseaddr;
6e70227d 5060
e142c38c 5061 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5062
fae299cd 5063 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5064
5065 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5066 from finish_block. */
2acceee2 5067 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5068 lowpc = highpc;
5069 lowpc += baseaddr;
5070 highpc += baseaddr;
5071
9291a0cd 5072 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5073
e142c38c 5074 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5075 if (attr)
5076 {
e142c38c 5077 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5078 }
5079
b0f35d58 5080 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5081 if (attr)
b0f35d58 5082 cu->producer = DW_STRING (attr);
303b6f5d 5083
c906108c
SS
5084 /* We assume that we're processing GCC output. */
5085 processing_gcc_compilation = 2;
c906108c 5086
df8a16a1
DJ
5087 processing_has_namespace_info = 0;
5088
c906108c
SS
5089 start_symtab (name, comp_dir, lowpc);
5090 record_debugformat ("DWARF 2");
303b6f5d 5091 record_producer (cu->producer);
c906108c 5092
e142c38c 5093 initialize_cu_func_list (cu);
c906108c 5094
cb1df416
DJ
5095 /* Decode line number information if present. We do this before
5096 processing child DIEs, so that the line header table is available
5097 for DW_AT_decl_file. */
e142c38c 5098 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5099 if (attr)
5100 {
debd256d 5101 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5102 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5103 if (line_header)
5104 {
cb1df416
DJ
5105 cu->line_header = line_header;
5106 make_cleanup (free_cu_line_header, cu);
aaa75496 5107 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5108 }
5fb290d7 5109 }
debd256d 5110
cb1df416
DJ
5111 /* Process all dies in compilation unit. */
5112 if (die->child != NULL)
5113 {
5114 child_die = die->child;
5115 while (child_die && child_die->tag)
5116 {
5117 process_die (child_die, cu);
5118 child_die = sibling_die (child_die);
5119 }
5120 }
5121
2e276125
JB
5122 /* Decode macro information, if present. Dwarf 2 macro information
5123 refers to information in the line number info statement program
5124 header, so we can only read it if we've read the header
5125 successfully. */
e142c38c 5126 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5127 if (attr && line_header)
2e276125
JB
5128 {
5129 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5130
2e276125 5131 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5132 comp_dir, abfd, cu);
2e276125 5133 }
debd256d 5134 do_cleanups (back_to);
5fb290d7
DJ
5135}
5136
348e048f
DE
5137/* For TUs we want to skip the first top level sibling if it's not the
5138 actual type being defined by this TU. In this case the first top
5139 level sibling is there to provide context only. */
5140
5141static void
5142read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5143{
5144 struct objfile *objfile = cu->objfile;
5145 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5146 CORE_ADDR lowpc;
5147 struct attribute *attr;
5148 char *name = NULL;
5149 char *comp_dir = NULL;
5150 struct die_info *child_die;
5151 bfd *abfd = objfile->obfd;
348e048f
DE
5152
5153 /* start_symtab needs a low pc, but we don't really have one.
5154 Do what read_file_scope would do in the absence of such info. */
5155 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5156
5157 /* Find the filename. Do not use dwarf2_name here, since the filename
5158 is not a source language identifier. */
5159 attr = dwarf2_attr (die, DW_AT_name, cu);
5160 if (attr)
5161 name = DW_STRING (attr);
5162
5163 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5164 if (attr)
5165 comp_dir = DW_STRING (attr);
5166 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5167 {
5168 comp_dir = ldirname (name);
5169 if (comp_dir != NULL)
5170 make_cleanup (xfree, comp_dir);
5171 }
5172
5173 if (name == NULL)
5174 name = "<unknown>";
5175
5176 attr = dwarf2_attr (die, DW_AT_language, cu);
5177 if (attr)
5178 set_cu_language (DW_UNSND (attr), cu);
5179
5180 /* This isn't technically needed today. It is done for symmetry
5181 with read_file_scope. */
5182 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5183 if (attr)
348e048f
DE
5184 cu->producer = DW_STRING (attr);
5185
5186 /* We assume that we're processing GCC output. */
5187 processing_gcc_compilation = 2;
5188
5189 processing_has_namespace_info = 0;
5190
5191 start_symtab (name, comp_dir, lowpc);
5192 record_debugformat ("DWARF 2");
5193 record_producer (cu->producer);
5194
5195 /* Process the dies in the type unit. */
5196 if (die->child == NULL)
5197 {
5198 dump_die_for_error (die);
5199 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5200 bfd_get_filename (abfd));
5201 }
5202
5203 child_die = die->child;
5204
5205 while (child_die && child_die->tag)
5206 {
5207 process_die (child_die, cu);
5208
5209 child_die = sibling_die (child_die);
5210 }
5211
5212 do_cleanups (back_to);
5213}
5214
5fb290d7 5215static void
e142c38c
DJ
5216add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5217 struct dwarf2_cu *cu)
5fb290d7
DJ
5218{
5219 struct function_range *thisfn;
5220
5221 thisfn = (struct function_range *)
7b5a2f43 5222 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5223 thisfn->name = name;
5224 thisfn->lowpc = lowpc;
5225 thisfn->highpc = highpc;
5226 thisfn->seen_line = 0;
5227 thisfn->next = NULL;
5228
e142c38c
DJ
5229 if (cu->last_fn == NULL)
5230 cu->first_fn = thisfn;
5fb290d7 5231 else
e142c38c 5232 cu->last_fn->next = thisfn;
5fb290d7 5233
e142c38c 5234 cu->last_fn = thisfn;
c906108c
SS
5235}
5236
d389af10
JK
5237/* qsort helper for inherit_abstract_dies. */
5238
5239static int
5240unsigned_int_compar (const void *ap, const void *bp)
5241{
5242 unsigned int a = *(unsigned int *) ap;
5243 unsigned int b = *(unsigned int *) bp;
5244
5245 return (a > b) - (b > a);
5246}
5247
5248/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5249 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5250 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5251
5252static void
5253inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5254{
5255 struct die_info *child_die;
5256 unsigned die_children_count;
5257 /* CU offsets which were referenced by children of the current DIE. */
5258 unsigned *offsets;
5259 unsigned *offsets_end, *offsetp;
5260 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5261 struct die_info *origin_die;
5262 /* Iterator of the ORIGIN_DIE children. */
5263 struct die_info *origin_child_die;
5264 struct cleanup *cleanups;
5265 struct attribute *attr;
cd02d79d
PA
5266 struct dwarf2_cu *origin_cu;
5267 struct pending **origin_previous_list_in_scope;
d389af10
JK
5268
5269 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5270 if (!attr)
5271 return;
5272
cd02d79d
PA
5273 /* Note that following die references may follow to a die in a
5274 different cu. */
5275
5276 origin_cu = cu;
5277 origin_die = follow_die_ref (die, attr, &origin_cu);
5278
5279 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5280 symbols in. */
5281 origin_previous_list_in_scope = origin_cu->list_in_scope;
5282 origin_cu->list_in_scope = cu->list_in_scope;
5283
edb3359d
DJ
5284 if (die->tag != origin_die->tag
5285 && !(die->tag == DW_TAG_inlined_subroutine
5286 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5287 complaint (&symfile_complaints,
5288 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5289 die->offset, origin_die->offset);
5290
5291 child_die = die->child;
5292 die_children_count = 0;
5293 while (child_die && child_die->tag)
5294 {
5295 child_die = sibling_die (child_die);
5296 die_children_count++;
5297 }
5298 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5299 cleanups = make_cleanup (xfree, offsets);
5300
5301 offsets_end = offsets;
5302 child_die = die->child;
5303 while (child_die && child_die->tag)
5304 {
c38f313d
DJ
5305 /* For each CHILD_DIE, find the corresponding child of
5306 ORIGIN_DIE. If there is more than one layer of
5307 DW_AT_abstract_origin, follow them all; there shouldn't be,
5308 but GCC versions at least through 4.4 generate this (GCC PR
5309 40573). */
5310 struct die_info *child_origin_die = child_die;
cd02d79d 5311 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5312
c38f313d
DJ
5313 while (1)
5314 {
cd02d79d
PA
5315 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5316 child_origin_cu);
c38f313d
DJ
5317 if (attr == NULL)
5318 break;
cd02d79d
PA
5319 child_origin_die = follow_die_ref (child_origin_die, attr,
5320 &child_origin_cu);
c38f313d
DJ
5321 }
5322
d389af10
JK
5323 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5324 counterpart may exist. */
c38f313d 5325 if (child_origin_die != child_die)
d389af10 5326 {
edb3359d
DJ
5327 if (child_die->tag != child_origin_die->tag
5328 && !(child_die->tag == DW_TAG_inlined_subroutine
5329 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5330 complaint (&symfile_complaints,
5331 _("Child DIE 0x%x and its abstract origin 0x%x have "
5332 "different tags"), child_die->offset,
5333 child_origin_die->offset);
c38f313d
DJ
5334 if (child_origin_die->parent != origin_die)
5335 complaint (&symfile_complaints,
5336 _("Child DIE 0x%x and its abstract origin 0x%x have "
5337 "different parents"), child_die->offset,
5338 child_origin_die->offset);
5339 else
5340 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5341 }
5342 child_die = sibling_die (child_die);
5343 }
5344 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5345 unsigned_int_compar);
5346 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5347 if (offsetp[-1] == *offsetp)
5348 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5349 "to DIE 0x%x as their abstract origin"),
5350 die->offset, *offsetp);
5351
5352 offsetp = offsets;
5353 origin_child_die = origin_die->child;
5354 while (origin_child_die && origin_child_die->tag)
5355 {
5356 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5357 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5358 offsetp++;
5359 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5360 {
5361 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5362 process_die (origin_child_die, origin_cu);
d389af10
JK
5363 }
5364 origin_child_die = sibling_die (origin_child_die);
5365 }
cd02d79d 5366 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5367
5368 do_cleanups (cleanups);
5369}
5370
c906108c 5371static void
e7c27a73 5372read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5373{
e7c27a73 5374 struct objfile *objfile = cu->objfile;
52f0bd74 5375 struct context_stack *new;
c906108c
SS
5376 CORE_ADDR lowpc;
5377 CORE_ADDR highpc;
5378 struct die_info *child_die;
edb3359d 5379 struct attribute *attr, *call_line, *call_file;
c906108c 5380 char *name;
e142c38c 5381 CORE_ADDR baseaddr;
801e3a5b 5382 struct block *block;
edb3359d 5383 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5384 VEC (symbolp) *template_args = NULL;
5385 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5386
5387 if (inlined_func)
5388 {
5389 /* If we do not have call site information, we can't show the
5390 caller of this inlined function. That's too confusing, so
5391 only use the scope for local variables. */
5392 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5393 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5394 if (call_line == NULL || call_file == NULL)
5395 {
5396 read_lexical_block_scope (die, cu);
5397 return;
5398 }
5399 }
c906108c 5400
e142c38c
DJ
5401 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5402
94af9270 5403 name = dwarf2_name (die, cu);
c906108c 5404
e8d05480
JB
5405 /* Ignore functions with missing or empty names. These are actually
5406 illegal according to the DWARF standard. */
5407 if (name == NULL)
5408 {
5409 complaint (&symfile_complaints,
5410 _("missing name for subprogram DIE at %d"), die->offset);
5411 return;
5412 }
5413
5414 /* Ignore functions with missing or invalid low and high pc attributes. */
5415 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5416 {
ae4d0c03
PM
5417 attr = dwarf2_attr (die, DW_AT_external, cu);
5418 if (!attr || !DW_UNSND (attr))
5419 complaint (&symfile_complaints,
5420 _("cannot get low and high bounds for subprogram DIE at %d"),
5421 die->offset);
e8d05480
JB
5422 return;
5423 }
c906108c
SS
5424
5425 lowpc += baseaddr;
5426 highpc += baseaddr;
5427
5fb290d7 5428 /* Record the function range for dwarf_decode_lines. */
e142c38c 5429 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5430
34eaf542
TT
5431 /* If we have any template arguments, then we must allocate a
5432 different sort of symbol. */
5433 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5434 {
5435 if (child_die->tag == DW_TAG_template_type_param
5436 || child_die->tag == DW_TAG_template_value_param)
5437 {
5438 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5439 struct template_symbol);
5440 templ_func->base.is_cplus_template_function = 1;
5441 break;
5442 }
5443 }
5444
c906108c 5445 new = push_context (0, lowpc);
34eaf542
TT
5446 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5447 (struct symbol *) templ_func);
4c2df51b 5448
4cecd739
DJ
5449 /* If there is a location expression for DW_AT_frame_base, record
5450 it. */
e142c38c 5451 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5452 if (attr)
c034e007
AC
5453 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5454 expression is being recorded directly in the function's symbol
5455 and not in a separate frame-base object. I guess this hack is
5456 to avoid adding some sort of frame-base adjunct/annex to the
5457 function's symbol :-(. The problem with doing this is that it
5458 results in a function symbol with a location expression that
5459 has nothing to do with the location of the function, ouch! The
5460 relationship should be: a function's symbol has-a frame base; a
5461 frame-base has-a location expression. */
e7c27a73 5462 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5463
e142c38c 5464 cu->list_in_scope = &local_symbols;
c906108c 5465
639d11d3 5466 if (die->child != NULL)
c906108c 5467 {
639d11d3 5468 child_die = die->child;
c906108c
SS
5469 while (child_die && child_die->tag)
5470 {
34eaf542
TT
5471 if (child_die->tag == DW_TAG_template_type_param
5472 || child_die->tag == DW_TAG_template_value_param)
5473 {
5474 struct symbol *arg = new_symbol (child_die, NULL, cu);
5475
5476 VEC_safe_push (symbolp, template_args, arg);
5477 }
5478 else
5479 process_die (child_die, cu);
c906108c
SS
5480 child_die = sibling_die (child_die);
5481 }
5482 }
5483
d389af10
JK
5484 inherit_abstract_dies (die, cu);
5485
4a811a97
UW
5486 /* If we have a DW_AT_specification, we might need to import using
5487 directives from the context of the specification DIE. See the
5488 comment in determine_prefix. */
5489 if (cu->language == language_cplus
5490 && dwarf2_attr (die, DW_AT_specification, cu))
5491 {
5492 struct dwarf2_cu *spec_cu = cu;
5493 struct die_info *spec_die = die_specification (die, &spec_cu);
5494
5495 while (spec_die)
5496 {
5497 child_die = spec_die->child;
5498 while (child_die && child_die->tag)
5499 {
5500 if (child_die->tag == DW_TAG_imported_module)
5501 process_die (child_die, spec_cu);
5502 child_die = sibling_die (child_die);
5503 }
5504
5505 /* In some cases, GCC generates specification DIEs that
5506 themselves contain DW_AT_specification attributes. */
5507 spec_die = die_specification (spec_die, &spec_cu);
5508 }
5509 }
5510
c906108c
SS
5511 new = pop_context ();
5512 /* Make a block for the local symbols within. */
801e3a5b
JB
5513 block = finish_block (new->name, &local_symbols, new->old_blocks,
5514 lowpc, highpc, objfile);
5515
df8a16a1 5516 /* For C++, set the block's scope. */
f55ee35c 5517 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5518 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5519 determine_prefix (die, cu),
df8a16a1
DJ
5520 processing_has_namespace_info);
5521
801e3a5b
JB
5522 /* If we have address ranges, record them. */
5523 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5524
34eaf542
TT
5525 /* Attach template arguments to function. */
5526 if (! VEC_empty (symbolp, template_args))
5527 {
5528 gdb_assert (templ_func != NULL);
5529
5530 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5531 templ_func->template_arguments
5532 = obstack_alloc (&objfile->objfile_obstack,
5533 (templ_func->n_template_arguments
5534 * sizeof (struct symbol *)));
5535 memcpy (templ_func->template_arguments,
5536 VEC_address (symbolp, template_args),
5537 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5538 VEC_free (symbolp, template_args);
5539 }
5540
208d8187
JB
5541 /* In C++, we can have functions nested inside functions (e.g., when
5542 a function declares a class that has methods). This means that
5543 when we finish processing a function scope, we may need to go
5544 back to building a containing block's symbol lists. */
5545 local_symbols = new->locals;
5546 param_symbols = new->params;
27aa8d6a 5547 using_directives = new->using_directives;
208d8187 5548
921e78cf
JB
5549 /* If we've finished processing a top-level function, subsequent
5550 symbols go in the file symbol list. */
5551 if (outermost_context_p ())
e142c38c 5552 cu->list_in_scope = &file_symbols;
c906108c
SS
5553}
5554
5555/* Process all the DIES contained within a lexical block scope. Start
5556 a new scope, process the dies, and then close the scope. */
5557
5558static void
e7c27a73 5559read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5560{
e7c27a73 5561 struct objfile *objfile = cu->objfile;
52f0bd74 5562 struct context_stack *new;
c906108c
SS
5563 CORE_ADDR lowpc, highpc;
5564 struct die_info *child_die;
e142c38c
DJ
5565 CORE_ADDR baseaddr;
5566
5567 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5568
5569 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5570 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5571 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5572 be nasty. Might be easier to properly extend generic blocks to
af34e669 5573 describe ranges. */
d85a05f0 5574 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5575 return;
5576 lowpc += baseaddr;
5577 highpc += baseaddr;
5578
5579 push_context (0, lowpc);
639d11d3 5580 if (die->child != NULL)
c906108c 5581 {
639d11d3 5582 child_die = die->child;
c906108c
SS
5583 while (child_die && child_die->tag)
5584 {
e7c27a73 5585 process_die (child_die, cu);
c906108c
SS
5586 child_die = sibling_die (child_die);
5587 }
5588 }
5589 new = pop_context ();
5590
8540c487 5591 if (local_symbols != NULL || using_directives != NULL)
c906108c 5592 {
801e3a5b
JB
5593 struct block *block
5594 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5595 highpc, objfile);
5596
5597 /* Note that recording ranges after traversing children, as we
5598 do here, means that recording a parent's ranges entails
5599 walking across all its children's ranges as they appear in
5600 the address map, which is quadratic behavior.
5601
5602 It would be nicer to record the parent's ranges before
5603 traversing its children, simply overriding whatever you find
5604 there. But since we don't even decide whether to create a
5605 block until after we've traversed its children, that's hard
5606 to do. */
5607 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5608 }
5609 local_symbols = new->locals;
27aa8d6a 5610 using_directives = new->using_directives;
c906108c
SS
5611}
5612
43039443 5613/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5614 Return 1 if the attributes are present and valid, otherwise, return 0.
5615 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5616
5617static int
5618dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5619 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5620 struct partial_symtab *ranges_pst)
43039443
JK
5621{
5622 struct objfile *objfile = cu->objfile;
5623 struct comp_unit_head *cu_header = &cu->header;
5624 bfd *obfd = objfile->obfd;
5625 unsigned int addr_size = cu_header->addr_size;
5626 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5627 /* Base address selection entry. */
5628 CORE_ADDR base;
5629 int found_base;
5630 unsigned int dummy;
5631 gdb_byte *buffer;
5632 CORE_ADDR marker;
5633 int low_set;
5634 CORE_ADDR low = 0;
5635 CORE_ADDR high = 0;
ff013f42 5636 CORE_ADDR baseaddr;
43039443 5637
d00adf39
DE
5638 found_base = cu->base_known;
5639 base = cu->base_address;
43039443 5640
be391dca 5641 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5642 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5643 {
5644 complaint (&symfile_complaints,
5645 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5646 offset);
5647 return 0;
5648 }
dce234bc 5649 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5650
5651 /* Read in the largest possible address. */
5652 marker = read_address (obfd, buffer, cu, &dummy);
5653 if ((marker & mask) == mask)
5654 {
5655 /* If we found the largest possible address, then
5656 read the base address. */
5657 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5658 buffer += 2 * addr_size;
5659 offset += 2 * addr_size;
5660 found_base = 1;
5661 }
5662
5663 low_set = 0;
5664
e7030f15 5665 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5666
43039443
JK
5667 while (1)
5668 {
5669 CORE_ADDR range_beginning, range_end;
5670
5671 range_beginning = read_address (obfd, buffer, cu, &dummy);
5672 buffer += addr_size;
5673 range_end = read_address (obfd, buffer, cu, &dummy);
5674 buffer += addr_size;
5675 offset += 2 * addr_size;
5676
5677 /* An end of list marker is a pair of zero addresses. */
5678 if (range_beginning == 0 && range_end == 0)
5679 /* Found the end of list entry. */
5680 break;
5681
5682 /* Each base address selection entry is a pair of 2 values.
5683 The first is the largest possible address, the second is
5684 the base address. Check for a base address here. */
5685 if ((range_beginning & mask) == mask)
5686 {
5687 /* If we found the largest possible address, then
5688 read the base address. */
5689 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5690 found_base = 1;
5691 continue;
5692 }
5693
5694 if (!found_base)
5695 {
5696 /* We have no valid base address for the ranges
5697 data. */
5698 complaint (&symfile_complaints,
5699 _("Invalid .debug_ranges data (no base address)"));
5700 return 0;
5701 }
5702
5703 range_beginning += base;
5704 range_end += base;
5705
ff013f42
JK
5706 if (ranges_pst != NULL && range_beginning < range_end)
5707 addrmap_set_empty (objfile->psymtabs_addrmap,
5708 range_beginning + baseaddr, range_end - 1 + baseaddr,
5709 ranges_pst);
5710
43039443
JK
5711 /* FIXME: This is recording everything as a low-high
5712 segment of consecutive addresses. We should have a
5713 data structure for discontiguous block ranges
5714 instead. */
5715 if (! low_set)
5716 {
5717 low = range_beginning;
5718 high = range_end;
5719 low_set = 1;
5720 }
5721 else
5722 {
5723 if (range_beginning < low)
5724 low = range_beginning;
5725 if (range_end > high)
5726 high = range_end;
5727 }
5728 }
5729
5730 if (! low_set)
5731 /* If the first entry is an end-of-list marker, the range
5732 describes an empty scope, i.e. no instructions. */
5733 return 0;
5734
5735 if (low_return)
5736 *low_return = low;
5737 if (high_return)
5738 *high_return = high;
5739 return 1;
5740}
5741
af34e669
DJ
5742/* Get low and high pc attributes from a die. Return 1 if the attributes
5743 are present and valid, otherwise, return 0. Return -1 if the range is
5744 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5745static int
af34e669 5746dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5747 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5748 struct partial_symtab *pst)
c906108c
SS
5749{
5750 struct attribute *attr;
af34e669
DJ
5751 CORE_ADDR low = 0;
5752 CORE_ADDR high = 0;
5753 int ret = 0;
c906108c 5754
e142c38c 5755 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5756 if (attr)
af34e669
DJ
5757 {
5758 high = DW_ADDR (attr);
e142c38c 5759 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5760 if (attr)
5761 low = DW_ADDR (attr);
5762 else
5763 /* Found high w/o low attribute. */
5764 return 0;
5765
5766 /* Found consecutive range of addresses. */
5767 ret = 1;
5768 }
c906108c 5769 else
af34e669 5770 {
e142c38c 5771 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5772 if (attr != NULL)
5773 {
af34e669 5774 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5775 .debug_ranges section. */
d85a05f0 5776 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5777 return 0;
43039443 5778 /* Found discontinuous range of addresses. */
af34e669
DJ
5779 ret = -1;
5780 }
5781 }
c906108c
SS
5782
5783 if (high < low)
5784 return 0;
5785
5786 /* When using the GNU linker, .gnu.linkonce. sections are used to
5787 eliminate duplicate copies of functions and vtables and such.
5788 The linker will arbitrarily choose one and discard the others.
5789 The AT_*_pc values for such functions refer to local labels in
5790 these sections. If the section from that file was discarded, the
5791 labels are not in the output, so the relocs get a value of 0.
5792 If this is a discarded function, mark the pc bounds as invalid,
5793 so that GDB will ignore it. */
72dca2f5 5794 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5795 return 0;
5796
5797 *lowpc = low;
5798 *highpc = high;
af34e669 5799 return ret;
c906108c
SS
5800}
5801
b084d499
JB
5802/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5803 its low and high PC addresses. Do nothing if these addresses could not
5804 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5805 and HIGHPC to the high address if greater than HIGHPC. */
5806
5807static void
5808dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5809 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5810 struct dwarf2_cu *cu)
5811{
5812 CORE_ADDR low, high;
5813 struct die_info *child = die->child;
5814
d85a05f0 5815 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5816 {
5817 *lowpc = min (*lowpc, low);
5818 *highpc = max (*highpc, high);
5819 }
5820
5821 /* If the language does not allow nested subprograms (either inside
5822 subprograms or lexical blocks), we're done. */
5823 if (cu->language != language_ada)
5824 return;
6e70227d 5825
b084d499
JB
5826 /* Check all the children of the given DIE. If it contains nested
5827 subprograms, then check their pc bounds. Likewise, we need to
5828 check lexical blocks as well, as they may also contain subprogram
5829 definitions. */
5830 while (child && child->tag)
5831 {
5832 if (child->tag == DW_TAG_subprogram
5833 || child->tag == DW_TAG_lexical_block)
5834 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5835 child = sibling_die (child);
5836 }
5837}
5838
fae299cd
DC
5839/* Get the low and high pc's represented by the scope DIE, and store
5840 them in *LOWPC and *HIGHPC. If the correct values can't be
5841 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5842
5843static void
5844get_scope_pc_bounds (struct die_info *die,
5845 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5846 struct dwarf2_cu *cu)
5847{
5848 CORE_ADDR best_low = (CORE_ADDR) -1;
5849 CORE_ADDR best_high = (CORE_ADDR) 0;
5850 CORE_ADDR current_low, current_high;
5851
d85a05f0 5852 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5853 {
5854 best_low = current_low;
5855 best_high = current_high;
5856 }
5857 else
5858 {
5859 struct die_info *child = die->child;
5860
5861 while (child && child->tag)
5862 {
5863 switch (child->tag) {
5864 case DW_TAG_subprogram:
b084d499 5865 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5866 break;
5867 case DW_TAG_namespace:
f55ee35c 5868 case DW_TAG_module:
fae299cd
DC
5869 /* FIXME: carlton/2004-01-16: Should we do this for
5870 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5871 that current GCC's always emit the DIEs corresponding
5872 to definitions of methods of classes as children of a
5873 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5874 the DIEs giving the declarations, which could be
5875 anywhere). But I don't see any reason why the
5876 standards says that they have to be there. */
5877 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5878
5879 if (current_low != ((CORE_ADDR) -1))
5880 {
5881 best_low = min (best_low, current_low);
5882 best_high = max (best_high, current_high);
5883 }
5884 break;
5885 default:
5886 /* Ignore. */
5887 break;
5888 }
5889
5890 child = sibling_die (child);
5891 }
5892 }
5893
5894 *lowpc = best_low;
5895 *highpc = best_high;
5896}
5897
801e3a5b
JB
5898/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5899 in DIE. */
5900static void
5901dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5902 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5903{
5904 struct attribute *attr;
5905
5906 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5907 if (attr)
5908 {
5909 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5910
801e3a5b
JB
5911 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5912 if (attr)
5913 {
5914 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5915
801e3a5b
JB
5916 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5917 }
5918 }
5919
5920 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5921 if (attr)
5922 {
5923 bfd *obfd = cu->objfile->obfd;
5924
5925 /* The value of the DW_AT_ranges attribute is the offset of the
5926 address range list in the .debug_ranges section. */
5927 unsigned long offset = DW_UNSND (attr);
dce234bc 5928 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5929
5930 /* For some target architectures, but not others, the
5931 read_address function sign-extends the addresses it returns.
5932 To recognize base address selection entries, we need a
5933 mask. */
5934 unsigned int addr_size = cu->header.addr_size;
5935 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5936
5937 /* The base address, to which the next pair is relative. Note
5938 that this 'base' is a DWARF concept: most entries in a range
5939 list are relative, to reduce the number of relocs against the
5940 debugging information. This is separate from this function's
5941 'baseaddr' argument, which GDB uses to relocate debugging
5942 information from a shared library based on the address at
5943 which the library was loaded. */
d00adf39
DE
5944 CORE_ADDR base = cu->base_address;
5945 int base_known = cu->base_known;
801e3a5b 5946
be391dca 5947 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5948 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5949 {
5950 complaint (&symfile_complaints,
5951 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5952 offset);
5953 return;
5954 }
5955
5956 for (;;)
5957 {
5958 unsigned int bytes_read;
5959 CORE_ADDR start, end;
5960
5961 start = read_address (obfd, buffer, cu, &bytes_read);
5962 buffer += bytes_read;
5963 end = read_address (obfd, buffer, cu, &bytes_read);
5964 buffer += bytes_read;
5965
5966 /* Did we find the end of the range list? */
5967 if (start == 0 && end == 0)
5968 break;
5969
5970 /* Did we find a base address selection entry? */
5971 else if ((start & base_select_mask) == base_select_mask)
5972 {
5973 base = end;
5974 base_known = 1;
5975 }
5976
5977 /* We found an ordinary address range. */
5978 else
5979 {
5980 if (!base_known)
5981 {
5982 complaint (&symfile_complaints,
5983 _("Invalid .debug_ranges data (no base address)"));
5984 return;
5985 }
5986
6e70227d
DE
5987 record_block_range (block,
5988 baseaddr + base + start,
801e3a5b
JB
5989 baseaddr + base + end - 1);
5990 }
5991 }
5992 }
5993}
5994
c906108c
SS
5995/* Add an aggregate field to the field list. */
5996
5997static void
107d2387 5998dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 5999 struct dwarf2_cu *cu)
6e70227d 6000{
e7c27a73 6001 struct objfile *objfile = cu->objfile;
5e2b427d 6002 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6003 struct nextfield *new_field;
6004 struct attribute *attr;
6005 struct field *fp;
6006 char *fieldname = "";
6007
6008 /* Allocate a new field list entry and link it in. */
6009 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6010 make_cleanup (xfree, new_field);
c906108c 6011 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6012
6013 if (die->tag == DW_TAG_inheritance)
6014 {
6015 new_field->next = fip->baseclasses;
6016 fip->baseclasses = new_field;
6017 }
6018 else
6019 {
6020 new_field->next = fip->fields;
6021 fip->fields = new_field;
6022 }
c906108c
SS
6023 fip->nfields++;
6024
6025 /* Handle accessibility and virtuality of field.
6026 The default accessibility for members is public, the default
6027 accessibility for inheritance is private. */
6028 if (die->tag != DW_TAG_inheritance)
6029 new_field->accessibility = DW_ACCESS_public;
6030 else
6031 new_field->accessibility = DW_ACCESS_private;
6032 new_field->virtuality = DW_VIRTUALITY_none;
6033
e142c38c 6034 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6035 if (attr)
6036 new_field->accessibility = DW_UNSND (attr);
6037 if (new_field->accessibility != DW_ACCESS_public)
6038 fip->non_public_fields = 1;
e142c38c 6039 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6040 if (attr)
6041 new_field->virtuality = DW_UNSND (attr);
6042
6043 fp = &new_field->field;
a9a9bd0f 6044
e142c38c 6045 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6046 {
a9a9bd0f 6047 /* Data member other than a C++ static data member. */
6e70227d 6048
c906108c 6049 /* Get type of field. */
e7c27a73 6050 fp->type = die_type (die, cu);
c906108c 6051
d6a843b5 6052 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6053
c906108c 6054 /* Get bit size of field (zero if none). */
e142c38c 6055 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6056 if (attr)
6057 {
6058 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6059 }
6060 else
6061 {
6062 FIELD_BITSIZE (*fp) = 0;
6063 }
6064
6065 /* Get bit offset of field. */
e142c38c 6066 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6067 if (attr)
6068 {
d4b96c9a 6069 int byte_offset = 0;
c6a0999f 6070
3690dd37 6071 if (attr_form_is_section_offset (attr))
d4b96c9a 6072 dwarf2_complex_location_expr_complaint ();
3690dd37 6073 else if (attr_form_is_constant (attr))
c6a0999f 6074 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6075 else if (attr_form_is_block (attr))
c6a0999f 6076 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6077 else
6078 dwarf2_complex_location_expr_complaint ();
c6a0999f 6079
d6a843b5 6080 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6081 }
e142c38c 6082 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6083 if (attr)
6084 {
5e2b427d 6085 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6086 {
6087 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6088 additional bit offset from the MSB of the containing
6089 anonymous object to the MSB of the field. We don't
6090 have to do anything special since we don't need to
6091 know the size of the anonymous object. */
c906108c
SS
6092 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6093 }
6094 else
6095 {
6096 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6097 MSB of the anonymous object, subtract off the number of
6098 bits from the MSB of the field to the MSB of the
6099 object, and then subtract off the number of bits of
6100 the field itself. The result is the bit offset of
6101 the LSB of the field. */
c906108c
SS
6102 int anonymous_size;
6103 int bit_offset = DW_UNSND (attr);
6104
e142c38c 6105 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6106 if (attr)
6107 {
6108 /* The size of the anonymous object containing
6109 the bit field is explicit, so use the
6110 indicated size (in bytes). */
6111 anonymous_size = DW_UNSND (attr);
6112 }
6113 else
6114 {
6115 /* The size of the anonymous object containing
6116 the bit field must be inferred from the type
6117 attribute of the data member containing the
6118 bit field. */
6119 anonymous_size = TYPE_LENGTH (fp->type);
6120 }
6121 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6122 - bit_offset - FIELD_BITSIZE (*fp);
6123 }
6124 }
6125
6126 /* Get name of field. */
39cbfefa
DJ
6127 fieldname = dwarf2_name (die, cu);
6128 if (fieldname == NULL)
6129 fieldname = "";
d8151005
DJ
6130
6131 /* The name is already allocated along with this objfile, so we don't
6132 need to duplicate it for the type. */
6133 fp->name = fieldname;
c906108c
SS
6134
6135 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6136 pointer or virtual base class pointer) to private. */
e142c38c 6137 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6138 {
d48cc9dd 6139 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6140 new_field->accessibility = DW_ACCESS_private;
6141 fip->non_public_fields = 1;
6142 }
6143 }
a9a9bd0f 6144 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6145 {
a9a9bd0f
DC
6146 /* C++ static member. */
6147
6148 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6149 is a declaration, but all versions of G++ as of this writing
6150 (so through at least 3.2.1) incorrectly generate
6151 DW_TAG_variable tags. */
6e70227d 6152
c906108c 6153 char *physname;
c906108c 6154
a9a9bd0f 6155 /* Get name of field. */
39cbfefa
DJ
6156 fieldname = dwarf2_name (die, cu);
6157 if (fieldname == NULL)
c906108c
SS
6158 return;
6159
254e6b9e 6160 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6161 if (attr
6162 /* Only create a symbol if this is an external value.
6163 new_symbol checks this and puts the value in the global symbol
6164 table, which we want. If it is not external, new_symbol
6165 will try to put the value in cu->list_in_scope which is wrong. */
6166 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6167 {
6168 /* A static const member, not much different than an enum as far as
6169 we're concerned, except that we can support more types. */
6170 new_symbol (die, NULL, cu);
6171 }
6172
2df3850c 6173 /* Get physical name. */
94af9270 6174 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6175
d8151005
DJ
6176 /* The name is already allocated along with this objfile, so we don't
6177 need to duplicate it for the type. */
6178 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6179 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6180 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6181 }
6182 else if (die->tag == DW_TAG_inheritance)
6183 {
6184 /* C++ base class field. */
e142c38c 6185 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6186 if (attr)
d4b96c9a
JK
6187 {
6188 int byte_offset = 0;
6189
6190 if (attr_form_is_section_offset (attr))
6191 dwarf2_complex_location_expr_complaint ();
6192 else if (attr_form_is_constant (attr))
6193 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6194 else if (attr_form_is_block (attr))
6195 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6196 else
6197 dwarf2_complex_location_expr_complaint ();
6198
6199 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6200 }
c906108c 6201 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6202 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6203 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6204 fip->nbaseclasses++;
6205 }
6206}
6207
98751a41
JK
6208/* Add a typedef defined in the scope of the FIP's class. */
6209
6210static void
6211dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6212 struct dwarf2_cu *cu)
6e70227d 6213{
98751a41
JK
6214 struct objfile *objfile = cu->objfile;
6215 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6216 struct typedef_field_list *new_field;
6217 struct attribute *attr;
6218 struct typedef_field *fp;
6219 char *fieldname = "";
6220
6221 /* Allocate a new field list entry and link it in. */
6222 new_field = xzalloc (sizeof (*new_field));
6223 make_cleanup (xfree, new_field);
6224
6225 gdb_assert (die->tag == DW_TAG_typedef);
6226
6227 fp = &new_field->field;
6228
6229 /* Get name of field. */
6230 fp->name = dwarf2_name (die, cu);
6231 if (fp->name == NULL)
6232 return;
6233
6234 fp->type = read_type_die (die, cu);
6235
6236 new_field->next = fip->typedef_field_list;
6237 fip->typedef_field_list = new_field;
6238 fip->typedef_field_list_count++;
6239}
6240
c906108c
SS
6241/* Create the vector of fields, and attach it to the type. */
6242
6243static void
fba45db2 6244dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6245 struct dwarf2_cu *cu)
c906108c
SS
6246{
6247 int nfields = fip->nfields;
6248
6249 /* Record the field count, allocate space for the array of fields,
6250 and create blank accessibility bitfields if necessary. */
6251 TYPE_NFIELDS (type) = nfields;
6252 TYPE_FIELDS (type) = (struct field *)
6253 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6254 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6255
b4ba55a1 6256 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6257 {
6258 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6259
6260 TYPE_FIELD_PRIVATE_BITS (type) =
6261 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6262 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6263
6264 TYPE_FIELD_PROTECTED_BITS (type) =
6265 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6266 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6267
6268 TYPE_FIELD_IGNORE_BITS (type) =
6269 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6270 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6271 }
6272
6273 /* If the type has baseclasses, allocate and clear a bit vector for
6274 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6275 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6276 {
6277 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6278 unsigned char *pointer;
c906108c
SS
6279
6280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6281 pointer = TYPE_ALLOC (type, num_bytes);
6282 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6283 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6284 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6285 }
6286
6287 /* Copy the saved-up fields into the field vector. Start from the head
6288 of the list, adding to the tail of the field array, so that they end
6289 up in the same order in the array in which they were added to the list. */
6290 while (nfields-- > 0)
6291 {
7d0ccb61
DJ
6292 struct nextfield *fieldp;
6293
6294 if (fip->fields)
6295 {
6296 fieldp = fip->fields;
6297 fip->fields = fieldp->next;
6298 }
6299 else
6300 {
6301 fieldp = fip->baseclasses;
6302 fip->baseclasses = fieldp->next;
6303 }
6304
6305 TYPE_FIELD (type, nfields) = fieldp->field;
6306 switch (fieldp->accessibility)
c906108c 6307 {
c5aa993b 6308 case DW_ACCESS_private:
b4ba55a1
JB
6309 if (cu->language != language_ada)
6310 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6311 break;
c906108c 6312
c5aa993b 6313 case DW_ACCESS_protected:
b4ba55a1
JB
6314 if (cu->language != language_ada)
6315 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6316 break;
c906108c 6317
c5aa993b
JM
6318 case DW_ACCESS_public:
6319 break;
c906108c 6320
c5aa993b
JM
6321 default:
6322 /* Unknown accessibility. Complain and treat it as public. */
6323 {
e2e0b3e5 6324 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6325 fieldp->accessibility);
c5aa993b
JM
6326 }
6327 break;
c906108c
SS
6328 }
6329 if (nfields < fip->nbaseclasses)
6330 {
7d0ccb61 6331 switch (fieldp->virtuality)
c906108c 6332 {
c5aa993b
JM
6333 case DW_VIRTUALITY_virtual:
6334 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6335 if (cu->language == language_ada)
6336 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6337 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6338 break;
c906108c
SS
6339 }
6340 }
c906108c
SS
6341 }
6342}
6343
c906108c
SS
6344/* Add a member function to the proper fieldlist. */
6345
6346static void
107d2387 6347dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6348 struct type *type, struct dwarf2_cu *cu)
c906108c 6349{
e7c27a73 6350 struct objfile *objfile = cu->objfile;
c906108c
SS
6351 struct attribute *attr;
6352 struct fnfieldlist *flp;
6353 int i;
6354 struct fn_field *fnp;
6355 char *fieldname;
c906108c 6356 struct nextfnfield *new_fnfield;
f792889a 6357 struct type *this_type;
c906108c 6358
b4ba55a1
JB
6359 if (cu->language == language_ada)
6360 error ("unexpected member function in Ada type");
6361
2df3850c 6362 /* Get name of member function. */
39cbfefa
DJ
6363 fieldname = dwarf2_name (die, cu);
6364 if (fieldname == NULL)
2df3850c 6365 return;
c906108c 6366
c906108c
SS
6367 /* Look up member function name in fieldlist. */
6368 for (i = 0; i < fip->nfnfields; i++)
6369 {
27bfe10e 6370 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6371 break;
6372 }
6373
6374 /* Create new list element if necessary. */
6375 if (i < fip->nfnfields)
6376 flp = &fip->fnfieldlists[i];
6377 else
6378 {
6379 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6380 {
6381 fip->fnfieldlists = (struct fnfieldlist *)
6382 xrealloc (fip->fnfieldlists,
6383 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6384 * sizeof (struct fnfieldlist));
c906108c 6385 if (fip->nfnfields == 0)
c13c43fd 6386 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6387 }
6388 flp = &fip->fnfieldlists[fip->nfnfields];
6389 flp->name = fieldname;
6390 flp->length = 0;
6391 flp->head = NULL;
3da10d80 6392 i = fip->nfnfields++;
c906108c
SS
6393 }
6394
6395 /* Create a new member function field and chain it to the field list
6396 entry. */
6397 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6398 make_cleanup (xfree, new_fnfield);
c906108c
SS
6399 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6400 new_fnfield->next = flp->head;
6401 flp->head = new_fnfield;
6402 flp->length++;
6403
6404 /* Fill in the member function field info. */
6405 fnp = &new_fnfield->fnfield;
3da10d80
KS
6406
6407 /* Delay processing of the physname until later. */
6408 if (cu->language == language_cplus || cu->language == language_java)
6409 {
6410 add_to_method_list (type, i, flp->length - 1, fieldname,
6411 die, cu);
6412 }
6413 else
6414 {
6415 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6416 fnp->physname = physname ? physname : "";
6417 }
6418
c906108c 6419 fnp->type = alloc_type (objfile);
f792889a
DJ
6420 this_type = read_type_die (die, cu);
6421 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6422 {
f792889a 6423 int nparams = TYPE_NFIELDS (this_type);
c906108c 6424
f792889a 6425 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6426 of the method itself (TYPE_CODE_METHOD). */
6427 smash_to_method_type (fnp->type, type,
f792889a
DJ
6428 TYPE_TARGET_TYPE (this_type),
6429 TYPE_FIELDS (this_type),
6430 TYPE_NFIELDS (this_type),
6431 TYPE_VARARGS (this_type));
c906108c
SS
6432
6433 /* Handle static member functions.
c5aa993b
JM
6434 Dwarf2 has no clean way to discern C++ static and non-static
6435 member functions. G++ helps GDB by marking the first
6436 parameter for non-static member functions (which is the
6437 this pointer) as artificial. We obtain this information
6438 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6439 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6440 fnp->voffset = VOFFSET_STATIC;
6441 }
6442 else
e2e0b3e5 6443 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6444 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6445
6446 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6447 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6448 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6449
6450 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6451 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6452
6453 /* Get accessibility. */
e142c38c 6454 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6455 if (attr)
6456 {
6457 switch (DW_UNSND (attr))
6458 {
c5aa993b
JM
6459 case DW_ACCESS_private:
6460 fnp->is_private = 1;
6461 break;
6462 case DW_ACCESS_protected:
6463 fnp->is_protected = 1;
6464 break;
c906108c
SS
6465 }
6466 }
6467
b02dede2 6468 /* Check for artificial methods. */
e142c38c 6469 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6470 if (attr && DW_UNSND (attr) != 0)
6471 fnp->is_artificial = 1;
6472
0d564a31 6473 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6474 function. For older versions of GCC, this is an offset in the
6475 appropriate virtual table, as specified by DW_AT_containing_type.
6476 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6477 to the object address. */
6478
e142c38c 6479 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6480 if (attr)
8e19ed76 6481 {
aec5aa8b 6482 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6483 {
aec5aa8b
TT
6484 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6485 {
6486 /* Old-style GCC. */
6487 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6488 }
6489 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6490 || (DW_BLOCK (attr)->size > 1
6491 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6492 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6493 {
6494 struct dwarf_block blk;
6495 int offset;
6496
6497 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6498 ? 1 : 2);
6499 blk.size = DW_BLOCK (attr)->size - offset;
6500 blk.data = DW_BLOCK (attr)->data + offset;
6501 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6502 if ((fnp->voffset % cu->header.addr_size) != 0)
6503 dwarf2_complex_location_expr_complaint ();
6504 else
6505 fnp->voffset /= cu->header.addr_size;
6506 fnp->voffset += 2;
6507 }
6508 else
6509 dwarf2_complex_location_expr_complaint ();
6510
6511 if (!fnp->fcontext)
6512 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6513 }
3690dd37 6514 else if (attr_form_is_section_offset (attr))
8e19ed76 6515 {
4d3c2250 6516 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6517 }
6518 else
6519 {
4d3c2250
KB
6520 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6521 fieldname);
8e19ed76 6522 }
0d564a31 6523 }
d48cc9dd
DJ
6524 else
6525 {
6526 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6527 if (attr && DW_UNSND (attr))
6528 {
6529 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6530 complaint (&symfile_complaints,
6531 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6532 fieldname, die->offset);
9655fd1a 6533 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6534 TYPE_CPLUS_DYNAMIC (type) = 1;
6535 }
6536 }
c906108c
SS
6537}
6538
6539/* Create the vector of member function fields, and attach it to the type. */
6540
6541static void
fba45db2 6542dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6543 struct dwarf2_cu *cu)
c906108c
SS
6544{
6545 struct fnfieldlist *flp;
6546 int total_length = 0;
6547 int i;
6548
b4ba55a1
JB
6549 if (cu->language == language_ada)
6550 error ("unexpected member functions in Ada type");
6551
c906108c
SS
6552 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6553 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6554 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6555
6556 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6557 {
6558 struct nextfnfield *nfp = flp->head;
6559 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6560 int k;
6561
6562 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6563 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6564 fn_flp->fn_fields = (struct fn_field *)
6565 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6566 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6567 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6568
6569 total_length += flp->length;
6570 }
6571
6572 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6573 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6574}
6575
1168df01
JB
6576/* Returns non-zero if NAME is the name of a vtable member in CU's
6577 language, zero otherwise. */
6578static int
6579is_vtable_name (const char *name, struct dwarf2_cu *cu)
6580{
6581 static const char vptr[] = "_vptr";
987504bb 6582 static const char vtable[] = "vtable";
1168df01 6583
987504bb
JJ
6584 /* Look for the C++ and Java forms of the vtable. */
6585 if ((cu->language == language_java
6586 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6587 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6588 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6589 return 1;
6590
6591 return 0;
6592}
6593
c0dd20ea 6594/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6595 functions, with the ABI-specified layout. If TYPE describes
6596 such a structure, smash it into a member function type.
61049d3b
DJ
6597
6598 GCC shouldn't do this; it should just output pointer to member DIEs.
6599 This is GCC PR debug/28767. */
c0dd20ea 6600
0b92b5bb
TT
6601static void
6602quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6603{
0b92b5bb 6604 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6605
6606 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6607 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6608 return;
c0dd20ea
DJ
6609
6610 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6611 if (TYPE_FIELD_NAME (type, 0) == NULL
6612 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6613 || TYPE_FIELD_NAME (type, 1) == NULL
6614 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6615 return;
c0dd20ea
DJ
6616
6617 /* Find the type of the method. */
0b92b5bb 6618 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6619 if (pfn_type == NULL
6620 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6621 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6622 return;
c0dd20ea
DJ
6623
6624 /* Look for the "this" argument. */
6625 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6626 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6627 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6628 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6629 return;
c0dd20ea
DJ
6630
6631 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6632 new_type = alloc_type (objfile);
6633 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6634 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6635 TYPE_VARARGS (pfn_type));
0b92b5bb 6636 smash_to_methodptr_type (type, new_type);
c0dd20ea 6637}
1168df01 6638
c906108c
SS
6639/* Called when we find the DIE that starts a structure or union scope
6640 (definition) to process all dies that define the members of the
6641 structure or union.
6642
6643 NOTE: we need to call struct_type regardless of whether or not the
6644 DIE has an at_name attribute, since it might be an anonymous
6645 structure or union. This gets the type entered into our set of
6646 user defined types.
6647
6648 However, if the structure is incomplete (an opaque struct/union)
6649 then suppress creating a symbol table entry for it since gdb only
6650 wants to find the one with the complete definition. Note that if
6651 it is complete, we just call new_symbol, which does it's own
6652 checking about whether the struct/union is anonymous or not (and
6653 suppresses creating a symbol table entry itself). */
6654
f792889a 6655static struct type *
134d01f1 6656read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6657{
e7c27a73 6658 struct objfile *objfile = cu->objfile;
c906108c
SS
6659 struct type *type;
6660 struct attribute *attr;
39cbfefa 6661 char *name;
d3f41bb1 6662 struct cleanup *back_to;
c906108c 6663
348e048f
DE
6664 /* If the definition of this type lives in .debug_types, read that type.
6665 Don't follow DW_AT_specification though, that will take us back up
6666 the chain and we want to go down. */
6667 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6668 if (attr)
6669 {
6670 struct dwarf2_cu *type_cu = cu;
6671 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6672
348e048f
DE
6673 /* We could just recurse on read_structure_type, but we need to call
6674 get_die_type to ensure only one type for this DIE is created.
6675 This is important, for example, because for c++ classes we need
6676 TYPE_NAME set which is only done by new_symbol. Blech. */
6677 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6678
6679 /* TYPE_CU may not be the same as CU.
6680 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6681 return set_die_type (die, type, cu);
6682 }
6683
d3f41bb1
TT
6684 back_to = make_cleanup (null_cleanup, 0);
6685
c0dd20ea 6686 type = alloc_type (objfile);
c906108c 6687 INIT_CPLUS_SPECIFIC (type);
93311388 6688
39cbfefa
DJ
6689 name = dwarf2_name (die, cu);
6690 if (name != NULL)
c906108c 6691 {
987504bb
JJ
6692 if (cu->language == language_cplus
6693 || cu->language == language_java)
63d06c5c 6694 {
3da10d80
KS
6695 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6696
6697 /* dwarf2_full_name might have already finished building the DIE's
6698 type. If so, there is no need to continue. */
6699 if (get_die_type (die, cu) != NULL)
6700 return get_die_type (die, cu);
6701
6702 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6703 if (die->tag == DW_TAG_structure_type
6704 || die->tag == DW_TAG_class_type)
6705 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6706 }
6707 else
6708 {
d8151005
DJ
6709 /* The name is already allocated along with this objfile, so
6710 we don't need to duplicate it for the type. */
94af9270
KS
6711 TYPE_TAG_NAME (type) = (char *) name;
6712 if (die->tag == DW_TAG_class_type)
6713 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6714 }
c906108c
SS
6715 }
6716
6717 if (die->tag == DW_TAG_structure_type)
6718 {
6719 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6720 }
6721 else if (die->tag == DW_TAG_union_type)
6722 {
6723 TYPE_CODE (type) = TYPE_CODE_UNION;
6724 }
6725 else
6726 {
c906108c
SS
6727 TYPE_CODE (type) = TYPE_CODE_CLASS;
6728 }
6729
0cc2414c
TT
6730 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6731 TYPE_DECLARED_CLASS (type) = 1;
6732
e142c38c 6733 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6734 if (attr)
6735 {
6736 TYPE_LENGTH (type) = DW_UNSND (attr);
6737 }
6738 else
6739 {
6740 TYPE_LENGTH (type) = 0;
6741 }
6742
876cecd0 6743 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6744 if (die_is_declaration (die, cu))
876cecd0 6745 TYPE_STUB (type) = 1;
a6c727b2
DJ
6746 else if (attr == NULL && die->child == NULL
6747 && producer_is_realview (cu->producer))
6748 /* RealView does not output the required DW_AT_declaration
6749 on incomplete types. */
6750 TYPE_STUB (type) = 1;
dc718098 6751
c906108c
SS
6752 /* We need to add the type field to the die immediately so we don't
6753 infinitely recurse when dealing with pointers to the structure
6754 type within the structure itself. */
1c379e20 6755 set_die_type (die, type, cu);
c906108c 6756
7e314c57
JK
6757 /* set_die_type should be already done. */
6758 set_descriptive_type (type, die, cu);
6759
e142c38c 6760 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6761 {
6762 struct field_info fi;
6763 struct die_info *child_die;
34eaf542 6764 VEC (symbolp) *template_args = NULL;
c906108c
SS
6765
6766 memset (&fi, 0, sizeof (struct field_info));
6767
639d11d3 6768 child_die = die->child;
c906108c
SS
6769
6770 while (child_die && child_die->tag)
6771 {
a9a9bd0f
DC
6772 if (child_die->tag == DW_TAG_member
6773 || child_die->tag == DW_TAG_variable)
c906108c 6774 {
a9a9bd0f
DC
6775 /* NOTE: carlton/2002-11-05: A C++ static data member
6776 should be a DW_TAG_member that is a declaration, but
6777 all versions of G++ as of this writing (so through at
6778 least 3.2.1) incorrectly generate DW_TAG_variable
6779 tags for them instead. */
e7c27a73 6780 dwarf2_add_field (&fi, child_die, cu);
c906108c 6781 }
8713b1b1 6782 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6783 {
6784 /* C++ member function. */
e7c27a73 6785 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6786 }
6787 else if (child_die->tag == DW_TAG_inheritance)
6788 {
6789 /* C++ base class field. */
e7c27a73 6790 dwarf2_add_field (&fi, child_die, cu);
c906108c 6791 }
98751a41
JK
6792 else if (child_die->tag == DW_TAG_typedef)
6793 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6794 else if (child_die->tag == DW_TAG_template_type_param
6795 || child_die->tag == DW_TAG_template_value_param)
6796 {
6797 struct symbol *arg = new_symbol (child_die, NULL, cu);
6798
6799 VEC_safe_push (symbolp, template_args, arg);
6800 }
6801
c906108c
SS
6802 child_die = sibling_die (child_die);
6803 }
6804
34eaf542
TT
6805 /* Attach template arguments to type. */
6806 if (! VEC_empty (symbolp, template_args))
6807 {
6808 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6809 TYPE_N_TEMPLATE_ARGUMENTS (type)
6810 = VEC_length (symbolp, template_args);
6811 TYPE_TEMPLATE_ARGUMENTS (type)
6812 = obstack_alloc (&objfile->objfile_obstack,
6813 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6814 * sizeof (struct symbol *)));
6815 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6816 VEC_address (symbolp, template_args),
6817 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6818 * sizeof (struct symbol *)));
6819 VEC_free (symbolp, template_args);
6820 }
6821
c906108c
SS
6822 /* Attach fields and member functions to the type. */
6823 if (fi.nfields)
e7c27a73 6824 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6825 if (fi.nfnfields)
6826 {
e7c27a73 6827 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6828
c5aa993b 6829 /* Get the type which refers to the base class (possibly this
c906108c 6830 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6831 class from the DW_AT_containing_type attribute. This use of
6832 DW_AT_containing_type is a GNU extension. */
c906108c 6833
e142c38c 6834 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6835 {
e7c27a73 6836 struct type *t = die_containing_type (die, cu);
c906108c
SS
6837
6838 TYPE_VPTR_BASETYPE (type) = t;
6839 if (type == t)
6840 {
c906108c
SS
6841 int i;
6842
6843 /* Our own class provides vtbl ptr. */
6844 for (i = TYPE_NFIELDS (t) - 1;
6845 i >= TYPE_N_BASECLASSES (t);
6846 --i)
6847 {
6848 char *fieldname = TYPE_FIELD_NAME (t, i);
6849
1168df01 6850 if (is_vtable_name (fieldname, cu))
c906108c
SS
6851 {
6852 TYPE_VPTR_FIELDNO (type) = i;
6853 break;
6854 }
6855 }
6856
6857 /* Complain if virtual function table field not found. */
6858 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6859 complaint (&symfile_complaints,
e2e0b3e5 6860 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6861 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6862 "");
c906108c
SS
6863 }
6864 else
6865 {
6866 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6867 }
6868 }
f6235d4c
EZ
6869 else if (cu->producer
6870 && strncmp (cu->producer,
6871 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6872 {
6873 /* The IBM XLC compiler does not provide direct indication
6874 of the containing type, but the vtable pointer is
6875 always named __vfp. */
6876
6877 int i;
6878
6879 for (i = TYPE_NFIELDS (type) - 1;
6880 i >= TYPE_N_BASECLASSES (type);
6881 --i)
6882 {
6883 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6884 {
6885 TYPE_VPTR_FIELDNO (type) = i;
6886 TYPE_VPTR_BASETYPE (type) = type;
6887 break;
6888 }
6889 }
6890 }
c906108c 6891 }
98751a41
JK
6892
6893 /* Copy fi.typedef_field_list linked list elements content into the
6894 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6895 if (fi.typedef_field_list)
6896 {
6897 int i = fi.typedef_field_list_count;
6898
a0d7a4ff 6899 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6900 TYPE_TYPEDEF_FIELD_ARRAY (type)
6901 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6902 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6903
6904 /* Reverse the list order to keep the debug info elements order. */
6905 while (--i >= 0)
6906 {
6907 struct typedef_field *dest, *src;
6e70227d 6908
98751a41
JK
6909 dest = &TYPE_TYPEDEF_FIELD (type, i);
6910 src = &fi.typedef_field_list->field;
6911 fi.typedef_field_list = fi.typedef_field_list->next;
6912 *dest = *src;
6913 }
6914 }
c906108c 6915 }
63d06c5c 6916
0b92b5bb
TT
6917 quirk_gcc_member_function_pointer (type, cu->objfile);
6918
0114d602 6919 do_cleanups (back_to);
f792889a 6920 return type;
c906108c
SS
6921}
6922
134d01f1
DJ
6923static void
6924process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6925{
90aeadfc 6926 struct die_info *child_die = die->child;
f792889a 6927 struct type *this_type;
c906108c 6928
f792889a
DJ
6929 this_type = get_die_type (die, cu);
6930 if (this_type == NULL)
6931 this_type = read_structure_type (die, cu);
c906108c 6932
90aeadfc
DC
6933 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6934 snapshots) has been known to create a die giving a declaration
6935 for a class that has, as a child, a die giving a definition for a
6936 nested class. So we have to process our children even if the
6937 current die is a declaration. Normally, of course, a declaration
6938 won't have any children at all. */
134d01f1 6939
90aeadfc
DC
6940 while (child_die != NULL && child_die->tag)
6941 {
6942 if (child_die->tag == DW_TAG_member
6943 || child_die->tag == DW_TAG_variable
34eaf542
TT
6944 || child_die->tag == DW_TAG_inheritance
6945 || child_die->tag == DW_TAG_template_value_param
6946 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6947 {
90aeadfc 6948 /* Do nothing. */
134d01f1 6949 }
90aeadfc
DC
6950 else
6951 process_die (child_die, cu);
134d01f1 6952
90aeadfc 6953 child_die = sibling_die (child_die);
134d01f1
DJ
6954 }
6955
fa4028e9
JB
6956 /* Do not consider external references. According to the DWARF standard,
6957 these DIEs are identified by the fact that they have no byte_size
6958 attribute, and a declaration attribute. */
6959 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6960 || !die_is_declaration (die, cu))
f792889a 6961 new_symbol (die, this_type, cu);
134d01f1
DJ
6962}
6963
6964/* Given a DW_AT_enumeration_type die, set its type. We do not
6965 complete the type's fields yet, or create any symbols. */
c906108c 6966
f792889a 6967static struct type *
134d01f1 6968read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6969{
e7c27a73 6970 struct objfile *objfile = cu->objfile;
c906108c 6971 struct type *type;
c906108c 6972 struct attribute *attr;
0114d602 6973 const char *name;
134d01f1 6974
348e048f
DE
6975 /* If the definition of this type lives in .debug_types, read that type.
6976 Don't follow DW_AT_specification though, that will take us back up
6977 the chain and we want to go down. */
6978 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6979 if (attr)
6980 {
6981 struct dwarf2_cu *type_cu = cu;
6982 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6983
348e048f 6984 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6985
6986 /* TYPE_CU may not be the same as CU.
6987 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6988 return set_die_type (die, type, cu);
6989 }
6990
c906108c
SS
6991 type = alloc_type (objfile);
6992
6993 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6994 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6995 if (name != NULL)
0114d602 6996 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6997
e142c38c 6998 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6999 if (attr)
7000 {
7001 TYPE_LENGTH (type) = DW_UNSND (attr);
7002 }
7003 else
7004 {
7005 TYPE_LENGTH (type) = 0;
7006 }
7007
137033e9
JB
7008 /* The enumeration DIE can be incomplete. In Ada, any type can be
7009 declared as private in the package spec, and then defined only
7010 inside the package body. Such types are known as Taft Amendment
7011 Types. When another package uses such a type, an incomplete DIE
7012 may be generated by the compiler. */
02eb380e 7013 if (die_is_declaration (die, cu))
876cecd0 7014 TYPE_STUB (type) = 1;
02eb380e 7015
f792889a 7016 return set_die_type (die, type, cu);
134d01f1
DJ
7017}
7018
7019/* Given a pointer to a die which begins an enumeration, process all
7020 the dies that define the members of the enumeration, and create the
7021 symbol for the enumeration type.
7022
7023 NOTE: We reverse the order of the element list. */
7024
7025static void
7026process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7027{
f792889a 7028 struct type *this_type;
134d01f1 7029
f792889a
DJ
7030 this_type = get_die_type (die, cu);
7031 if (this_type == NULL)
7032 this_type = read_enumeration_type (die, cu);
9dc481d3 7033
639d11d3 7034 if (die->child != NULL)
c906108c 7035 {
9dc481d3
DE
7036 struct die_info *child_die;
7037 struct symbol *sym;
7038 struct field *fields = NULL;
7039 int num_fields = 0;
7040 int unsigned_enum = 1;
7041 char *name;
7042
639d11d3 7043 child_die = die->child;
c906108c
SS
7044 while (child_die && child_die->tag)
7045 {
7046 if (child_die->tag != DW_TAG_enumerator)
7047 {
e7c27a73 7048 process_die (child_die, cu);
c906108c
SS
7049 }
7050 else
7051 {
39cbfefa
DJ
7052 name = dwarf2_name (child_die, cu);
7053 if (name)
c906108c 7054 {
f792889a 7055 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7056 if (SYMBOL_VALUE (sym) < 0)
7057 unsigned_enum = 0;
7058
7059 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7060 {
7061 fields = (struct field *)
7062 xrealloc (fields,
7063 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7064 * sizeof (struct field));
c906108c
SS
7065 }
7066
3567439c 7067 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7068 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7069 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7070 FIELD_BITSIZE (fields[num_fields]) = 0;
7071
7072 num_fields++;
7073 }
7074 }
7075
7076 child_die = sibling_die (child_die);
7077 }
7078
7079 if (num_fields)
7080 {
f792889a
DJ
7081 TYPE_NFIELDS (this_type) = num_fields;
7082 TYPE_FIELDS (this_type) = (struct field *)
7083 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7084 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7085 sizeof (struct field) * num_fields);
b8c9b27d 7086 xfree (fields);
c906108c
SS
7087 }
7088 if (unsigned_enum)
876cecd0 7089 TYPE_UNSIGNED (this_type) = 1;
c906108c 7090 }
134d01f1 7091
f792889a 7092 new_symbol (die, this_type, cu);
c906108c
SS
7093}
7094
7095/* Extract all information from a DW_TAG_array_type DIE and put it in
7096 the DIE's type field. For now, this only handles one dimensional
7097 arrays. */
7098
f792889a 7099static struct type *
e7c27a73 7100read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7101{
e7c27a73 7102 struct objfile *objfile = cu->objfile;
c906108c 7103 struct die_info *child_die;
7e314c57 7104 struct type *type;
c906108c
SS
7105 struct type *element_type, *range_type, *index_type;
7106 struct type **range_types = NULL;
7107 struct attribute *attr;
7108 int ndim = 0;
7109 struct cleanup *back_to;
39cbfefa 7110 char *name;
c906108c 7111
e7c27a73 7112 element_type = die_type (die, cu);
c906108c 7113
7e314c57
JK
7114 /* The die_type call above may have already set the type for this DIE. */
7115 type = get_die_type (die, cu);
7116 if (type)
7117 return type;
7118
c906108c
SS
7119 /* Irix 6.2 native cc creates array types without children for
7120 arrays with unspecified length. */
639d11d3 7121 if (die->child == NULL)
c906108c 7122 {
46bf5051 7123 index_type = objfile_type (objfile)->builtin_int;
c906108c 7124 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7125 type = create_array_type (NULL, element_type, range_type);
7126 return set_die_type (die, type, cu);
c906108c
SS
7127 }
7128
7129 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7130 child_die = die->child;
c906108c
SS
7131 while (child_die && child_die->tag)
7132 {
7133 if (child_die->tag == DW_TAG_subrange_type)
7134 {
f792889a 7135 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7136
f792889a 7137 if (child_type != NULL)
a02abb62
JB
7138 {
7139 /* The range type was succesfully read. Save it for
7140 the array type creation. */
7141 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7142 {
7143 range_types = (struct type **)
7144 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7145 * sizeof (struct type *));
7146 if (ndim == 0)
7147 make_cleanup (free_current_contents, &range_types);
7148 }
f792889a 7149 range_types[ndim++] = child_type;
a02abb62 7150 }
c906108c
SS
7151 }
7152 child_die = sibling_die (child_die);
7153 }
7154
7155 /* Dwarf2 dimensions are output from left to right, create the
7156 necessary array types in backwards order. */
7ca2d3a3 7157
c906108c 7158 type = element_type;
7ca2d3a3
DL
7159
7160 if (read_array_order (die, cu) == DW_ORD_col_major)
7161 {
7162 int i = 0;
9a619af0 7163
7ca2d3a3
DL
7164 while (i < ndim)
7165 type = create_array_type (NULL, type, range_types[i++]);
7166 }
7167 else
7168 {
7169 while (ndim-- > 0)
7170 type = create_array_type (NULL, type, range_types[ndim]);
7171 }
c906108c 7172
f5f8a009
EZ
7173 /* Understand Dwarf2 support for vector types (like they occur on
7174 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7175 array type. This is not part of the Dwarf2/3 standard yet, but a
7176 custom vendor extension. The main difference between a regular
7177 array and the vector variant is that vectors are passed by value
7178 to functions. */
e142c38c 7179 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7180 if (attr)
ea37ba09 7181 make_vector_type (type);
f5f8a009 7182
39cbfefa
DJ
7183 name = dwarf2_name (die, cu);
7184 if (name)
7185 TYPE_NAME (type) = name;
6e70227d 7186
7e314c57
JK
7187 /* Install the type in the die. */
7188 set_die_type (die, type, cu);
7189
7190 /* set_die_type should be already done. */
b4ba55a1
JB
7191 set_descriptive_type (type, die, cu);
7192
c906108c
SS
7193 do_cleanups (back_to);
7194
7e314c57 7195 return type;
c906108c
SS
7196}
7197
7ca2d3a3 7198static enum dwarf_array_dim_ordering
6e70227d 7199read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7200{
7201 struct attribute *attr;
7202
7203 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7204
7205 if (attr) return DW_SND (attr);
7206
7207 /*
7208 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7209 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7210 laid out as per normal fortran.
7211
6e70227d 7212 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7213 version checking.
7214 */
7215
905e0470
PM
7216 if (cu->language == language_fortran
7217 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7218 {
7219 return DW_ORD_row_major;
7220 }
7221
6e70227d 7222 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7223 {
7224 case array_column_major:
7225 return DW_ORD_col_major;
7226 case array_row_major:
7227 default:
7228 return DW_ORD_row_major;
7229 };
7230}
7231
72019c9c
GM
7232/* Extract all information from a DW_TAG_set_type DIE and put it in
7233 the DIE's type field. */
7234
f792889a 7235static struct type *
72019c9c
GM
7236read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7237{
7e314c57
JK
7238 struct type *domain_type, *set_type;
7239 struct attribute *attr;
f792889a 7240
7e314c57
JK
7241 domain_type = die_type (die, cu);
7242
7243 /* The die_type call above may have already set the type for this DIE. */
7244 set_type = get_die_type (die, cu);
7245 if (set_type)
7246 return set_type;
7247
7248 set_type = create_set_type (NULL, domain_type);
7249
7250 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7251 if (attr)
7252 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7253
f792889a 7254 return set_die_type (die, set_type, cu);
72019c9c 7255}
7ca2d3a3 7256
c906108c
SS
7257/* First cut: install each common block member as a global variable. */
7258
7259static void
e7c27a73 7260read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7261{
7262 struct die_info *child_die;
7263 struct attribute *attr;
7264 struct symbol *sym;
7265 CORE_ADDR base = (CORE_ADDR) 0;
7266
e142c38c 7267 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7268 if (attr)
7269 {
8e19ed76
PS
7270 /* Support the .debug_loc offsets */
7271 if (attr_form_is_block (attr))
7272 {
e7c27a73 7273 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7274 }
3690dd37 7275 else if (attr_form_is_section_offset (attr))
8e19ed76 7276 {
4d3c2250 7277 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7278 }
7279 else
7280 {
4d3c2250
KB
7281 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7282 "common block member");
8e19ed76 7283 }
c906108c 7284 }
639d11d3 7285 if (die->child != NULL)
c906108c 7286 {
639d11d3 7287 child_die = die->child;
c906108c
SS
7288 while (child_die && child_die->tag)
7289 {
e7c27a73 7290 sym = new_symbol (child_die, NULL, cu);
e142c38c 7291 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
7292 if (attr)
7293 {
d4b96c9a
JK
7294 CORE_ADDR byte_offset = 0;
7295
7296 if (attr_form_is_section_offset (attr))
7297 dwarf2_complex_location_expr_complaint ();
7298 else if (attr_form_is_constant (attr))
7299 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7300 else if (attr_form_is_block (attr))
7301 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7302 else
7303 dwarf2_complex_location_expr_complaint ();
7304
7305 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7306 add_symbol_to_list (sym, &global_symbols);
7307 }
7308 child_die = sibling_die (child_die);
7309 }
7310 }
7311}
7312
0114d602 7313/* Create a type for a C++ namespace. */
d9fa45fe 7314
0114d602
DJ
7315static struct type *
7316read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7317{
e7c27a73 7318 struct objfile *objfile = cu->objfile;
0114d602 7319 const char *previous_prefix, *name;
9219021c 7320 int is_anonymous;
0114d602
DJ
7321 struct type *type;
7322
7323 /* For extensions, reuse the type of the original namespace. */
7324 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7325 {
7326 struct die_info *ext_die;
7327 struct dwarf2_cu *ext_cu = cu;
9a619af0 7328
0114d602
DJ
7329 ext_die = dwarf2_extension (die, &ext_cu);
7330 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7331
7332 /* EXT_CU may not be the same as CU.
7333 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7334 return set_die_type (die, type, cu);
7335 }
9219021c 7336
e142c38c 7337 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7338
7339 /* Now build the name of the current namespace. */
7340
0114d602
DJ
7341 previous_prefix = determine_prefix (die, cu);
7342 if (previous_prefix[0] != '\0')
7343 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7344 previous_prefix, name, 0, cu);
0114d602
DJ
7345
7346 /* Create the type. */
7347 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7348 objfile);
7349 TYPE_NAME (type) = (char *) name;
7350 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7351
60531b24 7352 return set_die_type (die, type, cu);
0114d602
DJ
7353}
7354
7355/* Read a C++ namespace. */
7356
7357static void
7358read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7359{
7360 struct objfile *objfile = cu->objfile;
7361 const char *name;
7362 int is_anonymous;
9219021c 7363
5c4e30ca
DC
7364 /* Add a symbol associated to this if we haven't seen the namespace
7365 before. Also, add a using directive if it's an anonymous
7366 namespace. */
9219021c 7367
f2f0e013 7368 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7369 {
7370 struct type *type;
7371
0114d602 7372 type = read_type_die (die, cu);
e7c27a73 7373 new_symbol (die, type, cu);
5c4e30ca 7374
0114d602 7375 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7376 if (is_anonymous)
0114d602
DJ
7377 {
7378 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7379
c0cc3a76 7380 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7381 NULL, &objfile->objfile_obstack);
0114d602 7382 }
5c4e30ca 7383 }
9219021c 7384
639d11d3 7385 if (die->child != NULL)
d9fa45fe 7386 {
639d11d3 7387 struct die_info *child_die = die->child;
6e70227d 7388
d9fa45fe
DC
7389 while (child_die && child_die->tag)
7390 {
e7c27a73 7391 process_die (child_die, cu);
d9fa45fe
DC
7392 child_die = sibling_die (child_die);
7393 }
7394 }
38d518c9
EZ
7395}
7396
f55ee35c
JK
7397/* Read a Fortran module as type. This DIE can be only a declaration used for
7398 imported module. Still we need that type as local Fortran "use ... only"
7399 declaration imports depend on the created type in determine_prefix. */
7400
7401static struct type *
7402read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7403{
7404 struct objfile *objfile = cu->objfile;
7405 char *module_name;
7406 struct type *type;
7407
7408 module_name = dwarf2_name (die, cu);
7409 if (!module_name)
7410 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7411 die->offset);
7412 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7413
7414 /* determine_prefix uses TYPE_TAG_NAME. */
7415 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7416
7417 return set_die_type (die, type, cu);
7418}
7419
5d7cb8df
JK
7420/* Read a Fortran module. */
7421
7422static void
7423read_module (struct die_info *die, struct dwarf2_cu *cu)
7424{
7425 struct die_info *child_die = die->child;
7426
5d7cb8df
JK
7427 while (child_die && child_die->tag)
7428 {
7429 process_die (child_die, cu);
7430 child_die = sibling_die (child_die);
7431 }
7432}
7433
38d518c9
EZ
7434/* Return the name of the namespace represented by DIE. Set
7435 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7436 namespace. */
7437
7438static const char *
e142c38c 7439namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7440{
7441 struct die_info *current_die;
7442 const char *name = NULL;
7443
7444 /* Loop through the extensions until we find a name. */
7445
7446 for (current_die = die;
7447 current_die != NULL;
f2f0e013 7448 current_die = dwarf2_extension (die, &cu))
38d518c9 7449 {
e142c38c 7450 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7451 if (name != NULL)
7452 break;
7453 }
7454
7455 /* Is it an anonymous namespace? */
7456
7457 *is_anonymous = (name == NULL);
7458 if (*is_anonymous)
7459 name = "(anonymous namespace)";
7460
7461 return name;
d9fa45fe
DC
7462}
7463
c906108c
SS
7464/* Extract all information from a DW_TAG_pointer_type DIE and add to
7465 the user defined type vector. */
7466
f792889a 7467static struct type *
e7c27a73 7468read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7469{
5e2b427d 7470 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7471 struct comp_unit_head *cu_header = &cu->header;
c906108c 7472 struct type *type;
8b2dbe47
KB
7473 struct attribute *attr_byte_size;
7474 struct attribute *attr_address_class;
7475 int byte_size, addr_class;
7e314c57
JK
7476 struct type *target_type;
7477
7478 target_type = die_type (die, cu);
c906108c 7479
7e314c57
JK
7480 /* The die_type call above may have already set the type for this DIE. */
7481 type = get_die_type (die, cu);
7482 if (type)
7483 return type;
7484
7485 type = lookup_pointer_type (target_type);
8b2dbe47 7486
e142c38c 7487 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7488 if (attr_byte_size)
7489 byte_size = DW_UNSND (attr_byte_size);
c906108c 7490 else
8b2dbe47
KB
7491 byte_size = cu_header->addr_size;
7492
e142c38c 7493 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7494 if (attr_address_class)
7495 addr_class = DW_UNSND (attr_address_class);
7496 else
7497 addr_class = DW_ADDR_none;
7498
7499 /* If the pointer size or address class is different than the
7500 default, create a type variant marked as such and set the
7501 length accordingly. */
7502 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7503 {
5e2b427d 7504 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7505 {
7506 int type_flags;
7507
849957d9 7508 type_flags = gdbarch_address_class_type_flags
5e2b427d 7509 (gdbarch, byte_size, addr_class);
876cecd0
TT
7510 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7511 == 0);
8b2dbe47
KB
7512 type = make_type_with_address_space (type, type_flags);
7513 }
7514 else if (TYPE_LENGTH (type) != byte_size)
7515 {
e2e0b3e5 7516 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7517 }
6e70227d 7518 else
9a619af0
MS
7519 {
7520 /* Should we also complain about unhandled address classes? */
7521 }
c906108c 7522 }
8b2dbe47
KB
7523
7524 TYPE_LENGTH (type) = byte_size;
f792889a 7525 return set_die_type (die, type, cu);
c906108c
SS
7526}
7527
7528/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7529 the user defined type vector. */
7530
f792889a 7531static struct type *
e7c27a73 7532read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7533{
7534 struct type *type;
7535 struct type *to_type;
7536 struct type *domain;
7537
e7c27a73
DJ
7538 to_type = die_type (die, cu);
7539 domain = die_containing_type (die, cu);
0d5de010 7540
7e314c57
JK
7541 /* The calls above may have already set the type for this DIE. */
7542 type = get_die_type (die, cu);
7543 if (type)
7544 return type;
7545
0d5de010
DJ
7546 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7547 type = lookup_methodptr_type (to_type);
7548 else
7549 type = lookup_memberptr_type (to_type, domain);
c906108c 7550
f792889a 7551 return set_die_type (die, type, cu);
c906108c
SS
7552}
7553
7554/* Extract all information from a DW_TAG_reference_type DIE and add to
7555 the user defined type vector. */
7556
f792889a 7557static struct type *
e7c27a73 7558read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7559{
e7c27a73 7560 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7561 struct type *type, *target_type;
c906108c
SS
7562 struct attribute *attr;
7563
7e314c57
JK
7564 target_type = die_type (die, cu);
7565
7566 /* The die_type call above may have already set the type for this DIE. */
7567 type = get_die_type (die, cu);
7568 if (type)
7569 return type;
7570
7571 type = lookup_reference_type (target_type);
e142c38c 7572 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7573 if (attr)
7574 {
7575 TYPE_LENGTH (type) = DW_UNSND (attr);
7576 }
7577 else
7578 {
107d2387 7579 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7580 }
f792889a 7581 return set_die_type (die, type, cu);
c906108c
SS
7582}
7583
f792889a 7584static struct type *
e7c27a73 7585read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7586{
f792889a 7587 struct type *base_type, *cv_type;
c906108c 7588
e7c27a73 7589 base_type = die_type (die, cu);
7e314c57
JK
7590
7591 /* The die_type call above may have already set the type for this DIE. */
7592 cv_type = get_die_type (die, cu);
7593 if (cv_type)
7594 return cv_type;
7595
f792889a
DJ
7596 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7597 return set_die_type (die, cv_type, cu);
c906108c
SS
7598}
7599
f792889a 7600static struct type *
e7c27a73 7601read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7602{
f792889a 7603 struct type *base_type, *cv_type;
c906108c 7604
e7c27a73 7605 base_type = die_type (die, cu);
7e314c57
JK
7606
7607 /* The die_type call above may have already set the type for this DIE. */
7608 cv_type = get_die_type (die, cu);
7609 if (cv_type)
7610 return cv_type;
7611
f792889a
DJ
7612 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7613 return set_die_type (die, cv_type, cu);
c906108c
SS
7614}
7615
7616/* Extract all information from a DW_TAG_string_type DIE and add to
7617 the user defined type vector. It isn't really a user defined type,
7618 but it behaves like one, with other DIE's using an AT_user_def_type
7619 attribute to reference it. */
7620
f792889a 7621static struct type *
e7c27a73 7622read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7623{
e7c27a73 7624 struct objfile *objfile = cu->objfile;
3b7538c0 7625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7626 struct type *type, *range_type, *index_type, *char_type;
7627 struct attribute *attr;
7628 unsigned int length;
7629
e142c38c 7630 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7631 if (attr)
7632 {
7633 length = DW_UNSND (attr);
7634 }
7635 else
7636 {
b21b22e0 7637 /* check for the DW_AT_byte_size attribute */
e142c38c 7638 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7639 if (attr)
7640 {
7641 length = DW_UNSND (attr);
7642 }
7643 else
7644 {
7645 length = 1;
7646 }
c906108c 7647 }
6ccb9162 7648
46bf5051 7649 index_type = objfile_type (objfile)->builtin_int;
c906108c 7650 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7651 char_type = language_string_char_type (cu->language_defn, gdbarch);
7652 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7653
f792889a 7654 return set_die_type (die, type, cu);
c906108c
SS
7655}
7656
7657/* Handle DIES due to C code like:
7658
7659 struct foo
c5aa993b
JM
7660 {
7661 int (*funcp)(int a, long l);
7662 int b;
7663 };
c906108c
SS
7664
7665 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7666 */
c906108c 7667
f792889a 7668static struct type *
e7c27a73 7669read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7670{
7671 struct type *type; /* Type that this function returns */
7672 struct type *ftype; /* Function that returns above type */
7673 struct attribute *attr;
7674
e7c27a73 7675 type = die_type (die, cu);
7e314c57
JK
7676
7677 /* The die_type call above may have already set the type for this DIE. */
7678 ftype = get_die_type (die, cu);
7679 if (ftype)
7680 return ftype;
7681
0c8b41f1 7682 ftype = lookup_function_type (type);
c906108c 7683
5b8101ae 7684 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7685 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7686 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7687 || cu->language == language_cplus
5b8101ae
PM
7688 || cu->language == language_java
7689 || cu->language == language_pascal)
876cecd0 7690 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7691 else if (producer_is_realview (cu->producer))
7692 /* RealView does not emit DW_AT_prototyped. We can not
7693 distinguish prototyped and unprototyped functions; default to
7694 prototyped, since that is more common in modern code (and
7695 RealView warns about unprototyped functions). */
7696 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7697
c055b101
CV
7698 /* Store the calling convention in the type if it's available in
7699 the subroutine die. Otherwise set the calling convention to
7700 the default value DW_CC_normal. */
7701 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7702 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7703
7704 /* We need to add the subroutine type to the die immediately so
7705 we don't infinitely recurse when dealing with parameters
7706 declared as the same subroutine type. */
7707 set_die_type (die, ftype, cu);
6e70227d 7708
639d11d3 7709 if (die->child != NULL)
c906108c 7710 {
8072405b 7711 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7712 struct die_info *child_die;
8072405b 7713 int nparams, iparams;
c906108c
SS
7714
7715 /* Count the number of parameters.
7716 FIXME: GDB currently ignores vararg functions, but knows about
7717 vararg member functions. */
8072405b 7718 nparams = 0;
639d11d3 7719 child_die = die->child;
c906108c
SS
7720 while (child_die && child_die->tag)
7721 {
7722 if (child_die->tag == DW_TAG_formal_parameter)
7723 nparams++;
7724 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7725 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7726 child_die = sibling_die (child_die);
7727 }
7728
7729 /* Allocate storage for parameters and fill them in. */
7730 TYPE_NFIELDS (ftype) = nparams;
7731 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7732 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7733
8072405b
JK
7734 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7735 even if we error out during the parameters reading below. */
7736 for (iparams = 0; iparams < nparams; iparams++)
7737 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7738
7739 iparams = 0;
639d11d3 7740 child_die = die->child;
c906108c
SS
7741 while (child_die && child_die->tag)
7742 {
7743 if (child_die->tag == DW_TAG_formal_parameter)
7744 {
3ce3b1ba
PA
7745 struct type *arg_type;
7746
7747 /* DWARF version 2 has no clean way to discern C++
7748 static and non-static member functions. G++ helps
7749 GDB by marking the first parameter for non-static
7750 member functions (which is the this pointer) as
7751 artificial. We pass this information to
7752 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7753
7754 DWARF version 3 added DW_AT_object_pointer, which GCC
7755 4.5 does not yet generate. */
e142c38c 7756 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7757 if (attr)
7758 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7759 else
418835cc
KS
7760 {
7761 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7762
7763 /* GCC/43521: In java, the formal parameter
7764 "this" is sometimes not marked with DW_AT_artificial. */
7765 if (cu->language == language_java)
7766 {
7767 const char *name = dwarf2_name (child_die, cu);
9a619af0 7768
418835cc
KS
7769 if (name && !strcmp (name, "this"))
7770 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7771 }
7772 }
3ce3b1ba
PA
7773 arg_type = die_type (child_die, cu);
7774
7775 /* RealView does not mark THIS as const, which the testsuite
7776 expects. GCC marks THIS as const in method definitions,
7777 but not in the class specifications (GCC PR 43053). */
7778 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7779 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7780 {
7781 int is_this = 0;
7782 struct dwarf2_cu *arg_cu = cu;
7783 const char *name = dwarf2_name (child_die, cu);
7784
7785 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7786 if (attr)
7787 {
7788 /* If the compiler emits this, use it. */
7789 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7790 is_this = 1;
7791 }
7792 else if (name && strcmp (name, "this") == 0)
7793 /* Function definitions will have the argument names. */
7794 is_this = 1;
7795 else if (name == NULL && iparams == 0)
7796 /* Declarations may not have the names, so like
7797 elsewhere in GDB, assume an artificial first
7798 argument is "this". */
7799 is_this = 1;
7800
7801 if (is_this)
7802 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7803 arg_type, 0);
7804 }
7805
7806 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7807 iparams++;
7808 }
7809 child_die = sibling_die (child_die);
7810 }
7811 }
7812
76c10ea2 7813 return ftype;
c906108c
SS
7814}
7815
f792889a 7816static struct type *
e7c27a73 7817read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7818{
e7c27a73 7819 struct objfile *objfile = cu->objfile;
0114d602 7820 const char *name = NULL;
f792889a 7821 struct type *this_type;
c906108c 7822
94af9270 7823 name = dwarf2_full_name (NULL, die, cu);
f792889a 7824 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7825 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7826 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7827 set_die_type (die, this_type, cu);
7828 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7829 return this_type;
c906108c
SS
7830}
7831
7832/* Find a representation of a given base type and install
7833 it in the TYPE field of the die. */
7834
f792889a 7835static struct type *
e7c27a73 7836read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7837{
e7c27a73 7838 struct objfile *objfile = cu->objfile;
c906108c
SS
7839 struct type *type;
7840 struct attribute *attr;
7841 int encoding = 0, size = 0;
39cbfefa 7842 char *name;
6ccb9162
UW
7843 enum type_code code = TYPE_CODE_INT;
7844 int type_flags = 0;
7845 struct type *target_type = NULL;
c906108c 7846
e142c38c 7847 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7848 if (attr)
7849 {
7850 encoding = DW_UNSND (attr);
7851 }
e142c38c 7852 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7853 if (attr)
7854 {
7855 size = DW_UNSND (attr);
7856 }
39cbfefa 7857 name = dwarf2_name (die, cu);
6ccb9162 7858 if (!name)
c906108c 7859 {
6ccb9162
UW
7860 complaint (&symfile_complaints,
7861 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7862 }
6ccb9162
UW
7863
7864 switch (encoding)
c906108c 7865 {
6ccb9162
UW
7866 case DW_ATE_address:
7867 /* Turn DW_ATE_address into a void * pointer. */
7868 code = TYPE_CODE_PTR;
7869 type_flags |= TYPE_FLAG_UNSIGNED;
7870 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7871 break;
7872 case DW_ATE_boolean:
7873 code = TYPE_CODE_BOOL;
7874 type_flags |= TYPE_FLAG_UNSIGNED;
7875 break;
7876 case DW_ATE_complex_float:
7877 code = TYPE_CODE_COMPLEX;
7878 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7879 break;
7880 case DW_ATE_decimal_float:
7881 code = TYPE_CODE_DECFLOAT;
7882 break;
7883 case DW_ATE_float:
7884 code = TYPE_CODE_FLT;
7885 break;
7886 case DW_ATE_signed:
7887 break;
7888 case DW_ATE_unsigned:
7889 type_flags |= TYPE_FLAG_UNSIGNED;
7890 break;
7891 case DW_ATE_signed_char:
6e70227d 7892 if (cu->language == language_ada || cu->language == language_m2
868a0084 7893 || cu->language == language_pascal)
6ccb9162
UW
7894 code = TYPE_CODE_CHAR;
7895 break;
7896 case DW_ATE_unsigned_char:
868a0084
PM
7897 if (cu->language == language_ada || cu->language == language_m2
7898 || cu->language == language_pascal)
6ccb9162
UW
7899 code = TYPE_CODE_CHAR;
7900 type_flags |= TYPE_FLAG_UNSIGNED;
7901 break;
75079b2b
TT
7902 case DW_ATE_UTF:
7903 /* We just treat this as an integer and then recognize the
7904 type by name elsewhere. */
7905 break;
7906
6ccb9162
UW
7907 default:
7908 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7909 dwarf_type_encoding_name (encoding));
7910 break;
c906108c 7911 }
6ccb9162 7912
0114d602
DJ
7913 type = init_type (code, size, type_flags, NULL, objfile);
7914 TYPE_NAME (type) = name;
6ccb9162
UW
7915 TYPE_TARGET_TYPE (type) = target_type;
7916
0114d602 7917 if (name && strcmp (name, "char") == 0)
876cecd0 7918 TYPE_NOSIGN (type) = 1;
0114d602 7919
f792889a 7920 return set_die_type (die, type, cu);
c906108c
SS
7921}
7922
a02abb62
JB
7923/* Read the given DW_AT_subrange DIE. */
7924
f792889a 7925static struct type *
a02abb62
JB
7926read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7927{
5e2b427d 7928 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7929 struct type *base_type;
7930 struct type *range_type;
7931 struct attribute *attr;
43bbcdc2
PH
7932 LONGEST low = 0;
7933 LONGEST high = -1;
39cbfefa 7934 char *name;
43bbcdc2 7935 LONGEST negative_mask;
e77813c8 7936
a02abb62 7937 base_type = die_type (die, cu);
953ac07e
JK
7938 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7939 check_typedef (base_type);
a02abb62 7940
7e314c57
JK
7941 /* The die_type call above may have already set the type for this DIE. */
7942 range_type = get_die_type (die, cu);
7943 if (range_type)
7944 return range_type;
7945
e142c38c 7946 if (cu->language == language_fortran)
6e70227d 7947 {
a02abb62
JB
7948 /* FORTRAN implies a lower bound of 1, if not given. */
7949 low = 1;
7950 }
7951
dd5e6932
DJ
7952 /* FIXME: For variable sized arrays either of these could be
7953 a variable rather than a constant value. We'll allow it,
7954 but we don't know how to handle it. */
e142c38c 7955 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7956 if (attr)
7957 low = dwarf2_get_attr_constant_value (attr, 0);
7958
e142c38c 7959 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7960 if (attr)
6e70227d 7961 {
e77813c8 7962 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7963 {
7964 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7965 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7966 FIXME: GDB does not yet know how to handle dynamic
7967 arrays properly, treat them as arrays with unspecified
7968 length for now.
7969
7970 FIXME: jimb/2003-09-22: GDB does not really know
7971 how to handle arrays of unspecified length
7972 either; we just represent them as zero-length
7973 arrays. Choose an appropriate upper bound given
7974 the lower bound we've computed above. */
7975 high = low - 1;
7976 }
7977 else
7978 high = dwarf2_get_attr_constant_value (attr, 1);
7979 }
e77813c8
PM
7980 else
7981 {
7982 attr = dwarf2_attr (die, DW_AT_count, cu);
7983 if (attr)
7984 {
7985 int count = dwarf2_get_attr_constant_value (attr, 1);
7986 high = low + count - 1;
7987 }
7988 }
7989
7990 /* Dwarf-2 specifications explicitly allows to create subrange types
7991 without specifying a base type.
7992 In that case, the base type must be set to the type of
7993 the lower bound, upper bound or count, in that order, if any of these
7994 three attributes references an object that has a type.
7995 If no base type is found, the Dwarf-2 specifications say that
7996 a signed integer type of size equal to the size of an address should
7997 be used.
7998 For the following C code: `extern char gdb_int [];'
7999 GCC produces an empty range DIE.
8000 FIXME: muller/2010-05-28: Possible references to object for low bound,
8001 high bound or count are not yet handled by this code.
8002 */
8003 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8004 {
8005 struct objfile *objfile = cu->objfile;
8006 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8007 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8008 struct type *int_type = objfile_type (objfile)->builtin_int;
8009
8010 /* Test "int", "long int", and "long long int" objfile types,
8011 and select the first one having a size above or equal to the
8012 architecture address size. */
8013 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8014 base_type = int_type;
8015 else
8016 {
8017 int_type = objfile_type (objfile)->builtin_long;
8018 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8019 base_type = int_type;
8020 else
8021 {
8022 int_type = objfile_type (objfile)->builtin_long_long;
8023 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8024 base_type = int_type;
8025 }
8026 }
8027 }
a02abb62 8028
6e70227d 8029 negative_mask =
43bbcdc2
PH
8030 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8031 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8032 low |= negative_mask;
8033 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8034 high |= negative_mask;
8035
a02abb62
JB
8036 range_type = create_range_type (NULL, base_type, low, high);
8037
bbb0eef6
JK
8038 /* Mark arrays with dynamic length at least as an array of unspecified
8039 length. GDB could check the boundary but before it gets implemented at
8040 least allow accessing the array elements. */
8041 if (attr && attr->form == DW_FORM_block1)
8042 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8043
39cbfefa
DJ
8044 name = dwarf2_name (die, cu);
8045 if (name)
8046 TYPE_NAME (range_type) = name;
6e70227d 8047
e142c38c 8048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8049 if (attr)
8050 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8051
7e314c57
JK
8052 set_die_type (die, range_type, cu);
8053
8054 /* set_die_type should be already done. */
b4ba55a1
JB
8055 set_descriptive_type (range_type, die, cu);
8056
7e314c57 8057 return range_type;
a02abb62 8058}
6e70227d 8059
f792889a 8060static struct type *
81a17f79
JB
8061read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8062{
8063 struct type *type;
81a17f79 8064
81a17f79
JB
8065 /* For now, we only support the C meaning of an unspecified type: void. */
8066
0114d602
DJ
8067 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8068 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8069
f792889a 8070 return set_die_type (die, type, cu);
81a17f79 8071}
a02abb62 8072
51545339
DJ
8073/* Trivial hash function for die_info: the hash value of a DIE
8074 is its offset in .debug_info for this objfile. */
8075
8076static hashval_t
8077die_hash (const void *item)
8078{
8079 const struct die_info *die = item;
9a619af0 8080
51545339
DJ
8081 return die->offset;
8082}
8083
8084/* Trivial comparison function for die_info structures: two DIEs
8085 are equal if they have the same offset. */
8086
8087static int
8088die_eq (const void *item_lhs, const void *item_rhs)
8089{
8090 const struct die_info *die_lhs = item_lhs;
8091 const struct die_info *die_rhs = item_rhs;
9a619af0 8092
51545339
DJ
8093 return die_lhs->offset == die_rhs->offset;
8094}
8095
c906108c
SS
8096/* Read a whole compilation unit into a linked list of dies. */
8097
f9aca02d 8098static struct die_info *
93311388 8099read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8100{
93311388 8101 struct die_reader_specs reader_specs;
98bfdba5 8102 int read_abbrevs = 0;
1d9ec526 8103 struct cleanup *back_to = NULL;
98bfdba5
PA
8104 struct die_info *die;
8105
8106 if (cu->dwarf2_abbrevs == NULL)
8107 {
8108 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8109 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8110 read_abbrevs = 1;
8111 }
93311388 8112
348e048f 8113 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8114 cu->die_hash
8115 = htab_create_alloc_ex (cu->header.length / 12,
8116 die_hash,
8117 die_eq,
8118 NULL,
8119 &cu->comp_unit_obstack,
8120 hashtab_obstack_allocate,
8121 dummy_obstack_deallocate);
8122
93311388
DE
8123 init_cu_die_reader (&reader_specs, cu);
8124
98bfdba5
PA
8125 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8126
8127 if (read_abbrevs)
8128 do_cleanups (back_to);
8129
8130 return die;
639d11d3
DC
8131}
8132
d97bc12b
DE
8133/* Main entry point for reading a DIE and all children.
8134 Read the DIE and dump it if requested. */
8135
8136static struct die_info *
93311388
DE
8137read_die_and_children (const struct die_reader_specs *reader,
8138 gdb_byte *info_ptr,
d97bc12b
DE
8139 gdb_byte **new_info_ptr,
8140 struct die_info *parent)
8141{
93311388 8142 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8143 new_info_ptr, parent);
8144
8145 if (dwarf2_die_debug)
8146 {
348e048f
DE
8147 fprintf_unfiltered (gdb_stdlog,
8148 "\nRead die from %s of %s:\n",
8149 reader->buffer == dwarf2_per_objfile->info.buffer
8150 ? ".debug_info"
8151 : reader->buffer == dwarf2_per_objfile->types.buffer
8152 ? ".debug_types"
8153 : "unknown section",
8154 reader->abfd->filename);
d97bc12b
DE
8155 dump_die (result, dwarf2_die_debug);
8156 }
8157
8158 return result;
8159}
8160
639d11d3
DC
8161/* Read a single die and all its descendents. Set the die's sibling
8162 field to NULL; set other fields in the die correctly, and set all
8163 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8164 location of the info_ptr after reading all of those dies. PARENT
8165 is the parent of the die in question. */
8166
8167static struct die_info *
93311388
DE
8168read_die_and_children_1 (const struct die_reader_specs *reader,
8169 gdb_byte *info_ptr,
d97bc12b
DE
8170 gdb_byte **new_info_ptr,
8171 struct die_info *parent)
639d11d3
DC
8172{
8173 struct die_info *die;
fe1b8b76 8174 gdb_byte *cur_ptr;
639d11d3
DC
8175 int has_children;
8176
93311388 8177 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8178 if (die == NULL)
8179 {
8180 *new_info_ptr = cur_ptr;
8181 return NULL;
8182 }
93311388 8183 store_in_ref_table (die, reader->cu);
639d11d3
DC
8184
8185 if (has_children)
348e048f 8186 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8187 else
8188 {
8189 die->child = NULL;
8190 *new_info_ptr = cur_ptr;
8191 }
8192
8193 die->sibling = NULL;
8194 die->parent = parent;
8195 return die;
8196}
8197
8198/* Read a die, all of its descendents, and all of its siblings; set
8199 all of the fields of all of the dies correctly. Arguments are as
8200 in read_die_and_children. */
8201
8202static struct die_info *
93311388
DE
8203read_die_and_siblings (const struct die_reader_specs *reader,
8204 gdb_byte *info_ptr,
fe1b8b76 8205 gdb_byte **new_info_ptr,
639d11d3
DC
8206 struct die_info *parent)
8207{
8208 struct die_info *first_die, *last_sibling;
fe1b8b76 8209 gdb_byte *cur_ptr;
639d11d3 8210
c906108c 8211 cur_ptr = info_ptr;
639d11d3
DC
8212 first_die = last_sibling = NULL;
8213
8214 while (1)
c906108c 8215 {
639d11d3 8216 struct die_info *die
93311388 8217 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8218
1d325ec1 8219 if (die == NULL)
c906108c 8220 {
639d11d3
DC
8221 *new_info_ptr = cur_ptr;
8222 return first_die;
c906108c 8223 }
1d325ec1
DJ
8224
8225 if (!first_die)
8226 first_die = die;
c906108c 8227 else
1d325ec1
DJ
8228 last_sibling->sibling = die;
8229
8230 last_sibling = die;
c906108c 8231 }
c906108c
SS
8232}
8233
93311388
DE
8234/* Read the die from the .debug_info section buffer. Set DIEP to
8235 point to a newly allocated die with its information, except for its
8236 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8237 whether the die has children or not. */
8238
8239static gdb_byte *
8240read_full_die (const struct die_reader_specs *reader,
8241 struct die_info **diep, gdb_byte *info_ptr,
8242 int *has_children)
8243{
8244 unsigned int abbrev_number, bytes_read, i, offset;
8245 struct abbrev_info *abbrev;
8246 struct die_info *die;
8247 struct dwarf2_cu *cu = reader->cu;
8248 bfd *abfd = reader->abfd;
8249
8250 offset = info_ptr - reader->buffer;
8251 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8252 info_ptr += bytes_read;
8253 if (!abbrev_number)
8254 {
8255 *diep = NULL;
8256 *has_children = 0;
8257 return info_ptr;
8258 }
8259
8260 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8261 if (!abbrev)
348e048f
DE
8262 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8263 abbrev_number,
8264 bfd_get_filename (abfd));
8265
93311388
DE
8266 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8267 die->offset = offset;
8268 die->tag = abbrev->tag;
8269 die->abbrev = abbrev_number;
8270
8271 die->num_attrs = abbrev->num_attrs;
8272
8273 for (i = 0; i < abbrev->num_attrs; ++i)
8274 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8275 abfd, info_ptr, cu);
8276
8277 *diep = die;
8278 *has_children = abbrev->has_children;
8279 return info_ptr;
8280}
8281
c906108c
SS
8282/* In DWARF version 2, the description of the debugging information is
8283 stored in a separate .debug_abbrev section. Before we read any
8284 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8285 in a hash table. This function also sets flags in CU describing
8286 the data found in the abbrev table. */
c906108c
SS
8287
8288static void
e7c27a73 8289dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8290{
e7c27a73 8291 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8292 gdb_byte *abbrev_ptr;
c906108c
SS
8293 struct abbrev_info *cur_abbrev;
8294 unsigned int abbrev_number, bytes_read, abbrev_name;
8295 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8296 struct attr_abbrev *cur_attrs;
8297 unsigned int allocated_attrs;
c906108c 8298
57349743 8299 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8300 obstack_init (&cu->abbrev_obstack);
8301 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8302 (ABBREV_HASH_SIZE
8303 * sizeof (struct abbrev_info *)));
8304 memset (cu->dwarf2_abbrevs, 0,
8305 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8306
be391dca
TT
8307 dwarf2_read_section (dwarf2_per_objfile->objfile,
8308 &dwarf2_per_objfile->abbrev);
dce234bc 8309 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8310 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8311 abbrev_ptr += bytes_read;
8312
f3dd6933
DJ
8313 allocated_attrs = ATTR_ALLOC_CHUNK;
8314 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8315
c906108c
SS
8316 /* loop until we reach an abbrev number of 0 */
8317 while (abbrev_number)
8318 {
f3dd6933 8319 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8320
8321 /* read in abbrev header */
8322 cur_abbrev->number = abbrev_number;
8323 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8324 abbrev_ptr += bytes_read;
8325 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8326 abbrev_ptr += 1;
8327
72bf9492
DJ
8328 if (cur_abbrev->tag == DW_TAG_namespace)
8329 cu->has_namespace_info = 1;
8330
c906108c
SS
8331 /* now read in declarations */
8332 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8333 abbrev_ptr += bytes_read;
8334 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8335 abbrev_ptr += bytes_read;
8336 while (abbrev_name)
8337 {
f3dd6933 8338 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8339 {
f3dd6933
DJ
8340 allocated_attrs += ATTR_ALLOC_CHUNK;
8341 cur_attrs
8342 = xrealloc (cur_attrs, (allocated_attrs
8343 * sizeof (struct attr_abbrev)));
c906108c 8344 }
ae038cb0
DJ
8345
8346 /* Record whether this compilation unit might have
8347 inter-compilation-unit references. If we don't know what form
8348 this attribute will have, then it might potentially be a
8349 DW_FORM_ref_addr, so we conservatively expect inter-CU
8350 references. */
8351
8352 if (abbrev_form == DW_FORM_ref_addr
8353 || abbrev_form == DW_FORM_indirect)
8354 cu->has_form_ref_addr = 1;
8355
f3dd6933
DJ
8356 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8357 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8358 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8359 abbrev_ptr += bytes_read;
8360 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8361 abbrev_ptr += bytes_read;
8362 }
8363
f3dd6933
DJ
8364 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8365 (cur_abbrev->num_attrs
8366 * sizeof (struct attr_abbrev)));
8367 memcpy (cur_abbrev->attrs, cur_attrs,
8368 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8369
c906108c 8370 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8371 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8372 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8373
8374 /* Get next abbreviation.
8375 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8376 always properly terminated with an abbrev number of 0.
8377 Exit loop if we encounter an abbreviation which we have
8378 already read (which means we are about to read the abbreviations
8379 for the next compile unit) or if the end of the abbreviation
8380 table is reached. */
dce234bc
PP
8381 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8382 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8383 break;
8384 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8385 abbrev_ptr += bytes_read;
e7c27a73 8386 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8387 break;
8388 }
f3dd6933
DJ
8389
8390 xfree (cur_attrs);
c906108c
SS
8391}
8392
f3dd6933 8393/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8394
c906108c 8395static void
f3dd6933 8396dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8397{
f3dd6933 8398 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8399
f3dd6933
DJ
8400 obstack_free (&cu->abbrev_obstack, NULL);
8401 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8402}
8403
8404/* Lookup an abbrev_info structure in the abbrev hash table. */
8405
8406static struct abbrev_info *
e7c27a73 8407dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8408{
8409 unsigned int hash_number;
8410 struct abbrev_info *abbrev;
8411
8412 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8413 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8414
8415 while (abbrev)
8416 {
8417 if (abbrev->number == number)
8418 return abbrev;
8419 else
8420 abbrev = abbrev->next;
8421 }
8422 return NULL;
8423}
8424
72bf9492
DJ
8425/* Returns nonzero if TAG represents a type that we might generate a partial
8426 symbol for. */
8427
8428static int
8429is_type_tag_for_partial (int tag)
8430{
8431 switch (tag)
8432 {
8433#if 0
8434 /* Some types that would be reasonable to generate partial symbols for,
8435 that we don't at present. */
8436 case DW_TAG_array_type:
8437 case DW_TAG_file_type:
8438 case DW_TAG_ptr_to_member_type:
8439 case DW_TAG_set_type:
8440 case DW_TAG_string_type:
8441 case DW_TAG_subroutine_type:
8442#endif
8443 case DW_TAG_base_type:
8444 case DW_TAG_class_type:
680b30c7 8445 case DW_TAG_interface_type:
72bf9492
DJ
8446 case DW_TAG_enumeration_type:
8447 case DW_TAG_structure_type:
8448 case DW_TAG_subrange_type:
8449 case DW_TAG_typedef:
8450 case DW_TAG_union_type:
8451 return 1;
8452 default:
8453 return 0;
8454 }
8455}
8456
8457/* Load all DIEs that are interesting for partial symbols into memory. */
8458
8459static struct partial_die_info *
93311388
DE
8460load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8461 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8462{
8463 struct partial_die_info *part_die;
8464 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8465 struct abbrev_info *abbrev;
8466 unsigned int bytes_read;
5afb4e99 8467 unsigned int load_all = 0;
72bf9492
DJ
8468
8469 int nesting_level = 1;
8470
8471 parent_die = NULL;
8472 last_die = NULL;
8473
5afb4e99
DJ
8474 if (cu->per_cu && cu->per_cu->load_all_dies)
8475 load_all = 1;
8476
72bf9492
DJ
8477 cu->partial_dies
8478 = htab_create_alloc_ex (cu->header.length / 12,
8479 partial_die_hash,
8480 partial_die_eq,
8481 NULL,
8482 &cu->comp_unit_obstack,
8483 hashtab_obstack_allocate,
8484 dummy_obstack_deallocate);
8485
8486 part_die = obstack_alloc (&cu->comp_unit_obstack,
8487 sizeof (struct partial_die_info));
8488
8489 while (1)
8490 {
8491 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8492
8493 /* A NULL abbrev means the end of a series of children. */
8494 if (abbrev == NULL)
8495 {
8496 if (--nesting_level == 0)
8497 {
8498 /* PART_DIE was probably the last thing allocated on the
8499 comp_unit_obstack, so we could call obstack_free
8500 here. We don't do that because the waste is small,
8501 and will be cleaned up when we're done with this
8502 compilation unit. This way, we're also more robust
8503 against other users of the comp_unit_obstack. */
8504 return first_die;
8505 }
8506 info_ptr += bytes_read;
8507 last_die = parent_die;
8508 parent_die = parent_die->die_parent;
8509 continue;
8510 }
8511
98bfdba5
PA
8512 /* Check for template arguments. We never save these; if
8513 they're seen, we just mark the parent, and go on our way. */
8514 if (parent_die != NULL
8515 && cu->language == language_cplus
8516 && (abbrev->tag == DW_TAG_template_type_param
8517 || abbrev->tag == DW_TAG_template_value_param))
8518 {
8519 parent_die->has_template_arguments = 1;
8520
8521 if (!load_all)
8522 {
8523 /* We don't need a partial DIE for the template argument. */
8524 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8525 cu);
8526 continue;
8527 }
8528 }
8529
8530 /* We only recurse into subprograms looking for template arguments.
8531 Skip their other children. */
8532 if (!load_all
8533 && cu->language == language_cplus
8534 && parent_die != NULL
8535 && parent_die->tag == DW_TAG_subprogram)
8536 {
8537 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8538 continue;
8539 }
8540
5afb4e99
DJ
8541 /* Check whether this DIE is interesting enough to save. Normally
8542 we would not be interested in members here, but there may be
8543 later variables referencing them via DW_AT_specification (for
8544 static members). */
8545 if (!load_all
8546 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8547 && abbrev->tag != DW_TAG_enumerator
8548 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8549 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8550 && abbrev->tag != DW_TAG_variable
5afb4e99 8551 && abbrev->tag != DW_TAG_namespace
f55ee35c 8552 && abbrev->tag != DW_TAG_module
5afb4e99 8553 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8554 {
8555 /* Otherwise we skip to the next sibling, if any. */
93311388 8556 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8557 continue;
8558 }
8559
93311388
DE
8560 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8561 buffer, info_ptr, cu);
72bf9492
DJ
8562
8563 /* This two-pass algorithm for processing partial symbols has a
8564 high cost in cache pressure. Thus, handle some simple cases
8565 here which cover the majority of C partial symbols. DIEs
8566 which neither have specification tags in them, nor could have
8567 specification tags elsewhere pointing at them, can simply be
8568 processed and discarded.
8569
8570 This segment is also optional; scan_partial_symbols and
8571 add_partial_symbol will handle these DIEs if we chain
8572 them in normally. When compilers which do not emit large
8573 quantities of duplicate debug information are more common,
8574 this code can probably be removed. */
8575
8576 /* Any complete simple types at the top level (pretty much all
8577 of them, for a language without namespaces), can be processed
8578 directly. */
8579 if (parent_die == NULL
8580 && part_die->has_specification == 0
8581 && part_die->is_declaration == 0
8582 && (part_die->tag == DW_TAG_typedef
8583 || part_die->tag == DW_TAG_base_type
8584 || part_die->tag == DW_TAG_subrange_type))
8585 {
8586 if (building_psymtab && part_die->name != NULL)
04a679b8 8587 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8588 VAR_DOMAIN, LOC_TYPEDEF,
8589 &cu->objfile->static_psymbols,
8590 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8591 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8592 continue;
8593 }
8594
8595 /* If we're at the second level, and we're an enumerator, and
8596 our parent has no specification (meaning possibly lives in a
8597 namespace elsewhere), then we can add the partial symbol now
8598 instead of queueing it. */
8599 if (part_die->tag == DW_TAG_enumerator
8600 && parent_die != NULL
8601 && parent_die->die_parent == NULL
8602 && parent_die->tag == DW_TAG_enumeration_type
8603 && parent_die->has_specification == 0)
8604 {
8605 if (part_die->name == NULL)
e2e0b3e5 8606 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8607 else if (building_psymtab)
04a679b8 8608 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8609 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8610 (cu->language == language_cplus
8611 || cu->language == language_java)
72bf9492
DJ
8612 ? &cu->objfile->global_psymbols
8613 : &cu->objfile->static_psymbols,
8614 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8615
93311388 8616 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8617 continue;
8618 }
8619
8620 /* We'll save this DIE so link it in. */
8621 part_die->die_parent = parent_die;
8622 part_die->die_sibling = NULL;
8623 part_die->die_child = NULL;
8624
8625 if (last_die && last_die == parent_die)
8626 last_die->die_child = part_die;
8627 else if (last_die)
8628 last_die->die_sibling = part_die;
8629
8630 last_die = part_die;
8631
8632 if (first_die == NULL)
8633 first_die = part_die;
8634
8635 /* Maybe add the DIE to the hash table. Not all DIEs that we
8636 find interesting need to be in the hash table, because we
8637 also have the parent/sibling/child chains; only those that we
8638 might refer to by offset later during partial symbol reading.
8639
8640 For now this means things that might have be the target of a
8641 DW_AT_specification, DW_AT_abstract_origin, or
8642 DW_AT_extension. DW_AT_extension will refer only to
8643 namespaces; DW_AT_abstract_origin refers to functions (and
8644 many things under the function DIE, but we do not recurse
8645 into function DIEs during partial symbol reading) and
8646 possibly variables as well; DW_AT_specification refers to
8647 declarations. Declarations ought to have the DW_AT_declaration
8648 flag. It happens that GCC forgets to put it in sometimes, but
8649 only for functions, not for types.
8650
8651 Adding more things than necessary to the hash table is harmless
8652 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8653 wasted time in find_partial_die, when we reread the compilation
8654 unit with load_all_dies set. */
72bf9492 8655
5afb4e99
DJ
8656 if (load_all
8657 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8658 || abbrev->tag == DW_TAG_variable
8659 || abbrev->tag == DW_TAG_namespace
8660 || part_die->is_declaration)
8661 {
8662 void **slot;
8663
8664 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8665 part_die->offset, INSERT);
8666 *slot = part_die;
8667 }
8668
8669 part_die = obstack_alloc (&cu->comp_unit_obstack,
8670 sizeof (struct partial_die_info));
8671
8672 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8673 we have no reason to follow the children of structures; for other
98bfdba5
PA
8674 languages we have to, so that we can get at method physnames
8675 to infer fully qualified class names, for DW_AT_specification,
8676 and for C++ template arguments. For C++, we also look one level
8677 inside functions to find template arguments (if the name of the
8678 function does not already contain the template arguments).
bc30ff58
JB
8679
8680 For Ada, we need to scan the children of subprograms and lexical
8681 blocks as well because Ada allows the definition of nested
8682 entities that could be interesting for the debugger, such as
8683 nested subprograms for instance. */
72bf9492 8684 if (last_die->has_children
5afb4e99
DJ
8685 && (load_all
8686 || last_die->tag == DW_TAG_namespace
f55ee35c 8687 || last_die->tag == DW_TAG_module
72bf9492 8688 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8689 || (cu->language == language_cplus
8690 && last_die->tag == DW_TAG_subprogram
8691 && (last_die->name == NULL
8692 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8693 || (cu->language != language_c
8694 && (last_die->tag == DW_TAG_class_type
680b30c7 8695 || last_die->tag == DW_TAG_interface_type
72bf9492 8696 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8697 || last_die->tag == DW_TAG_union_type))
8698 || (cu->language == language_ada
8699 && (last_die->tag == DW_TAG_subprogram
8700 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8701 {
8702 nesting_level++;
8703 parent_die = last_die;
8704 continue;
8705 }
8706
8707 /* Otherwise we skip to the next sibling, if any. */
93311388 8708 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8709
8710 /* Back to the top, do it again. */
8711 }
8712}
8713
c906108c
SS
8714/* Read a minimal amount of information into the minimal die structure. */
8715
fe1b8b76 8716static gdb_byte *
72bf9492
DJ
8717read_partial_die (struct partial_die_info *part_die,
8718 struct abbrev_info *abbrev,
8719 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8720 gdb_byte *buffer, gdb_byte *info_ptr,
8721 struct dwarf2_cu *cu)
c906108c 8722{
fa238c03 8723 unsigned int i;
c906108c 8724 struct attribute attr;
c5aa993b 8725 int has_low_pc_attr = 0;
c906108c
SS
8726 int has_high_pc_attr = 0;
8727
72bf9492 8728 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8729
93311388 8730 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8731
8732 info_ptr += abbrev_len;
8733
8734 if (abbrev == NULL)
8735 return info_ptr;
8736
c906108c
SS
8737 part_die->tag = abbrev->tag;
8738 part_die->has_children = abbrev->has_children;
c906108c
SS
8739
8740 for (i = 0; i < abbrev->num_attrs; ++i)
8741 {
e7c27a73 8742 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8743
8744 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8745 partial symbol table. */
c906108c
SS
8746 switch (attr.name)
8747 {
8748 case DW_AT_name:
71c25dea
TT
8749 switch (part_die->tag)
8750 {
8751 case DW_TAG_compile_unit:
348e048f 8752 case DW_TAG_type_unit:
71c25dea
TT
8753 /* Compilation units have a DW_AT_name that is a filename, not
8754 a source language identifier. */
8755 case DW_TAG_enumeration_type:
8756 case DW_TAG_enumerator:
8757 /* These tags always have simple identifiers already; no need
8758 to canonicalize them. */
8759 part_die->name = DW_STRING (&attr);
8760 break;
8761 default:
8762 part_die->name
8763 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8764 &cu->objfile->objfile_obstack);
71c25dea
TT
8765 break;
8766 }
c906108c 8767 break;
31ef98ae 8768 case DW_AT_linkage_name:
c906108c 8769 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8770 /* Note that both forms of linkage name might appear. We
8771 assume they will be the same, and we only store the last
8772 one we see. */
94af9270
KS
8773 if (cu->language == language_ada)
8774 part_die->name = DW_STRING (&attr);
c906108c
SS
8775 break;
8776 case DW_AT_low_pc:
8777 has_low_pc_attr = 1;
8778 part_die->lowpc = DW_ADDR (&attr);
8779 break;
8780 case DW_AT_high_pc:
8781 has_high_pc_attr = 1;
8782 part_die->highpc = DW_ADDR (&attr);
8783 break;
8784 case DW_AT_location:
8e19ed76
PS
8785 /* Support the .debug_loc offsets */
8786 if (attr_form_is_block (&attr))
8787 {
8788 part_die->locdesc = DW_BLOCK (&attr);
8789 }
3690dd37 8790 else if (attr_form_is_section_offset (&attr))
8e19ed76 8791 {
4d3c2250 8792 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8793 }
8794 else
8795 {
4d3c2250
KB
8796 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8797 "partial symbol information");
8e19ed76 8798 }
c906108c 8799 break;
c906108c
SS
8800 case DW_AT_external:
8801 part_die->is_external = DW_UNSND (&attr);
8802 break;
8803 case DW_AT_declaration:
8804 part_die->is_declaration = DW_UNSND (&attr);
8805 break;
8806 case DW_AT_type:
8807 part_die->has_type = 1;
8808 break;
8809 case DW_AT_abstract_origin:
8810 case DW_AT_specification:
72bf9492
DJ
8811 case DW_AT_extension:
8812 part_die->has_specification = 1;
c764a876 8813 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8814 break;
8815 case DW_AT_sibling:
8816 /* Ignore absolute siblings, they might point outside of
8817 the current compile unit. */
8818 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8819 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8820 else
93311388 8821 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8822 break;
fa4028e9
JB
8823 case DW_AT_byte_size:
8824 part_die->has_byte_size = 1;
8825 break;
68511cec
CES
8826 case DW_AT_calling_convention:
8827 /* DWARF doesn't provide a way to identify a program's source-level
8828 entry point. DW_AT_calling_convention attributes are only meant
8829 to describe functions' calling conventions.
8830
8831 However, because it's a necessary piece of information in
8832 Fortran, and because DW_CC_program is the only piece of debugging
8833 information whose definition refers to a 'main program' at all,
8834 several compilers have begun marking Fortran main programs with
8835 DW_CC_program --- even when those functions use the standard
8836 calling conventions.
8837
8838 So until DWARF specifies a way to provide this information and
8839 compilers pick up the new representation, we'll support this
8840 practice. */
8841 if (DW_UNSND (&attr) == DW_CC_program
8842 && cu->language == language_fortran)
8843 set_main_name (part_die->name);
8844 break;
c906108c
SS
8845 default:
8846 break;
8847 }
8848 }
8849
c906108c
SS
8850 /* When using the GNU linker, .gnu.linkonce. sections are used to
8851 eliminate duplicate copies of functions and vtables and such.
8852 The linker will arbitrarily choose one and discard the others.
8853 The AT_*_pc values for such functions refer to local labels in
8854 these sections. If the section from that file was discarded, the
8855 labels are not in the output, so the relocs get a value of 0.
8856 If this is a discarded function, mark the pc bounds as invalid,
8857 so that GDB will ignore it. */
8858 if (has_low_pc_attr && has_high_pc_attr
8859 && part_die->lowpc < part_die->highpc
8860 && (part_die->lowpc != 0
72dca2f5 8861 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8862 part_die->has_pc_info = 1;
85cbf3d3 8863
c906108c
SS
8864 return info_ptr;
8865}
8866
72bf9492
DJ
8867/* Find a cached partial DIE at OFFSET in CU. */
8868
8869static struct partial_die_info *
c764a876 8870find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8871{
8872 struct partial_die_info *lookup_die = NULL;
8873 struct partial_die_info part_die;
8874
8875 part_die.offset = offset;
8876 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8877
72bf9492
DJ
8878 return lookup_die;
8879}
8880
348e048f
DE
8881/* Find a partial DIE at OFFSET, which may or may not be in CU,
8882 except in the case of .debug_types DIEs which do not reference
8883 outside their CU (they do however referencing other types via
8884 DW_FORM_sig8). */
72bf9492
DJ
8885
8886static struct partial_die_info *
c764a876 8887find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8888{
5afb4e99
DJ
8889 struct dwarf2_per_cu_data *per_cu = NULL;
8890 struct partial_die_info *pd = NULL;
72bf9492 8891
348e048f
DE
8892 if (cu->per_cu->from_debug_types)
8893 {
8894 pd = find_partial_die_in_comp_unit (offset, cu);
8895 if (pd != NULL)
8896 return pd;
8897 goto not_found;
8898 }
8899
45452591 8900 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8901 {
8902 pd = find_partial_die_in_comp_unit (offset, cu);
8903 if (pd != NULL)
8904 return pd;
8905 }
72bf9492 8906
ae038cb0
DJ
8907 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8908
98bfdba5
PA
8909 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8910 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8911
8912 per_cu->cu->last_used = 0;
5afb4e99
DJ
8913 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8914
8915 if (pd == NULL && per_cu->load_all_dies == 0)
8916 {
8917 struct cleanup *back_to;
8918 struct partial_die_info comp_unit_die;
8919 struct abbrev_info *abbrev;
8920 unsigned int bytes_read;
8921 char *info_ptr;
8922
8923 per_cu->load_all_dies = 1;
8924
8925 /* Re-read the DIEs. */
8926 back_to = make_cleanup (null_cleanup, 0);
8927 if (per_cu->cu->dwarf2_abbrevs == NULL)
8928 {
8929 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8930 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8931 }
dce234bc 8932 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8933 + per_cu->cu->header.offset
8934 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8935 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8936 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8937 per_cu->cu->objfile->obfd,
8938 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8939 per_cu->cu);
8940 if (comp_unit_die.has_children)
93311388
DE
8941 load_partial_dies (per_cu->cu->objfile->obfd,
8942 dwarf2_per_objfile->info.buffer, info_ptr,
8943 0, per_cu->cu);
5afb4e99
DJ
8944 do_cleanups (back_to);
8945
8946 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8947 }
8948
348e048f
DE
8949 not_found:
8950
5afb4e99
DJ
8951 if (pd == NULL)
8952 internal_error (__FILE__, __LINE__,
c764a876 8953 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8954 offset, bfd_get_filename (cu->objfile->obfd));
8955 return pd;
72bf9492
DJ
8956}
8957
8958/* Adjust PART_DIE before generating a symbol for it. This function
8959 may set the is_external flag or change the DIE's name. */
8960
8961static void
8962fixup_partial_die (struct partial_die_info *part_die,
8963 struct dwarf2_cu *cu)
8964{
8965 /* If we found a reference attribute and the DIE has no name, try
8966 to find a name in the referred to DIE. */
8967
8968 if (part_die->name == NULL && part_die->has_specification)
8969 {
8970 struct partial_die_info *spec_die;
72bf9492 8971
10b3939b 8972 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8973
10b3939b 8974 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8975
8976 if (spec_die->name)
8977 {
8978 part_die->name = spec_die->name;
8979
8980 /* Copy DW_AT_external attribute if it is set. */
8981 if (spec_die->is_external)
8982 part_die->is_external = spec_die->is_external;
8983 }
8984 }
8985
8986 /* Set default names for some unnamed DIEs. */
8987 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8988 || part_die->tag == DW_TAG_class_type))
8989 part_die->name = "(anonymous class)";
8990
8991 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8992 part_die->name = "(anonymous namespace)";
8993
8994 if (part_die->tag == DW_TAG_structure_type
8995 || part_die->tag == DW_TAG_class_type
8996 || part_die->tag == DW_TAG_union_type)
8997 guess_structure_name (part_die, cu);
8998}
8999
a8329558 9000/* Read an attribute value described by an attribute form. */
c906108c 9001
fe1b8b76 9002static gdb_byte *
a8329558 9003read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9004 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9005 struct dwarf2_cu *cu)
c906108c 9006{
e7c27a73 9007 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9008 unsigned int bytes_read;
9009 struct dwarf_block *blk;
9010
a8329558
KW
9011 attr->form = form;
9012 switch (form)
c906108c 9013 {
c906108c 9014 case DW_FORM_ref_addr:
ae411497
TT
9015 if (cu->header.version == 2)
9016 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9017 else
9018 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9019 info_ptr += bytes_read;
9020 break;
9021 case DW_FORM_addr:
e7c27a73 9022 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9023 info_ptr += bytes_read;
c906108c
SS
9024 break;
9025 case DW_FORM_block2:
7b5a2f43 9026 blk = dwarf_alloc_block (cu);
c906108c
SS
9027 blk->size = read_2_bytes (abfd, info_ptr);
9028 info_ptr += 2;
9029 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9030 info_ptr += blk->size;
9031 DW_BLOCK (attr) = blk;
9032 break;
9033 case DW_FORM_block4:
7b5a2f43 9034 blk = dwarf_alloc_block (cu);
c906108c
SS
9035 blk->size = read_4_bytes (abfd, info_ptr);
9036 info_ptr += 4;
9037 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9038 info_ptr += blk->size;
9039 DW_BLOCK (attr) = blk;
9040 break;
9041 case DW_FORM_data2:
9042 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9043 info_ptr += 2;
9044 break;
9045 case DW_FORM_data4:
9046 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9047 info_ptr += 4;
9048 break;
9049 case DW_FORM_data8:
9050 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9051 info_ptr += 8;
9052 break;
2dc7f7b3
TT
9053 case DW_FORM_sec_offset:
9054 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9055 info_ptr += bytes_read;
9056 break;
c906108c 9057 case DW_FORM_string:
9b1c24c8 9058 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9059 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9060 info_ptr += bytes_read;
9061 break;
4bdf3d34
JJ
9062 case DW_FORM_strp:
9063 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9064 &bytes_read);
8285870a 9065 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9066 info_ptr += bytes_read;
9067 break;
2dc7f7b3 9068 case DW_FORM_exprloc:
c906108c 9069 case DW_FORM_block:
7b5a2f43 9070 blk = dwarf_alloc_block (cu);
c906108c
SS
9071 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9072 info_ptr += bytes_read;
9073 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9074 info_ptr += blk->size;
9075 DW_BLOCK (attr) = blk;
9076 break;
9077 case DW_FORM_block1:
7b5a2f43 9078 blk = dwarf_alloc_block (cu);
c906108c
SS
9079 blk->size = read_1_byte (abfd, info_ptr);
9080 info_ptr += 1;
9081 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9082 info_ptr += blk->size;
9083 DW_BLOCK (attr) = blk;
9084 break;
9085 case DW_FORM_data1:
9086 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9087 info_ptr += 1;
9088 break;
9089 case DW_FORM_flag:
9090 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9091 info_ptr += 1;
9092 break;
2dc7f7b3
TT
9093 case DW_FORM_flag_present:
9094 DW_UNSND (attr) = 1;
9095 break;
c906108c
SS
9096 case DW_FORM_sdata:
9097 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9098 info_ptr += bytes_read;
9099 break;
9100 case DW_FORM_udata:
9101 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9102 info_ptr += bytes_read;
9103 break;
9104 case DW_FORM_ref1:
10b3939b 9105 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9106 info_ptr += 1;
9107 break;
9108 case DW_FORM_ref2:
10b3939b 9109 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9110 info_ptr += 2;
9111 break;
9112 case DW_FORM_ref4:
10b3939b 9113 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9114 info_ptr += 4;
9115 break;
613e1657 9116 case DW_FORM_ref8:
10b3939b 9117 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9118 info_ptr += 8;
9119 break;
348e048f
DE
9120 case DW_FORM_sig8:
9121 /* Convert the signature to something we can record in DW_UNSND
9122 for later lookup.
9123 NOTE: This is NULL if the type wasn't found. */
9124 DW_SIGNATURED_TYPE (attr) =
9125 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9126 info_ptr += 8;
9127 break;
c906108c 9128 case DW_FORM_ref_udata:
10b3939b
DJ
9129 DW_ADDR (attr) = (cu->header.offset
9130 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9131 info_ptr += bytes_read;
9132 break;
c906108c 9133 case DW_FORM_indirect:
a8329558
KW
9134 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9135 info_ptr += bytes_read;
e7c27a73 9136 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9137 break;
c906108c 9138 default:
8a3fe4f8 9139 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9140 dwarf_form_name (form),
9141 bfd_get_filename (abfd));
c906108c 9142 }
28e94949
JB
9143
9144 /* We have seen instances where the compiler tried to emit a byte
9145 size attribute of -1 which ended up being encoded as an unsigned
9146 0xffffffff. Although 0xffffffff is technically a valid size value,
9147 an object of this size seems pretty unlikely so we can relatively
9148 safely treat these cases as if the size attribute was invalid and
9149 treat them as zero by default. */
9150 if (attr->name == DW_AT_byte_size
9151 && form == DW_FORM_data4
9152 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9153 {
9154 complaint
9155 (&symfile_complaints,
43bbcdc2
PH
9156 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9157 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9158 DW_UNSND (attr) = 0;
9159 }
28e94949 9160
c906108c
SS
9161 return info_ptr;
9162}
9163
a8329558
KW
9164/* Read an attribute described by an abbreviated attribute. */
9165
fe1b8b76 9166static gdb_byte *
a8329558 9167read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9168 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9169{
9170 attr->name = abbrev->name;
e7c27a73 9171 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9172}
9173
c906108c
SS
9174/* read dwarf information from a buffer */
9175
9176static unsigned int
fe1b8b76 9177read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9178{
fe1b8b76 9179 return bfd_get_8 (abfd, buf);
c906108c
SS
9180}
9181
9182static int
fe1b8b76 9183read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9184{
fe1b8b76 9185 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9186}
9187
9188static unsigned int
fe1b8b76 9189read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9190{
fe1b8b76 9191 return bfd_get_16 (abfd, buf);
c906108c
SS
9192}
9193
9194static int
fe1b8b76 9195read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9196{
fe1b8b76 9197 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9198}
9199
9200static unsigned int
fe1b8b76 9201read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9202{
fe1b8b76 9203 return bfd_get_32 (abfd, buf);
c906108c
SS
9204}
9205
9206static int
fe1b8b76 9207read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9208{
fe1b8b76 9209 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9210}
9211
93311388 9212static ULONGEST
fe1b8b76 9213read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9214{
fe1b8b76 9215 return bfd_get_64 (abfd, buf);
c906108c
SS
9216}
9217
9218static CORE_ADDR
fe1b8b76 9219read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9220 unsigned int *bytes_read)
c906108c 9221{
e7c27a73 9222 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9223 CORE_ADDR retval = 0;
9224
107d2387 9225 if (cu_header->signed_addr_p)
c906108c 9226 {
107d2387
AC
9227 switch (cu_header->addr_size)
9228 {
9229 case 2:
fe1b8b76 9230 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9231 break;
9232 case 4:
fe1b8b76 9233 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9234 break;
9235 case 8:
fe1b8b76 9236 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9237 break;
9238 default:
8e65ff28 9239 internal_error (__FILE__, __LINE__,
e2e0b3e5 9240 _("read_address: bad switch, signed [in module %s]"),
659b0389 9241 bfd_get_filename (abfd));
107d2387
AC
9242 }
9243 }
9244 else
9245 {
9246 switch (cu_header->addr_size)
9247 {
9248 case 2:
fe1b8b76 9249 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9250 break;
9251 case 4:
fe1b8b76 9252 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9253 break;
9254 case 8:
fe1b8b76 9255 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9256 break;
9257 default:
8e65ff28 9258 internal_error (__FILE__, __LINE__,
e2e0b3e5 9259 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9260 bfd_get_filename (abfd));
107d2387 9261 }
c906108c 9262 }
64367e0a 9263
107d2387
AC
9264 *bytes_read = cu_header->addr_size;
9265 return retval;
c906108c
SS
9266}
9267
f7ef9339 9268/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9269 specification allows the initial length to take up either 4 bytes
9270 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9271 bytes describe the length and all offsets will be 8 bytes in length
9272 instead of 4.
9273
f7ef9339
KB
9274 An older, non-standard 64-bit format is also handled by this
9275 function. The older format in question stores the initial length
9276 as an 8-byte quantity without an escape value. Lengths greater
9277 than 2^32 aren't very common which means that the initial 4 bytes
9278 is almost always zero. Since a length value of zero doesn't make
9279 sense for the 32-bit format, this initial zero can be considered to
9280 be an escape value which indicates the presence of the older 64-bit
9281 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9282 greater than 4GB. If it becomes necessary to handle lengths
9283 somewhat larger than 4GB, we could allow other small values (such
9284 as the non-sensical values of 1, 2, and 3) to also be used as
9285 escape values indicating the presence of the old format.
f7ef9339 9286
917c78fc
MK
9287 The value returned via bytes_read should be used to increment the
9288 relevant pointer after calling read_initial_length().
c764a876 9289
613e1657
KB
9290 [ Note: read_initial_length() and read_offset() are based on the
9291 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9292 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9293 from:
9294
f7ef9339 9295 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9296
613e1657
KB
9297 This document is only a draft and is subject to change. (So beware.)
9298
f7ef9339 9299 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9300 determined empirically by examining 64-bit ELF files produced by
9301 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9302
9303 - Kevin, July 16, 2002
613e1657
KB
9304 ] */
9305
9306static LONGEST
c764a876 9307read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9308{
fe1b8b76 9309 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9310
dd373385 9311 if (length == 0xffffffff)
613e1657 9312 {
fe1b8b76 9313 length = bfd_get_64 (abfd, buf + 4);
613e1657 9314 *bytes_read = 12;
613e1657 9315 }
dd373385 9316 else if (length == 0)
f7ef9339 9317 {
dd373385 9318 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9319 length = bfd_get_64 (abfd, buf);
f7ef9339 9320 *bytes_read = 8;
f7ef9339 9321 }
613e1657
KB
9322 else
9323 {
9324 *bytes_read = 4;
613e1657
KB
9325 }
9326
c764a876
DE
9327 return length;
9328}
dd373385 9329
c764a876
DE
9330/* Cover function for read_initial_length.
9331 Returns the length of the object at BUF, and stores the size of the
9332 initial length in *BYTES_READ and stores the size that offsets will be in
9333 *OFFSET_SIZE.
9334 If the initial length size is not equivalent to that specified in
9335 CU_HEADER then issue a complaint.
9336 This is useful when reading non-comp-unit headers. */
dd373385 9337
c764a876
DE
9338static LONGEST
9339read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9340 const struct comp_unit_head *cu_header,
9341 unsigned int *bytes_read,
9342 unsigned int *offset_size)
9343{
9344 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9345
9346 gdb_assert (cu_header->initial_length_size == 4
9347 || cu_header->initial_length_size == 8
9348 || cu_header->initial_length_size == 12);
9349
9350 if (cu_header->initial_length_size != *bytes_read)
9351 complaint (&symfile_complaints,
9352 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9353
c764a876 9354 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9355 return length;
613e1657
KB
9356}
9357
9358/* Read an offset from the data stream. The size of the offset is
917c78fc 9359 given by cu_header->offset_size. */
613e1657
KB
9360
9361static LONGEST
fe1b8b76 9362read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9363 unsigned int *bytes_read)
c764a876
DE
9364{
9365 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9366
c764a876
DE
9367 *bytes_read = cu_header->offset_size;
9368 return offset;
9369}
9370
9371/* Read an offset from the data stream. */
9372
9373static LONGEST
9374read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9375{
9376 LONGEST retval = 0;
9377
c764a876 9378 switch (offset_size)
613e1657
KB
9379 {
9380 case 4:
fe1b8b76 9381 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9382 break;
9383 case 8:
fe1b8b76 9384 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9385 break;
9386 default:
8e65ff28 9387 internal_error (__FILE__, __LINE__,
c764a876 9388 _("read_offset_1: bad switch [in module %s]"),
659b0389 9389 bfd_get_filename (abfd));
613e1657
KB
9390 }
9391
917c78fc 9392 return retval;
613e1657
KB
9393}
9394
fe1b8b76
JB
9395static gdb_byte *
9396read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9397{
9398 /* If the size of a host char is 8 bits, we can return a pointer
9399 to the buffer, otherwise we have to copy the data to a buffer
9400 allocated on the temporary obstack. */
4bdf3d34 9401 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9402 return buf;
c906108c
SS
9403}
9404
9405static char *
9b1c24c8 9406read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9407{
9408 /* If the size of a host char is 8 bits, we can return a pointer
9409 to the string, otherwise we have to copy the string to a buffer
9410 allocated on the temporary obstack. */
4bdf3d34 9411 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9412 if (*buf == '\0')
9413 {
9414 *bytes_read_ptr = 1;
9415 return NULL;
9416 }
fe1b8b76
JB
9417 *bytes_read_ptr = strlen ((char *) buf) + 1;
9418 return (char *) buf;
4bdf3d34
JJ
9419}
9420
9421static char *
fe1b8b76 9422read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9423 const struct comp_unit_head *cu_header,
9424 unsigned int *bytes_read_ptr)
9425{
c764a876 9426 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9427
be391dca 9428 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9429 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9430 {
8a3fe4f8 9431 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9432 bfd_get_filename (abfd));
4bdf3d34 9433 return NULL;
c906108c 9434 }
dce234bc 9435 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9436 {
8a3fe4f8 9437 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9438 bfd_get_filename (abfd));
c906108c
SS
9439 return NULL;
9440 }
4bdf3d34 9441 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9442 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9443 return NULL;
dce234bc 9444 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9445}
9446
ce5d95e1 9447static unsigned long
fe1b8b76 9448read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9449{
ce5d95e1
JB
9450 unsigned long result;
9451 unsigned int num_read;
c906108c
SS
9452 int i, shift;
9453 unsigned char byte;
9454
9455 result = 0;
9456 shift = 0;
9457 num_read = 0;
9458 i = 0;
9459 while (1)
9460 {
fe1b8b76 9461 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9462 buf++;
9463 num_read++;
ce5d95e1 9464 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9465 if ((byte & 128) == 0)
9466 {
9467 break;
9468 }
9469 shift += 7;
9470 }
9471 *bytes_read_ptr = num_read;
9472 return result;
9473}
9474
ce5d95e1 9475static long
fe1b8b76 9476read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9477{
ce5d95e1 9478 long result;
77e0b926 9479 int i, shift, num_read;
c906108c
SS
9480 unsigned char byte;
9481
9482 result = 0;
9483 shift = 0;
c906108c
SS
9484 num_read = 0;
9485 i = 0;
9486 while (1)
9487 {
fe1b8b76 9488 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9489 buf++;
9490 num_read++;
ce5d95e1 9491 result |= ((long)(byte & 127) << shift);
c906108c
SS
9492 shift += 7;
9493 if ((byte & 128) == 0)
9494 {
9495 break;
9496 }
9497 }
77e0b926
DJ
9498 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9499 result |= -(((long)1) << shift);
c906108c
SS
9500 *bytes_read_ptr = num_read;
9501 return result;
9502}
9503
4bb7a0a7
DJ
9504/* Return a pointer to just past the end of an LEB128 number in BUF. */
9505
fe1b8b76
JB
9506static gdb_byte *
9507skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9508{
9509 int byte;
9510
9511 while (1)
9512 {
fe1b8b76 9513 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9514 buf++;
9515 if ((byte & 128) == 0)
9516 return buf;
9517 }
9518}
9519
c906108c 9520static void
e142c38c 9521set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9522{
9523 switch (lang)
9524 {
9525 case DW_LANG_C89:
76bee0cc 9526 case DW_LANG_C99:
c906108c 9527 case DW_LANG_C:
e142c38c 9528 cu->language = language_c;
c906108c
SS
9529 break;
9530 case DW_LANG_C_plus_plus:
e142c38c 9531 cu->language = language_cplus;
c906108c 9532 break;
6aecb9c2
JB
9533 case DW_LANG_D:
9534 cu->language = language_d;
9535 break;
c906108c
SS
9536 case DW_LANG_Fortran77:
9537 case DW_LANG_Fortran90:
b21b22e0 9538 case DW_LANG_Fortran95:
e142c38c 9539 cu->language = language_fortran;
c906108c
SS
9540 break;
9541 case DW_LANG_Mips_Assembler:
e142c38c 9542 cu->language = language_asm;
c906108c 9543 break;
bebd888e 9544 case DW_LANG_Java:
e142c38c 9545 cu->language = language_java;
bebd888e 9546 break;
c906108c 9547 case DW_LANG_Ada83:
8aaf0b47 9548 case DW_LANG_Ada95:
bc5f45f8
JB
9549 cu->language = language_ada;
9550 break;
72019c9c
GM
9551 case DW_LANG_Modula2:
9552 cu->language = language_m2;
9553 break;
fe8e67fd
PM
9554 case DW_LANG_Pascal83:
9555 cu->language = language_pascal;
9556 break;
22566fbd
DJ
9557 case DW_LANG_ObjC:
9558 cu->language = language_objc;
9559 break;
c906108c
SS
9560 case DW_LANG_Cobol74:
9561 case DW_LANG_Cobol85:
c906108c 9562 default:
e142c38c 9563 cu->language = language_minimal;
c906108c
SS
9564 break;
9565 }
e142c38c 9566 cu->language_defn = language_def (cu->language);
c906108c
SS
9567}
9568
9569/* Return the named attribute or NULL if not there. */
9570
9571static struct attribute *
e142c38c 9572dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9573{
9574 unsigned int i;
9575 struct attribute *spec = NULL;
9576
9577 for (i = 0; i < die->num_attrs; ++i)
9578 {
9579 if (die->attrs[i].name == name)
10b3939b 9580 return &die->attrs[i];
c906108c
SS
9581 if (die->attrs[i].name == DW_AT_specification
9582 || die->attrs[i].name == DW_AT_abstract_origin)
9583 spec = &die->attrs[i];
9584 }
c906108c 9585
10b3939b 9586 if (spec)
f2f0e013
DJ
9587 {
9588 die = follow_die_ref (die, spec, &cu);
9589 return dwarf2_attr (die, name, cu);
9590 }
c5aa993b 9591
c906108c
SS
9592 return NULL;
9593}
9594
348e048f
DE
9595/* Return the named attribute or NULL if not there,
9596 but do not follow DW_AT_specification, etc.
9597 This is for use in contexts where we're reading .debug_types dies.
9598 Following DW_AT_specification, DW_AT_abstract_origin will take us
9599 back up the chain, and we want to go down. */
9600
9601static struct attribute *
9602dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9603 struct dwarf2_cu *cu)
9604{
9605 unsigned int i;
9606
9607 for (i = 0; i < die->num_attrs; ++i)
9608 if (die->attrs[i].name == name)
9609 return &die->attrs[i];
9610
9611 return NULL;
9612}
9613
05cf31d1
JB
9614/* Return non-zero iff the attribute NAME is defined for the given DIE,
9615 and holds a non-zero value. This function should only be used for
2dc7f7b3 9616 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9617
9618static int
9619dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9620{
9621 struct attribute *attr = dwarf2_attr (die, name, cu);
9622
9623 return (attr && DW_UNSND (attr));
9624}
9625
3ca72b44 9626static int
e142c38c 9627die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9628{
05cf31d1
JB
9629 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9630 which value is non-zero. However, we have to be careful with
9631 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9632 (via dwarf2_flag_true_p) follows this attribute. So we may
9633 end up accidently finding a declaration attribute that belongs
9634 to a different DIE referenced by the specification attribute,
9635 even though the given DIE does not have a declaration attribute. */
9636 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9637 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9638}
9639
63d06c5c 9640/* Return the die giving the specification for DIE, if there is
f2f0e013 9641 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9642 containing the return value on output. If there is no
9643 specification, but there is an abstract origin, that is
9644 returned. */
63d06c5c
DC
9645
9646static struct die_info *
f2f0e013 9647die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9648{
f2f0e013
DJ
9649 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9650 *spec_cu);
63d06c5c 9651
edb3359d
DJ
9652 if (spec_attr == NULL)
9653 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9654
63d06c5c
DC
9655 if (spec_attr == NULL)
9656 return NULL;
9657 else
f2f0e013 9658 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9659}
c906108c 9660
debd256d
JB
9661/* Free the line_header structure *LH, and any arrays and strings it
9662 refers to. */
9663static void
9664free_line_header (struct line_header *lh)
9665{
9666 if (lh->standard_opcode_lengths)
a8bc7b56 9667 xfree (lh->standard_opcode_lengths);
debd256d
JB
9668
9669 /* Remember that all the lh->file_names[i].name pointers are
9670 pointers into debug_line_buffer, and don't need to be freed. */
9671 if (lh->file_names)
a8bc7b56 9672 xfree (lh->file_names);
debd256d
JB
9673
9674 /* Similarly for the include directory names. */
9675 if (lh->include_dirs)
a8bc7b56 9676 xfree (lh->include_dirs);
debd256d 9677
a8bc7b56 9678 xfree (lh);
debd256d
JB
9679}
9680
9681
9682/* Add an entry to LH's include directory table. */
9683static void
9684add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9685{
debd256d
JB
9686 /* Grow the array if necessary. */
9687 if (lh->include_dirs_size == 0)
c5aa993b 9688 {
debd256d
JB
9689 lh->include_dirs_size = 1; /* for testing */
9690 lh->include_dirs = xmalloc (lh->include_dirs_size
9691 * sizeof (*lh->include_dirs));
9692 }
9693 else if (lh->num_include_dirs >= lh->include_dirs_size)
9694 {
9695 lh->include_dirs_size *= 2;
9696 lh->include_dirs = xrealloc (lh->include_dirs,
9697 (lh->include_dirs_size
9698 * sizeof (*lh->include_dirs)));
c5aa993b 9699 }
c906108c 9700
debd256d
JB
9701 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9702}
6e70227d 9703
debd256d
JB
9704
9705/* Add an entry to LH's file name table. */
9706static void
9707add_file_name (struct line_header *lh,
9708 char *name,
9709 unsigned int dir_index,
9710 unsigned int mod_time,
9711 unsigned int length)
9712{
9713 struct file_entry *fe;
9714
9715 /* Grow the array if necessary. */
9716 if (lh->file_names_size == 0)
9717 {
9718 lh->file_names_size = 1; /* for testing */
9719 lh->file_names = xmalloc (lh->file_names_size
9720 * sizeof (*lh->file_names));
9721 }
9722 else if (lh->num_file_names >= lh->file_names_size)
9723 {
9724 lh->file_names_size *= 2;
9725 lh->file_names = xrealloc (lh->file_names,
9726 (lh->file_names_size
9727 * sizeof (*lh->file_names)));
9728 }
9729
9730 fe = &lh->file_names[lh->num_file_names++];
9731 fe->name = name;
9732 fe->dir_index = dir_index;
9733 fe->mod_time = mod_time;
9734 fe->length = length;
aaa75496 9735 fe->included_p = 0;
cb1df416 9736 fe->symtab = NULL;
debd256d 9737}
6e70227d 9738
debd256d
JB
9739
9740/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9741 .debug_line, according to the endianness of ABFD. Return a pointer
9742 to a struct line_header, allocated using xmalloc.
debd256d
JB
9743
9744 NOTE: the strings in the include directory and file name tables of
9745 the returned object point into debug_line_buffer, and must not be
9746 freed. */
9747static struct line_header *
9748dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9749 struct dwarf2_cu *cu)
debd256d
JB
9750{
9751 struct cleanup *back_to;
9752 struct line_header *lh;
fe1b8b76 9753 gdb_byte *line_ptr;
c764a876 9754 unsigned int bytes_read, offset_size;
debd256d
JB
9755 int i;
9756 char *cur_dir, *cur_file;
9757
be391dca 9758 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9759 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9760 {
e2e0b3e5 9761 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9762 return 0;
9763 }
9764
a738430d
MK
9765 /* Make sure that at least there's room for the total_length field.
9766 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9767 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9768 {
4d3c2250 9769 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9770 return 0;
9771 }
9772
9773 lh = xmalloc (sizeof (*lh));
9774 memset (lh, 0, sizeof (*lh));
9775 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9776 (void *) lh);
9777
dce234bc 9778 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9779
a738430d 9780 /* Read in the header. */
6e70227d 9781 lh->total_length =
c764a876
DE
9782 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9783 &bytes_read, &offset_size);
debd256d 9784 line_ptr += bytes_read;
dce234bc
PP
9785 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9786 + dwarf2_per_objfile->line.size))
debd256d 9787 {
4d3c2250 9788 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9789 return 0;
9790 }
9791 lh->statement_program_end = line_ptr + lh->total_length;
9792 lh->version = read_2_bytes (abfd, line_ptr);
9793 line_ptr += 2;
c764a876
DE
9794 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9795 line_ptr += offset_size;
debd256d
JB
9796 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9797 line_ptr += 1;
2dc7f7b3
TT
9798 if (lh->version >= 4)
9799 {
9800 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9801 line_ptr += 1;
9802 }
9803 else
9804 lh->maximum_ops_per_instruction = 1;
9805
9806 if (lh->maximum_ops_per_instruction == 0)
9807 {
9808 lh->maximum_ops_per_instruction = 1;
9809 complaint (&symfile_complaints,
9810 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9811 }
9812
debd256d
JB
9813 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9814 line_ptr += 1;
9815 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9816 line_ptr += 1;
9817 lh->line_range = read_1_byte (abfd, line_ptr);
9818 line_ptr += 1;
9819 lh->opcode_base = read_1_byte (abfd, line_ptr);
9820 line_ptr += 1;
9821 lh->standard_opcode_lengths
fe1b8b76 9822 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9823
9824 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9825 for (i = 1; i < lh->opcode_base; ++i)
9826 {
9827 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9828 line_ptr += 1;
9829 }
9830
a738430d 9831 /* Read directory table. */
9b1c24c8 9832 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9833 {
9834 line_ptr += bytes_read;
9835 add_include_dir (lh, cur_dir);
9836 }
9837 line_ptr += bytes_read;
9838
a738430d 9839 /* Read file name table. */
9b1c24c8 9840 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9841 {
9842 unsigned int dir_index, mod_time, length;
9843
9844 line_ptr += bytes_read;
9845 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9846 line_ptr += bytes_read;
9847 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9848 line_ptr += bytes_read;
9849 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9850 line_ptr += bytes_read;
9851
9852 add_file_name (lh, cur_file, dir_index, mod_time, length);
9853 }
9854 line_ptr += bytes_read;
6e70227d 9855 lh->statement_program_start = line_ptr;
debd256d 9856
dce234bc
PP
9857 if (line_ptr > (dwarf2_per_objfile->line.buffer
9858 + dwarf2_per_objfile->line.size))
4d3c2250 9859 complaint (&symfile_complaints,
e2e0b3e5 9860 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9861
9862 discard_cleanups (back_to);
9863 return lh;
9864}
c906108c 9865
5fb290d7
DJ
9866/* This function exists to work around a bug in certain compilers
9867 (particularly GCC 2.95), in which the first line number marker of a
9868 function does not show up until after the prologue, right before
9869 the second line number marker. This function shifts ADDRESS down
9870 to the beginning of the function if necessary, and is called on
9871 addresses passed to record_line. */
9872
9873static CORE_ADDR
e142c38c 9874check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9875{
9876 struct function_range *fn;
9877
9878 /* Find the function_range containing address. */
e142c38c 9879 if (!cu->first_fn)
5fb290d7
DJ
9880 return address;
9881
e142c38c
DJ
9882 if (!cu->cached_fn)
9883 cu->cached_fn = cu->first_fn;
5fb290d7 9884
e142c38c 9885 fn = cu->cached_fn;
5fb290d7
DJ
9886 while (fn)
9887 if (fn->lowpc <= address && fn->highpc > address)
9888 goto found;
9889 else
9890 fn = fn->next;
9891
e142c38c
DJ
9892 fn = cu->first_fn;
9893 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9894 if (fn->lowpc <= address && fn->highpc > address)
9895 goto found;
9896 else
9897 fn = fn->next;
9898
9899 return address;
9900
9901 found:
9902 if (fn->seen_line)
9903 return address;
9904 if (address != fn->lowpc)
4d3c2250 9905 complaint (&symfile_complaints,
e2e0b3e5 9906 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9907 (unsigned long) address, fn->name);
5fb290d7
DJ
9908 fn->seen_line = 1;
9909 return fn->lowpc;
9910}
9911
c6da4cef
DE
9912/* Subroutine of dwarf_decode_lines to simplify it.
9913 Return the file name of the psymtab for included file FILE_INDEX
9914 in line header LH of PST.
9915 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9916 If space for the result is malloc'd, it will be freed by a cleanup.
9917 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
9918
9919static char *
9920psymtab_include_file_name (const struct line_header *lh, int file_index,
9921 const struct partial_symtab *pst,
9922 const char *comp_dir)
9923{
9924 const struct file_entry fe = lh->file_names [file_index];
9925 char *include_name = fe.name;
9926 char *include_name_to_compare = include_name;
9927 char *dir_name = NULL;
9928 char *pst_filename;
9929 int file_is_pst;
9930
9931 if (fe.dir_index)
9932 dir_name = lh->include_dirs[fe.dir_index - 1];
9933
9934 if (!IS_ABSOLUTE_PATH (include_name)
9935 && (dir_name != NULL || comp_dir != NULL))
9936 {
9937 /* Avoid creating a duplicate psymtab for PST.
9938 We do this by comparing INCLUDE_NAME and PST_FILENAME.
9939 Before we do the comparison, however, we need to account
9940 for DIR_NAME and COMP_DIR.
9941 First prepend dir_name (if non-NULL). If we still don't
9942 have an absolute path prepend comp_dir (if non-NULL).
9943 However, the directory we record in the include-file's
9944 psymtab does not contain COMP_DIR (to match the
9945 corresponding symtab(s)).
9946
9947 Example:
9948
9949 bash$ cd /tmp
9950 bash$ gcc -g ./hello.c
9951 include_name = "hello.c"
9952 dir_name = "."
9953 DW_AT_comp_dir = comp_dir = "/tmp"
9954 DW_AT_name = "./hello.c" */
9955
9956 if (dir_name != NULL)
9957 {
9958 include_name = concat (dir_name, SLASH_STRING,
9959 include_name, (char *)NULL);
9960 include_name_to_compare = include_name;
9961 make_cleanup (xfree, include_name);
9962 }
9963 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
9964 {
9965 include_name_to_compare = concat (comp_dir, SLASH_STRING,
9966 include_name, (char *)NULL);
9967 }
9968 }
9969
9970 pst_filename = pst->filename;
9971 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9972 {
9973 pst_filename = concat (pst->dirname, SLASH_STRING,
9974 pst_filename, (char *)NULL);
9975 }
9976
9977 file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
9978
9979 if (include_name_to_compare != include_name)
9980 xfree (include_name_to_compare);
9981 if (pst_filename != pst->filename)
9982 xfree (pst_filename);
9983
9984 if (file_is_pst)
9985 return NULL;
9986 return include_name;
9987}
9988
aaa75496
JB
9989/* Decode the Line Number Program (LNP) for the given line_header
9990 structure and CU. The actual information extracted and the type
9991 of structures created from the LNP depends on the value of PST.
9992
9993 1. If PST is NULL, then this procedure uses the data from the program
9994 to create all necessary symbol tables, and their linetables.
6e70227d 9995
aaa75496
JB
9996 2. If PST is not NULL, this procedure reads the program to determine
9997 the list of files included by the unit represented by PST, and
c6da4cef
DE
9998 builds all the associated partial symbol tables.
9999
10000 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10001 It is used for relative paths in the line table.
10002 NOTE: When processing partial symtabs (pst != NULL),
10003 comp_dir == pst->dirname.
10004
10005 NOTE: It is important that psymtabs have the same file name (via strcmp)
10006 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10007 symtab we don't use it in the name of the psymtabs we create.
10008 E.g. expand_line_sal requires this when finding psymtabs to expand.
10009 A good testcase for this is mb-inline.exp. */
debd256d 10010
c906108c 10011static void
debd256d 10012dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 10013 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10014{
a8c50c1f 10015 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10016 gdb_byte *line_end;
a8c50c1f 10017 unsigned int bytes_read, extended_len;
c906108c 10018 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10019 CORE_ADDR baseaddr;
10020 struct objfile *objfile = cu->objfile;
fbf65064 10021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10022 const int decode_for_pst_p = (pst != NULL);
cb1df416 10023 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10024
10025 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10026
debd256d
JB
10027 line_ptr = lh->statement_program_start;
10028 line_end = lh->statement_program_end;
c906108c
SS
10029
10030 /* Read the statement sequences until there's nothing left. */
10031 while (line_ptr < line_end)
10032 {
10033 /* state machine registers */
10034 CORE_ADDR address = 0;
10035 unsigned int file = 1;
10036 unsigned int line = 1;
10037 unsigned int column = 0;
debd256d 10038 int is_stmt = lh->default_is_stmt;
c906108c
SS
10039 int basic_block = 0;
10040 int end_sequence = 0;
fbf65064 10041 CORE_ADDR addr;
2dc7f7b3 10042 unsigned char op_index = 0;
c906108c 10043
aaa75496 10044 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10045 {
aaa75496 10046 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10047 /* lh->include_dirs and lh->file_names are 0-based, but the
10048 directory and file name numbers in the statement program
10049 are 1-based. */
10050 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10051 char *dir = NULL;
a738430d 10052
debd256d
JB
10053 if (fe->dir_index)
10054 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10055
10056 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10057 }
10058
a738430d 10059 /* Decode the table. */
c5aa993b 10060 while (!end_sequence)
c906108c
SS
10061 {
10062 op_code = read_1_byte (abfd, line_ptr);
10063 line_ptr += 1;
59205f5a
JB
10064 if (line_ptr > line_end)
10065 {
10066 dwarf2_debug_line_missing_end_sequence_complaint ();
10067 break;
10068 }
9aa1fe7e 10069
debd256d 10070 if (op_code >= lh->opcode_base)
6e70227d 10071 {
a738430d 10072 /* Special operand. */
debd256d 10073 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10074 address += (((op_index + (adj_opcode / lh->line_range))
10075 / lh->maximum_ops_per_instruction)
10076 * lh->minimum_instruction_length);
10077 op_index = ((op_index + (adj_opcode / lh->line_range))
10078 % lh->maximum_ops_per_instruction);
debd256d 10079 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10080 if (lh->num_file_names < file || file == 0)
25e43795 10081 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10082 /* For now we ignore lines not starting on an
10083 instruction boundary. */
10084 else if (op_index == 0)
25e43795
DJ
10085 {
10086 lh->file_names[file - 1].included_p = 1;
ca5f395d 10087 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10088 {
10089 if (last_subfile != current_subfile)
10090 {
10091 addr = gdbarch_addr_bits_remove (gdbarch, address);
10092 if (last_subfile)
10093 record_line (last_subfile, 0, addr);
10094 last_subfile = current_subfile;
10095 }
25e43795 10096 /* Append row to matrix using current values. */
fbf65064
UW
10097 addr = check_cu_functions (address, cu);
10098 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10099 record_line (current_subfile, line, addr);
366da635 10100 }
25e43795 10101 }
ca5f395d 10102 basic_block = 0;
9aa1fe7e
GK
10103 }
10104 else switch (op_code)
c906108c
SS
10105 {
10106 case DW_LNS_extended_op:
a8c50c1f 10107 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10108 line_ptr += bytes_read;
a8c50c1f 10109 extended_end = line_ptr + extended_len;
c906108c
SS
10110 extended_op = read_1_byte (abfd, line_ptr);
10111 line_ptr += 1;
10112 switch (extended_op)
10113 {
10114 case DW_LNE_end_sequence:
10115 end_sequence = 1;
c906108c
SS
10116 break;
10117 case DW_LNE_set_address:
e7c27a73 10118 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10119 op_index = 0;
107d2387
AC
10120 line_ptr += bytes_read;
10121 address += baseaddr;
c906108c
SS
10122 break;
10123 case DW_LNE_define_file:
debd256d
JB
10124 {
10125 char *cur_file;
10126 unsigned int dir_index, mod_time, length;
6e70227d 10127
9b1c24c8 10128 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10129 line_ptr += bytes_read;
10130 dir_index =
10131 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10132 line_ptr += bytes_read;
10133 mod_time =
10134 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10135 line_ptr += bytes_read;
10136 length =
10137 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10138 line_ptr += bytes_read;
10139 add_file_name (lh, cur_file, dir_index, mod_time, length);
10140 }
c906108c 10141 break;
d0c6ba3d
CC
10142 case DW_LNE_set_discriminator:
10143 /* The discriminator is not interesting to the debugger;
10144 just ignore it. */
10145 line_ptr = extended_end;
10146 break;
c906108c 10147 default:
4d3c2250 10148 complaint (&symfile_complaints,
e2e0b3e5 10149 _("mangled .debug_line section"));
debd256d 10150 return;
c906108c 10151 }
a8c50c1f
DJ
10152 /* Make sure that we parsed the extended op correctly. If e.g.
10153 we expected a different address size than the producer used,
10154 we may have read the wrong number of bytes. */
10155 if (line_ptr != extended_end)
10156 {
10157 complaint (&symfile_complaints,
10158 _("mangled .debug_line section"));
10159 return;
10160 }
c906108c
SS
10161 break;
10162 case DW_LNS_copy:
59205f5a 10163 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10164 dwarf2_debug_line_missing_file_complaint ();
10165 else
366da635 10166 {
25e43795 10167 lh->file_names[file - 1].included_p = 1;
ca5f395d 10168 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10169 {
10170 if (last_subfile != current_subfile)
10171 {
10172 addr = gdbarch_addr_bits_remove (gdbarch, address);
10173 if (last_subfile)
10174 record_line (last_subfile, 0, addr);
10175 last_subfile = current_subfile;
10176 }
10177 addr = check_cu_functions (address, cu);
10178 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10179 record_line (current_subfile, line, addr);
10180 }
366da635 10181 }
c906108c
SS
10182 basic_block = 0;
10183 break;
10184 case DW_LNS_advance_pc:
2dc7f7b3
TT
10185 {
10186 CORE_ADDR adjust
10187 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10188
10189 address += (((op_index + adjust)
10190 / lh->maximum_ops_per_instruction)
10191 * lh->minimum_instruction_length);
10192 op_index = ((op_index + adjust)
10193 % lh->maximum_ops_per_instruction);
10194 line_ptr += bytes_read;
10195 }
c906108c
SS
10196 break;
10197 case DW_LNS_advance_line:
10198 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10199 line_ptr += bytes_read;
10200 break;
10201 case DW_LNS_set_file:
debd256d 10202 {
a738430d
MK
10203 /* The arrays lh->include_dirs and lh->file_names are
10204 0-based, but the directory and file name numbers in
10205 the statement program are 1-based. */
debd256d 10206 struct file_entry *fe;
4f1520fb 10207 char *dir = NULL;
a738430d 10208
debd256d
JB
10209 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10210 line_ptr += bytes_read;
59205f5a 10211 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10212 dwarf2_debug_line_missing_file_complaint ();
10213 else
10214 {
10215 fe = &lh->file_names[file - 1];
10216 if (fe->dir_index)
10217 dir = lh->include_dirs[fe->dir_index - 1];
10218 if (!decode_for_pst_p)
10219 {
10220 last_subfile = current_subfile;
10221 dwarf2_start_subfile (fe->name, dir, comp_dir);
10222 }
10223 }
debd256d 10224 }
c906108c
SS
10225 break;
10226 case DW_LNS_set_column:
10227 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10228 line_ptr += bytes_read;
10229 break;
10230 case DW_LNS_negate_stmt:
10231 is_stmt = (!is_stmt);
10232 break;
10233 case DW_LNS_set_basic_block:
10234 basic_block = 1;
10235 break;
c2c6d25f
JM
10236 /* Add to the address register of the state machine the
10237 address increment value corresponding to special opcode
a738430d
MK
10238 255. I.e., this value is scaled by the minimum
10239 instruction length since special opcode 255 would have
10240 scaled the the increment. */
c906108c 10241 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10242 {
10243 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10244
10245 address += (((op_index + adjust)
10246 / lh->maximum_ops_per_instruction)
10247 * lh->minimum_instruction_length);
10248 op_index = ((op_index + adjust)
10249 % lh->maximum_ops_per_instruction);
10250 }
c906108c
SS
10251 break;
10252 case DW_LNS_fixed_advance_pc:
10253 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10254 op_index = 0;
c906108c
SS
10255 line_ptr += 2;
10256 break;
9aa1fe7e 10257 default:
a738430d
MK
10258 {
10259 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10260 int i;
a738430d 10261
debd256d 10262 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10263 {
10264 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10265 line_ptr += bytes_read;
10266 }
10267 }
c906108c
SS
10268 }
10269 }
59205f5a
JB
10270 if (lh->num_file_names < file || file == 0)
10271 dwarf2_debug_line_missing_file_complaint ();
10272 else
10273 {
10274 lh->file_names[file - 1].included_p = 1;
10275 if (!decode_for_pst_p)
fbf65064
UW
10276 {
10277 addr = gdbarch_addr_bits_remove (gdbarch, address);
10278 record_line (current_subfile, 0, addr);
10279 }
59205f5a 10280 }
c906108c 10281 }
aaa75496
JB
10282
10283 if (decode_for_pst_p)
10284 {
10285 int file_index;
10286
10287 /* Now that we're done scanning the Line Header Program, we can
10288 create the psymtab of each included file. */
10289 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10290 if (lh->file_names[file_index].included_p == 1)
10291 {
c6da4cef
DE
10292 char *include_name =
10293 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10294 if (include_name != NULL)
aaa75496
JB
10295 dwarf2_create_include_psymtab (include_name, pst, objfile);
10296 }
10297 }
cb1df416
DJ
10298 else
10299 {
10300 /* Make sure a symtab is created for every file, even files
10301 which contain only variables (i.e. no code with associated
10302 line numbers). */
10303
10304 int i;
10305 struct file_entry *fe;
10306
10307 for (i = 0; i < lh->num_file_names; i++)
10308 {
10309 char *dir = NULL;
9a619af0 10310
cb1df416
DJ
10311 fe = &lh->file_names[i];
10312 if (fe->dir_index)
10313 dir = lh->include_dirs[fe->dir_index - 1];
10314 dwarf2_start_subfile (fe->name, dir, comp_dir);
10315
10316 /* Skip the main file; we don't need it, and it must be
10317 allocated last, so that it will show up before the
10318 non-primary symtabs in the objfile's symtab list. */
10319 if (current_subfile == first_subfile)
10320 continue;
10321
10322 if (current_subfile->symtab == NULL)
10323 current_subfile->symtab = allocate_symtab (current_subfile->name,
10324 cu->objfile);
10325 fe->symtab = current_subfile->symtab;
10326 }
10327 }
c906108c
SS
10328}
10329
10330/* Start a subfile for DWARF. FILENAME is the name of the file and
10331 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10332 or NULL if not known. COMP_DIR is the compilation directory for the
10333 linetable's compilation unit or NULL if not known.
c906108c
SS
10334 This routine tries to keep line numbers from identical absolute and
10335 relative file names in a common subfile.
10336
10337 Using the `list' example from the GDB testsuite, which resides in
10338 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10339 of /srcdir/list0.c yields the following debugging information for list0.c:
10340
c5aa993b
JM
10341 DW_AT_name: /srcdir/list0.c
10342 DW_AT_comp_dir: /compdir
357e46e7 10343 files.files[0].name: list0.h
c5aa993b 10344 files.files[0].dir: /srcdir
357e46e7 10345 files.files[1].name: list0.c
c5aa993b 10346 files.files[1].dir: /srcdir
c906108c
SS
10347
10348 The line number information for list0.c has to end up in a single
4f1520fb
FR
10349 subfile, so that `break /srcdir/list0.c:1' works as expected.
10350 start_subfile will ensure that this happens provided that we pass the
10351 concatenation of files.files[1].dir and files.files[1].name as the
10352 subfile's name. */
c906108c
SS
10353
10354static void
4f1520fb 10355dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10356{
4f1520fb
FR
10357 char *fullname;
10358
10359 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10360 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10361 second argument to start_subfile. To be consistent, we do the
10362 same here. In order not to lose the line information directory,
10363 we concatenate it to the filename when it makes sense.
10364 Note that the Dwarf3 standard says (speaking of filenames in line
10365 information): ``The directory index is ignored for file names
10366 that represent full path names''. Thus ignoring dirname in the
10367 `else' branch below isn't an issue. */
c906108c 10368
d5166ae1 10369 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10370 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10371 else
10372 fullname = filename;
c906108c 10373
4f1520fb
FR
10374 start_subfile (fullname, comp_dir);
10375
10376 if (fullname != filename)
10377 xfree (fullname);
c906108c
SS
10378}
10379
4c2df51b
DJ
10380static void
10381var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10382 struct dwarf2_cu *cu)
4c2df51b 10383{
e7c27a73
DJ
10384 struct objfile *objfile = cu->objfile;
10385 struct comp_unit_head *cu_header = &cu->header;
10386
4c2df51b
DJ
10387 /* NOTE drow/2003-01-30: There used to be a comment and some special
10388 code here to turn a symbol with DW_AT_external and a
10389 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10390 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10391 with some versions of binutils) where shared libraries could have
10392 relocations against symbols in their debug information - the
10393 minimal symbol would have the right address, but the debug info
10394 would not. It's no longer necessary, because we will explicitly
10395 apply relocations when we read in the debug information now. */
10396
10397 /* A DW_AT_location attribute with no contents indicates that a
10398 variable has been optimized away. */
10399 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10400 {
10401 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10402 return;
10403 }
10404
10405 /* Handle one degenerate form of location expression specially, to
10406 preserve GDB's previous behavior when section offsets are
10407 specified. If this is just a DW_OP_addr then mark this symbol
10408 as LOC_STATIC. */
10409
10410 if (attr_form_is_block (attr)
10411 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10412 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10413 {
891d2f0b 10414 unsigned int dummy;
4c2df51b
DJ
10415
10416 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10417 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10418 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10419 fixup_symbol_section (sym, objfile);
10420 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10421 SYMBOL_SECTION (sym));
4c2df51b
DJ
10422 return;
10423 }
10424
10425 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10426 expression evaluator, and use LOC_COMPUTED only when necessary
10427 (i.e. when the value of a register or memory location is
10428 referenced, or a thread-local block, etc.). Then again, it might
10429 not be worthwhile. I'm assuming that it isn't unless performance
10430 or memory numbers show me otherwise. */
10431
e7c27a73 10432 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10433 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10434}
10435
c906108c
SS
10436/* Given a pointer to a DWARF information entry, figure out if we need
10437 to make a symbol table entry for it, and if so, create a new entry
10438 and return a pointer to it.
10439 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10440 used the passed type.
10441 If SPACE is not NULL, use it to hold the new symbol. If it is
10442 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10443
10444static struct symbol *
34eaf542
TT
10445new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10446 struct symbol *space)
c906108c 10447{
e7c27a73 10448 struct objfile *objfile = cu->objfile;
c906108c
SS
10449 struct symbol *sym = NULL;
10450 char *name;
10451 struct attribute *attr = NULL;
10452 struct attribute *attr2 = NULL;
e142c38c 10453 CORE_ADDR baseaddr;
e37fd15a
SW
10454 struct pending **list_to_add = NULL;
10455
edb3359d 10456 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10457
10458 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10459
94af9270 10460 name = dwarf2_name (die, cu);
c906108c
SS
10461 if (name)
10462 {
94af9270 10463 const char *linkagename;
34eaf542 10464 int suppress_add = 0;
94af9270 10465
34eaf542
TT
10466 if (space)
10467 sym = space;
10468 else
10469 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10470 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10471
10472 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10473 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10474 linkagename = dwarf2_physname (name, die, cu);
10475 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10476
f55ee35c
JK
10477 /* Fortran does not have mangling standard and the mangling does differ
10478 between gfortran, iFort etc. */
10479 if (cu->language == language_fortran
b250c185 10480 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10481 symbol_set_demangled_name (&(sym->ginfo),
10482 (char *) dwarf2_full_name (name, die, cu),
10483 NULL);
f55ee35c 10484
c906108c 10485 /* Default assumptions.
c5aa993b 10486 Use the passed type or decode it from the die. */
176620f1 10487 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10488 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10489 if (type != NULL)
10490 SYMBOL_TYPE (sym) = type;
10491 else
e7c27a73 10492 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10493 attr = dwarf2_attr (die,
10494 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10495 cu);
c906108c
SS
10496 if (attr)
10497 {
10498 SYMBOL_LINE (sym) = DW_UNSND (attr);
10499 }
cb1df416 10500
edb3359d
DJ
10501 attr = dwarf2_attr (die,
10502 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10503 cu);
cb1df416
DJ
10504 if (attr)
10505 {
10506 int file_index = DW_UNSND (attr);
9a619af0 10507
cb1df416
DJ
10508 if (cu->line_header == NULL
10509 || file_index > cu->line_header->num_file_names)
10510 complaint (&symfile_complaints,
10511 _("file index out of range"));
1c3d648d 10512 else if (file_index > 0)
cb1df416
DJ
10513 {
10514 struct file_entry *fe;
9a619af0 10515
cb1df416
DJ
10516 fe = &cu->line_header->file_names[file_index - 1];
10517 SYMBOL_SYMTAB (sym) = fe->symtab;
10518 }
10519 }
10520
c906108c
SS
10521 switch (die->tag)
10522 {
10523 case DW_TAG_label:
e142c38c 10524 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10525 if (attr)
10526 {
10527 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10528 }
0f5238ed
TT
10529 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10530 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10531 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10532 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10533 break;
10534 case DW_TAG_subprogram:
10535 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10536 finish_block. */
10537 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10538 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10539 if ((attr2 && (DW_UNSND (attr2) != 0))
10540 || cu->language == language_ada)
c906108c 10541 {
2cfa0c8d
JB
10542 /* Subprograms marked external are stored as a global symbol.
10543 Ada subprograms, whether marked external or not, are always
10544 stored as a global symbol, because we want to be able to
10545 access them globally. For instance, we want to be able
10546 to break on a nested subprogram without having to
10547 specify the context. */
e37fd15a 10548 list_to_add = &global_symbols;
c906108c
SS
10549 }
10550 else
10551 {
e37fd15a 10552 list_to_add = cu->list_in_scope;
c906108c
SS
10553 }
10554 break;
edb3359d
DJ
10555 case DW_TAG_inlined_subroutine:
10556 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10557 finish_block. */
10558 SYMBOL_CLASS (sym) = LOC_BLOCK;
10559 SYMBOL_INLINED (sym) = 1;
10560 /* Do not add the symbol to any lists. It will be found via
10561 BLOCK_FUNCTION from the blockvector. */
10562 break;
34eaf542
TT
10563 case DW_TAG_template_value_param:
10564 suppress_add = 1;
10565 /* Fall through. */
c906108c 10566 case DW_TAG_variable:
254e6b9e 10567 case DW_TAG_member:
c906108c
SS
10568 /* Compilation with minimal debug info may result in variables
10569 with missing type entries. Change the misleading `void' type
10570 to something sensible. */
10571 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10572 SYMBOL_TYPE (sym)
46bf5051 10573 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10574
e142c38c 10575 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10576 /* In the case of DW_TAG_member, we should only be called for
10577 static const members. */
10578 if (die->tag == DW_TAG_member)
10579 {
3863f96c
DE
10580 /* dwarf2_add_field uses die_is_declaration,
10581 so we do the same. */
254e6b9e
DE
10582 gdb_assert (die_is_declaration (die, cu));
10583 gdb_assert (attr);
10584 }
c906108c
SS
10585 if (attr)
10586 {
e7c27a73 10587 dwarf2_const_value (attr, sym, cu);
e142c38c 10588 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10589 if (!suppress_add)
34eaf542
TT
10590 {
10591 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10592 list_to_add = &global_symbols;
34eaf542 10593 else
e37fd15a 10594 list_to_add = cu->list_in_scope;
34eaf542 10595 }
c906108c
SS
10596 break;
10597 }
e142c38c 10598 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10599 if (attr)
10600 {
e7c27a73 10601 var_decode_location (attr, sym, cu);
e142c38c 10602 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10603 if (SYMBOL_CLASS (sym) == LOC_STATIC
10604 && SYMBOL_VALUE_ADDRESS (sym) == 0
10605 && !dwarf2_per_objfile->has_section_at_zero)
10606 {
10607 /* When a static variable is eliminated by the linker,
10608 the corresponding debug information is not stripped
10609 out, but the variable address is set to null;
10610 do not add such variables into symbol table. */
10611 }
10612 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10613 {
f55ee35c
JK
10614 /* Workaround gfortran PR debug/40040 - it uses
10615 DW_AT_location for variables in -fPIC libraries which may
10616 get overriden by other libraries/executable and get
10617 a different address. Resolve it by the minimal symbol
10618 which may come from inferior's executable using copy
10619 relocation. Make this workaround only for gfortran as for
10620 other compilers GDB cannot guess the minimal symbol
10621 Fortran mangling kind. */
10622 if (cu->language == language_fortran && die->parent
10623 && die->parent->tag == DW_TAG_module
10624 && cu->producer
10625 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10626 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10627
1c809c68
TT
10628 /* A variable with DW_AT_external is never static,
10629 but it may be block-scoped. */
10630 list_to_add = (cu->list_in_scope == &file_symbols
10631 ? &global_symbols : cu->list_in_scope);
1c809c68 10632 }
c906108c 10633 else
e37fd15a 10634 list_to_add = cu->list_in_scope;
c906108c
SS
10635 }
10636 else
10637 {
10638 /* We do not know the address of this symbol.
c5aa993b
JM
10639 If it is an external symbol and we have type information
10640 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10641 The address of the variable will then be determined from
10642 the minimal symbol table whenever the variable is
10643 referenced. */
e142c38c 10644 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10645 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10646 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10647 {
0fe7935b
DJ
10648 /* A variable with DW_AT_external is never static, but it
10649 may be block-scoped. */
10650 list_to_add = (cu->list_in_scope == &file_symbols
10651 ? &global_symbols : cu->list_in_scope);
10652
c906108c 10653 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10654 }
442ddf59
JK
10655 else if (!die_is_declaration (die, cu))
10656 {
10657 /* Use the default LOC_OPTIMIZED_OUT class. */
10658 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10659 if (!suppress_add)
10660 list_to_add = cu->list_in_scope;
442ddf59 10661 }
c906108c
SS
10662 }
10663 break;
10664 case DW_TAG_formal_parameter:
edb3359d
DJ
10665 /* If we are inside a function, mark this as an argument. If
10666 not, we might be looking at an argument to an inlined function
10667 when we do not have enough information to show inlined frames;
10668 pretend it's a local variable in that case so that the user can
10669 still see it. */
10670 if (context_stack_depth > 0
10671 && context_stack[context_stack_depth - 1].name != NULL)
10672 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10673 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10674 if (attr)
10675 {
e7c27a73 10676 var_decode_location (attr, sym, cu);
c906108c 10677 }
e142c38c 10678 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10679 if (attr)
10680 {
e7c27a73 10681 dwarf2_const_value (attr, sym, cu);
c906108c 10682 }
f346a30d
PM
10683 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10684 if (attr && DW_UNSND (attr))
10685 {
10686 struct type *ref_type;
10687
10688 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10689 SYMBOL_TYPE (sym) = ref_type;
10690 }
10691
e37fd15a 10692 list_to_add = cu->list_in_scope;
c906108c
SS
10693 break;
10694 case DW_TAG_unspecified_parameters:
10695 /* From varargs functions; gdb doesn't seem to have any
10696 interest in this information, so just ignore it for now.
10697 (FIXME?) */
10698 break;
34eaf542
TT
10699 case DW_TAG_template_type_param:
10700 suppress_add = 1;
10701 /* Fall through. */
c906108c 10702 case DW_TAG_class_type:
680b30c7 10703 case DW_TAG_interface_type:
c906108c
SS
10704 case DW_TAG_structure_type:
10705 case DW_TAG_union_type:
72019c9c 10706 case DW_TAG_set_type:
c906108c
SS
10707 case DW_TAG_enumeration_type:
10708 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10709 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10710
63d06c5c 10711 {
987504bb 10712 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10713 really ever be static objects: otherwise, if you try
10714 to, say, break of a class's method and you're in a file
10715 which doesn't mention that class, it won't work unless
10716 the check for all static symbols in lookup_symbol_aux
10717 saves you. See the OtherFileClass tests in
10718 gdb.c++/namespace.exp. */
10719
e37fd15a 10720 if (!suppress_add)
34eaf542 10721 {
34eaf542
TT
10722 list_to_add = (cu->list_in_scope == &file_symbols
10723 && (cu->language == language_cplus
10724 || cu->language == language_java)
10725 ? &global_symbols : cu->list_in_scope);
34eaf542 10726 }
63d06c5c
DC
10727
10728 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 10729 defines a typedef for "foo". A Java class declaration also
5eeb2539 10730 defines a typedef for the class. */
987504bb 10731 if (cu->language == language_cplus
8c6860bb
JB
10732 || cu->language == language_java
10733 || cu->language == language_ada)
63d06c5c 10734 {
d8151005
DJ
10735 /* The symbol's name is already allocated along with
10736 this objfile, so we don't need to duplicate it for
10737 the type. */
63d06c5c 10738 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 10739 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
10740 }
10741 }
c906108c
SS
10742 break;
10743 case DW_TAG_typedef:
63d06c5c
DC
10744 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10745 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10746 list_to_add = cu->list_in_scope;
63d06c5c 10747 break;
c906108c 10748 case DW_TAG_base_type:
a02abb62 10749 case DW_TAG_subrange_type:
c906108c 10750 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10751 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10752 list_to_add = cu->list_in_scope;
c906108c
SS
10753 break;
10754 case DW_TAG_enumerator:
e142c38c 10755 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10756 if (attr)
10757 {
e7c27a73 10758 dwarf2_const_value (attr, sym, cu);
c906108c 10759 }
63d06c5c
DC
10760 {
10761 /* NOTE: carlton/2003-11-10: See comment above in the
10762 DW_TAG_class_type, etc. block. */
10763
e142c38c 10764 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10765 && (cu->language == language_cplus
10766 || cu->language == language_java)
e142c38c 10767 ? &global_symbols : cu->list_in_scope);
63d06c5c 10768 }
c906108c 10769 break;
5c4e30ca
DC
10770 case DW_TAG_namespace:
10771 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10772 list_to_add = &global_symbols;
5c4e30ca 10773 break;
c906108c
SS
10774 default:
10775 /* Not a tag we recognize. Hopefully we aren't processing
10776 trash data, but since we must specifically ignore things
10777 we don't recognize, there is nothing else we should do at
10778 this point. */
e2e0b3e5 10779 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10780 dwarf_tag_name (die->tag));
c906108c
SS
10781 break;
10782 }
df8a16a1 10783
e37fd15a
SW
10784 if (suppress_add)
10785 {
10786 sym->hash_next = objfile->template_symbols;
10787 objfile->template_symbols = sym;
10788 list_to_add = NULL;
10789 }
10790
10791 if (list_to_add != NULL)
10792 add_symbol_to_list (sym, list_to_add);
10793
df8a16a1
DJ
10794 /* For the benefit of old versions of GCC, check for anonymous
10795 namespaces based on the demangled name. */
10796 if (!processing_has_namespace_info
94af9270 10797 && cu->language == language_cplus)
df8a16a1 10798 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10799 }
10800 return (sym);
10801}
10802
34eaf542
TT
10803/* A wrapper for new_symbol_full that always allocates a new symbol. */
10804
10805static struct symbol *
10806new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10807{
10808 return new_symbol_full (die, type, cu, NULL);
10809}
10810
98bfdba5
PA
10811/* Given an attr with a DW_FORM_dataN value in host byte order,
10812 zero-extend it as appropriate for the symbol's type. The DWARF
10813 standard (v4) is not entirely clear about the meaning of using
10814 DW_FORM_dataN for a constant with a signed type, where the type is
10815 wider than the data. The conclusion of a discussion on the DWARF
10816 list was that this is unspecified. We choose to always zero-extend
10817 because that is the interpretation long in use by GCC. */
c906108c 10818
98bfdba5
PA
10819static gdb_byte *
10820dwarf2_const_value_data (struct attribute *attr, struct type *type,
10821 const char *name, struct obstack *obstack,
10822 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10823{
e7c27a73 10824 struct objfile *objfile = cu->objfile;
e17a4113
UW
10825 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10826 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10827 LONGEST l = DW_UNSND (attr);
10828
10829 if (bits < sizeof (*value) * 8)
10830 {
10831 l &= ((LONGEST) 1 << bits) - 1;
10832 *value = l;
10833 }
10834 else if (bits == sizeof (*value) * 8)
10835 *value = l;
10836 else
10837 {
10838 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10839 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10840 return bytes;
10841 }
10842
10843 return NULL;
10844}
10845
10846/* Read a constant value from an attribute. Either set *VALUE, or if
10847 the value does not fit in *VALUE, set *BYTES - either already
10848 allocated on the objfile obstack, or newly allocated on OBSTACK,
10849 or, set *BATON, if we translated the constant to a location
10850 expression. */
10851
10852static void
10853dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10854 const char *name, struct obstack *obstack,
10855 struct dwarf2_cu *cu,
10856 long *value, gdb_byte **bytes,
10857 struct dwarf2_locexpr_baton **baton)
10858{
10859 struct objfile *objfile = cu->objfile;
10860 struct comp_unit_head *cu_header = &cu->header;
c906108c 10861 struct dwarf_block *blk;
98bfdba5
PA
10862 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10863 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10864
10865 *value = 0;
10866 *bytes = NULL;
10867 *baton = NULL;
c906108c
SS
10868
10869 switch (attr->form)
10870 {
10871 case DW_FORM_addr:
ac56253d 10872 {
ac56253d
TT
10873 gdb_byte *data;
10874
98bfdba5
PA
10875 if (TYPE_LENGTH (type) != cu_header->addr_size)
10876 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10877 cu_header->addr_size,
98bfdba5 10878 TYPE_LENGTH (type));
ac56253d
TT
10879 /* Symbols of this form are reasonably rare, so we just
10880 piggyback on the existing location code rather than writing
10881 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10882 *baton = obstack_alloc (&objfile->objfile_obstack,
10883 sizeof (struct dwarf2_locexpr_baton));
10884 (*baton)->per_cu = cu->per_cu;
10885 gdb_assert ((*baton)->per_cu);
ac56253d 10886
98bfdba5
PA
10887 (*baton)->size = 2 + cu_header->addr_size;
10888 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10889 (*baton)->data = data;
ac56253d
TT
10890
10891 data[0] = DW_OP_addr;
10892 store_unsigned_integer (&data[1], cu_header->addr_size,
10893 byte_order, DW_ADDR (attr));
10894 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10895 }
c906108c 10896 break;
4ac36638 10897 case DW_FORM_string:
93b5768b 10898 case DW_FORM_strp:
98bfdba5
PA
10899 /* DW_STRING is already allocated on the objfile obstack, point
10900 directly to it. */
10901 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10902 break;
c906108c
SS
10903 case DW_FORM_block1:
10904 case DW_FORM_block2:
10905 case DW_FORM_block4:
10906 case DW_FORM_block:
2dc7f7b3 10907 case DW_FORM_exprloc:
c906108c 10908 blk = DW_BLOCK (attr);
98bfdba5
PA
10909 if (TYPE_LENGTH (type) != blk->size)
10910 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10911 TYPE_LENGTH (type));
10912 *bytes = blk->data;
c906108c 10913 break;
2df3850c
JM
10914
10915 /* The DW_AT_const_value attributes are supposed to carry the
10916 symbol's value "represented as it would be on the target
10917 architecture." By the time we get here, it's already been
10918 converted to host endianness, so we just need to sign- or
10919 zero-extend it as appropriate. */
10920 case DW_FORM_data1:
98bfdba5 10921 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10922 break;
c906108c 10923 case DW_FORM_data2:
98bfdba5 10924 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10925 break;
c906108c 10926 case DW_FORM_data4:
98bfdba5 10927 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10928 break;
c906108c 10929 case DW_FORM_data8:
98bfdba5 10930 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
10931 break;
10932
c906108c 10933 case DW_FORM_sdata:
98bfdba5 10934 *value = DW_SND (attr);
2df3850c
JM
10935 break;
10936
c906108c 10937 case DW_FORM_udata:
98bfdba5 10938 *value = DW_UNSND (attr);
c906108c 10939 break;
2df3850c 10940
c906108c 10941 default:
4d3c2250 10942 complaint (&symfile_complaints,
e2e0b3e5 10943 _("unsupported const value attribute form: '%s'"),
4d3c2250 10944 dwarf_form_name (attr->form));
98bfdba5 10945 *value = 0;
c906108c
SS
10946 break;
10947 }
10948}
10949
2df3850c 10950
98bfdba5
PA
10951/* Copy constant value from an attribute to a symbol. */
10952
2df3850c 10953static void
98bfdba5
PA
10954dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10955 struct dwarf2_cu *cu)
2df3850c 10956{
98bfdba5
PA
10957 struct objfile *objfile = cu->objfile;
10958 struct comp_unit_head *cu_header = &cu->header;
10959 long value;
10960 gdb_byte *bytes;
10961 struct dwarf2_locexpr_baton *baton;
2df3850c 10962
98bfdba5
PA
10963 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10964 SYMBOL_PRINT_NAME (sym),
10965 &objfile->objfile_obstack, cu,
10966 &value, &bytes, &baton);
2df3850c 10967
98bfdba5
PA
10968 if (baton != NULL)
10969 {
10970 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10971 SYMBOL_LOCATION_BATON (sym) = baton;
10972 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10973 }
10974 else if (bytes != NULL)
10975 {
10976 SYMBOL_VALUE_BYTES (sym) = bytes;
10977 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10978 }
10979 else
10980 {
10981 SYMBOL_VALUE (sym) = value;
10982 SYMBOL_CLASS (sym) = LOC_CONST;
10983 }
2df3850c
JM
10984}
10985
c906108c
SS
10986/* Return the type of the die in question using its DW_AT_type attribute. */
10987
10988static struct type *
e7c27a73 10989die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10990{
c906108c 10991 struct attribute *type_attr;
c906108c 10992
e142c38c 10993 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10994 if (!type_attr)
10995 {
10996 /* A missing DW_AT_type represents a void type. */
46bf5051 10997 return objfile_type (cu->objfile)->builtin_void;
c906108c 10998 }
348e048f 10999
673bfd45 11000 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11001}
11002
b4ba55a1
JB
11003/* True iff CU's producer generates GNAT Ada auxiliary information
11004 that allows to find parallel types through that information instead
11005 of having to do expensive parallel lookups by type name. */
11006
11007static int
11008need_gnat_info (struct dwarf2_cu *cu)
11009{
11010 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11011 of GNAT produces this auxiliary information, without any indication
11012 that it is produced. Part of enhancing the FSF version of GNAT
11013 to produce that information will be to put in place an indicator
11014 that we can use in order to determine whether the descriptive type
11015 info is available or not. One suggestion that has been made is
11016 to use a new attribute, attached to the CU die. For now, assume
11017 that the descriptive type info is not available. */
11018 return 0;
11019}
11020
b4ba55a1
JB
11021/* Return the auxiliary type of the die in question using its
11022 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11023 attribute is not present. */
11024
11025static struct type *
11026die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11027{
b4ba55a1 11028 struct attribute *type_attr;
b4ba55a1
JB
11029
11030 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11031 if (!type_attr)
11032 return NULL;
11033
673bfd45 11034 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11035}
11036
11037/* If DIE has a descriptive_type attribute, then set the TYPE's
11038 descriptive type accordingly. */
11039
11040static void
11041set_descriptive_type (struct type *type, struct die_info *die,
11042 struct dwarf2_cu *cu)
11043{
11044 struct type *descriptive_type = die_descriptive_type (die, cu);
11045
11046 if (descriptive_type)
11047 {
11048 ALLOCATE_GNAT_AUX_TYPE (type);
11049 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11050 }
11051}
11052
c906108c
SS
11053/* Return the containing type of the die in question using its
11054 DW_AT_containing_type attribute. */
11055
11056static struct type *
e7c27a73 11057die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11058{
c906108c 11059 struct attribute *type_attr;
c906108c 11060
e142c38c 11061 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11062 if (!type_attr)
11063 error (_("Dwarf Error: Problem turning containing type into gdb type "
11064 "[in module %s]"), cu->objfile->name);
11065
673bfd45 11066 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11067}
11068
673bfd45
DE
11069/* Look up the type of DIE in CU using its type attribute ATTR.
11070 If there is no type substitute an error marker. */
11071
c906108c 11072static struct type *
673bfd45
DE
11073lookup_die_type (struct die_info *die, struct attribute *attr,
11074 struct dwarf2_cu *cu)
c906108c 11075{
f792889a
DJ
11076 struct type *this_type;
11077
673bfd45
DE
11078 /* First see if we have it cached. */
11079
11080 if (is_ref_attr (attr))
11081 {
11082 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11083
11084 this_type = get_die_type_at_offset (offset, cu->per_cu);
11085 }
11086 else if (attr->form == DW_FORM_sig8)
11087 {
11088 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11089 struct dwarf2_cu *sig_cu;
11090 unsigned int offset;
11091
11092 /* sig_type will be NULL if the signatured type is missing from
11093 the debug info. */
11094 if (sig_type == NULL)
11095 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11096 "at 0x%x [in module %s]"),
11097 die->offset, cu->objfile->name);
11098
11099 gdb_assert (sig_type->per_cu.from_debug_types);
11100 offset = sig_type->offset + sig_type->type_offset;
11101 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11102 }
11103 else
11104 {
11105 dump_die_for_error (die);
11106 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11107 dwarf_attr_name (attr->name), cu->objfile->name);
11108 }
11109
11110 /* If not cached we need to read it in. */
11111
11112 if (this_type == NULL)
11113 {
11114 struct die_info *type_die;
11115 struct dwarf2_cu *type_cu = cu;
11116
11117 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11118 /* If the type is cached, we should have found it above. */
11119 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11120 this_type = read_type_die_1 (type_die, type_cu);
11121 }
11122
11123 /* If we still don't have a type use an error marker. */
11124
11125 if (this_type == NULL)
c906108c 11126 {
b00fdb78
TT
11127 char *message, *saved;
11128
11129 /* read_type_die already issued a complaint. */
11130 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11131 cu->objfile->name,
11132 cu->header.offset,
11133 die->offset);
11134 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11135 message, strlen (message));
11136 xfree (message);
11137
11138 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11139 }
673bfd45 11140
f792889a 11141 return this_type;
c906108c
SS
11142}
11143
673bfd45
DE
11144/* Return the type in DIE, CU.
11145 Returns NULL for invalid types.
11146
11147 This first does a lookup in the appropriate type_hash table,
11148 and only reads the die in if necessary.
11149
11150 NOTE: This can be called when reading in partial or full symbols. */
11151
f792889a 11152static struct type *
e7c27a73 11153read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11154{
f792889a
DJ
11155 struct type *this_type;
11156
11157 this_type = get_die_type (die, cu);
11158 if (this_type)
11159 return this_type;
11160
673bfd45
DE
11161 return read_type_die_1 (die, cu);
11162}
11163
11164/* Read the type in DIE, CU.
11165 Returns NULL for invalid types. */
11166
11167static struct type *
11168read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11169{
11170 struct type *this_type = NULL;
11171
c906108c
SS
11172 switch (die->tag)
11173 {
11174 case DW_TAG_class_type:
680b30c7 11175 case DW_TAG_interface_type:
c906108c
SS
11176 case DW_TAG_structure_type:
11177 case DW_TAG_union_type:
f792889a 11178 this_type = read_structure_type (die, cu);
c906108c
SS
11179 break;
11180 case DW_TAG_enumeration_type:
f792889a 11181 this_type = read_enumeration_type (die, cu);
c906108c
SS
11182 break;
11183 case DW_TAG_subprogram:
11184 case DW_TAG_subroutine_type:
edb3359d 11185 case DW_TAG_inlined_subroutine:
f792889a 11186 this_type = read_subroutine_type (die, cu);
c906108c
SS
11187 break;
11188 case DW_TAG_array_type:
f792889a 11189 this_type = read_array_type (die, cu);
c906108c 11190 break;
72019c9c 11191 case DW_TAG_set_type:
f792889a 11192 this_type = read_set_type (die, cu);
72019c9c 11193 break;
c906108c 11194 case DW_TAG_pointer_type:
f792889a 11195 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11196 break;
11197 case DW_TAG_ptr_to_member_type:
f792889a 11198 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11199 break;
11200 case DW_TAG_reference_type:
f792889a 11201 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11202 break;
11203 case DW_TAG_const_type:
f792889a 11204 this_type = read_tag_const_type (die, cu);
c906108c
SS
11205 break;
11206 case DW_TAG_volatile_type:
f792889a 11207 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11208 break;
11209 case DW_TAG_string_type:
f792889a 11210 this_type = read_tag_string_type (die, cu);
c906108c
SS
11211 break;
11212 case DW_TAG_typedef:
f792889a 11213 this_type = read_typedef (die, cu);
c906108c 11214 break;
a02abb62 11215 case DW_TAG_subrange_type:
f792889a 11216 this_type = read_subrange_type (die, cu);
a02abb62 11217 break;
c906108c 11218 case DW_TAG_base_type:
f792889a 11219 this_type = read_base_type (die, cu);
c906108c 11220 break;
81a17f79 11221 case DW_TAG_unspecified_type:
f792889a 11222 this_type = read_unspecified_type (die, cu);
81a17f79 11223 break;
0114d602
DJ
11224 case DW_TAG_namespace:
11225 this_type = read_namespace_type (die, cu);
11226 break;
f55ee35c
JK
11227 case DW_TAG_module:
11228 this_type = read_module_type (die, cu);
11229 break;
c906108c 11230 default:
a1f5b845 11231 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11232 dwarf_tag_name (die->tag));
c906108c
SS
11233 break;
11234 }
63d06c5c 11235
f792889a 11236 return this_type;
63d06c5c
DC
11237}
11238
fdde2d81 11239/* Return the name of the namespace/class that DIE is defined within,
0114d602 11240 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11241
0114d602
DJ
11242 For example, if we're within the method foo() in the following
11243 code:
11244
11245 namespace N {
11246 class C {
11247 void foo () {
11248 }
11249 };
11250 }
11251
11252 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11253
11254static char *
e142c38c 11255determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11256{
0114d602
DJ
11257 struct die_info *parent, *spec_die;
11258 struct dwarf2_cu *spec_cu;
11259 struct type *parent_type;
63d06c5c 11260
f55ee35c
JK
11261 if (cu->language != language_cplus && cu->language != language_java
11262 && cu->language != language_fortran)
0114d602
DJ
11263 return "";
11264
11265 /* We have to be careful in the presence of DW_AT_specification.
11266 For example, with GCC 3.4, given the code
11267
11268 namespace N {
11269 void foo() {
11270 // Definition of N::foo.
11271 }
11272 }
11273
11274 then we'll have a tree of DIEs like this:
11275
11276 1: DW_TAG_compile_unit
11277 2: DW_TAG_namespace // N
11278 3: DW_TAG_subprogram // declaration of N::foo
11279 4: DW_TAG_subprogram // definition of N::foo
11280 DW_AT_specification // refers to die #3
11281
11282 Thus, when processing die #4, we have to pretend that we're in
11283 the context of its DW_AT_specification, namely the contex of die
11284 #3. */
11285 spec_cu = cu;
11286 spec_die = die_specification (die, &spec_cu);
11287 if (spec_die == NULL)
11288 parent = die->parent;
11289 else
63d06c5c 11290 {
0114d602
DJ
11291 parent = spec_die->parent;
11292 cu = spec_cu;
63d06c5c 11293 }
0114d602
DJ
11294
11295 if (parent == NULL)
11296 return "";
98bfdba5
PA
11297 else if (parent->building_fullname)
11298 {
11299 const char *name;
11300 const char *parent_name;
11301
11302 /* It has been seen on RealView 2.2 built binaries,
11303 DW_TAG_template_type_param types actually _defined_ as
11304 children of the parent class:
11305
11306 enum E {};
11307 template class <class Enum> Class{};
11308 Class<enum E> class_e;
11309
11310 1: DW_TAG_class_type (Class)
11311 2: DW_TAG_enumeration_type (E)
11312 3: DW_TAG_enumerator (enum1:0)
11313 3: DW_TAG_enumerator (enum2:1)
11314 ...
11315 2: DW_TAG_template_type_param
11316 DW_AT_type DW_FORM_ref_udata (E)
11317
11318 Besides being broken debug info, it can put GDB into an
11319 infinite loop. Consider:
11320
11321 When we're building the full name for Class<E>, we'll start
11322 at Class, and go look over its template type parameters,
11323 finding E. We'll then try to build the full name of E, and
11324 reach here. We're now trying to build the full name of E,
11325 and look over the parent DIE for containing scope. In the
11326 broken case, if we followed the parent DIE of E, we'd again
11327 find Class, and once again go look at its template type
11328 arguments, etc., etc. Simply don't consider such parent die
11329 as source-level parent of this die (it can't be, the language
11330 doesn't allow it), and break the loop here. */
11331 name = dwarf2_name (die, cu);
11332 parent_name = dwarf2_name (parent, cu);
11333 complaint (&symfile_complaints,
11334 _("template param type '%s' defined within parent '%s'"),
11335 name ? name : "<unknown>",
11336 parent_name ? parent_name : "<unknown>");
11337 return "";
11338 }
63d06c5c 11339 else
0114d602
DJ
11340 switch (parent->tag)
11341 {
63d06c5c 11342 case DW_TAG_namespace:
0114d602 11343 parent_type = read_type_die (parent, cu);
acebe513
UW
11344 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11345 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11346 Work around this problem here. */
11347 if (cu->language == language_cplus
11348 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11349 return "";
0114d602
DJ
11350 /* We give a name to even anonymous namespaces. */
11351 return TYPE_TAG_NAME (parent_type);
63d06c5c 11352 case DW_TAG_class_type:
680b30c7 11353 case DW_TAG_interface_type:
63d06c5c 11354 case DW_TAG_structure_type:
0114d602 11355 case DW_TAG_union_type:
f55ee35c 11356 case DW_TAG_module:
0114d602
DJ
11357 parent_type = read_type_die (parent, cu);
11358 if (TYPE_TAG_NAME (parent_type) != NULL)
11359 return TYPE_TAG_NAME (parent_type);
11360 else
11361 /* An anonymous structure is only allowed non-static data
11362 members; no typedefs, no member functions, et cetera.
11363 So it does not need a prefix. */
11364 return "";
63d06c5c 11365 default:
8176b9b8 11366 return determine_prefix (parent, cu);
63d06c5c 11367 }
63d06c5c
DC
11368}
11369
987504bb
JJ
11370/* Return a newly-allocated string formed by concatenating PREFIX and
11371 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11372 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11373 perform an obconcat, otherwise allocate storage for the result. The CU argument
11374 is used to determine the language and hence, the appropriate separator. */
11375
f55ee35c 11376#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11377
11378static char *
f55ee35c
JK
11379typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11380 int physname, struct dwarf2_cu *cu)
63d06c5c 11381{
f55ee35c 11382 const char *lead = "";
5c315b68 11383 const char *sep;
63d06c5c 11384
987504bb
JJ
11385 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11386 sep = "";
11387 else if (cu->language == language_java)
11388 sep = ".";
f55ee35c
JK
11389 else if (cu->language == language_fortran && physname)
11390 {
11391 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11392 DW_AT_MIPS_linkage_name is preferred and used instead. */
11393
11394 lead = "__";
11395 sep = "_MOD_";
11396 }
987504bb
JJ
11397 else
11398 sep = "::";
63d06c5c 11399
6dd47d34
DE
11400 if (prefix == NULL)
11401 prefix = "";
11402 if (suffix == NULL)
11403 suffix = "";
11404
987504bb
JJ
11405 if (obs == NULL)
11406 {
11407 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11408
f55ee35c
JK
11409 strcpy (retval, lead);
11410 strcat (retval, prefix);
6dd47d34
DE
11411 strcat (retval, sep);
11412 strcat (retval, suffix);
63d06c5c
DC
11413 return retval;
11414 }
987504bb
JJ
11415 else
11416 {
11417 /* We have an obstack. */
f55ee35c 11418 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11419 }
63d06c5c
DC
11420}
11421
c906108c
SS
11422/* Return sibling of die, NULL if no sibling. */
11423
f9aca02d 11424static struct die_info *
fba45db2 11425sibling_die (struct die_info *die)
c906108c 11426{
639d11d3 11427 return die->sibling;
c906108c
SS
11428}
11429
71c25dea
TT
11430/* Get name of a die, return NULL if not found. */
11431
11432static char *
11433dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11434 struct obstack *obstack)
11435{
11436 if (name && cu->language == language_cplus)
11437 {
11438 char *canon_name = cp_canonicalize_string (name);
11439
11440 if (canon_name != NULL)
11441 {
11442 if (strcmp (canon_name, name) != 0)
11443 name = obsavestring (canon_name, strlen (canon_name),
11444 obstack);
11445 xfree (canon_name);
11446 }
11447 }
11448
11449 return name;
c906108c
SS
11450}
11451
9219021c
DC
11452/* Get name of a die, return NULL if not found. */
11453
11454static char *
e142c38c 11455dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11456{
11457 struct attribute *attr;
11458
e142c38c 11459 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11460 if (!attr || !DW_STRING (attr))
11461 return NULL;
11462
11463 switch (die->tag)
11464 {
11465 case DW_TAG_compile_unit:
11466 /* Compilation units have a DW_AT_name that is a filename, not
11467 a source language identifier. */
11468 case DW_TAG_enumeration_type:
11469 case DW_TAG_enumerator:
11470 /* These tags always have simple identifiers already; no need
11471 to canonicalize them. */
11472 return DW_STRING (attr);
907af001 11473
418835cc
KS
11474 case DW_TAG_subprogram:
11475 /* Java constructors will all be named "<init>", so return
11476 the class name when we see this special case. */
11477 if (cu->language == language_java
11478 && DW_STRING (attr) != NULL
11479 && strcmp (DW_STRING (attr), "<init>") == 0)
11480 {
11481 struct dwarf2_cu *spec_cu = cu;
11482 struct die_info *spec_die;
11483
11484 /* GCJ will output '<init>' for Java constructor names.
11485 For this special case, return the name of the parent class. */
11486
11487 /* GCJ may output suprogram DIEs with AT_specification set.
11488 If so, use the name of the specified DIE. */
11489 spec_die = die_specification (die, &spec_cu);
11490 if (spec_die != NULL)
11491 return dwarf2_name (spec_die, spec_cu);
11492
11493 do
11494 {
11495 die = die->parent;
11496 if (die->tag == DW_TAG_class_type)
11497 return dwarf2_name (die, cu);
11498 }
11499 while (die->tag != DW_TAG_compile_unit);
11500 }
907af001
UW
11501 break;
11502
11503 case DW_TAG_class_type:
11504 case DW_TAG_interface_type:
11505 case DW_TAG_structure_type:
11506 case DW_TAG_union_type:
11507 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11508 structures or unions. These were of the form "._%d" in GCC 4.1,
11509 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11510 and GCC 4.4. We work around this problem by ignoring these. */
11511 if (strncmp (DW_STRING (attr), "._", 2) == 0
11512 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11513 return NULL;
11514 break;
11515
71c25dea 11516 default:
907af001
UW
11517 break;
11518 }
11519
11520 if (!DW_STRING_IS_CANONICAL (attr))
11521 {
11522 DW_STRING (attr)
11523 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11524 &cu->objfile->objfile_obstack);
11525 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11526 }
907af001 11527 return DW_STRING (attr);
9219021c
DC
11528}
11529
11530/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11531 is none. *EXT_CU is the CU containing DIE on input, and the CU
11532 containing the return value on output. */
9219021c
DC
11533
11534static struct die_info *
f2f0e013 11535dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11536{
11537 struct attribute *attr;
9219021c 11538
f2f0e013 11539 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11540 if (attr == NULL)
11541 return NULL;
11542
f2f0e013 11543 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11544}
11545
c906108c
SS
11546/* Convert a DIE tag into its string name. */
11547
11548static char *
aa1ee363 11549dwarf_tag_name (unsigned tag)
c906108c
SS
11550{
11551 switch (tag)
11552 {
11553 case DW_TAG_padding:
11554 return "DW_TAG_padding";
11555 case DW_TAG_array_type:
11556 return "DW_TAG_array_type";
11557 case DW_TAG_class_type:
11558 return "DW_TAG_class_type";
11559 case DW_TAG_entry_point:
11560 return "DW_TAG_entry_point";
11561 case DW_TAG_enumeration_type:
11562 return "DW_TAG_enumeration_type";
11563 case DW_TAG_formal_parameter:
11564 return "DW_TAG_formal_parameter";
11565 case DW_TAG_imported_declaration:
11566 return "DW_TAG_imported_declaration";
11567 case DW_TAG_label:
11568 return "DW_TAG_label";
11569 case DW_TAG_lexical_block:
11570 return "DW_TAG_lexical_block";
11571 case DW_TAG_member:
11572 return "DW_TAG_member";
11573 case DW_TAG_pointer_type:
11574 return "DW_TAG_pointer_type";
11575 case DW_TAG_reference_type:
11576 return "DW_TAG_reference_type";
11577 case DW_TAG_compile_unit:
11578 return "DW_TAG_compile_unit";
11579 case DW_TAG_string_type:
11580 return "DW_TAG_string_type";
11581 case DW_TAG_structure_type:
11582 return "DW_TAG_structure_type";
11583 case DW_TAG_subroutine_type:
11584 return "DW_TAG_subroutine_type";
11585 case DW_TAG_typedef:
11586 return "DW_TAG_typedef";
11587 case DW_TAG_union_type:
11588 return "DW_TAG_union_type";
11589 case DW_TAG_unspecified_parameters:
11590 return "DW_TAG_unspecified_parameters";
11591 case DW_TAG_variant:
11592 return "DW_TAG_variant";
11593 case DW_TAG_common_block:
11594 return "DW_TAG_common_block";
11595 case DW_TAG_common_inclusion:
11596 return "DW_TAG_common_inclusion";
11597 case DW_TAG_inheritance:
11598 return "DW_TAG_inheritance";
11599 case DW_TAG_inlined_subroutine:
11600 return "DW_TAG_inlined_subroutine";
11601 case DW_TAG_module:
11602 return "DW_TAG_module";
11603 case DW_TAG_ptr_to_member_type:
11604 return "DW_TAG_ptr_to_member_type";
11605 case DW_TAG_set_type:
11606 return "DW_TAG_set_type";
11607 case DW_TAG_subrange_type:
11608 return "DW_TAG_subrange_type";
11609 case DW_TAG_with_stmt:
11610 return "DW_TAG_with_stmt";
11611 case DW_TAG_access_declaration:
11612 return "DW_TAG_access_declaration";
11613 case DW_TAG_base_type:
11614 return "DW_TAG_base_type";
11615 case DW_TAG_catch_block:
11616 return "DW_TAG_catch_block";
11617 case DW_TAG_const_type:
11618 return "DW_TAG_const_type";
11619 case DW_TAG_constant:
11620 return "DW_TAG_constant";
11621 case DW_TAG_enumerator:
11622 return "DW_TAG_enumerator";
11623 case DW_TAG_file_type:
11624 return "DW_TAG_file_type";
11625 case DW_TAG_friend:
11626 return "DW_TAG_friend";
11627 case DW_TAG_namelist:
11628 return "DW_TAG_namelist";
11629 case DW_TAG_namelist_item:
11630 return "DW_TAG_namelist_item";
11631 case DW_TAG_packed_type:
11632 return "DW_TAG_packed_type";
11633 case DW_TAG_subprogram:
11634 return "DW_TAG_subprogram";
11635 case DW_TAG_template_type_param:
11636 return "DW_TAG_template_type_param";
11637 case DW_TAG_template_value_param:
11638 return "DW_TAG_template_value_param";
11639 case DW_TAG_thrown_type:
11640 return "DW_TAG_thrown_type";
11641 case DW_TAG_try_block:
11642 return "DW_TAG_try_block";
11643 case DW_TAG_variant_part:
11644 return "DW_TAG_variant_part";
11645 case DW_TAG_variable:
11646 return "DW_TAG_variable";
11647 case DW_TAG_volatile_type:
11648 return "DW_TAG_volatile_type";
d9fa45fe
DC
11649 case DW_TAG_dwarf_procedure:
11650 return "DW_TAG_dwarf_procedure";
11651 case DW_TAG_restrict_type:
11652 return "DW_TAG_restrict_type";
11653 case DW_TAG_interface_type:
11654 return "DW_TAG_interface_type";
11655 case DW_TAG_namespace:
11656 return "DW_TAG_namespace";
11657 case DW_TAG_imported_module:
11658 return "DW_TAG_imported_module";
11659 case DW_TAG_unspecified_type:
11660 return "DW_TAG_unspecified_type";
11661 case DW_TAG_partial_unit:
11662 return "DW_TAG_partial_unit";
11663 case DW_TAG_imported_unit:
11664 return "DW_TAG_imported_unit";
b7619582
GF
11665 case DW_TAG_condition:
11666 return "DW_TAG_condition";
11667 case DW_TAG_shared_type:
11668 return "DW_TAG_shared_type";
348e048f
DE
11669 case DW_TAG_type_unit:
11670 return "DW_TAG_type_unit";
c906108c
SS
11671 case DW_TAG_MIPS_loop:
11672 return "DW_TAG_MIPS_loop";
b7619582
GF
11673 case DW_TAG_HP_array_descriptor:
11674 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11675 case DW_TAG_format_label:
11676 return "DW_TAG_format_label";
11677 case DW_TAG_function_template:
11678 return "DW_TAG_function_template";
11679 case DW_TAG_class_template:
11680 return "DW_TAG_class_template";
b7619582
GF
11681 case DW_TAG_GNU_BINCL:
11682 return "DW_TAG_GNU_BINCL";
11683 case DW_TAG_GNU_EINCL:
11684 return "DW_TAG_GNU_EINCL";
11685 case DW_TAG_upc_shared_type:
11686 return "DW_TAG_upc_shared_type";
11687 case DW_TAG_upc_strict_type:
11688 return "DW_TAG_upc_strict_type";
11689 case DW_TAG_upc_relaxed_type:
11690 return "DW_TAG_upc_relaxed_type";
11691 case DW_TAG_PGI_kanji_type:
11692 return "DW_TAG_PGI_kanji_type";
11693 case DW_TAG_PGI_interface_block:
11694 return "DW_TAG_PGI_interface_block";
c906108c
SS
11695 default:
11696 return "DW_TAG_<unknown>";
11697 }
11698}
11699
11700/* Convert a DWARF attribute code into its string name. */
11701
11702static char *
aa1ee363 11703dwarf_attr_name (unsigned attr)
c906108c
SS
11704{
11705 switch (attr)
11706 {
11707 case DW_AT_sibling:
11708 return "DW_AT_sibling";
11709 case DW_AT_location:
11710 return "DW_AT_location";
11711 case DW_AT_name:
11712 return "DW_AT_name";
11713 case DW_AT_ordering:
11714 return "DW_AT_ordering";
11715 case DW_AT_subscr_data:
11716 return "DW_AT_subscr_data";
11717 case DW_AT_byte_size:
11718 return "DW_AT_byte_size";
11719 case DW_AT_bit_offset:
11720 return "DW_AT_bit_offset";
11721 case DW_AT_bit_size:
11722 return "DW_AT_bit_size";
11723 case DW_AT_element_list:
11724 return "DW_AT_element_list";
11725 case DW_AT_stmt_list:
11726 return "DW_AT_stmt_list";
11727 case DW_AT_low_pc:
11728 return "DW_AT_low_pc";
11729 case DW_AT_high_pc:
11730 return "DW_AT_high_pc";
11731 case DW_AT_language:
11732 return "DW_AT_language";
11733 case DW_AT_member:
11734 return "DW_AT_member";
11735 case DW_AT_discr:
11736 return "DW_AT_discr";
11737 case DW_AT_discr_value:
11738 return "DW_AT_discr_value";
11739 case DW_AT_visibility:
11740 return "DW_AT_visibility";
11741 case DW_AT_import:
11742 return "DW_AT_import";
11743 case DW_AT_string_length:
11744 return "DW_AT_string_length";
11745 case DW_AT_common_reference:
11746 return "DW_AT_common_reference";
11747 case DW_AT_comp_dir:
11748 return "DW_AT_comp_dir";
11749 case DW_AT_const_value:
11750 return "DW_AT_const_value";
11751 case DW_AT_containing_type:
11752 return "DW_AT_containing_type";
11753 case DW_AT_default_value:
11754 return "DW_AT_default_value";
11755 case DW_AT_inline:
11756 return "DW_AT_inline";
11757 case DW_AT_is_optional:
11758 return "DW_AT_is_optional";
11759 case DW_AT_lower_bound:
11760 return "DW_AT_lower_bound";
11761 case DW_AT_producer:
11762 return "DW_AT_producer";
11763 case DW_AT_prototyped:
11764 return "DW_AT_prototyped";
11765 case DW_AT_return_addr:
11766 return "DW_AT_return_addr";
11767 case DW_AT_start_scope:
11768 return "DW_AT_start_scope";
09fa0d7c
JK
11769 case DW_AT_bit_stride:
11770 return "DW_AT_bit_stride";
c906108c
SS
11771 case DW_AT_upper_bound:
11772 return "DW_AT_upper_bound";
11773 case DW_AT_abstract_origin:
11774 return "DW_AT_abstract_origin";
11775 case DW_AT_accessibility:
11776 return "DW_AT_accessibility";
11777 case DW_AT_address_class:
11778 return "DW_AT_address_class";
11779 case DW_AT_artificial:
11780 return "DW_AT_artificial";
11781 case DW_AT_base_types:
11782 return "DW_AT_base_types";
11783 case DW_AT_calling_convention:
11784 return "DW_AT_calling_convention";
11785 case DW_AT_count:
11786 return "DW_AT_count";
11787 case DW_AT_data_member_location:
11788 return "DW_AT_data_member_location";
11789 case DW_AT_decl_column:
11790 return "DW_AT_decl_column";
11791 case DW_AT_decl_file:
11792 return "DW_AT_decl_file";
11793 case DW_AT_decl_line:
11794 return "DW_AT_decl_line";
11795 case DW_AT_declaration:
11796 return "DW_AT_declaration";
11797 case DW_AT_discr_list:
11798 return "DW_AT_discr_list";
11799 case DW_AT_encoding:
11800 return "DW_AT_encoding";
11801 case DW_AT_external:
11802 return "DW_AT_external";
11803 case DW_AT_frame_base:
11804 return "DW_AT_frame_base";
11805 case DW_AT_friend:
11806 return "DW_AT_friend";
11807 case DW_AT_identifier_case:
11808 return "DW_AT_identifier_case";
11809 case DW_AT_macro_info:
11810 return "DW_AT_macro_info";
11811 case DW_AT_namelist_items:
11812 return "DW_AT_namelist_items";
11813 case DW_AT_priority:
11814 return "DW_AT_priority";
11815 case DW_AT_segment:
11816 return "DW_AT_segment";
11817 case DW_AT_specification:
11818 return "DW_AT_specification";
11819 case DW_AT_static_link:
11820 return "DW_AT_static_link";
11821 case DW_AT_type:
11822 return "DW_AT_type";
11823 case DW_AT_use_location:
11824 return "DW_AT_use_location";
11825 case DW_AT_variable_parameter:
11826 return "DW_AT_variable_parameter";
11827 case DW_AT_virtuality:
11828 return "DW_AT_virtuality";
11829 case DW_AT_vtable_elem_location:
11830 return "DW_AT_vtable_elem_location";
b7619582 11831 /* DWARF 3 values. */
d9fa45fe
DC
11832 case DW_AT_allocated:
11833 return "DW_AT_allocated";
11834 case DW_AT_associated:
11835 return "DW_AT_associated";
11836 case DW_AT_data_location:
11837 return "DW_AT_data_location";
09fa0d7c
JK
11838 case DW_AT_byte_stride:
11839 return "DW_AT_byte_stride";
d9fa45fe
DC
11840 case DW_AT_entry_pc:
11841 return "DW_AT_entry_pc";
11842 case DW_AT_use_UTF8:
11843 return "DW_AT_use_UTF8";
11844 case DW_AT_extension:
11845 return "DW_AT_extension";
11846 case DW_AT_ranges:
11847 return "DW_AT_ranges";
11848 case DW_AT_trampoline:
11849 return "DW_AT_trampoline";
11850 case DW_AT_call_column:
11851 return "DW_AT_call_column";
11852 case DW_AT_call_file:
11853 return "DW_AT_call_file";
11854 case DW_AT_call_line:
11855 return "DW_AT_call_line";
b7619582
GF
11856 case DW_AT_description:
11857 return "DW_AT_description";
11858 case DW_AT_binary_scale:
11859 return "DW_AT_binary_scale";
11860 case DW_AT_decimal_scale:
11861 return "DW_AT_decimal_scale";
11862 case DW_AT_small:
11863 return "DW_AT_small";
11864 case DW_AT_decimal_sign:
11865 return "DW_AT_decimal_sign";
11866 case DW_AT_digit_count:
11867 return "DW_AT_digit_count";
11868 case DW_AT_picture_string:
11869 return "DW_AT_picture_string";
11870 case DW_AT_mutable:
11871 return "DW_AT_mutable";
11872 case DW_AT_threads_scaled:
11873 return "DW_AT_threads_scaled";
11874 case DW_AT_explicit:
11875 return "DW_AT_explicit";
11876 case DW_AT_object_pointer:
11877 return "DW_AT_object_pointer";
11878 case DW_AT_endianity:
11879 return "DW_AT_endianity";
11880 case DW_AT_elemental:
11881 return "DW_AT_elemental";
11882 case DW_AT_pure:
11883 return "DW_AT_pure";
11884 case DW_AT_recursive:
11885 return "DW_AT_recursive";
348e048f
DE
11886 /* DWARF 4 values. */
11887 case DW_AT_signature:
11888 return "DW_AT_signature";
31ef98ae
TT
11889 case DW_AT_linkage_name:
11890 return "DW_AT_linkage_name";
b7619582 11891 /* SGI/MIPS extensions. */
c764a876 11892#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11893 case DW_AT_MIPS_fde:
11894 return "DW_AT_MIPS_fde";
c764a876 11895#endif
c906108c
SS
11896 case DW_AT_MIPS_loop_begin:
11897 return "DW_AT_MIPS_loop_begin";
11898 case DW_AT_MIPS_tail_loop_begin:
11899 return "DW_AT_MIPS_tail_loop_begin";
11900 case DW_AT_MIPS_epilog_begin:
11901 return "DW_AT_MIPS_epilog_begin";
11902 case DW_AT_MIPS_loop_unroll_factor:
11903 return "DW_AT_MIPS_loop_unroll_factor";
11904 case DW_AT_MIPS_software_pipeline_depth:
11905 return "DW_AT_MIPS_software_pipeline_depth";
11906 case DW_AT_MIPS_linkage_name:
11907 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11908 case DW_AT_MIPS_stride:
11909 return "DW_AT_MIPS_stride";
11910 case DW_AT_MIPS_abstract_name:
11911 return "DW_AT_MIPS_abstract_name";
11912 case DW_AT_MIPS_clone_origin:
11913 return "DW_AT_MIPS_clone_origin";
11914 case DW_AT_MIPS_has_inlines:
11915 return "DW_AT_MIPS_has_inlines";
b7619582 11916 /* HP extensions. */
c764a876 11917#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11918 case DW_AT_HP_block_index:
11919 return "DW_AT_HP_block_index";
c764a876 11920#endif
b7619582
GF
11921 case DW_AT_HP_unmodifiable:
11922 return "DW_AT_HP_unmodifiable";
11923 case DW_AT_HP_actuals_stmt_list:
11924 return "DW_AT_HP_actuals_stmt_list";
11925 case DW_AT_HP_proc_per_section:
11926 return "DW_AT_HP_proc_per_section";
11927 case DW_AT_HP_raw_data_ptr:
11928 return "DW_AT_HP_raw_data_ptr";
11929 case DW_AT_HP_pass_by_reference:
11930 return "DW_AT_HP_pass_by_reference";
11931 case DW_AT_HP_opt_level:
11932 return "DW_AT_HP_opt_level";
11933 case DW_AT_HP_prof_version_id:
11934 return "DW_AT_HP_prof_version_id";
11935 case DW_AT_HP_opt_flags:
11936 return "DW_AT_HP_opt_flags";
11937 case DW_AT_HP_cold_region_low_pc:
11938 return "DW_AT_HP_cold_region_low_pc";
11939 case DW_AT_HP_cold_region_high_pc:
11940 return "DW_AT_HP_cold_region_high_pc";
11941 case DW_AT_HP_all_variables_modifiable:
11942 return "DW_AT_HP_all_variables_modifiable";
11943 case DW_AT_HP_linkage_name:
11944 return "DW_AT_HP_linkage_name";
11945 case DW_AT_HP_prof_flags:
11946 return "DW_AT_HP_prof_flags";
11947 /* GNU extensions. */
c906108c
SS
11948 case DW_AT_sf_names:
11949 return "DW_AT_sf_names";
11950 case DW_AT_src_info:
11951 return "DW_AT_src_info";
11952 case DW_AT_mac_info:
11953 return "DW_AT_mac_info";
11954 case DW_AT_src_coords:
11955 return "DW_AT_src_coords";
11956 case DW_AT_body_begin:
11957 return "DW_AT_body_begin";
11958 case DW_AT_body_end:
11959 return "DW_AT_body_end";
f5f8a009
EZ
11960 case DW_AT_GNU_vector:
11961 return "DW_AT_GNU_vector";
2de00c64
DE
11962 case DW_AT_GNU_odr_signature:
11963 return "DW_AT_GNU_odr_signature";
b7619582
GF
11964 /* VMS extensions. */
11965 case DW_AT_VMS_rtnbeg_pd_address:
11966 return "DW_AT_VMS_rtnbeg_pd_address";
11967 /* UPC extension. */
11968 case DW_AT_upc_threads_scaled:
11969 return "DW_AT_upc_threads_scaled";
11970 /* PGI (STMicroelectronics) extensions. */
11971 case DW_AT_PGI_lbase:
11972 return "DW_AT_PGI_lbase";
11973 case DW_AT_PGI_soffset:
11974 return "DW_AT_PGI_soffset";
11975 case DW_AT_PGI_lstride:
11976 return "DW_AT_PGI_lstride";
c906108c
SS
11977 default:
11978 return "DW_AT_<unknown>";
11979 }
11980}
11981
11982/* Convert a DWARF value form code into its string name. */
11983
11984static char *
aa1ee363 11985dwarf_form_name (unsigned form)
c906108c
SS
11986{
11987 switch (form)
11988 {
11989 case DW_FORM_addr:
11990 return "DW_FORM_addr";
11991 case DW_FORM_block2:
11992 return "DW_FORM_block2";
11993 case DW_FORM_block4:
11994 return "DW_FORM_block4";
11995 case DW_FORM_data2:
11996 return "DW_FORM_data2";
11997 case DW_FORM_data4:
11998 return "DW_FORM_data4";
11999 case DW_FORM_data8:
12000 return "DW_FORM_data8";
12001 case DW_FORM_string:
12002 return "DW_FORM_string";
12003 case DW_FORM_block:
12004 return "DW_FORM_block";
12005 case DW_FORM_block1:
12006 return "DW_FORM_block1";
12007 case DW_FORM_data1:
12008 return "DW_FORM_data1";
12009 case DW_FORM_flag:
12010 return "DW_FORM_flag";
12011 case DW_FORM_sdata:
12012 return "DW_FORM_sdata";
12013 case DW_FORM_strp:
12014 return "DW_FORM_strp";
12015 case DW_FORM_udata:
12016 return "DW_FORM_udata";
12017 case DW_FORM_ref_addr:
12018 return "DW_FORM_ref_addr";
12019 case DW_FORM_ref1:
12020 return "DW_FORM_ref1";
12021 case DW_FORM_ref2:
12022 return "DW_FORM_ref2";
12023 case DW_FORM_ref4:
12024 return "DW_FORM_ref4";
12025 case DW_FORM_ref8:
12026 return "DW_FORM_ref8";
12027 case DW_FORM_ref_udata:
12028 return "DW_FORM_ref_udata";
12029 case DW_FORM_indirect:
12030 return "DW_FORM_indirect";
348e048f
DE
12031 case DW_FORM_sec_offset:
12032 return "DW_FORM_sec_offset";
12033 case DW_FORM_exprloc:
12034 return "DW_FORM_exprloc";
12035 case DW_FORM_flag_present:
12036 return "DW_FORM_flag_present";
12037 case DW_FORM_sig8:
12038 return "DW_FORM_sig8";
c906108c
SS
12039 default:
12040 return "DW_FORM_<unknown>";
12041 }
12042}
12043
12044/* Convert a DWARF stack opcode into its string name. */
12045
9eae7c52
TT
12046const char *
12047dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12048{
12049 switch (op)
12050 {
12051 case DW_OP_addr:
12052 return "DW_OP_addr";
12053 case DW_OP_deref:
12054 return "DW_OP_deref";
12055 case DW_OP_const1u:
12056 return "DW_OP_const1u";
12057 case DW_OP_const1s:
12058 return "DW_OP_const1s";
12059 case DW_OP_const2u:
12060 return "DW_OP_const2u";
12061 case DW_OP_const2s:
12062 return "DW_OP_const2s";
12063 case DW_OP_const4u:
12064 return "DW_OP_const4u";
12065 case DW_OP_const4s:
12066 return "DW_OP_const4s";
12067 case DW_OP_const8u:
12068 return "DW_OP_const8u";
12069 case DW_OP_const8s:
12070 return "DW_OP_const8s";
12071 case DW_OP_constu:
12072 return "DW_OP_constu";
12073 case DW_OP_consts:
12074 return "DW_OP_consts";
12075 case DW_OP_dup:
12076 return "DW_OP_dup";
12077 case DW_OP_drop:
12078 return "DW_OP_drop";
12079 case DW_OP_over:
12080 return "DW_OP_over";
12081 case DW_OP_pick:
12082 return "DW_OP_pick";
12083 case DW_OP_swap:
12084 return "DW_OP_swap";
12085 case DW_OP_rot:
12086 return "DW_OP_rot";
12087 case DW_OP_xderef:
12088 return "DW_OP_xderef";
12089 case DW_OP_abs:
12090 return "DW_OP_abs";
12091 case DW_OP_and:
12092 return "DW_OP_and";
12093 case DW_OP_div:
12094 return "DW_OP_div";
12095 case DW_OP_minus:
12096 return "DW_OP_minus";
12097 case DW_OP_mod:
12098 return "DW_OP_mod";
12099 case DW_OP_mul:
12100 return "DW_OP_mul";
12101 case DW_OP_neg:
12102 return "DW_OP_neg";
12103 case DW_OP_not:
12104 return "DW_OP_not";
12105 case DW_OP_or:
12106 return "DW_OP_or";
12107 case DW_OP_plus:
12108 return "DW_OP_plus";
12109 case DW_OP_plus_uconst:
12110 return "DW_OP_plus_uconst";
12111 case DW_OP_shl:
12112 return "DW_OP_shl";
12113 case DW_OP_shr:
12114 return "DW_OP_shr";
12115 case DW_OP_shra:
12116 return "DW_OP_shra";
12117 case DW_OP_xor:
12118 return "DW_OP_xor";
12119 case DW_OP_bra:
12120 return "DW_OP_bra";
12121 case DW_OP_eq:
12122 return "DW_OP_eq";
12123 case DW_OP_ge:
12124 return "DW_OP_ge";
12125 case DW_OP_gt:
12126 return "DW_OP_gt";
12127 case DW_OP_le:
12128 return "DW_OP_le";
12129 case DW_OP_lt:
12130 return "DW_OP_lt";
12131 case DW_OP_ne:
12132 return "DW_OP_ne";
12133 case DW_OP_skip:
12134 return "DW_OP_skip";
12135 case DW_OP_lit0:
12136 return "DW_OP_lit0";
12137 case DW_OP_lit1:
12138 return "DW_OP_lit1";
12139 case DW_OP_lit2:
12140 return "DW_OP_lit2";
12141 case DW_OP_lit3:
12142 return "DW_OP_lit3";
12143 case DW_OP_lit4:
12144 return "DW_OP_lit4";
12145 case DW_OP_lit5:
12146 return "DW_OP_lit5";
12147 case DW_OP_lit6:
12148 return "DW_OP_lit6";
12149 case DW_OP_lit7:
12150 return "DW_OP_lit7";
12151 case DW_OP_lit8:
12152 return "DW_OP_lit8";
12153 case DW_OP_lit9:
12154 return "DW_OP_lit9";
12155 case DW_OP_lit10:
12156 return "DW_OP_lit10";
12157 case DW_OP_lit11:
12158 return "DW_OP_lit11";
12159 case DW_OP_lit12:
12160 return "DW_OP_lit12";
12161 case DW_OP_lit13:
12162 return "DW_OP_lit13";
12163 case DW_OP_lit14:
12164 return "DW_OP_lit14";
12165 case DW_OP_lit15:
12166 return "DW_OP_lit15";
12167 case DW_OP_lit16:
12168 return "DW_OP_lit16";
12169 case DW_OP_lit17:
12170 return "DW_OP_lit17";
12171 case DW_OP_lit18:
12172 return "DW_OP_lit18";
12173 case DW_OP_lit19:
12174 return "DW_OP_lit19";
12175 case DW_OP_lit20:
12176 return "DW_OP_lit20";
12177 case DW_OP_lit21:
12178 return "DW_OP_lit21";
12179 case DW_OP_lit22:
12180 return "DW_OP_lit22";
12181 case DW_OP_lit23:
12182 return "DW_OP_lit23";
12183 case DW_OP_lit24:
12184 return "DW_OP_lit24";
12185 case DW_OP_lit25:
12186 return "DW_OP_lit25";
12187 case DW_OP_lit26:
12188 return "DW_OP_lit26";
12189 case DW_OP_lit27:
12190 return "DW_OP_lit27";
12191 case DW_OP_lit28:
12192 return "DW_OP_lit28";
12193 case DW_OP_lit29:
12194 return "DW_OP_lit29";
12195 case DW_OP_lit30:
12196 return "DW_OP_lit30";
12197 case DW_OP_lit31:
12198 return "DW_OP_lit31";
12199 case DW_OP_reg0:
12200 return "DW_OP_reg0";
12201 case DW_OP_reg1:
12202 return "DW_OP_reg1";
12203 case DW_OP_reg2:
12204 return "DW_OP_reg2";
12205 case DW_OP_reg3:
12206 return "DW_OP_reg3";
12207 case DW_OP_reg4:
12208 return "DW_OP_reg4";
12209 case DW_OP_reg5:
12210 return "DW_OP_reg5";
12211 case DW_OP_reg6:
12212 return "DW_OP_reg6";
12213 case DW_OP_reg7:
12214 return "DW_OP_reg7";
12215 case DW_OP_reg8:
12216 return "DW_OP_reg8";
12217 case DW_OP_reg9:
12218 return "DW_OP_reg9";
12219 case DW_OP_reg10:
12220 return "DW_OP_reg10";
12221 case DW_OP_reg11:
12222 return "DW_OP_reg11";
12223 case DW_OP_reg12:
12224 return "DW_OP_reg12";
12225 case DW_OP_reg13:
12226 return "DW_OP_reg13";
12227 case DW_OP_reg14:
12228 return "DW_OP_reg14";
12229 case DW_OP_reg15:
12230 return "DW_OP_reg15";
12231 case DW_OP_reg16:
12232 return "DW_OP_reg16";
12233 case DW_OP_reg17:
12234 return "DW_OP_reg17";
12235 case DW_OP_reg18:
12236 return "DW_OP_reg18";
12237 case DW_OP_reg19:
12238 return "DW_OP_reg19";
12239 case DW_OP_reg20:
12240 return "DW_OP_reg20";
12241 case DW_OP_reg21:
12242 return "DW_OP_reg21";
12243 case DW_OP_reg22:
12244 return "DW_OP_reg22";
12245 case DW_OP_reg23:
12246 return "DW_OP_reg23";
12247 case DW_OP_reg24:
12248 return "DW_OP_reg24";
12249 case DW_OP_reg25:
12250 return "DW_OP_reg25";
12251 case DW_OP_reg26:
12252 return "DW_OP_reg26";
12253 case DW_OP_reg27:
12254 return "DW_OP_reg27";
12255 case DW_OP_reg28:
12256 return "DW_OP_reg28";
12257 case DW_OP_reg29:
12258 return "DW_OP_reg29";
12259 case DW_OP_reg30:
12260 return "DW_OP_reg30";
12261 case DW_OP_reg31:
12262 return "DW_OP_reg31";
12263 case DW_OP_breg0:
12264 return "DW_OP_breg0";
12265 case DW_OP_breg1:
12266 return "DW_OP_breg1";
12267 case DW_OP_breg2:
12268 return "DW_OP_breg2";
12269 case DW_OP_breg3:
12270 return "DW_OP_breg3";
12271 case DW_OP_breg4:
12272 return "DW_OP_breg4";
12273 case DW_OP_breg5:
12274 return "DW_OP_breg5";
12275 case DW_OP_breg6:
12276 return "DW_OP_breg6";
12277 case DW_OP_breg7:
12278 return "DW_OP_breg7";
12279 case DW_OP_breg8:
12280 return "DW_OP_breg8";
12281 case DW_OP_breg9:
12282 return "DW_OP_breg9";
12283 case DW_OP_breg10:
12284 return "DW_OP_breg10";
12285 case DW_OP_breg11:
12286 return "DW_OP_breg11";
12287 case DW_OP_breg12:
12288 return "DW_OP_breg12";
12289 case DW_OP_breg13:
12290 return "DW_OP_breg13";
12291 case DW_OP_breg14:
12292 return "DW_OP_breg14";
12293 case DW_OP_breg15:
12294 return "DW_OP_breg15";
12295 case DW_OP_breg16:
12296 return "DW_OP_breg16";
12297 case DW_OP_breg17:
12298 return "DW_OP_breg17";
12299 case DW_OP_breg18:
12300 return "DW_OP_breg18";
12301 case DW_OP_breg19:
12302 return "DW_OP_breg19";
12303 case DW_OP_breg20:
12304 return "DW_OP_breg20";
12305 case DW_OP_breg21:
12306 return "DW_OP_breg21";
12307 case DW_OP_breg22:
12308 return "DW_OP_breg22";
12309 case DW_OP_breg23:
12310 return "DW_OP_breg23";
12311 case DW_OP_breg24:
12312 return "DW_OP_breg24";
12313 case DW_OP_breg25:
12314 return "DW_OP_breg25";
12315 case DW_OP_breg26:
12316 return "DW_OP_breg26";
12317 case DW_OP_breg27:
12318 return "DW_OP_breg27";
12319 case DW_OP_breg28:
12320 return "DW_OP_breg28";
12321 case DW_OP_breg29:
12322 return "DW_OP_breg29";
12323 case DW_OP_breg30:
12324 return "DW_OP_breg30";
12325 case DW_OP_breg31:
12326 return "DW_OP_breg31";
12327 case DW_OP_regx:
12328 return "DW_OP_regx";
12329 case DW_OP_fbreg:
12330 return "DW_OP_fbreg";
12331 case DW_OP_bregx:
12332 return "DW_OP_bregx";
12333 case DW_OP_piece:
12334 return "DW_OP_piece";
12335 case DW_OP_deref_size:
12336 return "DW_OP_deref_size";
12337 case DW_OP_xderef_size:
12338 return "DW_OP_xderef_size";
12339 case DW_OP_nop:
12340 return "DW_OP_nop";
b7619582 12341 /* DWARF 3 extensions. */
ed348acc
EZ
12342 case DW_OP_push_object_address:
12343 return "DW_OP_push_object_address";
12344 case DW_OP_call2:
12345 return "DW_OP_call2";
12346 case DW_OP_call4:
12347 return "DW_OP_call4";
12348 case DW_OP_call_ref:
12349 return "DW_OP_call_ref";
b7619582
GF
12350 case DW_OP_form_tls_address:
12351 return "DW_OP_form_tls_address";
12352 case DW_OP_call_frame_cfa:
12353 return "DW_OP_call_frame_cfa";
12354 case DW_OP_bit_piece:
12355 return "DW_OP_bit_piece";
9eae7c52
TT
12356 /* DWARF 4 extensions. */
12357 case DW_OP_implicit_value:
12358 return "DW_OP_implicit_value";
12359 case DW_OP_stack_value:
12360 return "DW_OP_stack_value";
12361 /* GNU extensions. */
ed348acc
EZ
12362 case DW_OP_GNU_push_tls_address:
12363 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12364 case DW_OP_GNU_uninit:
12365 return "DW_OP_GNU_uninit";
c906108c 12366 default:
9eae7c52 12367 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12368 }
12369}
12370
12371static char *
fba45db2 12372dwarf_bool_name (unsigned mybool)
c906108c
SS
12373{
12374 if (mybool)
12375 return "TRUE";
12376 else
12377 return "FALSE";
12378}
12379
12380/* Convert a DWARF type code into its string name. */
12381
12382static char *
aa1ee363 12383dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12384{
12385 switch (enc)
12386 {
b7619582
GF
12387 case DW_ATE_void:
12388 return "DW_ATE_void";
c906108c
SS
12389 case DW_ATE_address:
12390 return "DW_ATE_address";
12391 case DW_ATE_boolean:
12392 return "DW_ATE_boolean";
12393 case DW_ATE_complex_float:
12394 return "DW_ATE_complex_float";
12395 case DW_ATE_float:
12396 return "DW_ATE_float";
12397 case DW_ATE_signed:
12398 return "DW_ATE_signed";
12399 case DW_ATE_signed_char:
12400 return "DW_ATE_signed_char";
12401 case DW_ATE_unsigned:
12402 return "DW_ATE_unsigned";
12403 case DW_ATE_unsigned_char:
12404 return "DW_ATE_unsigned_char";
b7619582 12405 /* DWARF 3. */
d9fa45fe
DC
12406 case DW_ATE_imaginary_float:
12407 return "DW_ATE_imaginary_float";
b7619582
GF
12408 case DW_ATE_packed_decimal:
12409 return "DW_ATE_packed_decimal";
12410 case DW_ATE_numeric_string:
12411 return "DW_ATE_numeric_string";
12412 case DW_ATE_edited:
12413 return "DW_ATE_edited";
12414 case DW_ATE_signed_fixed:
12415 return "DW_ATE_signed_fixed";
12416 case DW_ATE_unsigned_fixed:
12417 return "DW_ATE_unsigned_fixed";
12418 case DW_ATE_decimal_float:
12419 return "DW_ATE_decimal_float";
75079b2b
TT
12420 /* DWARF 4. */
12421 case DW_ATE_UTF:
12422 return "DW_ATE_UTF";
b7619582
GF
12423 /* HP extensions. */
12424 case DW_ATE_HP_float80:
12425 return "DW_ATE_HP_float80";
12426 case DW_ATE_HP_complex_float80:
12427 return "DW_ATE_HP_complex_float80";
12428 case DW_ATE_HP_float128:
12429 return "DW_ATE_HP_float128";
12430 case DW_ATE_HP_complex_float128:
12431 return "DW_ATE_HP_complex_float128";
12432 case DW_ATE_HP_floathpintel:
12433 return "DW_ATE_HP_floathpintel";
12434 case DW_ATE_HP_imaginary_float80:
12435 return "DW_ATE_HP_imaginary_float80";
12436 case DW_ATE_HP_imaginary_float128:
12437 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12438 default:
12439 return "DW_ATE_<unknown>";
12440 }
12441}
12442
12443/* Convert a DWARF call frame info operation to its string name. */
12444
12445#if 0
12446static char *
aa1ee363 12447dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12448{
12449 switch (cfi_opc)
12450 {
12451 case DW_CFA_advance_loc:
12452 return "DW_CFA_advance_loc";
12453 case DW_CFA_offset:
12454 return "DW_CFA_offset";
12455 case DW_CFA_restore:
12456 return "DW_CFA_restore";
12457 case DW_CFA_nop:
12458 return "DW_CFA_nop";
12459 case DW_CFA_set_loc:
12460 return "DW_CFA_set_loc";
12461 case DW_CFA_advance_loc1:
12462 return "DW_CFA_advance_loc1";
12463 case DW_CFA_advance_loc2:
12464 return "DW_CFA_advance_loc2";
12465 case DW_CFA_advance_loc4:
12466 return "DW_CFA_advance_loc4";
12467 case DW_CFA_offset_extended:
12468 return "DW_CFA_offset_extended";
12469 case DW_CFA_restore_extended:
12470 return "DW_CFA_restore_extended";
12471 case DW_CFA_undefined:
12472 return "DW_CFA_undefined";
12473 case DW_CFA_same_value:
12474 return "DW_CFA_same_value";
12475 case DW_CFA_register:
12476 return "DW_CFA_register";
12477 case DW_CFA_remember_state:
12478 return "DW_CFA_remember_state";
12479 case DW_CFA_restore_state:
12480 return "DW_CFA_restore_state";
12481 case DW_CFA_def_cfa:
12482 return "DW_CFA_def_cfa";
12483 case DW_CFA_def_cfa_register:
12484 return "DW_CFA_def_cfa_register";
12485 case DW_CFA_def_cfa_offset:
12486 return "DW_CFA_def_cfa_offset";
b7619582 12487 /* DWARF 3. */
985cb1a3
JM
12488 case DW_CFA_def_cfa_expression:
12489 return "DW_CFA_def_cfa_expression";
12490 case DW_CFA_expression:
12491 return "DW_CFA_expression";
12492 case DW_CFA_offset_extended_sf:
12493 return "DW_CFA_offset_extended_sf";
12494 case DW_CFA_def_cfa_sf:
12495 return "DW_CFA_def_cfa_sf";
12496 case DW_CFA_def_cfa_offset_sf:
12497 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12498 case DW_CFA_val_offset:
12499 return "DW_CFA_val_offset";
12500 case DW_CFA_val_offset_sf:
12501 return "DW_CFA_val_offset_sf";
12502 case DW_CFA_val_expression:
12503 return "DW_CFA_val_expression";
12504 /* SGI/MIPS specific. */
c906108c
SS
12505 case DW_CFA_MIPS_advance_loc8:
12506 return "DW_CFA_MIPS_advance_loc8";
b7619582 12507 /* GNU extensions. */
985cb1a3
JM
12508 case DW_CFA_GNU_window_save:
12509 return "DW_CFA_GNU_window_save";
12510 case DW_CFA_GNU_args_size:
12511 return "DW_CFA_GNU_args_size";
12512 case DW_CFA_GNU_negative_offset_extended:
12513 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12514 default:
12515 return "DW_CFA_<unknown>";
12516 }
12517}
12518#endif
12519
f9aca02d 12520static void
d97bc12b 12521dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12522{
12523 unsigned int i;
12524
d97bc12b
DE
12525 print_spaces (indent, f);
12526 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12527 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12528
12529 if (die->parent != NULL)
12530 {
12531 print_spaces (indent, f);
12532 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12533 die->parent->offset);
12534 }
12535
12536 print_spaces (indent, f);
12537 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12538 dwarf_bool_name (die->child != NULL));
c906108c 12539
d97bc12b
DE
12540 print_spaces (indent, f);
12541 fprintf_unfiltered (f, " attributes:\n");
12542
c906108c
SS
12543 for (i = 0; i < die->num_attrs; ++i)
12544 {
d97bc12b
DE
12545 print_spaces (indent, f);
12546 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12547 dwarf_attr_name (die->attrs[i].name),
12548 dwarf_form_name (die->attrs[i].form));
d97bc12b 12549
c906108c
SS
12550 switch (die->attrs[i].form)
12551 {
12552 case DW_FORM_ref_addr:
12553 case DW_FORM_addr:
d97bc12b 12554 fprintf_unfiltered (f, "address: ");
5af949e3 12555 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12556 break;
12557 case DW_FORM_block2:
12558 case DW_FORM_block4:
12559 case DW_FORM_block:
12560 case DW_FORM_block1:
d97bc12b 12561 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12562 break;
2dc7f7b3
TT
12563 case DW_FORM_exprloc:
12564 fprintf_unfiltered (f, "expression: size %u",
12565 DW_BLOCK (&die->attrs[i])->size);
12566 break;
10b3939b
DJ
12567 case DW_FORM_ref1:
12568 case DW_FORM_ref2:
12569 case DW_FORM_ref4:
d97bc12b 12570 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12571 (long) (DW_ADDR (&die->attrs[i])));
12572 break;
c906108c
SS
12573 case DW_FORM_data1:
12574 case DW_FORM_data2:
12575 case DW_FORM_data4:
ce5d95e1 12576 case DW_FORM_data8:
c906108c
SS
12577 case DW_FORM_udata:
12578 case DW_FORM_sdata:
43bbcdc2
PH
12579 fprintf_unfiltered (f, "constant: %s",
12580 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12581 break;
2dc7f7b3
TT
12582 case DW_FORM_sec_offset:
12583 fprintf_unfiltered (f, "section offset: %s",
12584 pulongest (DW_UNSND (&die->attrs[i])));
12585 break;
348e048f
DE
12586 case DW_FORM_sig8:
12587 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12588 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12589 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12590 else
12591 fprintf_unfiltered (f, "signatured type, offset: unknown");
12592 break;
c906108c 12593 case DW_FORM_string:
4bdf3d34 12594 case DW_FORM_strp:
8285870a 12595 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12596 DW_STRING (&die->attrs[i])
8285870a
JK
12597 ? DW_STRING (&die->attrs[i]) : "",
12598 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12599 break;
12600 case DW_FORM_flag:
12601 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12602 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12603 else
d97bc12b 12604 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12605 break;
2dc7f7b3
TT
12606 case DW_FORM_flag_present:
12607 fprintf_unfiltered (f, "flag: TRUE");
12608 break;
a8329558
KW
12609 case DW_FORM_indirect:
12610 /* the reader will have reduced the indirect form to
12611 the "base form" so this form should not occur */
d97bc12b 12612 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12613 break;
c906108c 12614 default:
d97bc12b 12615 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12616 die->attrs[i].form);
d97bc12b 12617 break;
c906108c 12618 }
d97bc12b 12619 fprintf_unfiltered (f, "\n");
c906108c
SS
12620 }
12621}
12622
f9aca02d 12623static void
d97bc12b 12624dump_die_for_error (struct die_info *die)
c906108c 12625{
d97bc12b
DE
12626 dump_die_shallow (gdb_stderr, 0, die);
12627}
12628
12629static void
12630dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12631{
12632 int indent = level * 4;
12633
12634 gdb_assert (die != NULL);
12635
12636 if (level >= max_level)
12637 return;
12638
12639 dump_die_shallow (f, indent, die);
12640
12641 if (die->child != NULL)
c906108c 12642 {
d97bc12b
DE
12643 print_spaces (indent, f);
12644 fprintf_unfiltered (f, " Children:");
12645 if (level + 1 < max_level)
12646 {
12647 fprintf_unfiltered (f, "\n");
12648 dump_die_1 (f, level + 1, max_level, die->child);
12649 }
12650 else
12651 {
12652 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12653 }
12654 }
12655
12656 if (die->sibling != NULL && level > 0)
12657 {
12658 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12659 }
12660}
12661
d97bc12b
DE
12662/* This is called from the pdie macro in gdbinit.in.
12663 It's not static so gcc will keep a copy callable from gdb. */
12664
12665void
12666dump_die (struct die_info *die, int max_level)
12667{
12668 dump_die_1 (gdb_stdlog, 0, max_level, die);
12669}
12670
f9aca02d 12671static void
51545339 12672store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12673{
51545339 12674 void **slot;
c906108c 12675
51545339
DJ
12676 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12677
12678 *slot = die;
c906108c
SS
12679}
12680
93311388
DE
12681static int
12682is_ref_attr (struct attribute *attr)
c906108c 12683{
c906108c
SS
12684 switch (attr->form)
12685 {
12686 case DW_FORM_ref_addr:
c906108c
SS
12687 case DW_FORM_ref1:
12688 case DW_FORM_ref2:
12689 case DW_FORM_ref4:
613e1657 12690 case DW_FORM_ref8:
c906108c 12691 case DW_FORM_ref_udata:
93311388 12692 return 1;
c906108c 12693 default:
93311388 12694 return 0;
c906108c 12695 }
93311388
DE
12696}
12697
12698static unsigned int
12699dwarf2_get_ref_die_offset (struct attribute *attr)
12700{
12701 if (is_ref_attr (attr))
12702 return DW_ADDR (attr);
12703
12704 complaint (&symfile_complaints,
12705 _("unsupported die ref attribute form: '%s'"),
12706 dwarf_form_name (attr->form));
12707 return 0;
c906108c
SS
12708}
12709
43bbcdc2
PH
12710/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12711 * the value held by the attribute is not constant. */
a02abb62 12712
43bbcdc2 12713static LONGEST
a02abb62
JB
12714dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12715{
12716 if (attr->form == DW_FORM_sdata)
12717 return DW_SND (attr);
12718 else if (attr->form == DW_FORM_udata
12719 || attr->form == DW_FORM_data1
12720 || attr->form == DW_FORM_data2
12721 || attr->form == DW_FORM_data4
12722 || attr->form == DW_FORM_data8)
12723 return DW_UNSND (attr);
12724 else
12725 {
e2e0b3e5 12726 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12727 dwarf_form_name (attr->form));
12728 return default_value;
12729 }
12730}
12731
03dd20cc 12732/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12733 unit and add it to our queue.
12734 The result is non-zero if PER_CU was queued, otherwise the result is zero
12735 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12736
348e048f 12737static int
03dd20cc
DJ
12738maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12739 struct dwarf2_per_cu_data *per_cu)
12740{
98bfdba5
PA
12741 /* We may arrive here during partial symbol reading, if we need full
12742 DIEs to process an unusual case (e.g. template arguments). Do
12743 not queue PER_CU, just tell our caller to load its DIEs. */
12744 if (dwarf2_per_objfile->reading_partial_symbols)
12745 {
12746 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12747 return 1;
12748 return 0;
12749 }
12750
03dd20cc
DJ
12751 /* Mark the dependence relation so that we don't flush PER_CU
12752 too early. */
12753 dwarf2_add_dependence (this_cu, per_cu);
12754
12755 /* If it's already on the queue, we have nothing to do. */
12756 if (per_cu->queued)
348e048f 12757 return 0;
03dd20cc
DJ
12758
12759 /* If the compilation unit is already loaded, just mark it as
12760 used. */
12761 if (per_cu->cu != NULL)
12762 {
12763 per_cu->cu->last_used = 0;
348e048f 12764 return 0;
03dd20cc
DJ
12765 }
12766
12767 /* Add it to the queue. */
12768 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12769
12770 return 1;
12771}
12772
12773/* Follow reference or signature attribute ATTR of SRC_DIE.
12774 On entry *REF_CU is the CU of SRC_DIE.
12775 On exit *REF_CU is the CU of the result. */
12776
12777static struct die_info *
12778follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12779 struct dwarf2_cu **ref_cu)
12780{
12781 struct die_info *die;
12782
12783 if (is_ref_attr (attr))
12784 die = follow_die_ref (src_die, attr, ref_cu);
12785 else if (attr->form == DW_FORM_sig8)
12786 die = follow_die_sig (src_die, attr, ref_cu);
12787 else
12788 {
12789 dump_die_for_error (src_die);
12790 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12791 (*ref_cu)->objfile->name);
12792 }
12793
12794 return die;
03dd20cc
DJ
12795}
12796
5c631832 12797/* Follow reference OFFSET.
673bfd45
DE
12798 On entry *REF_CU is the CU of the source die referencing OFFSET.
12799 On exit *REF_CU is the CU of the result.
12800 Returns NULL if OFFSET is invalid. */
f504f079 12801
f9aca02d 12802static struct die_info *
5c631832 12803follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12804{
10b3939b 12805 struct die_info temp_die;
f2f0e013 12806 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12807
348e048f
DE
12808 gdb_assert (cu->per_cu != NULL);
12809
98bfdba5
PA
12810 target_cu = cu;
12811
348e048f
DE
12812 if (cu->per_cu->from_debug_types)
12813 {
12814 /* .debug_types CUs cannot reference anything outside their CU.
12815 If they need to, they have to reference a signatured type via
12816 DW_FORM_sig8. */
12817 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12818 return NULL;
348e048f
DE
12819 }
12820 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12821 {
12822 struct dwarf2_per_cu_data *per_cu;
9a619af0 12823
45452591 12824 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12825
12826 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12827 if (maybe_queue_comp_unit (cu, per_cu))
12828 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12829
10b3939b
DJ
12830 target_cu = per_cu->cu;
12831 }
98bfdba5
PA
12832 else if (cu->dies == NULL)
12833 {
12834 /* We're loading full DIEs during partial symbol reading. */
12835 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12836 load_full_comp_unit (cu->per_cu, cu->objfile);
12837 }
c906108c 12838
f2f0e013 12839 *ref_cu = target_cu;
51545339 12840 temp_die.offset = offset;
5c631832
JK
12841 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12842}
10b3939b 12843
5c631832
JK
12844/* Follow reference attribute ATTR of SRC_DIE.
12845 On entry *REF_CU is the CU of SRC_DIE.
12846 On exit *REF_CU is the CU of the result. */
12847
12848static struct die_info *
12849follow_die_ref (struct die_info *src_die, struct attribute *attr,
12850 struct dwarf2_cu **ref_cu)
12851{
12852 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12853 struct dwarf2_cu *cu = *ref_cu;
12854 struct die_info *die;
12855
12856 die = follow_die_offset (offset, ref_cu);
12857 if (!die)
12858 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12859 "at 0x%x [in module %s]"),
12860 offset, src_die->offset, cu->objfile->name);
348e048f 12861
5c631832
JK
12862 return die;
12863}
12864
12865/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12866 value is intended for DW_OP_call*. */
12867
12868struct dwarf2_locexpr_baton
12869dwarf2_fetch_die_location_block (unsigned int offset,
12870 struct dwarf2_per_cu_data *per_cu)
12871{
12872 struct dwarf2_cu *cu = per_cu->cu;
12873 struct die_info *die;
12874 struct attribute *attr;
12875 struct dwarf2_locexpr_baton retval;
12876
12877 die = follow_die_offset (offset, &cu);
12878 if (!die)
12879 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12880 offset, per_cu->cu->objfile->name);
12881
12882 attr = dwarf2_attr (die, DW_AT_location, cu);
12883 if (!attr)
12884 {
12885 /* DWARF: "If there is no such attribute, then there is no effect.". */
12886
12887 retval.data = NULL;
12888 retval.size = 0;
12889 }
12890 else
12891 {
12892 if (!attr_form_is_block (attr))
12893 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12894 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12895 offset, per_cu->cu->objfile->name);
12896
12897 retval.data = DW_BLOCK (attr)->data;
12898 retval.size = DW_BLOCK (attr)->size;
12899 }
12900 retval.per_cu = cu->per_cu;
12901 return retval;
348e048f
DE
12902}
12903
12904/* Follow the signature attribute ATTR in SRC_DIE.
12905 On entry *REF_CU is the CU of SRC_DIE.
12906 On exit *REF_CU is the CU of the result. */
12907
12908static struct die_info *
12909follow_die_sig (struct die_info *src_die, struct attribute *attr,
12910 struct dwarf2_cu **ref_cu)
12911{
12912 struct objfile *objfile = (*ref_cu)->objfile;
12913 struct die_info temp_die;
12914 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12915 struct dwarf2_cu *sig_cu;
12916 struct die_info *die;
12917
12918 /* sig_type will be NULL if the signatured type is missing from
12919 the debug info. */
12920 if (sig_type == NULL)
12921 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12922 "at 0x%x [in module %s]"),
12923 src_die->offset, objfile->name);
12924
12925 /* If necessary, add it to the queue and load its DIEs. */
12926
12927 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12928 read_signatured_type (objfile, sig_type);
12929
12930 gdb_assert (sig_type->per_cu.cu != NULL);
12931
12932 sig_cu = sig_type->per_cu.cu;
12933 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12934 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12935 if (die)
12936 {
12937 *ref_cu = sig_cu;
12938 return die;
12939 }
12940
12941 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12942 "at 0x%x [in module %s]"),
12943 sig_type->type_offset, src_die->offset, objfile->name);
12944}
12945
12946/* Given an offset of a signatured type, return its signatured_type. */
12947
12948static struct signatured_type *
12949lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12950{
12951 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12952 unsigned int length, initial_length_size;
12953 unsigned int sig_offset;
12954 struct signatured_type find_entry, *type_sig;
12955
12956 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12957 sig_offset = (initial_length_size
12958 + 2 /*version*/
12959 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12960 + 1 /*address_size*/);
12961 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12962 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12963
12964 /* This is only used to lookup previously recorded types.
12965 If we didn't find it, it's our bug. */
12966 gdb_assert (type_sig != NULL);
12967 gdb_assert (offset == type_sig->offset);
12968
12969 return type_sig;
12970}
12971
12972/* Read in signatured type at OFFSET and build its CU and die(s). */
12973
12974static void
12975read_signatured_type_at_offset (struct objfile *objfile,
12976 unsigned int offset)
12977{
12978 struct signatured_type *type_sig;
12979
be391dca
TT
12980 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12981
348e048f
DE
12982 /* We have the section offset, but we need the signature to do the
12983 hash table lookup. */
12984 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12985
12986 gdb_assert (type_sig->per_cu.cu == NULL);
12987
12988 read_signatured_type (objfile, type_sig);
12989
12990 gdb_assert (type_sig->per_cu.cu != NULL);
12991}
12992
12993/* Read in a signatured type and build its CU and DIEs. */
12994
12995static void
12996read_signatured_type (struct objfile *objfile,
12997 struct signatured_type *type_sig)
12998{
1fd400ff 12999 gdb_byte *types_ptr;
348e048f
DE
13000 struct die_reader_specs reader_specs;
13001 struct dwarf2_cu *cu;
13002 ULONGEST signature;
13003 struct cleanup *back_to, *free_cu_cleanup;
13004 struct attribute *attr;
13005
1fd400ff
TT
13006 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13007 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13008
348e048f
DE
13009 gdb_assert (type_sig->per_cu.cu == NULL);
13010
13011 cu = xmalloc (sizeof (struct dwarf2_cu));
13012 memset (cu, 0, sizeof (struct dwarf2_cu));
13013 obstack_init (&cu->comp_unit_obstack);
13014 cu->objfile = objfile;
13015 type_sig->per_cu.cu = cu;
13016 cu->per_cu = &type_sig->per_cu;
13017
13018 /* If an error occurs while loading, release our storage. */
13019 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13020
13021 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13022 types_ptr, objfile->obfd);
13023 gdb_assert (signature == type_sig->signature);
13024
13025 cu->die_hash
13026 = htab_create_alloc_ex (cu->header.length / 12,
13027 die_hash,
13028 die_eq,
13029 NULL,
13030 &cu->comp_unit_obstack,
13031 hashtab_obstack_allocate,
13032 dummy_obstack_deallocate);
13033
13034 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13035 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13036
13037 init_cu_die_reader (&reader_specs, cu);
13038
13039 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13040 NULL /*parent*/);
13041
13042 /* We try not to read any attributes in this function, because not
13043 all objfiles needed for references have been loaded yet, and symbol
13044 table processing isn't initialized. But we have to set the CU language,
13045 or we won't be able to build types correctly. */
13046 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13047 if (attr)
13048 set_cu_language (DW_UNSND (attr), cu);
13049 else
13050 set_cu_language (language_minimal, cu);
13051
13052 do_cleanups (back_to);
13053
13054 /* We've successfully allocated this compilation unit. Let our caller
13055 clean it up when finished with it. */
13056 discard_cleanups (free_cu_cleanup);
13057
13058 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13059 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13060}
13061
c906108c
SS
13062/* Decode simple location descriptions.
13063 Given a pointer to a dwarf block that defines a location, compute
13064 the location and return the value.
13065
4cecd739
DJ
13066 NOTE drow/2003-11-18: This function is called in two situations
13067 now: for the address of static or global variables (partial symbols
13068 only) and for offsets into structures which are expected to be
13069 (more or less) constant. The partial symbol case should go away,
13070 and only the constant case should remain. That will let this
13071 function complain more accurately. A few special modes are allowed
13072 without complaint for global variables (for instance, global
13073 register values and thread-local values).
c906108c
SS
13074
13075 A location description containing no operations indicates that the
4cecd739 13076 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13077 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13078 callers will only want a very basic result and this can become a
13079 complaint.
c906108c 13080
c906108c
SS
13081 Note that stack[0] is unused except as a default error return.
13082 Note that stack overflow is not yet handled. */
13083
13084static CORE_ADDR
e7c27a73 13085decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13086{
e7c27a73 13087 struct objfile *objfile = cu->objfile;
c906108c
SS
13088 int i;
13089 int size = blk->size;
fe1b8b76 13090 gdb_byte *data = blk->data;
c906108c
SS
13091 CORE_ADDR stack[64];
13092 int stacki;
13093 unsigned int bytes_read, unsnd;
fe1b8b76 13094 gdb_byte op;
c906108c
SS
13095
13096 i = 0;
13097 stacki = 0;
13098 stack[stacki] = 0;
c906108c
SS
13099
13100 while (i < size)
13101 {
c906108c
SS
13102 op = data[i++];
13103 switch (op)
13104 {
f1bea926
JM
13105 case DW_OP_lit0:
13106 case DW_OP_lit1:
13107 case DW_OP_lit2:
13108 case DW_OP_lit3:
13109 case DW_OP_lit4:
13110 case DW_OP_lit5:
13111 case DW_OP_lit6:
13112 case DW_OP_lit7:
13113 case DW_OP_lit8:
13114 case DW_OP_lit9:
13115 case DW_OP_lit10:
13116 case DW_OP_lit11:
13117 case DW_OP_lit12:
13118 case DW_OP_lit13:
13119 case DW_OP_lit14:
13120 case DW_OP_lit15:
13121 case DW_OP_lit16:
13122 case DW_OP_lit17:
13123 case DW_OP_lit18:
13124 case DW_OP_lit19:
13125 case DW_OP_lit20:
13126 case DW_OP_lit21:
13127 case DW_OP_lit22:
13128 case DW_OP_lit23:
13129 case DW_OP_lit24:
13130 case DW_OP_lit25:
13131 case DW_OP_lit26:
13132 case DW_OP_lit27:
13133 case DW_OP_lit28:
13134 case DW_OP_lit29:
13135 case DW_OP_lit30:
13136 case DW_OP_lit31:
13137 stack[++stacki] = op - DW_OP_lit0;
13138 break;
13139
c906108c
SS
13140 case DW_OP_reg0:
13141 case DW_OP_reg1:
13142 case DW_OP_reg2:
13143 case DW_OP_reg3:
13144 case DW_OP_reg4:
13145 case DW_OP_reg5:
13146 case DW_OP_reg6:
13147 case DW_OP_reg7:
13148 case DW_OP_reg8:
13149 case DW_OP_reg9:
13150 case DW_OP_reg10:
13151 case DW_OP_reg11:
13152 case DW_OP_reg12:
13153 case DW_OP_reg13:
13154 case DW_OP_reg14:
13155 case DW_OP_reg15:
13156 case DW_OP_reg16:
13157 case DW_OP_reg17:
13158 case DW_OP_reg18:
13159 case DW_OP_reg19:
13160 case DW_OP_reg20:
13161 case DW_OP_reg21:
13162 case DW_OP_reg22:
13163 case DW_OP_reg23:
13164 case DW_OP_reg24:
13165 case DW_OP_reg25:
13166 case DW_OP_reg26:
13167 case DW_OP_reg27:
13168 case DW_OP_reg28:
13169 case DW_OP_reg29:
13170 case DW_OP_reg30:
13171 case DW_OP_reg31:
c906108c 13172 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13173 if (i < size)
13174 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13175 break;
13176
13177 case DW_OP_regx:
c906108c
SS
13178 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13179 i += bytes_read;
c906108c 13180 stack[++stacki] = unsnd;
4cecd739
DJ
13181 if (i < size)
13182 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13183 break;
13184
13185 case DW_OP_addr:
107d2387 13186 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13187 cu, &bytes_read);
107d2387 13188 i += bytes_read;
c906108c
SS
13189 break;
13190
13191 case DW_OP_const1u:
13192 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13193 i += 1;
13194 break;
13195
13196 case DW_OP_const1s:
13197 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13198 i += 1;
13199 break;
13200
13201 case DW_OP_const2u:
13202 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13203 i += 2;
13204 break;
13205
13206 case DW_OP_const2s:
13207 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13208 i += 2;
13209 break;
13210
13211 case DW_OP_const4u:
13212 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13213 i += 4;
13214 break;
13215
13216 case DW_OP_const4s:
13217 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13218 i += 4;
13219 break;
13220
13221 case DW_OP_constu:
13222 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13223 &bytes_read);
c906108c
SS
13224 i += bytes_read;
13225 break;
13226
13227 case DW_OP_consts:
13228 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13229 i += bytes_read;
13230 break;
13231
f1bea926
JM
13232 case DW_OP_dup:
13233 stack[stacki + 1] = stack[stacki];
13234 stacki++;
13235 break;
13236
c906108c
SS
13237 case DW_OP_plus:
13238 stack[stacki - 1] += stack[stacki];
13239 stacki--;
13240 break;
13241
13242 case DW_OP_plus_uconst:
13243 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13244 i += bytes_read;
13245 break;
13246
13247 case DW_OP_minus:
f1bea926 13248 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13249 stacki--;
13250 break;
13251
7a292a7a 13252 case DW_OP_deref:
7a292a7a 13253 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13254 this using GDB's address_class enum. This is valid for partial
13255 global symbols, although the variable's address will be bogus
13256 in the psymtab. */
7a292a7a 13257 if (i < size)
4d3c2250 13258 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13259 break;
13260
9d774e44 13261 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13262 /* The top of the stack has the offset from the beginning
13263 of the thread control block at which the variable is located. */
13264 /* Nothing should follow this operator, so the top of stack would
13265 be returned. */
4cecd739
DJ
13266 /* This is valid for partial global symbols, but the variable's
13267 address will be bogus in the psymtab. */
9d774e44 13268 if (i < size)
4d3c2250 13269 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13270 break;
13271
42be36b3
CT
13272 case DW_OP_GNU_uninit:
13273 break;
13274
c906108c 13275 default:
e2e0b3e5 13276 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13277 dwarf_stack_op_name (op, 1));
c906108c
SS
13278 return (stack[stacki]);
13279 }
13280 }
13281 return (stack[stacki]);
13282}
13283
13284/* memory allocation interface */
13285
c906108c 13286static struct dwarf_block *
7b5a2f43 13287dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13288{
13289 struct dwarf_block *blk;
13290
13291 blk = (struct dwarf_block *)
7b5a2f43 13292 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13293 return (blk);
13294}
13295
13296static struct abbrev_info *
f3dd6933 13297dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13298{
13299 struct abbrev_info *abbrev;
13300
f3dd6933
DJ
13301 abbrev = (struct abbrev_info *)
13302 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13303 memset (abbrev, 0, sizeof (struct abbrev_info));
13304 return (abbrev);
13305}
13306
13307static struct die_info *
b60c80d6 13308dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13309{
13310 struct die_info *die;
b60c80d6
DJ
13311 size_t size = sizeof (struct die_info);
13312
13313 if (num_attrs > 1)
13314 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13315
b60c80d6 13316 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13317 memset (die, 0, sizeof (struct die_info));
13318 return (die);
13319}
2e276125
JB
13320
13321\f
13322/* Macro support. */
13323
13324
13325/* Return the full name of file number I in *LH's file name table.
13326 Use COMP_DIR as the name of the current directory of the
13327 compilation. The result is allocated using xmalloc; the caller is
13328 responsible for freeing it. */
13329static char *
13330file_full_name (int file, struct line_header *lh, const char *comp_dir)
13331{
6a83a1e6
EZ
13332 /* Is the file number a valid index into the line header's file name
13333 table? Remember that file numbers start with one, not zero. */
13334 if (1 <= file && file <= lh->num_file_names)
13335 {
13336 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13337
6a83a1e6
EZ
13338 if (IS_ABSOLUTE_PATH (fe->name))
13339 return xstrdup (fe->name);
13340 else
13341 {
13342 const char *dir;
13343 int dir_len;
13344 char *full_name;
13345
13346 if (fe->dir_index)
13347 dir = lh->include_dirs[fe->dir_index - 1];
13348 else
13349 dir = comp_dir;
13350
13351 if (dir)
13352 {
13353 dir_len = strlen (dir);
13354 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13355 strcpy (full_name, dir);
13356 full_name[dir_len] = '/';
13357 strcpy (full_name + dir_len + 1, fe->name);
13358 return full_name;
13359 }
13360 else
13361 return xstrdup (fe->name);
13362 }
13363 }
2e276125
JB
13364 else
13365 {
6a83a1e6
EZ
13366 /* The compiler produced a bogus file number. We can at least
13367 record the macro definitions made in the file, even if we
13368 won't be able to find the file by name. */
13369 char fake_name[80];
9a619af0 13370
6a83a1e6 13371 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13372
6e70227d 13373 complaint (&symfile_complaints,
6a83a1e6
EZ
13374 _("bad file number in macro information (%d)"),
13375 file);
2e276125 13376
6a83a1e6 13377 return xstrdup (fake_name);
2e276125
JB
13378 }
13379}
13380
13381
13382static struct macro_source_file *
13383macro_start_file (int file, int line,
13384 struct macro_source_file *current_file,
13385 const char *comp_dir,
13386 struct line_header *lh, struct objfile *objfile)
13387{
13388 /* The full name of this source file. */
13389 char *full_name = file_full_name (file, lh, comp_dir);
13390
13391 /* We don't create a macro table for this compilation unit
13392 at all until we actually get a filename. */
13393 if (! pending_macros)
4a146b47 13394 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13395 objfile->macro_cache);
2e276125
JB
13396
13397 if (! current_file)
13398 /* If we have no current file, then this must be the start_file
13399 directive for the compilation unit's main source file. */
13400 current_file = macro_set_main (pending_macros, full_name);
13401 else
13402 current_file = macro_include (current_file, line, full_name);
13403
13404 xfree (full_name);
6e70227d 13405
2e276125
JB
13406 return current_file;
13407}
13408
13409
13410/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13411 followed by a null byte. */
13412static char *
13413copy_string (const char *buf, int len)
13414{
13415 char *s = xmalloc (len + 1);
9a619af0 13416
2e276125
JB
13417 memcpy (s, buf, len);
13418 s[len] = '\0';
2e276125
JB
13419 return s;
13420}
13421
13422
13423static const char *
13424consume_improper_spaces (const char *p, const char *body)
13425{
13426 if (*p == ' ')
13427 {
4d3c2250 13428 complaint (&symfile_complaints,
e2e0b3e5 13429 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13430 body);
2e276125
JB
13431
13432 while (*p == ' ')
13433 p++;
13434 }
13435
13436 return p;
13437}
13438
13439
13440static void
13441parse_macro_definition (struct macro_source_file *file, int line,
13442 const char *body)
13443{
13444 const char *p;
13445
13446 /* The body string takes one of two forms. For object-like macro
13447 definitions, it should be:
13448
13449 <macro name> " " <definition>
13450
13451 For function-like macro definitions, it should be:
13452
13453 <macro name> "() " <definition>
13454 or
13455 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13456
13457 Spaces may appear only where explicitly indicated, and in the
13458 <definition>.
13459
13460 The Dwarf 2 spec says that an object-like macro's name is always
13461 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13462 the space when the macro's definition is the empty string.
2e276125
JB
13463
13464 The Dwarf 2 spec says that there should be no spaces between the
13465 formal arguments in a function-like macro's formal argument list,
13466 but versions of GCC around March 2002 include spaces after the
13467 commas. */
13468
13469
13470 /* Find the extent of the macro name. The macro name is terminated
13471 by either a space or null character (for an object-like macro) or
13472 an opening paren (for a function-like macro). */
13473 for (p = body; *p; p++)
13474 if (*p == ' ' || *p == '(')
13475 break;
13476
13477 if (*p == ' ' || *p == '\0')
13478 {
13479 /* It's an object-like macro. */
13480 int name_len = p - body;
13481 char *name = copy_string (body, name_len);
13482 const char *replacement;
13483
13484 if (*p == ' ')
13485 replacement = body + name_len + 1;
13486 else
13487 {
4d3c2250 13488 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13489 replacement = body + name_len;
13490 }
6e70227d 13491
2e276125
JB
13492 macro_define_object (file, line, name, replacement);
13493
13494 xfree (name);
13495 }
13496 else if (*p == '(')
13497 {
13498 /* It's a function-like macro. */
13499 char *name = copy_string (body, p - body);
13500 int argc = 0;
13501 int argv_size = 1;
13502 char **argv = xmalloc (argv_size * sizeof (*argv));
13503
13504 p++;
13505
13506 p = consume_improper_spaces (p, body);
13507
13508 /* Parse the formal argument list. */
13509 while (*p && *p != ')')
13510 {
13511 /* Find the extent of the current argument name. */
13512 const char *arg_start = p;
13513
13514 while (*p && *p != ',' && *p != ')' && *p != ' ')
13515 p++;
13516
13517 if (! *p || p == arg_start)
4d3c2250 13518 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13519 else
13520 {
13521 /* Make sure argv has room for the new argument. */
13522 if (argc >= argv_size)
13523 {
13524 argv_size *= 2;
13525 argv = xrealloc (argv, argv_size * sizeof (*argv));
13526 }
13527
13528 argv[argc++] = copy_string (arg_start, p - arg_start);
13529 }
13530
13531 p = consume_improper_spaces (p, body);
13532
13533 /* Consume the comma, if present. */
13534 if (*p == ',')
13535 {
13536 p++;
13537
13538 p = consume_improper_spaces (p, body);
13539 }
13540 }
13541
13542 if (*p == ')')
13543 {
13544 p++;
13545
13546 if (*p == ' ')
13547 /* Perfectly formed definition, no complaints. */
13548 macro_define_function (file, line, name,
6e70227d 13549 argc, (const char **) argv,
2e276125
JB
13550 p + 1);
13551 else if (*p == '\0')
13552 {
13553 /* Complain, but do define it. */
4d3c2250 13554 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13555 macro_define_function (file, line, name,
6e70227d 13556 argc, (const char **) argv,
2e276125
JB
13557 p);
13558 }
13559 else
13560 /* Just complain. */
4d3c2250 13561 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13562 }
13563 else
13564 /* Just complain. */
4d3c2250 13565 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13566
13567 xfree (name);
13568 {
13569 int i;
13570
13571 for (i = 0; i < argc; i++)
13572 xfree (argv[i]);
13573 }
13574 xfree (argv);
13575 }
13576 else
4d3c2250 13577 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13578}
13579
13580
13581static void
13582dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13583 char *comp_dir, bfd *abfd,
e7c27a73 13584 struct dwarf2_cu *cu)
2e276125 13585{
fe1b8b76 13586 gdb_byte *mac_ptr, *mac_end;
2e276125 13587 struct macro_source_file *current_file = 0;
757a13d0
JK
13588 enum dwarf_macinfo_record_type macinfo_type;
13589 int at_commandline;
2e276125 13590
be391dca
TT
13591 dwarf2_read_section (dwarf2_per_objfile->objfile,
13592 &dwarf2_per_objfile->macinfo);
dce234bc 13593 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13594 {
e2e0b3e5 13595 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13596 return;
13597 }
13598
757a13d0
JK
13599 /* First pass: Find the name of the base filename.
13600 This filename is needed in order to process all macros whose definition
13601 (or undefinition) comes from the command line. These macros are defined
13602 before the first DW_MACINFO_start_file entry, and yet still need to be
13603 associated to the base file.
13604
13605 To determine the base file name, we scan the macro definitions until we
13606 reach the first DW_MACINFO_start_file entry. We then initialize
13607 CURRENT_FILE accordingly so that any macro definition found before the
13608 first DW_MACINFO_start_file can still be associated to the base file. */
13609
dce234bc
PP
13610 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13611 mac_end = dwarf2_per_objfile->macinfo.buffer
13612 + dwarf2_per_objfile->macinfo.size;
2e276125 13613
757a13d0 13614 do
2e276125 13615 {
2e276125
JB
13616 /* Do we at least have room for a macinfo type byte? */
13617 if (mac_ptr >= mac_end)
13618 {
757a13d0
JK
13619 /* Complaint is printed during the second pass as GDB will probably
13620 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13621 break;
2e276125
JB
13622 }
13623
13624 macinfo_type = read_1_byte (abfd, mac_ptr);
13625 mac_ptr++;
13626
13627 switch (macinfo_type)
13628 {
13629 /* A zero macinfo type indicates the end of the macro
13630 information. */
13631 case 0:
757a13d0
JK
13632 break;
13633
13634 case DW_MACINFO_define:
13635 case DW_MACINFO_undef:
13636 /* Only skip the data by MAC_PTR. */
13637 {
13638 unsigned int bytes_read;
13639
13640 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13641 mac_ptr += bytes_read;
9b1c24c8 13642 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13643 mac_ptr += bytes_read;
13644 }
13645 break;
13646
13647 case DW_MACINFO_start_file:
13648 {
13649 unsigned int bytes_read;
13650 int line, file;
13651
13652 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13653 mac_ptr += bytes_read;
13654 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13655 mac_ptr += bytes_read;
13656
13657 current_file = macro_start_file (file, line, current_file, comp_dir,
13658 lh, cu->objfile);
13659 }
13660 break;
13661
13662 case DW_MACINFO_end_file:
13663 /* No data to skip by MAC_PTR. */
13664 break;
13665
13666 case DW_MACINFO_vendor_ext:
13667 /* Only skip the data by MAC_PTR. */
13668 {
13669 unsigned int bytes_read;
13670
13671 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13672 mac_ptr += bytes_read;
9b1c24c8 13673 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13674 mac_ptr += bytes_read;
13675 }
13676 break;
13677
13678 default:
13679 break;
13680 }
13681 } while (macinfo_type != 0 && current_file == NULL);
13682
13683 /* Second pass: Process all entries.
13684
13685 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13686 command-line macro definitions/undefinitions. This flag is unset when we
13687 reach the first DW_MACINFO_start_file entry. */
13688
dce234bc 13689 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13690
13691 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13692 GDB is still reading the definitions from command line. First
13693 DW_MACINFO_start_file will need to be ignored as it was already executed
13694 to create CURRENT_FILE for the main source holding also the command line
13695 definitions. On first met DW_MACINFO_start_file this flag is reset to
13696 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13697
13698 at_commandline = 1;
13699
13700 do
13701 {
13702 /* Do we at least have room for a macinfo type byte? */
13703 if (mac_ptr >= mac_end)
13704 {
13705 dwarf2_macros_too_long_complaint ();
13706 break;
13707 }
13708
13709 macinfo_type = read_1_byte (abfd, mac_ptr);
13710 mac_ptr++;
13711
13712 switch (macinfo_type)
13713 {
13714 /* A zero macinfo type indicates the end of the macro
13715 information. */
13716 case 0:
13717 break;
2e276125
JB
13718
13719 case DW_MACINFO_define:
13720 case DW_MACINFO_undef:
13721 {
891d2f0b 13722 unsigned int bytes_read;
2e276125
JB
13723 int line;
13724 char *body;
13725
13726 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13727 mac_ptr += bytes_read;
9b1c24c8 13728 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13729 mac_ptr += bytes_read;
13730
13731 if (! current_file)
757a13d0
JK
13732 {
13733 /* DWARF violation as no main source is present. */
13734 complaint (&symfile_complaints,
13735 _("debug info with no main source gives macro %s "
13736 "on line %d: %s"),
6e70227d
DE
13737 macinfo_type == DW_MACINFO_define ?
13738 _("definition") :
905e0470
PM
13739 macinfo_type == DW_MACINFO_undef ?
13740 _("undefinition") :
13741 _("something-or-other"), line, body);
757a13d0
JK
13742 break;
13743 }
13744 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13745 complaint (&symfile_complaints,
757a13d0
JK
13746 _("debug info gives %s macro %s with %s line %d: %s"),
13747 at_commandline ? _("command-line") : _("in-file"),
905e0470 13748 macinfo_type == DW_MACINFO_define ?
6e70227d 13749 _("definition") :
905e0470
PM
13750 macinfo_type == DW_MACINFO_undef ?
13751 _("undefinition") :
13752 _("something-or-other"),
757a13d0
JK
13753 line == 0 ? _("zero") : _("non-zero"), line, body);
13754
13755 if (macinfo_type == DW_MACINFO_define)
13756 parse_macro_definition (current_file, line, body);
13757 else if (macinfo_type == DW_MACINFO_undef)
13758 macro_undef (current_file, line, body);
2e276125
JB
13759 }
13760 break;
13761
13762 case DW_MACINFO_start_file:
13763 {
891d2f0b 13764 unsigned int bytes_read;
2e276125
JB
13765 int line, file;
13766
13767 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13768 mac_ptr += bytes_read;
13769 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13770 mac_ptr += bytes_read;
13771
757a13d0
JK
13772 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13773 complaint (&symfile_complaints,
13774 _("debug info gives source %d included "
13775 "from %s at %s line %d"),
13776 file, at_commandline ? _("command-line") : _("file"),
13777 line == 0 ? _("zero") : _("non-zero"), line);
13778
13779 if (at_commandline)
13780 {
13781 /* This DW_MACINFO_start_file was executed in the pass one. */
13782 at_commandline = 0;
13783 }
13784 else
13785 current_file = macro_start_file (file, line,
13786 current_file, comp_dir,
13787 lh, cu->objfile);
2e276125
JB
13788 }
13789 break;
13790
13791 case DW_MACINFO_end_file:
13792 if (! current_file)
4d3c2250 13793 complaint (&symfile_complaints,
e2e0b3e5 13794 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13795 else
13796 {
13797 current_file = current_file->included_by;
13798 if (! current_file)
13799 {
13800 enum dwarf_macinfo_record_type next_type;
13801
13802 /* GCC circa March 2002 doesn't produce the zero
13803 type byte marking the end of the compilation
13804 unit. Complain if it's not there, but exit no
13805 matter what. */
13806
13807 /* Do we at least have room for a macinfo type byte? */
13808 if (mac_ptr >= mac_end)
13809 {
4d3c2250 13810 dwarf2_macros_too_long_complaint ();
2e276125
JB
13811 return;
13812 }
13813
13814 /* We don't increment mac_ptr here, so this is just
13815 a look-ahead. */
13816 next_type = read_1_byte (abfd, mac_ptr);
13817 if (next_type != 0)
4d3c2250 13818 complaint (&symfile_complaints,
e2e0b3e5 13819 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13820
13821 return;
13822 }
13823 }
13824 break;
13825
13826 case DW_MACINFO_vendor_ext:
13827 {
891d2f0b 13828 unsigned int bytes_read;
2e276125
JB
13829 int constant;
13830 char *string;
13831
13832 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13833 mac_ptr += bytes_read;
9b1c24c8 13834 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13835 mac_ptr += bytes_read;
13836
13837 /* We don't recognize any vendor extensions. */
13838 }
13839 break;
13840 }
757a13d0 13841 } while (macinfo_type != 0);
2e276125 13842}
8e19ed76
PS
13843
13844/* Check if the attribute's form is a DW_FORM_block*
13845 if so return true else false. */
13846static int
13847attr_form_is_block (struct attribute *attr)
13848{
13849 return (attr == NULL ? 0 :
13850 attr->form == DW_FORM_block1
13851 || attr->form == DW_FORM_block2
13852 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13853 || attr->form == DW_FORM_block
13854 || attr->form == DW_FORM_exprloc);
8e19ed76 13855}
4c2df51b 13856
c6a0999f
JB
13857/* Return non-zero if ATTR's value is a section offset --- classes
13858 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13859 You may use DW_UNSND (attr) to retrieve such offsets.
13860
13861 Section 7.5.4, "Attribute Encodings", explains that no attribute
13862 may have a value that belongs to more than one of these classes; it
13863 would be ambiguous if we did, because we use the same forms for all
13864 of them. */
3690dd37
JB
13865static int
13866attr_form_is_section_offset (struct attribute *attr)
13867{
13868 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13869 || attr->form == DW_FORM_data8
13870 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13871}
13872
13873
13874/* Return non-zero if ATTR's value falls in the 'constant' class, or
13875 zero otherwise. When this function returns true, you can apply
13876 dwarf2_get_attr_constant_value to it.
13877
13878 However, note that for some attributes you must check
13879 attr_form_is_section_offset before using this test. DW_FORM_data4
13880 and DW_FORM_data8 are members of both the constant class, and of
13881 the classes that contain offsets into other debug sections
13882 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13883 that, if an attribute's can be either a constant or one of the
13884 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13885 taken as section offsets, not constants. */
13886static int
13887attr_form_is_constant (struct attribute *attr)
13888{
13889 switch (attr->form)
13890 {
13891 case DW_FORM_sdata:
13892 case DW_FORM_udata:
13893 case DW_FORM_data1:
13894 case DW_FORM_data2:
13895 case DW_FORM_data4:
13896 case DW_FORM_data8:
13897 return 1;
13898 default:
13899 return 0;
13900 }
13901}
13902
4c2df51b
DJ
13903static void
13904dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13905 struct dwarf2_cu *cu)
4c2df51b 13906{
3690dd37 13907 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13908 /* ".debug_loc" may not exist at all, or the offset may be outside
13909 the section. If so, fall through to the complaint in the
13910 other branch. */
dce234bc 13911 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13912 {
0d53c4c4 13913 struct dwarf2_loclist_baton *baton;
4c2df51b 13914
4a146b47 13915 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13916 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13917 baton->per_cu = cu->per_cu;
13918 gdb_assert (baton->per_cu);
4c2df51b 13919
be391dca
TT
13920 dwarf2_read_section (dwarf2_per_objfile->objfile,
13921 &dwarf2_per_objfile->loc);
13922
0d53c4c4
DJ
13923 /* We don't know how long the location list is, but make sure we
13924 don't run off the edge of the section. */
dce234bc
PP
13925 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13926 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13927 baton->base_address = cu->base_address;
13928 if (cu->base_known == 0)
0d53c4c4 13929 complaint (&symfile_complaints,
e2e0b3e5 13930 _("Location list used without specifying the CU base address."));
4c2df51b 13931
768a979c 13932 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13933 SYMBOL_LOCATION_BATON (sym) = baton;
13934 }
13935 else
13936 {
13937 struct dwarf2_locexpr_baton *baton;
13938
4a146b47 13939 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13940 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13941 baton->per_cu = cu->per_cu;
13942 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13943
13944 if (attr_form_is_block (attr))
13945 {
13946 /* Note that we're just copying the block's data pointer
13947 here, not the actual data. We're still pointing into the
6502dd73
DJ
13948 info_buffer for SYM's objfile; right now we never release
13949 that buffer, but when we do clean up properly this may
13950 need to change. */
0d53c4c4
DJ
13951 baton->size = DW_BLOCK (attr)->size;
13952 baton->data = DW_BLOCK (attr)->data;
13953 }
13954 else
13955 {
13956 dwarf2_invalid_attrib_class_complaint ("location description",
13957 SYMBOL_NATURAL_NAME (sym));
13958 baton->size = 0;
13959 baton->data = NULL;
13960 }
6e70227d 13961
768a979c 13962 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13963 SYMBOL_LOCATION_BATON (sym) = baton;
13964 }
4c2df51b 13965}
6502dd73 13966
9aa1f1e3
TT
13967/* Return the OBJFILE associated with the compilation unit CU. If CU
13968 came from a separate debuginfo file, then the master objfile is
13969 returned. */
ae0d2f24
UW
13970
13971struct objfile *
13972dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13973{
9291a0cd 13974 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13975
13976 /* Return the master objfile, so that we can report and look up the
13977 correct file containing this variable. */
13978 if (objfile->separate_debug_objfile_backlink)
13979 objfile = objfile->separate_debug_objfile_backlink;
13980
13981 return objfile;
13982}
13983
13984/* Return the address size given in the compilation unit header for CU. */
13985
13986CORE_ADDR
13987dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13988{
13989 if (per_cu->cu)
13990 return per_cu->cu->header.addr_size;
13991 else
13992 {
13993 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13994 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13995 struct dwarf2_per_objfile *per_objfile
13996 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13997 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13998 struct comp_unit_head cu_header;
9a619af0 13999
ae0d2f24
UW
14000 memset (&cu_header, 0, sizeof cu_header);
14001 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14002 return cu_header.addr_size;
14003 }
14004}
14005
9eae7c52
TT
14006/* Return the offset size given in the compilation unit header for CU. */
14007
14008int
14009dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14010{
14011 if (per_cu->cu)
14012 return per_cu->cu->header.offset_size;
14013 else
14014 {
14015 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14016 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14017 struct dwarf2_per_objfile *per_objfile
14018 = objfile_data (objfile, dwarf2_objfile_data_key);
14019 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14020 struct comp_unit_head cu_header;
14021
14022 memset (&cu_header, 0, sizeof cu_header);
14023 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14024 return cu_header.offset_size;
14025 }
14026}
14027
9aa1f1e3
TT
14028/* Return the text offset of the CU. The returned offset comes from
14029 this CU's objfile. If this objfile came from a separate debuginfo
14030 file, then the offset may be different from the corresponding
14031 offset in the parent objfile. */
14032
14033CORE_ADDR
14034dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14035{
bb3fa9d0 14036 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14037
14038 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14039}
14040
348e048f
DE
14041/* Locate the .debug_info compilation unit from CU's objfile which contains
14042 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14043
14044static struct dwarf2_per_cu_data *
c764a876 14045dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14046 struct objfile *objfile)
14047{
14048 struct dwarf2_per_cu_data *this_cu;
14049 int low, high;
14050
ae038cb0
DJ
14051 low = 0;
14052 high = dwarf2_per_objfile->n_comp_units - 1;
14053 while (high > low)
14054 {
14055 int mid = low + (high - low) / 2;
9a619af0 14056
ae038cb0
DJ
14057 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14058 high = mid;
14059 else
14060 low = mid + 1;
14061 }
14062 gdb_assert (low == high);
14063 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14064 {
10b3939b 14065 if (low == 0)
8a3fe4f8
AC
14066 error (_("Dwarf Error: could not find partial DIE containing "
14067 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14068 (long) offset, bfd_get_filename (objfile->obfd));
14069
ae038cb0
DJ
14070 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14071 return dwarf2_per_objfile->all_comp_units[low-1];
14072 }
14073 else
14074 {
14075 this_cu = dwarf2_per_objfile->all_comp_units[low];
14076 if (low == dwarf2_per_objfile->n_comp_units - 1
14077 && offset >= this_cu->offset + this_cu->length)
c764a876 14078 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14079 gdb_assert (offset < this_cu->offset + this_cu->length);
14080 return this_cu;
14081 }
14082}
14083
10b3939b
DJ
14084/* Locate the compilation unit from OBJFILE which is located at exactly
14085 OFFSET. Raises an error on failure. */
14086
ae038cb0 14087static struct dwarf2_per_cu_data *
c764a876 14088dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14089{
14090 struct dwarf2_per_cu_data *this_cu;
9a619af0 14091
ae038cb0
DJ
14092 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14093 if (this_cu->offset != offset)
c764a876 14094 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14095 return this_cu;
14096}
14097
93311388
DE
14098/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14099
14100static struct dwarf2_cu *
14101alloc_one_comp_unit (struct objfile *objfile)
14102{
14103 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14104 cu->objfile = objfile;
14105 obstack_init (&cu->comp_unit_obstack);
14106 return cu;
14107}
14108
ae038cb0
DJ
14109/* Release one cached compilation unit, CU. We unlink it from the tree
14110 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14111 the caller is responsible for that.
14112 NOTE: DATA is a void * because this function is also used as a
14113 cleanup routine. */
ae038cb0
DJ
14114
14115static void
14116free_one_comp_unit (void *data)
14117{
14118 struct dwarf2_cu *cu = data;
14119
14120 if (cu->per_cu != NULL)
14121 cu->per_cu->cu = NULL;
14122 cu->per_cu = NULL;
14123
14124 obstack_free (&cu->comp_unit_obstack, NULL);
14125
14126 xfree (cu);
14127}
14128
72bf9492 14129/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14130 when we're finished with it. We can't free the pointer itself, but be
14131 sure to unlink it from the cache. Also release any associated storage
14132 and perform cache maintenance.
72bf9492
DJ
14133
14134 Only used during partial symbol parsing. */
14135
14136static void
14137free_stack_comp_unit (void *data)
14138{
14139 struct dwarf2_cu *cu = data;
14140
14141 obstack_free (&cu->comp_unit_obstack, NULL);
14142 cu->partial_dies = NULL;
ae038cb0
DJ
14143
14144 if (cu->per_cu != NULL)
14145 {
14146 /* This compilation unit is on the stack in our caller, so we
14147 should not xfree it. Just unlink it. */
14148 cu->per_cu->cu = NULL;
14149 cu->per_cu = NULL;
14150
14151 /* If we had a per-cu pointer, then we may have other compilation
14152 units loaded, so age them now. */
14153 age_cached_comp_units ();
14154 }
14155}
14156
14157/* Free all cached compilation units. */
14158
14159static void
14160free_cached_comp_units (void *data)
14161{
14162 struct dwarf2_per_cu_data *per_cu, **last_chain;
14163
14164 per_cu = dwarf2_per_objfile->read_in_chain;
14165 last_chain = &dwarf2_per_objfile->read_in_chain;
14166 while (per_cu != NULL)
14167 {
14168 struct dwarf2_per_cu_data *next_cu;
14169
14170 next_cu = per_cu->cu->read_in_chain;
14171
14172 free_one_comp_unit (per_cu->cu);
14173 *last_chain = next_cu;
14174
14175 per_cu = next_cu;
14176 }
14177}
14178
14179/* Increase the age counter on each cached compilation unit, and free
14180 any that are too old. */
14181
14182static void
14183age_cached_comp_units (void)
14184{
14185 struct dwarf2_per_cu_data *per_cu, **last_chain;
14186
14187 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14188 per_cu = dwarf2_per_objfile->read_in_chain;
14189 while (per_cu != NULL)
14190 {
14191 per_cu->cu->last_used ++;
14192 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14193 dwarf2_mark (per_cu->cu);
14194 per_cu = per_cu->cu->read_in_chain;
14195 }
14196
14197 per_cu = dwarf2_per_objfile->read_in_chain;
14198 last_chain = &dwarf2_per_objfile->read_in_chain;
14199 while (per_cu != NULL)
14200 {
14201 struct dwarf2_per_cu_data *next_cu;
14202
14203 next_cu = per_cu->cu->read_in_chain;
14204
14205 if (!per_cu->cu->mark)
14206 {
14207 free_one_comp_unit (per_cu->cu);
14208 *last_chain = next_cu;
14209 }
14210 else
14211 last_chain = &per_cu->cu->read_in_chain;
14212
14213 per_cu = next_cu;
14214 }
14215}
14216
14217/* Remove a single compilation unit from the cache. */
14218
14219static void
14220free_one_cached_comp_unit (void *target_cu)
14221{
14222 struct dwarf2_per_cu_data *per_cu, **last_chain;
14223
14224 per_cu = dwarf2_per_objfile->read_in_chain;
14225 last_chain = &dwarf2_per_objfile->read_in_chain;
14226 while (per_cu != NULL)
14227 {
14228 struct dwarf2_per_cu_data *next_cu;
14229
14230 next_cu = per_cu->cu->read_in_chain;
14231
14232 if (per_cu->cu == target_cu)
14233 {
14234 free_one_comp_unit (per_cu->cu);
14235 *last_chain = next_cu;
14236 break;
14237 }
14238 else
14239 last_chain = &per_cu->cu->read_in_chain;
14240
14241 per_cu = next_cu;
14242 }
14243}
14244
fe3e1990
DJ
14245/* Release all extra memory associated with OBJFILE. */
14246
14247void
14248dwarf2_free_objfile (struct objfile *objfile)
14249{
14250 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14251
14252 if (dwarf2_per_objfile == NULL)
14253 return;
14254
14255 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14256 free_cached_comp_units (NULL);
14257
9291a0cd
TT
14258 if (dwarf2_per_objfile->using_index)
14259 {
14260 int i;
14261
14262 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14263 {
14264 int j;
14265 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14266
14267 if (!cu->v.quick->lines)
14268 continue;
14269
14270 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
14271 {
14272 if (cu->v.quick->file_names)
14273 xfree ((void *) cu->v.quick->file_names[j]);
14274 if (cu->v.quick->full_names)
14275 xfree ((void *) cu->v.quick->full_names[j]);
14276 }
14277
14278 free_line_header (cu->v.quick->lines);
14279 }
14280 }
14281
fe3e1990
DJ
14282 /* Everything else should be on the objfile obstack. */
14283}
14284
1c379e20
DJ
14285/* A pair of DIE offset and GDB type pointer. We store these
14286 in a hash table separate from the DIEs, and preserve them
14287 when the DIEs are flushed out of cache. */
14288
14289struct dwarf2_offset_and_type
14290{
14291 unsigned int offset;
14292 struct type *type;
14293};
14294
14295/* Hash function for a dwarf2_offset_and_type. */
14296
14297static hashval_t
14298offset_and_type_hash (const void *item)
14299{
14300 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14301
1c379e20
DJ
14302 return ofs->offset;
14303}
14304
14305/* Equality function for a dwarf2_offset_and_type. */
14306
14307static int
14308offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14309{
14310 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14311 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14312
1c379e20
DJ
14313 return ofs_lhs->offset == ofs_rhs->offset;
14314}
14315
14316/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14317 table if necessary. For convenience, return TYPE.
14318
14319 The DIEs reading must have careful ordering to:
14320 * Not cause infite loops trying to read in DIEs as a prerequisite for
14321 reading current DIE.
14322 * Not trying to dereference contents of still incompletely read in types
14323 while reading in other DIEs.
14324 * Enable referencing still incompletely read in types just by a pointer to
14325 the type without accessing its fields.
14326
14327 Therefore caller should follow these rules:
14328 * Try to fetch any prerequisite types we may need to build this DIE type
14329 before building the type and calling set_die_type.
e71ec853 14330 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14331 possible before fetching more types to complete the current type.
14332 * Make the type as complete as possible before fetching more types. */
1c379e20 14333
f792889a 14334static struct type *
1c379e20
DJ
14335set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14336{
14337 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14338 struct objfile *objfile = cu->objfile;
14339 htab_t *type_hash_ptr;
1c379e20 14340
b4ba55a1
JB
14341 /* For Ada types, make sure that the gnat-specific data is always
14342 initialized (if not already set). There are a few types where
14343 we should not be doing so, because the type-specific area is
14344 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14345 where the type-specific area is used to store the floatformat).
14346 But this is not a problem, because the gnat-specific information
14347 is actually not needed for these types. */
14348 if (need_gnat_info (cu)
14349 && TYPE_CODE (type) != TYPE_CODE_FUNC
14350 && TYPE_CODE (type) != TYPE_CODE_FLT
14351 && !HAVE_GNAT_AUX_INFO (type))
14352 INIT_GNAT_SPECIFIC (type);
14353
673bfd45
DE
14354 if (cu->per_cu->from_debug_types)
14355 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14356 else
14357 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14358
14359 if (*type_hash_ptr == NULL)
f792889a 14360 {
673bfd45
DE
14361 *type_hash_ptr
14362 = htab_create_alloc_ex (127,
f792889a
DJ
14363 offset_and_type_hash,
14364 offset_and_type_eq,
14365 NULL,
673bfd45 14366 &objfile->objfile_obstack,
f792889a
DJ
14367 hashtab_obstack_allocate,
14368 dummy_obstack_deallocate);
f792889a 14369 }
1c379e20
DJ
14370
14371 ofs.offset = die->offset;
14372 ofs.type = type;
14373 slot = (struct dwarf2_offset_and_type **)
673bfd45 14374 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14375 if (*slot)
14376 complaint (&symfile_complaints,
14377 _("A problem internal to GDB: DIE 0x%x has type already set"),
14378 die->offset);
673bfd45 14379 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14380 **slot = ofs;
f792889a 14381 return type;
1c379e20
DJ
14382}
14383
673bfd45
DE
14384/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14385 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14386
14387static struct type *
673bfd45
DE
14388get_die_type_at_offset (unsigned int offset,
14389 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14390{
14391 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14392 htab_t type_hash;
f792889a 14393
673bfd45
DE
14394 if (per_cu->from_debug_types)
14395 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14396 else
14397 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14398 if (type_hash == NULL)
14399 return NULL;
1c379e20 14400
673bfd45 14401 ofs.offset = offset;
1c379e20
DJ
14402 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14403 if (slot)
14404 return slot->type;
14405 else
14406 return NULL;
14407}
14408
673bfd45
DE
14409/* Look up the type for DIE in the appropriate type_hash table,
14410 or return NULL if DIE does not have a saved type. */
14411
14412static struct type *
14413get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14414{
14415 return get_die_type_at_offset (die->offset, cu->per_cu);
14416}
14417
10b3939b
DJ
14418/* Add a dependence relationship from CU to REF_PER_CU. */
14419
14420static void
14421dwarf2_add_dependence (struct dwarf2_cu *cu,
14422 struct dwarf2_per_cu_data *ref_per_cu)
14423{
14424 void **slot;
14425
14426 if (cu->dependencies == NULL)
14427 cu->dependencies
14428 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14429 NULL, &cu->comp_unit_obstack,
14430 hashtab_obstack_allocate,
14431 dummy_obstack_deallocate);
14432
14433 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14434 if (*slot == NULL)
14435 *slot = ref_per_cu;
14436}
1c379e20 14437
f504f079
DE
14438/* Subroutine of dwarf2_mark to pass to htab_traverse.
14439 Set the mark field in every compilation unit in the
ae038cb0
DJ
14440 cache that we must keep because we are keeping CU. */
14441
10b3939b
DJ
14442static int
14443dwarf2_mark_helper (void **slot, void *data)
14444{
14445 struct dwarf2_per_cu_data *per_cu;
14446
14447 per_cu = (struct dwarf2_per_cu_data *) *slot;
14448 if (per_cu->cu->mark)
14449 return 1;
14450 per_cu->cu->mark = 1;
14451
14452 if (per_cu->cu->dependencies != NULL)
14453 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14454
14455 return 1;
14456}
14457
f504f079
DE
14458/* Set the mark field in CU and in every other compilation unit in the
14459 cache that we must keep because we are keeping CU. */
14460
ae038cb0
DJ
14461static void
14462dwarf2_mark (struct dwarf2_cu *cu)
14463{
14464 if (cu->mark)
14465 return;
14466 cu->mark = 1;
10b3939b
DJ
14467 if (cu->dependencies != NULL)
14468 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14469}
14470
14471static void
14472dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14473{
14474 while (per_cu)
14475 {
14476 per_cu->cu->mark = 0;
14477 per_cu = per_cu->cu->read_in_chain;
14478 }
72bf9492
DJ
14479}
14480
72bf9492
DJ
14481/* Trivial hash function for partial_die_info: the hash value of a DIE
14482 is its offset in .debug_info for this objfile. */
14483
14484static hashval_t
14485partial_die_hash (const void *item)
14486{
14487 const struct partial_die_info *part_die = item;
9a619af0 14488
72bf9492
DJ
14489 return part_die->offset;
14490}
14491
14492/* Trivial comparison function for partial_die_info structures: two DIEs
14493 are equal if they have the same offset. */
14494
14495static int
14496partial_die_eq (const void *item_lhs, const void *item_rhs)
14497{
14498 const struct partial_die_info *part_die_lhs = item_lhs;
14499 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14500
72bf9492
DJ
14501 return part_die_lhs->offset == part_die_rhs->offset;
14502}
14503
ae038cb0
DJ
14504static struct cmd_list_element *set_dwarf2_cmdlist;
14505static struct cmd_list_element *show_dwarf2_cmdlist;
14506
14507static void
14508set_dwarf2_cmd (char *args, int from_tty)
14509{
14510 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14511}
14512
14513static void
14514show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14515{
ae038cb0
DJ
14516 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14517}
14518
dce234bc
PP
14519/* If section described by INFO was mmapped, munmap it now. */
14520
14521static void
14522munmap_section_buffer (struct dwarf2_section_info *info)
14523{
14524 if (info->was_mmapped)
14525 {
14526#ifdef HAVE_MMAP
14527 intptr_t begin = (intptr_t) info->buffer;
14528 intptr_t map_begin = begin & ~(pagesize - 1);
14529 size_t map_length = info->size + begin - map_begin;
9a619af0 14530
dce234bc
PP
14531 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14532#else
14533 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14534 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14535#endif
14536 }
14537}
14538
14539/* munmap debug sections for OBJFILE, if necessary. */
14540
14541static void
c1bd65d0 14542dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14543{
14544 struct dwarf2_per_objfile *data = d;
9a619af0 14545
dce234bc
PP
14546 munmap_section_buffer (&data->info);
14547 munmap_section_buffer (&data->abbrev);
14548 munmap_section_buffer (&data->line);
14549 munmap_section_buffer (&data->str);
14550 munmap_section_buffer (&data->macinfo);
14551 munmap_section_buffer (&data->ranges);
14552 munmap_section_buffer (&data->loc);
14553 munmap_section_buffer (&data->frame);
14554 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14555 munmap_section_buffer (&data->gdb_index);
14556}
14557
14558\f
14559
14560/* The contents of the hash table we create when building the string
14561 table. */
14562struct strtab_entry
14563{
14564 offset_type offset;
14565 const char *str;
14566};
14567
14568/* Hash function for a strtab_entry. */
14569static hashval_t
14570hash_strtab_entry (const void *e)
14571{
14572 const struct strtab_entry *entry = e;
14573 return mapped_index_string_hash (entry->str);
14574}
14575
14576/* Equality function for a strtab_entry. */
14577static int
14578eq_strtab_entry (const void *a, const void *b)
14579{
14580 const struct strtab_entry *ea = a;
14581 const struct strtab_entry *eb = b;
14582 return !strcmp (ea->str, eb->str);
14583}
14584
14585/* Create a strtab_entry hash table. */
14586static htab_t
14587create_strtab (void)
14588{
14589 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14590 xfree, xcalloc, xfree);
14591}
14592
14593/* Add a string to the constant pool. Return the string's offset in
14594 host order. */
14595static offset_type
14596add_string (htab_t table, struct obstack *cpool, const char *str)
14597{
14598 void **slot;
14599 struct strtab_entry entry;
14600 struct strtab_entry *result;
14601
14602 entry.str = str;
14603 slot = htab_find_slot (table, &entry, INSERT);
14604 if (*slot)
14605 result = *slot;
14606 else
14607 {
14608 result = XNEW (struct strtab_entry);
14609 result->offset = obstack_object_size (cpool);
14610 result->str = str;
14611 obstack_grow_str0 (cpool, str);
14612 *slot = result;
14613 }
14614 return result->offset;
14615}
14616
14617/* An entry in the symbol table. */
14618struct symtab_index_entry
14619{
14620 /* The name of the symbol. */
14621 const char *name;
14622 /* The offset of the name in the constant pool. */
14623 offset_type index_offset;
14624 /* A sorted vector of the indices of all the CUs that hold an object
14625 of this name. */
14626 VEC (offset_type) *cu_indices;
14627};
14628
14629/* The symbol table. This is a power-of-2-sized hash table. */
14630struct mapped_symtab
14631{
14632 offset_type n_elements;
14633 offset_type size;
14634 struct symtab_index_entry **data;
14635};
14636
14637/* Hash function for a symtab_index_entry. */
14638static hashval_t
14639hash_symtab_entry (const void *e)
14640{
14641 const struct symtab_index_entry *entry = e;
14642 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14643 sizeof (offset_type) * VEC_length (offset_type,
14644 entry->cu_indices),
14645 0);
14646}
14647
14648/* Equality function for a symtab_index_entry. */
14649static int
14650eq_symtab_entry (const void *a, const void *b)
14651{
14652 const struct symtab_index_entry *ea = a;
14653 const struct symtab_index_entry *eb = b;
14654 int len = VEC_length (offset_type, ea->cu_indices);
14655 if (len != VEC_length (offset_type, eb->cu_indices))
14656 return 0;
14657 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14658 VEC_address (offset_type, eb->cu_indices),
14659 sizeof (offset_type) * len);
14660}
14661
14662/* Destroy a symtab_index_entry. */
14663static void
14664delete_symtab_entry (void *p)
14665{
14666 struct symtab_index_entry *entry = p;
14667 VEC_free (offset_type, entry->cu_indices);
14668 xfree (entry);
14669}
14670
14671/* Create a hash table holding symtab_index_entry objects. */
14672static htab_t
14673create_index_table (void)
14674{
14675 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14676 delete_symtab_entry, xcalloc, xfree);
14677}
14678
14679/* Create a new mapped symtab object. */
14680static struct mapped_symtab *
14681create_mapped_symtab (void)
14682{
14683 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14684 symtab->n_elements = 0;
14685 symtab->size = 1024;
14686 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14687 return symtab;
14688}
14689
14690/* Destroy a mapped_symtab. */
14691static void
14692cleanup_mapped_symtab (void *p)
14693{
14694 struct mapped_symtab *symtab = p;
14695 /* The contents of the array are freed when the other hash table is
14696 destroyed. */
14697 xfree (symtab->data);
14698 xfree (symtab);
14699}
14700
14701/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14702 the slot. */
14703static struct symtab_index_entry **
14704find_slot (struct mapped_symtab *symtab, const char *name)
14705{
14706 offset_type index, step, hash = mapped_index_string_hash (name);
14707
14708 index = hash & (symtab->size - 1);
14709 step = ((hash * 17) & (symtab->size - 1)) | 1;
14710
14711 for (;;)
14712 {
14713 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14714 return &symtab->data[index];
14715 index = (index + step) & (symtab->size - 1);
14716 }
14717}
14718
14719/* Expand SYMTAB's hash table. */
14720static void
14721hash_expand (struct mapped_symtab *symtab)
14722{
14723 offset_type old_size = symtab->size;
14724 offset_type i;
14725 struct symtab_index_entry **old_entries = symtab->data;
14726
14727 symtab->size *= 2;
14728 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14729
14730 for (i = 0; i < old_size; ++i)
14731 {
14732 if (old_entries[i])
14733 {
14734 struct symtab_index_entry **slot = find_slot (symtab,
14735 old_entries[i]->name);
14736 *slot = old_entries[i];
14737 }
14738 }
14739
14740 xfree (old_entries);
14741}
14742
14743/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14744 is the index of the CU in which the symbol appears. */
14745static void
14746add_index_entry (struct mapped_symtab *symtab, const char *name,
14747 offset_type cu_index)
14748{
14749 struct symtab_index_entry **slot;
14750
14751 ++symtab->n_elements;
14752 if (4 * symtab->n_elements / 3 >= symtab->size)
14753 hash_expand (symtab);
14754
14755 slot = find_slot (symtab, name);
14756 if (!*slot)
14757 {
14758 *slot = XNEW (struct symtab_index_entry);
14759 (*slot)->name = name;
14760 (*slot)->cu_indices = NULL;
14761 }
14762 /* Don't push an index twice. Due to how we add entries we only
14763 have to check the last one. */
14764 if (VEC_empty (offset_type, (*slot)->cu_indices)
14765 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14766 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14767}
14768
14769/* Add a vector of indices to the constant pool. */
14770static offset_type
14771add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14772 struct symtab_index_entry *entry)
14773{
14774 void **slot;
14775
14776 slot = htab_find_slot (index_table, entry, INSERT);
14777 if (!*slot)
14778 {
14779 offset_type len = VEC_length (offset_type, entry->cu_indices);
14780 offset_type val = MAYBE_SWAP (len);
14781 offset_type iter;
14782 int i;
14783
14784 *slot = entry;
14785 entry->index_offset = obstack_object_size (cpool);
14786
14787 obstack_grow (cpool, &val, sizeof (val));
14788 for (i = 0;
14789 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14790 ++i)
14791 {
14792 val = MAYBE_SWAP (iter);
14793 obstack_grow (cpool, &val, sizeof (val));
14794 }
14795 }
14796 else
14797 {
14798 struct symtab_index_entry *old_entry = *slot;
14799 entry->index_offset = old_entry->index_offset;
14800 entry = old_entry;
14801 }
14802 return entry->index_offset;
14803}
14804
14805/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14806 constant pool entries going into the obstack CPOOL. */
14807static void
14808write_hash_table (struct mapped_symtab *symtab,
14809 struct obstack *output, struct obstack *cpool)
14810{
14811 offset_type i;
14812 htab_t index_table;
14813 htab_t str_table;
14814
14815 index_table = create_index_table ();
14816 str_table = create_strtab ();
14817 /* We add all the index vectors to the constant pool first, to
14818 ensure alignment is ok. */
14819 for (i = 0; i < symtab->size; ++i)
14820 {
14821 if (symtab->data[i])
14822 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14823 }
14824
14825 /* Now write out the hash table. */
14826 for (i = 0; i < symtab->size; ++i)
14827 {
14828 offset_type str_off, vec_off;
14829
14830 if (symtab->data[i])
14831 {
14832 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14833 vec_off = symtab->data[i]->index_offset;
14834 }
14835 else
14836 {
14837 /* While 0 is a valid constant pool index, it is not valid
14838 to have 0 for both offsets. */
14839 str_off = 0;
14840 vec_off = 0;
14841 }
14842
14843 str_off = MAYBE_SWAP (str_off);
14844 vec_off = MAYBE_SWAP (vec_off);
14845
14846 obstack_grow (output, &str_off, sizeof (str_off));
14847 obstack_grow (output, &vec_off, sizeof (vec_off));
14848 }
14849
14850 htab_delete (str_table);
14851 htab_delete (index_table);
14852}
14853
14854/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14855 from PST; CU_INDEX is the index of the CU in the vector of all
14856 CUs. */
14857static void
14858add_address_entry (struct objfile *objfile,
14859 struct obstack *addr_obstack, struct partial_symtab *pst,
14860 unsigned int cu_index)
14861{
14862 offset_type offset;
14863 char addr[8];
14864 CORE_ADDR baseaddr;
14865
1fd400ff
TT
14866 /* Don't bother recording empty ranges. */
14867 if (pst->textlow == pst->texthigh)
14868 return;
14869
9291a0cd
TT
14870 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14871
14872 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14873 obstack_grow (addr_obstack, addr, 8);
14874 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14875 obstack_grow (addr_obstack, addr, 8);
14876 offset = MAYBE_SWAP (cu_index);
14877 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14878}
14879
14880/* Add a list of partial symbols to SYMTAB. */
14881static void
14882write_psymbols (struct mapped_symtab *symtab,
14883 struct partial_symbol **psymp,
14884 int count,
14885 offset_type cu_index)
14886{
14887 for (; count-- > 0; ++psymp)
14888 {
14889 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14890 error (_("Ada is not currently supported by the index"));
14891 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14892 }
14893}
14894
14895/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14896 exception if there is an error. */
14897static void
14898write_obstack (FILE *file, struct obstack *obstack)
14899{
14900 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14901 file)
14902 != obstack_object_size (obstack))
14903 error (_("couldn't data write to file"));
14904}
14905
14906/* Unlink a file if the argument is not NULL. */
14907static void
14908unlink_if_set (void *p)
14909{
14910 char **filename = p;
14911 if (*filename)
14912 unlink (*filename);
14913}
14914
1fd400ff
TT
14915/* A helper struct used when iterating over debug_types. */
14916struct signatured_type_index_data
14917{
14918 struct objfile *objfile;
14919 struct mapped_symtab *symtab;
14920 struct obstack *types_list;
14921 int cu_index;
14922};
14923
14924/* A helper function that writes a single signatured_type to an
14925 obstack. */
14926static int
14927write_one_signatured_type (void **slot, void *d)
14928{
14929 struct signatured_type_index_data *info = d;
14930 struct signatured_type *entry = (struct signatured_type *) *slot;
14931 struct dwarf2_per_cu_data *cu = &entry->per_cu;
14932 struct partial_symtab *psymtab = cu->v.psymtab;
14933 gdb_byte val[8];
14934
14935 write_psymbols (info->symtab,
14936 info->objfile->global_psymbols.list + psymtab->globals_offset,
14937 psymtab->n_global_syms, info->cu_index);
14938 write_psymbols (info->symtab,
14939 info->objfile->static_psymbols.list + psymtab->statics_offset,
14940 psymtab->n_static_syms, info->cu_index);
14941
14942 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14943 obstack_grow (info->types_list, val, 8);
14944 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14945 obstack_grow (info->types_list, val, 8);
14946 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14947 obstack_grow (info->types_list, val, 8);
14948
14949 ++info->cu_index;
14950
14951 return 1;
14952}
14953
9291a0cd
TT
14954/* Create an index file for OBJFILE in the directory DIR. */
14955static void
14956write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14957{
14958 struct cleanup *cleanup;
14959 char *filename, *cleanup_filename;
1fd400ff
TT
14960 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14961 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14962 int i;
14963 FILE *out_file;
14964 struct mapped_symtab *symtab;
14965 offset_type val, size_of_contents, total_len;
14966 struct stat st;
14967 char buf[8];
14968
14969 if (!objfile->psymtabs)
14970 return;
14971 if (dwarf2_per_objfile->using_index)
14972 error (_("Cannot use an index to create the index"));
14973
14974 if (stat (objfile->name, &st) < 0)
14975 perror_with_name (_("Could not stat"));
14976
14977 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14978 INDEX_SUFFIX, (char *) NULL);
14979 cleanup = make_cleanup (xfree, filename);
14980
14981 out_file = fopen (filename, "wb");
14982 if (!out_file)
14983 error (_("Can't open `%s' for writing"), filename);
14984
14985 cleanup_filename = filename;
14986 make_cleanup (unlink_if_set, &cleanup_filename);
14987
14988 symtab = create_mapped_symtab ();
14989 make_cleanup (cleanup_mapped_symtab, symtab);
14990
14991 obstack_init (&addr_obstack);
14992 make_cleanup_obstack_free (&addr_obstack);
14993
14994 obstack_init (&cu_list);
14995 make_cleanup_obstack_free (&cu_list);
14996
1fd400ff
TT
14997 obstack_init (&types_cu_list);
14998 make_cleanup_obstack_free (&types_cu_list);
14999
15000 /* The list is already sorted, so we don't need to do additional
15001 work here. Also, the debug_types entries do not appear in
15002 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15003 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15004 {
15005 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
15006 struct partial_symtab *psymtab = cu->v.psymtab;
15007 gdb_byte val[8];
15008
15009 write_psymbols (symtab,
15010 objfile->global_psymbols.list + psymtab->globals_offset,
15011 psymtab->n_global_syms, i);
15012 write_psymbols (symtab,
15013 objfile->static_psymbols.list + psymtab->statics_offset,
15014 psymtab->n_static_syms, i);
15015
15016 add_address_entry (objfile, &addr_obstack, psymtab, i);
15017
15018 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
15019 obstack_grow (&cu_list, val, 8);
15020 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
15021 obstack_grow (&cu_list, val, 8);
15022 }
15023
1fd400ff
TT
15024 /* Write out the .debug_type entries, if any. */
15025 if (dwarf2_per_objfile->signatured_types)
15026 {
15027 struct signatured_type_index_data sig_data;
15028
15029 sig_data.objfile = objfile;
15030 sig_data.symtab = symtab;
15031 sig_data.types_list = &types_cu_list;
15032 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15033 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15034 write_one_signatured_type, &sig_data);
15035 }
15036
9291a0cd
TT
15037 obstack_init (&constant_pool);
15038 make_cleanup_obstack_free (&constant_pool);
15039 obstack_init (&symtab_obstack);
15040 make_cleanup_obstack_free (&symtab_obstack);
15041 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15042
15043 obstack_init (&contents);
15044 make_cleanup_obstack_free (&contents);
1fd400ff 15045 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15046 total_len = size_of_contents;
15047
15048 /* The version number. */
1fd400ff 15049 val = MAYBE_SWAP (2);
9291a0cd
TT
15050 obstack_grow (&contents, &val, sizeof (val));
15051
15052 /* The offset of the CU list from the start of the file. */
15053 val = MAYBE_SWAP (total_len);
15054 obstack_grow (&contents, &val, sizeof (val));
15055 total_len += obstack_object_size (&cu_list);
15056
1fd400ff
TT
15057 /* The offset of the types CU list from the start of the file. */
15058 val = MAYBE_SWAP (total_len);
15059 obstack_grow (&contents, &val, sizeof (val));
15060 total_len += obstack_object_size (&types_cu_list);
15061
9291a0cd
TT
15062 /* The offset of the address table from the start of the file. */
15063 val = MAYBE_SWAP (total_len);
15064 obstack_grow (&contents, &val, sizeof (val));
15065 total_len += obstack_object_size (&addr_obstack);
15066
15067 /* The offset of the symbol table from the start of the file. */
15068 val = MAYBE_SWAP (total_len);
15069 obstack_grow (&contents, &val, sizeof (val));
15070 total_len += obstack_object_size (&symtab_obstack);
15071
15072 /* The offset of the constant pool from the start of the file. */
15073 val = MAYBE_SWAP (total_len);
15074 obstack_grow (&contents, &val, sizeof (val));
15075 total_len += obstack_object_size (&constant_pool);
15076
15077 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15078
15079 write_obstack (out_file, &contents);
15080 write_obstack (out_file, &cu_list);
1fd400ff 15081 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15082 write_obstack (out_file, &addr_obstack);
15083 write_obstack (out_file, &symtab_obstack);
15084 write_obstack (out_file, &constant_pool);
15085
15086 fclose (out_file);
15087
15088 /* We want to keep the file, so we set cleanup_filename to NULL
15089 here. See unlink_if_set. */
15090 cleanup_filename = NULL;
15091
15092 do_cleanups (cleanup);
15093}
15094
15095/* The mapped index file format is designed to be directly mmap()able
15096 on any architecture. In most cases, a datum is represented using a
15097 little-endian 32-bit integer value, called an offset_type. Big
15098 endian machines must byte-swap the values before using them.
15099 Exceptions to this rule are noted. The data is laid out such that
15100 alignment is always respected.
15101
15102 A mapped index consists of several sections.
15103
15104 1. The file header. This is a sequence of values, of offset_type
15105 unless otherwise noted:
1fd400ff
TT
15106 [0] The version number. Currently 1 or 2. The differences are
15107 noted below. Version 1 did not account for .debug_types sections;
15108 the presence of a .debug_types section invalidates any version 1
15109 index that may exist.
9291a0cd 15110 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
15111 [1.5] In version 2, the offset, from the start of the file, of the
15112 types CU list. This offset does not appear in version 1. Note
15113 that this can be empty, in which case this offset will be equal to
15114 the next offset.
9291a0cd
TT
15115 [2] The offset, from the start of the file, of the address section.
15116 [3] The offset, from the start of the file, of the symbol table.
15117 [4] The offset, from the start of the file, of the constant pool.
15118
15119 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15120 little-endian values, sorted by the CU offset. The first element
15121 in each pair is the offset of a CU in the .debug_info section. The
15122 second element in each pair is the length of that CU. References
15123 to a CU elsewhere in the map are done using a CU index, which is
15124 just the 0-based index into this table. Note that if there are
15125 type CUs, then conceptually CUs and type CUs form a single list for
15126 the purposes of CU indices.
15127
15128 2.5 The types CU list. This does not appear in a version 1 index.
15129 This is a sequence of triplets of 64-bit little-endian values. In
15130 a triplet, the first value is the CU offset, the second value is
15131 the type offset in the CU, and the third value is the type
15132 signature. The types CU list is not sorted.
9291a0cd
TT
15133
15134 3. The address section. The address section consists of a sequence
15135 of address entries. Each address entry has three elements.
15136 [0] The low address. This is a 64-bit little-endian value.
15137 [1] The high address. This is a 64-bit little-endian value.
15138 [2] The CU index. This is an offset_type value.
15139
15140 4. The symbol table. This is a hash table. The size of the hash
15141 table is always a power of 2. The initial hash and the step are
15142 currently defined by the `find_slot' function.
15143
15144 Each slot in the hash table consists of a pair of offset_type
15145 values. The first value is the offset of the symbol's name in the
15146 constant pool. The second value is the offset of the CU vector in
15147 the constant pool.
15148
15149 If both values are 0, then this slot in the hash table is empty.
15150 This is ok because while 0 is a valid constant pool index, it
15151 cannot be a valid index for both a string and a CU vector.
15152
15153 A string in the constant pool is stored as a \0-terminated string,
15154 as you'd expect.
15155
15156 A CU vector in the constant pool is a sequence of offset_type
15157 values. The first value is the number of CU indices in the vector.
15158 Each subsequent value is the index of a CU in the CU list. This
15159 element in the hash table is used to indicate which CUs define the
15160 symbol.
15161
15162 5. The constant pool. This is simply a bunch of bytes. It is
15163 organized so that alignment is correct: CU vectors are stored
15164 first, followed by strings. */
15165static void
15166save_gdb_index_command (char *arg, int from_tty)
15167{
15168 struct objfile *objfile;
15169
15170 if (!arg || !*arg)
96d19272 15171 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15172
15173 ALL_OBJFILES (objfile)
15174 {
15175 struct stat st;
15176
15177 /* If the objfile does not correspond to an actual file, skip it. */
15178 if (stat (objfile->name, &st) < 0)
15179 continue;
15180
15181 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15182 if (dwarf2_per_objfile)
15183 {
15184 volatile struct gdb_exception except;
15185
15186 TRY_CATCH (except, RETURN_MASK_ERROR)
15187 {
15188 write_psymtabs_to_index (objfile, arg);
15189 }
15190 if (except.reason < 0)
15191 exception_fprintf (gdb_stderr, except,
15192 _("Error while writing index for `%s': "),
15193 objfile->name);
15194 }
15195 }
dce234bc
PP
15196}
15197
9291a0cd
TT
15198\f
15199
9eae7c52
TT
15200int dwarf2_always_disassemble;
15201
15202static void
15203show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15204 struct cmd_list_element *c, const char *value)
15205{
15206 fprintf_filtered (file, _("\
15207Whether to always disassemble DWARF expressions is %s.\n"),
15208 value);
15209}
15210
6502dd73
DJ
15211void _initialize_dwarf2_read (void);
15212
15213void
15214_initialize_dwarf2_read (void)
15215{
96d19272
JK
15216 struct cmd_list_element *c;
15217
dce234bc 15218 dwarf2_objfile_data_key
c1bd65d0 15219 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15220
1bedd215
AC
15221 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15222Set DWARF 2 specific variables.\n\
15223Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15224 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15225 0/*allow-unknown*/, &maintenance_set_cmdlist);
15226
1bedd215
AC
15227 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15228Show DWARF 2 specific variables\n\
15229Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15230 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15231 0/*allow-unknown*/, &maintenance_show_cmdlist);
15232
15233 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15234 &dwarf2_max_cache_age, _("\
15235Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15236Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15237A higher limit means that cached compilation units will be stored\n\
15238in memory longer, and more total memory will be used. Zero disables\n\
15239caching, which can slow down startup."),
2c5b56ce 15240 NULL,
920d2a44 15241 show_dwarf2_max_cache_age,
2c5b56ce 15242 &set_dwarf2_cmdlist,
ae038cb0 15243 &show_dwarf2_cmdlist);
d97bc12b 15244
9eae7c52
TT
15245 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15246 &dwarf2_always_disassemble, _("\
15247Set whether `info address' always disassembles DWARF expressions."), _("\
15248Show whether `info address' always disassembles DWARF expressions."), _("\
15249When enabled, DWARF expressions are always printed in an assembly-like\n\
15250syntax. When disabled, expressions will be printed in a more\n\
15251conversational style, when possible."),
15252 NULL,
15253 show_dwarf2_always_disassemble,
15254 &set_dwarf2_cmdlist,
15255 &show_dwarf2_cmdlist);
15256
d97bc12b
DE
15257 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15258Set debugging of the dwarf2 DIE reader."), _("\
15259Show debugging of the dwarf2 DIE reader."), _("\
15260When enabled (non-zero), DIEs are dumped after they are read in.\n\
15261The value is the maximum depth to print."),
15262 NULL,
15263 NULL,
15264 &setdebuglist, &showdebuglist);
9291a0cd 15265
96d19272
JK
15266 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15267 _("Save a .gdb-index file"),
15268 &save_cmdlist);
15269 set_cmd_completer (c, filename_completer);
6502dd73 15270}
This page took 1.871141 seconds and 4 git commands to generate.