*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
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;
36374493 1993 struct dwarf2_section_info* sec;
9291a0cd
TT
1994 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1995 int has_children, i;
1996 struct dwarf2_cu cu;
1997 unsigned int bytes_read, buffer_size;
1998 struct die_reader_specs reader_specs;
1999 char *name, *comp_dir;
2000
2001 if (this_cu->v.quick->read_lines)
2002 return;
2003 this_cu->v.quick->read_lines = 1;
2004
2005 memset (&cu, 0, sizeof (cu));
2006 cu.objfile = objfile;
2007 obstack_init (&cu.comp_unit_obstack);
2008
2009 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2010
36374493
DE
2011 if (this_cu->from_debug_types)
2012 sec = &dwarf2_per_objfile->types;
2013 else
2014 sec = &dwarf2_per_objfile->info;
2015 dwarf2_read_section (objfile, sec);
2016 buffer_size = sec->size;
2017 buffer = sec->buffer;
9291a0cd
TT
2018 info_ptr = buffer + this_cu->offset;
2019 beg_of_comp_unit = info_ptr;
2020
2021 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2022 buffer, buffer_size,
2023 abfd);
2024
2025 /* Complete the cu_header. */
2026 cu.header.offset = beg_of_comp_unit - buffer;
2027 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2028
2029 this_cu->cu = &cu;
2030 cu.per_cu = this_cu;
2031
2032 dwarf2_read_abbrevs (abfd, &cu);
2033 make_cleanup (dwarf2_free_abbrev_table, &cu);
2034
2035 if (this_cu->from_debug_types)
2036 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2037 init_cu_die_reader (&reader_specs, &cu);
2038 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2039 &has_children);
2040
2041 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2042 if (attr)
2043 {
2044 unsigned int line_offset = DW_UNSND (attr);
2045 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2046 }
2047 if (lh == NULL)
2048 {
2049 do_cleanups (cleanups);
2050 return;
2051 }
2052
2053 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2054
2055 this_cu->v.quick->lines = lh;
2056
2057 this_cu->v.quick->file_names
2058 = obstack_alloc (&objfile->objfile_obstack,
2059 lh->num_file_names * sizeof (char *));
2060 for (i = 0; i < lh->num_file_names; ++i)
2061 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2062
2063 do_cleanups (cleanups);
2064}
2065
2066/* A helper for the "quick" functions which computes and caches the
2067 real path for a given file name from the line table.
2068 dw2_require_line_header must have been called before this is
2069 invoked. */
2070static const char *
2071dw2_require_full_path (struct objfile *objfile,
e254ef6a 2072 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2073 int index)
2074{
e254ef6a
DE
2075 if (!per_cu->v.quick->full_names)
2076 per_cu->v.quick->full_names
9291a0cd 2077 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2078 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2079 sizeof (char *));
2080
e254ef6a
DE
2081 if (!per_cu->v.quick->full_names[index])
2082 per_cu->v.quick->full_names[index]
2083 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2084
e254ef6a 2085 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2086}
2087
2088static struct symtab *
2089dw2_find_last_source_symtab (struct objfile *objfile)
2090{
2091 int index;
2092 dw2_setup (objfile);
2093 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2094 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2095}
2096
2097static void
2098dw2_forget_cached_source_info (struct objfile *objfile)
2099{
2100 int i;
2101
2102 dw2_setup (objfile);
1fd400ff
TT
2103 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2104 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2105 {
e254ef6a 2106 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2107
e254ef6a 2108 if (per_cu->v.quick->full_names)
9291a0cd
TT
2109 {
2110 int j;
2111
e254ef6a
DE
2112 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2113 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
2114 }
2115 }
2116}
2117
2118static int
2119dw2_lookup_symtab (struct objfile *objfile, const char *name,
2120 const char *full_path, const char *real_path,
2121 struct symtab **result)
2122{
2123 int i;
2124 int check_basename = lbasename (name) == name;
2125 struct dwarf2_per_cu_data *base_cu = NULL;
2126
2127 dw2_setup (objfile);
1fd400ff
TT
2128 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2129 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2130 {
2131 int j;
e254ef6a 2132 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2133
e254ef6a 2134 if (per_cu->v.quick->symtab)
9291a0cd
TT
2135 continue;
2136
e254ef6a
DE
2137 dw2_require_line_header (objfile, per_cu);
2138 if (!per_cu->v.quick->lines)
9291a0cd
TT
2139 continue;
2140
e254ef6a 2141 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2142 {
e254ef6a 2143 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2144
2145 if (FILENAME_CMP (name, this_name) == 0)
2146 {
e254ef6a 2147 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2148 return 1;
2149 }
2150
2151 if (check_basename && ! base_cu
2152 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2153 base_cu = per_cu;
9291a0cd
TT
2154
2155 if (full_path != NULL)
2156 {
2157 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2158 per_cu, j);
9291a0cd
TT
2159
2160 if (this_full_name
2161 && FILENAME_CMP (full_path, this_full_name) == 0)
2162 {
e254ef6a 2163 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2164 return 1;
2165 }
2166 }
2167
2168 if (real_path != NULL)
2169 {
2170 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2171 per_cu, j);
9291a0cd
TT
2172
2173 if (this_full_name != NULL)
2174 {
2175 char *rp = gdb_realpath (this_full_name);
2176 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2177 {
2178 xfree (rp);
e254ef6a 2179 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2180 return 1;
2181 }
2182 xfree (rp);
2183 }
2184 }
2185 }
2186 }
2187
2188 if (base_cu)
2189 {
2190 *result = dw2_instantiate_symtab (objfile, base_cu);
2191 return 1;
2192 }
2193
2194 return 0;
2195}
2196
2197static struct symtab *
2198dw2_lookup_symbol (struct objfile *objfile, int block_index,
2199 const char *name, domain_enum domain)
2200{
faf68db7 2201 /* We do all the work in the expand_one_symtab_matching hook
9291a0cd
TT
2202 instead. */
2203 return NULL;
2204}
2205
2206/* A helper function that expands all symtabs that hold an object
2207 named NAME. */
2208static void
2209dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2210{
2211 dw2_setup (objfile);
2212
2213 if (dwarf2_per_objfile->index_table)
2214 {
2215 offset_type *vec;
2216
2217 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2218 name, &vec))
2219 {
2220 offset_type i, len = MAYBE_SWAP (*vec);
2221 for (i = 0; i < len; ++i)
2222 {
2223 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2224 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2225
e254ef6a 2226 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2227 }
2228 }
2229 }
2230}
2231
faf68db7
TT
2232static struct symbol *
2233dw2_expand_one_symtab_matching (struct objfile *objfile,
2234 int kind, const char *name,
2235 domain_enum domain,
2236 struct symbol *(*matcher) (struct symtab *,
2237 int,
2238 const char *,
2239 domain_enum,
2240 void *),
2241 void *data)
9291a0cd 2242{
faf68db7
TT
2243 dw2_setup (objfile);
2244
2245 if (dwarf2_per_objfile->index_table)
2246 {
2247 offset_type *vec;
2248
2249 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2250 name, &vec))
2251 {
2252 offset_type i, len = MAYBE_SWAP (*vec);
2253 for (i = 0; i < len; ++i)
2254 {
2255 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2256 struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2257 struct symtab *symtab;
2258 struct symbol *sym;
2259
2260 if (cu->v.quick->symtab)
2261 continue;
2262
2263 symtab = dw2_instantiate_symtab (objfile, cu);
2264 sym = matcher (symtab, kind, name, domain, data);
2265 if (sym)
2266 return sym;
2267 }
2268 }
2269 }
2270
2271 return NULL;
9291a0cd
TT
2272}
2273
2274static void
2275dw2_print_stats (struct objfile *objfile)
2276{
2277 int i, count;
2278
2279 dw2_setup (objfile);
2280 count = 0;
1fd400ff
TT
2281 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2282 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2283 {
e254ef6a 2284 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2285
e254ef6a 2286 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2287 ++count;
2288 }
2289 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2290}
2291
2292static void
2293dw2_dump (struct objfile *objfile)
2294{
2295 /* Nothing worth printing. */
2296}
2297
2298static void
2299dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2300 struct section_offsets *delta)
2301{
2302 /* There's nothing to relocate here. */
2303}
2304
2305static void
2306dw2_expand_symtabs_for_function (struct objfile *objfile,
2307 const char *func_name)
2308{
2309 dw2_do_expand_symtabs_matching (objfile, func_name);
2310}
2311
2312static void
2313dw2_expand_all_symtabs (struct objfile *objfile)
2314{
2315 int i;
2316
2317 dw2_setup (objfile);
1fd400ff
TT
2318
2319 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2320 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2321 {
e254ef6a 2322 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2323
e254ef6a 2324 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2325 }
2326}
2327
2328static void
2329dw2_expand_symtabs_with_filename (struct objfile *objfile,
2330 const char *filename)
2331{
2332 int i;
2333
2334 dw2_setup (objfile);
1fd400ff
TT
2335 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2336 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2337 {
2338 int j;
e254ef6a 2339 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2340
e254ef6a 2341 if (per_cu->v.quick->symtab)
9291a0cd
TT
2342 continue;
2343
e254ef6a
DE
2344 dw2_require_line_header (objfile, per_cu);
2345 if (!per_cu->v.quick->lines)
9291a0cd
TT
2346 continue;
2347
e254ef6a 2348 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2349 {
e254ef6a 2350 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2351 if (strcmp (this_name, filename) == 0)
2352 {
e254ef6a 2353 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2354 break;
2355 }
2356 }
2357 }
2358}
2359
dd786858 2360static const char *
9291a0cd
TT
2361dw2_find_symbol_file (struct objfile *objfile, const char *name)
2362{
e254ef6a 2363 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2364 offset_type *vec;
2365
2366 dw2_setup (objfile);
2367
2368 if (!dwarf2_per_objfile->index_table)
2369 return NULL;
2370
2371 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2372 name, &vec))
2373 return NULL;
2374
2375 /* Note that this just looks at the very first one named NAME -- but
2376 actually we are looking for a function. find_main_filename
2377 should be rewritten so that it doesn't require a custom hook. It
2378 could just use the ordinary symbol tables. */
2379 /* vec[0] is the length, which must always be >0. */
e254ef6a 2380 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2381
e254ef6a
DE
2382 dw2_require_line_header (objfile, per_cu);
2383 if (!per_cu->v.quick->lines)
9291a0cd
TT
2384 return NULL;
2385
e254ef6a 2386 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2387}
2388
2389static void
2390dw2_map_ada_symtabs (struct objfile *objfile,
2391 int (*wild_match) (const char *, int, const char *),
2392 int (*is_name_suffix) (const char *),
2393 void (*callback) (struct objfile *,
2394 struct symtab *, void *),
2395 const char *name, int global,
2396 domain_enum namespace, int wild,
2397 void *data)
2398{
2399 /* For now, we don't support Ada, so this function can't be
2400 reached. */
2401 internal_error (__FILE__, __LINE__,
2402 _("map_ada_symtabs called via index method"));
2403}
2404
2405static void
2406dw2_expand_symtabs_matching (struct objfile *objfile,
2407 int (*file_matcher) (const char *, void *),
2408 int (*name_matcher) (const char *, void *),
2409 domain_enum kind,
2410 void *data)
2411{
2412 int i;
2413 offset_type iter;
2414
2415 dw2_setup (objfile);
2416 if (!dwarf2_per_objfile->index_table)
2417 return;
2418
1fd400ff
TT
2419 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2420 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2421 {
2422 int j;
e254ef6a 2423 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2424
e254ef6a
DE
2425 per_cu->v.quick->mark = 0;
2426 if (per_cu->v.quick->symtab)
9291a0cd
TT
2427 continue;
2428
e254ef6a
DE
2429 dw2_require_line_header (objfile, per_cu);
2430 if (!per_cu->v.quick->lines)
9291a0cd
TT
2431 continue;
2432
e254ef6a 2433 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2434 {
e254ef6a 2435 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2436 {
e254ef6a 2437 per_cu->v.quick->mark = 1;
9291a0cd
TT
2438 break;
2439 }
2440 }
2441 }
2442
2443 for (iter = 0;
2444 iter < dwarf2_per_objfile->index_table->index_table_slots;
2445 ++iter)
2446 {
2447 offset_type idx = 2 * iter;
2448 const char *name;
2449 offset_type *vec, vec_len, vec_idx;
2450
2451 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2452 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2453 continue;
2454
2455 name = (dwarf2_per_objfile->index_table->constant_pool
2456 + dwarf2_per_objfile->index_table->index_table[idx]);
2457
2458 if (! (*name_matcher) (name, data))
2459 continue;
2460
2461 /* The name was matched, now expand corresponding CUs that were
2462 marked. */
2463 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2464 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2465 vec_len = MAYBE_SWAP (vec[0]);
2466 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2467 {
e254ef6a 2468 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2469
e254ef6a
DE
2470 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2471 if (per_cu->v.quick->mark)
2472 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2473 }
2474 }
2475}
2476
2477static struct symtab *
2478dw2_find_pc_sect_symtab (struct objfile *objfile,
2479 struct minimal_symbol *msymbol,
2480 CORE_ADDR pc,
2481 struct obj_section *section,
2482 int warn_if_readin)
2483{
2484 struct dwarf2_per_cu_data *data;
2485
2486 dw2_setup (objfile);
2487
2488 if (!objfile->psymtabs_addrmap)
2489 return NULL;
2490
2491 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2492 if (!data)
2493 return NULL;
2494
2495 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2496 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2497 paddress (get_objfile_arch (objfile), pc));
2498
2499 return dw2_instantiate_symtab (objfile, data);
2500}
2501
2502static void
2503dw2_map_symbol_names (struct objfile *objfile,
2504 void (*fun) (const char *, void *),
2505 void *data)
2506{
2507 offset_type iter;
2508 dw2_setup (objfile);
2509
2510 if (!dwarf2_per_objfile->index_table)
2511 return;
2512
2513 for (iter = 0;
2514 iter < dwarf2_per_objfile->index_table->index_table_slots;
2515 ++iter)
2516 {
2517 offset_type idx = 2 * iter;
2518 const char *name;
2519 offset_type *vec, vec_len, vec_idx;
2520
2521 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2522 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2523 continue;
2524
2525 name = (dwarf2_per_objfile->index_table->constant_pool
2526 + dwarf2_per_objfile->index_table->index_table[idx]);
2527
2528 (*fun) (name, data);
2529 }
2530}
2531
2532static void
2533dw2_map_symbol_filenames (struct objfile *objfile,
2534 void (*fun) (const char *, const char *, void *),
2535 void *data)
2536{
2537 int i;
2538
2539 dw2_setup (objfile);
1fd400ff
TT
2540 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2541 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2542 {
2543 int j;
e254ef6a 2544 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2545
e254ef6a 2546 if (per_cu->v.quick->symtab)
9291a0cd
TT
2547 continue;
2548
e254ef6a
DE
2549 dw2_require_line_header (objfile, per_cu);
2550 if (!per_cu->v.quick->lines)
9291a0cd
TT
2551 continue;
2552
e254ef6a 2553 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2554 {
e254ef6a
DE
2555 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2556 j);
2557 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2558 }
2559 }
2560}
2561
2562static int
2563dw2_has_symbols (struct objfile *objfile)
2564{
2565 return 1;
2566}
2567
2568const struct quick_symbol_functions dwarf2_gdb_index_functions =
2569{
2570 dw2_has_symbols,
2571 dw2_find_last_source_symtab,
2572 dw2_forget_cached_source_info,
2573 dw2_lookup_symtab,
2574 dw2_lookup_symbol,
faf68db7 2575 dw2_expand_one_symtab_matching,
9291a0cd
TT
2576 dw2_print_stats,
2577 dw2_dump,
2578 dw2_relocate,
2579 dw2_expand_symtabs_for_function,
2580 dw2_expand_all_symtabs,
2581 dw2_expand_symtabs_with_filename,
2582 dw2_find_symbol_file,
2583 dw2_map_ada_symtabs,
2584 dw2_expand_symtabs_matching,
2585 dw2_find_pc_sect_symtab,
2586 dw2_map_symbol_names,
2587 dw2_map_symbol_filenames
2588};
2589
2590/* Initialize for reading DWARF for this objfile. Return 0 if this
2591 file will use psymtabs, or 1 if using the GNU index. */
2592
2593int
2594dwarf2_initialize_objfile (struct objfile *objfile)
2595{
2596 /* If we're about to read full symbols, don't bother with the
2597 indices. In this case we also don't care if some other debug
2598 format is making psymtabs, because they are all about to be
2599 expanded anyway. */
2600 if ((objfile->flags & OBJF_READNOW))
2601 {
2602 int i;
2603
2604 dwarf2_per_objfile->using_index = 1;
2605 create_all_comp_units (objfile);
1fd400ff 2606 create_debug_types_hash_table (objfile);
9291a0cd 2607
1fd400ff
TT
2608 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2609 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2610 {
e254ef6a 2611 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2612
e254ef6a
DE
2613 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2614 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2615 }
2616
2617 /* Return 1 so that gdb sees the "quick" functions. However,
2618 these functions will be no-ops because we will have expanded
2619 all symtabs. */
2620 return 1;
2621 }
2622
2623 if (dwarf2_read_index (objfile))
2624 return 1;
2625
2626 dwarf2_build_psymtabs (objfile);
2627 return 0;
2628}
2629
2630\f
2631
dce234bc
PP
2632/* Build a partial symbol table. */
2633
2634void
f29dff0a 2635dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2636{
f29dff0a 2637 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2638 {
2639 init_psymbol_list (objfile, 1024);
2640 }
2641
d146bf1e 2642 dwarf2_build_psymtabs_hard (objfile);
c906108c 2643}
c906108c 2644
45452591
DE
2645/* Return TRUE if OFFSET is within CU_HEADER. */
2646
2647static inline int
2648offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2649{
2650 unsigned int bottom = cu_header->offset;
2651 unsigned int top = (cu_header->offset
2652 + cu_header->length
2653 + cu_header->initial_length_size);
9a619af0 2654
45452591
DE
2655 return (offset >= bottom && offset < top);
2656}
2657
93311388
DE
2658/* Read in the comp unit header information from the debug_info at info_ptr.
2659 NOTE: This leaves members offset, first_die_offset to be filled in
2660 by the caller. */
107d2387 2661
fe1b8b76 2662static gdb_byte *
107d2387 2663read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2664 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2665{
2666 int signed_addr;
891d2f0b 2667 unsigned int bytes_read;
c764a876
DE
2668
2669 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2670 cu_header->initial_length_size = bytes_read;
2671 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2672 info_ptr += bytes_read;
107d2387
AC
2673 cu_header->version = read_2_bytes (abfd, info_ptr);
2674 info_ptr += 2;
613e1657 2675 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2676 &bytes_read);
613e1657 2677 info_ptr += bytes_read;
107d2387
AC
2678 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2679 info_ptr += 1;
2680 signed_addr = bfd_get_sign_extend_vma (abfd);
2681 if (signed_addr < 0)
8e65ff28 2682 internal_error (__FILE__, __LINE__,
e2e0b3e5 2683 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2684 cu_header->signed_addr_p = signed_addr;
c764a876 2685
107d2387
AC
2686 return info_ptr;
2687}
2688
fe1b8b76
JB
2689static gdb_byte *
2690partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2691 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2692 bfd *abfd)
2693{
fe1b8b76 2694 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2695
2696 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2697
2dc7f7b3 2698 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2699 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2700 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2701 bfd_get_filename (abfd));
72bf9492 2702
dce234bc 2703 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2704 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2705 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2706 (long) header->abbrev_offset,
93311388 2707 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2708 bfd_get_filename (abfd));
2709
2710 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2711 > buffer + buffer_size)
8a3fe4f8
AC
2712 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2713 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2714 (long) header->length,
93311388 2715 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2716 bfd_get_filename (abfd));
2717
2718 return info_ptr;
2719}
2720
348e048f
DE
2721/* Read in the types comp unit header information from .debug_types entry at
2722 types_ptr. The result is a pointer to one past the end of the header. */
2723
2724static gdb_byte *
2725read_type_comp_unit_head (struct comp_unit_head *cu_header,
2726 ULONGEST *signature,
2727 gdb_byte *types_ptr, bfd *abfd)
2728{
348e048f
DE
2729 gdb_byte *initial_types_ptr = types_ptr;
2730
6e70227d 2731 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2732 &dwarf2_per_objfile->types);
348e048f
DE
2733 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2734
2735 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2736
2737 *signature = read_8_bytes (abfd, types_ptr);
2738 types_ptr += 8;
2739 types_ptr += cu_header->offset_size;
2740 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2741
2742 return types_ptr;
2743}
2744
aaa75496
JB
2745/* Allocate a new partial symtab for file named NAME and mark this new
2746 partial symtab as being an include of PST. */
2747
2748static void
2749dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2750 struct objfile *objfile)
2751{
2752 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2753
2754 subpst->section_offsets = pst->section_offsets;
2755 subpst->textlow = 0;
2756 subpst->texthigh = 0;
2757
2758 subpst->dependencies = (struct partial_symtab **)
2759 obstack_alloc (&objfile->objfile_obstack,
2760 sizeof (struct partial_symtab *));
2761 subpst->dependencies[0] = pst;
2762 subpst->number_of_dependencies = 1;
2763
2764 subpst->globals_offset = 0;
2765 subpst->n_global_syms = 0;
2766 subpst->statics_offset = 0;
2767 subpst->n_static_syms = 0;
2768 subpst->symtab = NULL;
2769 subpst->read_symtab = pst->read_symtab;
2770 subpst->readin = 0;
2771
2772 /* No private part is necessary for include psymtabs. This property
2773 can be used to differentiate between such include psymtabs and
10b3939b 2774 the regular ones. */
58a9656e 2775 subpst->read_symtab_private = NULL;
aaa75496
JB
2776}
2777
2778/* Read the Line Number Program data and extract the list of files
2779 included by the source file represented by PST. Build an include
d85a05f0 2780 partial symtab for each of these included files. */
aaa75496
JB
2781
2782static void
2783dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2784 struct die_info *die,
aaa75496
JB
2785 struct partial_symtab *pst)
2786{
2787 struct objfile *objfile = cu->objfile;
2788 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2789 struct line_header *lh = NULL;
2790 struct attribute *attr;
aaa75496 2791
d85a05f0
DJ
2792 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2793 if (attr)
2794 {
2795 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2796
d85a05f0
DJ
2797 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2798 }
aaa75496
JB
2799 if (lh == NULL)
2800 return; /* No linetable, so no includes. */
2801
c6da4cef
DE
2802 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2803 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2804
2805 free_line_header (lh);
2806}
2807
348e048f
DE
2808static hashval_t
2809hash_type_signature (const void *item)
2810{
2811 const struct signatured_type *type_sig = item;
9a619af0 2812
348e048f
DE
2813 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2814 return type_sig->signature;
2815}
2816
2817static int
2818eq_type_signature (const void *item_lhs, const void *item_rhs)
2819{
2820 const struct signatured_type *lhs = item_lhs;
2821 const struct signatured_type *rhs = item_rhs;
9a619af0 2822
348e048f
DE
2823 return lhs->signature == rhs->signature;
2824}
2825
1fd400ff
TT
2826/* Allocate a hash table for signatured types. */
2827
2828static htab_t
673bfd45 2829allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2830{
2831 return htab_create_alloc_ex (41,
2832 hash_type_signature,
2833 eq_type_signature,
2834 NULL,
2835 &objfile->objfile_obstack,
2836 hashtab_obstack_allocate,
2837 dummy_obstack_deallocate);
2838}
2839
2840/* A helper function to add a signatured type CU to a list. */
2841
2842static int
2843add_signatured_type_cu_to_list (void **slot, void *datum)
2844{
2845 struct signatured_type *sigt = *slot;
2846 struct dwarf2_per_cu_data ***datap = datum;
2847
2848 **datap = &sigt->per_cu;
2849 ++*datap;
2850
2851 return 1;
2852}
2853
348e048f
DE
2854/* Create the hash table of all entries in the .debug_types section.
2855 The result is zero if there is an error (e.g. missing .debug_types section),
2856 otherwise non-zero. */
2857
2858static int
2859create_debug_types_hash_table (struct objfile *objfile)
2860{
be391dca 2861 gdb_byte *info_ptr;
348e048f 2862 htab_t types_htab;
1fd400ff 2863 struct dwarf2_per_cu_data **iter;
348e048f 2864
be391dca
TT
2865 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2866 info_ptr = dwarf2_per_objfile->types.buffer;
2867
348e048f
DE
2868 if (info_ptr == NULL)
2869 {
2870 dwarf2_per_objfile->signatured_types = NULL;
2871 return 0;
2872 }
2873
673bfd45 2874 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2875
2876 if (dwarf2_die_debug)
2877 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2878
2879 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2880 {
2881 unsigned int offset;
2882 unsigned int offset_size;
2883 unsigned int type_offset;
2884 unsigned int length, initial_length_size;
2885 unsigned short version;
2886 ULONGEST signature;
2887 struct signatured_type *type_sig;
2888 void **slot;
2889 gdb_byte *ptr = info_ptr;
2890
2891 offset = ptr - dwarf2_per_objfile->types.buffer;
2892
2893 /* We need to read the type's signature in order to build the hash
2894 table, but we don't need to read anything else just yet. */
2895
2896 /* Sanity check to ensure entire cu is present. */
2897 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2898 if (ptr + length + initial_length_size
2899 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2900 {
2901 complaint (&symfile_complaints,
2902 _("debug type entry runs off end of `.debug_types' section, ignored"));
2903 break;
2904 }
2905
2906 offset_size = initial_length_size == 4 ? 4 : 8;
2907 ptr += initial_length_size;
2908 version = bfd_get_16 (objfile->obfd, ptr);
2909 ptr += 2;
2910 ptr += offset_size; /* abbrev offset */
2911 ptr += 1; /* address size */
2912 signature = bfd_get_64 (objfile->obfd, ptr);
2913 ptr += 8;
2914 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2915
2916 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2917 memset (type_sig, 0, sizeof (*type_sig));
2918 type_sig->signature = signature;
2919 type_sig->offset = offset;
2920 type_sig->type_offset = type_offset;
ca1f3406 2921 type_sig->per_cu.objfile = objfile;
1fd400ff 2922 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2923
2924 slot = htab_find_slot (types_htab, type_sig, INSERT);
2925 gdb_assert (slot != NULL);
2926 *slot = type_sig;
2927
2928 if (dwarf2_die_debug)
2929 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2930 offset, phex (signature, sizeof (signature)));
2931
2932 info_ptr = info_ptr + initial_length_size + length;
2933 }
2934
2935 dwarf2_per_objfile->signatured_types = types_htab;
2936
1fd400ff
TT
2937 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2938 dwarf2_per_objfile->type_comp_units
2939 = obstack_alloc (&objfile->objfile_obstack,
2940 dwarf2_per_objfile->n_type_comp_units
2941 * sizeof (struct dwarf2_per_cu_data *));
2942 iter = &dwarf2_per_objfile->type_comp_units[0];
2943 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2944 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2945 == dwarf2_per_objfile->n_type_comp_units);
2946
348e048f
DE
2947 return 1;
2948}
2949
2950/* Lookup a signature based type.
2951 Returns NULL if SIG is not present in the table. */
2952
2953static struct signatured_type *
2954lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2955{
2956 struct signatured_type find_entry, *entry;
2957
2958 if (dwarf2_per_objfile->signatured_types == NULL)
2959 {
2960 complaint (&symfile_complaints,
2961 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2962 return 0;
2963 }
2964
2965 find_entry.signature = sig;
2966 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2967 return entry;
2968}
2969
d85a05f0
DJ
2970/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2971
2972static void
2973init_cu_die_reader (struct die_reader_specs *reader,
2974 struct dwarf2_cu *cu)
2975{
2976 reader->abfd = cu->objfile->obfd;
2977 reader->cu = cu;
2978 if (cu->per_cu->from_debug_types)
be391dca
TT
2979 {
2980 gdb_assert (dwarf2_per_objfile->types.readin);
2981 reader->buffer = dwarf2_per_objfile->types.buffer;
2982 }
d85a05f0 2983 else
be391dca
TT
2984 {
2985 gdb_assert (dwarf2_per_objfile->info.readin);
2986 reader->buffer = dwarf2_per_objfile->info.buffer;
2987 }
d85a05f0
DJ
2988}
2989
2990/* Find the base address of the compilation unit for range lists and
2991 location lists. It will normally be specified by DW_AT_low_pc.
2992 In DWARF-3 draft 4, the base address could be overridden by
2993 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2994 compilation units with discontinuous ranges. */
2995
2996static void
2997dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2998{
2999 struct attribute *attr;
3000
3001 cu->base_known = 0;
3002 cu->base_address = 0;
3003
3004 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3005 if (attr)
3006 {
3007 cu->base_address = DW_ADDR (attr);
3008 cu->base_known = 1;
3009 }
3010 else
3011 {
3012 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3013 if (attr)
3014 {
3015 cu->base_address = DW_ADDR (attr);
3016 cu->base_known = 1;
3017 }
3018 }
3019}
3020
348e048f
DE
3021/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3022 to combine the common parts.
93311388 3023 Process a compilation unit for a psymtab.
348e048f
DE
3024 BUFFER is a pointer to the beginning of the dwarf section buffer,
3025 either .debug_info or debug_types.
93311388
DE
3026 INFO_PTR is a pointer to the start of the CU.
3027 Returns a pointer to the next CU. */
aaa75496 3028
93311388
DE
3029static gdb_byte *
3030process_psymtab_comp_unit (struct objfile *objfile,
3031 struct dwarf2_per_cu_data *this_cu,
3032 gdb_byte *buffer, gdb_byte *info_ptr,
3033 unsigned int buffer_size)
c906108c 3034{
c906108c 3035 bfd *abfd = objfile->obfd;
93311388 3036 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3037 struct die_info *comp_unit_die;
c906108c 3038 struct partial_symtab *pst;
5734ee8b 3039 CORE_ADDR baseaddr;
93311388
DE
3040 struct cleanup *back_to_inner;
3041 struct dwarf2_cu cu;
d85a05f0
DJ
3042 int has_children, has_pc_info;
3043 struct attribute *attr;
d85a05f0
DJ
3044 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3045 struct die_reader_specs reader_specs;
c906108c 3046
93311388
DE
3047 memset (&cu, 0, sizeof (cu));
3048 cu.objfile = objfile;
3049 obstack_init (&cu.comp_unit_obstack);
c906108c 3050
93311388 3051 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3052
93311388
DE
3053 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3054 buffer, buffer_size,
3055 abfd);
10b3939b 3056
93311388
DE
3057 /* Complete the cu_header. */
3058 cu.header.offset = beg_of_comp_unit - buffer;
3059 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3060
93311388 3061 cu.list_in_scope = &file_symbols;
af703f96 3062
328c9494
DJ
3063 /* If this compilation unit was already read in, free the
3064 cached copy in order to read it in again. This is
3065 necessary because we skipped some symbols when we first
3066 read in the compilation unit (see load_partial_dies).
3067 This problem could be avoided, but the benefit is
3068 unclear. */
3069 if (this_cu->cu != NULL)
3070 free_one_cached_comp_unit (this_cu->cu);
3071
3072 /* Note that this is a pointer to our stack frame, being
3073 added to a global data structure. It will be cleaned up
3074 in free_stack_comp_unit when we finish with this
3075 compilation unit. */
3076 this_cu->cu = &cu;
d85a05f0
DJ
3077 cu.per_cu = this_cu;
3078
93311388
DE
3079 /* Read the abbrevs for this compilation unit into a table. */
3080 dwarf2_read_abbrevs (abfd, &cu);
3081 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3082
93311388 3083 /* Read the compilation unit die. */
348e048f
DE
3084 if (this_cu->from_debug_types)
3085 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3086 init_cu_die_reader (&reader_specs, &cu);
3087 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3088 &has_children);
93311388 3089
348e048f
DE
3090 if (this_cu->from_debug_types)
3091 {
3092 /* offset,length haven't been set yet for type units. */
3093 this_cu->offset = cu.header.offset;
3094 this_cu->length = cu.header.length + cu.header.initial_length_size;
3095 }
d85a05f0 3096 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3097 {
93311388
DE
3098 info_ptr = (beg_of_comp_unit + cu.header.length
3099 + cu.header.initial_length_size);
3100 do_cleanups (back_to_inner);
3101 return info_ptr;
3102 }
72bf9492 3103
93311388 3104 /* Set the language we're debugging. */
d85a05f0
DJ
3105 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3106 if (attr)
3107 set_cu_language (DW_UNSND (attr), &cu);
3108 else
3109 set_cu_language (language_minimal, &cu);
c906108c 3110
93311388 3111 /* Allocate a new partial symbol table structure. */
d85a05f0 3112 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3113 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3114 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3115 /* TEXTLOW and TEXTHIGH are set below. */
3116 0,
3117 objfile->global_psymbols.next,
3118 objfile->static_psymbols.next);
72bf9492 3119
d85a05f0
DJ
3120 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3121 if (attr != NULL)
3122 pst->dirname = DW_STRING (attr);
72bf9492 3123
e38df1d0 3124 pst->read_symtab_private = this_cu;
72bf9492 3125
93311388 3126 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3127
93311388
DE
3128 /* Store the function that reads in the rest of the symbol table */
3129 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3130
9291a0cd 3131 this_cu->v.psymtab = pst;
c906108c 3132
d85a05f0
DJ
3133 dwarf2_find_base_address (comp_unit_die, &cu);
3134
93311388
DE
3135 /* Possibly set the default values of LOWPC and HIGHPC from
3136 `DW_AT_ranges'. */
d85a05f0
DJ
3137 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3138 &best_highpc, &cu, pst);
3139 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3140 /* Store the contiguous range if it is not empty; it can be empty for
3141 CUs with no code. */
3142 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3143 best_lowpc + baseaddr,
3144 best_highpc + baseaddr - 1, pst);
93311388
DE
3145
3146 /* Check if comp unit has_children.
3147 If so, read the rest of the partial symbols from this comp unit.
3148 If not, there's no more debug_info for this comp unit. */
d85a05f0 3149 if (has_children)
93311388
DE
3150 {
3151 struct partial_die_info *first_die;
3152 CORE_ADDR lowpc, highpc;
31ffec48 3153
93311388
DE
3154 lowpc = ((CORE_ADDR) -1);
3155 highpc = ((CORE_ADDR) 0);
c906108c 3156
93311388 3157 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3158
93311388 3159 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3160 ! has_pc_info, &cu);
57c22c6c 3161
93311388
DE
3162 /* If we didn't find a lowpc, set it to highpc to avoid
3163 complaints from `maint check'. */
3164 if (lowpc == ((CORE_ADDR) -1))
3165 lowpc = highpc;
10b3939b 3166
93311388
DE
3167 /* If the compilation unit didn't have an explicit address range,
3168 then use the information extracted from its child dies. */
d85a05f0 3169 if (! has_pc_info)
93311388 3170 {
d85a05f0
DJ
3171 best_lowpc = lowpc;
3172 best_highpc = highpc;
93311388
DE
3173 }
3174 }
d85a05f0
DJ
3175 pst->textlow = best_lowpc + baseaddr;
3176 pst->texthigh = best_highpc + baseaddr;
c906108c 3177
93311388
DE
3178 pst->n_global_syms = objfile->global_psymbols.next -
3179 (objfile->global_psymbols.list + pst->globals_offset);
3180 pst->n_static_syms = objfile->static_psymbols.next -
3181 (objfile->static_psymbols.list + pst->statics_offset);
3182 sort_pst_symbols (pst);
c906108c 3183
93311388
DE
3184 info_ptr = (beg_of_comp_unit + cu.header.length
3185 + cu.header.initial_length_size);
ae038cb0 3186
348e048f
DE
3187 if (this_cu->from_debug_types)
3188 {
3189 /* It's not clear we want to do anything with stmt lists here.
3190 Waiting to see what gcc ultimately does. */
3191 }
d85a05f0 3192 else
93311388
DE
3193 {
3194 /* Get the list of files included in the current compilation unit,
3195 and build a psymtab for each of them. */
d85a05f0 3196 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3197 }
ae038cb0 3198
93311388 3199 do_cleanups (back_to_inner);
ae038cb0 3200
93311388
DE
3201 return info_ptr;
3202}
ff013f42 3203
348e048f
DE
3204/* Traversal function for htab_traverse_noresize.
3205 Process one .debug_types comp-unit. */
3206
3207static int
3208process_type_comp_unit (void **slot, void *info)
3209{
3210 struct signatured_type *entry = (struct signatured_type *) *slot;
3211 struct objfile *objfile = (struct objfile *) info;
3212 struct dwarf2_per_cu_data *this_cu;
3213
3214 this_cu = &entry->per_cu;
348e048f 3215
be391dca 3216 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3217 process_psymtab_comp_unit (objfile, this_cu,
3218 dwarf2_per_objfile->types.buffer,
3219 dwarf2_per_objfile->types.buffer + entry->offset,
3220 dwarf2_per_objfile->types.size);
3221
3222 return 1;
3223}
3224
3225/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3226 Build partial symbol tables for the .debug_types comp-units. */
3227
3228static void
3229build_type_psymtabs (struct objfile *objfile)
3230{
3231 if (! create_debug_types_hash_table (objfile))
3232 return;
3233
3234 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3235 process_type_comp_unit, objfile);
3236}
3237
60606b2c
TT
3238/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3239
3240static void
3241psymtabs_addrmap_cleanup (void *o)
3242{
3243 struct objfile *objfile = o;
ec61707d 3244
60606b2c
TT
3245 objfile->psymtabs_addrmap = NULL;
3246}
3247
93311388
DE
3248/* Build the partial symbol table by doing a quick pass through the
3249 .debug_info and .debug_abbrev sections. */
72bf9492 3250
93311388 3251static void
c67a9c90 3252dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3253{
93311388 3254 gdb_byte *info_ptr;
60606b2c
TT
3255 struct cleanup *back_to, *addrmap_cleanup;
3256 struct obstack temp_obstack;
93311388 3257
98bfdba5
PA
3258 dwarf2_per_objfile->reading_partial_symbols = 1;
3259
be391dca 3260 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3261 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3262
93311388
DE
3263 /* Any cached compilation units will be linked by the per-objfile
3264 read_in_chain. Make sure to free them when we're done. */
3265 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3266
348e048f
DE
3267 build_type_psymtabs (objfile);
3268
93311388 3269 create_all_comp_units (objfile);
c906108c 3270
60606b2c
TT
3271 /* Create a temporary address map on a temporary obstack. We later
3272 copy this to the final obstack. */
3273 obstack_init (&temp_obstack);
3274 make_cleanup_obstack_free (&temp_obstack);
3275 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3276 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3277
93311388
DE
3278 /* Since the objects we're extracting from .debug_info vary in
3279 length, only the individual functions to extract them (like
3280 read_comp_unit_head and load_partial_die) can really know whether
3281 the buffer is large enough to hold another complete object.
c906108c 3282
93311388
DE
3283 At the moment, they don't actually check that. If .debug_info
3284 holds just one extra byte after the last compilation unit's dies,
3285 then read_comp_unit_head will happily read off the end of the
3286 buffer. read_partial_die is similarly casual. Those functions
3287 should be fixed.
c906108c 3288
93311388
DE
3289 For this loop condition, simply checking whether there's any data
3290 left at all should be sufficient. */
c906108c 3291
93311388
DE
3292 while (info_ptr < (dwarf2_per_objfile->info.buffer
3293 + dwarf2_per_objfile->info.size))
3294 {
3295 struct dwarf2_per_cu_data *this_cu;
dd373385 3296
93311388
DE
3297 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3298 objfile);
aaa75496 3299
93311388
DE
3300 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3301 dwarf2_per_objfile->info.buffer,
3302 info_ptr,
3303 dwarf2_per_objfile->info.size);
c906108c 3304 }
ff013f42
JK
3305
3306 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3307 &objfile->objfile_obstack);
60606b2c 3308 discard_cleanups (addrmap_cleanup);
ff013f42 3309
ae038cb0
DJ
3310 do_cleanups (back_to);
3311}
3312
93311388 3313/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3314
3315static void
93311388
DE
3316load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3317 struct objfile *objfile)
ae038cb0
DJ
3318{
3319 bfd *abfd = objfile->obfd;
fe1b8b76 3320 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3321 struct die_info *comp_unit_die;
ae038cb0 3322 struct dwarf2_cu *cu;
1d9ec526 3323 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3324 struct attribute *attr;
3325 int has_children;
3326 struct die_reader_specs reader_specs;
98bfdba5 3327 int read_cu = 0;
ae038cb0 3328
348e048f
DE
3329 gdb_assert (! this_cu->from_debug_types);
3330
be391dca 3331 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3332 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3333 beg_of_comp_unit = info_ptr;
3334
98bfdba5
PA
3335 if (this_cu->cu == NULL)
3336 {
3337 cu = alloc_one_comp_unit (objfile);
ae038cb0 3338
98bfdba5 3339 read_cu = 1;
ae038cb0 3340
98bfdba5
PA
3341 /* If an error occurs while loading, release our storage. */
3342 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3343
98bfdba5
PA
3344 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3345 dwarf2_per_objfile->info.buffer,
3346 dwarf2_per_objfile->info.size,
3347 abfd);
ae038cb0 3348
98bfdba5
PA
3349 /* Complete the cu_header. */
3350 cu->header.offset = this_cu->offset;
3351 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3352
3353 /* Link this compilation unit into the compilation unit tree. */
3354 this_cu->cu = cu;
3355 cu->per_cu = this_cu;
98bfdba5
PA
3356
3357 /* Link this CU into read_in_chain. */
3358 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3359 dwarf2_per_objfile->read_in_chain = this_cu;
3360 }
3361 else
3362 {
3363 cu = this_cu->cu;
3364 info_ptr += cu->header.first_die_offset;
3365 }
ae038cb0
DJ
3366
3367 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3368 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3369 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3370 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3371
3372 /* Read the compilation unit die. */
d85a05f0
DJ
3373 init_cu_die_reader (&reader_specs, cu);
3374 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3375 &has_children);
ae038cb0
DJ
3376
3377 /* Set the language we're debugging. */
d85a05f0
DJ
3378 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3379 if (attr)
3380 set_cu_language (DW_UNSND (attr), cu);
3381 else
3382 set_cu_language (language_minimal, cu);
ae038cb0 3383
ae038cb0
DJ
3384 /* Check if comp unit has_children.
3385 If so, read the rest of the partial symbols from this comp unit.
3386 If not, there's no more debug_info for this comp unit. */
d85a05f0 3387 if (has_children)
93311388 3388 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3389
98bfdba5
PA
3390 do_cleanups (free_abbrevs_cleanup);
3391
3392 if (read_cu)
3393 {
3394 /* We've successfully allocated this compilation unit. Let our
3395 caller clean it up when finished with it. */
3396 discard_cleanups (free_cu_cleanup);
3397 }
ae038cb0
DJ
3398}
3399
3400/* Create a list of all compilation units in OBJFILE. We do this only
3401 if an inter-comp-unit reference is found; presumably if there is one,
3402 there will be many, and one will occur early in the .debug_info section.
3403 So there's no point in building this list incrementally. */
3404
3405static void
3406create_all_comp_units (struct objfile *objfile)
3407{
3408 int n_allocated;
3409 int n_comp_units;
3410 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3411 gdb_byte *info_ptr;
3412
3413 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3414 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3415
3416 n_comp_units = 0;
3417 n_allocated = 10;
3418 all_comp_units = xmalloc (n_allocated
3419 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3420
dce234bc 3421 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3422 {
c764a876 3423 unsigned int length, initial_length_size;
ae038cb0 3424 struct dwarf2_per_cu_data *this_cu;
c764a876 3425 unsigned int offset;
ae038cb0 3426
dce234bc 3427 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3428
3429 /* Read just enough information to find out where the next
3430 compilation unit is. */
c764a876
DE
3431 length = read_initial_length (objfile->obfd, info_ptr,
3432 &initial_length_size);
ae038cb0
DJ
3433
3434 /* Save the compilation unit for later lookup. */
3435 this_cu = obstack_alloc (&objfile->objfile_obstack,
3436 sizeof (struct dwarf2_per_cu_data));
3437 memset (this_cu, 0, sizeof (*this_cu));
3438 this_cu->offset = offset;
c764a876 3439 this_cu->length = length + initial_length_size;
9291a0cd 3440 this_cu->objfile = objfile;
ae038cb0
DJ
3441
3442 if (n_comp_units == n_allocated)
3443 {
3444 n_allocated *= 2;
3445 all_comp_units = xrealloc (all_comp_units,
3446 n_allocated
3447 * sizeof (struct dwarf2_per_cu_data *));
3448 }
3449 all_comp_units[n_comp_units++] = this_cu;
3450
3451 info_ptr = info_ptr + this_cu->length;
3452 }
3453
3454 dwarf2_per_objfile->all_comp_units
3455 = obstack_alloc (&objfile->objfile_obstack,
3456 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3457 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3458 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3459 xfree (all_comp_units);
3460 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3461}
3462
5734ee8b
DJ
3463/* Process all loaded DIEs for compilation unit CU, starting at
3464 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3465 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3466 DW_AT_ranges). If NEED_PC is set, then this function will set
3467 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3468 and record the covered ranges in the addrmap. */
c906108c 3469
72bf9492
DJ
3470static void
3471scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3472 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3473{
72bf9492 3474 struct partial_die_info *pdi;
c906108c 3475
91c24f0a
DC
3476 /* Now, march along the PDI's, descending into ones which have
3477 interesting children but skipping the children of the other ones,
3478 until we reach the end of the compilation unit. */
c906108c 3479
72bf9492 3480 pdi = first_die;
91c24f0a 3481
72bf9492
DJ
3482 while (pdi != NULL)
3483 {
3484 fixup_partial_die (pdi, cu);
c906108c 3485
f55ee35c 3486 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3487 children, so we need to look at them. Ditto for anonymous
3488 enums. */
933c6fe4 3489
72bf9492 3490 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3491 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3492 {
72bf9492 3493 switch (pdi->tag)
c906108c
SS
3494 {
3495 case DW_TAG_subprogram:
5734ee8b 3496 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3497 break;
3498 case DW_TAG_variable:
3499 case DW_TAG_typedef:
91c24f0a 3500 case DW_TAG_union_type:
72bf9492 3501 if (!pdi->is_declaration)
63d06c5c 3502 {
72bf9492 3503 add_partial_symbol (pdi, cu);
63d06c5c
DC
3504 }
3505 break;
c906108c 3506 case DW_TAG_class_type:
680b30c7 3507 case DW_TAG_interface_type:
c906108c 3508 case DW_TAG_structure_type:
72bf9492 3509 if (!pdi->is_declaration)
c906108c 3510 {
72bf9492 3511 add_partial_symbol (pdi, cu);
c906108c
SS
3512 }
3513 break;
91c24f0a 3514 case DW_TAG_enumeration_type:
72bf9492
DJ
3515 if (!pdi->is_declaration)
3516 add_partial_enumeration (pdi, cu);
c906108c
SS
3517 break;
3518 case DW_TAG_base_type:
a02abb62 3519 case DW_TAG_subrange_type:
c906108c 3520 /* File scope base type definitions are added to the partial
c5aa993b 3521 symbol table. */
72bf9492 3522 add_partial_symbol (pdi, cu);
c906108c 3523 break;
d9fa45fe 3524 case DW_TAG_namespace:
5734ee8b 3525 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3526 break;
5d7cb8df
JK
3527 case DW_TAG_module:
3528 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3529 break;
c906108c
SS
3530 default:
3531 break;
3532 }
3533 }
3534
72bf9492
DJ
3535 /* If the die has a sibling, skip to the sibling. */
3536
3537 pdi = pdi->die_sibling;
3538 }
3539}
3540
3541/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3542
72bf9492 3543 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3544 name is concatenated with "::" and the partial DIE's name. For
3545 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3546 Enumerators are an exception; they use the scope of their parent
3547 enumeration type, i.e. the name of the enumeration type is not
3548 prepended to the enumerator.
91c24f0a 3549
72bf9492
DJ
3550 There are two complexities. One is DW_AT_specification; in this
3551 case "parent" means the parent of the target of the specification,
3552 instead of the direct parent of the DIE. The other is compilers
3553 which do not emit DW_TAG_namespace; in this case we try to guess
3554 the fully qualified name of structure types from their members'
3555 linkage names. This must be done using the DIE's children rather
3556 than the children of any DW_AT_specification target. We only need
3557 to do this for structures at the top level, i.e. if the target of
3558 any DW_AT_specification (if any; otherwise the DIE itself) does not
3559 have a parent. */
3560
3561/* Compute the scope prefix associated with PDI's parent, in
3562 compilation unit CU. The result will be allocated on CU's
3563 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3564 field. NULL is returned if no prefix is necessary. */
3565static char *
3566partial_die_parent_scope (struct partial_die_info *pdi,
3567 struct dwarf2_cu *cu)
3568{
3569 char *grandparent_scope;
3570 struct partial_die_info *parent, *real_pdi;
91c24f0a 3571
72bf9492
DJ
3572 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3573 then this means the parent of the specification DIE. */
3574
3575 real_pdi = pdi;
72bf9492 3576 while (real_pdi->has_specification)
10b3939b 3577 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3578
3579 parent = real_pdi->die_parent;
3580 if (parent == NULL)
3581 return NULL;
3582
3583 if (parent->scope_set)
3584 return parent->scope;
3585
3586 fixup_partial_die (parent, cu);
3587
10b3939b 3588 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3589
acebe513
UW
3590 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3591 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3592 Work around this problem here. */
3593 if (cu->language == language_cplus
6e70227d 3594 && parent->tag == DW_TAG_namespace
acebe513
UW
3595 && strcmp (parent->name, "::") == 0
3596 && grandparent_scope == NULL)
3597 {
3598 parent->scope = NULL;
3599 parent->scope_set = 1;
3600 return NULL;
3601 }
3602
72bf9492 3603 if (parent->tag == DW_TAG_namespace
f55ee35c 3604 || parent->tag == DW_TAG_module
72bf9492
DJ
3605 || parent->tag == DW_TAG_structure_type
3606 || parent->tag == DW_TAG_class_type
680b30c7 3607 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3608 || parent->tag == DW_TAG_union_type
3609 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3610 {
3611 if (grandparent_scope == NULL)
3612 parent->scope = parent->name;
3613 else
987504bb 3614 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3615 parent->name, 0, cu);
72bf9492 3616 }
ceeb3d5a 3617 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3618 /* Enumerators should not get the name of the enumeration as a prefix. */
3619 parent->scope = grandparent_scope;
3620 else
3621 {
3622 /* FIXME drow/2004-04-01: What should we be doing with
3623 function-local names? For partial symbols, we should probably be
3624 ignoring them. */
3625 complaint (&symfile_complaints,
e2e0b3e5 3626 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3627 parent->tag, pdi->offset);
3628 parent->scope = grandparent_scope;
c906108c
SS
3629 }
3630
72bf9492
DJ
3631 parent->scope_set = 1;
3632 return parent->scope;
3633}
3634
3635/* Return the fully scoped name associated with PDI, from compilation unit
3636 CU. The result will be allocated with malloc. */
3637static char *
3638partial_die_full_name (struct partial_die_info *pdi,
3639 struct dwarf2_cu *cu)
3640{
3641 char *parent_scope;
3642
98bfdba5
PA
3643 /* If this is a template instantiation, we can not work out the
3644 template arguments from partial DIEs. So, unfortunately, we have
3645 to go through the full DIEs. At least any work we do building
3646 types here will be reused if full symbols are loaded later. */
3647 if (pdi->has_template_arguments)
3648 {
3649 fixup_partial_die (pdi, cu);
3650
3651 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3652 {
3653 struct die_info *die;
3654 struct attribute attr;
3655 struct dwarf2_cu *ref_cu = cu;
3656
3657 attr.name = 0;
3658 attr.form = DW_FORM_ref_addr;
3659 attr.u.addr = pdi->offset;
3660 die = follow_die_ref (NULL, &attr, &ref_cu);
3661
3662 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3663 }
3664 }
3665
72bf9492
DJ
3666 parent_scope = partial_die_parent_scope (pdi, cu);
3667 if (parent_scope == NULL)
3668 return NULL;
3669 else
f55ee35c 3670 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3671}
3672
3673static void
72bf9492 3674add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3675{
e7c27a73 3676 struct objfile *objfile = cu->objfile;
c906108c 3677 CORE_ADDR addr = 0;
decbce07 3678 char *actual_name = NULL;
5c4e30ca 3679 const struct partial_symbol *psym = NULL;
e142c38c 3680 CORE_ADDR baseaddr;
72bf9492 3681 int built_actual_name = 0;
e142c38c
DJ
3682
3683 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3684
94af9270
KS
3685 actual_name = partial_die_full_name (pdi, cu);
3686 if (actual_name)
3687 built_actual_name = 1;
63d06c5c 3688
72bf9492
DJ
3689 if (actual_name == NULL)
3690 actual_name = pdi->name;
3691
c906108c
SS
3692 switch (pdi->tag)
3693 {
3694 case DW_TAG_subprogram:
2cfa0c8d 3695 if (pdi->is_external || cu->language == language_ada)
c906108c 3696 {
2cfa0c8d
JB
3697 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3698 of the global scope. But in Ada, we want to be able to access
3699 nested procedures globally. So all Ada subprograms are stored
3700 in the global scope. */
38d518c9 3701 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3702 mst_text, objfile); */
38d518c9 3703 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3704 built_actual_name,
5c4e30ca
DC
3705 VAR_DOMAIN, LOC_BLOCK,
3706 &objfile->global_psymbols,
3707 0, pdi->lowpc + baseaddr,
e142c38c 3708 cu->language, objfile);
c906108c
SS
3709 }
3710 else
3711 {
38d518c9 3712 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3713 mst_file_text, objfile); */
38d518c9 3714 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3715 built_actual_name,
5c4e30ca
DC
3716 VAR_DOMAIN, LOC_BLOCK,
3717 &objfile->static_psymbols,
3718 0, pdi->lowpc + baseaddr,
e142c38c 3719 cu->language, objfile);
c906108c
SS
3720 }
3721 break;
3722 case DW_TAG_variable:
caac4577
JG
3723 if (pdi->locdesc)
3724 addr = decode_locdesc (pdi->locdesc, cu);
3725
3726 if (pdi->locdesc
3727 && addr == 0
3728 && !dwarf2_per_objfile->has_section_at_zero)
3729 {
3730 /* A global or static variable may also have been stripped
3731 out by the linker if unused, in which case its address
3732 will be nullified; do not add such variables into partial
3733 symbol table then. */
3734 }
3735 else if (pdi->is_external)
c906108c
SS
3736 {
3737 /* Global Variable.
3738 Don't enter into the minimal symbol tables as there is
3739 a minimal symbol table entry from the ELF symbols already.
3740 Enter into partial symbol table if it has a location
3741 descriptor or a type.
3742 If the location descriptor is missing, new_symbol will create
3743 a LOC_UNRESOLVED symbol, the address of the variable will then
3744 be determined from the minimal symbol table whenever the variable
3745 is referenced.
3746 The address for the partial symbol table entry is not
3747 used by GDB, but it comes in handy for debugging partial symbol
3748 table building. */
3749
c906108c 3750 if (pdi->locdesc || pdi->has_type)
38d518c9 3751 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3752 built_actual_name,
5c4e30ca
DC
3753 VAR_DOMAIN, LOC_STATIC,
3754 &objfile->global_psymbols,
3755 0, addr + baseaddr,
e142c38c 3756 cu->language, objfile);
c906108c
SS
3757 }
3758 else
3759 {
3760 /* Static Variable. Skip symbols without location descriptors. */
3761 if (pdi->locdesc == NULL)
decbce07
MS
3762 {
3763 if (built_actual_name)
3764 xfree (actual_name);
3765 return;
3766 }
38d518c9 3767 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3768 mst_file_data, objfile); */
38d518c9 3769 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3770 built_actual_name,
5c4e30ca
DC
3771 VAR_DOMAIN, LOC_STATIC,
3772 &objfile->static_psymbols,
3773 0, addr + baseaddr,
e142c38c 3774 cu->language, objfile);
c906108c
SS
3775 }
3776 break;
3777 case DW_TAG_typedef:
3778 case DW_TAG_base_type:
a02abb62 3779 case DW_TAG_subrange_type:
38d518c9 3780 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3781 built_actual_name,
176620f1 3782 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3783 &objfile->static_psymbols,
e142c38c 3784 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3785 break;
72bf9492
DJ
3786 case DW_TAG_namespace:
3787 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3788 built_actual_name,
72bf9492
DJ
3789 VAR_DOMAIN, LOC_TYPEDEF,
3790 &objfile->global_psymbols,
3791 0, (CORE_ADDR) 0, cu->language, objfile);
3792 break;
c906108c 3793 case DW_TAG_class_type:
680b30c7 3794 case DW_TAG_interface_type:
c906108c
SS
3795 case DW_TAG_structure_type:
3796 case DW_TAG_union_type:
3797 case DW_TAG_enumeration_type:
fa4028e9
JB
3798 /* Skip external references. The DWARF standard says in the section
3799 about "Structure, Union, and Class Type Entries": "An incomplete
3800 structure, union or class type is represented by a structure,
3801 union or class entry that does not have a byte size attribute
3802 and that has a DW_AT_declaration attribute." */
3803 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3804 {
3805 if (built_actual_name)
3806 xfree (actual_name);
3807 return;
3808 }
fa4028e9 3809
63d06c5c
DC
3810 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3811 static vs. global. */
38d518c9 3812 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3813 built_actual_name,
176620f1 3814 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3815 (cu->language == language_cplus
3816 || cu->language == language_java)
63d06c5c
DC
3817 ? &objfile->global_psymbols
3818 : &objfile->static_psymbols,
e142c38c 3819 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3820
c906108c
SS
3821 break;
3822 case DW_TAG_enumerator:
38d518c9 3823 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3824 built_actual_name,
176620f1 3825 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3826 (cu->language == language_cplus
3827 || cu->language == language_java)
f6fe98ef
DJ
3828 ? &objfile->global_psymbols
3829 : &objfile->static_psymbols,
e142c38c 3830 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3831 break;
3832 default:
3833 break;
3834 }
5c4e30ca 3835
72bf9492
DJ
3836 if (built_actual_name)
3837 xfree (actual_name);
c906108c
SS
3838}
3839
5c4e30ca
DC
3840/* Read a partial die corresponding to a namespace; also, add a symbol
3841 corresponding to that namespace to the symbol table. NAMESPACE is
3842 the name of the enclosing namespace. */
91c24f0a 3843
72bf9492
DJ
3844static void
3845add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3846 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3847 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3848{
72bf9492 3849 /* Add a symbol for the namespace. */
e7c27a73 3850
72bf9492 3851 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3852
3853 /* Now scan partial symbols in that namespace. */
3854
91c24f0a 3855 if (pdi->has_children)
5734ee8b 3856 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3857}
3858
5d7cb8df
JK
3859/* Read a partial die corresponding to a Fortran module. */
3860
3861static void
3862add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3863 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3864{
f55ee35c 3865 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3866
3867 if (pdi->has_children)
3868 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3869}
3870
bc30ff58
JB
3871/* Read a partial die corresponding to a subprogram and create a partial
3872 symbol for that subprogram. When the CU language allows it, this
3873 routine also defines a partial symbol for each nested subprogram
3874 that this subprogram contains.
6e70227d 3875
bc30ff58
JB
3876 DIE my also be a lexical block, in which case we simply search
3877 recursively for suprograms defined inside that lexical block.
3878 Again, this is only performed when the CU language allows this
3879 type of definitions. */
3880
3881static void
3882add_partial_subprogram (struct partial_die_info *pdi,
3883 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3884 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3885{
3886 if (pdi->tag == DW_TAG_subprogram)
3887 {
3888 if (pdi->has_pc_info)
3889 {
3890 if (pdi->lowpc < *lowpc)
3891 *lowpc = pdi->lowpc;
3892 if (pdi->highpc > *highpc)
3893 *highpc = pdi->highpc;
5734ee8b
DJ
3894 if (need_pc)
3895 {
3896 CORE_ADDR baseaddr;
3897 struct objfile *objfile = cu->objfile;
3898
3899 baseaddr = ANOFFSET (objfile->section_offsets,
3900 SECT_OFF_TEXT (objfile));
3901 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3902 pdi->lowpc + baseaddr,
3903 pdi->highpc - 1 + baseaddr,
9291a0cd 3904 cu->per_cu->v.psymtab);
5734ee8b 3905 }
bc30ff58 3906 if (!pdi->is_declaration)
e8d05480
JB
3907 /* Ignore subprogram DIEs that do not have a name, they are
3908 illegal. Do not emit a complaint at this point, we will
3909 do so when we convert this psymtab into a symtab. */
3910 if (pdi->name)
3911 add_partial_symbol (pdi, cu);
bc30ff58
JB
3912 }
3913 }
6e70227d 3914
bc30ff58
JB
3915 if (! pdi->has_children)
3916 return;
3917
3918 if (cu->language == language_ada)
3919 {
3920 pdi = pdi->die_child;
3921 while (pdi != NULL)
3922 {
3923 fixup_partial_die (pdi, cu);
3924 if (pdi->tag == DW_TAG_subprogram
3925 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3926 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3927 pdi = pdi->die_sibling;
3928 }
3929 }
3930}
3931
72bf9492
DJ
3932/* See if we can figure out if the class lives in a namespace. We do
3933 this by looking for a member function; its demangled name will
3934 contain namespace info, if there is any. */
63d06c5c 3935
72bf9492
DJ
3936static void
3937guess_structure_name (struct partial_die_info *struct_pdi,
3938 struct dwarf2_cu *cu)
63d06c5c 3939{
987504bb
JJ
3940 if ((cu->language == language_cplus
3941 || cu->language == language_java)
72bf9492 3942 && cu->has_namespace_info == 0
63d06c5c
DC
3943 && struct_pdi->has_children)
3944 {
63d06c5c
DC
3945 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3946 what template types look like, because the demangler
3947 frequently doesn't give the same name as the debug info. We
3948 could fix this by only using the demangled name to get the
134d01f1 3949 prefix (but see comment in read_structure_type). */
63d06c5c 3950
72bf9492 3951 struct partial_die_info *real_pdi;
5d51ca54 3952
72bf9492
DJ
3953 /* If this DIE (this DIE's specification, if any) has a parent, then
3954 we should not do this. We'll prepend the parent's fully qualified
3955 name when we create the partial symbol. */
5d51ca54 3956
72bf9492 3957 real_pdi = struct_pdi;
72bf9492 3958 while (real_pdi->has_specification)
10b3939b 3959 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3960
72bf9492
DJ
3961 if (real_pdi->die_parent != NULL)
3962 return;
63d06c5c 3963 }
63d06c5c
DC
3964}
3965
91c24f0a
DC
3966/* Read a partial die corresponding to an enumeration type. */
3967
72bf9492
DJ
3968static void
3969add_partial_enumeration (struct partial_die_info *enum_pdi,
3970 struct dwarf2_cu *cu)
91c24f0a 3971{
72bf9492 3972 struct partial_die_info *pdi;
91c24f0a
DC
3973
3974 if (enum_pdi->name != NULL)
72bf9492
DJ
3975 add_partial_symbol (enum_pdi, cu);
3976
3977 pdi = enum_pdi->die_child;
3978 while (pdi)
91c24f0a 3979 {
72bf9492 3980 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3981 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3982 else
72bf9492
DJ
3983 add_partial_symbol (pdi, cu);
3984 pdi = pdi->die_sibling;
91c24f0a 3985 }
91c24f0a
DC
3986}
3987
4bb7a0a7
DJ
3988/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3989 Return the corresponding abbrev, or NULL if the number is zero (indicating
3990 an empty DIE). In either case *BYTES_READ will be set to the length of
3991 the initial number. */
3992
3993static struct abbrev_info *
fe1b8b76 3994peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3995 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3996{
3997 bfd *abfd = cu->objfile->obfd;
3998 unsigned int abbrev_number;
3999 struct abbrev_info *abbrev;
4000
4001 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4002
4003 if (abbrev_number == 0)
4004 return NULL;
4005
4006 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4007 if (!abbrev)
4008 {
8a3fe4f8 4009 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
4010 bfd_get_filename (abfd));
4011 }
4012
4013 return abbrev;
4014}
4015
93311388
DE
4016/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4017 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4018 DIE. Any children of the skipped DIEs will also be skipped. */
4019
fe1b8b76 4020static gdb_byte *
93311388 4021skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4022{
4023 struct abbrev_info *abbrev;
4024 unsigned int bytes_read;
4025
4026 while (1)
4027 {
4028 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4029 if (abbrev == NULL)
4030 return info_ptr + bytes_read;
4031 else
93311388 4032 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4033 }
4034}
4035
93311388
DE
4036/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4037 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4038 abbrev corresponding to that skipped uleb128 should be passed in
4039 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4040 children. */
4041
fe1b8b76 4042static gdb_byte *
93311388
DE
4043skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4044 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4045{
4046 unsigned int bytes_read;
4047 struct attribute attr;
4048 bfd *abfd = cu->objfile->obfd;
4049 unsigned int form, i;
4050
4051 for (i = 0; i < abbrev->num_attrs; i++)
4052 {
4053 /* The only abbrev we care about is DW_AT_sibling. */
4054 if (abbrev->attrs[i].name == DW_AT_sibling)
4055 {
4056 read_attribute (&attr, &abbrev->attrs[i],
4057 abfd, info_ptr, cu);
4058 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4059 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4060 else
93311388 4061 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4062 }
4063
4064 /* If it isn't DW_AT_sibling, skip this attribute. */
4065 form = abbrev->attrs[i].form;
4066 skip_attribute:
4067 switch (form)
4068 {
4bb7a0a7 4069 case DW_FORM_ref_addr:
ae411497
TT
4070 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4071 and later it is offset sized. */
4072 if (cu->header.version == 2)
4073 info_ptr += cu->header.addr_size;
4074 else
4075 info_ptr += cu->header.offset_size;
4076 break;
4077 case DW_FORM_addr:
4bb7a0a7
DJ
4078 info_ptr += cu->header.addr_size;
4079 break;
4080 case DW_FORM_data1:
4081 case DW_FORM_ref1:
4082 case DW_FORM_flag:
4083 info_ptr += 1;
4084 break;
2dc7f7b3
TT
4085 case DW_FORM_flag_present:
4086 break;
4bb7a0a7
DJ
4087 case DW_FORM_data2:
4088 case DW_FORM_ref2:
4089 info_ptr += 2;
4090 break;
4091 case DW_FORM_data4:
4092 case DW_FORM_ref4:
4093 info_ptr += 4;
4094 break;
4095 case DW_FORM_data8:
4096 case DW_FORM_ref8:
348e048f 4097 case DW_FORM_sig8:
4bb7a0a7
DJ
4098 info_ptr += 8;
4099 break;
4100 case DW_FORM_string:
9b1c24c8 4101 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4102 info_ptr += bytes_read;
4103 break;
2dc7f7b3 4104 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4105 case DW_FORM_strp:
4106 info_ptr += cu->header.offset_size;
4107 break;
2dc7f7b3 4108 case DW_FORM_exprloc:
4bb7a0a7
DJ
4109 case DW_FORM_block:
4110 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4111 info_ptr += bytes_read;
4112 break;
4113 case DW_FORM_block1:
4114 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4115 break;
4116 case DW_FORM_block2:
4117 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4118 break;
4119 case DW_FORM_block4:
4120 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4121 break;
4122 case DW_FORM_sdata:
4123 case DW_FORM_udata:
4124 case DW_FORM_ref_udata:
4125 info_ptr = skip_leb128 (abfd, info_ptr);
4126 break;
4127 case DW_FORM_indirect:
4128 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4129 info_ptr += bytes_read;
4130 /* We need to continue parsing from here, so just go back to
4131 the top. */
4132 goto skip_attribute;
4133
4134 default:
8a3fe4f8 4135 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4136 dwarf_form_name (form),
4137 bfd_get_filename (abfd));
4138 }
4139 }
4140
4141 if (abbrev->has_children)
93311388 4142 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4143 else
4144 return info_ptr;
4145}
4146
93311388
DE
4147/* Locate ORIG_PDI's sibling.
4148 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4149 in BUFFER. */
91c24f0a 4150
fe1b8b76 4151static gdb_byte *
93311388
DE
4152locate_pdi_sibling (struct partial_die_info *orig_pdi,
4153 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4154 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4155{
4156 /* Do we know the sibling already? */
72bf9492 4157
91c24f0a
DC
4158 if (orig_pdi->sibling)
4159 return orig_pdi->sibling;
4160
4161 /* Are there any children to deal with? */
4162
4163 if (!orig_pdi->has_children)
4164 return info_ptr;
4165
4bb7a0a7 4166 /* Skip the children the long way. */
91c24f0a 4167
93311388 4168 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4169}
4170
c906108c
SS
4171/* Expand this partial symbol table into a full symbol table. */
4172
4173static void
fba45db2 4174dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4175{
c906108c
SS
4176 if (pst != NULL)
4177 {
4178 if (pst->readin)
4179 {
8a3fe4f8 4180 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4181 }
4182 else
4183 {
4184 if (info_verbose)
4185 {
a3f17187 4186 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4187 gdb_flush (gdb_stdout);
4188 }
4189
10b3939b
DJ
4190 /* Restore our global data. */
4191 dwarf2_per_objfile = objfile_data (pst->objfile,
4192 dwarf2_objfile_data_key);
4193
b2ab525c
KB
4194 /* If this psymtab is constructed from a debug-only objfile, the
4195 has_section_at_zero flag will not necessarily be correct. We
4196 can get the correct value for this flag by looking at the data
4197 associated with the (presumably stripped) associated objfile. */
4198 if (pst->objfile->separate_debug_objfile_backlink)
4199 {
4200 struct dwarf2_per_objfile *dpo_backlink
4201 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4202 dwarf2_objfile_data_key);
9a619af0 4203
b2ab525c
KB
4204 dwarf2_per_objfile->has_section_at_zero
4205 = dpo_backlink->has_section_at_zero;
4206 }
4207
98bfdba5
PA
4208 dwarf2_per_objfile->reading_partial_symbols = 0;
4209
c906108c
SS
4210 psymtab_to_symtab_1 (pst);
4211
4212 /* Finish up the debug error message. */
4213 if (info_verbose)
a3f17187 4214 printf_filtered (_("done.\n"));
c906108c
SS
4215 }
4216 }
4217}
4218
10b3939b
DJ
4219/* Add PER_CU to the queue. */
4220
4221static void
03dd20cc 4222queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4223{
4224 struct dwarf2_queue_item *item;
4225
4226 per_cu->queued = 1;
4227 item = xmalloc (sizeof (*item));
4228 item->per_cu = per_cu;
4229 item->next = NULL;
4230
4231 if (dwarf2_queue == NULL)
4232 dwarf2_queue = item;
4233 else
4234 dwarf2_queue_tail->next = item;
4235
4236 dwarf2_queue_tail = item;
4237}
4238
4239/* Process the queue. */
4240
4241static void
4242process_queue (struct objfile *objfile)
4243{
4244 struct dwarf2_queue_item *item, *next_item;
4245
03dd20cc
DJ
4246 /* The queue starts out with one item, but following a DIE reference
4247 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4248 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4249 {
9291a0cd
TT
4250 if (dwarf2_per_objfile->using_index
4251 ? !item->per_cu->v.quick->symtab
4252 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4253 process_full_comp_unit (item->per_cu);
4254
4255 item->per_cu->queued = 0;
4256 next_item = item->next;
4257 xfree (item);
4258 }
4259
4260 dwarf2_queue_tail = NULL;
4261}
4262
4263/* Free all allocated queue entries. This function only releases anything if
4264 an error was thrown; if the queue was processed then it would have been
4265 freed as we went along. */
4266
4267static void
4268dwarf2_release_queue (void *dummy)
4269{
4270 struct dwarf2_queue_item *item, *last;
4271
4272 item = dwarf2_queue;
4273 while (item)
4274 {
4275 /* Anything still marked queued is likely to be in an
4276 inconsistent state, so discard it. */
4277 if (item->per_cu->queued)
4278 {
4279 if (item->per_cu->cu != NULL)
4280 free_one_cached_comp_unit (item->per_cu->cu);
4281 item->per_cu->queued = 0;
4282 }
4283
4284 last = item;
4285 item = item->next;
4286 xfree (last);
4287 }
4288
4289 dwarf2_queue = dwarf2_queue_tail = NULL;
4290}
4291
4292/* Read in full symbols for PST, and anything it depends on. */
4293
c906108c 4294static void
fba45db2 4295psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4296{
10b3939b 4297 struct dwarf2_per_cu_data *per_cu;
c906108c 4298 struct cleanup *back_to;
aaa75496
JB
4299 int i;
4300
4301 for (i = 0; i < pst->number_of_dependencies; i++)
4302 if (!pst->dependencies[i]->readin)
4303 {
4304 /* Inform about additional files that need to be read in. */
4305 if (info_verbose)
4306 {
a3f17187 4307 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4308 fputs_filtered (" ", gdb_stdout);
4309 wrap_here ("");
4310 fputs_filtered ("and ", gdb_stdout);
4311 wrap_here ("");
4312 printf_filtered ("%s...", pst->dependencies[i]->filename);
4313 wrap_here (""); /* Flush output */
4314 gdb_flush (gdb_stdout);
4315 }
4316 psymtab_to_symtab_1 (pst->dependencies[i]);
4317 }
4318
e38df1d0 4319 per_cu = pst->read_symtab_private;
10b3939b
DJ
4320
4321 if (per_cu == NULL)
aaa75496
JB
4322 {
4323 /* It's an include file, no symbols to read for it.
4324 Everything is in the parent symtab. */
4325 pst->readin = 1;
4326 return;
4327 }
c906108c 4328
9291a0cd 4329 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4330}
4331
93311388 4332/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4333
93311388 4334static void
31ffec48 4335load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4336{
31ffec48 4337 bfd *abfd = objfile->obfd;
10b3939b 4338 struct dwarf2_cu *cu;
c764a876 4339 unsigned int offset;
93311388 4340 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4341 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4342 struct attribute *attr;
98bfdba5 4343 int read_cu = 0;
6502dd73 4344
348e048f
DE
4345 gdb_assert (! per_cu->from_debug_types);
4346
c906108c 4347 /* Set local variables from the partial symbol table info. */
10b3939b 4348 offset = per_cu->offset;
6502dd73 4349
be391dca 4350 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4351 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4352 beg_of_comp_unit = info_ptr;
63d06c5c 4353
98bfdba5
PA
4354 if (per_cu->cu == NULL)
4355 {
4356 cu = alloc_one_comp_unit (objfile);
4357
4358 read_cu = 1;
c906108c 4359
98bfdba5
PA
4360 /* If an error occurs while loading, release our storage. */
4361 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4362
98bfdba5
PA
4363 /* Read in the comp_unit header. */
4364 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4365
98bfdba5
PA
4366 /* Complete the cu_header. */
4367 cu->header.offset = offset;
4368 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4369
98bfdba5
PA
4370 /* Read the abbrevs for this compilation unit. */
4371 dwarf2_read_abbrevs (abfd, cu);
4372 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4373
98bfdba5
PA
4374 /* Link this compilation unit into the compilation unit tree. */
4375 per_cu->cu = cu;
4376 cu->per_cu = per_cu;
98bfdba5
PA
4377
4378 /* Link this CU into read_in_chain. */
4379 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4380 dwarf2_per_objfile->read_in_chain = per_cu;
4381 }
4382 else
4383 {
4384 cu = per_cu->cu;
4385 info_ptr += cu->header.first_die_offset;
4386 }
e142c38c 4387
93311388 4388 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4389
4390 /* We try not to read any attributes in this function, because not
4391 all objfiles needed for references have been loaded yet, and symbol
4392 table processing isn't initialized. But we have to set the CU language,
4393 or we won't be able to build types correctly. */
4394 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4395 if (attr)
4396 set_cu_language (DW_UNSND (attr), cu);
4397 else
4398 set_cu_language (language_minimal, cu);
4399
a6c727b2
DJ
4400 /* Similarly, if we do not read the producer, we can not apply
4401 producer-specific interpretation. */
4402 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4403 if (attr)
4404 cu->producer = DW_STRING (attr);
4405
98bfdba5
PA
4406 if (read_cu)
4407 {
4408 do_cleanups (free_abbrevs_cleanup);
e142c38c 4409
98bfdba5
PA
4410 /* We've successfully allocated this compilation unit. Let our
4411 caller clean it up when finished with it. */
4412 discard_cleanups (free_cu_cleanup);
4413 }
10b3939b
DJ
4414}
4415
3da10d80
KS
4416/* Add a DIE to the delayed physname list. */
4417
4418static void
4419add_to_method_list (struct type *type, int fnfield_index, int index,
4420 const char *name, struct die_info *die,
4421 struct dwarf2_cu *cu)
4422{
4423 struct delayed_method_info mi;
4424 mi.type = type;
4425 mi.fnfield_index = fnfield_index;
4426 mi.index = index;
4427 mi.name = name;
4428 mi.die = die;
4429 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4430}
4431
4432/* A cleanup for freeing the delayed method list. */
4433
4434static void
4435free_delayed_list (void *ptr)
4436{
4437 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4438 if (cu->method_list != NULL)
4439 {
4440 VEC_free (delayed_method_info, cu->method_list);
4441 cu->method_list = NULL;
4442 }
4443}
4444
4445/* Compute the physnames of any methods on the CU's method list.
4446
4447 The computation of method physnames is delayed in order to avoid the
4448 (bad) condition that one of the method's formal parameters is of an as yet
4449 incomplete type. */
4450
4451static void
4452compute_delayed_physnames (struct dwarf2_cu *cu)
4453{
4454 int i;
4455 struct delayed_method_info *mi;
4456 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4457 {
4458 char *physname;
4459 struct fn_fieldlist *fn_flp
4460 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4461 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4462 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4463 }
4464}
4465
10b3939b
DJ
4466/* Generate full symbol information for PST and CU, whose DIEs have
4467 already been loaded into memory. */
4468
4469static void
4470process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4471{
10b3939b 4472 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4473 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4474 CORE_ADDR lowpc, highpc;
4475 struct symtab *symtab;
3da10d80 4476 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4477 CORE_ADDR baseaddr;
4478
4479 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4480
10b3939b
DJ
4481 buildsym_init ();
4482 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4483 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4484
4485 cu->list_in_scope = &file_symbols;
c906108c 4486
d85a05f0 4487 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4488
c906108c 4489 /* Do line number decoding in read_file_scope () */
10b3939b 4490 process_die (cu->dies, cu);
c906108c 4491
3da10d80
KS
4492 /* Now that we have processed all the DIEs in the CU, all the types
4493 should be complete, and it should now be safe to compute all of the
4494 physnames. */
4495 compute_delayed_physnames (cu);
4496 do_cleanups (delayed_list_cleanup);
4497
fae299cd
DC
4498 /* Some compilers don't define a DW_AT_high_pc attribute for the
4499 compilation unit. If the DW_AT_high_pc is missing, synthesize
4500 it, by scanning the DIE's below the compilation unit. */
10b3939b 4501 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4502
613e1657 4503 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4504
4505 /* Set symtab language to language from DW_AT_language.
4506 If the compilation is from a C file generated by language preprocessors,
4507 do not set the language if it was already deduced by start_subfile. */
4508 if (symtab != NULL
10b3939b 4509 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4510 {
10b3939b 4511 symtab->language = cu->language;
c906108c 4512 }
9291a0cd
TT
4513
4514 if (dwarf2_per_objfile->using_index)
4515 per_cu->v.quick->symtab = symtab;
4516 else
4517 {
4518 struct partial_symtab *pst = per_cu->v.psymtab;
4519 pst->symtab = symtab;
4520 pst->readin = 1;
4521 }
c906108c
SS
4522
4523 do_cleanups (back_to);
4524}
4525
4526/* Process a die and its children. */
4527
4528static void
e7c27a73 4529process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4530{
4531 switch (die->tag)
4532 {
4533 case DW_TAG_padding:
4534 break;
4535 case DW_TAG_compile_unit:
e7c27a73 4536 read_file_scope (die, cu);
c906108c 4537 break;
348e048f
DE
4538 case DW_TAG_type_unit:
4539 read_type_unit_scope (die, cu);
4540 break;
c906108c 4541 case DW_TAG_subprogram:
c906108c 4542 case DW_TAG_inlined_subroutine:
edb3359d 4543 read_func_scope (die, cu);
c906108c
SS
4544 break;
4545 case DW_TAG_lexical_block:
14898363
L
4546 case DW_TAG_try_block:
4547 case DW_TAG_catch_block:
e7c27a73 4548 read_lexical_block_scope (die, cu);
c906108c
SS
4549 break;
4550 case DW_TAG_class_type:
680b30c7 4551 case DW_TAG_interface_type:
c906108c
SS
4552 case DW_TAG_structure_type:
4553 case DW_TAG_union_type:
134d01f1 4554 process_structure_scope (die, cu);
c906108c
SS
4555 break;
4556 case DW_TAG_enumeration_type:
134d01f1 4557 process_enumeration_scope (die, cu);
c906108c 4558 break;
134d01f1 4559
f792889a
DJ
4560 /* These dies have a type, but processing them does not create
4561 a symbol or recurse to process the children. Therefore we can
4562 read them on-demand through read_type_die. */
c906108c 4563 case DW_TAG_subroutine_type:
72019c9c 4564 case DW_TAG_set_type:
c906108c 4565 case DW_TAG_array_type:
c906108c 4566 case DW_TAG_pointer_type:
c906108c 4567 case DW_TAG_ptr_to_member_type:
c906108c 4568 case DW_TAG_reference_type:
c906108c 4569 case DW_TAG_string_type:
c906108c 4570 break;
134d01f1 4571
c906108c 4572 case DW_TAG_base_type:
a02abb62 4573 case DW_TAG_subrange_type:
cb249c71 4574 case DW_TAG_typedef:
134d01f1
DJ
4575 /* Add a typedef symbol for the type definition, if it has a
4576 DW_AT_name. */
f792889a 4577 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4578 break;
c906108c 4579 case DW_TAG_common_block:
e7c27a73 4580 read_common_block (die, cu);
c906108c
SS
4581 break;
4582 case DW_TAG_common_inclusion:
4583 break;
d9fa45fe 4584 case DW_TAG_namespace:
63d06c5c 4585 processing_has_namespace_info = 1;
e7c27a73 4586 read_namespace (die, cu);
d9fa45fe 4587 break;
5d7cb8df 4588 case DW_TAG_module:
f55ee35c 4589 processing_has_namespace_info = 1;
5d7cb8df
JK
4590 read_module (die, cu);
4591 break;
d9fa45fe
DC
4592 case DW_TAG_imported_declaration:
4593 case DW_TAG_imported_module:
63d06c5c 4594 processing_has_namespace_info = 1;
27aa8d6a
SW
4595 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4596 || cu->language != language_fortran))
4597 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4598 dwarf_tag_name (die->tag));
4599 read_import_statement (die, cu);
d9fa45fe 4600 break;
c906108c 4601 default:
e7c27a73 4602 new_symbol (die, NULL, cu);
c906108c
SS
4603 break;
4604 }
4605}
4606
94af9270
KS
4607/* A helper function for dwarf2_compute_name which determines whether DIE
4608 needs to have the name of the scope prepended to the name listed in the
4609 die. */
4610
4611static int
4612die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4613{
1c809c68
TT
4614 struct attribute *attr;
4615
94af9270
KS
4616 switch (die->tag)
4617 {
4618 case DW_TAG_namespace:
4619 case DW_TAG_typedef:
4620 case DW_TAG_class_type:
4621 case DW_TAG_interface_type:
4622 case DW_TAG_structure_type:
4623 case DW_TAG_union_type:
4624 case DW_TAG_enumeration_type:
4625 case DW_TAG_enumerator:
4626 case DW_TAG_subprogram:
4627 case DW_TAG_member:
4628 return 1;
4629
4630 case DW_TAG_variable:
4631 /* We only need to prefix "globally" visible variables. These include
4632 any variable marked with DW_AT_external or any variable that
4633 lives in a namespace. [Variables in anonymous namespaces
4634 require prefixing, but they are not DW_AT_external.] */
4635
4636 if (dwarf2_attr (die, DW_AT_specification, cu))
4637 {
4638 struct dwarf2_cu *spec_cu = cu;
9a619af0 4639
94af9270
KS
4640 return die_needs_namespace (die_specification (die, &spec_cu),
4641 spec_cu);
4642 }
4643
1c809c68 4644 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4645 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4646 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4647 return 0;
4648 /* A variable in a lexical block of some kind does not need a
4649 namespace, even though in C++ such variables may be external
4650 and have a mangled name. */
4651 if (die->parent->tag == DW_TAG_lexical_block
4652 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4653 || die->parent->tag == DW_TAG_catch_block
4654 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4655 return 0;
4656 return 1;
94af9270
KS
4657
4658 default:
4659 return 0;
4660 }
4661}
4662
98bfdba5
PA
4663/* Retrieve the last character from a mem_file. */
4664
4665static void
4666do_ui_file_peek_last (void *object, const char *buffer, long length)
4667{
4668 char *last_char_p = (char *) object;
4669
4670 if (length > 0)
4671 *last_char_p = buffer[length - 1];
4672}
4673
94af9270
KS
4674/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4675 compute the physname for the object, which include a method's
4676 formal parameters (C++/Java) and return type (Java).
4677
af6b7be1
JB
4678 For Ada, return the DIE's linkage name rather than the fully qualified
4679 name. PHYSNAME is ignored..
4680
94af9270
KS
4681 The result is allocated on the objfile_obstack and canonicalized. */
4682
4683static const char *
4684dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4685 int physname)
4686{
4687 if (name == NULL)
4688 name = dwarf2_name (die, cu);
4689
f55ee35c
JK
4690 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4691 compute it by typename_concat inside GDB. */
4692 if (cu->language == language_ada
4693 || (cu->language == language_fortran && physname))
4694 {
4695 /* For Ada unit, we prefer the linkage name over the name, as
4696 the former contains the exported name, which the user expects
4697 to be able to reference. Ideally, we want the user to be able
4698 to reference this entity using either natural or linkage name,
4699 but we haven't started looking at this enhancement yet. */
4700 struct attribute *attr;
4701
4702 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4703 if (attr == NULL)
4704 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4705 if (attr && DW_STRING (attr))
4706 return DW_STRING (attr);
4707 }
4708
94af9270
KS
4709 /* These are the only languages we know how to qualify names in. */
4710 if (name != NULL
f55ee35c
JK
4711 && (cu->language == language_cplus || cu->language == language_java
4712 || cu->language == language_fortran))
94af9270
KS
4713 {
4714 if (die_needs_namespace (die, cu))
4715 {
4716 long length;
4717 char *prefix;
4718 struct ui_file *buf;
4719
4720 prefix = determine_prefix (die, cu);
4721 buf = mem_fileopen ();
4722 if (*prefix != '\0')
4723 {
f55ee35c
JK
4724 char *prefixed_name = typename_concat (NULL, prefix, name,
4725 physname, cu);
9a619af0 4726
94af9270
KS
4727 fputs_unfiltered (prefixed_name, buf);
4728 xfree (prefixed_name);
4729 }
4730 else
4731 fputs_unfiltered (name ? name : "", buf);
4732
98bfdba5
PA
4733 /* Template parameters may be specified in the DIE's DW_AT_name, or
4734 as children with DW_TAG_template_type_param or
4735 DW_TAG_value_type_param. If the latter, add them to the name
4736 here. If the name already has template parameters, then
4737 skip this step; some versions of GCC emit both, and
4738 it is more efficient to use the pre-computed name.
4739
4740 Something to keep in mind about this process: it is very
4741 unlikely, or in some cases downright impossible, to produce
4742 something that will match the mangled name of a function.
4743 If the definition of the function has the same debug info,
4744 we should be able to match up with it anyway. But fallbacks
4745 using the minimal symbol, for instance to find a method
4746 implemented in a stripped copy of libstdc++, will not work.
4747 If we do not have debug info for the definition, we will have to
4748 match them up some other way.
4749
4750 When we do name matching there is a related problem with function
4751 templates; two instantiated function templates are allowed to
4752 differ only by their return types, which we do not add here. */
4753
4754 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4755 {
4756 struct attribute *attr;
4757 struct die_info *child;
4758 int first = 1;
4759
4760 die->building_fullname = 1;
4761
4762 for (child = die->child; child != NULL; child = child->sibling)
4763 {
4764 struct type *type;
4765 long value;
4766 gdb_byte *bytes;
4767 struct dwarf2_locexpr_baton *baton;
4768 struct value *v;
4769
4770 if (child->tag != DW_TAG_template_type_param
4771 && child->tag != DW_TAG_template_value_param)
4772 continue;
4773
4774 if (first)
4775 {
4776 fputs_unfiltered ("<", buf);
4777 first = 0;
4778 }
4779 else
4780 fputs_unfiltered (", ", buf);
4781
4782 attr = dwarf2_attr (child, DW_AT_type, cu);
4783 if (attr == NULL)
4784 {
4785 complaint (&symfile_complaints,
4786 _("template parameter missing DW_AT_type"));
4787 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4788 continue;
4789 }
4790 type = die_type (child, cu);
4791
4792 if (child->tag == DW_TAG_template_type_param)
4793 {
4794 c_print_type (type, "", buf, -1, 0);
4795 continue;
4796 }
4797
4798 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4799 if (attr == NULL)
4800 {
4801 complaint (&symfile_complaints,
4802 _("template parameter missing DW_AT_const_value"));
4803 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4804 continue;
4805 }
4806
4807 dwarf2_const_value_attr (attr, type, name,
4808 &cu->comp_unit_obstack, cu,
4809 &value, &bytes, &baton);
4810
4811 if (TYPE_NOSIGN (type))
4812 /* GDB prints characters as NUMBER 'CHAR'. If that's
4813 changed, this can use value_print instead. */
4814 c_printchar (value, type, buf);
4815 else
4816 {
4817 struct value_print_options opts;
4818
4819 if (baton != NULL)
4820 v = dwarf2_evaluate_loc_desc (type, NULL,
4821 baton->data,
4822 baton->size,
4823 baton->per_cu);
4824 else if (bytes != NULL)
4825 {
4826 v = allocate_value (type);
4827 memcpy (value_contents_writeable (v), bytes,
4828 TYPE_LENGTH (type));
4829 }
4830 else
4831 v = value_from_longest (type, value);
4832
4833 /* Specify decimal so that we do not depend on the radix. */
4834 get_formatted_print_options (&opts, 'd');
4835 opts.raw = 1;
4836 value_print (v, buf, &opts);
4837 release_value (v);
4838 value_free (v);
4839 }
4840 }
4841
4842 die->building_fullname = 0;
4843
4844 if (!first)
4845 {
4846 /* Close the argument list, with a space if necessary
4847 (nested templates). */
4848 char last_char = '\0';
4849 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4850 if (last_char == '>')
4851 fputs_unfiltered (" >", buf);
4852 else
4853 fputs_unfiltered (">", buf);
4854 }
4855 }
4856
94af9270
KS
4857 /* For Java and C++ methods, append formal parameter type
4858 information, if PHYSNAME. */
6e70227d 4859
94af9270
KS
4860 if (physname && die->tag == DW_TAG_subprogram
4861 && (cu->language == language_cplus
4862 || cu->language == language_java))
4863 {
4864 struct type *type = read_type_die (die, cu);
4865
4866 c_type_print_args (type, buf, 0, cu->language);
4867
4868 if (cu->language == language_java)
4869 {
4870 /* For java, we must append the return type to method
4871 names. */
4872 if (die->tag == DW_TAG_subprogram)
4873 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4874 0, 0);
4875 }
4876 else if (cu->language == language_cplus)
4877 {
4878 if (TYPE_NFIELDS (type) > 0
4879 && TYPE_FIELD_ARTIFICIAL (type, 0)
4880 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4881 fputs_unfiltered (" const", buf);
4882 }
4883 }
4884
4885 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4886 &length);
4887 ui_file_delete (buf);
4888
4889 if (cu->language == language_cplus)
4890 {
4891 char *cname
4892 = dwarf2_canonicalize_name (name, cu,
4893 &cu->objfile->objfile_obstack);
9a619af0 4894
94af9270
KS
4895 if (cname != NULL)
4896 name = cname;
4897 }
4898 }
4899 }
4900
4901 return name;
4902}
4903
0114d602
DJ
4904/* Return the fully qualified name of DIE, based on its DW_AT_name.
4905 If scope qualifiers are appropriate they will be added. The result
4906 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4907 not have a name. NAME may either be from a previous call to
4908 dwarf2_name or NULL.
4909
4910 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4911
4912static const char *
94af9270 4913dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4914{
94af9270
KS
4915 return dwarf2_compute_name (name, die, cu, 0);
4916}
0114d602 4917
94af9270
KS
4918/* Construct a physname for the given DIE in CU. NAME may either be
4919 from a previous call to dwarf2_name or NULL. The result will be
4920 allocated on the objfile_objstack or NULL if the DIE does not have a
4921 name.
0114d602 4922
94af9270 4923 The output string will be canonicalized (if C++/Java). */
0114d602 4924
94af9270
KS
4925static const char *
4926dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4927{
4928 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4929}
4930
27aa8d6a
SW
4931/* Read the import statement specified by the given die and record it. */
4932
4933static void
4934read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4935{
4936 struct attribute *import_attr;
4937 struct die_info *imported_die;
de4affc9 4938 struct dwarf2_cu *imported_cu;
27aa8d6a 4939 const char *imported_name;
794684b6 4940 const char *imported_name_prefix;
13387711
SW
4941 const char *canonical_name;
4942 const char *import_alias;
4943 const char *imported_declaration = NULL;
794684b6 4944 const char *import_prefix;
13387711
SW
4945
4946 char *temp;
27aa8d6a
SW
4947
4948 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4949 if (import_attr == NULL)
4950 {
4951 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4952 dwarf_tag_name (die->tag));
4953 return;
4954 }
4955
de4affc9
CC
4956 imported_cu = cu;
4957 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4958 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4959 if (imported_name == NULL)
4960 {
4961 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4962
4963 The import in the following code:
4964 namespace A
4965 {
4966 typedef int B;
4967 }
4968
4969 int main ()
4970 {
4971 using A::B;
4972 B b;
4973 return b;
4974 }
4975
4976 ...
4977 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4978 <52> DW_AT_decl_file : 1
4979 <53> DW_AT_decl_line : 6
4980 <54> DW_AT_import : <0x75>
4981 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4982 <59> DW_AT_name : B
4983 <5b> DW_AT_decl_file : 1
4984 <5c> DW_AT_decl_line : 2
4985 <5d> DW_AT_type : <0x6e>
4986 ...
4987 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4988 <76> DW_AT_byte_size : 4
4989 <77> DW_AT_encoding : 5 (signed)
4990
4991 imports the wrong die ( 0x75 instead of 0x58 ).
4992 This case will be ignored until the gcc bug is fixed. */
4993 return;
4994 }
4995
82856980
SW
4996 /* Figure out the local name after import. */
4997 import_alias = dwarf2_name (die, cu);
27aa8d6a 4998
794684b6
SW
4999 /* Figure out where the statement is being imported to. */
5000 import_prefix = determine_prefix (die, cu);
5001
5002 /* Figure out what the scope of the imported die is and prepend it
5003 to the name of the imported die. */
de4affc9 5004 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5005
f55ee35c
JK
5006 if (imported_die->tag != DW_TAG_namespace
5007 && imported_die->tag != DW_TAG_module)
794684b6 5008 {
13387711
SW
5009 imported_declaration = imported_name;
5010 canonical_name = imported_name_prefix;
794684b6 5011 }
13387711 5012 else if (strlen (imported_name_prefix) > 0)
794684b6 5013 {
13387711
SW
5014 temp = alloca (strlen (imported_name_prefix)
5015 + 2 + strlen (imported_name) + 1);
5016 strcpy (temp, imported_name_prefix);
5017 strcat (temp, "::");
5018 strcat (temp, imported_name);
5019 canonical_name = temp;
794684b6 5020 }
13387711
SW
5021 else
5022 canonical_name = imported_name;
794684b6 5023
c0cc3a76
SW
5024 cp_add_using_directive (import_prefix,
5025 canonical_name,
5026 import_alias,
13387711 5027 imported_declaration,
c0cc3a76 5028 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5029}
5030
5fb290d7 5031static void
e142c38c 5032initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5033{
e142c38c 5034 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5035}
5036
cb1df416
DJ
5037static void
5038free_cu_line_header (void *arg)
5039{
5040 struct dwarf2_cu *cu = arg;
5041
5042 free_line_header (cu->line_header);
5043 cu->line_header = NULL;
5044}
5045
9291a0cd
TT
5046static void
5047find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5048 char **name, char **comp_dir)
5049{
5050 struct attribute *attr;
5051
5052 *name = NULL;
5053 *comp_dir = NULL;
5054
5055 /* Find the filename. Do not use dwarf2_name here, since the filename
5056 is not a source language identifier. */
5057 attr = dwarf2_attr (die, DW_AT_name, cu);
5058 if (attr)
5059 {
5060 *name = DW_STRING (attr);
5061 }
5062
5063 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5064 if (attr)
5065 *comp_dir = DW_STRING (attr);
5066 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5067 {
5068 *comp_dir = ldirname (*name);
5069 if (*comp_dir != NULL)
5070 make_cleanup (xfree, *comp_dir);
5071 }
5072 if (*comp_dir != NULL)
5073 {
5074 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5075 directory, get rid of it. */
5076 char *cp = strchr (*comp_dir, ':');
5077
5078 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5079 *comp_dir = cp + 1;
5080 }
5081
5082 if (*name == NULL)
5083 *name = "<unknown>";
5084}
5085
c906108c 5086static void
e7c27a73 5087read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5088{
e7c27a73 5089 struct objfile *objfile = cu->objfile;
debd256d 5090 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5091 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5092 CORE_ADDR highpc = ((CORE_ADDR) 0);
5093 struct attribute *attr;
e1024ff1 5094 char *name = NULL;
c906108c
SS
5095 char *comp_dir = NULL;
5096 struct die_info *child_die;
5097 bfd *abfd = objfile->obfd;
debd256d 5098 struct line_header *line_header = 0;
e142c38c 5099 CORE_ADDR baseaddr;
6e70227d 5100
e142c38c 5101 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5102
fae299cd 5103 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5104
5105 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5106 from finish_block. */
2acceee2 5107 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5108 lowpc = highpc;
5109 lowpc += baseaddr;
5110 highpc += baseaddr;
5111
9291a0cd 5112 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5113
e142c38c 5114 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5115 if (attr)
5116 {
e142c38c 5117 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5118 }
5119
b0f35d58 5120 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5121 if (attr)
b0f35d58 5122 cu->producer = DW_STRING (attr);
303b6f5d 5123
c906108c
SS
5124 /* We assume that we're processing GCC output. */
5125 processing_gcc_compilation = 2;
c906108c 5126
df8a16a1
DJ
5127 processing_has_namespace_info = 0;
5128
c906108c
SS
5129 start_symtab (name, comp_dir, lowpc);
5130 record_debugformat ("DWARF 2");
303b6f5d 5131 record_producer (cu->producer);
c906108c 5132
e142c38c 5133 initialize_cu_func_list (cu);
c906108c 5134
cb1df416
DJ
5135 /* Decode line number information if present. We do this before
5136 processing child DIEs, so that the line header table is available
5137 for DW_AT_decl_file. */
e142c38c 5138 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5139 if (attr)
5140 {
debd256d 5141 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5142 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5143 if (line_header)
5144 {
cb1df416
DJ
5145 cu->line_header = line_header;
5146 make_cleanup (free_cu_line_header, cu);
aaa75496 5147 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5148 }
5fb290d7 5149 }
debd256d 5150
cb1df416
DJ
5151 /* Process all dies in compilation unit. */
5152 if (die->child != NULL)
5153 {
5154 child_die = die->child;
5155 while (child_die && child_die->tag)
5156 {
5157 process_die (child_die, cu);
5158 child_die = sibling_die (child_die);
5159 }
5160 }
5161
2e276125
JB
5162 /* Decode macro information, if present. Dwarf 2 macro information
5163 refers to information in the line number info statement program
5164 header, so we can only read it if we've read the header
5165 successfully. */
e142c38c 5166 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5167 if (attr && line_header)
2e276125
JB
5168 {
5169 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5170
2e276125 5171 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5172 comp_dir, abfd, cu);
2e276125 5173 }
debd256d 5174 do_cleanups (back_to);
5fb290d7
DJ
5175}
5176
348e048f
DE
5177/* For TUs we want to skip the first top level sibling if it's not the
5178 actual type being defined by this TU. In this case the first top
5179 level sibling is there to provide context only. */
5180
5181static void
5182read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5183{
5184 struct objfile *objfile = cu->objfile;
5185 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5186 CORE_ADDR lowpc;
5187 struct attribute *attr;
5188 char *name = NULL;
5189 char *comp_dir = NULL;
5190 struct die_info *child_die;
5191 bfd *abfd = objfile->obfd;
348e048f
DE
5192
5193 /* start_symtab needs a low pc, but we don't really have one.
5194 Do what read_file_scope would do in the absence of such info. */
5195 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5196
5197 /* Find the filename. Do not use dwarf2_name here, since the filename
5198 is not a source language identifier. */
5199 attr = dwarf2_attr (die, DW_AT_name, cu);
5200 if (attr)
5201 name = DW_STRING (attr);
5202
5203 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5204 if (attr)
5205 comp_dir = DW_STRING (attr);
5206 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5207 {
5208 comp_dir = ldirname (name);
5209 if (comp_dir != NULL)
5210 make_cleanup (xfree, comp_dir);
5211 }
5212
5213 if (name == NULL)
5214 name = "<unknown>";
5215
5216 attr = dwarf2_attr (die, DW_AT_language, cu);
5217 if (attr)
5218 set_cu_language (DW_UNSND (attr), cu);
5219
5220 /* This isn't technically needed today. It is done for symmetry
5221 with read_file_scope. */
5222 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5223 if (attr)
348e048f
DE
5224 cu->producer = DW_STRING (attr);
5225
5226 /* We assume that we're processing GCC output. */
5227 processing_gcc_compilation = 2;
5228
5229 processing_has_namespace_info = 0;
5230
5231 start_symtab (name, comp_dir, lowpc);
5232 record_debugformat ("DWARF 2");
5233 record_producer (cu->producer);
5234
5235 /* Process the dies in the type unit. */
5236 if (die->child == NULL)
5237 {
5238 dump_die_for_error (die);
5239 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5240 bfd_get_filename (abfd));
5241 }
5242
5243 child_die = die->child;
5244
5245 while (child_die && child_die->tag)
5246 {
5247 process_die (child_die, cu);
5248
5249 child_die = sibling_die (child_die);
5250 }
5251
5252 do_cleanups (back_to);
5253}
5254
5fb290d7 5255static void
e142c38c
DJ
5256add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5257 struct dwarf2_cu *cu)
5fb290d7
DJ
5258{
5259 struct function_range *thisfn;
5260
5261 thisfn = (struct function_range *)
7b5a2f43 5262 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5263 thisfn->name = name;
5264 thisfn->lowpc = lowpc;
5265 thisfn->highpc = highpc;
5266 thisfn->seen_line = 0;
5267 thisfn->next = NULL;
5268
e142c38c
DJ
5269 if (cu->last_fn == NULL)
5270 cu->first_fn = thisfn;
5fb290d7 5271 else
e142c38c 5272 cu->last_fn->next = thisfn;
5fb290d7 5273
e142c38c 5274 cu->last_fn = thisfn;
c906108c
SS
5275}
5276
d389af10
JK
5277/* qsort helper for inherit_abstract_dies. */
5278
5279static int
5280unsigned_int_compar (const void *ap, const void *bp)
5281{
5282 unsigned int a = *(unsigned int *) ap;
5283 unsigned int b = *(unsigned int *) bp;
5284
5285 return (a > b) - (b > a);
5286}
5287
5288/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5289 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5290 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5291
5292static void
5293inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5294{
5295 struct die_info *child_die;
5296 unsigned die_children_count;
5297 /* CU offsets which were referenced by children of the current DIE. */
5298 unsigned *offsets;
5299 unsigned *offsets_end, *offsetp;
5300 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5301 struct die_info *origin_die;
5302 /* Iterator of the ORIGIN_DIE children. */
5303 struct die_info *origin_child_die;
5304 struct cleanup *cleanups;
5305 struct attribute *attr;
cd02d79d
PA
5306 struct dwarf2_cu *origin_cu;
5307 struct pending **origin_previous_list_in_scope;
d389af10
JK
5308
5309 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5310 if (!attr)
5311 return;
5312
cd02d79d
PA
5313 /* Note that following die references may follow to a die in a
5314 different cu. */
5315
5316 origin_cu = cu;
5317 origin_die = follow_die_ref (die, attr, &origin_cu);
5318
5319 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5320 symbols in. */
5321 origin_previous_list_in_scope = origin_cu->list_in_scope;
5322 origin_cu->list_in_scope = cu->list_in_scope;
5323
edb3359d
DJ
5324 if (die->tag != origin_die->tag
5325 && !(die->tag == DW_TAG_inlined_subroutine
5326 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5327 complaint (&symfile_complaints,
5328 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5329 die->offset, origin_die->offset);
5330
5331 child_die = die->child;
5332 die_children_count = 0;
5333 while (child_die && child_die->tag)
5334 {
5335 child_die = sibling_die (child_die);
5336 die_children_count++;
5337 }
5338 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5339 cleanups = make_cleanup (xfree, offsets);
5340
5341 offsets_end = offsets;
5342 child_die = die->child;
5343 while (child_die && child_die->tag)
5344 {
c38f313d
DJ
5345 /* For each CHILD_DIE, find the corresponding child of
5346 ORIGIN_DIE. If there is more than one layer of
5347 DW_AT_abstract_origin, follow them all; there shouldn't be,
5348 but GCC versions at least through 4.4 generate this (GCC PR
5349 40573). */
5350 struct die_info *child_origin_die = child_die;
cd02d79d 5351 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5352
c38f313d
DJ
5353 while (1)
5354 {
cd02d79d
PA
5355 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5356 child_origin_cu);
c38f313d
DJ
5357 if (attr == NULL)
5358 break;
cd02d79d
PA
5359 child_origin_die = follow_die_ref (child_origin_die, attr,
5360 &child_origin_cu);
c38f313d
DJ
5361 }
5362
d389af10
JK
5363 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5364 counterpart may exist. */
c38f313d 5365 if (child_origin_die != child_die)
d389af10 5366 {
edb3359d
DJ
5367 if (child_die->tag != child_origin_die->tag
5368 && !(child_die->tag == DW_TAG_inlined_subroutine
5369 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5370 complaint (&symfile_complaints,
5371 _("Child DIE 0x%x and its abstract origin 0x%x have "
5372 "different tags"), child_die->offset,
5373 child_origin_die->offset);
c38f313d
DJ
5374 if (child_origin_die->parent != origin_die)
5375 complaint (&symfile_complaints,
5376 _("Child DIE 0x%x and its abstract origin 0x%x have "
5377 "different parents"), child_die->offset,
5378 child_origin_die->offset);
5379 else
5380 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5381 }
5382 child_die = sibling_die (child_die);
5383 }
5384 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5385 unsigned_int_compar);
5386 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5387 if (offsetp[-1] == *offsetp)
5388 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5389 "to DIE 0x%x as their abstract origin"),
5390 die->offset, *offsetp);
5391
5392 offsetp = offsets;
5393 origin_child_die = origin_die->child;
5394 while (origin_child_die && origin_child_die->tag)
5395 {
5396 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5397 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5398 offsetp++;
5399 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5400 {
5401 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5402 process_die (origin_child_die, origin_cu);
d389af10
JK
5403 }
5404 origin_child_die = sibling_die (origin_child_die);
5405 }
cd02d79d 5406 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5407
5408 do_cleanups (cleanups);
5409}
5410
c906108c 5411static void
e7c27a73 5412read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5413{
e7c27a73 5414 struct objfile *objfile = cu->objfile;
52f0bd74 5415 struct context_stack *new;
c906108c
SS
5416 CORE_ADDR lowpc;
5417 CORE_ADDR highpc;
5418 struct die_info *child_die;
edb3359d 5419 struct attribute *attr, *call_line, *call_file;
c906108c 5420 char *name;
e142c38c 5421 CORE_ADDR baseaddr;
801e3a5b 5422 struct block *block;
edb3359d 5423 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5424 VEC (symbolp) *template_args = NULL;
5425 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5426
5427 if (inlined_func)
5428 {
5429 /* If we do not have call site information, we can't show the
5430 caller of this inlined function. That's too confusing, so
5431 only use the scope for local variables. */
5432 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5433 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5434 if (call_line == NULL || call_file == NULL)
5435 {
5436 read_lexical_block_scope (die, cu);
5437 return;
5438 }
5439 }
c906108c 5440
e142c38c
DJ
5441 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5442
94af9270 5443 name = dwarf2_name (die, cu);
c906108c 5444
e8d05480
JB
5445 /* Ignore functions with missing or empty names. These are actually
5446 illegal according to the DWARF standard. */
5447 if (name == NULL)
5448 {
5449 complaint (&symfile_complaints,
5450 _("missing name for subprogram DIE at %d"), die->offset);
5451 return;
5452 }
5453
5454 /* Ignore functions with missing or invalid low and high pc attributes. */
5455 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5456 {
ae4d0c03
PM
5457 attr = dwarf2_attr (die, DW_AT_external, cu);
5458 if (!attr || !DW_UNSND (attr))
5459 complaint (&symfile_complaints,
5460 _("cannot get low and high bounds for subprogram DIE at %d"),
5461 die->offset);
e8d05480
JB
5462 return;
5463 }
c906108c
SS
5464
5465 lowpc += baseaddr;
5466 highpc += baseaddr;
5467
5fb290d7 5468 /* Record the function range for dwarf_decode_lines. */
e142c38c 5469 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5470
34eaf542
TT
5471 /* If we have any template arguments, then we must allocate a
5472 different sort of symbol. */
5473 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5474 {
5475 if (child_die->tag == DW_TAG_template_type_param
5476 || child_die->tag == DW_TAG_template_value_param)
5477 {
5478 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5479 struct template_symbol);
5480 templ_func->base.is_cplus_template_function = 1;
5481 break;
5482 }
5483 }
5484
c906108c 5485 new = push_context (0, lowpc);
34eaf542
TT
5486 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5487 (struct symbol *) templ_func);
4c2df51b 5488
4cecd739
DJ
5489 /* If there is a location expression for DW_AT_frame_base, record
5490 it. */
e142c38c 5491 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5492 if (attr)
c034e007
AC
5493 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5494 expression is being recorded directly in the function's symbol
5495 and not in a separate frame-base object. I guess this hack is
5496 to avoid adding some sort of frame-base adjunct/annex to the
5497 function's symbol :-(. The problem with doing this is that it
5498 results in a function symbol with a location expression that
5499 has nothing to do with the location of the function, ouch! The
5500 relationship should be: a function's symbol has-a frame base; a
5501 frame-base has-a location expression. */
e7c27a73 5502 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5503
e142c38c 5504 cu->list_in_scope = &local_symbols;
c906108c 5505
639d11d3 5506 if (die->child != NULL)
c906108c 5507 {
639d11d3 5508 child_die = die->child;
c906108c
SS
5509 while (child_die && child_die->tag)
5510 {
34eaf542
TT
5511 if (child_die->tag == DW_TAG_template_type_param
5512 || child_die->tag == DW_TAG_template_value_param)
5513 {
5514 struct symbol *arg = new_symbol (child_die, NULL, cu);
5515
5516 VEC_safe_push (symbolp, template_args, arg);
5517 }
5518 else
5519 process_die (child_die, cu);
c906108c
SS
5520 child_die = sibling_die (child_die);
5521 }
5522 }
5523
d389af10
JK
5524 inherit_abstract_dies (die, cu);
5525
4a811a97
UW
5526 /* If we have a DW_AT_specification, we might need to import using
5527 directives from the context of the specification DIE. See the
5528 comment in determine_prefix. */
5529 if (cu->language == language_cplus
5530 && dwarf2_attr (die, DW_AT_specification, cu))
5531 {
5532 struct dwarf2_cu *spec_cu = cu;
5533 struct die_info *spec_die = die_specification (die, &spec_cu);
5534
5535 while (spec_die)
5536 {
5537 child_die = spec_die->child;
5538 while (child_die && child_die->tag)
5539 {
5540 if (child_die->tag == DW_TAG_imported_module)
5541 process_die (child_die, spec_cu);
5542 child_die = sibling_die (child_die);
5543 }
5544
5545 /* In some cases, GCC generates specification DIEs that
5546 themselves contain DW_AT_specification attributes. */
5547 spec_die = die_specification (spec_die, &spec_cu);
5548 }
5549 }
5550
c906108c
SS
5551 new = pop_context ();
5552 /* Make a block for the local symbols within. */
801e3a5b
JB
5553 block = finish_block (new->name, &local_symbols, new->old_blocks,
5554 lowpc, highpc, objfile);
5555
df8a16a1 5556 /* For C++, set the block's scope. */
f55ee35c 5557 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5558 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5559 determine_prefix (die, cu),
df8a16a1
DJ
5560 processing_has_namespace_info);
5561
801e3a5b
JB
5562 /* If we have address ranges, record them. */
5563 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5564
34eaf542
TT
5565 /* Attach template arguments to function. */
5566 if (! VEC_empty (symbolp, template_args))
5567 {
5568 gdb_assert (templ_func != NULL);
5569
5570 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5571 templ_func->template_arguments
5572 = obstack_alloc (&objfile->objfile_obstack,
5573 (templ_func->n_template_arguments
5574 * sizeof (struct symbol *)));
5575 memcpy (templ_func->template_arguments,
5576 VEC_address (symbolp, template_args),
5577 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5578 VEC_free (symbolp, template_args);
5579 }
5580
208d8187
JB
5581 /* In C++, we can have functions nested inside functions (e.g., when
5582 a function declares a class that has methods). This means that
5583 when we finish processing a function scope, we may need to go
5584 back to building a containing block's symbol lists. */
5585 local_symbols = new->locals;
5586 param_symbols = new->params;
27aa8d6a 5587 using_directives = new->using_directives;
208d8187 5588
921e78cf
JB
5589 /* If we've finished processing a top-level function, subsequent
5590 symbols go in the file symbol list. */
5591 if (outermost_context_p ())
e142c38c 5592 cu->list_in_scope = &file_symbols;
c906108c
SS
5593}
5594
5595/* Process all the DIES contained within a lexical block scope. Start
5596 a new scope, process the dies, and then close the scope. */
5597
5598static void
e7c27a73 5599read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5600{
e7c27a73 5601 struct objfile *objfile = cu->objfile;
52f0bd74 5602 struct context_stack *new;
c906108c
SS
5603 CORE_ADDR lowpc, highpc;
5604 struct die_info *child_die;
e142c38c
DJ
5605 CORE_ADDR baseaddr;
5606
5607 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5608
5609 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5610 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5611 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5612 be nasty. Might be easier to properly extend generic blocks to
af34e669 5613 describe ranges. */
d85a05f0 5614 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5615 return;
5616 lowpc += baseaddr;
5617 highpc += baseaddr;
5618
5619 push_context (0, lowpc);
639d11d3 5620 if (die->child != NULL)
c906108c 5621 {
639d11d3 5622 child_die = die->child;
c906108c
SS
5623 while (child_die && child_die->tag)
5624 {
e7c27a73 5625 process_die (child_die, cu);
c906108c
SS
5626 child_die = sibling_die (child_die);
5627 }
5628 }
5629 new = pop_context ();
5630
8540c487 5631 if (local_symbols != NULL || using_directives != NULL)
c906108c 5632 {
801e3a5b
JB
5633 struct block *block
5634 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5635 highpc, objfile);
5636
5637 /* Note that recording ranges after traversing children, as we
5638 do here, means that recording a parent's ranges entails
5639 walking across all its children's ranges as they appear in
5640 the address map, which is quadratic behavior.
5641
5642 It would be nicer to record the parent's ranges before
5643 traversing its children, simply overriding whatever you find
5644 there. But since we don't even decide whether to create a
5645 block until after we've traversed its children, that's hard
5646 to do. */
5647 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5648 }
5649 local_symbols = new->locals;
27aa8d6a 5650 using_directives = new->using_directives;
c906108c
SS
5651}
5652
43039443 5653/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5654 Return 1 if the attributes are present and valid, otherwise, return 0.
5655 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5656
5657static int
5658dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5659 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5660 struct partial_symtab *ranges_pst)
43039443
JK
5661{
5662 struct objfile *objfile = cu->objfile;
5663 struct comp_unit_head *cu_header = &cu->header;
5664 bfd *obfd = objfile->obfd;
5665 unsigned int addr_size = cu_header->addr_size;
5666 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5667 /* Base address selection entry. */
5668 CORE_ADDR base;
5669 int found_base;
5670 unsigned int dummy;
5671 gdb_byte *buffer;
5672 CORE_ADDR marker;
5673 int low_set;
5674 CORE_ADDR low = 0;
5675 CORE_ADDR high = 0;
ff013f42 5676 CORE_ADDR baseaddr;
43039443 5677
d00adf39
DE
5678 found_base = cu->base_known;
5679 base = cu->base_address;
43039443 5680
be391dca 5681 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5682 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5683 {
5684 complaint (&symfile_complaints,
5685 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5686 offset);
5687 return 0;
5688 }
dce234bc 5689 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5690
5691 /* Read in the largest possible address. */
5692 marker = read_address (obfd, buffer, cu, &dummy);
5693 if ((marker & mask) == mask)
5694 {
5695 /* If we found the largest possible address, then
5696 read the base address. */
5697 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5698 buffer += 2 * addr_size;
5699 offset += 2 * addr_size;
5700 found_base = 1;
5701 }
5702
5703 low_set = 0;
5704
e7030f15 5705 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5706
43039443
JK
5707 while (1)
5708 {
5709 CORE_ADDR range_beginning, range_end;
5710
5711 range_beginning = read_address (obfd, buffer, cu, &dummy);
5712 buffer += addr_size;
5713 range_end = read_address (obfd, buffer, cu, &dummy);
5714 buffer += addr_size;
5715 offset += 2 * addr_size;
5716
5717 /* An end of list marker is a pair of zero addresses. */
5718 if (range_beginning == 0 && range_end == 0)
5719 /* Found the end of list entry. */
5720 break;
5721
5722 /* Each base address selection entry is a pair of 2 values.
5723 The first is the largest possible address, the second is
5724 the base address. Check for a base address here. */
5725 if ((range_beginning & mask) == mask)
5726 {
5727 /* If we found the largest possible address, then
5728 read the base address. */
5729 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5730 found_base = 1;
5731 continue;
5732 }
5733
5734 if (!found_base)
5735 {
5736 /* We have no valid base address for the ranges
5737 data. */
5738 complaint (&symfile_complaints,
5739 _("Invalid .debug_ranges data (no base address)"));
5740 return 0;
5741 }
5742
5743 range_beginning += base;
5744 range_end += base;
5745
ff013f42
JK
5746 if (ranges_pst != NULL && range_beginning < range_end)
5747 addrmap_set_empty (objfile->psymtabs_addrmap,
5748 range_beginning + baseaddr, range_end - 1 + baseaddr,
5749 ranges_pst);
5750
43039443
JK
5751 /* FIXME: This is recording everything as a low-high
5752 segment of consecutive addresses. We should have a
5753 data structure for discontiguous block ranges
5754 instead. */
5755 if (! low_set)
5756 {
5757 low = range_beginning;
5758 high = range_end;
5759 low_set = 1;
5760 }
5761 else
5762 {
5763 if (range_beginning < low)
5764 low = range_beginning;
5765 if (range_end > high)
5766 high = range_end;
5767 }
5768 }
5769
5770 if (! low_set)
5771 /* If the first entry is an end-of-list marker, the range
5772 describes an empty scope, i.e. no instructions. */
5773 return 0;
5774
5775 if (low_return)
5776 *low_return = low;
5777 if (high_return)
5778 *high_return = high;
5779 return 1;
5780}
5781
af34e669
DJ
5782/* Get low and high pc attributes from a die. Return 1 if the attributes
5783 are present and valid, otherwise, return 0. Return -1 if the range is
5784 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5785static int
af34e669 5786dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5787 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5788 struct partial_symtab *pst)
c906108c
SS
5789{
5790 struct attribute *attr;
af34e669
DJ
5791 CORE_ADDR low = 0;
5792 CORE_ADDR high = 0;
5793 int ret = 0;
c906108c 5794
e142c38c 5795 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5796 if (attr)
af34e669
DJ
5797 {
5798 high = DW_ADDR (attr);
e142c38c 5799 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5800 if (attr)
5801 low = DW_ADDR (attr);
5802 else
5803 /* Found high w/o low attribute. */
5804 return 0;
5805
5806 /* Found consecutive range of addresses. */
5807 ret = 1;
5808 }
c906108c 5809 else
af34e669 5810 {
e142c38c 5811 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5812 if (attr != NULL)
5813 {
af34e669 5814 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5815 .debug_ranges section. */
d85a05f0 5816 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5817 return 0;
43039443 5818 /* Found discontinuous range of addresses. */
af34e669
DJ
5819 ret = -1;
5820 }
5821 }
c906108c
SS
5822
5823 if (high < low)
5824 return 0;
5825
5826 /* When using the GNU linker, .gnu.linkonce. sections are used to
5827 eliminate duplicate copies of functions and vtables and such.
5828 The linker will arbitrarily choose one and discard the others.
5829 The AT_*_pc values for such functions refer to local labels in
5830 these sections. If the section from that file was discarded, the
5831 labels are not in the output, so the relocs get a value of 0.
5832 If this is a discarded function, mark the pc bounds as invalid,
5833 so that GDB will ignore it. */
72dca2f5 5834 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5835 return 0;
5836
5837 *lowpc = low;
5838 *highpc = high;
af34e669 5839 return ret;
c906108c
SS
5840}
5841
b084d499
JB
5842/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5843 its low and high PC addresses. Do nothing if these addresses could not
5844 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5845 and HIGHPC to the high address if greater than HIGHPC. */
5846
5847static void
5848dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5849 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5850 struct dwarf2_cu *cu)
5851{
5852 CORE_ADDR low, high;
5853 struct die_info *child = die->child;
5854
d85a05f0 5855 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5856 {
5857 *lowpc = min (*lowpc, low);
5858 *highpc = max (*highpc, high);
5859 }
5860
5861 /* If the language does not allow nested subprograms (either inside
5862 subprograms or lexical blocks), we're done. */
5863 if (cu->language != language_ada)
5864 return;
6e70227d 5865
b084d499
JB
5866 /* Check all the children of the given DIE. If it contains nested
5867 subprograms, then check their pc bounds. Likewise, we need to
5868 check lexical blocks as well, as they may also contain subprogram
5869 definitions. */
5870 while (child && child->tag)
5871 {
5872 if (child->tag == DW_TAG_subprogram
5873 || child->tag == DW_TAG_lexical_block)
5874 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5875 child = sibling_die (child);
5876 }
5877}
5878
fae299cd
DC
5879/* Get the low and high pc's represented by the scope DIE, and store
5880 them in *LOWPC and *HIGHPC. If the correct values can't be
5881 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5882
5883static void
5884get_scope_pc_bounds (struct die_info *die,
5885 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5886 struct dwarf2_cu *cu)
5887{
5888 CORE_ADDR best_low = (CORE_ADDR) -1;
5889 CORE_ADDR best_high = (CORE_ADDR) 0;
5890 CORE_ADDR current_low, current_high;
5891
d85a05f0 5892 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5893 {
5894 best_low = current_low;
5895 best_high = current_high;
5896 }
5897 else
5898 {
5899 struct die_info *child = die->child;
5900
5901 while (child && child->tag)
5902 {
5903 switch (child->tag) {
5904 case DW_TAG_subprogram:
b084d499 5905 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5906 break;
5907 case DW_TAG_namespace:
f55ee35c 5908 case DW_TAG_module:
fae299cd
DC
5909 /* FIXME: carlton/2004-01-16: Should we do this for
5910 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5911 that current GCC's always emit the DIEs corresponding
5912 to definitions of methods of classes as children of a
5913 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5914 the DIEs giving the declarations, which could be
5915 anywhere). But I don't see any reason why the
5916 standards says that they have to be there. */
5917 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5918
5919 if (current_low != ((CORE_ADDR) -1))
5920 {
5921 best_low = min (best_low, current_low);
5922 best_high = max (best_high, current_high);
5923 }
5924 break;
5925 default:
5926 /* Ignore. */
5927 break;
5928 }
5929
5930 child = sibling_die (child);
5931 }
5932 }
5933
5934 *lowpc = best_low;
5935 *highpc = best_high;
5936}
5937
801e3a5b
JB
5938/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5939 in DIE. */
5940static void
5941dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5942 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5943{
5944 struct attribute *attr;
5945
5946 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5947 if (attr)
5948 {
5949 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5950
801e3a5b
JB
5951 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5952 if (attr)
5953 {
5954 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5955
801e3a5b
JB
5956 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5957 }
5958 }
5959
5960 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5961 if (attr)
5962 {
5963 bfd *obfd = cu->objfile->obfd;
5964
5965 /* The value of the DW_AT_ranges attribute is the offset of the
5966 address range list in the .debug_ranges section. */
5967 unsigned long offset = DW_UNSND (attr);
dce234bc 5968 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5969
5970 /* For some target architectures, but not others, the
5971 read_address function sign-extends the addresses it returns.
5972 To recognize base address selection entries, we need a
5973 mask. */
5974 unsigned int addr_size = cu->header.addr_size;
5975 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5976
5977 /* The base address, to which the next pair is relative. Note
5978 that this 'base' is a DWARF concept: most entries in a range
5979 list are relative, to reduce the number of relocs against the
5980 debugging information. This is separate from this function's
5981 'baseaddr' argument, which GDB uses to relocate debugging
5982 information from a shared library based on the address at
5983 which the library was loaded. */
d00adf39
DE
5984 CORE_ADDR base = cu->base_address;
5985 int base_known = cu->base_known;
801e3a5b 5986
be391dca 5987 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5988 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5989 {
5990 complaint (&symfile_complaints,
5991 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5992 offset);
5993 return;
5994 }
5995
5996 for (;;)
5997 {
5998 unsigned int bytes_read;
5999 CORE_ADDR start, end;
6000
6001 start = read_address (obfd, buffer, cu, &bytes_read);
6002 buffer += bytes_read;
6003 end = read_address (obfd, buffer, cu, &bytes_read);
6004 buffer += bytes_read;
6005
6006 /* Did we find the end of the range list? */
6007 if (start == 0 && end == 0)
6008 break;
6009
6010 /* Did we find a base address selection entry? */
6011 else if ((start & base_select_mask) == base_select_mask)
6012 {
6013 base = end;
6014 base_known = 1;
6015 }
6016
6017 /* We found an ordinary address range. */
6018 else
6019 {
6020 if (!base_known)
6021 {
6022 complaint (&symfile_complaints,
6023 _("Invalid .debug_ranges data (no base address)"));
6024 return;
6025 }
6026
6e70227d
DE
6027 record_block_range (block,
6028 baseaddr + base + start,
801e3a5b
JB
6029 baseaddr + base + end - 1);
6030 }
6031 }
6032 }
6033}
6034
c906108c
SS
6035/* Add an aggregate field to the field list. */
6036
6037static void
107d2387 6038dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6039 struct dwarf2_cu *cu)
6e70227d 6040{
e7c27a73 6041 struct objfile *objfile = cu->objfile;
5e2b427d 6042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6043 struct nextfield *new_field;
6044 struct attribute *attr;
6045 struct field *fp;
6046 char *fieldname = "";
6047
6048 /* Allocate a new field list entry and link it in. */
6049 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6050 make_cleanup (xfree, new_field);
c906108c 6051 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6052
6053 if (die->tag == DW_TAG_inheritance)
6054 {
6055 new_field->next = fip->baseclasses;
6056 fip->baseclasses = new_field;
6057 }
6058 else
6059 {
6060 new_field->next = fip->fields;
6061 fip->fields = new_field;
6062 }
c906108c
SS
6063 fip->nfields++;
6064
6065 /* Handle accessibility and virtuality of field.
6066 The default accessibility for members is public, the default
6067 accessibility for inheritance is private. */
6068 if (die->tag != DW_TAG_inheritance)
6069 new_field->accessibility = DW_ACCESS_public;
6070 else
6071 new_field->accessibility = DW_ACCESS_private;
6072 new_field->virtuality = DW_VIRTUALITY_none;
6073
e142c38c 6074 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6075 if (attr)
6076 new_field->accessibility = DW_UNSND (attr);
6077 if (new_field->accessibility != DW_ACCESS_public)
6078 fip->non_public_fields = 1;
e142c38c 6079 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6080 if (attr)
6081 new_field->virtuality = DW_UNSND (attr);
6082
6083 fp = &new_field->field;
a9a9bd0f 6084
e142c38c 6085 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6086 {
a9a9bd0f 6087 /* Data member other than a C++ static data member. */
6e70227d 6088
c906108c 6089 /* Get type of field. */
e7c27a73 6090 fp->type = die_type (die, cu);
c906108c 6091
d6a843b5 6092 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6093
c906108c 6094 /* Get bit size of field (zero if none). */
e142c38c 6095 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6096 if (attr)
6097 {
6098 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6099 }
6100 else
6101 {
6102 FIELD_BITSIZE (*fp) = 0;
6103 }
6104
6105 /* Get bit offset of field. */
e142c38c 6106 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6107 if (attr)
6108 {
d4b96c9a 6109 int byte_offset = 0;
c6a0999f 6110
3690dd37 6111 if (attr_form_is_section_offset (attr))
d4b96c9a 6112 dwarf2_complex_location_expr_complaint ();
3690dd37 6113 else if (attr_form_is_constant (attr))
c6a0999f 6114 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6115 else if (attr_form_is_block (attr))
c6a0999f 6116 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6117 else
6118 dwarf2_complex_location_expr_complaint ();
c6a0999f 6119
d6a843b5 6120 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6121 }
e142c38c 6122 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6123 if (attr)
6124 {
5e2b427d 6125 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6126 {
6127 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6128 additional bit offset from the MSB of the containing
6129 anonymous object to the MSB of the field. We don't
6130 have to do anything special since we don't need to
6131 know the size of the anonymous object. */
c906108c
SS
6132 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6133 }
6134 else
6135 {
6136 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6137 MSB of the anonymous object, subtract off the number of
6138 bits from the MSB of the field to the MSB of the
6139 object, and then subtract off the number of bits of
6140 the field itself. The result is the bit offset of
6141 the LSB of the field. */
c906108c
SS
6142 int anonymous_size;
6143 int bit_offset = DW_UNSND (attr);
6144
e142c38c 6145 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6146 if (attr)
6147 {
6148 /* The size of the anonymous object containing
6149 the bit field is explicit, so use the
6150 indicated size (in bytes). */
6151 anonymous_size = DW_UNSND (attr);
6152 }
6153 else
6154 {
6155 /* The size of the anonymous object containing
6156 the bit field must be inferred from the type
6157 attribute of the data member containing the
6158 bit field. */
6159 anonymous_size = TYPE_LENGTH (fp->type);
6160 }
6161 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6162 - bit_offset - FIELD_BITSIZE (*fp);
6163 }
6164 }
6165
6166 /* Get name of field. */
39cbfefa
DJ
6167 fieldname = dwarf2_name (die, cu);
6168 if (fieldname == NULL)
6169 fieldname = "";
d8151005
DJ
6170
6171 /* The name is already allocated along with this objfile, so we don't
6172 need to duplicate it for the type. */
6173 fp->name = fieldname;
c906108c
SS
6174
6175 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6176 pointer or virtual base class pointer) to private. */
e142c38c 6177 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6178 {
d48cc9dd 6179 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6180 new_field->accessibility = DW_ACCESS_private;
6181 fip->non_public_fields = 1;
6182 }
6183 }
a9a9bd0f 6184 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6185 {
a9a9bd0f
DC
6186 /* C++ static member. */
6187
6188 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6189 is a declaration, but all versions of G++ as of this writing
6190 (so through at least 3.2.1) incorrectly generate
6191 DW_TAG_variable tags. */
6e70227d 6192
c906108c 6193 char *physname;
c906108c 6194
a9a9bd0f 6195 /* Get name of field. */
39cbfefa
DJ
6196 fieldname = dwarf2_name (die, cu);
6197 if (fieldname == NULL)
c906108c
SS
6198 return;
6199
254e6b9e 6200 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6201 if (attr
6202 /* Only create a symbol if this is an external value.
6203 new_symbol checks this and puts the value in the global symbol
6204 table, which we want. If it is not external, new_symbol
6205 will try to put the value in cu->list_in_scope which is wrong. */
6206 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6207 {
6208 /* A static const member, not much different than an enum as far as
6209 we're concerned, except that we can support more types. */
6210 new_symbol (die, NULL, cu);
6211 }
6212
2df3850c 6213 /* Get physical name. */
94af9270 6214 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6215
d8151005
DJ
6216 /* The name is already allocated along with this objfile, so we don't
6217 need to duplicate it for the type. */
6218 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6219 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6220 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6221 }
6222 else if (die->tag == DW_TAG_inheritance)
6223 {
6224 /* C++ base class field. */
e142c38c 6225 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6226 if (attr)
d4b96c9a
JK
6227 {
6228 int byte_offset = 0;
6229
6230 if (attr_form_is_section_offset (attr))
6231 dwarf2_complex_location_expr_complaint ();
6232 else if (attr_form_is_constant (attr))
6233 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6234 else if (attr_form_is_block (attr))
6235 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6236 else
6237 dwarf2_complex_location_expr_complaint ();
6238
6239 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6240 }
c906108c 6241 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6242 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6243 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6244 fip->nbaseclasses++;
6245 }
6246}
6247
98751a41
JK
6248/* Add a typedef defined in the scope of the FIP's class. */
6249
6250static void
6251dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6252 struct dwarf2_cu *cu)
6e70227d 6253{
98751a41
JK
6254 struct objfile *objfile = cu->objfile;
6255 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6256 struct typedef_field_list *new_field;
6257 struct attribute *attr;
6258 struct typedef_field *fp;
6259 char *fieldname = "";
6260
6261 /* Allocate a new field list entry and link it in. */
6262 new_field = xzalloc (sizeof (*new_field));
6263 make_cleanup (xfree, new_field);
6264
6265 gdb_assert (die->tag == DW_TAG_typedef);
6266
6267 fp = &new_field->field;
6268
6269 /* Get name of field. */
6270 fp->name = dwarf2_name (die, cu);
6271 if (fp->name == NULL)
6272 return;
6273
6274 fp->type = read_type_die (die, cu);
6275
6276 new_field->next = fip->typedef_field_list;
6277 fip->typedef_field_list = new_field;
6278 fip->typedef_field_list_count++;
6279}
6280
c906108c
SS
6281/* Create the vector of fields, and attach it to the type. */
6282
6283static void
fba45db2 6284dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6285 struct dwarf2_cu *cu)
c906108c
SS
6286{
6287 int nfields = fip->nfields;
6288
6289 /* Record the field count, allocate space for the array of fields,
6290 and create blank accessibility bitfields if necessary. */
6291 TYPE_NFIELDS (type) = nfields;
6292 TYPE_FIELDS (type) = (struct field *)
6293 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6294 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6295
b4ba55a1 6296 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6297 {
6298 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6299
6300 TYPE_FIELD_PRIVATE_BITS (type) =
6301 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6302 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6303
6304 TYPE_FIELD_PROTECTED_BITS (type) =
6305 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6306 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6307
8b93c54c
TT
6308 /* We don't set TYPE_FIELD_IGNORE_BITS here. The DWARF reader
6309 never sets any bits in that array, so leaving it NULL lets us
6310 save a little memory. */
c906108c
SS
6311 }
6312
6313 /* If the type has baseclasses, allocate and clear a bit vector for
6314 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6315 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6316 {
6317 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6318 unsigned char *pointer;
c906108c
SS
6319
6320 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6321 pointer = TYPE_ALLOC (type, num_bytes);
6322 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6323 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6324 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6325 }
6326
6327 /* Copy the saved-up fields into the field vector. Start from the head
6328 of the list, adding to the tail of the field array, so that they end
6329 up in the same order in the array in which they were added to the list. */
6330 while (nfields-- > 0)
6331 {
7d0ccb61
DJ
6332 struct nextfield *fieldp;
6333
6334 if (fip->fields)
6335 {
6336 fieldp = fip->fields;
6337 fip->fields = fieldp->next;
6338 }
6339 else
6340 {
6341 fieldp = fip->baseclasses;
6342 fip->baseclasses = fieldp->next;
6343 }
6344
6345 TYPE_FIELD (type, nfields) = fieldp->field;
6346 switch (fieldp->accessibility)
c906108c 6347 {
c5aa993b 6348 case DW_ACCESS_private:
b4ba55a1
JB
6349 if (cu->language != language_ada)
6350 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6351 break;
c906108c 6352
c5aa993b 6353 case DW_ACCESS_protected:
b4ba55a1
JB
6354 if (cu->language != language_ada)
6355 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6356 break;
c906108c 6357
c5aa993b
JM
6358 case DW_ACCESS_public:
6359 break;
c906108c 6360
c5aa993b
JM
6361 default:
6362 /* Unknown accessibility. Complain and treat it as public. */
6363 {
e2e0b3e5 6364 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6365 fieldp->accessibility);
c5aa993b
JM
6366 }
6367 break;
c906108c
SS
6368 }
6369 if (nfields < fip->nbaseclasses)
6370 {
7d0ccb61 6371 switch (fieldp->virtuality)
c906108c 6372 {
c5aa993b
JM
6373 case DW_VIRTUALITY_virtual:
6374 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6375 if (cu->language == language_ada)
6376 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6377 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6378 break;
c906108c
SS
6379 }
6380 }
c906108c
SS
6381 }
6382}
6383
c906108c
SS
6384/* Add a member function to the proper fieldlist. */
6385
6386static void
107d2387 6387dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6388 struct type *type, struct dwarf2_cu *cu)
c906108c 6389{
e7c27a73 6390 struct objfile *objfile = cu->objfile;
c906108c
SS
6391 struct attribute *attr;
6392 struct fnfieldlist *flp;
6393 int i;
6394 struct fn_field *fnp;
6395 char *fieldname;
c906108c 6396 struct nextfnfield *new_fnfield;
f792889a 6397 struct type *this_type;
c906108c 6398
b4ba55a1
JB
6399 if (cu->language == language_ada)
6400 error ("unexpected member function in Ada type");
6401
2df3850c 6402 /* Get name of member function. */
39cbfefa
DJ
6403 fieldname = dwarf2_name (die, cu);
6404 if (fieldname == NULL)
2df3850c 6405 return;
c906108c 6406
c906108c
SS
6407 /* Look up member function name in fieldlist. */
6408 for (i = 0; i < fip->nfnfields; i++)
6409 {
27bfe10e 6410 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6411 break;
6412 }
6413
6414 /* Create new list element if necessary. */
6415 if (i < fip->nfnfields)
6416 flp = &fip->fnfieldlists[i];
6417 else
6418 {
6419 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6420 {
6421 fip->fnfieldlists = (struct fnfieldlist *)
6422 xrealloc (fip->fnfieldlists,
6423 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6424 * sizeof (struct fnfieldlist));
c906108c 6425 if (fip->nfnfields == 0)
c13c43fd 6426 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6427 }
6428 flp = &fip->fnfieldlists[fip->nfnfields];
6429 flp->name = fieldname;
6430 flp->length = 0;
6431 flp->head = NULL;
3da10d80 6432 i = fip->nfnfields++;
c906108c
SS
6433 }
6434
6435 /* Create a new member function field and chain it to the field list
6436 entry. */
6437 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6438 make_cleanup (xfree, new_fnfield);
c906108c
SS
6439 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6440 new_fnfield->next = flp->head;
6441 flp->head = new_fnfield;
6442 flp->length++;
6443
6444 /* Fill in the member function field info. */
6445 fnp = &new_fnfield->fnfield;
3da10d80
KS
6446
6447 /* Delay processing of the physname until later. */
6448 if (cu->language == language_cplus || cu->language == language_java)
6449 {
6450 add_to_method_list (type, i, flp->length - 1, fieldname,
6451 die, cu);
6452 }
6453 else
6454 {
6455 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6456 fnp->physname = physname ? physname : "";
6457 }
6458
c906108c 6459 fnp->type = alloc_type (objfile);
f792889a
DJ
6460 this_type = read_type_die (die, cu);
6461 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6462 {
f792889a 6463 int nparams = TYPE_NFIELDS (this_type);
c906108c 6464
f792889a 6465 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6466 of the method itself (TYPE_CODE_METHOD). */
6467 smash_to_method_type (fnp->type, type,
f792889a
DJ
6468 TYPE_TARGET_TYPE (this_type),
6469 TYPE_FIELDS (this_type),
6470 TYPE_NFIELDS (this_type),
6471 TYPE_VARARGS (this_type));
c906108c
SS
6472
6473 /* Handle static member functions.
c5aa993b
JM
6474 Dwarf2 has no clean way to discern C++ static and non-static
6475 member functions. G++ helps GDB by marking the first
6476 parameter for non-static member functions (which is the
6477 this pointer) as artificial. We obtain this information
6478 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6479 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6480 fnp->voffset = VOFFSET_STATIC;
6481 }
6482 else
e2e0b3e5 6483 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6484 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6485
6486 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6487 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6488 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6489
6490 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6491 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6492
6493 /* Get accessibility. */
e142c38c 6494 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6495 if (attr)
6496 {
6497 switch (DW_UNSND (attr))
6498 {
c5aa993b
JM
6499 case DW_ACCESS_private:
6500 fnp->is_private = 1;
6501 break;
6502 case DW_ACCESS_protected:
6503 fnp->is_protected = 1;
6504 break;
c906108c
SS
6505 }
6506 }
6507
b02dede2 6508 /* Check for artificial methods. */
e142c38c 6509 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6510 if (attr && DW_UNSND (attr) != 0)
6511 fnp->is_artificial = 1;
6512
0d564a31 6513 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6514 function. For older versions of GCC, this is an offset in the
6515 appropriate virtual table, as specified by DW_AT_containing_type.
6516 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6517 to the object address. */
6518
e142c38c 6519 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6520 if (attr)
8e19ed76 6521 {
aec5aa8b 6522 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6523 {
aec5aa8b
TT
6524 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6525 {
6526 /* Old-style GCC. */
6527 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6528 }
6529 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6530 || (DW_BLOCK (attr)->size > 1
6531 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6532 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6533 {
6534 struct dwarf_block blk;
6535 int offset;
6536
6537 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6538 ? 1 : 2);
6539 blk.size = DW_BLOCK (attr)->size - offset;
6540 blk.data = DW_BLOCK (attr)->data + offset;
6541 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6542 if ((fnp->voffset % cu->header.addr_size) != 0)
6543 dwarf2_complex_location_expr_complaint ();
6544 else
6545 fnp->voffset /= cu->header.addr_size;
6546 fnp->voffset += 2;
6547 }
6548 else
6549 dwarf2_complex_location_expr_complaint ();
6550
6551 if (!fnp->fcontext)
6552 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6553 }
3690dd37 6554 else if (attr_form_is_section_offset (attr))
8e19ed76 6555 {
4d3c2250 6556 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6557 }
6558 else
6559 {
4d3c2250
KB
6560 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6561 fieldname);
8e19ed76 6562 }
0d564a31 6563 }
d48cc9dd
DJ
6564 else
6565 {
6566 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6567 if (attr && DW_UNSND (attr))
6568 {
6569 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6570 complaint (&symfile_complaints,
6571 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6572 fieldname, die->offset);
9655fd1a 6573 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6574 TYPE_CPLUS_DYNAMIC (type) = 1;
6575 }
6576 }
c906108c
SS
6577}
6578
6579/* Create the vector of member function fields, and attach it to the type. */
6580
6581static void
fba45db2 6582dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6583 struct dwarf2_cu *cu)
c906108c
SS
6584{
6585 struct fnfieldlist *flp;
6586 int total_length = 0;
6587 int i;
6588
b4ba55a1
JB
6589 if (cu->language == language_ada)
6590 error ("unexpected member functions in Ada type");
6591
c906108c
SS
6592 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6593 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6594 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6595
6596 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6597 {
6598 struct nextfnfield *nfp = flp->head;
6599 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6600 int k;
6601
6602 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6603 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6604 fn_flp->fn_fields = (struct fn_field *)
6605 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6606 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6607 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6608
6609 total_length += flp->length;
6610 }
6611
6612 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6613 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6614}
6615
1168df01
JB
6616/* Returns non-zero if NAME is the name of a vtable member in CU's
6617 language, zero otherwise. */
6618static int
6619is_vtable_name (const char *name, struct dwarf2_cu *cu)
6620{
6621 static const char vptr[] = "_vptr";
987504bb 6622 static const char vtable[] = "vtable";
1168df01 6623
987504bb
JJ
6624 /* Look for the C++ and Java forms of the vtable. */
6625 if ((cu->language == language_java
6626 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6627 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6628 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6629 return 1;
6630
6631 return 0;
6632}
6633
c0dd20ea 6634/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6635 functions, with the ABI-specified layout. If TYPE describes
6636 such a structure, smash it into a member function type.
61049d3b
DJ
6637
6638 GCC shouldn't do this; it should just output pointer to member DIEs.
6639 This is GCC PR debug/28767. */
c0dd20ea 6640
0b92b5bb
TT
6641static void
6642quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6643{
0b92b5bb 6644 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6645
6646 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6647 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6648 return;
c0dd20ea
DJ
6649
6650 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6651 if (TYPE_FIELD_NAME (type, 0) == NULL
6652 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6653 || TYPE_FIELD_NAME (type, 1) == NULL
6654 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6655 return;
c0dd20ea
DJ
6656
6657 /* Find the type of the method. */
0b92b5bb 6658 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6659 if (pfn_type == NULL
6660 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6661 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6662 return;
c0dd20ea
DJ
6663
6664 /* Look for the "this" argument. */
6665 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6666 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6667 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6668 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6669 return;
c0dd20ea
DJ
6670
6671 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6672 new_type = alloc_type (objfile);
6673 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6674 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6675 TYPE_VARARGS (pfn_type));
0b92b5bb 6676 smash_to_methodptr_type (type, new_type);
c0dd20ea 6677}
1168df01 6678
c906108c
SS
6679/* Called when we find the DIE that starts a structure or union scope
6680 (definition) to process all dies that define the members of the
6681 structure or union.
6682
6683 NOTE: we need to call struct_type regardless of whether or not the
6684 DIE has an at_name attribute, since it might be an anonymous
6685 structure or union. This gets the type entered into our set of
6686 user defined types.
6687
6688 However, if the structure is incomplete (an opaque struct/union)
6689 then suppress creating a symbol table entry for it since gdb only
6690 wants to find the one with the complete definition. Note that if
6691 it is complete, we just call new_symbol, which does it's own
6692 checking about whether the struct/union is anonymous or not (and
6693 suppresses creating a symbol table entry itself). */
6694
f792889a 6695static struct type *
134d01f1 6696read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6697{
e7c27a73 6698 struct objfile *objfile = cu->objfile;
c906108c
SS
6699 struct type *type;
6700 struct attribute *attr;
39cbfefa 6701 char *name;
d3f41bb1 6702 struct cleanup *back_to;
c906108c 6703
348e048f
DE
6704 /* If the definition of this type lives in .debug_types, read that type.
6705 Don't follow DW_AT_specification though, that will take us back up
6706 the chain and we want to go down. */
6707 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6708 if (attr)
6709 {
6710 struct dwarf2_cu *type_cu = cu;
6711 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6712
348e048f
DE
6713 /* We could just recurse on read_structure_type, but we need to call
6714 get_die_type to ensure only one type for this DIE is created.
6715 This is important, for example, because for c++ classes we need
6716 TYPE_NAME set which is only done by new_symbol. Blech. */
6717 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6718
6719 /* TYPE_CU may not be the same as CU.
6720 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6721 return set_die_type (die, type, cu);
6722 }
6723
d3f41bb1
TT
6724 back_to = make_cleanup (null_cleanup, 0);
6725
c0dd20ea 6726 type = alloc_type (objfile);
c906108c 6727 INIT_CPLUS_SPECIFIC (type);
93311388 6728
39cbfefa
DJ
6729 name = dwarf2_name (die, cu);
6730 if (name != NULL)
c906108c 6731 {
987504bb
JJ
6732 if (cu->language == language_cplus
6733 || cu->language == language_java)
63d06c5c 6734 {
3da10d80
KS
6735 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6736
6737 /* dwarf2_full_name might have already finished building the DIE's
6738 type. If so, there is no need to continue. */
6739 if (get_die_type (die, cu) != NULL)
6740 return get_die_type (die, cu);
6741
6742 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6743 if (die->tag == DW_TAG_structure_type
6744 || die->tag == DW_TAG_class_type)
6745 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6746 }
6747 else
6748 {
d8151005
DJ
6749 /* The name is already allocated along with this objfile, so
6750 we don't need to duplicate it for the type. */
94af9270
KS
6751 TYPE_TAG_NAME (type) = (char *) name;
6752 if (die->tag == DW_TAG_class_type)
6753 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6754 }
c906108c
SS
6755 }
6756
6757 if (die->tag == DW_TAG_structure_type)
6758 {
6759 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6760 }
6761 else if (die->tag == DW_TAG_union_type)
6762 {
6763 TYPE_CODE (type) = TYPE_CODE_UNION;
6764 }
6765 else
6766 {
c906108c
SS
6767 TYPE_CODE (type) = TYPE_CODE_CLASS;
6768 }
6769
0cc2414c
TT
6770 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6771 TYPE_DECLARED_CLASS (type) = 1;
6772
e142c38c 6773 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6774 if (attr)
6775 {
6776 TYPE_LENGTH (type) = DW_UNSND (attr);
6777 }
6778 else
6779 {
6780 TYPE_LENGTH (type) = 0;
6781 }
6782
876cecd0 6783 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6784 if (die_is_declaration (die, cu))
876cecd0 6785 TYPE_STUB (type) = 1;
a6c727b2
DJ
6786 else if (attr == NULL && die->child == NULL
6787 && producer_is_realview (cu->producer))
6788 /* RealView does not output the required DW_AT_declaration
6789 on incomplete types. */
6790 TYPE_STUB (type) = 1;
dc718098 6791
c906108c
SS
6792 /* We need to add the type field to the die immediately so we don't
6793 infinitely recurse when dealing with pointers to the structure
6794 type within the structure itself. */
1c379e20 6795 set_die_type (die, type, cu);
c906108c 6796
7e314c57
JK
6797 /* set_die_type should be already done. */
6798 set_descriptive_type (type, die, cu);
6799
e142c38c 6800 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6801 {
6802 struct field_info fi;
6803 struct die_info *child_die;
34eaf542 6804 VEC (symbolp) *template_args = NULL;
c906108c
SS
6805
6806 memset (&fi, 0, sizeof (struct field_info));
6807
639d11d3 6808 child_die = die->child;
c906108c
SS
6809
6810 while (child_die && child_die->tag)
6811 {
a9a9bd0f
DC
6812 if (child_die->tag == DW_TAG_member
6813 || child_die->tag == DW_TAG_variable)
c906108c 6814 {
a9a9bd0f
DC
6815 /* NOTE: carlton/2002-11-05: A C++ static data member
6816 should be a DW_TAG_member that is a declaration, but
6817 all versions of G++ as of this writing (so through at
6818 least 3.2.1) incorrectly generate DW_TAG_variable
6819 tags for them instead. */
e7c27a73 6820 dwarf2_add_field (&fi, child_die, cu);
c906108c 6821 }
8713b1b1 6822 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6823 {
6824 /* C++ member function. */
e7c27a73 6825 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6826 }
6827 else if (child_die->tag == DW_TAG_inheritance)
6828 {
6829 /* C++ base class field. */
e7c27a73 6830 dwarf2_add_field (&fi, child_die, cu);
c906108c 6831 }
98751a41
JK
6832 else if (child_die->tag == DW_TAG_typedef)
6833 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6834 else if (child_die->tag == DW_TAG_template_type_param
6835 || child_die->tag == DW_TAG_template_value_param)
6836 {
6837 struct symbol *arg = new_symbol (child_die, NULL, cu);
6838
6839 VEC_safe_push (symbolp, template_args, arg);
6840 }
6841
c906108c
SS
6842 child_die = sibling_die (child_die);
6843 }
6844
34eaf542
TT
6845 /* Attach template arguments to type. */
6846 if (! VEC_empty (symbolp, template_args))
6847 {
6848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6849 TYPE_N_TEMPLATE_ARGUMENTS (type)
6850 = VEC_length (symbolp, template_args);
6851 TYPE_TEMPLATE_ARGUMENTS (type)
6852 = obstack_alloc (&objfile->objfile_obstack,
6853 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6854 * sizeof (struct symbol *)));
6855 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6856 VEC_address (symbolp, template_args),
6857 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6858 * sizeof (struct symbol *)));
6859 VEC_free (symbolp, template_args);
6860 }
6861
c906108c
SS
6862 /* Attach fields and member functions to the type. */
6863 if (fi.nfields)
e7c27a73 6864 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6865 if (fi.nfnfields)
6866 {
e7c27a73 6867 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6868
c5aa993b 6869 /* Get the type which refers to the base class (possibly this
c906108c 6870 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6871 class from the DW_AT_containing_type attribute. This use of
6872 DW_AT_containing_type is a GNU extension. */
c906108c 6873
e142c38c 6874 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6875 {
e7c27a73 6876 struct type *t = die_containing_type (die, cu);
c906108c
SS
6877
6878 TYPE_VPTR_BASETYPE (type) = t;
6879 if (type == t)
6880 {
c906108c
SS
6881 int i;
6882
6883 /* Our own class provides vtbl ptr. */
6884 for (i = TYPE_NFIELDS (t) - 1;
6885 i >= TYPE_N_BASECLASSES (t);
6886 --i)
6887 {
6888 char *fieldname = TYPE_FIELD_NAME (t, i);
6889
1168df01 6890 if (is_vtable_name (fieldname, cu))
c906108c
SS
6891 {
6892 TYPE_VPTR_FIELDNO (type) = i;
6893 break;
6894 }
6895 }
6896
6897 /* Complain if virtual function table field not found. */
6898 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6899 complaint (&symfile_complaints,
e2e0b3e5 6900 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6901 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6902 "");
c906108c
SS
6903 }
6904 else
6905 {
6906 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6907 }
6908 }
f6235d4c
EZ
6909 else if (cu->producer
6910 && strncmp (cu->producer,
6911 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6912 {
6913 /* The IBM XLC compiler does not provide direct indication
6914 of the containing type, but the vtable pointer is
6915 always named __vfp. */
6916
6917 int i;
6918
6919 for (i = TYPE_NFIELDS (type) - 1;
6920 i >= TYPE_N_BASECLASSES (type);
6921 --i)
6922 {
6923 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6924 {
6925 TYPE_VPTR_FIELDNO (type) = i;
6926 TYPE_VPTR_BASETYPE (type) = type;
6927 break;
6928 }
6929 }
6930 }
c906108c 6931 }
98751a41
JK
6932
6933 /* Copy fi.typedef_field_list linked list elements content into the
6934 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6935 if (fi.typedef_field_list)
6936 {
6937 int i = fi.typedef_field_list_count;
6938
a0d7a4ff 6939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6940 TYPE_TYPEDEF_FIELD_ARRAY (type)
6941 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6942 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6943
6944 /* Reverse the list order to keep the debug info elements order. */
6945 while (--i >= 0)
6946 {
6947 struct typedef_field *dest, *src;
6e70227d 6948
98751a41
JK
6949 dest = &TYPE_TYPEDEF_FIELD (type, i);
6950 src = &fi.typedef_field_list->field;
6951 fi.typedef_field_list = fi.typedef_field_list->next;
6952 *dest = *src;
6953 }
6954 }
c906108c 6955 }
63d06c5c 6956
0b92b5bb
TT
6957 quirk_gcc_member_function_pointer (type, cu->objfile);
6958
0114d602 6959 do_cleanups (back_to);
f792889a 6960 return type;
c906108c
SS
6961}
6962
134d01f1
DJ
6963static void
6964process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6965{
90aeadfc 6966 struct die_info *child_die = die->child;
f792889a 6967 struct type *this_type;
c906108c 6968
f792889a
DJ
6969 this_type = get_die_type (die, cu);
6970 if (this_type == NULL)
6971 this_type = read_structure_type (die, cu);
c906108c 6972
90aeadfc
DC
6973 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6974 snapshots) has been known to create a die giving a declaration
6975 for a class that has, as a child, a die giving a definition for a
6976 nested class. So we have to process our children even if the
6977 current die is a declaration. Normally, of course, a declaration
6978 won't have any children at all. */
134d01f1 6979
90aeadfc
DC
6980 while (child_die != NULL && child_die->tag)
6981 {
6982 if (child_die->tag == DW_TAG_member
6983 || child_die->tag == DW_TAG_variable
34eaf542
TT
6984 || child_die->tag == DW_TAG_inheritance
6985 || child_die->tag == DW_TAG_template_value_param
6986 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6987 {
90aeadfc 6988 /* Do nothing. */
134d01f1 6989 }
90aeadfc
DC
6990 else
6991 process_die (child_die, cu);
134d01f1 6992
90aeadfc 6993 child_die = sibling_die (child_die);
134d01f1
DJ
6994 }
6995
fa4028e9
JB
6996 /* Do not consider external references. According to the DWARF standard,
6997 these DIEs are identified by the fact that they have no byte_size
6998 attribute, and a declaration attribute. */
6999 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7000 || !die_is_declaration (die, cu))
f792889a 7001 new_symbol (die, this_type, cu);
134d01f1
DJ
7002}
7003
7004/* Given a DW_AT_enumeration_type die, set its type. We do not
7005 complete the type's fields yet, or create any symbols. */
c906108c 7006
f792889a 7007static struct type *
134d01f1 7008read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7009{
e7c27a73 7010 struct objfile *objfile = cu->objfile;
c906108c 7011 struct type *type;
c906108c 7012 struct attribute *attr;
0114d602 7013 const char *name;
134d01f1 7014
348e048f
DE
7015 /* If the definition of this type lives in .debug_types, read that type.
7016 Don't follow DW_AT_specification though, that will take us back up
7017 the chain and we want to go down. */
7018 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7019 if (attr)
7020 {
7021 struct dwarf2_cu *type_cu = cu;
7022 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7023
348e048f 7024 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7025
7026 /* TYPE_CU may not be the same as CU.
7027 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7028 return set_die_type (die, type, cu);
7029 }
7030
c906108c
SS
7031 type = alloc_type (objfile);
7032
7033 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7034 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7035 if (name != NULL)
0114d602 7036 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7037
e142c38c 7038 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7039 if (attr)
7040 {
7041 TYPE_LENGTH (type) = DW_UNSND (attr);
7042 }
7043 else
7044 {
7045 TYPE_LENGTH (type) = 0;
7046 }
7047
137033e9
JB
7048 /* The enumeration DIE can be incomplete. In Ada, any type can be
7049 declared as private in the package spec, and then defined only
7050 inside the package body. Such types are known as Taft Amendment
7051 Types. When another package uses such a type, an incomplete DIE
7052 may be generated by the compiler. */
02eb380e 7053 if (die_is_declaration (die, cu))
876cecd0 7054 TYPE_STUB (type) = 1;
02eb380e 7055
f792889a 7056 return set_die_type (die, type, cu);
134d01f1
DJ
7057}
7058
7059/* Given a pointer to a die which begins an enumeration, process all
7060 the dies that define the members of the enumeration, and create the
7061 symbol for the enumeration type.
7062
7063 NOTE: We reverse the order of the element list. */
7064
7065static void
7066process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7067{
f792889a 7068 struct type *this_type;
134d01f1 7069
f792889a
DJ
7070 this_type = get_die_type (die, cu);
7071 if (this_type == NULL)
7072 this_type = read_enumeration_type (die, cu);
9dc481d3 7073
639d11d3 7074 if (die->child != NULL)
c906108c 7075 {
9dc481d3
DE
7076 struct die_info *child_die;
7077 struct symbol *sym;
7078 struct field *fields = NULL;
7079 int num_fields = 0;
7080 int unsigned_enum = 1;
7081 char *name;
7082
639d11d3 7083 child_die = die->child;
c906108c
SS
7084 while (child_die && child_die->tag)
7085 {
7086 if (child_die->tag != DW_TAG_enumerator)
7087 {
e7c27a73 7088 process_die (child_die, cu);
c906108c
SS
7089 }
7090 else
7091 {
39cbfefa
DJ
7092 name = dwarf2_name (child_die, cu);
7093 if (name)
c906108c 7094 {
f792889a 7095 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7096 if (SYMBOL_VALUE (sym) < 0)
7097 unsigned_enum = 0;
7098
7099 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7100 {
7101 fields = (struct field *)
7102 xrealloc (fields,
7103 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7104 * sizeof (struct field));
c906108c
SS
7105 }
7106
3567439c 7107 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7108 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7109 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7110 FIELD_BITSIZE (fields[num_fields]) = 0;
7111
7112 num_fields++;
7113 }
7114 }
7115
7116 child_die = sibling_die (child_die);
7117 }
7118
7119 if (num_fields)
7120 {
f792889a
DJ
7121 TYPE_NFIELDS (this_type) = num_fields;
7122 TYPE_FIELDS (this_type) = (struct field *)
7123 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7124 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7125 sizeof (struct field) * num_fields);
b8c9b27d 7126 xfree (fields);
c906108c
SS
7127 }
7128 if (unsigned_enum)
876cecd0 7129 TYPE_UNSIGNED (this_type) = 1;
c906108c 7130 }
134d01f1 7131
f792889a 7132 new_symbol (die, this_type, cu);
c906108c
SS
7133}
7134
7135/* Extract all information from a DW_TAG_array_type DIE and put it in
7136 the DIE's type field. For now, this only handles one dimensional
7137 arrays. */
7138
f792889a 7139static struct type *
e7c27a73 7140read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7141{
e7c27a73 7142 struct objfile *objfile = cu->objfile;
c906108c 7143 struct die_info *child_die;
7e314c57 7144 struct type *type;
c906108c
SS
7145 struct type *element_type, *range_type, *index_type;
7146 struct type **range_types = NULL;
7147 struct attribute *attr;
7148 int ndim = 0;
7149 struct cleanup *back_to;
39cbfefa 7150 char *name;
c906108c 7151
e7c27a73 7152 element_type = die_type (die, cu);
c906108c 7153
7e314c57
JK
7154 /* The die_type call above may have already set the type for this DIE. */
7155 type = get_die_type (die, cu);
7156 if (type)
7157 return type;
7158
c906108c
SS
7159 /* Irix 6.2 native cc creates array types without children for
7160 arrays with unspecified length. */
639d11d3 7161 if (die->child == NULL)
c906108c 7162 {
46bf5051 7163 index_type = objfile_type (objfile)->builtin_int;
c906108c 7164 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7165 type = create_array_type (NULL, element_type, range_type);
7166 return set_die_type (die, type, cu);
c906108c
SS
7167 }
7168
7169 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7170 child_die = die->child;
c906108c
SS
7171 while (child_die && child_die->tag)
7172 {
7173 if (child_die->tag == DW_TAG_subrange_type)
7174 {
f792889a 7175 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7176
f792889a 7177 if (child_type != NULL)
a02abb62
JB
7178 {
7179 /* The range type was succesfully read. Save it for
7180 the array type creation. */
7181 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7182 {
7183 range_types = (struct type **)
7184 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7185 * sizeof (struct type *));
7186 if (ndim == 0)
7187 make_cleanup (free_current_contents, &range_types);
7188 }
f792889a 7189 range_types[ndim++] = child_type;
a02abb62 7190 }
c906108c
SS
7191 }
7192 child_die = sibling_die (child_die);
7193 }
7194
7195 /* Dwarf2 dimensions are output from left to right, create the
7196 necessary array types in backwards order. */
7ca2d3a3 7197
c906108c 7198 type = element_type;
7ca2d3a3
DL
7199
7200 if (read_array_order (die, cu) == DW_ORD_col_major)
7201 {
7202 int i = 0;
9a619af0 7203
7ca2d3a3
DL
7204 while (i < ndim)
7205 type = create_array_type (NULL, type, range_types[i++]);
7206 }
7207 else
7208 {
7209 while (ndim-- > 0)
7210 type = create_array_type (NULL, type, range_types[ndim]);
7211 }
c906108c 7212
f5f8a009
EZ
7213 /* Understand Dwarf2 support for vector types (like they occur on
7214 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7215 array type. This is not part of the Dwarf2/3 standard yet, but a
7216 custom vendor extension. The main difference between a regular
7217 array and the vector variant is that vectors are passed by value
7218 to functions. */
e142c38c 7219 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7220 if (attr)
ea37ba09 7221 make_vector_type (type);
f5f8a009 7222
39cbfefa
DJ
7223 name = dwarf2_name (die, cu);
7224 if (name)
7225 TYPE_NAME (type) = name;
6e70227d 7226
7e314c57
JK
7227 /* Install the type in the die. */
7228 set_die_type (die, type, cu);
7229
7230 /* set_die_type should be already done. */
b4ba55a1
JB
7231 set_descriptive_type (type, die, cu);
7232
c906108c
SS
7233 do_cleanups (back_to);
7234
7e314c57 7235 return type;
c906108c
SS
7236}
7237
7ca2d3a3 7238static enum dwarf_array_dim_ordering
6e70227d 7239read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7240{
7241 struct attribute *attr;
7242
7243 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7244
7245 if (attr) return DW_SND (attr);
7246
7247 /*
7248 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7249 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7250 laid out as per normal fortran.
7251
6e70227d 7252 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7253 version checking.
7254 */
7255
905e0470
PM
7256 if (cu->language == language_fortran
7257 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7258 {
7259 return DW_ORD_row_major;
7260 }
7261
6e70227d 7262 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7263 {
7264 case array_column_major:
7265 return DW_ORD_col_major;
7266 case array_row_major:
7267 default:
7268 return DW_ORD_row_major;
7269 };
7270}
7271
72019c9c
GM
7272/* Extract all information from a DW_TAG_set_type DIE and put it in
7273 the DIE's type field. */
7274
f792889a 7275static struct type *
72019c9c
GM
7276read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7277{
7e314c57
JK
7278 struct type *domain_type, *set_type;
7279 struct attribute *attr;
f792889a 7280
7e314c57
JK
7281 domain_type = die_type (die, cu);
7282
7283 /* The die_type call above may have already set the type for this DIE. */
7284 set_type = get_die_type (die, cu);
7285 if (set_type)
7286 return set_type;
7287
7288 set_type = create_set_type (NULL, domain_type);
7289
7290 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7291 if (attr)
7292 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7293
f792889a 7294 return set_die_type (die, set_type, cu);
72019c9c 7295}
7ca2d3a3 7296
c906108c
SS
7297/* First cut: install each common block member as a global variable. */
7298
7299static void
e7c27a73 7300read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7301{
7302 struct die_info *child_die;
7303 struct attribute *attr;
7304 struct symbol *sym;
7305 CORE_ADDR base = (CORE_ADDR) 0;
7306
e142c38c 7307 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7308 if (attr)
7309 {
8e19ed76
PS
7310 /* Support the .debug_loc offsets */
7311 if (attr_form_is_block (attr))
7312 {
e7c27a73 7313 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7314 }
3690dd37 7315 else if (attr_form_is_section_offset (attr))
8e19ed76 7316 {
4d3c2250 7317 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7318 }
7319 else
7320 {
4d3c2250
KB
7321 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7322 "common block member");
8e19ed76 7323 }
c906108c 7324 }
639d11d3 7325 if (die->child != NULL)
c906108c 7326 {
639d11d3 7327 child_die = die->child;
c906108c
SS
7328 while (child_die && child_die->tag)
7329 {
e7c27a73 7330 sym = new_symbol (child_die, NULL, cu);
e142c38c 7331 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
7332 if (attr)
7333 {
d4b96c9a
JK
7334 CORE_ADDR byte_offset = 0;
7335
7336 if (attr_form_is_section_offset (attr))
7337 dwarf2_complex_location_expr_complaint ();
7338 else if (attr_form_is_constant (attr))
7339 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7340 else if (attr_form_is_block (attr))
7341 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7342 else
7343 dwarf2_complex_location_expr_complaint ();
7344
7345 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7346 add_symbol_to_list (sym, &global_symbols);
7347 }
7348 child_die = sibling_die (child_die);
7349 }
7350 }
7351}
7352
0114d602 7353/* Create a type for a C++ namespace. */
d9fa45fe 7354
0114d602
DJ
7355static struct type *
7356read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7357{
e7c27a73 7358 struct objfile *objfile = cu->objfile;
0114d602 7359 const char *previous_prefix, *name;
9219021c 7360 int is_anonymous;
0114d602
DJ
7361 struct type *type;
7362
7363 /* For extensions, reuse the type of the original namespace. */
7364 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7365 {
7366 struct die_info *ext_die;
7367 struct dwarf2_cu *ext_cu = cu;
9a619af0 7368
0114d602
DJ
7369 ext_die = dwarf2_extension (die, &ext_cu);
7370 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7371
7372 /* EXT_CU may not be the same as CU.
7373 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7374 return set_die_type (die, type, cu);
7375 }
9219021c 7376
e142c38c 7377 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7378
7379 /* Now build the name of the current namespace. */
7380
0114d602
DJ
7381 previous_prefix = determine_prefix (die, cu);
7382 if (previous_prefix[0] != '\0')
7383 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7384 previous_prefix, name, 0, cu);
0114d602
DJ
7385
7386 /* Create the type. */
7387 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7388 objfile);
7389 TYPE_NAME (type) = (char *) name;
7390 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7391
60531b24 7392 return set_die_type (die, type, cu);
0114d602
DJ
7393}
7394
7395/* Read a C++ namespace. */
7396
7397static void
7398read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7399{
7400 struct objfile *objfile = cu->objfile;
7401 const char *name;
7402 int is_anonymous;
9219021c 7403
5c4e30ca
DC
7404 /* Add a symbol associated to this if we haven't seen the namespace
7405 before. Also, add a using directive if it's an anonymous
7406 namespace. */
9219021c 7407
f2f0e013 7408 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7409 {
7410 struct type *type;
7411
0114d602 7412 type = read_type_die (die, cu);
e7c27a73 7413 new_symbol (die, type, cu);
5c4e30ca 7414
0114d602 7415 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7416 if (is_anonymous)
0114d602
DJ
7417 {
7418 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7419
c0cc3a76 7420 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7421 NULL, &objfile->objfile_obstack);
0114d602 7422 }
5c4e30ca 7423 }
9219021c 7424
639d11d3 7425 if (die->child != NULL)
d9fa45fe 7426 {
639d11d3 7427 struct die_info *child_die = die->child;
6e70227d 7428
d9fa45fe
DC
7429 while (child_die && child_die->tag)
7430 {
e7c27a73 7431 process_die (child_die, cu);
d9fa45fe
DC
7432 child_die = sibling_die (child_die);
7433 }
7434 }
38d518c9
EZ
7435}
7436
f55ee35c
JK
7437/* Read a Fortran module as type. This DIE can be only a declaration used for
7438 imported module. Still we need that type as local Fortran "use ... only"
7439 declaration imports depend on the created type in determine_prefix. */
7440
7441static struct type *
7442read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7443{
7444 struct objfile *objfile = cu->objfile;
7445 char *module_name;
7446 struct type *type;
7447
7448 module_name = dwarf2_name (die, cu);
7449 if (!module_name)
7450 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7451 die->offset);
7452 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7453
7454 /* determine_prefix uses TYPE_TAG_NAME. */
7455 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7456
7457 return set_die_type (die, type, cu);
7458}
7459
5d7cb8df
JK
7460/* Read a Fortran module. */
7461
7462static void
7463read_module (struct die_info *die, struct dwarf2_cu *cu)
7464{
7465 struct die_info *child_die = die->child;
7466
5d7cb8df
JK
7467 while (child_die && child_die->tag)
7468 {
7469 process_die (child_die, cu);
7470 child_die = sibling_die (child_die);
7471 }
7472}
7473
38d518c9
EZ
7474/* Return the name of the namespace represented by DIE. Set
7475 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7476 namespace. */
7477
7478static const char *
e142c38c 7479namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7480{
7481 struct die_info *current_die;
7482 const char *name = NULL;
7483
7484 /* Loop through the extensions until we find a name. */
7485
7486 for (current_die = die;
7487 current_die != NULL;
f2f0e013 7488 current_die = dwarf2_extension (die, &cu))
38d518c9 7489 {
e142c38c 7490 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7491 if (name != NULL)
7492 break;
7493 }
7494
7495 /* Is it an anonymous namespace? */
7496
7497 *is_anonymous = (name == NULL);
7498 if (*is_anonymous)
7499 name = "(anonymous namespace)";
7500
7501 return name;
d9fa45fe
DC
7502}
7503
c906108c
SS
7504/* Extract all information from a DW_TAG_pointer_type DIE and add to
7505 the user defined type vector. */
7506
f792889a 7507static struct type *
e7c27a73 7508read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7509{
5e2b427d 7510 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7511 struct comp_unit_head *cu_header = &cu->header;
c906108c 7512 struct type *type;
8b2dbe47
KB
7513 struct attribute *attr_byte_size;
7514 struct attribute *attr_address_class;
7515 int byte_size, addr_class;
7e314c57
JK
7516 struct type *target_type;
7517
7518 target_type = die_type (die, cu);
c906108c 7519
7e314c57
JK
7520 /* The die_type call above may have already set the type for this DIE. */
7521 type = get_die_type (die, cu);
7522 if (type)
7523 return type;
7524
7525 type = lookup_pointer_type (target_type);
8b2dbe47 7526
e142c38c 7527 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7528 if (attr_byte_size)
7529 byte_size = DW_UNSND (attr_byte_size);
c906108c 7530 else
8b2dbe47
KB
7531 byte_size = cu_header->addr_size;
7532
e142c38c 7533 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7534 if (attr_address_class)
7535 addr_class = DW_UNSND (attr_address_class);
7536 else
7537 addr_class = DW_ADDR_none;
7538
7539 /* If the pointer size or address class is different than the
7540 default, create a type variant marked as such and set the
7541 length accordingly. */
7542 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7543 {
5e2b427d 7544 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7545 {
7546 int type_flags;
7547
849957d9 7548 type_flags = gdbarch_address_class_type_flags
5e2b427d 7549 (gdbarch, byte_size, addr_class);
876cecd0
TT
7550 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7551 == 0);
8b2dbe47
KB
7552 type = make_type_with_address_space (type, type_flags);
7553 }
7554 else if (TYPE_LENGTH (type) != byte_size)
7555 {
e2e0b3e5 7556 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7557 }
6e70227d 7558 else
9a619af0
MS
7559 {
7560 /* Should we also complain about unhandled address classes? */
7561 }
c906108c 7562 }
8b2dbe47
KB
7563
7564 TYPE_LENGTH (type) = byte_size;
f792889a 7565 return set_die_type (die, type, cu);
c906108c
SS
7566}
7567
7568/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7569 the user defined type vector. */
7570
f792889a 7571static struct type *
e7c27a73 7572read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7573{
7574 struct type *type;
7575 struct type *to_type;
7576 struct type *domain;
7577
e7c27a73
DJ
7578 to_type = die_type (die, cu);
7579 domain = die_containing_type (die, cu);
0d5de010 7580
7e314c57
JK
7581 /* The calls above may have already set the type for this DIE. */
7582 type = get_die_type (die, cu);
7583 if (type)
7584 return type;
7585
0d5de010
DJ
7586 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7587 type = lookup_methodptr_type (to_type);
7588 else
7589 type = lookup_memberptr_type (to_type, domain);
c906108c 7590
f792889a 7591 return set_die_type (die, type, cu);
c906108c
SS
7592}
7593
7594/* Extract all information from a DW_TAG_reference_type DIE and add to
7595 the user defined type vector. */
7596
f792889a 7597static struct type *
e7c27a73 7598read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7599{
e7c27a73 7600 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7601 struct type *type, *target_type;
c906108c
SS
7602 struct attribute *attr;
7603
7e314c57
JK
7604 target_type = die_type (die, cu);
7605
7606 /* The die_type call above may have already set the type for this DIE. */
7607 type = get_die_type (die, cu);
7608 if (type)
7609 return type;
7610
7611 type = lookup_reference_type (target_type);
e142c38c 7612 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7613 if (attr)
7614 {
7615 TYPE_LENGTH (type) = DW_UNSND (attr);
7616 }
7617 else
7618 {
107d2387 7619 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7620 }
f792889a 7621 return set_die_type (die, type, cu);
c906108c
SS
7622}
7623
f792889a 7624static struct type *
e7c27a73 7625read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7626{
f792889a 7627 struct type *base_type, *cv_type;
c906108c 7628
e7c27a73 7629 base_type = die_type (die, cu);
7e314c57
JK
7630
7631 /* The die_type call above may have already set the type for this DIE. */
7632 cv_type = get_die_type (die, cu);
7633 if (cv_type)
7634 return cv_type;
7635
f792889a
DJ
7636 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7637 return set_die_type (die, cv_type, cu);
c906108c
SS
7638}
7639
f792889a 7640static struct type *
e7c27a73 7641read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7642{
f792889a 7643 struct type *base_type, *cv_type;
c906108c 7644
e7c27a73 7645 base_type = die_type (die, cu);
7e314c57
JK
7646
7647 /* The die_type call above may have already set the type for this DIE. */
7648 cv_type = get_die_type (die, cu);
7649 if (cv_type)
7650 return cv_type;
7651
f792889a
DJ
7652 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7653 return set_die_type (die, cv_type, cu);
c906108c
SS
7654}
7655
7656/* Extract all information from a DW_TAG_string_type DIE and add to
7657 the user defined type vector. It isn't really a user defined type,
7658 but it behaves like one, with other DIE's using an AT_user_def_type
7659 attribute to reference it. */
7660
f792889a 7661static struct type *
e7c27a73 7662read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7663{
e7c27a73 7664 struct objfile *objfile = cu->objfile;
3b7538c0 7665 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7666 struct type *type, *range_type, *index_type, *char_type;
7667 struct attribute *attr;
7668 unsigned int length;
7669
e142c38c 7670 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7671 if (attr)
7672 {
7673 length = DW_UNSND (attr);
7674 }
7675 else
7676 {
b21b22e0 7677 /* check for the DW_AT_byte_size attribute */
e142c38c 7678 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7679 if (attr)
7680 {
7681 length = DW_UNSND (attr);
7682 }
7683 else
7684 {
7685 length = 1;
7686 }
c906108c 7687 }
6ccb9162 7688
46bf5051 7689 index_type = objfile_type (objfile)->builtin_int;
c906108c 7690 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7691 char_type = language_string_char_type (cu->language_defn, gdbarch);
7692 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7693
f792889a 7694 return set_die_type (die, type, cu);
c906108c
SS
7695}
7696
7697/* Handle DIES due to C code like:
7698
7699 struct foo
c5aa993b
JM
7700 {
7701 int (*funcp)(int a, long l);
7702 int b;
7703 };
c906108c
SS
7704
7705 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7706 */
c906108c 7707
f792889a 7708static struct type *
e7c27a73 7709read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7710{
7711 struct type *type; /* Type that this function returns */
7712 struct type *ftype; /* Function that returns above type */
7713 struct attribute *attr;
7714
e7c27a73 7715 type = die_type (die, cu);
7e314c57
JK
7716
7717 /* The die_type call above may have already set the type for this DIE. */
7718 ftype = get_die_type (die, cu);
7719 if (ftype)
7720 return ftype;
7721
0c8b41f1 7722 ftype = lookup_function_type (type);
c906108c 7723
5b8101ae 7724 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7725 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7726 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7727 || cu->language == language_cplus
5b8101ae
PM
7728 || cu->language == language_java
7729 || cu->language == language_pascal)
876cecd0 7730 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7731 else if (producer_is_realview (cu->producer))
7732 /* RealView does not emit DW_AT_prototyped. We can not
7733 distinguish prototyped and unprototyped functions; default to
7734 prototyped, since that is more common in modern code (and
7735 RealView warns about unprototyped functions). */
7736 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7737
c055b101
CV
7738 /* Store the calling convention in the type if it's available in
7739 the subroutine die. Otherwise set the calling convention to
7740 the default value DW_CC_normal. */
7741 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7742 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7743
7744 /* We need to add the subroutine type to the die immediately so
7745 we don't infinitely recurse when dealing with parameters
7746 declared as the same subroutine type. */
7747 set_die_type (die, ftype, cu);
6e70227d 7748
639d11d3 7749 if (die->child != NULL)
c906108c 7750 {
8072405b 7751 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7752 struct die_info *child_die;
8072405b 7753 int nparams, iparams;
c906108c
SS
7754
7755 /* Count the number of parameters.
7756 FIXME: GDB currently ignores vararg functions, but knows about
7757 vararg member functions. */
8072405b 7758 nparams = 0;
639d11d3 7759 child_die = die->child;
c906108c
SS
7760 while (child_die && child_die->tag)
7761 {
7762 if (child_die->tag == DW_TAG_formal_parameter)
7763 nparams++;
7764 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7765 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7766 child_die = sibling_die (child_die);
7767 }
7768
7769 /* Allocate storage for parameters and fill them in. */
7770 TYPE_NFIELDS (ftype) = nparams;
7771 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7772 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7773
8072405b
JK
7774 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7775 even if we error out during the parameters reading below. */
7776 for (iparams = 0; iparams < nparams; iparams++)
7777 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7778
7779 iparams = 0;
639d11d3 7780 child_die = die->child;
c906108c
SS
7781 while (child_die && child_die->tag)
7782 {
7783 if (child_die->tag == DW_TAG_formal_parameter)
7784 {
3ce3b1ba
PA
7785 struct type *arg_type;
7786
7787 /* DWARF version 2 has no clean way to discern C++
7788 static and non-static member functions. G++ helps
7789 GDB by marking the first parameter for non-static
7790 member functions (which is the this pointer) as
7791 artificial. We pass this information to
7792 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7793
7794 DWARF version 3 added DW_AT_object_pointer, which GCC
7795 4.5 does not yet generate. */
e142c38c 7796 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7797 if (attr)
7798 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7799 else
418835cc
KS
7800 {
7801 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7802
7803 /* GCC/43521: In java, the formal parameter
7804 "this" is sometimes not marked with DW_AT_artificial. */
7805 if (cu->language == language_java)
7806 {
7807 const char *name = dwarf2_name (child_die, cu);
9a619af0 7808
418835cc
KS
7809 if (name && !strcmp (name, "this"))
7810 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7811 }
7812 }
3ce3b1ba
PA
7813 arg_type = die_type (child_die, cu);
7814
7815 /* RealView does not mark THIS as const, which the testsuite
7816 expects. GCC marks THIS as const in method definitions,
7817 but not in the class specifications (GCC PR 43053). */
7818 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7819 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7820 {
7821 int is_this = 0;
7822 struct dwarf2_cu *arg_cu = cu;
7823 const char *name = dwarf2_name (child_die, cu);
7824
7825 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7826 if (attr)
7827 {
7828 /* If the compiler emits this, use it. */
7829 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7830 is_this = 1;
7831 }
7832 else if (name && strcmp (name, "this") == 0)
7833 /* Function definitions will have the argument names. */
7834 is_this = 1;
7835 else if (name == NULL && iparams == 0)
7836 /* Declarations may not have the names, so like
7837 elsewhere in GDB, assume an artificial first
7838 argument is "this". */
7839 is_this = 1;
7840
7841 if (is_this)
7842 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7843 arg_type, 0);
7844 }
7845
7846 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7847 iparams++;
7848 }
7849 child_die = sibling_die (child_die);
7850 }
7851 }
7852
76c10ea2 7853 return ftype;
c906108c
SS
7854}
7855
f792889a 7856static struct type *
e7c27a73 7857read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7858{
e7c27a73 7859 struct objfile *objfile = cu->objfile;
0114d602 7860 const char *name = NULL;
f792889a 7861 struct type *this_type;
c906108c 7862
94af9270 7863 name = dwarf2_full_name (NULL, die, cu);
f792889a 7864 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7865 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7866 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7867 set_die_type (die, this_type, cu);
7868 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7869 return this_type;
c906108c
SS
7870}
7871
7872/* Find a representation of a given base type and install
7873 it in the TYPE field of the die. */
7874
f792889a 7875static struct type *
e7c27a73 7876read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7877{
e7c27a73 7878 struct objfile *objfile = cu->objfile;
c906108c
SS
7879 struct type *type;
7880 struct attribute *attr;
7881 int encoding = 0, size = 0;
39cbfefa 7882 char *name;
6ccb9162
UW
7883 enum type_code code = TYPE_CODE_INT;
7884 int type_flags = 0;
7885 struct type *target_type = NULL;
c906108c 7886
e142c38c 7887 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7888 if (attr)
7889 {
7890 encoding = DW_UNSND (attr);
7891 }
e142c38c 7892 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7893 if (attr)
7894 {
7895 size = DW_UNSND (attr);
7896 }
39cbfefa 7897 name = dwarf2_name (die, cu);
6ccb9162 7898 if (!name)
c906108c 7899 {
6ccb9162
UW
7900 complaint (&symfile_complaints,
7901 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7902 }
6ccb9162
UW
7903
7904 switch (encoding)
c906108c 7905 {
6ccb9162
UW
7906 case DW_ATE_address:
7907 /* Turn DW_ATE_address into a void * pointer. */
7908 code = TYPE_CODE_PTR;
7909 type_flags |= TYPE_FLAG_UNSIGNED;
7910 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7911 break;
7912 case DW_ATE_boolean:
7913 code = TYPE_CODE_BOOL;
7914 type_flags |= TYPE_FLAG_UNSIGNED;
7915 break;
7916 case DW_ATE_complex_float:
7917 code = TYPE_CODE_COMPLEX;
7918 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7919 break;
7920 case DW_ATE_decimal_float:
7921 code = TYPE_CODE_DECFLOAT;
7922 break;
7923 case DW_ATE_float:
7924 code = TYPE_CODE_FLT;
7925 break;
7926 case DW_ATE_signed:
7927 break;
7928 case DW_ATE_unsigned:
7929 type_flags |= TYPE_FLAG_UNSIGNED;
7930 break;
7931 case DW_ATE_signed_char:
6e70227d 7932 if (cu->language == language_ada || cu->language == language_m2
868a0084 7933 || cu->language == language_pascal)
6ccb9162
UW
7934 code = TYPE_CODE_CHAR;
7935 break;
7936 case DW_ATE_unsigned_char:
868a0084
PM
7937 if (cu->language == language_ada || cu->language == language_m2
7938 || cu->language == language_pascal)
6ccb9162
UW
7939 code = TYPE_CODE_CHAR;
7940 type_flags |= TYPE_FLAG_UNSIGNED;
7941 break;
75079b2b
TT
7942 case DW_ATE_UTF:
7943 /* We just treat this as an integer and then recognize the
7944 type by name elsewhere. */
7945 break;
7946
6ccb9162
UW
7947 default:
7948 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7949 dwarf_type_encoding_name (encoding));
7950 break;
c906108c 7951 }
6ccb9162 7952
0114d602
DJ
7953 type = init_type (code, size, type_flags, NULL, objfile);
7954 TYPE_NAME (type) = name;
6ccb9162
UW
7955 TYPE_TARGET_TYPE (type) = target_type;
7956
0114d602 7957 if (name && strcmp (name, "char") == 0)
876cecd0 7958 TYPE_NOSIGN (type) = 1;
0114d602 7959
f792889a 7960 return set_die_type (die, type, cu);
c906108c
SS
7961}
7962
a02abb62
JB
7963/* Read the given DW_AT_subrange DIE. */
7964
f792889a 7965static struct type *
a02abb62
JB
7966read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7967{
5e2b427d 7968 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7969 struct type *base_type;
7970 struct type *range_type;
7971 struct attribute *attr;
43bbcdc2
PH
7972 LONGEST low = 0;
7973 LONGEST high = -1;
39cbfefa 7974 char *name;
43bbcdc2 7975 LONGEST negative_mask;
e77813c8 7976
a02abb62 7977 base_type = die_type (die, cu);
953ac07e
JK
7978 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7979 check_typedef (base_type);
a02abb62 7980
7e314c57
JK
7981 /* The die_type call above may have already set the type for this DIE. */
7982 range_type = get_die_type (die, cu);
7983 if (range_type)
7984 return range_type;
7985
e142c38c 7986 if (cu->language == language_fortran)
6e70227d 7987 {
a02abb62
JB
7988 /* FORTRAN implies a lower bound of 1, if not given. */
7989 low = 1;
7990 }
7991
dd5e6932
DJ
7992 /* FIXME: For variable sized arrays either of these could be
7993 a variable rather than a constant value. We'll allow it,
7994 but we don't know how to handle it. */
e142c38c 7995 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7996 if (attr)
7997 low = dwarf2_get_attr_constant_value (attr, 0);
7998
e142c38c 7999 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8000 if (attr)
6e70227d 8001 {
e77813c8 8002 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8003 {
8004 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8005 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8006 FIXME: GDB does not yet know how to handle dynamic
8007 arrays properly, treat them as arrays with unspecified
8008 length for now.
8009
8010 FIXME: jimb/2003-09-22: GDB does not really know
8011 how to handle arrays of unspecified length
8012 either; we just represent them as zero-length
8013 arrays. Choose an appropriate upper bound given
8014 the lower bound we've computed above. */
8015 high = low - 1;
8016 }
8017 else
8018 high = dwarf2_get_attr_constant_value (attr, 1);
8019 }
e77813c8
PM
8020 else
8021 {
8022 attr = dwarf2_attr (die, DW_AT_count, cu);
8023 if (attr)
8024 {
8025 int count = dwarf2_get_attr_constant_value (attr, 1);
8026 high = low + count - 1;
8027 }
8028 }
8029
8030 /* Dwarf-2 specifications explicitly allows to create subrange types
8031 without specifying a base type.
8032 In that case, the base type must be set to the type of
8033 the lower bound, upper bound or count, in that order, if any of these
8034 three attributes references an object that has a type.
8035 If no base type is found, the Dwarf-2 specifications say that
8036 a signed integer type of size equal to the size of an address should
8037 be used.
8038 For the following C code: `extern char gdb_int [];'
8039 GCC produces an empty range DIE.
8040 FIXME: muller/2010-05-28: Possible references to object for low bound,
8041 high bound or count are not yet handled by this code.
8042 */
8043 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8044 {
8045 struct objfile *objfile = cu->objfile;
8046 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8047 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8048 struct type *int_type = objfile_type (objfile)->builtin_int;
8049
8050 /* Test "int", "long int", and "long long int" objfile types,
8051 and select the first one having a size above or equal to the
8052 architecture address size. */
8053 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8054 base_type = int_type;
8055 else
8056 {
8057 int_type = objfile_type (objfile)->builtin_long;
8058 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8059 base_type = int_type;
8060 else
8061 {
8062 int_type = objfile_type (objfile)->builtin_long_long;
8063 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8064 base_type = int_type;
8065 }
8066 }
8067 }
a02abb62 8068
6e70227d 8069 negative_mask =
43bbcdc2
PH
8070 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8071 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8072 low |= negative_mask;
8073 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8074 high |= negative_mask;
8075
a02abb62
JB
8076 range_type = create_range_type (NULL, base_type, low, high);
8077
bbb0eef6
JK
8078 /* Mark arrays with dynamic length at least as an array of unspecified
8079 length. GDB could check the boundary but before it gets implemented at
8080 least allow accessing the array elements. */
8081 if (attr && attr->form == DW_FORM_block1)
8082 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8083
39cbfefa
DJ
8084 name = dwarf2_name (die, cu);
8085 if (name)
8086 TYPE_NAME (range_type) = name;
6e70227d 8087
e142c38c 8088 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8089 if (attr)
8090 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8091
7e314c57
JK
8092 set_die_type (die, range_type, cu);
8093
8094 /* set_die_type should be already done. */
b4ba55a1
JB
8095 set_descriptive_type (range_type, die, cu);
8096
7e314c57 8097 return range_type;
a02abb62 8098}
6e70227d 8099
f792889a 8100static struct type *
81a17f79
JB
8101read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8102{
8103 struct type *type;
81a17f79 8104
81a17f79
JB
8105 /* For now, we only support the C meaning of an unspecified type: void. */
8106
0114d602
DJ
8107 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8108 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8109
f792889a 8110 return set_die_type (die, type, cu);
81a17f79 8111}
a02abb62 8112
51545339
DJ
8113/* Trivial hash function for die_info: the hash value of a DIE
8114 is its offset in .debug_info for this objfile. */
8115
8116static hashval_t
8117die_hash (const void *item)
8118{
8119 const struct die_info *die = item;
9a619af0 8120
51545339
DJ
8121 return die->offset;
8122}
8123
8124/* Trivial comparison function for die_info structures: two DIEs
8125 are equal if they have the same offset. */
8126
8127static int
8128die_eq (const void *item_lhs, const void *item_rhs)
8129{
8130 const struct die_info *die_lhs = item_lhs;
8131 const struct die_info *die_rhs = item_rhs;
9a619af0 8132
51545339
DJ
8133 return die_lhs->offset == die_rhs->offset;
8134}
8135
c906108c
SS
8136/* Read a whole compilation unit into a linked list of dies. */
8137
f9aca02d 8138static struct die_info *
93311388 8139read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8140{
93311388 8141 struct die_reader_specs reader_specs;
98bfdba5 8142 int read_abbrevs = 0;
1d9ec526 8143 struct cleanup *back_to = NULL;
98bfdba5
PA
8144 struct die_info *die;
8145
8146 if (cu->dwarf2_abbrevs == NULL)
8147 {
8148 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8149 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8150 read_abbrevs = 1;
8151 }
93311388 8152
348e048f 8153 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8154 cu->die_hash
8155 = htab_create_alloc_ex (cu->header.length / 12,
8156 die_hash,
8157 die_eq,
8158 NULL,
8159 &cu->comp_unit_obstack,
8160 hashtab_obstack_allocate,
8161 dummy_obstack_deallocate);
8162
93311388
DE
8163 init_cu_die_reader (&reader_specs, cu);
8164
98bfdba5
PA
8165 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8166
8167 if (read_abbrevs)
8168 do_cleanups (back_to);
8169
8170 return die;
639d11d3
DC
8171}
8172
d97bc12b
DE
8173/* Main entry point for reading a DIE and all children.
8174 Read the DIE and dump it if requested. */
8175
8176static struct die_info *
93311388
DE
8177read_die_and_children (const struct die_reader_specs *reader,
8178 gdb_byte *info_ptr,
d97bc12b
DE
8179 gdb_byte **new_info_ptr,
8180 struct die_info *parent)
8181{
93311388 8182 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8183 new_info_ptr, parent);
8184
8185 if (dwarf2_die_debug)
8186 {
348e048f
DE
8187 fprintf_unfiltered (gdb_stdlog,
8188 "\nRead die from %s of %s:\n",
8189 reader->buffer == dwarf2_per_objfile->info.buffer
8190 ? ".debug_info"
8191 : reader->buffer == dwarf2_per_objfile->types.buffer
8192 ? ".debug_types"
8193 : "unknown section",
8194 reader->abfd->filename);
d97bc12b
DE
8195 dump_die (result, dwarf2_die_debug);
8196 }
8197
8198 return result;
8199}
8200
639d11d3
DC
8201/* Read a single die and all its descendents. Set the die's sibling
8202 field to NULL; set other fields in the die correctly, and set all
8203 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8204 location of the info_ptr after reading all of those dies. PARENT
8205 is the parent of the die in question. */
8206
8207static struct die_info *
93311388
DE
8208read_die_and_children_1 (const struct die_reader_specs *reader,
8209 gdb_byte *info_ptr,
d97bc12b
DE
8210 gdb_byte **new_info_ptr,
8211 struct die_info *parent)
639d11d3
DC
8212{
8213 struct die_info *die;
fe1b8b76 8214 gdb_byte *cur_ptr;
639d11d3
DC
8215 int has_children;
8216
93311388 8217 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8218 if (die == NULL)
8219 {
8220 *new_info_ptr = cur_ptr;
8221 return NULL;
8222 }
93311388 8223 store_in_ref_table (die, reader->cu);
639d11d3
DC
8224
8225 if (has_children)
348e048f 8226 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8227 else
8228 {
8229 die->child = NULL;
8230 *new_info_ptr = cur_ptr;
8231 }
8232
8233 die->sibling = NULL;
8234 die->parent = parent;
8235 return die;
8236}
8237
8238/* Read a die, all of its descendents, and all of its siblings; set
8239 all of the fields of all of the dies correctly. Arguments are as
8240 in read_die_and_children. */
8241
8242static struct die_info *
93311388
DE
8243read_die_and_siblings (const struct die_reader_specs *reader,
8244 gdb_byte *info_ptr,
fe1b8b76 8245 gdb_byte **new_info_ptr,
639d11d3
DC
8246 struct die_info *parent)
8247{
8248 struct die_info *first_die, *last_sibling;
fe1b8b76 8249 gdb_byte *cur_ptr;
639d11d3 8250
c906108c 8251 cur_ptr = info_ptr;
639d11d3
DC
8252 first_die = last_sibling = NULL;
8253
8254 while (1)
c906108c 8255 {
639d11d3 8256 struct die_info *die
93311388 8257 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8258
1d325ec1 8259 if (die == NULL)
c906108c 8260 {
639d11d3
DC
8261 *new_info_ptr = cur_ptr;
8262 return first_die;
c906108c 8263 }
1d325ec1
DJ
8264
8265 if (!first_die)
8266 first_die = die;
c906108c 8267 else
1d325ec1
DJ
8268 last_sibling->sibling = die;
8269
8270 last_sibling = die;
c906108c 8271 }
c906108c
SS
8272}
8273
93311388
DE
8274/* Read the die from the .debug_info section buffer. Set DIEP to
8275 point to a newly allocated die with its information, except for its
8276 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8277 whether the die has children or not. */
8278
8279static gdb_byte *
8280read_full_die (const struct die_reader_specs *reader,
8281 struct die_info **diep, gdb_byte *info_ptr,
8282 int *has_children)
8283{
8284 unsigned int abbrev_number, bytes_read, i, offset;
8285 struct abbrev_info *abbrev;
8286 struct die_info *die;
8287 struct dwarf2_cu *cu = reader->cu;
8288 bfd *abfd = reader->abfd;
8289
8290 offset = info_ptr - reader->buffer;
8291 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8292 info_ptr += bytes_read;
8293 if (!abbrev_number)
8294 {
8295 *diep = NULL;
8296 *has_children = 0;
8297 return info_ptr;
8298 }
8299
8300 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8301 if (!abbrev)
348e048f
DE
8302 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8303 abbrev_number,
8304 bfd_get_filename (abfd));
8305
93311388
DE
8306 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8307 die->offset = offset;
8308 die->tag = abbrev->tag;
8309 die->abbrev = abbrev_number;
8310
8311 die->num_attrs = abbrev->num_attrs;
8312
8313 for (i = 0; i < abbrev->num_attrs; ++i)
8314 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8315 abfd, info_ptr, cu);
8316
8317 *diep = die;
8318 *has_children = abbrev->has_children;
8319 return info_ptr;
8320}
8321
c906108c
SS
8322/* In DWARF version 2, the description of the debugging information is
8323 stored in a separate .debug_abbrev section. Before we read any
8324 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8325 in a hash table. This function also sets flags in CU describing
8326 the data found in the abbrev table. */
c906108c
SS
8327
8328static void
e7c27a73 8329dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8330{
e7c27a73 8331 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8332 gdb_byte *abbrev_ptr;
c906108c
SS
8333 struct abbrev_info *cur_abbrev;
8334 unsigned int abbrev_number, bytes_read, abbrev_name;
8335 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8336 struct attr_abbrev *cur_attrs;
8337 unsigned int allocated_attrs;
c906108c 8338
57349743 8339 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8340 obstack_init (&cu->abbrev_obstack);
8341 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8342 (ABBREV_HASH_SIZE
8343 * sizeof (struct abbrev_info *)));
8344 memset (cu->dwarf2_abbrevs, 0,
8345 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8346
be391dca
TT
8347 dwarf2_read_section (dwarf2_per_objfile->objfile,
8348 &dwarf2_per_objfile->abbrev);
dce234bc 8349 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8350 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8351 abbrev_ptr += bytes_read;
8352
f3dd6933
DJ
8353 allocated_attrs = ATTR_ALLOC_CHUNK;
8354 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8355
c906108c
SS
8356 /* loop until we reach an abbrev number of 0 */
8357 while (abbrev_number)
8358 {
f3dd6933 8359 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8360
8361 /* read in abbrev header */
8362 cur_abbrev->number = abbrev_number;
8363 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8364 abbrev_ptr += bytes_read;
8365 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8366 abbrev_ptr += 1;
8367
72bf9492
DJ
8368 if (cur_abbrev->tag == DW_TAG_namespace)
8369 cu->has_namespace_info = 1;
8370
c906108c
SS
8371 /* now read in declarations */
8372 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8373 abbrev_ptr += bytes_read;
8374 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8375 abbrev_ptr += bytes_read;
8376 while (abbrev_name)
8377 {
f3dd6933 8378 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8379 {
f3dd6933
DJ
8380 allocated_attrs += ATTR_ALLOC_CHUNK;
8381 cur_attrs
8382 = xrealloc (cur_attrs, (allocated_attrs
8383 * sizeof (struct attr_abbrev)));
c906108c 8384 }
ae038cb0
DJ
8385
8386 /* Record whether this compilation unit might have
8387 inter-compilation-unit references. If we don't know what form
8388 this attribute will have, then it might potentially be a
8389 DW_FORM_ref_addr, so we conservatively expect inter-CU
8390 references. */
8391
8392 if (abbrev_form == DW_FORM_ref_addr
8393 || abbrev_form == DW_FORM_indirect)
8394 cu->has_form_ref_addr = 1;
8395
f3dd6933
DJ
8396 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8397 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8398 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8399 abbrev_ptr += bytes_read;
8400 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8401 abbrev_ptr += bytes_read;
8402 }
8403
f3dd6933
DJ
8404 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8405 (cur_abbrev->num_attrs
8406 * sizeof (struct attr_abbrev)));
8407 memcpy (cur_abbrev->attrs, cur_attrs,
8408 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8409
c906108c 8410 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8411 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8412 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8413
8414 /* Get next abbreviation.
8415 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8416 always properly terminated with an abbrev number of 0.
8417 Exit loop if we encounter an abbreviation which we have
8418 already read (which means we are about to read the abbreviations
8419 for the next compile unit) or if the end of the abbreviation
8420 table is reached. */
dce234bc
PP
8421 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8422 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8423 break;
8424 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8425 abbrev_ptr += bytes_read;
e7c27a73 8426 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8427 break;
8428 }
f3dd6933
DJ
8429
8430 xfree (cur_attrs);
c906108c
SS
8431}
8432
f3dd6933 8433/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8434
c906108c 8435static void
f3dd6933 8436dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8437{
f3dd6933 8438 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8439
f3dd6933
DJ
8440 obstack_free (&cu->abbrev_obstack, NULL);
8441 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8442}
8443
8444/* Lookup an abbrev_info structure in the abbrev hash table. */
8445
8446static struct abbrev_info *
e7c27a73 8447dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8448{
8449 unsigned int hash_number;
8450 struct abbrev_info *abbrev;
8451
8452 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8453 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8454
8455 while (abbrev)
8456 {
8457 if (abbrev->number == number)
8458 return abbrev;
8459 else
8460 abbrev = abbrev->next;
8461 }
8462 return NULL;
8463}
8464
72bf9492
DJ
8465/* Returns nonzero if TAG represents a type that we might generate a partial
8466 symbol for. */
8467
8468static int
8469is_type_tag_for_partial (int tag)
8470{
8471 switch (tag)
8472 {
8473#if 0
8474 /* Some types that would be reasonable to generate partial symbols for,
8475 that we don't at present. */
8476 case DW_TAG_array_type:
8477 case DW_TAG_file_type:
8478 case DW_TAG_ptr_to_member_type:
8479 case DW_TAG_set_type:
8480 case DW_TAG_string_type:
8481 case DW_TAG_subroutine_type:
8482#endif
8483 case DW_TAG_base_type:
8484 case DW_TAG_class_type:
680b30c7 8485 case DW_TAG_interface_type:
72bf9492
DJ
8486 case DW_TAG_enumeration_type:
8487 case DW_TAG_structure_type:
8488 case DW_TAG_subrange_type:
8489 case DW_TAG_typedef:
8490 case DW_TAG_union_type:
8491 return 1;
8492 default:
8493 return 0;
8494 }
8495}
8496
8497/* Load all DIEs that are interesting for partial symbols into memory. */
8498
8499static struct partial_die_info *
93311388
DE
8500load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8501 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8502{
8503 struct partial_die_info *part_die;
8504 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8505 struct abbrev_info *abbrev;
8506 unsigned int bytes_read;
5afb4e99 8507 unsigned int load_all = 0;
72bf9492
DJ
8508
8509 int nesting_level = 1;
8510
8511 parent_die = NULL;
8512 last_die = NULL;
8513
5afb4e99
DJ
8514 if (cu->per_cu && cu->per_cu->load_all_dies)
8515 load_all = 1;
8516
72bf9492
DJ
8517 cu->partial_dies
8518 = htab_create_alloc_ex (cu->header.length / 12,
8519 partial_die_hash,
8520 partial_die_eq,
8521 NULL,
8522 &cu->comp_unit_obstack,
8523 hashtab_obstack_allocate,
8524 dummy_obstack_deallocate);
8525
8526 part_die = obstack_alloc (&cu->comp_unit_obstack,
8527 sizeof (struct partial_die_info));
8528
8529 while (1)
8530 {
8531 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8532
8533 /* A NULL abbrev means the end of a series of children. */
8534 if (abbrev == NULL)
8535 {
8536 if (--nesting_level == 0)
8537 {
8538 /* PART_DIE was probably the last thing allocated on the
8539 comp_unit_obstack, so we could call obstack_free
8540 here. We don't do that because the waste is small,
8541 and will be cleaned up when we're done with this
8542 compilation unit. This way, we're also more robust
8543 against other users of the comp_unit_obstack. */
8544 return first_die;
8545 }
8546 info_ptr += bytes_read;
8547 last_die = parent_die;
8548 parent_die = parent_die->die_parent;
8549 continue;
8550 }
8551
98bfdba5
PA
8552 /* Check for template arguments. We never save these; if
8553 they're seen, we just mark the parent, and go on our way. */
8554 if (parent_die != NULL
8555 && cu->language == language_cplus
8556 && (abbrev->tag == DW_TAG_template_type_param
8557 || abbrev->tag == DW_TAG_template_value_param))
8558 {
8559 parent_die->has_template_arguments = 1;
8560
8561 if (!load_all)
8562 {
8563 /* We don't need a partial DIE for the template argument. */
8564 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8565 cu);
8566 continue;
8567 }
8568 }
8569
8570 /* We only recurse into subprograms looking for template arguments.
8571 Skip their other children. */
8572 if (!load_all
8573 && cu->language == language_cplus
8574 && parent_die != NULL
8575 && parent_die->tag == DW_TAG_subprogram)
8576 {
8577 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8578 continue;
8579 }
8580
5afb4e99
DJ
8581 /* Check whether this DIE is interesting enough to save. Normally
8582 we would not be interested in members here, but there may be
8583 later variables referencing them via DW_AT_specification (for
8584 static members). */
8585 if (!load_all
8586 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8587 && abbrev->tag != DW_TAG_enumerator
8588 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8589 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8590 && abbrev->tag != DW_TAG_variable
5afb4e99 8591 && abbrev->tag != DW_TAG_namespace
f55ee35c 8592 && abbrev->tag != DW_TAG_module
5afb4e99 8593 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8594 {
8595 /* Otherwise we skip to the next sibling, if any. */
93311388 8596 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8597 continue;
8598 }
8599
93311388
DE
8600 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8601 buffer, info_ptr, cu);
72bf9492
DJ
8602
8603 /* This two-pass algorithm for processing partial symbols has a
8604 high cost in cache pressure. Thus, handle some simple cases
8605 here which cover the majority of C partial symbols. DIEs
8606 which neither have specification tags in them, nor could have
8607 specification tags elsewhere pointing at them, can simply be
8608 processed and discarded.
8609
8610 This segment is also optional; scan_partial_symbols and
8611 add_partial_symbol will handle these DIEs if we chain
8612 them in normally. When compilers which do not emit large
8613 quantities of duplicate debug information are more common,
8614 this code can probably be removed. */
8615
8616 /* Any complete simple types at the top level (pretty much all
8617 of them, for a language without namespaces), can be processed
8618 directly. */
8619 if (parent_die == NULL
8620 && part_die->has_specification == 0
8621 && part_die->is_declaration == 0
8622 && (part_die->tag == DW_TAG_typedef
8623 || part_die->tag == DW_TAG_base_type
8624 || part_die->tag == DW_TAG_subrange_type))
8625 {
8626 if (building_psymtab && part_die->name != NULL)
04a679b8 8627 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8628 VAR_DOMAIN, LOC_TYPEDEF,
8629 &cu->objfile->static_psymbols,
8630 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8631 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8632 continue;
8633 }
8634
8635 /* If we're at the second level, and we're an enumerator, and
8636 our parent has no specification (meaning possibly lives in a
8637 namespace elsewhere), then we can add the partial symbol now
8638 instead of queueing it. */
8639 if (part_die->tag == DW_TAG_enumerator
8640 && parent_die != NULL
8641 && parent_die->die_parent == NULL
8642 && parent_die->tag == DW_TAG_enumeration_type
8643 && parent_die->has_specification == 0)
8644 {
8645 if (part_die->name == NULL)
e2e0b3e5 8646 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8647 else if (building_psymtab)
04a679b8 8648 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8649 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8650 (cu->language == language_cplus
8651 || cu->language == language_java)
72bf9492
DJ
8652 ? &cu->objfile->global_psymbols
8653 : &cu->objfile->static_psymbols,
8654 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8655
93311388 8656 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8657 continue;
8658 }
8659
8660 /* We'll save this DIE so link it in. */
8661 part_die->die_parent = parent_die;
8662 part_die->die_sibling = NULL;
8663 part_die->die_child = NULL;
8664
8665 if (last_die && last_die == parent_die)
8666 last_die->die_child = part_die;
8667 else if (last_die)
8668 last_die->die_sibling = part_die;
8669
8670 last_die = part_die;
8671
8672 if (first_die == NULL)
8673 first_die = part_die;
8674
8675 /* Maybe add the DIE to the hash table. Not all DIEs that we
8676 find interesting need to be in the hash table, because we
8677 also have the parent/sibling/child chains; only those that we
8678 might refer to by offset later during partial symbol reading.
8679
8680 For now this means things that might have be the target of a
8681 DW_AT_specification, DW_AT_abstract_origin, or
8682 DW_AT_extension. DW_AT_extension will refer only to
8683 namespaces; DW_AT_abstract_origin refers to functions (and
8684 many things under the function DIE, but we do not recurse
8685 into function DIEs during partial symbol reading) and
8686 possibly variables as well; DW_AT_specification refers to
8687 declarations. Declarations ought to have the DW_AT_declaration
8688 flag. It happens that GCC forgets to put it in sometimes, but
8689 only for functions, not for types.
8690
8691 Adding more things than necessary to the hash table is harmless
8692 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8693 wasted time in find_partial_die, when we reread the compilation
8694 unit with load_all_dies set. */
72bf9492 8695
5afb4e99
DJ
8696 if (load_all
8697 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8698 || abbrev->tag == DW_TAG_variable
8699 || abbrev->tag == DW_TAG_namespace
8700 || part_die->is_declaration)
8701 {
8702 void **slot;
8703
8704 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8705 part_die->offset, INSERT);
8706 *slot = part_die;
8707 }
8708
8709 part_die = obstack_alloc (&cu->comp_unit_obstack,
8710 sizeof (struct partial_die_info));
8711
8712 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8713 we have no reason to follow the children of structures; for other
98bfdba5
PA
8714 languages we have to, so that we can get at method physnames
8715 to infer fully qualified class names, for DW_AT_specification,
8716 and for C++ template arguments. For C++, we also look one level
8717 inside functions to find template arguments (if the name of the
8718 function does not already contain the template arguments).
bc30ff58
JB
8719
8720 For Ada, we need to scan the children of subprograms and lexical
8721 blocks as well because Ada allows the definition of nested
8722 entities that could be interesting for the debugger, such as
8723 nested subprograms for instance. */
72bf9492 8724 if (last_die->has_children
5afb4e99
DJ
8725 && (load_all
8726 || last_die->tag == DW_TAG_namespace
f55ee35c 8727 || last_die->tag == DW_TAG_module
72bf9492 8728 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8729 || (cu->language == language_cplus
8730 && last_die->tag == DW_TAG_subprogram
8731 && (last_die->name == NULL
8732 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8733 || (cu->language != language_c
8734 && (last_die->tag == DW_TAG_class_type
680b30c7 8735 || last_die->tag == DW_TAG_interface_type
72bf9492 8736 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8737 || last_die->tag == DW_TAG_union_type))
8738 || (cu->language == language_ada
8739 && (last_die->tag == DW_TAG_subprogram
8740 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8741 {
8742 nesting_level++;
8743 parent_die = last_die;
8744 continue;
8745 }
8746
8747 /* Otherwise we skip to the next sibling, if any. */
93311388 8748 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8749
8750 /* Back to the top, do it again. */
8751 }
8752}
8753
c906108c
SS
8754/* Read a minimal amount of information into the minimal die structure. */
8755
fe1b8b76 8756static gdb_byte *
72bf9492
DJ
8757read_partial_die (struct partial_die_info *part_die,
8758 struct abbrev_info *abbrev,
8759 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8760 gdb_byte *buffer, gdb_byte *info_ptr,
8761 struct dwarf2_cu *cu)
c906108c 8762{
fa238c03 8763 unsigned int i;
c906108c 8764 struct attribute attr;
c5aa993b 8765 int has_low_pc_attr = 0;
c906108c
SS
8766 int has_high_pc_attr = 0;
8767
72bf9492 8768 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8769
93311388 8770 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8771
8772 info_ptr += abbrev_len;
8773
8774 if (abbrev == NULL)
8775 return info_ptr;
8776
c906108c
SS
8777 part_die->tag = abbrev->tag;
8778 part_die->has_children = abbrev->has_children;
c906108c
SS
8779
8780 for (i = 0; i < abbrev->num_attrs; ++i)
8781 {
e7c27a73 8782 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8783
8784 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8785 partial symbol table. */
c906108c
SS
8786 switch (attr.name)
8787 {
8788 case DW_AT_name:
71c25dea
TT
8789 switch (part_die->tag)
8790 {
8791 case DW_TAG_compile_unit:
348e048f 8792 case DW_TAG_type_unit:
71c25dea
TT
8793 /* Compilation units have a DW_AT_name that is a filename, not
8794 a source language identifier. */
8795 case DW_TAG_enumeration_type:
8796 case DW_TAG_enumerator:
8797 /* These tags always have simple identifiers already; no need
8798 to canonicalize them. */
8799 part_die->name = DW_STRING (&attr);
8800 break;
8801 default:
8802 part_die->name
8803 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8804 &cu->objfile->objfile_obstack);
71c25dea
TT
8805 break;
8806 }
c906108c 8807 break;
31ef98ae 8808 case DW_AT_linkage_name:
c906108c 8809 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8810 /* Note that both forms of linkage name might appear. We
8811 assume they will be the same, and we only store the last
8812 one we see. */
94af9270
KS
8813 if (cu->language == language_ada)
8814 part_die->name = DW_STRING (&attr);
c906108c
SS
8815 break;
8816 case DW_AT_low_pc:
8817 has_low_pc_attr = 1;
8818 part_die->lowpc = DW_ADDR (&attr);
8819 break;
8820 case DW_AT_high_pc:
8821 has_high_pc_attr = 1;
8822 part_die->highpc = DW_ADDR (&attr);
8823 break;
8824 case DW_AT_location:
8e19ed76
PS
8825 /* Support the .debug_loc offsets */
8826 if (attr_form_is_block (&attr))
8827 {
8828 part_die->locdesc = DW_BLOCK (&attr);
8829 }
3690dd37 8830 else if (attr_form_is_section_offset (&attr))
8e19ed76 8831 {
4d3c2250 8832 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8833 }
8834 else
8835 {
4d3c2250
KB
8836 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8837 "partial symbol information");
8e19ed76 8838 }
c906108c 8839 break;
c906108c
SS
8840 case DW_AT_external:
8841 part_die->is_external = DW_UNSND (&attr);
8842 break;
8843 case DW_AT_declaration:
8844 part_die->is_declaration = DW_UNSND (&attr);
8845 break;
8846 case DW_AT_type:
8847 part_die->has_type = 1;
8848 break;
8849 case DW_AT_abstract_origin:
8850 case DW_AT_specification:
72bf9492
DJ
8851 case DW_AT_extension:
8852 part_die->has_specification = 1;
c764a876 8853 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8854 break;
8855 case DW_AT_sibling:
8856 /* Ignore absolute siblings, they might point outside of
8857 the current compile unit. */
8858 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8859 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8860 else
93311388 8861 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8862 break;
fa4028e9
JB
8863 case DW_AT_byte_size:
8864 part_die->has_byte_size = 1;
8865 break;
68511cec
CES
8866 case DW_AT_calling_convention:
8867 /* DWARF doesn't provide a way to identify a program's source-level
8868 entry point. DW_AT_calling_convention attributes are only meant
8869 to describe functions' calling conventions.
8870
8871 However, because it's a necessary piece of information in
8872 Fortran, and because DW_CC_program is the only piece of debugging
8873 information whose definition refers to a 'main program' at all,
8874 several compilers have begun marking Fortran main programs with
8875 DW_CC_program --- even when those functions use the standard
8876 calling conventions.
8877
8878 So until DWARF specifies a way to provide this information and
8879 compilers pick up the new representation, we'll support this
8880 practice. */
8881 if (DW_UNSND (&attr) == DW_CC_program
8882 && cu->language == language_fortran)
8883 set_main_name (part_die->name);
8884 break;
c906108c
SS
8885 default:
8886 break;
8887 }
8888 }
8889
c906108c
SS
8890 /* When using the GNU linker, .gnu.linkonce. sections are used to
8891 eliminate duplicate copies of functions and vtables and such.
8892 The linker will arbitrarily choose one and discard the others.
8893 The AT_*_pc values for such functions refer to local labels in
8894 these sections. If the section from that file was discarded, the
8895 labels are not in the output, so the relocs get a value of 0.
8896 If this is a discarded function, mark the pc bounds as invalid,
8897 so that GDB will ignore it. */
8898 if (has_low_pc_attr && has_high_pc_attr
8899 && part_die->lowpc < part_die->highpc
8900 && (part_die->lowpc != 0
72dca2f5 8901 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8902 part_die->has_pc_info = 1;
85cbf3d3 8903
c906108c
SS
8904 return info_ptr;
8905}
8906
72bf9492
DJ
8907/* Find a cached partial DIE at OFFSET in CU. */
8908
8909static struct partial_die_info *
c764a876 8910find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8911{
8912 struct partial_die_info *lookup_die = NULL;
8913 struct partial_die_info part_die;
8914
8915 part_die.offset = offset;
8916 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8917
72bf9492
DJ
8918 return lookup_die;
8919}
8920
348e048f
DE
8921/* Find a partial DIE at OFFSET, which may or may not be in CU,
8922 except in the case of .debug_types DIEs which do not reference
8923 outside their CU (they do however referencing other types via
8924 DW_FORM_sig8). */
72bf9492
DJ
8925
8926static struct partial_die_info *
c764a876 8927find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8928{
5afb4e99
DJ
8929 struct dwarf2_per_cu_data *per_cu = NULL;
8930 struct partial_die_info *pd = NULL;
72bf9492 8931
348e048f
DE
8932 if (cu->per_cu->from_debug_types)
8933 {
8934 pd = find_partial_die_in_comp_unit (offset, cu);
8935 if (pd != NULL)
8936 return pd;
8937 goto not_found;
8938 }
8939
45452591 8940 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8941 {
8942 pd = find_partial_die_in_comp_unit (offset, cu);
8943 if (pd != NULL)
8944 return pd;
8945 }
72bf9492 8946
ae038cb0
DJ
8947 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8948
98bfdba5
PA
8949 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8950 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8951
8952 per_cu->cu->last_used = 0;
5afb4e99
DJ
8953 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8954
8955 if (pd == NULL && per_cu->load_all_dies == 0)
8956 {
8957 struct cleanup *back_to;
8958 struct partial_die_info comp_unit_die;
8959 struct abbrev_info *abbrev;
8960 unsigned int bytes_read;
8961 char *info_ptr;
8962
8963 per_cu->load_all_dies = 1;
8964
8965 /* Re-read the DIEs. */
8966 back_to = make_cleanup (null_cleanup, 0);
8967 if (per_cu->cu->dwarf2_abbrevs == NULL)
8968 {
8969 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8970 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8971 }
dce234bc 8972 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8973 + per_cu->cu->header.offset
8974 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8975 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8976 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8977 per_cu->cu->objfile->obfd,
8978 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8979 per_cu->cu);
8980 if (comp_unit_die.has_children)
93311388
DE
8981 load_partial_dies (per_cu->cu->objfile->obfd,
8982 dwarf2_per_objfile->info.buffer, info_ptr,
8983 0, per_cu->cu);
5afb4e99
DJ
8984 do_cleanups (back_to);
8985
8986 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8987 }
8988
348e048f
DE
8989 not_found:
8990
5afb4e99
DJ
8991 if (pd == NULL)
8992 internal_error (__FILE__, __LINE__,
c764a876 8993 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8994 offset, bfd_get_filename (cu->objfile->obfd));
8995 return pd;
72bf9492
DJ
8996}
8997
8998/* Adjust PART_DIE before generating a symbol for it. This function
8999 may set the is_external flag or change the DIE's name. */
9000
9001static void
9002fixup_partial_die (struct partial_die_info *part_die,
9003 struct dwarf2_cu *cu)
9004{
9005 /* If we found a reference attribute and the DIE has no name, try
9006 to find a name in the referred to DIE. */
9007
9008 if (part_die->name == NULL && part_die->has_specification)
9009 {
9010 struct partial_die_info *spec_die;
72bf9492 9011
10b3939b 9012 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9013
10b3939b 9014 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9015
9016 if (spec_die->name)
9017 {
9018 part_die->name = spec_die->name;
9019
9020 /* Copy DW_AT_external attribute if it is set. */
9021 if (spec_die->is_external)
9022 part_die->is_external = spec_die->is_external;
9023 }
9024 }
9025
9026 /* Set default names for some unnamed DIEs. */
9027 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
9028 || part_die->tag == DW_TAG_class_type))
9029 part_die->name = "(anonymous class)";
9030
9031 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9032 part_die->name = "(anonymous namespace)";
9033
9034 if (part_die->tag == DW_TAG_structure_type
9035 || part_die->tag == DW_TAG_class_type
9036 || part_die->tag == DW_TAG_union_type)
9037 guess_structure_name (part_die, cu);
9038}
9039
a8329558 9040/* Read an attribute value described by an attribute form. */
c906108c 9041
fe1b8b76 9042static gdb_byte *
a8329558 9043read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9044 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9045 struct dwarf2_cu *cu)
c906108c 9046{
e7c27a73 9047 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9048 unsigned int bytes_read;
9049 struct dwarf_block *blk;
9050
a8329558
KW
9051 attr->form = form;
9052 switch (form)
c906108c 9053 {
c906108c 9054 case DW_FORM_ref_addr:
ae411497
TT
9055 if (cu->header.version == 2)
9056 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9057 else
9058 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9059 info_ptr += bytes_read;
9060 break;
9061 case DW_FORM_addr:
e7c27a73 9062 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9063 info_ptr += bytes_read;
c906108c
SS
9064 break;
9065 case DW_FORM_block2:
7b5a2f43 9066 blk = dwarf_alloc_block (cu);
c906108c
SS
9067 blk->size = read_2_bytes (abfd, info_ptr);
9068 info_ptr += 2;
9069 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9070 info_ptr += blk->size;
9071 DW_BLOCK (attr) = blk;
9072 break;
9073 case DW_FORM_block4:
7b5a2f43 9074 blk = dwarf_alloc_block (cu);
c906108c
SS
9075 blk->size = read_4_bytes (abfd, info_ptr);
9076 info_ptr += 4;
9077 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9078 info_ptr += blk->size;
9079 DW_BLOCK (attr) = blk;
9080 break;
9081 case DW_FORM_data2:
9082 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9083 info_ptr += 2;
9084 break;
9085 case DW_FORM_data4:
9086 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9087 info_ptr += 4;
9088 break;
9089 case DW_FORM_data8:
9090 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9091 info_ptr += 8;
9092 break;
2dc7f7b3
TT
9093 case DW_FORM_sec_offset:
9094 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9095 info_ptr += bytes_read;
9096 break;
c906108c 9097 case DW_FORM_string:
9b1c24c8 9098 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9099 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9100 info_ptr += bytes_read;
9101 break;
4bdf3d34
JJ
9102 case DW_FORM_strp:
9103 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9104 &bytes_read);
8285870a 9105 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9106 info_ptr += bytes_read;
9107 break;
2dc7f7b3 9108 case DW_FORM_exprloc:
c906108c 9109 case DW_FORM_block:
7b5a2f43 9110 blk = dwarf_alloc_block (cu);
c906108c
SS
9111 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9112 info_ptr += bytes_read;
9113 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9114 info_ptr += blk->size;
9115 DW_BLOCK (attr) = blk;
9116 break;
9117 case DW_FORM_block1:
7b5a2f43 9118 blk = dwarf_alloc_block (cu);
c906108c
SS
9119 blk->size = read_1_byte (abfd, info_ptr);
9120 info_ptr += 1;
9121 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9122 info_ptr += blk->size;
9123 DW_BLOCK (attr) = blk;
9124 break;
9125 case DW_FORM_data1:
9126 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9127 info_ptr += 1;
9128 break;
9129 case DW_FORM_flag:
9130 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9131 info_ptr += 1;
9132 break;
2dc7f7b3
TT
9133 case DW_FORM_flag_present:
9134 DW_UNSND (attr) = 1;
9135 break;
c906108c
SS
9136 case DW_FORM_sdata:
9137 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9138 info_ptr += bytes_read;
9139 break;
9140 case DW_FORM_udata:
9141 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9142 info_ptr += bytes_read;
9143 break;
9144 case DW_FORM_ref1:
10b3939b 9145 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9146 info_ptr += 1;
9147 break;
9148 case DW_FORM_ref2:
10b3939b 9149 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9150 info_ptr += 2;
9151 break;
9152 case DW_FORM_ref4:
10b3939b 9153 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9154 info_ptr += 4;
9155 break;
613e1657 9156 case DW_FORM_ref8:
10b3939b 9157 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9158 info_ptr += 8;
9159 break;
348e048f
DE
9160 case DW_FORM_sig8:
9161 /* Convert the signature to something we can record in DW_UNSND
9162 for later lookup.
9163 NOTE: This is NULL if the type wasn't found. */
9164 DW_SIGNATURED_TYPE (attr) =
9165 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9166 info_ptr += 8;
9167 break;
c906108c 9168 case DW_FORM_ref_udata:
10b3939b
DJ
9169 DW_ADDR (attr) = (cu->header.offset
9170 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9171 info_ptr += bytes_read;
9172 break;
c906108c 9173 case DW_FORM_indirect:
a8329558
KW
9174 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9175 info_ptr += bytes_read;
e7c27a73 9176 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9177 break;
c906108c 9178 default:
8a3fe4f8 9179 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9180 dwarf_form_name (form),
9181 bfd_get_filename (abfd));
c906108c 9182 }
28e94949
JB
9183
9184 /* We have seen instances where the compiler tried to emit a byte
9185 size attribute of -1 which ended up being encoded as an unsigned
9186 0xffffffff. Although 0xffffffff is technically a valid size value,
9187 an object of this size seems pretty unlikely so we can relatively
9188 safely treat these cases as if the size attribute was invalid and
9189 treat them as zero by default. */
9190 if (attr->name == DW_AT_byte_size
9191 && form == DW_FORM_data4
9192 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9193 {
9194 complaint
9195 (&symfile_complaints,
43bbcdc2
PH
9196 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9197 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9198 DW_UNSND (attr) = 0;
9199 }
28e94949 9200
c906108c
SS
9201 return info_ptr;
9202}
9203
a8329558
KW
9204/* Read an attribute described by an abbreviated attribute. */
9205
fe1b8b76 9206static gdb_byte *
a8329558 9207read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9208 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9209{
9210 attr->name = abbrev->name;
e7c27a73 9211 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9212}
9213
c906108c
SS
9214/* read dwarf information from a buffer */
9215
9216static unsigned int
fe1b8b76 9217read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9218{
fe1b8b76 9219 return bfd_get_8 (abfd, buf);
c906108c
SS
9220}
9221
9222static int
fe1b8b76 9223read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9224{
fe1b8b76 9225 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9226}
9227
9228static unsigned int
fe1b8b76 9229read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9230{
fe1b8b76 9231 return bfd_get_16 (abfd, buf);
c906108c
SS
9232}
9233
9234static int
fe1b8b76 9235read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9236{
fe1b8b76 9237 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9238}
9239
9240static unsigned int
fe1b8b76 9241read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9242{
fe1b8b76 9243 return bfd_get_32 (abfd, buf);
c906108c
SS
9244}
9245
9246static int
fe1b8b76 9247read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9248{
fe1b8b76 9249 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9250}
9251
93311388 9252static ULONGEST
fe1b8b76 9253read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9254{
fe1b8b76 9255 return bfd_get_64 (abfd, buf);
c906108c
SS
9256}
9257
9258static CORE_ADDR
fe1b8b76 9259read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9260 unsigned int *bytes_read)
c906108c 9261{
e7c27a73 9262 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9263 CORE_ADDR retval = 0;
9264
107d2387 9265 if (cu_header->signed_addr_p)
c906108c 9266 {
107d2387
AC
9267 switch (cu_header->addr_size)
9268 {
9269 case 2:
fe1b8b76 9270 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9271 break;
9272 case 4:
fe1b8b76 9273 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9274 break;
9275 case 8:
fe1b8b76 9276 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9277 break;
9278 default:
8e65ff28 9279 internal_error (__FILE__, __LINE__,
e2e0b3e5 9280 _("read_address: bad switch, signed [in module %s]"),
659b0389 9281 bfd_get_filename (abfd));
107d2387
AC
9282 }
9283 }
9284 else
9285 {
9286 switch (cu_header->addr_size)
9287 {
9288 case 2:
fe1b8b76 9289 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9290 break;
9291 case 4:
fe1b8b76 9292 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9293 break;
9294 case 8:
fe1b8b76 9295 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9296 break;
9297 default:
8e65ff28 9298 internal_error (__FILE__, __LINE__,
e2e0b3e5 9299 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9300 bfd_get_filename (abfd));
107d2387 9301 }
c906108c 9302 }
64367e0a 9303
107d2387
AC
9304 *bytes_read = cu_header->addr_size;
9305 return retval;
c906108c
SS
9306}
9307
f7ef9339 9308/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9309 specification allows the initial length to take up either 4 bytes
9310 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9311 bytes describe the length and all offsets will be 8 bytes in length
9312 instead of 4.
9313
f7ef9339
KB
9314 An older, non-standard 64-bit format is also handled by this
9315 function. The older format in question stores the initial length
9316 as an 8-byte quantity without an escape value. Lengths greater
9317 than 2^32 aren't very common which means that the initial 4 bytes
9318 is almost always zero. Since a length value of zero doesn't make
9319 sense for the 32-bit format, this initial zero can be considered to
9320 be an escape value which indicates the presence of the older 64-bit
9321 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9322 greater than 4GB. If it becomes necessary to handle lengths
9323 somewhat larger than 4GB, we could allow other small values (such
9324 as the non-sensical values of 1, 2, and 3) to also be used as
9325 escape values indicating the presence of the old format.
f7ef9339 9326
917c78fc
MK
9327 The value returned via bytes_read should be used to increment the
9328 relevant pointer after calling read_initial_length().
c764a876 9329
613e1657
KB
9330 [ Note: read_initial_length() and read_offset() are based on the
9331 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9332 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9333 from:
9334
f7ef9339 9335 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9336
613e1657
KB
9337 This document is only a draft and is subject to change. (So beware.)
9338
f7ef9339 9339 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9340 determined empirically by examining 64-bit ELF files produced by
9341 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9342
9343 - Kevin, July 16, 2002
613e1657
KB
9344 ] */
9345
9346static LONGEST
c764a876 9347read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9348{
fe1b8b76 9349 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9350
dd373385 9351 if (length == 0xffffffff)
613e1657 9352 {
fe1b8b76 9353 length = bfd_get_64 (abfd, buf + 4);
613e1657 9354 *bytes_read = 12;
613e1657 9355 }
dd373385 9356 else if (length == 0)
f7ef9339 9357 {
dd373385 9358 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9359 length = bfd_get_64 (abfd, buf);
f7ef9339 9360 *bytes_read = 8;
f7ef9339 9361 }
613e1657
KB
9362 else
9363 {
9364 *bytes_read = 4;
613e1657
KB
9365 }
9366
c764a876
DE
9367 return length;
9368}
dd373385 9369
c764a876
DE
9370/* Cover function for read_initial_length.
9371 Returns the length of the object at BUF, and stores the size of the
9372 initial length in *BYTES_READ and stores the size that offsets will be in
9373 *OFFSET_SIZE.
9374 If the initial length size is not equivalent to that specified in
9375 CU_HEADER then issue a complaint.
9376 This is useful when reading non-comp-unit headers. */
dd373385 9377
c764a876
DE
9378static LONGEST
9379read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9380 const struct comp_unit_head *cu_header,
9381 unsigned int *bytes_read,
9382 unsigned int *offset_size)
9383{
9384 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9385
9386 gdb_assert (cu_header->initial_length_size == 4
9387 || cu_header->initial_length_size == 8
9388 || cu_header->initial_length_size == 12);
9389
9390 if (cu_header->initial_length_size != *bytes_read)
9391 complaint (&symfile_complaints,
9392 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9393
c764a876 9394 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9395 return length;
613e1657
KB
9396}
9397
9398/* Read an offset from the data stream. The size of the offset is
917c78fc 9399 given by cu_header->offset_size. */
613e1657
KB
9400
9401static LONGEST
fe1b8b76 9402read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9403 unsigned int *bytes_read)
c764a876
DE
9404{
9405 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9406
c764a876
DE
9407 *bytes_read = cu_header->offset_size;
9408 return offset;
9409}
9410
9411/* Read an offset from the data stream. */
9412
9413static LONGEST
9414read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9415{
9416 LONGEST retval = 0;
9417
c764a876 9418 switch (offset_size)
613e1657
KB
9419 {
9420 case 4:
fe1b8b76 9421 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9422 break;
9423 case 8:
fe1b8b76 9424 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9425 break;
9426 default:
8e65ff28 9427 internal_error (__FILE__, __LINE__,
c764a876 9428 _("read_offset_1: bad switch [in module %s]"),
659b0389 9429 bfd_get_filename (abfd));
613e1657
KB
9430 }
9431
917c78fc 9432 return retval;
613e1657
KB
9433}
9434
fe1b8b76
JB
9435static gdb_byte *
9436read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9437{
9438 /* If the size of a host char is 8 bits, we can return a pointer
9439 to the buffer, otherwise we have to copy the data to a buffer
9440 allocated on the temporary obstack. */
4bdf3d34 9441 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9442 return buf;
c906108c
SS
9443}
9444
9445static char *
9b1c24c8 9446read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9447{
9448 /* If the size of a host char is 8 bits, we can return a pointer
9449 to the string, otherwise we have to copy the string to a buffer
9450 allocated on the temporary obstack. */
4bdf3d34 9451 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9452 if (*buf == '\0')
9453 {
9454 *bytes_read_ptr = 1;
9455 return NULL;
9456 }
fe1b8b76
JB
9457 *bytes_read_ptr = strlen ((char *) buf) + 1;
9458 return (char *) buf;
4bdf3d34
JJ
9459}
9460
9461static char *
fe1b8b76 9462read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9463 const struct comp_unit_head *cu_header,
9464 unsigned int *bytes_read_ptr)
9465{
c764a876 9466 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9467
be391dca 9468 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9469 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9470 {
8a3fe4f8 9471 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9472 bfd_get_filename (abfd));
4bdf3d34 9473 return NULL;
c906108c 9474 }
dce234bc 9475 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9476 {
8a3fe4f8 9477 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9478 bfd_get_filename (abfd));
c906108c
SS
9479 return NULL;
9480 }
4bdf3d34 9481 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9482 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9483 return NULL;
dce234bc 9484 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9485}
9486
ce5d95e1 9487static unsigned long
fe1b8b76 9488read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9489{
ce5d95e1
JB
9490 unsigned long result;
9491 unsigned int num_read;
c906108c
SS
9492 int i, shift;
9493 unsigned char byte;
9494
9495 result = 0;
9496 shift = 0;
9497 num_read = 0;
9498 i = 0;
9499 while (1)
9500 {
fe1b8b76 9501 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9502 buf++;
9503 num_read++;
ce5d95e1 9504 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9505 if ((byte & 128) == 0)
9506 {
9507 break;
9508 }
9509 shift += 7;
9510 }
9511 *bytes_read_ptr = num_read;
9512 return result;
9513}
9514
ce5d95e1 9515static long
fe1b8b76 9516read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9517{
ce5d95e1 9518 long result;
77e0b926 9519 int i, shift, num_read;
c906108c
SS
9520 unsigned char byte;
9521
9522 result = 0;
9523 shift = 0;
c906108c
SS
9524 num_read = 0;
9525 i = 0;
9526 while (1)
9527 {
fe1b8b76 9528 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9529 buf++;
9530 num_read++;
ce5d95e1 9531 result |= ((long)(byte & 127) << shift);
c906108c
SS
9532 shift += 7;
9533 if ((byte & 128) == 0)
9534 {
9535 break;
9536 }
9537 }
77e0b926
DJ
9538 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9539 result |= -(((long)1) << shift);
c906108c
SS
9540 *bytes_read_ptr = num_read;
9541 return result;
9542}
9543
4bb7a0a7
DJ
9544/* Return a pointer to just past the end of an LEB128 number in BUF. */
9545
fe1b8b76
JB
9546static gdb_byte *
9547skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9548{
9549 int byte;
9550
9551 while (1)
9552 {
fe1b8b76 9553 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9554 buf++;
9555 if ((byte & 128) == 0)
9556 return buf;
9557 }
9558}
9559
c906108c 9560static void
e142c38c 9561set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9562{
9563 switch (lang)
9564 {
9565 case DW_LANG_C89:
76bee0cc 9566 case DW_LANG_C99:
c906108c 9567 case DW_LANG_C:
e142c38c 9568 cu->language = language_c;
c906108c
SS
9569 break;
9570 case DW_LANG_C_plus_plus:
e142c38c 9571 cu->language = language_cplus;
c906108c 9572 break;
6aecb9c2
JB
9573 case DW_LANG_D:
9574 cu->language = language_d;
9575 break;
c906108c
SS
9576 case DW_LANG_Fortran77:
9577 case DW_LANG_Fortran90:
b21b22e0 9578 case DW_LANG_Fortran95:
e142c38c 9579 cu->language = language_fortran;
c906108c
SS
9580 break;
9581 case DW_LANG_Mips_Assembler:
e142c38c 9582 cu->language = language_asm;
c906108c 9583 break;
bebd888e 9584 case DW_LANG_Java:
e142c38c 9585 cu->language = language_java;
bebd888e 9586 break;
c906108c 9587 case DW_LANG_Ada83:
8aaf0b47 9588 case DW_LANG_Ada95:
bc5f45f8
JB
9589 cu->language = language_ada;
9590 break;
72019c9c
GM
9591 case DW_LANG_Modula2:
9592 cu->language = language_m2;
9593 break;
fe8e67fd
PM
9594 case DW_LANG_Pascal83:
9595 cu->language = language_pascal;
9596 break;
22566fbd
DJ
9597 case DW_LANG_ObjC:
9598 cu->language = language_objc;
9599 break;
c906108c
SS
9600 case DW_LANG_Cobol74:
9601 case DW_LANG_Cobol85:
c906108c 9602 default:
e142c38c 9603 cu->language = language_minimal;
c906108c
SS
9604 break;
9605 }
e142c38c 9606 cu->language_defn = language_def (cu->language);
c906108c
SS
9607}
9608
9609/* Return the named attribute or NULL if not there. */
9610
9611static struct attribute *
e142c38c 9612dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9613{
9614 unsigned int i;
9615 struct attribute *spec = NULL;
9616
9617 for (i = 0; i < die->num_attrs; ++i)
9618 {
9619 if (die->attrs[i].name == name)
10b3939b 9620 return &die->attrs[i];
c906108c
SS
9621 if (die->attrs[i].name == DW_AT_specification
9622 || die->attrs[i].name == DW_AT_abstract_origin)
9623 spec = &die->attrs[i];
9624 }
c906108c 9625
10b3939b 9626 if (spec)
f2f0e013
DJ
9627 {
9628 die = follow_die_ref (die, spec, &cu);
9629 return dwarf2_attr (die, name, cu);
9630 }
c5aa993b 9631
c906108c
SS
9632 return NULL;
9633}
9634
348e048f
DE
9635/* Return the named attribute or NULL if not there,
9636 but do not follow DW_AT_specification, etc.
9637 This is for use in contexts where we're reading .debug_types dies.
9638 Following DW_AT_specification, DW_AT_abstract_origin will take us
9639 back up the chain, and we want to go down. */
9640
9641static struct attribute *
9642dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9643 struct dwarf2_cu *cu)
9644{
9645 unsigned int i;
9646
9647 for (i = 0; i < die->num_attrs; ++i)
9648 if (die->attrs[i].name == name)
9649 return &die->attrs[i];
9650
9651 return NULL;
9652}
9653
05cf31d1
JB
9654/* Return non-zero iff the attribute NAME is defined for the given DIE,
9655 and holds a non-zero value. This function should only be used for
2dc7f7b3 9656 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9657
9658static int
9659dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9660{
9661 struct attribute *attr = dwarf2_attr (die, name, cu);
9662
9663 return (attr && DW_UNSND (attr));
9664}
9665
3ca72b44 9666static int
e142c38c 9667die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9668{
05cf31d1
JB
9669 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9670 which value is non-zero. However, we have to be careful with
9671 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9672 (via dwarf2_flag_true_p) follows this attribute. So we may
9673 end up accidently finding a declaration attribute that belongs
9674 to a different DIE referenced by the specification attribute,
9675 even though the given DIE does not have a declaration attribute. */
9676 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9677 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9678}
9679
63d06c5c 9680/* Return the die giving the specification for DIE, if there is
f2f0e013 9681 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9682 containing the return value on output. If there is no
9683 specification, but there is an abstract origin, that is
9684 returned. */
63d06c5c
DC
9685
9686static struct die_info *
f2f0e013 9687die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9688{
f2f0e013
DJ
9689 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9690 *spec_cu);
63d06c5c 9691
edb3359d
DJ
9692 if (spec_attr == NULL)
9693 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9694
63d06c5c
DC
9695 if (spec_attr == NULL)
9696 return NULL;
9697 else
f2f0e013 9698 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9699}
c906108c 9700
debd256d
JB
9701/* Free the line_header structure *LH, and any arrays and strings it
9702 refers to. */
9703static void
9704free_line_header (struct line_header *lh)
9705{
9706 if (lh->standard_opcode_lengths)
a8bc7b56 9707 xfree (lh->standard_opcode_lengths);
debd256d
JB
9708
9709 /* Remember that all the lh->file_names[i].name pointers are
9710 pointers into debug_line_buffer, and don't need to be freed. */
9711 if (lh->file_names)
a8bc7b56 9712 xfree (lh->file_names);
debd256d
JB
9713
9714 /* Similarly for the include directory names. */
9715 if (lh->include_dirs)
a8bc7b56 9716 xfree (lh->include_dirs);
debd256d 9717
a8bc7b56 9718 xfree (lh);
debd256d
JB
9719}
9720
9721
9722/* Add an entry to LH's include directory table. */
9723static void
9724add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9725{
debd256d
JB
9726 /* Grow the array if necessary. */
9727 if (lh->include_dirs_size == 0)
c5aa993b 9728 {
debd256d
JB
9729 lh->include_dirs_size = 1; /* for testing */
9730 lh->include_dirs = xmalloc (lh->include_dirs_size
9731 * sizeof (*lh->include_dirs));
9732 }
9733 else if (lh->num_include_dirs >= lh->include_dirs_size)
9734 {
9735 lh->include_dirs_size *= 2;
9736 lh->include_dirs = xrealloc (lh->include_dirs,
9737 (lh->include_dirs_size
9738 * sizeof (*lh->include_dirs)));
c5aa993b 9739 }
c906108c 9740
debd256d
JB
9741 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9742}
6e70227d 9743
debd256d
JB
9744
9745/* Add an entry to LH's file name table. */
9746static void
9747add_file_name (struct line_header *lh,
9748 char *name,
9749 unsigned int dir_index,
9750 unsigned int mod_time,
9751 unsigned int length)
9752{
9753 struct file_entry *fe;
9754
9755 /* Grow the array if necessary. */
9756 if (lh->file_names_size == 0)
9757 {
9758 lh->file_names_size = 1; /* for testing */
9759 lh->file_names = xmalloc (lh->file_names_size
9760 * sizeof (*lh->file_names));
9761 }
9762 else if (lh->num_file_names >= lh->file_names_size)
9763 {
9764 lh->file_names_size *= 2;
9765 lh->file_names = xrealloc (lh->file_names,
9766 (lh->file_names_size
9767 * sizeof (*lh->file_names)));
9768 }
9769
9770 fe = &lh->file_names[lh->num_file_names++];
9771 fe->name = name;
9772 fe->dir_index = dir_index;
9773 fe->mod_time = mod_time;
9774 fe->length = length;
aaa75496 9775 fe->included_p = 0;
cb1df416 9776 fe->symtab = NULL;
debd256d 9777}
6e70227d 9778
debd256d
JB
9779
9780/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9781 .debug_line, according to the endianness of ABFD. Return a pointer
9782 to a struct line_header, allocated using xmalloc.
debd256d
JB
9783
9784 NOTE: the strings in the include directory and file name tables of
9785 the returned object point into debug_line_buffer, and must not be
9786 freed. */
9787static struct line_header *
9788dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9789 struct dwarf2_cu *cu)
debd256d
JB
9790{
9791 struct cleanup *back_to;
9792 struct line_header *lh;
fe1b8b76 9793 gdb_byte *line_ptr;
c764a876 9794 unsigned int bytes_read, offset_size;
debd256d
JB
9795 int i;
9796 char *cur_dir, *cur_file;
9797
be391dca 9798 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9799 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9800 {
e2e0b3e5 9801 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9802 return 0;
9803 }
9804
a738430d
MK
9805 /* Make sure that at least there's room for the total_length field.
9806 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9807 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9808 {
4d3c2250 9809 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9810 return 0;
9811 }
9812
9813 lh = xmalloc (sizeof (*lh));
9814 memset (lh, 0, sizeof (*lh));
9815 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9816 (void *) lh);
9817
dce234bc 9818 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9819
a738430d 9820 /* Read in the header. */
6e70227d 9821 lh->total_length =
c764a876
DE
9822 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9823 &bytes_read, &offset_size);
debd256d 9824 line_ptr += bytes_read;
dce234bc
PP
9825 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9826 + dwarf2_per_objfile->line.size))
debd256d 9827 {
4d3c2250 9828 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9829 return 0;
9830 }
9831 lh->statement_program_end = line_ptr + lh->total_length;
9832 lh->version = read_2_bytes (abfd, line_ptr);
9833 line_ptr += 2;
c764a876
DE
9834 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9835 line_ptr += offset_size;
debd256d
JB
9836 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9837 line_ptr += 1;
2dc7f7b3
TT
9838 if (lh->version >= 4)
9839 {
9840 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9841 line_ptr += 1;
9842 }
9843 else
9844 lh->maximum_ops_per_instruction = 1;
9845
9846 if (lh->maximum_ops_per_instruction == 0)
9847 {
9848 lh->maximum_ops_per_instruction = 1;
9849 complaint (&symfile_complaints,
9850 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9851 }
9852
debd256d
JB
9853 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9854 line_ptr += 1;
9855 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9856 line_ptr += 1;
9857 lh->line_range = read_1_byte (abfd, line_ptr);
9858 line_ptr += 1;
9859 lh->opcode_base = read_1_byte (abfd, line_ptr);
9860 line_ptr += 1;
9861 lh->standard_opcode_lengths
fe1b8b76 9862 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9863
9864 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9865 for (i = 1; i < lh->opcode_base; ++i)
9866 {
9867 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9868 line_ptr += 1;
9869 }
9870
a738430d 9871 /* Read directory table. */
9b1c24c8 9872 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9873 {
9874 line_ptr += bytes_read;
9875 add_include_dir (lh, cur_dir);
9876 }
9877 line_ptr += bytes_read;
9878
a738430d 9879 /* Read file name table. */
9b1c24c8 9880 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9881 {
9882 unsigned int dir_index, mod_time, length;
9883
9884 line_ptr += bytes_read;
9885 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9886 line_ptr += bytes_read;
9887 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9888 line_ptr += bytes_read;
9889 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9890 line_ptr += bytes_read;
9891
9892 add_file_name (lh, cur_file, dir_index, mod_time, length);
9893 }
9894 line_ptr += bytes_read;
6e70227d 9895 lh->statement_program_start = line_ptr;
debd256d 9896
dce234bc
PP
9897 if (line_ptr > (dwarf2_per_objfile->line.buffer
9898 + dwarf2_per_objfile->line.size))
4d3c2250 9899 complaint (&symfile_complaints,
e2e0b3e5 9900 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9901
9902 discard_cleanups (back_to);
9903 return lh;
9904}
c906108c 9905
5fb290d7
DJ
9906/* This function exists to work around a bug in certain compilers
9907 (particularly GCC 2.95), in which the first line number marker of a
9908 function does not show up until after the prologue, right before
9909 the second line number marker. This function shifts ADDRESS down
9910 to the beginning of the function if necessary, and is called on
9911 addresses passed to record_line. */
9912
9913static CORE_ADDR
e142c38c 9914check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9915{
9916 struct function_range *fn;
9917
9918 /* Find the function_range containing address. */
e142c38c 9919 if (!cu->first_fn)
5fb290d7
DJ
9920 return address;
9921
e142c38c
DJ
9922 if (!cu->cached_fn)
9923 cu->cached_fn = cu->first_fn;
5fb290d7 9924
e142c38c 9925 fn = cu->cached_fn;
5fb290d7
DJ
9926 while (fn)
9927 if (fn->lowpc <= address && fn->highpc > address)
9928 goto found;
9929 else
9930 fn = fn->next;
9931
e142c38c
DJ
9932 fn = cu->first_fn;
9933 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9934 if (fn->lowpc <= address && fn->highpc > address)
9935 goto found;
9936 else
9937 fn = fn->next;
9938
9939 return address;
9940
9941 found:
9942 if (fn->seen_line)
9943 return address;
9944 if (address != fn->lowpc)
4d3c2250 9945 complaint (&symfile_complaints,
e2e0b3e5 9946 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9947 (unsigned long) address, fn->name);
5fb290d7
DJ
9948 fn->seen_line = 1;
9949 return fn->lowpc;
9950}
9951
c6da4cef
DE
9952/* Subroutine of dwarf_decode_lines to simplify it.
9953 Return the file name of the psymtab for included file FILE_INDEX
9954 in line header LH of PST.
9955 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9956 If space for the result is malloc'd, it will be freed by a cleanup.
9957 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
9958
9959static char *
9960psymtab_include_file_name (const struct line_header *lh, int file_index,
9961 const struct partial_symtab *pst,
9962 const char *comp_dir)
9963{
9964 const struct file_entry fe = lh->file_names [file_index];
9965 char *include_name = fe.name;
9966 char *include_name_to_compare = include_name;
9967 char *dir_name = NULL;
9968 char *pst_filename;
9969 int file_is_pst;
9970
9971 if (fe.dir_index)
9972 dir_name = lh->include_dirs[fe.dir_index - 1];
9973
9974 if (!IS_ABSOLUTE_PATH (include_name)
9975 && (dir_name != NULL || comp_dir != NULL))
9976 {
9977 /* Avoid creating a duplicate psymtab for PST.
9978 We do this by comparing INCLUDE_NAME and PST_FILENAME.
9979 Before we do the comparison, however, we need to account
9980 for DIR_NAME and COMP_DIR.
9981 First prepend dir_name (if non-NULL). If we still don't
9982 have an absolute path prepend comp_dir (if non-NULL).
9983 However, the directory we record in the include-file's
9984 psymtab does not contain COMP_DIR (to match the
9985 corresponding symtab(s)).
9986
9987 Example:
9988
9989 bash$ cd /tmp
9990 bash$ gcc -g ./hello.c
9991 include_name = "hello.c"
9992 dir_name = "."
9993 DW_AT_comp_dir = comp_dir = "/tmp"
9994 DW_AT_name = "./hello.c" */
9995
9996 if (dir_name != NULL)
9997 {
9998 include_name = concat (dir_name, SLASH_STRING,
9999 include_name, (char *)NULL);
10000 include_name_to_compare = include_name;
10001 make_cleanup (xfree, include_name);
10002 }
10003 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10004 {
10005 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10006 include_name, (char *)NULL);
10007 }
10008 }
10009
10010 pst_filename = pst->filename;
10011 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10012 {
10013 pst_filename = concat (pst->dirname, SLASH_STRING,
10014 pst_filename, (char *)NULL);
10015 }
10016
10017 file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
10018
10019 if (include_name_to_compare != include_name)
10020 xfree (include_name_to_compare);
10021 if (pst_filename != pst->filename)
10022 xfree (pst_filename);
10023
10024 if (file_is_pst)
10025 return NULL;
10026 return include_name;
10027}
10028
aaa75496
JB
10029/* Decode the Line Number Program (LNP) for the given line_header
10030 structure and CU. The actual information extracted and the type
10031 of structures created from the LNP depends on the value of PST.
10032
10033 1. If PST is NULL, then this procedure uses the data from the program
10034 to create all necessary symbol tables, and their linetables.
6e70227d 10035
aaa75496
JB
10036 2. If PST is not NULL, this procedure reads the program to determine
10037 the list of files included by the unit represented by PST, and
c6da4cef
DE
10038 builds all the associated partial symbol tables.
10039
10040 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10041 It is used for relative paths in the line table.
10042 NOTE: When processing partial symtabs (pst != NULL),
10043 comp_dir == pst->dirname.
10044
10045 NOTE: It is important that psymtabs have the same file name (via strcmp)
10046 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10047 symtab we don't use it in the name of the psymtabs we create.
10048 E.g. expand_line_sal requires this when finding psymtabs to expand.
10049 A good testcase for this is mb-inline.exp. */
debd256d 10050
c906108c 10051static void
debd256d 10052dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 10053 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10054{
a8c50c1f 10055 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10056 gdb_byte *line_end;
a8c50c1f 10057 unsigned int bytes_read, extended_len;
c906108c 10058 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10059 CORE_ADDR baseaddr;
10060 struct objfile *objfile = cu->objfile;
fbf65064 10061 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10062 const int decode_for_pst_p = (pst != NULL);
cb1df416 10063 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10064
10065 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10066
debd256d
JB
10067 line_ptr = lh->statement_program_start;
10068 line_end = lh->statement_program_end;
c906108c
SS
10069
10070 /* Read the statement sequences until there's nothing left. */
10071 while (line_ptr < line_end)
10072 {
10073 /* state machine registers */
10074 CORE_ADDR address = 0;
10075 unsigned int file = 1;
10076 unsigned int line = 1;
10077 unsigned int column = 0;
debd256d 10078 int is_stmt = lh->default_is_stmt;
c906108c
SS
10079 int basic_block = 0;
10080 int end_sequence = 0;
fbf65064 10081 CORE_ADDR addr;
2dc7f7b3 10082 unsigned char op_index = 0;
c906108c 10083
aaa75496 10084 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10085 {
aaa75496 10086 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10087 /* lh->include_dirs and lh->file_names are 0-based, but the
10088 directory and file name numbers in the statement program
10089 are 1-based. */
10090 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10091 char *dir = NULL;
a738430d 10092
debd256d
JB
10093 if (fe->dir_index)
10094 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10095
10096 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10097 }
10098
a738430d 10099 /* Decode the table. */
c5aa993b 10100 while (!end_sequence)
c906108c
SS
10101 {
10102 op_code = read_1_byte (abfd, line_ptr);
10103 line_ptr += 1;
59205f5a
JB
10104 if (line_ptr > line_end)
10105 {
10106 dwarf2_debug_line_missing_end_sequence_complaint ();
10107 break;
10108 }
9aa1fe7e 10109
debd256d 10110 if (op_code >= lh->opcode_base)
6e70227d 10111 {
a738430d 10112 /* Special operand. */
debd256d 10113 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10114 address += (((op_index + (adj_opcode / lh->line_range))
10115 / lh->maximum_ops_per_instruction)
10116 * lh->minimum_instruction_length);
10117 op_index = ((op_index + (adj_opcode / lh->line_range))
10118 % lh->maximum_ops_per_instruction);
debd256d 10119 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10120 if (lh->num_file_names < file || file == 0)
25e43795 10121 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10122 /* For now we ignore lines not starting on an
10123 instruction boundary. */
10124 else if (op_index == 0)
25e43795
DJ
10125 {
10126 lh->file_names[file - 1].included_p = 1;
ca5f395d 10127 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10128 {
10129 if (last_subfile != current_subfile)
10130 {
10131 addr = gdbarch_addr_bits_remove (gdbarch, address);
10132 if (last_subfile)
10133 record_line (last_subfile, 0, addr);
10134 last_subfile = current_subfile;
10135 }
25e43795 10136 /* Append row to matrix using current values. */
fbf65064
UW
10137 addr = check_cu_functions (address, cu);
10138 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10139 record_line (current_subfile, line, addr);
366da635 10140 }
25e43795 10141 }
ca5f395d 10142 basic_block = 0;
9aa1fe7e
GK
10143 }
10144 else switch (op_code)
c906108c
SS
10145 {
10146 case DW_LNS_extended_op:
a8c50c1f 10147 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10148 line_ptr += bytes_read;
a8c50c1f 10149 extended_end = line_ptr + extended_len;
c906108c
SS
10150 extended_op = read_1_byte (abfd, line_ptr);
10151 line_ptr += 1;
10152 switch (extended_op)
10153 {
10154 case DW_LNE_end_sequence:
10155 end_sequence = 1;
c906108c
SS
10156 break;
10157 case DW_LNE_set_address:
e7c27a73 10158 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10159 op_index = 0;
107d2387
AC
10160 line_ptr += bytes_read;
10161 address += baseaddr;
c906108c
SS
10162 break;
10163 case DW_LNE_define_file:
debd256d
JB
10164 {
10165 char *cur_file;
10166 unsigned int dir_index, mod_time, length;
6e70227d 10167
9b1c24c8 10168 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10169 line_ptr += bytes_read;
10170 dir_index =
10171 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10172 line_ptr += bytes_read;
10173 mod_time =
10174 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10175 line_ptr += bytes_read;
10176 length =
10177 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10178 line_ptr += bytes_read;
10179 add_file_name (lh, cur_file, dir_index, mod_time, length);
10180 }
c906108c 10181 break;
d0c6ba3d
CC
10182 case DW_LNE_set_discriminator:
10183 /* The discriminator is not interesting to the debugger;
10184 just ignore it. */
10185 line_ptr = extended_end;
10186 break;
c906108c 10187 default:
4d3c2250 10188 complaint (&symfile_complaints,
e2e0b3e5 10189 _("mangled .debug_line section"));
debd256d 10190 return;
c906108c 10191 }
a8c50c1f
DJ
10192 /* Make sure that we parsed the extended op correctly. If e.g.
10193 we expected a different address size than the producer used,
10194 we may have read the wrong number of bytes. */
10195 if (line_ptr != extended_end)
10196 {
10197 complaint (&symfile_complaints,
10198 _("mangled .debug_line section"));
10199 return;
10200 }
c906108c
SS
10201 break;
10202 case DW_LNS_copy:
59205f5a 10203 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10204 dwarf2_debug_line_missing_file_complaint ();
10205 else
366da635 10206 {
25e43795 10207 lh->file_names[file - 1].included_p = 1;
ca5f395d 10208 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10209 {
10210 if (last_subfile != current_subfile)
10211 {
10212 addr = gdbarch_addr_bits_remove (gdbarch, address);
10213 if (last_subfile)
10214 record_line (last_subfile, 0, addr);
10215 last_subfile = current_subfile;
10216 }
10217 addr = check_cu_functions (address, cu);
10218 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10219 record_line (current_subfile, line, addr);
10220 }
366da635 10221 }
c906108c
SS
10222 basic_block = 0;
10223 break;
10224 case DW_LNS_advance_pc:
2dc7f7b3
TT
10225 {
10226 CORE_ADDR adjust
10227 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10228
10229 address += (((op_index + adjust)
10230 / lh->maximum_ops_per_instruction)
10231 * lh->minimum_instruction_length);
10232 op_index = ((op_index + adjust)
10233 % lh->maximum_ops_per_instruction);
10234 line_ptr += bytes_read;
10235 }
c906108c
SS
10236 break;
10237 case DW_LNS_advance_line:
10238 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10239 line_ptr += bytes_read;
10240 break;
10241 case DW_LNS_set_file:
debd256d 10242 {
a738430d
MK
10243 /* The arrays lh->include_dirs and lh->file_names are
10244 0-based, but the directory and file name numbers in
10245 the statement program are 1-based. */
debd256d 10246 struct file_entry *fe;
4f1520fb 10247 char *dir = NULL;
a738430d 10248
debd256d
JB
10249 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10250 line_ptr += bytes_read;
59205f5a 10251 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10252 dwarf2_debug_line_missing_file_complaint ();
10253 else
10254 {
10255 fe = &lh->file_names[file - 1];
10256 if (fe->dir_index)
10257 dir = lh->include_dirs[fe->dir_index - 1];
10258 if (!decode_for_pst_p)
10259 {
10260 last_subfile = current_subfile;
10261 dwarf2_start_subfile (fe->name, dir, comp_dir);
10262 }
10263 }
debd256d 10264 }
c906108c
SS
10265 break;
10266 case DW_LNS_set_column:
10267 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10268 line_ptr += bytes_read;
10269 break;
10270 case DW_LNS_negate_stmt:
10271 is_stmt = (!is_stmt);
10272 break;
10273 case DW_LNS_set_basic_block:
10274 basic_block = 1;
10275 break;
c2c6d25f
JM
10276 /* Add to the address register of the state machine the
10277 address increment value corresponding to special opcode
a738430d
MK
10278 255. I.e., this value is scaled by the minimum
10279 instruction length since special opcode 255 would have
10280 scaled the the increment. */
c906108c 10281 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10282 {
10283 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10284
10285 address += (((op_index + adjust)
10286 / lh->maximum_ops_per_instruction)
10287 * lh->minimum_instruction_length);
10288 op_index = ((op_index + adjust)
10289 % lh->maximum_ops_per_instruction);
10290 }
c906108c
SS
10291 break;
10292 case DW_LNS_fixed_advance_pc:
10293 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10294 op_index = 0;
c906108c
SS
10295 line_ptr += 2;
10296 break;
9aa1fe7e 10297 default:
a738430d
MK
10298 {
10299 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10300 int i;
a738430d 10301
debd256d 10302 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10303 {
10304 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10305 line_ptr += bytes_read;
10306 }
10307 }
c906108c
SS
10308 }
10309 }
59205f5a
JB
10310 if (lh->num_file_names < file || file == 0)
10311 dwarf2_debug_line_missing_file_complaint ();
10312 else
10313 {
10314 lh->file_names[file - 1].included_p = 1;
10315 if (!decode_for_pst_p)
fbf65064
UW
10316 {
10317 addr = gdbarch_addr_bits_remove (gdbarch, address);
10318 record_line (current_subfile, 0, addr);
10319 }
59205f5a 10320 }
c906108c 10321 }
aaa75496
JB
10322
10323 if (decode_for_pst_p)
10324 {
10325 int file_index;
10326
10327 /* Now that we're done scanning the Line Header Program, we can
10328 create the psymtab of each included file. */
10329 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10330 if (lh->file_names[file_index].included_p == 1)
10331 {
c6da4cef
DE
10332 char *include_name =
10333 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10334 if (include_name != NULL)
aaa75496
JB
10335 dwarf2_create_include_psymtab (include_name, pst, objfile);
10336 }
10337 }
cb1df416
DJ
10338 else
10339 {
10340 /* Make sure a symtab is created for every file, even files
10341 which contain only variables (i.e. no code with associated
10342 line numbers). */
10343
10344 int i;
10345 struct file_entry *fe;
10346
10347 for (i = 0; i < lh->num_file_names; i++)
10348 {
10349 char *dir = NULL;
9a619af0 10350
cb1df416
DJ
10351 fe = &lh->file_names[i];
10352 if (fe->dir_index)
10353 dir = lh->include_dirs[fe->dir_index - 1];
10354 dwarf2_start_subfile (fe->name, dir, comp_dir);
10355
10356 /* Skip the main file; we don't need it, and it must be
10357 allocated last, so that it will show up before the
10358 non-primary symtabs in the objfile's symtab list. */
10359 if (current_subfile == first_subfile)
10360 continue;
10361
10362 if (current_subfile->symtab == NULL)
10363 current_subfile->symtab = allocate_symtab (current_subfile->name,
10364 cu->objfile);
10365 fe->symtab = current_subfile->symtab;
10366 }
10367 }
c906108c
SS
10368}
10369
10370/* Start a subfile for DWARF. FILENAME is the name of the file and
10371 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10372 or NULL if not known. COMP_DIR is the compilation directory for the
10373 linetable's compilation unit or NULL if not known.
c906108c
SS
10374 This routine tries to keep line numbers from identical absolute and
10375 relative file names in a common subfile.
10376
10377 Using the `list' example from the GDB testsuite, which resides in
10378 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10379 of /srcdir/list0.c yields the following debugging information for list0.c:
10380
c5aa993b
JM
10381 DW_AT_name: /srcdir/list0.c
10382 DW_AT_comp_dir: /compdir
357e46e7 10383 files.files[0].name: list0.h
c5aa993b 10384 files.files[0].dir: /srcdir
357e46e7 10385 files.files[1].name: list0.c
c5aa993b 10386 files.files[1].dir: /srcdir
c906108c
SS
10387
10388 The line number information for list0.c has to end up in a single
4f1520fb
FR
10389 subfile, so that `break /srcdir/list0.c:1' works as expected.
10390 start_subfile will ensure that this happens provided that we pass the
10391 concatenation of files.files[1].dir and files.files[1].name as the
10392 subfile's name. */
c906108c
SS
10393
10394static void
4f1520fb 10395dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 10396{
4f1520fb
FR
10397 char *fullname;
10398
10399 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10400 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10401 second argument to start_subfile. To be consistent, we do the
10402 same here. In order not to lose the line information directory,
10403 we concatenate it to the filename when it makes sense.
10404 Note that the Dwarf3 standard says (speaking of filenames in line
10405 information): ``The directory index is ignored for file names
10406 that represent full path names''. Thus ignoring dirname in the
10407 `else' branch below isn't an issue. */
c906108c 10408
d5166ae1 10409 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10410 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10411 else
10412 fullname = filename;
c906108c 10413
4f1520fb
FR
10414 start_subfile (fullname, comp_dir);
10415
10416 if (fullname != filename)
10417 xfree (fullname);
c906108c
SS
10418}
10419
4c2df51b
DJ
10420static void
10421var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10422 struct dwarf2_cu *cu)
4c2df51b 10423{
e7c27a73
DJ
10424 struct objfile *objfile = cu->objfile;
10425 struct comp_unit_head *cu_header = &cu->header;
10426
4c2df51b
DJ
10427 /* NOTE drow/2003-01-30: There used to be a comment and some special
10428 code here to turn a symbol with DW_AT_external and a
10429 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10430 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10431 with some versions of binutils) where shared libraries could have
10432 relocations against symbols in their debug information - the
10433 minimal symbol would have the right address, but the debug info
10434 would not. It's no longer necessary, because we will explicitly
10435 apply relocations when we read in the debug information now. */
10436
10437 /* A DW_AT_location attribute with no contents indicates that a
10438 variable has been optimized away. */
10439 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10440 {
10441 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10442 return;
10443 }
10444
10445 /* Handle one degenerate form of location expression specially, to
10446 preserve GDB's previous behavior when section offsets are
10447 specified. If this is just a DW_OP_addr then mark this symbol
10448 as LOC_STATIC. */
10449
10450 if (attr_form_is_block (attr)
10451 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10452 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10453 {
891d2f0b 10454 unsigned int dummy;
4c2df51b
DJ
10455
10456 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10457 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10458 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10459 fixup_symbol_section (sym, objfile);
10460 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10461 SYMBOL_SECTION (sym));
4c2df51b
DJ
10462 return;
10463 }
10464
10465 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10466 expression evaluator, and use LOC_COMPUTED only when necessary
10467 (i.e. when the value of a register or memory location is
10468 referenced, or a thread-local block, etc.). Then again, it might
10469 not be worthwhile. I'm assuming that it isn't unless performance
10470 or memory numbers show me otherwise. */
10471
e7c27a73 10472 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10473 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10474}
10475
c906108c
SS
10476/* Given a pointer to a DWARF information entry, figure out if we need
10477 to make a symbol table entry for it, and if so, create a new entry
10478 and return a pointer to it.
10479 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10480 used the passed type.
10481 If SPACE is not NULL, use it to hold the new symbol. If it is
10482 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10483
10484static struct symbol *
34eaf542
TT
10485new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10486 struct symbol *space)
c906108c 10487{
e7c27a73 10488 struct objfile *objfile = cu->objfile;
c906108c
SS
10489 struct symbol *sym = NULL;
10490 char *name;
10491 struct attribute *attr = NULL;
10492 struct attribute *attr2 = NULL;
e142c38c 10493 CORE_ADDR baseaddr;
e37fd15a
SW
10494 struct pending **list_to_add = NULL;
10495
edb3359d 10496 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10497
10498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10499
94af9270 10500 name = dwarf2_name (die, cu);
c906108c
SS
10501 if (name)
10502 {
94af9270 10503 const char *linkagename;
34eaf542 10504 int suppress_add = 0;
94af9270 10505
34eaf542
TT
10506 if (space)
10507 sym = space;
10508 else
10509 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10510 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10511
10512 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10513 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10514 linkagename = dwarf2_physname (name, die, cu);
10515 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10516
f55ee35c
JK
10517 /* Fortran does not have mangling standard and the mangling does differ
10518 between gfortran, iFort etc. */
10519 if (cu->language == language_fortran
b250c185 10520 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10521 symbol_set_demangled_name (&(sym->ginfo),
10522 (char *) dwarf2_full_name (name, die, cu),
10523 NULL);
f55ee35c 10524
c906108c 10525 /* Default assumptions.
c5aa993b 10526 Use the passed type or decode it from the die. */
176620f1 10527 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10528 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10529 if (type != NULL)
10530 SYMBOL_TYPE (sym) = type;
10531 else
e7c27a73 10532 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10533 attr = dwarf2_attr (die,
10534 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10535 cu);
c906108c
SS
10536 if (attr)
10537 {
10538 SYMBOL_LINE (sym) = DW_UNSND (attr);
10539 }
cb1df416 10540
edb3359d
DJ
10541 attr = dwarf2_attr (die,
10542 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10543 cu);
cb1df416
DJ
10544 if (attr)
10545 {
10546 int file_index = DW_UNSND (attr);
9a619af0 10547
cb1df416
DJ
10548 if (cu->line_header == NULL
10549 || file_index > cu->line_header->num_file_names)
10550 complaint (&symfile_complaints,
10551 _("file index out of range"));
1c3d648d 10552 else if (file_index > 0)
cb1df416
DJ
10553 {
10554 struct file_entry *fe;
9a619af0 10555
cb1df416
DJ
10556 fe = &cu->line_header->file_names[file_index - 1];
10557 SYMBOL_SYMTAB (sym) = fe->symtab;
10558 }
10559 }
10560
c906108c
SS
10561 switch (die->tag)
10562 {
10563 case DW_TAG_label:
e142c38c 10564 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10565 if (attr)
10566 {
10567 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10568 }
0f5238ed
TT
10569 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10570 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10571 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10572 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10573 break;
10574 case DW_TAG_subprogram:
10575 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10576 finish_block. */
10577 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10578 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10579 if ((attr2 && (DW_UNSND (attr2) != 0))
10580 || cu->language == language_ada)
c906108c 10581 {
2cfa0c8d
JB
10582 /* Subprograms marked external are stored as a global symbol.
10583 Ada subprograms, whether marked external or not, are always
10584 stored as a global symbol, because we want to be able to
10585 access them globally. For instance, we want to be able
10586 to break on a nested subprogram without having to
10587 specify the context. */
e37fd15a 10588 list_to_add = &global_symbols;
c906108c
SS
10589 }
10590 else
10591 {
e37fd15a 10592 list_to_add = cu->list_in_scope;
c906108c
SS
10593 }
10594 break;
edb3359d
DJ
10595 case DW_TAG_inlined_subroutine:
10596 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10597 finish_block. */
10598 SYMBOL_CLASS (sym) = LOC_BLOCK;
10599 SYMBOL_INLINED (sym) = 1;
10600 /* Do not add the symbol to any lists. It will be found via
10601 BLOCK_FUNCTION from the blockvector. */
10602 break;
34eaf542
TT
10603 case DW_TAG_template_value_param:
10604 suppress_add = 1;
10605 /* Fall through. */
c906108c 10606 case DW_TAG_variable:
254e6b9e 10607 case DW_TAG_member:
c906108c
SS
10608 /* Compilation with minimal debug info may result in variables
10609 with missing type entries. Change the misleading `void' type
10610 to something sensible. */
10611 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10612 SYMBOL_TYPE (sym)
46bf5051 10613 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10614
e142c38c 10615 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10616 /* In the case of DW_TAG_member, we should only be called for
10617 static const members. */
10618 if (die->tag == DW_TAG_member)
10619 {
3863f96c
DE
10620 /* dwarf2_add_field uses die_is_declaration,
10621 so we do the same. */
254e6b9e
DE
10622 gdb_assert (die_is_declaration (die, cu));
10623 gdb_assert (attr);
10624 }
c906108c
SS
10625 if (attr)
10626 {
e7c27a73 10627 dwarf2_const_value (attr, sym, cu);
e142c38c 10628 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10629 if (!suppress_add)
34eaf542
TT
10630 {
10631 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10632 list_to_add = &global_symbols;
34eaf542 10633 else
e37fd15a 10634 list_to_add = cu->list_in_scope;
34eaf542 10635 }
c906108c
SS
10636 break;
10637 }
e142c38c 10638 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10639 if (attr)
10640 {
e7c27a73 10641 var_decode_location (attr, sym, cu);
e142c38c 10642 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10643 if (SYMBOL_CLASS (sym) == LOC_STATIC
10644 && SYMBOL_VALUE_ADDRESS (sym) == 0
10645 && !dwarf2_per_objfile->has_section_at_zero)
10646 {
10647 /* When a static variable is eliminated by the linker,
10648 the corresponding debug information is not stripped
10649 out, but the variable address is set to null;
10650 do not add such variables into symbol table. */
10651 }
10652 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10653 {
f55ee35c
JK
10654 /* Workaround gfortran PR debug/40040 - it uses
10655 DW_AT_location for variables in -fPIC libraries which may
10656 get overriden by other libraries/executable and get
10657 a different address. Resolve it by the minimal symbol
10658 which may come from inferior's executable using copy
10659 relocation. Make this workaround only for gfortran as for
10660 other compilers GDB cannot guess the minimal symbol
10661 Fortran mangling kind. */
10662 if (cu->language == language_fortran && die->parent
10663 && die->parent->tag == DW_TAG_module
10664 && cu->producer
10665 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10666 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10667
1c809c68
TT
10668 /* A variable with DW_AT_external is never static,
10669 but it may be block-scoped. */
10670 list_to_add = (cu->list_in_scope == &file_symbols
10671 ? &global_symbols : cu->list_in_scope);
1c809c68 10672 }
c906108c 10673 else
e37fd15a 10674 list_to_add = cu->list_in_scope;
c906108c
SS
10675 }
10676 else
10677 {
10678 /* We do not know the address of this symbol.
c5aa993b
JM
10679 If it is an external symbol and we have type information
10680 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10681 The address of the variable will then be determined from
10682 the minimal symbol table whenever the variable is
10683 referenced. */
e142c38c 10684 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10685 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10686 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10687 {
0fe7935b
DJ
10688 /* A variable with DW_AT_external is never static, but it
10689 may be block-scoped. */
10690 list_to_add = (cu->list_in_scope == &file_symbols
10691 ? &global_symbols : cu->list_in_scope);
10692
c906108c 10693 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10694 }
442ddf59
JK
10695 else if (!die_is_declaration (die, cu))
10696 {
10697 /* Use the default LOC_OPTIMIZED_OUT class. */
10698 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10699 if (!suppress_add)
10700 list_to_add = cu->list_in_scope;
442ddf59 10701 }
c906108c
SS
10702 }
10703 break;
10704 case DW_TAG_formal_parameter:
edb3359d
DJ
10705 /* If we are inside a function, mark this as an argument. If
10706 not, we might be looking at an argument to an inlined function
10707 when we do not have enough information to show inlined frames;
10708 pretend it's a local variable in that case so that the user can
10709 still see it. */
10710 if (context_stack_depth > 0
10711 && context_stack[context_stack_depth - 1].name != NULL)
10712 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10713 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10714 if (attr)
10715 {
e7c27a73 10716 var_decode_location (attr, sym, cu);
c906108c 10717 }
e142c38c 10718 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10719 if (attr)
10720 {
e7c27a73 10721 dwarf2_const_value (attr, sym, cu);
c906108c 10722 }
f346a30d
PM
10723 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10724 if (attr && DW_UNSND (attr))
10725 {
10726 struct type *ref_type;
10727
10728 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10729 SYMBOL_TYPE (sym) = ref_type;
10730 }
10731
e37fd15a 10732 list_to_add = cu->list_in_scope;
c906108c
SS
10733 break;
10734 case DW_TAG_unspecified_parameters:
10735 /* From varargs functions; gdb doesn't seem to have any
10736 interest in this information, so just ignore it for now.
10737 (FIXME?) */
10738 break;
34eaf542
TT
10739 case DW_TAG_template_type_param:
10740 suppress_add = 1;
10741 /* Fall through. */
c906108c 10742 case DW_TAG_class_type:
680b30c7 10743 case DW_TAG_interface_type:
c906108c
SS
10744 case DW_TAG_structure_type:
10745 case DW_TAG_union_type:
72019c9c 10746 case DW_TAG_set_type:
c906108c
SS
10747 case DW_TAG_enumeration_type:
10748 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10749 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10750
63d06c5c 10751 {
987504bb 10752 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10753 really ever be static objects: otherwise, if you try
10754 to, say, break of a class's method and you're in a file
10755 which doesn't mention that class, it won't work unless
10756 the check for all static symbols in lookup_symbol_aux
10757 saves you. See the OtherFileClass tests in
10758 gdb.c++/namespace.exp. */
10759
e37fd15a 10760 if (!suppress_add)
34eaf542 10761 {
34eaf542
TT
10762 list_to_add = (cu->list_in_scope == &file_symbols
10763 && (cu->language == language_cplus
10764 || cu->language == language_java)
10765 ? &global_symbols : cu->list_in_scope);
63d06c5c 10766
64382290
TT
10767 /* The semantics of C++ state that "struct foo {
10768 ... }" also defines a typedef for "foo". A Java
10769 class declaration also defines a typedef for the
10770 class. */
10771 if (cu->language == language_cplus
10772 || cu->language == language_java
10773 || cu->language == language_ada)
10774 {
10775 /* The symbol's name is already allocated along
10776 with this objfile, so we don't need to
10777 duplicate it for the type. */
10778 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10779 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10780 }
63d06c5c
DC
10781 }
10782 }
c906108c
SS
10783 break;
10784 case DW_TAG_typedef:
63d06c5c
DC
10785 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10786 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10787 list_to_add = cu->list_in_scope;
63d06c5c 10788 break;
c906108c 10789 case DW_TAG_base_type:
a02abb62 10790 case DW_TAG_subrange_type:
c906108c 10791 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10792 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10793 list_to_add = cu->list_in_scope;
c906108c
SS
10794 break;
10795 case DW_TAG_enumerator:
e142c38c 10796 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10797 if (attr)
10798 {
e7c27a73 10799 dwarf2_const_value (attr, sym, cu);
c906108c 10800 }
63d06c5c
DC
10801 {
10802 /* NOTE: carlton/2003-11-10: See comment above in the
10803 DW_TAG_class_type, etc. block. */
10804
e142c38c 10805 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10806 && (cu->language == language_cplus
10807 || cu->language == language_java)
e142c38c 10808 ? &global_symbols : cu->list_in_scope);
63d06c5c 10809 }
c906108c 10810 break;
5c4e30ca
DC
10811 case DW_TAG_namespace:
10812 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10813 list_to_add = &global_symbols;
5c4e30ca 10814 break;
c906108c
SS
10815 default:
10816 /* Not a tag we recognize. Hopefully we aren't processing
10817 trash data, but since we must specifically ignore things
10818 we don't recognize, there is nothing else we should do at
10819 this point. */
e2e0b3e5 10820 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10821 dwarf_tag_name (die->tag));
c906108c
SS
10822 break;
10823 }
df8a16a1 10824
e37fd15a
SW
10825 if (suppress_add)
10826 {
10827 sym->hash_next = objfile->template_symbols;
10828 objfile->template_symbols = sym;
10829 list_to_add = NULL;
10830 }
10831
10832 if (list_to_add != NULL)
10833 add_symbol_to_list (sym, list_to_add);
10834
df8a16a1
DJ
10835 /* For the benefit of old versions of GCC, check for anonymous
10836 namespaces based on the demangled name. */
10837 if (!processing_has_namespace_info
94af9270 10838 && cu->language == language_cplus)
df8a16a1 10839 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10840 }
10841 return (sym);
10842}
10843
34eaf542
TT
10844/* A wrapper for new_symbol_full that always allocates a new symbol. */
10845
10846static struct symbol *
10847new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10848{
10849 return new_symbol_full (die, type, cu, NULL);
10850}
10851
98bfdba5
PA
10852/* Given an attr with a DW_FORM_dataN value in host byte order,
10853 zero-extend it as appropriate for the symbol's type. The DWARF
10854 standard (v4) is not entirely clear about the meaning of using
10855 DW_FORM_dataN for a constant with a signed type, where the type is
10856 wider than the data. The conclusion of a discussion on the DWARF
10857 list was that this is unspecified. We choose to always zero-extend
10858 because that is the interpretation long in use by GCC. */
c906108c 10859
98bfdba5
PA
10860static gdb_byte *
10861dwarf2_const_value_data (struct attribute *attr, struct type *type,
10862 const char *name, struct obstack *obstack,
10863 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10864{
e7c27a73 10865 struct objfile *objfile = cu->objfile;
e17a4113
UW
10866 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10867 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10868 LONGEST l = DW_UNSND (attr);
10869
10870 if (bits < sizeof (*value) * 8)
10871 {
10872 l &= ((LONGEST) 1 << bits) - 1;
10873 *value = l;
10874 }
10875 else if (bits == sizeof (*value) * 8)
10876 *value = l;
10877 else
10878 {
10879 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10880 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10881 return bytes;
10882 }
10883
10884 return NULL;
10885}
10886
10887/* Read a constant value from an attribute. Either set *VALUE, or if
10888 the value does not fit in *VALUE, set *BYTES - either already
10889 allocated on the objfile obstack, or newly allocated on OBSTACK,
10890 or, set *BATON, if we translated the constant to a location
10891 expression. */
10892
10893static void
10894dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10895 const char *name, struct obstack *obstack,
10896 struct dwarf2_cu *cu,
10897 long *value, gdb_byte **bytes,
10898 struct dwarf2_locexpr_baton **baton)
10899{
10900 struct objfile *objfile = cu->objfile;
10901 struct comp_unit_head *cu_header = &cu->header;
c906108c 10902 struct dwarf_block *blk;
98bfdba5
PA
10903 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10904 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10905
10906 *value = 0;
10907 *bytes = NULL;
10908 *baton = NULL;
c906108c
SS
10909
10910 switch (attr->form)
10911 {
10912 case DW_FORM_addr:
ac56253d 10913 {
ac56253d
TT
10914 gdb_byte *data;
10915
98bfdba5
PA
10916 if (TYPE_LENGTH (type) != cu_header->addr_size)
10917 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 10918 cu_header->addr_size,
98bfdba5 10919 TYPE_LENGTH (type));
ac56253d
TT
10920 /* Symbols of this form are reasonably rare, so we just
10921 piggyback on the existing location code rather than writing
10922 a new implementation of symbol_computed_ops. */
98bfdba5
PA
10923 *baton = obstack_alloc (&objfile->objfile_obstack,
10924 sizeof (struct dwarf2_locexpr_baton));
10925 (*baton)->per_cu = cu->per_cu;
10926 gdb_assert ((*baton)->per_cu);
ac56253d 10927
98bfdba5
PA
10928 (*baton)->size = 2 + cu_header->addr_size;
10929 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10930 (*baton)->data = data;
ac56253d
TT
10931
10932 data[0] = DW_OP_addr;
10933 store_unsigned_integer (&data[1], cu_header->addr_size,
10934 byte_order, DW_ADDR (attr));
10935 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 10936 }
c906108c 10937 break;
4ac36638 10938 case DW_FORM_string:
93b5768b 10939 case DW_FORM_strp:
98bfdba5
PA
10940 /* DW_STRING is already allocated on the objfile obstack, point
10941 directly to it. */
10942 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 10943 break;
c906108c
SS
10944 case DW_FORM_block1:
10945 case DW_FORM_block2:
10946 case DW_FORM_block4:
10947 case DW_FORM_block:
2dc7f7b3 10948 case DW_FORM_exprloc:
c906108c 10949 blk = DW_BLOCK (attr);
98bfdba5
PA
10950 if (TYPE_LENGTH (type) != blk->size)
10951 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10952 TYPE_LENGTH (type));
10953 *bytes = blk->data;
c906108c 10954 break;
2df3850c
JM
10955
10956 /* The DW_AT_const_value attributes are supposed to carry the
10957 symbol's value "represented as it would be on the target
10958 architecture." By the time we get here, it's already been
10959 converted to host endianness, so we just need to sign- or
10960 zero-extend it as appropriate. */
10961 case DW_FORM_data1:
98bfdba5 10962 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 10963 break;
c906108c 10964 case DW_FORM_data2:
98bfdba5 10965 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 10966 break;
c906108c 10967 case DW_FORM_data4:
98bfdba5 10968 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 10969 break;
c906108c 10970 case DW_FORM_data8:
98bfdba5 10971 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
10972 break;
10973
c906108c 10974 case DW_FORM_sdata:
98bfdba5 10975 *value = DW_SND (attr);
2df3850c
JM
10976 break;
10977
c906108c 10978 case DW_FORM_udata:
98bfdba5 10979 *value = DW_UNSND (attr);
c906108c 10980 break;
2df3850c 10981
c906108c 10982 default:
4d3c2250 10983 complaint (&symfile_complaints,
e2e0b3e5 10984 _("unsupported const value attribute form: '%s'"),
4d3c2250 10985 dwarf_form_name (attr->form));
98bfdba5 10986 *value = 0;
c906108c
SS
10987 break;
10988 }
10989}
10990
2df3850c 10991
98bfdba5
PA
10992/* Copy constant value from an attribute to a symbol. */
10993
2df3850c 10994static void
98bfdba5
PA
10995dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10996 struct dwarf2_cu *cu)
2df3850c 10997{
98bfdba5
PA
10998 struct objfile *objfile = cu->objfile;
10999 struct comp_unit_head *cu_header = &cu->header;
11000 long value;
11001 gdb_byte *bytes;
11002 struct dwarf2_locexpr_baton *baton;
2df3850c 11003
98bfdba5
PA
11004 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11005 SYMBOL_PRINT_NAME (sym),
11006 &objfile->objfile_obstack, cu,
11007 &value, &bytes, &baton);
2df3850c 11008
98bfdba5
PA
11009 if (baton != NULL)
11010 {
11011 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11012 SYMBOL_LOCATION_BATON (sym) = baton;
11013 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11014 }
11015 else if (bytes != NULL)
11016 {
11017 SYMBOL_VALUE_BYTES (sym) = bytes;
11018 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11019 }
11020 else
11021 {
11022 SYMBOL_VALUE (sym) = value;
11023 SYMBOL_CLASS (sym) = LOC_CONST;
11024 }
2df3850c
JM
11025}
11026
c906108c
SS
11027/* Return the type of the die in question using its DW_AT_type attribute. */
11028
11029static struct type *
e7c27a73 11030die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11031{
c906108c 11032 struct attribute *type_attr;
c906108c 11033
e142c38c 11034 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11035 if (!type_attr)
11036 {
11037 /* A missing DW_AT_type represents a void type. */
46bf5051 11038 return objfile_type (cu->objfile)->builtin_void;
c906108c 11039 }
348e048f 11040
673bfd45 11041 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11042}
11043
b4ba55a1
JB
11044/* True iff CU's producer generates GNAT Ada auxiliary information
11045 that allows to find parallel types through that information instead
11046 of having to do expensive parallel lookups by type name. */
11047
11048static int
11049need_gnat_info (struct dwarf2_cu *cu)
11050{
11051 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11052 of GNAT produces this auxiliary information, without any indication
11053 that it is produced. Part of enhancing the FSF version of GNAT
11054 to produce that information will be to put in place an indicator
11055 that we can use in order to determine whether the descriptive type
11056 info is available or not. One suggestion that has been made is
11057 to use a new attribute, attached to the CU die. For now, assume
11058 that the descriptive type info is not available. */
11059 return 0;
11060}
11061
b4ba55a1
JB
11062/* Return the auxiliary type of the die in question using its
11063 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11064 attribute is not present. */
11065
11066static struct type *
11067die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11068{
b4ba55a1 11069 struct attribute *type_attr;
b4ba55a1
JB
11070
11071 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11072 if (!type_attr)
11073 return NULL;
11074
673bfd45 11075 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11076}
11077
11078/* If DIE has a descriptive_type attribute, then set the TYPE's
11079 descriptive type accordingly. */
11080
11081static void
11082set_descriptive_type (struct type *type, struct die_info *die,
11083 struct dwarf2_cu *cu)
11084{
11085 struct type *descriptive_type = die_descriptive_type (die, cu);
11086
11087 if (descriptive_type)
11088 {
11089 ALLOCATE_GNAT_AUX_TYPE (type);
11090 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11091 }
11092}
11093
c906108c
SS
11094/* Return the containing type of the die in question using its
11095 DW_AT_containing_type attribute. */
11096
11097static struct type *
e7c27a73 11098die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11099{
c906108c 11100 struct attribute *type_attr;
c906108c 11101
e142c38c 11102 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11103 if (!type_attr)
11104 error (_("Dwarf Error: Problem turning containing type into gdb type "
11105 "[in module %s]"), cu->objfile->name);
11106
673bfd45 11107 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11108}
11109
673bfd45
DE
11110/* Look up the type of DIE in CU using its type attribute ATTR.
11111 If there is no type substitute an error marker. */
11112
c906108c 11113static struct type *
673bfd45
DE
11114lookup_die_type (struct die_info *die, struct attribute *attr,
11115 struct dwarf2_cu *cu)
c906108c 11116{
f792889a
DJ
11117 struct type *this_type;
11118
673bfd45
DE
11119 /* First see if we have it cached. */
11120
11121 if (is_ref_attr (attr))
11122 {
11123 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11124
11125 this_type = get_die_type_at_offset (offset, cu->per_cu);
11126 }
11127 else if (attr->form == DW_FORM_sig8)
11128 {
11129 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11130 struct dwarf2_cu *sig_cu;
11131 unsigned int offset;
11132
11133 /* sig_type will be NULL if the signatured type is missing from
11134 the debug info. */
11135 if (sig_type == NULL)
11136 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11137 "at 0x%x [in module %s]"),
11138 die->offset, cu->objfile->name);
11139
11140 gdb_assert (sig_type->per_cu.from_debug_types);
11141 offset = sig_type->offset + sig_type->type_offset;
11142 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11143 }
11144 else
11145 {
11146 dump_die_for_error (die);
11147 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11148 dwarf_attr_name (attr->name), cu->objfile->name);
11149 }
11150
11151 /* If not cached we need to read it in. */
11152
11153 if (this_type == NULL)
11154 {
11155 struct die_info *type_die;
11156 struct dwarf2_cu *type_cu = cu;
11157
11158 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11159 /* If the type is cached, we should have found it above. */
11160 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11161 this_type = read_type_die_1 (type_die, type_cu);
11162 }
11163
11164 /* If we still don't have a type use an error marker. */
11165
11166 if (this_type == NULL)
c906108c 11167 {
b00fdb78
TT
11168 char *message, *saved;
11169
11170 /* read_type_die already issued a complaint. */
11171 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11172 cu->objfile->name,
11173 cu->header.offset,
11174 die->offset);
11175 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11176 message, strlen (message));
11177 xfree (message);
11178
11179 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11180 }
673bfd45 11181
f792889a 11182 return this_type;
c906108c
SS
11183}
11184
673bfd45
DE
11185/* Return the type in DIE, CU.
11186 Returns NULL for invalid types.
11187
11188 This first does a lookup in the appropriate type_hash table,
11189 and only reads the die in if necessary.
11190
11191 NOTE: This can be called when reading in partial or full symbols. */
11192
f792889a 11193static struct type *
e7c27a73 11194read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11195{
f792889a
DJ
11196 struct type *this_type;
11197
11198 this_type = get_die_type (die, cu);
11199 if (this_type)
11200 return this_type;
11201
673bfd45
DE
11202 return read_type_die_1 (die, cu);
11203}
11204
11205/* Read the type in DIE, CU.
11206 Returns NULL for invalid types. */
11207
11208static struct type *
11209read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11210{
11211 struct type *this_type = NULL;
11212
c906108c
SS
11213 switch (die->tag)
11214 {
11215 case DW_TAG_class_type:
680b30c7 11216 case DW_TAG_interface_type:
c906108c
SS
11217 case DW_TAG_structure_type:
11218 case DW_TAG_union_type:
f792889a 11219 this_type = read_structure_type (die, cu);
c906108c
SS
11220 break;
11221 case DW_TAG_enumeration_type:
f792889a 11222 this_type = read_enumeration_type (die, cu);
c906108c
SS
11223 break;
11224 case DW_TAG_subprogram:
11225 case DW_TAG_subroutine_type:
edb3359d 11226 case DW_TAG_inlined_subroutine:
f792889a 11227 this_type = read_subroutine_type (die, cu);
c906108c
SS
11228 break;
11229 case DW_TAG_array_type:
f792889a 11230 this_type = read_array_type (die, cu);
c906108c 11231 break;
72019c9c 11232 case DW_TAG_set_type:
f792889a 11233 this_type = read_set_type (die, cu);
72019c9c 11234 break;
c906108c 11235 case DW_TAG_pointer_type:
f792889a 11236 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11237 break;
11238 case DW_TAG_ptr_to_member_type:
f792889a 11239 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11240 break;
11241 case DW_TAG_reference_type:
f792889a 11242 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11243 break;
11244 case DW_TAG_const_type:
f792889a 11245 this_type = read_tag_const_type (die, cu);
c906108c
SS
11246 break;
11247 case DW_TAG_volatile_type:
f792889a 11248 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11249 break;
11250 case DW_TAG_string_type:
f792889a 11251 this_type = read_tag_string_type (die, cu);
c906108c
SS
11252 break;
11253 case DW_TAG_typedef:
f792889a 11254 this_type = read_typedef (die, cu);
c906108c 11255 break;
a02abb62 11256 case DW_TAG_subrange_type:
f792889a 11257 this_type = read_subrange_type (die, cu);
a02abb62 11258 break;
c906108c 11259 case DW_TAG_base_type:
f792889a 11260 this_type = read_base_type (die, cu);
c906108c 11261 break;
81a17f79 11262 case DW_TAG_unspecified_type:
f792889a 11263 this_type = read_unspecified_type (die, cu);
81a17f79 11264 break;
0114d602
DJ
11265 case DW_TAG_namespace:
11266 this_type = read_namespace_type (die, cu);
11267 break;
f55ee35c
JK
11268 case DW_TAG_module:
11269 this_type = read_module_type (die, cu);
11270 break;
c906108c 11271 default:
a1f5b845 11272 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11273 dwarf_tag_name (die->tag));
c906108c
SS
11274 break;
11275 }
63d06c5c 11276
f792889a 11277 return this_type;
63d06c5c
DC
11278}
11279
fdde2d81 11280/* Return the name of the namespace/class that DIE is defined within,
0114d602 11281 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11282
0114d602
DJ
11283 For example, if we're within the method foo() in the following
11284 code:
11285
11286 namespace N {
11287 class C {
11288 void foo () {
11289 }
11290 };
11291 }
11292
11293 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11294
11295static char *
e142c38c 11296determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11297{
0114d602
DJ
11298 struct die_info *parent, *spec_die;
11299 struct dwarf2_cu *spec_cu;
11300 struct type *parent_type;
63d06c5c 11301
f55ee35c
JK
11302 if (cu->language != language_cplus && cu->language != language_java
11303 && cu->language != language_fortran)
0114d602
DJ
11304 return "";
11305
11306 /* We have to be careful in the presence of DW_AT_specification.
11307 For example, with GCC 3.4, given the code
11308
11309 namespace N {
11310 void foo() {
11311 // Definition of N::foo.
11312 }
11313 }
11314
11315 then we'll have a tree of DIEs like this:
11316
11317 1: DW_TAG_compile_unit
11318 2: DW_TAG_namespace // N
11319 3: DW_TAG_subprogram // declaration of N::foo
11320 4: DW_TAG_subprogram // definition of N::foo
11321 DW_AT_specification // refers to die #3
11322
11323 Thus, when processing die #4, we have to pretend that we're in
11324 the context of its DW_AT_specification, namely the contex of die
11325 #3. */
11326 spec_cu = cu;
11327 spec_die = die_specification (die, &spec_cu);
11328 if (spec_die == NULL)
11329 parent = die->parent;
11330 else
63d06c5c 11331 {
0114d602
DJ
11332 parent = spec_die->parent;
11333 cu = spec_cu;
63d06c5c 11334 }
0114d602
DJ
11335
11336 if (parent == NULL)
11337 return "";
98bfdba5
PA
11338 else if (parent->building_fullname)
11339 {
11340 const char *name;
11341 const char *parent_name;
11342
11343 /* It has been seen on RealView 2.2 built binaries,
11344 DW_TAG_template_type_param types actually _defined_ as
11345 children of the parent class:
11346
11347 enum E {};
11348 template class <class Enum> Class{};
11349 Class<enum E> class_e;
11350
11351 1: DW_TAG_class_type (Class)
11352 2: DW_TAG_enumeration_type (E)
11353 3: DW_TAG_enumerator (enum1:0)
11354 3: DW_TAG_enumerator (enum2:1)
11355 ...
11356 2: DW_TAG_template_type_param
11357 DW_AT_type DW_FORM_ref_udata (E)
11358
11359 Besides being broken debug info, it can put GDB into an
11360 infinite loop. Consider:
11361
11362 When we're building the full name for Class<E>, we'll start
11363 at Class, and go look over its template type parameters,
11364 finding E. We'll then try to build the full name of E, and
11365 reach here. We're now trying to build the full name of E,
11366 and look over the parent DIE for containing scope. In the
11367 broken case, if we followed the parent DIE of E, we'd again
11368 find Class, and once again go look at its template type
11369 arguments, etc., etc. Simply don't consider such parent die
11370 as source-level parent of this die (it can't be, the language
11371 doesn't allow it), and break the loop here. */
11372 name = dwarf2_name (die, cu);
11373 parent_name = dwarf2_name (parent, cu);
11374 complaint (&symfile_complaints,
11375 _("template param type '%s' defined within parent '%s'"),
11376 name ? name : "<unknown>",
11377 parent_name ? parent_name : "<unknown>");
11378 return "";
11379 }
63d06c5c 11380 else
0114d602
DJ
11381 switch (parent->tag)
11382 {
63d06c5c 11383 case DW_TAG_namespace:
0114d602 11384 parent_type = read_type_die (parent, cu);
acebe513
UW
11385 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11386 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11387 Work around this problem here. */
11388 if (cu->language == language_cplus
11389 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11390 return "";
0114d602
DJ
11391 /* We give a name to even anonymous namespaces. */
11392 return TYPE_TAG_NAME (parent_type);
63d06c5c 11393 case DW_TAG_class_type:
680b30c7 11394 case DW_TAG_interface_type:
63d06c5c 11395 case DW_TAG_structure_type:
0114d602 11396 case DW_TAG_union_type:
f55ee35c 11397 case DW_TAG_module:
0114d602
DJ
11398 parent_type = read_type_die (parent, cu);
11399 if (TYPE_TAG_NAME (parent_type) != NULL)
11400 return TYPE_TAG_NAME (parent_type);
11401 else
11402 /* An anonymous structure is only allowed non-static data
11403 members; no typedefs, no member functions, et cetera.
11404 So it does not need a prefix. */
11405 return "";
63d06c5c 11406 default:
8176b9b8 11407 return determine_prefix (parent, cu);
63d06c5c 11408 }
63d06c5c
DC
11409}
11410
987504bb
JJ
11411/* Return a newly-allocated string formed by concatenating PREFIX and
11412 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11413 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11414 perform an obconcat, otherwise allocate storage for the result. The CU argument
11415 is used to determine the language and hence, the appropriate separator. */
11416
f55ee35c 11417#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11418
11419static char *
f55ee35c
JK
11420typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11421 int physname, struct dwarf2_cu *cu)
63d06c5c 11422{
f55ee35c 11423 const char *lead = "";
5c315b68 11424 const char *sep;
63d06c5c 11425
987504bb
JJ
11426 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11427 sep = "";
11428 else if (cu->language == language_java)
11429 sep = ".";
f55ee35c
JK
11430 else if (cu->language == language_fortran && physname)
11431 {
11432 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11433 DW_AT_MIPS_linkage_name is preferred and used instead. */
11434
11435 lead = "__";
11436 sep = "_MOD_";
11437 }
987504bb
JJ
11438 else
11439 sep = "::";
63d06c5c 11440
6dd47d34
DE
11441 if (prefix == NULL)
11442 prefix = "";
11443 if (suffix == NULL)
11444 suffix = "";
11445
987504bb
JJ
11446 if (obs == NULL)
11447 {
11448 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11449
f55ee35c
JK
11450 strcpy (retval, lead);
11451 strcat (retval, prefix);
6dd47d34
DE
11452 strcat (retval, sep);
11453 strcat (retval, suffix);
63d06c5c
DC
11454 return retval;
11455 }
987504bb
JJ
11456 else
11457 {
11458 /* We have an obstack. */
f55ee35c 11459 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11460 }
63d06c5c
DC
11461}
11462
c906108c
SS
11463/* Return sibling of die, NULL if no sibling. */
11464
f9aca02d 11465static struct die_info *
fba45db2 11466sibling_die (struct die_info *die)
c906108c 11467{
639d11d3 11468 return die->sibling;
c906108c
SS
11469}
11470
71c25dea
TT
11471/* Get name of a die, return NULL if not found. */
11472
11473static char *
11474dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11475 struct obstack *obstack)
11476{
11477 if (name && cu->language == language_cplus)
11478 {
11479 char *canon_name = cp_canonicalize_string (name);
11480
11481 if (canon_name != NULL)
11482 {
11483 if (strcmp (canon_name, name) != 0)
11484 name = obsavestring (canon_name, strlen (canon_name),
11485 obstack);
11486 xfree (canon_name);
11487 }
11488 }
11489
11490 return name;
c906108c
SS
11491}
11492
9219021c
DC
11493/* Get name of a die, return NULL if not found. */
11494
11495static char *
e142c38c 11496dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11497{
11498 struct attribute *attr;
11499
e142c38c 11500 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11501 if (!attr || !DW_STRING (attr))
11502 return NULL;
11503
11504 switch (die->tag)
11505 {
11506 case DW_TAG_compile_unit:
11507 /* Compilation units have a DW_AT_name that is a filename, not
11508 a source language identifier. */
11509 case DW_TAG_enumeration_type:
11510 case DW_TAG_enumerator:
11511 /* These tags always have simple identifiers already; no need
11512 to canonicalize them. */
11513 return DW_STRING (attr);
907af001 11514
418835cc
KS
11515 case DW_TAG_subprogram:
11516 /* Java constructors will all be named "<init>", so return
11517 the class name when we see this special case. */
11518 if (cu->language == language_java
11519 && DW_STRING (attr) != NULL
11520 && strcmp (DW_STRING (attr), "<init>") == 0)
11521 {
11522 struct dwarf2_cu *spec_cu = cu;
11523 struct die_info *spec_die;
11524
11525 /* GCJ will output '<init>' for Java constructor names.
11526 For this special case, return the name of the parent class. */
11527
11528 /* GCJ may output suprogram DIEs with AT_specification set.
11529 If so, use the name of the specified DIE. */
11530 spec_die = die_specification (die, &spec_cu);
11531 if (spec_die != NULL)
11532 return dwarf2_name (spec_die, spec_cu);
11533
11534 do
11535 {
11536 die = die->parent;
11537 if (die->tag == DW_TAG_class_type)
11538 return dwarf2_name (die, cu);
11539 }
11540 while (die->tag != DW_TAG_compile_unit);
11541 }
907af001
UW
11542 break;
11543
11544 case DW_TAG_class_type:
11545 case DW_TAG_interface_type:
11546 case DW_TAG_structure_type:
11547 case DW_TAG_union_type:
11548 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11549 structures or unions. These were of the form "._%d" in GCC 4.1,
11550 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11551 and GCC 4.4. We work around this problem by ignoring these. */
11552 if (strncmp (DW_STRING (attr), "._", 2) == 0
11553 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11554 return NULL;
11555 break;
11556
71c25dea 11557 default:
907af001
UW
11558 break;
11559 }
11560
11561 if (!DW_STRING_IS_CANONICAL (attr))
11562 {
11563 DW_STRING (attr)
11564 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11565 &cu->objfile->objfile_obstack);
11566 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11567 }
907af001 11568 return DW_STRING (attr);
9219021c
DC
11569}
11570
11571/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11572 is none. *EXT_CU is the CU containing DIE on input, and the CU
11573 containing the return value on output. */
9219021c
DC
11574
11575static struct die_info *
f2f0e013 11576dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11577{
11578 struct attribute *attr;
9219021c 11579
f2f0e013 11580 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11581 if (attr == NULL)
11582 return NULL;
11583
f2f0e013 11584 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11585}
11586
c906108c
SS
11587/* Convert a DIE tag into its string name. */
11588
11589static char *
aa1ee363 11590dwarf_tag_name (unsigned tag)
c906108c
SS
11591{
11592 switch (tag)
11593 {
11594 case DW_TAG_padding:
11595 return "DW_TAG_padding";
11596 case DW_TAG_array_type:
11597 return "DW_TAG_array_type";
11598 case DW_TAG_class_type:
11599 return "DW_TAG_class_type";
11600 case DW_TAG_entry_point:
11601 return "DW_TAG_entry_point";
11602 case DW_TAG_enumeration_type:
11603 return "DW_TAG_enumeration_type";
11604 case DW_TAG_formal_parameter:
11605 return "DW_TAG_formal_parameter";
11606 case DW_TAG_imported_declaration:
11607 return "DW_TAG_imported_declaration";
11608 case DW_TAG_label:
11609 return "DW_TAG_label";
11610 case DW_TAG_lexical_block:
11611 return "DW_TAG_lexical_block";
11612 case DW_TAG_member:
11613 return "DW_TAG_member";
11614 case DW_TAG_pointer_type:
11615 return "DW_TAG_pointer_type";
11616 case DW_TAG_reference_type:
11617 return "DW_TAG_reference_type";
11618 case DW_TAG_compile_unit:
11619 return "DW_TAG_compile_unit";
11620 case DW_TAG_string_type:
11621 return "DW_TAG_string_type";
11622 case DW_TAG_structure_type:
11623 return "DW_TAG_structure_type";
11624 case DW_TAG_subroutine_type:
11625 return "DW_TAG_subroutine_type";
11626 case DW_TAG_typedef:
11627 return "DW_TAG_typedef";
11628 case DW_TAG_union_type:
11629 return "DW_TAG_union_type";
11630 case DW_TAG_unspecified_parameters:
11631 return "DW_TAG_unspecified_parameters";
11632 case DW_TAG_variant:
11633 return "DW_TAG_variant";
11634 case DW_TAG_common_block:
11635 return "DW_TAG_common_block";
11636 case DW_TAG_common_inclusion:
11637 return "DW_TAG_common_inclusion";
11638 case DW_TAG_inheritance:
11639 return "DW_TAG_inheritance";
11640 case DW_TAG_inlined_subroutine:
11641 return "DW_TAG_inlined_subroutine";
11642 case DW_TAG_module:
11643 return "DW_TAG_module";
11644 case DW_TAG_ptr_to_member_type:
11645 return "DW_TAG_ptr_to_member_type";
11646 case DW_TAG_set_type:
11647 return "DW_TAG_set_type";
11648 case DW_TAG_subrange_type:
11649 return "DW_TAG_subrange_type";
11650 case DW_TAG_with_stmt:
11651 return "DW_TAG_with_stmt";
11652 case DW_TAG_access_declaration:
11653 return "DW_TAG_access_declaration";
11654 case DW_TAG_base_type:
11655 return "DW_TAG_base_type";
11656 case DW_TAG_catch_block:
11657 return "DW_TAG_catch_block";
11658 case DW_TAG_const_type:
11659 return "DW_TAG_const_type";
11660 case DW_TAG_constant:
11661 return "DW_TAG_constant";
11662 case DW_TAG_enumerator:
11663 return "DW_TAG_enumerator";
11664 case DW_TAG_file_type:
11665 return "DW_TAG_file_type";
11666 case DW_TAG_friend:
11667 return "DW_TAG_friend";
11668 case DW_TAG_namelist:
11669 return "DW_TAG_namelist";
11670 case DW_TAG_namelist_item:
11671 return "DW_TAG_namelist_item";
11672 case DW_TAG_packed_type:
11673 return "DW_TAG_packed_type";
11674 case DW_TAG_subprogram:
11675 return "DW_TAG_subprogram";
11676 case DW_TAG_template_type_param:
11677 return "DW_TAG_template_type_param";
11678 case DW_TAG_template_value_param:
11679 return "DW_TAG_template_value_param";
11680 case DW_TAG_thrown_type:
11681 return "DW_TAG_thrown_type";
11682 case DW_TAG_try_block:
11683 return "DW_TAG_try_block";
11684 case DW_TAG_variant_part:
11685 return "DW_TAG_variant_part";
11686 case DW_TAG_variable:
11687 return "DW_TAG_variable";
11688 case DW_TAG_volatile_type:
11689 return "DW_TAG_volatile_type";
d9fa45fe
DC
11690 case DW_TAG_dwarf_procedure:
11691 return "DW_TAG_dwarf_procedure";
11692 case DW_TAG_restrict_type:
11693 return "DW_TAG_restrict_type";
11694 case DW_TAG_interface_type:
11695 return "DW_TAG_interface_type";
11696 case DW_TAG_namespace:
11697 return "DW_TAG_namespace";
11698 case DW_TAG_imported_module:
11699 return "DW_TAG_imported_module";
11700 case DW_TAG_unspecified_type:
11701 return "DW_TAG_unspecified_type";
11702 case DW_TAG_partial_unit:
11703 return "DW_TAG_partial_unit";
11704 case DW_TAG_imported_unit:
11705 return "DW_TAG_imported_unit";
b7619582
GF
11706 case DW_TAG_condition:
11707 return "DW_TAG_condition";
11708 case DW_TAG_shared_type:
11709 return "DW_TAG_shared_type";
348e048f
DE
11710 case DW_TAG_type_unit:
11711 return "DW_TAG_type_unit";
c906108c
SS
11712 case DW_TAG_MIPS_loop:
11713 return "DW_TAG_MIPS_loop";
b7619582
GF
11714 case DW_TAG_HP_array_descriptor:
11715 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11716 case DW_TAG_format_label:
11717 return "DW_TAG_format_label";
11718 case DW_TAG_function_template:
11719 return "DW_TAG_function_template";
11720 case DW_TAG_class_template:
11721 return "DW_TAG_class_template";
b7619582
GF
11722 case DW_TAG_GNU_BINCL:
11723 return "DW_TAG_GNU_BINCL";
11724 case DW_TAG_GNU_EINCL:
11725 return "DW_TAG_GNU_EINCL";
11726 case DW_TAG_upc_shared_type:
11727 return "DW_TAG_upc_shared_type";
11728 case DW_TAG_upc_strict_type:
11729 return "DW_TAG_upc_strict_type";
11730 case DW_TAG_upc_relaxed_type:
11731 return "DW_TAG_upc_relaxed_type";
11732 case DW_TAG_PGI_kanji_type:
11733 return "DW_TAG_PGI_kanji_type";
11734 case DW_TAG_PGI_interface_block:
11735 return "DW_TAG_PGI_interface_block";
c906108c
SS
11736 default:
11737 return "DW_TAG_<unknown>";
11738 }
11739}
11740
11741/* Convert a DWARF attribute code into its string name. */
11742
11743static char *
aa1ee363 11744dwarf_attr_name (unsigned attr)
c906108c
SS
11745{
11746 switch (attr)
11747 {
11748 case DW_AT_sibling:
11749 return "DW_AT_sibling";
11750 case DW_AT_location:
11751 return "DW_AT_location";
11752 case DW_AT_name:
11753 return "DW_AT_name";
11754 case DW_AT_ordering:
11755 return "DW_AT_ordering";
11756 case DW_AT_subscr_data:
11757 return "DW_AT_subscr_data";
11758 case DW_AT_byte_size:
11759 return "DW_AT_byte_size";
11760 case DW_AT_bit_offset:
11761 return "DW_AT_bit_offset";
11762 case DW_AT_bit_size:
11763 return "DW_AT_bit_size";
11764 case DW_AT_element_list:
11765 return "DW_AT_element_list";
11766 case DW_AT_stmt_list:
11767 return "DW_AT_stmt_list";
11768 case DW_AT_low_pc:
11769 return "DW_AT_low_pc";
11770 case DW_AT_high_pc:
11771 return "DW_AT_high_pc";
11772 case DW_AT_language:
11773 return "DW_AT_language";
11774 case DW_AT_member:
11775 return "DW_AT_member";
11776 case DW_AT_discr:
11777 return "DW_AT_discr";
11778 case DW_AT_discr_value:
11779 return "DW_AT_discr_value";
11780 case DW_AT_visibility:
11781 return "DW_AT_visibility";
11782 case DW_AT_import:
11783 return "DW_AT_import";
11784 case DW_AT_string_length:
11785 return "DW_AT_string_length";
11786 case DW_AT_common_reference:
11787 return "DW_AT_common_reference";
11788 case DW_AT_comp_dir:
11789 return "DW_AT_comp_dir";
11790 case DW_AT_const_value:
11791 return "DW_AT_const_value";
11792 case DW_AT_containing_type:
11793 return "DW_AT_containing_type";
11794 case DW_AT_default_value:
11795 return "DW_AT_default_value";
11796 case DW_AT_inline:
11797 return "DW_AT_inline";
11798 case DW_AT_is_optional:
11799 return "DW_AT_is_optional";
11800 case DW_AT_lower_bound:
11801 return "DW_AT_lower_bound";
11802 case DW_AT_producer:
11803 return "DW_AT_producer";
11804 case DW_AT_prototyped:
11805 return "DW_AT_prototyped";
11806 case DW_AT_return_addr:
11807 return "DW_AT_return_addr";
11808 case DW_AT_start_scope:
11809 return "DW_AT_start_scope";
09fa0d7c
JK
11810 case DW_AT_bit_stride:
11811 return "DW_AT_bit_stride";
c906108c
SS
11812 case DW_AT_upper_bound:
11813 return "DW_AT_upper_bound";
11814 case DW_AT_abstract_origin:
11815 return "DW_AT_abstract_origin";
11816 case DW_AT_accessibility:
11817 return "DW_AT_accessibility";
11818 case DW_AT_address_class:
11819 return "DW_AT_address_class";
11820 case DW_AT_artificial:
11821 return "DW_AT_artificial";
11822 case DW_AT_base_types:
11823 return "DW_AT_base_types";
11824 case DW_AT_calling_convention:
11825 return "DW_AT_calling_convention";
11826 case DW_AT_count:
11827 return "DW_AT_count";
11828 case DW_AT_data_member_location:
11829 return "DW_AT_data_member_location";
11830 case DW_AT_decl_column:
11831 return "DW_AT_decl_column";
11832 case DW_AT_decl_file:
11833 return "DW_AT_decl_file";
11834 case DW_AT_decl_line:
11835 return "DW_AT_decl_line";
11836 case DW_AT_declaration:
11837 return "DW_AT_declaration";
11838 case DW_AT_discr_list:
11839 return "DW_AT_discr_list";
11840 case DW_AT_encoding:
11841 return "DW_AT_encoding";
11842 case DW_AT_external:
11843 return "DW_AT_external";
11844 case DW_AT_frame_base:
11845 return "DW_AT_frame_base";
11846 case DW_AT_friend:
11847 return "DW_AT_friend";
11848 case DW_AT_identifier_case:
11849 return "DW_AT_identifier_case";
11850 case DW_AT_macro_info:
11851 return "DW_AT_macro_info";
11852 case DW_AT_namelist_items:
11853 return "DW_AT_namelist_items";
11854 case DW_AT_priority:
11855 return "DW_AT_priority";
11856 case DW_AT_segment:
11857 return "DW_AT_segment";
11858 case DW_AT_specification:
11859 return "DW_AT_specification";
11860 case DW_AT_static_link:
11861 return "DW_AT_static_link";
11862 case DW_AT_type:
11863 return "DW_AT_type";
11864 case DW_AT_use_location:
11865 return "DW_AT_use_location";
11866 case DW_AT_variable_parameter:
11867 return "DW_AT_variable_parameter";
11868 case DW_AT_virtuality:
11869 return "DW_AT_virtuality";
11870 case DW_AT_vtable_elem_location:
11871 return "DW_AT_vtable_elem_location";
b7619582 11872 /* DWARF 3 values. */
d9fa45fe
DC
11873 case DW_AT_allocated:
11874 return "DW_AT_allocated";
11875 case DW_AT_associated:
11876 return "DW_AT_associated";
11877 case DW_AT_data_location:
11878 return "DW_AT_data_location";
09fa0d7c
JK
11879 case DW_AT_byte_stride:
11880 return "DW_AT_byte_stride";
d9fa45fe
DC
11881 case DW_AT_entry_pc:
11882 return "DW_AT_entry_pc";
11883 case DW_AT_use_UTF8:
11884 return "DW_AT_use_UTF8";
11885 case DW_AT_extension:
11886 return "DW_AT_extension";
11887 case DW_AT_ranges:
11888 return "DW_AT_ranges";
11889 case DW_AT_trampoline:
11890 return "DW_AT_trampoline";
11891 case DW_AT_call_column:
11892 return "DW_AT_call_column";
11893 case DW_AT_call_file:
11894 return "DW_AT_call_file";
11895 case DW_AT_call_line:
11896 return "DW_AT_call_line";
b7619582
GF
11897 case DW_AT_description:
11898 return "DW_AT_description";
11899 case DW_AT_binary_scale:
11900 return "DW_AT_binary_scale";
11901 case DW_AT_decimal_scale:
11902 return "DW_AT_decimal_scale";
11903 case DW_AT_small:
11904 return "DW_AT_small";
11905 case DW_AT_decimal_sign:
11906 return "DW_AT_decimal_sign";
11907 case DW_AT_digit_count:
11908 return "DW_AT_digit_count";
11909 case DW_AT_picture_string:
11910 return "DW_AT_picture_string";
11911 case DW_AT_mutable:
11912 return "DW_AT_mutable";
11913 case DW_AT_threads_scaled:
11914 return "DW_AT_threads_scaled";
11915 case DW_AT_explicit:
11916 return "DW_AT_explicit";
11917 case DW_AT_object_pointer:
11918 return "DW_AT_object_pointer";
11919 case DW_AT_endianity:
11920 return "DW_AT_endianity";
11921 case DW_AT_elemental:
11922 return "DW_AT_elemental";
11923 case DW_AT_pure:
11924 return "DW_AT_pure";
11925 case DW_AT_recursive:
11926 return "DW_AT_recursive";
348e048f
DE
11927 /* DWARF 4 values. */
11928 case DW_AT_signature:
11929 return "DW_AT_signature";
31ef98ae
TT
11930 case DW_AT_linkage_name:
11931 return "DW_AT_linkage_name";
b7619582 11932 /* SGI/MIPS extensions. */
c764a876 11933#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11934 case DW_AT_MIPS_fde:
11935 return "DW_AT_MIPS_fde";
c764a876 11936#endif
c906108c
SS
11937 case DW_AT_MIPS_loop_begin:
11938 return "DW_AT_MIPS_loop_begin";
11939 case DW_AT_MIPS_tail_loop_begin:
11940 return "DW_AT_MIPS_tail_loop_begin";
11941 case DW_AT_MIPS_epilog_begin:
11942 return "DW_AT_MIPS_epilog_begin";
11943 case DW_AT_MIPS_loop_unroll_factor:
11944 return "DW_AT_MIPS_loop_unroll_factor";
11945 case DW_AT_MIPS_software_pipeline_depth:
11946 return "DW_AT_MIPS_software_pipeline_depth";
11947 case DW_AT_MIPS_linkage_name:
11948 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11949 case DW_AT_MIPS_stride:
11950 return "DW_AT_MIPS_stride";
11951 case DW_AT_MIPS_abstract_name:
11952 return "DW_AT_MIPS_abstract_name";
11953 case DW_AT_MIPS_clone_origin:
11954 return "DW_AT_MIPS_clone_origin";
11955 case DW_AT_MIPS_has_inlines:
11956 return "DW_AT_MIPS_has_inlines";
b7619582 11957 /* HP extensions. */
c764a876 11958#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11959 case DW_AT_HP_block_index:
11960 return "DW_AT_HP_block_index";
c764a876 11961#endif
b7619582
GF
11962 case DW_AT_HP_unmodifiable:
11963 return "DW_AT_HP_unmodifiable";
11964 case DW_AT_HP_actuals_stmt_list:
11965 return "DW_AT_HP_actuals_stmt_list";
11966 case DW_AT_HP_proc_per_section:
11967 return "DW_AT_HP_proc_per_section";
11968 case DW_AT_HP_raw_data_ptr:
11969 return "DW_AT_HP_raw_data_ptr";
11970 case DW_AT_HP_pass_by_reference:
11971 return "DW_AT_HP_pass_by_reference";
11972 case DW_AT_HP_opt_level:
11973 return "DW_AT_HP_opt_level";
11974 case DW_AT_HP_prof_version_id:
11975 return "DW_AT_HP_prof_version_id";
11976 case DW_AT_HP_opt_flags:
11977 return "DW_AT_HP_opt_flags";
11978 case DW_AT_HP_cold_region_low_pc:
11979 return "DW_AT_HP_cold_region_low_pc";
11980 case DW_AT_HP_cold_region_high_pc:
11981 return "DW_AT_HP_cold_region_high_pc";
11982 case DW_AT_HP_all_variables_modifiable:
11983 return "DW_AT_HP_all_variables_modifiable";
11984 case DW_AT_HP_linkage_name:
11985 return "DW_AT_HP_linkage_name";
11986 case DW_AT_HP_prof_flags:
11987 return "DW_AT_HP_prof_flags";
11988 /* GNU extensions. */
c906108c
SS
11989 case DW_AT_sf_names:
11990 return "DW_AT_sf_names";
11991 case DW_AT_src_info:
11992 return "DW_AT_src_info";
11993 case DW_AT_mac_info:
11994 return "DW_AT_mac_info";
11995 case DW_AT_src_coords:
11996 return "DW_AT_src_coords";
11997 case DW_AT_body_begin:
11998 return "DW_AT_body_begin";
11999 case DW_AT_body_end:
12000 return "DW_AT_body_end";
f5f8a009
EZ
12001 case DW_AT_GNU_vector:
12002 return "DW_AT_GNU_vector";
2de00c64
DE
12003 case DW_AT_GNU_odr_signature:
12004 return "DW_AT_GNU_odr_signature";
b7619582
GF
12005 /* VMS extensions. */
12006 case DW_AT_VMS_rtnbeg_pd_address:
12007 return "DW_AT_VMS_rtnbeg_pd_address";
12008 /* UPC extension. */
12009 case DW_AT_upc_threads_scaled:
12010 return "DW_AT_upc_threads_scaled";
12011 /* PGI (STMicroelectronics) extensions. */
12012 case DW_AT_PGI_lbase:
12013 return "DW_AT_PGI_lbase";
12014 case DW_AT_PGI_soffset:
12015 return "DW_AT_PGI_soffset";
12016 case DW_AT_PGI_lstride:
12017 return "DW_AT_PGI_lstride";
c906108c
SS
12018 default:
12019 return "DW_AT_<unknown>";
12020 }
12021}
12022
12023/* Convert a DWARF value form code into its string name. */
12024
12025static char *
aa1ee363 12026dwarf_form_name (unsigned form)
c906108c
SS
12027{
12028 switch (form)
12029 {
12030 case DW_FORM_addr:
12031 return "DW_FORM_addr";
12032 case DW_FORM_block2:
12033 return "DW_FORM_block2";
12034 case DW_FORM_block4:
12035 return "DW_FORM_block4";
12036 case DW_FORM_data2:
12037 return "DW_FORM_data2";
12038 case DW_FORM_data4:
12039 return "DW_FORM_data4";
12040 case DW_FORM_data8:
12041 return "DW_FORM_data8";
12042 case DW_FORM_string:
12043 return "DW_FORM_string";
12044 case DW_FORM_block:
12045 return "DW_FORM_block";
12046 case DW_FORM_block1:
12047 return "DW_FORM_block1";
12048 case DW_FORM_data1:
12049 return "DW_FORM_data1";
12050 case DW_FORM_flag:
12051 return "DW_FORM_flag";
12052 case DW_FORM_sdata:
12053 return "DW_FORM_sdata";
12054 case DW_FORM_strp:
12055 return "DW_FORM_strp";
12056 case DW_FORM_udata:
12057 return "DW_FORM_udata";
12058 case DW_FORM_ref_addr:
12059 return "DW_FORM_ref_addr";
12060 case DW_FORM_ref1:
12061 return "DW_FORM_ref1";
12062 case DW_FORM_ref2:
12063 return "DW_FORM_ref2";
12064 case DW_FORM_ref4:
12065 return "DW_FORM_ref4";
12066 case DW_FORM_ref8:
12067 return "DW_FORM_ref8";
12068 case DW_FORM_ref_udata:
12069 return "DW_FORM_ref_udata";
12070 case DW_FORM_indirect:
12071 return "DW_FORM_indirect";
348e048f
DE
12072 case DW_FORM_sec_offset:
12073 return "DW_FORM_sec_offset";
12074 case DW_FORM_exprloc:
12075 return "DW_FORM_exprloc";
12076 case DW_FORM_flag_present:
12077 return "DW_FORM_flag_present";
12078 case DW_FORM_sig8:
12079 return "DW_FORM_sig8";
c906108c
SS
12080 default:
12081 return "DW_FORM_<unknown>";
12082 }
12083}
12084
12085/* Convert a DWARF stack opcode into its string name. */
12086
9eae7c52
TT
12087const char *
12088dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12089{
12090 switch (op)
12091 {
12092 case DW_OP_addr:
12093 return "DW_OP_addr";
12094 case DW_OP_deref:
12095 return "DW_OP_deref";
12096 case DW_OP_const1u:
12097 return "DW_OP_const1u";
12098 case DW_OP_const1s:
12099 return "DW_OP_const1s";
12100 case DW_OP_const2u:
12101 return "DW_OP_const2u";
12102 case DW_OP_const2s:
12103 return "DW_OP_const2s";
12104 case DW_OP_const4u:
12105 return "DW_OP_const4u";
12106 case DW_OP_const4s:
12107 return "DW_OP_const4s";
12108 case DW_OP_const8u:
12109 return "DW_OP_const8u";
12110 case DW_OP_const8s:
12111 return "DW_OP_const8s";
12112 case DW_OP_constu:
12113 return "DW_OP_constu";
12114 case DW_OP_consts:
12115 return "DW_OP_consts";
12116 case DW_OP_dup:
12117 return "DW_OP_dup";
12118 case DW_OP_drop:
12119 return "DW_OP_drop";
12120 case DW_OP_over:
12121 return "DW_OP_over";
12122 case DW_OP_pick:
12123 return "DW_OP_pick";
12124 case DW_OP_swap:
12125 return "DW_OP_swap";
12126 case DW_OP_rot:
12127 return "DW_OP_rot";
12128 case DW_OP_xderef:
12129 return "DW_OP_xderef";
12130 case DW_OP_abs:
12131 return "DW_OP_abs";
12132 case DW_OP_and:
12133 return "DW_OP_and";
12134 case DW_OP_div:
12135 return "DW_OP_div";
12136 case DW_OP_minus:
12137 return "DW_OP_minus";
12138 case DW_OP_mod:
12139 return "DW_OP_mod";
12140 case DW_OP_mul:
12141 return "DW_OP_mul";
12142 case DW_OP_neg:
12143 return "DW_OP_neg";
12144 case DW_OP_not:
12145 return "DW_OP_not";
12146 case DW_OP_or:
12147 return "DW_OP_or";
12148 case DW_OP_plus:
12149 return "DW_OP_plus";
12150 case DW_OP_plus_uconst:
12151 return "DW_OP_plus_uconst";
12152 case DW_OP_shl:
12153 return "DW_OP_shl";
12154 case DW_OP_shr:
12155 return "DW_OP_shr";
12156 case DW_OP_shra:
12157 return "DW_OP_shra";
12158 case DW_OP_xor:
12159 return "DW_OP_xor";
12160 case DW_OP_bra:
12161 return "DW_OP_bra";
12162 case DW_OP_eq:
12163 return "DW_OP_eq";
12164 case DW_OP_ge:
12165 return "DW_OP_ge";
12166 case DW_OP_gt:
12167 return "DW_OP_gt";
12168 case DW_OP_le:
12169 return "DW_OP_le";
12170 case DW_OP_lt:
12171 return "DW_OP_lt";
12172 case DW_OP_ne:
12173 return "DW_OP_ne";
12174 case DW_OP_skip:
12175 return "DW_OP_skip";
12176 case DW_OP_lit0:
12177 return "DW_OP_lit0";
12178 case DW_OP_lit1:
12179 return "DW_OP_lit1";
12180 case DW_OP_lit2:
12181 return "DW_OP_lit2";
12182 case DW_OP_lit3:
12183 return "DW_OP_lit3";
12184 case DW_OP_lit4:
12185 return "DW_OP_lit4";
12186 case DW_OP_lit5:
12187 return "DW_OP_lit5";
12188 case DW_OP_lit6:
12189 return "DW_OP_lit6";
12190 case DW_OP_lit7:
12191 return "DW_OP_lit7";
12192 case DW_OP_lit8:
12193 return "DW_OP_lit8";
12194 case DW_OP_lit9:
12195 return "DW_OP_lit9";
12196 case DW_OP_lit10:
12197 return "DW_OP_lit10";
12198 case DW_OP_lit11:
12199 return "DW_OP_lit11";
12200 case DW_OP_lit12:
12201 return "DW_OP_lit12";
12202 case DW_OP_lit13:
12203 return "DW_OP_lit13";
12204 case DW_OP_lit14:
12205 return "DW_OP_lit14";
12206 case DW_OP_lit15:
12207 return "DW_OP_lit15";
12208 case DW_OP_lit16:
12209 return "DW_OP_lit16";
12210 case DW_OP_lit17:
12211 return "DW_OP_lit17";
12212 case DW_OP_lit18:
12213 return "DW_OP_lit18";
12214 case DW_OP_lit19:
12215 return "DW_OP_lit19";
12216 case DW_OP_lit20:
12217 return "DW_OP_lit20";
12218 case DW_OP_lit21:
12219 return "DW_OP_lit21";
12220 case DW_OP_lit22:
12221 return "DW_OP_lit22";
12222 case DW_OP_lit23:
12223 return "DW_OP_lit23";
12224 case DW_OP_lit24:
12225 return "DW_OP_lit24";
12226 case DW_OP_lit25:
12227 return "DW_OP_lit25";
12228 case DW_OP_lit26:
12229 return "DW_OP_lit26";
12230 case DW_OP_lit27:
12231 return "DW_OP_lit27";
12232 case DW_OP_lit28:
12233 return "DW_OP_lit28";
12234 case DW_OP_lit29:
12235 return "DW_OP_lit29";
12236 case DW_OP_lit30:
12237 return "DW_OP_lit30";
12238 case DW_OP_lit31:
12239 return "DW_OP_lit31";
12240 case DW_OP_reg0:
12241 return "DW_OP_reg0";
12242 case DW_OP_reg1:
12243 return "DW_OP_reg1";
12244 case DW_OP_reg2:
12245 return "DW_OP_reg2";
12246 case DW_OP_reg3:
12247 return "DW_OP_reg3";
12248 case DW_OP_reg4:
12249 return "DW_OP_reg4";
12250 case DW_OP_reg5:
12251 return "DW_OP_reg5";
12252 case DW_OP_reg6:
12253 return "DW_OP_reg6";
12254 case DW_OP_reg7:
12255 return "DW_OP_reg7";
12256 case DW_OP_reg8:
12257 return "DW_OP_reg8";
12258 case DW_OP_reg9:
12259 return "DW_OP_reg9";
12260 case DW_OP_reg10:
12261 return "DW_OP_reg10";
12262 case DW_OP_reg11:
12263 return "DW_OP_reg11";
12264 case DW_OP_reg12:
12265 return "DW_OP_reg12";
12266 case DW_OP_reg13:
12267 return "DW_OP_reg13";
12268 case DW_OP_reg14:
12269 return "DW_OP_reg14";
12270 case DW_OP_reg15:
12271 return "DW_OP_reg15";
12272 case DW_OP_reg16:
12273 return "DW_OP_reg16";
12274 case DW_OP_reg17:
12275 return "DW_OP_reg17";
12276 case DW_OP_reg18:
12277 return "DW_OP_reg18";
12278 case DW_OP_reg19:
12279 return "DW_OP_reg19";
12280 case DW_OP_reg20:
12281 return "DW_OP_reg20";
12282 case DW_OP_reg21:
12283 return "DW_OP_reg21";
12284 case DW_OP_reg22:
12285 return "DW_OP_reg22";
12286 case DW_OP_reg23:
12287 return "DW_OP_reg23";
12288 case DW_OP_reg24:
12289 return "DW_OP_reg24";
12290 case DW_OP_reg25:
12291 return "DW_OP_reg25";
12292 case DW_OP_reg26:
12293 return "DW_OP_reg26";
12294 case DW_OP_reg27:
12295 return "DW_OP_reg27";
12296 case DW_OP_reg28:
12297 return "DW_OP_reg28";
12298 case DW_OP_reg29:
12299 return "DW_OP_reg29";
12300 case DW_OP_reg30:
12301 return "DW_OP_reg30";
12302 case DW_OP_reg31:
12303 return "DW_OP_reg31";
12304 case DW_OP_breg0:
12305 return "DW_OP_breg0";
12306 case DW_OP_breg1:
12307 return "DW_OP_breg1";
12308 case DW_OP_breg2:
12309 return "DW_OP_breg2";
12310 case DW_OP_breg3:
12311 return "DW_OP_breg3";
12312 case DW_OP_breg4:
12313 return "DW_OP_breg4";
12314 case DW_OP_breg5:
12315 return "DW_OP_breg5";
12316 case DW_OP_breg6:
12317 return "DW_OP_breg6";
12318 case DW_OP_breg7:
12319 return "DW_OP_breg7";
12320 case DW_OP_breg8:
12321 return "DW_OP_breg8";
12322 case DW_OP_breg9:
12323 return "DW_OP_breg9";
12324 case DW_OP_breg10:
12325 return "DW_OP_breg10";
12326 case DW_OP_breg11:
12327 return "DW_OP_breg11";
12328 case DW_OP_breg12:
12329 return "DW_OP_breg12";
12330 case DW_OP_breg13:
12331 return "DW_OP_breg13";
12332 case DW_OP_breg14:
12333 return "DW_OP_breg14";
12334 case DW_OP_breg15:
12335 return "DW_OP_breg15";
12336 case DW_OP_breg16:
12337 return "DW_OP_breg16";
12338 case DW_OP_breg17:
12339 return "DW_OP_breg17";
12340 case DW_OP_breg18:
12341 return "DW_OP_breg18";
12342 case DW_OP_breg19:
12343 return "DW_OP_breg19";
12344 case DW_OP_breg20:
12345 return "DW_OP_breg20";
12346 case DW_OP_breg21:
12347 return "DW_OP_breg21";
12348 case DW_OP_breg22:
12349 return "DW_OP_breg22";
12350 case DW_OP_breg23:
12351 return "DW_OP_breg23";
12352 case DW_OP_breg24:
12353 return "DW_OP_breg24";
12354 case DW_OP_breg25:
12355 return "DW_OP_breg25";
12356 case DW_OP_breg26:
12357 return "DW_OP_breg26";
12358 case DW_OP_breg27:
12359 return "DW_OP_breg27";
12360 case DW_OP_breg28:
12361 return "DW_OP_breg28";
12362 case DW_OP_breg29:
12363 return "DW_OP_breg29";
12364 case DW_OP_breg30:
12365 return "DW_OP_breg30";
12366 case DW_OP_breg31:
12367 return "DW_OP_breg31";
12368 case DW_OP_regx:
12369 return "DW_OP_regx";
12370 case DW_OP_fbreg:
12371 return "DW_OP_fbreg";
12372 case DW_OP_bregx:
12373 return "DW_OP_bregx";
12374 case DW_OP_piece:
12375 return "DW_OP_piece";
12376 case DW_OP_deref_size:
12377 return "DW_OP_deref_size";
12378 case DW_OP_xderef_size:
12379 return "DW_OP_xderef_size";
12380 case DW_OP_nop:
12381 return "DW_OP_nop";
b7619582 12382 /* DWARF 3 extensions. */
ed348acc
EZ
12383 case DW_OP_push_object_address:
12384 return "DW_OP_push_object_address";
12385 case DW_OP_call2:
12386 return "DW_OP_call2";
12387 case DW_OP_call4:
12388 return "DW_OP_call4";
12389 case DW_OP_call_ref:
12390 return "DW_OP_call_ref";
b7619582
GF
12391 case DW_OP_form_tls_address:
12392 return "DW_OP_form_tls_address";
12393 case DW_OP_call_frame_cfa:
12394 return "DW_OP_call_frame_cfa";
12395 case DW_OP_bit_piece:
12396 return "DW_OP_bit_piece";
9eae7c52
TT
12397 /* DWARF 4 extensions. */
12398 case DW_OP_implicit_value:
12399 return "DW_OP_implicit_value";
12400 case DW_OP_stack_value:
12401 return "DW_OP_stack_value";
12402 /* GNU extensions. */
ed348acc
EZ
12403 case DW_OP_GNU_push_tls_address:
12404 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12405 case DW_OP_GNU_uninit:
12406 return "DW_OP_GNU_uninit";
c906108c 12407 default:
9eae7c52 12408 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12409 }
12410}
12411
12412static char *
fba45db2 12413dwarf_bool_name (unsigned mybool)
c906108c
SS
12414{
12415 if (mybool)
12416 return "TRUE";
12417 else
12418 return "FALSE";
12419}
12420
12421/* Convert a DWARF type code into its string name. */
12422
12423static char *
aa1ee363 12424dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12425{
12426 switch (enc)
12427 {
b7619582
GF
12428 case DW_ATE_void:
12429 return "DW_ATE_void";
c906108c
SS
12430 case DW_ATE_address:
12431 return "DW_ATE_address";
12432 case DW_ATE_boolean:
12433 return "DW_ATE_boolean";
12434 case DW_ATE_complex_float:
12435 return "DW_ATE_complex_float";
12436 case DW_ATE_float:
12437 return "DW_ATE_float";
12438 case DW_ATE_signed:
12439 return "DW_ATE_signed";
12440 case DW_ATE_signed_char:
12441 return "DW_ATE_signed_char";
12442 case DW_ATE_unsigned:
12443 return "DW_ATE_unsigned";
12444 case DW_ATE_unsigned_char:
12445 return "DW_ATE_unsigned_char";
b7619582 12446 /* DWARF 3. */
d9fa45fe
DC
12447 case DW_ATE_imaginary_float:
12448 return "DW_ATE_imaginary_float";
b7619582
GF
12449 case DW_ATE_packed_decimal:
12450 return "DW_ATE_packed_decimal";
12451 case DW_ATE_numeric_string:
12452 return "DW_ATE_numeric_string";
12453 case DW_ATE_edited:
12454 return "DW_ATE_edited";
12455 case DW_ATE_signed_fixed:
12456 return "DW_ATE_signed_fixed";
12457 case DW_ATE_unsigned_fixed:
12458 return "DW_ATE_unsigned_fixed";
12459 case DW_ATE_decimal_float:
12460 return "DW_ATE_decimal_float";
75079b2b
TT
12461 /* DWARF 4. */
12462 case DW_ATE_UTF:
12463 return "DW_ATE_UTF";
b7619582
GF
12464 /* HP extensions. */
12465 case DW_ATE_HP_float80:
12466 return "DW_ATE_HP_float80";
12467 case DW_ATE_HP_complex_float80:
12468 return "DW_ATE_HP_complex_float80";
12469 case DW_ATE_HP_float128:
12470 return "DW_ATE_HP_float128";
12471 case DW_ATE_HP_complex_float128:
12472 return "DW_ATE_HP_complex_float128";
12473 case DW_ATE_HP_floathpintel:
12474 return "DW_ATE_HP_floathpintel";
12475 case DW_ATE_HP_imaginary_float80:
12476 return "DW_ATE_HP_imaginary_float80";
12477 case DW_ATE_HP_imaginary_float128:
12478 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12479 default:
12480 return "DW_ATE_<unknown>";
12481 }
12482}
12483
12484/* Convert a DWARF call frame info operation to its string name. */
12485
12486#if 0
12487static char *
aa1ee363 12488dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12489{
12490 switch (cfi_opc)
12491 {
12492 case DW_CFA_advance_loc:
12493 return "DW_CFA_advance_loc";
12494 case DW_CFA_offset:
12495 return "DW_CFA_offset";
12496 case DW_CFA_restore:
12497 return "DW_CFA_restore";
12498 case DW_CFA_nop:
12499 return "DW_CFA_nop";
12500 case DW_CFA_set_loc:
12501 return "DW_CFA_set_loc";
12502 case DW_CFA_advance_loc1:
12503 return "DW_CFA_advance_loc1";
12504 case DW_CFA_advance_loc2:
12505 return "DW_CFA_advance_loc2";
12506 case DW_CFA_advance_loc4:
12507 return "DW_CFA_advance_loc4";
12508 case DW_CFA_offset_extended:
12509 return "DW_CFA_offset_extended";
12510 case DW_CFA_restore_extended:
12511 return "DW_CFA_restore_extended";
12512 case DW_CFA_undefined:
12513 return "DW_CFA_undefined";
12514 case DW_CFA_same_value:
12515 return "DW_CFA_same_value";
12516 case DW_CFA_register:
12517 return "DW_CFA_register";
12518 case DW_CFA_remember_state:
12519 return "DW_CFA_remember_state";
12520 case DW_CFA_restore_state:
12521 return "DW_CFA_restore_state";
12522 case DW_CFA_def_cfa:
12523 return "DW_CFA_def_cfa";
12524 case DW_CFA_def_cfa_register:
12525 return "DW_CFA_def_cfa_register";
12526 case DW_CFA_def_cfa_offset:
12527 return "DW_CFA_def_cfa_offset";
b7619582 12528 /* DWARF 3. */
985cb1a3
JM
12529 case DW_CFA_def_cfa_expression:
12530 return "DW_CFA_def_cfa_expression";
12531 case DW_CFA_expression:
12532 return "DW_CFA_expression";
12533 case DW_CFA_offset_extended_sf:
12534 return "DW_CFA_offset_extended_sf";
12535 case DW_CFA_def_cfa_sf:
12536 return "DW_CFA_def_cfa_sf";
12537 case DW_CFA_def_cfa_offset_sf:
12538 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12539 case DW_CFA_val_offset:
12540 return "DW_CFA_val_offset";
12541 case DW_CFA_val_offset_sf:
12542 return "DW_CFA_val_offset_sf";
12543 case DW_CFA_val_expression:
12544 return "DW_CFA_val_expression";
12545 /* SGI/MIPS specific. */
c906108c
SS
12546 case DW_CFA_MIPS_advance_loc8:
12547 return "DW_CFA_MIPS_advance_loc8";
b7619582 12548 /* GNU extensions. */
985cb1a3
JM
12549 case DW_CFA_GNU_window_save:
12550 return "DW_CFA_GNU_window_save";
12551 case DW_CFA_GNU_args_size:
12552 return "DW_CFA_GNU_args_size";
12553 case DW_CFA_GNU_negative_offset_extended:
12554 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12555 default:
12556 return "DW_CFA_<unknown>";
12557 }
12558}
12559#endif
12560
f9aca02d 12561static void
d97bc12b 12562dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12563{
12564 unsigned int i;
12565
d97bc12b
DE
12566 print_spaces (indent, f);
12567 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12568 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12569
12570 if (die->parent != NULL)
12571 {
12572 print_spaces (indent, f);
12573 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12574 die->parent->offset);
12575 }
12576
12577 print_spaces (indent, f);
12578 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12579 dwarf_bool_name (die->child != NULL));
c906108c 12580
d97bc12b
DE
12581 print_spaces (indent, f);
12582 fprintf_unfiltered (f, " attributes:\n");
12583
c906108c
SS
12584 for (i = 0; i < die->num_attrs; ++i)
12585 {
d97bc12b
DE
12586 print_spaces (indent, f);
12587 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12588 dwarf_attr_name (die->attrs[i].name),
12589 dwarf_form_name (die->attrs[i].form));
d97bc12b 12590
c906108c
SS
12591 switch (die->attrs[i].form)
12592 {
12593 case DW_FORM_ref_addr:
12594 case DW_FORM_addr:
d97bc12b 12595 fprintf_unfiltered (f, "address: ");
5af949e3 12596 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12597 break;
12598 case DW_FORM_block2:
12599 case DW_FORM_block4:
12600 case DW_FORM_block:
12601 case DW_FORM_block1:
d97bc12b 12602 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12603 break;
2dc7f7b3
TT
12604 case DW_FORM_exprloc:
12605 fprintf_unfiltered (f, "expression: size %u",
12606 DW_BLOCK (&die->attrs[i])->size);
12607 break;
10b3939b
DJ
12608 case DW_FORM_ref1:
12609 case DW_FORM_ref2:
12610 case DW_FORM_ref4:
d97bc12b 12611 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12612 (long) (DW_ADDR (&die->attrs[i])));
12613 break;
c906108c
SS
12614 case DW_FORM_data1:
12615 case DW_FORM_data2:
12616 case DW_FORM_data4:
ce5d95e1 12617 case DW_FORM_data8:
c906108c
SS
12618 case DW_FORM_udata:
12619 case DW_FORM_sdata:
43bbcdc2
PH
12620 fprintf_unfiltered (f, "constant: %s",
12621 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12622 break;
2dc7f7b3
TT
12623 case DW_FORM_sec_offset:
12624 fprintf_unfiltered (f, "section offset: %s",
12625 pulongest (DW_UNSND (&die->attrs[i])));
12626 break;
348e048f
DE
12627 case DW_FORM_sig8:
12628 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12629 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12630 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12631 else
12632 fprintf_unfiltered (f, "signatured type, offset: unknown");
12633 break;
c906108c 12634 case DW_FORM_string:
4bdf3d34 12635 case DW_FORM_strp:
8285870a 12636 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12637 DW_STRING (&die->attrs[i])
8285870a
JK
12638 ? DW_STRING (&die->attrs[i]) : "",
12639 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12640 break;
12641 case DW_FORM_flag:
12642 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12643 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12644 else
d97bc12b 12645 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12646 break;
2dc7f7b3
TT
12647 case DW_FORM_flag_present:
12648 fprintf_unfiltered (f, "flag: TRUE");
12649 break;
a8329558
KW
12650 case DW_FORM_indirect:
12651 /* the reader will have reduced the indirect form to
12652 the "base form" so this form should not occur */
d97bc12b 12653 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12654 break;
c906108c 12655 default:
d97bc12b 12656 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12657 die->attrs[i].form);
d97bc12b 12658 break;
c906108c 12659 }
d97bc12b 12660 fprintf_unfiltered (f, "\n");
c906108c
SS
12661 }
12662}
12663
f9aca02d 12664static void
d97bc12b 12665dump_die_for_error (struct die_info *die)
c906108c 12666{
d97bc12b
DE
12667 dump_die_shallow (gdb_stderr, 0, die);
12668}
12669
12670static void
12671dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12672{
12673 int indent = level * 4;
12674
12675 gdb_assert (die != NULL);
12676
12677 if (level >= max_level)
12678 return;
12679
12680 dump_die_shallow (f, indent, die);
12681
12682 if (die->child != NULL)
c906108c 12683 {
d97bc12b
DE
12684 print_spaces (indent, f);
12685 fprintf_unfiltered (f, " Children:");
12686 if (level + 1 < max_level)
12687 {
12688 fprintf_unfiltered (f, "\n");
12689 dump_die_1 (f, level + 1, max_level, die->child);
12690 }
12691 else
12692 {
12693 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12694 }
12695 }
12696
12697 if (die->sibling != NULL && level > 0)
12698 {
12699 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12700 }
12701}
12702
d97bc12b
DE
12703/* This is called from the pdie macro in gdbinit.in.
12704 It's not static so gcc will keep a copy callable from gdb. */
12705
12706void
12707dump_die (struct die_info *die, int max_level)
12708{
12709 dump_die_1 (gdb_stdlog, 0, max_level, die);
12710}
12711
f9aca02d 12712static void
51545339 12713store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12714{
51545339 12715 void **slot;
c906108c 12716
51545339
DJ
12717 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12718
12719 *slot = die;
c906108c
SS
12720}
12721
93311388
DE
12722static int
12723is_ref_attr (struct attribute *attr)
c906108c 12724{
c906108c
SS
12725 switch (attr->form)
12726 {
12727 case DW_FORM_ref_addr:
c906108c
SS
12728 case DW_FORM_ref1:
12729 case DW_FORM_ref2:
12730 case DW_FORM_ref4:
613e1657 12731 case DW_FORM_ref8:
c906108c 12732 case DW_FORM_ref_udata:
93311388 12733 return 1;
c906108c 12734 default:
93311388 12735 return 0;
c906108c 12736 }
93311388
DE
12737}
12738
12739static unsigned int
12740dwarf2_get_ref_die_offset (struct attribute *attr)
12741{
12742 if (is_ref_attr (attr))
12743 return DW_ADDR (attr);
12744
12745 complaint (&symfile_complaints,
12746 _("unsupported die ref attribute form: '%s'"),
12747 dwarf_form_name (attr->form));
12748 return 0;
c906108c
SS
12749}
12750
43bbcdc2
PH
12751/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12752 * the value held by the attribute is not constant. */
a02abb62 12753
43bbcdc2 12754static LONGEST
a02abb62
JB
12755dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12756{
12757 if (attr->form == DW_FORM_sdata)
12758 return DW_SND (attr);
12759 else if (attr->form == DW_FORM_udata
12760 || attr->form == DW_FORM_data1
12761 || attr->form == DW_FORM_data2
12762 || attr->form == DW_FORM_data4
12763 || attr->form == DW_FORM_data8)
12764 return DW_UNSND (attr);
12765 else
12766 {
e2e0b3e5 12767 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12768 dwarf_form_name (attr->form));
12769 return default_value;
12770 }
12771}
12772
03dd20cc 12773/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12774 unit and add it to our queue.
12775 The result is non-zero if PER_CU was queued, otherwise the result is zero
12776 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12777
348e048f 12778static int
03dd20cc
DJ
12779maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12780 struct dwarf2_per_cu_data *per_cu)
12781{
98bfdba5
PA
12782 /* We may arrive here during partial symbol reading, if we need full
12783 DIEs to process an unusual case (e.g. template arguments). Do
12784 not queue PER_CU, just tell our caller to load its DIEs. */
12785 if (dwarf2_per_objfile->reading_partial_symbols)
12786 {
12787 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12788 return 1;
12789 return 0;
12790 }
12791
03dd20cc
DJ
12792 /* Mark the dependence relation so that we don't flush PER_CU
12793 too early. */
12794 dwarf2_add_dependence (this_cu, per_cu);
12795
12796 /* If it's already on the queue, we have nothing to do. */
12797 if (per_cu->queued)
348e048f 12798 return 0;
03dd20cc
DJ
12799
12800 /* If the compilation unit is already loaded, just mark it as
12801 used. */
12802 if (per_cu->cu != NULL)
12803 {
12804 per_cu->cu->last_used = 0;
348e048f 12805 return 0;
03dd20cc
DJ
12806 }
12807
12808 /* Add it to the queue. */
12809 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12810
12811 return 1;
12812}
12813
12814/* Follow reference or signature attribute ATTR of SRC_DIE.
12815 On entry *REF_CU is the CU of SRC_DIE.
12816 On exit *REF_CU is the CU of the result. */
12817
12818static struct die_info *
12819follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12820 struct dwarf2_cu **ref_cu)
12821{
12822 struct die_info *die;
12823
12824 if (is_ref_attr (attr))
12825 die = follow_die_ref (src_die, attr, ref_cu);
12826 else if (attr->form == DW_FORM_sig8)
12827 die = follow_die_sig (src_die, attr, ref_cu);
12828 else
12829 {
12830 dump_die_for_error (src_die);
12831 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12832 (*ref_cu)->objfile->name);
12833 }
12834
12835 return die;
03dd20cc
DJ
12836}
12837
5c631832 12838/* Follow reference OFFSET.
673bfd45
DE
12839 On entry *REF_CU is the CU of the source die referencing OFFSET.
12840 On exit *REF_CU is the CU of the result.
12841 Returns NULL if OFFSET is invalid. */
f504f079 12842
f9aca02d 12843static struct die_info *
5c631832 12844follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12845{
10b3939b 12846 struct die_info temp_die;
f2f0e013 12847 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12848
348e048f
DE
12849 gdb_assert (cu->per_cu != NULL);
12850
98bfdba5
PA
12851 target_cu = cu;
12852
348e048f
DE
12853 if (cu->per_cu->from_debug_types)
12854 {
12855 /* .debug_types CUs cannot reference anything outside their CU.
12856 If they need to, they have to reference a signatured type via
12857 DW_FORM_sig8. */
12858 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12859 return NULL;
348e048f
DE
12860 }
12861 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12862 {
12863 struct dwarf2_per_cu_data *per_cu;
9a619af0 12864
45452591 12865 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12866
12867 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12868 if (maybe_queue_comp_unit (cu, per_cu))
12869 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12870
10b3939b
DJ
12871 target_cu = per_cu->cu;
12872 }
98bfdba5
PA
12873 else if (cu->dies == NULL)
12874 {
12875 /* We're loading full DIEs during partial symbol reading. */
12876 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12877 load_full_comp_unit (cu->per_cu, cu->objfile);
12878 }
c906108c 12879
f2f0e013 12880 *ref_cu = target_cu;
51545339 12881 temp_die.offset = offset;
5c631832
JK
12882 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12883}
10b3939b 12884
5c631832
JK
12885/* Follow reference attribute ATTR of SRC_DIE.
12886 On entry *REF_CU is the CU of SRC_DIE.
12887 On exit *REF_CU is the CU of the result. */
12888
12889static struct die_info *
12890follow_die_ref (struct die_info *src_die, struct attribute *attr,
12891 struct dwarf2_cu **ref_cu)
12892{
12893 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12894 struct dwarf2_cu *cu = *ref_cu;
12895 struct die_info *die;
12896
12897 die = follow_die_offset (offset, ref_cu);
12898 if (!die)
12899 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12900 "at 0x%x [in module %s]"),
12901 offset, src_die->offset, cu->objfile->name);
348e048f 12902
5c631832
JK
12903 return die;
12904}
12905
12906/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12907 value is intended for DW_OP_call*. */
12908
12909struct dwarf2_locexpr_baton
12910dwarf2_fetch_die_location_block (unsigned int offset,
12911 struct dwarf2_per_cu_data *per_cu)
12912{
12913 struct dwarf2_cu *cu = per_cu->cu;
12914 struct die_info *die;
12915 struct attribute *attr;
12916 struct dwarf2_locexpr_baton retval;
12917
12918 die = follow_die_offset (offset, &cu);
12919 if (!die)
12920 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12921 offset, per_cu->cu->objfile->name);
12922
12923 attr = dwarf2_attr (die, DW_AT_location, cu);
12924 if (!attr)
12925 {
12926 /* DWARF: "If there is no such attribute, then there is no effect.". */
12927
12928 retval.data = NULL;
12929 retval.size = 0;
12930 }
12931 else
12932 {
12933 if (!attr_form_is_block (attr))
12934 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12935 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12936 offset, per_cu->cu->objfile->name);
12937
12938 retval.data = DW_BLOCK (attr)->data;
12939 retval.size = DW_BLOCK (attr)->size;
12940 }
12941 retval.per_cu = cu->per_cu;
12942 return retval;
348e048f
DE
12943}
12944
12945/* Follow the signature attribute ATTR in SRC_DIE.
12946 On entry *REF_CU is the CU of SRC_DIE.
12947 On exit *REF_CU is the CU of the result. */
12948
12949static struct die_info *
12950follow_die_sig (struct die_info *src_die, struct attribute *attr,
12951 struct dwarf2_cu **ref_cu)
12952{
12953 struct objfile *objfile = (*ref_cu)->objfile;
12954 struct die_info temp_die;
12955 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12956 struct dwarf2_cu *sig_cu;
12957 struct die_info *die;
12958
12959 /* sig_type will be NULL if the signatured type is missing from
12960 the debug info. */
12961 if (sig_type == NULL)
12962 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12963 "at 0x%x [in module %s]"),
12964 src_die->offset, objfile->name);
12965
12966 /* If necessary, add it to the queue and load its DIEs. */
12967
12968 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12969 read_signatured_type (objfile, sig_type);
12970
12971 gdb_assert (sig_type->per_cu.cu != NULL);
12972
12973 sig_cu = sig_type->per_cu.cu;
12974 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12975 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12976 if (die)
12977 {
12978 *ref_cu = sig_cu;
12979 return die;
12980 }
12981
12982 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12983 "at 0x%x [in module %s]"),
12984 sig_type->type_offset, src_die->offset, objfile->name);
12985}
12986
12987/* Given an offset of a signatured type, return its signatured_type. */
12988
12989static struct signatured_type *
12990lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12991{
12992 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12993 unsigned int length, initial_length_size;
12994 unsigned int sig_offset;
12995 struct signatured_type find_entry, *type_sig;
12996
12997 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12998 sig_offset = (initial_length_size
12999 + 2 /*version*/
13000 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13001 + 1 /*address_size*/);
13002 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13003 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13004
13005 /* This is only used to lookup previously recorded types.
13006 If we didn't find it, it's our bug. */
13007 gdb_assert (type_sig != NULL);
13008 gdb_assert (offset == type_sig->offset);
13009
13010 return type_sig;
13011}
13012
13013/* Read in signatured type at OFFSET and build its CU and die(s). */
13014
13015static void
13016read_signatured_type_at_offset (struct objfile *objfile,
13017 unsigned int offset)
13018{
13019 struct signatured_type *type_sig;
13020
be391dca
TT
13021 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13022
348e048f
DE
13023 /* We have the section offset, but we need the signature to do the
13024 hash table lookup. */
13025 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13026
13027 gdb_assert (type_sig->per_cu.cu == NULL);
13028
13029 read_signatured_type (objfile, type_sig);
13030
13031 gdb_assert (type_sig->per_cu.cu != NULL);
13032}
13033
13034/* Read in a signatured type and build its CU and DIEs. */
13035
13036static void
13037read_signatured_type (struct objfile *objfile,
13038 struct signatured_type *type_sig)
13039{
1fd400ff 13040 gdb_byte *types_ptr;
348e048f
DE
13041 struct die_reader_specs reader_specs;
13042 struct dwarf2_cu *cu;
13043 ULONGEST signature;
13044 struct cleanup *back_to, *free_cu_cleanup;
13045 struct attribute *attr;
13046
1fd400ff
TT
13047 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13048 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13049
348e048f
DE
13050 gdb_assert (type_sig->per_cu.cu == NULL);
13051
13052 cu = xmalloc (sizeof (struct dwarf2_cu));
13053 memset (cu, 0, sizeof (struct dwarf2_cu));
13054 obstack_init (&cu->comp_unit_obstack);
13055 cu->objfile = objfile;
13056 type_sig->per_cu.cu = cu;
13057 cu->per_cu = &type_sig->per_cu;
13058
13059 /* If an error occurs while loading, release our storage. */
13060 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13061
13062 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13063 types_ptr, objfile->obfd);
13064 gdb_assert (signature == type_sig->signature);
13065
13066 cu->die_hash
13067 = htab_create_alloc_ex (cu->header.length / 12,
13068 die_hash,
13069 die_eq,
13070 NULL,
13071 &cu->comp_unit_obstack,
13072 hashtab_obstack_allocate,
13073 dummy_obstack_deallocate);
13074
13075 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13076 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13077
13078 init_cu_die_reader (&reader_specs, cu);
13079
13080 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13081 NULL /*parent*/);
13082
13083 /* We try not to read any attributes in this function, because not
13084 all objfiles needed for references have been loaded yet, and symbol
13085 table processing isn't initialized. But we have to set the CU language,
13086 or we won't be able to build types correctly. */
13087 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13088 if (attr)
13089 set_cu_language (DW_UNSND (attr), cu);
13090 else
13091 set_cu_language (language_minimal, cu);
13092
13093 do_cleanups (back_to);
13094
13095 /* We've successfully allocated this compilation unit. Let our caller
13096 clean it up when finished with it. */
13097 discard_cleanups (free_cu_cleanup);
13098
13099 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13100 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13101}
13102
c906108c
SS
13103/* Decode simple location descriptions.
13104 Given a pointer to a dwarf block that defines a location, compute
13105 the location and return the value.
13106
4cecd739
DJ
13107 NOTE drow/2003-11-18: This function is called in two situations
13108 now: for the address of static or global variables (partial symbols
13109 only) and for offsets into structures which are expected to be
13110 (more or less) constant. The partial symbol case should go away,
13111 and only the constant case should remain. That will let this
13112 function complain more accurately. A few special modes are allowed
13113 without complaint for global variables (for instance, global
13114 register values and thread-local values).
c906108c
SS
13115
13116 A location description containing no operations indicates that the
4cecd739 13117 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13118 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13119 callers will only want a very basic result and this can become a
13120 complaint.
c906108c 13121
c906108c
SS
13122 Note that stack[0] is unused except as a default error return.
13123 Note that stack overflow is not yet handled. */
13124
13125static CORE_ADDR
e7c27a73 13126decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13127{
e7c27a73 13128 struct objfile *objfile = cu->objfile;
c906108c
SS
13129 int i;
13130 int size = blk->size;
fe1b8b76 13131 gdb_byte *data = blk->data;
c906108c
SS
13132 CORE_ADDR stack[64];
13133 int stacki;
13134 unsigned int bytes_read, unsnd;
fe1b8b76 13135 gdb_byte op;
c906108c
SS
13136
13137 i = 0;
13138 stacki = 0;
13139 stack[stacki] = 0;
c906108c
SS
13140
13141 while (i < size)
13142 {
c906108c
SS
13143 op = data[i++];
13144 switch (op)
13145 {
f1bea926
JM
13146 case DW_OP_lit0:
13147 case DW_OP_lit1:
13148 case DW_OP_lit2:
13149 case DW_OP_lit3:
13150 case DW_OP_lit4:
13151 case DW_OP_lit5:
13152 case DW_OP_lit6:
13153 case DW_OP_lit7:
13154 case DW_OP_lit8:
13155 case DW_OP_lit9:
13156 case DW_OP_lit10:
13157 case DW_OP_lit11:
13158 case DW_OP_lit12:
13159 case DW_OP_lit13:
13160 case DW_OP_lit14:
13161 case DW_OP_lit15:
13162 case DW_OP_lit16:
13163 case DW_OP_lit17:
13164 case DW_OP_lit18:
13165 case DW_OP_lit19:
13166 case DW_OP_lit20:
13167 case DW_OP_lit21:
13168 case DW_OP_lit22:
13169 case DW_OP_lit23:
13170 case DW_OP_lit24:
13171 case DW_OP_lit25:
13172 case DW_OP_lit26:
13173 case DW_OP_lit27:
13174 case DW_OP_lit28:
13175 case DW_OP_lit29:
13176 case DW_OP_lit30:
13177 case DW_OP_lit31:
13178 stack[++stacki] = op - DW_OP_lit0;
13179 break;
13180
c906108c
SS
13181 case DW_OP_reg0:
13182 case DW_OP_reg1:
13183 case DW_OP_reg2:
13184 case DW_OP_reg3:
13185 case DW_OP_reg4:
13186 case DW_OP_reg5:
13187 case DW_OP_reg6:
13188 case DW_OP_reg7:
13189 case DW_OP_reg8:
13190 case DW_OP_reg9:
13191 case DW_OP_reg10:
13192 case DW_OP_reg11:
13193 case DW_OP_reg12:
13194 case DW_OP_reg13:
13195 case DW_OP_reg14:
13196 case DW_OP_reg15:
13197 case DW_OP_reg16:
13198 case DW_OP_reg17:
13199 case DW_OP_reg18:
13200 case DW_OP_reg19:
13201 case DW_OP_reg20:
13202 case DW_OP_reg21:
13203 case DW_OP_reg22:
13204 case DW_OP_reg23:
13205 case DW_OP_reg24:
13206 case DW_OP_reg25:
13207 case DW_OP_reg26:
13208 case DW_OP_reg27:
13209 case DW_OP_reg28:
13210 case DW_OP_reg29:
13211 case DW_OP_reg30:
13212 case DW_OP_reg31:
c906108c 13213 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13214 if (i < size)
13215 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13216 break;
13217
13218 case DW_OP_regx:
c906108c
SS
13219 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13220 i += bytes_read;
c906108c 13221 stack[++stacki] = unsnd;
4cecd739
DJ
13222 if (i < size)
13223 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13224 break;
13225
13226 case DW_OP_addr:
107d2387 13227 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13228 cu, &bytes_read);
107d2387 13229 i += bytes_read;
c906108c
SS
13230 break;
13231
13232 case DW_OP_const1u:
13233 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13234 i += 1;
13235 break;
13236
13237 case DW_OP_const1s:
13238 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13239 i += 1;
13240 break;
13241
13242 case DW_OP_const2u:
13243 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13244 i += 2;
13245 break;
13246
13247 case DW_OP_const2s:
13248 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13249 i += 2;
13250 break;
13251
13252 case DW_OP_const4u:
13253 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13254 i += 4;
13255 break;
13256
13257 case DW_OP_const4s:
13258 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13259 i += 4;
13260 break;
13261
13262 case DW_OP_constu:
13263 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13264 &bytes_read);
c906108c
SS
13265 i += bytes_read;
13266 break;
13267
13268 case DW_OP_consts:
13269 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13270 i += bytes_read;
13271 break;
13272
f1bea926
JM
13273 case DW_OP_dup:
13274 stack[stacki + 1] = stack[stacki];
13275 stacki++;
13276 break;
13277
c906108c
SS
13278 case DW_OP_plus:
13279 stack[stacki - 1] += stack[stacki];
13280 stacki--;
13281 break;
13282
13283 case DW_OP_plus_uconst:
13284 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13285 i += bytes_read;
13286 break;
13287
13288 case DW_OP_minus:
f1bea926 13289 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13290 stacki--;
13291 break;
13292
7a292a7a 13293 case DW_OP_deref:
7a292a7a 13294 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13295 this using GDB's address_class enum. This is valid for partial
13296 global symbols, although the variable's address will be bogus
13297 in the psymtab. */
7a292a7a 13298 if (i < size)
4d3c2250 13299 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13300 break;
13301
9d774e44 13302 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13303 /* The top of the stack has the offset from the beginning
13304 of the thread control block at which the variable is located. */
13305 /* Nothing should follow this operator, so the top of stack would
13306 be returned. */
4cecd739
DJ
13307 /* This is valid for partial global symbols, but the variable's
13308 address will be bogus in the psymtab. */
9d774e44 13309 if (i < size)
4d3c2250 13310 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13311 break;
13312
42be36b3
CT
13313 case DW_OP_GNU_uninit:
13314 break;
13315
c906108c 13316 default:
e2e0b3e5 13317 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13318 dwarf_stack_op_name (op, 1));
c906108c
SS
13319 return (stack[stacki]);
13320 }
13321 }
13322 return (stack[stacki]);
13323}
13324
13325/* memory allocation interface */
13326
c906108c 13327static struct dwarf_block *
7b5a2f43 13328dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13329{
13330 struct dwarf_block *blk;
13331
13332 blk = (struct dwarf_block *)
7b5a2f43 13333 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13334 return (blk);
13335}
13336
13337static struct abbrev_info *
f3dd6933 13338dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13339{
13340 struct abbrev_info *abbrev;
13341
f3dd6933
DJ
13342 abbrev = (struct abbrev_info *)
13343 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13344 memset (abbrev, 0, sizeof (struct abbrev_info));
13345 return (abbrev);
13346}
13347
13348static struct die_info *
b60c80d6 13349dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13350{
13351 struct die_info *die;
b60c80d6
DJ
13352 size_t size = sizeof (struct die_info);
13353
13354 if (num_attrs > 1)
13355 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13356
b60c80d6 13357 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13358 memset (die, 0, sizeof (struct die_info));
13359 return (die);
13360}
2e276125
JB
13361
13362\f
13363/* Macro support. */
13364
13365
13366/* Return the full name of file number I in *LH's file name table.
13367 Use COMP_DIR as the name of the current directory of the
13368 compilation. The result is allocated using xmalloc; the caller is
13369 responsible for freeing it. */
13370static char *
13371file_full_name (int file, struct line_header *lh, const char *comp_dir)
13372{
6a83a1e6
EZ
13373 /* Is the file number a valid index into the line header's file name
13374 table? Remember that file numbers start with one, not zero. */
13375 if (1 <= file && file <= lh->num_file_names)
13376 {
13377 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13378
6a83a1e6
EZ
13379 if (IS_ABSOLUTE_PATH (fe->name))
13380 return xstrdup (fe->name);
13381 else
13382 {
13383 const char *dir;
13384 int dir_len;
13385 char *full_name;
13386
13387 if (fe->dir_index)
13388 dir = lh->include_dirs[fe->dir_index - 1];
13389 else
13390 dir = comp_dir;
13391
13392 if (dir)
13393 {
13394 dir_len = strlen (dir);
13395 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13396 strcpy (full_name, dir);
13397 full_name[dir_len] = '/';
13398 strcpy (full_name + dir_len + 1, fe->name);
13399 return full_name;
13400 }
13401 else
13402 return xstrdup (fe->name);
13403 }
13404 }
2e276125
JB
13405 else
13406 {
6a83a1e6
EZ
13407 /* The compiler produced a bogus file number. We can at least
13408 record the macro definitions made in the file, even if we
13409 won't be able to find the file by name. */
13410 char fake_name[80];
9a619af0 13411
6a83a1e6 13412 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13413
6e70227d 13414 complaint (&symfile_complaints,
6a83a1e6
EZ
13415 _("bad file number in macro information (%d)"),
13416 file);
2e276125 13417
6a83a1e6 13418 return xstrdup (fake_name);
2e276125
JB
13419 }
13420}
13421
13422
13423static struct macro_source_file *
13424macro_start_file (int file, int line,
13425 struct macro_source_file *current_file,
13426 const char *comp_dir,
13427 struct line_header *lh, struct objfile *objfile)
13428{
13429 /* The full name of this source file. */
13430 char *full_name = file_full_name (file, lh, comp_dir);
13431
13432 /* We don't create a macro table for this compilation unit
13433 at all until we actually get a filename. */
13434 if (! pending_macros)
4a146b47 13435 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13436 objfile->macro_cache);
2e276125
JB
13437
13438 if (! current_file)
13439 /* If we have no current file, then this must be the start_file
13440 directive for the compilation unit's main source file. */
13441 current_file = macro_set_main (pending_macros, full_name);
13442 else
13443 current_file = macro_include (current_file, line, full_name);
13444
13445 xfree (full_name);
6e70227d 13446
2e276125
JB
13447 return current_file;
13448}
13449
13450
13451/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13452 followed by a null byte. */
13453static char *
13454copy_string (const char *buf, int len)
13455{
13456 char *s = xmalloc (len + 1);
9a619af0 13457
2e276125
JB
13458 memcpy (s, buf, len);
13459 s[len] = '\0';
2e276125
JB
13460 return s;
13461}
13462
13463
13464static const char *
13465consume_improper_spaces (const char *p, const char *body)
13466{
13467 if (*p == ' ')
13468 {
4d3c2250 13469 complaint (&symfile_complaints,
e2e0b3e5 13470 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13471 body);
2e276125
JB
13472
13473 while (*p == ' ')
13474 p++;
13475 }
13476
13477 return p;
13478}
13479
13480
13481static void
13482parse_macro_definition (struct macro_source_file *file, int line,
13483 const char *body)
13484{
13485 const char *p;
13486
13487 /* The body string takes one of two forms. For object-like macro
13488 definitions, it should be:
13489
13490 <macro name> " " <definition>
13491
13492 For function-like macro definitions, it should be:
13493
13494 <macro name> "() " <definition>
13495 or
13496 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13497
13498 Spaces may appear only where explicitly indicated, and in the
13499 <definition>.
13500
13501 The Dwarf 2 spec says that an object-like macro's name is always
13502 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13503 the space when the macro's definition is the empty string.
2e276125
JB
13504
13505 The Dwarf 2 spec says that there should be no spaces between the
13506 formal arguments in a function-like macro's formal argument list,
13507 but versions of GCC around March 2002 include spaces after the
13508 commas. */
13509
13510
13511 /* Find the extent of the macro name. The macro name is terminated
13512 by either a space or null character (for an object-like macro) or
13513 an opening paren (for a function-like macro). */
13514 for (p = body; *p; p++)
13515 if (*p == ' ' || *p == '(')
13516 break;
13517
13518 if (*p == ' ' || *p == '\0')
13519 {
13520 /* It's an object-like macro. */
13521 int name_len = p - body;
13522 char *name = copy_string (body, name_len);
13523 const char *replacement;
13524
13525 if (*p == ' ')
13526 replacement = body + name_len + 1;
13527 else
13528 {
4d3c2250 13529 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13530 replacement = body + name_len;
13531 }
6e70227d 13532
2e276125
JB
13533 macro_define_object (file, line, name, replacement);
13534
13535 xfree (name);
13536 }
13537 else if (*p == '(')
13538 {
13539 /* It's a function-like macro. */
13540 char *name = copy_string (body, p - body);
13541 int argc = 0;
13542 int argv_size = 1;
13543 char **argv = xmalloc (argv_size * sizeof (*argv));
13544
13545 p++;
13546
13547 p = consume_improper_spaces (p, body);
13548
13549 /* Parse the formal argument list. */
13550 while (*p && *p != ')')
13551 {
13552 /* Find the extent of the current argument name. */
13553 const char *arg_start = p;
13554
13555 while (*p && *p != ',' && *p != ')' && *p != ' ')
13556 p++;
13557
13558 if (! *p || p == arg_start)
4d3c2250 13559 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13560 else
13561 {
13562 /* Make sure argv has room for the new argument. */
13563 if (argc >= argv_size)
13564 {
13565 argv_size *= 2;
13566 argv = xrealloc (argv, argv_size * sizeof (*argv));
13567 }
13568
13569 argv[argc++] = copy_string (arg_start, p - arg_start);
13570 }
13571
13572 p = consume_improper_spaces (p, body);
13573
13574 /* Consume the comma, if present. */
13575 if (*p == ',')
13576 {
13577 p++;
13578
13579 p = consume_improper_spaces (p, body);
13580 }
13581 }
13582
13583 if (*p == ')')
13584 {
13585 p++;
13586
13587 if (*p == ' ')
13588 /* Perfectly formed definition, no complaints. */
13589 macro_define_function (file, line, name,
6e70227d 13590 argc, (const char **) argv,
2e276125
JB
13591 p + 1);
13592 else if (*p == '\0')
13593 {
13594 /* Complain, but do define it. */
4d3c2250 13595 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13596 macro_define_function (file, line, name,
6e70227d 13597 argc, (const char **) argv,
2e276125
JB
13598 p);
13599 }
13600 else
13601 /* Just complain. */
4d3c2250 13602 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13603 }
13604 else
13605 /* Just complain. */
4d3c2250 13606 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13607
13608 xfree (name);
13609 {
13610 int i;
13611
13612 for (i = 0; i < argc; i++)
13613 xfree (argv[i]);
13614 }
13615 xfree (argv);
13616 }
13617 else
4d3c2250 13618 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13619}
13620
13621
13622static void
13623dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13624 char *comp_dir, bfd *abfd,
e7c27a73 13625 struct dwarf2_cu *cu)
2e276125 13626{
fe1b8b76 13627 gdb_byte *mac_ptr, *mac_end;
2e276125 13628 struct macro_source_file *current_file = 0;
757a13d0
JK
13629 enum dwarf_macinfo_record_type macinfo_type;
13630 int at_commandline;
2e276125 13631
be391dca
TT
13632 dwarf2_read_section (dwarf2_per_objfile->objfile,
13633 &dwarf2_per_objfile->macinfo);
dce234bc 13634 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13635 {
e2e0b3e5 13636 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13637 return;
13638 }
13639
757a13d0
JK
13640 /* First pass: Find the name of the base filename.
13641 This filename is needed in order to process all macros whose definition
13642 (or undefinition) comes from the command line. These macros are defined
13643 before the first DW_MACINFO_start_file entry, and yet still need to be
13644 associated to the base file.
13645
13646 To determine the base file name, we scan the macro definitions until we
13647 reach the first DW_MACINFO_start_file entry. We then initialize
13648 CURRENT_FILE accordingly so that any macro definition found before the
13649 first DW_MACINFO_start_file can still be associated to the base file. */
13650
dce234bc
PP
13651 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13652 mac_end = dwarf2_per_objfile->macinfo.buffer
13653 + dwarf2_per_objfile->macinfo.size;
2e276125 13654
757a13d0 13655 do
2e276125 13656 {
2e276125
JB
13657 /* Do we at least have room for a macinfo type byte? */
13658 if (mac_ptr >= mac_end)
13659 {
757a13d0
JK
13660 /* Complaint is printed during the second pass as GDB will probably
13661 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13662 break;
2e276125
JB
13663 }
13664
13665 macinfo_type = read_1_byte (abfd, mac_ptr);
13666 mac_ptr++;
13667
13668 switch (macinfo_type)
13669 {
13670 /* A zero macinfo type indicates the end of the macro
13671 information. */
13672 case 0:
757a13d0
JK
13673 break;
13674
13675 case DW_MACINFO_define:
13676 case DW_MACINFO_undef:
13677 /* Only skip the data by MAC_PTR. */
13678 {
13679 unsigned int bytes_read;
13680
13681 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13682 mac_ptr += bytes_read;
9b1c24c8 13683 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13684 mac_ptr += bytes_read;
13685 }
13686 break;
13687
13688 case DW_MACINFO_start_file:
13689 {
13690 unsigned int bytes_read;
13691 int line, file;
13692
13693 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13694 mac_ptr += bytes_read;
13695 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13696 mac_ptr += bytes_read;
13697
13698 current_file = macro_start_file (file, line, current_file, comp_dir,
13699 lh, cu->objfile);
13700 }
13701 break;
13702
13703 case DW_MACINFO_end_file:
13704 /* No data to skip by MAC_PTR. */
13705 break;
13706
13707 case DW_MACINFO_vendor_ext:
13708 /* Only skip the data by MAC_PTR. */
13709 {
13710 unsigned int bytes_read;
13711
13712 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13713 mac_ptr += bytes_read;
9b1c24c8 13714 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13715 mac_ptr += bytes_read;
13716 }
13717 break;
13718
13719 default:
13720 break;
13721 }
13722 } while (macinfo_type != 0 && current_file == NULL);
13723
13724 /* Second pass: Process all entries.
13725
13726 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13727 command-line macro definitions/undefinitions. This flag is unset when we
13728 reach the first DW_MACINFO_start_file entry. */
13729
dce234bc 13730 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13731
13732 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13733 GDB is still reading the definitions from command line. First
13734 DW_MACINFO_start_file will need to be ignored as it was already executed
13735 to create CURRENT_FILE for the main source holding also the command line
13736 definitions. On first met DW_MACINFO_start_file this flag is reset to
13737 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13738
13739 at_commandline = 1;
13740
13741 do
13742 {
13743 /* Do we at least have room for a macinfo type byte? */
13744 if (mac_ptr >= mac_end)
13745 {
13746 dwarf2_macros_too_long_complaint ();
13747 break;
13748 }
13749
13750 macinfo_type = read_1_byte (abfd, mac_ptr);
13751 mac_ptr++;
13752
13753 switch (macinfo_type)
13754 {
13755 /* A zero macinfo type indicates the end of the macro
13756 information. */
13757 case 0:
13758 break;
2e276125
JB
13759
13760 case DW_MACINFO_define:
13761 case DW_MACINFO_undef:
13762 {
891d2f0b 13763 unsigned int bytes_read;
2e276125
JB
13764 int line;
13765 char *body;
13766
13767 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13768 mac_ptr += bytes_read;
9b1c24c8 13769 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13770 mac_ptr += bytes_read;
13771
13772 if (! current_file)
757a13d0
JK
13773 {
13774 /* DWARF violation as no main source is present. */
13775 complaint (&symfile_complaints,
13776 _("debug info with no main source gives macro %s "
13777 "on line %d: %s"),
6e70227d
DE
13778 macinfo_type == DW_MACINFO_define ?
13779 _("definition") :
905e0470
PM
13780 macinfo_type == DW_MACINFO_undef ?
13781 _("undefinition") :
13782 _("something-or-other"), line, body);
757a13d0
JK
13783 break;
13784 }
13785 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13786 complaint (&symfile_complaints,
757a13d0
JK
13787 _("debug info gives %s macro %s with %s line %d: %s"),
13788 at_commandline ? _("command-line") : _("in-file"),
905e0470 13789 macinfo_type == DW_MACINFO_define ?
6e70227d 13790 _("definition") :
905e0470
PM
13791 macinfo_type == DW_MACINFO_undef ?
13792 _("undefinition") :
13793 _("something-or-other"),
757a13d0
JK
13794 line == 0 ? _("zero") : _("non-zero"), line, body);
13795
13796 if (macinfo_type == DW_MACINFO_define)
13797 parse_macro_definition (current_file, line, body);
13798 else if (macinfo_type == DW_MACINFO_undef)
13799 macro_undef (current_file, line, body);
2e276125
JB
13800 }
13801 break;
13802
13803 case DW_MACINFO_start_file:
13804 {
891d2f0b 13805 unsigned int bytes_read;
2e276125
JB
13806 int line, file;
13807
13808 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13809 mac_ptr += bytes_read;
13810 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13811 mac_ptr += bytes_read;
13812
757a13d0
JK
13813 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13814 complaint (&symfile_complaints,
13815 _("debug info gives source %d included "
13816 "from %s at %s line %d"),
13817 file, at_commandline ? _("command-line") : _("file"),
13818 line == 0 ? _("zero") : _("non-zero"), line);
13819
13820 if (at_commandline)
13821 {
13822 /* This DW_MACINFO_start_file was executed in the pass one. */
13823 at_commandline = 0;
13824 }
13825 else
13826 current_file = macro_start_file (file, line,
13827 current_file, comp_dir,
13828 lh, cu->objfile);
2e276125
JB
13829 }
13830 break;
13831
13832 case DW_MACINFO_end_file:
13833 if (! current_file)
4d3c2250 13834 complaint (&symfile_complaints,
e2e0b3e5 13835 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13836 else
13837 {
13838 current_file = current_file->included_by;
13839 if (! current_file)
13840 {
13841 enum dwarf_macinfo_record_type next_type;
13842
13843 /* GCC circa March 2002 doesn't produce the zero
13844 type byte marking the end of the compilation
13845 unit. Complain if it's not there, but exit no
13846 matter what. */
13847
13848 /* Do we at least have room for a macinfo type byte? */
13849 if (mac_ptr >= mac_end)
13850 {
4d3c2250 13851 dwarf2_macros_too_long_complaint ();
2e276125
JB
13852 return;
13853 }
13854
13855 /* We don't increment mac_ptr here, so this is just
13856 a look-ahead. */
13857 next_type = read_1_byte (abfd, mac_ptr);
13858 if (next_type != 0)
4d3c2250 13859 complaint (&symfile_complaints,
e2e0b3e5 13860 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13861
13862 return;
13863 }
13864 }
13865 break;
13866
13867 case DW_MACINFO_vendor_ext:
13868 {
891d2f0b 13869 unsigned int bytes_read;
2e276125
JB
13870 int constant;
13871 char *string;
13872
13873 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13874 mac_ptr += bytes_read;
9b1c24c8 13875 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13876 mac_ptr += bytes_read;
13877
13878 /* We don't recognize any vendor extensions. */
13879 }
13880 break;
13881 }
757a13d0 13882 } while (macinfo_type != 0);
2e276125 13883}
8e19ed76
PS
13884
13885/* Check if the attribute's form is a DW_FORM_block*
13886 if so return true else false. */
13887static int
13888attr_form_is_block (struct attribute *attr)
13889{
13890 return (attr == NULL ? 0 :
13891 attr->form == DW_FORM_block1
13892 || attr->form == DW_FORM_block2
13893 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13894 || attr->form == DW_FORM_block
13895 || attr->form == DW_FORM_exprloc);
8e19ed76 13896}
4c2df51b 13897
c6a0999f
JB
13898/* Return non-zero if ATTR's value is a section offset --- classes
13899 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13900 You may use DW_UNSND (attr) to retrieve such offsets.
13901
13902 Section 7.5.4, "Attribute Encodings", explains that no attribute
13903 may have a value that belongs to more than one of these classes; it
13904 would be ambiguous if we did, because we use the same forms for all
13905 of them. */
3690dd37
JB
13906static int
13907attr_form_is_section_offset (struct attribute *attr)
13908{
13909 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13910 || attr->form == DW_FORM_data8
13911 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13912}
13913
13914
13915/* Return non-zero if ATTR's value falls in the 'constant' class, or
13916 zero otherwise. When this function returns true, you can apply
13917 dwarf2_get_attr_constant_value to it.
13918
13919 However, note that for some attributes you must check
13920 attr_form_is_section_offset before using this test. DW_FORM_data4
13921 and DW_FORM_data8 are members of both the constant class, and of
13922 the classes that contain offsets into other debug sections
13923 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13924 that, if an attribute's can be either a constant or one of the
13925 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13926 taken as section offsets, not constants. */
13927static int
13928attr_form_is_constant (struct attribute *attr)
13929{
13930 switch (attr->form)
13931 {
13932 case DW_FORM_sdata:
13933 case DW_FORM_udata:
13934 case DW_FORM_data1:
13935 case DW_FORM_data2:
13936 case DW_FORM_data4:
13937 case DW_FORM_data8:
13938 return 1;
13939 default:
13940 return 0;
13941 }
13942}
13943
4c2df51b
DJ
13944static void
13945dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13946 struct dwarf2_cu *cu)
4c2df51b 13947{
3690dd37 13948 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13949 /* ".debug_loc" may not exist at all, or the offset may be outside
13950 the section. If so, fall through to the complaint in the
13951 other branch. */
dce234bc 13952 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13953 {
0d53c4c4 13954 struct dwarf2_loclist_baton *baton;
4c2df51b 13955
4a146b47 13956 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13957 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13958 baton->per_cu = cu->per_cu;
13959 gdb_assert (baton->per_cu);
4c2df51b 13960
be391dca
TT
13961 dwarf2_read_section (dwarf2_per_objfile->objfile,
13962 &dwarf2_per_objfile->loc);
13963
0d53c4c4
DJ
13964 /* We don't know how long the location list is, but make sure we
13965 don't run off the edge of the section. */
dce234bc
PP
13966 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13967 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13968 baton->base_address = cu->base_address;
13969 if (cu->base_known == 0)
0d53c4c4 13970 complaint (&symfile_complaints,
e2e0b3e5 13971 _("Location list used without specifying the CU base address."));
4c2df51b 13972
768a979c 13973 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13974 SYMBOL_LOCATION_BATON (sym) = baton;
13975 }
13976 else
13977 {
13978 struct dwarf2_locexpr_baton *baton;
13979
4a146b47 13980 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13981 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13982 baton->per_cu = cu->per_cu;
13983 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13984
13985 if (attr_form_is_block (attr))
13986 {
13987 /* Note that we're just copying the block's data pointer
13988 here, not the actual data. We're still pointing into the
6502dd73
DJ
13989 info_buffer for SYM's objfile; right now we never release
13990 that buffer, but when we do clean up properly this may
13991 need to change. */
0d53c4c4
DJ
13992 baton->size = DW_BLOCK (attr)->size;
13993 baton->data = DW_BLOCK (attr)->data;
13994 }
13995 else
13996 {
13997 dwarf2_invalid_attrib_class_complaint ("location description",
13998 SYMBOL_NATURAL_NAME (sym));
13999 baton->size = 0;
14000 baton->data = NULL;
14001 }
6e70227d 14002
768a979c 14003 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14004 SYMBOL_LOCATION_BATON (sym) = baton;
14005 }
4c2df51b 14006}
6502dd73 14007
9aa1f1e3
TT
14008/* Return the OBJFILE associated with the compilation unit CU. If CU
14009 came from a separate debuginfo file, then the master objfile is
14010 returned. */
ae0d2f24
UW
14011
14012struct objfile *
14013dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14014{
9291a0cd 14015 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14016
14017 /* Return the master objfile, so that we can report and look up the
14018 correct file containing this variable. */
14019 if (objfile->separate_debug_objfile_backlink)
14020 objfile = objfile->separate_debug_objfile_backlink;
14021
14022 return objfile;
14023}
14024
14025/* Return the address size given in the compilation unit header for CU. */
14026
14027CORE_ADDR
14028dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14029{
14030 if (per_cu->cu)
14031 return per_cu->cu->header.addr_size;
14032 else
14033 {
14034 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14035 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14036 struct dwarf2_per_objfile *per_objfile
14037 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14038 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14039 struct comp_unit_head cu_header;
9a619af0 14040
ae0d2f24
UW
14041 memset (&cu_header, 0, sizeof cu_header);
14042 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14043 return cu_header.addr_size;
14044 }
14045}
14046
9eae7c52
TT
14047/* Return the offset size given in the compilation unit header for CU. */
14048
14049int
14050dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14051{
14052 if (per_cu->cu)
14053 return per_cu->cu->header.offset_size;
14054 else
14055 {
14056 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14057 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14058 struct dwarf2_per_objfile *per_objfile
14059 = objfile_data (objfile, dwarf2_objfile_data_key);
14060 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14061 struct comp_unit_head cu_header;
14062
14063 memset (&cu_header, 0, sizeof cu_header);
14064 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14065 return cu_header.offset_size;
14066 }
14067}
14068
9aa1f1e3
TT
14069/* Return the text offset of the CU. The returned offset comes from
14070 this CU's objfile. If this objfile came from a separate debuginfo
14071 file, then the offset may be different from the corresponding
14072 offset in the parent objfile. */
14073
14074CORE_ADDR
14075dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14076{
bb3fa9d0 14077 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14078
14079 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14080}
14081
348e048f
DE
14082/* Locate the .debug_info compilation unit from CU's objfile which contains
14083 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14084
14085static struct dwarf2_per_cu_data *
c764a876 14086dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14087 struct objfile *objfile)
14088{
14089 struct dwarf2_per_cu_data *this_cu;
14090 int low, high;
14091
ae038cb0
DJ
14092 low = 0;
14093 high = dwarf2_per_objfile->n_comp_units - 1;
14094 while (high > low)
14095 {
14096 int mid = low + (high - low) / 2;
9a619af0 14097
ae038cb0
DJ
14098 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14099 high = mid;
14100 else
14101 low = mid + 1;
14102 }
14103 gdb_assert (low == high);
14104 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14105 {
10b3939b 14106 if (low == 0)
8a3fe4f8
AC
14107 error (_("Dwarf Error: could not find partial DIE containing "
14108 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14109 (long) offset, bfd_get_filename (objfile->obfd));
14110
ae038cb0
DJ
14111 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14112 return dwarf2_per_objfile->all_comp_units[low-1];
14113 }
14114 else
14115 {
14116 this_cu = dwarf2_per_objfile->all_comp_units[low];
14117 if (low == dwarf2_per_objfile->n_comp_units - 1
14118 && offset >= this_cu->offset + this_cu->length)
c764a876 14119 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14120 gdb_assert (offset < this_cu->offset + this_cu->length);
14121 return this_cu;
14122 }
14123}
14124
10b3939b
DJ
14125/* Locate the compilation unit from OBJFILE which is located at exactly
14126 OFFSET. Raises an error on failure. */
14127
ae038cb0 14128static struct dwarf2_per_cu_data *
c764a876 14129dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14130{
14131 struct dwarf2_per_cu_data *this_cu;
9a619af0 14132
ae038cb0
DJ
14133 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14134 if (this_cu->offset != offset)
c764a876 14135 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14136 return this_cu;
14137}
14138
93311388
DE
14139/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14140
14141static struct dwarf2_cu *
14142alloc_one_comp_unit (struct objfile *objfile)
14143{
14144 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14145 cu->objfile = objfile;
14146 obstack_init (&cu->comp_unit_obstack);
14147 return cu;
14148}
14149
ae038cb0
DJ
14150/* Release one cached compilation unit, CU. We unlink it from the tree
14151 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14152 the caller is responsible for that.
14153 NOTE: DATA is a void * because this function is also used as a
14154 cleanup routine. */
ae038cb0
DJ
14155
14156static void
14157free_one_comp_unit (void *data)
14158{
14159 struct dwarf2_cu *cu = data;
14160
14161 if (cu->per_cu != NULL)
14162 cu->per_cu->cu = NULL;
14163 cu->per_cu = NULL;
14164
14165 obstack_free (&cu->comp_unit_obstack, NULL);
14166
14167 xfree (cu);
14168}
14169
72bf9492 14170/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14171 when we're finished with it. We can't free the pointer itself, but be
14172 sure to unlink it from the cache. Also release any associated storage
14173 and perform cache maintenance.
72bf9492
DJ
14174
14175 Only used during partial symbol parsing. */
14176
14177static void
14178free_stack_comp_unit (void *data)
14179{
14180 struct dwarf2_cu *cu = data;
14181
14182 obstack_free (&cu->comp_unit_obstack, NULL);
14183 cu->partial_dies = NULL;
ae038cb0
DJ
14184
14185 if (cu->per_cu != NULL)
14186 {
14187 /* This compilation unit is on the stack in our caller, so we
14188 should not xfree it. Just unlink it. */
14189 cu->per_cu->cu = NULL;
14190 cu->per_cu = NULL;
14191
14192 /* If we had a per-cu pointer, then we may have other compilation
14193 units loaded, so age them now. */
14194 age_cached_comp_units ();
14195 }
14196}
14197
14198/* Free all cached compilation units. */
14199
14200static void
14201free_cached_comp_units (void *data)
14202{
14203 struct dwarf2_per_cu_data *per_cu, **last_chain;
14204
14205 per_cu = dwarf2_per_objfile->read_in_chain;
14206 last_chain = &dwarf2_per_objfile->read_in_chain;
14207 while (per_cu != NULL)
14208 {
14209 struct dwarf2_per_cu_data *next_cu;
14210
14211 next_cu = per_cu->cu->read_in_chain;
14212
14213 free_one_comp_unit (per_cu->cu);
14214 *last_chain = next_cu;
14215
14216 per_cu = next_cu;
14217 }
14218}
14219
14220/* Increase the age counter on each cached compilation unit, and free
14221 any that are too old. */
14222
14223static void
14224age_cached_comp_units (void)
14225{
14226 struct dwarf2_per_cu_data *per_cu, **last_chain;
14227
14228 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14229 per_cu = dwarf2_per_objfile->read_in_chain;
14230 while (per_cu != NULL)
14231 {
14232 per_cu->cu->last_used ++;
14233 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14234 dwarf2_mark (per_cu->cu);
14235 per_cu = per_cu->cu->read_in_chain;
14236 }
14237
14238 per_cu = dwarf2_per_objfile->read_in_chain;
14239 last_chain = &dwarf2_per_objfile->read_in_chain;
14240 while (per_cu != NULL)
14241 {
14242 struct dwarf2_per_cu_data *next_cu;
14243
14244 next_cu = per_cu->cu->read_in_chain;
14245
14246 if (!per_cu->cu->mark)
14247 {
14248 free_one_comp_unit (per_cu->cu);
14249 *last_chain = next_cu;
14250 }
14251 else
14252 last_chain = &per_cu->cu->read_in_chain;
14253
14254 per_cu = next_cu;
14255 }
14256}
14257
14258/* Remove a single compilation unit from the cache. */
14259
14260static void
14261free_one_cached_comp_unit (void *target_cu)
14262{
14263 struct dwarf2_per_cu_data *per_cu, **last_chain;
14264
14265 per_cu = dwarf2_per_objfile->read_in_chain;
14266 last_chain = &dwarf2_per_objfile->read_in_chain;
14267 while (per_cu != NULL)
14268 {
14269 struct dwarf2_per_cu_data *next_cu;
14270
14271 next_cu = per_cu->cu->read_in_chain;
14272
14273 if (per_cu->cu == target_cu)
14274 {
14275 free_one_comp_unit (per_cu->cu);
14276 *last_chain = next_cu;
14277 break;
14278 }
14279 else
14280 last_chain = &per_cu->cu->read_in_chain;
14281
14282 per_cu = next_cu;
14283 }
14284}
14285
fe3e1990
DJ
14286/* Release all extra memory associated with OBJFILE. */
14287
14288void
14289dwarf2_free_objfile (struct objfile *objfile)
14290{
14291 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14292
14293 if (dwarf2_per_objfile == NULL)
14294 return;
14295
14296 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14297 free_cached_comp_units (NULL);
14298
9291a0cd
TT
14299 if (dwarf2_per_objfile->using_index)
14300 {
14301 int i;
14302
14303 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14304 {
14305 int j;
e254ef6a
DE
14306 struct dwarf2_per_cu_data *per_cu =
14307 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14308
e254ef6a 14309 if (!per_cu->v.quick->lines)
9291a0cd
TT
14310 continue;
14311
e254ef6a 14312 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14313 {
e254ef6a
DE
14314 if (per_cu->v.quick->file_names)
14315 xfree ((void *) per_cu->v.quick->file_names[j]);
14316 if (per_cu->v.quick->full_names)
14317 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14318 }
14319
e254ef6a 14320 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14321 }
14322 }
14323
fe3e1990
DJ
14324 /* Everything else should be on the objfile obstack. */
14325}
14326
1c379e20
DJ
14327/* A pair of DIE offset and GDB type pointer. We store these
14328 in a hash table separate from the DIEs, and preserve them
14329 when the DIEs are flushed out of cache. */
14330
14331struct dwarf2_offset_and_type
14332{
14333 unsigned int offset;
14334 struct type *type;
14335};
14336
14337/* Hash function for a dwarf2_offset_and_type. */
14338
14339static hashval_t
14340offset_and_type_hash (const void *item)
14341{
14342 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14343
1c379e20
DJ
14344 return ofs->offset;
14345}
14346
14347/* Equality function for a dwarf2_offset_and_type. */
14348
14349static int
14350offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14351{
14352 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14353 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14354
1c379e20
DJ
14355 return ofs_lhs->offset == ofs_rhs->offset;
14356}
14357
14358/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14359 table if necessary. For convenience, return TYPE.
14360
14361 The DIEs reading must have careful ordering to:
14362 * Not cause infite loops trying to read in DIEs as a prerequisite for
14363 reading current DIE.
14364 * Not trying to dereference contents of still incompletely read in types
14365 while reading in other DIEs.
14366 * Enable referencing still incompletely read in types just by a pointer to
14367 the type without accessing its fields.
14368
14369 Therefore caller should follow these rules:
14370 * Try to fetch any prerequisite types we may need to build this DIE type
14371 before building the type and calling set_die_type.
e71ec853 14372 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14373 possible before fetching more types to complete the current type.
14374 * Make the type as complete as possible before fetching more types. */
1c379e20 14375
f792889a 14376static struct type *
1c379e20
DJ
14377set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14378{
14379 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14380 struct objfile *objfile = cu->objfile;
14381 htab_t *type_hash_ptr;
1c379e20 14382
b4ba55a1
JB
14383 /* For Ada types, make sure that the gnat-specific data is always
14384 initialized (if not already set). There are a few types where
14385 we should not be doing so, because the type-specific area is
14386 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14387 where the type-specific area is used to store the floatformat).
14388 But this is not a problem, because the gnat-specific information
14389 is actually not needed for these types. */
14390 if (need_gnat_info (cu)
14391 && TYPE_CODE (type) != TYPE_CODE_FUNC
14392 && TYPE_CODE (type) != TYPE_CODE_FLT
14393 && !HAVE_GNAT_AUX_INFO (type))
14394 INIT_GNAT_SPECIFIC (type);
14395
673bfd45
DE
14396 if (cu->per_cu->from_debug_types)
14397 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14398 else
14399 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14400
14401 if (*type_hash_ptr == NULL)
f792889a 14402 {
673bfd45
DE
14403 *type_hash_ptr
14404 = htab_create_alloc_ex (127,
f792889a
DJ
14405 offset_and_type_hash,
14406 offset_and_type_eq,
14407 NULL,
673bfd45 14408 &objfile->objfile_obstack,
f792889a
DJ
14409 hashtab_obstack_allocate,
14410 dummy_obstack_deallocate);
f792889a 14411 }
1c379e20
DJ
14412
14413 ofs.offset = die->offset;
14414 ofs.type = type;
14415 slot = (struct dwarf2_offset_and_type **)
673bfd45 14416 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14417 if (*slot)
14418 complaint (&symfile_complaints,
14419 _("A problem internal to GDB: DIE 0x%x has type already set"),
14420 die->offset);
673bfd45 14421 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14422 **slot = ofs;
f792889a 14423 return type;
1c379e20
DJ
14424}
14425
673bfd45
DE
14426/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14427 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14428
14429static struct type *
673bfd45
DE
14430get_die_type_at_offset (unsigned int offset,
14431 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14432{
14433 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14434 htab_t type_hash;
f792889a 14435
673bfd45
DE
14436 if (per_cu->from_debug_types)
14437 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14438 else
14439 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14440 if (type_hash == NULL)
14441 return NULL;
1c379e20 14442
673bfd45 14443 ofs.offset = offset;
1c379e20
DJ
14444 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14445 if (slot)
14446 return slot->type;
14447 else
14448 return NULL;
14449}
14450
673bfd45
DE
14451/* Look up the type for DIE in the appropriate type_hash table,
14452 or return NULL if DIE does not have a saved type. */
14453
14454static struct type *
14455get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14456{
14457 return get_die_type_at_offset (die->offset, cu->per_cu);
14458}
14459
10b3939b
DJ
14460/* Add a dependence relationship from CU to REF_PER_CU. */
14461
14462static void
14463dwarf2_add_dependence (struct dwarf2_cu *cu,
14464 struct dwarf2_per_cu_data *ref_per_cu)
14465{
14466 void **slot;
14467
14468 if (cu->dependencies == NULL)
14469 cu->dependencies
14470 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14471 NULL, &cu->comp_unit_obstack,
14472 hashtab_obstack_allocate,
14473 dummy_obstack_deallocate);
14474
14475 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14476 if (*slot == NULL)
14477 *slot = ref_per_cu;
14478}
1c379e20 14479
f504f079
DE
14480/* Subroutine of dwarf2_mark to pass to htab_traverse.
14481 Set the mark field in every compilation unit in the
ae038cb0
DJ
14482 cache that we must keep because we are keeping CU. */
14483
10b3939b
DJ
14484static int
14485dwarf2_mark_helper (void **slot, void *data)
14486{
14487 struct dwarf2_per_cu_data *per_cu;
14488
14489 per_cu = (struct dwarf2_per_cu_data *) *slot;
14490 if (per_cu->cu->mark)
14491 return 1;
14492 per_cu->cu->mark = 1;
14493
14494 if (per_cu->cu->dependencies != NULL)
14495 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14496
14497 return 1;
14498}
14499
f504f079
DE
14500/* Set the mark field in CU and in every other compilation unit in the
14501 cache that we must keep because we are keeping CU. */
14502
ae038cb0
DJ
14503static void
14504dwarf2_mark (struct dwarf2_cu *cu)
14505{
14506 if (cu->mark)
14507 return;
14508 cu->mark = 1;
10b3939b
DJ
14509 if (cu->dependencies != NULL)
14510 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14511}
14512
14513static void
14514dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14515{
14516 while (per_cu)
14517 {
14518 per_cu->cu->mark = 0;
14519 per_cu = per_cu->cu->read_in_chain;
14520 }
72bf9492
DJ
14521}
14522
72bf9492
DJ
14523/* Trivial hash function for partial_die_info: the hash value of a DIE
14524 is its offset in .debug_info for this objfile. */
14525
14526static hashval_t
14527partial_die_hash (const void *item)
14528{
14529 const struct partial_die_info *part_die = item;
9a619af0 14530
72bf9492
DJ
14531 return part_die->offset;
14532}
14533
14534/* Trivial comparison function for partial_die_info structures: two DIEs
14535 are equal if they have the same offset. */
14536
14537static int
14538partial_die_eq (const void *item_lhs, const void *item_rhs)
14539{
14540 const struct partial_die_info *part_die_lhs = item_lhs;
14541 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14542
72bf9492
DJ
14543 return part_die_lhs->offset == part_die_rhs->offset;
14544}
14545
ae038cb0
DJ
14546static struct cmd_list_element *set_dwarf2_cmdlist;
14547static struct cmd_list_element *show_dwarf2_cmdlist;
14548
14549static void
14550set_dwarf2_cmd (char *args, int from_tty)
14551{
14552 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14553}
14554
14555static void
14556show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14557{
ae038cb0
DJ
14558 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14559}
14560
dce234bc
PP
14561/* If section described by INFO was mmapped, munmap it now. */
14562
14563static void
14564munmap_section_buffer (struct dwarf2_section_info *info)
14565{
14566 if (info->was_mmapped)
14567 {
14568#ifdef HAVE_MMAP
14569 intptr_t begin = (intptr_t) info->buffer;
14570 intptr_t map_begin = begin & ~(pagesize - 1);
14571 size_t map_length = info->size + begin - map_begin;
9a619af0 14572
dce234bc
PP
14573 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14574#else
14575 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14576 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14577#endif
14578 }
14579}
14580
14581/* munmap debug sections for OBJFILE, if necessary. */
14582
14583static void
c1bd65d0 14584dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14585{
14586 struct dwarf2_per_objfile *data = d;
9a619af0 14587
16be1145
DE
14588 /* This is sorted according to the order they're defined in to make it easier
14589 to keep in sync. */
dce234bc
PP
14590 munmap_section_buffer (&data->info);
14591 munmap_section_buffer (&data->abbrev);
14592 munmap_section_buffer (&data->line);
16be1145 14593 munmap_section_buffer (&data->loc);
dce234bc 14594 munmap_section_buffer (&data->macinfo);
16be1145 14595 munmap_section_buffer (&data->str);
dce234bc 14596 munmap_section_buffer (&data->ranges);
16be1145 14597 munmap_section_buffer (&data->types);
dce234bc
PP
14598 munmap_section_buffer (&data->frame);
14599 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14600 munmap_section_buffer (&data->gdb_index);
14601}
14602
14603\f
14604
14605/* The contents of the hash table we create when building the string
14606 table. */
14607struct strtab_entry
14608{
14609 offset_type offset;
14610 const char *str;
14611};
14612
14613/* Hash function for a strtab_entry. */
14614static hashval_t
14615hash_strtab_entry (const void *e)
14616{
14617 const struct strtab_entry *entry = e;
14618 return mapped_index_string_hash (entry->str);
14619}
14620
14621/* Equality function for a strtab_entry. */
14622static int
14623eq_strtab_entry (const void *a, const void *b)
14624{
14625 const struct strtab_entry *ea = a;
14626 const struct strtab_entry *eb = b;
14627 return !strcmp (ea->str, eb->str);
14628}
14629
14630/* Create a strtab_entry hash table. */
14631static htab_t
14632create_strtab (void)
14633{
14634 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14635 xfree, xcalloc, xfree);
14636}
14637
14638/* Add a string to the constant pool. Return the string's offset in
14639 host order. */
14640static offset_type
14641add_string (htab_t table, struct obstack *cpool, const char *str)
14642{
14643 void **slot;
14644 struct strtab_entry entry;
14645 struct strtab_entry *result;
14646
14647 entry.str = str;
14648 slot = htab_find_slot (table, &entry, INSERT);
14649 if (*slot)
14650 result = *slot;
14651 else
14652 {
14653 result = XNEW (struct strtab_entry);
14654 result->offset = obstack_object_size (cpool);
14655 result->str = str;
14656 obstack_grow_str0 (cpool, str);
14657 *slot = result;
14658 }
14659 return result->offset;
14660}
14661
14662/* An entry in the symbol table. */
14663struct symtab_index_entry
14664{
14665 /* The name of the symbol. */
14666 const char *name;
14667 /* The offset of the name in the constant pool. */
14668 offset_type index_offset;
14669 /* A sorted vector of the indices of all the CUs that hold an object
14670 of this name. */
14671 VEC (offset_type) *cu_indices;
14672};
14673
14674/* The symbol table. This is a power-of-2-sized hash table. */
14675struct mapped_symtab
14676{
14677 offset_type n_elements;
14678 offset_type size;
14679 struct symtab_index_entry **data;
14680};
14681
14682/* Hash function for a symtab_index_entry. */
14683static hashval_t
14684hash_symtab_entry (const void *e)
14685{
14686 const struct symtab_index_entry *entry = e;
14687 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14688 sizeof (offset_type) * VEC_length (offset_type,
14689 entry->cu_indices),
14690 0);
14691}
14692
14693/* Equality function for a symtab_index_entry. */
14694static int
14695eq_symtab_entry (const void *a, const void *b)
14696{
14697 const struct symtab_index_entry *ea = a;
14698 const struct symtab_index_entry *eb = b;
14699 int len = VEC_length (offset_type, ea->cu_indices);
14700 if (len != VEC_length (offset_type, eb->cu_indices))
14701 return 0;
14702 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14703 VEC_address (offset_type, eb->cu_indices),
14704 sizeof (offset_type) * len);
14705}
14706
14707/* Destroy a symtab_index_entry. */
14708static void
14709delete_symtab_entry (void *p)
14710{
14711 struct symtab_index_entry *entry = p;
14712 VEC_free (offset_type, entry->cu_indices);
14713 xfree (entry);
14714}
14715
14716/* Create a hash table holding symtab_index_entry objects. */
14717static htab_t
14718create_index_table (void)
14719{
14720 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14721 delete_symtab_entry, xcalloc, xfree);
14722}
14723
14724/* Create a new mapped symtab object. */
14725static struct mapped_symtab *
14726create_mapped_symtab (void)
14727{
14728 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14729 symtab->n_elements = 0;
14730 symtab->size = 1024;
14731 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14732 return symtab;
14733}
14734
14735/* Destroy a mapped_symtab. */
14736static void
14737cleanup_mapped_symtab (void *p)
14738{
14739 struct mapped_symtab *symtab = p;
14740 /* The contents of the array are freed when the other hash table is
14741 destroyed. */
14742 xfree (symtab->data);
14743 xfree (symtab);
14744}
14745
14746/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14747 the slot. */
14748static struct symtab_index_entry **
14749find_slot (struct mapped_symtab *symtab, const char *name)
14750{
14751 offset_type index, step, hash = mapped_index_string_hash (name);
14752
14753 index = hash & (symtab->size - 1);
14754 step = ((hash * 17) & (symtab->size - 1)) | 1;
14755
14756 for (;;)
14757 {
14758 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14759 return &symtab->data[index];
14760 index = (index + step) & (symtab->size - 1);
14761 }
14762}
14763
14764/* Expand SYMTAB's hash table. */
14765static void
14766hash_expand (struct mapped_symtab *symtab)
14767{
14768 offset_type old_size = symtab->size;
14769 offset_type i;
14770 struct symtab_index_entry **old_entries = symtab->data;
14771
14772 symtab->size *= 2;
14773 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14774
14775 for (i = 0; i < old_size; ++i)
14776 {
14777 if (old_entries[i])
14778 {
14779 struct symtab_index_entry **slot = find_slot (symtab,
14780 old_entries[i]->name);
14781 *slot = old_entries[i];
14782 }
14783 }
14784
14785 xfree (old_entries);
14786}
14787
14788/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14789 is the index of the CU in which the symbol appears. */
14790static void
14791add_index_entry (struct mapped_symtab *symtab, const char *name,
14792 offset_type cu_index)
14793{
14794 struct symtab_index_entry **slot;
14795
14796 ++symtab->n_elements;
14797 if (4 * symtab->n_elements / 3 >= symtab->size)
14798 hash_expand (symtab);
14799
14800 slot = find_slot (symtab, name);
14801 if (!*slot)
14802 {
14803 *slot = XNEW (struct symtab_index_entry);
14804 (*slot)->name = name;
14805 (*slot)->cu_indices = NULL;
14806 }
14807 /* Don't push an index twice. Due to how we add entries we only
14808 have to check the last one. */
14809 if (VEC_empty (offset_type, (*slot)->cu_indices)
14810 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14811 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14812}
14813
14814/* Add a vector of indices to the constant pool. */
14815static offset_type
14816add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14817 struct symtab_index_entry *entry)
14818{
14819 void **slot;
14820
14821 slot = htab_find_slot (index_table, entry, INSERT);
14822 if (!*slot)
14823 {
14824 offset_type len = VEC_length (offset_type, entry->cu_indices);
14825 offset_type val = MAYBE_SWAP (len);
14826 offset_type iter;
14827 int i;
14828
14829 *slot = entry;
14830 entry->index_offset = obstack_object_size (cpool);
14831
14832 obstack_grow (cpool, &val, sizeof (val));
14833 for (i = 0;
14834 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14835 ++i)
14836 {
14837 val = MAYBE_SWAP (iter);
14838 obstack_grow (cpool, &val, sizeof (val));
14839 }
14840 }
14841 else
14842 {
14843 struct symtab_index_entry *old_entry = *slot;
14844 entry->index_offset = old_entry->index_offset;
14845 entry = old_entry;
14846 }
14847 return entry->index_offset;
14848}
14849
14850/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14851 constant pool entries going into the obstack CPOOL. */
14852static void
14853write_hash_table (struct mapped_symtab *symtab,
14854 struct obstack *output, struct obstack *cpool)
14855{
14856 offset_type i;
14857 htab_t index_table;
14858 htab_t str_table;
14859
14860 index_table = create_index_table ();
14861 str_table = create_strtab ();
14862 /* We add all the index vectors to the constant pool first, to
14863 ensure alignment is ok. */
14864 for (i = 0; i < symtab->size; ++i)
14865 {
14866 if (symtab->data[i])
14867 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14868 }
14869
14870 /* Now write out the hash table. */
14871 for (i = 0; i < symtab->size; ++i)
14872 {
14873 offset_type str_off, vec_off;
14874
14875 if (symtab->data[i])
14876 {
14877 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14878 vec_off = symtab->data[i]->index_offset;
14879 }
14880 else
14881 {
14882 /* While 0 is a valid constant pool index, it is not valid
14883 to have 0 for both offsets. */
14884 str_off = 0;
14885 vec_off = 0;
14886 }
14887
14888 str_off = MAYBE_SWAP (str_off);
14889 vec_off = MAYBE_SWAP (vec_off);
14890
14891 obstack_grow (output, &str_off, sizeof (str_off));
14892 obstack_grow (output, &vec_off, sizeof (vec_off));
14893 }
14894
14895 htab_delete (str_table);
14896 htab_delete (index_table);
14897}
14898
14899/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14900 from PST; CU_INDEX is the index of the CU in the vector of all
14901 CUs. */
14902static void
14903add_address_entry (struct objfile *objfile,
14904 struct obstack *addr_obstack, struct partial_symtab *pst,
14905 unsigned int cu_index)
14906{
14907 offset_type offset;
14908 char addr[8];
14909 CORE_ADDR baseaddr;
14910
1fd400ff
TT
14911 /* Don't bother recording empty ranges. */
14912 if (pst->textlow == pst->texthigh)
14913 return;
14914
9291a0cd
TT
14915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14916
14917 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14918 obstack_grow (addr_obstack, addr, 8);
14919 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14920 obstack_grow (addr_obstack, addr, 8);
14921 offset = MAYBE_SWAP (cu_index);
14922 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14923}
14924
14925/* Add a list of partial symbols to SYMTAB. */
14926static void
14927write_psymbols (struct mapped_symtab *symtab,
14928 struct partial_symbol **psymp,
14929 int count,
14930 offset_type cu_index)
14931{
14932 for (; count-- > 0; ++psymp)
14933 {
14934 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14935 error (_("Ada is not currently supported by the index"));
14936 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14937 }
14938}
14939
14940/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14941 exception if there is an error. */
14942static void
14943write_obstack (FILE *file, struct obstack *obstack)
14944{
14945 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14946 file)
14947 != obstack_object_size (obstack))
14948 error (_("couldn't data write to file"));
14949}
14950
14951/* Unlink a file if the argument is not NULL. */
14952static void
14953unlink_if_set (void *p)
14954{
14955 char **filename = p;
14956 if (*filename)
14957 unlink (*filename);
14958}
14959
1fd400ff
TT
14960/* A helper struct used when iterating over debug_types. */
14961struct signatured_type_index_data
14962{
14963 struct objfile *objfile;
14964 struct mapped_symtab *symtab;
14965 struct obstack *types_list;
14966 int cu_index;
14967};
14968
14969/* A helper function that writes a single signatured_type to an
14970 obstack. */
14971static int
14972write_one_signatured_type (void **slot, void *d)
14973{
14974 struct signatured_type_index_data *info = d;
14975 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
14976 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
14977 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
14978 gdb_byte val[8];
14979
14980 write_psymbols (info->symtab,
14981 info->objfile->global_psymbols.list + psymtab->globals_offset,
14982 psymtab->n_global_syms, info->cu_index);
14983 write_psymbols (info->symtab,
14984 info->objfile->static_psymbols.list + psymtab->statics_offset,
14985 psymtab->n_static_syms, info->cu_index);
14986
14987 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14988 obstack_grow (info->types_list, val, 8);
14989 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14990 obstack_grow (info->types_list, val, 8);
14991 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14992 obstack_grow (info->types_list, val, 8);
14993
14994 ++info->cu_index;
14995
14996 return 1;
14997}
14998
9291a0cd
TT
14999/* Create an index file for OBJFILE in the directory DIR. */
15000static void
15001write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15002{
15003 struct cleanup *cleanup;
15004 char *filename, *cleanup_filename;
1fd400ff
TT
15005 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15006 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15007 int i;
15008 FILE *out_file;
15009 struct mapped_symtab *symtab;
15010 offset_type val, size_of_contents, total_len;
15011 struct stat st;
15012 char buf[8];
15013
15014 if (!objfile->psymtabs)
15015 return;
15016 if (dwarf2_per_objfile->using_index)
15017 error (_("Cannot use an index to create the index"));
15018
15019 if (stat (objfile->name, &st) < 0)
15020 perror_with_name (_("Could not stat"));
15021
15022 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15023 INDEX_SUFFIX, (char *) NULL);
15024 cleanup = make_cleanup (xfree, filename);
15025
15026 out_file = fopen (filename, "wb");
15027 if (!out_file)
15028 error (_("Can't open `%s' for writing"), filename);
15029
15030 cleanup_filename = filename;
15031 make_cleanup (unlink_if_set, &cleanup_filename);
15032
15033 symtab = create_mapped_symtab ();
15034 make_cleanup (cleanup_mapped_symtab, symtab);
15035
15036 obstack_init (&addr_obstack);
15037 make_cleanup_obstack_free (&addr_obstack);
15038
15039 obstack_init (&cu_list);
15040 make_cleanup_obstack_free (&cu_list);
15041
1fd400ff
TT
15042 obstack_init (&types_cu_list);
15043 make_cleanup_obstack_free (&types_cu_list);
15044
15045 /* The list is already sorted, so we don't need to do additional
15046 work here. Also, the debug_types entries do not appear in
15047 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15048 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15049 {
e254ef6a
DE
15050 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15051 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15052 gdb_byte val[8];
15053
15054 write_psymbols (symtab,
15055 objfile->global_psymbols.list + psymtab->globals_offset,
15056 psymtab->n_global_syms, i);
15057 write_psymbols (symtab,
15058 objfile->static_psymbols.list + psymtab->statics_offset,
15059 psymtab->n_static_syms, i);
15060
15061 add_address_entry (objfile, &addr_obstack, psymtab, i);
15062
e254ef6a 15063 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15064 obstack_grow (&cu_list, val, 8);
e254ef6a 15065 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15066 obstack_grow (&cu_list, val, 8);
15067 }
15068
1fd400ff
TT
15069 /* Write out the .debug_type entries, if any. */
15070 if (dwarf2_per_objfile->signatured_types)
15071 {
15072 struct signatured_type_index_data sig_data;
15073
15074 sig_data.objfile = objfile;
15075 sig_data.symtab = symtab;
15076 sig_data.types_list = &types_cu_list;
15077 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15078 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15079 write_one_signatured_type, &sig_data);
15080 }
15081
9291a0cd
TT
15082 obstack_init (&constant_pool);
15083 make_cleanup_obstack_free (&constant_pool);
15084 obstack_init (&symtab_obstack);
15085 make_cleanup_obstack_free (&symtab_obstack);
15086 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15087
15088 obstack_init (&contents);
15089 make_cleanup_obstack_free (&contents);
1fd400ff 15090 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15091 total_len = size_of_contents;
15092
15093 /* The version number. */
1fd400ff 15094 val = MAYBE_SWAP (2);
9291a0cd
TT
15095 obstack_grow (&contents, &val, sizeof (val));
15096
15097 /* The offset of the CU list from the start of the file. */
15098 val = MAYBE_SWAP (total_len);
15099 obstack_grow (&contents, &val, sizeof (val));
15100 total_len += obstack_object_size (&cu_list);
15101
1fd400ff
TT
15102 /* The offset of the types CU list from the start of the file. */
15103 val = MAYBE_SWAP (total_len);
15104 obstack_grow (&contents, &val, sizeof (val));
15105 total_len += obstack_object_size (&types_cu_list);
15106
9291a0cd
TT
15107 /* The offset of the address table from the start of the file. */
15108 val = MAYBE_SWAP (total_len);
15109 obstack_grow (&contents, &val, sizeof (val));
15110 total_len += obstack_object_size (&addr_obstack);
15111
15112 /* The offset of the symbol table from the start of the file. */
15113 val = MAYBE_SWAP (total_len);
15114 obstack_grow (&contents, &val, sizeof (val));
15115 total_len += obstack_object_size (&symtab_obstack);
15116
15117 /* The offset of the constant pool from the start of the file. */
15118 val = MAYBE_SWAP (total_len);
15119 obstack_grow (&contents, &val, sizeof (val));
15120 total_len += obstack_object_size (&constant_pool);
15121
15122 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15123
15124 write_obstack (out_file, &contents);
15125 write_obstack (out_file, &cu_list);
1fd400ff 15126 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15127 write_obstack (out_file, &addr_obstack);
15128 write_obstack (out_file, &symtab_obstack);
15129 write_obstack (out_file, &constant_pool);
15130
15131 fclose (out_file);
15132
15133 /* We want to keep the file, so we set cleanup_filename to NULL
15134 here. See unlink_if_set. */
15135 cleanup_filename = NULL;
15136
15137 do_cleanups (cleanup);
15138}
15139
15140/* The mapped index file format is designed to be directly mmap()able
15141 on any architecture. In most cases, a datum is represented using a
15142 little-endian 32-bit integer value, called an offset_type. Big
15143 endian machines must byte-swap the values before using them.
15144 Exceptions to this rule are noted. The data is laid out such that
15145 alignment is always respected.
15146
15147 A mapped index consists of several sections.
15148
15149 1. The file header. This is a sequence of values, of offset_type
15150 unless otherwise noted:
1fd400ff
TT
15151 [0] The version number. Currently 1 or 2. The differences are
15152 noted below. Version 1 did not account for .debug_types sections;
15153 the presence of a .debug_types section invalidates any version 1
15154 index that may exist.
9291a0cd 15155 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
15156 [1.5] In version 2, the offset, from the start of the file, of the
15157 types CU list. This offset does not appear in version 1. Note
15158 that this can be empty, in which case this offset will be equal to
15159 the next offset.
9291a0cd
TT
15160 [2] The offset, from the start of the file, of the address section.
15161 [3] The offset, from the start of the file, of the symbol table.
15162 [4] The offset, from the start of the file, of the constant pool.
15163
15164 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15165 little-endian values, sorted by the CU offset. The first element
15166 in each pair is the offset of a CU in the .debug_info section. The
15167 second element in each pair is the length of that CU. References
15168 to a CU elsewhere in the map are done using a CU index, which is
15169 just the 0-based index into this table. Note that if there are
15170 type CUs, then conceptually CUs and type CUs form a single list for
15171 the purposes of CU indices.
15172
15173 2.5 The types CU list. This does not appear in a version 1 index.
15174 This is a sequence of triplets of 64-bit little-endian values. In
15175 a triplet, the first value is the CU offset, the second value is
15176 the type offset in the CU, and the third value is the type
15177 signature. The types CU list is not sorted.
9291a0cd
TT
15178
15179 3. The address section. The address section consists of a sequence
15180 of address entries. Each address entry has three elements.
15181 [0] The low address. This is a 64-bit little-endian value.
15182 [1] The high address. This is a 64-bit little-endian value.
15183 [2] The CU index. This is an offset_type value.
15184
15185 4. The symbol table. This is a hash table. The size of the hash
15186 table is always a power of 2. The initial hash and the step are
15187 currently defined by the `find_slot' function.
15188
15189 Each slot in the hash table consists of a pair of offset_type
15190 values. The first value is the offset of the symbol's name in the
15191 constant pool. The second value is the offset of the CU vector in
15192 the constant pool.
15193
15194 If both values are 0, then this slot in the hash table is empty.
15195 This is ok because while 0 is a valid constant pool index, it
15196 cannot be a valid index for both a string and a CU vector.
15197
15198 A string in the constant pool is stored as a \0-terminated string,
15199 as you'd expect.
15200
15201 A CU vector in the constant pool is a sequence of offset_type
15202 values. The first value is the number of CU indices in the vector.
15203 Each subsequent value is the index of a CU in the CU list. This
15204 element in the hash table is used to indicate which CUs define the
15205 symbol.
15206
15207 5. The constant pool. This is simply a bunch of bytes. It is
15208 organized so that alignment is correct: CU vectors are stored
15209 first, followed by strings. */
15210static void
15211save_gdb_index_command (char *arg, int from_tty)
15212{
15213 struct objfile *objfile;
15214
15215 if (!arg || !*arg)
96d19272 15216 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15217
15218 ALL_OBJFILES (objfile)
15219 {
15220 struct stat st;
15221
15222 /* If the objfile does not correspond to an actual file, skip it. */
15223 if (stat (objfile->name, &st) < 0)
15224 continue;
15225
15226 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15227 if (dwarf2_per_objfile)
15228 {
15229 volatile struct gdb_exception except;
15230
15231 TRY_CATCH (except, RETURN_MASK_ERROR)
15232 {
15233 write_psymtabs_to_index (objfile, arg);
15234 }
15235 if (except.reason < 0)
15236 exception_fprintf (gdb_stderr, except,
15237 _("Error while writing index for `%s': "),
15238 objfile->name);
15239 }
15240 }
dce234bc
PP
15241}
15242
9291a0cd
TT
15243\f
15244
9eae7c52
TT
15245int dwarf2_always_disassemble;
15246
15247static void
15248show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15249 struct cmd_list_element *c, const char *value)
15250{
15251 fprintf_filtered (file, _("\
15252Whether to always disassemble DWARF expressions is %s.\n"),
15253 value);
15254}
15255
6502dd73
DJ
15256void _initialize_dwarf2_read (void);
15257
15258void
15259_initialize_dwarf2_read (void)
15260{
96d19272
JK
15261 struct cmd_list_element *c;
15262
dce234bc 15263 dwarf2_objfile_data_key
c1bd65d0 15264 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15265
1bedd215
AC
15266 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15267Set DWARF 2 specific variables.\n\
15268Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15269 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15270 0/*allow-unknown*/, &maintenance_set_cmdlist);
15271
1bedd215
AC
15272 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15273Show DWARF 2 specific variables\n\
15274Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15275 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15276 0/*allow-unknown*/, &maintenance_show_cmdlist);
15277
15278 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15279 &dwarf2_max_cache_age, _("\
15280Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15281Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15282A higher limit means that cached compilation units will be stored\n\
15283in memory longer, and more total memory will be used. Zero disables\n\
15284caching, which can slow down startup."),
2c5b56ce 15285 NULL,
920d2a44 15286 show_dwarf2_max_cache_age,
2c5b56ce 15287 &set_dwarf2_cmdlist,
ae038cb0 15288 &show_dwarf2_cmdlist);
d97bc12b 15289
9eae7c52
TT
15290 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15291 &dwarf2_always_disassemble, _("\
15292Set whether `info address' always disassembles DWARF expressions."), _("\
15293Show whether `info address' always disassembles DWARF expressions."), _("\
15294When enabled, DWARF expressions are always printed in an assembly-like\n\
15295syntax. When disabled, expressions will be printed in a more\n\
15296conversational style, when possible."),
15297 NULL,
15298 show_dwarf2_always_disassemble,
15299 &set_dwarf2_cmdlist,
15300 &show_dwarf2_cmdlist);
15301
d97bc12b
DE
15302 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15303Set debugging of the dwarf2 DIE reader."), _("\
15304Show debugging of the dwarf2 DIE reader."), _("\
15305When enabled (non-zero), DIEs are dumped after they are read in.\n\
15306The value is the maximum depth to print."),
15307 NULL,
15308 NULL,
15309 &setdebuglist, &showdebuglist);
9291a0cd 15310
96d19272
JK
15311 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15312 _("Save a .gdb-index file"),
15313 &save_cmdlist);
15314 set_cmd_completer (c, filename_completer);
6502dd73 15315}
This page took 2.11291 seconds and 4 git commands to generate.